blob: f5f804ce7324edb9bb590d5f4fda7e6442dd96f4 [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
Martijn Coenened323352017-07-27 23:52:24 +02001016 assert_spin_locked(&proc->inner_lock);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001017 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{
Martijn Coenened323352017-07-27 23:52:24 +02001047 assert_spin_locked(&proc->inner_lock);
Martijn Coenen053be422017-06-06 15:17:46 -07001048
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
Martijn Coenenecd972d2017-05-26 10:48:56 -07001111static void binder_do_set_priority(struct task_struct *task,
1112 struct binder_priority desired,
1113 bool verify)
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001114{
1115 int priority; /* user-space prio value */
1116 bool has_cap_nice;
1117 unsigned int policy = desired.sched_policy;
1118
1119 if (task->policy == policy && task->normal_prio == desired.prio)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001120 return;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001121
1122 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1123
1124 priority = to_userspace_prio(policy, desired.prio);
1125
Martijn Coenenecd972d2017-05-26 10:48:56 -07001126 if (verify && is_rt_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001127 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1128
1129 if (max_rtprio == 0) {
1130 policy = SCHED_NORMAL;
1131 priority = MIN_NICE;
1132 } else if (priority > max_rtprio) {
1133 priority = max_rtprio;
1134 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001135 }
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001136
Martijn Coenenecd972d2017-05-26 10:48:56 -07001137 if (verify && is_fair_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001138 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1139
1140 if (min_nice > MAX_NICE) {
1141 binder_user_error("%d RLIMIT_NICE not set\n",
1142 task->pid);
1143 return;
1144 } else if (priority < min_nice) {
1145 priority = min_nice;
1146 }
1147 }
1148
1149 if (policy != desired.sched_policy ||
1150 to_kernel_prio(policy, priority) != desired.prio)
1151 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1152 "%d: priority %d not allowed, using %d instead\n",
1153 task->pid, desired.prio,
1154 to_kernel_prio(policy, priority));
1155
1156 /* Set the actual priority */
1157 if (task->policy != policy || is_rt_policy(policy)) {
1158 struct sched_param params;
1159
1160 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1161
1162 sched_setscheduler_nocheck(task,
1163 policy | SCHED_RESET_ON_FORK,
1164 &params);
1165 }
1166 if (is_fair_policy(policy))
1167 set_user_nice(task, priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001168}
1169
Martijn Coenenecd972d2017-05-26 10:48:56 -07001170static void binder_set_priority(struct task_struct *task,
1171 struct binder_priority desired)
1172{
1173 binder_do_set_priority(task, desired, /* verify = */ true);
1174}
1175
1176static void binder_restore_priority(struct task_struct *task,
1177 struct binder_priority desired)
1178{
1179 binder_do_set_priority(task, desired, /* verify = */ false);
1180}
1181
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001182static void binder_transaction_priority(struct task_struct *task,
1183 struct binder_transaction *t,
Martijn Coenenc46810c2017-06-23 10:13:43 -07001184 struct binder_priority node_prio,
1185 bool inherit_rt)
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001186{
1187 struct binder_priority desired_prio;
1188
1189 if (t->set_priority_called)
1190 return;
1191
1192 t->set_priority_called = true;
1193 t->saved_priority.sched_policy = task->policy;
1194 t->saved_priority.prio = task->normal_prio;
1195
Martijn Coenenc46810c2017-06-23 10:13:43 -07001196 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1197 desired_prio.prio = NICE_TO_PRIO(0);
1198 desired_prio.sched_policy = SCHED_NORMAL;
1199 } else {
1200 desired_prio.prio = t->priority.prio;
1201 desired_prio.sched_policy = t->priority.sched_policy;
1202 }
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001203
1204 if (node_prio.prio < t->priority.prio ||
1205 (node_prio.prio == t->priority.prio &&
1206 node_prio.sched_policy == SCHED_FIFO)) {
1207 /*
1208 * In case the minimum priority on the node is
1209 * higher (lower value), use that priority. If
1210 * the priority is the same, but the node uses
1211 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1212 * run unbounded, unlike SCHED_RR.
1213 */
1214 desired_prio = node_prio;
1215 }
1216
1217 binder_set_priority(task, desired_prio);
1218}
1219
Todd Kjos425d23f2017-06-12 12:07:26 -07001220static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1221 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001222{
1223 struct rb_node *n = proc->nodes.rb_node;
1224 struct binder_node *node;
1225
Martijn Coenened323352017-07-27 23:52:24 +02001226 assert_spin_locked(&proc->inner_lock);
Todd Kjos425d23f2017-06-12 12:07:26 -07001227
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001228 while (n) {
1229 node = rb_entry(n, struct binder_node, rb_node);
1230
1231 if (ptr < node->ptr)
1232 n = n->rb_left;
1233 else if (ptr > node->ptr)
1234 n = n->rb_right;
Todd Kjosf22abc72017-05-09 11:08:05 -07001235 else {
1236 /*
1237 * take an implicit weak reference
1238 * to ensure node stays alive until
1239 * call to binder_put_node()
1240 */
Todd Kjos425d23f2017-06-12 12:07:26 -07001241 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001242 return node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001243 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001244 }
1245 return NULL;
1246}
1247
Todd Kjos425d23f2017-06-12 12:07:26 -07001248static struct binder_node *binder_get_node(struct binder_proc *proc,
1249 binder_uintptr_t ptr)
1250{
1251 struct binder_node *node;
1252
1253 binder_inner_proc_lock(proc);
1254 node = binder_get_node_ilocked(proc, ptr);
1255 binder_inner_proc_unlock(proc);
1256 return node;
1257}
1258
1259static struct binder_node *binder_init_node_ilocked(
1260 struct binder_proc *proc,
1261 struct binder_node *new_node,
1262 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001263{
1264 struct rb_node **p = &proc->nodes.rb_node;
1265 struct rb_node *parent = NULL;
1266 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001267 binder_uintptr_t ptr = fp ? fp->binder : 0;
1268 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1269 __u32 flags = fp ? fp->flags : 0;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001270 s8 priority;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001271
Martijn Coenened323352017-07-27 23:52:24 +02001272 assert_spin_locked(&proc->inner_lock);
1273
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001274 while (*p) {
Todd Kjos425d23f2017-06-12 12:07:26 -07001275
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001276 parent = *p;
1277 node = rb_entry(parent, struct binder_node, rb_node);
1278
1279 if (ptr < node->ptr)
1280 p = &(*p)->rb_left;
1281 else if (ptr > node->ptr)
1282 p = &(*p)->rb_right;
Todd Kjos425d23f2017-06-12 12:07:26 -07001283 else {
1284 /*
1285 * A matching node is already in
1286 * the rb tree. Abandon the init
1287 * and return it.
1288 */
1289 binder_inc_node_tmpref_ilocked(node);
1290 return node;
1291 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001292 }
Todd Kjos425d23f2017-06-12 12:07:26 -07001293 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001294 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosf22abc72017-05-09 11:08:05 -07001295 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001296 rb_link_node(&node->rb_node, parent, p);
1297 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -07001298 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001299 node->proc = proc;
1300 node->ptr = ptr;
1301 node->cookie = cookie;
1302 node->work.type = BINDER_WORK_NODE;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001303 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1304 node->sched_policy = (flags & FLAT_BINDER_FLAG_PRIORITY_MASK) >>
1305 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1306 node->min_priority = to_kernel_prio(node->sched_policy, priority);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001307 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Martijn Coenenc46810c2017-06-23 10:13:43 -07001308 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
Todd Kjosfc7a7e22017-05-29 16:44:24 -07001309 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001310 INIT_LIST_HEAD(&node->work.entry);
1311 INIT_LIST_HEAD(&node->async_todo);
1312 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001313 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001314 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001315 (u64)node->ptr, (u64)node->cookie);
Todd Kjos425d23f2017-06-12 12:07:26 -07001316
1317 return node;
1318}
1319
1320static struct binder_node *binder_new_node(struct binder_proc *proc,
1321 struct flat_binder_object *fp)
1322{
1323 struct binder_node *node;
1324 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1325
1326 if (!new_node)
1327 return NULL;
1328 binder_inner_proc_lock(proc);
1329 node = binder_init_node_ilocked(proc, new_node, fp);
1330 binder_inner_proc_unlock(proc);
1331 if (node != new_node)
1332 /*
1333 * The node was already added by another thread
1334 */
1335 kfree(new_node);
1336
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001337 return node;
1338}
1339
Todd Kjose7f23ed2017-03-21 13:06:01 -07001340static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001341{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001342 kfree(node);
1343 binder_stats_deleted(BINDER_STAT_NODE);
1344}
1345
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001346static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1347 int internal,
1348 struct list_head *target_list)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001349{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001350 struct binder_proc *proc = node->proc;
1351
Martijn Coenened323352017-07-27 23:52:24 +02001352 assert_spin_locked(&node->lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001353 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001354 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001355 if (strong) {
1356 if (internal) {
1357 if (target_list == NULL &&
1358 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001359 !(node->proc &&
1360 node == node->proc->context->
1361 binder_context_mgr_node &&
1362 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301363 pr_err("invalid inc strong node for %d\n",
1364 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001365 return -EINVAL;
1366 }
1367 node->internal_strong_refs++;
1368 } else
1369 node->local_strong_refs++;
1370 if (!node->has_strong_ref && target_list) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001371 binder_dequeue_work_ilocked(&node->work);
1372 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001373 }
1374 } else {
1375 if (!internal)
1376 node->local_weak_refs++;
1377 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1378 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301379 pr_err("invalid inc weak node for %d\n",
1380 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001381 return -EINVAL;
1382 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001383 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001384 }
1385 }
1386 return 0;
1387}
1388
Todd Kjose7f23ed2017-03-21 13:06:01 -07001389static int binder_inc_node(struct binder_node *node, int strong, int internal,
1390 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001391{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001392 int ret;
1393
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001394 binder_node_inner_lock(node);
1395 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1396 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001397
1398 return ret;
1399}
1400
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001401static bool binder_dec_node_nilocked(struct binder_node *node,
1402 int strong, int internal)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001403{
1404 struct binder_proc *proc = node->proc;
1405
Martijn Coenened323352017-07-27 23:52:24 +02001406 assert_spin_locked(&node->lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001407 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001408 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001409 if (strong) {
1410 if (internal)
1411 node->internal_strong_refs--;
1412 else
1413 node->local_strong_refs--;
1414 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001415 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001416 } else {
1417 if (!internal)
1418 node->local_weak_refs--;
Todd Kjosf22abc72017-05-09 11:08:05 -07001419 if (node->local_weak_refs || node->tmp_refs ||
1420 !hlist_empty(&node->refs))
Todd Kjose7f23ed2017-03-21 13:06:01 -07001421 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001422 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001423
1424 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001425 if (list_empty(&node->work.entry)) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001426 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07001427 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001428 }
1429 } else {
1430 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosf22abc72017-05-09 11:08:05 -07001431 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07001432 if (proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001433 binder_dequeue_work_ilocked(&node->work);
1434 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001435 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301436 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001437 node->debug_id);
1438 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001439 BUG_ON(!list_empty(&node->work.entry));
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001440 spin_lock(&binder_dead_nodes_lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001441 /*
1442 * tmp_refs could have changed so
1443 * check it again
1444 */
1445 if (node->tmp_refs) {
1446 spin_unlock(&binder_dead_nodes_lock);
1447 return false;
1448 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001449 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001450 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001451 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301452 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001453 node->debug_id);
1454 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001455 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001456 }
1457 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001458 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001459}
1460
Todd Kjose7f23ed2017-03-21 13:06:01 -07001461static void binder_dec_node(struct binder_node *node, int strong, int internal)
1462{
1463 bool free_node;
1464
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001465 binder_node_inner_lock(node);
1466 free_node = binder_dec_node_nilocked(node, strong, internal);
1467 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001468 if (free_node)
1469 binder_free_node(node);
1470}
1471
1472static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosf22abc72017-05-09 11:08:05 -07001473{
1474 /*
1475 * No call to binder_inc_node() is needed since we
1476 * don't need to inform userspace of any changes to
1477 * tmp_refs
1478 */
1479 node->tmp_refs++;
1480}
1481
1482/**
Todd Kjose7f23ed2017-03-21 13:06:01 -07001483 * binder_inc_node_tmpref() - take a temporary reference on node
1484 * @node: node to reference
1485 *
1486 * Take reference on node to prevent the node from being freed
1487 * while referenced only by a local variable. The inner lock is
1488 * needed to serialize with the node work on the queue (which
1489 * isn't needed after the node is dead). If the node is dead
1490 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1491 * node->tmp_refs against dead-node-only cases where the node
1492 * lock cannot be acquired (eg traversing the dead node list to
1493 * print nodes)
1494 */
1495static void binder_inc_node_tmpref(struct binder_node *node)
1496{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001497 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001498 if (node->proc)
1499 binder_inner_proc_lock(node->proc);
1500 else
1501 spin_lock(&binder_dead_nodes_lock);
1502 binder_inc_node_tmpref_ilocked(node);
1503 if (node->proc)
1504 binder_inner_proc_unlock(node->proc);
1505 else
1506 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001507 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001508}
1509
1510/**
Todd Kjosf22abc72017-05-09 11:08:05 -07001511 * binder_dec_node_tmpref() - remove a temporary reference on node
1512 * @node: node to reference
1513 *
1514 * Release temporary reference on node taken via binder_inc_node_tmpref()
1515 */
1516static void binder_dec_node_tmpref(struct binder_node *node)
1517{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001518 bool free_node;
1519
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001520 binder_node_inner_lock(node);
1521 if (!node->proc)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001522 spin_lock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001523 node->tmp_refs--;
1524 BUG_ON(node->tmp_refs < 0);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001525 if (!node->proc)
1526 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001527 /*
1528 * Call binder_dec_node() to check if all refcounts are 0
1529 * and cleanup is needed. Calling with strong=0 and internal=1
1530 * causes no actual reference to be released in binder_dec_node().
1531 * If that changes, a change is needed here too.
1532 */
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001533 free_node = binder_dec_node_nilocked(node, 0, 1);
1534 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001535 if (free_node)
1536 binder_free_node(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07001537}
1538
1539static void binder_put_node(struct binder_node *node)
1540{
1541 binder_dec_node_tmpref(node);
1542}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001543
Todd Kjos5346bf32016-10-20 16:43:34 -07001544static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1545 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001546{
1547 struct rb_node *n = proc->refs_by_desc.rb_node;
1548 struct binder_ref *ref;
1549
1550 while (n) {
1551 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1552
Todd Kjosb0117bb2017-05-08 09:16:27 -07001553 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001554 n = n->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001555 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001556 n = n->rb_right;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001557 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001558 binder_user_error("tried to use weak ref as strong ref\n");
1559 return NULL;
1560 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001561 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001562 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001563 }
1564 return NULL;
1565}
1566
Todd Kjosb0117bb2017-05-08 09:16:27 -07001567/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001568 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjosb0117bb2017-05-08 09:16:27 -07001569 * @proc: binder_proc that owns the ref
1570 * @node: binder_node of target
1571 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1572 *
1573 * Look up the ref for the given node and return it if it exists
1574 *
1575 * If it doesn't exist and the caller provides a newly allocated
1576 * ref, initialize the fields of the newly allocated ref and insert
1577 * into the given proc rb_trees and node refs list.
1578 *
1579 * Return: the ref for node. It is possible that another thread
1580 * allocated/initialized the ref first in which case the
1581 * returned ref would be different than the passed-in
1582 * new_ref. new_ref must be kfree'd by the caller in
1583 * this case.
1584 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001585static struct binder_ref *binder_get_ref_for_node_olocked(
1586 struct binder_proc *proc,
1587 struct binder_node *node,
1588 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001589{
Todd Kjosb0117bb2017-05-08 09:16:27 -07001590 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001591 struct rb_node **p = &proc->refs_by_node.rb_node;
1592 struct rb_node *parent = NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001593 struct binder_ref *ref;
1594 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001595
1596 while (*p) {
1597 parent = *p;
1598 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1599
1600 if (node < ref->node)
1601 p = &(*p)->rb_left;
1602 else if (node > ref->node)
1603 p = &(*p)->rb_right;
1604 else
1605 return ref;
1606 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001607 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001608 return NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001609
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001610 binder_stats_created(BINDER_STAT_REF);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001611 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001612 new_ref->proc = proc;
1613 new_ref->node = node;
1614 rb_link_node(&new_ref->rb_node_node, parent, p);
1615 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1616
Todd Kjosb0117bb2017-05-08 09:16:27 -07001617 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001618 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1619 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001620 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001621 break;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001622 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001623 }
1624
1625 p = &proc->refs_by_desc.rb_node;
1626 while (*p) {
1627 parent = *p;
1628 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1629
Todd Kjosb0117bb2017-05-08 09:16:27 -07001630 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001631 p = &(*p)->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001632 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001633 p = &(*p)->rb_right;
1634 else
1635 BUG();
1636 }
1637 rb_link_node(&new_ref->rb_node_desc, parent, p);
1638 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001639
1640 binder_node_lock(node);
Todd Kjos4cbe5752017-05-01 17:21:51 -07001641 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001642
Todd Kjos4cbe5752017-05-01 17:21:51 -07001643 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1644 "%d new ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001645 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjos4cbe5752017-05-01 17:21:51 -07001646 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001647 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001648 return new_ref;
1649}
1650
Todd Kjos5346bf32016-10-20 16:43:34 -07001651static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001652{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001653 bool delete_node = false;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001654
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001655 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301656 "%d delete ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001657 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301658 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001659
1660 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1661 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001662
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001663 binder_node_inner_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001664 if (ref->data.strong)
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001665 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001666
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001667 hlist_del(&ref->node_entry);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001668 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1669 binder_node_inner_unlock(ref->node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001670 /*
1671 * Clear ref->node unless we want the caller to free the node
1672 */
1673 if (!delete_node) {
1674 /*
1675 * The caller uses ref->node to determine
1676 * whether the node needs to be freed. Clear
1677 * it since the node is still alive.
1678 */
1679 ref->node = NULL;
1680 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001681
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001682 if (ref->death) {
1683 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301684 "%d delete ref %d desc %d has death notification\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001685 ref->proc->pid, ref->data.debug_id,
1686 ref->data.desc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001687 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001688 binder_stats_deleted(BINDER_STAT_DEATH);
1689 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001690 binder_stats_deleted(BINDER_STAT_REF);
1691}
1692
Todd Kjosb0117bb2017-05-08 09:16:27 -07001693/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001694 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjosb0117bb2017-05-08 09:16:27 -07001695 * @ref: ref to be incremented
1696 * @strong: if true, strong increment, else weak
1697 * @target_list: list to queue node work on
1698 *
Todd Kjos5346bf32016-10-20 16:43:34 -07001699 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjosb0117bb2017-05-08 09:16:27 -07001700 *
1701 * Return: 0, if successful, else errno
1702 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001703static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1704 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001705{
1706 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001707
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001708 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001709 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001710 ret = binder_inc_node(ref->node, 1, 1, target_list);
1711 if (ret)
1712 return ret;
1713 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001714 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001715 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001716 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001717 ret = binder_inc_node(ref->node, 0, 1, target_list);
1718 if (ret)
1719 return ret;
1720 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001721 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001722 }
1723 return 0;
1724}
1725
Todd Kjosb0117bb2017-05-08 09:16:27 -07001726/**
1727 * binder_dec_ref() - dec the ref for given handle
1728 * @ref: ref to be decremented
1729 * @strong: if true, strong decrement, else weak
1730 *
1731 * Decrement the ref.
1732 *
Todd Kjosb0117bb2017-05-08 09:16:27 -07001733 * Return: true if ref is cleaned up and ready to be freed
1734 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001735static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001736{
1737 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001738 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301739 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001740 ref->proc->pid, ref->data.debug_id,
1741 ref->data.desc, ref->data.strong,
1742 ref->data.weak);
1743 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001744 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001745 ref->data.strong--;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001746 if (ref->data.strong == 0)
1747 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001748 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001749 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301750 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001751 ref->proc->pid, ref->data.debug_id,
1752 ref->data.desc, ref->data.strong,
1753 ref->data.weak);
1754 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001755 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001756 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001757 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001758 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001759 binder_cleanup_ref_olocked(ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001760 return true;
1761 }
1762 return false;
1763}
1764
1765/**
1766 * binder_get_node_from_ref() - get the node from the given proc/desc
1767 * @proc: proc containing the ref
1768 * @desc: the handle associated with the ref
1769 * @need_strong_ref: if true, only return node if ref is strong
1770 * @rdata: the id/refcount data for the ref
1771 *
1772 * Given a proc and ref handle, return the associated binder_node
1773 *
1774 * Return: a binder_node or NULL if not found or not strong when strong required
1775 */
1776static struct binder_node *binder_get_node_from_ref(
1777 struct binder_proc *proc,
1778 u32 desc, bool need_strong_ref,
1779 struct binder_ref_data *rdata)
1780{
1781 struct binder_node *node;
1782 struct binder_ref *ref;
1783
Todd Kjos5346bf32016-10-20 16:43:34 -07001784 binder_proc_lock(proc);
1785 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001786 if (!ref)
1787 goto err_no_ref;
1788 node = ref->node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001789 /*
1790 * Take an implicit reference on the node to ensure
1791 * it stays alive until the call to binder_put_node()
1792 */
1793 binder_inc_node_tmpref(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001794 if (rdata)
1795 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001796 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001797
1798 return node;
1799
1800err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001801 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001802 return NULL;
1803}
1804
1805/**
1806 * binder_free_ref() - free the binder_ref
1807 * @ref: ref to free
1808 *
Todd Kjose7f23ed2017-03-21 13:06:01 -07001809 * Free the binder_ref. Free the binder_node indicated by ref->node
1810 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjosb0117bb2017-05-08 09:16:27 -07001811 */
1812static void binder_free_ref(struct binder_ref *ref)
1813{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001814 if (ref->node)
1815 binder_free_node(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001816 kfree(ref->death);
1817 kfree(ref);
1818}
1819
1820/**
1821 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1822 * @proc: proc containing the ref
1823 * @desc: the handle associated with the ref
1824 * @increment: true=inc reference, false=dec reference
1825 * @strong: true=strong reference, false=weak reference
1826 * @rdata: the id/refcount data for the ref
1827 *
1828 * Given a proc and ref handle, increment or decrement the ref
1829 * according to "increment" arg.
1830 *
1831 * Return: 0 if successful, else errno
1832 */
1833static int binder_update_ref_for_handle(struct binder_proc *proc,
1834 uint32_t desc, bool increment, bool strong,
1835 struct binder_ref_data *rdata)
1836{
1837 int ret = 0;
1838 struct binder_ref *ref;
1839 bool delete_ref = false;
1840
Todd Kjos5346bf32016-10-20 16:43:34 -07001841 binder_proc_lock(proc);
1842 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001843 if (!ref) {
1844 ret = -EINVAL;
1845 goto err_no_ref;
1846 }
1847 if (increment)
Todd Kjos5346bf32016-10-20 16:43:34 -07001848 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001849 else
Todd Kjos5346bf32016-10-20 16:43:34 -07001850 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001851
1852 if (rdata)
1853 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001854 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001855
1856 if (delete_ref)
1857 binder_free_ref(ref);
1858 return ret;
1859
1860err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001861 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001862 return ret;
1863}
1864
1865/**
1866 * binder_dec_ref_for_handle() - dec the ref for given handle
1867 * @proc: proc containing the ref
1868 * @desc: the handle associated with the ref
1869 * @strong: true=strong reference, false=weak reference
1870 * @rdata: the id/refcount data for the ref
1871 *
1872 * Just calls binder_update_ref_for_handle() to decrement the ref.
1873 *
1874 * Return: 0 if successful, else errno
1875 */
1876static int binder_dec_ref_for_handle(struct binder_proc *proc,
1877 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1878{
1879 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1880}
1881
1882
1883/**
1884 * binder_inc_ref_for_node() - increment the ref for given proc/node
1885 * @proc: proc containing the ref
1886 * @node: target node
1887 * @strong: true=strong reference, false=weak reference
1888 * @target_list: worklist to use if node is incremented
1889 * @rdata: the id/refcount data for the ref
1890 *
1891 * Given a proc and node, increment the ref. Create the ref if it
1892 * doesn't already exist
1893 *
1894 * Return: 0 if successful, else errno
1895 */
1896static int binder_inc_ref_for_node(struct binder_proc *proc,
1897 struct binder_node *node,
1898 bool strong,
1899 struct list_head *target_list,
1900 struct binder_ref_data *rdata)
1901{
1902 struct binder_ref *ref;
1903 struct binder_ref *new_ref = NULL;
1904 int ret = 0;
1905
Todd Kjos5346bf32016-10-20 16:43:34 -07001906 binder_proc_lock(proc);
1907 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001908 if (!ref) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001909 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001910 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1911 if (!new_ref)
1912 return -ENOMEM;
Todd Kjos5346bf32016-10-20 16:43:34 -07001913 binder_proc_lock(proc);
1914 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001915 }
Todd Kjos5346bf32016-10-20 16:43:34 -07001916 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001917 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001918 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001919 if (new_ref && ref != new_ref)
1920 /*
1921 * Another thread created the ref first so
1922 * free the one we allocated
1923 */
1924 kfree(new_ref);
1925 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001926}
1927
Martijn Coenen995a36e2017-06-02 13:36:52 -07001928static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1929 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001930{
Todd Kjos21ef40a2017-03-30 18:02:13 -07001931 BUG_ON(!target_thread);
Martijn Coenened323352017-07-27 23:52:24 +02001932 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjos21ef40a2017-03-30 18:02:13 -07001933 BUG_ON(target_thread->transaction_stack != t);
1934 BUG_ON(target_thread->transaction_stack->from != target_thread);
1935 target_thread->transaction_stack =
1936 target_thread->transaction_stack->from_parent;
1937 t->from = NULL;
1938}
1939
Todd Kjos2f993e22017-05-12 14:42:55 -07001940/**
1941 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1942 * @thread: thread to decrement
1943 *
1944 * A thread needs to be kept alive while being used to create or
1945 * handle a transaction. binder_get_txn_from() is used to safely
1946 * extract t->from from a binder_transaction and keep the thread
1947 * indicated by t->from from being freed. When done with that
1948 * binder_thread, this function is called to decrement the
1949 * tmp_ref and free if appropriate (thread has been released
1950 * and no transaction being processed by the driver)
1951 */
1952static void binder_thread_dec_tmpref(struct binder_thread *thread)
1953{
1954 /*
1955 * atomic is used to protect the counter value while
1956 * it cannot reach zero or thread->is_dead is false
Todd Kjos2f993e22017-05-12 14:42:55 -07001957 */
Todd Kjosb4827902017-05-25 15:52:17 -07001958 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001959 atomic_dec(&thread->tmp_ref);
1960 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07001961 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001962 binder_free_thread(thread);
1963 return;
1964 }
Todd Kjosb4827902017-05-25 15:52:17 -07001965 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001966}
1967
1968/**
1969 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1970 * @proc: proc to decrement
1971 *
1972 * A binder_proc needs to be kept alive while being used to create or
1973 * handle a transaction. proc->tmp_ref is incremented when
1974 * creating a new transaction or the binder_proc is currently in-use
1975 * by threads that are being released. When done with the binder_proc,
1976 * this function is called to decrement the counter and free the
1977 * proc if appropriate (proc has been released, all threads have
1978 * been released and not currenly in-use to process a transaction).
1979 */
1980static void binder_proc_dec_tmpref(struct binder_proc *proc)
1981{
Todd Kjosb4827902017-05-25 15:52:17 -07001982 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001983 proc->tmp_ref--;
1984 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1985 !proc->tmp_ref) {
Todd Kjosb4827902017-05-25 15:52:17 -07001986 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001987 binder_free_proc(proc);
1988 return;
1989 }
Todd Kjosb4827902017-05-25 15:52:17 -07001990 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001991}
1992
1993/**
1994 * binder_get_txn_from() - safely extract the "from" thread in transaction
1995 * @t: binder transaction for t->from
1996 *
1997 * Atomically return the "from" thread and increment the tmp_ref
1998 * count for the thread to ensure it stays alive until
1999 * binder_thread_dec_tmpref() is called.
2000 *
2001 * Return: the value of t->from
2002 */
2003static struct binder_thread *binder_get_txn_from(
2004 struct binder_transaction *t)
2005{
2006 struct binder_thread *from;
2007
2008 spin_lock(&t->lock);
2009 from = t->from;
2010 if (from)
2011 atomic_inc(&from->tmp_ref);
2012 spin_unlock(&t->lock);
2013 return from;
2014}
2015
Martijn Coenen995a36e2017-06-02 13:36:52 -07002016/**
2017 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2018 * @t: binder transaction for t->from
2019 *
2020 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2021 * to guarantee that the thread cannot be released while operating on it.
2022 * The caller must call binder_inner_proc_unlock() to release the inner lock
2023 * as well as call binder_dec_thread_txn() to release the reference.
2024 *
2025 * Return: the value of t->from
2026 */
2027static struct binder_thread *binder_get_txn_from_and_acq_inner(
2028 struct binder_transaction *t)
2029{
2030 struct binder_thread *from;
2031
2032 from = binder_get_txn_from(t);
2033 if (!from)
2034 return NULL;
2035 binder_inner_proc_lock(from->proc);
2036 if (t->from) {
2037 BUG_ON(from != t->from);
2038 return from;
2039 }
2040 binder_inner_proc_unlock(from->proc);
2041 binder_thread_dec_tmpref(from);
2042 return NULL;
2043}
2044
Todd Kjos21ef40a2017-03-30 18:02:13 -07002045static void binder_free_transaction(struct binder_transaction *t)
2046{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002047 if (t->buffer)
2048 t->buffer->transaction = NULL;
2049 kfree(t);
2050 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2051}
2052
2053static void binder_send_failed_reply(struct binder_transaction *t,
2054 uint32_t error_code)
2055{
2056 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002057 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09002058
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002059 BUG_ON(t->flags & TF_ONE_WAY);
2060 while (1) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002061 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002062 if (target_thread) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002063 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2064 "send failed reply for transaction %d to %d:%d\n",
2065 t->debug_id,
2066 target_thread->proc->pid,
2067 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002068
Martijn Coenen995a36e2017-06-02 13:36:52 -07002069 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos858b8da2017-04-21 17:35:12 -07002070 if (target_thread->reply_error.cmd == BR_OK) {
2071 target_thread->reply_error.cmd = error_code;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002072 binder_enqueue_work_ilocked(
Todd Kjos1c89e6b2016-10-20 10:33:00 -07002073 &target_thread->reply_error.work,
Todd Kjos858b8da2017-04-21 17:35:12 -07002074 &target_thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002075 wake_up_interruptible(&target_thread->wait);
2076 } else {
Todd Kjos858b8da2017-04-21 17:35:12 -07002077 WARN(1, "Unexpected reply error: %u\n",
2078 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002079 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002080 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002081 binder_thread_dec_tmpref(target_thread);
Todd Kjos858b8da2017-04-21 17:35:12 -07002082 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002083 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002084 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002085 next = t->from_parent;
2086
2087 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2088 "send failed reply for transaction %d, target dead\n",
2089 t->debug_id);
2090
Todd Kjos21ef40a2017-03-30 18:02:13 -07002091 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002092 if (next == NULL) {
2093 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2094 "reply failed, no target thread at root\n");
2095 return;
2096 }
2097 t = next;
2098 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2099 "reply failed, no target thread -- retry %d\n",
2100 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002101 }
2102}
2103
Martijn Coenen00c80372016-07-13 12:06:49 +02002104/**
2105 * binder_validate_object() - checks for a valid metadata object in a buffer.
2106 * @buffer: binder_buffer that we're parsing.
2107 * @offset: offset in the buffer at which to validate an object.
2108 *
2109 * Return: If there's a valid metadata object at @offset in @buffer, the
2110 * size of that object. Otherwise, it returns zero.
2111 */
2112static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2113{
2114 /* Check if we can read a header first */
2115 struct binder_object_header *hdr;
2116 size_t object_size = 0;
2117
2118 if (offset > buffer->data_size - sizeof(*hdr) ||
2119 buffer->data_size < sizeof(*hdr) ||
2120 !IS_ALIGNED(offset, sizeof(u32)))
2121 return 0;
2122
2123 /* Ok, now see if we can read a complete object. */
2124 hdr = (struct binder_object_header *)(buffer->data + offset);
2125 switch (hdr->type) {
2126 case BINDER_TYPE_BINDER:
2127 case BINDER_TYPE_WEAK_BINDER:
2128 case BINDER_TYPE_HANDLE:
2129 case BINDER_TYPE_WEAK_HANDLE:
2130 object_size = sizeof(struct flat_binder_object);
2131 break;
2132 case BINDER_TYPE_FD:
2133 object_size = sizeof(struct binder_fd_object);
2134 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002135 case BINDER_TYPE_PTR:
2136 object_size = sizeof(struct binder_buffer_object);
2137 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002138 case BINDER_TYPE_FDA:
2139 object_size = sizeof(struct binder_fd_array_object);
2140 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02002141 default:
2142 return 0;
2143 }
2144 if (offset <= buffer->data_size - object_size &&
2145 buffer->data_size >= object_size)
2146 return object_size;
2147 else
2148 return 0;
2149}
2150
Martijn Coenen5a6da532016-09-30 14:10:07 +02002151/**
2152 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2153 * @b: binder_buffer containing the object
2154 * @index: index in offset array at which the binder_buffer_object is
2155 * located
2156 * @start: points to the start of the offset array
2157 * @num_valid: the number of valid offsets in the offset array
2158 *
2159 * Return: If @index is within the valid range of the offset array
2160 * described by @start and @num_valid, and if there's a valid
2161 * binder_buffer_object at the offset found in index @index
2162 * of the offset array, that object is returned. Otherwise,
2163 * %NULL is returned.
2164 * Note that the offset found in index @index itself is not
2165 * verified; this function assumes that @num_valid elements
2166 * from @start were previously verified to have valid offsets.
2167 */
2168static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2169 binder_size_t index,
2170 binder_size_t *start,
2171 binder_size_t num_valid)
2172{
2173 struct binder_buffer_object *buffer_obj;
2174 binder_size_t *offp;
2175
2176 if (index >= num_valid)
2177 return NULL;
2178
2179 offp = start + index;
2180 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2181 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2182 return NULL;
2183
2184 return buffer_obj;
2185}
2186
2187/**
2188 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2189 * @b: transaction buffer
2190 * @objects_start start of objects buffer
2191 * @buffer: binder_buffer_object in which to fix up
2192 * @offset: start offset in @buffer to fix up
2193 * @last_obj: last binder_buffer_object that we fixed up in
2194 * @last_min_offset: minimum fixup offset in @last_obj
2195 *
2196 * Return: %true if a fixup in buffer @buffer at offset @offset is
2197 * allowed.
2198 *
2199 * For safety reasons, we only allow fixups inside a buffer to happen
2200 * at increasing offsets; additionally, we only allow fixup on the last
2201 * buffer object that was verified, or one of its parents.
2202 *
2203 * Example of what is allowed:
2204 *
2205 * A
2206 * B (parent = A, offset = 0)
2207 * C (parent = A, offset = 16)
2208 * D (parent = C, offset = 0)
2209 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2210 *
2211 * Examples of what is not allowed:
2212 *
2213 * Decreasing offsets within the same parent:
2214 * A
2215 * C (parent = A, offset = 16)
2216 * B (parent = A, offset = 0) // decreasing offset within A
2217 *
2218 * Referring to a parent that wasn't the last object or any of its parents:
2219 * A
2220 * B (parent = A, offset = 0)
2221 * C (parent = A, offset = 0)
2222 * C (parent = A, offset = 16)
2223 * D (parent = B, offset = 0) // B is not A or any of A's parents
2224 */
2225static bool binder_validate_fixup(struct binder_buffer *b,
2226 binder_size_t *objects_start,
2227 struct binder_buffer_object *buffer,
2228 binder_size_t fixup_offset,
2229 struct binder_buffer_object *last_obj,
2230 binder_size_t last_min_offset)
2231{
2232 if (!last_obj) {
2233 /* Nothing to fix up in */
2234 return false;
2235 }
2236
2237 while (last_obj != buffer) {
2238 /*
2239 * Safe to retrieve the parent of last_obj, since it
2240 * was already previously verified by the driver.
2241 */
2242 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2243 return false;
2244 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2245 last_obj = (struct binder_buffer_object *)
2246 (b->data + *(objects_start + last_obj->parent));
2247 }
2248 return (fixup_offset >= last_min_offset);
2249}
2250
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002251static void binder_transaction_buffer_release(struct binder_proc *proc,
2252 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002253 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002254{
Martijn Coenen5a6da532016-09-30 14:10:07 +02002255 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002256 int debug_id = buffer->debug_id;
2257
2258 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302259 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002260 proc->pid, buffer->debug_id,
2261 buffer->data_size, buffer->offsets_size, failed_at);
2262
2263 if (buffer->target_node)
2264 binder_dec_node(buffer->target_node, 1, 0);
2265
Martijn Coenen5a6da532016-09-30 14:10:07 +02002266 off_start = (binder_size_t *)(buffer->data +
2267 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002268 if (failed_at)
2269 off_end = failed_at;
2270 else
Martijn Coenen5a6da532016-09-30 14:10:07 +02002271 off_end = (void *)off_start + buffer->offsets_size;
2272 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002273 struct binder_object_header *hdr;
2274 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002275
Martijn Coenen00c80372016-07-13 12:06:49 +02002276 if (object_size == 0) {
2277 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002278 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002279 continue;
2280 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002281 hdr = (struct binder_object_header *)(buffer->data + *offp);
2282 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002283 case BINDER_TYPE_BINDER:
2284 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002285 struct flat_binder_object *fp;
2286 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002287
Martijn Coenen00c80372016-07-13 12:06:49 +02002288 fp = to_flat_binder_object(hdr);
2289 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002290 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002291 pr_err("transaction release %d bad node %016llx\n",
2292 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002293 break;
2294 }
2295 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002296 " node %d u%016llx\n",
2297 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002298 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2299 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002300 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002301 } break;
2302 case BINDER_TYPE_HANDLE:
2303 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002304 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002305 struct binder_ref_data rdata;
2306 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002307
Martijn Coenen00c80372016-07-13 12:06:49 +02002308 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002309 ret = binder_dec_ref_for_handle(proc, fp->handle,
2310 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2311
2312 if (ret) {
2313 pr_err("transaction release %d bad handle %d, ret = %d\n",
2314 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002315 break;
2316 }
2317 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002318 " ref %d desc %d\n",
2319 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002320 } break;
2321
Martijn Coenen00c80372016-07-13 12:06:49 +02002322 case BINDER_TYPE_FD: {
2323 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2324
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002325 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002326 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002327 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002328 task_close_fd(proc, fp->fd);
2329 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002330 case BINDER_TYPE_PTR:
2331 /*
2332 * Nothing to do here, this will get cleaned up when the
2333 * transaction buffer gets freed
2334 */
2335 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002336 case BINDER_TYPE_FDA: {
2337 struct binder_fd_array_object *fda;
2338 struct binder_buffer_object *parent;
2339 uintptr_t parent_buffer;
2340 u32 *fd_array;
2341 size_t fd_index;
2342 binder_size_t fd_buf_size;
2343
2344 fda = to_binder_fd_array_object(hdr);
2345 parent = binder_validate_ptr(buffer, fda->parent,
2346 off_start,
2347 offp - off_start);
2348 if (!parent) {
2349 pr_err("transaction release %d bad parent offset",
2350 debug_id);
2351 continue;
2352 }
2353 /*
2354 * Since the parent was already fixed up, convert it
2355 * back to kernel address space to access it
2356 */
2357 parent_buffer = parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002358 binder_alloc_get_user_buffer_offset(
2359 &proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002360
2361 fd_buf_size = sizeof(u32) * fda->num_fds;
2362 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2363 pr_err("transaction release %d invalid number of fds (%lld)\n",
2364 debug_id, (u64)fda->num_fds);
2365 continue;
2366 }
2367 if (fd_buf_size > parent->length ||
2368 fda->parent_offset > parent->length - fd_buf_size) {
2369 /* No space for all file descriptors here. */
2370 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2371 debug_id, (u64)fda->num_fds);
2372 continue;
2373 }
2374 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2375 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2376 task_close_fd(proc, fd_array[fd_index]);
2377 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002378 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002379 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002380 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002381 break;
2382 }
2383 }
2384}
2385
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002386static int binder_translate_binder(struct flat_binder_object *fp,
2387 struct binder_transaction *t,
2388 struct binder_thread *thread)
2389{
2390 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002391 struct binder_proc *proc = thread->proc;
2392 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002393 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002394 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002395
2396 node = binder_get_node(proc, fp->binder);
2397 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002398 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002399 if (!node)
2400 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002401 }
2402 if (fp->cookie != node->cookie) {
2403 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2404 proc->pid, thread->pid, (u64)fp->binder,
2405 node->debug_id, (u64)fp->cookie,
2406 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002407 ret = -EINVAL;
2408 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002409 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002410 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2411 ret = -EPERM;
2412 goto done;
2413 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002414
Todd Kjosb0117bb2017-05-08 09:16:27 -07002415 ret = binder_inc_ref_for_node(target_proc, node,
2416 fp->hdr.type == BINDER_TYPE_BINDER,
2417 &thread->todo, &rdata);
2418 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002419 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002420
2421 if (fp->hdr.type == BINDER_TYPE_BINDER)
2422 fp->hdr.type = BINDER_TYPE_HANDLE;
2423 else
2424 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2425 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002426 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002427 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002428
Todd Kjosb0117bb2017-05-08 09:16:27 -07002429 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002430 binder_debug(BINDER_DEBUG_TRANSACTION,
2431 " node %d u%016llx -> ref %d desc %d\n",
2432 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002433 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002434done:
2435 binder_put_node(node);
2436 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002437}
2438
2439static int binder_translate_handle(struct flat_binder_object *fp,
2440 struct binder_transaction *t,
2441 struct binder_thread *thread)
2442{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002443 struct binder_proc *proc = thread->proc;
2444 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002445 struct binder_node *node;
2446 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002447 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002448
Todd Kjosb0117bb2017-05-08 09:16:27 -07002449 node = binder_get_node_from_ref(proc, fp->handle,
2450 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2451 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002452 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2453 proc->pid, thread->pid, fp->handle);
2454 return -EINVAL;
2455 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002456 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2457 ret = -EPERM;
2458 goto done;
2459 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002460
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002461 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002462 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002463 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2464 fp->hdr.type = BINDER_TYPE_BINDER;
2465 else
2466 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002467 fp->binder = node->ptr;
2468 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002469 if (node->proc)
2470 binder_inner_proc_lock(node->proc);
2471 binder_inc_node_nilocked(node,
2472 fp->hdr.type == BINDER_TYPE_BINDER,
2473 0, NULL);
2474 if (node->proc)
2475 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002476 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002477 binder_debug(BINDER_DEBUG_TRANSACTION,
2478 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002479 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2480 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002481 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002482 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002483 int ret;
2484 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002485
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002486 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002487 ret = binder_inc_ref_for_node(target_proc, node,
2488 fp->hdr.type == BINDER_TYPE_HANDLE,
2489 NULL, &dest_rdata);
2490 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002491 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002492
2493 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002494 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002495 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002496 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2497 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002498 binder_debug(BINDER_DEBUG_TRANSACTION,
2499 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002500 src_rdata.debug_id, src_rdata.desc,
2501 dest_rdata.debug_id, dest_rdata.desc,
2502 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002503 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002504done:
2505 binder_put_node(node);
2506 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002507}
2508
2509static int binder_translate_fd(int fd,
2510 struct binder_transaction *t,
2511 struct binder_thread *thread,
2512 struct binder_transaction *in_reply_to)
2513{
2514 struct binder_proc *proc = thread->proc;
2515 struct binder_proc *target_proc = t->to_proc;
2516 int target_fd;
2517 struct file *file;
2518 int ret;
2519 bool target_allows_fd;
2520
2521 if (in_reply_to)
2522 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2523 else
2524 target_allows_fd = t->buffer->target_node->accept_fds;
2525 if (!target_allows_fd) {
2526 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2527 proc->pid, thread->pid,
2528 in_reply_to ? "reply" : "transaction",
2529 fd);
2530 ret = -EPERM;
2531 goto err_fd_not_accepted;
2532 }
2533
2534 file = fget(fd);
2535 if (!file) {
2536 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2537 proc->pid, thread->pid, fd);
2538 ret = -EBADF;
2539 goto err_fget;
2540 }
2541 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2542 if (ret < 0) {
2543 ret = -EPERM;
2544 goto err_security;
2545 }
2546
2547 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2548 if (target_fd < 0) {
2549 ret = -ENOMEM;
2550 goto err_get_unused_fd;
2551 }
2552 task_fd_install(target_proc, target_fd, file);
2553 trace_binder_transaction_fd(t, fd, target_fd);
2554 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2555 fd, target_fd);
2556
2557 return target_fd;
2558
2559err_get_unused_fd:
2560err_security:
2561 fput(file);
2562err_fget:
2563err_fd_not_accepted:
2564 return ret;
2565}
2566
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002567static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2568 struct binder_buffer_object *parent,
2569 struct binder_transaction *t,
2570 struct binder_thread *thread,
2571 struct binder_transaction *in_reply_to)
2572{
2573 binder_size_t fdi, fd_buf_size, num_installed_fds;
2574 int target_fd;
2575 uintptr_t parent_buffer;
2576 u32 *fd_array;
2577 struct binder_proc *proc = thread->proc;
2578 struct binder_proc *target_proc = t->to_proc;
2579
2580 fd_buf_size = sizeof(u32) * fda->num_fds;
2581 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2582 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2583 proc->pid, thread->pid, (u64)fda->num_fds);
2584 return -EINVAL;
2585 }
2586 if (fd_buf_size > parent->length ||
2587 fda->parent_offset > parent->length - fd_buf_size) {
2588 /* No space for all file descriptors here. */
2589 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2590 proc->pid, thread->pid, (u64)fda->num_fds);
2591 return -EINVAL;
2592 }
2593 /*
2594 * Since the parent was already fixed up, convert it
2595 * back to the kernel address space to access it
2596 */
Todd Kjosd325d372016-10-10 10:40:53 -07002597 parent_buffer = parent->buffer -
2598 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002599 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2600 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2601 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2602 proc->pid, thread->pid);
2603 return -EINVAL;
2604 }
2605 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2606 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2607 in_reply_to);
2608 if (target_fd < 0)
2609 goto err_translate_fd_failed;
2610 fd_array[fdi] = target_fd;
2611 }
2612 return 0;
2613
2614err_translate_fd_failed:
2615 /*
2616 * Failed to allocate fd or security error, free fds
2617 * installed so far.
2618 */
2619 num_installed_fds = fdi;
2620 for (fdi = 0; fdi < num_installed_fds; fdi++)
2621 task_close_fd(target_proc, fd_array[fdi]);
2622 return target_fd;
2623}
2624
Martijn Coenen5a6da532016-09-30 14:10:07 +02002625static int binder_fixup_parent(struct binder_transaction *t,
2626 struct binder_thread *thread,
2627 struct binder_buffer_object *bp,
2628 binder_size_t *off_start,
2629 binder_size_t num_valid,
2630 struct binder_buffer_object *last_fixup_obj,
2631 binder_size_t last_fixup_min_off)
2632{
2633 struct binder_buffer_object *parent;
2634 u8 *parent_buffer;
2635 struct binder_buffer *b = t->buffer;
2636 struct binder_proc *proc = thread->proc;
2637 struct binder_proc *target_proc = t->to_proc;
2638
2639 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2640 return 0;
2641
2642 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2643 if (!parent) {
2644 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2645 proc->pid, thread->pid);
2646 return -EINVAL;
2647 }
2648
2649 if (!binder_validate_fixup(b, off_start,
2650 parent, bp->parent_offset,
2651 last_fixup_obj,
2652 last_fixup_min_off)) {
2653 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2654 proc->pid, thread->pid);
2655 return -EINVAL;
2656 }
2657
2658 if (parent->length < sizeof(binder_uintptr_t) ||
2659 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2660 /* No space for a pointer here! */
2661 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2662 proc->pid, thread->pid);
2663 return -EINVAL;
2664 }
2665 parent_buffer = (u8 *)(parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002666 binder_alloc_get_user_buffer_offset(
2667 &target_proc->alloc));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002668 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2669
2670 return 0;
2671}
2672
Martijn Coenen053be422017-06-06 15:17:46 -07002673/**
2674 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2675 * @t: transaction to send
2676 * @proc: process to send the transaction to
2677 * @thread: thread in @proc to send the transaction to (may be NULL)
2678 *
2679 * This function queues a transaction to the specified process. It will try
2680 * to find a thread in the target process to handle the transaction and
2681 * wake it up. If no thread is found, the work is queued to the proc
2682 * waitqueue.
2683 *
2684 * If the @thread parameter is not NULL, the transaction is always queued
2685 * to the waitlist of that specific thread.
2686 *
2687 * Return: true if the transactions was successfully queued
2688 * false if the target process or thread is dead
2689 */
2690static bool binder_proc_transaction(struct binder_transaction *t,
2691 struct binder_proc *proc,
2692 struct binder_thread *thread)
2693{
2694 struct list_head *target_list = NULL;
2695 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002696 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002697 bool oneway = !!(t->flags & TF_ONE_WAY);
2698 bool wakeup = true;
2699
2700 BUG_ON(!node);
2701 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002702 node_prio.prio = node->min_priority;
2703 node_prio.sched_policy = node->sched_policy;
2704
Martijn Coenen053be422017-06-06 15:17:46 -07002705 if (oneway) {
2706 BUG_ON(thread);
2707 if (node->has_async_transaction) {
2708 target_list = &node->async_todo;
2709 wakeup = false;
2710 } else {
2711 node->has_async_transaction = 1;
2712 }
2713 }
2714
2715 binder_inner_proc_lock(proc);
2716
2717 if (proc->is_dead || (thread && thread->is_dead)) {
2718 binder_inner_proc_unlock(proc);
2719 binder_node_unlock(node);
2720 return false;
2721 }
2722
2723 if (!thread && !target_list)
2724 thread = binder_select_thread_ilocked(proc);
2725
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002726 if (thread) {
Martijn Coenen053be422017-06-06 15:17:46 -07002727 target_list = &thread->todo;
Martijn Coenenc46810c2017-06-23 10:13:43 -07002728 binder_transaction_priority(thread->task, t, node_prio,
2729 node->inherit_rt);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002730 } else if (!target_list) {
Martijn Coenen053be422017-06-06 15:17:46 -07002731 target_list = &proc->todo;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002732 } else {
Martijn Coenen053be422017-06-06 15:17:46 -07002733 BUG_ON(target_list != &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002734 }
Martijn Coenen053be422017-06-06 15:17:46 -07002735
2736 binder_enqueue_work_ilocked(&t->work, target_list);
2737
2738 if (wakeup)
2739 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2740
2741 binder_inner_proc_unlock(proc);
2742 binder_node_unlock(node);
2743
2744 return true;
2745}
2746
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002747static void binder_transaction(struct binder_proc *proc,
2748 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02002749 struct binder_transaction_data *tr, int reply,
2750 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002751{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002752 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002753 struct binder_transaction *t;
2754 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002755 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002756 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002757 u8 *sg_bufp, *sg_buf_end;
Todd Kjos2f993e22017-05-12 14:42:55 -07002758 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002759 struct binder_thread *target_thread = NULL;
2760 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002761 struct binder_transaction *in_reply_to = NULL;
2762 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07002763 uint32_t return_error = 0;
2764 uint32_t return_error_param = 0;
2765 uint32_t return_error_line = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002766 struct binder_buffer_object *last_fixup_obj = NULL;
2767 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002768 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07002769 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002770
2771 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07002772 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002773 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2774 e->from_proc = proc->pid;
2775 e->from_thread = thread->pid;
2776 e->target_handle = tr->target.handle;
2777 e->data_size = tr->data_size;
2778 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02002779 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002780
2781 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002782 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002783 in_reply_to = thread->transaction_stack;
2784 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002785 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302786 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002787 proc->pid, thread->pid);
2788 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002789 return_error_param = -EPROTO;
2790 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002791 goto err_empty_call_stack;
2792 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002793 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002794 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302795 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 +09002796 proc->pid, thread->pid, in_reply_to->debug_id,
2797 in_reply_to->to_proc ?
2798 in_reply_to->to_proc->pid : 0,
2799 in_reply_to->to_thread ?
2800 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002801 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002802 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002803 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002804 return_error_param = -EPROTO;
2805 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002806 in_reply_to = NULL;
2807 goto err_bad_call_stack;
2808 }
2809 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002810 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002811 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002812 if (target_thread == NULL) {
2813 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002814 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002815 goto err_dead_binder;
2816 }
2817 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302818 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 +09002819 proc->pid, thread->pid,
2820 target_thread->transaction_stack ?
2821 target_thread->transaction_stack->debug_id : 0,
2822 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002823 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002824 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002825 return_error_param = -EPROTO;
2826 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002827 in_reply_to = NULL;
2828 target_thread = NULL;
2829 goto err_dead_binder;
2830 }
2831 target_proc = target_thread->proc;
Todd Kjos2f993e22017-05-12 14:42:55 -07002832 target_proc->tmp_ref++;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002833 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002834 } else {
2835 if (tr->target.handle) {
2836 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002837
Todd Kjosc37162d2017-05-26 11:56:29 -07002838 /*
2839 * There must already be a strong ref
2840 * on this node. If so, do a strong
2841 * increment on the node to ensure it
2842 * stays alive until the transaction is
2843 * done.
2844 */
Todd Kjos5346bf32016-10-20 16:43:34 -07002845 binder_proc_lock(proc);
2846 ref = binder_get_ref_olocked(proc, tr->target.handle,
2847 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07002848 if (ref) {
2849 binder_inc_node(ref->node, 1, 0, NULL);
2850 target_node = ref->node;
2851 }
Todd Kjos5346bf32016-10-20 16:43:34 -07002852 binder_proc_unlock(proc);
Todd Kjosc37162d2017-05-26 11:56:29 -07002853 if (target_node == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302854 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002855 proc->pid, thread->pid);
2856 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002857 return_error_param = -EINVAL;
2858 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002859 goto err_invalid_target_handle;
2860 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002861 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002862 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002863 target_node = context->binder_context_mgr_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002864 if (target_node == NULL) {
2865 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002866 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjose598d172017-03-22 17:19:52 -07002867 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002868 goto err_no_context_mgr_node;
2869 }
Todd Kjosc37162d2017-05-26 11:56:29 -07002870 binder_inc_node(target_node, 1, 0, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002871 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002872 }
2873 e->to_node = target_node->debug_id;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002874 binder_node_lock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002875 target_proc = target_node->proc;
2876 if (target_proc == NULL) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002877 binder_node_unlock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002878 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002879 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002880 goto err_dead_binder;
2881 }
Todd Kjosb4827902017-05-25 15:52:17 -07002882 binder_inner_proc_lock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002883 target_proc->tmp_ref++;
Todd Kjosb4827902017-05-25 15:52:17 -07002884 binder_inner_proc_unlock(target_proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002885 binder_node_unlock(target_node);
Stephen Smalley79af7302015-01-21 10:54:10 -05002886 if (security_binder_transaction(proc->tsk,
2887 target_proc->tsk) < 0) {
2888 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002889 return_error_param = -EPERM;
2890 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05002891 goto err_invalid_target_handle;
2892 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002893 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002894 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2895 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002896
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002897 tmp = thread->transaction_stack;
2898 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002899 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302900 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 +09002901 proc->pid, thread->pid, tmp->debug_id,
2902 tmp->to_proc ? tmp->to_proc->pid : 0,
2903 tmp->to_thread ?
2904 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002905 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002906 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002907 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002908 return_error_param = -EPROTO;
2909 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002910 goto err_bad_call_stack;
2911 }
2912 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002913 struct binder_thread *from;
2914
2915 spin_lock(&tmp->lock);
2916 from = tmp->from;
2917 if (from && from->proc == target_proc) {
2918 atomic_inc(&from->tmp_ref);
2919 target_thread = from;
2920 spin_unlock(&tmp->lock);
2921 break;
2922 }
2923 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002924 tmp = tmp->from_parent;
2925 }
2926 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002927 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002928 }
Martijn Coenen053be422017-06-06 15:17:46 -07002929 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002930 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002931 e->to_proc = target_proc->pid;
2932
2933 /* TODO: reuse incoming transaction for reply */
2934 t = kzalloc(sizeof(*t), GFP_KERNEL);
2935 if (t == NULL) {
2936 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002937 return_error_param = -ENOMEM;
2938 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002939 goto err_alloc_t_failed;
2940 }
2941 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07002942 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002943
2944 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2945 if (tcomplete == NULL) {
2946 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002947 return_error_param = -ENOMEM;
2948 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002949 goto err_alloc_tcomplete_failed;
2950 }
2951 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2952
Todd Kjos1cfe6272017-05-24 13:33:28 -07002953 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002954
2955 if (reply)
2956 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02002957 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002958 proc->pid, thread->pid, t->debug_id,
2959 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002960 (u64)tr->data.ptr.buffer,
2961 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02002962 (u64)tr->data_size, (u64)tr->offsets_size,
2963 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002964 else
2965 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02002966 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002967 proc->pid, thread->pid, t->debug_id,
2968 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002969 (u64)tr->data.ptr.buffer,
2970 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02002971 (u64)tr->data_size, (u64)tr->offsets_size,
2972 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002973
2974 if (!reply && !(tr->flags & TF_ONE_WAY))
2975 t->from = thread;
2976 else
2977 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03002978 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002979 t->to_proc = target_proc;
2980 t->to_thread = target_thread;
2981 t->code = tr->code;
2982 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07002983 if (!(t->flags & TF_ONE_WAY) &&
2984 binder_supported_policy(current->policy)) {
2985 /* Inherit supported policies for synchronous transactions */
2986 t->priority.sched_policy = current->policy;
2987 t->priority.prio = current->normal_prio;
2988 } else {
2989 /* Otherwise, fall back to the default priority */
2990 t->priority = target_proc->default_priority;
2991 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002992
2993 trace_binder_transaction(reply, t, target_node);
2994
Todd Kjosd325d372016-10-10 10:40:53 -07002995 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02002996 tr->offsets_size, extra_buffers_size,
2997 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07002998 if (IS_ERR(t->buffer)) {
2999 /*
3000 * -ESRCH indicates VMA cleared. The target is dying.
3001 */
3002 return_error_param = PTR_ERR(t->buffer);
3003 return_error = return_error_param == -ESRCH ?
3004 BR_DEAD_REPLY : BR_FAILED_REPLY;
3005 return_error_line = __LINE__;
3006 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003007 goto err_binder_alloc_buf_failed;
3008 }
3009 t->buffer->allow_user_free = 0;
3010 t->buffer->debug_id = t->debug_id;
3011 t->buffer->transaction = t;
3012 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003013 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003014 off_start = (binder_size_t *)(t->buffer->data +
3015 ALIGN(tr->data_size, sizeof(void *)));
3016 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003017
Arve Hjønnevågda498892014-02-21 14:40:26 -08003018 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3019 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303020 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3021 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003022 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003023 return_error_param = -EFAULT;
3024 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003025 goto err_copy_data_failed;
3026 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003027 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3028 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303029 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3030 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003031 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003032 return_error_param = -EFAULT;
3033 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003034 goto err_copy_data_failed;
3035 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003036 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3037 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3038 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003039 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003040 return_error_param = -EINVAL;
3041 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003042 goto err_bad_offset;
3043 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003044 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3045 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3046 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303047 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003048 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003049 return_error_param = -EINVAL;
3050 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003051 goto err_bad_offset;
3052 }
3053 off_end = (void *)off_start + tr->offsets_size;
3054 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3055 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003056 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003057 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003058 struct binder_object_header *hdr;
3059 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09003060
Martijn Coenen00c80372016-07-13 12:06:49 +02003061 if (object_size == 0 || *offp < off_min) {
3062 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 -08003063 proc->pid, thread->pid, (u64)*offp,
3064 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003065 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003066 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003067 return_error_param = -EINVAL;
3068 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003069 goto err_bad_offset;
3070 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003071
3072 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3073 off_min = *offp + object_size;
3074 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003075 case BINDER_TYPE_BINDER:
3076 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003077 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003078
Martijn Coenen00c80372016-07-13 12:06:49 +02003079 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003080 ret = binder_translate_binder(fp, t, thread);
3081 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003082 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003083 return_error_param = ret;
3084 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003085 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003086 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003087 } break;
3088 case BINDER_TYPE_HANDLE:
3089 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003090 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003091
Martijn Coenen00c80372016-07-13 12:06:49 +02003092 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003093 ret = binder_translate_handle(fp, t, thread);
3094 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003095 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003096 return_error_param = ret;
3097 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003098 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003099 }
3100 } break;
3101
3102 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003103 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003104 int target_fd = binder_translate_fd(fp->fd, t, thread,
3105 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003106
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003107 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003108 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003109 return_error_param = target_fd;
3110 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003111 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003112 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003113 fp->pad_binder = 0;
3114 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003115 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003116 case BINDER_TYPE_FDA: {
3117 struct binder_fd_array_object *fda =
3118 to_binder_fd_array_object(hdr);
3119 struct binder_buffer_object *parent =
3120 binder_validate_ptr(t->buffer, fda->parent,
3121 off_start,
3122 offp - off_start);
3123 if (!parent) {
3124 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3125 proc->pid, thread->pid);
3126 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003127 return_error_param = -EINVAL;
3128 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003129 goto err_bad_parent;
3130 }
3131 if (!binder_validate_fixup(t->buffer, off_start,
3132 parent, fda->parent_offset,
3133 last_fixup_obj,
3134 last_fixup_min_off)) {
3135 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3136 proc->pid, thread->pid);
3137 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003138 return_error_param = -EINVAL;
3139 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003140 goto err_bad_parent;
3141 }
3142 ret = binder_translate_fd_array(fda, parent, t, thread,
3143 in_reply_to);
3144 if (ret < 0) {
3145 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003146 return_error_param = ret;
3147 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003148 goto err_translate_failed;
3149 }
3150 last_fixup_obj = parent;
3151 last_fixup_min_off =
3152 fda->parent_offset + sizeof(u32) * fda->num_fds;
3153 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003154 case BINDER_TYPE_PTR: {
3155 struct binder_buffer_object *bp =
3156 to_binder_buffer_object(hdr);
3157 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003158
Martijn Coenen5a6da532016-09-30 14:10:07 +02003159 if (bp->length > buf_left) {
3160 binder_user_error("%d:%d got transaction with too large buffer\n",
3161 proc->pid, thread->pid);
3162 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003163 return_error_param = -EINVAL;
3164 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003165 goto err_bad_offset;
3166 }
3167 if (copy_from_user(sg_bufp,
3168 (const void __user *)(uintptr_t)
3169 bp->buffer, bp->length)) {
3170 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3171 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003172 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003173 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003174 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003175 goto err_copy_data_failed;
3176 }
3177 /* Fixup buffer pointer to target proc address space */
3178 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjosd325d372016-10-10 10:40:53 -07003179 binder_alloc_get_user_buffer_offset(
3180 &target_proc->alloc);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003181 sg_bufp += ALIGN(bp->length, sizeof(u64));
3182
3183 ret = binder_fixup_parent(t, thread, bp, off_start,
3184 offp - off_start,
3185 last_fixup_obj,
3186 last_fixup_min_off);
3187 if (ret < 0) {
3188 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003189 return_error_param = ret;
3190 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003191 goto err_translate_failed;
3192 }
3193 last_fixup_obj = bp;
3194 last_fixup_min_off = 0;
3195 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003196 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003197 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003198 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003199 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003200 return_error_param = -EINVAL;
3201 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003202 goto err_bad_object_type;
3203 }
3204 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003205 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003206 binder_enqueue_work(proc, tcomplete, &thread->todo);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003207 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003208
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003209 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003210 binder_inner_proc_lock(target_proc);
3211 if (target_thread->is_dead) {
3212 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003213 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003214 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003215 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003216 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen053be422017-06-06 15:17:46 -07003217 binder_enqueue_work_ilocked(&t->work, &target_thread->todo);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003218 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003219 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003220 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003221 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003222 } else if (!(t->flags & TF_ONE_WAY)) {
3223 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003224 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003225 t->need_reply = 1;
3226 t->from_parent = thread->transaction_stack;
3227 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003228 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003229 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003230 binder_inner_proc_lock(proc);
3231 binder_pop_transaction_ilocked(thread, t);
3232 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003233 goto err_dead_proc_or_thread;
3234 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003235 } else {
3236 BUG_ON(target_node == NULL);
3237 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen053be422017-06-06 15:17:46 -07003238 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003239 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003240 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003241 if (target_thread)
3242 binder_thread_dec_tmpref(target_thread);
3243 binder_proc_dec_tmpref(target_proc);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003244 /*
3245 * write barrier to synchronize with initialization
3246 * of log entry
3247 */
3248 smp_wmb();
3249 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003250 return;
3251
Todd Kjos2f993e22017-05-12 14:42:55 -07003252err_dead_proc_or_thread:
3253 return_error = BR_DEAD_REPLY;
3254 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003255err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003256err_bad_object_type:
3257err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003258err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003259err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003260 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003261 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjosc37162d2017-05-26 11:56:29 -07003262 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003263 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003264 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003265err_binder_alloc_buf_failed:
3266 kfree(tcomplete);
3267 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3268err_alloc_tcomplete_failed:
3269 kfree(t);
3270 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3271err_alloc_t_failed:
3272err_bad_call_stack:
3273err_empty_call_stack:
3274err_dead_binder:
3275err_invalid_target_handle:
3276err_no_context_mgr_node:
Todd Kjos2f993e22017-05-12 14:42:55 -07003277 if (target_thread)
3278 binder_thread_dec_tmpref(target_thread);
3279 if (target_proc)
3280 binder_proc_dec_tmpref(target_proc);
Todd Kjosc37162d2017-05-26 11:56:29 -07003281 if (target_node)
3282 binder_dec_node(target_node, 1, 0);
3283
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003284 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003285 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3286 proc->pid, thread->pid, return_error, return_error_param,
3287 (u64)tr->data_size, (u64)tr->offsets_size,
3288 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003289
3290 {
3291 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003292
Todd Kjose598d172017-03-22 17:19:52 -07003293 e->return_error = return_error;
3294 e->return_error_param = return_error_param;
3295 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003296 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3297 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003298 /*
3299 * write barrier to synchronize with initialization
3300 * of log entry
3301 */
3302 smp_wmb();
3303 WRITE_ONCE(e->debug_id_done, t_debug_id);
3304 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003305 }
3306
Todd Kjos858b8da2017-04-21 17:35:12 -07003307 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003308 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003309 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003310 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003311 binder_enqueue_work(thread->proc,
3312 &thread->return_error.work,
3313 &thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003314 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003315 } else {
3316 thread->return_error.cmd = return_error;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003317 binder_enqueue_work(thread->proc,
3318 &thread->return_error.work,
3319 &thread->todo);
Todd Kjos858b8da2017-04-21 17:35:12 -07003320 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003321}
3322
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003323static int binder_thread_write(struct binder_proc *proc,
3324 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003325 binder_uintptr_t binder_buffer, size_t size,
3326 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003327{
3328 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003329 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003330 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003331 void __user *ptr = buffer + *consumed;
3332 void __user *end = buffer + size;
3333
Todd Kjos858b8da2017-04-21 17:35:12 -07003334 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003335 int ret;
3336
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003337 if (get_user(cmd, (uint32_t __user *)ptr))
3338 return -EFAULT;
3339 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003340 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003341 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003342 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3343 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3344 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003345 }
3346 switch (cmd) {
3347 case BC_INCREFS:
3348 case BC_ACQUIRE:
3349 case BC_RELEASE:
3350 case BC_DECREFS: {
3351 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003352 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003353 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3354 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3355 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003356
3357 if (get_user(target, (uint32_t __user *)ptr))
3358 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003359
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003360 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003361 ret = -1;
3362 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003363 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003364 mutex_lock(&context->context_mgr_node_lock);
3365 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003366 if (ctx_mgr_node)
3367 ret = binder_inc_ref_for_node(
3368 proc, ctx_mgr_node,
3369 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003370 mutex_unlock(&context->context_mgr_node_lock);
3371 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003372 if (ret)
3373 ret = binder_update_ref_for_handle(
3374 proc, target, increment, strong,
3375 &rdata);
3376 if (!ret && rdata.desc != target) {
3377 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3378 proc->pid, thread->pid,
3379 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003380 }
3381 switch (cmd) {
3382 case BC_INCREFS:
3383 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003384 break;
3385 case BC_ACQUIRE:
3386 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003387 break;
3388 case BC_RELEASE:
3389 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003390 break;
3391 case BC_DECREFS:
3392 default:
3393 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003394 break;
3395 }
3396 if (ret) {
3397 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3398 proc->pid, thread->pid, debug_string,
3399 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003400 break;
3401 }
3402 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003403 "%d:%d %s ref %d desc %d s %d w %d\n",
3404 proc->pid, thread->pid, debug_string,
3405 rdata.debug_id, rdata.desc, rdata.strong,
3406 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003407 break;
3408 }
3409 case BC_INCREFS_DONE:
3410 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003411 binder_uintptr_t node_ptr;
3412 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003413 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003414 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003415
Arve Hjønnevågda498892014-02-21 14:40:26 -08003416 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003417 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003418 ptr += sizeof(binder_uintptr_t);
3419 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003420 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003421 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003422 node = binder_get_node(proc, node_ptr);
3423 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003424 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003425 proc->pid, thread->pid,
3426 cmd == BC_INCREFS_DONE ?
3427 "BC_INCREFS_DONE" :
3428 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003429 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003430 break;
3431 }
3432 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003433 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003434 proc->pid, thread->pid,
3435 cmd == BC_INCREFS_DONE ?
3436 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003437 (u64)node_ptr, node->debug_id,
3438 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003439 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003440 break;
3441 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003442 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003443 if (cmd == BC_ACQUIRE_DONE) {
3444 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303445 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003446 proc->pid, thread->pid,
3447 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003448 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003449 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003450 break;
3451 }
3452 node->pending_strong_ref = 0;
3453 } else {
3454 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303455 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003456 proc->pid, thread->pid,
3457 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003458 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003459 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003460 break;
3461 }
3462 node->pending_weak_ref = 0;
3463 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003464 free_node = binder_dec_node_nilocked(node,
3465 cmd == BC_ACQUIRE_DONE, 0);
3466 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003467 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003468 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003469 proc->pid, thread->pid,
3470 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003471 node->debug_id, node->local_strong_refs,
3472 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003473 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003474 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003475 break;
3476 }
3477 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303478 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003479 return -EINVAL;
3480 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303481 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003482 return -EINVAL;
3483
3484 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003485 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003486 struct binder_buffer *buffer;
3487
Arve Hjønnevågda498892014-02-21 14:40:26 -08003488 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003489 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003490 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003491
Todd Kjos076072a2017-04-21 14:32:11 -07003492 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3493 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003494 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003495 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3496 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003497 break;
3498 }
3499 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003500 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3501 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003502 break;
3503 }
3504 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003505 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3506 proc->pid, thread->pid, (u64)data_ptr,
3507 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003508 buffer->transaction ? "active" : "finished");
3509
3510 if (buffer->transaction) {
3511 buffer->transaction->buffer = NULL;
3512 buffer->transaction = NULL;
3513 }
3514 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003515 struct binder_node *buf_node;
3516 struct binder_work *w;
3517
3518 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003519 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003520 BUG_ON(!buf_node->has_async_transaction);
3521 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003522 w = binder_dequeue_work_head_ilocked(
3523 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003524 if (!w) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003525 buf_node->has_async_transaction = 0;
Martijn Coenen4501c042017-08-10 13:56:16 +02003526 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003527 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003528 w, &proc->todo);
3529 binder_wakeup_proc_ilocked(proc);
3530 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003531 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003532 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003533 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003534 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07003535 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003536 break;
3537 }
3538
Martijn Coenen5a6da532016-09-30 14:10:07 +02003539 case BC_TRANSACTION_SG:
3540 case BC_REPLY_SG: {
3541 struct binder_transaction_data_sg tr;
3542
3543 if (copy_from_user(&tr, ptr, sizeof(tr)))
3544 return -EFAULT;
3545 ptr += sizeof(tr);
3546 binder_transaction(proc, thread, &tr.transaction_data,
3547 cmd == BC_REPLY_SG, tr.buffers_size);
3548 break;
3549 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003550 case BC_TRANSACTION:
3551 case BC_REPLY: {
3552 struct binder_transaction_data tr;
3553
3554 if (copy_from_user(&tr, ptr, sizeof(tr)))
3555 return -EFAULT;
3556 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003557 binder_transaction(proc, thread, &tr,
3558 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003559 break;
3560 }
3561
3562 case BC_REGISTER_LOOPER:
3563 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303564 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003565 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003566 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003567 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3568 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303569 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003570 proc->pid, thread->pid);
3571 } else if (proc->requested_threads == 0) {
3572 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303573 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003574 proc->pid, thread->pid);
3575 } else {
3576 proc->requested_threads--;
3577 proc->requested_threads_started++;
3578 }
3579 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003580 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003581 break;
3582 case BC_ENTER_LOOPER:
3583 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303584 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003585 proc->pid, thread->pid);
3586 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3587 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303588 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003589 proc->pid, thread->pid);
3590 }
3591 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3592 break;
3593 case BC_EXIT_LOOPER:
3594 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303595 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003596 proc->pid, thread->pid);
3597 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3598 break;
3599
3600 case BC_REQUEST_DEATH_NOTIFICATION:
3601 case BC_CLEAR_DEATH_NOTIFICATION: {
3602 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003603 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003604 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003605 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003606
3607 if (get_user(target, (uint32_t __user *)ptr))
3608 return -EFAULT;
3609 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003610 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003611 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003612 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003613 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3614 /*
3615 * Allocate memory for death notification
3616 * before taking lock
3617 */
3618 death = kzalloc(sizeof(*death), GFP_KERNEL);
3619 if (death == NULL) {
3620 WARN_ON(thread->return_error.cmd !=
3621 BR_OK);
3622 thread->return_error.cmd = BR_ERROR;
3623 binder_enqueue_work(
3624 thread->proc,
3625 &thread->return_error.work,
3626 &thread->todo);
3627 binder_debug(
3628 BINDER_DEBUG_FAILED_TRANSACTION,
3629 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3630 proc->pid, thread->pid);
3631 break;
3632 }
3633 }
3634 binder_proc_lock(proc);
3635 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003636 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303637 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003638 proc->pid, thread->pid,
3639 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3640 "BC_REQUEST_DEATH_NOTIFICATION" :
3641 "BC_CLEAR_DEATH_NOTIFICATION",
3642 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003643 binder_proc_unlock(proc);
3644 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003645 break;
3646 }
3647
3648 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003649 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003650 proc->pid, thread->pid,
3651 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3652 "BC_REQUEST_DEATH_NOTIFICATION" :
3653 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003654 (u64)cookie, ref->data.debug_id,
3655 ref->data.desc, ref->data.strong,
3656 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003657
Martijn Coenenf9eac642017-05-22 11:26:23 -07003658 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003659 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3660 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303661 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003662 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003663 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003664 binder_proc_unlock(proc);
3665 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003666 break;
3667 }
3668 binder_stats_created(BINDER_STAT_DEATH);
3669 INIT_LIST_HEAD(&death->work.entry);
3670 death->cookie = cookie;
3671 ref->death = death;
3672 if (ref->node->proc == NULL) {
3673 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02003674
3675 binder_inner_proc_lock(proc);
3676 binder_enqueue_work_ilocked(
3677 &ref->death->work, &proc->todo);
3678 binder_wakeup_proc_ilocked(proc);
3679 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003680 }
3681 } else {
3682 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303683 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003684 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003685 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003686 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003687 break;
3688 }
3689 death = ref->death;
3690 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003691 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003692 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003693 (u64)death->cookie,
3694 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003695 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003696 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003697 break;
3698 }
3699 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003700 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003701 if (list_empty(&death->work.entry)) {
3702 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003703 if (thread->looper &
3704 (BINDER_LOOPER_STATE_REGISTERED |
3705 BINDER_LOOPER_STATE_ENTERED))
3706 binder_enqueue_work_ilocked(
3707 &death->work,
3708 &thread->todo);
3709 else {
3710 binder_enqueue_work_ilocked(
3711 &death->work,
3712 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003713 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07003714 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003715 }
3716 } else {
3717 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3718 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3719 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003720 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003721 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07003722 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003723 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003724 } break;
3725 case BC_DEAD_BINDER_DONE: {
3726 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003727 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003728 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003729
Arve Hjønnevågda498892014-02-21 14:40:26 -08003730 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003731 return -EFAULT;
3732
Lisa Du7a64cd82016-02-17 09:32:52 +08003733 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003734 binder_inner_proc_lock(proc);
3735 list_for_each_entry(w, &proc->delivered_death,
3736 entry) {
3737 struct binder_ref_death *tmp_death =
3738 container_of(w,
3739 struct binder_ref_death,
3740 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003741
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003742 if (tmp_death->cookie == cookie) {
3743 death = tmp_death;
3744 break;
3745 }
3746 }
3747 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003748 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3749 proc->pid, thread->pid, (u64)cookie,
3750 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003751 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003752 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3753 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003754 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003755 break;
3756 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003757 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003758 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3759 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003760 if (thread->looper &
3761 (BINDER_LOOPER_STATE_REGISTERED |
3762 BINDER_LOOPER_STATE_ENTERED))
3763 binder_enqueue_work_ilocked(
3764 &death->work, &thread->todo);
3765 else {
3766 binder_enqueue_work_ilocked(
3767 &death->work,
3768 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07003769 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003770 }
3771 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003772 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003773 } break;
3774
3775 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303776 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003777 proc->pid, thread->pid, cmd);
3778 return -EINVAL;
3779 }
3780 *consumed = ptr - buffer;
3781 }
3782 return 0;
3783}
3784
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003785static void binder_stat_br(struct binder_proc *proc,
3786 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003787{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003788 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003789 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003790 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3791 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3792 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003793 }
3794}
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 Coenenecd972d2017-05-26 10:48:56 -07003895 binder_restore_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 poll_wait(filp, &thread->wait, wait);
4427
Martijn Coenen47810932017-08-10 12:32:00 +02004428 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004429 return POLLIN;
4430
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004431 return 0;
4432}
4433
Tair Rzayev78260ac2014-06-03 22:27:21 +03004434static int binder_ioctl_write_read(struct file *filp,
4435 unsigned int cmd, unsigned long arg,
4436 struct binder_thread *thread)
4437{
4438 int ret = 0;
4439 struct binder_proc *proc = filp->private_data;
4440 unsigned int size = _IOC_SIZE(cmd);
4441 void __user *ubuf = (void __user *)arg;
4442 struct binder_write_read bwr;
4443
4444 if (size != sizeof(struct binder_write_read)) {
4445 ret = -EINVAL;
4446 goto out;
4447 }
4448 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4449 ret = -EFAULT;
4450 goto out;
4451 }
4452 binder_debug(BINDER_DEBUG_READ_WRITE,
4453 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4454 proc->pid, thread->pid,
4455 (u64)bwr.write_size, (u64)bwr.write_buffer,
4456 (u64)bwr.read_size, (u64)bwr.read_buffer);
4457
4458 if (bwr.write_size > 0) {
4459 ret = binder_thread_write(proc, thread,
4460 bwr.write_buffer,
4461 bwr.write_size,
4462 &bwr.write_consumed);
4463 trace_binder_write_done(ret);
4464 if (ret < 0) {
4465 bwr.read_consumed = 0;
4466 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4467 ret = -EFAULT;
4468 goto out;
4469 }
4470 }
4471 if (bwr.read_size > 0) {
4472 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4473 bwr.read_size,
4474 &bwr.read_consumed,
4475 filp->f_flags & O_NONBLOCK);
4476 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004477 binder_inner_proc_lock(proc);
4478 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004479 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004480 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004481 if (ret < 0) {
4482 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4483 ret = -EFAULT;
4484 goto out;
4485 }
4486 }
4487 binder_debug(BINDER_DEBUG_READ_WRITE,
4488 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4489 proc->pid, thread->pid,
4490 (u64)bwr.write_consumed, (u64)bwr.write_size,
4491 (u64)bwr.read_consumed, (u64)bwr.read_size);
4492 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4493 ret = -EFAULT;
4494 goto out;
4495 }
4496out:
4497 return ret;
4498}
4499
4500static int binder_ioctl_set_ctx_mgr(struct file *filp)
4501{
4502 int ret = 0;
4503 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004504 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004505 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004506 kuid_t curr_euid = current_euid();
4507
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004508 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004509 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004510 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4511 ret = -EBUSY;
4512 goto out;
4513 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004514 ret = security_binder_set_context_mgr(proc->tsk);
4515 if (ret < 0)
4516 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004517 if (uid_valid(context->binder_context_mgr_uid)) {
4518 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004519 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4520 from_kuid(&init_user_ns, curr_euid),
4521 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004522 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004523 ret = -EPERM;
4524 goto out;
4525 }
4526 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004527 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004528 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004529 new_node = binder_new_node(proc, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004530 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004531 ret = -ENOMEM;
4532 goto out;
4533 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004534 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004535 new_node->local_weak_refs++;
4536 new_node->local_strong_refs++;
4537 new_node->has_strong_ref = 1;
4538 new_node->has_weak_ref = 1;
4539 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004540 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004541 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004542out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004543 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004544 return ret;
4545}
4546
Colin Cross833babb32017-06-20 13:54:44 -07004547static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4548 struct binder_node_debug_info *info) {
4549 struct rb_node *n;
4550 binder_uintptr_t ptr = info->ptr;
4551
4552 memset(info, 0, sizeof(*info));
4553
4554 binder_inner_proc_lock(proc);
4555 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4556 struct binder_node *node = rb_entry(n, struct binder_node,
4557 rb_node);
4558 if (node->ptr > ptr) {
4559 info->ptr = node->ptr;
4560 info->cookie = node->cookie;
4561 info->has_strong_ref = node->has_strong_ref;
4562 info->has_weak_ref = node->has_weak_ref;
4563 break;
4564 }
4565 }
4566 binder_inner_proc_unlock(proc);
4567
4568 return 0;
4569}
4570
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004571static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4572{
4573 int ret;
4574 struct binder_proc *proc = filp->private_data;
4575 struct binder_thread *thread;
4576 unsigned int size = _IOC_SIZE(cmd);
4577 void __user *ubuf = (void __user *)arg;
4578
Tair Rzayev78260ac2014-06-03 22:27:21 +03004579 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4580 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004581
Sherry Yang435416b2017-06-22 14:37:45 -07004582 binder_selftest_alloc(&proc->alloc);
4583
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004584 trace_binder_ioctl(cmd, arg);
4585
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004586 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4587 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004588 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004589
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004590 thread = binder_get_thread(proc);
4591 if (thread == NULL) {
4592 ret = -ENOMEM;
4593 goto err;
4594 }
4595
4596 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004597 case BINDER_WRITE_READ:
4598 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4599 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004600 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004601 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004602 case BINDER_SET_MAX_THREADS: {
4603 int max_threads;
4604
4605 if (copy_from_user(&max_threads, ubuf,
4606 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004607 ret = -EINVAL;
4608 goto err;
4609 }
Todd Kjosd600e902017-05-25 17:35:02 -07004610 binder_inner_proc_lock(proc);
4611 proc->max_threads = max_threads;
4612 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004613 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004614 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004615 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004616 ret = binder_ioctl_set_ctx_mgr(filp);
4617 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004618 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004619 break;
4620 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304621 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004622 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07004623 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004624 thread = NULL;
4625 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004626 case BINDER_VERSION: {
4627 struct binder_version __user *ver = ubuf;
4628
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004629 if (size != sizeof(struct binder_version)) {
4630 ret = -EINVAL;
4631 goto err;
4632 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004633 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4634 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004635 ret = -EINVAL;
4636 goto err;
4637 }
4638 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004639 }
Colin Cross833babb32017-06-20 13:54:44 -07004640 case BINDER_GET_NODE_DEBUG_INFO: {
4641 struct binder_node_debug_info info;
4642
4643 if (copy_from_user(&info, ubuf, sizeof(info))) {
4644 ret = -EFAULT;
4645 goto err;
4646 }
4647
4648 ret = binder_ioctl_get_node_debug_info(proc, &info);
4649 if (ret < 0)
4650 goto err;
4651
4652 if (copy_to_user(ubuf, &info, sizeof(info))) {
4653 ret = -EFAULT;
4654 goto err;
4655 }
4656 break;
4657 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004658 default:
4659 ret = -EINVAL;
4660 goto err;
4661 }
4662 ret = 0;
4663err:
4664 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08004665 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004666 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4667 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304668 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 -07004669err_unlocked:
4670 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004671 return ret;
4672}
4673
4674static void binder_vma_open(struct vm_area_struct *vma)
4675{
4676 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004677
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004678 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304679 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004680 proc->pid, vma->vm_start, vma->vm_end,
4681 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4682 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004683}
4684
4685static void binder_vma_close(struct vm_area_struct *vma)
4686{
4687 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004688
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004689 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304690 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004691 proc->pid, vma->vm_start, vma->vm_end,
4692 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4693 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07004694 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004695 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4696}
4697
Vinayak Menonddac7d52014-06-02 18:17:59 +05304698static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4699{
4700 return VM_FAULT_SIGBUS;
4701}
4702
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004703static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004704 .open = binder_vma_open,
4705 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304706 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004707};
4708
Todd Kjosd325d372016-10-10 10:40:53 -07004709static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4710{
4711 int ret;
4712 struct binder_proc *proc = filp->private_data;
4713 const char *failure_string;
4714
4715 if (proc->tsk != current->group_leader)
4716 return -EINVAL;
4717
4718 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4719 vma->vm_end = vma->vm_start + SZ_4M;
4720
4721 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4722 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4723 __func__, proc->pid, vma->vm_start, vma->vm_end,
4724 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4725 (unsigned long)pgprot_val(vma->vm_page_prot));
4726
4727 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4728 ret = -EPERM;
4729 failure_string = "bad vm_flags";
4730 goto err_bad_arg;
4731 }
4732 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4733 vma->vm_ops = &binder_vm_ops;
4734 vma->vm_private_data = proc;
4735
4736 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4737 if (ret)
4738 return ret;
4739 proc->files = get_files_struct(current);
4740 return 0;
4741
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004742err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004743 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004744 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4745 return ret;
4746}
4747
4748static int binder_open(struct inode *nodp, struct file *filp)
4749{
4750 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004751 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004752
4753 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4754 current->group_leader->pid, current->pid);
4755
4756 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4757 if (proc == NULL)
4758 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07004759 spin_lock_init(&proc->inner_lock);
4760 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01004761 get_task_struct(current->group_leader);
4762 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004763 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07004764 if (binder_supported_policy(current->policy)) {
4765 proc->default_priority.sched_policy = current->policy;
4766 proc->default_priority.prio = current->normal_prio;
4767 } else {
4768 proc->default_priority.sched_policy = SCHED_NORMAL;
4769 proc->default_priority.prio = NICE_TO_PRIO(0);
4770 }
4771
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004772 binder_dev = container_of(filp->private_data, struct binder_device,
4773 miscdev);
4774 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07004775 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004776
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004777 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004778 proc->pid = current->group_leader->pid;
4779 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004780 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004781 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004782
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004783 mutex_lock(&binder_procs_lock);
4784 hlist_add_head(&proc->proc_node, &binder_procs);
4785 mutex_unlock(&binder_procs_lock);
4786
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004787 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004788 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004789
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004790 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004791 /*
4792 * proc debug entries are shared between contexts, so
4793 * this will fail if the process tries to open the driver
4794 * again with a different context. The priting code will
4795 * anyway print all contexts that a given PID has, so this
4796 * is not a problem.
4797 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004798 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004799 binder_debugfs_dir_entry_proc,
4800 (void *)(unsigned long)proc->pid,
4801 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004802 }
4803
4804 return 0;
4805}
4806
4807static int binder_flush(struct file *filp, fl_owner_t id)
4808{
4809 struct binder_proc *proc = filp->private_data;
4810
4811 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4812
4813 return 0;
4814}
4815
4816static void binder_deferred_flush(struct binder_proc *proc)
4817{
4818 struct rb_node *n;
4819 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004820
Todd Kjosb4827902017-05-25 15:52:17 -07004821 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004822 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4823 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004824
Todd Kjos6798e6d2017-01-06 14:19:25 -08004825 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004826 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4827 wake_up_interruptible(&thread->wait);
4828 wake_count++;
4829 }
4830 }
Todd Kjosb4827902017-05-25 15:52:17 -07004831 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004832
4833 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4834 "binder_flush: %d woke %d threads\n", proc->pid,
4835 wake_count);
4836}
4837
4838static int binder_release(struct inode *nodp, struct file *filp)
4839{
4840 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004841
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004842 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004843 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4844
4845 return 0;
4846}
4847
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004848static int binder_node_release(struct binder_node *node, int refs)
4849{
4850 struct binder_ref *ref;
4851 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004852 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004853
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004854 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004855
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004856 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004857 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004858 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07004859 /*
4860 * The caller must have taken a temporary ref on the node,
4861 */
4862 BUG_ON(!node->tmp_refs);
4863 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004864 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004865 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004866 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004867
4868 return refs;
4869 }
4870
4871 node->proc = NULL;
4872 node->local_strong_refs = 0;
4873 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004874 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004875
4876 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004877 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004878 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004879
4880 hlist_for_each_entry(ref, &node->refs, node_entry) {
4881 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004882 /*
4883 * Need the node lock to synchronize
4884 * with new notification requests and the
4885 * inner lock to synchronize with queued
4886 * death notifications.
4887 */
4888 binder_inner_proc_lock(ref->proc);
4889 if (!ref->death) {
4890 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08004891 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004892 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004893
4894 death++;
4895
Martijn Coenenf9eac642017-05-22 11:26:23 -07004896 BUG_ON(!list_empty(&ref->death->work.entry));
4897 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4898 binder_enqueue_work_ilocked(&ref->death->work,
4899 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07004900 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004901 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004902 }
4903
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004904 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4905 "node %d now dead, refs %d, death %d\n",
4906 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004907 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004908 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004909
4910 return refs;
4911}
4912
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004913static void binder_deferred_release(struct binder_proc *proc)
4914{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004915 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004916 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07004917 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004918
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004919 BUG_ON(proc->files);
4920
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004921 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004922 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004923 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004924
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004925 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004926 if (context->binder_context_mgr_node &&
4927 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004928 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004929 "%s: %d context_mgr_node gone\n",
4930 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004931 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004932 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004933 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07004934 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004935 /*
4936 * Make sure proc stays alive after we
4937 * remove all the threads
4938 */
4939 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004940
Todd Kjos2f993e22017-05-12 14:42:55 -07004941 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004942 threads = 0;
4943 active_transactions = 0;
4944 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004945 struct binder_thread *thread;
4946
4947 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004948 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004949 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07004950 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07004951 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004952 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004953
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004954 nodes = 0;
4955 incoming_refs = 0;
4956 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004957 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004958
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004959 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004960 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07004961 /*
4962 * take a temporary ref on the node before
4963 * calling binder_node_release() which will either
4964 * kfree() the node or call binder_put_node()
4965 */
Todd Kjos425d23f2017-06-12 12:07:26 -07004966 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004967 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07004968 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004969 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07004970 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004971 }
Todd Kjos425d23f2017-06-12 12:07:26 -07004972 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004973
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004974 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07004975 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004976 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004977 struct binder_ref *ref;
4978
4979 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004980 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07004981 binder_cleanup_ref_olocked(ref);
4982 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07004983 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07004984 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004985 }
Todd Kjos5346bf32016-10-20 16:43:34 -07004986 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004987
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004988 binder_release_work(proc, &proc->todo);
4989 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004990
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004991 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07004992 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004993 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07004994 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004995
Todd Kjos2f993e22017-05-12 14:42:55 -07004996 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004997}
4998
4999static void binder_deferred_func(struct work_struct *work)
5000{
5001 struct binder_proc *proc;
5002 struct files_struct *files;
5003
5004 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005005
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005006 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005007 mutex_lock(&binder_deferred_lock);
5008 if (!hlist_empty(&binder_deferred_list)) {
5009 proc = hlist_entry(binder_deferred_list.first,
5010 struct binder_proc, deferred_work_node);
5011 hlist_del_init(&proc->deferred_work_node);
5012 defer = proc->deferred_work;
5013 proc->deferred_work = 0;
5014 } else {
5015 proc = NULL;
5016 defer = 0;
5017 }
5018 mutex_unlock(&binder_deferred_lock);
5019
5020 files = NULL;
5021 if (defer & BINDER_DEFERRED_PUT_FILES) {
5022 files = proc->files;
5023 if (files)
5024 proc->files = NULL;
5025 }
5026
5027 if (defer & BINDER_DEFERRED_FLUSH)
5028 binder_deferred_flush(proc);
5029
5030 if (defer & BINDER_DEFERRED_RELEASE)
5031 binder_deferred_release(proc); /* frees proc */
5032
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005033 if (files)
5034 put_files_struct(files);
5035 } while (proc);
5036}
5037static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5038
5039static void
5040binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5041{
5042 mutex_lock(&binder_deferred_lock);
5043 proc->deferred_work |= defer;
5044 if (hlist_unhashed(&proc->deferred_work_node)) {
5045 hlist_add_head(&proc->deferred_work_node,
5046 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305047 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005048 }
5049 mutex_unlock(&binder_deferred_lock);
5050}
5051
Todd Kjos6d241a42017-04-21 14:32:11 -07005052static void print_binder_transaction_ilocked(struct seq_file *m,
5053 struct binder_proc *proc,
5054 const char *prefix,
5055 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005056{
Todd Kjos6d241a42017-04-21 14:32:11 -07005057 struct binder_proc *to_proc;
5058 struct binder_buffer *buffer = t->buffer;
5059
Todd Kjos2f993e22017-05-12 14:42:55 -07005060 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005061 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005062 seq_printf(m,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005063 "%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 -07005064 prefix, t->debug_id, t,
5065 t->from ? t->from->proc->pid : 0,
5066 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005067 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005068 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005069 t->code, t->flags, t->priority.sched_policy,
5070 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005071 spin_unlock(&t->lock);
5072
Todd Kjos6d241a42017-04-21 14:32:11 -07005073 if (proc != to_proc) {
5074 /*
5075 * Can only safely deref buffer if we are holding the
5076 * correct proc inner lock for this node
5077 */
5078 seq_puts(m, "\n");
5079 return;
5080 }
5081
5082 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005083 seq_puts(m, " buffer free\n");
5084 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005085 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005086 if (buffer->target_node)
5087 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005088 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005089 buffer->data_size, buffer->offsets_size,
5090 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005091}
5092
Todd Kjos6d241a42017-04-21 14:32:11 -07005093static void print_binder_work_ilocked(struct seq_file *m,
5094 struct binder_proc *proc,
5095 const char *prefix,
5096 const char *transaction_prefix,
5097 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005098{
5099 struct binder_node *node;
5100 struct binder_transaction *t;
5101
5102 switch (w->type) {
5103 case BINDER_WORK_TRANSACTION:
5104 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005105 print_binder_transaction_ilocked(
5106 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005107 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005108 case BINDER_WORK_RETURN_ERROR: {
5109 struct binder_error *e = container_of(
5110 w, struct binder_error, work);
5111
5112 seq_printf(m, "%stransaction error: %u\n",
5113 prefix, e->cmd);
5114 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005115 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005116 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005117 break;
5118 case BINDER_WORK_NODE:
5119 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005120 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5121 prefix, node->debug_id,
5122 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005123 break;
5124 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005125 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005126 break;
5127 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005128 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005129 break;
5130 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005131 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005132 break;
5133 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005134 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005135 break;
5136 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005137}
5138
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005139static void print_binder_thread_ilocked(struct seq_file *m,
5140 struct binder_thread *thread,
5141 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005142{
5143 struct binder_transaction *t;
5144 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005145 size_t start_pos = m->count;
5146 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005147
Todd Kjos2f993e22017-05-12 14:42:55 -07005148 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005149 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005150 thread->looper_need_return,
5151 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005152 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005153 t = thread->transaction_stack;
5154 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005155 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005156 print_binder_transaction_ilocked(m, thread->proc,
5157 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005158 t = t->from_parent;
5159 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005160 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005161 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005162 t = t->to_parent;
5163 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005164 print_binder_transaction_ilocked(m, thread->proc,
5165 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005166 t = NULL;
5167 }
5168 }
5169 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005170 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005171 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005172 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005173 if (!print_always && m->count == header_pos)
5174 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005175}
5176
Todd Kjos425d23f2017-06-12 12:07:26 -07005177static void print_binder_node_nilocked(struct seq_file *m,
5178 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005179{
5180 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005181 struct binder_work *w;
5182 int count;
5183
5184 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005185 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005186 count++;
5187
Martijn Coenen6aac9792017-06-07 09:29:14 -07005188 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 -08005189 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005190 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005191 node->has_strong_ref, node->has_weak_ref,
5192 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005193 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005194 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005195 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005196 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005197 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005198 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005199 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005200 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005201 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005202 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005203 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005204 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005205}
5206
Todd Kjos5346bf32016-10-20 16:43:34 -07005207static void print_binder_ref_olocked(struct seq_file *m,
5208 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005209{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005210 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005211 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5212 ref->data.debug_id, ref->data.desc,
5213 ref->node->proc ? "" : "dead ",
5214 ref->node->debug_id, ref->data.strong,
5215 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005216 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005217}
5218
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005219static void print_binder_proc(struct seq_file *m,
5220 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005221{
5222 struct binder_work *w;
5223 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005224 size_t start_pos = m->count;
5225 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005226 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005227
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005228 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005229 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005230 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005231
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005232 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005233 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005234 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005235 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005236
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005237 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005238 struct binder_node *node = rb_entry(n, struct binder_node,
5239 rb_node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005240 /*
5241 * take a temporary reference on the node so it
5242 * survives and isn't removed from the tree
5243 * while we print it.
5244 */
5245 binder_inc_node_tmpref_ilocked(node);
5246 /* Need to drop inner lock to take node lock */
5247 binder_inner_proc_unlock(proc);
5248 if (last_node)
5249 binder_put_node(last_node);
5250 binder_node_inner_lock(node);
5251 print_binder_node_nilocked(m, node);
5252 binder_node_inner_unlock(node);
5253 last_node = node;
5254 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005255 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005256 binder_inner_proc_unlock(proc);
5257 if (last_node)
5258 binder_put_node(last_node);
5259
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005260 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005261 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005262 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005263 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005264 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005265 print_binder_ref_olocked(m, rb_entry(n,
5266 struct binder_ref,
5267 rb_node_desc));
5268 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005269 }
Todd Kjosd325d372016-10-10 10:40:53 -07005270 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005271 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005272 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005273 print_binder_work_ilocked(m, proc, " ",
5274 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005275 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005276 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005277 break;
5278 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005279 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005280 if (!print_all && m->count == header_pos)
5281 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005282}
5283
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005284static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005285 "BR_ERROR",
5286 "BR_OK",
5287 "BR_TRANSACTION",
5288 "BR_REPLY",
5289 "BR_ACQUIRE_RESULT",
5290 "BR_DEAD_REPLY",
5291 "BR_TRANSACTION_COMPLETE",
5292 "BR_INCREFS",
5293 "BR_ACQUIRE",
5294 "BR_RELEASE",
5295 "BR_DECREFS",
5296 "BR_ATTEMPT_ACQUIRE",
5297 "BR_NOOP",
5298 "BR_SPAWN_LOOPER",
5299 "BR_FINISHED",
5300 "BR_DEAD_BINDER",
5301 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5302 "BR_FAILED_REPLY"
5303};
5304
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005305static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005306 "BC_TRANSACTION",
5307 "BC_REPLY",
5308 "BC_ACQUIRE_RESULT",
5309 "BC_FREE_BUFFER",
5310 "BC_INCREFS",
5311 "BC_ACQUIRE",
5312 "BC_RELEASE",
5313 "BC_DECREFS",
5314 "BC_INCREFS_DONE",
5315 "BC_ACQUIRE_DONE",
5316 "BC_ATTEMPT_ACQUIRE",
5317 "BC_REGISTER_LOOPER",
5318 "BC_ENTER_LOOPER",
5319 "BC_EXIT_LOOPER",
5320 "BC_REQUEST_DEATH_NOTIFICATION",
5321 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005322 "BC_DEAD_BINDER_DONE",
5323 "BC_TRANSACTION_SG",
5324 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005325};
5326
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005327static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005328 "proc",
5329 "thread",
5330 "node",
5331 "ref",
5332 "death",
5333 "transaction",
5334 "transaction_complete"
5335};
5336
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005337static void print_binder_stats(struct seq_file *m, const char *prefix,
5338 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005339{
5340 int i;
5341
5342 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005343 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005344 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005345 int temp = atomic_read(&stats->bc[i]);
5346
5347 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005348 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005349 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005350 }
5351
5352 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005353 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005354 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005355 int temp = atomic_read(&stats->br[i]);
5356
5357 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005358 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005359 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005360 }
5361
5362 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005363 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005364 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005365 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005366 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005367 int created = atomic_read(&stats->obj_created[i]);
5368 int deleted = atomic_read(&stats->obj_deleted[i]);
5369
5370 if (created || deleted)
5371 seq_printf(m, "%s%s: active %d total %d\n",
5372 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005373 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005374 created - deleted,
5375 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005376 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005377}
5378
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005379static void print_binder_proc_stats(struct seq_file *m,
5380 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005381{
5382 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005383 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005384 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005385 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005386 size_t free_async_space =
5387 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005388
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005389 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005390 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005391 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005392 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005393 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005394 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5395 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005396
5397 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5398 ready_threads++;
5399
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005400 seq_printf(m, " threads: %d\n", count);
5401 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005402 " ready threads %d\n"
5403 " free async space %zd\n", proc->requested_threads,
5404 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005405 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005406 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005407 count = 0;
5408 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5409 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005410 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005411 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005412 count = 0;
5413 strong = 0;
5414 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005415 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005416 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5417 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5418 rb_node_desc);
5419 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005420 strong += ref->data.strong;
5421 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005422 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005423 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005424 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005425
Todd Kjosd325d372016-10-10 10:40:53 -07005426 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005427 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005428
5429 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005430 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005431 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005432 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005433 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005434 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005435 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005436 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005437
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005438 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005439}
5440
5441
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005442static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005443{
5444 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005445 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005446 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005447
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005448 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005449
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005450 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005451 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005452 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005453 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5454 /*
5455 * take a temporary reference on the node so it
5456 * survives and isn't removed from the list
5457 * while we print it.
5458 */
5459 node->tmp_refs++;
5460 spin_unlock(&binder_dead_nodes_lock);
5461 if (last_node)
5462 binder_put_node(last_node);
5463 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005464 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005465 binder_node_unlock(node);
5466 last_node = node;
5467 spin_lock(&binder_dead_nodes_lock);
5468 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005469 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005470 if (last_node)
5471 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005472
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005473 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005474 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005475 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005476 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005477
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005478 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005479}
5480
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005481static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005482{
5483 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005484
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005485 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005486
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005487 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005488
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005489 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005490 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005491 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005492 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005493
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005494 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005495}
5496
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005497static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005498{
5499 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005500
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005501 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005502 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005503 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005504 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005505 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005506
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005507 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005508}
5509
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005510static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005511{
Riley Andrews83050a42016-02-09 21:05:33 -08005512 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005513 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005514
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005515 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005516 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005517 if (itr->pid == pid) {
5518 seq_puts(m, "binder proc state:\n");
5519 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005520 }
5521 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005522 mutex_unlock(&binder_procs_lock);
5523
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005524 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005525}
5526
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005527static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005528 struct binder_transaction_log_entry *e)
5529{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005530 int debug_id = READ_ONCE(e->debug_id_done);
5531 /*
5532 * read barrier to guarantee debug_id_done read before
5533 * we print the log values
5534 */
5535 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005536 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005537 "%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 -07005538 e->debug_id, (e->call_type == 2) ? "reply" :
5539 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005540 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005541 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5542 e->return_error, e->return_error_param,
5543 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005544 /*
5545 * read-barrier to guarantee read of debug_id_done after
5546 * done printing the fields of the entry
5547 */
5548 smp_rmb();
5549 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5550 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005551}
5552
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005553static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005554{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005555 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005556 unsigned int log_cur = atomic_read(&log->cur);
5557 unsigned int count;
5558 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005559 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005560
Todd Kjos1cfe6272017-05-24 13:33:28 -07005561 count = log_cur + 1;
5562 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5563 0 : count % ARRAY_SIZE(log->entry);
5564 if (count > ARRAY_SIZE(log->entry) || log->full)
5565 count = ARRAY_SIZE(log->entry);
5566 for (i = 0; i < count; i++) {
5567 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5568
5569 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005570 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005571 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005572}
5573
5574static const struct file_operations binder_fops = {
5575 .owner = THIS_MODULE,
5576 .poll = binder_poll,
5577 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005578 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005579 .mmap = binder_mmap,
5580 .open = binder_open,
5581 .flush = binder_flush,
5582 .release = binder_release,
5583};
5584
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005585BINDER_DEBUG_ENTRY(state);
5586BINDER_DEBUG_ENTRY(stats);
5587BINDER_DEBUG_ENTRY(transactions);
5588BINDER_DEBUG_ENTRY(transaction_log);
5589
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005590static int __init init_binder_device(const char *name)
5591{
5592 int ret;
5593 struct binder_device *binder_device;
5594
5595 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5596 if (!binder_device)
5597 return -ENOMEM;
5598
5599 binder_device->miscdev.fops = &binder_fops;
5600 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5601 binder_device->miscdev.name = name;
5602
5603 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5604 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005605 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005606
5607 ret = misc_register(&binder_device->miscdev);
5608 if (ret < 0) {
5609 kfree(binder_device);
5610 return ret;
5611 }
5612
5613 hlist_add_head(&binder_device->hlist, &binder_devices);
5614
5615 return ret;
5616}
5617
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005618static int __init binder_init(void)
5619{
5620 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005621 char *device_name, *device_names;
5622 struct binder_device *device;
5623 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005624
Sherry Yang5828d702017-07-29 13:24:11 -07005625 binder_alloc_shrinker_init();
5626
Todd Kjos1cfe6272017-05-24 13:33:28 -07005627 atomic_set(&binder_transaction_log.cur, ~0U);
5628 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5629
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005630 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5631 if (binder_debugfs_dir_entry_root)
5632 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5633 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005634
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005635 if (binder_debugfs_dir_entry_root) {
5636 debugfs_create_file("state",
5637 S_IRUGO,
5638 binder_debugfs_dir_entry_root,
5639 NULL,
5640 &binder_state_fops);
5641 debugfs_create_file("stats",
5642 S_IRUGO,
5643 binder_debugfs_dir_entry_root,
5644 NULL,
5645 &binder_stats_fops);
5646 debugfs_create_file("transactions",
5647 S_IRUGO,
5648 binder_debugfs_dir_entry_root,
5649 NULL,
5650 &binder_transactions_fops);
5651 debugfs_create_file("transaction_log",
5652 S_IRUGO,
5653 binder_debugfs_dir_entry_root,
5654 &binder_transaction_log,
5655 &binder_transaction_log_fops);
5656 debugfs_create_file("failed_transaction_log",
5657 S_IRUGO,
5658 binder_debugfs_dir_entry_root,
5659 &binder_transaction_log_failed,
5660 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005661 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005662
5663 /*
5664 * Copy the module_parameter string, because we don't want to
5665 * tokenize it in-place.
5666 */
5667 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5668 if (!device_names) {
5669 ret = -ENOMEM;
5670 goto err_alloc_device_names_failed;
5671 }
5672 strcpy(device_names, binder_devices_param);
5673
5674 while ((device_name = strsep(&device_names, ","))) {
5675 ret = init_binder_device(device_name);
5676 if (ret)
5677 goto err_init_binder_device_failed;
5678 }
5679
5680 return ret;
5681
5682err_init_binder_device_failed:
5683 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5684 misc_deregister(&device->miscdev);
5685 hlist_del(&device->hlist);
5686 kfree(device);
5687 }
5688err_alloc_device_names_failed:
5689 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5690
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005691 return ret;
5692}
5693
5694device_initcall(binder_init);
5695
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005696#define CREATE_TRACE_POINTS
5697#include "binder_trace.h"
5698
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005699MODULE_LICENSE("GPL v2");