blob: ff367b8faece821cf3db8db896769220dd6549cf [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 Kjosf09daf12017-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 * @stats: per-process binder statistics
516 * (atomics, no lock needed)
517 * @delivered_death: list of delivered death notification
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700518 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700519 * @max_threads: cap on number of binder threads
Todd Kjosd600e902017-05-25 17:35:02 -0700520 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700521 * @requested_threads: number of binder threads requested but not
522 * yet started. In current implementation, can
523 * only be 0 or 1.
Todd Kjosd600e902017-05-25 17:35:02 -0700524 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700525 * @requested_threads_started: number binder threads started
Todd Kjosd600e902017-05-25 17:35:02 -0700526 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700527 * @tmp_ref: temporary reference to indicate proc is in use
Todd Kjosb4827902017-05-25 15:52:17 -0700528 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700529 * @default_priority: default scheduler priority
530 * (invariant after initialized)
531 * @debugfs_entry: debugfs node
532 * @alloc: binder allocator bookkeeping
533 * @context: binder_context for this proc
534 * (invariant after initialized)
535 * @inner_lock: can nest under outer_lock and/or node lock
536 * @outer_lock: no nesting under innor or node lock
537 * Lock order: 1) outer, 2) node, 3) inner
538 *
539 * Bookkeeping structure for binder processes
540 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900541struct binder_proc {
542 struct hlist_node proc_node;
543 struct rb_root threads;
544 struct rb_root nodes;
545 struct rb_root refs_by_desc;
546 struct rb_root refs_by_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700547 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900548 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900549 struct task_struct *tsk;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900550 struct hlist_node deferred_work_node;
551 int deferred_work;
Todd Kjos2f993e22017-05-12 14:42:55 -0700552 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900553
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900554 struct list_head todo;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900555 struct binder_stats stats;
556 struct list_head delivered_death;
557 int max_threads;
558 int requested_threads;
559 int requested_threads_started;
Todd Kjos2f993e22017-05-12 14:42:55 -0700560 int tmp_ref;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700561 struct binder_priority default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700562 struct dentry *debugfs_entry;
Todd Kjosf85d2292016-10-10 10:39:59 -0700563 struct binder_alloc alloc;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200564 struct binder_context *context;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700565 spinlock_t inner_lock;
566 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900567};
568
569enum {
570 BINDER_LOOPER_STATE_REGISTERED = 0x01,
571 BINDER_LOOPER_STATE_ENTERED = 0x02,
572 BINDER_LOOPER_STATE_EXITED = 0x04,
573 BINDER_LOOPER_STATE_INVALID = 0x08,
574 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700575 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900576};
577
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700578/**
579 * struct binder_thread - binder thread bookkeeping
580 * @proc: binder process for this thread
581 * (invariant after initialization)
582 * @rb_node: element for proc->threads rbtree
Todd Kjosb4827902017-05-25 15:52:17 -0700583 * (protected by @proc->inner_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700584 * @waiting_thread_node: element for @proc->waiting_threads list
585 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700586 * @pid: PID for this thread
587 * (invariant after initialization)
588 * @looper: bitmap of looping state
589 * (only accessed by this thread)
590 * @looper_needs_return: looping thread needs to exit driver
591 * (no lock needed)
592 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700593 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700594 * @todo: list of work to do for this thread
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700595 * (protected by @proc->inner_lock)
Martijn Coenen1af61802017-10-19 15:04:46 +0200596 * @process_todo: whether work in @todo should be processed
597 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700598 * @return_error: transaction errors reported by this thread
599 * (only accessed by this thread)
600 * @reply_error: transaction errors reported by target thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700601 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700602 * @wait: wait queue for thread work
603 * @stats: per-thread statistics
604 * (atomics, no lock needed)
605 * @tmp_ref: temporary reference to indicate thread is in use
606 * (atomic since @proc->inner_lock cannot
607 * always be acquired)
608 * @is_dead: thread is dead and awaiting free
609 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700610 * (protected by @proc->inner_lock)
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700611 * @task: struct task_struct for this thread
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700612 *
613 * Bookkeeping structure for binder threads.
614 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900615struct binder_thread {
616 struct binder_proc *proc;
617 struct rb_node rb_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700618 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900619 int pid;
Todd Kjos6798e6d2017-01-06 14:19:25 -0800620 int looper; /* only modified by this thread */
621 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900622 struct binder_transaction *transaction_stack;
623 struct list_head todo;
Martijn Coenen1af61802017-10-19 15:04:46 +0200624 bool process_todo;
Todd Kjos858b8da2017-04-21 17:35:12 -0700625 struct binder_error return_error;
626 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900627 wait_queue_head_t wait;
628 struct binder_stats stats;
Todd Kjos2f993e22017-05-12 14:42:55 -0700629 atomic_t tmp_ref;
630 bool is_dead;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700631 struct task_struct *task;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900632};
633
634struct binder_transaction {
635 int debug_id;
636 struct binder_work work;
637 struct binder_thread *from;
638 struct binder_transaction *from_parent;
639 struct binder_proc *to_proc;
640 struct binder_thread *to_thread;
641 struct binder_transaction *to_parent;
642 unsigned need_reply:1;
643 /* unsigned is_dead:1; */ /* not used at the moment */
644
645 struct binder_buffer *buffer;
646 unsigned int code;
647 unsigned int flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700648 struct binder_priority priority;
649 struct binder_priority saved_priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700650 bool set_priority_called;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600651 kuid_t sender_euid;
Todd Kjos2f993e22017-05-12 14:42:55 -0700652 /**
653 * @lock: protects @from, @to_proc, and @to_thread
654 *
655 * @from, @to_proc, and @to_thread can be set to NULL
656 * during thread teardown
657 */
658 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900659};
660
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700661/**
662 * binder_proc_lock() - Acquire outer lock for given binder_proc
663 * @proc: struct binder_proc to acquire
664 *
665 * Acquires proc->outer_lock. Used to protect binder_ref
666 * structures associated with the given proc.
667 */
668#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
669static void
670_binder_proc_lock(struct binder_proc *proc, int line)
671{
672 binder_debug(BINDER_DEBUG_SPINLOCKS,
673 "%s: line=%d\n", __func__, line);
674 spin_lock(&proc->outer_lock);
675}
676
677/**
678 * binder_proc_unlock() - Release spinlock for given binder_proc
679 * @proc: struct binder_proc to acquire
680 *
681 * Release lock acquired via binder_proc_lock()
682 */
683#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
684static void
685_binder_proc_unlock(struct binder_proc *proc, int line)
686{
687 binder_debug(BINDER_DEBUG_SPINLOCKS,
688 "%s: line=%d\n", __func__, line);
689 spin_unlock(&proc->outer_lock);
690}
691
692/**
693 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
694 * @proc: struct binder_proc to acquire
695 *
696 * Acquires proc->inner_lock. Used to protect todo lists
697 */
698#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
699static void
700_binder_inner_proc_lock(struct binder_proc *proc, int line)
701{
702 binder_debug(BINDER_DEBUG_SPINLOCKS,
703 "%s: line=%d\n", __func__, line);
704 spin_lock(&proc->inner_lock);
705}
706
707/**
708 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
709 * @proc: struct binder_proc to acquire
710 *
711 * Release lock acquired via binder_inner_proc_lock()
712 */
713#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
714static void
715_binder_inner_proc_unlock(struct binder_proc *proc, int line)
716{
717 binder_debug(BINDER_DEBUG_SPINLOCKS,
718 "%s: line=%d\n", __func__, line);
719 spin_unlock(&proc->inner_lock);
720}
721
722/**
723 * binder_node_lock() - Acquire spinlock for given binder_node
724 * @node: struct binder_node to acquire
725 *
726 * Acquires node->lock. Used to protect binder_node fields
727 */
728#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
729static void
730_binder_node_lock(struct binder_node *node, int line)
731{
732 binder_debug(BINDER_DEBUG_SPINLOCKS,
733 "%s: line=%d\n", __func__, line);
734 spin_lock(&node->lock);
735}
736
737/**
738 * binder_node_unlock() - Release spinlock for given binder_proc
739 * @node: struct binder_node to acquire
740 *
741 * Release lock acquired via binder_node_lock()
742 */
743#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
744static void
745_binder_node_unlock(struct binder_node *node, int line)
746{
747 binder_debug(BINDER_DEBUG_SPINLOCKS,
748 "%s: line=%d\n", __func__, line);
749 spin_unlock(&node->lock);
750}
751
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700752/**
753 * binder_node_inner_lock() - Acquire node and inner locks
754 * @node: struct binder_node to acquire
755 *
756 * Acquires node->lock. If node->proc also acquires
757 * proc->inner_lock. Used to protect binder_node fields
758 */
759#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
760static void
761_binder_node_inner_lock(struct binder_node *node, int line)
762{
763 binder_debug(BINDER_DEBUG_SPINLOCKS,
764 "%s: line=%d\n", __func__, line);
765 spin_lock(&node->lock);
766 if (node->proc)
767 binder_inner_proc_lock(node->proc);
768}
769
770/**
771 * binder_node_unlock() - Release node and inner locks
772 * @node: struct binder_node to acquire
773 *
774 * Release lock acquired via binder_node_lock()
775 */
776#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
777static void
778_binder_node_inner_unlock(struct binder_node *node, int line)
779{
780 struct binder_proc *proc = node->proc;
781
782 binder_debug(BINDER_DEBUG_SPINLOCKS,
783 "%s: line=%d\n", __func__, line);
784 if (proc)
785 binder_inner_proc_unlock(proc);
786 spin_unlock(&node->lock);
787}
788
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700789static bool binder_worklist_empty_ilocked(struct list_head *list)
790{
791 return list_empty(list);
792}
793
794/**
795 * binder_worklist_empty() - Check if no items on the work list
796 * @proc: binder_proc associated with list
797 * @list: list to check
798 *
799 * Return: true if there are no items on list, else false
800 */
801static bool binder_worklist_empty(struct binder_proc *proc,
802 struct list_head *list)
803{
804 bool ret;
805
806 binder_inner_proc_lock(proc);
807 ret = binder_worklist_empty_ilocked(list);
808 binder_inner_proc_unlock(proc);
809 return ret;
810}
811
Martijn Coenen1af61802017-10-19 15:04:46 +0200812/**
813 * binder_enqueue_work_ilocked() - Add an item to the work list
814 * @work: struct binder_work to add to list
815 * @target_list: list to add work to
816 *
817 * Adds the work to the specified list. Asserts that work
818 * is not already on a list.
819 *
820 * Requires the proc->inner_lock to be held.
821 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700822static void
823binder_enqueue_work_ilocked(struct binder_work *work,
824 struct list_head *target_list)
825{
826 BUG_ON(target_list == NULL);
827 BUG_ON(work->entry.next && !list_empty(&work->entry));
828 list_add_tail(&work->entry, target_list);
829}
830
831/**
Martijn Coenendac2e9c2017-11-13 09:55:21 +0100832 * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work
Martijn Coenen1af61802017-10-19 15:04:46 +0200833 * @thread: thread to queue work to
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700834 * @work: struct binder_work to add to list
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700835 *
Martijn Coenen1af61802017-10-19 15:04:46 +0200836 * Adds the work to the todo list of the thread. Doesn't set the process_todo
837 * flag, which means that (if it wasn't already set) the thread will go to
838 * sleep without handling this work when it calls read.
839 *
840 * Requires the proc->inner_lock to be held.
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700841 */
842static void
Martijn Coenendac2e9c2017-11-13 09:55:21 +0100843binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread,
844 struct binder_work *work)
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700845{
Martijn Coenen1af61802017-10-19 15:04:46 +0200846 binder_enqueue_work_ilocked(work, &thread->todo);
847}
848
849/**
850 * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list
851 * @thread: thread to queue work to
852 * @work: struct binder_work to add to list
853 *
854 * Adds the work to the todo list of the thread, and enables processing
855 * of the todo queue.
856 *
857 * Requires the proc->inner_lock to be held.
858 */
859static void
860binder_enqueue_thread_work_ilocked(struct binder_thread *thread,
861 struct binder_work *work)
862{
863 binder_enqueue_work_ilocked(work, &thread->todo);
864 thread->process_todo = true;
865}
866
867/**
868 * binder_enqueue_thread_work() - Add an item to the thread work list
869 * @thread: thread to queue work to
870 * @work: struct binder_work to add to list
871 *
872 * Adds the work to the todo list of the thread, and enables processing
873 * of the todo queue.
874 */
875static void
876binder_enqueue_thread_work(struct binder_thread *thread,
877 struct binder_work *work)
878{
879 binder_inner_proc_lock(thread->proc);
880 binder_enqueue_thread_work_ilocked(thread, work);
881 binder_inner_proc_unlock(thread->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700882}
883
884static void
885binder_dequeue_work_ilocked(struct binder_work *work)
886{
887 list_del_init(&work->entry);
888}
889
890/**
891 * binder_dequeue_work() - Removes an item from the work list
892 * @proc: binder_proc associated with list
893 * @work: struct binder_work to remove from list
894 *
895 * Removes the specified work item from whatever list it is on.
896 * Can safely be called if work is not on any list.
897 */
898static void
899binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
900{
901 binder_inner_proc_lock(proc);
902 binder_dequeue_work_ilocked(work);
903 binder_inner_proc_unlock(proc);
904}
905
906static struct binder_work *binder_dequeue_work_head_ilocked(
907 struct list_head *list)
908{
909 struct binder_work *w;
910
911 w = list_first_entry_or_null(list, struct binder_work, entry);
912 if (w)
913 list_del_init(&w->entry);
914 return w;
915}
916
917/**
918 * binder_dequeue_work_head() - Dequeues the item at head of list
919 * @proc: binder_proc associated with list
920 * @list: list to dequeue head
921 *
922 * Removes the head of the list if there are items on the list
923 *
924 * Return: pointer dequeued binder_work, NULL if list was empty
925 */
926static struct binder_work *binder_dequeue_work_head(
927 struct binder_proc *proc,
928 struct list_head *list)
929{
930 struct binder_work *w;
931
932 binder_inner_proc_lock(proc);
933 w = binder_dequeue_work_head_ilocked(list);
934 binder_inner_proc_unlock(proc);
935 return w;
936}
937
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900938static void
939binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos2f993e22017-05-12 14:42:55 -0700940static void binder_free_thread(struct binder_thread *thread);
941static void binder_free_proc(struct binder_proc *proc);
Todd Kjos425d23f2017-06-12 12:07:26 -0700942static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900943
Todd Kjosf09daf12017-11-10 15:30:27 -0800944struct files_struct *binder_get_files_struct(struct binder_proc *proc)
945{
946 return get_files_struct(proc->tsk);
947}
948
Sachin Kamatefde99c2012-08-17 16:39:36 +0530949static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900950{
Todd Kjosf09daf12017-11-10 15:30:27 -0800951 struct files_struct *files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900952 unsigned long rlim_cur;
953 unsigned long irqs;
Todd Kjosf09daf12017-11-10 15:30:27 -0800954 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900955
Todd Kjosf09daf12017-11-10 15:30:27 -0800956 files = binder_get_files_struct(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900957 if (files == NULL)
958 return -ESRCH;
959
Todd Kjosf09daf12017-11-10 15:30:27 -0800960 if (!lock_task_sighand(proc->tsk, &irqs)) {
961 ret = -EMFILE;
962 goto err;
963 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900964
Al Virodcfadfa2012-08-12 17:27:30 -0400965 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
966 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900967
Todd Kjosf09daf12017-11-10 15:30:27 -0800968 ret = __alloc_fd(files, 0, rlim_cur, flags);
969err:
970 put_files_struct(files);
971 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900972}
973
974/*
975 * copied from fd_install
976 */
977static void task_fd_install(
978 struct binder_proc *proc, unsigned int fd, struct file *file)
979{
Todd Kjosf09daf12017-11-10 15:30:27 -0800980 struct files_struct *files = binder_get_files_struct(proc);
981
982 if (files) {
983 __fd_install(files, fd, file);
984 put_files_struct(files);
985 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900986}
987
988/*
989 * copied from sys_close
990 */
991static long task_close_fd(struct binder_proc *proc, unsigned int fd)
992{
Todd Kjosf09daf12017-11-10 15:30:27 -0800993 struct files_struct *files = binder_get_files_struct(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900994 int retval;
995
Todd Kjosf09daf12017-11-10 15:30:27 -0800996 if (files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900997 return -ESRCH;
998
Todd Kjosf09daf12017-11-10 15:30:27 -0800999 retval = __close_fd(files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001000 /* can't restart close syscall because file table entry was cleared */
1001 if (unlikely(retval == -ERESTARTSYS ||
1002 retval == -ERESTARTNOINTR ||
1003 retval == -ERESTARTNOHAND ||
1004 retval == -ERESTART_RESTARTBLOCK))
1005 retval = -EINTR;
Todd Kjosf09daf12017-11-10 15:30:27 -08001006 put_files_struct(files);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001007
1008 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001009}
1010
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001011static bool binder_has_work_ilocked(struct binder_thread *thread,
1012 bool do_proc_work)
1013{
Martijn Coenen1af61802017-10-19 15:04:46 +02001014 return thread->process_todo ||
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001015 thread->looper_need_return ||
1016 (do_proc_work &&
1017 !binder_worklist_empty_ilocked(&thread->proc->todo));
1018}
1019
1020static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
1021{
1022 bool has_work;
1023
1024 binder_inner_proc_lock(thread->proc);
1025 has_work = binder_has_work_ilocked(thread, do_proc_work);
1026 binder_inner_proc_unlock(thread->proc);
1027
1028 return has_work;
1029}
1030
1031static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
1032{
1033 return !thread->transaction_stack &&
1034 binder_worklist_empty_ilocked(&thread->todo) &&
1035 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
1036 BINDER_LOOPER_STATE_REGISTERED));
1037}
1038
1039static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
1040 bool sync)
1041{
1042 struct rb_node *n;
1043 struct binder_thread *thread;
1044
1045 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
1046 thread = rb_entry(n, struct binder_thread, rb_node);
1047 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
1048 binder_available_for_proc_work_ilocked(thread)) {
1049 if (sync)
1050 wake_up_interruptible_sync(&thread->wait);
1051 else
1052 wake_up_interruptible(&thread->wait);
1053 }
1054 }
1055}
1056
Martijn Coenen053be422017-06-06 15:17:46 -07001057/**
1058 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1059 * @proc: process to select a thread from
1060 *
1061 * Note that calling this function moves the thread off the waiting_threads
1062 * list, so it can only be woken up by the caller of this function, or a
1063 * signal. Therefore, callers *should* always wake up the thread this function
1064 * returns.
1065 *
1066 * Return: If there's a thread currently waiting for process work,
1067 * returns that thread. Otherwise returns NULL.
1068 */
1069static struct binder_thread *
1070binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001071{
1072 struct binder_thread *thread;
1073
Martijn Coenened323352017-07-27 23:52:24 +02001074 assert_spin_locked(&proc->inner_lock);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001075 thread = list_first_entry_or_null(&proc->waiting_threads,
1076 struct binder_thread,
1077 waiting_thread_node);
1078
Martijn Coenen053be422017-06-06 15:17:46 -07001079 if (thread)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001080 list_del_init(&thread->waiting_thread_node);
Martijn Coenen053be422017-06-06 15:17:46 -07001081
1082 return thread;
1083}
1084
1085/**
1086 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1087 * @proc: process to wake up a thread in
1088 * @thread: specific thread to wake-up (may be NULL)
1089 * @sync: whether to do a synchronous wake-up
1090 *
1091 * This function wakes up a thread in the @proc process.
1092 * The caller may provide a specific thread to wake-up in
1093 * the @thread parameter. If @thread is NULL, this function
1094 * will wake up threads that have called poll().
1095 *
1096 * Note that for this function to work as expected, callers
1097 * should first call binder_select_thread() to find a thread
1098 * to handle the work (if they don't have a thread already),
1099 * and pass the result into the @thread parameter.
1100 */
1101static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1102 struct binder_thread *thread,
1103 bool sync)
1104{
Martijn Coenened323352017-07-27 23:52:24 +02001105 assert_spin_locked(&proc->inner_lock);
Martijn Coenen053be422017-06-06 15:17:46 -07001106
1107 if (thread) {
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001108 if (sync)
1109 wake_up_interruptible_sync(&thread->wait);
1110 else
1111 wake_up_interruptible(&thread->wait);
1112 return;
1113 }
1114
1115 /* Didn't find a thread waiting for proc work; this can happen
1116 * in two scenarios:
1117 * 1. All threads are busy handling transactions
1118 * In that case, one of those threads should call back into
1119 * the kernel driver soon and pick up this work.
1120 * 2. Threads are using the (e)poll interface, in which case
1121 * they may be blocked on the waitqueue without having been
1122 * added to waiting_threads. For this case, we just iterate
1123 * over all threads not handling transaction work, and
1124 * wake them all up. We wake all because we don't know whether
1125 * a thread that called into (e)poll is handling non-binder
1126 * work currently.
1127 */
1128 binder_wakeup_poll_threads_ilocked(proc, sync);
1129}
1130
Martijn Coenen053be422017-06-06 15:17:46 -07001131static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1132{
1133 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1134
1135 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1136}
1137
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001138static bool is_rt_policy(int policy)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001139{
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001140 return policy == SCHED_FIFO || policy == SCHED_RR;
1141}
Seunghun Lee10f62862014-05-01 01:30:23 +09001142
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001143static bool is_fair_policy(int policy)
1144{
1145 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
1146}
1147
1148static bool binder_supported_policy(int policy)
1149{
1150 return is_fair_policy(policy) || is_rt_policy(policy);
1151}
1152
1153static int to_userspace_prio(int policy, int kernel_priority)
1154{
1155 if (is_fair_policy(policy))
1156 return PRIO_TO_NICE(kernel_priority);
1157 else
1158 return MAX_USER_RT_PRIO - 1 - kernel_priority;
1159}
1160
1161static int to_kernel_prio(int policy, int user_priority)
1162{
1163 if (is_fair_policy(policy))
1164 return NICE_TO_PRIO(user_priority);
1165 else
1166 return MAX_USER_RT_PRIO - 1 - user_priority;
1167}
1168
Martijn Coenenecd972d2017-05-26 10:48:56 -07001169static void binder_do_set_priority(struct task_struct *task,
1170 struct binder_priority desired,
1171 bool verify)
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001172{
1173 int priority; /* user-space prio value */
1174 bool has_cap_nice;
1175 unsigned int policy = desired.sched_policy;
1176
1177 if (task->policy == policy && task->normal_prio == desired.prio)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001178 return;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001179
1180 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1181
1182 priority = to_userspace_prio(policy, desired.prio);
1183
Martijn Coenenecd972d2017-05-26 10:48:56 -07001184 if (verify && is_rt_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001185 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1186
1187 if (max_rtprio == 0) {
1188 policy = SCHED_NORMAL;
1189 priority = MIN_NICE;
1190 } else if (priority > max_rtprio) {
1191 priority = max_rtprio;
1192 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001193 }
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001194
Martijn Coenenecd972d2017-05-26 10:48:56 -07001195 if (verify && is_fair_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001196 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1197
1198 if (min_nice > MAX_NICE) {
1199 binder_user_error("%d RLIMIT_NICE not set\n",
1200 task->pid);
1201 return;
1202 } else if (priority < min_nice) {
1203 priority = min_nice;
1204 }
1205 }
1206
1207 if (policy != desired.sched_policy ||
1208 to_kernel_prio(policy, priority) != desired.prio)
1209 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1210 "%d: priority %d not allowed, using %d instead\n",
1211 task->pid, desired.prio,
1212 to_kernel_prio(policy, priority));
1213
Martijn Coenen81402ea2017-05-08 09:33:22 -07001214 trace_binder_set_priority(task->tgid, task->pid, task->normal_prio,
1215 to_kernel_prio(policy, priority),
1216 desired.prio);
1217
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001218 /* Set the actual priority */
1219 if (task->policy != policy || is_rt_policy(policy)) {
1220 struct sched_param params;
1221
1222 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1223
1224 sched_setscheduler_nocheck(task,
1225 policy | SCHED_RESET_ON_FORK,
1226 &params);
1227 }
1228 if (is_fair_policy(policy))
1229 set_user_nice(task, priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001230}
1231
Martijn Coenenecd972d2017-05-26 10:48:56 -07001232static void binder_set_priority(struct task_struct *task,
1233 struct binder_priority desired)
1234{
1235 binder_do_set_priority(task, desired, /* verify = */ true);
1236}
1237
1238static void binder_restore_priority(struct task_struct *task,
1239 struct binder_priority desired)
1240{
1241 binder_do_set_priority(task, desired, /* verify = */ false);
1242}
1243
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001244static void binder_transaction_priority(struct task_struct *task,
1245 struct binder_transaction *t,
Martijn Coenenc46810c2017-06-23 10:13:43 -07001246 struct binder_priority node_prio,
1247 bool inherit_rt)
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001248{
Ganesh Mahendran9add7c42017-09-27 15:12:25 +08001249 struct binder_priority desired_prio = t->priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001250
1251 if (t->set_priority_called)
1252 return;
1253
1254 t->set_priority_called = true;
1255 t->saved_priority.sched_policy = task->policy;
1256 t->saved_priority.prio = task->normal_prio;
1257
Martijn Coenenc46810c2017-06-23 10:13:43 -07001258 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1259 desired_prio.prio = NICE_TO_PRIO(0);
1260 desired_prio.sched_policy = SCHED_NORMAL;
Martijn Coenenc46810c2017-06-23 10:13:43 -07001261 }
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001262
1263 if (node_prio.prio < t->priority.prio ||
1264 (node_prio.prio == t->priority.prio &&
1265 node_prio.sched_policy == SCHED_FIFO)) {
1266 /*
1267 * In case the minimum priority on the node is
1268 * higher (lower value), use that priority. If
1269 * the priority is the same, but the node uses
1270 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1271 * run unbounded, unlike SCHED_RR.
1272 */
1273 desired_prio = node_prio;
1274 }
1275
1276 binder_set_priority(task, desired_prio);
1277}
1278
Todd Kjos425d23f2017-06-12 12:07:26 -07001279static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1280 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001281{
1282 struct rb_node *n = proc->nodes.rb_node;
1283 struct binder_node *node;
1284
Martijn Coenened323352017-07-27 23:52:24 +02001285 assert_spin_locked(&proc->inner_lock);
Todd Kjos425d23f2017-06-12 12:07:26 -07001286
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001287 while (n) {
1288 node = rb_entry(n, struct binder_node, rb_node);
1289
1290 if (ptr < node->ptr)
1291 n = n->rb_left;
1292 else if (ptr > node->ptr)
1293 n = n->rb_right;
Todd Kjosf22abc72017-05-09 11:08:05 -07001294 else {
1295 /*
1296 * take an implicit weak reference
1297 * to ensure node stays alive until
1298 * call to binder_put_node()
1299 */
Todd Kjos425d23f2017-06-12 12:07:26 -07001300 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001301 return node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001302 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001303 }
1304 return NULL;
1305}
1306
Todd Kjos425d23f2017-06-12 12:07:26 -07001307static struct binder_node *binder_get_node(struct binder_proc *proc,
1308 binder_uintptr_t ptr)
1309{
1310 struct binder_node *node;
1311
1312 binder_inner_proc_lock(proc);
1313 node = binder_get_node_ilocked(proc, ptr);
1314 binder_inner_proc_unlock(proc);
1315 return node;
1316}
1317
1318static struct binder_node *binder_init_node_ilocked(
1319 struct binder_proc *proc,
1320 struct binder_node *new_node,
1321 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001322{
1323 struct rb_node **p = &proc->nodes.rb_node;
1324 struct rb_node *parent = NULL;
1325 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001326 binder_uintptr_t ptr = fp ? fp->binder : 0;
1327 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1328 __u32 flags = fp ? fp->flags : 0;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001329 s8 priority;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001330
Martijn Coenened323352017-07-27 23:52:24 +02001331 assert_spin_locked(&proc->inner_lock);
1332
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001333 while (*p) {
Todd Kjos425d23f2017-06-12 12:07:26 -07001334
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001335 parent = *p;
1336 node = rb_entry(parent, struct binder_node, rb_node);
1337
1338 if (ptr < node->ptr)
1339 p = &(*p)->rb_left;
1340 else if (ptr > node->ptr)
1341 p = &(*p)->rb_right;
Todd Kjos425d23f2017-06-12 12:07:26 -07001342 else {
1343 /*
1344 * A matching node is already in
1345 * the rb tree. Abandon the init
1346 * and return it.
1347 */
1348 binder_inc_node_tmpref_ilocked(node);
1349 return node;
1350 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001351 }
Todd Kjos425d23f2017-06-12 12:07:26 -07001352 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001353 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosf22abc72017-05-09 11:08:05 -07001354 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001355 rb_link_node(&node->rb_node, parent, p);
1356 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -07001357 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001358 node->proc = proc;
1359 node->ptr = ptr;
1360 node->cookie = cookie;
1361 node->work.type = BINDER_WORK_NODE;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001362 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
Ganesh Mahendran6cd26312017-09-26 17:56:25 +08001363 node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >>
Martijn Coenen6aac9792017-06-07 09:29:14 -07001364 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1365 node->min_priority = to_kernel_prio(node->sched_policy, priority);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001366 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Martijn Coenenc46810c2017-06-23 10:13:43 -07001367 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
Todd Kjosfc7a7e22017-05-29 16:44:24 -07001368 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001369 INIT_LIST_HEAD(&node->work.entry);
1370 INIT_LIST_HEAD(&node->async_todo);
1371 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001372 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001373 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001374 (u64)node->ptr, (u64)node->cookie);
Todd Kjos425d23f2017-06-12 12:07:26 -07001375
1376 return node;
1377}
1378
1379static struct binder_node *binder_new_node(struct binder_proc *proc,
1380 struct flat_binder_object *fp)
1381{
1382 struct binder_node *node;
1383 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1384
1385 if (!new_node)
1386 return NULL;
1387 binder_inner_proc_lock(proc);
1388 node = binder_init_node_ilocked(proc, new_node, fp);
1389 binder_inner_proc_unlock(proc);
1390 if (node != new_node)
1391 /*
1392 * The node was already added by another thread
1393 */
1394 kfree(new_node);
1395
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001396 return node;
1397}
1398
Todd Kjose7f23ed2017-03-21 13:06:01 -07001399static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001400{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001401 kfree(node);
1402 binder_stats_deleted(BINDER_STAT_NODE);
1403}
1404
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001405static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1406 int internal,
1407 struct list_head *target_list)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001408{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001409 struct binder_proc *proc = node->proc;
1410
Martijn Coenened323352017-07-27 23:52:24 +02001411 assert_spin_locked(&node->lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001412 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001413 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001414 if (strong) {
1415 if (internal) {
1416 if (target_list == NULL &&
1417 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001418 !(node->proc &&
1419 node == node->proc->context->
1420 binder_context_mgr_node &&
1421 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301422 pr_err("invalid inc strong node for %d\n",
1423 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001424 return -EINVAL;
1425 }
1426 node->internal_strong_refs++;
1427 } else
1428 node->local_strong_refs++;
1429 if (!node->has_strong_ref && target_list) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001430 binder_dequeue_work_ilocked(&node->work);
Martijn Coenen1af61802017-10-19 15:04:46 +02001431 /*
1432 * Note: this function is the only place where we queue
1433 * directly to a thread->todo without using the
1434 * corresponding binder_enqueue_thread_work() helper
1435 * functions; in this case it's ok to not set the
1436 * process_todo flag, since we know this node work will
1437 * always be followed by other work that starts queue
1438 * processing: in case of synchronous transactions, a
1439 * BR_REPLY or BR_ERROR; in case of oneway
1440 * transactions, a BR_TRANSACTION_COMPLETE.
1441 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001442 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001443 }
1444 } else {
1445 if (!internal)
1446 node->local_weak_refs++;
1447 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1448 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301449 pr_err("invalid inc weak node for %d\n",
1450 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001451 return -EINVAL;
1452 }
Martijn Coenen1af61802017-10-19 15:04:46 +02001453 /*
1454 * See comment above
1455 */
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001456 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001457 }
1458 }
1459 return 0;
1460}
1461
Todd Kjose7f23ed2017-03-21 13:06:01 -07001462static int binder_inc_node(struct binder_node *node, int strong, int internal,
1463 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001464{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001465 int ret;
1466
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001467 binder_node_inner_lock(node);
1468 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1469 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001470
1471 return ret;
1472}
1473
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001474static bool binder_dec_node_nilocked(struct binder_node *node,
1475 int strong, int internal)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001476{
1477 struct binder_proc *proc = node->proc;
1478
Martijn Coenened323352017-07-27 23:52:24 +02001479 assert_spin_locked(&node->lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001480 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001481 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001482 if (strong) {
1483 if (internal)
1484 node->internal_strong_refs--;
1485 else
1486 node->local_strong_refs--;
1487 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001488 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001489 } else {
1490 if (!internal)
1491 node->local_weak_refs--;
Todd Kjosf22abc72017-05-09 11:08:05 -07001492 if (node->local_weak_refs || node->tmp_refs ||
1493 !hlist_empty(&node->refs))
Todd Kjose7f23ed2017-03-21 13:06:01 -07001494 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001495 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001496
1497 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001498 if (list_empty(&node->work.entry)) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001499 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07001500 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001501 }
1502 } else {
1503 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosf22abc72017-05-09 11:08:05 -07001504 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07001505 if (proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001506 binder_dequeue_work_ilocked(&node->work);
1507 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001508 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301509 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001510 node->debug_id);
1511 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001512 BUG_ON(!list_empty(&node->work.entry));
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001513 spin_lock(&binder_dead_nodes_lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001514 /*
1515 * tmp_refs could have changed so
1516 * check it again
1517 */
1518 if (node->tmp_refs) {
1519 spin_unlock(&binder_dead_nodes_lock);
1520 return false;
1521 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001522 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001523 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001524 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301525 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001526 node->debug_id);
1527 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001528 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001529 }
1530 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001531 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001532}
1533
Todd Kjose7f23ed2017-03-21 13:06:01 -07001534static void binder_dec_node(struct binder_node *node, int strong, int internal)
1535{
1536 bool free_node;
1537
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001538 binder_node_inner_lock(node);
1539 free_node = binder_dec_node_nilocked(node, strong, internal);
1540 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001541 if (free_node)
1542 binder_free_node(node);
1543}
1544
1545static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosf22abc72017-05-09 11:08:05 -07001546{
1547 /*
1548 * No call to binder_inc_node() is needed since we
1549 * don't need to inform userspace of any changes to
1550 * tmp_refs
1551 */
1552 node->tmp_refs++;
1553}
1554
1555/**
Todd Kjose7f23ed2017-03-21 13:06:01 -07001556 * binder_inc_node_tmpref() - take a temporary reference on node
1557 * @node: node to reference
1558 *
1559 * Take reference on node to prevent the node from being freed
1560 * while referenced only by a local variable. The inner lock is
1561 * needed to serialize with the node work on the queue (which
1562 * isn't needed after the node is dead). If the node is dead
1563 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1564 * node->tmp_refs against dead-node-only cases where the node
1565 * lock cannot be acquired (eg traversing the dead node list to
1566 * print nodes)
1567 */
1568static void binder_inc_node_tmpref(struct binder_node *node)
1569{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001570 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001571 if (node->proc)
1572 binder_inner_proc_lock(node->proc);
1573 else
1574 spin_lock(&binder_dead_nodes_lock);
1575 binder_inc_node_tmpref_ilocked(node);
1576 if (node->proc)
1577 binder_inner_proc_unlock(node->proc);
1578 else
1579 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001580 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001581}
1582
1583/**
Todd Kjosf22abc72017-05-09 11:08:05 -07001584 * binder_dec_node_tmpref() - remove a temporary reference on node
1585 * @node: node to reference
1586 *
1587 * Release temporary reference on node taken via binder_inc_node_tmpref()
1588 */
1589static void binder_dec_node_tmpref(struct binder_node *node)
1590{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001591 bool free_node;
1592
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001593 binder_node_inner_lock(node);
1594 if (!node->proc)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001595 spin_lock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001596 node->tmp_refs--;
1597 BUG_ON(node->tmp_refs < 0);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001598 if (!node->proc)
1599 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001600 /*
1601 * Call binder_dec_node() to check if all refcounts are 0
1602 * and cleanup is needed. Calling with strong=0 and internal=1
1603 * causes no actual reference to be released in binder_dec_node().
1604 * If that changes, a change is needed here too.
1605 */
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001606 free_node = binder_dec_node_nilocked(node, 0, 1);
1607 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001608 if (free_node)
1609 binder_free_node(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07001610}
1611
1612static void binder_put_node(struct binder_node *node)
1613{
1614 binder_dec_node_tmpref(node);
1615}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001616
Todd Kjos5346bf32016-10-20 16:43:34 -07001617static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1618 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001619{
1620 struct rb_node *n = proc->refs_by_desc.rb_node;
1621 struct binder_ref *ref;
1622
1623 while (n) {
1624 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1625
Todd Kjosb0117bb2017-05-08 09:16:27 -07001626 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001627 n = n->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001628 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001629 n = n->rb_right;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001630 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001631 binder_user_error("tried to use weak ref as strong ref\n");
1632 return NULL;
1633 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001634 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001635 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001636 }
1637 return NULL;
1638}
1639
Todd Kjosb0117bb2017-05-08 09:16:27 -07001640/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001641 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjosb0117bb2017-05-08 09:16:27 -07001642 * @proc: binder_proc that owns the ref
1643 * @node: binder_node of target
1644 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1645 *
1646 * Look up the ref for the given node and return it if it exists
1647 *
1648 * If it doesn't exist and the caller provides a newly allocated
1649 * ref, initialize the fields of the newly allocated ref and insert
1650 * into the given proc rb_trees and node refs list.
1651 *
1652 * Return: the ref for node. It is possible that another thread
1653 * allocated/initialized the ref first in which case the
1654 * returned ref would be different than the passed-in
1655 * new_ref. new_ref must be kfree'd by the caller in
1656 * this case.
1657 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001658static struct binder_ref *binder_get_ref_for_node_olocked(
1659 struct binder_proc *proc,
1660 struct binder_node *node,
1661 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001662{
Todd Kjosb0117bb2017-05-08 09:16:27 -07001663 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001664 struct rb_node **p = &proc->refs_by_node.rb_node;
1665 struct rb_node *parent = NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001666 struct binder_ref *ref;
1667 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001668
1669 while (*p) {
1670 parent = *p;
1671 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1672
1673 if (node < ref->node)
1674 p = &(*p)->rb_left;
1675 else if (node > ref->node)
1676 p = &(*p)->rb_right;
1677 else
1678 return ref;
1679 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001680 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001681 return NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001682
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001683 binder_stats_created(BINDER_STAT_REF);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001684 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001685 new_ref->proc = proc;
1686 new_ref->node = node;
1687 rb_link_node(&new_ref->rb_node_node, parent, p);
1688 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1689
Todd Kjosb0117bb2017-05-08 09:16:27 -07001690 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001691 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1692 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001693 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001694 break;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001695 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001696 }
1697
1698 p = &proc->refs_by_desc.rb_node;
1699 while (*p) {
1700 parent = *p;
1701 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1702
Todd Kjosb0117bb2017-05-08 09:16:27 -07001703 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001704 p = &(*p)->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001705 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001706 p = &(*p)->rb_right;
1707 else
1708 BUG();
1709 }
1710 rb_link_node(&new_ref->rb_node_desc, parent, p);
1711 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001712
1713 binder_node_lock(node);
Todd Kjos4cbe5752017-05-01 17:21:51 -07001714 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001715
Todd Kjos4cbe5752017-05-01 17:21:51 -07001716 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1717 "%d new ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001718 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjos4cbe5752017-05-01 17:21:51 -07001719 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001720 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001721 return new_ref;
1722}
1723
Todd Kjos5346bf32016-10-20 16:43:34 -07001724static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001725{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001726 bool delete_node = false;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001727
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001728 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301729 "%d delete ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001730 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301731 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001732
1733 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1734 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001735
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001736 binder_node_inner_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001737 if (ref->data.strong)
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001738 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001739
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001740 hlist_del(&ref->node_entry);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001741 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1742 binder_node_inner_unlock(ref->node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001743 /*
1744 * Clear ref->node unless we want the caller to free the node
1745 */
1746 if (!delete_node) {
1747 /*
1748 * The caller uses ref->node to determine
1749 * whether the node needs to be freed. Clear
1750 * it since the node is still alive.
1751 */
1752 ref->node = NULL;
1753 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001754
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001755 if (ref->death) {
1756 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301757 "%d delete ref %d desc %d has death notification\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001758 ref->proc->pid, ref->data.debug_id,
1759 ref->data.desc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001760 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001761 binder_stats_deleted(BINDER_STAT_DEATH);
1762 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001763 binder_stats_deleted(BINDER_STAT_REF);
1764}
1765
Todd Kjosb0117bb2017-05-08 09:16:27 -07001766/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001767 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjosb0117bb2017-05-08 09:16:27 -07001768 * @ref: ref to be incremented
1769 * @strong: if true, strong increment, else weak
1770 * @target_list: list to queue node work on
1771 *
Todd Kjos5346bf32016-10-20 16:43:34 -07001772 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjosb0117bb2017-05-08 09:16:27 -07001773 *
1774 * Return: 0, if successful, else errno
1775 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001776static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1777 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001778{
1779 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001780
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001781 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001782 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001783 ret = binder_inc_node(ref->node, 1, 1, target_list);
1784 if (ret)
1785 return ret;
1786 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001787 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001788 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001789 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001790 ret = binder_inc_node(ref->node, 0, 1, target_list);
1791 if (ret)
1792 return ret;
1793 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001794 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001795 }
1796 return 0;
1797}
1798
Todd Kjosb0117bb2017-05-08 09:16:27 -07001799/**
1800 * binder_dec_ref() - dec the ref for given handle
1801 * @ref: ref to be decremented
1802 * @strong: if true, strong decrement, else weak
1803 *
1804 * Decrement the ref.
1805 *
Todd Kjosb0117bb2017-05-08 09:16:27 -07001806 * Return: true if ref is cleaned up and ready to be freed
1807 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001808static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001809{
1810 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001811 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301812 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001813 ref->proc->pid, ref->data.debug_id,
1814 ref->data.desc, ref->data.strong,
1815 ref->data.weak);
1816 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001817 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001818 ref->data.strong--;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001819 if (ref->data.strong == 0)
1820 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001821 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001822 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301823 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001824 ref->proc->pid, ref->data.debug_id,
1825 ref->data.desc, ref->data.strong,
1826 ref->data.weak);
1827 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001828 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001829 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001830 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001831 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001832 binder_cleanup_ref_olocked(ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001833 return true;
1834 }
1835 return false;
1836}
1837
1838/**
1839 * binder_get_node_from_ref() - get the node from the given proc/desc
1840 * @proc: proc containing the ref
1841 * @desc: the handle associated with the ref
1842 * @need_strong_ref: if true, only return node if ref is strong
1843 * @rdata: the id/refcount data for the ref
1844 *
1845 * Given a proc and ref handle, return the associated binder_node
1846 *
1847 * Return: a binder_node or NULL if not found or not strong when strong required
1848 */
1849static struct binder_node *binder_get_node_from_ref(
1850 struct binder_proc *proc,
1851 u32 desc, bool need_strong_ref,
1852 struct binder_ref_data *rdata)
1853{
1854 struct binder_node *node;
1855 struct binder_ref *ref;
1856
Todd Kjos5346bf32016-10-20 16:43:34 -07001857 binder_proc_lock(proc);
1858 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001859 if (!ref)
1860 goto err_no_ref;
1861 node = ref->node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001862 /*
1863 * Take an implicit reference on the node to ensure
1864 * it stays alive until the call to binder_put_node()
1865 */
1866 binder_inc_node_tmpref(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001867 if (rdata)
1868 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001869 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001870
1871 return node;
1872
1873err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001874 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001875 return NULL;
1876}
1877
1878/**
1879 * binder_free_ref() - free the binder_ref
1880 * @ref: ref to free
1881 *
Todd Kjose7f23ed2017-03-21 13:06:01 -07001882 * Free the binder_ref. Free the binder_node indicated by ref->node
1883 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjosb0117bb2017-05-08 09:16:27 -07001884 */
1885static void binder_free_ref(struct binder_ref *ref)
1886{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001887 if (ref->node)
1888 binder_free_node(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001889 kfree(ref->death);
1890 kfree(ref);
1891}
1892
1893/**
1894 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1895 * @proc: proc containing the ref
1896 * @desc: the handle associated with the ref
1897 * @increment: true=inc reference, false=dec reference
1898 * @strong: true=strong reference, false=weak reference
1899 * @rdata: the id/refcount data for the ref
1900 *
1901 * Given a proc and ref handle, increment or decrement the ref
1902 * according to "increment" arg.
1903 *
1904 * Return: 0 if successful, else errno
1905 */
1906static int binder_update_ref_for_handle(struct binder_proc *proc,
1907 uint32_t desc, bool increment, bool strong,
1908 struct binder_ref_data *rdata)
1909{
1910 int ret = 0;
1911 struct binder_ref *ref;
1912 bool delete_ref = false;
1913
Todd Kjos5346bf32016-10-20 16:43:34 -07001914 binder_proc_lock(proc);
1915 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001916 if (!ref) {
1917 ret = -EINVAL;
1918 goto err_no_ref;
1919 }
1920 if (increment)
Todd Kjos5346bf32016-10-20 16:43:34 -07001921 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001922 else
Todd Kjos5346bf32016-10-20 16:43:34 -07001923 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001924
1925 if (rdata)
1926 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001927 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001928
1929 if (delete_ref)
1930 binder_free_ref(ref);
1931 return ret;
1932
1933err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001934 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001935 return ret;
1936}
1937
1938/**
1939 * binder_dec_ref_for_handle() - dec the ref for given handle
1940 * @proc: proc containing the ref
1941 * @desc: the handle associated with the ref
1942 * @strong: true=strong reference, false=weak reference
1943 * @rdata: the id/refcount data for the ref
1944 *
1945 * Just calls binder_update_ref_for_handle() to decrement the ref.
1946 *
1947 * Return: 0 if successful, else errno
1948 */
1949static int binder_dec_ref_for_handle(struct binder_proc *proc,
1950 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1951{
1952 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1953}
1954
1955
1956/**
1957 * binder_inc_ref_for_node() - increment the ref for given proc/node
1958 * @proc: proc containing the ref
1959 * @node: target node
1960 * @strong: true=strong reference, false=weak reference
1961 * @target_list: worklist to use if node is incremented
1962 * @rdata: the id/refcount data for the ref
1963 *
1964 * Given a proc and node, increment the ref. Create the ref if it
1965 * doesn't already exist
1966 *
1967 * Return: 0 if successful, else errno
1968 */
1969static int binder_inc_ref_for_node(struct binder_proc *proc,
1970 struct binder_node *node,
1971 bool strong,
1972 struct list_head *target_list,
1973 struct binder_ref_data *rdata)
1974{
1975 struct binder_ref *ref;
1976 struct binder_ref *new_ref = NULL;
1977 int ret = 0;
1978
Todd Kjos5346bf32016-10-20 16:43:34 -07001979 binder_proc_lock(proc);
1980 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001981 if (!ref) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001982 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001983 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1984 if (!new_ref)
1985 return -ENOMEM;
Todd Kjos5346bf32016-10-20 16:43:34 -07001986 binder_proc_lock(proc);
1987 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001988 }
Todd Kjos5346bf32016-10-20 16:43:34 -07001989 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001990 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001991 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001992 if (new_ref && ref != new_ref)
1993 /*
1994 * Another thread created the ref first so
1995 * free the one we allocated
1996 */
1997 kfree(new_ref);
1998 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001999}
2000
Martijn Coenen995a36e2017-06-02 13:36:52 -07002001static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
2002 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002003{
Todd Kjos21ef40a2017-03-30 18:02:13 -07002004 BUG_ON(!target_thread);
Martijn Coenened323352017-07-27 23:52:24 +02002005 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjos21ef40a2017-03-30 18:02:13 -07002006 BUG_ON(target_thread->transaction_stack != t);
2007 BUG_ON(target_thread->transaction_stack->from != target_thread);
2008 target_thread->transaction_stack =
2009 target_thread->transaction_stack->from_parent;
2010 t->from = NULL;
2011}
2012
Todd Kjos2f993e22017-05-12 14:42:55 -07002013/**
2014 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
2015 * @thread: thread to decrement
2016 *
2017 * A thread needs to be kept alive while being used to create or
2018 * handle a transaction. binder_get_txn_from() is used to safely
2019 * extract t->from from a binder_transaction and keep the thread
2020 * indicated by t->from from being freed. When done with that
2021 * binder_thread, this function is called to decrement the
2022 * tmp_ref and free if appropriate (thread has been released
2023 * and no transaction being processed by the driver)
2024 */
2025static void binder_thread_dec_tmpref(struct binder_thread *thread)
2026{
2027 /*
2028 * atomic is used to protect the counter value while
2029 * it cannot reach zero or thread->is_dead is false
Todd Kjos2f993e22017-05-12 14:42:55 -07002030 */
Todd Kjosb4827902017-05-25 15:52:17 -07002031 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002032 atomic_dec(&thread->tmp_ref);
2033 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07002034 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002035 binder_free_thread(thread);
2036 return;
2037 }
Todd Kjosb4827902017-05-25 15:52:17 -07002038 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002039}
2040
2041/**
2042 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
2043 * @proc: proc to decrement
2044 *
2045 * A binder_proc needs to be kept alive while being used to create or
2046 * handle a transaction. proc->tmp_ref is incremented when
2047 * creating a new transaction or the binder_proc is currently in-use
2048 * by threads that are being released. When done with the binder_proc,
2049 * this function is called to decrement the counter and free the
2050 * proc if appropriate (proc has been released, all threads have
2051 * been released and not currenly in-use to process a transaction).
2052 */
2053static void binder_proc_dec_tmpref(struct binder_proc *proc)
2054{
Todd Kjosb4827902017-05-25 15:52:17 -07002055 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002056 proc->tmp_ref--;
2057 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
2058 !proc->tmp_ref) {
Todd Kjosb4827902017-05-25 15:52:17 -07002059 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002060 binder_free_proc(proc);
2061 return;
2062 }
Todd Kjosb4827902017-05-25 15:52:17 -07002063 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002064}
2065
2066/**
2067 * binder_get_txn_from() - safely extract the "from" thread in transaction
2068 * @t: binder transaction for t->from
2069 *
2070 * Atomically return the "from" thread and increment the tmp_ref
2071 * count for the thread to ensure it stays alive until
2072 * binder_thread_dec_tmpref() is called.
2073 *
2074 * Return: the value of t->from
2075 */
2076static struct binder_thread *binder_get_txn_from(
2077 struct binder_transaction *t)
2078{
2079 struct binder_thread *from;
2080
2081 spin_lock(&t->lock);
2082 from = t->from;
2083 if (from)
2084 atomic_inc(&from->tmp_ref);
2085 spin_unlock(&t->lock);
2086 return from;
2087}
2088
Martijn Coenen995a36e2017-06-02 13:36:52 -07002089/**
2090 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2091 * @t: binder transaction for t->from
2092 *
2093 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2094 * to guarantee that the thread cannot be released while operating on it.
2095 * The caller must call binder_inner_proc_unlock() to release the inner lock
2096 * as well as call binder_dec_thread_txn() to release the reference.
2097 *
2098 * Return: the value of t->from
2099 */
2100static struct binder_thread *binder_get_txn_from_and_acq_inner(
2101 struct binder_transaction *t)
2102{
2103 struct binder_thread *from;
2104
2105 from = binder_get_txn_from(t);
2106 if (!from)
2107 return NULL;
2108 binder_inner_proc_lock(from->proc);
2109 if (t->from) {
2110 BUG_ON(from != t->from);
2111 return from;
2112 }
2113 binder_inner_proc_unlock(from->proc);
2114 binder_thread_dec_tmpref(from);
2115 return NULL;
2116}
2117
Todd Kjos21ef40a2017-03-30 18:02:13 -07002118static void binder_free_transaction(struct binder_transaction *t)
2119{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002120 if (t->buffer)
2121 t->buffer->transaction = NULL;
2122 kfree(t);
2123 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2124}
2125
2126static void binder_send_failed_reply(struct binder_transaction *t,
2127 uint32_t error_code)
2128{
2129 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002130 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09002131
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002132 BUG_ON(t->flags & TF_ONE_WAY);
2133 while (1) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002134 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002135 if (target_thread) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002136 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2137 "send failed reply for transaction %d to %d:%d\n",
2138 t->debug_id,
2139 target_thread->proc->pid,
2140 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002141
Martijn Coenen995a36e2017-06-02 13:36:52 -07002142 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos858b8da2017-04-21 17:35:12 -07002143 if (target_thread->reply_error.cmd == BR_OK) {
2144 target_thread->reply_error.cmd = error_code;
Martijn Coenen1af61802017-10-19 15:04:46 +02002145 binder_enqueue_thread_work_ilocked(
2146 target_thread,
2147 &target_thread->reply_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002148 wake_up_interruptible(&target_thread->wait);
2149 } else {
Todd Kjos858b8da2017-04-21 17:35:12 -07002150 WARN(1, "Unexpected reply error: %u\n",
2151 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002152 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002153 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002154 binder_thread_dec_tmpref(target_thread);
Todd Kjos858b8da2017-04-21 17:35:12 -07002155 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002156 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002157 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002158 next = t->from_parent;
2159
2160 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2161 "send failed reply for transaction %d, target dead\n",
2162 t->debug_id);
2163
Todd Kjos21ef40a2017-03-30 18:02:13 -07002164 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002165 if (next == NULL) {
2166 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2167 "reply failed, no target thread at root\n");
2168 return;
2169 }
2170 t = next;
2171 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2172 "reply failed, no target thread -- retry %d\n",
2173 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002174 }
2175}
2176
Martijn Coenen00c80372016-07-13 12:06:49 +02002177/**
Martijn Coenen3217ccc2017-08-24 15:23:36 +02002178 * binder_cleanup_transaction() - cleans up undelivered transaction
2179 * @t: transaction that needs to be cleaned up
2180 * @reason: reason the transaction wasn't delivered
2181 * @error_code: error to return to caller (if synchronous call)
2182 */
2183static void binder_cleanup_transaction(struct binder_transaction *t,
2184 const char *reason,
2185 uint32_t error_code)
2186{
2187 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2188 binder_send_failed_reply(t, error_code);
2189 } else {
2190 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2191 "undelivered transaction %d, %s\n",
2192 t->debug_id, reason);
2193 binder_free_transaction(t);
2194 }
2195}
2196
2197/**
Martijn Coenen00c80372016-07-13 12:06:49 +02002198 * binder_validate_object() - checks for a valid metadata object in a buffer.
2199 * @buffer: binder_buffer that we're parsing.
2200 * @offset: offset in the buffer at which to validate an object.
2201 *
2202 * Return: If there's a valid metadata object at @offset in @buffer, the
2203 * size of that object. Otherwise, it returns zero.
2204 */
2205static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2206{
2207 /* Check if we can read a header first */
2208 struct binder_object_header *hdr;
2209 size_t object_size = 0;
2210
2211 if (offset > buffer->data_size - sizeof(*hdr) ||
2212 buffer->data_size < sizeof(*hdr) ||
2213 !IS_ALIGNED(offset, sizeof(u32)))
2214 return 0;
2215
2216 /* Ok, now see if we can read a complete object. */
2217 hdr = (struct binder_object_header *)(buffer->data + offset);
2218 switch (hdr->type) {
2219 case BINDER_TYPE_BINDER:
2220 case BINDER_TYPE_WEAK_BINDER:
2221 case BINDER_TYPE_HANDLE:
2222 case BINDER_TYPE_WEAK_HANDLE:
2223 object_size = sizeof(struct flat_binder_object);
2224 break;
2225 case BINDER_TYPE_FD:
2226 object_size = sizeof(struct binder_fd_object);
2227 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002228 case BINDER_TYPE_PTR:
2229 object_size = sizeof(struct binder_buffer_object);
2230 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002231 case BINDER_TYPE_FDA:
2232 object_size = sizeof(struct binder_fd_array_object);
2233 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02002234 default:
2235 return 0;
2236 }
2237 if (offset <= buffer->data_size - object_size &&
2238 buffer->data_size >= object_size)
2239 return object_size;
2240 else
2241 return 0;
2242}
2243
Martijn Coenen5a6da532016-09-30 14:10:07 +02002244/**
2245 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2246 * @b: binder_buffer containing the object
2247 * @index: index in offset array at which the binder_buffer_object is
2248 * located
2249 * @start: points to the start of the offset array
2250 * @num_valid: the number of valid offsets in the offset array
2251 *
2252 * Return: If @index is within the valid range of the offset array
2253 * described by @start and @num_valid, and if there's a valid
2254 * binder_buffer_object at the offset found in index @index
2255 * of the offset array, that object is returned. Otherwise,
2256 * %NULL is returned.
2257 * Note that the offset found in index @index itself is not
2258 * verified; this function assumes that @num_valid elements
2259 * from @start were previously verified to have valid offsets.
2260 */
2261static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2262 binder_size_t index,
2263 binder_size_t *start,
2264 binder_size_t num_valid)
2265{
2266 struct binder_buffer_object *buffer_obj;
2267 binder_size_t *offp;
2268
2269 if (index >= num_valid)
2270 return NULL;
2271
2272 offp = start + index;
2273 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2274 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2275 return NULL;
2276
2277 return buffer_obj;
2278}
2279
2280/**
2281 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2282 * @b: transaction buffer
2283 * @objects_start start of objects buffer
2284 * @buffer: binder_buffer_object in which to fix up
2285 * @offset: start offset in @buffer to fix up
2286 * @last_obj: last binder_buffer_object that we fixed up in
2287 * @last_min_offset: minimum fixup offset in @last_obj
2288 *
2289 * Return: %true if a fixup in buffer @buffer at offset @offset is
2290 * allowed.
2291 *
2292 * For safety reasons, we only allow fixups inside a buffer to happen
2293 * at increasing offsets; additionally, we only allow fixup on the last
2294 * buffer object that was verified, or one of its parents.
2295 *
2296 * Example of what is allowed:
2297 *
2298 * A
2299 * B (parent = A, offset = 0)
2300 * C (parent = A, offset = 16)
2301 * D (parent = C, offset = 0)
2302 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2303 *
2304 * Examples of what is not allowed:
2305 *
2306 * Decreasing offsets within the same parent:
2307 * A
2308 * C (parent = A, offset = 16)
2309 * B (parent = A, offset = 0) // decreasing offset within A
2310 *
2311 * Referring to a parent that wasn't the last object or any of its parents:
2312 * A
2313 * B (parent = A, offset = 0)
2314 * C (parent = A, offset = 0)
2315 * C (parent = A, offset = 16)
2316 * D (parent = B, offset = 0) // B is not A or any of A's parents
2317 */
2318static bool binder_validate_fixup(struct binder_buffer *b,
2319 binder_size_t *objects_start,
2320 struct binder_buffer_object *buffer,
2321 binder_size_t fixup_offset,
2322 struct binder_buffer_object *last_obj,
2323 binder_size_t last_min_offset)
2324{
2325 if (!last_obj) {
2326 /* Nothing to fix up in */
2327 return false;
2328 }
2329
2330 while (last_obj != buffer) {
2331 /*
2332 * Safe to retrieve the parent of last_obj, since it
2333 * was already previously verified by the driver.
2334 */
2335 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2336 return false;
2337 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2338 last_obj = (struct binder_buffer_object *)
2339 (b->data + *(objects_start + last_obj->parent));
2340 }
2341 return (fixup_offset >= last_min_offset);
2342}
2343
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002344static void binder_transaction_buffer_release(struct binder_proc *proc,
2345 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002346 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002347{
Martijn Coenen5a6da532016-09-30 14:10:07 +02002348 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002349 int debug_id = buffer->debug_id;
2350
2351 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302352 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002353 proc->pid, buffer->debug_id,
2354 buffer->data_size, buffer->offsets_size, failed_at);
2355
2356 if (buffer->target_node)
2357 binder_dec_node(buffer->target_node, 1, 0);
2358
Martijn Coenen5a6da532016-09-30 14:10:07 +02002359 off_start = (binder_size_t *)(buffer->data +
2360 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002361 if (failed_at)
2362 off_end = failed_at;
2363 else
Martijn Coenen5a6da532016-09-30 14:10:07 +02002364 off_end = (void *)off_start + buffer->offsets_size;
2365 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002366 struct binder_object_header *hdr;
2367 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002368
Martijn Coenen00c80372016-07-13 12:06:49 +02002369 if (object_size == 0) {
2370 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002371 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002372 continue;
2373 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002374 hdr = (struct binder_object_header *)(buffer->data + *offp);
2375 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002376 case BINDER_TYPE_BINDER:
2377 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002378 struct flat_binder_object *fp;
2379 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002380
Martijn Coenen00c80372016-07-13 12:06:49 +02002381 fp = to_flat_binder_object(hdr);
2382 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002383 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002384 pr_err("transaction release %d bad node %016llx\n",
2385 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002386 break;
2387 }
2388 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002389 " node %d u%016llx\n",
2390 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002391 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2392 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002393 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002394 } break;
2395 case BINDER_TYPE_HANDLE:
2396 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002397 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002398 struct binder_ref_data rdata;
2399 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002400
Martijn Coenen00c80372016-07-13 12:06:49 +02002401 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002402 ret = binder_dec_ref_for_handle(proc, fp->handle,
2403 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2404
2405 if (ret) {
2406 pr_err("transaction release %d bad handle %d, ret = %d\n",
2407 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002408 break;
2409 }
2410 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002411 " ref %d desc %d\n",
2412 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002413 } break;
2414
Martijn Coenen00c80372016-07-13 12:06:49 +02002415 case BINDER_TYPE_FD: {
2416 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2417
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002418 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002419 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002420 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002421 task_close_fd(proc, fp->fd);
2422 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002423 case BINDER_TYPE_PTR:
2424 /*
2425 * Nothing to do here, this will get cleaned up when the
2426 * transaction buffer gets freed
2427 */
2428 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002429 case BINDER_TYPE_FDA: {
2430 struct binder_fd_array_object *fda;
2431 struct binder_buffer_object *parent;
2432 uintptr_t parent_buffer;
2433 u32 *fd_array;
2434 size_t fd_index;
2435 binder_size_t fd_buf_size;
2436
2437 fda = to_binder_fd_array_object(hdr);
2438 parent = binder_validate_ptr(buffer, fda->parent,
2439 off_start,
2440 offp - off_start);
2441 if (!parent) {
2442 pr_err("transaction release %d bad parent offset",
2443 debug_id);
2444 continue;
2445 }
2446 /*
2447 * Since the parent was already fixed up, convert it
2448 * back to kernel address space to access it
2449 */
2450 parent_buffer = parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002451 binder_alloc_get_user_buffer_offset(
2452 &proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002453
2454 fd_buf_size = sizeof(u32) * fda->num_fds;
2455 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2456 pr_err("transaction release %d invalid number of fds (%lld)\n",
2457 debug_id, (u64)fda->num_fds);
2458 continue;
2459 }
2460 if (fd_buf_size > parent->length ||
2461 fda->parent_offset > parent->length - fd_buf_size) {
2462 /* No space for all file descriptors here. */
2463 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2464 debug_id, (u64)fda->num_fds);
2465 continue;
2466 }
Arnd Bergmanne312c3f2017-09-05 10:56:13 +02002467 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002468 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2469 task_close_fd(proc, fd_array[fd_index]);
2470 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002471 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002472 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002473 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002474 break;
2475 }
2476 }
2477}
2478
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002479static int binder_translate_binder(struct flat_binder_object *fp,
2480 struct binder_transaction *t,
2481 struct binder_thread *thread)
2482{
2483 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002484 struct binder_proc *proc = thread->proc;
2485 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002486 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002487 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002488
2489 node = binder_get_node(proc, fp->binder);
2490 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002491 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002492 if (!node)
2493 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002494 }
2495 if (fp->cookie != node->cookie) {
2496 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2497 proc->pid, thread->pid, (u64)fp->binder,
2498 node->debug_id, (u64)fp->cookie,
2499 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002500 ret = -EINVAL;
2501 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002502 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002503 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2504 ret = -EPERM;
2505 goto done;
2506 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002507
Todd Kjosb0117bb2017-05-08 09:16:27 -07002508 ret = binder_inc_ref_for_node(target_proc, node,
2509 fp->hdr.type == BINDER_TYPE_BINDER,
2510 &thread->todo, &rdata);
2511 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002512 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002513
2514 if (fp->hdr.type == BINDER_TYPE_BINDER)
2515 fp->hdr.type = BINDER_TYPE_HANDLE;
2516 else
2517 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2518 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002519 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002520 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002521
Todd Kjosb0117bb2017-05-08 09:16:27 -07002522 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002523 binder_debug(BINDER_DEBUG_TRANSACTION,
2524 " node %d u%016llx -> ref %d desc %d\n",
2525 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002526 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002527done:
2528 binder_put_node(node);
2529 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002530}
2531
2532static int binder_translate_handle(struct flat_binder_object *fp,
2533 struct binder_transaction *t,
2534 struct binder_thread *thread)
2535{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002536 struct binder_proc *proc = thread->proc;
2537 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002538 struct binder_node *node;
2539 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002540 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002541
Todd Kjosb0117bb2017-05-08 09:16:27 -07002542 node = binder_get_node_from_ref(proc, fp->handle,
2543 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2544 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002545 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2546 proc->pid, thread->pid, fp->handle);
2547 return -EINVAL;
2548 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002549 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2550 ret = -EPERM;
2551 goto done;
2552 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002553
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002554 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002555 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002556 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2557 fp->hdr.type = BINDER_TYPE_BINDER;
2558 else
2559 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002560 fp->binder = node->ptr;
2561 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002562 if (node->proc)
2563 binder_inner_proc_lock(node->proc);
2564 binder_inc_node_nilocked(node,
2565 fp->hdr.type == BINDER_TYPE_BINDER,
2566 0, NULL);
2567 if (node->proc)
2568 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002569 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002570 binder_debug(BINDER_DEBUG_TRANSACTION,
2571 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002572 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2573 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002574 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002575 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002576 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002577
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002578 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002579 ret = binder_inc_ref_for_node(target_proc, node,
2580 fp->hdr.type == BINDER_TYPE_HANDLE,
2581 NULL, &dest_rdata);
2582 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002583 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002584
2585 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002586 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002587 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002588 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2589 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002590 binder_debug(BINDER_DEBUG_TRANSACTION,
2591 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002592 src_rdata.debug_id, src_rdata.desc,
2593 dest_rdata.debug_id, dest_rdata.desc,
2594 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002595 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002596done:
2597 binder_put_node(node);
2598 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002599}
2600
2601static int binder_translate_fd(int fd,
2602 struct binder_transaction *t,
2603 struct binder_thread *thread,
2604 struct binder_transaction *in_reply_to)
2605{
2606 struct binder_proc *proc = thread->proc;
2607 struct binder_proc *target_proc = t->to_proc;
2608 int target_fd;
2609 struct file *file;
2610 int ret;
2611 bool target_allows_fd;
2612
2613 if (in_reply_to)
2614 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2615 else
2616 target_allows_fd = t->buffer->target_node->accept_fds;
2617 if (!target_allows_fd) {
2618 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2619 proc->pid, thread->pid,
2620 in_reply_to ? "reply" : "transaction",
2621 fd);
2622 ret = -EPERM;
2623 goto err_fd_not_accepted;
2624 }
2625
2626 file = fget(fd);
2627 if (!file) {
2628 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2629 proc->pid, thread->pid, fd);
2630 ret = -EBADF;
2631 goto err_fget;
2632 }
2633 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2634 if (ret < 0) {
2635 ret = -EPERM;
2636 goto err_security;
2637 }
2638
2639 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2640 if (target_fd < 0) {
2641 ret = -ENOMEM;
2642 goto err_get_unused_fd;
2643 }
2644 task_fd_install(target_proc, target_fd, file);
2645 trace_binder_transaction_fd(t, fd, target_fd);
2646 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2647 fd, target_fd);
2648
2649 return target_fd;
2650
2651err_get_unused_fd:
2652err_security:
2653 fput(file);
2654err_fget:
2655err_fd_not_accepted:
2656 return ret;
2657}
2658
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002659static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2660 struct binder_buffer_object *parent,
2661 struct binder_transaction *t,
2662 struct binder_thread *thread,
2663 struct binder_transaction *in_reply_to)
2664{
2665 binder_size_t fdi, fd_buf_size, num_installed_fds;
2666 int target_fd;
2667 uintptr_t parent_buffer;
2668 u32 *fd_array;
2669 struct binder_proc *proc = thread->proc;
2670 struct binder_proc *target_proc = t->to_proc;
2671
2672 fd_buf_size = sizeof(u32) * fda->num_fds;
2673 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2674 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2675 proc->pid, thread->pid, (u64)fda->num_fds);
2676 return -EINVAL;
2677 }
2678 if (fd_buf_size > parent->length ||
2679 fda->parent_offset > parent->length - fd_buf_size) {
2680 /* No space for all file descriptors here. */
2681 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2682 proc->pid, thread->pid, (u64)fda->num_fds);
2683 return -EINVAL;
2684 }
2685 /*
2686 * Since the parent was already fixed up, convert it
2687 * back to the kernel address space to access it
2688 */
Todd Kjosd325d372016-10-10 10:40:53 -07002689 parent_buffer = parent->buffer -
2690 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Arnd Bergmanne312c3f2017-09-05 10:56:13 +02002691 fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002692 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2693 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2694 proc->pid, thread->pid);
2695 return -EINVAL;
2696 }
2697 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2698 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2699 in_reply_to);
2700 if (target_fd < 0)
2701 goto err_translate_fd_failed;
2702 fd_array[fdi] = target_fd;
2703 }
2704 return 0;
2705
2706err_translate_fd_failed:
2707 /*
2708 * Failed to allocate fd or security error, free fds
2709 * installed so far.
2710 */
2711 num_installed_fds = fdi;
2712 for (fdi = 0; fdi < num_installed_fds; fdi++)
2713 task_close_fd(target_proc, fd_array[fdi]);
2714 return target_fd;
2715}
2716
Martijn Coenen5a6da532016-09-30 14:10:07 +02002717static int binder_fixup_parent(struct binder_transaction *t,
2718 struct binder_thread *thread,
2719 struct binder_buffer_object *bp,
2720 binder_size_t *off_start,
2721 binder_size_t num_valid,
2722 struct binder_buffer_object *last_fixup_obj,
2723 binder_size_t last_fixup_min_off)
2724{
2725 struct binder_buffer_object *parent;
2726 u8 *parent_buffer;
2727 struct binder_buffer *b = t->buffer;
2728 struct binder_proc *proc = thread->proc;
2729 struct binder_proc *target_proc = t->to_proc;
2730
2731 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2732 return 0;
2733
2734 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2735 if (!parent) {
2736 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2737 proc->pid, thread->pid);
2738 return -EINVAL;
2739 }
2740
2741 if (!binder_validate_fixup(b, off_start,
2742 parent, bp->parent_offset,
2743 last_fixup_obj,
2744 last_fixup_min_off)) {
2745 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2746 proc->pid, thread->pid);
2747 return -EINVAL;
2748 }
2749
2750 if (parent->length < sizeof(binder_uintptr_t) ||
2751 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2752 /* No space for a pointer here! */
2753 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2754 proc->pid, thread->pid);
2755 return -EINVAL;
2756 }
Arnd Bergmanne312c3f2017-09-05 10:56:13 +02002757 parent_buffer = (u8 *)((uintptr_t)parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002758 binder_alloc_get_user_buffer_offset(
2759 &target_proc->alloc));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002760 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2761
2762 return 0;
2763}
2764
Martijn Coenen053be422017-06-06 15:17:46 -07002765/**
2766 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2767 * @t: transaction to send
2768 * @proc: process to send the transaction to
2769 * @thread: thread in @proc to send the transaction to (may be NULL)
2770 *
2771 * This function queues a transaction to the specified process. It will try
2772 * to find a thread in the target process to handle the transaction and
2773 * wake it up. If no thread is found, the work is queued to the proc
2774 * waitqueue.
2775 *
2776 * If the @thread parameter is not NULL, the transaction is always queued
2777 * to the waitlist of that specific thread.
2778 *
2779 * Return: true if the transactions was successfully queued
2780 * false if the target process or thread is dead
2781 */
2782static bool binder_proc_transaction(struct binder_transaction *t,
2783 struct binder_proc *proc,
2784 struct binder_thread *thread)
2785{
Martijn Coenen053be422017-06-06 15:17:46 -07002786 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002787 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002788 bool oneway = !!(t->flags & TF_ONE_WAY);
Martijn Coenen1af61802017-10-19 15:04:46 +02002789 bool pending_async = false;
Martijn Coenen053be422017-06-06 15:17:46 -07002790
2791 BUG_ON(!node);
2792 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002793 node_prio.prio = node->min_priority;
2794 node_prio.sched_policy = node->sched_policy;
2795
Martijn Coenen053be422017-06-06 15:17:46 -07002796 if (oneway) {
2797 BUG_ON(thread);
2798 if (node->has_async_transaction) {
Martijn Coenen1af61802017-10-19 15:04:46 +02002799 pending_async = true;
Martijn Coenen053be422017-06-06 15:17:46 -07002800 } else {
2801 node->has_async_transaction = 1;
2802 }
2803 }
2804
2805 binder_inner_proc_lock(proc);
2806
2807 if (proc->is_dead || (thread && thread->is_dead)) {
2808 binder_inner_proc_unlock(proc);
2809 binder_node_unlock(node);
2810 return false;
2811 }
2812
Martijn Coenen1af61802017-10-19 15:04:46 +02002813 if (!thread && !pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002814 thread = binder_select_thread_ilocked(proc);
2815
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002816 if (thread) {
Martijn Coenenc46810c2017-06-23 10:13:43 -07002817 binder_transaction_priority(thread->task, t, node_prio,
2818 node->inherit_rt);
Martijn Coenen1af61802017-10-19 15:04:46 +02002819 binder_enqueue_thread_work_ilocked(thread, &t->work);
2820 } else if (!pending_async) {
2821 binder_enqueue_work_ilocked(&t->work, &proc->todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002822 } else {
Martijn Coenen1af61802017-10-19 15:04:46 +02002823 binder_enqueue_work_ilocked(&t->work, &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002824 }
Martijn Coenen053be422017-06-06 15:17:46 -07002825
Martijn Coenen1af61802017-10-19 15:04:46 +02002826 if (!pending_async)
Martijn Coenen053be422017-06-06 15:17:46 -07002827 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2828
2829 binder_inner_proc_unlock(proc);
2830 binder_node_unlock(node);
2831
2832 return true;
2833}
2834
Todd Kjos291d9682017-09-25 08:55:09 -07002835/**
2836 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2837 * @node: struct binder_node for which to get refs
2838 * @proc: returns @node->proc if valid
2839 * @error: if no @proc then returns BR_DEAD_REPLY
2840 *
2841 * User-space normally keeps the node alive when creating a transaction
2842 * since it has a reference to the target. The local strong ref keeps it
2843 * alive if the sending process dies before the target process processes
2844 * the transaction. If the source process is malicious or has a reference
2845 * counting bug, relying on the local strong ref can fail.
2846 *
2847 * Since user-space can cause the local strong ref to go away, we also take
2848 * a tmpref on the node to ensure it survives while we are constructing
2849 * the transaction. We also need a tmpref on the proc while we are
2850 * constructing the transaction, so we take that here as well.
2851 *
2852 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2853 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2854 * target proc has died, @error is set to BR_DEAD_REPLY
2855 */
2856static struct binder_node *binder_get_node_refs_for_txn(
2857 struct binder_node *node,
2858 struct binder_proc **procp,
2859 uint32_t *error)
2860{
2861 struct binder_node *target_node = NULL;
2862
2863 binder_node_inner_lock(node);
2864 if (node->proc) {
2865 target_node = node;
2866 binder_inc_node_nilocked(node, 1, 0, NULL);
2867 binder_inc_node_tmpref_ilocked(node);
2868 node->proc->tmp_ref++;
2869 *procp = node->proc;
2870 } else
2871 *error = BR_DEAD_REPLY;
2872 binder_node_inner_unlock(node);
2873
2874 return target_node;
2875}
2876
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002877static void binder_transaction(struct binder_proc *proc,
2878 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02002879 struct binder_transaction_data *tr, int reply,
2880 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002881{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002882 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002883 struct binder_transaction *t;
2884 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002885 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002886 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002887 u8 *sg_bufp, *sg_buf_end;
Todd Kjos2f993e22017-05-12 14:42:55 -07002888 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002889 struct binder_thread *target_thread = NULL;
2890 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002891 struct binder_transaction *in_reply_to = NULL;
2892 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07002893 uint32_t return_error = 0;
2894 uint32_t return_error_param = 0;
2895 uint32_t return_error_line = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002896 struct binder_buffer_object *last_fixup_obj = NULL;
2897 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002898 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07002899 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002900
2901 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07002902 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002903 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2904 e->from_proc = proc->pid;
2905 e->from_thread = thread->pid;
2906 e->target_handle = tr->target.handle;
2907 e->data_size = tr->data_size;
2908 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02002909 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002910
2911 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002912 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002913 in_reply_to = thread->transaction_stack;
2914 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002915 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302916 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002917 proc->pid, thread->pid);
2918 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002919 return_error_param = -EPROTO;
2920 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002921 goto err_empty_call_stack;
2922 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002923 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002924 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302925 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 +09002926 proc->pid, thread->pid, in_reply_to->debug_id,
2927 in_reply_to->to_proc ?
2928 in_reply_to->to_proc->pid : 0,
2929 in_reply_to->to_thread ?
2930 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002931 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002932 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002933 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002934 return_error_param = -EPROTO;
2935 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002936 in_reply_to = NULL;
2937 goto err_bad_call_stack;
2938 }
2939 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002940 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002941 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002942 if (target_thread == NULL) {
2943 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002944 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002945 goto err_dead_binder;
2946 }
2947 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302948 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 +09002949 proc->pid, thread->pid,
2950 target_thread->transaction_stack ?
2951 target_thread->transaction_stack->debug_id : 0,
2952 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002953 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002954 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002955 return_error_param = -EPROTO;
2956 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002957 in_reply_to = NULL;
2958 target_thread = NULL;
2959 goto err_dead_binder;
2960 }
2961 target_proc = target_thread->proc;
Todd Kjos2f993e22017-05-12 14:42:55 -07002962 target_proc->tmp_ref++;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002963 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002964 } else {
2965 if (tr->target.handle) {
2966 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002967
Todd Kjosc37162d2017-05-26 11:56:29 -07002968 /*
2969 * There must already be a strong ref
2970 * on this node. If so, do a strong
2971 * increment on the node to ensure it
2972 * stays alive until the transaction is
2973 * done.
2974 */
Todd Kjos5346bf32016-10-20 16:43:34 -07002975 binder_proc_lock(proc);
2976 ref = binder_get_ref_olocked(proc, tr->target.handle,
2977 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07002978 if (ref) {
Todd Kjos291d9682017-09-25 08:55:09 -07002979 target_node = binder_get_node_refs_for_txn(
2980 ref->node, &target_proc,
2981 &return_error);
2982 } else {
2983 binder_user_error("%d:%d got transaction to invalid handle\n",
2984 proc->pid, thread->pid);
2985 return_error = BR_FAILED_REPLY;
Todd Kjosc37162d2017-05-26 11:56:29 -07002986 }
Todd Kjos5346bf32016-10-20 16:43:34 -07002987 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002988 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002989 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002990 target_node = context->binder_context_mgr_node;
Todd Kjos291d9682017-09-25 08:55:09 -07002991 if (target_node)
2992 target_node = binder_get_node_refs_for_txn(
2993 target_node, &target_proc,
2994 &return_error);
2995 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002996 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002997 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002998 }
Todd Kjos291d9682017-09-25 08:55:09 -07002999 if (!target_node) {
3000 /*
3001 * return_error is set above
3002 */
3003 return_error_param = -EINVAL;
Todd Kjose598d172017-03-22 17:19:52 -07003004 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003005 goto err_dead_binder;
3006 }
Todd Kjos291d9682017-09-25 08:55:09 -07003007 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05003008 if (security_binder_transaction(proc->tsk,
3009 target_proc->tsk) < 0) {
3010 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003011 return_error_param = -EPERM;
3012 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05003013 goto err_invalid_target_handle;
3014 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003015 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003016 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
3017 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003018
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003019 tmp = thread->transaction_stack;
3020 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003021 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05303022 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 +09003023 proc->pid, thread->pid, tmp->debug_id,
3024 tmp->to_proc ? tmp->to_proc->pid : 0,
3025 tmp->to_thread ?
3026 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07003027 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003028 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003029 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003030 return_error_param = -EPROTO;
3031 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003032 goto err_bad_call_stack;
3033 }
3034 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003035 struct binder_thread *from;
3036
3037 spin_lock(&tmp->lock);
3038 from = tmp->from;
3039 if (from && from->proc == target_proc) {
3040 atomic_inc(&from->tmp_ref);
3041 target_thread = from;
3042 spin_unlock(&tmp->lock);
3043 break;
3044 }
3045 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003046 tmp = tmp->from_parent;
3047 }
3048 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07003049 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003050 }
Martijn Coenen053be422017-06-06 15:17:46 -07003051 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003052 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003053 e->to_proc = target_proc->pid;
3054
3055 /* TODO: reuse incoming transaction for reply */
3056 t = kzalloc(sizeof(*t), GFP_KERNEL);
3057 if (t == NULL) {
3058 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003059 return_error_param = -ENOMEM;
3060 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003061 goto err_alloc_t_failed;
3062 }
3063 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07003064 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003065
3066 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3067 if (tcomplete == NULL) {
3068 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003069 return_error_param = -ENOMEM;
3070 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003071 goto err_alloc_tcomplete_failed;
3072 }
3073 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3074
Todd Kjos1cfe6272017-05-24 13:33:28 -07003075 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003076
3077 if (reply)
3078 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003079 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003080 proc->pid, thread->pid, t->debug_id,
3081 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003082 (u64)tr->data.ptr.buffer,
3083 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003084 (u64)tr->data_size, (u64)tr->offsets_size,
3085 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003086 else
3087 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003088 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003089 proc->pid, thread->pid, t->debug_id,
3090 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003091 (u64)tr->data.ptr.buffer,
3092 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003093 (u64)tr->data_size, (u64)tr->offsets_size,
3094 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003095
3096 if (!reply && !(tr->flags & TF_ONE_WAY))
3097 t->from = thread;
3098 else
3099 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03003100 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003101 t->to_proc = target_proc;
3102 t->to_thread = target_thread;
3103 t->code = tr->code;
3104 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003105 if (!(t->flags & TF_ONE_WAY) &&
3106 binder_supported_policy(current->policy)) {
3107 /* Inherit supported policies for synchronous transactions */
3108 t->priority.sched_policy = current->policy;
3109 t->priority.prio = current->normal_prio;
3110 } else {
3111 /* Otherwise, fall back to the default priority */
3112 t->priority = target_proc->default_priority;
3113 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003114
3115 trace_binder_transaction(reply, t, target_node);
3116
Todd Kjosd325d372016-10-10 10:40:53 -07003117 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02003118 tr->offsets_size, extra_buffers_size,
3119 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07003120 if (IS_ERR(t->buffer)) {
3121 /*
3122 * -ESRCH indicates VMA cleared. The target is dying.
3123 */
3124 return_error_param = PTR_ERR(t->buffer);
3125 return_error = return_error_param == -ESRCH ?
3126 BR_DEAD_REPLY : BR_FAILED_REPLY;
3127 return_error_line = __LINE__;
3128 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003129 goto err_binder_alloc_buf_failed;
3130 }
3131 t->buffer->allow_user_free = 0;
3132 t->buffer->debug_id = t->debug_id;
3133 t->buffer->transaction = t;
3134 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003135 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003136 off_start = (binder_size_t *)(t->buffer->data +
3137 ALIGN(tr->data_size, sizeof(void *)));
3138 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003139
Arve Hjønnevågda498892014-02-21 14:40:26 -08003140 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3141 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303142 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3143 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003144 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003145 return_error_param = -EFAULT;
3146 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003147 goto err_copy_data_failed;
3148 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003149 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3150 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303151 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3152 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003153 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003154 return_error_param = -EFAULT;
3155 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003156 goto err_copy_data_failed;
3157 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003158 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3159 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3160 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003161 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003162 return_error_param = -EINVAL;
3163 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003164 goto err_bad_offset;
3165 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003166 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3167 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3168 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303169 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003170 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003171 return_error_param = -EINVAL;
3172 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003173 goto err_bad_offset;
3174 }
3175 off_end = (void *)off_start + tr->offsets_size;
3176 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3177 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003178 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003179 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003180 struct binder_object_header *hdr;
3181 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09003182
Martijn Coenen00c80372016-07-13 12:06:49 +02003183 if (object_size == 0 || *offp < off_min) {
3184 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 -08003185 proc->pid, thread->pid, (u64)*offp,
3186 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003187 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003188 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003189 return_error_param = -EINVAL;
3190 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003191 goto err_bad_offset;
3192 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003193
3194 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3195 off_min = *offp + object_size;
3196 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003197 case BINDER_TYPE_BINDER:
3198 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003199 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003200
Martijn Coenen00c80372016-07-13 12:06:49 +02003201 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003202 ret = binder_translate_binder(fp, t, thread);
3203 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003204 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003205 return_error_param = ret;
3206 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003207 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003208 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003209 } break;
3210 case BINDER_TYPE_HANDLE:
3211 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003212 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003213
Martijn Coenen00c80372016-07-13 12:06:49 +02003214 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003215 ret = binder_translate_handle(fp, t, thread);
3216 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003217 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003218 return_error_param = ret;
3219 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003220 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003221 }
3222 } break;
3223
3224 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003225 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003226 int target_fd = binder_translate_fd(fp->fd, t, thread,
3227 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003228
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003229 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003230 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003231 return_error_param = target_fd;
3232 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003233 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003234 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003235 fp->pad_binder = 0;
3236 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003237 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003238 case BINDER_TYPE_FDA: {
3239 struct binder_fd_array_object *fda =
3240 to_binder_fd_array_object(hdr);
3241 struct binder_buffer_object *parent =
3242 binder_validate_ptr(t->buffer, fda->parent,
3243 off_start,
3244 offp - off_start);
3245 if (!parent) {
3246 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3247 proc->pid, thread->pid);
3248 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003249 return_error_param = -EINVAL;
3250 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003251 goto err_bad_parent;
3252 }
3253 if (!binder_validate_fixup(t->buffer, off_start,
3254 parent, fda->parent_offset,
3255 last_fixup_obj,
3256 last_fixup_min_off)) {
3257 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3258 proc->pid, thread->pid);
3259 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003260 return_error_param = -EINVAL;
3261 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003262 goto err_bad_parent;
3263 }
3264 ret = binder_translate_fd_array(fda, parent, t, thread,
3265 in_reply_to);
3266 if (ret < 0) {
3267 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003268 return_error_param = ret;
3269 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003270 goto err_translate_failed;
3271 }
3272 last_fixup_obj = parent;
3273 last_fixup_min_off =
3274 fda->parent_offset + sizeof(u32) * fda->num_fds;
3275 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003276 case BINDER_TYPE_PTR: {
3277 struct binder_buffer_object *bp =
3278 to_binder_buffer_object(hdr);
3279 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003280
Martijn Coenen5a6da532016-09-30 14:10:07 +02003281 if (bp->length > buf_left) {
3282 binder_user_error("%d:%d got transaction with too large buffer\n",
3283 proc->pid, thread->pid);
3284 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003285 return_error_param = -EINVAL;
3286 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003287 goto err_bad_offset;
3288 }
3289 if (copy_from_user(sg_bufp,
3290 (const void __user *)(uintptr_t)
3291 bp->buffer, bp->length)) {
3292 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3293 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003294 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003295 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003296 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003297 goto err_copy_data_failed;
3298 }
3299 /* Fixup buffer pointer to target proc address space */
3300 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjosd325d372016-10-10 10:40:53 -07003301 binder_alloc_get_user_buffer_offset(
3302 &target_proc->alloc);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003303 sg_bufp += ALIGN(bp->length, sizeof(u64));
3304
3305 ret = binder_fixup_parent(t, thread, bp, off_start,
3306 offp - off_start,
3307 last_fixup_obj,
3308 last_fixup_min_off);
3309 if (ret < 0) {
3310 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003311 return_error_param = ret;
3312 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003313 goto err_translate_failed;
3314 }
3315 last_fixup_obj = bp;
3316 last_fixup_min_off = 0;
3317 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003318 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003319 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003320 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003321 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003322 return_error_param = -EINVAL;
3323 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003324 goto err_bad_object_type;
3325 }
3326 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003327 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003328 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003329
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003330 if (reply) {
Martijn Coenen1af61802017-10-19 15:04:46 +02003331 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003332 binder_inner_proc_lock(target_proc);
3333 if (target_thread->is_dead) {
3334 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003335 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003336 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003337 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003338 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen1af61802017-10-19 15:04:46 +02003339 binder_enqueue_thread_work_ilocked(target_thread, &t->work);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003340 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003341 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003342 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003343 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003344 } else if (!(t->flags & TF_ONE_WAY)) {
3345 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003346 binder_inner_proc_lock(proc);
Martijn Coenendac2e9c2017-11-13 09:55:21 +01003347 /*
3348 * Defer the TRANSACTION_COMPLETE, so we don't return to
3349 * userspace immediately; this allows the target process to
3350 * immediately start processing this transaction, reducing
3351 * latency. We will then return the TRANSACTION_COMPLETE when
3352 * the target replies (or there is an error).
3353 */
3354 binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003355 t->need_reply = 1;
3356 t->from_parent = thread->transaction_stack;
3357 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003358 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003359 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003360 binder_inner_proc_lock(proc);
3361 binder_pop_transaction_ilocked(thread, t);
3362 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003363 goto err_dead_proc_or_thread;
3364 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003365 } else {
3366 BUG_ON(target_node == NULL);
3367 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen1af61802017-10-19 15:04:46 +02003368 binder_enqueue_thread_work(thread, tcomplete);
Martijn Coenen053be422017-06-06 15:17:46 -07003369 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003370 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003371 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003372 if (target_thread)
3373 binder_thread_dec_tmpref(target_thread);
3374 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003375 if (target_node)
3376 binder_dec_node_tmpref(target_node);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003377 /*
3378 * write barrier to synchronize with initialization
3379 * of log entry
3380 */
3381 smp_wmb();
3382 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003383 return;
3384
Todd Kjos2f993e22017-05-12 14:42:55 -07003385err_dead_proc_or_thread:
3386 return_error = BR_DEAD_REPLY;
3387 return_error_line = __LINE__;
Xu YiPing86578a02017-05-22 11:26:23 -07003388 binder_dequeue_work(proc, tcomplete);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003389err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003390err_bad_object_type:
3391err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003392err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003393err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003394 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003395 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjos291d9682017-09-25 08:55:09 -07003396 if (target_node)
3397 binder_dec_node_tmpref(target_node);
Todd Kjosc37162d2017-05-26 11:56:29 -07003398 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003399 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003400 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401err_binder_alloc_buf_failed:
3402 kfree(tcomplete);
3403 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3404err_alloc_tcomplete_failed:
3405 kfree(t);
3406 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3407err_alloc_t_failed:
3408err_bad_call_stack:
3409err_empty_call_stack:
3410err_dead_binder:
3411err_invalid_target_handle:
Todd Kjos2f993e22017-05-12 14:42:55 -07003412 if (target_thread)
3413 binder_thread_dec_tmpref(target_thread);
3414 if (target_proc)
3415 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003416 if (target_node) {
Todd Kjosc37162d2017-05-26 11:56:29 -07003417 binder_dec_node(target_node, 1, 0);
Todd Kjos291d9682017-09-25 08:55:09 -07003418 binder_dec_node_tmpref(target_node);
3419 }
Todd Kjosc37162d2017-05-26 11:56:29 -07003420
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003421 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003422 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3423 proc->pid, thread->pid, return_error, return_error_param,
3424 (u64)tr->data_size, (u64)tr->offsets_size,
3425 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003426
3427 {
3428 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003429
Todd Kjose598d172017-03-22 17:19:52 -07003430 e->return_error = return_error;
3431 e->return_error_param = return_error_param;
3432 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003433 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3434 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003435 /*
3436 * write barrier to synchronize with initialization
3437 * of log entry
3438 */
3439 smp_wmb();
3440 WRITE_ONCE(e->debug_id_done, t_debug_id);
3441 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003442 }
3443
Todd Kjos858b8da2017-04-21 17:35:12 -07003444 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003445 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003446 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003447 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Martijn Coenen1af61802017-10-19 15:04:46 +02003448 binder_enqueue_thread_work(thread, &thread->return_error.work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003449 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003450 } else {
3451 thread->return_error.cmd = return_error;
Martijn Coenen1af61802017-10-19 15:04:46 +02003452 binder_enqueue_thread_work(thread, &thread->return_error.work);
Todd Kjos858b8da2017-04-21 17:35:12 -07003453 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003454}
3455
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003456static int binder_thread_write(struct binder_proc *proc,
3457 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003458 binder_uintptr_t binder_buffer, size_t size,
3459 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003460{
3461 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003462 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003463 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003464 void __user *ptr = buffer + *consumed;
3465 void __user *end = buffer + size;
3466
Todd Kjos858b8da2017-04-21 17:35:12 -07003467 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003468 int ret;
3469
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003470 if (get_user(cmd, (uint32_t __user *)ptr))
3471 return -EFAULT;
3472 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003473 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003474 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003475 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3476 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3477 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003478 }
3479 switch (cmd) {
3480 case BC_INCREFS:
3481 case BC_ACQUIRE:
3482 case BC_RELEASE:
3483 case BC_DECREFS: {
3484 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003485 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003486 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3487 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3488 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003489
3490 if (get_user(target, (uint32_t __user *)ptr))
3491 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003492
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003493 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003494 ret = -1;
3495 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003496 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003497 mutex_lock(&context->context_mgr_node_lock);
3498 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003499 if (ctx_mgr_node)
3500 ret = binder_inc_ref_for_node(
3501 proc, ctx_mgr_node,
3502 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003503 mutex_unlock(&context->context_mgr_node_lock);
3504 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003505 if (ret)
3506 ret = binder_update_ref_for_handle(
3507 proc, target, increment, strong,
3508 &rdata);
3509 if (!ret && rdata.desc != target) {
3510 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3511 proc->pid, thread->pid,
3512 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003513 }
3514 switch (cmd) {
3515 case BC_INCREFS:
3516 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003517 break;
3518 case BC_ACQUIRE:
3519 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003520 break;
3521 case BC_RELEASE:
3522 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003523 break;
3524 case BC_DECREFS:
3525 default:
3526 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003527 break;
3528 }
3529 if (ret) {
3530 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3531 proc->pid, thread->pid, debug_string,
3532 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003533 break;
3534 }
3535 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003536 "%d:%d %s ref %d desc %d s %d w %d\n",
3537 proc->pid, thread->pid, debug_string,
3538 rdata.debug_id, rdata.desc, rdata.strong,
3539 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003540 break;
3541 }
3542 case BC_INCREFS_DONE:
3543 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003544 binder_uintptr_t node_ptr;
3545 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003546 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003547 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003548
Arve Hjønnevågda498892014-02-21 14:40:26 -08003549 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003550 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003551 ptr += sizeof(binder_uintptr_t);
3552 if (get_user(cookie, (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);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003555 node = binder_get_node(proc, node_ptr);
3556 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003557 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003558 proc->pid, thread->pid,
3559 cmd == BC_INCREFS_DONE ?
3560 "BC_INCREFS_DONE" :
3561 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003562 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003563 break;
3564 }
3565 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003566 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003567 proc->pid, thread->pid,
3568 cmd == BC_INCREFS_DONE ?
3569 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003570 (u64)node_ptr, node->debug_id,
3571 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003572 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003573 break;
3574 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003575 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003576 if (cmd == BC_ACQUIRE_DONE) {
3577 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303578 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003579 proc->pid, thread->pid,
3580 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003581 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003582 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003583 break;
3584 }
3585 node->pending_strong_ref = 0;
3586 } else {
3587 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303588 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003589 proc->pid, thread->pid,
3590 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003591 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003592 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003593 break;
3594 }
3595 node->pending_weak_ref = 0;
3596 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003597 free_node = binder_dec_node_nilocked(node,
3598 cmd == BC_ACQUIRE_DONE, 0);
3599 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003600 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003601 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003602 proc->pid, thread->pid,
3603 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003604 node->debug_id, node->local_strong_refs,
3605 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003606 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003607 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003608 break;
3609 }
3610 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303611 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003612 return -EINVAL;
3613 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303614 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003615 return -EINVAL;
3616
3617 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003618 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003619 struct binder_buffer *buffer;
3620
Arve Hjønnevågda498892014-02-21 14:40:26 -08003621 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003622 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003623 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003624
Todd Kjos076072a2017-04-21 14:32:11 -07003625 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3626 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003627 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003628 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3629 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003630 break;
3631 }
3632 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003633 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3634 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003635 break;
3636 }
3637 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003638 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3639 proc->pid, thread->pid, (u64)data_ptr,
3640 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003641 buffer->transaction ? "active" : "finished");
3642
3643 if (buffer->transaction) {
3644 buffer->transaction->buffer = NULL;
3645 buffer->transaction = NULL;
3646 }
3647 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003648 struct binder_node *buf_node;
3649 struct binder_work *w;
3650
3651 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003652 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003653 BUG_ON(!buf_node->has_async_transaction);
3654 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003655 w = binder_dequeue_work_head_ilocked(
3656 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003657 if (!w) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003658 buf_node->has_async_transaction = 0;
Martijn Coenen4501c042017-08-10 13:56:16 +02003659 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003660 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003661 w, &proc->todo);
3662 binder_wakeup_proc_ilocked(proc);
3663 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003664 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003665 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003666 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003667 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07003668 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003669 break;
3670 }
3671
Martijn Coenen5a6da532016-09-30 14:10:07 +02003672 case BC_TRANSACTION_SG:
3673 case BC_REPLY_SG: {
3674 struct binder_transaction_data_sg tr;
3675
3676 if (copy_from_user(&tr, ptr, sizeof(tr)))
3677 return -EFAULT;
3678 ptr += sizeof(tr);
3679 binder_transaction(proc, thread, &tr.transaction_data,
3680 cmd == BC_REPLY_SG, tr.buffers_size);
3681 break;
3682 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003683 case BC_TRANSACTION:
3684 case BC_REPLY: {
3685 struct binder_transaction_data tr;
3686
3687 if (copy_from_user(&tr, ptr, sizeof(tr)))
3688 return -EFAULT;
3689 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003690 binder_transaction(proc, thread, &tr,
3691 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003692 break;
3693 }
3694
3695 case BC_REGISTER_LOOPER:
3696 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303697 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003698 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003699 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003700 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3701 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303702 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003703 proc->pid, thread->pid);
3704 } else if (proc->requested_threads == 0) {
3705 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303706 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003707 proc->pid, thread->pid);
3708 } else {
3709 proc->requested_threads--;
3710 proc->requested_threads_started++;
3711 }
3712 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003713 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003714 break;
3715 case BC_ENTER_LOOPER:
3716 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303717 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003718 proc->pid, thread->pid);
3719 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3720 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303721 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003722 proc->pid, thread->pid);
3723 }
3724 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3725 break;
3726 case BC_EXIT_LOOPER:
3727 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303728 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003729 proc->pid, thread->pid);
3730 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3731 break;
3732
3733 case BC_REQUEST_DEATH_NOTIFICATION:
3734 case BC_CLEAR_DEATH_NOTIFICATION: {
3735 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003736 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003737 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003738 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003739
3740 if (get_user(target, (uint32_t __user *)ptr))
3741 return -EFAULT;
3742 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003743 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003744 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003745 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003746 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3747 /*
3748 * Allocate memory for death notification
3749 * before taking lock
3750 */
3751 death = kzalloc(sizeof(*death), GFP_KERNEL);
3752 if (death == NULL) {
3753 WARN_ON(thread->return_error.cmd !=
3754 BR_OK);
3755 thread->return_error.cmd = BR_ERROR;
Martijn Coenen1af61802017-10-19 15:04:46 +02003756 binder_enqueue_thread_work(
3757 thread,
3758 &thread->return_error.work);
Todd Kjos5346bf32016-10-20 16:43:34 -07003759 binder_debug(
3760 BINDER_DEBUG_FAILED_TRANSACTION,
3761 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3762 proc->pid, thread->pid);
3763 break;
3764 }
3765 }
3766 binder_proc_lock(proc);
3767 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003768 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303769 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003770 proc->pid, thread->pid,
3771 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3772 "BC_REQUEST_DEATH_NOTIFICATION" :
3773 "BC_CLEAR_DEATH_NOTIFICATION",
3774 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003775 binder_proc_unlock(proc);
3776 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003777 break;
3778 }
3779
3780 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003781 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003782 proc->pid, thread->pid,
3783 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3784 "BC_REQUEST_DEATH_NOTIFICATION" :
3785 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003786 (u64)cookie, ref->data.debug_id,
3787 ref->data.desc, ref->data.strong,
3788 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003789
Martijn Coenenf9eac642017-05-22 11:26:23 -07003790 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003791 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3792 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303793 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003794 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003795 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003796 binder_proc_unlock(proc);
3797 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003798 break;
3799 }
3800 binder_stats_created(BINDER_STAT_DEATH);
3801 INIT_LIST_HEAD(&death->work.entry);
3802 death->cookie = cookie;
3803 ref->death = death;
3804 if (ref->node->proc == NULL) {
3805 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02003806
3807 binder_inner_proc_lock(proc);
3808 binder_enqueue_work_ilocked(
3809 &ref->death->work, &proc->todo);
3810 binder_wakeup_proc_ilocked(proc);
3811 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003812 }
3813 } else {
3814 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303815 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003816 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003817 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003818 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003819 break;
3820 }
3821 death = ref->death;
3822 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003823 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003824 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003825 (u64)death->cookie,
3826 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003827 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003828 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003829 break;
3830 }
3831 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003832 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003833 if (list_empty(&death->work.entry)) {
3834 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003835 if (thread->looper &
3836 (BINDER_LOOPER_STATE_REGISTERED |
3837 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02003838 binder_enqueue_thread_work_ilocked(
3839 thread,
3840 &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003841 else {
3842 binder_enqueue_work_ilocked(
3843 &death->work,
3844 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003845 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07003846 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003847 }
3848 } else {
3849 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3850 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3851 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003852 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003853 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07003854 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003855 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003856 } break;
3857 case BC_DEAD_BINDER_DONE: {
3858 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003859 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003860 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003861
Arve Hjønnevågda498892014-02-21 14:40:26 -08003862 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003863 return -EFAULT;
3864
Lisa Du7a64cd82016-02-17 09:32:52 +08003865 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003866 binder_inner_proc_lock(proc);
3867 list_for_each_entry(w, &proc->delivered_death,
3868 entry) {
3869 struct binder_ref_death *tmp_death =
3870 container_of(w,
3871 struct binder_ref_death,
3872 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003873
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003874 if (tmp_death->cookie == cookie) {
3875 death = tmp_death;
3876 break;
3877 }
3878 }
3879 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003880 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3881 proc->pid, thread->pid, (u64)cookie,
3882 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003883 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003884 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3885 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003886 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003887 break;
3888 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003889 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003890 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3891 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003892 if (thread->looper &
3893 (BINDER_LOOPER_STATE_REGISTERED |
3894 BINDER_LOOPER_STATE_ENTERED))
Martijn Coenen1af61802017-10-19 15:04:46 +02003895 binder_enqueue_thread_work_ilocked(
3896 thread, &death->work);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003897 else {
3898 binder_enqueue_work_ilocked(
3899 &death->work,
3900 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07003901 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003902 }
3903 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003904 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003905 } break;
3906
3907 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303908 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003909 proc->pid, thread->pid, cmd);
3910 return -EINVAL;
3911 }
3912 *consumed = ptr - buffer;
3913 }
3914 return 0;
3915}
3916
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003917static void binder_stat_br(struct binder_proc *proc,
3918 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003919{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003920 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003921 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003922 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3923 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3924 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003925 }
3926}
3927
Todd Kjos60792612017-05-24 10:51:01 -07003928static int binder_put_node_cmd(struct binder_proc *proc,
3929 struct binder_thread *thread,
3930 void __user **ptrp,
3931 binder_uintptr_t node_ptr,
3932 binder_uintptr_t node_cookie,
3933 int node_debug_id,
3934 uint32_t cmd, const char *cmd_name)
3935{
3936 void __user *ptr = *ptrp;
3937
3938 if (put_user(cmd, (uint32_t __user *)ptr))
3939 return -EFAULT;
3940 ptr += sizeof(uint32_t);
3941
3942 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3943 return -EFAULT;
3944 ptr += sizeof(binder_uintptr_t);
3945
3946 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3947 return -EFAULT;
3948 ptr += sizeof(binder_uintptr_t);
3949
3950 binder_stat_br(proc, thread, cmd);
3951 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3952 proc->pid, thread->pid, cmd_name, node_debug_id,
3953 (u64)node_ptr, (u64)node_cookie);
3954
3955 *ptrp = ptr;
3956 return 0;
3957}
3958
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003959static int binder_wait_for_work(struct binder_thread *thread,
3960 bool do_proc_work)
3961{
3962 DEFINE_WAIT(wait);
3963 struct binder_proc *proc = thread->proc;
3964 int ret = 0;
3965
3966 freezer_do_not_count();
3967 binder_inner_proc_lock(proc);
3968 for (;;) {
3969 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3970 if (binder_has_work_ilocked(thread, do_proc_work))
3971 break;
3972 if (do_proc_work)
3973 list_add(&thread->waiting_thread_node,
3974 &proc->waiting_threads);
3975 binder_inner_proc_unlock(proc);
3976 schedule();
3977 binder_inner_proc_lock(proc);
3978 list_del_init(&thread->waiting_thread_node);
3979 if (signal_pending(current)) {
3980 ret = -ERESTARTSYS;
3981 break;
3982 }
3983 }
3984 finish_wait(&thread->wait, &wait);
3985 binder_inner_proc_unlock(proc);
3986 freezer_count();
3987
3988 return ret;
3989}
3990
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003991static int binder_thread_read(struct binder_proc *proc,
3992 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003993 binder_uintptr_t binder_buffer, size_t size,
3994 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003995{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003996 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003997 void __user *ptr = buffer + *consumed;
3998 void __user *end = buffer + size;
3999
4000 int ret = 0;
4001 int wait_for_proc_work;
4002
4003 if (*consumed == 0) {
4004 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
4005 return -EFAULT;
4006 ptr += sizeof(uint32_t);
4007 }
4008
4009retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07004010 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004011 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07004012 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004013
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004014 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004015
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004016 trace_binder_wait_for_work(wait_for_proc_work,
4017 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004018 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004019 if (wait_for_proc_work) {
4020 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4021 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05304022 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 +09004023 proc->pid, thread->pid, thread->looper);
4024 wait_event_interruptible(binder_user_error_wait,
4025 binder_stop_on_user_error < 2);
4026 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07004027 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004028 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004029
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004030 if (non_block) {
4031 if (!binder_has_work(thread, wait_for_proc_work))
4032 ret = -EAGAIN;
4033 } else {
4034 ret = binder_wait_for_work(thread, wait_for_proc_work);
4035 }
4036
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004037 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
4038
4039 if (ret)
4040 return ret;
4041
4042 while (1) {
4043 uint32_t cmd;
4044 struct binder_transaction_data tr;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004045 struct binder_work *w = NULL;
4046 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004047 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07004048 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004049
Todd Kjose7f23ed2017-03-21 13:06:01 -07004050 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004051 if (!binder_worklist_empty_ilocked(&thread->todo))
4052 list = &thread->todo;
4053 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
4054 wait_for_proc_work)
4055 list = &proc->todo;
4056 else {
4057 binder_inner_proc_unlock(proc);
4058
Dmitry Voytik395262a2014-09-08 18:16:34 +04004059 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08004060 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004061 goto retry;
4062 break;
4063 }
4064
Todd Kjose7f23ed2017-03-21 13:06:01 -07004065 if (end - ptr < sizeof(tr) + 4) {
4066 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004067 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004068 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004069 w = binder_dequeue_work_head_ilocked(list);
Martijn Coenen1af61802017-10-19 15:04:46 +02004070 if (binder_worklist_empty_ilocked(&thread->todo))
4071 thread->process_todo = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004072
4073 switch (w->type) {
4074 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004075 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004076 t = container_of(w, struct binder_transaction, work);
4077 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004078 case BINDER_WORK_RETURN_ERROR: {
4079 struct binder_error *e = container_of(
4080 w, struct binder_error, work);
4081
4082 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004083 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07004084 if (put_user(e->cmd, (uint32_t __user *)ptr))
4085 return -EFAULT;
4086 e->cmd = BR_OK;
4087 ptr += sizeof(uint32_t);
4088
4089 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07004090 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004091 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004092 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004093 cmd = BR_TRANSACTION_COMPLETE;
4094 if (put_user(cmd, (uint32_t __user *)ptr))
4095 return -EFAULT;
4096 ptr += sizeof(uint32_t);
4097
4098 binder_stat_br(proc, thread, cmd);
4099 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304100 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004101 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004102 kfree(w);
4103 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4104 } break;
4105 case BINDER_WORK_NODE: {
4106 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07004107 int strong, weak;
4108 binder_uintptr_t node_ptr = node->ptr;
4109 binder_uintptr_t node_cookie = node->cookie;
4110 int node_debug_id = node->debug_id;
4111 int has_weak_ref;
4112 int has_strong_ref;
4113 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004114
Todd Kjos60792612017-05-24 10:51:01 -07004115 BUG_ON(proc != node->proc);
4116 strong = node->internal_strong_refs ||
4117 node->local_strong_refs;
4118 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07004119 node->local_weak_refs ||
4120 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004121 has_strong_ref = node->has_strong_ref;
4122 has_weak_ref = node->has_weak_ref;
4123
4124 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004125 node->has_weak_ref = 1;
4126 node->pending_weak_ref = 1;
4127 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004128 }
4129 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004130 node->has_strong_ref = 1;
4131 node->pending_strong_ref = 1;
4132 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004133 }
4134 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004135 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004136 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004137 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004138 if (!weak && !strong) {
4139 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4140 "%d:%d node %d u%016llx c%016llx deleted\n",
4141 proc->pid, thread->pid,
4142 node_debug_id,
4143 (u64)node_ptr,
4144 (u64)node_cookie);
4145 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004146 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004147 binder_node_lock(node);
4148 /*
4149 * Acquire the node lock before freeing the
4150 * node to serialize with other threads that
4151 * may have been holding the node lock while
4152 * decrementing this node (avoids race where
4153 * this thread frees while the other thread
4154 * is unlocking the node after the final
4155 * decrement)
4156 */
4157 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004158 binder_free_node(node);
4159 } else
4160 binder_inner_proc_unlock(proc);
4161
Todd Kjos60792612017-05-24 10:51:01 -07004162 if (weak && !has_weak_ref)
4163 ret = binder_put_node_cmd(
4164 proc, thread, &ptr, node_ptr,
4165 node_cookie, node_debug_id,
4166 BR_INCREFS, "BR_INCREFS");
4167 if (!ret && strong && !has_strong_ref)
4168 ret = binder_put_node_cmd(
4169 proc, thread, &ptr, node_ptr,
4170 node_cookie, node_debug_id,
4171 BR_ACQUIRE, "BR_ACQUIRE");
4172 if (!ret && !strong && has_strong_ref)
4173 ret = binder_put_node_cmd(
4174 proc, thread, &ptr, node_ptr,
4175 node_cookie, node_debug_id,
4176 BR_RELEASE, "BR_RELEASE");
4177 if (!ret && !weak && has_weak_ref)
4178 ret = binder_put_node_cmd(
4179 proc, thread, &ptr, node_ptr,
4180 node_cookie, node_debug_id,
4181 BR_DECREFS, "BR_DECREFS");
4182 if (orig_ptr == ptr)
4183 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4184 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4185 proc->pid, thread->pid,
4186 node_debug_id,
4187 (u64)node_ptr,
4188 (u64)node_cookie);
4189 if (ret)
4190 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004191 } break;
4192 case BINDER_WORK_DEAD_BINDER:
4193 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4194 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4195 struct binder_ref_death *death;
4196 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004197 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004198
4199 death = container_of(w, struct binder_ref_death, work);
4200 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4201 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4202 else
4203 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004204 cookie = death->cookie;
4205
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004206 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004207 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004208 proc->pid, thread->pid,
4209 cmd == BR_DEAD_BINDER ?
4210 "BR_DEAD_BINDER" :
4211 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004212 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004213 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004214 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004215 kfree(death);
4216 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004217 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004218 binder_enqueue_work_ilocked(
4219 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004220 binder_inner_proc_unlock(proc);
4221 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004222 if (put_user(cmd, (uint32_t __user *)ptr))
4223 return -EFAULT;
4224 ptr += sizeof(uint32_t);
4225 if (put_user(cookie,
4226 (binder_uintptr_t __user *)ptr))
4227 return -EFAULT;
4228 ptr += sizeof(binder_uintptr_t);
4229 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004230 if (cmd == BR_DEAD_BINDER)
4231 goto done; /* DEAD_BINDER notifications can cause transactions */
4232 } break;
4233 }
4234
4235 if (!t)
4236 continue;
4237
4238 BUG_ON(t->buffer == NULL);
4239 if (t->buffer->target_node) {
4240 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004241 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004242
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004243 tr.target.ptr = target_node->ptr;
4244 tr.cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004245 node_prio.sched_policy = target_node->sched_policy;
4246 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004247 binder_transaction_priority(current, t, node_prio,
4248 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004249 cmd = BR_TRANSACTION;
4250 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004251 tr.target.ptr = 0;
4252 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004253 cmd = BR_REPLY;
4254 }
4255 tr.code = t->code;
4256 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06004257 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004258
Todd Kjos2f993e22017-05-12 14:42:55 -07004259 t_from = binder_get_txn_from(t);
4260 if (t_from) {
4261 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004262
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004263 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004264 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004265 } else {
4266 tr.sender_pid = 0;
4267 }
4268
4269 tr.data_size = t->buffer->data_size;
4270 tr.offsets_size = t->buffer->offsets_size;
Todd Kjosd325d372016-10-10 10:40:53 -07004271 tr.data.ptr.buffer = (binder_uintptr_t)
4272 ((uintptr_t)t->buffer->data +
4273 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004274 tr.data.ptr.offsets = tr.data.ptr.buffer +
4275 ALIGN(t->buffer->data_size,
4276 sizeof(void *));
4277
Todd Kjos2f993e22017-05-12 14:42:55 -07004278 if (put_user(cmd, (uint32_t __user *)ptr)) {
4279 if (t_from)
4280 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004281
4282 binder_cleanup_transaction(t, "put_user failed",
4283 BR_FAILED_REPLY);
4284
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004285 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004286 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004287 ptr += sizeof(uint32_t);
Todd Kjos2f993e22017-05-12 14:42:55 -07004288 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4289 if (t_from)
4290 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004291
4292 binder_cleanup_transaction(t, "copy_to_user failed",
4293 BR_FAILED_REPLY);
4294
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004295 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004296 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004297 ptr += sizeof(tr);
4298
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004299 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004300 binder_stat_br(proc, thread, cmd);
4301 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004302 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004303 proc->pid, thread->pid,
4304 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4305 "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004306 t->debug_id, t_from ? t_from->proc->pid : 0,
4307 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004308 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004309 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004310
Todd Kjos2f993e22017-05-12 14:42:55 -07004311 if (t_from)
4312 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004313 t->buffer->allow_user_free = 1;
4314 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004315 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004316 t->to_parent = thread->transaction_stack;
4317 t->to_thread = thread;
4318 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004319 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004320 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004321 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004322 }
4323 break;
4324 }
4325
4326done:
4327
4328 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004329 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004330 if (proc->requested_threads == 0 &&
4331 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004332 proc->requested_threads_started < proc->max_threads &&
4333 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4334 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4335 /*spawn a new thread if we leave this out */) {
4336 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004337 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004338 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304339 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004340 proc->pid, thread->pid);
4341 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4342 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004343 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004344 } else
4345 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004346 return 0;
4347}
4348
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004349static void binder_release_work(struct binder_proc *proc,
4350 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004351{
4352 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004353
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004354 while (1) {
4355 w = binder_dequeue_work_head(proc, list);
4356 if (!w)
4357 return;
4358
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004359 switch (w->type) {
4360 case BINDER_WORK_TRANSACTION: {
4361 struct binder_transaction *t;
4362
4363 t = container_of(w, struct binder_transaction, work);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004364
4365 binder_cleanup_transaction(t, "process died.",
4366 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004367 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004368 case BINDER_WORK_RETURN_ERROR: {
4369 struct binder_error *e = container_of(
4370 w, struct binder_error, work);
4371
4372 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4373 "undelivered TRANSACTION_ERROR: %u\n",
4374 e->cmd);
4375 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004376 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004377 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304378 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004379 kfree(w);
4380 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4381 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004382 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4383 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4384 struct binder_ref_death *death;
4385
4386 death = container_of(w, struct binder_ref_death, work);
4387 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004388 "undelivered death notification, %016llx\n",
4389 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004390 kfree(death);
4391 binder_stats_deleted(BINDER_STAT_DEATH);
4392 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004393 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304394 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004395 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004396 break;
4397 }
4398 }
4399
4400}
4401
Todd Kjosb4827902017-05-25 15:52:17 -07004402static struct binder_thread *binder_get_thread_ilocked(
4403 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004404{
4405 struct binder_thread *thread = NULL;
4406 struct rb_node *parent = NULL;
4407 struct rb_node **p = &proc->threads.rb_node;
4408
4409 while (*p) {
4410 parent = *p;
4411 thread = rb_entry(parent, struct binder_thread, rb_node);
4412
4413 if (current->pid < thread->pid)
4414 p = &(*p)->rb_left;
4415 else if (current->pid > thread->pid)
4416 p = &(*p)->rb_right;
4417 else
Todd Kjosb4827902017-05-25 15:52:17 -07004418 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004419 }
Todd Kjosb4827902017-05-25 15:52:17 -07004420 if (!new_thread)
4421 return NULL;
4422 thread = new_thread;
4423 binder_stats_created(BINDER_STAT_THREAD);
4424 thread->proc = proc;
4425 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004426 get_task_struct(current);
4427 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004428 atomic_set(&thread->tmp_ref, 0);
4429 init_waitqueue_head(&thread->wait);
4430 INIT_LIST_HEAD(&thread->todo);
4431 rb_link_node(&thread->rb_node, parent, p);
4432 rb_insert_color(&thread->rb_node, &proc->threads);
4433 thread->looper_need_return = true;
4434 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4435 thread->return_error.cmd = BR_OK;
4436 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4437 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004438 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004439 return thread;
4440}
4441
4442static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4443{
4444 struct binder_thread *thread;
4445 struct binder_thread *new_thread;
4446
4447 binder_inner_proc_lock(proc);
4448 thread = binder_get_thread_ilocked(proc, NULL);
4449 binder_inner_proc_unlock(proc);
4450 if (!thread) {
4451 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4452 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004453 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004454 binder_inner_proc_lock(proc);
4455 thread = binder_get_thread_ilocked(proc, new_thread);
4456 binder_inner_proc_unlock(proc);
4457 if (thread != new_thread)
4458 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004459 }
4460 return thread;
4461}
4462
Todd Kjos2f993e22017-05-12 14:42:55 -07004463static void binder_free_proc(struct binder_proc *proc)
4464{
4465 BUG_ON(!list_empty(&proc->todo));
4466 BUG_ON(!list_empty(&proc->delivered_death));
4467 binder_alloc_deferred_release(&proc->alloc);
4468 put_task_struct(proc->tsk);
4469 binder_stats_deleted(BINDER_STAT_PROC);
4470 kfree(proc);
4471}
4472
4473static void binder_free_thread(struct binder_thread *thread)
4474{
4475 BUG_ON(!list_empty(&thread->todo));
4476 binder_stats_deleted(BINDER_STAT_THREAD);
4477 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004478 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004479 kfree(thread);
4480}
4481
4482static int binder_thread_release(struct binder_proc *proc,
4483 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004484{
4485 struct binder_transaction *t;
4486 struct binder_transaction *send_reply = NULL;
4487 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004488 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004489
Todd Kjosb4827902017-05-25 15:52:17 -07004490 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004491 /*
4492 * take a ref on the proc so it survives
4493 * after we remove this thread from proc->threads.
4494 * The corresponding dec is when we actually
4495 * free the thread in binder_free_thread()
4496 */
4497 proc->tmp_ref++;
4498 /*
4499 * take a ref on this thread to ensure it
4500 * survives while we are releasing it
4501 */
4502 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004503 rb_erase(&thread->rb_node, &proc->threads);
4504 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004505 if (t) {
4506 spin_lock(&t->lock);
4507 if (t->to_thread == thread)
4508 send_reply = t;
4509 }
4510 thread->is_dead = true;
4511
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004512 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004513 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004514 active_transactions++;
4515 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304516 "release %d:%d transaction %d %s, still active\n",
4517 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004518 t->debug_id,
4519 (t->to_thread == thread) ? "in" : "out");
4520
4521 if (t->to_thread == thread) {
4522 t->to_proc = NULL;
4523 t->to_thread = NULL;
4524 if (t->buffer) {
4525 t->buffer->transaction = NULL;
4526 t->buffer = NULL;
4527 }
4528 t = t->to_parent;
4529 } else if (t->from == thread) {
4530 t->from = NULL;
4531 t = t->from_parent;
4532 } else
4533 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004534 spin_unlock(&last_t->lock);
4535 if (t)
4536 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004537 }
Martijn Coenen550c01d2018-01-05 11:27:07 +01004538
4539 /*
4540 * If this thread used poll, make sure we remove the waitqueue
4541 * from any epoll data structures holding it with POLLFREE.
4542 * waitqueue_active() is safe to use here because we're holding
4543 * the inner lock.
4544 */
4545 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
4546 waitqueue_active(&thread->wait)) {
4547 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
4548 }
4549
Todd Kjosb4827902017-05-25 15:52:17 -07004550 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004551
Martijn Coenen72766d72018-02-16 09:47:15 +01004552 /*
4553 * This is needed to avoid races between wake_up_poll() above and
4554 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
4555 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
4556 * lock, so we can be sure it's done after calling synchronize_rcu().
4557 */
4558 if (thread->looper & BINDER_LOOPER_STATE_POLL)
4559 synchronize_rcu();
4560
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004561 if (send_reply)
4562 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004563 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004564 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004565 return active_transactions;
4566}
4567
4568static unsigned int binder_poll(struct file *filp,
4569 struct poll_table_struct *wait)
4570{
4571 struct binder_proc *proc = filp->private_data;
4572 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004573 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004574
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004575 thread = binder_get_thread(proc);
Eric Biggers4be5a282018-01-30 23:11:24 -08004576 if (!thread)
4577 return POLLERR;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004578
Martijn Coenen995a36e2017-06-02 13:36:52 -07004579 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004580 thread->looper |= BINDER_LOOPER_STATE_POLL;
4581 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4582
Martijn Coenen995a36e2017-06-02 13:36:52 -07004583 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004584
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004585 poll_wait(filp, &thread->wait, wait);
4586
Martijn Coenen47810932017-08-10 12:32:00 +02004587 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004588 return POLLIN;
4589
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004590 return 0;
4591}
4592
Tair Rzayev78260ac2014-06-03 22:27:21 +03004593static int binder_ioctl_write_read(struct file *filp,
4594 unsigned int cmd, unsigned long arg,
4595 struct binder_thread *thread)
4596{
4597 int ret = 0;
4598 struct binder_proc *proc = filp->private_data;
4599 unsigned int size = _IOC_SIZE(cmd);
4600 void __user *ubuf = (void __user *)arg;
4601 struct binder_write_read bwr;
4602
4603 if (size != sizeof(struct binder_write_read)) {
4604 ret = -EINVAL;
4605 goto out;
4606 }
4607 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4608 ret = -EFAULT;
4609 goto out;
4610 }
4611 binder_debug(BINDER_DEBUG_READ_WRITE,
4612 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4613 proc->pid, thread->pid,
4614 (u64)bwr.write_size, (u64)bwr.write_buffer,
4615 (u64)bwr.read_size, (u64)bwr.read_buffer);
4616
4617 if (bwr.write_size > 0) {
4618 ret = binder_thread_write(proc, thread,
4619 bwr.write_buffer,
4620 bwr.write_size,
4621 &bwr.write_consumed);
4622 trace_binder_write_done(ret);
4623 if (ret < 0) {
4624 bwr.read_consumed = 0;
4625 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4626 ret = -EFAULT;
4627 goto out;
4628 }
4629 }
4630 if (bwr.read_size > 0) {
4631 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4632 bwr.read_size,
4633 &bwr.read_consumed,
4634 filp->f_flags & O_NONBLOCK);
4635 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004636 binder_inner_proc_lock(proc);
4637 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004638 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004639 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004640 if (ret < 0) {
4641 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4642 ret = -EFAULT;
4643 goto out;
4644 }
4645 }
4646 binder_debug(BINDER_DEBUG_READ_WRITE,
4647 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4648 proc->pid, thread->pid,
4649 (u64)bwr.write_consumed, (u64)bwr.write_size,
4650 (u64)bwr.read_consumed, (u64)bwr.read_size);
4651 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4652 ret = -EFAULT;
4653 goto out;
4654 }
4655out:
4656 return ret;
4657}
4658
4659static int binder_ioctl_set_ctx_mgr(struct file *filp)
4660{
4661 int ret = 0;
4662 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004663 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004664 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004665 kuid_t curr_euid = current_euid();
4666
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004667 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004668 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004669 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4670 ret = -EBUSY;
4671 goto out;
4672 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004673 ret = security_binder_set_context_mgr(proc->tsk);
4674 if (ret < 0)
4675 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004676 if (uid_valid(context->binder_context_mgr_uid)) {
4677 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004678 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4679 from_kuid(&init_user_ns, curr_euid),
4680 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004681 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004682 ret = -EPERM;
4683 goto out;
4684 }
4685 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004686 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004687 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004688 new_node = binder_new_node(proc, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004689 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004690 ret = -ENOMEM;
4691 goto out;
4692 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004693 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004694 new_node->local_weak_refs++;
4695 new_node->local_strong_refs++;
4696 new_node->has_strong_ref = 1;
4697 new_node->has_weak_ref = 1;
4698 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004699 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004700 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004701out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004702 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004703 return ret;
4704}
4705
Colin Cross833babb32017-06-20 13:54:44 -07004706static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4707 struct binder_node_debug_info *info) {
4708 struct rb_node *n;
4709 binder_uintptr_t ptr = info->ptr;
4710
4711 memset(info, 0, sizeof(*info));
4712
4713 binder_inner_proc_lock(proc);
4714 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4715 struct binder_node *node = rb_entry(n, struct binder_node,
4716 rb_node);
4717 if (node->ptr > ptr) {
4718 info->ptr = node->ptr;
4719 info->cookie = node->cookie;
4720 info->has_strong_ref = node->has_strong_ref;
4721 info->has_weak_ref = node->has_weak_ref;
4722 break;
4723 }
4724 }
4725 binder_inner_proc_unlock(proc);
4726
4727 return 0;
4728}
4729
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004730static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4731{
4732 int ret;
4733 struct binder_proc *proc = filp->private_data;
4734 struct binder_thread *thread;
4735 unsigned int size = _IOC_SIZE(cmd);
4736 void __user *ubuf = (void __user *)arg;
4737
Tair Rzayev78260ac2014-06-03 22:27:21 +03004738 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4739 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004740
Sherry Yang435416b2017-06-22 14:37:45 -07004741 binder_selftest_alloc(&proc->alloc);
4742
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004743 trace_binder_ioctl(cmd, arg);
4744
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004745 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4746 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004747 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004748
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004749 thread = binder_get_thread(proc);
4750 if (thread == NULL) {
4751 ret = -ENOMEM;
4752 goto err;
4753 }
4754
4755 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004756 case BINDER_WRITE_READ:
4757 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4758 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004759 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004760 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004761 case BINDER_SET_MAX_THREADS: {
4762 int max_threads;
4763
4764 if (copy_from_user(&max_threads, ubuf,
4765 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004766 ret = -EINVAL;
4767 goto err;
4768 }
Todd Kjosd600e902017-05-25 17:35:02 -07004769 binder_inner_proc_lock(proc);
4770 proc->max_threads = max_threads;
4771 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004772 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004773 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004774 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004775 ret = binder_ioctl_set_ctx_mgr(filp);
4776 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004777 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004778 break;
4779 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304780 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004781 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07004782 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004783 thread = NULL;
4784 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004785 case BINDER_VERSION: {
4786 struct binder_version __user *ver = ubuf;
4787
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004788 if (size != sizeof(struct binder_version)) {
4789 ret = -EINVAL;
4790 goto err;
4791 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004792 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4793 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004794 ret = -EINVAL;
4795 goto err;
4796 }
4797 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004798 }
Colin Cross833babb32017-06-20 13:54:44 -07004799 case BINDER_GET_NODE_DEBUG_INFO: {
4800 struct binder_node_debug_info info;
4801
4802 if (copy_from_user(&info, ubuf, sizeof(info))) {
4803 ret = -EFAULT;
4804 goto err;
4805 }
4806
4807 ret = binder_ioctl_get_node_debug_info(proc, &info);
4808 if (ret < 0)
4809 goto err;
4810
4811 if (copy_to_user(ubuf, &info, sizeof(info))) {
4812 ret = -EFAULT;
4813 goto err;
4814 }
4815 break;
4816 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004817 default:
4818 ret = -EINVAL;
4819 goto err;
4820 }
4821 ret = 0;
4822err:
4823 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08004824 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004825 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4826 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304827 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 -07004828err_unlocked:
4829 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004830 return ret;
4831}
4832
4833static void binder_vma_open(struct vm_area_struct *vma)
4834{
4835 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004836
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004837 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304838 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004839 proc->pid, vma->vm_start, vma->vm_end,
4840 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4841 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004842}
4843
4844static void binder_vma_close(struct vm_area_struct *vma)
4845{
4846 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004847
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004848 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304849 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004850 proc->pid, vma->vm_start, vma->vm_end,
4851 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4852 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07004853 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004854}
4855
Vinayak Menonddac7d52014-06-02 18:17:59 +05304856static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4857{
4858 return VM_FAULT_SIGBUS;
4859}
4860
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004861static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004862 .open = binder_vma_open,
4863 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304864 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004865};
4866
Todd Kjosd325d372016-10-10 10:40:53 -07004867static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4868{
4869 int ret;
4870 struct binder_proc *proc = filp->private_data;
4871 const char *failure_string;
4872
4873 if (proc->tsk != current->group_leader)
4874 return -EINVAL;
4875
4876 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4877 vma->vm_end = vma->vm_start + SZ_4M;
4878
4879 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4880 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4881 __func__, proc->pid, vma->vm_start, vma->vm_end,
4882 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4883 (unsigned long)pgprot_val(vma->vm_page_prot));
4884
4885 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4886 ret = -EPERM;
4887 failure_string = "bad vm_flags";
4888 goto err_bad_arg;
4889 }
4890 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4891 vma->vm_ops = &binder_vm_ops;
4892 vma->vm_private_data = proc;
4893
4894 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
Todd Kjosf09daf12017-11-10 15:30:27 -08004895
4896 return ret;
Todd Kjosd325d372016-10-10 10:40:53 -07004897
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004898err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004899 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004900 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4901 return ret;
4902}
4903
4904static int binder_open(struct inode *nodp, struct file *filp)
4905{
4906 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004907 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004908
4909 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4910 current->group_leader->pid, current->pid);
4911
4912 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4913 if (proc == NULL)
4914 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07004915 spin_lock_init(&proc->inner_lock);
4916 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01004917 get_task_struct(current->group_leader);
4918 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004919 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07004920 if (binder_supported_policy(current->policy)) {
4921 proc->default_priority.sched_policy = current->policy;
4922 proc->default_priority.prio = current->normal_prio;
4923 } else {
4924 proc->default_priority.sched_policy = SCHED_NORMAL;
4925 proc->default_priority.prio = NICE_TO_PRIO(0);
4926 }
4927
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004928 binder_dev = container_of(filp->private_data, struct binder_device,
4929 miscdev);
4930 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07004931 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004932
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004933 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004934 proc->pid = current->group_leader->pid;
4935 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004936 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004937 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004938
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004939 mutex_lock(&binder_procs_lock);
4940 hlist_add_head(&proc->proc_node, &binder_procs);
4941 mutex_unlock(&binder_procs_lock);
4942
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004943 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004944 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004945
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004946 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004947 /*
4948 * proc debug entries are shared between contexts, so
4949 * this will fail if the process tries to open the driver
4950 * again with a different context. The priting code will
4951 * anyway print all contexts that a given PID has, so this
4952 * is not a problem.
4953 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004954 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004955 binder_debugfs_dir_entry_proc,
4956 (void *)(unsigned long)proc->pid,
4957 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004958 }
4959
4960 return 0;
4961}
4962
4963static int binder_flush(struct file *filp, fl_owner_t id)
4964{
4965 struct binder_proc *proc = filp->private_data;
4966
4967 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4968
4969 return 0;
4970}
4971
4972static void binder_deferred_flush(struct binder_proc *proc)
4973{
4974 struct rb_node *n;
4975 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004976
Todd Kjosb4827902017-05-25 15:52:17 -07004977 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004978 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4979 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004980
Todd Kjos6798e6d2017-01-06 14:19:25 -08004981 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004982 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4983 wake_up_interruptible(&thread->wait);
4984 wake_count++;
4985 }
4986 }
Todd Kjosb4827902017-05-25 15:52:17 -07004987 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004988
4989 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4990 "binder_flush: %d woke %d threads\n", proc->pid,
4991 wake_count);
4992}
4993
4994static int binder_release(struct inode *nodp, struct file *filp)
4995{
4996 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004997
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004998 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004999 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
5000
5001 return 0;
5002}
5003
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005004static int binder_node_release(struct binder_node *node, int refs)
5005{
5006 struct binder_ref *ref;
5007 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005008 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005009
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005010 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005011
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005012 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005013 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005014 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07005015 /*
5016 * The caller must have taken a temporary ref on the node,
5017 */
5018 BUG_ON(!node->tmp_refs);
5019 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07005020 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005021 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07005022 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005023
5024 return refs;
5025 }
5026
5027 node->proc = NULL;
5028 node->local_strong_refs = 0;
5029 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07005030 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005031
5032 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005033 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005034 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005035
5036 hlist_for_each_entry(ref, &node->refs, node_entry) {
5037 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005038 /*
5039 * Need the node lock to synchronize
5040 * with new notification requests and the
5041 * inner lock to synchronize with queued
5042 * death notifications.
5043 */
5044 binder_inner_proc_lock(ref->proc);
5045 if (!ref->death) {
5046 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08005047 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07005048 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005049
5050 death++;
5051
Martijn Coenenf9eac642017-05-22 11:26:23 -07005052 BUG_ON(!list_empty(&ref->death->work.entry));
5053 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
5054 binder_enqueue_work_ilocked(&ref->death->work,
5055 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07005056 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005057 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005058 }
5059
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005060 binder_debug(BINDER_DEBUG_DEAD_BINDER,
5061 "node %d now dead, refs %d, death %d\n",
5062 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005063 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07005064 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005065
5066 return refs;
5067}
5068
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005069static void binder_deferred_release(struct binder_proc *proc)
5070{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005071 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005072 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07005073 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005074
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005075 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005076 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005077 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005078
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005079 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005080 if (context->binder_context_mgr_node &&
5081 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005082 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005083 "%s: %d context_mgr_node gone\n",
5084 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02005085 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005086 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005087 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07005088 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07005089 /*
5090 * Make sure proc stays alive after we
5091 * remove all the threads
5092 */
5093 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005094
Todd Kjos2f993e22017-05-12 14:42:55 -07005095 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005096 threads = 0;
5097 active_transactions = 0;
5098 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005099 struct binder_thread *thread;
5100
5101 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07005102 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005103 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07005104 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07005105 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005106 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005107
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005108 nodes = 0;
5109 incoming_refs = 0;
5110 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005111 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005112
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005113 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005114 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07005115 /*
5116 * take a temporary ref on the node before
5117 * calling binder_node_release() which will either
5118 * kfree() the node or call binder_put_node()
5119 */
Todd Kjos425d23f2017-06-12 12:07:26 -07005120 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005121 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07005122 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005123 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07005124 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005125 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005126 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005127
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005128 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005129 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005130 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005131 struct binder_ref *ref;
5132
5133 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005134 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07005135 binder_cleanup_ref_olocked(ref);
5136 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005137 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07005138 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005139 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005140 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005141
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005142 binder_release_work(proc, &proc->todo);
5143 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005144
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005145 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005146 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005147 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005148 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005149
Todd Kjos2f993e22017-05-12 14:42:55 -07005150 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005151}
5152
5153static void binder_deferred_func(struct work_struct *work)
5154{
5155 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005156 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005157
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005158 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005159 mutex_lock(&binder_deferred_lock);
5160 if (!hlist_empty(&binder_deferred_list)) {
5161 proc = hlist_entry(binder_deferred_list.first,
5162 struct binder_proc, deferred_work_node);
5163 hlist_del_init(&proc->deferred_work_node);
5164 defer = proc->deferred_work;
5165 proc->deferred_work = 0;
5166 } else {
5167 proc = NULL;
5168 defer = 0;
5169 }
5170 mutex_unlock(&binder_deferred_lock);
5171
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005172 if (defer & BINDER_DEFERRED_FLUSH)
5173 binder_deferred_flush(proc);
5174
5175 if (defer & BINDER_DEFERRED_RELEASE)
5176 binder_deferred_release(proc); /* frees proc */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005177 } while (proc);
5178}
5179static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5180
5181static void
5182binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5183{
5184 mutex_lock(&binder_deferred_lock);
5185 proc->deferred_work |= defer;
5186 if (hlist_unhashed(&proc->deferred_work_node)) {
5187 hlist_add_head(&proc->deferred_work_node,
5188 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305189 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005190 }
5191 mutex_unlock(&binder_deferred_lock);
5192}
5193
Todd Kjos6d241a42017-04-21 14:32:11 -07005194static void print_binder_transaction_ilocked(struct seq_file *m,
5195 struct binder_proc *proc,
5196 const char *prefix,
5197 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005198{
Todd Kjos6d241a42017-04-21 14:32:11 -07005199 struct binder_proc *to_proc;
5200 struct binder_buffer *buffer = t->buffer;
5201
Todd Kjos2f993e22017-05-12 14:42:55 -07005202 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005203 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005204 seq_printf(m,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005205 "%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 -07005206 prefix, t->debug_id, t,
5207 t->from ? t->from->proc->pid : 0,
5208 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005209 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005210 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005211 t->code, t->flags, t->priority.sched_policy,
5212 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005213 spin_unlock(&t->lock);
5214
Todd Kjos6d241a42017-04-21 14:32:11 -07005215 if (proc != to_proc) {
5216 /*
5217 * Can only safely deref buffer if we are holding the
5218 * correct proc inner lock for this node
5219 */
5220 seq_puts(m, "\n");
5221 return;
5222 }
5223
5224 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005225 seq_puts(m, " buffer free\n");
5226 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005227 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005228 if (buffer->target_node)
5229 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005230 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005231 buffer->data_size, buffer->offsets_size,
5232 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005233}
5234
Todd Kjos6d241a42017-04-21 14:32:11 -07005235static void print_binder_work_ilocked(struct seq_file *m,
5236 struct binder_proc *proc,
5237 const char *prefix,
5238 const char *transaction_prefix,
5239 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005240{
5241 struct binder_node *node;
5242 struct binder_transaction *t;
5243
5244 switch (w->type) {
5245 case BINDER_WORK_TRANSACTION:
5246 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005247 print_binder_transaction_ilocked(
5248 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005249 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005250 case BINDER_WORK_RETURN_ERROR: {
5251 struct binder_error *e = container_of(
5252 w, struct binder_error, work);
5253
5254 seq_printf(m, "%stransaction error: %u\n",
5255 prefix, e->cmd);
5256 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005257 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005258 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005259 break;
5260 case BINDER_WORK_NODE:
5261 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005262 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5263 prefix, node->debug_id,
5264 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005265 break;
5266 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005267 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005268 break;
5269 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005270 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005271 break;
5272 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005273 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005274 break;
5275 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005276 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005277 break;
5278 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005279}
5280
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005281static void print_binder_thread_ilocked(struct seq_file *m,
5282 struct binder_thread *thread,
5283 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005284{
5285 struct binder_transaction *t;
5286 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005287 size_t start_pos = m->count;
5288 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005289
Todd Kjos2f993e22017-05-12 14:42:55 -07005290 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005291 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005292 thread->looper_need_return,
5293 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005294 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005295 t = thread->transaction_stack;
5296 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005297 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005298 print_binder_transaction_ilocked(m, thread->proc,
5299 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005300 t = t->from_parent;
5301 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005302 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005303 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005304 t = t->to_parent;
5305 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005306 print_binder_transaction_ilocked(m, thread->proc,
5307 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005308 t = NULL;
5309 }
5310 }
5311 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005312 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005313 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005314 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005315 if (!print_always && m->count == header_pos)
5316 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005317}
5318
Todd Kjos425d23f2017-06-12 12:07:26 -07005319static void print_binder_node_nilocked(struct seq_file *m,
5320 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005321{
5322 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005323 struct binder_work *w;
5324 int count;
5325
5326 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005327 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005328 count++;
5329
Martijn Coenen6aac9792017-06-07 09:29:14 -07005330 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 -08005331 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005332 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005333 node->has_strong_ref, node->has_weak_ref,
5334 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005335 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005336 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005337 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005338 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005339 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005340 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005341 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005342 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005343 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005344 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005345 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005346 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005347}
5348
Todd Kjos5346bf32016-10-20 16:43:34 -07005349static void print_binder_ref_olocked(struct seq_file *m,
5350 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005351{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005352 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005353 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5354 ref->data.debug_id, ref->data.desc,
5355 ref->node->proc ? "" : "dead ",
5356 ref->node->debug_id, ref->data.strong,
5357 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005358 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005359}
5360
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005361static void print_binder_proc(struct seq_file *m,
5362 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005363{
5364 struct binder_work *w;
5365 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005366 size_t start_pos = m->count;
5367 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005368 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005369
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005370 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005371 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005372 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005373
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005374 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005375 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005376 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005377 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005378
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005379 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005380 struct binder_node *node = rb_entry(n, struct binder_node,
5381 rb_node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005382 /*
5383 * take a temporary reference on the node so it
5384 * survives and isn't removed from the tree
5385 * while we print it.
5386 */
5387 binder_inc_node_tmpref_ilocked(node);
5388 /* Need to drop inner lock to take node lock */
5389 binder_inner_proc_unlock(proc);
5390 if (last_node)
5391 binder_put_node(last_node);
5392 binder_node_inner_lock(node);
5393 print_binder_node_nilocked(m, node);
5394 binder_node_inner_unlock(node);
5395 last_node = node;
5396 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005397 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005398 binder_inner_proc_unlock(proc);
5399 if (last_node)
5400 binder_put_node(last_node);
5401
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005402 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005403 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005404 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005405 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005406 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005407 print_binder_ref_olocked(m, rb_entry(n,
5408 struct binder_ref,
5409 rb_node_desc));
5410 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005411 }
Todd Kjosd325d372016-10-10 10:40:53 -07005412 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005413 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005414 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005415 print_binder_work_ilocked(m, proc, " ",
5416 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005417 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005418 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005419 break;
5420 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005421 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005422 if (!print_all && m->count == header_pos)
5423 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005424}
5425
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005426static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005427 "BR_ERROR",
5428 "BR_OK",
5429 "BR_TRANSACTION",
5430 "BR_REPLY",
5431 "BR_ACQUIRE_RESULT",
5432 "BR_DEAD_REPLY",
5433 "BR_TRANSACTION_COMPLETE",
5434 "BR_INCREFS",
5435 "BR_ACQUIRE",
5436 "BR_RELEASE",
5437 "BR_DECREFS",
5438 "BR_ATTEMPT_ACQUIRE",
5439 "BR_NOOP",
5440 "BR_SPAWN_LOOPER",
5441 "BR_FINISHED",
5442 "BR_DEAD_BINDER",
5443 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5444 "BR_FAILED_REPLY"
5445};
5446
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005447static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005448 "BC_TRANSACTION",
5449 "BC_REPLY",
5450 "BC_ACQUIRE_RESULT",
5451 "BC_FREE_BUFFER",
5452 "BC_INCREFS",
5453 "BC_ACQUIRE",
5454 "BC_RELEASE",
5455 "BC_DECREFS",
5456 "BC_INCREFS_DONE",
5457 "BC_ACQUIRE_DONE",
5458 "BC_ATTEMPT_ACQUIRE",
5459 "BC_REGISTER_LOOPER",
5460 "BC_ENTER_LOOPER",
5461 "BC_EXIT_LOOPER",
5462 "BC_REQUEST_DEATH_NOTIFICATION",
5463 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005464 "BC_DEAD_BINDER_DONE",
5465 "BC_TRANSACTION_SG",
5466 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005467};
5468
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005469static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005470 "proc",
5471 "thread",
5472 "node",
5473 "ref",
5474 "death",
5475 "transaction",
5476 "transaction_complete"
5477};
5478
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005479static void print_binder_stats(struct seq_file *m, const char *prefix,
5480 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005481{
5482 int i;
5483
5484 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005485 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005486 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005487 int temp = atomic_read(&stats->bc[i]);
5488
5489 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005490 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005491 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005492 }
5493
5494 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005495 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005496 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005497 int temp = atomic_read(&stats->br[i]);
5498
5499 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005500 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005501 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005502 }
5503
5504 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005505 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005506 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005507 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005508 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005509 int created = atomic_read(&stats->obj_created[i]);
5510 int deleted = atomic_read(&stats->obj_deleted[i]);
5511
5512 if (created || deleted)
5513 seq_printf(m, "%s%s: active %d total %d\n",
5514 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005515 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005516 created - deleted,
5517 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005518 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005519}
5520
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005521static void print_binder_proc_stats(struct seq_file *m,
5522 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005523{
5524 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005525 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005526 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005527 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005528 size_t free_async_space =
5529 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005530
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005531 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005532 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005533 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005534 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005535 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005536 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5537 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005538
5539 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5540 ready_threads++;
5541
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005542 seq_printf(m, " threads: %d\n", count);
5543 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005544 " ready threads %d\n"
5545 " free async space %zd\n", proc->requested_threads,
5546 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005547 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005548 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005549 count = 0;
5550 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5551 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005552 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005553 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005554 count = 0;
5555 strong = 0;
5556 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005557 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005558 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5559 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5560 rb_node_desc);
5561 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005562 strong += ref->data.strong;
5563 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005564 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005565 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005566 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005567
Todd Kjosd325d372016-10-10 10:40:53 -07005568 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005569 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005570
Sherry Yang91004422017-08-22 17:26:57 -07005571 binder_alloc_print_pages(m, &proc->alloc);
5572
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005573 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005574 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005575 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005576 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005577 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005578 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005579 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005580 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005581
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005582 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005583}
5584
5585
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005586static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005587{
5588 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005589 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005590 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005591
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005592 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005593
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005594 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005595 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005596 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005597 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5598 /*
5599 * take a temporary reference on the node so it
5600 * survives and isn't removed from the list
5601 * while we print it.
5602 */
5603 node->tmp_refs++;
5604 spin_unlock(&binder_dead_nodes_lock);
5605 if (last_node)
5606 binder_put_node(last_node);
5607 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005608 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005609 binder_node_unlock(node);
5610 last_node = node;
5611 spin_lock(&binder_dead_nodes_lock);
5612 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005613 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005614 if (last_node)
5615 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005616
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005617 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005618 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005619 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005620 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005621
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005622 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005623}
5624
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005625static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005626{
5627 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005628
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005629 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005630
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005631 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005632
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005633 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005634 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005635 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005636 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005637
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005638 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005639}
5640
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005641static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005642{
5643 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005644
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005645 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005646 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005647 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005648 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005649 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005650
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005651 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005652}
5653
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005654static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005655{
Riley Andrews83050a42016-02-09 21:05:33 -08005656 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005657 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005658
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005659 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005660 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005661 if (itr->pid == pid) {
5662 seq_puts(m, "binder proc state:\n");
5663 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005664 }
5665 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005666 mutex_unlock(&binder_procs_lock);
5667
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005668 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005669}
5670
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005671static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005672 struct binder_transaction_log_entry *e)
5673{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005674 int debug_id = READ_ONCE(e->debug_id_done);
5675 /*
5676 * read barrier to guarantee debug_id_done read before
5677 * we print the log values
5678 */
5679 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005680 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005681 "%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 -07005682 e->debug_id, (e->call_type == 2) ? "reply" :
5683 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005684 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005685 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5686 e->return_error, e->return_error_param,
5687 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005688 /*
5689 * read-barrier to guarantee read of debug_id_done after
5690 * done printing the fields of the entry
5691 */
5692 smp_rmb();
5693 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5694 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005695}
5696
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005697static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005698{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005699 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005700 unsigned int log_cur = atomic_read(&log->cur);
5701 unsigned int count;
5702 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005703 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005704
Todd Kjos1cfe6272017-05-24 13:33:28 -07005705 count = log_cur + 1;
5706 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5707 0 : count % ARRAY_SIZE(log->entry);
5708 if (count > ARRAY_SIZE(log->entry) || log->full)
5709 count = ARRAY_SIZE(log->entry);
5710 for (i = 0; i < count; i++) {
5711 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5712
5713 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005714 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005715 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005716}
5717
5718static const struct file_operations binder_fops = {
5719 .owner = THIS_MODULE,
5720 .poll = binder_poll,
5721 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005722 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005723 .mmap = binder_mmap,
5724 .open = binder_open,
5725 .flush = binder_flush,
5726 .release = binder_release,
5727};
5728
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005729BINDER_DEBUG_ENTRY(state);
5730BINDER_DEBUG_ENTRY(stats);
5731BINDER_DEBUG_ENTRY(transactions);
5732BINDER_DEBUG_ENTRY(transaction_log);
5733
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005734static int __init init_binder_device(const char *name)
5735{
5736 int ret;
5737 struct binder_device *binder_device;
5738
5739 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5740 if (!binder_device)
5741 return -ENOMEM;
5742
5743 binder_device->miscdev.fops = &binder_fops;
5744 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5745 binder_device->miscdev.name = name;
5746
5747 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5748 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005749 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005750
5751 ret = misc_register(&binder_device->miscdev);
5752 if (ret < 0) {
5753 kfree(binder_device);
5754 return ret;
5755 }
5756
5757 hlist_add_head(&binder_device->hlist, &binder_devices);
5758
5759 return ret;
5760}
5761
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005762static int __init binder_init(void)
5763{
5764 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005765 char *device_name, *device_names;
5766 struct binder_device *device;
5767 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005768
Sherry Yang5828d702017-07-29 13:24:11 -07005769 binder_alloc_shrinker_init();
5770
Todd Kjos1cfe6272017-05-24 13:33:28 -07005771 atomic_set(&binder_transaction_log.cur, ~0U);
5772 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5773
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005774 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5775 if (binder_debugfs_dir_entry_root)
5776 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5777 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005778
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005779 if (binder_debugfs_dir_entry_root) {
5780 debugfs_create_file("state",
5781 S_IRUGO,
5782 binder_debugfs_dir_entry_root,
5783 NULL,
5784 &binder_state_fops);
5785 debugfs_create_file("stats",
5786 S_IRUGO,
5787 binder_debugfs_dir_entry_root,
5788 NULL,
5789 &binder_stats_fops);
5790 debugfs_create_file("transactions",
5791 S_IRUGO,
5792 binder_debugfs_dir_entry_root,
5793 NULL,
5794 &binder_transactions_fops);
5795 debugfs_create_file("transaction_log",
5796 S_IRUGO,
5797 binder_debugfs_dir_entry_root,
5798 &binder_transaction_log,
5799 &binder_transaction_log_fops);
5800 debugfs_create_file("failed_transaction_log",
5801 S_IRUGO,
5802 binder_debugfs_dir_entry_root,
5803 &binder_transaction_log_failed,
5804 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005805 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005806
5807 /*
5808 * Copy the module_parameter string, because we don't want to
5809 * tokenize it in-place.
5810 */
5811 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5812 if (!device_names) {
5813 ret = -ENOMEM;
5814 goto err_alloc_device_names_failed;
5815 }
5816 strcpy(device_names, binder_devices_param);
5817
5818 while ((device_name = strsep(&device_names, ","))) {
5819 ret = init_binder_device(device_name);
5820 if (ret)
5821 goto err_init_binder_device_failed;
5822 }
5823
5824 return ret;
5825
5826err_init_binder_device_failed:
5827 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5828 misc_deregister(&device->miscdev);
5829 hlist_del(&device->hlist);
5830 kfree(device);
5831 }
5832err_alloc_device_names_failed:
5833 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5834
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005835 return ret;
5836}
5837
5838device_initcall(binder_init);
5839
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005840#define CREATE_TRACE_POINTS
5841#include "binder_trace.h"
5842
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005843MODULE_LICENSE("GPL v2");