blob: 1ef2f68bfcb2fb1c7cea1538e7a2c163e193142b [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 {
Todd Kjoseb28a0f2017-11-10 15:30:27 -0800468 BINDER_DEFERRED_FLUSH = 0x01,
469 BINDER_DEFERRED_RELEASE = 0x02,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900470};
471
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700472/**
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700473 * struct binder_priority - scheduler policy and priority
474 * @sched_policy scheduler policy
475 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
476 *
477 * The binder driver supports inheriting the following scheduler policies:
478 * SCHED_NORMAL
479 * SCHED_BATCH
480 * SCHED_FIFO
481 * SCHED_RR
482 */
483struct binder_priority {
484 unsigned int sched_policy;
485 int prio;
486};
487
488/**
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700489 * struct binder_proc - binder process bookkeeping
490 * @proc_node: element for binder_procs list
491 * @threads: rbtree of binder_threads in this proc
Todd Kjosb4827902017-05-25 15:52:17 -0700492 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700493 * @nodes: rbtree of binder nodes associated with
494 * this proc ordered by node->ptr
Todd Kjos425d23f2017-06-12 12:07:26 -0700495 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700496 * @refs_by_desc: rbtree of refs ordered by ref->desc
Todd Kjos5346bf32016-10-20 16:43:34 -0700497 * (protected by @outer_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700498 * @refs_by_node: rbtree of refs ordered by ref->node
Todd Kjos5346bf32016-10-20 16:43:34 -0700499 * (protected by @outer_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700500 * @waiting_threads: threads currently waiting for proc work
501 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700502 * @pid PID of group_leader of process
503 * (invariant after initialized)
504 * @tsk task_struct for group_leader of process
505 * (invariant after initialized)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700506 * @deferred_work_node: element for binder_deferred_list
507 * (protected by binder_deferred_lock)
508 * @deferred_work: bitmap of deferred work to perform
509 * (protected by binder_deferred_lock)
510 * @is_dead: process is dead and awaiting free
511 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700512 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700513 * @todo: list of work for this process
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700514 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700515 * @wait: wait queue head to wait for proc work
516 * (invariant after initialized)
517 * @stats: per-process binder statistics
518 * (atomics, no lock needed)
519 * @delivered_death: list of delivered death notification
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700520 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700521 * @max_threads: cap on number of binder threads
Todd Kjosd600e902017-05-25 17:35:02 -0700522 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700523 * @requested_threads: number of binder threads requested but not
524 * yet started. In current implementation, can
525 * only be 0 or 1.
Todd Kjosd600e902017-05-25 17:35:02 -0700526 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700527 * @requested_threads_started: number binder threads started
Todd Kjosd600e902017-05-25 17:35:02 -0700528 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700529 * @tmp_ref: temporary reference to indicate proc is in use
Todd Kjosb4827902017-05-25 15:52:17 -0700530 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700531 * @default_priority: default scheduler priority
532 * (invariant after initialized)
533 * @debugfs_entry: debugfs node
534 * @alloc: binder allocator bookkeeping
535 * @context: binder_context for this proc
536 * (invariant after initialized)
537 * @inner_lock: can nest under outer_lock and/or node lock
538 * @outer_lock: no nesting under innor or node lock
539 * Lock order: 1) outer, 2) node, 3) inner
540 *
541 * Bookkeeping structure for binder processes
542 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900543struct binder_proc {
544 struct hlist_node proc_node;
545 struct rb_root threads;
546 struct rb_root nodes;
547 struct rb_root refs_by_desc;
548 struct rb_root refs_by_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700549 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900550 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900551 struct task_struct *tsk;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900552 struct hlist_node deferred_work_node;
553 int deferred_work;
Todd Kjos2f993e22017-05-12 14:42:55 -0700554 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900555
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900556 struct list_head todo;
557 wait_queue_head_t wait;
558 struct binder_stats stats;
559 struct list_head delivered_death;
560 int max_threads;
561 int requested_threads;
562 int requested_threads_started;
Todd Kjos2f993e22017-05-12 14:42:55 -0700563 int tmp_ref;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700564 struct binder_priority default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700565 struct dentry *debugfs_entry;
Todd Kjosf85d2292016-10-10 10:39:59 -0700566 struct binder_alloc alloc;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200567 struct binder_context *context;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700568 spinlock_t inner_lock;
569 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900570};
571
572enum {
573 BINDER_LOOPER_STATE_REGISTERED = 0x01,
574 BINDER_LOOPER_STATE_ENTERED = 0x02,
575 BINDER_LOOPER_STATE_EXITED = 0x04,
576 BINDER_LOOPER_STATE_INVALID = 0x08,
577 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700578 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900579};
580
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700581/**
582 * struct binder_thread - binder thread bookkeeping
583 * @proc: binder process for this thread
584 * (invariant after initialization)
585 * @rb_node: element for proc->threads rbtree
Todd Kjosb4827902017-05-25 15:52:17 -0700586 * (protected by @proc->inner_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700587 * @waiting_thread_node: element for @proc->waiting_threads list
588 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700589 * @pid: PID for this thread
590 * (invariant after initialization)
591 * @looper: bitmap of looping state
592 * (only accessed by this thread)
593 * @looper_needs_return: looping thread needs to exit driver
594 * (no lock needed)
595 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700596 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700597 * @todo: list of work to do for this thread
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700598 * (protected by @proc->inner_lock)
Martijn Coenen77fdc9d2017-10-19 15:04:46 +0200599 * @process_todo: whether work in @todo should be processed
600 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700601 * @return_error: transaction errors reported by this thread
602 * (only accessed by this thread)
603 * @reply_error: transaction errors reported by target thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700604 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700605 * @wait: wait queue for thread work
606 * @stats: per-thread statistics
607 * (atomics, no lock needed)
608 * @tmp_ref: temporary reference to indicate thread is in use
609 * (atomic since @proc->inner_lock cannot
610 * always be acquired)
611 * @is_dead: thread is dead and awaiting free
612 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700613 * (protected by @proc->inner_lock)
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700614 * @task: struct task_struct for this thread
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700615 *
616 * Bookkeeping structure for binder threads.
617 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900618struct binder_thread {
619 struct binder_proc *proc;
620 struct rb_node rb_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700621 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900622 int pid;
Todd Kjos6798e6d2017-01-06 14:19:25 -0800623 int looper; /* only modified by this thread */
624 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900625 struct binder_transaction *transaction_stack;
626 struct list_head todo;
Martijn Coenen77fdc9d2017-10-19 15:04:46 +0200627 bool process_todo;
Todd Kjos858b8da2017-04-21 17:35:12 -0700628 struct binder_error return_error;
629 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900630 wait_queue_head_t wait;
631 struct binder_stats stats;
Todd Kjos2f993e22017-05-12 14:42:55 -0700632 atomic_t tmp_ref;
633 bool is_dead;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700634 struct task_struct *task;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900635};
636
637struct binder_transaction {
638 int debug_id;
639 struct binder_work work;
640 struct binder_thread *from;
641 struct binder_transaction *from_parent;
642 struct binder_proc *to_proc;
643 struct binder_thread *to_thread;
644 struct binder_transaction *to_parent;
645 unsigned need_reply:1;
646 /* unsigned is_dead:1; */ /* not used at the moment */
647
648 struct binder_buffer *buffer;
649 unsigned int code;
650 unsigned int flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700651 struct binder_priority priority;
652 struct binder_priority saved_priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700653 bool set_priority_called;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600654 kuid_t sender_euid;
Todd Kjos2f993e22017-05-12 14:42:55 -0700655 /**
656 * @lock: protects @from, @to_proc, and @to_thread
657 *
658 * @from, @to_proc, and @to_thread can be set to NULL
659 * during thread teardown
660 */
661 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900662};
663
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700664/**
665 * binder_proc_lock() - Acquire outer lock for given binder_proc
666 * @proc: struct binder_proc to acquire
667 *
668 * Acquires proc->outer_lock. Used to protect binder_ref
669 * structures associated with the given proc.
670 */
671#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
672static void
673_binder_proc_lock(struct binder_proc *proc, int line)
674{
675 binder_debug(BINDER_DEBUG_SPINLOCKS,
676 "%s: line=%d\n", __func__, line);
677 spin_lock(&proc->outer_lock);
678}
679
680/**
681 * binder_proc_unlock() - Release spinlock for given binder_proc
682 * @proc: struct binder_proc to acquire
683 *
684 * Release lock acquired via binder_proc_lock()
685 */
686#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
687static void
688_binder_proc_unlock(struct binder_proc *proc, int line)
689{
690 binder_debug(BINDER_DEBUG_SPINLOCKS,
691 "%s: line=%d\n", __func__, line);
692 spin_unlock(&proc->outer_lock);
693}
694
695/**
696 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
697 * @proc: struct binder_proc to acquire
698 *
699 * Acquires proc->inner_lock. Used to protect todo lists
700 */
701#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
702static void
703_binder_inner_proc_lock(struct binder_proc *proc, int line)
704{
705 binder_debug(BINDER_DEBUG_SPINLOCKS,
706 "%s: line=%d\n", __func__, line);
707 spin_lock(&proc->inner_lock);
708}
709
710/**
711 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
712 * @proc: struct binder_proc to acquire
713 *
714 * Release lock acquired via binder_inner_proc_lock()
715 */
716#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
717static void
718_binder_inner_proc_unlock(struct binder_proc *proc, int line)
719{
720 binder_debug(BINDER_DEBUG_SPINLOCKS,
721 "%s: line=%d\n", __func__, line);
722 spin_unlock(&proc->inner_lock);
723}
724
725/**
726 * binder_node_lock() - Acquire spinlock for given binder_node
727 * @node: struct binder_node to acquire
728 *
729 * Acquires node->lock. Used to protect binder_node fields
730 */
731#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
732static void
733_binder_node_lock(struct binder_node *node, int line)
734{
735 binder_debug(BINDER_DEBUG_SPINLOCKS,
736 "%s: line=%d\n", __func__, line);
737 spin_lock(&node->lock);
738}
739
740/**
741 * binder_node_unlock() - Release spinlock for given binder_proc
742 * @node: struct binder_node to acquire
743 *
744 * Release lock acquired via binder_node_lock()
745 */
746#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
747static void
748_binder_node_unlock(struct binder_node *node, int line)
749{
750 binder_debug(BINDER_DEBUG_SPINLOCKS,
751 "%s: line=%d\n", __func__, line);
752 spin_unlock(&node->lock);
753}
754
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700755/**
756 * binder_node_inner_lock() - Acquire node and inner locks
757 * @node: struct binder_node to acquire
758 *
759 * Acquires node->lock. If node->proc also acquires
760 * proc->inner_lock. Used to protect binder_node fields
761 */
762#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
763static void
764_binder_node_inner_lock(struct binder_node *node, int line)
765{
766 binder_debug(BINDER_DEBUG_SPINLOCKS,
767 "%s: line=%d\n", __func__, line);
768 spin_lock(&node->lock);
769 if (node->proc)
770 binder_inner_proc_lock(node->proc);
771}
772
773/**
774 * binder_node_unlock() - Release node and inner locks
775 * @node: struct binder_node to acquire
776 *
777 * Release lock acquired via binder_node_lock()
778 */
779#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
780static void
781_binder_node_inner_unlock(struct binder_node *node, int line)
782{
783 struct binder_proc *proc = node->proc;
784
785 binder_debug(BINDER_DEBUG_SPINLOCKS,
786 "%s: line=%d\n", __func__, line);
787 if (proc)
788 binder_inner_proc_unlock(proc);
789 spin_unlock(&node->lock);
790}
791
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700792static bool binder_worklist_empty_ilocked(struct list_head *list)
793{
794 return list_empty(list);
795}
796
797/**
798 * binder_worklist_empty() - Check if no items on the work list
799 * @proc: binder_proc associated with list
800 * @list: list to check
801 *
802 * Return: true if there are no items on list, else false
803 */
804static bool binder_worklist_empty(struct binder_proc *proc,
805 struct list_head *list)
806{
807 bool ret;
808
809 binder_inner_proc_lock(proc);
810 ret = binder_worklist_empty_ilocked(list);
811 binder_inner_proc_unlock(proc);
812 return ret;
813}
814
Martijn Coenen77fdc9d2017-10-19 15:04:46 +0200815/**
816 * binder_enqueue_work_ilocked() - Add an item to the work list
817 * @work: struct binder_work to add to list
818 * @target_list: list to add work to
819 *
820 * Adds the work to the specified list. Asserts that work
821 * is not already on a list.
822 *
823 * Requires the proc->inner_lock to be held.
824 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700825static void
826binder_enqueue_work_ilocked(struct binder_work *work,
827 struct list_head *target_list)
828{
829 BUG_ON(target_list == NULL);
830 BUG_ON(work->entry.next && !list_empty(&work->entry));
831 list_add_tail(&work->entry, target_list);
832}
833
834/**
Martijn Coenen21d7d4a2017-11-13 09:55:21 +0100835 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
Martijn Coenen77fdc9d2017-10-19 15:04:46 +0200836 * @thread: thread to queue work to
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700837 * @work: struct binder_work to add to list
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700838 *
Martijn Coenen77fdc9d2017-10-19 15:04:46 +0200839 * Adds the work to the todo list of the thread. Doesn't set the process_todo
840 * flag, which means that (if it wasn't already set) the thread will go to
841 * sleep without handling this work when it calls read.
842 *
843 * Requires the proc->inner_lock to be held.
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700844 */
845static void
Martijn Coenen21d7d4a2017-11-13 09:55:21 +0100846binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
847 struct binder_work *work)
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700848{
Martijn Coenen77fdc9d2017-10-19 15:04:46 +0200849 binder_enqueue_work_ilocked(work, &thread->todo);
850}
851
852/**
853 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
854 * @thread: thread to queue work to
855 * @work: struct binder_work to add to list
856 *
857 * Adds the work to the todo list of the thread, and enables processing
858 * of the todo queue.
859 *
860 * Requires the proc->inner_lock to be held.
861 */
862static void
863binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
864 struct binder_work *work)
865{
866 binder_enqueue_work_ilocked(work, &thread->todo);
867 thread->process_todo = true;
868}
869
870/**
871 * binder_enqueue_thread_work() - Add an item to the thread work list
872 * @thread: thread to queue work to
873 * @work: struct binder_work to add to list
874 *
875 * Adds the work to the todo list of the thread, and enables processing
876 * of the todo queue.
877 */
878static void
879binder_enqueue_thread_work(struct binder_thread *thread,
880 struct binder_work *work)
881{
882 binder_inner_proc_lock(thread->proc);
883 binder_enqueue_thread_work_ilocked(thread, work);
884 binder_inner_proc_unlock(thread->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700885}
886
887static void
888binder_dequeue_work_ilocked(struct binder_work *work)
889{
890 list_del_init(&work->entry);
891}
892
893/**
894 * binder_dequeue_work() - Removes an item from the work list
895 * @proc: binder_proc associated with list
896 * @work: struct binder_work to remove from list
897 *
898 * Removes the specified work item from whatever list it is on.
899 * Can safely be called if work is not on any list.
900 */
901static void
902binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
903{
904 binder_inner_proc_lock(proc);
905 binder_dequeue_work_ilocked(work);
906 binder_inner_proc_unlock(proc);
907}
908
909static struct binder_work *binder_dequeue_work_head_ilocked(
910 struct list_head *list)
911{
912 struct binder_work *w;
913
914 w = list_first_entry_or_null(list, struct binder_work, entry);
915 if (w)
916 list_del_init(&w->entry);
917 return w;
918}
919
920/**
921 * binder_dequeue_work_head() - Dequeues the item at head of list
922 * @proc: binder_proc associated with list
923 * @list: list to dequeue head
924 *
925 * Removes the head of the list if there are items on the list
926 *
927 * Return: pointer dequeued binder_work, NULL if list was empty
928 */
929static struct binder_work *binder_dequeue_work_head(
930 struct binder_proc *proc,
931 struct list_head *list)
932{
933 struct binder_work *w;
934
935 binder_inner_proc_lock(proc);
936 w = binder_dequeue_work_head_ilocked(list);
937 binder_inner_proc_unlock(proc);
938 return w;
939}
940
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900941static void
942binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos2f993e22017-05-12 14:42:55 -0700943static void binder_free_thread(struct binder_thread *thread);
944static void binder_free_proc(struct binder_proc *proc);
Todd Kjos425d23f2017-06-12 12:07:26 -0700945static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900946
Todd Kjoseb28a0f2017-11-10 15:30:27 -0800947struct files_struct *binder_get_files_struct(struct binder_proc *proc)
948{
949 return get_files_struct(proc->tsk);
950}
951
Sachin Kamatefde99c2012-08-17 16:39:36 +0530952static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900953{
Todd Kjoseb28a0f2017-11-10 15:30:27 -0800954 struct files_struct *files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900955 unsigned long rlim_cur;
956 unsigned long irqs;
Todd Kjoseb28a0f2017-11-10 15:30:27 -0800957 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900958
Todd Kjoseb28a0f2017-11-10 15:30:27 -0800959 files = binder_get_files_struct(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900960 if (files == NULL)
961 return -ESRCH;
962
Todd Kjoseb28a0f2017-11-10 15:30:27 -0800963 if (!lock_task_sighand(proc->tsk, &irqs)) {
964 ret = -EMFILE;
965 goto err;
966 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900967
Al Virodcfadfa2012-08-12 17:27:30 -0400968 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
969 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900970
Todd Kjoseb28a0f2017-11-10 15:30:27 -0800971 ret = __alloc_fd(files, 0, rlim_cur, flags);
972err:
973 put_files_struct(files);
974 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900975}
976
977/*
978 * copied from fd_install
979 */
980static void task_fd_install(
981 struct binder_proc *proc, unsigned int fd, struct file *file)
982{
Todd Kjoseb28a0f2017-11-10 15:30:27 -0800983 struct files_struct *files = binder_get_files_struct(proc);
984
985 if (files) {
986 __fd_install(files, fd, file);
987 put_files_struct(files);
988 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900989}
990
991/*
992 * copied from sys_close
993 */
994static long task_close_fd(struct binder_proc *proc, unsigned int fd)
995{
Todd Kjoseb28a0f2017-11-10 15:30:27 -0800996 struct files_struct *files = binder_get_files_struct(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900997 int retval;
998
Todd Kjoseb28a0f2017-11-10 15:30:27 -0800999 if (files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001000 return -ESRCH;
1001
Todd Kjoseb28a0f2017-11-10 15:30:27 -08001002 retval = __close_fd(files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001003 /* can't restart close syscall because file table entry was cleared */
1004 if (unlikely(retval == -ERESTARTSYS ||
1005 retval == -ERESTARTNOINTR ||
1006 retval == -ERESTARTNOHAND ||
1007 retval == -ERESTART_RESTARTBLOCK))
1008 retval = -EINTR;
Todd Kjoseb28a0f2017-11-10 15:30:27 -08001009 put_files_struct(files);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001010
1011 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001012}
1013
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001014static bool binder_has_work_ilocked(struct binder_thread *thread,
1015 bool do_proc_work)
1016{
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02001017 return thread->process_todo ||
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001018 thread->looper_need_return ||
1019 (do_proc_work &&
1020 !binder_worklist_empty_ilocked(&thread->proc->todo));
1021}
1022
1023static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
1024{
1025 bool has_work;
1026
1027 binder_inner_proc_lock(thread->proc);
1028 has_work = binder_has_work_ilocked(thread, do_proc_work);
1029 binder_inner_proc_unlock(thread->proc);
1030
1031 return has_work;
1032}
1033
1034static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1035{
1036 return !thread->transaction_stack &&
1037 binder_worklist_empty_ilocked(&thread->todo) &&
1038 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1039 BINDER_LOOPER_STATE_REGISTERED));
1040}
1041
1042static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1043 bool sync)
1044{
1045 struct rb_node *n;
1046 struct binder_thread *thread;
1047
1048 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1049 thread = rb_entry(n, struct binder_thread, rb_node);
1050 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1051 binder_available_for_proc_work_ilocked(thread)) {
1052 if (sync)
1053 wake_up_interruptible_sync(&thread->wait);
1054 else
1055 wake_up_interruptible(&thread->wait);
1056 }
1057 }
1058}
1059
Martijn Coenen053be422017-06-06 15:17:46 -07001060/**
1061 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1062 * @proc: process to select a thread from
1063 *
1064 * Note that calling this function moves the thread off the waiting_threads
1065 * list, so it can only be woken up by the caller of this function, or a
1066 * signal. Therefore, callers *should* always wake up the thread this function
1067 * returns.
1068 *
1069 * Return: If there's a thread currently waiting for process work,
1070 * returns that thread. Otherwise returns NULL.
1071 */
1072static struct binder_thread *
1073binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001074{
1075 struct binder_thread *thread;
1076
Martijn Coenened323352017-07-27 23:52:24 +02001077 assert_spin_locked(&proc->inner_lock);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001078 thread = list_first_entry_or_null(&proc->waiting_threads,
1079 struct binder_thread,
1080 waiting_thread_node);
1081
Martijn Coenen053be422017-06-06 15:17:46 -07001082 if (thread)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001083 list_del_init(&thread->waiting_thread_node);
Martijn Coenen053be422017-06-06 15:17:46 -07001084
1085 return thread;
1086}
1087
1088/**
1089 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1090 * @proc: process to wake up a thread in
1091 * @thread: specific thread to wake-up (may be NULL)
1092 * @sync: whether to do a synchronous wake-up
1093 *
1094 * This function wakes up a thread in the @proc process.
1095 * The caller may provide a specific thread to wake-up in
1096 * the @thread parameter. If @thread is NULL, this function
1097 * will wake up threads that have called poll().
1098 *
1099 * Note that for this function to work as expected, callers
1100 * should first call binder_select_thread() to find a thread
1101 * to handle the work (if they don't have a thread already),
1102 * and pass the result into the @thread parameter.
1103 */
1104static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1105 struct binder_thread *thread,
1106 bool sync)
1107{
Martijn Coenened323352017-07-27 23:52:24 +02001108 assert_spin_locked(&proc->inner_lock);
Martijn Coenen053be422017-06-06 15:17:46 -07001109
1110 if (thread) {
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001111 if (sync)
1112 wake_up_interruptible_sync(&thread->wait);
1113 else
1114 wake_up_interruptible(&thread->wait);
1115 return;
1116 }
1117
1118 /* Didn't find a thread waiting for proc work; this can happen
1119 * in two scenarios:
1120 * 1. All threads are busy handling transactions
1121 * In that case, one of those threads should call back into
1122 * the kernel driver soon and pick up this work.
1123 * 2. Threads are using the (e)poll interface, in which case
1124 * they may be blocked on the waitqueue without having been
1125 * added to waiting_threads. For this case, we just iterate
1126 * over all threads not handling transaction work, and
1127 * wake them all up. We wake all because we don't know whether
1128 * a thread that called into (e)poll is handling non-binder
1129 * work currently.
1130 */
1131 binder_wakeup_poll_threads_ilocked(proc, sync);
1132}
1133
Martijn Coenen053be422017-06-06 15:17:46 -07001134static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1135{
1136 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1137
1138 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1139}
1140
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001141static bool is_rt_policy(int policy)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001142{
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001143 return policy == SCHED_FIFO || policy == SCHED_RR;
1144}
Seunghun Lee10f62862014-05-01 01:30:23 +09001145
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001146static bool is_fair_policy(int policy)
1147{
1148 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
1149}
1150
1151static bool binder_supported_policy(int policy)
1152{
1153 return is_fair_policy(policy) || is_rt_policy(policy);
1154}
1155
1156static int to_userspace_prio(int policy, int kernel_priority)
1157{
1158 if (is_fair_policy(policy))
1159 return PRIO_TO_NICE(kernel_priority);
1160 else
1161 return MAX_USER_RT_PRIO - 1 - kernel_priority;
1162}
1163
1164static int to_kernel_prio(int policy, int user_priority)
1165{
1166 if (is_fair_policy(policy))
1167 return NICE_TO_PRIO(user_priority);
1168 else
1169 return MAX_USER_RT_PRIO - 1 - user_priority;
1170}
1171
Martijn Coenenecd972d2017-05-26 10:48:56 -07001172static void binder_do_set_priority(struct task_struct *task,
1173 struct binder_priority desired,
1174 bool verify)
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001175{
1176 int priority; /* user-space prio value */
1177 bool has_cap_nice;
1178 unsigned int policy = desired.sched_policy;
1179
1180 if (task->policy == policy && task->normal_prio == desired.prio)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001181 return;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001182
1183 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1184
1185 priority = to_userspace_prio(policy, desired.prio);
1186
Martijn Coenenecd972d2017-05-26 10:48:56 -07001187 if (verify && is_rt_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001188 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1189
1190 if (max_rtprio == 0) {
1191 policy = SCHED_NORMAL;
1192 priority = MIN_NICE;
1193 } else if (priority > max_rtprio) {
1194 priority = max_rtprio;
1195 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001196 }
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001197
Martijn Coenenecd972d2017-05-26 10:48:56 -07001198 if (verify && is_fair_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001199 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1200
1201 if (min_nice > MAX_NICE) {
1202 binder_user_error("%d RLIMIT_NICE not set\n",
1203 task->pid);
1204 return;
1205 } else if (priority < min_nice) {
1206 priority = min_nice;
1207 }
1208 }
1209
1210 if (policy != desired.sched_policy ||
1211 to_kernel_prio(policy, priority) != desired.prio)
1212 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1213 "%d: priority %d not allowed, using %d instead\n",
1214 task->pid, desired.prio,
1215 to_kernel_prio(policy, priority));
1216
Martijn Coenen81402ea2017-05-08 09:33:22 -07001217 trace_binder_set_priority(task->tgid, task->pid, task->normal_prio,
1218 to_kernel_prio(policy, priority),
1219 desired.prio);
1220
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001221 /* Set the actual priority */
1222 if (task->policy != policy || is_rt_policy(policy)) {
1223 struct sched_param params;
1224
1225 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1226
1227 sched_setscheduler_nocheck(task,
1228 policy | SCHED_RESET_ON_FORK,
1229 &params);
1230 }
1231 if (is_fair_policy(policy))
1232 set_user_nice(task, priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001233}
1234
Martijn Coenenecd972d2017-05-26 10:48:56 -07001235static void binder_set_priority(struct task_struct *task,
1236 struct binder_priority desired)
1237{
1238 binder_do_set_priority(task, desired, /* verify = */ true);
1239}
1240
1241static void binder_restore_priority(struct task_struct *task,
1242 struct binder_priority desired)
1243{
1244 binder_do_set_priority(task, desired, /* verify = */ false);
1245}
1246
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001247static void binder_transaction_priority(struct task_struct *task,
1248 struct binder_transaction *t,
Martijn Coenenc46810c2017-06-23 10:13:43 -07001249 struct binder_priority node_prio,
1250 bool inherit_rt)
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001251{
Ganesh Mahendran598fd892017-09-27 15:12:25 +08001252 struct binder_priority desired_prio = t->priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001253
1254 if (t->set_priority_called)
1255 return;
1256
1257 t->set_priority_called = true;
1258 t->saved_priority.sched_policy = task->policy;
1259 t->saved_priority.prio = task->normal_prio;
1260
Martijn Coenenc46810c2017-06-23 10:13:43 -07001261 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1262 desired_prio.prio = NICE_TO_PRIO(0);
1263 desired_prio.sched_policy = SCHED_NORMAL;
Martijn Coenenc46810c2017-06-23 10:13:43 -07001264 }
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001265
1266 if (node_prio.prio < t->priority.prio ||
1267 (node_prio.prio == t->priority.prio &&
1268 node_prio.sched_policy == SCHED_FIFO)) {
1269 /*
1270 * In case the minimum priority on the node is
1271 * higher (lower value), use that priority. If
1272 * the priority is the same, but the node uses
1273 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1274 * run unbounded, unlike SCHED_RR.
1275 */
1276 desired_prio = node_prio;
1277 }
1278
1279 binder_set_priority(task, desired_prio);
1280}
1281
Todd Kjos425d23f2017-06-12 12:07:26 -07001282static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1283 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001284{
1285 struct rb_node *n = proc->nodes.rb_node;
1286 struct binder_node *node;
1287
Martijn Coenened323352017-07-27 23:52:24 +02001288 assert_spin_locked(&proc->inner_lock);
Todd Kjos425d23f2017-06-12 12:07:26 -07001289
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001290 while (n) {
1291 node = rb_entry(n, struct binder_node, rb_node);
1292
1293 if (ptr < node->ptr)
1294 n = n->rb_left;
1295 else if (ptr > node->ptr)
1296 n = n->rb_right;
Todd Kjosf22abc72017-05-09 11:08:05 -07001297 else {
1298 /*
1299 * take an implicit weak reference
1300 * to ensure node stays alive until
1301 * call to binder_put_node()
1302 */
Todd Kjos425d23f2017-06-12 12:07:26 -07001303 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001304 return node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001305 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001306 }
1307 return NULL;
1308}
1309
Todd Kjos425d23f2017-06-12 12:07:26 -07001310static struct binder_node *binder_get_node(struct binder_proc *proc,
1311 binder_uintptr_t ptr)
1312{
1313 struct binder_node *node;
1314
1315 binder_inner_proc_lock(proc);
1316 node = binder_get_node_ilocked(proc, ptr);
1317 binder_inner_proc_unlock(proc);
1318 return node;
1319}
1320
1321static struct binder_node *binder_init_node_ilocked(
1322 struct binder_proc *proc,
1323 struct binder_node *new_node,
1324 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001325{
1326 struct rb_node **p = &proc->nodes.rb_node;
1327 struct rb_node *parent = NULL;
1328 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001329 binder_uintptr_t ptr = fp ? fp->binder : 0;
1330 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1331 __u32 flags = fp ? fp->flags : 0;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001332 s8 priority;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001333
Martijn Coenened323352017-07-27 23:52:24 +02001334 assert_spin_locked(&proc->inner_lock);
1335
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001336 while (*p) {
Todd Kjos425d23f2017-06-12 12:07:26 -07001337
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001338 parent = *p;
1339 node = rb_entry(parent, struct binder_node, rb_node);
1340
1341 if (ptr < node->ptr)
1342 p = &(*p)->rb_left;
1343 else if (ptr > node->ptr)
1344 p = &(*p)->rb_right;
Todd Kjos425d23f2017-06-12 12:07:26 -07001345 else {
1346 /*
1347 * A matching node is already in
1348 * the rb tree. Abandon the init
1349 * and return it.
1350 */
1351 binder_inc_node_tmpref_ilocked(node);
1352 return node;
1353 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001354 }
Todd Kjos425d23f2017-06-12 12:07:26 -07001355 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001356 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosf22abc72017-05-09 11:08:05 -07001357 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001358 rb_link_node(&node->rb_node, parent, p);
1359 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -07001360 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001361 node->proc = proc;
1362 node->ptr = ptr;
1363 node->cookie = cookie;
1364 node->work.type = BINDER_WORK_NODE;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001365 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
Ganesh Mahendranefdd9932017-09-26 17:56:25 +08001366 node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >>
Martijn Coenen6aac9792017-06-07 09:29:14 -07001367 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1368 node->min_priority = to_kernel_prio(node->sched_policy, priority);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001369 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Martijn Coenenc46810c2017-06-23 10:13:43 -07001370 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
Todd Kjosfc7a7e22017-05-29 16:44:24 -07001371 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001372 INIT_LIST_HEAD(&node->work.entry);
1373 INIT_LIST_HEAD(&node->async_todo);
1374 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001375 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001376 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001377 (u64)node->ptr, (u64)node->cookie);
Todd Kjos425d23f2017-06-12 12:07:26 -07001378
1379 return node;
1380}
1381
1382static struct binder_node *binder_new_node(struct binder_proc *proc,
1383 struct flat_binder_object *fp)
1384{
1385 struct binder_node *node;
1386 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1387
1388 if (!new_node)
1389 return NULL;
1390 binder_inner_proc_lock(proc);
1391 node = binder_init_node_ilocked(proc, new_node, fp);
1392 binder_inner_proc_unlock(proc);
1393 if (node != new_node)
1394 /*
1395 * The node was already added by another thread
1396 */
1397 kfree(new_node);
1398
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001399 return node;
1400}
1401
Todd Kjose7f23ed2017-03-21 13:06:01 -07001402static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001403{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001404 kfree(node);
1405 binder_stats_deleted(BINDER_STAT_NODE);
1406}
1407
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001408static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1409 int internal,
1410 struct list_head *target_list)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001411{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001412 struct binder_proc *proc = node->proc;
1413
Martijn Coenened323352017-07-27 23:52:24 +02001414 assert_spin_locked(&node->lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001415 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001416 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001417 if (strong) {
1418 if (internal) {
1419 if (target_list == NULL &&
1420 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001421 !(node->proc &&
1422 node == node->proc->context->
1423 binder_context_mgr_node &&
1424 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301425 pr_err("invalid inc strong node for %d\n",
1426 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001427 return -EINVAL;
1428 }
1429 node->internal_strong_refs++;
1430 } else
1431 node->local_strong_refs++;
1432 if (!node->has_strong_ref && target_list) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001433 binder_dequeue_work_ilocked(&node->work);
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02001434 /*
1435 * Note: this function is the only place where we queue
1436 * directly to a thread->todo without using the
1437 * corresponding binder_enqueue_thread_work() helper
1438 * functions; in this case it's ok to not set the
1439 * process_todo flag, since we know this node work will
1440 * always be followed by other work that starts queue
1441 * processing: in case of synchronous transactions, a
1442 * BR_REPLY or BR_ERROR; in case of oneway
1443 * transactions, a BR_TRANSACTION_COMPLETE.
1444 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001445 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001446 }
1447 } else {
1448 if (!internal)
1449 node->local_weak_refs++;
1450 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1451 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301452 pr_err("invalid inc weak node for %d\n",
1453 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001454 return -EINVAL;
1455 }
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02001456 /*
1457 * See comment above
1458 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001459 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001460 }
1461 }
1462 return 0;
1463}
1464
Todd Kjose7f23ed2017-03-21 13:06:01 -07001465static int binder_inc_node(struct binder_node *node, int strong, int internal,
1466 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001467{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001468 int ret;
1469
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001470 binder_node_inner_lock(node);
1471 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1472 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001473
1474 return ret;
1475}
1476
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001477static bool binder_dec_node_nilocked(struct binder_node *node,
1478 int strong, int internal)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001479{
1480 struct binder_proc *proc = node->proc;
1481
Martijn Coenened323352017-07-27 23:52:24 +02001482 assert_spin_locked(&node->lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001483 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001484 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001485 if (strong) {
1486 if (internal)
1487 node->internal_strong_refs--;
1488 else
1489 node->local_strong_refs--;
1490 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001491 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001492 } else {
1493 if (!internal)
1494 node->local_weak_refs--;
Todd Kjosf22abc72017-05-09 11:08:05 -07001495 if (node->local_weak_refs || node->tmp_refs ||
1496 !hlist_empty(&node->refs))
Todd Kjose7f23ed2017-03-21 13:06:01 -07001497 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001498 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001499
1500 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001501 if (list_empty(&node->work.entry)) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001502 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07001503 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001504 }
1505 } else {
1506 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosf22abc72017-05-09 11:08:05 -07001507 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07001508 if (proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001509 binder_dequeue_work_ilocked(&node->work);
1510 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001511 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301512 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001513 node->debug_id);
1514 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001515 BUG_ON(!list_empty(&node->work.entry));
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001516 spin_lock(&binder_dead_nodes_lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001517 /*
1518 * tmp_refs could have changed so
1519 * check it again
1520 */
1521 if (node->tmp_refs) {
1522 spin_unlock(&binder_dead_nodes_lock);
1523 return false;
1524 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001525 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001526 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001527 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301528 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001529 node->debug_id);
1530 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001531 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001532 }
1533 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001534 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001535}
1536
Todd Kjose7f23ed2017-03-21 13:06:01 -07001537static void binder_dec_node(struct binder_node *node, int strong, int internal)
1538{
1539 bool free_node;
1540
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001541 binder_node_inner_lock(node);
1542 free_node = binder_dec_node_nilocked(node, strong, internal);
1543 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001544 if (free_node)
1545 binder_free_node(node);
1546}
1547
1548static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosf22abc72017-05-09 11:08:05 -07001549{
1550 /*
1551 * No call to binder_inc_node() is needed since we
1552 * don't need to inform userspace of any changes to
1553 * tmp_refs
1554 */
1555 node->tmp_refs++;
1556}
1557
1558/**
Todd Kjose7f23ed2017-03-21 13:06:01 -07001559 * binder_inc_node_tmpref() - take a temporary reference on node
1560 * @node: node to reference
1561 *
1562 * Take reference on node to prevent the node from being freed
1563 * while referenced only by a local variable. The inner lock is
1564 * needed to serialize with the node work on the queue (which
1565 * isn't needed after the node is dead). If the node is dead
1566 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1567 * node->tmp_refs against dead-node-only cases where the node
1568 * lock cannot be acquired (eg traversing the dead node list to
1569 * print nodes)
1570 */
1571static void binder_inc_node_tmpref(struct binder_node *node)
1572{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001573 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001574 if (node->proc)
1575 binder_inner_proc_lock(node->proc);
1576 else
1577 spin_lock(&binder_dead_nodes_lock);
1578 binder_inc_node_tmpref_ilocked(node);
1579 if (node->proc)
1580 binder_inner_proc_unlock(node->proc);
1581 else
1582 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001583 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001584}
1585
1586/**
Todd Kjosf22abc72017-05-09 11:08:05 -07001587 * binder_dec_node_tmpref() - remove a temporary reference on node
1588 * @node: node to reference
1589 *
1590 * Release temporary reference on node taken via binder_inc_node_tmpref()
1591 */
1592static void binder_dec_node_tmpref(struct binder_node *node)
1593{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001594 bool free_node;
1595
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001596 binder_node_inner_lock(node);
1597 if (!node->proc)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001598 spin_lock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001599 node->tmp_refs--;
1600 BUG_ON(node->tmp_refs < 0);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001601 if (!node->proc)
1602 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001603 /*
1604 * Call binder_dec_node() to check if all refcounts are 0
1605 * and cleanup is needed. Calling with strong=0 and internal=1
1606 * causes no actual reference to be released in binder_dec_node().
1607 * If that changes, a change is needed here too.
1608 */
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001609 free_node = binder_dec_node_nilocked(node, 0, 1);
1610 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001611 if (free_node)
1612 binder_free_node(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07001613}
1614
1615static void binder_put_node(struct binder_node *node)
1616{
1617 binder_dec_node_tmpref(node);
1618}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001619
Todd Kjos5346bf32016-10-20 16:43:34 -07001620static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1621 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001622{
1623 struct rb_node *n = proc->refs_by_desc.rb_node;
1624 struct binder_ref *ref;
1625
1626 while (n) {
1627 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1628
Todd Kjosb0117bb2017-05-08 09:16:27 -07001629 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001630 n = n->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001631 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001632 n = n->rb_right;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001633 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001634 binder_user_error("tried to use weak ref as strong ref\n");
1635 return NULL;
1636 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001637 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001638 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001639 }
1640 return NULL;
1641}
1642
Todd Kjosb0117bb2017-05-08 09:16:27 -07001643/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001644 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjosb0117bb2017-05-08 09:16:27 -07001645 * @proc: binder_proc that owns the ref
1646 * @node: binder_node of target
1647 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1648 *
1649 * Look up the ref for the given node and return it if it exists
1650 *
1651 * If it doesn't exist and the caller provides a newly allocated
1652 * ref, initialize the fields of the newly allocated ref and insert
1653 * into the given proc rb_trees and node refs list.
1654 *
1655 * Return: the ref for node. It is possible that another thread
1656 * allocated/initialized the ref first in which case the
1657 * returned ref would be different than the passed-in
1658 * new_ref. new_ref must be kfree'd by the caller in
1659 * this case.
1660 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001661static struct binder_ref *binder_get_ref_for_node_olocked(
1662 struct binder_proc *proc,
1663 struct binder_node *node,
1664 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001665{
Todd Kjosb0117bb2017-05-08 09:16:27 -07001666 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001667 struct rb_node **p = &proc->refs_by_node.rb_node;
1668 struct rb_node *parent = NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001669 struct binder_ref *ref;
1670 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001671
1672 while (*p) {
1673 parent = *p;
1674 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1675
1676 if (node < ref->node)
1677 p = &(*p)->rb_left;
1678 else if (node > ref->node)
1679 p = &(*p)->rb_right;
1680 else
1681 return ref;
1682 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001683 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001684 return NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001685
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001686 binder_stats_created(BINDER_STAT_REF);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001687 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001688 new_ref->proc = proc;
1689 new_ref->node = node;
1690 rb_link_node(&new_ref->rb_node_node, parent, p);
1691 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1692
Todd Kjosb0117bb2017-05-08 09:16:27 -07001693 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001694 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1695 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001696 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001697 break;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001698 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001699 }
1700
1701 p = &proc->refs_by_desc.rb_node;
1702 while (*p) {
1703 parent = *p;
1704 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1705
Todd Kjosb0117bb2017-05-08 09:16:27 -07001706 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001707 p = &(*p)->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001708 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001709 p = &(*p)->rb_right;
1710 else
1711 BUG();
1712 }
1713 rb_link_node(&new_ref->rb_node_desc, parent, p);
1714 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001715
1716 binder_node_lock(node);
Todd Kjos4cbe5752017-05-01 17:21:51 -07001717 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001718
Todd Kjos4cbe5752017-05-01 17:21:51 -07001719 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1720 "%d new ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001721 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjos4cbe5752017-05-01 17:21:51 -07001722 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001723 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001724 return new_ref;
1725}
1726
Todd Kjos5346bf32016-10-20 16:43:34 -07001727static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001728{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001729 bool delete_node = false;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001730
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001731 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301732 "%d delete ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001733 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301734 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001735
1736 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1737 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001738
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001739 binder_node_inner_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001740 if (ref->data.strong)
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001741 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001742
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001743 hlist_del(&ref->node_entry);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001744 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1745 binder_node_inner_unlock(ref->node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001746 /*
1747 * Clear ref->node unless we want the caller to free the node
1748 */
1749 if (!delete_node) {
1750 /*
1751 * The caller uses ref->node to determine
1752 * whether the node needs to be freed. Clear
1753 * it since the node is still alive.
1754 */
1755 ref->node = NULL;
1756 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001757
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001758 if (ref->death) {
1759 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301760 "%d delete ref %d desc %d has death notification\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001761 ref->proc->pid, ref->data.debug_id,
1762 ref->data.desc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001763 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001764 binder_stats_deleted(BINDER_STAT_DEATH);
1765 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001766 binder_stats_deleted(BINDER_STAT_REF);
1767}
1768
Todd Kjosb0117bb2017-05-08 09:16:27 -07001769/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001770 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjosb0117bb2017-05-08 09:16:27 -07001771 * @ref: ref to be incremented
1772 * @strong: if true, strong increment, else weak
1773 * @target_list: list to queue node work on
1774 *
Todd Kjos5346bf32016-10-20 16:43:34 -07001775 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjosb0117bb2017-05-08 09:16:27 -07001776 *
1777 * Return: 0, if successful, else errno
1778 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001779static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1780 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001781{
1782 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001783
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001784 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001785 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001786 ret = binder_inc_node(ref->node, 1, 1, target_list);
1787 if (ret)
1788 return ret;
1789 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001790 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001791 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001792 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001793 ret = binder_inc_node(ref->node, 0, 1, target_list);
1794 if (ret)
1795 return ret;
1796 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001797 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001798 }
1799 return 0;
1800}
1801
Todd Kjosb0117bb2017-05-08 09:16:27 -07001802/**
1803 * binder_dec_ref() - dec the ref for given handle
1804 * @ref: ref to be decremented
1805 * @strong: if true, strong decrement, else weak
1806 *
1807 * Decrement the ref.
1808 *
Todd Kjosb0117bb2017-05-08 09:16:27 -07001809 * Return: true if ref is cleaned up and ready to be freed
1810 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001811static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001812{
1813 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001814 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301815 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001816 ref->proc->pid, ref->data.debug_id,
1817 ref->data.desc, ref->data.strong,
1818 ref->data.weak);
1819 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001820 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001821 ref->data.strong--;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001822 if (ref->data.strong == 0)
1823 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001824 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001825 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301826 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001827 ref->proc->pid, ref->data.debug_id,
1828 ref->data.desc, ref->data.strong,
1829 ref->data.weak);
1830 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001831 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001832 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001833 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001834 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001835 binder_cleanup_ref_olocked(ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001836 return true;
1837 }
1838 return false;
1839}
1840
1841/**
1842 * binder_get_node_from_ref() - get the node from the given proc/desc
1843 * @proc: proc containing the ref
1844 * @desc: the handle associated with the ref
1845 * @need_strong_ref: if true, only return node if ref is strong
1846 * @rdata: the id/refcount data for the ref
1847 *
1848 * Given a proc and ref handle, return the associated binder_node
1849 *
1850 * Return: a binder_node or NULL if not found or not strong when strong required
1851 */
1852static struct binder_node *binder_get_node_from_ref(
1853 struct binder_proc *proc,
1854 u32 desc, bool need_strong_ref,
1855 struct binder_ref_data *rdata)
1856{
1857 struct binder_node *node;
1858 struct binder_ref *ref;
1859
Todd Kjos5346bf32016-10-20 16:43:34 -07001860 binder_proc_lock(proc);
1861 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001862 if (!ref)
1863 goto err_no_ref;
1864 node = ref->node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001865 /*
1866 * Take an implicit reference on the node to ensure
1867 * it stays alive until the call to binder_put_node()
1868 */
1869 binder_inc_node_tmpref(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001870 if (rdata)
1871 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001872 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001873
1874 return node;
1875
1876err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001877 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001878 return NULL;
1879}
1880
1881/**
1882 * binder_free_ref() - free the binder_ref
1883 * @ref: ref to free
1884 *
Todd Kjose7f23ed2017-03-21 13:06:01 -07001885 * Free the binder_ref. Free the binder_node indicated by ref->node
1886 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjosb0117bb2017-05-08 09:16:27 -07001887 */
1888static void binder_free_ref(struct binder_ref *ref)
1889{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001890 if (ref->node)
1891 binder_free_node(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001892 kfree(ref->death);
1893 kfree(ref);
1894}
1895
1896/**
1897 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1898 * @proc: proc containing the ref
1899 * @desc: the handle associated with the ref
1900 * @increment: true=inc reference, false=dec reference
1901 * @strong: true=strong reference, false=weak reference
1902 * @rdata: the id/refcount data for the ref
1903 *
1904 * Given a proc and ref handle, increment or decrement the ref
1905 * according to "increment" arg.
1906 *
1907 * Return: 0 if successful, else errno
1908 */
1909static int binder_update_ref_for_handle(struct binder_proc *proc,
1910 uint32_t desc, bool increment, bool strong,
1911 struct binder_ref_data *rdata)
1912{
1913 int ret = 0;
1914 struct binder_ref *ref;
1915 bool delete_ref = false;
1916
Todd Kjos5346bf32016-10-20 16:43:34 -07001917 binder_proc_lock(proc);
1918 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001919 if (!ref) {
1920 ret = -EINVAL;
1921 goto err_no_ref;
1922 }
1923 if (increment)
Todd Kjos5346bf32016-10-20 16:43:34 -07001924 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001925 else
Todd Kjos5346bf32016-10-20 16:43:34 -07001926 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001927
1928 if (rdata)
1929 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001930 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001931
1932 if (delete_ref)
1933 binder_free_ref(ref);
1934 return ret;
1935
1936err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001937 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001938 return ret;
1939}
1940
1941/**
1942 * binder_dec_ref_for_handle() - dec the ref for given handle
1943 * @proc: proc containing the ref
1944 * @desc: the handle associated with the ref
1945 * @strong: true=strong reference, false=weak reference
1946 * @rdata: the id/refcount data for the ref
1947 *
1948 * Just calls binder_update_ref_for_handle() to decrement the ref.
1949 *
1950 * Return: 0 if successful, else errno
1951 */
1952static int binder_dec_ref_for_handle(struct binder_proc *proc,
1953 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1954{
1955 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1956}
1957
1958
1959/**
1960 * binder_inc_ref_for_node() - increment the ref for given proc/node
1961 * @proc: proc containing the ref
1962 * @node: target node
1963 * @strong: true=strong reference, false=weak reference
1964 * @target_list: worklist to use if node is incremented
1965 * @rdata: the id/refcount data for the ref
1966 *
1967 * Given a proc and node, increment the ref. Create the ref if it
1968 * doesn't already exist
1969 *
1970 * Return: 0 if successful, else errno
1971 */
1972static int binder_inc_ref_for_node(struct binder_proc *proc,
1973 struct binder_node *node,
1974 bool strong,
1975 struct list_head *target_list,
1976 struct binder_ref_data *rdata)
1977{
1978 struct binder_ref *ref;
1979 struct binder_ref *new_ref = NULL;
1980 int ret = 0;
1981
Todd Kjos5346bf32016-10-20 16:43:34 -07001982 binder_proc_lock(proc);
1983 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001984 if (!ref) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001985 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001986 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1987 if (!new_ref)
1988 return -ENOMEM;
Todd Kjos5346bf32016-10-20 16:43:34 -07001989 binder_proc_lock(proc);
1990 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001991 }
Todd Kjos5346bf32016-10-20 16:43:34 -07001992 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001993 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001994 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001995 if (new_ref && ref != new_ref)
1996 /*
1997 * Another thread created the ref first so
1998 * free the one we allocated
1999 */
2000 kfree(new_ref);
2001 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002002}
2003
Martijn Coenen995a36e2017-06-02 13:36:52 -07002004static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
2005 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002006{
Todd Kjos21ef40a2017-03-30 18:02:13 -07002007 BUG_ON(!target_thread);
Martijn Coenened323352017-07-27 23:52:24 +02002008 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjos21ef40a2017-03-30 18:02:13 -07002009 BUG_ON(target_thread->transaction_stack != t);
2010 BUG_ON(target_thread->transaction_stack->from != target_thread);
2011 target_thread->transaction_stack =
2012 target_thread->transaction_stack->from_parent;
2013 t->from = NULL;
2014}
2015
Todd Kjos2f993e22017-05-12 14:42:55 -07002016/**
2017 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2018 * @thread: thread to decrement
2019 *
2020 * A thread needs to be kept alive while being used to create or
2021 * handle a transaction. binder_get_txn_from() is used to safely
2022 * extract t->from from a binder_transaction and keep the thread
2023 * indicated by t->from from being freed. When done with that
2024 * binder_thread, this function is called to decrement the
2025 * tmp_ref and free if appropriate (thread has been released
2026 * and no transaction being processed by the driver)
2027 */
2028static void binder_thread_dec_tmpref(struct binder_thread *thread)
2029{
2030 /*
2031 * atomic is used to protect the counter value while
2032 * it cannot reach zero or thread->is_dead is false
Todd Kjos2f993e22017-05-12 14:42:55 -07002033 */
Todd Kjosb4827902017-05-25 15:52:17 -07002034 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002035 atomic_dec(&thread->tmp_ref);
2036 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07002037 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002038 binder_free_thread(thread);
2039 return;
2040 }
Todd Kjosb4827902017-05-25 15:52:17 -07002041 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002042}
2043
2044/**
2045 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2046 * @proc: proc to decrement
2047 *
2048 * A binder_proc needs to be kept alive while being used to create or
2049 * handle a transaction. proc->tmp_ref is incremented when
2050 * creating a new transaction or the binder_proc is currently in-use
2051 * by threads that are being released. When done with the binder_proc,
2052 * this function is called to decrement the counter and free the
2053 * proc if appropriate (proc has been released, all threads have
2054 * been released and not currenly in-use to process a transaction).
2055 */
2056static void binder_proc_dec_tmpref(struct binder_proc *proc)
2057{
Todd Kjosb4827902017-05-25 15:52:17 -07002058 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002059 proc->tmp_ref--;
2060 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
2061 !proc->tmp_ref) {
Todd Kjosb4827902017-05-25 15:52:17 -07002062 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002063 binder_free_proc(proc);
2064 return;
2065 }
Todd Kjosb4827902017-05-25 15:52:17 -07002066 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002067}
2068
2069/**
2070 * binder_get_txn_from() - safely extract the "from" thread in transaction
2071 * @t: binder transaction for t->from
2072 *
2073 * Atomically return the "from" thread and increment the tmp_ref
2074 * count for the thread to ensure it stays alive until
2075 * binder_thread_dec_tmpref() is called.
2076 *
2077 * Return: the value of t->from
2078 */
2079static struct binder_thread *binder_get_txn_from(
2080 struct binder_transaction *t)
2081{
2082 struct binder_thread *from;
2083
2084 spin_lock(&t->lock);
2085 from = t->from;
2086 if (from)
2087 atomic_inc(&from->tmp_ref);
2088 spin_unlock(&t->lock);
2089 return from;
2090}
2091
Martijn Coenen995a36e2017-06-02 13:36:52 -07002092/**
2093 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2094 * @t: binder transaction for t->from
2095 *
2096 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2097 * to guarantee that the thread cannot be released while operating on it.
2098 * The caller must call binder_inner_proc_unlock() to release the inner lock
2099 * as well as call binder_dec_thread_txn() to release the reference.
2100 *
2101 * Return: the value of t->from
2102 */
2103static struct binder_thread *binder_get_txn_from_and_acq_inner(
2104 struct binder_transaction *t)
2105{
2106 struct binder_thread *from;
2107
2108 from = binder_get_txn_from(t);
2109 if (!from)
2110 return NULL;
2111 binder_inner_proc_lock(from->proc);
2112 if (t->from) {
2113 BUG_ON(from != t->from);
2114 return from;
2115 }
2116 binder_inner_proc_unlock(from->proc);
2117 binder_thread_dec_tmpref(from);
2118 return NULL;
2119}
2120
Todd Kjos21ef40a2017-03-30 18:02:13 -07002121static void binder_free_transaction(struct binder_transaction *t)
2122{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002123 if (t->buffer)
2124 t->buffer->transaction = NULL;
2125 kfree(t);
2126 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2127}
2128
2129static void binder_send_failed_reply(struct binder_transaction *t,
2130 uint32_t error_code)
2131{
2132 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002133 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09002134
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002135 BUG_ON(t->flags & TF_ONE_WAY);
2136 while (1) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002137 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002138 if (target_thread) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002139 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2140 "send failed reply for transaction %d to %d:%d\n",
2141 t->debug_id,
2142 target_thread->proc->pid,
2143 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002144
Martijn Coenen995a36e2017-06-02 13:36:52 -07002145 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos858b8da2017-04-21 17:35:12 -07002146 if (target_thread->reply_error.cmd == BR_OK) {
2147 target_thread->reply_error.cmd = error_code;
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02002148 binder_enqueue_thread_work_ilocked(
2149 target_thread,
2150 &target_thread->reply_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002151 wake_up_interruptible(&target_thread->wait);
2152 } else {
Todd Kjos858b8da2017-04-21 17:35:12 -07002153 WARN(1, "Unexpected reply error: %u\n",
2154 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002155 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002156 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002157 binder_thread_dec_tmpref(target_thread);
Todd Kjos858b8da2017-04-21 17:35:12 -07002158 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002159 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002160 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002161 next = t->from_parent;
2162
2163 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2164 "send failed reply for transaction %d, target dead\n",
2165 t->debug_id);
2166
Todd Kjos21ef40a2017-03-30 18:02:13 -07002167 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002168 if (next == NULL) {
2169 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2170 "reply failed, no target thread at root\n");
2171 return;
2172 }
2173 t = next;
2174 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2175 "reply failed, no target thread -- retry %d\n",
2176 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002177 }
2178}
2179
Martijn Coenen00c80372016-07-13 12:06:49 +02002180/**
Martijn Coenen3217ccc2017-08-24 15:23:36 +02002181 * binder_cleanup_transaction() - cleans up undelivered transaction
2182 * @t: transaction that needs to be cleaned up
2183 * @reason: reason the transaction wasn't delivered
2184 * @error_code: error to return to caller (if synchronous call)
2185 */
2186static void binder_cleanup_transaction(struct binder_transaction *t,
2187 const char *reason,
2188 uint32_t error_code)
2189{
2190 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2191 binder_send_failed_reply(t, error_code);
2192 } else {
2193 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2194 "undelivered transaction %d, %s\n",
2195 t->debug_id, reason);
2196 binder_free_transaction(t);
2197 }
2198}
2199
2200/**
Martijn Coenen00c80372016-07-13 12:06:49 +02002201 * binder_validate_object() - checks for a valid metadata object in a buffer.
2202 * @buffer: binder_buffer that we're parsing.
2203 * @offset: offset in the buffer at which to validate an object.
2204 *
2205 * Return: If there's a valid metadata object at @offset in @buffer, the
2206 * size of that object. Otherwise, it returns zero.
2207 */
2208static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2209{
2210 /* Check if we can read a header first */
2211 struct binder_object_header *hdr;
2212 size_t object_size = 0;
2213
2214 if (offset > buffer->data_size - sizeof(*hdr) ||
2215 buffer->data_size < sizeof(*hdr) ||
2216 !IS_ALIGNED(offset, sizeof(u32)))
2217 return 0;
2218
2219 /* Ok, now see if we can read a complete object. */
2220 hdr = (struct binder_object_header *)(buffer->data + offset);
2221 switch (hdr->type) {
2222 case BINDER_TYPE_BINDER:
2223 case BINDER_TYPE_WEAK_BINDER:
2224 case BINDER_TYPE_HANDLE:
2225 case BINDER_TYPE_WEAK_HANDLE:
2226 object_size = sizeof(struct flat_binder_object);
2227 break;
2228 case BINDER_TYPE_FD:
2229 object_size = sizeof(struct binder_fd_object);
2230 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002231 case BINDER_TYPE_PTR:
2232 object_size = sizeof(struct binder_buffer_object);
2233 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002234 case BINDER_TYPE_FDA:
2235 object_size = sizeof(struct binder_fd_array_object);
2236 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02002237 default:
2238 return 0;
2239 }
2240 if (offset <= buffer->data_size - object_size &&
2241 buffer->data_size >= object_size)
2242 return object_size;
2243 else
2244 return 0;
2245}
2246
Martijn Coenen5a6da532016-09-30 14:10:07 +02002247/**
2248 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2249 * @b: binder_buffer containing the object
2250 * @index: index in offset array at which the binder_buffer_object is
2251 * located
2252 * @start: points to the start of the offset array
2253 * @num_valid: the number of valid offsets in the offset array
2254 *
2255 * Return: If @index is within the valid range of the offset array
2256 * described by @start and @num_valid, and if there's a valid
2257 * binder_buffer_object at the offset found in index @index
2258 * of the offset array, that object is returned. Otherwise,
2259 * %NULL is returned.
2260 * Note that the offset found in index @index itself is not
2261 * verified; this function assumes that @num_valid elements
2262 * from @start were previously verified to have valid offsets.
2263 */
2264static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2265 binder_size_t index,
2266 binder_size_t *start,
2267 binder_size_t num_valid)
2268{
2269 struct binder_buffer_object *buffer_obj;
2270 binder_size_t *offp;
2271
2272 if (index >= num_valid)
2273 return NULL;
2274
2275 offp = start + index;
2276 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2277 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2278 return NULL;
2279
2280 return buffer_obj;
2281}
2282
2283/**
2284 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2285 * @b: transaction buffer
2286 * @objects_start start of objects buffer
2287 * @buffer: binder_buffer_object in which to fix up
2288 * @offset: start offset in @buffer to fix up
2289 * @last_obj: last binder_buffer_object that we fixed up in
2290 * @last_min_offset: minimum fixup offset in @last_obj
2291 *
2292 * Return: %true if a fixup in buffer @buffer at offset @offset is
2293 * allowed.
2294 *
2295 * For safety reasons, we only allow fixups inside a buffer to happen
2296 * at increasing offsets; additionally, we only allow fixup on the last
2297 * buffer object that was verified, or one of its parents.
2298 *
2299 * Example of what is allowed:
2300 *
2301 * A
2302 * B (parent = A, offset = 0)
2303 * C (parent = A, offset = 16)
2304 * D (parent = C, offset = 0)
2305 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2306 *
2307 * Examples of what is not allowed:
2308 *
2309 * Decreasing offsets within the same parent:
2310 * A
2311 * C (parent = A, offset = 16)
2312 * B (parent = A, offset = 0) // decreasing offset within A
2313 *
2314 * Referring to a parent that wasn't the last object or any of its parents:
2315 * A
2316 * B (parent = A, offset = 0)
2317 * C (parent = A, offset = 0)
2318 * C (parent = A, offset = 16)
2319 * D (parent = B, offset = 0) // B is not A or any of A's parents
2320 */
2321static bool binder_validate_fixup(struct binder_buffer *b,
2322 binder_size_t *objects_start,
2323 struct binder_buffer_object *buffer,
2324 binder_size_t fixup_offset,
2325 struct binder_buffer_object *last_obj,
2326 binder_size_t last_min_offset)
2327{
2328 if (!last_obj) {
2329 /* Nothing to fix up in */
2330 return false;
2331 }
2332
2333 while (last_obj != buffer) {
2334 /*
2335 * Safe to retrieve the parent of last_obj, since it
2336 * was already previously verified by the driver.
2337 */
2338 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2339 return false;
2340 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2341 last_obj = (struct binder_buffer_object *)
2342 (b->data + *(objects_start + last_obj->parent));
2343 }
2344 return (fixup_offset >= last_min_offset);
2345}
2346
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002347static void binder_transaction_buffer_release(struct binder_proc *proc,
2348 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002349 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002350{
Martijn Coenen5a6da532016-09-30 14:10:07 +02002351 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002352 int debug_id = buffer->debug_id;
2353
2354 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302355 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002356 proc->pid, buffer->debug_id,
2357 buffer->data_size, buffer->offsets_size, failed_at);
2358
2359 if (buffer->target_node)
2360 binder_dec_node(buffer->target_node, 1, 0);
2361
Martijn Coenen5a6da532016-09-30 14:10:07 +02002362 off_start = (binder_size_t *)(buffer->data +
2363 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002364 if (failed_at)
2365 off_end = failed_at;
2366 else
Martijn Coenen5a6da532016-09-30 14:10:07 +02002367 off_end = (void *)off_start + buffer->offsets_size;
2368 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002369 struct binder_object_header *hdr;
2370 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002371
Martijn Coenen00c80372016-07-13 12:06:49 +02002372 if (object_size == 0) {
2373 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002374 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002375 continue;
2376 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002377 hdr = (struct binder_object_header *)(buffer->data + *offp);
2378 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002379 case BINDER_TYPE_BINDER:
2380 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002381 struct flat_binder_object *fp;
2382 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002383
Martijn Coenen00c80372016-07-13 12:06:49 +02002384 fp = to_flat_binder_object(hdr);
2385 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002386 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002387 pr_err("transaction release %d bad node %016llx\n",
2388 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002389 break;
2390 }
2391 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002392 " node %d u%016llx\n",
2393 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002394 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2395 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002396 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002397 } break;
2398 case BINDER_TYPE_HANDLE:
2399 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002400 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002401 struct binder_ref_data rdata;
2402 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002403
Martijn Coenen00c80372016-07-13 12:06:49 +02002404 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002405 ret = binder_dec_ref_for_handle(proc, fp->handle,
2406 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2407
2408 if (ret) {
2409 pr_err("transaction release %d bad handle %d, ret = %d\n",
2410 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002411 break;
2412 }
2413 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002414 " ref %d desc %d\n",
2415 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002416 } break;
2417
Martijn Coenen00c80372016-07-13 12:06:49 +02002418 case BINDER_TYPE_FD: {
2419 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2420
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002421 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002422 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002423 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002424 task_close_fd(proc, fp->fd);
2425 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002426 case BINDER_TYPE_PTR:
2427 /*
2428 * Nothing to do here, this will get cleaned up when the
2429 * transaction buffer gets freed
2430 */
2431 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002432 case BINDER_TYPE_FDA: {
2433 struct binder_fd_array_object *fda;
2434 struct binder_buffer_object *parent;
2435 uintptr_t parent_buffer;
2436 u32 *fd_array;
2437 size_t fd_index;
2438 binder_size_t fd_buf_size;
2439
2440 fda = to_binder_fd_array_object(hdr);
2441 parent = binder_validate_ptr(buffer, fda->parent,
2442 off_start,
2443 offp - off_start);
2444 if (!parent) {
2445 pr_err("transaction release %d bad parent offset",
2446 debug_id);
2447 continue;
2448 }
2449 /*
2450 * Since the parent was already fixed up, convert it
2451 * back to kernel address space to access it
2452 */
2453 parent_buffer = parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002454 binder_alloc_get_user_buffer_offset(
2455 &proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002456
2457 fd_buf_size = sizeof(u32) * fda->num_fds;
2458 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2459 pr_err("transaction release %d invalid number of fds (%lld)\n",
2460 debug_id, (u64)fda->num_fds);
2461 continue;
2462 }
2463 if (fd_buf_size > parent->length ||
2464 fda->parent_offset > parent->length - fd_buf_size) {
2465 /* No space for all file descriptors here. */
2466 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2467 debug_id, (u64)fda->num_fds);
2468 continue;
2469 }
Arnd Bergmann0b291472017-09-05 10:56:13 +02002470 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002471 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2472 task_close_fd(proc, fd_array[fd_index]);
2473 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002474 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002475 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002476 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002477 break;
2478 }
2479 }
2480}
2481
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002482static int binder_translate_binder(struct flat_binder_object *fp,
2483 struct binder_transaction *t,
2484 struct binder_thread *thread)
2485{
2486 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002487 struct binder_proc *proc = thread->proc;
2488 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002489 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002490 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002491
2492 node = binder_get_node(proc, fp->binder);
2493 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002494 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002495 if (!node)
2496 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002497 }
2498 if (fp->cookie != node->cookie) {
2499 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2500 proc->pid, thread->pid, (u64)fp->binder,
2501 node->debug_id, (u64)fp->cookie,
2502 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002503 ret = -EINVAL;
2504 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002505 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002506 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2507 ret = -EPERM;
2508 goto done;
2509 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002510
Todd Kjosb0117bb2017-05-08 09:16:27 -07002511 ret = binder_inc_ref_for_node(target_proc, node,
2512 fp->hdr.type == BINDER_TYPE_BINDER,
2513 &thread->todo, &rdata);
2514 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002515 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002516
2517 if (fp->hdr.type == BINDER_TYPE_BINDER)
2518 fp->hdr.type = BINDER_TYPE_HANDLE;
2519 else
2520 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2521 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002522 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002523 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002524
Todd Kjosb0117bb2017-05-08 09:16:27 -07002525 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002526 binder_debug(BINDER_DEBUG_TRANSACTION,
2527 " node %d u%016llx -> ref %d desc %d\n",
2528 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002529 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002530done:
2531 binder_put_node(node);
2532 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002533}
2534
2535static int binder_translate_handle(struct flat_binder_object *fp,
2536 struct binder_transaction *t,
2537 struct binder_thread *thread)
2538{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002539 struct binder_proc *proc = thread->proc;
2540 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002541 struct binder_node *node;
2542 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002543 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002544
Todd Kjosb0117bb2017-05-08 09:16:27 -07002545 node = binder_get_node_from_ref(proc, fp->handle,
2546 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2547 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002548 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2549 proc->pid, thread->pid, fp->handle);
2550 return -EINVAL;
2551 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002552 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2553 ret = -EPERM;
2554 goto done;
2555 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002556
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002557 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002558 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002559 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2560 fp->hdr.type = BINDER_TYPE_BINDER;
2561 else
2562 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002563 fp->binder = node->ptr;
2564 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002565 if (node->proc)
2566 binder_inner_proc_lock(node->proc);
2567 binder_inc_node_nilocked(node,
2568 fp->hdr.type == BINDER_TYPE_BINDER,
2569 0, NULL);
2570 if (node->proc)
2571 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002572 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002573 binder_debug(BINDER_DEBUG_TRANSACTION,
2574 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002575 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2576 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002577 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002578 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002579 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002580
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002581 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002582 ret = binder_inc_ref_for_node(target_proc, node,
2583 fp->hdr.type == BINDER_TYPE_HANDLE,
2584 NULL, &dest_rdata);
2585 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002586 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002587
2588 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002589 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002590 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002591 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2592 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002593 binder_debug(BINDER_DEBUG_TRANSACTION,
2594 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002595 src_rdata.debug_id, src_rdata.desc,
2596 dest_rdata.debug_id, dest_rdata.desc,
2597 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002598 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002599done:
2600 binder_put_node(node);
2601 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002602}
2603
2604static int binder_translate_fd(int fd,
2605 struct binder_transaction *t,
2606 struct binder_thread *thread,
2607 struct binder_transaction *in_reply_to)
2608{
2609 struct binder_proc *proc = thread->proc;
2610 struct binder_proc *target_proc = t->to_proc;
2611 int target_fd;
2612 struct file *file;
2613 int ret;
2614 bool target_allows_fd;
2615
2616 if (in_reply_to)
2617 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2618 else
2619 target_allows_fd = t->buffer->target_node->accept_fds;
2620 if (!target_allows_fd) {
2621 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2622 proc->pid, thread->pid,
2623 in_reply_to ? "reply" : "transaction",
2624 fd);
2625 ret = -EPERM;
2626 goto err_fd_not_accepted;
2627 }
2628
2629 file = fget(fd);
2630 if (!file) {
2631 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2632 proc->pid, thread->pid, fd);
2633 ret = -EBADF;
2634 goto err_fget;
2635 }
2636 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2637 if (ret < 0) {
2638 ret = -EPERM;
2639 goto err_security;
2640 }
2641
2642 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2643 if (target_fd < 0) {
2644 ret = -ENOMEM;
2645 goto err_get_unused_fd;
2646 }
2647 task_fd_install(target_proc, target_fd, file);
2648 trace_binder_transaction_fd(t, fd, target_fd);
2649 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2650 fd, target_fd);
2651
2652 return target_fd;
2653
2654err_get_unused_fd:
2655err_security:
2656 fput(file);
2657err_fget:
2658err_fd_not_accepted:
2659 return ret;
2660}
2661
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002662static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2663 struct binder_buffer_object *parent,
2664 struct binder_transaction *t,
2665 struct binder_thread *thread,
2666 struct binder_transaction *in_reply_to)
2667{
2668 binder_size_t fdi, fd_buf_size, num_installed_fds;
2669 int target_fd;
2670 uintptr_t parent_buffer;
2671 u32 *fd_array;
2672 struct binder_proc *proc = thread->proc;
2673 struct binder_proc *target_proc = t->to_proc;
2674
2675 fd_buf_size = sizeof(u32) * fda->num_fds;
2676 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2677 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2678 proc->pid, thread->pid, (u64)fda->num_fds);
2679 return -EINVAL;
2680 }
2681 if (fd_buf_size > parent->length ||
2682 fda->parent_offset > parent->length - fd_buf_size) {
2683 /* No space for all file descriptors here. */
2684 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2685 proc->pid, thread->pid, (u64)fda->num_fds);
2686 return -EINVAL;
2687 }
2688 /*
2689 * Since the parent was already fixed up, convert it
2690 * back to the kernel address space to access it
2691 */
Todd Kjosd325d372016-10-10 10:40:53 -07002692 parent_buffer = parent->buffer -
2693 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Arnd Bergmann0b291472017-09-05 10:56:13 +02002694 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002695 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2696 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2697 proc->pid, thread->pid);
2698 return -EINVAL;
2699 }
2700 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2701 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2702 in_reply_to);
2703 if (target_fd < 0)
2704 goto err_translate_fd_failed;
2705 fd_array[fdi] = target_fd;
2706 }
2707 return 0;
2708
2709err_translate_fd_failed:
2710 /*
2711 * Failed to allocate fd or security error, free fds
2712 * installed so far.
2713 */
2714 num_installed_fds = fdi;
2715 for (fdi = 0; fdi < num_installed_fds; fdi++)
2716 task_close_fd(target_proc, fd_array[fdi]);
2717 return target_fd;
2718}
2719
Martijn Coenen5a6da532016-09-30 14:10:07 +02002720static int binder_fixup_parent(struct binder_transaction *t,
2721 struct binder_thread *thread,
2722 struct binder_buffer_object *bp,
2723 binder_size_t *off_start,
2724 binder_size_t num_valid,
2725 struct binder_buffer_object *last_fixup_obj,
2726 binder_size_t last_fixup_min_off)
2727{
2728 struct binder_buffer_object *parent;
2729 u8 *parent_buffer;
2730 struct binder_buffer *b = t->buffer;
2731 struct binder_proc *proc = thread->proc;
2732 struct binder_proc *target_proc = t->to_proc;
2733
2734 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2735 return 0;
2736
2737 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2738 if (!parent) {
2739 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2740 proc->pid, thread->pid);
2741 return -EINVAL;
2742 }
2743
2744 if (!binder_validate_fixup(b, off_start,
2745 parent, bp->parent_offset,
2746 last_fixup_obj,
2747 last_fixup_min_off)) {
2748 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2749 proc->pid, thread->pid);
2750 return -EINVAL;
2751 }
2752
2753 if (parent->length < sizeof(binder_uintptr_t) ||
2754 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2755 /* No space for a pointer here! */
2756 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2757 proc->pid, thread->pid);
2758 return -EINVAL;
2759 }
Arnd Bergmann0b291472017-09-05 10:56:13 +02002760 parent_buffer = (u8 *)((uintptr_t)parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002761 binder_alloc_get_user_buffer_offset(
2762 &target_proc->alloc));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002763 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2764
2765 return 0;
2766}
2767
Martijn Coenen053be422017-06-06 15:17:46 -07002768/**
2769 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2770 * @t: transaction to send
2771 * @proc: process to send the transaction to
2772 * @thread: thread in @proc to send the transaction to (may be NULL)
2773 *
2774 * This function queues a transaction to the specified process. It will try
2775 * to find a thread in the target process to handle the transaction and
2776 * wake it up. If no thread is found, the work is queued to the proc
2777 * waitqueue.
2778 *
2779 * If the @thread parameter is not NULL, the transaction is always queued
2780 * to the waitlist of that specific thread.
2781 *
2782 * Return: true if the transactions was successfully queued
2783 * false if the target process or thread is dead
2784 */
2785static bool binder_proc_transaction(struct binder_transaction *t,
2786 struct binder_proc *proc,
2787 struct binder_thread *thread)
2788{
Martijn Coenen053be422017-06-06 15:17:46 -07002789 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002790 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002791 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02002792 bool pending_async = false;
Martijn Coenen053be422017-06-06 15:17:46 -07002793
2794 BUG_ON(!node);
2795 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002796 node_prio.prio = node->min_priority;
2797 node_prio.sched_policy = node->sched_policy;
2798
Martijn Coenen053be422017-06-06 15:17:46 -07002799 if (oneway) {
2800 BUG_ON(thread);
2801 if (node->has_async_transaction) {
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02002802 pending_async = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002803 } else {
2804 node->has_async_transaction = 1;
2805 }
2806 }
2807
2808 binder_inner_proc_lock(proc);
2809
2810 if (proc->is_dead || (thread && thread->is_dead)) {
2811 binder_inner_proc_unlock(proc);
2812 binder_node_unlock(node);
2813 return false;
2814 }
2815
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02002816 if (!thread && !pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002817 thread = binder_select_thread_ilocked(proc);
2818
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002819 if (thread) {
Martijn Coenenc46810c2017-06-23 10:13:43 -07002820 binder_transaction_priority(thread->task, t, node_prio,
2821 node->inherit_rt);
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02002822 binder_enqueue_thread_work_ilocked(thread, &t->work);
2823 } else if (!pending_async) {
2824 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002825 } else {
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02002826 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002827 }
Martijn Coenen053be422017-06-06 15:17:46 -07002828
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02002829 if (!pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002830 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2831
2832 binder_inner_proc_unlock(proc);
2833 binder_node_unlock(node);
2834
2835 return true;
2836}
2837
Todd Kjos291d9682017-09-25 08:55:09 -07002838/**
2839 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2840 * @node: struct binder_node for which to get refs
2841 * @proc: returns @node->proc if valid
2842 * @error: if no @proc then returns BR_DEAD_REPLY
2843 *
2844 * User-space normally keeps the node alive when creating a transaction
2845 * since it has a reference to the target. The local strong ref keeps it
2846 * alive if the sending process dies before the target process processes
2847 * the transaction. If the source process is malicious or has a reference
2848 * counting bug, relying on the local strong ref can fail.
2849 *
2850 * Since user-space can cause the local strong ref to go away, we also take
2851 * a tmpref on the node to ensure it survives while we are constructing
2852 * the transaction. We also need a tmpref on the proc while we are
2853 * constructing the transaction, so we take that here as well.
2854 *
2855 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2856 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2857 * target proc has died, @error is set to BR_DEAD_REPLY
2858 */
2859static struct binder_node *binder_get_node_refs_for_txn(
2860 struct binder_node *node,
2861 struct binder_proc **procp,
2862 uint32_t *error)
2863{
2864 struct binder_node *target_node = NULL;
2865
2866 binder_node_inner_lock(node);
2867 if (node->proc) {
2868 target_node = node;
2869 binder_inc_node_nilocked(node, 1, 0, NULL);
2870 binder_inc_node_tmpref_ilocked(node);
2871 node->proc->tmp_ref++;
2872 *procp = node->proc;
2873 } else
2874 *error = BR_DEAD_REPLY;
2875 binder_node_inner_unlock(node);
2876
2877 return target_node;
2878}
2879
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002880static void binder_transaction(struct binder_proc *proc,
2881 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02002882 struct binder_transaction_data *tr, int reply,
2883 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002884{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002885 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002886 struct binder_transaction *t;
2887 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002888 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002889 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002890 u8 *sg_bufp, *sg_buf_end;
Todd Kjos2f993e22017-05-12 14:42:55 -07002891 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002892 struct binder_thread *target_thread = NULL;
2893 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002894 struct binder_transaction *in_reply_to = NULL;
2895 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07002896 uint32_t return_error = 0;
2897 uint32_t return_error_param = 0;
2898 uint32_t return_error_line = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002899 struct binder_buffer_object *last_fixup_obj = NULL;
2900 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002901 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07002902 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002903
2904 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07002905 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002906 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2907 e->from_proc = proc->pid;
2908 e->from_thread = thread->pid;
2909 e->target_handle = tr->target.handle;
2910 e->data_size = tr->data_size;
2911 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02002912 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002913
2914 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002915 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002916 in_reply_to = thread->transaction_stack;
2917 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002918 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302919 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002920 proc->pid, thread->pid);
2921 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002922 return_error_param = -EPROTO;
2923 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002924 goto err_empty_call_stack;
2925 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002926 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002927 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302928 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 +09002929 proc->pid, thread->pid, in_reply_to->debug_id,
2930 in_reply_to->to_proc ?
2931 in_reply_to->to_proc->pid : 0,
2932 in_reply_to->to_thread ?
2933 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002934 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002935 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002936 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002937 return_error_param = -EPROTO;
2938 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002939 in_reply_to = NULL;
2940 goto err_bad_call_stack;
2941 }
2942 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002943 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002944 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002945 if (target_thread == NULL) {
2946 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002947 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002948 goto err_dead_binder;
2949 }
2950 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302951 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 +09002952 proc->pid, thread->pid,
2953 target_thread->transaction_stack ?
2954 target_thread->transaction_stack->debug_id : 0,
2955 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002956 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002957 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002958 return_error_param = -EPROTO;
2959 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002960 in_reply_to = NULL;
2961 target_thread = NULL;
2962 goto err_dead_binder;
2963 }
2964 target_proc = target_thread->proc;
Todd Kjos2f993e22017-05-12 14:42:55 -07002965 target_proc->tmp_ref++;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002966 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002967 } else {
2968 if (tr->target.handle) {
2969 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002970
Todd Kjosc37162d2017-05-26 11:56:29 -07002971 /*
2972 * There must already be a strong ref
2973 * on this node. If so, do a strong
2974 * increment on the node to ensure it
2975 * stays alive until the transaction is
2976 * done.
2977 */
Todd Kjos5346bf32016-10-20 16:43:34 -07002978 binder_proc_lock(proc);
2979 ref = binder_get_ref_olocked(proc, tr->target.handle,
2980 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07002981 if (ref) {
Todd Kjos291d9682017-09-25 08:55:09 -07002982 target_node = binder_get_node_refs_for_txn(
2983 ref->node, &target_proc,
2984 &return_error);
2985 } else {
2986 binder_user_error("%d:%d got transaction to invalid handle\n",
2987 proc->pid, thread->pid);
2988 return_error = BR_FAILED_REPLY;
Todd Kjosc37162d2017-05-26 11:56:29 -07002989 }
Todd Kjos5346bf32016-10-20 16:43:34 -07002990 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002991 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002992 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002993 target_node = context->binder_context_mgr_node;
Todd Kjos291d9682017-09-25 08:55:09 -07002994 if (target_node)
2995 target_node = binder_get_node_refs_for_txn(
2996 target_node, &target_proc,
2997 &return_error);
2998 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002999 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003000 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003001 }
Todd Kjos291d9682017-09-25 08:55:09 -07003002 if (!target_node) {
3003 /*
3004 * return_error is set above
3005 */
3006 return_error_param = -EINVAL;
Todd Kjose598d172017-03-22 17:19:52 -07003007 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003008 goto err_dead_binder;
3009 }
Todd Kjos291d9682017-09-25 08:55:09 -07003010 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05003011 if (security_binder_transaction(proc->tsk,
3012 target_proc->tsk) < 0) {
3013 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003014 return_error_param = -EPERM;
3015 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05003016 goto err_invalid_target_handle;
3017 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003018 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003019 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3020 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003021
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003022 tmp = thread->transaction_stack;
3023 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003024 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303025 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 +09003026 proc->pid, thread->pid, tmp->debug_id,
3027 tmp->to_proc ? tmp->to_proc->pid : 0,
3028 tmp->to_thread ?
3029 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003030 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003031 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003032 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003033 return_error_param = -EPROTO;
3034 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003035 goto err_bad_call_stack;
3036 }
3037 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003038 struct binder_thread *from;
3039
3040 spin_lock(&tmp->lock);
3041 from = tmp->from;
3042 if (from && from->proc == target_proc) {
3043 atomic_inc(&from->tmp_ref);
3044 target_thread = from;
3045 spin_unlock(&tmp->lock);
3046 break;
3047 }
3048 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003049 tmp = tmp->from_parent;
3050 }
3051 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003052 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003053 }
Martijn Coenen053be422017-06-06 15:17:46 -07003054 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003055 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003056 e->to_proc = target_proc->pid;
3057
3058 /* TODO: reuse incoming transaction for reply */
3059 t = kzalloc(sizeof(*t), GFP_KERNEL);
3060 if (t == NULL) {
3061 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003062 return_error_param = -ENOMEM;
3063 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003064 goto err_alloc_t_failed;
3065 }
3066 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07003067 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003068
3069 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3070 if (tcomplete == NULL) {
3071 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003072 return_error_param = -ENOMEM;
3073 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003074 goto err_alloc_tcomplete_failed;
3075 }
3076 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3077
Todd Kjos1cfe6272017-05-24 13:33:28 -07003078 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003079
3080 if (reply)
3081 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003082 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003083 proc->pid, thread->pid, t->debug_id,
3084 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003085 (u64)tr->data.ptr.buffer,
3086 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003087 (u64)tr->data_size, (u64)tr->offsets_size,
3088 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003089 else
3090 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003091 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003092 proc->pid, thread->pid, t->debug_id,
3093 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003094 (u64)tr->data.ptr.buffer,
3095 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003096 (u64)tr->data_size, (u64)tr->offsets_size,
3097 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003098
3099 if (!reply && !(tr->flags & TF_ONE_WAY))
3100 t->from = thread;
3101 else
3102 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03003103 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003104 t->to_proc = target_proc;
3105 t->to_thread = target_thread;
3106 t->code = tr->code;
3107 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003108 if (!(t->flags & TF_ONE_WAY) &&
3109 binder_supported_policy(current->policy)) {
3110 /* Inherit supported policies for synchronous transactions */
3111 t->priority.sched_policy = current->policy;
3112 t->priority.prio = current->normal_prio;
3113 } else {
3114 /* Otherwise, fall back to the default priority */
3115 t->priority = target_proc->default_priority;
3116 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003117
3118 trace_binder_transaction(reply, t, target_node);
3119
Todd Kjosd325d372016-10-10 10:40:53 -07003120 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02003121 tr->offsets_size, extra_buffers_size,
3122 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07003123 if (IS_ERR(t->buffer)) {
3124 /*
3125 * -ESRCH indicates VMA cleared. The target is dying.
3126 */
3127 return_error_param = PTR_ERR(t->buffer);
3128 return_error = return_error_param == -ESRCH ?
3129 BR_DEAD_REPLY : BR_FAILED_REPLY;
3130 return_error_line = __LINE__;
3131 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003132 goto err_binder_alloc_buf_failed;
3133 }
3134 t->buffer->allow_user_free = 0;
3135 t->buffer->debug_id = t->debug_id;
3136 t->buffer->transaction = t;
3137 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003138 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003139 off_start = (binder_size_t *)(t->buffer->data +
3140 ALIGN(tr->data_size, sizeof(void *)));
3141 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003142
Arve Hjønnevågda498892014-02-21 14:40:26 -08003143 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3144 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303145 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3146 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003147 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003148 return_error_param = -EFAULT;
3149 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003150 goto err_copy_data_failed;
3151 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003152 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3153 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303154 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3155 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003156 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003157 return_error_param = -EFAULT;
3158 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003159 goto err_copy_data_failed;
3160 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003161 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3162 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3163 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003164 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003165 return_error_param = -EINVAL;
3166 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003167 goto err_bad_offset;
3168 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003169 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3170 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3171 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303172 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003173 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003174 return_error_param = -EINVAL;
3175 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003176 goto err_bad_offset;
3177 }
3178 off_end = (void *)off_start + tr->offsets_size;
3179 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3180 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003181 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003182 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003183 struct binder_object_header *hdr;
3184 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09003185
Martijn Coenen00c80372016-07-13 12:06:49 +02003186 if (object_size == 0 || *offp < off_min) {
3187 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 -08003188 proc->pid, thread->pid, (u64)*offp,
3189 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003190 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003191 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003192 return_error_param = -EINVAL;
3193 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003194 goto err_bad_offset;
3195 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003196
3197 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3198 off_min = *offp + object_size;
3199 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003200 case BINDER_TYPE_BINDER:
3201 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003202 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003203
Martijn Coenen00c80372016-07-13 12:06:49 +02003204 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003205 ret = binder_translate_binder(fp, t, thread);
3206 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003207 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003208 return_error_param = ret;
3209 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003210 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003211 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003212 } break;
3213 case BINDER_TYPE_HANDLE:
3214 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003215 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003216
Martijn Coenen00c80372016-07-13 12:06:49 +02003217 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003218 ret = binder_translate_handle(fp, t, thread);
3219 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003220 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003221 return_error_param = ret;
3222 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003223 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003224 }
3225 } break;
3226
3227 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003228 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003229 int target_fd = binder_translate_fd(fp->fd, t, thread,
3230 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003231
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003232 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003233 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003234 return_error_param = target_fd;
3235 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003236 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003237 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003238 fp->pad_binder = 0;
3239 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003240 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003241 case BINDER_TYPE_FDA: {
3242 struct binder_fd_array_object *fda =
3243 to_binder_fd_array_object(hdr);
3244 struct binder_buffer_object *parent =
3245 binder_validate_ptr(t->buffer, fda->parent,
3246 off_start,
3247 offp - off_start);
3248 if (!parent) {
3249 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3250 proc->pid, thread->pid);
3251 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003252 return_error_param = -EINVAL;
3253 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003254 goto err_bad_parent;
3255 }
3256 if (!binder_validate_fixup(t->buffer, off_start,
3257 parent, fda->parent_offset,
3258 last_fixup_obj,
3259 last_fixup_min_off)) {
3260 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3261 proc->pid, thread->pid);
3262 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003263 return_error_param = -EINVAL;
3264 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003265 goto err_bad_parent;
3266 }
3267 ret = binder_translate_fd_array(fda, parent, t, thread,
3268 in_reply_to);
3269 if (ret < 0) {
3270 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003271 return_error_param = ret;
3272 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003273 goto err_translate_failed;
3274 }
3275 last_fixup_obj = parent;
3276 last_fixup_min_off =
3277 fda->parent_offset + sizeof(u32) * fda->num_fds;
3278 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003279 case BINDER_TYPE_PTR: {
3280 struct binder_buffer_object *bp =
3281 to_binder_buffer_object(hdr);
3282 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003283
Martijn Coenen5a6da532016-09-30 14:10:07 +02003284 if (bp->length > buf_left) {
3285 binder_user_error("%d:%d got transaction with too large buffer\n",
3286 proc->pid, thread->pid);
3287 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003288 return_error_param = -EINVAL;
3289 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003290 goto err_bad_offset;
3291 }
3292 if (copy_from_user(sg_bufp,
3293 (const void __user *)(uintptr_t)
3294 bp->buffer, bp->length)) {
3295 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3296 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003297 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003298 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003299 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003300 goto err_copy_data_failed;
3301 }
3302 /* Fixup buffer pointer to target proc address space */
3303 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjosd325d372016-10-10 10:40:53 -07003304 binder_alloc_get_user_buffer_offset(
3305 &target_proc->alloc);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003306 sg_bufp += ALIGN(bp->length, sizeof(u64));
3307
3308 ret = binder_fixup_parent(t, thread, bp, off_start,
3309 offp - off_start,
3310 last_fixup_obj,
3311 last_fixup_min_off);
3312 if (ret < 0) {
3313 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003314 return_error_param = ret;
3315 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003316 goto err_translate_failed;
3317 }
3318 last_fixup_obj = bp;
3319 last_fixup_min_off = 0;
3320 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003321 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003322 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003323 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003324 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003325 return_error_param = -EINVAL;
3326 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003327 goto err_bad_object_type;
3328 }
3329 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003330 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003331 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003332
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003333 if (reply) {
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02003334 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003335 binder_inner_proc_lock(target_proc);
3336 if (target_thread->is_dead) {
3337 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003338 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003339 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003340 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003341 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02003342 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003343 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003344 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003345 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003346 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003347 } else if (!(t->flags & TF_ONE_WAY)) {
3348 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003349 binder_inner_proc_lock(proc);
Martijn Coenen21d7d4a2017-11-13 09:55:21 +01003350 /*
3351 * Defer the TRANSACTION_COMPLETE, so we don't return to
3352 * userspace immediately; this allows the target process to
3353 * immediately start processing this transaction, reducing
3354 * latency. We will then return the TRANSACTION_COMPLETE when
3355 * the target replies (or there is an error).
3356 */
3357 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003358 t->need_reply = 1;
3359 t->from_parent = thread->transaction_stack;
3360 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003361 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003362 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003363 binder_inner_proc_lock(proc);
3364 binder_pop_transaction_ilocked(thread, t);
3365 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003366 goto err_dead_proc_or_thread;
3367 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003368 } else {
3369 BUG_ON(target_node == NULL);
3370 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02003371 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen053be422017-06-06 15:17:46 -07003372 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003373 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003374 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003375 if (target_thread)
3376 binder_thread_dec_tmpref(target_thread);
3377 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003378 if (target_node)
3379 binder_dec_node_tmpref(target_node);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003380 /*
3381 * write barrier to synchronize with initialization
3382 * of log entry
3383 */
3384 smp_wmb();
3385 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003386 return;
3387
Todd Kjos2f993e22017-05-12 14:42:55 -07003388err_dead_proc_or_thread:
3389 return_error = BR_DEAD_REPLY;
3390 return_error_line = __LINE__;
Xu YiPing86578a02017-05-22 11:26:23 -07003391 binder_dequeue_work(proc, tcomplete);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003392err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003393err_bad_object_type:
3394err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003395err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003397 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003398 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjos291d9682017-09-25 08:55:09 -07003399 if (target_node)
3400 binder_dec_node_tmpref(target_node);
Todd Kjosc37162d2017-05-26 11:56:29 -07003401 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003402 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003403 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003404err_binder_alloc_buf_failed:
3405 kfree(tcomplete);
3406 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3407err_alloc_tcomplete_failed:
3408 kfree(t);
3409 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3410err_alloc_t_failed:
3411err_bad_call_stack:
3412err_empty_call_stack:
3413err_dead_binder:
3414err_invalid_target_handle:
Todd Kjos2f993e22017-05-12 14:42:55 -07003415 if (target_thread)
3416 binder_thread_dec_tmpref(target_thread);
3417 if (target_proc)
3418 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003419 if (target_node) {
Todd Kjosc37162d2017-05-26 11:56:29 -07003420 binder_dec_node(target_node, 1, 0);
Todd Kjos291d9682017-09-25 08:55:09 -07003421 binder_dec_node_tmpref(target_node);
3422 }
Todd Kjosc37162d2017-05-26 11:56:29 -07003423
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003424 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003425 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3426 proc->pid, thread->pid, return_error, return_error_param,
3427 (u64)tr->data_size, (u64)tr->offsets_size,
3428 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003429
3430 {
3431 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003432
Todd Kjose598d172017-03-22 17:19:52 -07003433 e->return_error = return_error;
3434 e->return_error_param = return_error_param;
3435 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003436 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3437 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003438 /*
3439 * write barrier to synchronize with initialization
3440 * of log entry
3441 */
3442 smp_wmb();
3443 WRITE_ONCE(e->debug_id_done, t_debug_id);
3444 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003445 }
3446
Todd Kjos858b8da2017-04-21 17:35:12 -07003447 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003448 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003449 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003450 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02003451 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003452 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003453 } else {
3454 thread->return_error.cmd = return_error;
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02003455 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos858b8da2017-04-21 17:35:12 -07003456 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003457}
3458
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003459static int binder_thread_write(struct binder_proc *proc,
3460 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003461 binder_uintptr_t binder_buffer, size_t size,
3462 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003463{
3464 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003465 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003466 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003467 void __user *ptr = buffer + *consumed;
3468 void __user *end = buffer + size;
3469
Todd Kjos858b8da2017-04-21 17:35:12 -07003470 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003471 int ret;
3472
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003473 if (get_user(cmd, (uint32_t __user *)ptr))
3474 return -EFAULT;
3475 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003476 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003477 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003478 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3479 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3480 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003481 }
3482 switch (cmd) {
3483 case BC_INCREFS:
3484 case BC_ACQUIRE:
3485 case BC_RELEASE:
3486 case BC_DECREFS: {
3487 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003488 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003489 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3490 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3491 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003492
3493 if (get_user(target, (uint32_t __user *)ptr))
3494 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003495
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003496 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003497 ret = -1;
3498 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003499 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003500 mutex_lock(&context->context_mgr_node_lock);
3501 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003502 if (ctx_mgr_node)
3503 ret = binder_inc_ref_for_node(
3504 proc, ctx_mgr_node,
3505 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003506 mutex_unlock(&context->context_mgr_node_lock);
3507 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003508 if (ret)
3509 ret = binder_update_ref_for_handle(
3510 proc, target, increment, strong,
3511 &rdata);
3512 if (!ret && rdata.desc != target) {
3513 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3514 proc->pid, thread->pid,
3515 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003516 }
3517 switch (cmd) {
3518 case BC_INCREFS:
3519 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003520 break;
3521 case BC_ACQUIRE:
3522 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003523 break;
3524 case BC_RELEASE:
3525 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003526 break;
3527 case BC_DECREFS:
3528 default:
3529 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003530 break;
3531 }
3532 if (ret) {
3533 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3534 proc->pid, thread->pid, debug_string,
3535 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003536 break;
3537 }
3538 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003539 "%d:%d %s ref %d desc %d s %d w %d\n",
3540 proc->pid, thread->pid, debug_string,
3541 rdata.debug_id, rdata.desc, rdata.strong,
3542 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003543 break;
3544 }
3545 case BC_INCREFS_DONE:
3546 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003547 binder_uintptr_t node_ptr;
3548 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003549 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003550 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003551
Arve Hjønnevågda498892014-02-21 14:40:26 -08003552 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003553 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003554 ptr += sizeof(binder_uintptr_t);
3555 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003556 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003557 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003558 node = binder_get_node(proc, node_ptr);
3559 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003560 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003561 proc->pid, thread->pid,
3562 cmd == BC_INCREFS_DONE ?
3563 "BC_INCREFS_DONE" :
3564 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003565 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003566 break;
3567 }
3568 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003569 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003570 proc->pid, thread->pid,
3571 cmd == BC_INCREFS_DONE ?
3572 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003573 (u64)node_ptr, node->debug_id,
3574 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003575 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003576 break;
3577 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003578 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003579 if (cmd == BC_ACQUIRE_DONE) {
3580 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303581 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003582 proc->pid, thread->pid,
3583 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003584 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003585 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003586 break;
3587 }
3588 node->pending_strong_ref = 0;
3589 } else {
3590 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303591 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003592 proc->pid, thread->pid,
3593 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003594 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003595 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003596 break;
3597 }
3598 node->pending_weak_ref = 0;
3599 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003600 free_node = binder_dec_node_nilocked(node,
3601 cmd == BC_ACQUIRE_DONE, 0);
3602 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003603 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003604 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003605 proc->pid, thread->pid,
3606 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003607 node->debug_id, node->local_strong_refs,
3608 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003609 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003610 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003611 break;
3612 }
3613 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303614 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003615 return -EINVAL;
3616 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303617 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003618 return -EINVAL;
3619
3620 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003621 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003622 struct binder_buffer *buffer;
3623
Arve Hjønnevågda498892014-02-21 14:40:26 -08003624 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003625 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003626 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003627
Todd Kjos076072a2017-04-21 14:32:11 -07003628 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3629 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003630 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003631 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3632 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003633 break;
3634 }
3635 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003636 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3637 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003638 break;
3639 }
3640 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003641 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3642 proc->pid, thread->pid, (u64)data_ptr,
3643 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003644 buffer->transaction ? "active" : "finished");
3645
3646 if (buffer->transaction) {
3647 buffer->transaction->buffer = NULL;
3648 buffer->transaction = NULL;
3649 }
3650 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003651 struct binder_node *buf_node;
3652 struct binder_work *w;
3653
3654 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003655 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003656 BUG_ON(!buf_node->has_async_transaction);
3657 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003658 w = binder_dequeue_work_head_ilocked(
3659 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003660 if (!w) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003661 buf_node->has_async_transaction = 0;
Martijn Coenen4501c042017-08-10 13:56:16 +02003662 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003663 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003664 w, &proc->todo);
3665 binder_wakeup_proc_ilocked(proc);
3666 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003667 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003668 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003669 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003670 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07003671 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003672 break;
3673 }
3674
Martijn Coenen5a6da532016-09-30 14:10:07 +02003675 case BC_TRANSACTION_SG:
3676 case BC_REPLY_SG: {
3677 struct binder_transaction_data_sg tr;
3678
3679 if (copy_from_user(&tr, ptr, sizeof(tr)))
3680 return -EFAULT;
3681 ptr += sizeof(tr);
3682 binder_transaction(proc, thread, &tr.transaction_data,
3683 cmd == BC_REPLY_SG, tr.buffers_size);
3684 break;
3685 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003686 case BC_TRANSACTION:
3687 case BC_REPLY: {
3688 struct binder_transaction_data tr;
3689
3690 if (copy_from_user(&tr, ptr, sizeof(tr)))
3691 return -EFAULT;
3692 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003693 binder_transaction(proc, thread, &tr,
3694 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003695 break;
3696 }
3697
3698 case BC_REGISTER_LOOPER:
3699 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303700 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003701 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003702 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003703 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3704 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303705 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003706 proc->pid, thread->pid);
3707 } else if (proc->requested_threads == 0) {
3708 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303709 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003710 proc->pid, thread->pid);
3711 } else {
3712 proc->requested_threads--;
3713 proc->requested_threads_started++;
3714 }
3715 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003716 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003717 break;
3718 case BC_ENTER_LOOPER:
3719 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303720 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003721 proc->pid, thread->pid);
3722 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3723 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303724 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003725 proc->pid, thread->pid);
3726 }
3727 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3728 break;
3729 case BC_EXIT_LOOPER:
3730 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303731 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003732 proc->pid, thread->pid);
3733 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3734 break;
3735
3736 case BC_REQUEST_DEATH_NOTIFICATION:
3737 case BC_CLEAR_DEATH_NOTIFICATION: {
3738 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003739 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003740 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003741 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003742
3743 if (get_user(target, (uint32_t __user *)ptr))
3744 return -EFAULT;
3745 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003746 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003747 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003748 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003749 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3750 /*
3751 * Allocate memory for death notification
3752 * before taking lock
3753 */
3754 death = kzalloc(sizeof(*death), GFP_KERNEL);
3755 if (death == NULL) {
3756 WARN_ON(thread->return_error.cmd !=
3757 BR_OK);
3758 thread->return_error.cmd = BR_ERROR;
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02003759 binder_enqueue_thread_work(
3760 thread,
3761 &thread->return_error.work);
Todd Kjos5346bf32016-10-20 16:43:34 -07003762 binder_debug(
3763 BINDER_DEBUG_FAILED_TRANSACTION,
3764 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3765 proc->pid, thread->pid);
3766 break;
3767 }
3768 }
3769 binder_proc_lock(proc);
3770 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003771 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303772 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003773 proc->pid, thread->pid,
3774 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3775 "BC_REQUEST_DEATH_NOTIFICATION" :
3776 "BC_CLEAR_DEATH_NOTIFICATION",
3777 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003778 binder_proc_unlock(proc);
3779 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003780 break;
3781 }
3782
3783 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003784 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003785 proc->pid, thread->pid,
3786 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3787 "BC_REQUEST_DEATH_NOTIFICATION" :
3788 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003789 (u64)cookie, ref->data.debug_id,
3790 ref->data.desc, ref->data.strong,
3791 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003792
Martijn Coenenf9eac642017-05-22 11:26:23 -07003793 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003794 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3795 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303796 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003797 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003798 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003799 binder_proc_unlock(proc);
3800 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003801 break;
3802 }
3803 binder_stats_created(BINDER_STAT_DEATH);
3804 INIT_LIST_HEAD(&death->work.entry);
3805 death->cookie = cookie;
3806 ref->death = death;
3807 if (ref->node->proc == NULL) {
3808 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02003809
3810 binder_inner_proc_lock(proc);
3811 binder_enqueue_work_ilocked(
3812 &ref->death->work, &proc->todo);
3813 binder_wakeup_proc_ilocked(proc);
3814 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003815 }
3816 } else {
3817 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303818 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003819 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003820 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003821 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003822 break;
3823 }
3824 death = ref->death;
3825 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003826 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003827 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003828 (u64)death->cookie,
3829 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003830 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003831 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003832 break;
3833 }
3834 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003835 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003836 if (list_empty(&death->work.entry)) {
3837 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003838 if (thread->looper &
3839 (BINDER_LOOPER_STATE_REGISTERED |
3840 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02003841 binder_enqueue_thread_work_ilocked(
3842 thread,
3843 &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003844 else {
3845 binder_enqueue_work_ilocked(
3846 &death->work,
3847 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003848 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07003849 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003850 }
3851 } else {
3852 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3853 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3854 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003855 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003856 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07003857 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003858 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003859 } break;
3860 case BC_DEAD_BINDER_DONE: {
3861 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003862 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003863 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003864
Arve Hjønnevågda498892014-02-21 14:40:26 -08003865 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003866 return -EFAULT;
3867
Lisa Du7a64cd82016-02-17 09:32:52 +08003868 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003869 binder_inner_proc_lock(proc);
3870 list_for_each_entry(w, &proc->delivered_death,
3871 entry) {
3872 struct binder_ref_death *tmp_death =
3873 container_of(w,
3874 struct binder_ref_death,
3875 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003876
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003877 if (tmp_death->cookie == cookie) {
3878 death = tmp_death;
3879 break;
3880 }
3881 }
3882 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003883 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3884 proc->pid, thread->pid, (u64)cookie,
3885 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003886 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003887 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3888 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003889 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003890 break;
3891 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003892 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003893 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3894 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003895 if (thread->looper &
3896 (BINDER_LOOPER_STATE_REGISTERED |
3897 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02003898 binder_enqueue_thread_work_ilocked(
3899 thread, &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003900 else {
3901 binder_enqueue_work_ilocked(
3902 &death->work,
3903 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07003904 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003905 }
3906 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003907 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003908 } break;
3909
3910 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303911 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003912 proc->pid, thread->pid, cmd);
3913 return -EINVAL;
3914 }
3915 *consumed = ptr - buffer;
3916 }
3917 return 0;
3918}
3919
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003920static void binder_stat_br(struct binder_proc *proc,
3921 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003922{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003923 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003924 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003925 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3926 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3927 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003928 }
3929}
3930
Todd Kjos60792612017-05-24 10:51:01 -07003931static int binder_put_node_cmd(struct binder_proc *proc,
3932 struct binder_thread *thread,
3933 void __user **ptrp,
3934 binder_uintptr_t node_ptr,
3935 binder_uintptr_t node_cookie,
3936 int node_debug_id,
3937 uint32_t cmd, const char *cmd_name)
3938{
3939 void __user *ptr = *ptrp;
3940
3941 if (put_user(cmd, (uint32_t __user *)ptr))
3942 return -EFAULT;
3943 ptr += sizeof(uint32_t);
3944
3945 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3946 return -EFAULT;
3947 ptr += sizeof(binder_uintptr_t);
3948
3949 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3950 return -EFAULT;
3951 ptr += sizeof(binder_uintptr_t);
3952
3953 binder_stat_br(proc, thread, cmd);
3954 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3955 proc->pid, thread->pid, cmd_name, node_debug_id,
3956 (u64)node_ptr, (u64)node_cookie);
3957
3958 *ptrp = ptr;
3959 return 0;
3960}
3961
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003962static int binder_wait_for_work(struct binder_thread *thread,
3963 bool do_proc_work)
3964{
3965 DEFINE_WAIT(wait);
3966 struct binder_proc *proc = thread->proc;
3967 int ret = 0;
3968
3969 freezer_do_not_count();
3970 binder_inner_proc_lock(proc);
3971 for (;;) {
3972 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3973 if (binder_has_work_ilocked(thread, do_proc_work))
3974 break;
3975 if (do_proc_work)
3976 list_add(&thread->waiting_thread_node,
3977 &proc->waiting_threads);
3978 binder_inner_proc_unlock(proc);
3979 schedule();
3980 binder_inner_proc_lock(proc);
3981 list_del_init(&thread->waiting_thread_node);
3982 if (signal_pending(current)) {
3983 ret = -ERESTARTSYS;
3984 break;
3985 }
3986 }
3987 finish_wait(&thread->wait, &wait);
3988 binder_inner_proc_unlock(proc);
3989 freezer_count();
3990
3991 return ret;
3992}
3993
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003994static int binder_thread_read(struct binder_proc *proc,
3995 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003996 binder_uintptr_t binder_buffer, size_t size,
3997 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003998{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003999 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004000 void __user *ptr = buffer + *consumed;
4001 void __user *end = buffer + size;
4002
4003 int ret = 0;
4004 int wait_for_proc_work;
4005
4006 if (*consumed == 0) {
4007 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4008 return -EFAULT;
4009 ptr += sizeof(uint32_t);
4010 }
4011
4012retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07004013 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004014 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07004015 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004016
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004017 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004018
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004019 trace_binder_wait_for_work(wait_for_proc_work,
4020 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004021 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004022 if (wait_for_proc_work) {
4023 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4024 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304025 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 +09004026 proc->pid, thread->pid, thread->looper);
4027 wait_event_interruptible(binder_user_error_wait,
4028 binder_stop_on_user_error < 2);
4029 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07004030 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004031 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004032
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004033 if (non_block) {
4034 if (!binder_has_work(thread, wait_for_proc_work))
4035 ret = -EAGAIN;
4036 } else {
4037 ret = binder_wait_for_work(thread, wait_for_proc_work);
4038 }
4039
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004040 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4041
4042 if (ret)
4043 return ret;
4044
4045 while (1) {
4046 uint32_t cmd;
4047 struct binder_transaction_data tr;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004048 struct binder_work *w = NULL;
4049 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004050 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07004051 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004052
Todd Kjose7f23ed2017-03-21 13:06:01 -07004053 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004054 if (!binder_worklist_empty_ilocked(&thread->todo))
4055 list = &thread->todo;
4056 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4057 wait_for_proc_work)
4058 list = &proc->todo;
4059 else {
4060 binder_inner_proc_unlock(proc);
4061
Dmitry Voytik395262a2014-09-08 18:16:34 +04004062 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08004063 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004064 goto retry;
4065 break;
4066 }
4067
Todd Kjose7f23ed2017-03-21 13:06:01 -07004068 if (end - ptr < sizeof(tr) + 4) {
4069 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004070 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004071 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004072 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen77fdc9d2017-10-19 15:04:46 +02004073 if (binder_worklist_empty_ilocked(&thread->todo))
4074 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004075
4076 switch (w->type) {
4077 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004078 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004079 t = container_of(w, struct binder_transaction, work);
4080 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004081 case BINDER_WORK_RETURN_ERROR: {
4082 struct binder_error *e = container_of(
4083 w, struct binder_error, work);
4084
4085 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004086 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07004087 if (put_user(e->cmd, (uint32_t __user *)ptr))
4088 return -EFAULT;
4089 e->cmd = BR_OK;
4090 ptr += sizeof(uint32_t);
4091
4092 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07004093 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004094 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004095 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004096 cmd = BR_TRANSACTION_COMPLETE;
4097 if (put_user(cmd, (uint32_t __user *)ptr))
4098 return -EFAULT;
4099 ptr += sizeof(uint32_t);
4100
4101 binder_stat_br(proc, thread, cmd);
4102 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304103 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004104 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004105 kfree(w);
4106 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4107 } break;
4108 case BINDER_WORK_NODE: {
4109 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07004110 int strong, weak;
4111 binder_uintptr_t node_ptr = node->ptr;
4112 binder_uintptr_t node_cookie = node->cookie;
4113 int node_debug_id = node->debug_id;
4114 int has_weak_ref;
4115 int has_strong_ref;
4116 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004117
Todd Kjos60792612017-05-24 10:51:01 -07004118 BUG_ON(proc != node->proc);
4119 strong = node->internal_strong_refs ||
4120 node->local_strong_refs;
4121 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07004122 node->local_weak_refs ||
4123 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004124 has_strong_ref = node->has_strong_ref;
4125 has_weak_ref = node->has_weak_ref;
4126
4127 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004128 node->has_weak_ref = 1;
4129 node->pending_weak_ref = 1;
4130 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004131 }
4132 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004133 node->has_strong_ref = 1;
4134 node->pending_strong_ref = 1;
4135 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004136 }
4137 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004138 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004139 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004140 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004141 if (!weak && !strong) {
4142 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4143 "%d:%d node %d u%016llx c%016llx deleted\n",
4144 proc->pid, thread->pid,
4145 node_debug_id,
4146 (u64)node_ptr,
4147 (u64)node_cookie);
4148 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004149 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004150 binder_node_lock(node);
4151 /*
4152 * Acquire the node lock before freeing the
4153 * node to serialize with other threads that
4154 * may have been holding the node lock while
4155 * decrementing this node (avoids race where
4156 * this thread frees while the other thread
4157 * is unlocking the node after the final
4158 * decrement)
4159 */
4160 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004161 binder_free_node(node);
4162 } else
4163 binder_inner_proc_unlock(proc);
4164
Todd Kjos60792612017-05-24 10:51:01 -07004165 if (weak && !has_weak_ref)
4166 ret = binder_put_node_cmd(
4167 proc, thread, &ptr, node_ptr,
4168 node_cookie, node_debug_id,
4169 BR_INCREFS, "BR_INCREFS");
4170 if (!ret && strong && !has_strong_ref)
4171 ret = binder_put_node_cmd(
4172 proc, thread, &ptr, node_ptr,
4173 node_cookie, node_debug_id,
4174 BR_ACQUIRE, "BR_ACQUIRE");
4175 if (!ret && !strong && has_strong_ref)
4176 ret = binder_put_node_cmd(
4177 proc, thread, &ptr, node_ptr,
4178 node_cookie, node_debug_id,
4179 BR_RELEASE, "BR_RELEASE");
4180 if (!ret && !weak && has_weak_ref)
4181 ret = binder_put_node_cmd(
4182 proc, thread, &ptr, node_ptr,
4183 node_cookie, node_debug_id,
4184 BR_DECREFS, "BR_DECREFS");
4185 if (orig_ptr == ptr)
4186 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4187 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4188 proc->pid, thread->pid,
4189 node_debug_id,
4190 (u64)node_ptr,
4191 (u64)node_cookie);
4192 if (ret)
4193 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004194 } break;
4195 case BINDER_WORK_DEAD_BINDER:
4196 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4197 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4198 struct binder_ref_death *death;
4199 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004200 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004201
4202 death = container_of(w, struct binder_ref_death, work);
4203 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4204 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4205 else
4206 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004207 cookie = death->cookie;
4208
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004209 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004210 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004211 proc->pid, thread->pid,
4212 cmd == BR_DEAD_BINDER ?
4213 "BR_DEAD_BINDER" :
4214 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004215 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004216 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004217 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004218 kfree(death);
4219 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004220 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004221 binder_enqueue_work_ilocked(
4222 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004223 binder_inner_proc_unlock(proc);
4224 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004225 if (put_user(cmd, (uint32_t __user *)ptr))
4226 return -EFAULT;
4227 ptr += sizeof(uint32_t);
4228 if (put_user(cookie,
4229 (binder_uintptr_t __user *)ptr))
4230 return -EFAULT;
4231 ptr += sizeof(binder_uintptr_t);
4232 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004233 if (cmd == BR_DEAD_BINDER)
4234 goto done; /* DEAD_BINDER notifications can cause transactions */
4235 } break;
4236 }
4237
4238 if (!t)
4239 continue;
4240
4241 BUG_ON(t->buffer == NULL);
4242 if (t->buffer->target_node) {
4243 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004244 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004245
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004246 tr.target.ptr = target_node->ptr;
4247 tr.cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004248 node_prio.sched_policy = target_node->sched_policy;
4249 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004250 binder_transaction_priority(current, t, node_prio,
4251 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004252 cmd = BR_TRANSACTION;
4253 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004254 tr.target.ptr = 0;
4255 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004256 cmd = BR_REPLY;
4257 }
4258 tr.code = t->code;
4259 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06004260 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004261
Todd Kjos2f993e22017-05-12 14:42:55 -07004262 t_from = binder_get_txn_from(t);
4263 if (t_from) {
4264 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004265
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004266 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004267 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004268 } else {
4269 tr.sender_pid = 0;
4270 }
4271
4272 tr.data_size = t->buffer->data_size;
4273 tr.offsets_size = t->buffer->offsets_size;
Todd Kjosd325d372016-10-10 10:40:53 -07004274 tr.data.ptr.buffer = (binder_uintptr_t)
4275 ((uintptr_t)t->buffer->data +
4276 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004277 tr.data.ptr.offsets = tr.data.ptr.buffer +
4278 ALIGN(t->buffer->data_size,
4279 sizeof(void *));
4280
Todd Kjos2f993e22017-05-12 14:42:55 -07004281 if (put_user(cmd, (uint32_t __user *)ptr)) {
4282 if (t_from)
4283 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004284
4285 binder_cleanup_transaction(t, "put_user failed",
4286 BR_FAILED_REPLY);
4287
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004288 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004289 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004290 ptr += sizeof(uint32_t);
Todd Kjos2f993e22017-05-12 14:42:55 -07004291 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4292 if (t_from)
4293 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004294
4295 binder_cleanup_transaction(t, "copy_to_user failed",
4296 BR_FAILED_REPLY);
4297
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004298 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004299 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004300 ptr += sizeof(tr);
4301
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004302 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004303 binder_stat_br(proc, thread, cmd);
4304 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004305 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004306 proc->pid, thread->pid,
4307 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4308 "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004309 t->debug_id, t_from ? t_from->proc->pid : 0,
4310 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004311 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004312 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004313
Todd Kjos2f993e22017-05-12 14:42:55 -07004314 if (t_from)
4315 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004316 t->buffer->allow_user_free = 1;
4317 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004318 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004319 t->to_parent = thread->transaction_stack;
4320 t->to_thread = thread;
4321 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004322 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004323 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004324 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004325 }
4326 break;
4327 }
4328
4329done:
4330
4331 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004332 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004333 if (proc->requested_threads == 0 &&
4334 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004335 proc->requested_threads_started < proc->max_threads &&
4336 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4337 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4338 /*spawn a new thread if we leave this out */) {
4339 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004340 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004341 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304342 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004343 proc->pid, thread->pid);
4344 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4345 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004346 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004347 } else
4348 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004349 return 0;
4350}
4351
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004352static void binder_release_work(struct binder_proc *proc,
4353 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004354{
4355 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004356
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004357 while (1) {
4358 w = binder_dequeue_work_head(proc, list);
4359 if (!w)
4360 return;
4361
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004362 switch (w->type) {
4363 case BINDER_WORK_TRANSACTION: {
4364 struct binder_transaction *t;
4365
4366 t = container_of(w, struct binder_transaction, work);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004367
4368 binder_cleanup_transaction(t, "process died.",
4369 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004370 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004371 case BINDER_WORK_RETURN_ERROR: {
4372 struct binder_error *e = container_of(
4373 w, struct binder_error, work);
4374
4375 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4376 "undelivered TRANSACTION_ERROR: %u\n",
4377 e->cmd);
4378 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004379 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004380 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304381 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004382 kfree(w);
4383 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4384 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004385 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4386 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4387 struct binder_ref_death *death;
4388
4389 death = container_of(w, struct binder_ref_death, work);
4390 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004391 "undelivered death notification, %016llx\n",
4392 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004393 kfree(death);
4394 binder_stats_deleted(BINDER_STAT_DEATH);
4395 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004396 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304397 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004398 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004399 break;
4400 }
4401 }
4402
4403}
4404
Todd Kjosb4827902017-05-25 15:52:17 -07004405static struct binder_thread *binder_get_thread_ilocked(
4406 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004407{
4408 struct binder_thread *thread = NULL;
4409 struct rb_node *parent = NULL;
4410 struct rb_node **p = &proc->threads.rb_node;
4411
4412 while (*p) {
4413 parent = *p;
4414 thread = rb_entry(parent, struct binder_thread, rb_node);
4415
4416 if (current->pid < thread->pid)
4417 p = &(*p)->rb_left;
4418 else if (current->pid > thread->pid)
4419 p = &(*p)->rb_right;
4420 else
Todd Kjosb4827902017-05-25 15:52:17 -07004421 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004422 }
Todd Kjosb4827902017-05-25 15:52:17 -07004423 if (!new_thread)
4424 return NULL;
4425 thread = new_thread;
4426 binder_stats_created(BINDER_STAT_THREAD);
4427 thread->proc = proc;
4428 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004429 get_task_struct(current);
4430 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004431 atomic_set(&thread->tmp_ref, 0);
4432 init_waitqueue_head(&thread->wait);
4433 INIT_LIST_HEAD(&thread->todo);
4434 rb_link_node(&thread->rb_node, parent, p);
4435 rb_insert_color(&thread->rb_node, &proc->threads);
4436 thread->looper_need_return = true;
4437 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4438 thread->return_error.cmd = BR_OK;
4439 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4440 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004441 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004442 return thread;
4443}
4444
4445static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4446{
4447 struct binder_thread *thread;
4448 struct binder_thread *new_thread;
4449
4450 binder_inner_proc_lock(proc);
4451 thread = binder_get_thread_ilocked(proc, NULL);
4452 binder_inner_proc_unlock(proc);
4453 if (!thread) {
4454 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4455 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004456 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004457 binder_inner_proc_lock(proc);
4458 thread = binder_get_thread_ilocked(proc, new_thread);
4459 binder_inner_proc_unlock(proc);
4460 if (thread != new_thread)
4461 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004462 }
4463 return thread;
4464}
4465
Todd Kjos2f993e22017-05-12 14:42:55 -07004466static void binder_free_proc(struct binder_proc *proc)
4467{
4468 BUG_ON(!list_empty(&proc->todo));
4469 BUG_ON(!list_empty(&proc->delivered_death));
4470 binder_alloc_deferred_release(&proc->alloc);
4471 put_task_struct(proc->tsk);
4472 binder_stats_deleted(BINDER_STAT_PROC);
4473 kfree(proc);
4474}
4475
4476static void binder_free_thread(struct binder_thread *thread)
4477{
4478 BUG_ON(!list_empty(&thread->todo));
4479 binder_stats_deleted(BINDER_STAT_THREAD);
4480 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004481 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004482 kfree(thread);
4483}
4484
4485static int binder_thread_release(struct binder_proc *proc,
4486 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004487{
4488 struct binder_transaction *t;
4489 struct binder_transaction *send_reply = NULL;
4490 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004491 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004492
Todd Kjosb4827902017-05-25 15:52:17 -07004493 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004494 /*
4495 * take a ref on the proc so it survives
4496 * after we remove this thread from proc->threads.
4497 * The corresponding dec is when we actually
4498 * free the thread in binder_free_thread()
4499 */
4500 proc->tmp_ref++;
4501 /*
4502 * take a ref on this thread to ensure it
4503 * survives while we are releasing it
4504 */
4505 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004506 rb_erase(&thread->rb_node, &proc->threads);
4507 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004508 if (t) {
4509 spin_lock(&t->lock);
4510 if (t->to_thread == thread)
4511 send_reply = t;
4512 }
4513 thread->is_dead = true;
4514
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004515 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004516 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004517 active_transactions++;
4518 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304519 "release %d:%d transaction %d %s, still active\n",
4520 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004521 t->debug_id,
4522 (t->to_thread == thread) ? "in" : "out");
4523
4524 if (t->to_thread == thread) {
4525 t->to_proc = NULL;
4526 t->to_thread = NULL;
4527 if (t->buffer) {
4528 t->buffer->transaction = NULL;
4529 t->buffer = NULL;
4530 }
4531 t = t->to_parent;
4532 } else if (t->from == thread) {
4533 t->from = NULL;
4534 t = t->from_parent;
4535 } else
4536 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004537 spin_unlock(&last_t->lock);
4538 if (t)
4539 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004540 }
Todd Kjosb4827902017-05-25 15:52:17 -07004541 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004542
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004543 if (send_reply)
4544 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004545 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004546 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004547 return active_transactions;
4548}
4549
4550static unsigned int binder_poll(struct file *filp,
4551 struct poll_table_struct *wait)
4552{
4553 struct binder_proc *proc = filp->private_data;
4554 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004555 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004556
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004557 thread = binder_get_thread(proc);
4558
Martijn Coenen995a36e2017-06-02 13:36:52 -07004559 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004560 thread->looper |= BINDER_LOOPER_STATE_POLL;
4561 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4562
Martijn Coenen995a36e2017-06-02 13:36:52 -07004563 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004564
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004565 poll_wait(filp, &thread->wait, wait);
4566
Martijn Coenen47810932017-08-10 12:32:00 +02004567 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004568 return POLLIN;
4569
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004570 return 0;
4571}
4572
Tair Rzayev78260ac2014-06-03 22:27:21 +03004573static int binder_ioctl_write_read(struct file *filp,
4574 unsigned int cmd, unsigned long arg,
4575 struct binder_thread *thread)
4576{
4577 int ret = 0;
4578 struct binder_proc *proc = filp->private_data;
4579 unsigned int size = _IOC_SIZE(cmd);
4580 void __user *ubuf = (void __user *)arg;
4581 struct binder_write_read bwr;
4582
4583 if (size != sizeof(struct binder_write_read)) {
4584 ret = -EINVAL;
4585 goto out;
4586 }
4587 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4588 ret = -EFAULT;
4589 goto out;
4590 }
4591 binder_debug(BINDER_DEBUG_READ_WRITE,
4592 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4593 proc->pid, thread->pid,
4594 (u64)bwr.write_size, (u64)bwr.write_buffer,
4595 (u64)bwr.read_size, (u64)bwr.read_buffer);
4596
4597 if (bwr.write_size > 0) {
4598 ret = binder_thread_write(proc, thread,
4599 bwr.write_buffer,
4600 bwr.write_size,
4601 &bwr.write_consumed);
4602 trace_binder_write_done(ret);
4603 if (ret < 0) {
4604 bwr.read_consumed = 0;
4605 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4606 ret = -EFAULT;
4607 goto out;
4608 }
4609 }
4610 if (bwr.read_size > 0) {
4611 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4612 bwr.read_size,
4613 &bwr.read_consumed,
4614 filp->f_flags & O_NONBLOCK);
4615 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004616 binder_inner_proc_lock(proc);
4617 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004618 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004619 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004620 if (ret < 0) {
4621 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4622 ret = -EFAULT;
4623 goto out;
4624 }
4625 }
4626 binder_debug(BINDER_DEBUG_READ_WRITE,
4627 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4628 proc->pid, thread->pid,
4629 (u64)bwr.write_consumed, (u64)bwr.write_size,
4630 (u64)bwr.read_consumed, (u64)bwr.read_size);
4631 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4632 ret = -EFAULT;
4633 goto out;
4634 }
4635out:
4636 return ret;
4637}
4638
4639static int binder_ioctl_set_ctx_mgr(struct file *filp)
4640{
4641 int ret = 0;
4642 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004643 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004644 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004645 kuid_t curr_euid = current_euid();
4646
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004647 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004648 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004649 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4650 ret = -EBUSY;
4651 goto out;
4652 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004653 ret = security_binder_set_context_mgr(proc->tsk);
4654 if (ret < 0)
4655 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004656 if (uid_valid(context->binder_context_mgr_uid)) {
4657 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004658 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4659 from_kuid(&init_user_ns, curr_euid),
4660 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004661 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004662 ret = -EPERM;
4663 goto out;
4664 }
4665 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004666 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004667 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004668 new_node = binder_new_node(proc, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004669 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004670 ret = -ENOMEM;
4671 goto out;
4672 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004673 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004674 new_node->local_weak_refs++;
4675 new_node->local_strong_refs++;
4676 new_node->has_strong_ref = 1;
4677 new_node->has_weak_ref = 1;
4678 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004679 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004680 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004681out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004682 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004683 return ret;
4684}
4685
Colin Cross833babb32017-06-20 13:54:44 -07004686static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4687 struct binder_node_debug_info *info) {
4688 struct rb_node *n;
4689 binder_uintptr_t ptr = info->ptr;
4690
4691 memset(info, 0, sizeof(*info));
4692
4693 binder_inner_proc_lock(proc);
4694 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4695 struct binder_node *node = rb_entry(n, struct binder_node,
4696 rb_node);
4697 if (node->ptr > ptr) {
4698 info->ptr = node->ptr;
4699 info->cookie = node->cookie;
4700 info->has_strong_ref = node->has_strong_ref;
4701 info->has_weak_ref = node->has_weak_ref;
4702 break;
4703 }
4704 }
4705 binder_inner_proc_unlock(proc);
4706
4707 return 0;
4708}
4709
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004710static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4711{
4712 int ret;
4713 struct binder_proc *proc = filp->private_data;
4714 struct binder_thread *thread;
4715 unsigned int size = _IOC_SIZE(cmd);
4716 void __user *ubuf = (void __user *)arg;
4717
Tair Rzayev78260ac2014-06-03 22:27:21 +03004718 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4719 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004720
Sherry Yang435416b2017-06-22 14:37:45 -07004721 binder_selftest_alloc(&proc->alloc);
4722
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004723 trace_binder_ioctl(cmd, arg);
4724
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004725 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4726 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004727 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004728
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004729 thread = binder_get_thread(proc);
4730 if (thread == NULL) {
4731 ret = -ENOMEM;
4732 goto err;
4733 }
4734
4735 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004736 case BINDER_WRITE_READ:
4737 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4738 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004739 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004740 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004741 case BINDER_SET_MAX_THREADS: {
4742 int max_threads;
4743
4744 if (copy_from_user(&max_threads, ubuf,
4745 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004746 ret = -EINVAL;
4747 goto err;
4748 }
Todd Kjosd600e902017-05-25 17:35:02 -07004749 binder_inner_proc_lock(proc);
4750 proc->max_threads = max_threads;
4751 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004752 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004753 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004754 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004755 ret = binder_ioctl_set_ctx_mgr(filp);
4756 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004757 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004758 break;
4759 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304760 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004761 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07004762 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004763 thread = NULL;
4764 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004765 case BINDER_VERSION: {
4766 struct binder_version __user *ver = ubuf;
4767
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004768 if (size != sizeof(struct binder_version)) {
4769 ret = -EINVAL;
4770 goto err;
4771 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004772 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4773 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004774 ret = -EINVAL;
4775 goto err;
4776 }
4777 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004778 }
Colin Cross833babb32017-06-20 13:54:44 -07004779 case BINDER_GET_NODE_DEBUG_INFO: {
4780 struct binder_node_debug_info info;
4781
4782 if (copy_from_user(&info, ubuf, sizeof(info))) {
4783 ret = -EFAULT;
4784 goto err;
4785 }
4786
4787 ret = binder_ioctl_get_node_debug_info(proc, &info);
4788 if (ret < 0)
4789 goto err;
4790
4791 if (copy_to_user(ubuf, &info, sizeof(info))) {
4792 ret = -EFAULT;
4793 goto err;
4794 }
4795 break;
4796 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004797 default:
4798 ret = -EINVAL;
4799 goto err;
4800 }
4801 ret = 0;
4802err:
4803 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08004804 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004805 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4806 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304807 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 -07004808err_unlocked:
4809 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004810 return ret;
4811}
4812
4813static void binder_vma_open(struct vm_area_struct *vma)
4814{
4815 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004816
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004817 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304818 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004819 proc->pid, vma->vm_start, vma->vm_end,
4820 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4821 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004822}
4823
4824static void binder_vma_close(struct vm_area_struct *vma)
4825{
4826 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004827
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004828 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304829 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004830 proc->pid, vma->vm_start, vma->vm_end,
4831 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4832 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07004833 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004834}
4835
Vinayak Menonddac7d52014-06-02 18:17:59 +05304836static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4837{
4838 return VM_FAULT_SIGBUS;
4839}
4840
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004841static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004842 .open = binder_vma_open,
4843 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304844 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004845};
4846
Todd Kjosd325d372016-10-10 10:40:53 -07004847static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4848{
4849 int ret;
4850 struct binder_proc *proc = filp->private_data;
4851 const char *failure_string;
4852
4853 if (proc->tsk != current->group_leader)
4854 return -EINVAL;
4855
4856 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4857 vma->vm_end = vma->vm_start + SZ_4M;
4858
4859 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4860 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4861 __func__, proc->pid, vma->vm_start, vma->vm_end,
4862 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4863 (unsigned long)pgprot_val(vma->vm_page_prot));
4864
4865 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4866 ret = -EPERM;
4867 failure_string = "bad vm_flags";
4868 goto err_bad_arg;
4869 }
4870 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4871 vma->vm_ops = &binder_vm_ops;
4872 vma->vm_private_data = proc;
4873
4874 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
Todd Kjoseb28a0f2017-11-10 15:30:27 -08004875
4876 return ret;
Todd Kjosd325d372016-10-10 10:40:53 -07004877
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004878err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004879 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004880 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4881 return ret;
4882}
4883
4884static int binder_open(struct inode *nodp, struct file *filp)
4885{
4886 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004887 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004888
4889 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4890 current->group_leader->pid, current->pid);
4891
4892 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4893 if (proc == NULL)
4894 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07004895 spin_lock_init(&proc->inner_lock);
4896 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01004897 get_task_struct(current->group_leader);
4898 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004899 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07004900 if (binder_supported_policy(current->policy)) {
4901 proc->default_priority.sched_policy = current->policy;
4902 proc->default_priority.prio = current->normal_prio;
4903 } else {
4904 proc->default_priority.sched_policy = SCHED_NORMAL;
4905 proc->default_priority.prio = NICE_TO_PRIO(0);
4906 }
4907
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004908 binder_dev = container_of(filp->private_data, struct binder_device,
4909 miscdev);
4910 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07004911 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004912
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004913 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004914 proc->pid = current->group_leader->pid;
4915 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004916 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004917 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004918
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004919 mutex_lock(&binder_procs_lock);
4920 hlist_add_head(&proc->proc_node, &binder_procs);
4921 mutex_unlock(&binder_procs_lock);
4922
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004923 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004924 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004925
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004926 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004927 /*
4928 * proc debug entries are shared between contexts, so
4929 * this will fail if the process tries to open the driver
4930 * again with a different context. The priting code will
4931 * anyway print all contexts that a given PID has, so this
4932 * is not a problem.
4933 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004934 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004935 binder_debugfs_dir_entry_proc,
4936 (void *)(unsigned long)proc->pid,
4937 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004938 }
4939
4940 return 0;
4941}
4942
4943static int binder_flush(struct file *filp, fl_owner_t id)
4944{
4945 struct binder_proc *proc = filp->private_data;
4946
4947 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4948
4949 return 0;
4950}
4951
4952static void binder_deferred_flush(struct binder_proc *proc)
4953{
4954 struct rb_node *n;
4955 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004956
Todd Kjosb4827902017-05-25 15:52:17 -07004957 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004958 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4959 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004960
Todd Kjos6798e6d2017-01-06 14:19:25 -08004961 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004962 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4963 wake_up_interruptible(&thread->wait);
4964 wake_count++;
4965 }
4966 }
Todd Kjosb4827902017-05-25 15:52:17 -07004967 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004968
4969 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4970 "binder_flush: %d woke %d threads\n", proc->pid,
4971 wake_count);
4972}
4973
4974static int binder_release(struct inode *nodp, struct file *filp)
4975{
4976 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004977
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004978 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004979 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4980
4981 return 0;
4982}
4983
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004984static int binder_node_release(struct binder_node *node, int refs)
4985{
4986 struct binder_ref *ref;
4987 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004988 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004989
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004990 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004991
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004992 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004993 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004994 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07004995 /*
4996 * The caller must have taken a temporary ref on the node,
4997 */
4998 BUG_ON(!node->tmp_refs);
4999 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07005000 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005001 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005002 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005003
5004 return refs;
5005 }
5006
5007 node->proc = NULL;
5008 node->local_strong_refs = 0;
5009 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005010 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005011
5012 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005013 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005014 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005015
5016 hlist_for_each_entry(ref, &node->refs, node_entry) {
5017 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005018 /*
5019 * Need the node lock to synchronize
5020 * with new notification requests and the
5021 * inner lock to synchronize with queued
5022 * death notifications.
5023 */
5024 binder_inner_proc_lock(ref->proc);
5025 if (!ref->death) {
5026 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08005027 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005028 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005029
5030 death++;
5031
Martijn Coenenf9eac642017-05-22 11:26:23 -07005032 BUG_ON(!list_empty(&ref->death->work.entry));
5033 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5034 binder_enqueue_work_ilocked(&ref->death->work,
5035 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07005036 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005037 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005038 }
5039
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005040 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5041 "node %d now dead, refs %d, death %d\n",
5042 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005043 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07005044 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005045
5046 return refs;
5047}
5048
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005049static void binder_deferred_release(struct binder_proc *proc)
5050{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005051 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005052 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07005053 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005054
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005055 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005056 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005057 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005058
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005059 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005060 if (context->binder_context_mgr_node &&
5061 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005062 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005063 "%s: %d context_mgr_node gone\n",
5064 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005065 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005066 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005067 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07005068 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07005069 /*
5070 * Make sure proc stays alive after we
5071 * remove all the threads
5072 */
5073 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005074
Todd Kjos2f993e22017-05-12 14:42:55 -07005075 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005076 threads = 0;
5077 active_transactions = 0;
5078 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005079 struct binder_thread *thread;
5080
5081 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07005082 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005083 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07005084 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07005085 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005086 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005087
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005088 nodes = 0;
5089 incoming_refs = 0;
5090 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005091 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005092
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005093 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005094 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07005095 /*
5096 * take a temporary ref on the node before
5097 * calling binder_node_release() which will either
5098 * kfree() the node or call binder_put_node()
5099 */
Todd Kjos425d23f2017-06-12 12:07:26 -07005100 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005101 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07005102 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005103 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07005104 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005105 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005106 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005107
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005108 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005109 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005110 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005111 struct binder_ref *ref;
5112
5113 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005114 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07005115 binder_cleanup_ref_olocked(ref);
5116 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005117 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07005118 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005119 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005120 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005121
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005122 binder_release_work(proc, &proc->todo);
5123 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005124
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005125 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005126 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005127 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005128 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005129
Todd Kjos2f993e22017-05-12 14:42:55 -07005130 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005131}
5132
5133static void binder_deferred_func(struct work_struct *work)
5134{
5135 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005136 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005137
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005138 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005139 mutex_lock(&binder_deferred_lock);
5140 if (!hlist_empty(&binder_deferred_list)) {
5141 proc = hlist_entry(binder_deferred_list.first,
5142 struct binder_proc, deferred_work_node);
5143 hlist_del_init(&proc->deferred_work_node);
5144 defer = proc->deferred_work;
5145 proc->deferred_work = 0;
5146 } else {
5147 proc = NULL;
5148 defer = 0;
5149 }
5150 mutex_unlock(&binder_deferred_lock);
5151
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005152 if (defer & BINDER_DEFERRED_FLUSH)
5153 binder_deferred_flush(proc);
5154
5155 if (defer & BINDER_DEFERRED_RELEASE)
5156 binder_deferred_release(proc); /* frees proc */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005157 } while (proc);
5158}
5159static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5160
5161static void
5162binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5163{
5164 mutex_lock(&binder_deferred_lock);
5165 proc->deferred_work |= defer;
5166 if (hlist_unhashed(&proc->deferred_work_node)) {
5167 hlist_add_head(&proc->deferred_work_node,
5168 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305169 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005170 }
5171 mutex_unlock(&binder_deferred_lock);
5172}
5173
Todd Kjos6d241a42017-04-21 14:32:11 -07005174static void print_binder_transaction_ilocked(struct seq_file *m,
5175 struct binder_proc *proc,
5176 const char *prefix,
5177 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005178{
Todd Kjos6d241a42017-04-21 14:32:11 -07005179 struct binder_proc *to_proc;
5180 struct binder_buffer *buffer = t->buffer;
5181
Todd Kjos2f993e22017-05-12 14:42:55 -07005182 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005183 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005184 seq_printf(m,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005185 "%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 -07005186 prefix, t->debug_id, t,
5187 t->from ? t->from->proc->pid : 0,
5188 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005189 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005190 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005191 t->code, t->flags, t->priority.sched_policy,
5192 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005193 spin_unlock(&t->lock);
5194
Todd Kjos6d241a42017-04-21 14:32:11 -07005195 if (proc != to_proc) {
5196 /*
5197 * Can only safely deref buffer if we are holding the
5198 * correct proc inner lock for this node
5199 */
5200 seq_puts(m, "\n");
5201 return;
5202 }
5203
5204 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005205 seq_puts(m, " buffer free\n");
5206 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005207 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005208 if (buffer->target_node)
5209 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005210 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005211 buffer->data_size, buffer->offsets_size,
5212 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005213}
5214
Todd Kjos6d241a42017-04-21 14:32:11 -07005215static void print_binder_work_ilocked(struct seq_file *m,
5216 struct binder_proc *proc,
5217 const char *prefix,
5218 const char *transaction_prefix,
5219 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005220{
5221 struct binder_node *node;
5222 struct binder_transaction *t;
5223
5224 switch (w->type) {
5225 case BINDER_WORK_TRANSACTION:
5226 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005227 print_binder_transaction_ilocked(
5228 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005229 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005230 case BINDER_WORK_RETURN_ERROR: {
5231 struct binder_error *e = container_of(
5232 w, struct binder_error, work);
5233
5234 seq_printf(m, "%stransaction error: %u\n",
5235 prefix, e->cmd);
5236 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005237 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005238 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005239 break;
5240 case BINDER_WORK_NODE:
5241 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005242 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5243 prefix, node->debug_id,
5244 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005245 break;
5246 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005247 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005248 break;
5249 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005250 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005251 break;
5252 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005253 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005254 break;
5255 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005256 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005257 break;
5258 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005259}
5260
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005261static void print_binder_thread_ilocked(struct seq_file *m,
5262 struct binder_thread *thread,
5263 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005264{
5265 struct binder_transaction *t;
5266 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005267 size_t start_pos = m->count;
5268 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005269
Todd Kjos2f993e22017-05-12 14:42:55 -07005270 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005271 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005272 thread->looper_need_return,
5273 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005274 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005275 t = thread->transaction_stack;
5276 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005277 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005278 print_binder_transaction_ilocked(m, thread->proc,
5279 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005280 t = t->from_parent;
5281 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005282 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005283 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005284 t = t->to_parent;
5285 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005286 print_binder_transaction_ilocked(m, thread->proc,
5287 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005288 t = NULL;
5289 }
5290 }
5291 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005292 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005293 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005294 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005295 if (!print_always && m->count == header_pos)
5296 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005297}
5298
Todd Kjos425d23f2017-06-12 12:07:26 -07005299static void print_binder_node_nilocked(struct seq_file *m,
5300 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005301{
5302 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005303 struct binder_work *w;
5304 int count;
5305
5306 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005307 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005308 count++;
5309
Martijn Coenen6aac9792017-06-07 09:29:14 -07005310 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 -08005311 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005312 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005313 node->has_strong_ref, node->has_weak_ref,
5314 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005315 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005316 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005317 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005318 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005319 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005320 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005321 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005322 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005323 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005324 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005325 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005326 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005327}
5328
Todd Kjos5346bf32016-10-20 16:43:34 -07005329static void print_binder_ref_olocked(struct seq_file *m,
5330 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005331{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005332 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005333 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5334 ref->data.debug_id, ref->data.desc,
5335 ref->node->proc ? "" : "dead ",
5336 ref->node->debug_id, ref->data.strong,
5337 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005338 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005339}
5340
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005341static void print_binder_proc(struct seq_file *m,
5342 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005343{
5344 struct binder_work *w;
5345 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005346 size_t start_pos = m->count;
5347 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005348 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005349
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005350 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005351 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005352 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005353
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005354 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005355 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005356 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005357 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005358
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005359 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005360 struct binder_node *node = rb_entry(n, struct binder_node,
5361 rb_node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005362 /*
5363 * take a temporary reference on the node so it
5364 * survives and isn't removed from the tree
5365 * while we print it.
5366 */
5367 binder_inc_node_tmpref_ilocked(node);
5368 /* Need to drop inner lock to take node lock */
5369 binder_inner_proc_unlock(proc);
5370 if (last_node)
5371 binder_put_node(last_node);
5372 binder_node_inner_lock(node);
5373 print_binder_node_nilocked(m, node);
5374 binder_node_inner_unlock(node);
5375 last_node = node;
5376 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005377 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005378 binder_inner_proc_unlock(proc);
5379 if (last_node)
5380 binder_put_node(last_node);
5381
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005382 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005383 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005384 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005385 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005386 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005387 print_binder_ref_olocked(m, rb_entry(n,
5388 struct binder_ref,
5389 rb_node_desc));
5390 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005391 }
Todd Kjosd325d372016-10-10 10:40:53 -07005392 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005393 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005394 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005395 print_binder_work_ilocked(m, proc, " ",
5396 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005397 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005398 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005399 break;
5400 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005401 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005402 if (!print_all && m->count == header_pos)
5403 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005404}
5405
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005406static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005407 "BR_ERROR",
5408 "BR_OK",
5409 "BR_TRANSACTION",
5410 "BR_REPLY",
5411 "BR_ACQUIRE_RESULT",
5412 "BR_DEAD_REPLY",
5413 "BR_TRANSACTION_COMPLETE",
5414 "BR_INCREFS",
5415 "BR_ACQUIRE",
5416 "BR_RELEASE",
5417 "BR_DECREFS",
5418 "BR_ATTEMPT_ACQUIRE",
5419 "BR_NOOP",
5420 "BR_SPAWN_LOOPER",
5421 "BR_FINISHED",
5422 "BR_DEAD_BINDER",
5423 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5424 "BR_FAILED_REPLY"
5425};
5426
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005427static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005428 "BC_TRANSACTION",
5429 "BC_REPLY",
5430 "BC_ACQUIRE_RESULT",
5431 "BC_FREE_BUFFER",
5432 "BC_INCREFS",
5433 "BC_ACQUIRE",
5434 "BC_RELEASE",
5435 "BC_DECREFS",
5436 "BC_INCREFS_DONE",
5437 "BC_ACQUIRE_DONE",
5438 "BC_ATTEMPT_ACQUIRE",
5439 "BC_REGISTER_LOOPER",
5440 "BC_ENTER_LOOPER",
5441 "BC_EXIT_LOOPER",
5442 "BC_REQUEST_DEATH_NOTIFICATION",
5443 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005444 "BC_DEAD_BINDER_DONE",
5445 "BC_TRANSACTION_SG",
5446 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005447};
5448
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005449static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005450 "proc",
5451 "thread",
5452 "node",
5453 "ref",
5454 "death",
5455 "transaction",
5456 "transaction_complete"
5457};
5458
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005459static void print_binder_stats(struct seq_file *m, const char *prefix,
5460 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005461{
5462 int i;
5463
5464 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005465 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005466 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005467 int temp = atomic_read(&stats->bc[i]);
5468
5469 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005470 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005471 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005472 }
5473
5474 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005475 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005476 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005477 int temp = atomic_read(&stats->br[i]);
5478
5479 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005480 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005481 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005482 }
5483
5484 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005485 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005486 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005487 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005488 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005489 int created = atomic_read(&stats->obj_created[i]);
5490 int deleted = atomic_read(&stats->obj_deleted[i]);
5491
5492 if (created || deleted)
5493 seq_printf(m, "%s%s: active %d total %d\n",
5494 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005495 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005496 created - deleted,
5497 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005498 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005499}
5500
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005501static void print_binder_proc_stats(struct seq_file *m,
5502 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005503{
5504 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005505 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005506 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005507 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005508 size_t free_async_space =
5509 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005510
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005511 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005512 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005513 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005514 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005515 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005516 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5517 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005518
5519 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5520 ready_threads++;
5521
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005522 seq_printf(m, " threads: %d\n", count);
5523 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005524 " ready threads %d\n"
5525 " free async space %zd\n", proc->requested_threads,
5526 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005527 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005528 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005529 count = 0;
5530 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5531 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005532 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005533 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005534 count = 0;
5535 strong = 0;
5536 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005537 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005538 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5539 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5540 rb_node_desc);
5541 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005542 strong += ref->data.strong;
5543 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005544 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005545 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005546 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005547
Todd Kjosd325d372016-10-10 10:40:53 -07005548 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005549 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005550
Sherry Yang91004422017-08-22 17:26:57 -07005551 binder_alloc_print_pages(m, &proc->alloc);
5552
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005553 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005554 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005555 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005556 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005557 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005558 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005559 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005560 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005561
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005562 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005563}
5564
5565
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005566static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005567{
5568 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005569 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005570 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005571
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005572 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005573
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005574 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005575 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005576 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005577 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5578 /*
5579 * take a temporary reference on the node so it
5580 * survives and isn't removed from the list
5581 * while we print it.
5582 */
5583 node->tmp_refs++;
5584 spin_unlock(&binder_dead_nodes_lock);
5585 if (last_node)
5586 binder_put_node(last_node);
5587 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005588 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005589 binder_node_unlock(node);
5590 last_node = node;
5591 spin_lock(&binder_dead_nodes_lock);
5592 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005593 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005594 if (last_node)
5595 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005596
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005597 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005598 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005599 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005600 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005601
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005602 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005603}
5604
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005605static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005606{
5607 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005608
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005609 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005610
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005611 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005612
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005613 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005614 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005615 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005616 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005617
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005618 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005619}
5620
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005621static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005622{
5623 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005624
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005625 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005626 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005627 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005628 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005629 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005630
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005631 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005632}
5633
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005634static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005635{
Riley Andrews83050a42016-02-09 21:05:33 -08005636 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005637 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005638
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005639 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005640 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005641 if (itr->pid == pid) {
5642 seq_puts(m, "binder proc state:\n");
5643 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005644 }
5645 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005646 mutex_unlock(&binder_procs_lock);
5647
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005648 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005649}
5650
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005651static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005652 struct binder_transaction_log_entry *e)
5653{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005654 int debug_id = READ_ONCE(e->debug_id_done);
5655 /*
5656 * read barrier to guarantee debug_id_done read before
5657 * we print the log values
5658 */
5659 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005660 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005661 "%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 -07005662 e->debug_id, (e->call_type == 2) ? "reply" :
5663 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005664 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005665 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5666 e->return_error, e->return_error_param,
5667 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005668 /*
5669 * read-barrier to guarantee read of debug_id_done after
5670 * done printing the fields of the entry
5671 */
5672 smp_rmb();
5673 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5674 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005675}
5676
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005677static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005678{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005679 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005680 unsigned int log_cur = atomic_read(&log->cur);
5681 unsigned int count;
5682 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005683 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005684
Todd Kjos1cfe6272017-05-24 13:33:28 -07005685 count = log_cur + 1;
5686 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5687 0 : count % ARRAY_SIZE(log->entry);
5688 if (count > ARRAY_SIZE(log->entry) || log->full)
5689 count = ARRAY_SIZE(log->entry);
5690 for (i = 0; i < count; i++) {
5691 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5692
5693 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005694 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005695 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005696}
5697
5698static const struct file_operations binder_fops = {
5699 .owner = THIS_MODULE,
5700 .poll = binder_poll,
5701 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005702 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005703 .mmap = binder_mmap,
5704 .open = binder_open,
5705 .flush = binder_flush,
5706 .release = binder_release,
5707};
5708
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005709BINDER_DEBUG_ENTRY(state);
5710BINDER_DEBUG_ENTRY(stats);
5711BINDER_DEBUG_ENTRY(transactions);
5712BINDER_DEBUG_ENTRY(transaction_log);
5713
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005714static int __init init_binder_device(const char *name)
5715{
5716 int ret;
5717 struct binder_device *binder_device;
5718
5719 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5720 if (!binder_device)
5721 return -ENOMEM;
5722
5723 binder_device->miscdev.fops = &binder_fops;
5724 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5725 binder_device->miscdev.name = name;
5726
5727 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5728 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005729 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005730
5731 ret = misc_register(&binder_device->miscdev);
5732 if (ret < 0) {
5733 kfree(binder_device);
5734 return ret;
5735 }
5736
5737 hlist_add_head(&binder_device->hlist, &binder_devices);
5738
5739 return ret;
5740}
5741
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005742static int __init binder_init(void)
5743{
5744 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005745 char *device_name, *device_names;
5746 struct binder_device *device;
5747 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005748
Sherry Yang5828d702017-07-29 13:24:11 -07005749 binder_alloc_shrinker_init();
5750
Todd Kjos1cfe6272017-05-24 13:33:28 -07005751 atomic_set(&binder_transaction_log.cur, ~0U);
5752 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5753
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005754 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5755 if (binder_debugfs_dir_entry_root)
5756 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5757 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005758
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005759 if (binder_debugfs_dir_entry_root) {
5760 debugfs_create_file("state",
5761 S_IRUGO,
5762 binder_debugfs_dir_entry_root,
5763 NULL,
5764 &binder_state_fops);
5765 debugfs_create_file("stats",
5766 S_IRUGO,
5767 binder_debugfs_dir_entry_root,
5768 NULL,
5769 &binder_stats_fops);
5770 debugfs_create_file("transactions",
5771 S_IRUGO,
5772 binder_debugfs_dir_entry_root,
5773 NULL,
5774 &binder_transactions_fops);
5775 debugfs_create_file("transaction_log",
5776 S_IRUGO,
5777 binder_debugfs_dir_entry_root,
5778 &binder_transaction_log,
5779 &binder_transaction_log_fops);
5780 debugfs_create_file("failed_transaction_log",
5781 S_IRUGO,
5782 binder_debugfs_dir_entry_root,
5783 &binder_transaction_log_failed,
5784 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005785 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005786
5787 /*
5788 * Copy the module_parameter string, because we don't want to
5789 * tokenize it in-place.
5790 */
5791 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5792 if (!device_names) {
5793 ret = -ENOMEM;
5794 goto err_alloc_device_names_failed;
5795 }
5796 strcpy(device_names, binder_devices_param);
5797
5798 while ((device_name = strsep(&device_names, ","))) {
5799 ret = init_binder_device(device_name);
5800 if (ret)
5801 goto err_init_binder_device_failed;
5802 }
5803
5804 return ret;
5805
5806err_init_binder_device_failed:
5807 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5808 misc_deregister(&device->miscdev);
5809 hlist_del(&device->hlist);
5810 kfree(device);
5811 }
5812err_alloc_device_names_failed:
5813 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5814
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005815 return ret;
5816}
5817
5818device_initcall(binder_init);
5819
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005820#define CREATE_TRACE_POINTS
5821#include "binder_trace.h"
5822
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005823MODULE_LICENSE("GPL v2");