blob: 256a1d5bcfc6b28edcaf39df2585cb6bb18a789f [file] [log] [blame]
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001/* binder.c
2 *
3 * Android IPC Subsystem
4 *
5 * Copyright (C) 2007-2008 Google, Inc.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Todd Kjosfc7a7e22017-05-29 16:44:24 -070018/*
19 * Locking overview
20 *
21 * There are 3 main spinlocks which must be acquired in the
22 * order shown:
23 *
24 * 1) proc->outer_lock : protects binder_ref
25 * binder_proc_lock() and binder_proc_unlock() are
26 * used to acq/rel.
27 * 2) node->lock : protects most fields of binder_node.
28 * binder_node_lock() and binder_node_unlock() are
29 * used to acq/rel
30 * 3) proc->inner_lock : protects the thread and node lists
Martijn Coenen22d64e4322017-06-02 11:15:44 -070031 * (proc->threads, proc->waiting_threads, proc->nodes)
32 * and all todo lists associated with the binder_proc
33 * (proc->todo, thread->todo, proc->delivered_death and
34 * node->async_todo), as well as thread->transaction_stack
Todd Kjosfc7a7e22017-05-29 16:44:24 -070035 * binder_inner_proc_lock() and binder_inner_proc_unlock()
36 * are used to acq/rel
37 *
38 * Any lock under procA must never be nested under any lock at the same
39 * level or below on procB.
40 *
41 * Functions that require a lock held on entry indicate which lock
42 * in the suffix of the function name:
43 *
44 * foo_olocked() : requires node->outer_lock
45 * foo_nlocked() : requires node->lock
46 * foo_ilocked() : requires proc->inner_lock
47 * foo_oilocked(): requires proc->outer_lock and proc->inner_lock
48 * foo_nilocked(): requires node->lock and proc->inner_lock
49 * ...
50 */
51
Anmol Sarma56b468f2012-10-30 22:35:43 +053052#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
53
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090054#include <asm/cacheflush.h>
55#include <linux/fdtable.h>
56#include <linux/file.h>
Colin Crosse2610b22013-05-06 23:50:15 +000057#include <linux/freezer.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090058#include <linux/fs.h>
59#include <linux/list.h>
60#include <linux/miscdevice.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090061#include <linux/module.h>
62#include <linux/mutex.h>
63#include <linux/nsproxy.h>
64#include <linux/poll.h>
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070065#include <linux/debugfs.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090066#include <linux/rbtree.h>
67#include <linux/sched.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070068#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090069#include <linux/uaccess.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080070#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050071#include <linux/security.h>
Todd Kjosfc7a7e22017-05-29 16:44:24 -070072#include <linux/spinlock.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090073
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020074#ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
75#define BINDER_IPC_32BIT 1
76#endif
77
78#include <uapi/linux/android/binder.h>
Todd Kjosb9341022016-10-10 10:40:53 -070079#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070080#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090081
Todd Kjos8d9f6f32016-10-17 12:33:15 -070082static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090083static DEFINE_MUTEX(binder_deferred_lock);
84
Martijn Coenen6b7c7122016-09-30 16:08:09 +020085static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090086static HLIST_HEAD(binder_procs);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070087static DEFINE_MUTEX(binder_procs_lock);
88
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090089static HLIST_HEAD(binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070090static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090091
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070092static struct dentry *binder_debugfs_dir_entry_root;
93static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjosc4bd08b2017-05-25 10:56:00 -070094static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090095
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070096#define BINDER_DEBUG_ENTRY(name) \
97static int binder_##name##_open(struct inode *inode, struct file *file) \
98{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070099 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -0700100} \
101\
102static const struct file_operations binder_##name##_fops = { \
103 .owner = THIS_MODULE, \
104 .open = binder_##name##_open, \
105 .read = seq_read, \
106 .llseek = seq_lseek, \
107 .release = single_release, \
108}
109
110static int binder_proc_show(struct seq_file *m, void *unused);
111BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900112
113/* This is only defined in include/asm-arm/sizes.h */
114#ifndef SZ_1K
115#define SZ_1K 0x400
116#endif
117
118#ifndef SZ_4M
119#define SZ_4M 0x400000
120#endif
121
122#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
123
124#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
125
126enum {
127 BINDER_DEBUG_USER_ERROR = 1U << 0,
128 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
129 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
130 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
131 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
132 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
133 BINDER_DEBUG_READ_WRITE = 1U << 6,
134 BINDER_DEBUG_USER_REFS = 1U << 7,
135 BINDER_DEBUG_THREADS = 1U << 8,
136 BINDER_DEBUG_TRANSACTION = 1U << 9,
137 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
138 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
139 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjosd325d372016-10-10 10:40:53 -0700140 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700141 BINDER_DEBUG_SPINLOCKS = 1U << 14,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900142};
143static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
144 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
145module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
146
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200147static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
148module_param_named(devices, binder_devices_param, charp, S_IRUGO);
149
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900150static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
151static int binder_stop_on_user_error;
152
153static int binder_set_stop_on_user_error(const char *val,
154 struct kernel_param *kp)
155{
156 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900157
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900158 ret = param_set_int(val, kp);
159 if (binder_stop_on_user_error < 2)
160 wake_up(&binder_user_error_wait);
161 return ret;
162}
163module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
164 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
165
166#define binder_debug(mask, x...) \
167 do { \
168 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400169 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900170 } while (0)
171
172#define binder_user_error(x...) \
173 do { \
174 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400175 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900176 if (binder_stop_on_user_error) \
177 binder_stop_on_user_error = 2; \
178 } while (0)
179
Martijn Coenen00c80372016-07-13 12:06:49 +0200180#define to_flat_binder_object(hdr) \
181 container_of(hdr, struct flat_binder_object, hdr)
182
183#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
184
Martijn Coenen5a6da532016-09-30 14:10:07 +0200185#define to_binder_buffer_object(hdr) \
186 container_of(hdr, struct binder_buffer_object, hdr)
187
Martijn Coenene3e0f4802016-10-18 13:58:55 +0200188#define to_binder_fd_array_object(hdr) \
189 container_of(hdr, struct binder_fd_array_object, hdr)
190
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900191enum binder_stat_types {
192 BINDER_STAT_PROC,
193 BINDER_STAT_THREAD,
194 BINDER_STAT_NODE,
195 BINDER_STAT_REF,
196 BINDER_STAT_DEATH,
197 BINDER_STAT_TRANSACTION,
198 BINDER_STAT_TRANSACTION_COMPLETE,
199 BINDER_STAT_COUNT
200};
201
202struct binder_stats {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700203 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
204 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
205 atomic_t obj_created[BINDER_STAT_COUNT];
206 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900207};
208
209static struct binder_stats binder_stats;
210
211static inline void binder_stats_deleted(enum binder_stat_types type)
212{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700213 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900214}
215
216static inline void binder_stats_created(enum binder_stat_types type)
217{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700218 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900219}
220
221struct binder_transaction_log_entry {
222 int debug_id;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700223 int debug_id_done;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900224 int call_type;
225 int from_proc;
226 int from_thread;
227 int target_handle;
228 int to_proc;
229 int to_thread;
230 int to_node;
231 int data_size;
232 int offsets_size;
Todd Kjose598d172017-03-22 17:19:52 -0700233 int return_error_line;
234 uint32_t return_error;
235 uint32_t return_error_param;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200236 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900237};
238struct binder_transaction_log {
Todd Kjos1cfe6272017-05-24 13:33:28 -0700239 atomic_t cur;
240 bool full;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900241 struct binder_transaction_log_entry entry[32];
242};
243static struct binder_transaction_log binder_transaction_log;
244static struct binder_transaction_log binder_transaction_log_failed;
245
246static struct binder_transaction_log_entry *binder_transaction_log_add(
247 struct binder_transaction_log *log)
248{
249 struct binder_transaction_log_entry *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700250 unsigned int cur = atomic_inc_return(&log->cur);
Seunghun Lee10f62862014-05-01 01:30:23 +0900251
Todd Kjos1cfe6272017-05-24 13:33:28 -0700252 if (cur >= ARRAY_SIZE(log->entry))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900253 log->full = 1;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700254 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
255 WRITE_ONCE(e->debug_id_done, 0);
256 /*
257 * write-barrier to synchronize access to e->debug_id_done.
258 * We make sure the initialized 0 value is seen before
259 * memset() other fields are zeroed by memset.
260 */
261 smp_wmb();
262 memset(e, 0, sizeof(*e));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900263 return e;
264}
265
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200266struct binder_context {
267 struct binder_node *binder_context_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -0700268 struct mutex context_mgr_node_lock;
269
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200270 kuid_t binder_context_mgr_uid;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200271 const char *name;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200272};
273
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200274struct binder_device {
275 struct hlist_node hlist;
276 struct miscdevice miscdev;
277 struct binder_context context;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200278};
279
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700280/**
281 * struct binder_work - work enqueued on a worklist
282 * @entry: node enqueued on list
283 * @type: type of work to be performed
284 *
285 * There are separate work lists for proc, thread, and node (async).
286 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900287struct binder_work {
288 struct list_head entry;
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700289
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900290 enum {
291 BINDER_WORK_TRANSACTION = 1,
292 BINDER_WORK_TRANSACTION_COMPLETE,
Todd Kjos858b8da2017-04-21 17:35:12 -0700293 BINDER_WORK_RETURN_ERROR,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900294 BINDER_WORK_NODE,
295 BINDER_WORK_DEAD_BINDER,
296 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
297 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
298 } type;
299};
300
Todd Kjos858b8da2017-04-21 17:35:12 -0700301struct binder_error {
302 struct binder_work work;
303 uint32_t cmd;
304};
305
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700306/**
307 * struct binder_node - binder node bookkeeping
308 * @debug_id: unique ID for debugging
309 * (invariant after initialized)
310 * @lock: lock for node fields
311 * @work: worklist element for node work
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700312 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700313 * @rb_node: element for proc->nodes tree
Todd Kjos425d23f2017-06-12 12:07:26 -0700314 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700315 * @dead_node: element for binder_dead_nodes list
316 * (protected by binder_dead_nodes_lock)
317 * @proc: binder_proc that owns this node
318 * (invariant after initialized)
319 * @refs: list of references on this node
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700320 * (protected by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700321 * @internal_strong_refs: used to take strong references when
322 * initiating a transaction
Todd Kjose7f23ed2017-03-21 13:06:01 -0700323 * (protected by @proc->inner_lock if @proc
324 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700325 * @local_weak_refs: weak user refs from local process
Todd Kjose7f23ed2017-03-21 13:06:01 -0700326 * (protected by @proc->inner_lock if @proc
327 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700328 * @local_strong_refs: strong user refs from local process
Todd Kjose7f23ed2017-03-21 13:06:01 -0700329 * (protected by @proc->inner_lock if @proc
330 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700331 * @tmp_refs: temporary kernel refs
Todd Kjose7f23ed2017-03-21 13:06:01 -0700332 * (protected by @proc->inner_lock while @proc
333 * is valid, and by binder_dead_nodes_lock
334 * if @proc is NULL. During inc/dec and node release
335 * it is also protected by @lock to provide safety
336 * as the node dies and @proc becomes NULL)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700337 * @ptr: userspace pointer for node
338 * (invariant, no lock needed)
339 * @cookie: userspace cookie for node
340 * (invariant, no lock needed)
341 * @has_strong_ref: userspace notified of strong ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700342 * (protected by @proc->inner_lock if @proc
343 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700344 * @pending_strong_ref: userspace has acked notification of strong ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700345 * (protected by @proc->inner_lock if @proc
346 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700347 * @has_weak_ref: userspace notified of weak ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700348 * (protected by @proc->inner_lock if @proc
349 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700350 * @pending_weak_ref: userspace has acked notification of weak ref
Todd Kjose7f23ed2017-03-21 13:06:01 -0700351 * (protected by @proc->inner_lock if @proc
352 * and by @lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700353 * @has_async_transaction: async transaction to node in progress
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700354 * (protected by @lock)
Martijn Coenen6aac9792017-06-07 09:29:14 -0700355 * @sched_policy: minimum scheduling policy for node
356 * (invariant after initialized)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700357 * @accept_fds: file descriptor operations supported for node
358 * (invariant after initialized)
359 * @min_priority: minimum scheduling priority
360 * (invariant after initialized)
Martijn Coenenc46810c2017-06-23 10:13:43 -0700361 * @inherit_rt: inherit RT scheduling policy from caller
362 * (invariant after initialized)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700363 * @async_todo: list of async work items
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700364 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700365 *
366 * Bookkeeping structure for binder nodes.
367 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900368struct binder_node {
369 int debug_id;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700370 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900371 struct binder_work work;
372 union {
373 struct rb_node rb_node;
374 struct hlist_node dead_node;
375 };
376 struct binder_proc *proc;
377 struct hlist_head refs;
378 int internal_strong_refs;
379 int local_weak_refs;
380 int local_strong_refs;
Todd Kjosf22abc72017-05-09 11:08:05 -0700381 int tmp_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800382 binder_uintptr_t ptr;
383 binder_uintptr_t cookie;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700384 struct {
385 /*
386 * bitfield elements protected by
387 * proc inner_lock
388 */
389 u8 has_strong_ref:1;
390 u8 pending_strong_ref:1;
391 u8 has_weak_ref:1;
392 u8 pending_weak_ref:1;
393 };
394 struct {
395 /*
396 * invariant after initialization
397 */
Martijn Coenen6aac9792017-06-07 09:29:14 -0700398 u8 sched_policy:2;
Martijn Coenenc46810c2017-06-23 10:13:43 -0700399 u8 inherit_rt:1;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700400 u8 accept_fds:1;
401 u8 min_priority;
402 };
403 bool has_async_transaction;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900404 struct list_head async_todo;
405};
406
407struct binder_ref_death {
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700408 /**
409 * @work: worklist element for death notifications
410 * (protected by inner_lock of the proc that
411 * this ref belongs to)
412 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900413 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800414 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900415};
416
Todd Kjosb0117bb2017-05-08 09:16:27 -0700417/**
418 * struct binder_ref_data - binder_ref counts and id
419 * @debug_id: unique ID for the ref
420 * @desc: unique userspace handle for ref
421 * @strong: strong ref count (debugging only if not locked)
422 * @weak: weak ref count (debugging only if not locked)
423 *
424 * Structure to hold ref count and ref id information. Since
425 * the actual ref can only be accessed with a lock, this structure
426 * is used to return information about the ref to callers of
427 * ref inc/dec functions.
428 */
429struct binder_ref_data {
430 int debug_id;
431 uint32_t desc;
432 int strong;
433 int weak;
434};
435
436/**
437 * struct binder_ref - struct to track references on nodes
438 * @data: binder_ref_data containing id, handle, and current refcounts
439 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
440 * @rb_node_node: node for lookup by @node in proc's rb_tree
441 * @node_entry: list entry for node->refs list in target node
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700442 * (protected by @node->lock)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700443 * @proc: binder_proc containing ref
444 * @node: binder_node of target node. When cleaning up a
445 * ref for deletion in binder_cleanup_ref, a non-NULL
446 * @node indicates the node must be freed
447 * @death: pointer to death notification (ref_death) if requested
Martijn Coenenf9eac642017-05-22 11:26:23 -0700448 * (protected by @node->lock)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700449 *
450 * Structure to track references from procA to target node (on procB). This
451 * structure is unsafe to access without holding @proc->outer_lock.
452 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900453struct binder_ref {
454 /* Lookups needed: */
455 /* node + proc => ref (transaction) */
456 /* desc + proc => ref (transaction, inc/dec ref) */
457 /* node => refs + procs (proc exit) */
Todd Kjosb0117bb2017-05-08 09:16:27 -0700458 struct binder_ref_data data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900459 struct rb_node rb_node_desc;
460 struct rb_node rb_node_node;
461 struct hlist_node node_entry;
462 struct binder_proc *proc;
463 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900464 struct binder_ref_death *death;
465};
466
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900467enum binder_deferred_state {
468 BINDER_DEFERRED_PUT_FILES = 0x01,
469 BINDER_DEFERRED_FLUSH = 0x02,
470 BINDER_DEFERRED_RELEASE = 0x04,
471};
472
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700473/**
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700474 * struct binder_priority - scheduler policy and priority
475 * @sched_policy scheduler policy
476 * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT
477 *
478 * The binder driver supports inheriting the following scheduler policies:
479 * SCHED_NORMAL
480 * SCHED_BATCH
481 * SCHED_FIFO
482 * SCHED_RR
483 */
484struct binder_priority {
485 unsigned int sched_policy;
486 int prio;
487};
488
489/**
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700490 * struct binder_proc - binder process bookkeeping
491 * @proc_node: element for binder_procs list
492 * @threads: rbtree of binder_threads in this proc
Todd Kjosb4827902017-05-25 15:52:17 -0700493 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700494 * @nodes: rbtree of binder nodes associated with
495 * this proc ordered by node->ptr
Todd Kjos425d23f2017-06-12 12:07:26 -0700496 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700497 * @refs_by_desc: rbtree of refs ordered by ref->desc
Todd Kjos5346bf32016-10-20 16:43:34 -0700498 * (protected by @outer_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700499 * @refs_by_node: rbtree of refs ordered by ref->node
Todd Kjos5346bf32016-10-20 16:43:34 -0700500 * (protected by @outer_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700501 * @waiting_threads: threads currently waiting for proc work
502 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700503 * @pid PID of group_leader of process
504 * (invariant after initialized)
505 * @tsk task_struct for group_leader of process
506 * (invariant after initialized)
507 * @files files_struct for process
508 * (invariant after initialized)
509 * @deferred_work_node: element for binder_deferred_list
510 * (protected by binder_deferred_lock)
511 * @deferred_work: bitmap of deferred work to perform
512 * (protected by binder_deferred_lock)
513 * @is_dead: process is dead and awaiting free
514 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700515 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700516 * @todo: list of work for this process
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700517 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700518 * @wait: wait queue head to wait for proc work
519 * (invariant after initialized)
520 * @stats: per-process binder statistics
521 * (atomics, no lock needed)
522 * @delivered_death: list of delivered death notification
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700523 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700524 * @max_threads: cap on number of binder threads
Todd Kjosd600e902017-05-25 17:35:02 -0700525 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700526 * @requested_threads: number of binder threads requested but not
527 * yet started. In current implementation, can
528 * only be 0 or 1.
Todd Kjosd600e902017-05-25 17:35:02 -0700529 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700530 * @requested_threads_started: number binder threads started
Todd Kjosd600e902017-05-25 17:35:02 -0700531 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700532 * @tmp_ref: temporary reference to indicate proc is in use
Todd Kjosb4827902017-05-25 15:52:17 -0700533 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700534 * @default_priority: default scheduler priority
535 * (invariant after initialized)
536 * @debugfs_entry: debugfs node
537 * @alloc: binder allocator bookkeeping
538 * @context: binder_context for this proc
539 * (invariant after initialized)
540 * @inner_lock: can nest under outer_lock and/or node lock
541 * @outer_lock: no nesting under innor or node lock
542 * Lock order: 1) outer, 2) node, 3) inner
543 *
544 * Bookkeeping structure for binder processes
545 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900546struct binder_proc {
547 struct hlist_node proc_node;
548 struct rb_root threads;
549 struct rb_root nodes;
550 struct rb_root refs_by_desc;
551 struct rb_root refs_by_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700552 struct list_head waiting_threads;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900553 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900554 struct task_struct *tsk;
555 struct files_struct *files;
556 struct hlist_node deferred_work_node;
557 int deferred_work;
Todd Kjos2f993e22017-05-12 14:42:55 -0700558 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900559
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900560 struct list_head todo;
561 wait_queue_head_t wait;
562 struct binder_stats stats;
563 struct list_head delivered_death;
564 int max_threads;
565 int requested_threads;
566 int requested_threads_started;
Todd Kjos2f993e22017-05-12 14:42:55 -0700567 int tmp_ref;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700568 struct binder_priority default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700569 struct dentry *debugfs_entry;
Todd Kjosf85d2292016-10-10 10:39:59 -0700570 struct binder_alloc alloc;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200571 struct binder_context *context;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700572 spinlock_t inner_lock;
573 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900574};
575
576enum {
577 BINDER_LOOPER_STATE_REGISTERED = 0x01,
578 BINDER_LOOPER_STATE_ENTERED = 0x02,
579 BINDER_LOOPER_STATE_EXITED = 0x04,
580 BINDER_LOOPER_STATE_INVALID = 0x08,
581 BINDER_LOOPER_STATE_WAITING = 0x10,
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700582 BINDER_LOOPER_STATE_POLL = 0x20,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900583};
584
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700585/**
586 * struct binder_thread - binder thread bookkeeping
587 * @proc: binder process for this thread
588 * (invariant after initialization)
589 * @rb_node: element for proc->threads rbtree
Todd Kjosb4827902017-05-25 15:52:17 -0700590 * (protected by @proc->inner_lock)
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700591 * @waiting_thread_node: element for @proc->waiting_threads list
592 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700593 * @pid: PID for this thread
594 * (invariant after initialization)
595 * @looper: bitmap of looping state
596 * (only accessed by this thread)
597 * @looper_needs_return: looping thread needs to exit driver
598 * (no lock needed)
599 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700600 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700601 * @todo: list of work to do for this thread
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700602 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700603 * @return_error: transaction errors reported by this thread
604 * (only accessed by this thread)
605 * @reply_error: transaction errors reported by target thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700606 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700607 * @wait: wait queue for thread work
608 * @stats: per-thread statistics
609 * (atomics, no lock needed)
610 * @tmp_ref: temporary reference to indicate thread is in use
611 * (atomic since @proc->inner_lock cannot
612 * always be acquired)
613 * @is_dead: thread is dead and awaiting free
614 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700615 * (protected by @proc->inner_lock)
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700616 * @task: struct task_struct for this thread
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700617 *
618 * Bookkeeping structure for binder threads.
619 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900620struct binder_thread {
621 struct binder_proc *proc;
622 struct rb_node rb_node;
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700623 struct list_head waiting_thread_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900624 int pid;
Todd Kjos6798e6d2017-01-06 14:19:25 -0800625 int looper; /* only modified by this thread */
626 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900627 struct binder_transaction *transaction_stack;
628 struct list_head todo;
Todd Kjos858b8da2017-04-21 17:35:12 -0700629 struct binder_error return_error;
630 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900631 wait_queue_head_t wait;
632 struct binder_stats stats;
Todd Kjos2f993e22017-05-12 14:42:55 -0700633 atomic_t tmp_ref;
634 bool is_dead;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700635 struct task_struct *task;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900636};
637
638struct binder_transaction {
639 int debug_id;
640 struct binder_work work;
641 struct binder_thread *from;
642 struct binder_transaction *from_parent;
643 struct binder_proc *to_proc;
644 struct binder_thread *to_thread;
645 struct binder_transaction *to_parent;
646 unsigned need_reply:1;
647 /* unsigned is_dead:1; */ /* not used at the moment */
648
649 struct binder_buffer *buffer;
650 unsigned int code;
651 unsigned int flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -0700652 struct binder_priority priority;
653 struct binder_priority saved_priority;
Martijn Coenen07a30fe2017-06-07 10:02:12 -0700654 bool set_priority_called;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600655 kuid_t sender_euid;
Todd Kjos2f993e22017-05-12 14:42:55 -0700656 /**
657 * @lock: protects @from, @to_proc, and @to_thread
658 *
659 * @from, @to_proc, and @to_thread can be set to NULL
660 * during thread teardown
661 */
662 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900663};
664
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700665/**
666 * binder_proc_lock() - Acquire outer lock for given binder_proc
667 * @proc: struct binder_proc to acquire
668 *
669 * Acquires proc->outer_lock. Used to protect binder_ref
670 * structures associated with the given proc.
671 */
672#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
673static void
674_binder_proc_lock(struct binder_proc *proc, int line)
675{
676 binder_debug(BINDER_DEBUG_SPINLOCKS,
677 "%s: line=%d\n", __func__, line);
678 spin_lock(&proc->outer_lock);
679}
680
681/**
682 * binder_proc_unlock() - Release spinlock for given binder_proc
683 * @proc: struct binder_proc to acquire
684 *
685 * Release lock acquired via binder_proc_lock()
686 */
687#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
688static void
689_binder_proc_unlock(struct binder_proc *proc, int line)
690{
691 binder_debug(BINDER_DEBUG_SPINLOCKS,
692 "%s: line=%d\n", __func__, line);
693 spin_unlock(&proc->outer_lock);
694}
695
696/**
697 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
698 * @proc: struct binder_proc to acquire
699 *
700 * Acquires proc->inner_lock. Used to protect todo lists
701 */
702#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
703static void
704_binder_inner_proc_lock(struct binder_proc *proc, int line)
705{
706 binder_debug(BINDER_DEBUG_SPINLOCKS,
707 "%s: line=%d\n", __func__, line);
708 spin_lock(&proc->inner_lock);
709}
710
711/**
712 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
713 * @proc: struct binder_proc to acquire
714 *
715 * Release lock acquired via binder_inner_proc_lock()
716 */
717#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
718static void
719_binder_inner_proc_unlock(struct binder_proc *proc, int line)
720{
721 binder_debug(BINDER_DEBUG_SPINLOCKS,
722 "%s: line=%d\n", __func__, line);
723 spin_unlock(&proc->inner_lock);
724}
725
726/**
727 * binder_node_lock() - Acquire spinlock for given binder_node
728 * @node: struct binder_node to acquire
729 *
730 * Acquires node->lock. Used to protect binder_node fields
731 */
732#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
733static void
734_binder_node_lock(struct binder_node *node, int line)
735{
736 binder_debug(BINDER_DEBUG_SPINLOCKS,
737 "%s: line=%d\n", __func__, line);
738 spin_lock(&node->lock);
739}
740
741/**
742 * binder_node_unlock() - Release spinlock for given binder_proc
743 * @node: struct binder_node to acquire
744 *
745 * Release lock acquired via binder_node_lock()
746 */
747#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
748static void
749_binder_node_unlock(struct binder_node *node, int line)
750{
751 binder_debug(BINDER_DEBUG_SPINLOCKS,
752 "%s: line=%d\n", __func__, line);
753 spin_unlock(&node->lock);
754}
755
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700756/**
757 * binder_node_inner_lock() - Acquire node and inner locks
758 * @node: struct binder_node to acquire
759 *
760 * Acquires node->lock. If node->proc also acquires
761 * proc->inner_lock. Used to protect binder_node fields
762 */
763#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
764static void
765_binder_node_inner_lock(struct binder_node *node, int line)
766{
767 binder_debug(BINDER_DEBUG_SPINLOCKS,
768 "%s: line=%d\n", __func__, line);
769 spin_lock(&node->lock);
770 if (node->proc)
771 binder_inner_proc_lock(node->proc);
772}
773
774/**
775 * binder_node_unlock() - Release node and inner locks
776 * @node: struct binder_node to acquire
777 *
778 * Release lock acquired via binder_node_lock()
779 */
780#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
781static void
782_binder_node_inner_unlock(struct binder_node *node, int line)
783{
784 struct binder_proc *proc = node->proc;
785
786 binder_debug(BINDER_DEBUG_SPINLOCKS,
787 "%s: line=%d\n", __func__, line);
788 if (proc)
789 binder_inner_proc_unlock(proc);
790 spin_unlock(&node->lock);
791}
792
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700793static bool binder_worklist_empty_ilocked(struct list_head *list)
794{
795 return list_empty(list);
796}
797
798/**
799 * binder_worklist_empty() - Check if no items on the work list
800 * @proc: binder_proc associated with list
801 * @list: list to check
802 *
803 * Return: true if there are no items on list, else false
804 */
805static bool binder_worklist_empty(struct binder_proc *proc,
806 struct list_head *list)
807{
808 bool ret;
809
810 binder_inner_proc_lock(proc);
811 ret = binder_worklist_empty_ilocked(list);
812 binder_inner_proc_unlock(proc);
813 return ret;
814}
815
816static void
817binder_enqueue_work_ilocked(struct binder_work *work,
818 struct list_head *target_list)
819{
820 BUG_ON(target_list == NULL);
821 BUG_ON(work->entry.next && !list_empty(&work->entry));
822 list_add_tail(&work->entry, target_list);
823}
824
825/**
826 * binder_enqueue_work() - Add an item to the work list
827 * @proc: binder_proc associated with list
828 * @work: struct binder_work to add to list
829 * @target_list: list to add work to
830 *
831 * Adds the work to the specified list. Asserts that work
832 * is not already on a list.
833 */
834static void
835binder_enqueue_work(struct binder_proc *proc,
836 struct binder_work *work,
837 struct list_head *target_list)
838{
839 binder_inner_proc_lock(proc);
840 binder_enqueue_work_ilocked(work, target_list);
841 binder_inner_proc_unlock(proc);
842}
843
844static void
845binder_dequeue_work_ilocked(struct binder_work *work)
846{
847 list_del_init(&work->entry);
848}
849
850/**
851 * binder_dequeue_work() - Removes an item from the work list
852 * @proc: binder_proc associated with list
853 * @work: struct binder_work to remove from list
854 *
855 * Removes the specified work item from whatever list it is on.
856 * Can safely be called if work is not on any list.
857 */
858static void
859binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
860{
861 binder_inner_proc_lock(proc);
862 binder_dequeue_work_ilocked(work);
863 binder_inner_proc_unlock(proc);
864}
865
866static struct binder_work *binder_dequeue_work_head_ilocked(
867 struct list_head *list)
868{
869 struct binder_work *w;
870
871 w = list_first_entry_or_null(list, struct binder_work, entry);
872 if (w)
873 list_del_init(&w->entry);
874 return w;
875}
876
877/**
878 * binder_dequeue_work_head() - Dequeues the item at head of list
879 * @proc: binder_proc associated with list
880 * @list: list to dequeue head
881 *
882 * Removes the head of the list if there are items on the list
883 *
884 * Return: pointer dequeued binder_work, NULL if list was empty
885 */
886static struct binder_work *binder_dequeue_work_head(
887 struct binder_proc *proc,
888 struct list_head *list)
889{
890 struct binder_work *w;
891
892 binder_inner_proc_lock(proc);
893 w = binder_dequeue_work_head_ilocked(list);
894 binder_inner_proc_unlock(proc);
895 return w;
896}
897
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900898static void
899binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos2f993e22017-05-12 14:42:55 -0700900static void binder_free_thread(struct binder_thread *thread);
901static void binder_free_proc(struct binder_proc *proc);
Todd Kjos425d23f2017-06-12 12:07:26 -0700902static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900903
Sachin Kamatefde99c2012-08-17 16:39:36 +0530904static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900905{
906 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900907 unsigned long rlim_cur;
908 unsigned long irqs;
909
910 if (files == NULL)
911 return -ESRCH;
912
Al Virodcfadfa2012-08-12 17:27:30 -0400913 if (!lock_task_sighand(proc->tsk, &irqs))
914 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900915
Al Virodcfadfa2012-08-12 17:27:30 -0400916 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
917 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900918
Al Virodcfadfa2012-08-12 17:27:30 -0400919 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900920}
921
922/*
923 * copied from fd_install
924 */
925static void task_fd_install(
926 struct binder_proc *proc, unsigned int fd, struct file *file)
927{
Al Virof869e8a2012-08-15 21:06:33 -0400928 if (proc->files)
929 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900930}
931
932/*
933 * copied from sys_close
934 */
935static long task_close_fd(struct binder_proc *proc, unsigned int fd)
936{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900937 int retval;
938
Al Viro483ce1d2012-08-19 12:04:24 -0400939 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900940 return -ESRCH;
941
Al Viro483ce1d2012-08-19 12:04:24 -0400942 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900943 /* can't restart close syscall because file table entry was cleared */
944 if (unlikely(retval == -ERESTARTSYS ||
945 retval == -ERESTARTNOINTR ||
946 retval == -ERESTARTNOHAND ||
947 retval == -ERESTART_RESTARTBLOCK))
948 retval = -EINTR;
949
950 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900951}
952
Martijn Coenen22d64e4322017-06-02 11:15:44 -0700953static bool binder_has_work_ilocked(struct binder_thread *thread,
954 bool do_proc_work)
955{
956 return !binder_worklist_empty_ilocked(&thread->todo) ||
957 thread->looper_need_return ||
958 (do_proc_work &&
959 !binder_worklist_empty_ilocked(&thread->proc->todo));
960}
961
962static bool binder_has_work(struct binder_thread *thread, bool do_proc_work)
963{
964 bool has_work;
965
966 binder_inner_proc_lock(thread->proc);
967 has_work = binder_has_work_ilocked(thread, do_proc_work);
968 binder_inner_proc_unlock(thread->proc);
969
970 return has_work;
971}
972
973static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread)
974{
975 return !thread->transaction_stack &&
976 binder_worklist_empty_ilocked(&thread->todo) &&
977 (thread->looper & (BINDER_LOOPER_STATE_ENTERED |
978 BINDER_LOOPER_STATE_REGISTERED));
979}
980
981static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc,
982 bool sync)
983{
984 struct rb_node *n;
985 struct binder_thread *thread;
986
987 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
988 thread = rb_entry(n, struct binder_thread, rb_node);
989 if (thread->looper & BINDER_LOOPER_STATE_POLL &&
990 binder_available_for_proc_work_ilocked(thread)) {
991 if (sync)
992 wake_up_interruptible_sync(&thread->wait);
993 else
994 wake_up_interruptible(&thread->wait);
995 }
996 }
997}
998
Martijn Coenen053be422017-06-06 15:17:46 -0700999/**
1000 * binder_select_thread_ilocked() - selects a thread for doing proc work.
1001 * @proc: process to select a thread from
1002 *
1003 * Note that calling this function moves the thread off the waiting_threads
1004 * list, so it can only be woken up by the caller of this function, or a
1005 * signal. Therefore, callers *should* always wake up the thread this function
1006 * returns.
1007 *
1008 * Return: If there's a thread currently waiting for process work,
1009 * returns that thread. Otherwise returns NULL.
1010 */
1011static struct binder_thread *
1012binder_select_thread_ilocked(struct binder_proc *proc)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001013{
1014 struct binder_thread *thread;
1015
Martijn Coenened323352017-07-27 23:52:24 +02001016 assert_spin_locked(&proc->inner_lock);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001017 thread = list_first_entry_or_null(&proc->waiting_threads,
1018 struct binder_thread,
1019 waiting_thread_node);
1020
Martijn Coenen053be422017-06-06 15:17:46 -07001021 if (thread)
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001022 list_del_init(&thread->waiting_thread_node);
Martijn Coenen053be422017-06-06 15:17:46 -07001023
1024 return thread;
1025}
1026
1027/**
1028 * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work.
1029 * @proc: process to wake up a thread in
1030 * @thread: specific thread to wake-up (may be NULL)
1031 * @sync: whether to do a synchronous wake-up
1032 *
1033 * This function wakes up a thread in the @proc process.
1034 * The caller may provide a specific thread to wake-up in
1035 * the @thread parameter. If @thread is NULL, this function
1036 * will wake up threads that have called poll().
1037 *
1038 * Note that for this function to work as expected, callers
1039 * should first call binder_select_thread() to find a thread
1040 * to handle the work (if they don't have a thread already),
1041 * and pass the result into the @thread parameter.
1042 */
1043static void binder_wakeup_thread_ilocked(struct binder_proc *proc,
1044 struct binder_thread *thread,
1045 bool sync)
1046{
Martijn Coenened323352017-07-27 23:52:24 +02001047 assert_spin_locked(&proc->inner_lock);
Martijn Coenen053be422017-06-06 15:17:46 -07001048
1049 if (thread) {
Martijn Coenen22d64e4322017-06-02 11:15:44 -07001050 if (sync)
1051 wake_up_interruptible_sync(&thread->wait);
1052 else
1053 wake_up_interruptible(&thread->wait);
1054 return;
1055 }
1056
1057 /* Didn't find a thread waiting for proc work; this can happen
1058 * in two scenarios:
1059 * 1. All threads are busy handling transactions
1060 * In that case, one of those threads should call back into
1061 * the kernel driver soon and pick up this work.
1062 * 2. Threads are using the (e)poll interface, in which case
1063 * they may be blocked on the waitqueue without having been
1064 * added to waiting_threads. For this case, we just iterate
1065 * over all threads not handling transaction work, and
1066 * wake them all up. We wake all because we don't know whether
1067 * a thread that called into (e)poll is handling non-binder
1068 * work currently.
1069 */
1070 binder_wakeup_poll_threads_ilocked(proc, sync);
1071}
1072
Martijn Coenen053be422017-06-06 15:17:46 -07001073static void binder_wakeup_proc_ilocked(struct binder_proc *proc)
1074{
1075 struct binder_thread *thread = binder_select_thread_ilocked(proc);
1076
1077 binder_wakeup_thread_ilocked(proc, thread, /* sync = */false);
1078}
1079
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001080static bool is_rt_policy(int policy)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001081{
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001082 return policy == SCHED_FIFO || policy == SCHED_RR;
1083}
Seunghun Lee10f62862014-05-01 01:30:23 +09001084
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001085static bool is_fair_policy(int policy)
1086{
1087 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
1088}
1089
1090static bool binder_supported_policy(int policy)
1091{
1092 return is_fair_policy(policy) || is_rt_policy(policy);
1093}
1094
1095static int to_userspace_prio(int policy, int kernel_priority)
1096{
1097 if (is_fair_policy(policy))
1098 return PRIO_TO_NICE(kernel_priority);
1099 else
1100 return MAX_USER_RT_PRIO - 1 - kernel_priority;
1101}
1102
1103static int to_kernel_prio(int policy, int user_priority)
1104{
1105 if (is_fair_policy(policy))
1106 return NICE_TO_PRIO(user_priority);
1107 else
1108 return MAX_USER_RT_PRIO - 1 - user_priority;
1109}
1110
Martijn Coenenecd972d2017-05-26 10:48:56 -07001111static void binder_do_set_priority(struct task_struct *task,
1112 struct binder_priority desired,
1113 bool verify)
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001114{
1115 int priority; /* user-space prio value */
1116 bool has_cap_nice;
1117 unsigned int policy = desired.sched_policy;
1118
1119 if (task->policy == policy && task->normal_prio == desired.prio)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001120 return;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001121
1122 has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE);
1123
1124 priority = to_userspace_prio(policy, desired.prio);
1125
Martijn Coenenecd972d2017-05-26 10:48:56 -07001126 if (verify && is_rt_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001127 long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO);
1128
1129 if (max_rtprio == 0) {
1130 policy = SCHED_NORMAL;
1131 priority = MIN_NICE;
1132 } else if (priority > max_rtprio) {
1133 priority = max_rtprio;
1134 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001135 }
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001136
Martijn Coenenecd972d2017-05-26 10:48:56 -07001137 if (verify && is_fair_policy(policy) && !has_cap_nice) {
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001138 long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE));
1139
1140 if (min_nice > MAX_NICE) {
1141 binder_user_error("%d RLIMIT_NICE not set\n",
1142 task->pid);
1143 return;
1144 } else if (priority < min_nice) {
1145 priority = min_nice;
1146 }
1147 }
1148
1149 if (policy != desired.sched_policy ||
1150 to_kernel_prio(policy, priority) != desired.prio)
1151 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
1152 "%d: priority %d not allowed, using %d instead\n",
1153 task->pid, desired.prio,
1154 to_kernel_prio(policy, priority));
1155
Martijn Coenen81402ea2017-05-08 09:33:22 -07001156 trace_binder_set_priority(task->tgid, task->pid, task->normal_prio,
1157 to_kernel_prio(policy, priority),
1158 desired.prio);
1159
Martijn Coenen57b2ac62017-06-06 17:04:42 -07001160 /* Set the actual priority */
1161 if (task->policy != policy || is_rt_policy(policy)) {
1162 struct sched_param params;
1163
1164 params.sched_priority = is_rt_policy(policy) ? priority : 0;
1165
1166 sched_setscheduler_nocheck(task,
1167 policy | SCHED_RESET_ON_FORK,
1168 &params);
1169 }
1170 if (is_fair_policy(policy))
1171 set_user_nice(task, priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001172}
1173
Martijn Coenenecd972d2017-05-26 10:48:56 -07001174static void binder_set_priority(struct task_struct *task,
1175 struct binder_priority desired)
1176{
1177 binder_do_set_priority(task, desired, /* verify = */ true);
1178}
1179
1180static void binder_restore_priority(struct task_struct *task,
1181 struct binder_priority desired)
1182{
1183 binder_do_set_priority(task, desired, /* verify = */ false);
1184}
1185
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001186static void binder_transaction_priority(struct task_struct *task,
1187 struct binder_transaction *t,
Martijn Coenenc46810c2017-06-23 10:13:43 -07001188 struct binder_priority node_prio,
1189 bool inherit_rt)
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001190{
1191 struct binder_priority desired_prio;
1192
1193 if (t->set_priority_called)
1194 return;
1195
1196 t->set_priority_called = true;
1197 t->saved_priority.sched_policy = task->policy;
1198 t->saved_priority.prio = task->normal_prio;
1199
Martijn Coenenc46810c2017-06-23 10:13:43 -07001200 if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) {
1201 desired_prio.prio = NICE_TO_PRIO(0);
1202 desired_prio.sched_policy = SCHED_NORMAL;
1203 } else {
1204 desired_prio.prio = t->priority.prio;
1205 desired_prio.sched_policy = t->priority.sched_policy;
1206 }
Martijn Coenen07a30fe2017-06-07 10:02:12 -07001207
1208 if (node_prio.prio < t->priority.prio ||
1209 (node_prio.prio == t->priority.prio &&
1210 node_prio.sched_policy == SCHED_FIFO)) {
1211 /*
1212 * In case the minimum priority on the node is
1213 * higher (lower value), use that priority. If
1214 * the priority is the same, but the node uses
1215 * SCHED_FIFO, prefer SCHED_FIFO, since it can
1216 * run unbounded, unlike SCHED_RR.
1217 */
1218 desired_prio = node_prio;
1219 }
1220
1221 binder_set_priority(task, desired_prio);
1222}
1223
Todd Kjos425d23f2017-06-12 12:07:26 -07001224static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
1225 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001226{
1227 struct rb_node *n = proc->nodes.rb_node;
1228 struct binder_node *node;
1229
Martijn Coenened323352017-07-27 23:52:24 +02001230 assert_spin_locked(&proc->inner_lock);
Todd Kjos425d23f2017-06-12 12:07:26 -07001231
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001232 while (n) {
1233 node = rb_entry(n, struct binder_node, rb_node);
1234
1235 if (ptr < node->ptr)
1236 n = n->rb_left;
1237 else if (ptr > node->ptr)
1238 n = n->rb_right;
Todd Kjosf22abc72017-05-09 11:08:05 -07001239 else {
1240 /*
1241 * take an implicit weak reference
1242 * to ensure node stays alive until
1243 * call to binder_put_node()
1244 */
Todd Kjos425d23f2017-06-12 12:07:26 -07001245 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001246 return node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001247 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001248 }
1249 return NULL;
1250}
1251
Todd Kjos425d23f2017-06-12 12:07:26 -07001252static struct binder_node *binder_get_node(struct binder_proc *proc,
1253 binder_uintptr_t ptr)
1254{
1255 struct binder_node *node;
1256
1257 binder_inner_proc_lock(proc);
1258 node = binder_get_node_ilocked(proc, ptr);
1259 binder_inner_proc_unlock(proc);
1260 return node;
1261}
1262
1263static struct binder_node *binder_init_node_ilocked(
1264 struct binder_proc *proc,
1265 struct binder_node *new_node,
1266 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001267{
1268 struct rb_node **p = &proc->nodes.rb_node;
1269 struct rb_node *parent = NULL;
1270 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001271 binder_uintptr_t ptr = fp ? fp->binder : 0;
1272 binder_uintptr_t cookie = fp ? fp->cookie : 0;
1273 __u32 flags = fp ? fp->flags : 0;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001274 s8 priority;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001275
Martijn Coenened323352017-07-27 23:52:24 +02001276 assert_spin_locked(&proc->inner_lock);
1277
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001278 while (*p) {
Todd Kjos425d23f2017-06-12 12:07:26 -07001279
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001280 parent = *p;
1281 node = rb_entry(parent, struct binder_node, rb_node);
1282
1283 if (ptr < node->ptr)
1284 p = &(*p)->rb_left;
1285 else if (ptr > node->ptr)
1286 p = &(*p)->rb_right;
Todd Kjos425d23f2017-06-12 12:07:26 -07001287 else {
1288 /*
1289 * A matching node is already in
1290 * the rb tree. Abandon the init
1291 * and return it.
1292 */
1293 binder_inc_node_tmpref_ilocked(node);
1294 return node;
1295 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001296 }
Todd Kjos425d23f2017-06-12 12:07:26 -07001297 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001298 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosf22abc72017-05-09 11:08:05 -07001299 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001300 rb_link_node(&node->rb_node, parent, p);
1301 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -07001302 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001303 node->proc = proc;
1304 node->ptr = ptr;
1305 node->cookie = cookie;
1306 node->work.type = BINDER_WORK_NODE;
Martijn Coenen6aac9792017-06-07 09:29:14 -07001307 priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1308 node->sched_policy = (flags & FLAT_BINDER_FLAG_PRIORITY_MASK) >>
1309 FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT;
1310 node->min_priority = to_kernel_prio(node->sched_policy, priority);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001311 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Martijn Coenenc46810c2017-06-23 10:13:43 -07001312 node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT);
Todd Kjosfc7a7e22017-05-29 16:44:24 -07001313 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001314 INIT_LIST_HEAD(&node->work.entry);
1315 INIT_LIST_HEAD(&node->async_todo);
1316 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001317 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001318 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001319 (u64)node->ptr, (u64)node->cookie);
Todd Kjos425d23f2017-06-12 12:07:26 -07001320
1321 return node;
1322}
1323
1324static struct binder_node *binder_new_node(struct binder_proc *proc,
1325 struct flat_binder_object *fp)
1326{
1327 struct binder_node *node;
1328 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1329
1330 if (!new_node)
1331 return NULL;
1332 binder_inner_proc_lock(proc);
1333 node = binder_init_node_ilocked(proc, new_node, fp);
1334 binder_inner_proc_unlock(proc);
1335 if (node != new_node)
1336 /*
1337 * The node was already added by another thread
1338 */
1339 kfree(new_node);
1340
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001341 return node;
1342}
1343
Todd Kjose7f23ed2017-03-21 13:06:01 -07001344static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001345{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001346 kfree(node);
1347 binder_stats_deleted(BINDER_STAT_NODE);
1348}
1349
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001350static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1351 int internal,
1352 struct list_head *target_list)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001353{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001354 struct binder_proc *proc = node->proc;
1355
Martijn Coenened323352017-07-27 23:52:24 +02001356 assert_spin_locked(&node->lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001357 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001358 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001359 if (strong) {
1360 if (internal) {
1361 if (target_list == NULL &&
1362 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001363 !(node->proc &&
1364 node == node->proc->context->
1365 binder_context_mgr_node &&
1366 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301367 pr_err("invalid inc strong node for %d\n",
1368 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001369 return -EINVAL;
1370 }
1371 node->internal_strong_refs++;
1372 } else
1373 node->local_strong_refs++;
1374 if (!node->has_strong_ref && target_list) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001375 binder_dequeue_work_ilocked(&node->work);
1376 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001377 }
1378 } else {
1379 if (!internal)
1380 node->local_weak_refs++;
1381 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1382 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301383 pr_err("invalid inc weak node for %d\n",
1384 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001385 return -EINVAL;
1386 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001387 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001388 }
1389 }
1390 return 0;
1391}
1392
Todd Kjose7f23ed2017-03-21 13:06:01 -07001393static int binder_inc_node(struct binder_node *node, int strong, int internal,
1394 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001395{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001396 int ret;
1397
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001398 binder_node_inner_lock(node);
1399 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1400 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001401
1402 return ret;
1403}
1404
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001405static bool binder_dec_node_nilocked(struct binder_node *node,
1406 int strong, int internal)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001407{
1408 struct binder_proc *proc = node->proc;
1409
Martijn Coenened323352017-07-27 23:52:24 +02001410 assert_spin_locked(&node->lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001411 if (proc)
Martijn Coenened323352017-07-27 23:52:24 +02001412 assert_spin_locked(&proc->inner_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001413 if (strong) {
1414 if (internal)
1415 node->internal_strong_refs--;
1416 else
1417 node->local_strong_refs--;
1418 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001419 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001420 } else {
1421 if (!internal)
1422 node->local_weak_refs--;
Todd Kjosf22abc72017-05-09 11:08:05 -07001423 if (node->local_weak_refs || node->tmp_refs ||
1424 !hlist_empty(&node->refs))
Todd Kjose7f23ed2017-03-21 13:06:01 -07001425 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001426 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001427
1428 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001429 if (list_empty(&node->work.entry)) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001430 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07001431 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001432 }
1433 } else {
1434 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosf22abc72017-05-09 11:08:05 -07001435 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07001436 if (proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001437 binder_dequeue_work_ilocked(&node->work);
1438 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001439 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301440 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001441 node->debug_id);
1442 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001443 BUG_ON(!list_empty(&node->work.entry));
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001444 spin_lock(&binder_dead_nodes_lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001445 /*
1446 * tmp_refs could have changed so
1447 * check it again
1448 */
1449 if (node->tmp_refs) {
1450 spin_unlock(&binder_dead_nodes_lock);
1451 return false;
1452 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001453 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001454 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001455 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301456 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001457 node->debug_id);
1458 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001459 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001460 }
1461 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001462 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001463}
1464
Todd Kjose7f23ed2017-03-21 13:06:01 -07001465static void binder_dec_node(struct binder_node *node, int strong, int internal)
1466{
1467 bool free_node;
1468
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001469 binder_node_inner_lock(node);
1470 free_node = binder_dec_node_nilocked(node, strong, internal);
1471 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001472 if (free_node)
1473 binder_free_node(node);
1474}
1475
1476static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosf22abc72017-05-09 11:08:05 -07001477{
1478 /*
1479 * No call to binder_inc_node() is needed since we
1480 * don't need to inform userspace of any changes to
1481 * tmp_refs
1482 */
1483 node->tmp_refs++;
1484}
1485
1486/**
Todd Kjose7f23ed2017-03-21 13:06:01 -07001487 * binder_inc_node_tmpref() - take a temporary reference on node
1488 * @node: node to reference
1489 *
1490 * Take reference on node to prevent the node from being freed
1491 * while referenced only by a local variable. The inner lock is
1492 * needed to serialize with the node work on the queue (which
1493 * isn't needed after the node is dead). If the node is dead
1494 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1495 * node->tmp_refs against dead-node-only cases where the node
1496 * lock cannot be acquired (eg traversing the dead node list to
1497 * print nodes)
1498 */
1499static void binder_inc_node_tmpref(struct binder_node *node)
1500{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001501 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001502 if (node->proc)
1503 binder_inner_proc_lock(node->proc);
1504 else
1505 spin_lock(&binder_dead_nodes_lock);
1506 binder_inc_node_tmpref_ilocked(node);
1507 if (node->proc)
1508 binder_inner_proc_unlock(node->proc);
1509 else
1510 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001511 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001512}
1513
1514/**
Todd Kjosf22abc72017-05-09 11:08:05 -07001515 * binder_dec_node_tmpref() - remove a temporary reference on node
1516 * @node: node to reference
1517 *
1518 * Release temporary reference on node taken via binder_inc_node_tmpref()
1519 */
1520static void binder_dec_node_tmpref(struct binder_node *node)
1521{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001522 bool free_node;
1523
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001524 binder_node_inner_lock(node);
1525 if (!node->proc)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001526 spin_lock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001527 node->tmp_refs--;
1528 BUG_ON(node->tmp_refs < 0);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001529 if (!node->proc)
1530 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001531 /*
1532 * Call binder_dec_node() to check if all refcounts are 0
1533 * and cleanup is needed. Calling with strong=0 and internal=1
1534 * causes no actual reference to be released in binder_dec_node().
1535 * If that changes, a change is needed here too.
1536 */
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001537 free_node = binder_dec_node_nilocked(node, 0, 1);
1538 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001539 if (free_node)
1540 binder_free_node(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07001541}
1542
1543static void binder_put_node(struct binder_node *node)
1544{
1545 binder_dec_node_tmpref(node);
1546}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001547
Todd Kjos5346bf32016-10-20 16:43:34 -07001548static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1549 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001550{
1551 struct rb_node *n = proc->refs_by_desc.rb_node;
1552 struct binder_ref *ref;
1553
1554 while (n) {
1555 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1556
Todd Kjosb0117bb2017-05-08 09:16:27 -07001557 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001558 n = n->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001559 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001560 n = n->rb_right;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001561 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001562 binder_user_error("tried to use weak ref as strong ref\n");
1563 return NULL;
1564 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001565 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001566 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001567 }
1568 return NULL;
1569}
1570
Todd Kjosb0117bb2017-05-08 09:16:27 -07001571/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001572 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjosb0117bb2017-05-08 09:16:27 -07001573 * @proc: binder_proc that owns the ref
1574 * @node: binder_node of target
1575 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1576 *
1577 * Look up the ref for the given node and return it if it exists
1578 *
1579 * If it doesn't exist and the caller provides a newly allocated
1580 * ref, initialize the fields of the newly allocated ref and insert
1581 * into the given proc rb_trees and node refs list.
1582 *
1583 * Return: the ref for node. It is possible that another thread
1584 * allocated/initialized the ref first in which case the
1585 * returned ref would be different than the passed-in
1586 * new_ref. new_ref must be kfree'd by the caller in
1587 * this case.
1588 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001589static struct binder_ref *binder_get_ref_for_node_olocked(
1590 struct binder_proc *proc,
1591 struct binder_node *node,
1592 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001593{
Todd Kjosb0117bb2017-05-08 09:16:27 -07001594 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001595 struct rb_node **p = &proc->refs_by_node.rb_node;
1596 struct rb_node *parent = NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001597 struct binder_ref *ref;
1598 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001599
1600 while (*p) {
1601 parent = *p;
1602 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1603
1604 if (node < ref->node)
1605 p = &(*p)->rb_left;
1606 else if (node > ref->node)
1607 p = &(*p)->rb_right;
1608 else
1609 return ref;
1610 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001611 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001612 return NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001613
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001614 binder_stats_created(BINDER_STAT_REF);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001615 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001616 new_ref->proc = proc;
1617 new_ref->node = node;
1618 rb_link_node(&new_ref->rb_node_node, parent, p);
1619 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1620
Todd Kjosb0117bb2017-05-08 09:16:27 -07001621 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001622 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1623 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001624 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001625 break;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001626 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001627 }
1628
1629 p = &proc->refs_by_desc.rb_node;
1630 while (*p) {
1631 parent = *p;
1632 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1633
Todd Kjosb0117bb2017-05-08 09:16:27 -07001634 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001635 p = &(*p)->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001636 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001637 p = &(*p)->rb_right;
1638 else
1639 BUG();
1640 }
1641 rb_link_node(&new_ref->rb_node_desc, parent, p);
1642 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001643
1644 binder_node_lock(node);
Todd Kjos4cbe5752017-05-01 17:21:51 -07001645 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001646
Todd Kjos4cbe5752017-05-01 17:21:51 -07001647 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1648 "%d new ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001649 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjos4cbe5752017-05-01 17:21:51 -07001650 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001651 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001652 return new_ref;
1653}
1654
Todd Kjos5346bf32016-10-20 16:43:34 -07001655static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001656{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001657 bool delete_node = false;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001658
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001659 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301660 "%d delete ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001661 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301662 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001663
1664 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1665 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001666
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001667 binder_node_inner_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001668 if (ref->data.strong)
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001669 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001670
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001671 hlist_del(&ref->node_entry);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001672 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1673 binder_node_inner_unlock(ref->node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001674 /*
1675 * Clear ref->node unless we want the caller to free the node
1676 */
1677 if (!delete_node) {
1678 /*
1679 * The caller uses ref->node to determine
1680 * whether the node needs to be freed. Clear
1681 * it since the node is still alive.
1682 */
1683 ref->node = NULL;
1684 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001685
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001686 if (ref->death) {
1687 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301688 "%d delete ref %d desc %d has death notification\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001689 ref->proc->pid, ref->data.debug_id,
1690 ref->data.desc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001691 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001692 binder_stats_deleted(BINDER_STAT_DEATH);
1693 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001694 binder_stats_deleted(BINDER_STAT_REF);
1695}
1696
Todd Kjosb0117bb2017-05-08 09:16:27 -07001697/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001698 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjosb0117bb2017-05-08 09:16:27 -07001699 * @ref: ref to be incremented
1700 * @strong: if true, strong increment, else weak
1701 * @target_list: list to queue node work on
1702 *
Todd Kjos5346bf32016-10-20 16:43:34 -07001703 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjosb0117bb2017-05-08 09:16:27 -07001704 *
1705 * Return: 0, if successful, else errno
1706 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001707static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1708 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001709{
1710 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001711
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001712 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001713 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001714 ret = binder_inc_node(ref->node, 1, 1, target_list);
1715 if (ret)
1716 return ret;
1717 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001718 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001719 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001720 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001721 ret = binder_inc_node(ref->node, 0, 1, target_list);
1722 if (ret)
1723 return ret;
1724 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001725 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001726 }
1727 return 0;
1728}
1729
Todd Kjosb0117bb2017-05-08 09:16:27 -07001730/**
1731 * binder_dec_ref() - dec the ref for given handle
1732 * @ref: ref to be decremented
1733 * @strong: if true, strong decrement, else weak
1734 *
1735 * Decrement the ref.
1736 *
Todd Kjosb0117bb2017-05-08 09:16:27 -07001737 * Return: true if ref is cleaned up and ready to be freed
1738 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001739static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001740{
1741 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001742 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301743 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001744 ref->proc->pid, ref->data.debug_id,
1745 ref->data.desc, ref->data.strong,
1746 ref->data.weak);
1747 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001748 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001749 ref->data.strong--;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001750 if (ref->data.strong == 0)
1751 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001752 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001753 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301754 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001755 ref->proc->pid, ref->data.debug_id,
1756 ref->data.desc, ref->data.strong,
1757 ref->data.weak);
1758 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001759 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001760 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001761 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001762 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001763 binder_cleanup_ref_olocked(ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001764 return true;
1765 }
1766 return false;
1767}
1768
1769/**
1770 * binder_get_node_from_ref() - get the node from the given proc/desc
1771 * @proc: proc containing the ref
1772 * @desc: the handle associated with the ref
1773 * @need_strong_ref: if true, only return node if ref is strong
1774 * @rdata: the id/refcount data for the ref
1775 *
1776 * Given a proc and ref handle, return the associated binder_node
1777 *
1778 * Return: a binder_node or NULL if not found or not strong when strong required
1779 */
1780static struct binder_node *binder_get_node_from_ref(
1781 struct binder_proc *proc,
1782 u32 desc, bool need_strong_ref,
1783 struct binder_ref_data *rdata)
1784{
1785 struct binder_node *node;
1786 struct binder_ref *ref;
1787
Todd Kjos5346bf32016-10-20 16:43:34 -07001788 binder_proc_lock(proc);
1789 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001790 if (!ref)
1791 goto err_no_ref;
1792 node = ref->node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001793 /*
1794 * Take an implicit reference on the node to ensure
1795 * it stays alive until the call to binder_put_node()
1796 */
1797 binder_inc_node_tmpref(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001798 if (rdata)
1799 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001800 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001801
1802 return node;
1803
1804err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001805 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001806 return NULL;
1807}
1808
1809/**
1810 * binder_free_ref() - free the binder_ref
1811 * @ref: ref to free
1812 *
Todd Kjose7f23ed2017-03-21 13:06:01 -07001813 * Free the binder_ref. Free the binder_node indicated by ref->node
1814 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjosb0117bb2017-05-08 09:16:27 -07001815 */
1816static void binder_free_ref(struct binder_ref *ref)
1817{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001818 if (ref->node)
1819 binder_free_node(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001820 kfree(ref->death);
1821 kfree(ref);
1822}
1823
1824/**
1825 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1826 * @proc: proc containing the ref
1827 * @desc: the handle associated with the ref
1828 * @increment: true=inc reference, false=dec reference
1829 * @strong: true=strong reference, false=weak reference
1830 * @rdata: the id/refcount data for the ref
1831 *
1832 * Given a proc and ref handle, increment or decrement the ref
1833 * according to "increment" arg.
1834 *
1835 * Return: 0 if successful, else errno
1836 */
1837static int binder_update_ref_for_handle(struct binder_proc *proc,
1838 uint32_t desc, bool increment, bool strong,
1839 struct binder_ref_data *rdata)
1840{
1841 int ret = 0;
1842 struct binder_ref *ref;
1843 bool delete_ref = false;
1844
Todd Kjos5346bf32016-10-20 16:43:34 -07001845 binder_proc_lock(proc);
1846 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001847 if (!ref) {
1848 ret = -EINVAL;
1849 goto err_no_ref;
1850 }
1851 if (increment)
Todd Kjos5346bf32016-10-20 16:43:34 -07001852 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001853 else
Todd Kjos5346bf32016-10-20 16:43:34 -07001854 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001855
1856 if (rdata)
1857 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001858 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001859
1860 if (delete_ref)
1861 binder_free_ref(ref);
1862 return ret;
1863
1864err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001865 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001866 return ret;
1867}
1868
1869/**
1870 * binder_dec_ref_for_handle() - dec the ref for given handle
1871 * @proc: proc containing the ref
1872 * @desc: the handle associated with the ref
1873 * @strong: true=strong reference, false=weak reference
1874 * @rdata: the id/refcount data for the ref
1875 *
1876 * Just calls binder_update_ref_for_handle() to decrement the ref.
1877 *
1878 * Return: 0 if successful, else errno
1879 */
1880static int binder_dec_ref_for_handle(struct binder_proc *proc,
1881 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1882{
1883 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1884}
1885
1886
1887/**
1888 * binder_inc_ref_for_node() - increment the ref for given proc/node
1889 * @proc: proc containing the ref
1890 * @node: target node
1891 * @strong: true=strong reference, false=weak reference
1892 * @target_list: worklist to use if node is incremented
1893 * @rdata: the id/refcount data for the ref
1894 *
1895 * Given a proc and node, increment the ref. Create the ref if it
1896 * doesn't already exist
1897 *
1898 * Return: 0 if successful, else errno
1899 */
1900static int binder_inc_ref_for_node(struct binder_proc *proc,
1901 struct binder_node *node,
1902 bool strong,
1903 struct list_head *target_list,
1904 struct binder_ref_data *rdata)
1905{
1906 struct binder_ref *ref;
1907 struct binder_ref *new_ref = NULL;
1908 int ret = 0;
1909
Todd Kjos5346bf32016-10-20 16:43:34 -07001910 binder_proc_lock(proc);
1911 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001912 if (!ref) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001913 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001914 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1915 if (!new_ref)
1916 return -ENOMEM;
Todd Kjos5346bf32016-10-20 16:43:34 -07001917 binder_proc_lock(proc);
1918 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001919 }
Todd Kjos5346bf32016-10-20 16:43:34 -07001920 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001921 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001922 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001923 if (new_ref && ref != new_ref)
1924 /*
1925 * Another thread created the ref first so
1926 * free the one we allocated
1927 */
1928 kfree(new_ref);
1929 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001930}
1931
Martijn Coenen995a36e2017-06-02 13:36:52 -07001932static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1933 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001934{
Todd Kjos21ef40a2017-03-30 18:02:13 -07001935 BUG_ON(!target_thread);
Martijn Coenened323352017-07-27 23:52:24 +02001936 assert_spin_locked(&target_thread->proc->inner_lock);
Todd Kjos21ef40a2017-03-30 18:02:13 -07001937 BUG_ON(target_thread->transaction_stack != t);
1938 BUG_ON(target_thread->transaction_stack->from != target_thread);
1939 target_thread->transaction_stack =
1940 target_thread->transaction_stack->from_parent;
1941 t->from = NULL;
1942}
1943
Todd Kjos2f993e22017-05-12 14:42:55 -07001944/**
1945 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1946 * @thread: thread to decrement
1947 *
1948 * A thread needs to be kept alive while being used to create or
1949 * handle a transaction. binder_get_txn_from() is used to safely
1950 * extract t->from from a binder_transaction and keep the thread
1951 * indicated by t->from from being freed. When done with that
1952 * binder_thread, this function is called to decrement the
1953 * tmp_ref and free if appropriate (thread has been released
1954 * and no transaction being processed by the driver)
1955 */
1956static void binder_thread_dec_tmpref(struct binder_thread *thread)
1957{
1958 /*
1959 * atomic is used to protect the counter value while
1960 * it cannot reach zero or thread->is_dead is false
Todd Kjos2f993e22017-05-12 14:42:55 -07001961 */
Todd Kjosb4827902017-05-25 15:52:17 -07001962 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001963 atomic_dec(&thread->tmp_ref);
1964 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07001965 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001966 binder_free_thread(thread);
1967 return;
1968 }
Todd Kjosb4827902017-05-25 15:52:17 -07001969 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001970}
1971
1972/**
1973 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1974 * @proc: proc to decrement
1975 *
1976 * A binder_proc needs to be kept alive while being used to create or
1977 * handle a transaction. proc->tmp_ref is incremented when
1978 * creating a new transaction or the binder_proc is currently in-use
1979 * by threads that are being released. When done with the binder_proc,
1980 * this function is called to decrement the counter and free the
1981 * proc if appropriate (proc has been released, all threads have
1982 * been released and not currenly in-use to process a transaction).
1983 */
1984static void binder_proc_dec_tmpref(struct binder_proc *proc)
1985{
Todd Kjosb4827902017-05-25 15:52:17 -07001986 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001987 proc->tmp_ref--;
1988 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1989 !proc->tmp_ref) {
Todd Kjosb4827902017-05-25 15:52:17 -07001990 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001991 binder_free_proc(proc);
1992 return;
1993 }
Todd Kjosb4827902017-05-25 15:52:17 -07001994 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001995}
1996
1997/**
1998 * binder_get_txn_from() - safely extract the "from" thread in transaction
1999 * @t: binder transaction for t->from
2000 *
2001 * Atomically return the "from" thread and increment the tmp_ref
2002 * count for the thread to ensure it stays alive until
2003 * binder_thread_dec_tmpref() is called.
2004 *
2005 * Return: the value of t->from
2006 */
2007static struct binder_thread *binder_get_txn_from(
2008 struct binder_transaction *t)
2009{
2010 struct binder_thread *from;
2011
2012 spin_lock(&t->lock);
2013 from = t->from;
2014 if (from)
2015 atomic_inc(&from->tmp_ref);
2016 spin_unlock(&t->lock);
2017 return from;
2018}
2019
Martijn Coenen995a36e2017-06-02 13:36:52 -07002020/**
2021 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
2022 * @t: binder transaction for t->from
2023 *
2024 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
2025 * to guarantee that the thread cannot be released while operating on it.
2026 * The caller must call binder_inner_proc_unlock() to release the inner lock
2027 * as well as call binder_dec_thread_txn() to release the reference.
2028 *
2029 * Return: the value of t->from
2030 */
2031static struct binder_thread *binder_get_txn_from_and_acq_inner(
2032 struct binder_transaction *t)
2033{
2034 struct binder_thread *from;
2035
2036 from = binder_get_txn_from(t);
2037 if (!from)
2038 return NULL;
2039 binder_inner_proc_lock(from->proc);
2040 if (t->from) {
2041 BUG_ON(from != t->from);
2042 return from;
2043 }
2044 binder_inner_proc_unlock(from->proc);
2045 binder_thread_dec_tmpref(from);
2046 return NULL;
2047}
2048
Todd Kjos21ef40a2017-03-30 18:02:13 -07002049static void binder_free_transaction(struct binder_transaction *t)
2050{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002051 if (t->buffer)
2052 t->buffer->transaction = NULL;
2053 kfree(t);
2054 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2055}
2056
2057static void binder_send_failed_reply(struct binder_transaction *t,
2058 uint32_t error_code)
2059{
2060 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002061 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09002062
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002063 BUG_ON(t->flags & TF_ONE_WAY);
2064 while (1) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002065 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002066 if (target_thread) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002067 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2068 "send failed reply for transaction %d to %d:%d\n",
2069 t->debug_id,
2070 target_thread->proc->pid,
2071 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002072
Martijn Coenen995a36e2017-06-02 13:36:52 -07002073 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos858b8da2017-04-21 17:35:12 -07002074 if (target_thread->reply_error.cmd == BR_OK) {
2075 target_thread->reply_error.cmd = error_code;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002076 binder_enqueue_work_ilocked(
Todd Kjos1c89e6b2016-10-20 10:33:00 -07002077 &target_thread->reply_error.work,
Todd Kjos858b8da2017-04-21 17:35:12 -07002078 &target_thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002079 wake_up_interruptible(&target_thread->wait);
2080 } else {
Todd Kjos858b8da2017-04-21 17:35:12 -07002081 WARN(1, "Unexpected reply error: %u\n",
2082 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002083 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002084 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002085 binder_thread_dec_tmpref(target_thread);
Todd Kjos858b8da2017-04-21 17:35:12 -07002086 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002087 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002088 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002089 next = t->from_parent;
2090
2091 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
2092 "send failed reply for transaction %d, target dead\n",
2093 t->debug_id);
2094
Todd Kjos21ef40a2017-03-30 18:02:13 -07002095 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03002096 if (next == NULL) {
2097 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2098 "reply failed, no target thread at root\n");
2099 return;
2100 }
2101 t = next;
2102 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2103 "reply failed, no target thread -- retry %d\n",
2104 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002105 }
2106}
2107
Martijn Coenen00c80372016-07-13 12:06:49 +02002108/**
Martijn Coenen3217ccc2017-08-24 15:23:36 +02002109 * binder_cleanup_transaction() - cleans up undelivered transaction
2110 * @t: transaction that needs to be cleaned up
2111 * @reason: reason the transaction wasn't delivered
2112 * @error_code: error to return to caller (if synchronous call)
2113 */
2114static void binder_cleanup_transaction(struct binder_transaction *t,
2115 const char *reason,
2116 uint32_t error_code)
2117{
2118 if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) {
2119 binder_send_failed_reply(t, error_code);
2120 } else {
2121 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
2122 "undelivered transaction %d, %s\n",
2123 t->debug_id, reason);
2124 binder_free_transaction(t);
2125 }
2126}
2127
2128/**
Martijn Coenen00c80372016-07-13 12:06:49 +02002129 * binder_validate_object() - checks for a valid metadata object in a buffer.
2130 * @buffer: binder_buffer that we're parsing.
2131 * @offset: offset in the buffer at which to validate an object.
2132 *
2133 * Return: If there's a valid metadata object at @offset in @buffer, the
2134 * size of that object. Otherwise, it returns zero.
2135 */
2136static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
2137{
2138 /* Check if we can read a header first */
2139 struct binder_object_header *hdr;
2140 size_t object_size = 0;
2141
2142 if (offset > buffer->data_size - sizeof(*hdr) ||
2143 buffer->data_size < sizeof(*hdr) ||
2144 !IS_ALIGNED(offset, sizeof(u32)))
2145 return 0;
2146
2147 /* Ok, now see if we can read a complete object. */
2148 hdr = (struct binder_object_header *)(buffer->data + offset);
2149 switch (hdr->type) {
2150 case BINDER_TYPE_BINDER:
2151 case BINDER_TYPE_WEAK_BINDER:
2152 case BINDER_TYPE_HANDLE:
2153 case BINDER_TYPE_WEAK_HANDLE:
2154 object_size = sizeof(struct flat_binder_object);
2155 break;
2156 case BINDER_TYPE_FD:
2157 object_size = sizeof(struct binder_fd_object);
2158 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002159 case BINDER_TYPE_PTR:
2160 object_size = sizeof(struct binder_buffer_object);
2161 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002162 case BINDER_TYPE_FDA:
2163 object_size = sizeof(struct binder_fd_array_object);
2164 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02002165 default:
2166 return 0;
2167 }
2168 if (offset <= buffer->data_size - object_size &&
2169 buffer->data_size >= object_size)
2170 return object_size;
2171 else
2172 return 0;
2173}
2174
Martijn Coenen5a6da532016-09-30 14:10:07 +02002175/**
2176 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
2177 * @b: binder_buffer containing the object
2178 * @index: index in offset array at which the binder_buffer_object is
2179 * located
2180 * @start: points to the start of the offset array
2181 * @num_valid: the number of valid offsets in the offset array
2182 *
2183 * Return: If @index is within the valid range of the offset array
2184 * described by @start and @num_valid, and if there's a valid
2185 * binder_buffer_object at the offset found in index @index
2186 * of the offset array, that object is returned. Otherwise,
2187 * %NULL is returned.
2188 * Note that the offset found in index @index itself is not
2189 * verified; this function assumes that @num_valid elements
2190 * from @start were previously verified to have valid offsets.
2191 */
2192static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
2193 binder_size_t index,
2194 binder_size_t *start,
2195 binder_size_t num_valid)
2196{
2197 struct binder_buffer_object *buffer_obj;
2198 binder_size_t *offp;
2199
2200 if (index >= num_valid)
2201 return NULL;
2202
2203 offp = start + index;
2204 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
2205 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
2206 return NULL;
2207
2208 return buffer_obj;
2209}
2210
2211/**
2212 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
2213 * @b: transaction buffer
2214 * @objects_start start of objects buffer
2215 * @buffer: binder_buffer_object in which to fix up
2216 * @offset: start offset in @buffer to fix up
2217 * @last_obj: last binder_buffer_object that we fixed up in
2218 * @last_min_offset: minimum fixup offset in @last_obj
2219 *
2220 * Return: %true if a fixup in buffer @buffer at offset @offset is
2221 * allowed.
2222 *
2223 * For safety reasons, we only allow fixups inside a buffer to happen
2224 * at increasing offsets; additionally, we only allow fixup on the last
2225 * buffer object that was verified, or one of its parents.
2226 *
2227 * Example of what is allowed:
2228 *
2229 * A
2230 * B (parent = A, offset = 0)
2231 * C (parent = A, offset = 16)
2232 * D (parent = C, offset = 0)
2233 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
2234 *
2235 * Examples of what is not allowed:
2236 *
2237 * Decreasing offsets within the same parent:
2238 * A
2239 * C (parent = A, offset = 16)
2240 * B (parent = A, offset = 0) // decreasing offset within A
2241 *
2242 * Referring to a parent that wasn't the last object or any of its parents:
2243 * A
2244 * B (parent = A, offset = 0)
2245 * C (parent = A, offset = 0)
2246 * C (parent = A, offset = 16)
2247 * D (parent = B, offset = 0) // B is not A or any of A's parents
2248 */
2249static bool binder_validate_fixup(struct binder_buffer *b,
2250 binder_size_t *objects_start,
2251 struct binder_buffer_object *buffer,
2252 binder_size_t fixup_offset,
2253 struct binder_buffer_object *last_obj,
2254 binder_size_t last_min_offset)
2255{
2256 if (!last_obj) {
2257 /* Nothing to fix up in */
2258 return false;
2259 }
2260
2261 while (last_obj != buffer) {
2262 /*
2263 * Safe to retrieve the parent of last_obj, since it
2264 * was already previously verified by the driver.
2265 */
2266 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
2267 return false;
2268 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
2269 last_obj = (struct binder_buffer_object *)
2270 (b->data + *(objects_start + last_obj->parent));
2271 }
2272 return (fixup_offset >= last_min_offset);
2273}
2274
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002275static void binder_transaction_buffer_release(struct binder_proc *proc,
2276 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002277 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002278{
Martijn Coenen5a6da532016-09-30 14:10:07 +02002279 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002280 int debug_id = buffer->debug_id;
2281
2282 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302283 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002284 proc->pid, buffer->debug_id,
2285 buffer->data_size, buffer->offsets_size, failed_at);
2286
2287 if (buffer->target_node)
2288 binder_dec_node(buffer->target_node, 1, 0);
2289
Martijn Coenen5a6da532016-09-30 14:10:07 +02002290 off_start = (binder_size_t *)(buffer->data +
2291 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002292 if (failed_at)
2293 off_end = failed_at;
2294 else
Martijn Coenen5a6da532016-09-30 14:10:07 +02002295 off_end = (void *)off_start + buffer->offsets_size;
2296 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002297 struct binder_object_header *hdr;
2298 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002299
Martijn Coenen00c80372016-07-13 12:06:49 +02002300 if (object_size == 0) {
2301 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002302 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002303 continue;
2304 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002305 hdr = (struct binder_object_header *)(buffer->data + *offp);
2306 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002307 case BINDER_TYPE_BINDER:
2308 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002309 struct flat_binder_object *fp;
2310 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002311
Martijn Coenen00c80372016-07-13 12:06:49 +02002312 fp = to_flat_binder_object(hdr);
2313 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002314 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002315 pr_err("transaction release %d bad node %016llx\n",
2316 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002317 break;
2318 }
2319 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002320 " node %d u%016llx\n",
2321 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002322 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2323 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002324 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002325 } break;
2326 case BINDER_TYPE_HANDLE:
2327 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002328 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002329 struct binder_ref_data rdata;
2330 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002331
Martijn Coenen00c80372016-07-13 12:06:49 +02002332 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002333 ret = binder_dec_ref_for_handle(proc, fp->handle,
2334 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2335
2336 if (ret) {
2337 pr_err("transaction release %d bad handle %d, ret = %d\n",
2338 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002339 break;
2340 }
2341 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002342 " ref %d desc %d\n",
2343 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002344 } break;
2345
Martijn Coenen00c80372016-07-13 12:06:49 +02002346 case BINDER_TYPE_FD: {
2347 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2348
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002349 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002350 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002351 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002352 task_close_fd(proc, fp->fd);
2353 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002354 case BINDER_TYPE_PTR:
2355 /*
2356 * Nothing to do here, this will get cleaned up when the
2357 * transaction buffer gets freed
2358 */
2359 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002360 case BINDER_TYPE_FDA: {
2361 struct binder_fd_array_object *fda;
2362 struct binder_buffer_object *parent;
2363 uintptr_t parent_buffer;
2364 u32 *fd_array;
2365 size_t fd_index;
2366 binder_size_t fd_buf_size;
2367
2368 fda = to_binder_fd_array_object(hdr);
2369 parent = binder_validate_ptr(buffer, fda->parent,
2370 off_start,
2371 offp - off_start);
2372 if (!parent) {
2373 pr_err("transaction release %d bad parent offset",
2374 debug_id);
2375 continue;
2376 }
2377 /*
2378 * Since the parent was already fixed up, convert it
2379 * back to kernel address space to access it
2380 */
2381 parent_buffer = parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002382 binder_alloc_get_user_buffer_offset(
2383 &proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002384
2385 fd_buf_size = sizeof(u32) * fda->num_fds;
2386 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2387 pr_err("transaction release %d invalid number of fds (%lld)\n",
2388 debug_id, (u64)fda->num_fds);
2389 continue;
2390 }
2391 if (fd_buf_size > parent->length ||
2392 fda->parent_offset > parent->length - fd_buf_size) {
2393 /* No space for all file descriptors here. */
2394 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2395 debug_id, (u64)fda->num_fds);
2396 continue;
2397 }
2398 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2399 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2400 task_close_fd(proc, fd_array[fd_index]);
2401 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002402 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002403 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002404 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002405 break;
2406 }
2407 }
2408}
2409
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002410static int binder_translate_binder(struct flat_binder_object *fp,
2411 struct binder_transaction *t,
2412 struct binder_thread *thread)
2413{
2414 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002415 struct binder_proc *proc = thread->proc;
2416 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002417 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002418 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002419
2420 node = binder_get_node(proc, fp->binder);
2421 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002422 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002423 if (!node)
2424 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002425 }
2426 if (fp->cookie != node->cookie) {
2427 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2428 proc->pid, thread->pid, (u64)fp->binder,
2429 node->debug_id, (u64)fp->cookie,
2430 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002431 ret = -EINVAL;
2432 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002433 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002434 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2435 ret = -EPERM;
2436 goto done;
2437 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002438
Todd Kjosb0117bb2017-05-08 09:16:27 -07002439 ret = binder_inc_ref_for_node(target_proc, node,
2440 fp->hdr.type == BINDER_TYPE_BINDER,
2441 &thread->todo, &rdata);
2442 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002443 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002444
2445 if (fp->hdr.type == BINDER_TYPE_BINDER)
2446 fp->hdr.type = BINDER_TYPE_HANDLE;
2447 else
2448 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2449 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002450 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002451 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002452
Todd Kjosb0117bb2017-05-08 09:16:27 -07002453 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002454 binder_debug(BINDER_DEBUG_TRANSACTION,
2455 " node %d u%016llx -> ref %d desc %d\n",
2456 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002457 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002458done:
2459 binder_put_node(node);
2460 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002461}
2462
2463static int binder_translate_handle(struct flat_binder_object *fp,
2464 struct binder_transaction *t,
2465 struct binder_thread *thread)
2466{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002467 struct binder_proc *proc = thread->proc;
2468 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002469 struct binder_node *node;
2470 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002471 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002472
Todd Kjosb0117bb2017-05-08 09:16:27 -07002473 node = binder_get_node_from_ref(proc, fp->handle,
2474 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2475 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002476 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2477 proc->pid, thread->pid, fp->handle);
2478 return -EINVAL;
2479 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002480 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2481 ret = -EPERM;
2482 goto done;
2483 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002484
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002485 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002486 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002487 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2488 fp->hdr.type = BINDER_TYPE_BINDER;
2489 else
2490 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002491 fp->binder = node->ptr;
2492 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002493 if (node->proc)
2494 binder_inner_proc_lock(node->proc);
2495 binder_inc_node_nilocked(node,
2496 fp->hdr.type == BINDER_TYPE_BINDER,
2497 0, NULL);
2498 if (node->proc)
2499 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002500 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002501 binder_debug(BINDER_DEBUG_TRANSACTION,
2502 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002503 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2504 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002505 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002506 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002507 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002508
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002509 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002510 ret = binder_inc_ref_for_node(target_proc, node,
2511 fp->hdr.type == BINDER_TYPE_HANDLE,
2512 NULL, &dest_rdata);
2513 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002514 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002515
2516 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002517 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002518 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002519 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2520 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002521 binder_debug(BINDER_DEBUG_TRANSACTION,
2522 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002523 src_rdata.debug_id, src_rdata.desc,
2524 dest_rdata.debug_id, dest_rdata.desc,
2525 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002526 }
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_fd(int fd,
2533 struct binder_transaction *t,
2534 struct binder_thread *thread,
2535 struct binder_transaction *in_reply_to)
2536{
2537 struct binder_proc *proc = thread->proc;
2538 struct binder_proc *target_proc = t->to_proc;
2539 int target_fd;
2540 struct file *file;
2541 int ret;
2542 bool target_allows_fd;
2543
2544 if (in_reply_to)
2545 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2546 else
2547 target_allows_fd = t->buffer->target_node->accept_fds;
2548 if (!target_allows_fd) {
2549 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2550 proc->pid, thread->pid,
2551 in_reply_to ? "reply" : "transaction",
2552 fd);
2553 ret = -EPERM;
2554 goto err_fd_not_accepted;
2555 }
2556
2557 file = fget(fd);
2558 if (!file) {
2559 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2560 proc->pid, thread->pid, fd);
2561 ret = -EBADF;
2562 goto err_fget;
2563 }
2564 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2565 if (ret < 0) {
2566 ret = -EPERM;
2567 goto err_security;
2568 }
2569
2570 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2571 if (target_fd < 0) {
2572 ret = -ENOMEM;
2573 goto err_get_unused_fd;
2574 }
2575 task_fd_install(target_proc, target_fd, file);
2576 trace_binder_transaction_fd(t, fd, target_fd);
2577 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2578 fd, target_fd);
2579
2580 return target_fd;
2581
2582err_get_unused_fd:
2583err_security:
2584 fput(file);
2585err_fget:
2586err_fd_not_accepted:
2587 return ret;
2588}
2589
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002590static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2591 struct binder_buffer_object *parent,
2592 struct binder_transaction *t,
2593 struct binder_thread *thread,
2594 struct binder_transaction *in_reply_to)
2595{
2596 binder_size_t fdi, fd_buf_size, num_installed_fds;
2597 int target_fd;
2598 uintptr_t parent_buffer;
2599 u32 *fd_array;
2600 struct binder_proc *proc = thread->proc;
2601 struct binder_proc *target_proc = t->to_proc;
2602
2603 fd_buf_size = sizeof(u32) * fda->num_fds;
2604 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2605 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2606 proc->pid, thread->pid, (u64)fda->num_fds);
2607 return -EINVAL;
2608 }
2609 if (fd_buf_size > parent->length ||
2610 fda->parent_offset > parent->length - fd_buf_size) {
2611 /* No space for all file descriptors here. */
2612 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2613 proc->pid, thread->pid, (u64)fda->num_fds);
2614 return -EINVAL;
2615 }
2616 /*
2617 * Since the parent was already fixed up, convert it
2618 * back to the kernel address space to access it
2619 */
Todd Kjosd325d372016-10-10 10:40:53 -07002620 parent_buffer = parent->buffer -
2621 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002622 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2623 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2624 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2625 proc->pid, thread->pid);
2626 return -EINVAL;
2627 }
2628 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2629 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2630 in_reply_to);
2631 if (target_fd < 0)
2632 goto err_translate_fd_failed;
2633 fd_array[fdi] = target_fd;
2634 }
2635 return 0;
2636
2637err_translate_fd_failed:
2638 /*
2639 * Failed to allocate fd or security error, free fds
2640 * installed so far.
2641 */
2642 num_installed_fds = fdi;
2643 for (fdi = 0; fdi < num_installed_fds; fdi++)
2644 task_close_fd(target_proc, fd_array[fdi]);
2645 return target_fd;
2646}
2647
Martijn Coenen5a6da532016-09-30 14:10:07 +02002648static int binder_fixup_parent(struct binder_transaction *t,
2649 struct binder_thread *thread,
2650 struct binder_buffer_object *bp,
2651 binder_size_t *off_start,
2652 binder_size_t num_valid,
2653 struct binder_buffer_object *last_fixup_obj,
2654 binder_size_t last_fixup_min_off)
2655{
2656 struct binder_buffer_object *parent;
2657 u8 *parent_buffer;
2658 struct binder_buffer *b = t->buffer;
2659 struct binder_proc *proc = thread->proc;
2660 struct binder_proc *target_proc = t->to_proc;
2661
2662 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2663 return 0;
2664
2665 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2666 if (!parent) {
2667 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2668 proc->pid, thread->pid);
2669 return -EINVAL;
2670 }
2671
2672 if (!binder_validate_fixup(b, off_start,
2673 parent, bp->parent_offset,
2674 last_fixup_obj,
2675 last_fixup_min_off)) {
2676 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2677 proc->pid, thread->pid);
2678 return -EINVAL;
2679 }
2680
2681 if (parent->length < sizeof(binder_uintptr_t) ||
2682 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2683 /* No space for a pointer here! */
2684 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2685 proc->pid, thread->pid);
2686 return -EINVAL;
2687 }
2688 parent_buffer = (u8 *)(parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002689 binder_alloc_get_user_buffer_offset(
2690 &target_proc->alloc));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002691 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2692
2693 return 0;
2694}
2695
Martijn Coenen053be422017-06-06 15:17:46 -07002696/**
2697 * binder_proc_transaction() - sends a transaction to a process and wakes it up
2698 * @t: transaction to send
2699 * @proc: process to send the transaction to
2700 * @thread: thread in @proc to send the transaction to (may be NULL)
2701 *
2702 * This function queues a transaction to the specified process. It will try
2703 * to find a thread in the target process to handle the transaction and
2704 * wake it up. If no thread is found, the work is queued to the proc
2705 * waitqueue.
2706 *
2707 * If the @thread parameter is not NULL, the transaction is always queued
2708 * to the waitlist of that specific thread.
2709 *
2710 * Return: true if the transactions was successfully queued
2711 * false if the target process or thread is dead
2712 */
2713static bool binder_proc_transaction(struct binder_transaction *t,
2714 struct binder_proc *proc,
2715 struct binder_thread *thread)
2716{
2717 struct list_head *target_list = NULL;
2718 struct binder_node *node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002719 struct binder_priority node_prio;
Martijn Coenen053be422017-06-06 15:17:46 -07002720 bool oneway = !!(t->flags & TF_ONE_WAY);
2721 bool wakeup = true;
2722
2723 BUG_ON(!node);
2724 binder_node_lock(node);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002725 node_prio.prio = node->min_priority;
2726 node_prio.sched_policy = node->sched_policy;
2727
Martijn Coenen053be422017-06-06 15:17:46 -07002728 if (oneway) {
2729 BUG_ON(thread);
2730 if (node->has_async_transaction) {
2731 target_list = &node->async_todo;
2732 wakeup = false;
2733 } else {
2734 node->has_async_transaction = 1;
2735 }
2736 }
2737
2738 binder_inner_proc_lock(proc);
2739
2740 if (proc->is_dead || (thread && thread->is_dead)) {
2741 binder_inner_proc_unlock(proc);
2742 binder_node_unlock(node);
2743 return false;
2744 }
2745
2746 if (!thread && !target_list)
2747 thread = binder_select_thread_ilocked(proc);
2748
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002749 if (thread) {
Martijn Coenen053be422017-06-06 15:17:46 -07002750 target_list = &thread->todo;
Martijn Coenenc46810c2017-06-23 10:13:43 -07002751 binder_transaction_priority(thread->task, t, node_prio,
2752 node->inherit_rt);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002753 } else if (!target_list) {
Martijn Coenen053be422017-06-06 15:17:46 -07002754 target_list = &proc->todo;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002755 } else {
Martijn Coenen053be422017-06-06 15:17:46 -07002756 BUG_ON(target_list != &node->async_todo);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07002757 }
Martijn Coenen053be422017-06-06 15:17:46 -07002758
2759 binder_enqueue_work_ilocked(&t->work, target_list);
2760
2761 if (wakeup)
2762 binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */);
2763
2764 binder_inner_proc_unlock(proc);
2765 binder_node_unlock(node);
2766
2767 return true;
2768}
2769
Todd Kjos291d9682017-09-25 08:55:09 -07002770/**
2771 * binder_get_node_refs_for_txn() - Get required refs on node for txn
2772 * @node: struct binder_node for which to get refs
2773 * @proc: returns @node->proc if valid
2774 * @error: if no @proc then returns BR_DEAD_REPLY
2775 *
2776 * User-space normally keeps the node alive when creating a transaction
2777 * since it has a reference to the target. The local strong ref keeps it
2778 * alive if the sending process dies before the target process processes
2779 * the transaction. If the source process is malicious or has a reference
2780 * counting bug, relying on the local strong ref can fail.
2781 *
2782 * Since user-space can cause the local strong ref to go away, we also take
2783 * a tmpref on the node to ensure it survives while we are constructing
2784 * the transaction. We also need a tmpref on the proc while we are
2785 * constructing the transaction, so we take that here as well.
2786 *
2787 * Return: The target_node with refs taken or NULL if no @node->proc is NULL.
2788 * Also sets @proc if valid. If the @node->proc is NULL indicating that the
2789 * target proc has died, @error is set to BR_DEAD_REPLY
2790 */
2791static struct binder_node *binder_get_node_refs_for_txn(
2792 struct binder_node *node,
2793 struct binder_proc **procp,
2794 uint32_t *error)
2795{
2796 struct binder_node *target_node = NULL;
2797
2798 binder_node_inner_lock(node);
2799 if (node->proc) {
2800 target_node = node;
2801 binder_inc_node_nilocked(node, 1, 0, NULL);
2802 binder_inc_node_tmpref_ilocked(node);
2803 node->proc->tmp_ref++;
2804 *procp = node->proc;
2805 } else
2806 *error = BR_DEAD_REPLY;
2807 binder_node_inner_unlock(node);
2808
2809 return target_node;
2810}
2811
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002812static void binder_transaction(struct binder_proc *proc,
2813 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02002814 struct binder_transaction_data *tr, int reply,
2815 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002816{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002817 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002818 struct binder_transaction *t;
2819 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002820 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002821 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002822 u8 *sg_bufp, *sg_buf_end;
Todd Kjos2f993e22017-05-12 14:42:55 -07002823 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002824 struct binder_thread *target_thread = NULL;
2825 struct binder_node *target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002826 struct binder_transaction *in_reply_to = NULL;
2827 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07002828 uint32_t return_error = 0;
2829 uint32_t return_error_param = 0;
2830 uint32_t return_error_line = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002831 struct binder_buffer_object *last_fixup_obj = NULL;
2832 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002833 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07002834 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002835
2836 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07002837 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002838 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2839 e->from_proc = proc->pid;
2840 e->from_thread = thread->pid;
2841 e->target_handle = tr->target.handle;
2842 e->data_size = tr->data_size;
2843 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02002844 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002845
2846 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002847 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002848 in_reply_to = thread->transaction_stack;
2849 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002850 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302851 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002852 proc->pid, thread->pid);
2853 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002854 return_error_param = -EPROTO;
2855 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002856 goto err_empty_call_stack;
2857 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002858 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002859 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302860 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 +09002861 proc->pid, thread->pid, in_reply_to->debug_id,
2862 in_reply_to->to_proc ?
2863 in_reply_to->to_proc->pid : 0,
2864 in_reply_to->to_thread ?
2865 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002866 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002867 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002868 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002869 return_error_param = -EPROTO;
2870 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002871 in_reply_to = NULL;
2872 goto err_bad_call_stack;
2873 }
2874 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002875 binder_inner_proc_unlock(proc);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002876 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002877 if (target_thread == NULL) {
2878 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002879 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002880 goto err_dead_binder;
2881 }
2882 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302883 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 +09002884 proc->pid, thread->pid,
2885 target_thread->transaction_stack ?
2886 target_thread->transaction_stack->debug_id : 0,
2887 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002888 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002889 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002890 return_error_param = -EPROTO;
2891 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002892 in_reply_to = NULL;
2893 target_thread = NULL;
2894 goto err_dead_binder;
2895 }
2896 target_proc = target_thread->proc;
Todd Kjos2f993e22017-05-12 14:42:55 -07002897 target_proc->tmp_ref++;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002898 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002899 } else {
2900 if (tr->target.handle) {
2901 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002902
Todd Kjosc37162d2017-05-26 11:56:29 -07002903 /*
2904 * There must already be a strong ref
2905 * on this node. If so, do a strong
2906 * increment on the node to ensure it
2907 * stays alive until the transaction is
2908 * done.
2909 */
Todd Kjos5346bf32016-10-20 16:43:34 -07002910 binder_proc_lock(proc);
2911 ref = binder_get_ref_olocked(proc, tr->target.handle,
2912 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07002913 if (ref) {
Todd Kjos291d9682017-09-25 08:55:09 -07002914 target_node = binder_get_node_refs_for_txn(
2915 ref->node, &target_proc,
2916 &return_error);
2917 } else {
2918 binder_user_error("%d:%d got transaction to invalid handle\n",
2919 proc->pid, thread->pid);
2920 return_error = BR_FAILED_REPLY;
Todd Kjosc37162d2017-05-26 11:56:29 -07002921 }
Todd Kjos5346bf32016-10-20 16:43:34 -07002922 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002923 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002924 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002925 target_node = context->binder_context_mgr_node;
Todd Kjos291d9682017-09-25 08:55:09 -07002926 if (target_node)
2927 target_node = binder_get_node_refs_for_txn(
2928 target_node, &target_proc,
2929 &return_error);
2930 else
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002931 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002932 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002933 }
Todd Kjos291d9682017-09-25 08:55:09 -07002934 if (!target_node) {
2935 /*
2936 * return_error is set above
2937 */
2938 return_error_param = -EINVAL;
Todd Kjose598d172017-03-22 17:19:52 -07002939 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002940 goto err_dead_binder;
2941 }
Todd Kjos291d9682017-09-25 08:55:09 -07002942 e->to_node = target_node->debug_id;
Stephen Smalley79af7302015-01-21 10:54:10 -05002943 if (security_binder_transaction(proc->tsk,
2944 target_proc->tsk) < 0) {
2945 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002946 return_error_param = -EPERM;
2947 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05002948 goto err_invalid_target_handle;
2949 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002950 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002951 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2952 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002953
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002954 tmp = thread->transaction_stack;
2955 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002956 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302957 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 +09002958 proc->pid, thread->pid, tmp->debug_id,
2959 tmp->to_proc ? tmp->to_proc->pid : 0,
2960 tmp->to_thread ?
2961 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002962 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002963 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002964 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002965 return_error_param = -EPROTO;
2966 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002967 goto err_bad_call_stack;
2968 }
2969 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002970 struct binder_thread *from;
2971
2972 spin_lock(&tmp->lock);
2973 from = tmp->from;
2974 if (from && from->proc == target_proc) {
2975 atomic_inc(&from->tmp_ref);
2976 target_thread = from;
2977 spin_unlock(&tmp->lock);
2978 break;
2979 }
2980 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002981 tmp = tmp->from_parent;
2982 }
2983 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002984 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002985 }
Martijn Coenen053be422017-06-06 15:17:46 -07002986 if (target_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002987 e->to_thread = target_thread->pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002988 e->to_proc = target_proc->pid;
2989
2990 /* TODO: reuse incoming transaction for reply */
2991 t = kzalloc(sizeof(*t), GFP_KERNEL);
2992 if (t == NULL) {
2993 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002994 return_error_param = -ENOMEM;
2995 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002996 goto err_alloc_t_failed;
2997 }
2998 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07002999 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003000
3001 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
3002 if (tcomplete == NULL) {
3003 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003004 return_error_param = -ENOMEM;
3005 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003006 goto err_alloc_tcomplete_failed;
3007 }
3008 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
3009
Todd Kjos1cfe6272017-05-24 13:33:28 -07003010 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003011
3012 if (reply)
3013 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003014 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003015 proc->pid, thread->pid, t->debug_id,
3016 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003017 (u64)tr->data.ptr.buffer,
3018 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003019 (u64)tr->data_size, (u64)tr->offsets_size,
3020 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003021 else
3022 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02003023 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003024 proc->pid, thread->pid, t->debug_id,
3025 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003026 (u64)tr->data.ptr.buffer,
3027 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02003028 (u64)tr->data_size, (u64)tr->offsets_size,
3029 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003030
3031 if (!reply && !(tr->flags & TF_ONE_WAY))
3032 t->from = thread;
3033 else
3034 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03003035 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003036 t->to_proc = target_proc;
3037 t->to_thread = target_thread;
3038 t->code = tr->code;
3039 t->flags = tr->flags;
Martijn Coenen57b2ac62017-06-06 17:04:42 -07003040 if (!(t->flags & TF_ONE_WAY) &&
3041 binder_supported_policy(current->policy)) {
3042 /* Inherit supported policies for synchronous transactions */
3043 t->priority.sched_policy = current->policy;
3044 t->priority.prio = current->normal_prio;
3045 } else {
3046 /* Otherwise, fall back to the default priority */
3047 t->priority = target_proc->default_priority;
3048 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003049
3050 trace_binder_transaction(reply, t, target_node);
3051
Todd Kjosd325d372016-10-10 10:40:53 -07003052 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02003053 tr->offsets_size, extra_buffers_size,
3054 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07003055 if (IS_ERR(t->buffer)) {
3056 /*
3057 * -ESRCH indicates VMA cleared. The target is dying.
3058 */
3059 return_error_param = PTR_ERR(t->buffer);
3060 return_error = return_error_param == -ESRCH ?
3061 BR_DEAD_REPLY : BR_FAILED_REPLY;
3062 return_error_line = __LINE__;
3063 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003064 goto err_binder_alloc_buf_failed;
3065 }
3066 t->buffer->allow_user_free = 0;
3067 t->buffer->debug_id = t->debug_id;
3068 t->buffer->transaction = t;
3069 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003070 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003071 off_start = (binder_size_t *)(t->buffer->data +
3072 ALIGN(tr->data_size, sizeof(void *)));
3073 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003074
Arve Hjønnevågda498892014-02-21 14:40:26 -08003075 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
3076 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303077 binder_user_error("%d:%d got transaction with invalid data ptr\n",
3078 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003079 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003080 return_error_param = -EFAULT;
3081 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003082 goto err_copy_data_failed;
3083 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003084 if (copy_from_user(offp, (const void __user *)(uintptr_t)
3085 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303086 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3087 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003088 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003089 return_error_param = -EFAULT;
3090 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003091 goto err_copy_data_failed;
3092 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08003093 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
3094 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
3095 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003096 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003097 return_error_param = -EINVAL;
3098 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003099 goto err_bad_offset;
3100 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02003101 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
3102 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
3103 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05303104 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003105 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003106 return_error_param = -EINVAL;
3107 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003108 goto err_bad_offset;
3109 }
3110 off_end = (void *)off_start + tr->offsets_size;
3111 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
3112 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08003113 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003114 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02003115 struct binder_object_header *hdr;
3116 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09003117
Martijn Coenen00c80372016-07-13 12:06:49 +02003118 if (object_size == 0 || *offp < off_min) {
3119 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 -08003120 proc->pid, thread->pid, (u64)*offp,
3121 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02003122 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003123 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003124 return_error_param = -EINVAL;
3125 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003126 goto err_bad_offset;
3127 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003128
3129 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
3130 off_min = *offp + object_size;
3131 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003132 case BINDER_TYPE_BINDER:
3133 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003134 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09003135
Martijn Coenen00c80372016-07-13 12:06:49 +02003136 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003137 ret = binder_translate_binder(fp, t, thread);
3138 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02003139 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003140 return_error_param = ret;
3141 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003142 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003143 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003144 } break;
3145 case BINDER_TYPE_HANDLE:
3146 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003147 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02003148
Martijn Coenen00c80372016-07-13 12:06:49 +02003149 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003150 ret = binder_translate_handle(fp, t, thread);
3151 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003152 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003153 return_error_param = ret;
3154 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003155 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003156 }
3157 } break;
3158
3159 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02003160 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003161 int target_fd = binder_translate_fd(fp->fd, t, thread,
3162 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003163
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003164 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003165 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003166 return_error_param = target_fd;
3167 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003168 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003169 }
Martijn Coenen00c80372016-07-13 12:06:49 +02003170 fp->pad_binder = 0;
3171 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003172 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003173 case BINDER_TYPE_FDA: {
3174 struct binder_fd_array_object *fda =
3175 to_binder_fd_array_object(hdr);
3176 struct binder_buffer_object *parent =
3177 binder_validate_ptr(t->buffer, fda->parent,
3178 off_start,
3179 offp - off_start);
3180 if (!parent) {
3181 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
3182 proc->pid, thread->pid);
3183 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003184 return_error_param = -EINVAL;
3185 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003186 goto err_bad_parent;
3187 }
3188 if (!binder_validate_fixup(t->buffer, off_start,
3189 parent, fda->parent_offset,
3190 last_fixup_obj,
3191 last_fixup_min_off)) {
3192 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
3193 proc->pid, thread->pid);
3194 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003195 return_error_param = -EINVAL;
3196 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003197 goto err_bad_parent;
3198 }
3199 ret = binder_translate_fd_array(fda, parent, t, thread,
3200 in_reply_to);
3201 if (ret < 0) {
3202 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003203 return_error_param = ret;
3204 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003205 goto err_translate_failed;
3206 }
3207 last_fixup_obj = parent;
3208 last_fixup_min_off =
3209 fda->parent_offset + sizeof(u32) * fda->num_fds;
3210 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003211 case BINDER_TYPE_PTR: {
3212 struct binder_buffer_object *bp =
3213 to_binder_buffer_object(hdr);
3214 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003215
Martijn Coenen5a6da532016-09-30 14:10:07 +02003216 if (bp->length > buf_left) {
3217 binder_user_error("%d:%d got transaction with too large buffer\n",
3218 proc->pid, thread->pid);
3219 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003220 return_error_param = -EINVAL;
3221 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003222 goto err_bad_offset;
3223 }
3224 if (copy_from_user(sg_bufp,
3225 (const void __user *)(uintptr_t)
3226 bp->buffer, bp->length)) {
3227 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
3228 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07003229 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003230 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003231 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003232 goto err_copy_data_failed;
3233 }
3234 /* Fixup buffer pointer to target proc address space */
3235 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjosd325d372016-10-10 10:40:53 -07003236 binder_alloc_get_user_buffer_offset(
3237 &target_proc->alloc);
Martijn Coenen5a6da532016-09-30 14:10:07 +02003238 sg_bufp += ALIGN(bp->length, sizeof(u64));
3239
3240 ret = binder_fixup_parent(t, thread, bp, off_start,
3241 offp - off_start,
3242 last_fixup_obj,
3243 last_fixup_min_off);
3244 if (ret < 0) {
3245 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003246 return_error_param = ret;
3247 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02003248 goto err_translate_failed;
3249 }
3250 last_fixup_obj = bp;
3251 last_fixup_min_off = 0;
3252 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003253 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01003254 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02003255 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003256 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07003257 return_error_param = -EINVAL;
3258 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003259 goto err_bad_object_type;
3260 }
3261 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003262 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003263 binder_enqueue_work(proc, tcomplete, &thread->todo);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003264 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07003265
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003266 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003267 binder_inner_proc_lock(target_proc);
3268 if (target_thread->is_dead) {
3269 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003270 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003271 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003272 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003273 binder_pop_transaction_ilocked(target_thread, in_reply_to);
Martijn Coenen053be422017-06-06 15:17:46 -07003274 binder_enqueue_work_ilocked(&t->work, &target_thread->todo);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003275 binder_inner_proc_unlock(target_proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003276 wake_up_interruptible_sync(&target_thread->wait);
Martijn Coenenecd972d2017-05-26 10:48:56 -07003277 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003278 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003279 } else if (!(t->flags & TF_ONE_WAY)) {
3280 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003281 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003282 t->need_reply = 1;
3283 t->from_parent = thread->transaction_stack;
3284 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003285 binder_inner_proc_unlock(proc);
Martijn Coenen053be422017-06-06 15:17:46 -07003286 if (!binder_proc_transaction(t, target_proc, target_thread)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003287 binder_inner_proc_lock(proc);
3288 binder_pop_transaction_ilocked(thread, t);
3289 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07003290 goto err_dead_proc_or_thread;
3291 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003292 } else {
3293 BUG_ON(target_node == NULL);
3294 BUG_ON(t->buffer->async_transaction != 1);
Martijn Coenen053be422017-06-06 15:17:46 -07003295 if (!binder_proc_transaction(t, target_proc, NULL))
Todd Kjos2f993e22017-05-12 14:42:55 -07003296 goto err_dead_proc_or_thread;
Riley Andrewsb5968812015-09-01 12:42:07 -07003297 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003298 if (target_thread)
3299 binder_thread_dec_tmpref(target_thread);
3300 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003301 if (target_node)
3302 binder_dec_node_tmpref(target_node);
Todd Kjos1cfe6272017-05-24 13:33:28 -07003303 /*
3304 * write barrier to synchronize with initialization
3305 * of log entry
3306 */
3307 smp_wmb();
3308 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003309 return;
3310
Todd Kjos2f993e22017-05-12 14:42:55 -07003311err_dead_proc_or_thread:
3312 return_error = BR_DEAD_REPLY;
3313 return_error_line = __LINE__;
Xu YiPing86578a02017-05-22 11:26:23 -07003314 binder_dequeue_work(proc, tcomplete);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02003315err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003316err_bad_object_type:
3317err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02003318err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003319err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003320 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003321 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjos291d9682017-09-25 08:55:09 -07003322 if (target_node)
3323 binder_dec_node_tmpref(target_node);
Todd Kjosc37162d2017-05-26 11:56:29 -07003324 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003325 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07003326 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003327err_binder_alloc_buf_failed:
3328 kfree(tcomplete);
3329 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3330err_alloc_tcomplete_failed:
3331 kfree(t);
3332 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3333err_alloc_t_failed:
3334err_bad_call_stack:
3335err_empty_call_stack:
3336err_dead_binder:
3337err_invalid_target_handle:
Todd Kjos2f993e22017-05-12 14:42:55 -07003338 if (target_thread)
3339 binder_thread_dec_tmpref(target_thread);
3340 if (target_proc)
3341 binder_proc_dec_tmpref(target_proc);
Todd Kjos291d9682017-09-25 08:55:09 -07003342 if (target_node) {
Todd Kjosc37162d2017-05-26 11:56:29 -07003343 binder_dec_node(target_node, 1, 0);
Todd Kjos291d9682017-09-25 08:55:09 -07003344 binder_dec_node_tmpref(target_node);
3345 }
Todd Kjosc37162d2017-05-26 11:56:29 -07003346
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003347 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07003348 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
3349 proc->pid, thread->pid, return_error, return_error_param,
3350 (u64)tr->data_size, (u64)tr->offsets_size,
3351 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003352
3353 {
3354 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09003355
Todd Kjose598d172017-03-22 17:19:52 -07003356 e->return_error = return_error;
3357 e->return_error_param = return_error_param;
3358 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003359 fe = binder_transaction_log_add(&binder_transaction_log_failed);
3360 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07003361 /*
3362 * write barrier to synchronize with initialization
3363 * of log entry
3364 */
3365 smp_wmb();
3366 WRITE_ONCE(e->debug_id_done, t_debug_id);
3367 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003368 }
3369
Todd Kjos858b8da2017-04-21 17:35:12 -07003370 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003371 if (in_reply_to) {
Martijn Coenenecd972d2017-05-26 10:48:56 -07003372 binder_restore_priority(current, in_reply_to->saved_priority);
Todd Kjos858b8da2017-04-21 17:35:12 -07003373 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003374 binder_enqueue_work(thread->proc,
3375 &thread->return_error.work,
3376 &thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003377 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07003378 } else {
3379 thread->return_error.cmd = return_error;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003380 binder_enqueue_work(thread->proc,
3381 &thread->return_error.work,
3382 &thread->todo);
Todd Kjos858b8da2017-04-21 17:35:12 -07003383 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003384}
3385
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003386static int binder_thread_write(struct binder_proc *proc,
3387 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003388 binder_uintptr_t binder_buffer, size_t size,
3389 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003390{
3391 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003392 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003393 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003394 void __user *ptr = buffer + *consumed;
3395 void __user *end = buffer + size;
3396
Todd Kjos858b8da2017-04-21 17:35:12 -07003397 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003398 int ret;
3399
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003400 if (get_user(cmd, (uint32_t __user *)ptr))
3401 return -EFAULT;
3402 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003403 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003404 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003405 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3406 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3407 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003408 }
3409 switch (cmd) {
3410 case BC_INCREFS:
3411 case BC_ACQUIRE:
3412 case BC_RELEASE:
3413 case BC_DECREFS: {
3414 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003415 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003416 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3417 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3418 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003419
3420 if (get_user(target, (uint32_t __user *)ptr))
3421 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003422
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003423 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003424 ret = -1;
3425 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003426 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003427 mutex_lock(&context->context_mgr_node_lock);
3428 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003429 if (ctx_mgr_node)
3430 ret = binder_inc_ref_for_node(
3431 proc, ctx_mgr_node,
3432 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003433 mutex_unlock(&context->context_mgr_node_lock);
3434 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003435 if (ret)
3436 ret = binder_update_ref_for_handle(
3437 proc, target, increment, strong,
3438 &rdata);
3439 if (!ret && rdata.desc != target) {
3440 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3441 proc->pid, thread->pid,
3442 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003443 }
3444 switch (cmd) {
3445 case BC_INCREFS:
3446 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003447 break;
3448 case BC_ACQUIRE:
3449 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003450 break;
3451 case BC_RELEASE:
3452 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003453 break;
3454 case BC_DECREFS:
3455 default:
3456 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003457 break;
3458 }
3459 if (ret) {
3460 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3461 proc->pid, thread->pid, debug_string,
3462 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003463 break;
3464 }
3465 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003466 "%d:%d %s ref %d desc %d s %d w %d\n",
3467 proc->pid, thread->pid, debug_string,
3468 rdata.debug_id, rdata.desc, rdata.strong,
3469 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003470 break;
3471 }
3472 case BC_INCREFS_DONE:
3473 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003474 binder_uintptr_t node_ptr;
3475 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003476 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003477 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003478
Arve Hjønnevågda498892014-02-21 14:40:26 -08003479 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003480 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003481 ptr += sizeof(binder_uintptr_t);
3482 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003483 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003484 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003485 node = binder_get_node(proc, node_ptr);
3486 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003487 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003488 proc->pid, thread->pid,
3489 cmd == BC_INCREFS_DONE ?
3490 "BC_INCREFS_DONE" :
3491 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003492 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003493 break;
3494 }
3495 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003496 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003497 proc->pid, thread->pid,
3498 cmd == BC_INCREFS_DONE ?
3499 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003500 (u64)node_ptr, node->debug_id,
3501 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003502 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003503 break;
3504 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003505 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003506 if (cmd == BC_ACQUIRE_DONE) {
3507 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303508 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003509 proc->pid, thread->pid,
3510 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003511 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003512 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003513 break;
3514 }
3515 node->pending_strong_ref = 0;
3516 } else {
3517 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303518 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003519 proc->pid, thread->pid,
3520 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003521 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003522 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003523 break;
3524 }
3525 node->pending_weak_ref = 0;
3526 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003527 free_node = binder_dec_node_nilocked(node,
3528 cmd == BC_ACQUIRE_DONE, 0);
3529 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003530 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003531 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003532 proc->pid, thread->pid,
3533 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003534 node->debug_id, node->local_strong_refs,
3535 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003536 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003537 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003538 break;
3539 }
3540 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303541 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003542 return -EINVAL;
3543 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303544 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003545 return -EINVAL;
3546
3547 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003548 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003549 struct binder_buffer *buffer;
3550
Arve Hjønnevågda498892014-02-21 14:40:26 -08003551 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003552 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003553 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003554
Todd Kjos076072a2017-04-21 14:32:11 -07003555 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3556 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003557 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003558 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3559 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003560 break;
3561 }
3562 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003563 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3564 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003565 break;
3566 }
3567 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003568 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3569 proc->pid, thread->pid, (u64)data_ptr,
3570 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003571 buffer->transaction ? "active" : "finished");
3572
3573 if (buffer->transaction) {
3574 buffer->transaction->buffer = NULL;
3575 buffer->transaction = NULL;
3576 }
3577 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003578 struct binder_node *buf_node;
3579 struct binder_work *w;
3580
3581 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003582 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003583 BUG_ON(!buf_node->has_async_transaction);
3584 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003585 w = binder_dequeue_work_head_ilocked(
3586 &buf_node->async_todo);
Martijn Coenen4501c042017-08-10 13:56:16 +02003587 if (!w) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003588 buf_node->has_async_transaction = 0;
Martijn Coenen4501c042017-08-10 13:56:16 +02003589 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003590 binder_enqueue_work_ilocked(
Martijn Coenen4501c042017-08-10 13:56:16 +02003591 w, &proc->todo);
3592 binder_wakeup_proc_ilocked(proc);
3593 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003594 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003595 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003596 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003597 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07003598 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003599 break;
3600 }
3601
Martijn Coenen5a6da532016-09-30 14:10:07 +02003602 case BC_TRANSACTION_SG:
3603 case BC_REPLY_SG: {
3604 struct binder_transaction_data_sg tr;
3605
3606 if (copy_from_user(&tr, ptr, sizeof(tr)))
3607 return -EFAULT;
3608 ptr += sizeof(tr);
3609 binder_transaction(proc, thread, &tr.transaction_data,
3610 cmd == BC_REPLY_SG, tr.buffers_size);
3611 break;
3612 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003613 case BC_TRANSACTION:
3614 case BC_REPLY: {
3615 struct binder_transaction_data tr;
3616
3617 if (copy_from_user(&tr, ptr, sizeof(tr)))
3618 return -EFAULT;
3619 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003620 binder_transaction(proc, thread, &tr,
3621 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003622 break;
3623 }
3624
3625 case BC_REGISTER_LOOPER:
3626 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303627 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003628 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003629 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003630 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3631 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303632 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003633 proc->pid, thread->pid);
3634 } else if (proc->requested_threads == 0) {
3635 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303636 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003637 proc->pid, thread->pid);
3638 } else {
3639 proc->requested_threads--;
3640 proc->requested_threads_started++;
3641 }
3642 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003643 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003644 break;
3645 case BC_ENTER_LOOPER:
3646 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303647 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003648 proc->pid, thread->pid);
3649 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3650 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303651 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003652 proc->pid, thread->pid);
3653 }
3654 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3655 break;
3656 case BC_EXIT_LOOPER:
3657 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303658 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003659 proc->pid, thread->pid);
3660 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3661 break;
3662
3663 case BC_REQUEST_DEATH_NOTIFICATION:
3664 case BC_CLEAR_DEATH_NOTIFICATION: {
3665 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003666 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003667 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003668 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003669
3670 if (get_user(target, (uint32_t __user *)ptr))
3671 return -EFAULT;
3672 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003673 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003674 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003675 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003676 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3677 /*
3678 * Allocate memory for death notification
3679 * before taking lock
3680 */
3681 death = kzalloc(sizeof(*death), GFP_KERNEL);
3682 if (death == NULL) {
3683 WARN_ON(thread->return_error.cmd !=
3684 BR_OK);
3685 thread->return_error.cmd = BR_ERROR;
3686 binder_enqueue_work(
3687 thread->proc,
3688 &thread->return_error.work,
3689 &thread->todo);
3690 binder_debug(
3691 BINDER_DEBUG_FAILED_TRANSACTION,
3692 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3693 proc->pid, thread->pid);
3694 break;
3695 }
3696 }
3697 binder_proc_lock(proc);
3698 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003699 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303700 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003701 proc->pid, thread->pid,
3702 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3703 "BC_REQUEST_DEATH_NOTIFICATION" :
3704 "BC_CLEAR_DEATH_NOTIFICATION",
3705 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003706 binder_proc_unlock(proc);
3707 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003708 break;
3709 }
3710
3711 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003712 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003713 proc->pid, thread->pid,
3714 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3715 "BC_REQUEST_DEATH_NOTIFICATION" :
3716 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003717 (u64)cookie, ref->data.debug_id,
3718 ref->data.desc, ref->data.strong,
3719 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003720
Martijn Coenenf9eac642017-05-22 11:26:23 -07003721 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003722 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3723 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303724 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003725 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003726 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003727 binder_proc_unlock(proc);
3728 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003729 break;
3730 }
3731 binder_stats_created(BINDER_STAT_DEATH);
3732 INIT_LIST_HEAD(&death->work.entry);
3733 death->cookie = cookie;
3734 ref->death = death;
3735 if (ref->node->proc == NULL) {
3736 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Martijn Coenen3bdbe4c2017-08-10 13:50:52 +02003737
3738 binder_inner_proc_lock(proc);
3739 binder_enqueue_work_ilocked(
3740 &ref->death->work, &proc->todo);
3741 binder_wakeup_proc_ilocked(proc);
3742 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003743 }
3744 } else {
3745 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303746 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003747 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003748 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003749 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003750 break;
3751 }
3752 death = ref->death;
3753 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003754 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003755 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003756 (u64)death->cookie,
3757 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003758 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003759 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003760 break;
3761 }
3762 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003763 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003764 if (list_empty(&death->work.entry)) {
3765 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003766 if (thread->looper &
3767 (BINDER_LOOPER_STATE_REGISTERED |
3768 BINDER_LOOPER_STATE_ENTERED))
3769 binder_enqueue_work_ilocked(
3770 &death->work,
3771 &thread->todo);
3772 else {
3773 binder_enqueue_work_ilocked(
3774 &death->work,
3775 &proc->todo);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003776 binder_wakeup_proc_ilocked(
Martijn Coenen053be422017-06-06 15:17:46 -07003777 proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003778 }
3779 } else {
3780 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3781 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3782 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003783 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003784 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07003785 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003786 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003787 } break;
3788 case BC_DEAD_BINDER_DONE: {
3789 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003790 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003791 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003792
Arve Hjønnevågda498892014-02-21 14:40:26 -08003793 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003794 return -EFAULT;
3795
Lisa Du7a64cd82016-02-17 09:32:52 +08003796 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003797 binder_inner_proc_lock(proc);
3798 list_for_each_entry(w, &proc->delivered_death,
3799 entry) {
3800 struct binder_ref_death *tmp_death =
3801 container_of(w,
3802 struct binder_ref_death,
3803 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003804
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003805 if (tmp_death->cookie == cookie) {
3806 death = tmp_death;
3807 break;
3808 }
3809 }
3810 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003811 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3812 proc->pid, thread->pid, (u64)cookie,
3813 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003814 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003815 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3816 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003817 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003818 break;
3819 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003820 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003821 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3822 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003823 if (thread->looper &
3824 (BINDER_LOOPER_STATE_REGISTERED |
3825 BINDER_LOOPER_STATE_ENTERED))
3826 binder_enqueue_work_ilocked(
3827 &death->work, &thread->todo);
3828 else {
3829 binder_enqueue_work_ilocked(
3830 &death->work,
3831 &proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07003832 binder_wakeup_proc_ilocked(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003833 }
3834 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003835 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003836 } break;
3837
3838 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303839 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003840 proc->pid, thread->pid, cmd);
3841 return -EINVAL;
3842 }
3843 *consumed = ptr - buffer;
3844 }
3845 return 0;
3846}
3847
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003848static void binder_stat_br(struct binder_proc *proc,
3849 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003850{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003851 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003852 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003853 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3854 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3855 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003856 }
3857}
3858
Todd Kjos60792612017-05-24 10:51:01 -07003859static int binder_put_node_cmd(struct binder_proc *proc,
3860 struct binder_thread *thread,
3861 void __user **ptrp,
3862 binder_uintptr_t node_ptr,
3863 binder_uintptr_t node_cookie,
3864 int node_debug_id,
3865 uint32_t cmd, const char *cmd_name)
3866{
3867 void __user *ptr = *ptrp;
3868
3869 if (put_user(cmd, (uint32_t __user *)ptr))
3870 return -EFAULT;
3871 ptr += sizeof(uint32_t);
3872
3873 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3874 return -EFAULT;
3875 ptr += sizeof(binder_uintptr_t);
3876
3877 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3878 return -EFAULT;
3879 ptr += sizeof(binder_uintptr_t);
3880
3881 binder_stat_br(proc, thread, cmd);
3882 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3883 proc->pid, thread->pid, cmd_name, node_debug_id,
3884 (u64)node_ptr, (u64)node_cookie);
3885
3886 *ptrp = ptr;
3887 return 0;
3888}
3889
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003890static int binder_wait_for_work(struct binder_thread *thread,
3891 bool do_proc_work)
3892{
3893 DEFINE_WAIT(wait);
3894 struct binder_proc *proc = thread->proc;
3895 int ret = 0;
3896
3897 freezer_do_not_count();
3898 binder_inner_proc_lock(proc);
3899 for (;;) {
3900 prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
3901 if (binder_has_work_ilocked(thread, do_proc_work))
3902 break;
3903 if (do_proc_work)
3904 list_add(&thread->waiting_thread_node,
3905 &proc->waiting_threads);
3906 binder_inner_proc_unlock(proc);
3907 schedule();
3908 binder_inner_proc_lock(proc);
3909 list_del_init(&thread->waiting_thread_node);
3910 if (signal_pending(current)) {
3911 ret = -ERESTARTSYS;
3912 break;
3913 }
3914 }
3915 finish_wait(&thread->wait, &wait);
3916 binder_inner_proc_unlock(proc);
3917 freezer_count();
3918
3919 return ret;
3920}
3921
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003922static int binder_thread_read(struct binder_proc *proc,
3923 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003924 binder_uintptr_t binder_buffer, size_t size,
3925 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003926{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003927 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003928 void __user *ptr = buffer + *consumed;
3929 void __user *end = buffer + size;
3930
3931 int ret = 0;
3932 int wait_for_proc_work;
3933
3934 if (*consumed == 0) {
3935 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3936 return -EFAULT;
3937 ptr += sizeof(uint32_t);
3938 }
3939
3940retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07003941 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003942 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07003943 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003944
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003945 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003946
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003947 trace_binder_wait_for_work(wait_for_proc_work,
3948 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003949 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003950 if (wait_for_proc_work) {
3951 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3952 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303953 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 +09003954 proc->pid, thread->pid, thread->looper);
3955 wait_event_interruptible(binder_user_error_wait,
3956 binder_stop_on_user_error < 2);
3957 }
Martijn Coenenecd972d2017-05-26 10:48:56 -07003958 binder_restore_priority(current, proc->default_priority);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003959 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003960
Martijn Coenen22d64e4322017-06-02 11:15:44 -07003961 if (non_block) {
3962 if (!binder_has_work(thread, wait_for_proc_work))
3963 ret = -EAGAIN;
3964 } else {
3965 ret = binder_wait_for_work(thread, wait_for_proc_work);
3966 }
3967
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003968 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3969
3970 if (ret)
3971 return ret;
3972
3973 while (1) {
3974 uint32_t cmd;
3975 struct binder_transaction_data tr;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003976 struct binder_work *w = NULL;
3977 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003978 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07003979 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003980
Todd Kjose7f23ed2017-03-21 13:06:01 -07003981 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003982 if (!binder_worklist_empty_ilocked(&thread->todo))
3983 list = &thread->todo;
3984 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3985 wait_for_proc_work)
3986 list = &proc->todo;
3987 else {
3988 binder_inner_proc_unlock(proc);
3989
Dmitry Voytik395262a2014-09-08 18:16:34 +04003990 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08003991 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003992 goto retry;
3993 break;
3994 }
3995
Todd Kjose7f23ed2017-03-21 13:06:01 -07003996 if (end - ptr < sizeof(tr) + 4) {
3997 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003998 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07003999 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004000 w = binder_dequeue_work_head_ilocked(list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004001
4002 switch (w->type) {
4003 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004004 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004005 t = container_of(w, struct binder_transaction, work);
4006 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004007 case BINDER_WORK_RETURN_ERROR: {
4008 struct binder_error *e = container_of(
4009 w, struct binder_error, work);
4010
4011 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004012 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07004013 if (put_user(e->cmd, (uint32_t __user *)ptr))
4014 return -EFAULT;
4015 e->cmd = BR_OK;
4016 ptr += sizeof(uint32_t);
4017
4018 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07004019 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004020 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004021 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004022 cmd = BR_TRANSACTION_COMPLETE;
4023 if (put_user(cmd, (uint32_t __user *)ptr))
4024 return -EFAULT;
4025 ptr += sizeof(uint32_t);
4026
4027 binder_stat_br(proc, thread, cmd);
4028 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304029 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004030 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004031 kfree(w);
4032 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4033 } break;
4034 case BINDER_WORK_NODE: {
4035 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07004036 int strong, weak;
4037 binder_uintptr_t node_ptr = node->ptr;
4038 binder_uintptr_t node_cookie = node->cookie;
4039 int node_debug_id = node->debug_id;
4040 int has_weak_ref;
4041 int has_strong_ref;
4042 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09004043
Todd Kjos60792612017-05-24 10:51:01 -07004044 BUG_ON(proc != node->proc);
4045 strong = node->internal_strong_refs ||
4046 node->local_strong_refs;
4047 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07004048 node->local_weak_refs ||
4049 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07004050 has_strong_ref = node->has_strong_ref;
4051 has_weak_ref = node->has_weak_ref;
4052
4053 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004054 node->has_weak_ref = 1;
4055 node->pending_weak_ref = 1;
4056 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004057 }
4058 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004059 node->has_strong_ref = 1;
4060 node->pending_strong_ref = 1;
4061 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07004062 }
4063 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004064 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004065 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004066 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07004067 if (!weak && !strong) {
4068 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4069 "%d:%d node %d u%016llx c%016llx deleted\n",
4070 proc->pid, thread->pid,
4071 node_debug_id,
4072 (u64)node_ptr,
4073 (u64)node_cookie);
4074 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004075 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004076 binder_node_lock(node);
4077 /*
4078 * Acquire the node lock before freeing the
4079 * node to serialize with other threads that
4080 * may have been holding the node lock while
4081 * decrementing this node (avoids race where
4082 * this thread frees while the other thread
4083 * is unlocking the node after the final
4084 * decrement)
4085 */
4086 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004087 binder_free_node(node);
4088 } else
4089 binder_inner_proc_unlock(proc);
4090
Todd Kjos60792612017-05-24 10:51:01 -07004091 if (weak && !has_weak_ref)
4092 ret = binder_put_node_cmd(
4093 proc, thread, &ptr, node_ptr,
4094 node_cookie, node_debug_id,
4095 BR_INCREFS, "BR_INCREFS");
4096 if (!ret && strong && !has_strong_ref)
4097 ret = binder_put_node_cmd(
4098 proc, thread, &ptr, node_ptr,
4099 node_cookie, node_debug_id,
4100 BR_ACQUIRE, "BR_ACQUIRE");
4101 if (!ret && !strong && has_strong_ref)
4102 ret = binder_put_node_cmd(
4103 proc, thread, &ptr, node_ptr,
4104 node_cookie, node_debug_id,
4105 BR_RELEASE, "BR_RELEASE");
4106 if (!ret && !weak && has_weak_ref)
4107 ret = binder_put_node_cmd(
4108 proc, thread, &ptr, node_ptr,
4109 node_cookie, node_debug_id,
4110 BR_DECREFS, "BR_DECREFS");
4111 if (orig_ptr == ptr)
4112 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
4113 "%d:%d node %d u%016llx c%016llx state unchanged\n",
4114 proc->pid, thread->pid,
4115 node_debug_id,
4116 (u64)node_ptr,
4117 (u64)node_cookie);
4118 if (ret)
4119 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004120 } break;
4121 case BINDER_WORK_DEAD_BINDER:
4122 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4123 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4124 struct binder_ref_death *death;
4125 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004126 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004127
4128 death = container_of(w, struct binder_ref_death, work);
4129 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
4130 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
4131 else
4132 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004133 cookie = death->cookie;
4134
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004135 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004136 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004137 proc->pid, thread->pid,
4138 cmd == BR_DEAD_BINDER ?
4139 "BR_DEAD_BINDER" :
4140 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07004141 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004142 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07004143 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004144 kfree(death);
4145 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004146 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004147 binder_enqueue_work_ilocked(
4148 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004149 binder_inner_proc_unlock(proc);
4150 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07004151 if (put_user(cmd, (uint32_t __user *)ptr))
4152 return -EFAULT;
4153 ptr += sizeof(uint32_t);
4154 if (put_user(cookie,
4155 (binder_uintptr_t __user *)ptr))
4156 return -EFAULT;
4157 ptr += sizeof(binder_uintptr_t);
4158 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004159 if (cmd == BR_DEAD_BINDER)
4160 goto done; /* DEAD_BINDER notifications can cause transactions */
4161 } break;
4162 }
4163
4164 if (!t)
4165 continue;
4166
4167 BUG_ON(t->buffer == NULL);
4168 if (t->buffer->target_node) {
4169 struct binder_node *target_node = t->buffer->target_node;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004170 struct binder_priority node_prio;
Seunghun Lee10f62862014-05-01 01:30:23 +09004171
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004172 tr.target.ptr = target_node->ptr;
4173 tr.cookie = target_node->cookie;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004174 node_prio.sched_policy = target_node->sched_policy;
4175 node_prio.prio = target_node->min_priority;
Martijn Coenenc46810c2017-06-23 10:13:43 -07004176 binder_transaction_priority(current, t, node_prio,
4177 target_node->inherit_rt);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004178 cmd = BR_TRANSACTION;
4179 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08004180 tr.target.ptr = 0;
4181 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004182 cmd = BR_REPLY;
4183 }
4184 tr.code = t->code;
4185 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06004186 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004187
Todd Kjos2f993e22017-05-12 14:42:55 -07004188 t_from = binder_get_txn_from(t);
4189 if (t_from) {
4190 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09004191
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004192 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08004193 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004194 } else {
4195 tr.sender_pid = 0;
4196 }
4197
4198 tr.data_size = t->buffer->data_size;
4199 tr.offsets_size = t->buffer->offsets_size;
Todd Kjosd325d372016-10-10 10:40:53 -07004200 tr.data.ptr.buffer = (binder_uintptr_t)
4201 ((uintptr_t)t->buffer->data +
4202 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004203 tr.data.ptr.offsets = tr.data.ptr.buffer +
4204 ALIGN(t->buffer->data_size,
4205 sizeof(void *));
4206
Todd Kjos2f993e22017-05-12 14:42:55 -07004207 if (put_user(cmd, (uint32_t __user *)ptr)) {
4208 if (t_from)
4209 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004210
4211 binder_cleanup_transaction(t, "put_user failed",
4212 BR_FAILED_REPLY);
4213
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004214 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004215 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004216 ptr += sizeof(uint32_t);
Todd Kjos2f993e22017-05-12 14:42:55 -07004217 if (copy_to_user(ptr, &tr, sizeof(tr))) {
4218 if (t_from)
4219 binder_thread_dec_tmpref(t_from);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004220
4221 binder_cleanup_transaction(t, "copy_to_user failed",
4222 BR_FAILED_REPLY);
4223
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004224 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07004225 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004226 ptr += sizeof(tr);
4227
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004228 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004229 binder_stat_br(proc, thread, cmd);
4230 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004231 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004232 proc->pid, thread->pid,
4233 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
4234 "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07004235 t->debug_id, t_from ? t_from->proc->pid : 0,
4236 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004237 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004238 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004239
Todd Kjos2f993e22017-05-12 14:42:55 -07004240 if (t_from)
4241 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004242 t->buffer->allow_user_free = 1;
4243 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07004244 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004245 t->to_parent = thread->transaction_stack;
4246 t->to_thread = thread;
4247 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07004248 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004249 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07004250 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004251 }
4252 break;
4253 }
4254
4255done:
4256
4257 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07004258 binder_inner_proc_lock(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004259 if (proc->requested_threads == 0 &&
4260 list_empty(&thread->proc->waiting_threads) &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004261 proc->requested_threads_started < proc->max_threads &&
4262 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
4263 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
4264 /*spawn a new thread if we leave this out */) {
4265 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07004266 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004267 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304268 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004269 proc->pid, thread->pid);
4270 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
4271 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07004272 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07004273 } else
4274 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004275 return 0;
4276}
4277
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004278static void binder_release_work(struct binder_proc *proc,
4279 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004280{
4281 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09004282
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004283 while (1) {
4284 w = binder_dequeue_work_head(proc, list);
4285 if (!w)
4286 return;
4287
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004288 switch (w->type) {
4289 case BINDER_WORK_TRANSACTION: {
4290 struct binder_transaction *t;
4291
4292 t = container_of(w, struct binder_transaction, work);
Martijn Coenen3217ccc2017-08-24 15:23:36 +02004293
4294 binder_cleanup_transaction(t, "process died.",
4295 BR_DEAD_REPLY);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004296 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004297 case BINDER_WORK_RETURN_ERROR: {
4298 struct binder_error *e = container_of(
4299 w, struct binder_error, work);
4300
4301 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
4302 "undelivered TRANSACTION_ERROR: %u\n",
4303 e->cmd);
4304 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004305 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004306 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304307 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004308 kfree(w);
4309 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
4310 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004311 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
4312 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
4313 struct binder_ref_death *death;
4314
4315 death = container_of(w, struct binder_ref_death, work);
4316 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004317 "undelivered death notification, %016llx\n",
4318 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004319 kfree(death);
4320 binder_stats_deleted(BINDER_STAT_DEATH);
4321 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004322 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304323 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07004324 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004325 break;
4326 }
4327 }
4328
4329}
4330
Todd Kjosb4827902017-05-25 15:52:17 -07004331static struct binder_thread *binder_get_thread_ilocked(
4332 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004333{
4334 struct binder_thread *thread = NULL;
4335 struct rb_node *parent = NULL;
4336 struct rb_node **p = &proc->threads.rb_node;
4337
4338 while (*p) {
4339 parent = *p;
4340 thread = rb_entry(parent, struct binder_thread, rb_node);
4341
4342 if (current->pid < thread->pid)
4343 p = &(*p)->rb_left;
4344 else if (current->pid > thread->pid)
4345 p = &(*p)->rb_right;
4346 else
Todd Kjosb4827902017-05-25 15:52:17 -07004347 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004348 }
Todd Kjosb4827902017-05-25 15:52:17 -07004349 if (!new_thread)
4350 return NULL;
4351 thread = new_thread;
4352 binder_stats_created(BINDER_STAT_THREAD);
4353 thread->proc = proc;
4354 thread->pid = current->pid;
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004355 get_task_struct(current);
4356 thread->task = current;
Todd Kjosb4827902017-05-25 15:52:17 -07004357 atomic_set(&thread->tmp_ref, 0);
4358 init_waitqueue_head(&thread->wait);
4359 INIT_LIST_HEAD(&thread->todo);
4360 rb_link_node(&thread->rb_node, parent, p);
4361 rb_insert_color(&thread->rb_node, &proc->threads);
4362 thread->looper_need_return = true;
4363 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
4364 thread->return_error.cmd = BR_OK;
4365 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
4366 thread->reply_error.cmd = BR_OK;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004367 INIT_LIST_HEAD(&new_thread->waiting_thread_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004368 return thread;
4369}
4370
4371static struct binder_thread *binder_get_thread(struct binder_proc *proc)
4372{
4373 struct binder_thread *thread;
4374 struct binder_thread *new_thread;
4375
4376 binder_inner_proc_lock(proc);
4377 thread = binder_get_thread_ilocked(proc, NULL);
4378 binder_inner_proc_unlock(proc);
4379 if (!thread) {
4380 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
4381 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004382 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07004383 binder_inner_proc_lock(proc);
4384 thread = binder_get_thread_ilocked(proc, new_thread);
4385 binder_inner_proc_unlock(proc);
4386 if (thread != new_thread)
4387 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004388 }
4389 return thread;
4390}
4391
Todd Kjos2f993e22017-05-12 14:42:55 -07004392static void binder_free_proc(struct binder_proc *proc)
4393{
4394 BUG_ON(!list_empty(&proc->todo));
4395 BUG_ON(!list_empty(&proc->delivered_death));
4396 binder_alloc_deferred_release(&proc->alloc);
4397 put_task_struct(proc->tsk);
4398 binder_stats_deleted(BINDER_STAT_PROC);
4399 kfree(proc);
4400}
4401
4402static void binder_free_thread(struct binder_thread *thread)
4403{
4404 BUG_ON(!list_empty(&thread->todo));
4405 binder_stats_deleted(BINDER_STAT_THREAD);
4406 binder_proc_dec_tmpref(thread->proc);
Martijn Coenen07a30fe2017-06-07 10:02:12 -07004407 put_task_struct(thread->task);
Todd Kjos2f993e22017-05-12 14:42:55 -07004408 kfree(thread);
4409}
4410
4411static int binder_thread_release(struct binder_proc *proc,
4412 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004413{
4414 struct binder_transaction *t;
4415 struct binder_transaction *send_reply = NULL;
4416 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004417 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004418
Todd Kjosb4827902017-05-25 15:52:17 -07004419 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004420 /*
4421 * take a ref on the proc so it survives
4422 * after we remove this thread from proc->threads.
4423 * The corresponding dec is when we actually
4424 * free the thread in binder_free_thread()
4425 */
4426 proc->tmp_ref++;
4427 /*
4428 * take a ref on this thread to ensure it
4429 * survives while we are releasing it
4430 */
4431 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004432 rb_erase(&thread->rb_node, &proc->threads);
4433 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004434 if (t) {
4435 spin_lock(&t->lock);
4436 if (t->to_thread == thread)
4437 send_reply = t;
4438 }
4439 thread->is_dead = true;
4440
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004441 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004442 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004443 active_transactions++;
4444 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304445 "release %d:%d transaction %d %s, still active\n",
4446 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004447 t->debug_id,
4448 (t->to_thread == thread) ? "in" : "out");
4449
4450 if (t->to_thread == thread) {
4451 t->to_proc = NULL;
4452 t->to_thread = NULL;
4453 if (t->buffer) {
4454 t->buffer->transaction = NULL;
4455 t->buffer = NULL;
4456 }
4457 t = t->to_parent;
4458 } else if (t->from == thread) {
4459 t->from = NULL;
4460 t = t->from_parent;
4461 } else
4462 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004463 spin_unlock(&last_t->lock);
4464 if (t)
4465 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004466 }
Todd Kjosb4827902017-05-25 15:52:17 -07004467 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004468
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004469 if (send_reply)
4470 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004471 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004472 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004473 return active_transactions;
4474}
4475
4476static unsigned int binder_poll(struct file *filp,
4477 struct poll_table_struct *wait)
4478{
4479 struct binder_proc *proc = filp->private_data;
4480 struct binder_thread *thread = NULL;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004481 bool wait_for_proc_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004482
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004483 thread = binder_get_thread(proc);
4484
Martijn Coenen995a36e2017-06-02 13:36:52 -07004485 binder_inner_proc_lock(thread->proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004486 thread->looper |= BINDER_LOOPER_STATE_POLL;
4487 wait_for_proc_work = binder_available_for_proc_work_ilocked(thread);
4488
Martijn Coenen995a36e2017-06-02 13:36:52 -07004489 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004490
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004491 poll_wait(filp, &thread->wait, wait);
4492
Martijn Coenen47810932017-08-10 12:32:00 +02004493 if (binder_has_work(thread, wait_for_proc_work))
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004494 return POLLIN;
4495
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004496 return 0;
4497}
4498
Tair Rzayev78260ac2014-06-03 22:27:21 +03004499static int binder_ioctl_write_read(struct file *filp,
4500 unsigned int cmd, unsigned long arg,
4501 struct binder_thread *thread)
4502{
4503 int ret = 0;
4504 struct binder_proc *proc = filp->private_data;
4505 unsigned int size = _IOC_SIZE(cmd);
4506 void __user *ubuf = (void __user *)arg;
4507 struct binder_write_read bwr;
4508
4509 if (size != sizeof(struct binder_write_read)) {
4510 ret = -EINVAL;
4511 goto out;
4512 }
4513 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4514 ret = -EFAULT;
4515 goto out;
4516 }
4517 binder_debug(BINDER_DEBUG_READ_WRITE,
4518 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4519 proc->pid, thread->pid,
4520 (u64)bwr.write_size, (u64)bwr.write_buffer,
4521 (u64)bwr.read_size, (u64)bwr.read_buffer);
4522
4523 if (bwr.write_size > 0) {
4524 ret = binder_thread_write(proc, thread,
4525 bwr.write_buffer,
4526 bwr.write_size,
4527 &bwr.write_consumed);
4528 trace_binder_write_done(ret);
4529 if (ret < 0) {
4530 bwr.read_consumed = 0;
4531 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4532 ret = -EFAULT;
4533 goto out;
4534 }
4535 }
4536 if (bwr.read_size > 0) {
4537 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4538 bwr.read_size,
4539 &bwr.read_consumed,
4540 filp->f_flags & O_NONBLOCK);
4541 trace_binder_read_done(ret);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004542 binder_inner_proc_lock(proc);
4543 if (!binder_worklist_empty_ilocked(&proc->todo))
Martijn Coenen053be422017-06-06 15:17:46 -07004544 binder_wakeup_proc_ilocked(proc);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004545 binder_inner_proc_unlock(proc);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004546 if (ret < 0) {
4547 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4548 ret = -EFAULT;
4549 goto out;
4550 }
4551 }
4552 binder_debug(BINDER_DEBUG_READ_WRITE,
4553 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4554 proc->pid, thread->pid,
4555 (u64)bwr.write_consumed, (u64)bwr.write_size,
4556 (u64)bwr.read_consumed, (u64)bwr.read_size);
4557 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4558 ret = -EFAULT;
4559 goto out;
4560 }
4561out:
4562 return ret;
4563}
4564
4565static int binder_ioctl_set_ctx_mgr(struct file *filp)
4566{
4567 int ret = 0;
4568 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004569 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004570 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004571 kuid_t curr_euid = current_euid();
4572
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004573 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004574 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004575 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4576 ret = -EBUSY;
4577 goto out;
4578 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004579 ret = security_binder_set_context_mgr(proc->tsk);
4580 if (ret < 0)
4581 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004582 if (uid_valid(context->binder_context_mgr_uid)) {
4583 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004584 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4585 from_kuid(&init_user_ns, curr_euid),
4586 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004587 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004588 ret = -EPERM;
4589 goto out;
4590 }
4591 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004592 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004593 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004594 new_node = binder_new_node(proc, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004595 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004596 ret = -ENOMEM;
4597 goto out;
4598 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004599 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004600 new_node->local_weak_refs++;
4601 new_node->local_strong_refs++;
4602 new_node->has_strong_ref = 1;
4603 new_node->has_weak_ref = 1;
4604 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004605 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004606 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004607out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004608 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004609 return ret;
4610}
4611
Colin Cross833babb32017-06-20 13:54:44 -07004612static int binder_ioctl_get_node_debug_info(struct binder_proc *proc,
4613 struct binder_node_debug_info *info) {
4614 struct rb_node *n;
4615 binder_uintptr_t ptr = info->ptr;
4616
4617 memset(info, 0, sizeof(*info));
4618
4619 binder_inner_proc_lock(proc);
4620 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
4621 struct binder_node *node = rb_entry(n, struct binder_node,
4622 rb_node);
4623 if (node->ptr > ptr) {
4624 info->ptr = node->ptr;
4625 info->cookie = node->cookie;
4626 info->has_strong_ref = node->has_strong_ref;
4627 info->has_weak_ref = node->has_weak_ref;
4628 break;
4629 }
4630 }
4631 binder_inner_proc_unlock(proc);
4632
4633 return 0;
4634}
4635
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004636static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4637{
4638 int ret;
4639 struct binder_proc *proc = filp->private_data;
4640 struct binder_thread *thread;
4641 unsigned int size = _IOC_SIZE(cmd);
4642 void __user *ubuf = (void __user *)arg;
4643
Tair Rzayev78260ac2014-06-03 22:27:21 +03004644 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4645 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004646
Sherry Yang435416b2017-06-22 14:37:45 -07004647 binder_selftest_alloc(&proc->alloc);
4648
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004649 trace_binder_ioctl(cmd, arg);
4650
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004651 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4652 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004653 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004654
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004655 thread = binder_get_thread(proc);
4656 if (thread == NULL) {
4657 ret = -ENOMEM;
4658 goto err;
4659 }
4660
4661 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004662 case BINDER_WRITE_READ:
4663 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4664 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004665 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004666 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004667 case BINDER_SET_MAX_THREADS: {
4668 int max_threads;
4669
4670 if (copy_from_user(&max_threads, ubuf,
4671 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004672 ret = -EINVAL;
4673 goto err;
4674 }
Todd Kjosd600e902017-05-25 17:35:02 -07004675 binder_inner_proc_lock(proc);
4676 proc->max_threads = max_threads;
4677 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004678 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004679 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004680 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004681 ret = binder_ioctl_set_ctx_mgr(filp);
4682 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004683 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004684 break;
4685 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304686 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004687 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07004688 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004689 thread = NULL;
4690 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004691 case BINDER_VERSION: {
4692 struct binder_version __user *ver = ubuf;
4693
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004694 if (size != sizeof(struct binder_version)) {
4695 ret = -EINVAL;
4696 goto err;
4697 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004698 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4699 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004700 ret = -EINVAL;
4701 goto err;
4702 }
4703 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004704 }
Colin Cross833babb32017-06-20 13:54:44 -07004705 case BINDER_GET_NODE_DEBUG_INFO: {
4706 struct binder_node_debug_info info;
4707
4708 if (copy_from_user(&info, ubuf, sizeof(info))) {
4709 ret = -EFAULT;
4710 goto err;
4711 }
4712
4713 ret = binder_ioctl_get_node_debug_info(proc, &info);
4714 if (ret < 0)
4715 goto err;
4716
4717 if (copy_to_user(ubuf, &info, sizeof(info))) {
4718 ret = -EFAULT;
4719 goto err;
4720 }
4721 break;
4722 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004723 default:
4724 ret = -EINVAL;
4725 goto err;
4726 }
4727 ret = 0;
4728err:
4729 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08004730 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004731 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4732 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304733 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 -07004734err_unlocked:
4735 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004736 return ret;
4737}
4738
4739static void binder_vma_open(struct vm_area_struct *vma)
4740{
4741 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004742
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004743 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304744 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004745 proc->pid, vma->vm_start, vma->vm_end,
4746 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4747 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004748}
4749
4750static void binder_vma_close(struct vm_area_struct *vma)
4751{
4752 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004753
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004754 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304755 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004756 proc->pid, vma->vm_start, vma->vm_end,
4757 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4758 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07004759 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004760 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4761}
4762
Vinayak Menonddac7d52014-06-02 18:17:59 +05304763static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4764{
4765 return VM_FAULT_SIGBUS;
4766}
4767
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004768static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004769 .open = binder_vma_open,
4770 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304771 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004772};
4773
Todd Kjosd325d372016-10-10 10:40:53 -07004774static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4775{
4776 int ret;
4777 struct binder_proc *proc = filp->private_data;
4778 const char *failure_string;
4779
4780 if (proc->tsk != current->group_leader)
4781 return -EINVAL;
4782
4783 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4784 vma->vm_end = vma->vm_start + SZ_4M;
4785
4786 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4787 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4788 __func__, proc->pid, vma->vm_start, vma->vm_end,
4789 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4790 (unsigned long)pgprot_val(vma->vm_page_prot));
4791
4792 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4793 ret = -EPERM;
4794 failure_string = "bad vm_flags";
4795 goto err_bad_arg;
4796 }
4797 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4798 vma->vm_ops = &binder_vm_ops;
4799 vma->vm_private_data = proc;
4800
4801 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4802 if (ret)
4803 return ret;
4804 proc->files = get_files_struct(current);
4805 return 0;
4806
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004807err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004808 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004809 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4810 return ret;
4811}
4812
4813static int binder_open(struct inode *nodp, struct file *filp)
4814{
4815 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004816 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004817
4818 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4819 current->group_leader->pid, current->pid);
4820
4821 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4822 if (proc == NULL)
4823 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07004824 spin_lock_init(&proc->inner_lock);
4825 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01004826 get_task_struct(current->group_leader);
4827 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004828 INIT_LIST_HEAD(&proc->todo);
Martijn Coenen57b2ac62017-06-06 17:04:42 -07004829 if (binder_supported_policy(current->policy)) {
4830 proc->default_priority.sched_policy = current->policy;
4831 proc->default_priority.prio = current->normal_prio;
4832 } else {
4833 proc->default_priority.sched_policy = SCHED_NORMAL;
4834 proc->default_priority.prio = NICE_TO_PRIO(0);
4835 }
4836
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004837 binder_dev = container_of(filp->private_data, struct binder_device,
4838 miscdev);
4839 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07004840 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004841
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004842 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004843 proc->pid = current->group_leader->pid;
4844 INIT_LIST_HEAD(&proc->delivered_death);
Martijn Coenen22d64e4322017-06-02 11:15:44 -07004845 INIT_LIST_HEAD(&proc->waiting_threads);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004846 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004847
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004848 mutex_lock(&binder_procs_lock);
4849 hlist_add_head(&proc->proc_node, &binder_procs);
4850 mutex_unlock(&binder_procs_lock);
4851
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004852 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004853 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004854
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004855 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004856 /*
4857 * proc debug entries are shared between contexts, so
4858 * this will fail if the process tries to open the driver
4859 * again with a different context. The priting code will
4860 * anyway print all contexts that a given PID has, so this
4861 * is not a problem.
4862 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004863 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004864 binder_debugfs_dir_entry_proc,
4865 (void *)(unsigned long)proc->pid,
4866 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004867 }
4868
4869 return 0;
4870}
4871
4872static int binder_flush(struct file *filp, fl_owner_t id)
4873{
4874 struct binder_proc *proc = filp->private_data;
4875
4876 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4877
4878 return 0;
4879}
4880
4881static void binder_deferred_flush(struct binder_proc *proc)
4882{
4883 struct rb_node *n;
4884 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004885
Todd Kjosb4827902017-05-25 15:52:17 -07004886 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004887 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4888 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004889
Todd Kjos6798e6d2017-01-06 14:19:25 -08004890 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004891 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4892 wake_up_interruptible(&thread->wait);
4893 wake_count++;
4894 }
4895 }
Todd Kjosb4827902017-05-25 15:52:17 -07004896 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004897
4898 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4899 "binder_flush: %d woke %d threads\n", proc->pid,
4900 wake_count);
4901}
4902
4903static int binder_release(struct inode *nodp, struct file *filp)
4904{
4905 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004906
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004907 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004908 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4909
4910 return 0;
4911}
4912
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004913static int binder_node_release(struct binder_node *node, int refs)
4914{
4915 struct binder_ref *ref;
4916 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004917 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004918
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004919 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004920
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004921 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004922 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004923 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07004924 /*
4925 * The caller must have taken a temporary ref on the node,
4926 */
4927 BUG_ON(!node->tmp_refs);
4928 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004929 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004930 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004931 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004932
4933 return refs;
4934 }
4935
4936 node->proc = NULL;
4937 node->local_strong_refs = 0;
4938 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004939 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004940
4941 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004942 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004943 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004944
4945 hlist_for_each_entry(ref, &node->refs, node_entry) {
4946 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004947 /*
4948 * Need the node lock to synchronize
4949 * with new notification requests and the
4950 * inner lock to synchronize with queued
4951 * death notifications.
4952 */
4953 binder_inner_proc_lock(ref->proc);
4954 if (!ref->death) {
4955 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08004956 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004957 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004958
4959 death++;
4960
Martijn Coenenf9eac642017-05-22 11:26:23 -07004961 BUG_ON(!list_empty(&ref->death->work.entry));
4962 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4963 binder_enqueue_work_ilocked(&ref->death->work,
4964 &ref->proc->todo);
Martijn Coenen053be422017-06-06 15:17:46 -07004965 binder_wakeup_proc_ilocked(ref->proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004966 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004967 }
4968
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004969 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4970 "node %d now dead, refs %d, death %d\n",
4971 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004972 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004973 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004974
4975 return refs;
4976}
4977
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004978static void binder_deferred_release(struct binder_proc *proc)
4979{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004980 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004981 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07004982 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004983
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004984 BUG_ON(proc->files);
4985
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004986 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004987 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004988 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004989
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004990 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004991 if (context->binder_context_mgr_node &&
4992 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004993 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004994 "%s: %d context_mgr_node gone\n",
4995 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004996 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004997 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004998 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07004999 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07005000 /*
5001 * Make sure proc stays alive after we
5002 * remove all the threads
5003 */
5004 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005005
Todd Kjos2f993e22017-05-12 14:42:55 -07005006 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005007 threads = 0;
5008 active_transactions = 0;
5009 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005010 struct binder_thread *thread;
5011
5012 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07005013 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005014 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07005015 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07005016 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005017 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005018
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005019 nodes = 0;
5020 incoming_refs = 0;
5021 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005022 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005023
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005024 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005025 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07005026 /*
5027 * take a temporary ref on the node before
5028 * calling binder_node_release() which will either
5029 * kfree() the node or call binder_put_node()
5030 */
Todd Kjos425d23f2017-06-12 12:07:26 -07005031 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005032 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07005033 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01005034 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07005035 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005036 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005037 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005038
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005039 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005040 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005041 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005042 struct binder_ref *ref;
5043
5044 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005045 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07005046 binder_cleanup_ref_olocked(ref);
5047 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005048 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07005049 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005050 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005051 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01005052
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005053 binder_release_work(proc, &proc->todo);
5054 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005055
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005056 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07005057 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01005058 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07005059 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005060
Todd Kjos2f993e22017-05-12 14:42:55 -07005061 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005062}
5063
5064static void binder_deferred_func(struct work_struct *work)
5065{
5066 struct binder_proc *proc;
5067 struct files_struct *files;
5068
5069 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09005070
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005071 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005072 mutex_lock(&binder_deferred_lock);
5073 if (!hlist_empty(&binder_deferred_list)) {
5074 proc = hlist_entry(binder_deferred_list.first,
5075 struct binder_proc, deferred_work_node);
5076 hlist_del_init(&proc->deferred_work_node);
5077 defer = proc->deferred_work;
5078 proc->deferred_work = 0;
5079 } else {
5080 proc = NULL;
5081 defer = 0;
5082 }
5083 mutex_unlock(&binder_deferred_lock);
5084
5085 files = NULL;
5086 if (defer & BINDER_DEFERRED_PUT_FILES) {
5087 files = proc->files;
5088 if (files)
5089 proc->files = NULL;
5090 }
5091
5092 if (defer & BINDER_DEFERRED_FLUSH)
5093 binder_deferred_flush(proc);
5094
5095 if (defer & BINDER_DEFERRED_RELEASE)
5096 binder_deferred_release(proc); /* frees proc */
5097
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005098 if (files)
5099 put_files_struct(files);
5100 } while (proc);
5101}
5102static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
5103
5104static void
5105binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
5106{
5107 mutex_lock(&binder_deferred_lock);
5108 proc->deferred_work |= defer;
5109 if (hlist_unhashed(&proc->deferred_work_node)) {
5110 hlist_add_head(&proc->deferred_work_node,
5111 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05305112 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005113 }
5114 mutex_unlock(&binder_deferred_lock);
5115}
5116
Todd Kjos6d241a42017-04-21 14:32:11 -07005117static void print_binder_transaction_ilocked(struct seq_file *m,
5118 struct binder_proc *proc,
5119 const char *prefix,
5120 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005121{
Todd Kjos6d241a42017-04-21 14:32:11 -07005122 struct binder_proc *to_proc;
5123 struct binder_buffer *buffer = t->buffer;
5124
Todd Kjos2f993e22017-05-12 14:42:55 -07005125 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07005126 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005127 seq_printf(m,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005128 "%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 -07005129 prefix, t->debug_id, t,
5130 t->from ? t->from->proc->pid : 0,
5131 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07005132 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005133 t->to_thread ? t->to_thread->pid : 0,
Martijn Coenen57b2ac62017-06-06 17:04:42 -07005134 t->code, t->flags, t->priority.sched_policy,
5135 t->priority.prio, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07005136 spin_unlock(&t->lock);
5137
Todd Kjos6d241a42017-04-21 14:32:11 -07005138 if (proc != to_proc) {
5139 /*
5140 * Can only safely deref buffer if we are holding the
5141 * correct proc inner lock for this node
5142 */
5143 seq_puts(m, "\n");
5144 return;
5145 }
5146
5147 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005148 seq_puts(m, " buffer free\n");
5149 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005150 }
Todd Kjos6d241a42017-04-21 14:32:11 -07005151 if (buffer->target_node)
5152 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005153 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07005154 buffer->data_size, buffer->offsets_size,
5155 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005156}
5157
Todd Kjos6d241a42017-04-21 14:32:11 -07005158static void print_binder_work_ilocked(struct seq_file *m,
5159 struct binder_proc *proc,
5160 const char *prefix,
5161 const char *transaction_prefix,
5162 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005163{
5164 struct binder_node *node;
5165 struct binder_transaction *t;
5166
5167 switch (w->type) {
5168 case BINDER_WORK_TRANSACTION:
5169 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07005170 print_binder_transaction_ilocked(
5171 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005172 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07005173 case BINDER_WORK_RETURN_ERROR: {
5174 struct binder_error *e = container_of(
5175 w, struct binder_error, work);
5176
5177 seq_printf(m, "%stransaction error: %u\n",
5178 prefix, e->cmd);
5179 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005180 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005181 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005182 break;
5183 case BINDER_WORK_NODE:
5184 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08005185 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
5186 prefix, node->debug_id,
5187 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005188 break;
5189 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005190 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005191 break;
5192 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005193 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005194 break;
5195 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005196 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005197 break;
5198 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005199 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005200 break;
5201 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005202}
5203
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005204static void print_binder_thread_ilocked(struct seq_file *m,
5205 struct binder_thread *thread,
5206 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005207{
5208 struct binder_transaction *t;
5209 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005210 size_t start_pos = m->count;
5211 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005212
Todd Kjos2f993e22017-05-12 14:42:55 -07005213 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08005214 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07005215 thread->looper_need_return,
5216 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005217 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005218 t = thread->transaction_stack;
5219 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005220 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005221 print_binder_transaction_ilocked(m, thread->proc,
5222 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005223 t = t->from_parent;
5224 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005225 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005226 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005227 t = t->to_parent;
5228 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07005229 print_binder_transaction_ilocked(m, thread->proc,
5230 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005231 t = NULL;
5232 }
5233 }
5234 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07005235 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005236 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005237 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005238 if (!print_always && m->count == header_pos)
5239 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005240}
5241
Todd Kjos425d23f2017-06-12 12:07:26 -07005242static void print_binder_node_nilocked(struct seq_file *m,
5243 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005244{
5245 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005246 struct binder_work *w;
5247 int count;
5248
5249 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08005250 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005251 count++;
5252
Martijn Coenen6aac9792017-06-07 09:29:14 -07005253 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 -08005254 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Martijn Coenen6aac9792017-06-07 09:29:14 -07005255 node->sched_policy, node->min_priority,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005256 node->has_strong_ref, node->has_weak_ref,
5257 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07005258 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005259 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005260 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08005261 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005262 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005263 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005264 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005265 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005266 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005267 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005268 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005269 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005270}
5271
Todd Kjos5346bf32016-10-20 16:43:34 -07005272static void print_binder_ref_olocked(struct seq_file *m,
5273 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005274{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005275 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07005276 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
5277 ref->data.debug_id, ref->data.desc,
5278 ref->node->proc ? "" : "dead ",
5279 ref->node->debug_id, ref->data.strong,
5280 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005281 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005282}
5283
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005284static void print_binder_proc(struct seq_file *m,
5285 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005286{
5287 struct binder_work *w;
5288 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005289 size_t start_pos = m->count;
5290 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07005291 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005292
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005293 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005294 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005295 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005296
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005297 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005298 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005299 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005300 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07005301
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005302 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005303 struct binder_node *node = rb_entry(n, struct binder_node,
5304 rb_node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005305 /*
5306 * take a temporary reference on the node so it
5307 * survives and isn't removed from the tree
5308 * while we print it.
5309 */
5310 binder_inc_node_tmpref_ilocked(node);
5311 /* Need to drop inner lock to take node lock */
5312 binder_inner_proc_unlock(proc);
5313 if (last_node)
5314 binder_put_node(last_node);
5315 binder_node_inner_lock(node);
5316 print_binder_node_nilocked(m, node);
5317 binder_node_inner_unlock(node);
5318 last_node = node;
5319 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005320 }
Todd Kjos425d23f2017-06-12 12:07:26 -07005321 binder_inner_proc_unlock(proc);
5322 if (last_node)
5323 binder_put_node(last_node);
5324
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005325 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07005326 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005327 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005328 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005329 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07005330 print_binder_ref_olocked(m, rb_entry(n,
5331 struct binder_ref,
5332 rb_node_desc));
5333 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005334 }
Todd Kjosd325d372016-10-10 10:40:53 -07005335 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005336 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005337 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07005338 print_binder_work_ilocked(m, proc, " ",
5339 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005340 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005341 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005342 break;
5343 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005344 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005345 if (!print_all && m->count == header_pos)
5346 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005347}
5348
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005349static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005350 "BR_ERROR",
5351 "BR_OK",
5352 "BR_TRANSACTION",
5353 "BR_REPLY",
5354 "BR_ACQUIRE_RESULT",
5355 "BR_DEAD_REPLY",
5356 "BR_TRANSACTION_COMPLETE",
5357 "BR_INCREFS",
5358 "BR_ACQUIRE",
5359 "BR_RELEASE",
5360 "BR_DECREFS",
5361 "BR_ATTEMPT_ACQUIRE",
5362 "BR_NOOP",
5363 "BR_SPAWN_LOOPER",
5364 "BR_FINISHED",
5365 "BR_DEAD_BINDER",
5366 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
5367 "BR_FAILED_REPLY"
5368};
5369
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005370static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005371 "BC_TRANSACTION",
5372 "BC_REPLY",
5373 "BC_ACQUIRE_RESULT",
5374 "BC_FREE_BUFFER",
5375 "BC_INCREFS",
5376 "BC_ACQUIRE",
5377 "BC_RELEASE",
5378 "BC_DECREFS",
5379 "BC_INCREFS_DONE",
5380 "BC_ACQUIRE_DONE",
5381 "BC_ATTEMPT_ACQUIRE",
5382 "BC_REGISTER_LOOPER",
5383 "BC_ENTER_LOOPER",
5384 "BC_EXIT_LOOPER",
5385 "BC_REQUEST_DEATH_NOTIFICATION",
5386 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02005387 "BC_DEAD_BINDER_DONE",
5388 "BC_TRANSACTION_SG",
5389 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005390};
5391
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10005392static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005393 "proc",
5394 "thread",
5395 "node",
5396 "ref",
5397 "death",
5398 "transaction",
5399 "transaction_complete"
5400};
5401
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005402static void print_binder_stats(struct seq_file *m, const char *prefix,
5403 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005404{
5405 int i;
5406
5407 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005408 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005409 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005410 int temp = atomic_read(&stats->bc[i]);
5411
5412 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005413 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005414 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005415 }
5416
5417 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005418 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005419 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005420 int temp = atomic_read(&stats->br[i]);
5421
5422 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005423 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005424 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005425 }
5426
5427 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005428 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005429 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005430 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005431 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005432 int created = atomic_read(&stats->obj_created[i]);
5433 int deleted = atomic_read(&stats->obj_deleted[i]);
5434
5435 if (created || deleted)
5436 seq_printf(m, "%s%s: active %d total %d\n",
5437 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005438 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005439 created - deleted,
5440 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005441 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005442}
5443
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005444static void print_binder_proc_stats(struct seq_file *m,
5445 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005446{
5447 struct binder_work *w;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005448 struct binder_thread *thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005449 struct rb_node *n;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005450 int count, strong, weak, ready_threads;
Todd Kjosb4827902017-05-25 15:52:17 -07005451 size_t free_async_space =
5452 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005453
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005454 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005455 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005456 count = 0;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005457 ready_threads = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005458 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005459 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5460 count++;
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005461
5462 list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node)
5463 ready_threads++;
5464
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005465 seq_printf(m, " threads: %d\n", count);
5466 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005467 " ready threads %d\n"
5468 " free async space %zd\n", proc->requested_threads,
5469 proc->requested_threads_started, proc->max_threads,
Martijn Coenen22d64e4322017-06-02 11:15:44 -07005470 ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005471 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005472 count = 0;
5473 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5474 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005475 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005476 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005477 count = 0;
5478 strong = 0;
5479 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005480 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005481 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5482 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5483 rb_node_desc);
5484 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005485 strong += ref->data.strong;
5486 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005487 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005488 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005489 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005490
Todd Kjosd325d372016-10-10 10:40:53 -07005491 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005492 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005493
Sherry Yang91004422017-08-22 17:26:57 -07005494 binder_alloc_print_pages(m, &proc->alloc);
5495
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005496 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005497 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005498 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005499 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005500 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005501 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005502 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005503 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005504
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005505 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005506}
5507
5508
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005509static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005510{
5511 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005512 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005513 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005514
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005515 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005516
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005517 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005518 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005519 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005520 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5521 /*
5522 * take a temporary reference on the node so it
5523 * survives and isn't removed from the list
5524 * while we print it.
5525 */
5526 node->tmp_refs++;
5527 spin_unlock(&binder_dead_nodes_lock);
5528 if (last_node)
5529 binder_put_node(last_node);
5530 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005531 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005532 binder_node_unlock(node);
5533 last_node = node;
5534 spin_lock(&binder_dead_nodes_lock);
5535 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005536 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005537 if (last_node)
5538 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005539
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005540 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005541 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005542 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005543 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005544
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005545 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005546}
5547
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005548static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005549{
5550 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005551
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005552 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005553
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005554 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005555
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005556 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005557 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005558 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005559 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005560
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005561 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005562}
5563
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005564static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005565{
5566 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005567
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005568 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005569 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005570 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005571 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005572 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005573
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005574 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005575}
5576
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005577static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005578{
Riley Andrews83050a42016-02-09 21:05:33 -08005579 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005580 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005581
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005582 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005583 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005584 if (itr->pid == pid) {
5585 seq_puts(m, "binder proc state:\n");
5586 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005587 }
5588 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005589 mutex_unlock(&binder_procs_lock);
5590
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005591 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005592}
5593
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005594static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005595 struct binder_transaction_log_entry *e)
5596{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005597 int debug_id = READ_ONCE(e->debug_id_done);
5598 /*
5599 * read barrier to guarantee debug_id_done read before
5600 * we print the log values
5601 */
5602 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005603 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005604 "%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 -07005605 e->debug_id, (e->call_type == 2) ? "reply" :
5606 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005607 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005608 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5609 e->return_error, e->return_error_param,
5610 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005611 /*
5612 * read-barrier to guarantee read of debug_id_done after
5613 * done printing the fields of the entry
5614 */
5615 smp_rmb();
5616 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5617 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005618}
5619
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005620static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005621{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005622 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005623 unsigned int log_cur = atomic_read(&log->cur);
5624 unsigned int count;
5625 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005626 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005627
Todd Kjos1cfe6272017-05-24 13:33:28 -07005628 count = log_cur + 1;
5629 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5630 0 : count % ARRAY_SIZE(log->entry);
5631 if (count > ARRAY_SIZE(log->entry) || log->full)
5632 count = ARRAY_SIZE(log->entry);
5633 for (i = 0; i < count; i++) {
5634 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5635
5636 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005637 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005638 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005639}
5640
5641static const struct file_operations binder_fops = {
5642 .owner = THIS_MODULE,
5643 .poll = binder_poll,
5644 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005645 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005646 .mmap = binder_mmap,
5647 .open = binder_open,
5648 .flush = binder_flush,
5649 .release = binder_release,
5650};
5651
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005652BINDER_DEBUG_ENTRY(state);
5653BINDER_DEBUG_ENTRY(stats);
5654BINDER_DEBUG_ENTRY(transactions);
5655BINDER_DEBUG_ENTRY(transaction_log);
5656
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005657static int __init init_binder_device(const char *name)
5658{
5659 int ret;
5660 struct binder_device *binder_device;
5661
5662 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5663 if (!binder_device)
5664 return -ENOMEM;
5665
5666 binder_device->miscdev.fops = &binder_fops;
5667 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5668 binder_device->miscdev.name = name;
5669
5670 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5671 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005672 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005673
5674 ret = misc_register(&binder_device->miscdev);
5675 if (ret < 0) {
5676 kfree(binder_device);
5677 return ret;
5678 }
5679
5680 hlist_add_head(&binder_device->hlist, &binder_devices);
5681
5682 return ret;
5683}
5684
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005685static int __init binder_init(void)
5686{
5687 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005688 char *device_name, *device_names;
5689 struct binder_device *device;
5690 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005691
Sherry Yang5828d702017-07-29 13:24:11 -07005692 binder_alloc_shrinker_init();
5693
Todd Kjos1cfe6272017-05-24 13:33:28 -07005694 atomic_set(&binder_transaction_log.cur, ~0U);
5695 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5696
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005697 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5698 if (binder_debugfs_dir_entry_root)
5699 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5700 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005701
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005702 if (binder_debugfs_dir_entry_root) {
5703 debugfs_create_file("state",
5704 S_IRUGO,
5705 binder_debugfs_dir_entry_root,
5706 NULL,
5707 &binder_state_fops);
5708 debugfs_create_file("stats",
5709 S_IRUGO,
5710 binder_debugfs_dir_entry_root,
5711 NULL,
5712 &binder_stats_fops);
5713 debugfs_create_file("transactions",
5714 S_IRUGO,
5715 binder_debugfs_dir_entry_root,
5716 NULL,
5717 &binder_transactions_fops);
5718 debugfs_create_file("transaction_log",
5719 S_IRUGO,
5720 binder_debugfs_dir_entry_root,
5721 &binder_transaction_log,
5722 &binder_transaction_log_fops);
5723 debugfs_create_file("failed_transaction_log",
5724 S_IRUGO,
5725 binder_debugfs_dir_entry_root,
5726 &binder_transaction_log_failed,
5727 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005728 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005729
5730 /*
5731 * Copy the module_parameter string, because we don't want to
5732 * tokenize it in-place.
5733 */
5734 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5735 if (!device_names) {
5736 ret = -ENOMEM;
5737 goto err_alloc_device_names_failed;
5738 }
5739 strcpy(device_names, binder_devices_param);
5740
5741 while ((device_name = strsep(&device_names, ","))) {
5742 ret = init_binder_device(device_name);
5743 if (ret)
5744 goto err_init_binder_device_failed;
5745 }
5746
5747 return ret;
5748
5749err_init_binder_device_failed:
5750 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5751 misc_deregister(&device->miscdev);
5752 hlist_del(&device->hlist);
5753 kfree(device);
5754 }
5755err_alloc_device_names_failed:
5756 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5757
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005758 return ret;
5759}
5760
5761device_initcall(binder_init);
5762
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005763#define CREATE_TRACE_POINTS
5764#include "binder_trace.h"
5765
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005766MODULE_LICENSE("GPL v2");