blob: 88edacdb3a6b01efd23cdc838a10397b11c1601f [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);
3524 if (!w)
3525 buf_node->has_async_transaction = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003526 else
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003527 binder_enqueue_work_ilocked(
3528 w, &thread->todo);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003529 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003530 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003531 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003532 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07003533 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003534 break;
3535 }
3536
Martijn Coenen5a6da532016-09-30 14:10:07 +02003537 case BC_TRANSACTION_SG:
3538 case BC_REPLY_SG: {
3539 struct binder_transaction_data_sg tr;
3540
3541 if (copy_from_user(&tr, ptr, sizeof(tr)))
3542 return -EFAULT;
3543 ptr += sizeof(tr);
3544 binder_transaction(proc, thread, &tr.transaction_data,
3545 cmd == BC_REPLY_SG, tr.buffers_size);
3546 break;
3547 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003548 case BC_TRANSACTION:
3549 case BC_REPLY: {
3550 struct binder_transaction_data tr;
3551
3552 if (copy_from_user(&tr, ptr, sizeof(tr)))
3553 return -EFAULT;
3554 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003555 binder_transaction(proc, thread, &tr,
3556 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003557 break;
3558 }
3559
3560 case BC_REGISTER_LOOPER:
3561 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303562 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003563 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003564 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003565 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3566 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303567 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003568 proc->pid, thread->pid);
3569 } else if (proc->requested_threads == 0) {
3570 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303571 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003572 proc->pid, thread->pid);
3573 } else {
3574 proc->requested_threads--;
3575 proc->requested_threads_started++;
3576 }
3577 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003578 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003579 break;
3580 case BC_ENTER_LOOPER:
3581 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303582 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003583 proc->pid, thread->pid);
3584 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3585 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303586 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003587 proc->pid, thread->pid);
3588 }
3589 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3590 break;
3591 case BC_EXIT_LOOPER:
3592 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303593 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003594 proc->pid, thread->pid);
3595 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3596 break;
3597
3598 case BC_REQUEST_DEATH_NOTIFICATION:
3599 case BC_CLEAR_DEATH_NOTIFICATION: {
3600 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003601 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003602 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003603 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003604
3605 if (get_user(target, (uint32_t __user *)ptr))
3606 return -EFAULT;
3607 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003608 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003609 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003610 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003611 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3612 /*
3613 * Allocate memory for death notification
3614 * before taking lock
3615 */
3616 death = kzalloc(sizeof(*death), GFP_KERNEL);
3617 if (death == NULL) {
3618 WARN_ON(thread->return_error.cmd !=
3619 BR_OK);
3620 thread->return_error.cmd = BR_ERROR;
3621 binder_enqueue_work(
3622 thread->proc,
3623 &thread->return_error.work,
3624 &thread->todo);
3625 binder_debug(
3626 BINDER_DEBUG_FAILED_TRANSACTION,
3627 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3628 proc->pid, thread->pid);
3629 break;
3630 }
3631 }
3632 binder_proc_lock(proc);
3633 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003634 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303635 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003636 proc->pid, thread->pid,
3637 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3638 "BC_REQUEST_DEATH_NOTIFICATION" :
3639 "BC_CLEAR_DEATH_NOTIFICATION",
3640 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003641 binder_proc_unlock(proc);
3642 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003643 break;
3644 }
3645
3646 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003647 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003648 proc->pid, thread->pid,
3649 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3650 "BC_REQUEST_DEATH_NOTIFICATION" :
3651 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003652 (u64)cookie, ref->data.debug_id,
3653 ref->data.desc, ref->data.strong,
3654 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003655
Martijn Coenenf9eac642017-05-22 11:26:23 -07003656 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003657 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3658 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303659 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003660 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003661 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003662 binder_proc_unlock(proc);
3663 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003664 break;
3665 }
3666 binder_stats_created(BINDER_STAT_DEATH);
3667 INIT_LIST_HEAD(&death->work.entry);
3668 death->cookie = cookie;
3669 ref->death = death;
3670 if (ref->node->proc == NULL) {
3671 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003672 if (thread->looper &
3673 (BINDER_LOOPER_STATE_REGISTERED |
3674 BINDER_LOOPER_STATE_ENTERED))
3675 binder_enqueue_work(
3676 proc,
3677 &ref->death->work,
3678 &thread->todo);
3679 else {
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003680 binder_inner_proc_lock(proc);
3681 binder_enqueue_work_ilocked(
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003682 &ref->death->work,
3683 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003684 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07003685 proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003686 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003687 }
3688 }
3689 } else {
3690 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303691 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003692 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003693 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003694 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003695 break;
3696 }
3697 death = ref->death;
3698 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003699 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003700 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003701 (u64)death->cookie,
3702 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003703 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003704 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003705 break;
3706 }
3707 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003708 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003709 if (list_empty(&death->work.entry)) {
3710 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003711 if (thread->looper &
3712 (BINDER_LOOPER_STATE_REGISTERED |
3713 BINDER_LOOPER_STATE_ENTERED))
3714 binder_enqueue_work_ilocked(
3715 &death->work,
3716 &thread->todo);
3717 else {
3718 binder_enqueue_work_ilocked(
3719 &death->work,
3720 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003721 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07003722 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003723 }
3724 } else {
3725 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3726 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3727 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003728 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003729 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07003730 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003731 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003732 } break;
3733 case BC_DEAD_BINDER_DONE: {
3734 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003735 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003736 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003737
Arve Hjønnevågda498892014-02-21 14:40:26 -08003738 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003739 return -EFAULT;
3740
Lisa Du7a64cd82016-02-17 09:32:52 +08003741 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003742 binder_inner_proc_lock(proc);
3743 list_for_each_entry(w, &proc->delivered_death,
3744 entry) {
3745 struct binder_ref_death *tmp_death =
3746 container_of(w,
3747 struct binder_ref_death,
3748 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003749
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003750 if (tmp_death->cookie == cookie) {
3751 death = tmp_death;
3752 break;
3753 }
3754 }
3755 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003756 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3757 proc->pid, thread->pid, (u64)cookie,
3758 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003759 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003760 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3761 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003762 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003763 break;
3764 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003765 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003766 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3767 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003768 if (thread->looper &
3769 (BINDER_LOOPER_STATE_REGISTERED |
3770 BINDER_LOOPER_STATE_ENTERED))
3771 binder_enqueue_work_ilocked(
3772 &death->work, &thread->todo);
3773 else {
3774 binder_enqueue_work_ilocked(
3775 &death->work,
3776 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07003777 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003778 }
3779 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003780 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003781 } break;
3782
3783 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303784 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003785 proc->pid, thread->pid, cmd);
3786 return -EINVAL;
3787 }
3788 *consumed = ptr - buffer;
3789 }
3790 return 0;
3791}
3792
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003793static void binder_stat_br(struct binder_proc *proc,
3794 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003795{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003796 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003797 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003798 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3799 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3800 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003801 }
3802}
3803
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003804static int binder_has_thread_work(struct binder_thread *thread)
3805{
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003806 return !binder_worklist_empty(thread->proc, &thread->todo) ||
3807 thread->looper_need_return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003808}
3809
Todd Kjos60792612017-05-24 10:51:01 -07003810static int binder_put_node_cmd(struct binder_proc *proc,
3811 struct binder_thread *thread,
3812 void __user **ptrp,
3813 binder_uintptr_t node_ptr,
3814 binder_uintptr_t node_cookie,
3815 int node_debug_id,
3816 uint32_t cmd, const char *cmd_name)
3817{
3818 void __user *ptr = *ptrp;
3819
3820 if (put_user(cmd, (uint32_t __user *)ptr))
3821 return -EFAULT;
3822 ptr += sizeof(uint32_t);
3823
3824 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3825 return -EFAULT;
3826 ptr += sizeof(binder_uintptr_t);
3827
3828 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3829 return -EFAULT;
3830 ptr += sizeof(binder_uintptr_t);
3831
3832 binder_stat_br(proc, thread, cmd);
3833 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3834 proc->pid, thread->pid, cmd_name, node_debug_id,
3835 (u64)node_ptr, (u64)node_cookie);
3836
3837 *ptrp = ptr;
3838 return 0;
3839}
3840
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003841static int binder_wait_for_work(struct binder_thread *thread,
3842 bool do_proc_work)
3843{
3844 DEFINE_WAIT(wait);
3845 struct binder_proc *proc = thread->proc;
3846 int ret = 0;
3847
3848 freezer_do_not_count();
3849 binder_inner_proc_lock(proc);
3850 for (;;) {
3851 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3852 if (binder_has_work_ilocked(thread, do_proc_work))
3853 break;
3854 if (do_proc_work)
3855 list_add(&thread->waiting_thread_node,
3856 &proc->waiting_threads);
3857 binder_inner_proc_unlock(proc);
3858 schedule();
3859 binder_inner_proc_lock(proc);
3860 list_del_init(&thread->waiting_thread_node);
3861 if (signal_pending(current)) {
3862 ret = -ERESTARTSYS;
3863 break;
3864 }
3865 }
3866 finish_wait(&thread->wait, &wait);
3867 binder_inner_proc_unlock(proc);
3868 freezer_count();
3869
3870 return ret;
3871}
3872
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003873static int binder_thread_read(struct binder_proc *proc,
3874 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003875 binder_uintptr_t binder_buffer, size_t size,
3876 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003877{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003878 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003879 void __user *ptr = buffer + *consumed;
3880 void __user *end = buffer + size;
3881
3882 int ret = 0;
3883 int wait_for_proc_work;
3884
3885 if (*consumed == 0) {
3886 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3887 return -EFAULT;
3888 ptr += sizeof(uint32_t);
3889 }
3890
3891retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07003892 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003893 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003894 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003895
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003896 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003897
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003898 trace_binder_wait_for_work(wait_for_proc_work,
3899 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003900 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003901 if (wait_for_proc_work) {
3902 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3903 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303904 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 +09003905 proc->pid, thread->pid, thread->looper);
3906 wait_event_interruptible(binder_user_error_wait,
3907 binder_stop_on_user_error < 2);
3908 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07003909 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003910 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003911
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003912 if (non_block) {
3913 if (!binder_has_work(thread, wait_for_proc_work))
3914 ret = -EAGAIN;
3915 } else {
3916 ret = binder_wait_for_work(thread, wait_for_proc_work);
3917 }
3918
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003919 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3920
3921 if (ret)
3922 return ret;
3923
3924 while (1) {
3925 uint32_t cmd;
3926 struct binder_transaction_data tr;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003927 struct binder_work *w = NULL;
3928 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003929 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07003930 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003931
Todd Kjose7f23ed2017-03-21 13:06:01 -07003932 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003933 if (!binder_worklist_empty_ilocked(&thread->todo))
3934 list = &thread->todo;
3935 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3936 wait_for_proc_work)
3937 list = &proc->todo;
3938 else {
3939 binder_inner_proc_unlock(proc);
3940
Dmitry Voytik395262a2014-09-08 18:16:34 +04003941 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08003942 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003943 goto retry;
3944 break;
3945 }
3946
Todd Kjose7f23ed2017-03-21 13:06:01 -07003947 if (end - ptr < sizeof(tr) + 4) {
3948 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003949 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07003950 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003951 w = binder_dequeue_work_head_ilocked(list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003952
3953 switch (w->type) {
3954 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07003955 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003956 t = container_of(w, struct binder_transaction, work);
3957 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07003958 case BINDER_WORK_RETURN_ERROR: {
3959 struct binder_error *e = container_of(
3960 w, struct binder_error, work);
3961
3962 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07003963 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07003964 if (put_user(e->cmd, (uint32_t __user *)ptr))
3965 return -EFAULT;
3966 e->cmd = BR_OK;
3967 ptr += sizeof(uint32_t);
3968
3969 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07003970 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003971 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07003972 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003973 cmd = BR_TRANSACTION_COMPLETE;
3974 if (put_user(cmd, (uint32_t __user *)ptr))
3975 return -EFAULT;
3976 ptr += sizeof(uint32_t);
3977
3978 binder_stat_br(proc, thread, cmd);
3979 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303980 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003981 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003982 kfree(w);
3983 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3984 } break;
3985 case BINDER_WORK_NODE: {
3986 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07003987 int strong, weak;
3988 binder_uintptr_t node_ptr = node->ptr;
3989 binder_uintptr_t node_cookie = node->cookie;
3990 int node_debug_id = node->debug_id;
3991 int has_weak_ref;
3992 int has_strong_ref;
3993 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09003994
Todd Kjos60792612017-05-24 10:51:01 -07003995 BUG_ON(proc != node->proc);
3996 strong = node->internal_strong_refs ||
3997 node->local_strong_refs;
3998 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07003999 node->local_weak_refs ||
4000 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004001 has_strong_ref = node->has_strong_ref;
4002 has_weak_ref = node->has_weak_ref;
4003
4004 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004005 node->has_weak_ref = 1;
4006 node->pending_weak_ref = 1;
4007 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004008 }
4009 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004010 node->has_strong_ref = 1;
4011 node->pending_strong_ref = 1;
4012 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004013 }
4014 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004015 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004016 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004017 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004018 if (!weak && !strong) {
4019 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4020 "%d:%d node %d u%016llx c%016llx deleted\n",
4021 proc->pid, thread->pid,
4022 node_debug_id,
4023 (u64)node_ptr,
4024 (u64)node_cookie);
4025 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004026 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004027 binder_node_lock(node);
4028 /*
4029 * Acquire the node lock before freeing the
4030 * node to serialize with other threads that
4031 * may have been holding the node lock while
4032 * decrementing this node (avoids race where
4033 * this thread frees while the other thread
4034 * is unlocking the node after the final
4035 * decrement)
4036 */
4037 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004038 binder_free_node(node);
4039 } else
4040 binder_inner_proc_unlock(proc);
4041
Todd Kjos60792612017-05-24 10:51:01 -07004042 if (weak && !has_weak_ref)
4043 ret = binder_put_node_cmd(
4044 proc, thread, &ptr, node_ptr,
4045 node_cookie, node_debug_id,
4046 BR_INCREFS, "BR_INCREFS");
4047 if (!ret && strong && !has_strong_ref)
4048 ret = binder_put_node_cmd(
4049 proc, thread, &ptr, node_ptr,
4050 node_cookie, node_debug_id,
4051 BR_ACQUIRE, "BR_ACQUIRE");
4052 if (!ret && !strong && has_strong_ref)
4053 ret = binder_put_node_cmd(
4054 proc, thread, &ptr, node_ptr,
4055 node_cookie, node_debug_id,
4056 BR_RELEASE, "BR_RELEASE");
4057 if (!ret && !weak && has_weak_ref)
4058 ret = binder_put_node_cmd(
4059 proc, thread, &ptr, node_ptr,
4060 node_cookie, node_debug_id,
4061 BR_DECREFS, "BR_DECREFS");
4062 if (orig_ptr == ptr)
4063 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4064 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4065 proc->pid, thread->pid,
4066 node_debug_id,
4067 (u64)node_ptr,
4068 (u64)node_cookie);
4069 if (ret)
4070 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004071 } break;
4072 case BINDER_WORK_DEAD_BINDER:
4073 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4074 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4075 struct binder_ref_death *death;
4076 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004077 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004078
4079 death = container_of(w, struct binder_ref_death, work);
4080 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4081 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4082 else
4083 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004084 cookie = death->cookie;
4085
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004086 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004087 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004088 proc->pid, thread->pid,
4089 cmd == BR_DEAD_BINDER ?
4090 "BR_DEAD_BINDER" :
4091 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004092 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004093 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004094 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004095 kfree(death);
4096 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004097 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004098 binder_enqueue_work_ilocked(
4099 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004100 binder_inner_proc_unlock(proc);
4101 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004102 if (put_user(cmd, (uint32_t __user *)ptr))
4103 return -EFAULT;
4104 ptr += sizeof(uint32_t);
4105 if (put_user(cookie,
4106 (binder_uintptr_t __user *)ptr))
4107 return -EFAULT;
4108 ptr += sizeof(binder_uintptr_t);
4109 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004110 if (cmd == BR_DEAD_BINDER)
4111 goto done; /* DEAD_BINDER notifications can cause transactions */
4112 } break;
4113 }
4114
4115 if (!t)
4116 continue;
4117
4118 BUG_ON(t->buffer == NULL);
4119 if (t->buffer->target_node) {
4120 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004121 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004122
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004123 tr.target.ptr = target_node->ptr;
4124 tr.cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004125 node_prio.sched_policy = target_node->sched_policy;
4126 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004127 binder_transaction_priority(current, t, node_prio,
4128 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004129 cmd = BR_TRANSACTION;
4130 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004131 tr.target.ptr = 0;
4132 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004133 cmd = BR_REPLY;
4134 }
4135 tr.code = t->code;
4136 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06004137 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004138
Todd Kjos2f993e22017-05-12 14:42:55 -07004139 t_from = binder_get_txn_from(t);
4140 if (t_from) {
4141 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004142
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004143 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004144 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004145 } else {
4146 tr.sender_pid = 0;
4147 }
4148
4149 tr.data_size = t->buffer->data_size;
4150 tr.offsets_size = t->buffer->offsets_size;
Todd Kjosd325d372016-10-10 10:40:53 -07004151 tr.data.ptr.buffer = (binder_uintptr_t)
4152 ((uintptr_t)t->buffer->data +
4153 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004154 tr.data.ptr.offsets = tr.data.ptr.buffer +
4155 ALIGN(t->buffer->data_size,
4156 sizeof(void *));
4157
Todd Kjos2f993e22017-05-12 14:42:55 -07004158 if (put_user(cmd, (uint32_t __user *)ptr)) {
4159 if (t_from)
4160 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004161 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004162 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004163 ptr += sizeof(uint32_t);
Todd Kjos2f993e22017-05-12 14:42:55 -07004164 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4165 if (t_from)
4166 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004167 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004168 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004169 ptr += sizeof(tr);
4170
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004171 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004172 binder_stat_br(proc, thread, cmd);
4173 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004174 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004175 proc->pid, thread->pid,
4176 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4177 "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004178 t->debug_id, t_from ? t_from->proc->pid : 0,
4179 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004180 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004181 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004182
Todd Kjos2f993e22017-05-12 14:42:55 -07004183 if (t_from)
4184 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004185 t->buffer->allow_user_free = 1;
4186 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004187 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004188 t->to_parent = thread->transaction_stack;
4189 t->to_thread = thread;
4190 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004191 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004192 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004193 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004194 }
4195 break;
4196 }
4197
4198done:
4199
4200 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004201 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004202 if (proc->requested_threads == 0 &&
4203 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004204 proc->requested_threads_started < proc->max_threads &&
4205 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4206 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4207 /*spawn a new thread if we leave this out */) {
4208 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004209 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004210 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304211 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004212 proc->pid, thread->pid);
4213 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4214 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004215 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004216 } else
4217 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004218 return 0;
4219}
4220
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004221static void binder_release_work(struct binder_proc *proc,
4222 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004223{
4224 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004225
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004226 while (1) {
4227 w = binder_dequeue_work_head(proc, list);
4228 if (!w)
4229 return;
4230
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004231 switch (w->type) {
4232 case BINDER_WORK_TRANSACTION: {
4233 struct binder_transaction *t;
4234
4235 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004236 if (t->buffer->target_node &&
4237 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004238 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004239 } else {
4240 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304241 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004242 t->debug_id);
Todd Kjos21ef40a2017-03-30 18:02:13 -07004243 binder_free_transaction(t);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004244 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004245 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004246 case BINDER_WORK_RETURN_ERROR: {
4247 struct binder_error *e = container_of(
4248 w, struct binder_error, work);
4249
4250 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4251 "undelivered TRANSACTION_ERROR: %u\n",
4252 e->cmd);
4253 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004254 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004255 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304256 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004257 kfree(w);
4258 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4259 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004260 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4261 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4262 struct binder_ref_death *death;
4263
4264 death = container_of(w, struct binder_ref_death, work);
4265 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004266 "undelivered death notification, %016llx\n",
4267 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004268 kfree(death);
4269 binder_stats_deleted(BINDER_STAT_DEATH);
4270 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004271 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304272 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004273 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004274 break;
4275 }
4276 }
4277
4278}
4279
Todd Kjosb4827902017-05-25 15:52:17 -07004280static struct binder_thread *binder_get_thread_ilocked(
4281 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004282{
4283 struct binder_thread *thread = NULL;
4284 struct rb_node *parent = NULL;
4285 struct rb_node **p = &proc->threads.rb_node;
4286
4287 while (*p) {
4288 parent = *p;
4289 thread = rb_entry(parent, struct binder_thread, rb_node);
4290
4291 if (current->pid < thread->pid)
4292 p = &(*p)->rb_left;
4293 else if (current->pid > thread->pid)
4294 p = &(*p)->rb_right;
4295 else
Todd Kjosb4827902017-05-25 15:52:17 -07004296 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004297 }
Todd Kjosb4827902017-05-25 15:52:17 -07004298 if (!new_thread)
4299 return NULL;
4300 thread = new_thread;
4301 binder_stats_created(BINDER_STAT_THREAD);
4302 thread->proc = proc;
4303 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004304 get_task_struct(current);
4305 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004306 atomic_set(&thread->tmp_ref, 0);
4307 init_waitqueue_head(&thread->wait);
4308 INIT_LIST_HEAD(&thread->todo);
4309 rb_link_node(&thread->rb_node, parent, p);
4310 rb_insert_color(&thread->rb_node, &proc->threads);
4311 thread->looper_need_return = true;
4312 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4313 thread->return_error.cmd = BR_OK;
4314 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4315 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004316 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004317 return thread;
4318}
4319
4320static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4321{
4322 struct binder_thread *thread;
4323 struct binder_thread *new_thread;
4324
4325 binder_inner_proc_lock(proc);
4326 thread = binder_get_thread_ilocked(proc, NULL);
4327 binder_inner_proc_unlock(proc);
4328 if (!thread) {
4329 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4330 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004331 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004332 binder_inner_proc_lock(proc);
4333 thread = binder_get_thread_ilocked(proc, new_thread);
4334 binder_inner_proc_unlock(proc);
4335 if (thread != new_thread)
4336 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004337 }
4338 return thread;
4339}
4340
Todd Kjos2f993e22017-05-12 14:42:55 -07004341static void binder_free_proc(struct binder_proc *proc)
4342{
4343 BUG_ON(!list_empty(&proc->todo));
4344 BUG_ON(!list_empty(&proc->delivered_death));
4345 binder_alloc_deferred_release(&proc->alloc);
4346 put_task_struct(proc->tsk);
4347 binder_stats_deleted(BINDER_STAT_PROC);
4348 kfree(proc);
4349}
4350
4351static void binder_free_thread(struct binder_thread *thread)
4352{
4353 BUG_ON(!list_empty(&thread->todo));
4354 binder_stats_deleted(BINDER_STAT_THREAD);
4355 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004356 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004357 kfree(thread);
4358}
4359
4360static int binder_thread_release(struct binder_proc *proc,
4361 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004362{
4363 struct binder_transaction *t;
4364 struct binder_transaction *send_reply = NULL;
4365 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004366 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004367
Todd Kjosb4827902017-05-25 15:52:17 -07004368 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004369 /*
4370 * take a ref on the proc so it survives
4371 * after we remove this thread from proc->threads.
4372 * The corresponding dec is when we actually
4373 * free the thread in binder_free_thread()
4374 */
4375 proc->tmp_ref++;
4376 /*
4377 * take a ref on this thread to ensure it
4378 * survives while we are releasing it
4379 */
4380 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004381 rb_erase(&thread->rb_node, &proc->threads);
4382 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004383 if (t) {
4384 spin_lock(&t->lock);
4385 if (t->to_thread == thread)
4386 send_reply = t;
4387 }
4388 thread->is_dead = true;
4389
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004390 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004391 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004392 active_transactions++;
4393 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304394 "release %d:%d transaction %d %s, still active\n",
4395 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004396 t->debug_id,
4397 (t->to_thread == thread) ? "in" : "out");
4398
4399 if (t->to_thread == thread) {
4400 t->to_proc = NULL;
4401 t->to_thread = NULL;
4402 if (t->buffer) {
4403 t->buffer->transaction = NULL;
4404 t->buffer = NULL;
4405 }
4406 t = t->to_parent;
4407 } else if (t->from == thread) {
4408 t->from = NULL;
4409 t = t->from_parent;
4410 } else
4411 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004412 spin_unlock(&last_t->lock);
4413 if (t)
4414 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004415 }
Todd Kjosb4827902017-05-25 15:52:17 -07004416 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004417
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004418 if (send_reply)
4419 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004420 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004421 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004422 return active_transactions;
4423}
4424
4425static unsigned int binder_poll(struct file *filp,
4426 struct poll_table_struct *wait)
4427{
4428 struct binder_proc *proc = filp->private_data;
4429 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004430 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004431
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004432 thread = binder_get_thread(proc);
4433
Martijn Coenen995a36e2017-06-02 13:36:52 -07004434 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004435 thread->looper |= BINDER_LOOPER_STATE_POLL;
4436 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4437
Martijn Coenen995a36e2017-06-02 13:36:52 -07004438 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004439
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004440 if (binder_has_work(thread, wait_for_proc_work))
4441 return POLLIN;
4442
4443 poll_wait(filp, &thread->wait, wait);
4444
4445 if (binder_has_thread_work(thread))
4446 return POLLIN;
4447
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004448 return 0;
4449}
4450
Tair Rzayev78260ac2014-06-03 22:27:21 +03004451static int binder_ioctl_write_read(struct file *filp,
4452 unsigned int cmd, unsigned long arg,
4453 struct binder_thread *thread)
4454{
4455 int ret = 0;
4456 struct binder_proc *proc = filp->private_data;
4457 unsigned int size = _IOC_SIZE(cmd);
4458 void __user *ubuf = (void __user *)arg;
4459 struct binder_write_read bwr;
4460
4461 if (size != sizeof(struct binder_write_read)) {
4462 ret = -EINVAL;
4463 goto out;
4464 }
4465 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4466 ret = -EFAULT;
4467 goto out;
4468 }
4469 binder_debug(BINDER_DEBUG_READ_WRITE,
4470 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4471 proc->pid, thread->pid,
4472 (u64)bwr.write_size, (u64)bwr.write_buffer,
4473 (u64)bwr.read_size, (u64)bwr.read_buffer);
4474
4475 if (bwr.write_size > 0) {
4476 ret = binder_thread_write(proc, thread,
4477 bwr.write_buffer,
4478 bwr.write_size,
4479 &bwr.write_consumed);
4480 trace_binder_write_done(ret);
4481 if (ret < 0) {
4482 bwr.read_consumed = 0;
4483 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4484 ret = -EFAULT;
4485 goto out;
4486 }
4487 }
4488 if (bwr.read_size > 0) {
4489 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4490 bwr.read_size,
4491 &bwr.read_consumed,
4492 filp->f_flags & O_NONBLOCK);
4493 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004494 binder_inner_proc_lock(proc);
4495 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004496 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004497 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004498 if (ret < 0) {
4499 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4500 ret = -EFAULT;
4501 goto out;
4502 }
4503 }
4504 binder_debug(BINDER_DEBUG_READ_WRITE,
4505 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4506 proc->pid, thread->pid,
4507 (u64)bwr.write_consumed, (u64)bwr.write_size,
4508 (u64)bwr.read_consumed, (u64)bwr.read_size);
4509 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4510 ret = -EFAULT;
4511 goto out;
4512 }
4513out:
4514 return ret;
4515}
4516
4517static int binder_ioctl_set_ctx_mgr(struct file *filp)
4518{
4519 int ret = 0;
4520 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004521 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004522 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004523 kuid_t curr_euid = current_euid();
4524
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004525 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004526 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004527 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4528 ret = -EBUSY;
4529 goto out;
4530 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004531 ret = security_binder_set_context_mgr(proc->tsk);
4532 if (ret < 0)
4533 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004534 if (uid_valid(context->binder_context_mgr_uid)) {
4535 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004536 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4537 from_kuid(&init_user_ns, curr_euid),
4538 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004539 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004540 ret = -EPERM;
4541 goto out;
4542 }
4543 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004544 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004545 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004546 new_node = binder_new_node(proc, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004547 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004548 ret = -ENOMEM;
4549 goto out;
4550 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004551 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004552 new_node->local_weak_refs++;
4553 new_node->local_strong_refs++;
4554 new_node->has_strong_ref = 1;
4555 new_node->has_weak_ref = 1;
4556 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004557 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004558 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004559out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004560 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004561 return ret;
4562}
4563
Colin Cross833babb32017-06-20 13:54:44 -07004564static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4565 struct binder_node_debug_info *info) {
4566 struct rb_node *n;
4567 binder_uintptr_t ptr = info->ptr;
4568
4569 memset(info, 0, sizeof(*info));
4570
4571 binder_inner_proc_lock(proc);
4572 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4573 struct binder_node *node = rb_entry(n, struct binder_node,
4574 rb_node);
4575 if (node->ptr > ptr) {
4576 info->ptr = node->ptr;
4577 info->cookie = node->cookie;
4578 info->has_strong_ref = node->has_strong_ref;
4579 info->has_weak_ref = node->has_weak_ref;
4580 break;
4581 }
4582 }
4583 binder_inner_proc_unlock(proc);
4584
4585 return 0;
4586}
4587
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004588static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4589{
4590 int ret;
4591 struct binder_proc *proc = filp->private_data;
4592 struct binder_thread *thread;
4593 unsigned int size = _IOC_SIZE(cmd);
4594 void __user *ubuf = (void __user *)arg;
4595
Tair Rzayev78260ac2014-06-03 22:27:21 +03004596 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4597 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004598
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004599 trace_binder_ioctl(cmd, arg);
4600
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004601 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4602 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004603 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004604
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004605 thread = binder_get_thread(proc);
4606 if (thread == NULL) {
4607 ret = -ENOMEM;
4608 goto err;
4609 }
4610
4611 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004612 case BINDER_WRITE_READ:
4613 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4614 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004615 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004616 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004617 case BINDER_SET_MAX_THREADS: {
4618 int max_threads;
4619
4620 if (copy_from_user(&max_threads, ubuf,
4621 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004622 ret = -EINVAL;
4623 goto err;
4624 }
Todd Kjosd600e902017-05-25 17:35:02 -07004625 binder_inner_proc_lock(proc);
4626 proc->max_threads = max_threads;
4627 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004628 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004629 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004630 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004631 ret = binder_ioctl_set_ctx_mgr(filp);
4632 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004633 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004634 break;
4635 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304636 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004637 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07004638 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004639 thread = NULL;
4640 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004641 case BINDER_VERSION: {
4642 struct binder_version __user *ver = ubuf;
4643
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004644 if (size != sizeof(struct binder_version)) {
4645 ret = -EINVAL;
4646 goto err;
4647 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004648 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4649 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004650 ret = -EINVAL;
4651 goto err;
4652 }
4653 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004654 }
Colin Cross833babb32017-06-20 13:54:44 -07004655 case BINDER_GET_NODE_DEBUG_INFO: {
4656 struct binder_node_debug_info info;
4657
4658 if (copy_from_user(&info, ubuf, sizeof(info))) {
4659 ret = -EFAULT;
4660 goto err;
4661 }
4662
4663 ret = binder_ioctl_get_node_debug_info(proc, &info);
4664 if (ret < 0)
4665 goto err;
4666
4667 if (copy_to_user(ubuf, &info, sizeof(info))) {
4668 ret = -EFAULT;
4669 goto err;
4670 }
4671 break;
4672 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004673 default:
4674 ret = -EINVAL;
4675 goto err;
4676 }
4677 ret = 0;
4678err:
4679 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08004680 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004681 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4682 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304683 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 -07004684err_unlocked:
4685 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004686 return ret;
4687}
4688
4689static void binder_vma_open(struct vm_area_struct *vma)
4690{
4691 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004692
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004693 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304694 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004695 proc->pid, vma->vm_start, vma->vm_end,
4696 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4697 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004698}
4699
4700static void binder_vma_close(struct vm_area_struct *vma)
4701{
4702 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004703
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004704 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304705 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004706 proc->pid, vma->vm_start, vma->vm_end,
4707 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4708 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07004709 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004710 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4711}
4712
Vinayak Menonddac7d52014-06-02 18:17:59 +05304713static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4714{
4715 return VM_FAULT_SIGBUS;
4716}
4717
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004718static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004719 .open = binder_vma_open,
4720 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304721 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004722};
4723
Todd Kjosd325d372016-10-10 10:40:53 -07004724static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4725{
4726 int ret;
4727 struct binder_proc *proc = filp->private_data;
4728 const char *failure_string;
4729
4730 if (proc->tsk != current->group_leader)
4731 return -EINVAL;
4732
4733 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4734 vma->vm_end = vma->vm_start + SZ_4M;
4735
4736 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4737 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4738 __func__, proc->pid, vma->vm_start, vma->vm_end,
4739 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4740 (unsigned long)pgprot_val(vma->vm_page_prot));
4741
4742 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4743 ret = -EPERM;
4744 failure_string = "bad vm_flags";
4745 goto err_bad_arg;
4746 }
4747 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4748 vma->vm_ops = &binder_vm_ops;
4749 vma->vm_private_data = proc;
4750
4751 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4752 if (ret)
4753 return ret;
4754 proc->files = get_files_struct(current);
4755 return 0;
4756
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004757err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004758 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004759 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4760 return ret;
4761}
4762
4763static int binder_open(struct inode *nodp, struct file *filp)
4764{
4765 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004766 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004767
4768 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4769 current->group_leader->pid, current->pid);
4770
4771 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4772 if (proc == NULL)
4773 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07004774 spin_lock_init(&proc->inner_lock);
4775 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01004776 get_task_struct(current->group_leader);
4777 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004778 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07004779 if (binder_supported_policy(current->policy)) {
4780 proc->default_priority.sched_policy = current->policy;
4781 proc->default_priority.prio = current->normal_prio;
4782 } else {
4783 proc->default_priority.sched_policy = SCHED_NORMAL;
4784 proc->default_priority.prio = NICE_TO_PRIO(0);
4785 }
4786
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004787 binder_dev = container_of(filp->private_data, struct binder_device,
4788 miscdev);
4789 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07004790 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004791
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004792 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004793 proc->pid = current->group_leader->pid;
4794 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004795 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004796 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004797
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004798 mutex_lock(&binder_procs_lock);
4799 hlist_add_head(&proc->proc_node, &binder_procs);
4800 mutex_unlock(&binder_procs_lock);
4801
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004802 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004803 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004804
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004805 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004806 /*
4807 * proc debug entries are shared between contexts, so
4808 * this will fail if the process tries to open the driver
4809 * again with a different context. The priting code will
4810 * anyway print all contexts that a given PID has, so this
4811 * is not a problem.
4812 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004813 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004814 binder_debugfs_dir_entry_proc,
4815 (void *)(unsigned long)proc->pid,
4816 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004817 }
4818
4819 return 0;
4820}
4821
4822static int binder_flush(struct file *filp, fl_owner_t id)
4823{
4824 struct binder_proc *proc = filp->private_data;
4825
4826 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4827
4828 return 0;
4829}
4830
4831static void binder_deferred_flush(struct binder_proc *proc)
4832{
4833 struct rb_node *n;
4834 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004835
Todd Kjosb4827902017-05-25 15:52:17 -07004836 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004837 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4838 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004839
Todd Kjos6798e6d2017-01-06 14:19:25 -08004840 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004841 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4842 wake_up_interruptible(&thread->wait);
4843 wake_count++;
4844 }
4845 }
Todd Kjosb4827902017-05-25 15:52:17 -07004846 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004847
4848 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4849 "binder_flush: %d woke %d threads\n", proc->pid,
4850 wake_count);
4851}
4852
4853static int binder_release(struct inode *nodp, struct file *filp)
4854{
4855 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004856
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004857 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004858 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4859
4860 return 0;
4861}
4862
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004863static int binder_node_release(struct binder_node *node, int refs)
4864{
4865 struct binder_ref *ref;
4866 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004867 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004868
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004869 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004870
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004871 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004872 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004873 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07004874 /*
4875 * The caller must have taken a temporary ref on the node,
4876 */
4877 BUG_ON(!node->tmp_refs);
4878 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004879 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004880 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004881 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004882
4883 return refs;
4884 }
4885
4886 node->proc = NULL;
4887 node->local_strong_refs = 0;
4888 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004889 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004890
4891 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004892 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004893 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004894
4895 hlist_for_each_entry(ref, &node->refs, node_entry) {
4896 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004897 /*
4898 * Need the node lock to synchronize
4899 * with new notification requests and the
4900 * inner lock to synchronize with queued
4901 * death notifications.
4902 */
4903 binder_inner_proc_lock(ref->proc);
4904 if (!ref->death) {
4905 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08004906 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004907 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004908
4909 death++;
4910
Martijn Coenenf9eac642017-05-22 11:26:23 -07004911 BUG_ON(!list_empty(&ref->death->work.entry));
4912 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4913 binder_enqueue_work_ilocked(&ref->death->work,
4914 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07004915 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004916 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004917 }
4918
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004919 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4920 "node %d now dead, refs %d, death %d\n",
4921 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004922 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004923 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004924
4925 return refs;
4926}
4927
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004928static void binder_deferred_release(struct binder_proc *proc)
4929{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004930 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004931 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07004932 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004933
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004934 BUG_ON(proc->files);
4935
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004936 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004937 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004938 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004939
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004940 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004941 if (context->binder_context_mgr_node &&
4942 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004943 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004944 "%s: %d context_mgr_node gone\n",
4945 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004946 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004947 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004948 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07004949 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004950 /*
4951 * Make sure proc stays alive after we
4952 * remove all the threads
4953 */
4954 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004955
Todd Kjos2f993e22017-05-12 14:42:55 -07004956 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004957 threads = 0;
4958 active_transactions = 0;
4959 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004960 struct binder_thread *thread;
4961
4962 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004963 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004964 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07004965 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07004966 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004967 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004968
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004969 nodes = 0;
4970 incoming_refs = 0;
4971 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004972 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004973
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004974 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004975 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07004976 /*
4977 * take a temporary ref on the node before
4978 * calling binder_node_release() which will either
4979 * kfree() the node or call binder_put_node()
4980 */
Todd Kjos425d23f2017-06-12 12:07:26 -07004981 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004982 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07004983 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004984 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07004985 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004986 }
Todd Kjos425d23f2017-06-12 12:07:26 -07004987 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004988
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004989 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07004990 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004991 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004992 struct binder_ref *ref;
4993
4994 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004995 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07004996 binder_cleanup_ref_olocked(ref);
4997 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07004998 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07004999 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005000 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005001 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005002
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005003 binder_release_work(proc, &proc->todo);
5004 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005005
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005006 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005007 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005008 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005009 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005010
Todd Kjos2f993e22017-05-12 14:42:55 -07005011 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005012}
5013
5014static void binder_deferred_func(struct work_struct *work)
5015{
5016 struct binder_proc *proc;
5017 struct files_struct *files;
5018
5019 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005020
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005021 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005022 mutex_lock(&binder_deferred_lock);
5023 if (!hlist_empty(&binder_deferred_list)) {
5024 proc = hlist_entry(binder_deferred_list.first,
5025 struct binder_proc, deferred_work_node);
5026 hlist_del_init(&proc->deferred_work_node);
5027 defer = proc->deferred_work;
5028 proc->deferred_work = 0;
5029 } else {
5030 proc = NULL;
5031 defer = 0;
5032 }
5033 mutex_unlock(&binder_deferred_lock);
5034
5035 files = NULL;
5036 if (defer & BINDER_DEFERRED_PUT_FILES) {
5037 files = proc->files;
5038 if (files)
5039 proc->files = NULL;
5040 }
5041
5042 if (defer & BINDER_DEFERRED_FLUSH)
5043 binder_deferred_flush(proc);
5044
5045 if (defer & BINDER_DEFERRED_RELEASE)
5046 binder_deferred_release(proc); /* frees proc */
5047
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005048 if (files)
5049 put_files_struct(files);
5050 } while (proc);
5051}
5052static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5053
5054static void
5055binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5056{
5057 mutex_lock(&binder_deferred_lock);
5058 proc->deferred_work |= defer;
5059 if (hlist_unhashed(&proc->deferred_work_node)) {
5060 hlist_add_head(&proc->deferred_work_node,
5061 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305062 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005063 }
5064 mutex_unlock(&binder_deferred_lock);
5065}
5066
Todd Kjos6d241a42017-04-21 14:32:11 -07005067static void print_binder_transaction_ilocked(struct seq_file *m,
5068 struct binder_proc *proc,
5069 const char *prefix,
5070 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005071{
Todd Kjos6d241a42017-04-21 14:32:11 -07005072 struct binder_proc *to_proc;
5073 struct binder_buffer *buffer = t->buffer;
5074
Todd Kjos2f993e22017-05-12 14:42:55 -07005075 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005076 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005077 seq_printf(m,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005078 "%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 -07005079 prefix, t->debug_id, t,
5080 t->from ? t->from->proc->pid : 0,
5081 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005082 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005083 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005084 t->code, t->flags, t->priority.sched_policy,
5085 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005086 spin_unlock(&t->lock);
5087
Todd Kjos6d241a42017-04-21 14:32:11 -07005088 if (proc != to_proc) {
5089 /*
5090 * Can only safely deref buffer if we are holding the
5091 * correct proc inner lock for this node
5092 */
5093 seq_puts(m, "\n");
5094 return;
5095 }
5096
5097 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005098 seq_puts(m, " buffer free\n");
5099 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005100 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005101 if (buffer->target_node)
5102 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005103 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005104 buffer->data_size, buffer->offsets_size,
5105 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005106}
5107
Todd Kjos6d241a42017-04-21 14:32:11 -07005108static void print_binder_work_ilocked(struct seq_file *m,
5109 struct binder_proc *proc,
5110 const char *prefix,
5111 const char *transaction_prefix,
5112 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005113{
5114 struct binder_node *node;
5115 struct binder_transaction *t;
5116
5117 switch (w->type) {
5118 case BINDER_WORK_TRANSACTION:
5119 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005120 print_binder_transaction_ilocked(
5121 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005122 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005123 case BINDER_WORK_RETURN_ERROR: {
5124 struct binder_error *e = container_of(
5125 w, struct binder_error, work);
5126
5127 seq_printf(m, "%stransaction error: %u\n",
5128 prefix, e->cmd);
5129 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005130 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005131 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005132 break;
5133 case BINDER_WORK_NODE:
5134 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005135 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5136 prefix, node->debug_id,
5137 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005138 break;
5139 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005140 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005141 break;
5142 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005143 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005144 break;
5145 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005146 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005147 break;
5148 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005149 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005150 break;
5151 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005152}
5153
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005154static void print_binder_thread_ilocked(struct seq_file *m,
5155 struct binder_thread *thread,
5156 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005157{
5158 struct binder_transaction *t;
5159 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005160 size_t start_pos = m->count;
5161 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005162
Todd Kjos2f993e22017-05-12 14:42:55 -07005163 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005164 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005165 thread->looper_need_return,
5166 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005167 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005168 t = thread->transaction_stack;
5169 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005170 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005171 print_binder_transaction_ilocked(m, thread->proc,
5172 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005173 t = t->from_parent;
5174 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005175 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005176 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005177 t = t->to_parent;
5178 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005179 print_binder_transaction_ilocked(m, thread->proc,
5180 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005181 t = NULL;
5182 }
5183 }
5184 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005185 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005186 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005187 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005188 if (!print_always && m->count == header_pos)
5189 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005190}
5191
Todd Kjos425d23f2017-06-12 12:07:26 -07005192static void print_binder_node_nilocked(struct seq_file *m,
5193 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005194{
5195 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005196 struct binder_work *w;
5197 int count;
5198
5199 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005200 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005201 count++;
5202
Martijn Coenen6aac9792017-06-07 09:29:14 -07005203 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 -08005204 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005205 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005206 node->has_strong_ref, node->has_weak_ref,
5207 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005208 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005209 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005210 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005211 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005212 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005213 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005214 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005215 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005216 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005217 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005218 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005219 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005220}
5221
Todd Kjos5346bf32016-10-20 16:43:34 -07005222static void print_binder_ref_olocked(struct seq_file *m,
5223 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005224{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005225 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005226 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5227 ref->data.debug_id, ref->data.desc,
5228 ref->node->proc ? "" : "dead ",
5229 ref->node->debug_id, ref->data.strong,
5230 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005231 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005232}
5233
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005234static void print_binder_proc(struct seq_file *m,
5235 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005236{
5237 struct binder_work *w;
5238 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005239 size_t start_pos = m->count;
5240 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005241 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005242
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005243 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005244 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005245 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005246
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005247 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005248 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005249 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005250 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005251
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005252 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005253 struct binder_node *node = rb_entry(n, struct binder_node,
5254 rb_node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005255 /*
5256 * take a temporary reference on the node so it
5257 * survives and isn't removed from the tree
5258 * while we print it.
5259 */
5260 binder_inc_node_tmpref_ilocked(node);
5261 /* Need to drop inner lock to take node lock */
5262 binder_inner_proc_unlock(proc);
5263 if (last_node)
5264 binder_put_node(last_node);
5265 binder_node_inner_lock(node);
5266 print_binder_node_nilocked(m, node);
5267 binder_node_inner_unlock(node);
5268 last_node = node;
5269 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005270 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005271 binder_inner_proc_unlock(proc);
5272 if (last_node)
5273 binder_put_node(last_node);
5274
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005275 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005276 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005277 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005278 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005279 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005280 print_binder_ref_olocked(m, rb_entry(n,
5281 struct binder_ref,
5282 rb_node_desc));
5283 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005284 }
Todd Kjosd325d372016-10-10 10:40:53 -07005285 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005286 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005287 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005288 print_binder_work_ilocked(m, proc, " ",
5289 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005290 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005291 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005292 break;
5293 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005294 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005295 if (!print_all && m->count == header_pos)
5296 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005297}
5298
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005299static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005300 "BR_ERROR",
5301 "BR_OK",
5302 "BR_TRANSACTION",
5303 "BR_REPLY",
5304 "BR_ACQUIRE_RESULT",
5305 "BR_DEAD_REPLY",
5306 "BR_TRANSACTION_COMPLETE",
5307 "BR_INCREFS",
5308 "BR_ACQUIRE",
5309 "BR_RELEASE",
5310 "BR_DECREFS",
5311 "BR_ATTEMPT_ACQUIRE",
5312 "BR_NOOP",
5313 "BR_SPAWN_LOOPER",
5314 "BR_FINISHED",
5315 "BR_DEAD_BINDER",
5316 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5317 "BR_FAILED_REPLY"
5318};
5319
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005320static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005321 "BC_TRANSACTION",
5322 "BC_REPLY",
5323 "BC_ACQUIRE_RESULT",
5324 "BC_FREE_BUFFER",
5325 "BC_INCREFS",
5326 "BC_ACQUIRE",
5327 "BC_RELEASE",
5328 "BC_DECREFS",
5329 "BC_INCREFS_DONE",
5330 "BC_ACQUIRE_DONE",
5331 "BC_ATTEMPT_ACQUIRE",
5332 "BC_REGISTER_LOOPER",
5333 "BC_ENTER_LOOPER",
5334 "BC_EXIT_LOOPER",
5335 "BC_REQUEST_DEATH_NOTIFICATION",
5336 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005337 "BC_DEAD_BINDER_DONE",
5338 "BC_TRANSACTION_SG",
5339 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005340};
5341
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005342static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005343 "proc",
5344 "thread",
5345 "node",
5346 "ref",
5347 "death",
5348 "transaction",
5349 "transaction_complete"
5350};
5351
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005352static void print_binder_stats(struct seq_file *m, const char *prefix,
5353 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005354{
5355 int i;
5356
5357 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005358 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005359 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005360 int temp = atomic_read(&stats->bc[i]);
5361
5362 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005363 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005364 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005365 }
5366
5367 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005368 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005369 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005370 int temp = atomic_read(&stats->br[i]);
5371
5372 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005373 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005374 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005375 }
5376
5377 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005378 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005379 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005380 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005381 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005382 int created = atomic_read(&stats->obj_created[i]);
5383 int deleted = atomic_read(&stats->obj_deleted[i]);
5384
5385 if (created || deleted)
5386 seq_printf(m, "%s%s: active %d total %d\n",
5387 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005388 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005389 created - deleted,
5390 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005391 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005392}
5393
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005394static void print_binder_proc_stats(struct seq_file *m,
5395 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005396{
5397 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005398 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005399 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005400 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005401 size_t free_async_space =
5402 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005403
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005404 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005405 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005406 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005407 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005408 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005409 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5410 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005411
5412 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5413 ready_threads++;
5414
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005415 seq_printf(m, " threads: %d\n", count);
5416 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005417 " ready threads %d\n"
5418 " free async space %zd\n", proc->requested_threads,
5419 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005420 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005421 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005422 count = 0;
5423 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5424 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005425 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005426 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005427 count = 0;
5428 strong = 0;
5429 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005430 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005431 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5432 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5433 rb_node_desc);
5434 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005435 strong += ref->data.strong;
5436 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005437 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005438 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005439 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005440
Todd Kjosd325d372016-10-10 10:40:53 -07005441 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005442 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005443
5444 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005445 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005446 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005447 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005448 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005449 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005450 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005451 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005452
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005453 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005454}
5455
5456
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005457static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005458{
5459 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005460 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005461 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005462
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005463 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005464
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005465 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005466 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005467 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005468 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5469 /*
5470 * take a temporary reference on the node so it
5471 * survives and isn't removed from the list
5472 * while we print it.
5473 */
5474 node->tmp_refs++;
5475 spin_unlock(&binder_dead_nodes_lock);
5476 if (last_node)
5477 binder_put_node(last_node);
5478 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005479 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005480 binder_node_unlock(node);
5481 last_node = node;
5482 spin_lock(&binder_dead_nodes_lock);
5483 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005484 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005485 if (last_node)
5486 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005487
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005488 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005489 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005490 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005491 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005492
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005493 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005494}
5495
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005496static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005497{
5498 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005499
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005500 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005501
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005502 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005503
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005504 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005505 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005506 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005507 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005508
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005509 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005510}
5511
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005512static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005513{
5514 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005515
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005516 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005517 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005518 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005519 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005520 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005521
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005522 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005523}
5524
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005525static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005526{
Riley Andrews83050a42016-02-09 21:05:33 -08005527 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005528 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005529
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005530 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005531 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005532 if (itr->pid == pid) {
5533 seq_puts(m, "binder proc state:\n");
5534 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005535 }
5536 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005537 mutex_unlock(&binder_procs_lock);
5538
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005539 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005540}
5541
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005542static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005543 struct binder_transaction_log_entry *e)
5544{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005545 int debug_id = READ_ONCE(e->debug_id_done);
5546 /*
5547 * read barrier to guarantee debug_id_done read before
5548 * we print the log values
5549 */
5550 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005551 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005552 "%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 -07005553 e->debug_id, (e->call_type == 2) ? "reply" :
5554 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005555 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005556 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5557 e->return_error, e->return_error_param,
5558 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005559 /*
5560 * read-barrier to guarantee read of debug_id_done after
5561 * done printing the fields of the entry
5562 */
5563 smp_rmb();
5564 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5565 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005566}
5567
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005568static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005569{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005570 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005571 unsigned int log_cur = atomic_read(&log->cur);
5572 unsigned int count;
5573 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005574 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005575
Todd Kjos1cfe6272017-05-24 13:33:28 -07005576 count = log_cur + 1;
5577 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5578 0 : count % ARRAY_SIZE(log->entry);
5579 if (count > ARRAY_SIZE(log->entry) || log->full)
5580 count = ARRAY_SIZE(log->entry);
5581 for (i = 0; i < count; i++) {
5582 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5583
5584 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005585 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005586 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005587}
5588
5589static const struct file_operations binder_fops = {
5590 .owner = THIS_MODULE,
5591 .poll = binder_poll,
5592 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005593 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005594 .mmap = binder_mmap,
5595 .open = binder_open,
5596 .flush = binder_flush,
5597 .release = binder_release,
5598};
5599
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005600BINDER_DEBUG_ENTRY(state);
5601BINDER_DEBUG_ENTRY(stats);
5602BINDER_DEBUG_ENTRY(transactions);
5603BINDER_DEBUG_ENTRY(transaction_log);
5604
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005605static int __init init_binder_device(const char *name)
5606{
5607 int ret;
5608 struct binder_device *binder_device;
5609
5610 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5611 if (!binder_device)
5612 return -ENOMEM;
5613
5614 binder_device->miscdev.fops = &binder_fops;
5615 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5616 binder_device->miscdev.name = name;
5617
5618 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5619 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005620 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005621
5622 ret = misc_register(&binder_device->miscdev);
5623 if (ret < 0) {
5624 kfree(binder_device);
5625 return ret;
5626 }
5627
5628 hlist_add_head(&binder_device->hlist, &binder_devices);
5629
5630 return ret;
5631}
5632
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005633static int __init binder_init(void)
5634{
5635 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005636 char *device_name, *device_names;
5637 struct binder_device *device;
5638 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005639
Todd Kjos1cfe6272017-05-24 13:33:28 -07005640 atomic_set(&binder_transaction_log.cur, ~0U);
5641 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5642
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005643 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5644 if (binder_debugfs_dir_entry_root)
5645 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5646 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005647
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005648 if (binder_debugfs_dir_entry_root) {
5649 debugfs_create_file("state",
5650 S_IRUGO,
5651 binder_debugfs_dir_entry_root,
5652 NULL,
5653 &binder_state_fops);
5654 debugfs_create_file("stats",
5655 S_IRUGO,
5656 binder_debugfs_dir_entry_root,
5657 NULL,
5658 &binder_stats_fops);
5659 debugfs_create_file("transactions",
5660 S_IRUGO,
5661 binder_debugfs_dir_entry_root,
5662 NULL,
5663 &binder_transactions_fops);
5664 debugfs_create_file("transaction_log",
5665 S_IRUGO,
5666 binder_debugfs_dir_entry_root,
5667 &binder_transaction_log,
5668 &binder_transaction_log_fops);
5669 debugfs_create_file("failed_transaction_log",
5670 S_IRUGO,
5671 binder_debugfs_dir_entry_root,
5672 &binder_transaction_log_failed,
5673 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005674 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005675
5676 /*
5677 * Copy the module_parameter string, because we don't want to
5678 * tokenize it in-place.
5679 */
5680 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5681 if (!device_names) {
5682 ret = -ENOMEM;
5683 goto err_alloc_device_names_failed;
5684 }
5685 strcpy(device_names, binder_devices_param);
5686
5687 while ((device_name = strsep(&device_names, ","))) {
5688 ret = init_binder_device(device_name);
5689 if (ret)
5690 goto err_init_binder_device_failed;
5691 }
5692
5693 return ret;
5694
5695err_init_binder_device_failed:
5696 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5697 misc_deregister(&device->miscdev);
5698 hlist_del(&device->hlist);
5699 kfree(device);
5700 }
5701err_alloc_device_names_failed:
5702 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5703
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005704 return ret;
5705}
5706
5707device_initcall(binder_init);
5708
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005709#define CREATE_TRACE_POINTS
5710#include "binder_trace.h"
5711
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005712MODULE_LICENSE("GPL v2");