blob: fcf85be2674d8c6500821da134a664374e4011eb [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 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002484
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002485 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002486 ret = binder_inc_ref_for_node(target_proc, node,
2487 fp->hdr.type == BINDER_TYPE_HANDLE,
2488 NULL, &dest_rdata);
2489 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002490 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002491
2492 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002493 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002494 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002495 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2496 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002497 binder_debug(BINDER_DEBUG_TRANSACTION,
2498 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002499 src_rdata.debug_id, src_rdata.desc,
2500 dest_rdata.debug_id, dest_rdata.desc,
2501 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002502 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002503done:
2504 binder_put_node(node);
2505 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002506}
2507
2508static int binder_translate_fd(int fd,
2509 struct binder_transaction *t,
2510 struct binder_thread *thread,
2511 struct binder_transaction *in_reply_to)
2512{
2513 struct binder_proc *proc = thread->proc;
2514 struct binder_proc *target_proc = t->to_proc;
2515 int target_fd;
2516 struct file *file;
2517 int ret;
2518 bool target_allows_fd;
2519
2520 if (in_reply_to)
2521 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2522 else
2523 target_allows_fd = t->buffer->target_node->accept_fds;
2524 if (!target_allows_fd) {
2525 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2526 proc->pid, thread->pid,
2527 in_reply_to ? "reply" : "transaction",
2528 fd);
2529 ret = -EPERM;
2530 goto err_fd_not_accepted;
2531 }
2532
2533 file = fget(fd);
2534 if (!file) {
2535 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2536 proc->pid, thread->pid, fd);
2537 ret = -EBADF;
2538 goto err_fget;
2539 }
2540 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2541 if (ret < 0) {
2542 ret = -EPERM;
2543 goto err_security;
2544 }
2545
2546 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2547 if (target_fd < 0) {
2548 ret = -ENOMEM;
2549 goto err_get_unused_fd;
2550 }
2551 task_fd_install(target_proc, target_fd, file);
2552 trace_binder_transaction_fd(t, fd, target_fd);
2553 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2554 fd, target_fd);
2555
2556 return target_fd;
2557
2558err_get_unused_fd:
2559err_security:
2560 fput(file);
2561err_fget:
2562err_fd_not_accepted:
2563 return ret;
2564}
2565
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002566static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2567 struct binder_buffer_object *parent,
2568 struct binder_transaction *t,
2569 struct binder_thread *thread,
2570 struct binder_transaction *in_reply_to)
2571{
2572 binder_size_t fdi, fd_buf_size, num_installed_fds;
2573 int target_fd;
2574 uintptr_t parent_buffer;
2575 u32 *fd_array;
2576 struct binder_proc *proc = thread->proc;
2577 struct binder_proc *target_proc = t->to_proc;
2578
2579 fd_buf_size = sizeof(u32) * fda->num_fds;
2580 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2581 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2582 proc->pid, thread->pid, (u64)fda->num_fds);
2583 return -EINVAL;
2584 }
2585 if (fd_buf_size > parent->length ||
2586 fda->parent_offset > parent->length - fd_buf_size) {
2587 /* No space for all file descriptors here. */
2588 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2589 proc->pid, thread->pid, (u64)fda->num_fds);
2590 return -EINVAL;
2591 }
2592 /*
2593 * Since the parent was already fixed up, convert it
2594 * back to the kernel address space to access it
2595 */
Todd Kjosd325d372016-10-10 10:40:53 -07002596 parent_buffer = parent->buffer -
2597 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002598 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2599 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2600 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2601 proc->pid, thread->pid);
2602 return -EINVAL;
2603 }
2604 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2605 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2606 in_reply_to);
2607 if (target_fd < 0)
2608 goto err_translate_fd_failed;
2609 fd_array[fdi] = target_fd;
2610 }
2611 return 0;
2612
2613err_translate_fd_failed:
2614 /*
2615 * Failed to allocate fd or security error, free fds
2616 * installed so far.
2617 */
2618 num_installed_fds = fdi;
2619 for (fdi = 0; fdi < num_installed_fds; fdi++)
2620 task_close_fd(target_proc, fd_array[fdi]);
2621 return target_fd;
2622}
2623
Martijn Coenen5a6da532016-09-30 14:10:07 +02002624static int binder_fixup_parent(struct binder_transaction *t,
2625 struct binder_thread *thread,
2626 struct binder_buffer_object *bp,
2627 binder_size_t *off_start,
2628 binder_size_t num_valid,
2629 struct binder_buffer_object *last_fixup_obj,
2630 binder_size_t last_fixup_min_off)
2631{
2632 struct binder_buffer_object *parent;
2633 u8 *parent_buffer;
2634 struct binder_buffer *b = t->buffer;
2635 struct binder_proc *proc = thread->proc;
2636 struct binder_proc *target_proc = t->to_proc;
2637
2638 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2639 return 0;
2640
2641 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2642 if (!parent) {
2643 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2644 proc->pid, thread->pid);
2645 return -EINVAL;
2646 }
2647
2648 if (!binder_validate_fixup(b, off_start,
2649 parent, bp->parent_offset,
2650 last_fixup_obj,
2651 last_fixup_min_off)) {
2652 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2653 proc->pid, thread->pid);
2654 return -EINVAL;
2655 }
2656
2657 if (parent->length < sizeof(binder_uintptr_t) ||
2658 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2659 /* No space for a pointer here! */
2660 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2661 proc->pid, thread->pid);
2662 return -EINVAL;
2663 }
2664 parent_buffer = (u8 *)(parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002665 binder_alloc_get_user_buffer_offset(
2666 &target_proc->alloc));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002667 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2668
2669 return 0;
2670}
2671
Martijn Coenen053be422017-06-06 15:17:46 -07002672/**
2673 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2674 * @t: transaction to send
2675 * @proc: process to send the transaction to
2676 * @thread: thread in @proc to send the transaction to (may be NULL)
2677 *
2678 * This function queues a transaction to the specified process. It will try
2679 * to find a thread in the target process to handle the transaction and
2680 * wake it up. If no thread is found, the work is queued to the proc
2681 * waitqueue.
2682 *
2683 * If the @thread parameter is not NULL, the transaction is always queued
2684 * to the waitlist of that specific thread.
2685 *
2686 * Return: true if the transactions was successfully queued
2687 * false if the target process or thread is dead
2688 */
2689static bool binder_proc_transaction(struct binder_transaction *t,
2690 struct binder_proc *proc,
2691 struct binder_thread *thread)
2692{
2693 struct list_head *target_list = NULL;
2694 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002695 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002696 bool oneway = !!(t->flags & TF_ONE_WAY);
2697 bool wakeup = true;
2698
2699 BUG_ON(!node);
2700 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002701 node_prio.prio = node->min_priority;
2702 node_prio.sched_policy = node->sched_policy;
2703
Martijn Coenen053be422017-06-06 15:17:46 -07002704 if (oneway) {
2705 BUG_ON(thread);
2706 if (node->has_async_transaction) {
2707 target_list = &node->async_todo;
2708 wakeup = false;
2709 } else {
2710 node->has_async_transaction = 1;
2711 }
2712 }
2713
2714 binder_inner_proc_lock(proc);
2715
2716 if (proc->is_dead || (thread && thread->is_dead)) {
2717 binder_inner_proc_unlock(proc);
2718 binder_node_unlock(node);
2719 return false;
2720 }
2721
2722 if (!thread && !target_list)
2723 thread = binder_select_thread_ilocked(proc);
2724
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002725 if (thread) {
Martijn Coenen053be422017-06-06 15:17:46 -07002726 target_list = &thread->todo;
Martijn Coenenc46810c2017-06-23 10:13:43 -07002727 binder_transaction_priority(thread->task, t, node_prio,
2728 node->inherit_rt);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002729 } else if (!target_list) {
Martijn Coenen053be422017-06-06 15:17:46 -07002730 target_list = &proc->todo;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002731 } else {
Martijn Coenen053be422017-06-06 15:17:46 -07002732 BUG_ON(target_list != &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002733 }
Martijn Coenen053be422017-06-06 15:17:46 -07002734
2735 binder_enqueue_work_ilocked(&t->work, target_list);
2736
2737 if (wakeup)
2738 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2739
2740 binder_inner_proc_unlock(proc);
2741 binder_node_unlock(node);
2742
2743 return true;
2744}
2745
Todd Kjos291d9682017-09-25 08:55:09 -07002746/**
2747 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2748 * @node: struct binder_node for which to get refs
2749 * @proc: returns @node->proc if valid
2750 * @error: if no @proc then returns BR_DEAD_REPLY
2751 *
2752 * User-space normally keeps the node alive when creating a transaction
2753 * since it has a reference to the target. The local strong ref keeps it
2754 * alive if the sending process dies before the target process processes
2755 * the transaction. If the source process is malicious or has a reference
2756 * counting bug, relying on the local strong ref can fail.
2757 *
2758 * Since user-space can cause the local strong ref to go away, we also take
2759 * a tmpref on the node to ensure it survives while we are constructing
2760 * the transaction. We also need a tmpref on the proc while we are
2761 * constructing the transaction, so we take that here as well.
2762 *
2763 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2764 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2765 * target proc has died, @error is set to BR_DEAD_REPLY
2766 */
2767static struct binder_node *binder_get_node_refs_for_txn(
2768 struct binder_node *node,
2769 struct binder_proc **procp,
2770 uint32_t *error)
2771{
2772 struct binder_node *target_node = NULL;
2773
2774 binder_node_inner_lock(node);
2775 if (node->proc) {
2776 target_node = node;
2777 binder_inc_node_nilocked(node, 1, 0, NULL);
2778 binder_inc_node_tmpref_ilocked(node);
2779 node->proc->tmp_ref++;
2780 *procp = node->proc;
2781 } else
2782 *error = BR_DEAD_REPLY;
2783 binder_node_inner_unlock(node);
2784
2785 return target_node;
2786}
2787
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002788static void binder_transaction(struct binder_proc *proc,
2789 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02002790 struct binder_transaction_data *tr, int reply,
2791 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002792{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002793 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002794 struct binder_transaction *t;
2795 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002796 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002797 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002798 u8 *sg_bufp, *sg_buf_end;
Todd Kjos2f993e22017-05-12 14:42:55 -07002799 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002800 struct binder_thread *target_thread = NULL;
2801 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002802 struct binder_transaction *in_reply_to = NULL;
2803 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07002804 uint32_t return_error = 0;
2805 uint32_t return_error_param = 0;
2806 uint32_t return_error_line = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002807 struct binder_buffer_object *last_fixup_obj = NULL;
2808 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002809 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07002810 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002811
2812 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07002813 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002814 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2815 e->from_proc = proc->pid;
2816 e->from_thread = thread->pid;
2817 e->target_handle = tr->target.handle;
2818 e->data_size = tr->data_size;
2819 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02002820 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002821
2822 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002823 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002824 in_reply_to = thread->transaction_stack;
2825 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002826 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302827 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002828 proc->pid, thread->pid);
2829 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002830 return_error_param = -EPROTO;
2831 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002832 goto err_empty_call_stack;
2833 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002834 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002835 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302836 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 +09002837 proc->pid, thread->pid, in_reply_to->debug_id,
2838 in_reply_to->to_proc ?
2839 in_reply_to->to_proc->pid : 0,
2840 in_reply_to->to_thread ?
2841 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002842 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002843 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002844 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002845 return_error_param = -EPROTO;
2846 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002847 in_reply_to = NULL;
2848 goto err_bad_call_stack;
2849 }
2850 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002851 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002852 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002853 if (target_thread == NULL) {
2854 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002855 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002856 goto err_dead_binder;
2857 }
2858 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302859 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 +09002860 proc->pid, thread->pid,
2861 target_thread->transaction_stack ?
2862 target_thread->transaction_stack->debug_id : 0,
2863 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002864 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002865 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002866 return_error_param = -EPROTO;
2867 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002868 in_reply_to = NULL;
2869 target_thread = NULL;
2870 goto err_dead_binder;
2871 }
2872 target_proc = target_thread->proc;
Todd Kjos2f993e22017-05-12 14:42:55 -07002873 target_proc->tmp_ref++;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002874 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002875 } else {
2876 if (tr->target.handle) {
2877 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002878
Todd Kjosc37162d2017-05-26 11:56:29 -07002879 /*
2880 * There must already be a strong ref
2881 * on this node. If so, do a strong
2882 * increment on the node to ensure it
2883 * stays alive until the transaction is
2884 * done.
2885 */
Todd Kjos5346bf32016-10-20 16:43:34 -07002886 binder_proc_lock(proc);
2887 ref = binder_get_ref_olocked(proc, tr->target.handle,
2888 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07002889 if (ref) {
Todd Kjos291d9682017-09-25 08:55:09 -07002890 target_node = binder_get_node_refs_for_txn(
2891 ref->node, &target_proc,
2892 &return_error);
2893 } else {
2894 binder_user_error("%d:%d got transaction to invalid handle\n",
2895 proc->pid, thread->pid);
2896 return_error = BR_FAILED_REPLY;
Todd Kjosc37162d2017-05-26 11:56:29 -07002897 }
Todd Kjos5346bf32016-10-20 16:43:34 -07002898 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002899 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002900 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002901 target_node = context->binder_context_mgr_node;
Todd Kjos291d9682017-09-25 08:55:09 -07002902 if (target_node)
2903 target_node = binder_get_node_refs_for_txn(
2904 target_node, &target_proc,
2905 &return_error);
2906 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002907 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002908 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002909 }
Todd Kjos291d9682017-09-25 08:55:09 -07002910 if (!target_node) {
2911 /*
2912 * return_error is set above
2913 */
2914 return_error_param = -EINVAL;
Todd Kjose598d172017-03-22 17:19:52 -07002915 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002916 goto err_dead_binder;
2917 }
Todd Kjos291d9682017-09-25 08:55:09 -07002918 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05002919 if (security_binder_transaction(proc->tsk,
2920 target_proc->tsk) < 0) {
2921 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002922 return_error_param = -EPERM;
2923 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05002924 goto err_invalid_target_handle;
2925 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002926 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002927 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2928 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002929
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002930 tmp = thread->transaction_stack;
2931 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002932 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302933 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 +09002934 proc->pid, thread->pid, tmp->debug_id,
2935 tmp->to_proc ? tmp->to_proc->pid : 0,
2936 tmp->to_thread ?
2937 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002938 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002939 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002940 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002941 return_error_param = -EPROTO;
2942 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002943 goto err_bad_call_stack;
2944 }
2945 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002946 struct binder_thread *from;
2947
2948 spin_lock(&tmp->lock);
2949 from = tmp->from;
2950 if (from && from->proc == target_proc) {
2951 atomic_inc(&from->tmp_ref);
2952 target_thread = from;
2953 spin_unlock(&tmp->lock);
2954 break;
2955 }
2956 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002957 tmp = tmp->from_parent;
2958 }
2959 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002960 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002961 }
Martijn Coenen053be422017-06-06 15:17:46 -07002962 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002963 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002964 e->to_proc = target_proc->pid;
2965
2966 /* TODO: reuse incoming transaction for reply */
2967 t = kzalloc(sizeof(*t), GFP_KERNEL);
2968 if (t == NULL) {
2969 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002970 return_error_param = -ENOMEM;
2971 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002972 goto err_alloc_t_failed;
2973 }
2974 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07002975 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002976
2977 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2978 if (tcomplete == NULL) {
2979 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002980 return_error_param = -ENOMEM;
2981 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002982 goto err_alloc_tcomplete_failed;
2983 }
2984 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2985
Todd Kjos1cfe6272017-05-24 13:33:28 -07002986 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002987
2988 if (reply)
2989 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02002990 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002991 proc->pid, thread->pid, t->debug_id,
2992 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002993 (u64)tr->data.ptr.buffer,
2994 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02002995 (u64)tr->data_size, (u64)tr->offsets_size,
2996 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002997 else
2998 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02002999 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003000 proc->pid, thread->pid, t->debug_id,
3001 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003002 (u64)tr->data.ptr.buffer,
3003 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003004 (u64)tr->data_size, (u64)tr->offsets_size,
3005 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003006
3007 if (!reply && !(tr->flags & TF_ONE_WAY))
3008 t->from = thread;
3009 else
3010 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03003011 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003012 t->to_proc = target_proc;
3013 t->to_thread = target_thread;
3014 t->code = tr->code;
3015 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003016 if (!(t->flags & TF_ONE_WAY) &&
3017 binder_supported_policy(current->policy)) {
3018 /* Inherit supported policies for synchronous transactions */
3019 t->priority.sched_policy = current->policy;
3020 t->priority.prio = current->normal_prio;
3021 } else {
3022 /* Otherwise, fall back to the default priority */
3023 t->priority = target_proc->default_priority;
3024 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003025
3026 trace_binder_transaction(reply, t, target_node);
3027
Todd Kjosd325d372016-10-10 10:40:53 -07003028 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02003029 tr->offsets_size, extra_buffers_size,
3030 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07003031 if (IS_ERR(t->buffer)) {
3032 /*
3033 * -ESRCH indicates VMA cleared. The target is dying.
3034 */
3035 return_error_param = PTR_ERR(t->buffer);
3036 return_error = return_error_param == -ESRCH ?
3037 BR_DEAD_REPLY : BR_FAILED_REPLY;
3038 return_error_line = __LINE__;
3039 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003040 goto err_binder_alloc_buf_failed;
3041 }
3042 t->buffer->allow_user_free = 0;
3043 t->buffer->debug_id = t->debug_id;
3044 t->buffer->transaction = t;
3045 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003046 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003047 off_start = (binder_size_t *)(t->buffer->data +
3048 ALIGN(tr->data_size, sizeof(void *)));
3049 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003050
Arve Hjønnevågda498892014-02-21 14:40:26 -08003051 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3052 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303053 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3054 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003055 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003056 return_error_param = -EFAULT;
3057 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003058 goto err_copy_data_failed;
3059 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003060 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3061 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303062 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3063 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003064 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003065 return_error_param = -EFAULT;
3066 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003067 goto err_copy_data_failed;
3068 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003069 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3070 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3071 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003072 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003073 return_error_param = -EINVAL;
3074 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003075 goto err_bad_offset;
3076 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003077 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3078 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3079 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303080 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003081 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003082 return_error_param = -EINVAL;
3083 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003084 goto err_bad_offset;
3085 }
3086 off_end = (void *)off_start + tr->offsets_size;
3087 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3088 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003089 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003090 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003091 struct binder_object_header *hdr;
3092 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09003093
Martijn Coenen00c80372016-07-13 12:06:49 +02003094 if (object_size == 0 || *offp < off_min) {
3095 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 -08003096 proc->pid, thread->pid, (u64)*offp,
3097 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003098 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003099 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003100 return_error_param = -EINVAL;
3101 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003102 goto err_bad_offset;
3103 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003104
3105 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3106 off_min = *offp + object_size;
3107 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003108 case BINDER_TYPE_BINDER:
3109 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003110 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003111
Martijn Coenen00c80372016-07-13 12:06:49 +02003112 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003113 ret = binder_translate_binder(fp, t, thread);
3114 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003115 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003116 return_error_param = ret;
3117 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003118 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003119 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003120 } break;
3121 case BINDER_TYPE_HANDLE:
3122 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003123 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003124
Martijn Coenen00c80372016-07-13 12:06:49 +02003125 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003126 ret = binder_translate_handle(fp, t, thread);
3127 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003128 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003129 return_error_param = ret;
3130 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003131 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003132 }
3133 } break;
3134
3135 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003136 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003137 int target_fd = binder_translate_fd(fp->fd, t, thread,
3138 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003139
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003140 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003141 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003142 return_error_param = target_fd;
3143 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003144 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003145 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003146 fp->pad_binder = 0;
3147 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003148 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003149 case BINDER_TYPE_FDA: {
3150 struct binder_fd_array_object *fda =
3151 to_binder_fd_array_object(hdr);
3152 struct binder_buffer_object *parent =
3153 binder_validate_ptr(t->buffer, fda->parent,
3154 off_start,
3155 offp - off_start);
3156 if (!parent) {
3157 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3158 proc->pid, thread->pid);
3159 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003160 return_error_param = -EINVAL;
3161 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003162 goto err_bad_parent;
3163 }
3164 if (!binder_validate_fixup(t->buffer, off_start,
3165 parent, fda->parent_offset,
3166 last_fixup_obj,
3167 last_fixup_min_off)) {
3168 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3169 proc->pid, thread->pid);
3170 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003171 return_error_param = -EINVAL;
3172 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003173 goto err_bad_parent;
3174 }
3175 ret = binder_translate_fd_array(fda, parent, t, thread,
3176 in_reply_to);
3177 if (ret < 0) {
3178 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003179 return_error_param = ret;
3180 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003181 goto err_translate_failed;
3182 }
3183 last_fixup_obj = parent;
3184 last_fixup_min_off =
3185 fda->parent_offset + sizeof(u32) * fda->num_fds;
3186 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003187 case BINDER_TYPE_PTR: {
3188 struct binder_buffer_object *bp =
3189 to_binder_buffer_object(hdr);
3190 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003191
Martijn Coenen5a6da532016-09-30 14:10:07 +02003192 if (bp->length > buf_left) {
3193 binder_user_error("%d:%d got transaction with too large buffer\n",
3194 proc->pid, thread->pid);
3195 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003196 return_error_param = -EINVAL;
3197 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003198 goto err_bad_offset;
3199 }
3200 if (copy_from_user(sg_bufp,
3201 (const void __user *)(uintptr_t)
3202 bp->buffer, bp->length)) {
3203 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3204 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003205 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003206 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003207 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003208 goto err_copy_data_failed;
3209 }
3210 /* Fixup buffer pointer to target proc address space */
3211 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjosd325d372016-10-10 10:40:53 -07003212 binder_alloc_get_user_buffer_offset(
3213 &target_proc->alloc);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003214 sg_bufp += ALIGN(bp->length, sizeof(u64));
3215
3216 ret = binder_fixup_parent(t, thread, bp, off_start,
3217 offp - off_start,
3218 last_fixup_obj,
3219 last_fixup_min_off);
3220 if (ret < 0) {
3221 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003222 return_error_param = ret;
3223 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003224 goto err_translate_failed;
3225 }
3226 last_fixup_obj = bp;
3227 last_fixup_min_off = 0;
3228 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003229 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003230 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003231 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003232 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003233 return_error_param = -EINVAL;
3234 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003235 goto err_bad_object_type;
3236 }
3237 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003238 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003239 binder_enqueue_work(proc, tcomplete, &thread->todo);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003240 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003241
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003242 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003243 binder_inner_proc_lock(target_proc);
3244 if (target_thread->is_dead) {
3245 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003246 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003247 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003248 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003249 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen053be422017-06-06 15:17:46 -07003250 binder_enqueue_work_ilocked(&t->work, &target_thread->todo);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003251 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003252 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003253 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003254 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003255 } else if (!(t->flags & TF_ONE_WAY)) {
3256 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003257 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003258 t->need_reply = 1;
3259 t->from_parent = thread->transaction_stack;
3260 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003261 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003262 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003263 binder_inner_proc_lock(proc);
3264 binder_pop_transaction_ilocked(thread, t);
3265 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003266 goto err_dead_proc_or_thread;
3267 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003268 } else {
3269 BUG_ON(target_node == NULL);
3270 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen053be422017-06-06 15:17:46 -07003271 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003272 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003273 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003274 if (target_thread)
3275 binder_thread_dec_tmpref(target_thread);
3276 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003277 if (target_node)
3278 binder_dec_node_tmpref(target_node);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003279 /*
3280 * write barrier to synchronize with initialization
3281 * of log entry
3282 */
3283 smp_wmb();
3284 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003285 return;
3286
Todd Kjos2f993e22017-05-12 14:42:55 -07003287err_dead_proc_or_thread:
3288 return_error = BR_DEAD_REPLY;
3289 return_error_line = __LINE__;
Xu YiPing86578a02017-05-22 11:26:23 -07003290 binder_dequeue_work(proc, tcomplete);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003291err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003292err_bad_object_type:
3293err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003294err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003295err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003296 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003297 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjos291d9682017-09-25 08:55:09 -07003298 if (target_node)
3299 binder_dec_node_tmpref(target_node);
Todd Kjosc37162d2017-05-26 11:56:29 -07003300 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003301 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003302 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003303err_binder_alloc_buf_failed:
3304 kfree(tcomplete);
3305 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3306err_alloc_tcomplete_failed:
3307 kfree(t);
3308 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3309err_alloc_t_failed:
3310err_bad_call_stack:
3311err_empty_call_stack:
3312err_dead_binder:
3313err_invalid_target_handle:
Todd Kjos2f993e22017-05-12 14:42:55 -07003314 if (target_thread)
3315 binder_thread_dec_tmpref(target_thread);
3316 if (target_proc)
3317 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003318 if (target_node) {
Todd Kjosc37162d2017-05-26 11:56:29 -07003319 binder_dec_node(target_node, 1, 0);
Todd Kjos291d9682017-09-25 08:55:09 -07003320 binder_dec_node_tmpref(target_node);
3321 }
Todd Kjosc37162d2017-05-26 11:56:29 -07003322
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003323 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003324 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3325 proc->pid, thread->pid, return_error, return_error_param,
3326 (u64)tr->data_size, (u64)tr->offsets_size,
3327 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003328
3329 {
3330 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003331
Todd Kjose598d172017-03-22 17:19:52 -07003332 e->return_error = return_error;
3333 e->return_error_param = return_error_param;
3334 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003335 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3336 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003337 /*
3338 * write barrier to synchronize with initialization
3339 * of log entry
3340 */
3341 smp_wmb();
3342 WRITE_ONCE(e->debug_id_done, t_debug_id);
3343 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003344 }
3345
Todd Kjos858b8da2017-04-21 17:35:12 -07003346 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003347 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003348 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003349 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003350 binder_enqueue_work(thread->proc,
3351 &thread->return_error.work,
3352 &thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003353 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003354 } else {
3355 thread->return_error.cmd = return_error;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003356 binder_enqueue_work(thread->proc,
3357 &thread->return_error.work,
3358 &thread->todo);
Todd Kjos858b8da2017-04-21 17:35:12 -07003359 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003360}
3361
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003362static int binder_thread_write(struct binder_proc *proc,
3363 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003364 binder_uintptr_t binder_buffer, size_t size,
3365 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003366{
3367 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003368 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003369 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003370 void __user *ptr = buffer + *consumed;
3371 void __user *end = buffer + size;
3372
Todd Kjos858b8da2017-04-21 17:35:12 -07003373 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003374 int ret;
3375
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003376 if (get_user(cmd, (uint32_t __user *)ptr))
3377 return -EFAULT;
3378 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003379 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003380 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003381 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3382 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3383 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003384 }
3385 switch (cmd) {
3386 case BC_INCREFS:
3387 case BC_ACQUIRE:
3388 case BC_RELEASE:
3389 case BC_DECREFS: {
3390 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003391 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003392 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3393 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3394 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003395
3396 if (get_user(target, (uint32_t __user *)ptr))
3397 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003398
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003399 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003400 ret = -1;
3401 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003402 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003403 mutex_lock(&context->context_mgr_node_lock);
3404 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003405 if (ctx_mgr_node)
3406 ret = binder_inc_ref_for_node(
3407 proc, ctx_mgr_node,
3408 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003409 mutex_unlock(&context->context_mgr_node_lock);
3410 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003411 if (ret)
3412 ret = binder_update_ref_for_handle(
3413 proc, target, increment, strong,
3414 &rdata);
3415 if (!ret && rdata.desc != target) {
3416 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3417 proc->pid, thread->pid,
3418 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003419 }
3420 switch (cmd) {
3421 case BC_INCREFS:
3422 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003423 break;
3424 case BC_ACQUIRE:
3425 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003426 break;
3427 case BC_RELEASE:
3428 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003429 break;
3430 case BC_DECREFS:
3431 default:
3432 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003433 break;
3434 }
3435 if (ret) {
3436 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3437 proc->pid, thread->pid, debug_string,
3438 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003439 break;
3440 }
3441 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003442 "%d:%d %s ref %d desc %d s %d w %d\n",
3443 proc->pid, thread->pid, debug_string,
3444 rdata.debug_id, rdata.desc, rdata.strong,
3445 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003446 break;
3447 }
3448 case BC_INCREFS_DONE:
3449 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003450 binder_uintptr_t node_ptr;
3451 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003452 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003453 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003454
Arve Hjønnevågda498892014-02-21 14:40:26 -08003455 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003456 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003457 ptr += sizeof(binder_uintptr_t);
3458 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003459 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003460 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003461 node = binder_get_node(proc, node_ptr);
3462 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003463 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003464 proc->pid, thread->pid,
3465 cmd == BC_INCREFS_DONE ?
3466 "BC_INCREFS_DONE" :
3467 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003468 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003469 break;
3470 }
3471 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003472 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003473 proc->pid, thread->pid,
3474 cmd == BC_INCREFS_DONE ?
3475 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003476 (u64)node_ptr, node->debug_id,
3477 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003478 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003479 break;
3480 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003481 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003482 if (cmd == BC_ACQUIRE_DONE) {
3483 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303484 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003485 proc->pid, thread->pid,
3486 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003487 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003488 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003489 break;
3490 }
3491 node->pending_strong_ref = 0;
3492 } else {
3493 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303494 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003495 proc->pid, thread->pid,
3496 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003497 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003498 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003499 break;
3500 }
3501 node->pending_weak_ref = 0;
3502 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003503 free_node = binder_dec_node_nilocked(node,
3504 cmd == BC_ACQUIRE_DONE, 0);
3505 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003506 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003507 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003508 proc->pid, thread->pid,
3509 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003510 node->debug_id, node->local_strong_refs,
3511 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003512 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003513 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003514 break;
3515 }
3516 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303517 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003518 return -EINVAL;
3519 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303520 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003521 return -EINVAL;
3522
3523 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003524 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003525 struct binder_buffer *buffer;
3526
Arve Hjønnevågda498892014-02-21 14:40:26 -08003527 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003528 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003529 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003530
Todd Kjos076072a2017-04-21 14:32:11 -07003531 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3532 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003533 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003534 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3535 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003536 break;
3537 }
3538 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003539 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3540 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003541 break;
3542 }
3543 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003544 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3545 proc->pid, thread->pid, (u64)data_ptr,
3546 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003547 buffer->transaction ? "active" : "finished");
3548
3549 if (buffer->transaction) {
3550 buffer->transaction->buffer = NULL;
3551 buffer->transaction = NULL;
3552 }
3553 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003554 struct binder_node *buf_node;
3555 struct binder_work *w;
3556
3557 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003558 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003559 BUG_ON(!buf_node->has_async_transaction);
3560 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003561 w = binder_dequeue_work_head_ilocked(
3562 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003563 if (!w) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003564 buf_node->has_async_transaction = 0;
Martijn Coenen4501c042017-08-10 13:56:16 +02003565 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003566 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003567 w, &proc->todo);
3568 binder_wakeup_proc_ilocked(proc);
3569 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003570 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003571 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003572 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003573 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07003574 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003575 break;
3576 }
3577
Martijn Coenen5a6da532016-09-30 14:10:07 +02003578 case BC_TRANSACTION_SG:
3579 case BC_REPLY_SG: {
3580 struct binder_transaction_data_sg tr;
3581
3582 if (copy_from_user(&tr, ptr, sizeof(tr)))
3583 return -EFAULT;
3584 ptr += sizeof(tr);
3585 binder_transaction(proc, thread, &tr.transaction_data,
3586 cmd == BC_REPLY_SG, tr.buffers_size);
3587 break;
3588 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003589 case BC_TRANSACTION:
3590 case BC_REPLY: {
3591 struct binder_transaction_data tr;
3592
3593 if (copy_from_user(&tr, ptr, sizeof(tr)))
3594 return -EFAULT;
3595 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003596 binder_transaction(proc, thread, &tr,
3597 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003598 break;
3599 }
3600
3601 case BC_REGISTER_LOOPER:
3602 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303603 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003604 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003605 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003606 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3607 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303608 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003609 proc->pid, thread->pid);
3610 } else if (proc->requested_threads == 0) {
3611 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303612 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003613 proc->pid, thread->pid);
3614 } else {
3615 proc->requested_threads--;
3616 proc->requested_threads_started++;
3617 }
3618 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003619 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003620 break;
3621 case BC_ENTER_LOOPER:
3622 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303623 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003624 proc->pid, thread->pid);
3625 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3626 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303627 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003628 proc->pid, thread->pid);
3629 }
3630 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3631 break;
3632 case BC_EXIT_LOOPER:
3633 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303634 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003635 proc->pid, thread->pid);
3636 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3637 break;
3638
3639 case BC_REQUEST_DEATH_NOTIFICATION:
3640 case BC_CLEAR_DEATH_NOTIFICATION: {
3641 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003642 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003643 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003644 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003645
3646 if (get_user(target, (uint32_t __user *)ptr))
3647 return -EFAULT;
3648 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003649 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003650 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003651 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003652 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3653 /*
3654 * Allocate memory for death notification
3655 * before taking lock
3656 */
3657 death = kzalloc(sizeof(*death), GFP_KERNEL);
3658 if (death == NULL) {
3659 WARN_ON(thread->return_error.cmd !=
3660 BR_OK);
3661 thread->return_error.cmd = BR_ERROR;
3662 binder_enqueue_work(
3663 thread->proc,
3664 &thread->return_error.work,
3665 &thread->todo);
3666 binder_debug(
3667 BINDER_DEBUG_FAILED_TRANSACTION,
3668 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3669 proc->pid, thread->pid);
3670 break;
3671 }
3672 }
3673 binder_proc_lock(proc);
3674 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003675 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303676 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003677 proc->pid, thread->pid,
3678 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3679 "BC_REQUEST_DEATH_NOTIFICATION" :
3680 "BC_CLEAR_DEATH_NOTIFICATION",
3681 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003682 binder_proc_unlock(proc);
3683 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003684 break;
3685 }
3686
3687 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003688 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003689 proc->pid, thread->pid,
3690 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3691 "BC_REQUEST_DEATH_NOTIFICATION" :
3692 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003693 (u64)cookie, ref->data.debug_id,
3694 ref->data.desc, ref->data.strong,
3695 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003696
Martijn Coenenf9eac642017-05-22 11:26:23 -07003697 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003698 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3699 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303700 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003701 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003702 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003703 binder_proc_unlock(proc);
3704 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003705 break;
3706 }
3707 binder_stats_created(BINDER_STAT_DEATH);
3708 INIT_LIST_HEAD(&death->work.entry);
3709 death->cookie = cookie;
3710 ref->death = death;
3711 if (ref->node->proc == NULL) {
3712 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02003713
3714 binder_inner_proc_lock(proc);
3715 binder_enqueue_work_ilocked(
3716 &ref->death->work, &proc->todo);
3717 binder_wakeup_proc_ilocked(proc);
3718 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003719 }
3720 } else {
3721 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303722 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003723 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003724 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003725 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003726 break;
3727 }
3728 death = ref->death;
3729 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003730 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003731 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003732 (u64)death->cookie,
3733 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003734 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003735 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003736 break;
3737 }
3738 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003739 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003740 if (list_empty(&death->work.entry)) {
3741 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003742 if (thread->looper &
3743 (BINDER_LOOPER_STATE_REGISTERED |
3744 BINDER_LOOPER_STATE_ENTERED))
3745 binder_enqueue_work_ilocked(
3746 &death->work,
3747 &thread->todo);
3748 else {
3749 binder_enqueue_work_ilocked(
3750 &death->work,
3751 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003752 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07003753 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003754 }
3755 } else {
3756 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3757 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3758 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003759 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003760 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07003761 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003762 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003763 } break;
3764 case BC_DEAD_BINDER_DONE: {
3765 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003766 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003767 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003768
Arve Hjønnevågda498892014-02-21 14:40:26 -08003769 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003770 return -EFAULT;
3771
Lisa Du7a64cd82016-02-17 09:32:52 +08003772 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003773 binder_inner_proc_lock(proc);
3774 list_for_each_entry(w, &proc->delivered_death,
3775 entry) {
3776 struct binder_ref_death *tmp_death =
3777 container_of(w,
3778 struct binder_ref_death,
3779 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003780
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003781 if (tmp_death->cookie == cookie) {
3782 death = tmp_death;
3783 break;
3784 }
3785 }
3786 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003787 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3788 proc->pid, thread->pid, (u64)cookie,
3789 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003790 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003791 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3792 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003793 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003794 break;
3795 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003796 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003797 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3798 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003799 if (thread->looper &
3800 (BINDER_LOOPER_STATE_REGISTERED |
3801 BINDER_LOOPER_STATE_ENTERED))
3802 binder_enqueue_work_ilocked(
3803 &death->work, &thread->todo);
3804 else {
3805 binder_enqueue_work_ilocked(
3806 &death->work,
3807 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07003808 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003809 }
3810 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003811 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003812 } break;
3813
3814 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303815 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003816 proc->pid, thread->pid, cmd);
3817 return -EINVAL;
3818 }
3819 *consumed = ptr - buffer;
3820 }
3821 return 0;
3822}
3823
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003824static void binder_stat_br(struct binder_proc *proc,
3825 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003826{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003827 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003828 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003829 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3830 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3831 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003832 }
3833}
3834
Todd Kjos60792612017-05-24 10:51:01 -07003835static int binder_put_node_cmd(struct binder_proc *proc,
3836 struct binder_thread *thread,
3837 void __user **ptrp,
3838 binder_uintptr_t node_ptr,
3839 binder_uintptr_t node_cookie,
3840 int node_debug_id,
3841 uint32_t cmd, const char *cmd_name)
3842{
3843 void __user *ptr = *ptrp;
3844
3845 if (put_user(cmd, (uint32_t __user *)ptr))
3846 return -EFAULT;
3847 ptr += sizeof(uint32_t);
3848
3849 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3850 return -EFAULT;
3851 ptr += sizeof(binder_uintptr_t);
3852
3853 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3854 return -EFAULT;
3855 ptr += sizeof(binder_uintptr_t);
3856
3857 binder_stat_br(proc, thread, cmd);
3858 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3859 proc->pid, thread->pid, cmd_name, node_debug_id,
3860 (u64)node_ptr, (u64)node_cookie);
3861
3862 *ptrp = ptr;
3863 return 0;
3864}
3865
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003866static int binder_wait_for_work(struct binder_thread *thread,
3867 bool do_proc_work)
3868{
3869 DEFINE_WAIT(wait);
3870 struct binder_proc *proc = thread->proc;
3871 int ret = 0;
3872
3873 freezer_do_not_count();
3874 binder_inner_proc_lock(proc);
3875 for (;;) {
3876 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3877 if (binder_has_work_ilocked(thread, do_proc_work))
3878 break;
3879 if (do_proc_work)
3880 list_add(&thread->waiting_thread_node,
3881 &proc->waiting_threads);
3882 binder_inner_proc_unlock(proc);
3883 schedule();
3884 binder_inner_proc_lock(proc);
3885 list_del_init(&thread->waiting_thread_node);
3886 if (signal_pending(current)) {
3887 ret = -ERESTARTSYS;
3888 break;
3889 }
3890 }
3891 finish_wait(&thread->wait, &wait);
3892 binder_inner_proc_unlock(proc);
3893 freezer_count();
3894
3895 return ret;
3896}
3897
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003898static int binder_thread_read(struct binder_proc *proc,
3899 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003900 binder_uintptr_t binder_buffer, size_t size,
3901 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003902{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003903 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003904 void __user *ptr = buffer + *consumed;
3905 void __user *end = buffer + size;
3906
3907 int ret = 0;
3908 int wait_for_proc_work;
3909
3910 if (*consumed == 0) {
3911 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3912 return -EFAULT;
3913 ptr += sizeof(uint32_t);
3914 }
3915
3916retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07003917 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003918 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003919 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003920
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003921 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003922
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003923 trace_binder_wait_for_work(wait_for_proc_work,
3924 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003925 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003926 if (wait_for_proc_work) {
3927 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3928 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303929 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 +09003930 proc->pid, thread->pid, thread->looper);
3931 wait_event_interruptible(binder_user_error_wait,
3932 binder_stop_on_user_error < 2);
3933 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07003934 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003935 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003936
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003937 if (non_block) {
3938 if (!binder_has_work(thread, wait_for_proc_work))
3939 ret = -EAGAIN;
3940 } else {
3941 ret = binder_wait_for_work(thread, wait_for_proc_work);
3942 }
3943
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003944 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3945
3946 if (ret)
3947 return ret;
3948
3949 while (1) {
3950 uint32_t cmd;
3951 struct binder_transaction_data tr;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003952 struct binder_work *w = NULL;
3953 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003954 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07003955 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003956
Todd Kjose7f23ed2017-03-21 13:06:01 -07003957 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003958 if (!binder_worklist_empty_ilocked(&thread->todo))
3959 list = &thread->todo;
3960 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3961 wait_for_proc_work)
3962 list = &proc->todo;
3963 else {
3964 binder_inner_proc_unlock(proc);
3965
Dmitry Voytik395262a2014-09-08 18:16:34 +04003966 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08003967 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003968 goto retry;
3969 break;
3970 }
3971
Todd Kjose7f23ed2017-03-21 13:06:01 -07003972 if (end - ptr < sizeof(tr) + 4) {
3973 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003974 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07003975 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003976 w = binder_dequeue_work_head_ilocked(list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003977
3978 switch (w->type) {
3979 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07003980 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003981 t = container_of(w, struct binder_transaction, work);
3982 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07003983 case BINDER_WORK_RETURN_ERROR: {
3984 struct binder_error *e = container_of(
3985 w, struct binder_error, work);
3986
3987 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07003988 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07003989 if (put_user(e->cmd, (uint32_t __user *)ptr))
3990 return -EFAULT;
3991 e->cmd = BR_OK;
3992 ptr += sizeof(uint32_t);
3993
3994 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07003995 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003996 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07003997 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003998 cmd = BR_TRANSACTION_COMPLETE;
3999 if (put_user(cmd, (uint32_t __user *)ptr))
4000 return -EFAULT;
4001 ptr += sizeof(uint32_t);
4002
4003 binder_stat_br(proc, thread, cmd);
4004 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304005 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004006 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004007 kfree(w);
4008 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4009 } break;
4010 case BINDER_WORK_NODE: {
4011 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07004012 int strong, weak;
4013 binder_uintptr_t node_ptr = node->ptr;
4014 binder_uintptr_t node_cookie = node->cookie;
4015 int node_debug_id = node->debug_id;
4016 int has_weak_ref;
4017 int has_strong_ref;
4018 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004019
Todd Kjos60792612017-05-24 10:51:01 -07004020 BUG_ON(proc != node->proc);
4021 strong = node->internal_strong_refs ||
4022 node->local_strong_refs;
4023 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07004024 node->local_weak_refs ||
4025 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004026 has_strong_ref = node->has_strong_ref;
4027 has_weak_ref = node->has_weak_ref;
4028
4029 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004030 node->has_weak_ref = 1;
4031 node->pending_weak_ref = 1;
4032 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004033 }
4034 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004035 node->has_strong_ref = 1;
4036 node->pending_strong_ref = 1;
4037 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004038 }
4039 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004040 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004041 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004042 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004043 if (!weak && !strong) {
4044 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4045 "%d:%d node %d u%016llx c%016llx deleted\n",
4046 proc->pid, thread->pid,
4047 node_debug_id,
4048 (u64)node_ptr,
4049 (u64)node_cookie);
4050 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004051 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004052 binder_node_lock(node);
4053 /*
4054 * Acquire the node lock before freeing the
4055 * node to serialize with other threads that
4056 * may have been holding the node lock while
4057 * decrementing this node (avoids race where
4058 * this thread frees while the other thread
4059 * is unlocking the node after the final
4060 * decrement)
4061 */
4062 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004063 binder_free_node(node);
4064 } else
4065 binder_inner_proc_unlock(proc);
4066
Todd Kjos60792612017-05-24 10:51:01 -07004067 if (weak && !has_weak_ref)
4068 ret = binder_put_node_cmd(
4069 proc, thread, &ptr, node_ptr,
4070 node_cookie, node_debug_id,
4071 BR_INCREFS, "BR_INCREFS");
4072 if (!ret && strong && !has_strong_ref)
4073 ret = binder_put_node_cmd(
4074 proc, thread, &ptr, node_ptr,
4075 node_cookie, node_debug_id,
4076 BR_ACQUIRE, "BR_ACQUIRE");
4077 if (!ret && !strong && has_strong_ref)
4078 ret = binder_put_node_cmd(
4079 proc, thread, &ptr, node_ptr,
4080 node_cookie, node_debug_id,
4081 BR_RELEASE, "BR_RELEASE");
4082 if (!ret && !weak && has_weak_ref)
4083 ret = binder_put_node_cmd(
4084 proc, thread, &ptr, node_ptr,
4085 node_cookie, node_debug_id,
4086 BR_DECREFS, "BR_DECREFS");
4087 if (orig_ptr == ptr)
4088 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4089 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4090 proc->pid, thread->pid,
4091 node_debug_id,
4092 (u64)node_ptr,
4093 (u64)node_cookie);
4094 if (ret)
4095 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004096 } break;
4097 case BINDER_WORK_DEAD_BINDER:
4098 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4099 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4100 struct binder_ref_death *death;
4101 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004102 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004103
4104 death = container_of(w, struct binder_ref_death, work);
4105 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4106 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4107 else
4108 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004109 cookie = death->cookie;
4110
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004111 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004112 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004113 proc->pid, thread->pid,
4114 cmd == BR_DEAD_BINDER ?
4115 "BR_DEAD_BINDER" :
4116 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004117 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004118 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004119 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004120 kfree(death);
4121 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004122 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004123 binder_enqueue_work_ilocked(
4124 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004125 binder_inner_proc_unlock(proc);
4126 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004127 if (put_user(cmd, (uint32_t __user *)ptr))
4128 return -EFAULT;
4129 ptr += sizeof(uint32_t);
4130 if (put_user(cookie,
4131 (binder_uintptr_t __user *)ptr))
4132 return -EFAULT;
4133 ptr += sizeof(binder_uintptr_t);
4134 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004135 if (cmd == BR_DEAD_BINDER)
4136 goto done; /* DEAD_BINDER notifications can cause transactions */
4137 } break;
4138 }
4139
4140 if (!t)
4141 continue;
4142
4143 BUG_ON(t->buffer == NULL);
4144 if (t->buffer->target_node) {
4145 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004146 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004147
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004148 tr.target.ptr = target_node->ptr;
4149 tr.cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004150 node_prio.sched_policy = target_node->sched_policy;
4151 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004152 binder_transaction_priority(current, t, node_prio,
4153 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004154 cmd = BR_TRANSACTION;
4155 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004156 tr.target.ptr = 0;
4157 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004158 cmd = BR_REPLY;
4159 }
4160 tr.code = t->code;
4161 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06004162 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004163
Todd Kjos2f993e22017-05-12 14:42:55 -07004164 t_from = binder_get_txn_from(t);
4165 if (t_from) {
4166 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004167
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004168 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004169 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004170 } else {
4171 tr.sender_pid = 0;
4172 }
4173
4174 tr.data_size = t->buffer->data_size;
4175 tr.offsets_size = t->buffer->offsets_size;
Todd Kjosd325d372016-10-10 10:40:53 -07004176 tr.data.ptr.buffer = (binder_uintptr_t)
4177 ((uintptr_t)t->buffer->data +
4178 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004179 tr.data.ptr.offsets = tr.data.ptr.buffer +
4180 ALIGN(t->buffer->data_size,
4181 sizeof(void *));
4182
Todd Kjos2f993e22017-05-12 14:42:55 -07004183 if (put_user(cmd, (uint32_t __user *)ptr)) {
4184 if (t_from)
4185 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004186 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004187 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004188 ptr += sizeof(uint32_t);
Todd Kjos2f993e22017-05-12 14:42:55 -07004189 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4190 if (t_from)
4191 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004192 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004193 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004194 ptr += sizeof(tr);
4195
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004196 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004197 binder_stat_br(proc, thread, cmd);
4198 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004199 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004200 proc->pid, thread->pid,
4201 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4202 "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004203 t->debug_id, t_from ? t_from->proc->pid : 0,
4204 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004205 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004206 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004207
Todd Kjos2f993e22017-05-12 14:42:55 -07004208 if (t_from)
4209 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004210 t->buffer->allow_user_free = 1;
4211 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004212 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004213 t->to_parent = thread->transaction_stack;
4214 t->to_thread = thread;
4215 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004216 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004217 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004218 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004219 }
4220 break;
4221 }
4222
4223done:
4224
4225 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004226 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004227 if (proc->requested_threads == 0 &&
4228 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004229 proc->requested_threads_started < proc->max_threads &&
4230 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4231 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4232 /*spawn a new thread if we leave this out */) {
4233 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004234 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004235 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304236 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004237 proc->pid, thread->pid);
4238 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4239 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004240 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004241 } else
4242 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004243 return 0;
4244}
4245
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004246static void binder_release_work(struct binder_proc *proc,
4247 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004248{
4249 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004250
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004251 while (1) {
4252 w = binder_dequeue_work_head(proc, list);
4253 if (!w)
4254 return;
4255
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004256 switch (w->type) {
4257 case BINDER_WORK_TRANSACTION: {
4258 struct binder_transaction *t;
4259
4260 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004261 if (t->buffer->target_node &&
4262 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004263 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004264 } else {
4265 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304266 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004267 t->debug_id);
Todd Kjos21ef40a2017-03-30 18:02:13 -07004268 binder_free_transaction(t);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004269 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004270 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004271 case BINDER_WORK_RETURN_ERROR: {
4272 struct binder_error *e = container_of(
4273 w, struct binder_error, work);
4274
4275 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4276 "undelivered TRANSACTION_ERROR: %u\n",
4277 e->cmd);
4278 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004279 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004280 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304281 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004282 kfree(w);
4283 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4284 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004285 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4286 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4287 struct binder_ref_death *death;
4288
4289 death = container_of(w, struct binder_ref_death, work);
4290 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004291 "undelivered death notification, %016llx\n",
4292 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004293 kfree(death);
4294 binder_stats_deleted(BINDER_STAT_DEATH);
4295 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004296 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304297 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004298 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004299 break;
4300 }
4301 }
4302
4303}
4304
Todd Kjosb4827902017-05-25 15:52:17 -07004305static struct binder_thread *binder_get_thread_ilocked(
4306 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004307{
4308 struct binder_thread *thread = NULL;
4309 struct rb_node *parent = NULL;
4310 struct rb_node **p = &proc->threads.rb_node;
4311
4312 while (*p) {
4313 parent = *p;
4314 thread = rb_entry(parent, struct binder_thread, rb_node);
4315
4316 if (current->pid < thread->pid)
4317 p = &(*p)->rb_left;
4318 else if (current->pid > thread->pid)
4319 p = &(*p)->rb_right;
4320 else
Todd Kjosb4827902017-05-25 15:52:17 -07004321 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004322 }
Todd Kjosb4827902017-05-25 15:52:17 -07004323 if (!new_thread)
4324 return NULL;
4325 thread = new_thread;
4326 binder_stats_created(BINDER_STAT_THREAD);
4327 thread->proc = proc;
4328 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004329 get_task_struct(current);
4330 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004331 atomic_set(&thread->tmp_ref, 0);
4332 init_waitqueue_head(&thread->wait);
4333 INIT_LIST_HEAD(&thread->todo);
4334 rb_link_node(&thread->rb_node, parent, p);
4335 rb_insert_color(&thread->rb_node, &proc->threads);
4336 thread->looper_need_return = true;
4337 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4338 thread->return_error.cmd = BR_OK;
4339 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4340 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004341 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004342 return thread;
4343}
4344
4345static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4346{
4347 struct binder_thread *thread;
4348 struct binder_thread *new_thread;
4349
4350 binder_inner_proc_lock(proc);
4351 thread = binder_get_thread_ilocked(proc, NULL);
4352 binder_inner_proc_unlock(proc);
4353 if (!thread) {
4354 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4355 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004356 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004357 binder_inner_proc_lock(proc);
4358 thread = binder_get_thread_ilocked(proc, new_thread);
4359 binder_inner_proc_unlock(proc);
4360 if (thread != new_thread)
4361 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004362 }
4363 return thread;
4364}
4365
Todd Kjos2f993e22017-05-12 14:42:55 -07004366static void binder_free_proc(struct binder_proc *proc)
4367{
4368 BUG_ON(!list_empty(&proc->todo));
4369 BUG_ON(!list_empty(&proc->delivered_death));
4370 binder_alloc_deferred_release(&proc->alloc);
4371 put_task_struct(proc->tsk);
4372 binder_stats_deleted(BINDER_STAT_PROC);
4373 kfree(proc);
4374}
4375
4376static void binder_free_thread(struct binder_thread *thread)
4377{
4378 BUG_ON(!list_empty(&thread->todo));
4379 binder_stats_deleted(BINDER_STAT_THREAD);
4380 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004381 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004382 kfree(thread);
4383}
4384
4385static int binder_thread_release(struct binder_proc *proc,
4386 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004387{
4388 struct binder_transaction *t;
4389 struct binder_transaction *send_reply = NULL;
4390 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004391 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004392
Todd Kjosb4827902017-05-25 15:52:17 -07004393 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004394 /*
4395 * take a ref on the proc so it survives
4396 * after we remove this thread from proc->threads.
4397 * The corresponding dec is when we actually
4398 * free the thread in binder_free_thread()
4399 */
4400 proc->tmp_ref++;
4401 /*
4402 * take a ref on this thread to ensure it
4403 * survives while we are releasing it
4404 */
4405 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004406 rb_erase(&thread->rb_node, &proc->threads);
4407 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004408 if (t) {
4409 spin_lock(&t->lock);
4410 if (t->to_thread == thread)
4411 send_reply = t;
4412 }
4413 thread->is_dead = true;
4414
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004415 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004416 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004417 active_transactions++;
4418 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304419 "release %d:%d transaction %d %s, still active\n",
4420 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004421 t->debug_id,
4422 (t->to_thread == thread) ? "in" : "out");
4423
4424 if (t->to_thread == thread) {
4425 t->to_proc = NULL;
4426 t->to_thread = NULL;
4427 if (t->buffer) {
4428 t->buffer->transaction = NULL;
4429 t->buffer = NULL;
4430 }
4431 t = t->to_parent;
4432 } else if (t->from == thread) {
4433 t->from = NULL;
4434 t = t->from_parent;
4435 } else
4436 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004437 spin_unlock(&last_t->lock);
4438 if (t)
4439 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004440 }
Todd Kjosb4827902017-05-25 15:52:17 -07004441 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004442
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004443 if (send_reply)
4444 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004445 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004446 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004447 return active_transactions;
4448}
4449
4450static unsigned int binder_poll(struct file *filp,
4451 struct poll_table_struct *wait)
4452{
4453 struct binder_proc *proc = filp->private_data;
4454 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004455 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004456
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004457 thread = binder_get_thread(proc);
4458
Martijn Coenen995a36e2017-06-02 13:36:52 -07004459 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004460 thread->looper |= BINDER_LOOPER_STATE_POLL;
4461 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4462
Martijn Coenen995a36e2017-06-02 13:36:52 -07004463 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004464
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004465 poll_wait(filp, &thread->wait, wait);
4466
Martijn Coenen47810932017-08-10 12:32:00 +02004467 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004468 return POLLIN;
4469
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004470 return 0;
4471}
4472
Tair Rzayev78260ac2014-06-03 22:27:21 +03004473static int binder_ioctl_write_read(struct file *filp,
4474 unsigned int cmd, unsigned long arg,
4475 struct binder_thread *thread)
4476{
4477 int ret = 0;
4478 struct binder_proc *proc = filp->private_data;
4479 unsigned int size = _IOC_SIZE(cmd);
4480 void __user *ubuf = (void __user *)arg;
4481 struct binder_write_read bwr;
4482
4483 if (size != sizeof(struct binder_write_read)) {
4484 ret = -EINVAL;
4485 goto out;
4486 }
4487 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4488 ret = -EFAULT;
4489 goto out;
4490 }
4491 binder_debug(BINDER_DEBUG_READ_WRITE,
4492 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4493 proc->pid, thread->pid,
4494 (u64)bwr.write_size, (u64)bwr.write_buffer,
4495 (u64)bwr.read_size, (u64)bwr.read_buffer);
4496
4497 if (bwr.write_size > 0) {
4498 ret = binder_thread_write(proc, thread,
4499 bwr.write_buffer,
4500 bwr.write_size,
4501 &bwr.write_consumed);
4502 trace_binder_write_done(ret);
4503 if (ret < 0) {
4504 bwr.read_consumed = 0;
4505 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4506 ret = -EFAULT;
4507 goto out;
4508 }
4509 }
4510 if (bwr.read_size > 0) {
4511 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4512 bwr.read_size,
4513 &bwr.read_consumed,
4514 filp->f_flags & O_NONBLOCK);
4515 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004516 binder_inner_proc_lock(proc);
4517 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004518 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004519 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004520 if (ret < 0) {
4521 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4522 ret = -EFAULT;
4523 goto out;
4524 }
4525 }
4526 binder_debug(BINDER_DEBUG_READ_WRITE,
4527 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4528 proc->pid, thread->pid,
4529 (u64)bwr.write_consumed, (u64)bwr.write_size,
4530 (u64)bwr.read_consumed, (u64)bwr.read_size);
4531 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4532 ret = -EFAULT;
4533 goto out;
4534 }
4535out:
4536 return ret;
4537}
4538
4539static int binder_ioctl_set_ctx_mgr(struct file *filp)
4540{
4541 int ret = 0;
4542 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004543 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004544 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004545 kuid_t curr_euid = current_euid();
4546
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004547 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004548 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004549 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4550 ret = -EBUSY;
4551 goto out;
4552 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004553 ret = security_binder_set_context_mgr(proc->tsk);
4554 if (ret < 0)
4555 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004556 if (uid_valid(context->binder_context_mgr_uid)) {
4557 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004558 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4559 from_kuid(&init_user_ns, curr_euid),
4560 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004561 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004562 ret = -EPERM;
4563 goto out;
4564 }
4565 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004566 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004567 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004568 new_node = binder_new_node(proc, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004569 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004570 ret = -ENOMEM;
4571 goto out;
4572 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004573 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004574 new_node->local_weak_refs++;
4575 new_node->local_strong_refs++;
4576 new_node->has_strong_ref = 1;
4577 new_node->has_weak_ref = 1;
4578 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004579 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004580 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004581out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004582 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004583 return ret;
4584}
4585
Colin Cross833babb32017-06-20 13:54:44 -07004586static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4587 struct binder_node_debug_info *info) {
4588 struct rb_node *n;
4589 binder_uintptr_t ptr = info->ptr;
4590
4591 memset(info, 0, sizeof(*info));
4592
4593 binder_inner_proc_lock(proc);
4594 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4595 struct binder_node *node = rb_entry(n, struct binder_node,
4596 rb_node);
4597 if (node->ptr > ptr) {
4598 info->ptr = node->ptr;
4599 info->cookie = node->cookie;
4600 info->has_strong_ref = node->has_strong_ref;
4601 info->has_weak_ref = node->has_weak_ref;
4602 break;
4603 }
4604 }
4605 binder_inner_proc_unlock(proc);
4606
4607 return 0;
4608}
4609
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004610static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4611{
4612 int ret;
4613 struct binder_proc *proc = filp->private_data;
4614 struct binder_thread *thread;
4615 unsigned int size = _IOC_SIZE(cmd);
4616 void __user *ubuf = (void __user *)arg;
4617
Tair Rzayev78260ac2014-06-03 22:27:21 +03004618 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4619 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004620
Sherry Yang435416b2017-06-22 14:37:45 -07004621 binder_selftest_alloc(&proc->alloc);
4622
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004623 trace_binder_ioctl(cmd, arg);
4624
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004625 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4626 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004627 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004628
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004629 thread = binder_get_thread(proc);
4630 if (thread == NULL) {
4631 ret = -ENOMEM;
4632 goto err;
4633 }
4634
4635 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004636 case BINDER_WRITE_READ:
4637 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4638 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004639 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004640 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004641 case BINDER_SET_MAX_THREADS: {
4642 int max_threads;
4643
4644 if (copy_from_user(&max_threads, ubuf,
4645 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004646 ret = -EINVAL;
4647 goto err;
4648 }
Todd Kjosd600e902017-05-25 17:35:02 -07004649 binder_inner_proc_lock(proc);
4650 proc->max_threads = max_threads;
4651 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004652 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004653 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004654 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004655 ret = binder_ioctl_set_ctx_mgr(filp);
4656 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004657 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004658 break;
4659 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304660 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004661 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07004662 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004663 thread = NULL;
4664 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004665 case BINDER_VERSION: {
4666 struct binder_version __user *ver = ubuf;
4667
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004668 if (size != sizeof(struct binder_version)) {
4669 ret = -EINVAL;
4670 goto err;
4671 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004672 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4673 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004674 ret = -EINVAL;
4675 goto err;
4676 }
4677 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004678 }
Colin Cross833babb32017-06-20 13:54:44 -07004679 case BINDER_GET_NODE_DEBUG_INFO: {
4680 struct binder_node_debug_info info;
4681
4682 if (copy_from_user(&info, ubuf, sizeof(info))) {
4683 ret = -EFAULT;
4684 goto err;
4685 }
4686
4687 ret = binder_ioctl_get_node_debug_info(proc, &info);
4688 if (ret < 0)
4689 goto err;
4690
4691 if (copy_to_user(ubuf, &info, sizeof(info))) {
4692 ret = -EFAULT;
4693 goto err;
4694 }
4695 break;
4696 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004697 default:
4698 ret = -EINVAL;
4699 goto err;
4700 }
4701 ret = 0;
4702err:
4703 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08004704 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004705 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4706 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304707 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 -07004708err_unlocked:
4709 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004710 return ret;
4711}
4712
4713static void binder_vma_open(struct vm_area_struct *vma)
4714{
4715 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004716
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004717 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304718 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004719 proc->pid, vma->vm_start, vma->vm_end,
4720 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4721 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004722}
4723
4724static void binder_vma_close(struct vm_area_struct *vma)
4725{
4726 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004727
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004728 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304729 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004730 proc->pid, vma->vm_start, vma->vm_end,
4731 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4732 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07004733 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004734 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4735}
4736
Vinayak Menonddac7d52014-06-02 18:17:59 +05304737static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4738{
4739 return VM_FAULT_SIGBUS;
4740}
4741
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004742static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004743 .open = binder_vma_open,
4744 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304745 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004746};
4747
Todd Kjosd325d372016-10-10 10:40:53 -07004748static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4749{
4750 int ret;
4751 struct binder_proc *proc = filp->private_data;
4752 const char *failure_string;
4753
4754 if (proc->tsk != current->group_leader)
4755 return -EINVAL;
4756
4757 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4758 vma->vm_end = vma->vm_start + SZ_4M;
4759
4760 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4761 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4762 __func__, proc->pid, vma->vm_start, vma->vm_end,
4763 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4764 (unsigned long)pgprot_val(vma->vm_page_prot));
4765
4766 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4767 ret = -EPERM;
4768 failure_string = "bad vm_flags";
4769 goto err_bad_arg;
4770 }
4771 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4772 vma->vm_ops = &binder_vm_ops;
4773 vma->vm_private_data = proc;
4774
4775 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4776 if (ret)
4777 return ret;
4778 proc->files = get_files_struct(current);
4779 return 0;
4780
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004781err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004782 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004783 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4784 return ret;
4785}
4786
4787static int binder_open(struct inode *nodp, struct file *filp)
4788{
4789 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004790 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004791
4792 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4793 current->group_leader->pid, current->pid);
4794
4795 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4796 if (proc == NULL)
4797 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07004798 spin_lock_init(&proc->inner_lock);
4799 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01004800 get_task_struct(current->group_leader);
4801 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004802 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07004803 if (binder_supported_policy(current->policy)) {
4804 proc->default_priority.sched_policy = current->policy;
4805 proc->default_priority.prio = current->normal_prio;
4806 } else {
4807 proc->default_priority.sched_policy = SCHED_NORMAL;
4808 proc->default_priority.prio = NICE_TO_PRIO(0);
4809 }
4810
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004811 binder_dev = container_of(filp->private_data, struct binder_device,
4812 miscdev);
4813 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07004814 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004815
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004816 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004817 proc->pid = current->group_leader->pid;
4818 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004819 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004820 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004821
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004822 mutex_lock(&binder_procs_lock);
4823 hlist_add_head(&proc->proc_node, &binder_procs);
4824 mutex_unlock(&binder_procs_lock);
4825
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004826 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004827 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004828
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004829 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004830 /*
4831 * proc debug entries are shared between contexts, so
4832 * this will fail if the process tries to open the driver
4833 * again with a different context. The priting code will
4834 * anyway print all contexts that a given PID has, so this
4835 * is not a problem.
4836 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004837 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004838 binder_debugfs_dir_entry_proc,
4839 (void *)(unsigned long)proc->pid,
4840 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004841 }
4842
4843 return 0;
4844}
4845
4846static int binder_flush(struct file *filp, fl_owner_t id)
4847{
4848 struct binder_proc *proc = filp->private_data;
4849
4850 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4851
4852 return 0;
4853}
4854
4855static void binder_deferred_flush(struct binder_proc *proc)
4856{
4857 struct rb_node *n;
4858 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004859
Todd Kjosb4827902017-05-25 15:52:17 -07004860 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004861 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4862 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004863
Todd Kjos6798e6d2017-01-06 14:19:25 -08004864 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004865 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4866 wake_up_interruptible(&thread->wait);
4867 wake_count++;
4868 }
4869 }
Todd Kjosb4827902017-05-25 15:52:17 -07004870 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004871
4872 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4873 "binder_flush: %d woke %d threads\n", proc->pid,
4874 wake_count);
4875}
4876
4877static int binder_release(struct inode *nodp, struct file *filp)
4878{
4879 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004880
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004881 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004882 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4883
4884 return 0;
4885}
4886
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004887static int binder_node_release(struct binder_node *node, int refs)
4888{
4889 struct binder_ref *ref;
4890 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004891 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004892
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004893 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004894
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004895 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004896 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004897 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07004898 /*
4899 * The caller must have taken a temporary ref on the node,
4900 */
4901 BUG_ON(!node->tmp_refs);
4902 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004903 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004904 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004905 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004906
4907 return refs;
4908 }
4909
4910 node->proc = NULL;
4911 node->local_strong_refs = 0;
4912 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004913 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004914
4915 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004916 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004917 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004918
4919 hlist_for_each_entry(ref, &node->refs, node_entry) {
4920 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004921 /*
4922 * Need the node lock to synchronize
4923 * with new notification requests and the
4924 * inner lock to synchronize with queued
4925 * death notifications.
4926 */
4927 binder_inner_proc_lock(ref->proc);
4928 if (!ref->death) {
4929 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08004930 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004931 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004932
4933 death++;
4934
Martijn Coenenf9eac642017-05-22 11:26:23 -07004935 BUG_ON(!list_empty(&ref->death->work.entry));
4936 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4937 binder_enqueue_work_ilocked(&ref->death->work,
4938 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07004939 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004940 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004941 }
4942
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004943 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4944 "node %d now dead, refs %d, death %d\n",
4945 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004946 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004947 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004948
4949 return refs;
4950}
4951
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004952static void binder_deferred_release(struct binder_proc *proc)
4953{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004954 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004955 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07004956 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004957
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004958 BUG_ON(proc->files);
4959
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004960 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004961 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004962 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004963
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004964 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004965 if (context->binder_context_mgr_node &&
4966 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004967 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004968 "%s: %d context_mgr_node gone\n",
4969 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004970 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004971 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004972 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07004973 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004974 /*
4975 * Make sure proc stays alive after we
4976 * remove all the threads
4977 */
4978 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004979
Todd Kjos2f993e22017-05-12 14:42:55 -07004980 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004981 threads = 0;
4982 active_transactions = 0;
4983 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004984 struct binder_thread *thread;
4985
4986 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004987 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004988 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07004989 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07004990 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004991 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004992
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004993 nodes = 0;
4994 incoming_refs = 0;
4995 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004996 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004997
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004998 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004999 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07005000 /*
5001 * take a temporary ref on the node before
5002 * calling binder_node_release() which will either
5003 * kfree() the node or call binder_put_node()
5004 */
Todd Kjos425d23f2017-06-12 12:07:26 -07005005 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005006 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07005007 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005008 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07005009 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005010 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005011 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005012
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005013 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005014 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005015 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005016 struct binder_ref *ref;
5017
5018 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005019 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07005020 binder_cleanup_ref_olocked(ref);
5021 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005022 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07005023 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005024 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005025 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005026
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005027 binder_release_work(proc, &proc->todo);
5028 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005029
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005030 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005031 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005032 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005033 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005034
Todd Kjos2f993e22017-05-12 14:42:55 -07005035 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005036}
5037
5038static void binder_deferred_func(struct work_struct *work)
5039{
5040 struct binder_proc *proc;
5041 struct files_struct *files;
5042
5043 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005044
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005045 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005046 mutex_lock(&binder_deferred_lock);
5047 if (!hlist_empty(&binder_deferred_list)) {
5048 proc = hlist_entry(binder_deferred_list.first,
5049 struct binder_proc, deferred_work_node);
5050 hlist_del_init(&proc->deferred_work_node);
5051 defer = proc->deferred_work;
5052 proc->deferred_work = 0;
5053 } else {
5054 proc = NULL;
5055 defer = 0;
5056 }
5057 mutex_unlock(&binder_deferred_lock);
5058
5059 files = NULL;
5060 if (defer & BINDER_DEFERRED_PUT_FILES) {
5061 files = proc->files;
5062 if (files)
5063 proc->files = NULL;
5064 }
5065
5066 if (defer & BINDER_DEFERRED_FLUSH)
5067 binder_deferred_flush(proc);
5068
5069 if (defer & BINDER_DEFERRED_RELEASE)
5070 binder_deferred_release(proc); /* frees proc */
5071
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005072 if (files)
5073 put_files_struct(files);
5074 } while (proc);
5075}
5076static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5077
5078static void
5079binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5080{
5081 mutex_lock(&binder_deferred_lock);
5082 proc->deferred_work |= defer;
5083 if (hlist_unhashed(&proc->deferred_work_node)) {
5084 hlist_add_head(&proc->deferred_work_node,
5085 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305086 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005087 }
5088 mutex_unlock(&binder_deferred_lock);
5089}
5090
Todd Kjos6d241a42017-04-21 14:32:11 -07005091static void print_binder_transaction_ilocked(struct seq_file *m,
5092 struct binder_proc *proc,
5093 const char *prefix,
5094 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005095{
Todd Kjos6d241a42017-04-21 14:32:11 -07005096 struct binder_proc *to_proc;
5097 struct binder_buffer *buffer = t->buffer;
5098
Todd Kjos2f993e22017-05-12 14:42:55 -07005099 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005100 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005101 seq_printf(m,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005102 "%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 -07005103 prefix, t->debug_id, t,
5104 t->from ? t->from->proc->pid : 0,
5105 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005106 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005107 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005108 t->code, t->flags, t->priority.sched_policy,
5109 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005110 spin_unlock(&t->lock);
5111
Todd Kjos6d241a42017-04-21 14:32:11 -07005112 if (proc != to_proc) {
5113 /*
5114 * Can only safely deref buffer if we are holding the
5115 * correct proc inner lock for this node
5116 */
5117 seq_puts(m, "\n");
5118 return;
5119 }
5120
5121 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005122 seq_puts(m, " buffer free\n");
5123 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005124 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005125 if (buffer->target_node)
5126 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005127 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005128 buffer->data_size, buffer->offsets_size,
5129 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005130}
5131
Todd Kjos6d241a42017-04-21 14:32:11 -07005132static void print_binder_work_ilocked(struct seq_file *m,
5133 struct binder_proc *proc,
5134 const char *prefix,
5135 const char *transaction_prefix,
5136 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005137{
5138 struct binder_node *node;
5139 struct binder_transaction *t;
5140
5141 switch (w->type) {
5142 case BINDER_WORK_TRANSACTION:
5143 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005144 print_binder_transaction_ilocked(
5145 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005146 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005147 case BINDER_WORK_RETURN_ERROR: {
5148 struct binder_error *e = container_of(
5149 w, struct binder_error, work);
5150
5151 seq_printf(m, "%stransaction error: %u\n",
5152 prefix, e->cmd);
5153 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005154 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005155 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005156 break;
5157 case BINDER_WORK_NODE:
5158 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005159 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5160 prefix, node->debug_id,
5161 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005162 break;
5163 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005164 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005165 break;
5166 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005167 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005168 break;
5169 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005170 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005171 break;
5172 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005173 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005174 break;
5175 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005176}
5177
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005178static void print_binder_thread_ilocked(struct seq_file *m,
5179 struct binder_thread *thread,
5180 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005181{
5182 struct binder_transaction *t;
5183 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005184 size_t start_pos = m->count;
5185 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005186
Todd Kjos2f993e22017-05-12 14:42:55 -07005187 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005188 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005189 thread->looper_need_return,
5190 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005191 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005192 t = thread->transaction_stack;
5193 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005194 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005195 print_binder_transaction_ilocked(m, thread->proc,
5196 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005197 t = t->from_parent;
5198 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005199 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005200 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005201 t = t->to_parent;
5202 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005203 print_binder_transaction_ilocked(m, thread->proc,
5204 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005205 t = NULL;
5206 }
5207 }
5208 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005209 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005210 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005211 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005212 if (!print_always && m->count == header_pos)
5213 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005214}
5215
Todd Kjos425d23f2017-06-12 12:07:26 -07005216static void print_binder_node_nilocked(struct seq_file *m,
5217 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005218{
5219 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005220 struct binder_work *w;
5221 int count;
5222
5223 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005224 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005225 count++;
5226
Martijn Coenen6aac9792017-06-07 09:29:14 -07005227 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 -08005228 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005229 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005230 node->has_strong_ref, node->has_weak_ref,
5231 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005232 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005233 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005234 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005235 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005236 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005237 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005238 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005239 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005240 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005241 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005242 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005243 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005244}
5245
Todd Kjos5346bf32016-10-20 16:43:34 -07005246static void print_binder_ref_olocked(struct seq_file *m,
5247 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005248{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005249 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005250 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5251 ref->data.debug_id, ref->data.desc,
5252 ref->node->proc ? "" : "dead ",
5253 ref->node->debug_id, ref->data.strong,
5254 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005255 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005256}
5257
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005258static void print_binder_proc(struct seq_file *m,
5259 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005260{
5261 struct binder_work *w;
5262 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005263 size_t start_pos = m->count;
5264 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005265 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005266
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005267 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005268 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005269 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005270
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005271 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005272 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005273 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005274 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005275
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005276 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005277 struct binder_node *node = rb_entry(n, struct binder_node,
5278 rb_node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005279 /*
5280 * take a temporary reference on the node so it
5281 * survives and isn't removed from the tree
5282 * while we print it.
5283 */
5284 binder_inc_node_tmpref_ilocked(node);
5285 /* Need to drop inner lock to take node lock */
5286 binder_inner_proc_unlock(proc);
5287 if (last_node)
5288 binder_put_node(last_node);
5289 binder_node_inner_lock(node);
5290 print_binder_node_nilocked(m, node);
5291 binder_node_inner_unlock(node);
5292 last_node = node;
5293 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005294 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005295 binder_inner_proc_unlock(proc);
5296 if (last_node)
5297 binder_put_node(last_node);
5298
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005299 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005300 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005301 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005302 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005303 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005304 print_binder_ref_olocked(m, rb_entry(n,
5305 struct binder_ref,
5306 rb_node_desc));
5307 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005308 }
Todd Kjosd325d372016-10-10 10:40:53 -07005309 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005310 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005311 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005312 print_binder_work_ilocked(m, proc, " ",
5313 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005314 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005315 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005316 break;
5317 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005318 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005319 if (!print_all && m->count == header_pos)
5320 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005321}
5322
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005323static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005324 "BR_ERROR",
5325 "BR_OK",
5326 "BR_TRANSACTION",
5327 "BR_REPLY",
5328 "BR_ACQUIRE_RESULT",
5329 "BR_DEAD_REPLY",
5330 "BR_TRANSACTION_COMPLETE",
5331 "BR_INCREFS",
5332 "BR_ACQUIRE",
5333 "BR_RELEASE",
5334 "BR_DECREFS",
5335 "BR_ATTEMPT_ACQUIRE",
5336 "BR_NOOP",
5337 "BR_SPAWN_LOOPER",
5338 "BR_FINISHED",
5339 "BR_DEAD_BINDER",
5340 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5341 "BR_FAILED_REPLY"
5342};
5343
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005344static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005345 "BC_TRANSACTION",
5346 "BC_REPLY",
5347 "BC_ACQUIRE_RESULT",
5348 "BC_FREE_BUFFER",
5349 "BC_INCREFS",
5350 "BC_ACQUIRE",
5351 "BC_RELEASE",
5352 "BC_DECREFS",
5353 "BC_INCREFS_DONE",
5354 "BC_ACQUIRE_DONE",
5355 "BC_ATTEMPT_ACQUIRE",
5356 "BC_REGISTER_LOOPER",
5357 "BC_ENTER_LOOPER",
5358 "BC_EXIT_LOOPER",
5359 "BC_REQUEST_DEATH_NOTIFICATION",
5360 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005361 "BC_DEAD_BINDER_DONE",
5362 "BC_TRANSACTION_SG",
5363 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005364};
5365
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005366static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005367 "proc",
5368 "thread",
5369 "node",
5370 "ref",
5371 "death",
5372 "transaction",
5373 "transaction_complete"
5374};
5375
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005376static void print_binder_stats(struct seq_file *m, const char *prefix,
5377 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005378{
5379 int i;
5380
5381 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005382 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005383 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005384 int temp = atomic_read(&stats->bc[i]);
5385
5386 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005387 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005388 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005389 }
5390
5391 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005392 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005393 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005394 int temp = atomic_read(&stats->br[i]);
5395
5396 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005397 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005398 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005399 }
5400
5401 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005402 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005403 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005404 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005405 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005406 int created = atomic_read(&stats->obj_created[i]);
5407 int deleted = atomic_read(&stats->obj_deleted[i]);
5408
5409 if (created || deleted)
5410 seq_printf(m, "%s%s: active %d total %d\n",
5411 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005412 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005413 created - deleted,
5414 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005415 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005416}
5417
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005418static void print_binder_proc_stats(struct seq_file *m,
5419 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005420{
5421 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005422 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005423 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005424 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005425 size_t free_async_space =
5426 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005427
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005428 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005429 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005430 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005431 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005432 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005433 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5434 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005435
5436 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5437 ready_threads++;
5438
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005439 seq_printf(m, " threads: %d\n", count);
5440 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005441 " ready threads %d\n"
5442 " free async space %zd\n", proc->requested_threads,
5443 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005444 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005445 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005446 count = 0;
5447 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5448 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005449 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005450 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005451 count = 0;
5452 strong = 0;
5453 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005454 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005455 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5456 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5457 rb_node_desc);
5458 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005459 strong += ref->data.strong;
5460 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005461 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005462 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005463 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005464
Todd Kjosd325d372016-10-10 10:40:53 -07005465 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005466 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005467
Sherry Yang91004422017-08-22 17:26:57 -07005468 binder_alloc_print_pages(m, &proc->alloc);
5469
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005470 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005471 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005472 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005473 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005474 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005475 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005476 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005477 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005478
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005479 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005480}
5481
5482
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005483static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005484{
5485 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005486 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005487 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005488
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005489 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005490
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005491 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005492 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005493 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005494 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5495 /*
5496 * take a temporary reference on the node so it
5497 * survives and isn't removed from the list
5498 * while we print it.
5499 */
5500 node->tmp_refs++;
5501 spin_unlock(&binder_dead_nodes_lock);
5502 if (last_node)
5503 binder_put_node(last_node);
5504 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005505 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005506 binder_node_unlock(node);
5507 last_node = node;
5508 spin_lock(&binder_dead_nodes_lock);
5509 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005510 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005511 if (last_node)
5512 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005513
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005514 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005515 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005516 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005517 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005518
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005519 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005520}
5521
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005522static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005523{
5524 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005525
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005526 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005527
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005528 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005529
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005530 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005531 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005532 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005533 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005534
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005535 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005536}
5537
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005538static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005539{
5540 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005541
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005542 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005543 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005544 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005545 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005546 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005547
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005548 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005549}
5550
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005551static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005552{
Riley Andrews83050a42016-02-09 21:05:33 -08005553 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005554 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005555
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005556 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005557 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005558 if (itr->pid == pid) {
5559 seq_puts(m, "binder proc state:\n");
5560 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005561 }
5562 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005563 mutex_unlock(&binder_procs_lock);
5564
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005565 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005566}
5567
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005568static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005569 struct binder_transaction_log_entry *e)
5570{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005571 int debug_id = READ_ONCE(e->debug_id_done);
5572 /*
5573 * read barrier to guarantee debug_id_done read before
5574 * we print the log values
5575 */
5576 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005577 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005578 "%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 -07005579 e->debug_id, (e->call_type == 2) ? "reply" :
5580 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005581 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005582 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5583 e->return_error, e->return_error_param,
5584 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005585 /*
5586 * read-barrier to guarantee read of debug_id_done after
5587 * done printing the fields of the entry
5588 */
5589 smp_rmb();
5590 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5591 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005592}
5593
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005594static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005595{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005596 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005597 unsigned int log_cur = atomic_read(&log->cur);
5598 unsigned int count;
5599 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005600 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005601
Todd Kjos1cfe6272017-05-24 13:33:28 -07005602 count = log_cur + 1;
5603 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5604 0 : count % ARRAY_SIZE(log->entry);
5605 if (count > ARRAY_SIZE(log->entry) || log->full)
5606 count = ARRAY_SIZE(log->entry);
5607 for (i = 0; i < count; i++) {
5608 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5609
5610 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005611 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005612 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005613}
5614
5615static const struct file_operations binder_fops = {
5616 .owner = THIS_MODULE,
5617 .poll = binder_poll,
5618 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005619 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005620 .mmap = binder_mmap,
5621 .open = binder_open,
5622 .flush = binder_flush,
5623 .release = binder_release,
5624};
5625
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005626BINDER_DEBUG_ENTRY(state);
5627BINDER_DEBUG_ENTRY(stats);
5628BINDER_DEBUG_ENTRY(transactions);
5629BINDER_DEBUG_ENTRY(transaction_log);
5630
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005631static int __init init_binder_device(const char *name)
5632{
5633 int ret;
5634 struct binder_device *binder_device;
5635
5636 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5637 if (!binder_device)
5638 return -ENOMEM;
5639
5640 binder_device->miscdev.fops = &binder_fops;
5641 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5642 binder_device->miscdev.name = name;
5643
5644 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5645 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005646 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005647
5648 ret = misc_register(&binder_device->miscdev);
5649 if (ret < 0) {
5650 kfree(binder_device);
5651 return ret;
5652 }
5653
5654 hlist_add_head(&binder_device->hlist, &binder_devices);
5655
5656 return ret;
5657}
5658
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005659static int __init binder_init(void)
5660{
5661 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005662 char *device_name, *device_names;
5663 struct binder_device *device;
5664 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005665
Sherry Yang5828d702017-07-29 13:24:11 -07005666 binder_alloc_shrinker_init();
5667
Todd Kjos1cfe6272017-05-24 13:33:28 -07005668 atomic_set(&binder_transaction_log.cur, ~0U);
5669 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5670
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005671 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5672 if (binder_debugfs_dir_entry_root)
5673 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5674 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005675
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005676 if (binder_debugfs_dir_entry_root) {
5677 debugfs_create_file("state",
5678 S_IRUGO,
5679 binder_debugfs_dir_entry_root,
5680 NULL,
5681 &binder_state_fops);
5682 debugfs_create_file("stats",
5683 S_IRUGO,
5684 binder_debugfs_dir_entry_root,
5685 NULL,
5686 &binder_stats_fops);
5687 debugfs_create_file("transactions",
5688 S_IRUGO,
5689 binder_debugfs_dir_entry_root,
5690 NULL,
5691 &binder_transactions_fops);
5692 debugfs_create_file("transaction_log",
5693 S_IRUGO,
5694 binder_debugfs_dir_entry_root,
5695 &binder_transaction_log,
5696 &binder_transaction_log_fops);
5697 debugfs_create_file("failed_transaction_log",
5698 S_IRUGO,
5699 binder_debugfs_dir_entry_root,
5700 &binder_transaction_log_failed,
5701 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005702 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005703
5704 /*
5705 * Copy the module_parameter string, because we don't want to
5706 * tokenize it in-place.
5707 */
5708 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5709 if (!device_names) {
5710 ret = -ENOMEM;
5711 goto err_alloc_device_names_failed;
5712 }
5713 strcpy(device_names, binder_devices_param);
5714
5715 while ((device_name = strsep(&device_names, ","))) {
5716 ret = init_binder_device(device_name);
5717 if (ret)
5718 goto err_init_binder_device_failed;
5719 }
5720
5721 return ret;
5722
5723err_init_binder_device_failed:
5724 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5725 misc_deregister(&device->miscdev);
5726 hlist_del(&device->hlist);
5727 kfree(device);
5728 }
5729err_alloc_device_names_failed:
5730 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5731
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005732 return ret;
5733}
5734
5735device_initcall(binder_init);
5736
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005737#define CREATE_TRACE_POINTS
5738#include "binder_trace.h"
5739
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005740MODULE_LICENSE("GPL v2");