blob: b351c850e63770b7ba7575725946465439250084 [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
31 * (proc->threads, proc->nodes) and all todo lists associated
32 * with the binder_proc (proc->todo, thread->todo,
Martijn Coenen995a36e2017-06-02 13:36:52 -070033 * proc->delivered_death and node->async_todo), as well as
34 * 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)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700355 * @accept_fds: file descriptor operations supported for node
356 * (invariant after initialized)
357 * @min_priority: minimum scheduling priority
358 * (invariant after initialized)
359 * @async_todo: list of async work items
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700360 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700361 *
362 * Bookkeeping structure for binder nodes.
363 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900364struct binder_node {
365 int debug_id;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700366 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900367 struct binder_work work;
368 union {
369 struct rb_node rb_node;
370 struct hlist_node dead_node;
371 };
372 struct binder_proc *proc;
373 struct hlist_head refs;
374 int internal_strong_refs;
375 int local_weak_refs;
376 int local_strong_refs;
Todd Kjosf22abc72017-05-09 11:08:05 -0700377 int tmp_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800378 binder_uintptr_t ptr;
379 binder_uintptr_t cookie;
Todd Kjose7f23ed2017-03-21 13:06:01 -0700380 struct {
381 /*
382 * bitfield elements protected by
383 * proc inner_lock
384 */
385 u8 has_strong_ref:1;
386 u8 pending_strong_ref:1;
387 u8 has_weak_ref:1;
388 u8 pending_weak_ref:1;
389 };
390 struct {
391 /*
392 * invariant after initialization
393 */
394 u8 accept_fds:1;
395 u8 min_priority;
396 };
397 bool has_async_transaction;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900398 struct list_head async_todo;
399};
400
401struct binder_ref_death {
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700402 /**
403 * @work: worklist element for death notifications
404 * (protected by inner_lock of the proc that
405 * this ref belongs to)
406 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900407 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800408 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900409};
410
Todd Kjosb0117bb2017-05-08 09:16:27 -0700411/**
412 * struct binder_ref_data - binder_ref counts and id
413 * @debug_id: unique ID for the ref
414 * @desc: unique userspace handle for ref
415 * @strong: strong ref count (debugging only if not locked)
416 * @weak: weak ref count (debugging only if not locked)
417 *
418 * Structure to hold ref count and ref id information. Since
419 * the actual ref can only be accessed with a lock, this structure
420 * is used to return information about the ref to callers of
421 * ref inc/dec functions.
422 */
423struct binder_ref_data {
424 int debug_id;
425 uint32_t desc;
426 int strong;
427 int weak;
428};
429
430/**
431 * struct binder_ref - struct to track references on nodes
432 * @data: binder_ref_data containing id, handle, and current refcounts
433 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
434 * @rb_node_node: node for lookup by @node in proc's rb_tree
435 * @node_entry: list entry for node->refs list in target node
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700436 * (protected by @node->lock)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700437 * @proc: binder_proc containing ref
438 * @node: binder_node of target node. When cleaning up a
439 * ref for deletion in binder_cleanup_ref, a non-NULL
440 * @node indicates the node must be freed
441 * @death: pointer to death notification (ref_death) if requested
Martijn Coenenf9eac642017-05-22 11:26:23 -0700442 * (protected by @node->lock)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700443 *
444 * Structure to track references from procA to target node (on procB). This
445 * structure is unsafe to access without holding @proc->outer_lock.
446 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900447struct binder_ref {
448 /* Lookups needed: */
449 /* node + proc => ref (transaction) */
450 /* desc + proc => ref (transaction, inc/dec ref) */
451 /* node => refs + procs (proc exit) */
Todd Kjosb0117bb2017-05-08 09:16:27 -0700452 struct binder_ref_data data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900453 struct rb_node rb_node_desc;
454 struct rb_node rb_node_node;
455 struct hlist_node node_entry;
456 struct binder_proc *proc;
457 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900458 struct binder_ref_death *death;
459};
460
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900461enum binder_deferred_state {
462 BINDER_DEFERRED_PUT_FILES = 0x01,
463 BINDER_DEFERRED_FLUSH = 0x02,
464 BINDER_DEFERRED_RELEASE = 0x04,
465};
466
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700467/**
468 * struct binder_proc - binder process bookkeeping
469 * @proc_node: element for binder_procs list
470 * @threads: rbtree of binder_threads in this proc
Todd Kjosb4827902017-05-25 15:52:17 -0700471 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700472 * @nodes: rbtree of binder nodes associated with
473 * this proc ordered by node->ptr
Todd Kjos425d23f2017-06-12 12:07:26 -0700474 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700475 * @refs_by_desc: rbtree of refs ordered by ref->desc
Todd Kjos5346bf32016-10-20 16:43:34 -0700476 * (protected by @outer_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700477 * @refs_by_node: rbtree of refs ordered by ref->node
Todd Kjos5346bf32016-10-20 16:43:34 -0700478 * (protected by @outer_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700479 * @pid PID of group_leader of process
480 * (invariant after initialized)
481 * @tsk task_struct for group_leader of process
482 * (invariant after initialized)
483 * @files files_struct for process
484 * (invariant after initialized)
485 * @deferred_work_node: element for binder_deferred_list
486 * (protected by binder_deferred_lock)
487 * @deferred_work: bitmap of deferred work to perform
488 * (protected by binder_deferred_lock)
489 * @is_dead: process is dead and awaiting free
490 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700491 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700492 * @todo: list of work for this process
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700493 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700494 * @wait: wait queue head to wait for proc work
495 * (invariant after initialized)
496 * @stats: per-process binder statistics
497 * (atomics, no lock needed)
498 * @delivered_death: list of delivered death notification
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700499 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700500 * @max_threads: cap on number of binder threads
Todd Kjosd600e902017-05-25 17:35:02 -0700501 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700502 * @requested_threads: number of binder threads requested but not
503 * yet started. In current implementation, can
504 * only be 0 or 1.
Todd Kjosd600e902017-05-25 17:35:02 -0700505 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700506 * @requested_threads_started: number binder threads started
Todd Kjosd600e902017-05-25 17:35:02 -0700507 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700508 * @ready_threads: number of threads waiting for proc work
Todd Kjosd600e902017-05-25 17:35:02 -0700509 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700510 * @tmp_ref: temporary reference to indicate proc is in use
Todd Kjosb4827902017-05-25 15:52:17 -0700511 * (protected by @inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700512 * @default_priority: default scheduler priority
513 * (invariant after initialized)
514 * @debugfs_entry: debugfs node
515 * @alloc: binder allocator bookkeeping
516 * @context: binder_context for this proc
517 * (invariant after initialized)
518 * @inner_lock: can nest under outer_lock and/or node lock
519 * @outer_lock: no nesting under innor or node lock
520 * Lock order: 1) outer, 2) node, 3) inner
521 *
522 * Bookkeeping structure for binder processes
523 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900524struct binder_proc {
525 struct hlist_node proc_node;
526 struct rb_root threads;
527 struct rb_root nodes;
528 struct rb_root refs_by_desc;
529 struct rb_root refs_by_node;
530 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900531 struct task_struct *tsk;
532 struct files_struct *files;
533 struct hlist_node deferred_work_node;
534 int deferred_work;
Todd Kjos2f993e22017-05-12 14:42:55 -0700535 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900536
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900537 struct list_head todo;
538 wait_queue_head_t wait;
539 struct binder_stats stats;
540 struct list_head delivered_death;
541 int max_threads;
542 int requested_threads;
543 int requested_threads_started;
544 int ready_threads;
Todd Kjos2f993e22017-05-12 14:42:55 -0700545 int tmp_ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900546 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700547 struct dentry *debugfs_entry;
Todd Kjosf85d2292016-10-10 10:39:59 -0700548 struct binder_alloc alloc;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200549 struct binder_context *context;
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700550 spinlock_t inner_lock;
551 spinlock_t outer_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900552};
553
554enum {
555 BINDER_LOOPER_STATE_REGISTERED = 0x01,
556 BINDER_LOOPER_STATE_ENTERED = 0x02,
557 BINDER_LOOPER_STATE_EXITED = 0x04,
558 BINDER_LOOPER_STATE_INVALID = 0x08,
559 BINDER_LOOPER_STATE_WAITING = 0x10,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900560};
561
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700562/**
563 * struct binder_thread - binder thread bookkeeping
564 * @proc: binder process for this thread
565 * (invariant after initialization)
566 * @rb_node: element for proc->threads rbtree
Todd Kjosb4827902017-05-25 15:52:17 -0700567 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700568 * @pid: PID for this thread
569 * (invariant after initialization)
570 * @looper: bitmap of looping state
571 * (only accessed by this thread)
572 * @looper_needs_return: looping thread needs to exit driver
573 * (no lock needed)
574 * @transaction_stack: stack of in-progress transactions for this thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700575 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700576 * @todo: list of work to do for this thread
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700577 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700578 * @return_error: transaction errors reported by this thread
579 * (only accessed by this thread)
580 * @reply_error: transaction errors reported by target thread
Martijn Coenen995a36e2017-06-02 13:36:52 -0700581 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700582 * @wait: wait queue for thread work
583 * @stats: per-thread statistics
584 * (atomics, no lock needed)
585 * @tmp_ref: temporary reference to indicate thread is in use
586 * (atomic since @proc->inner_lock cannot
587 * always be acquired)
588 * @is_dead: thread is dead and awaiting free
589 * when outstanding transactions are cleaned up
Todd Kjosb4827902017-05-25 15:52:17 -0700590 * (protected by @proc->inner_lock)
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700591 *
592 * Bookkeeping structure for binder threads.
593 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900594struct binder_thread {
595 struct binder_proc *proc;
596 struct rb_node rb_node;
597 int pid;
Todd Kjos6798e6d2017-01-06 14:19:25 -0800598 int looper; /* only modified by this thread */
599 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900600 struct binder_transaction *transaction_stack;
601 struct list_head todo;
Todd Kjos858b8da2017-04-21 17:35:12 -0700602 struct binder_error return_error;
603 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900604 wait_queue_head_t wait;
605 struct binder_stats stats;
Todd Kjos2f993e22017-05-12 14:42:55 -0700606 atomic_t tmp_ref;
607 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900608};
609
610struct binder_transaction {
611 int debug_id;
612 struct binder_work work;
613 struct binder_thread *from;
614 struct binder_transaction *from_parent;
615 struct binder_proc *to_proc;
616 struct binder_thread *to_thread;
617 struct binder_transaction *to_parent;
618 unsigned need_reply:1;
619 /* unsigned is_dead:1; */ /* not used at the moment */
620
621 struct binder_buffer *buffer;
622 unsigned int code;
623 unsigned int flags;
624 long priority;
625 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600626 kuid_t sender_euid;
Todd Kjos2f993e22017-05-12 14:42:55 -0700627 /**
628 * @lock: protects @from, @to_proc, and @to_thread
629 *
630 * @from, @to_proc, and @to_thread can be set to NULL
631 * during thread teardown
632 */
633 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900634};
635
Todd Kjosfc7a7e22017-05-29 16:44:24 -0700636/**
637 * binder_proc_lock() - Acquire outer lock for given binder_proc
638 * @proc: struct binder_proc to acquire
639 *
640 * Acquires proc->outer_lock. Used to protect binder_ref
641 * structures associated with the given proc.
642 */
643#define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__)
644static void
645_binder_proc_lock(struct binder_proc *proc, int line)
646{
647 binder_debug(BINDER_DEBUG_SPINLOCKS,
648 "%s: line=%d\n", __func__, line);
649 spin_lock(&proc->outer_lock);
650}
651
652/**
653 * binder_proc_unlock() - Release spinlock for given binder_proc
654 * @proc: struct binder_proc to acquire
655 *
656 * Release lock acquired via binder_proc_lock()
657 */
658#define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__)
659static void
660_binder_proc_unlock(struct binder_proc *proc, int line)
661{
662 binder_debug(BINDER_DEBUG_SPINLOCKS,
663 "%s: line=%d\n", __func__, line);
664 spin_unlock(&proc->outer_lock);
665}
666
667/**
668 * binder_inner_proc_lock() - Acquire inner lock for given binder_proc
669 * @proc: struct binder_proc to acquire
670 *
671 * Acquires proc->inner_lock. Used to protect todo lists
672 */
673#define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__)
674static void
675_binder_inner_proc_lock(struct binder_proc *proc, int line)
676{
677 binder_debug(BINDER_DEBUG_SPINLOCKS,
678 "%s: line=%d\n", __func__, line);
679 spin_lock(&proc->inner_lock);
680}
681
682/**
683 * binder_inner_proc_unlock() - Release inner lock for given binder_proc
684 * @proc: struct binder_proc to acquire
685 *
686 * Release lock acquired via binder_inner_proc_lock()
687 */
688#define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__)
689static void
690_binder_inner_proc_unlock(struct binder_proc *proc, int line)
691{
692 binder_debug(BINDER_DEBUG_SPINLOCKS,
693 "%s: line=%d\n", __func__, line);
694 spin_unlock(&proc->inner_lock);
695}
696
697/**
698 * binder_node_lock() - Acquire spinlock for given binder_node
699 * @node: struct binder_node to acquire
700 *
701 * Acquires node->lock. Used to protect binder_node fields
702 */
703#define binder_node_lock(node) _binder_node_lock(node, __LINE__)
704static void
705_binder_node_lock(struct binder_node *node, int line)
706{
707 binder_debug(BINDER_DEBUG_SPINLOCKS,
708 "%s: line=%d\n", __func__, line);
709 spin_lock(&node->lock);
710}
711
712/**
713 * binder_node_unlock() - Release spinlock for given binder_proc
714 * @node: struct binder_node to acquire
715 *
716 * Release lock acquired via binder_node_lock()
717 */
718#define binder_node_unlock(node) _binder_node_unlock(node, __LINE__)
719static void
720_binder_node_unlock(struct binder_node *node, int line)
721{
722 binder_debug(BINDER_DEBUG_SPINLOCKS,
723 "%s: line=%d\n", __func__, line);
724 spin_unlock(&node->lock);
725}
726
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700727/**
728 * binder_node_inner_lock() - Acquire node and inner locks
729 * @node: struct binder_node to acquire
730 *
731 * Acquires node->lock. If node->proc also acquires
732 * proc->inner_lock. Used to protect binder_node fields
733 */
734#define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__)
735static void
736_binder_node_inner_lock(struct binder_node *node, int line)
737{
738 binder_debug(BINDER_DEBUG_SPINLOCKS,
739 "%s: line=%d\n", __func__, line);
740 spin_lock(&node->lock);
741 if (node->proc)
742 binder_inner_proc_lock(node->proc);
743}
744
745/**
746 * binder_node_unlock() - Release node and inner locks
747 * @node: struct binder_node to acquire
748 *
749 * Release lock acquired via binder_node_lock()
750 */
751#define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__)
752static void
753_binder_node_inner_unlock(struct binder_node *node, int line)
754{
755 struct binder_proc *proc = node->proc;
756
757 binder_debug(BINDER_DEBUG_SPINLOCKS,
758 "%s: line=%d\n", __func__, line);
759 if (proc)
760 binder_inner_proc_unlock(proc);
761 spin_unlock(&node->lock);
762}
763
Todd Kjos1c89e6b2016-10-20 10:33:00 -0700764static bool binder_worklist_empty_ilocked(struct list_head *list)
765{
766 return list_empty(list);
767}
768
769/**
770 * binder_worklist_empty() - Check if no items on the work list
771 * @proc: binder_proc associated with list
772 * @list: list to check
773 *
774 * Return: true if there are no items on list, else false
775 */
776static bool binder_worklist_empty(struct binder_proc *proc,
777 struct list_head *list)
778{
779 bool ret;
780
781 binder_inner_proc_lock(proc);
782 ret = binder_worklist_empty_ilocked(list);
783 binder_inner_proc_unlock(proc);
784 return ret;
785}
786
787static void
788binder_enqueue_work_ilocked(struct binder_work *work,
789 struct list_head *target_list)
790{
791 BUG_ON(target_list == NULL);
792 BUG_ON(work->entry.next && !list_empty(&work->entry));
793 list_add_tail(&work->entry, target_list);
794}
795
796/**
797 * binder_enqueue_work() - Add an item to the work list
798 * @proc: binder_proc associated with list
799 * @work: struct binder_work to add to list
800 * @target_list: list to add work to
801 *
802 * Adds the work to the specified list. Asserts that work
803 * is not already on a list.
804 */
805static void
806binder_enqueue_work(struct binder_proc *proc,
807 struct binder_work *work,
808 struct list_head *target_list)
809{
810 binder_inner_proc_lock(proc);
811 binder_enqueue_work_ilocked(work, target_list);
812 binder_inner_proc_unlock(proc);
813}
814
815static void
816binder_dequeue_work_ilocked(struct binder_work *work)
817{
818 list_del_init(&work->entry);
819}
820
821/**
822 * binder_dequeue_work() - Removes an item from the work list
823 * @proc: binder_proc associated with list
824 * @work: struct binder_work to remove from list
825 *
826 * Removes the specified work item from whatever list it is on.
827 * Can safely be called if work is not on any list.
828 */
829static void
830binder_dequeue_work(struct binder_proc *proc, struct binder_work *work)
831{
832 binder_inner_proc_lock(proc);
833 binder_dequeue_work_ilocked(work);
834 binder_inner_proc_unlock(proc);
835}
836
837static struct binder_work *binder_dequeue_work_head_ilocked(
838 struct list_head *list)
839{
840 struct binder_work *w;
841
842 w = list_first_entry_or_null(list, struct binder_work, entry);
843 if (w)
844 list_del_init(&w->entry);
845 return w;
846}
847
848/**
849 * binder_dequeue_work_head() - Dequeues the item at head of list
850 * @proc: binder_proc associated with list
851 * @list: list to dequeue head
852 *
853 * Removes the head of the list if there are items on the list
854 *
855 * Return: pointer dequeued binder_work, NULL if list was empty
856 */
857static struct binder_work *binder_dequeue_work_head(
858 struct binder_proc *proc,
859 struct list_head *list)
860{
861 struct binder_work *w;
862
863 binder_inner_proc_lock(proc);
864 w = binder_dequeue_work_head_ilocked(list);
865 binder_inner_proc_unlock(proc);
866 return w;
867}
868
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900869static void
870binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos2f993e22017-05-12 14:42:55 -0700871static void binder_free_thread(struct binder_thread *thread);
872static void binder_free_proc(struct binder_proc *proc);
Todd Kjos425d23f2017-06-12 12:07:26 -0700873static void binder_inc_node_tmpref_ilocked(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900874
Sachin Kamatefde99c2012-08-17 16:39:36 +0530875static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900876{
877 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900878 unsigned long rlim_cur;
879 unsigned long irqs;
880
881 if (files == NULL)
882 return -ESRCH;
883
Al Virodcfadfa2012-08-12 17:27:30 -0400884 if (!lock_task_sighand(proc->tsk, &irqs))
885 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900886
Al Virodcfadfa2012-08-12 17:27:30 -0400887 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
888 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900889
Al Virodcfadfa2012-08-12 17:27:30 -0400890 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900891}
892
893/*
894 * copied from fd_install
895 */
896static void task_fd_install(
897 struct binder_proc *proc, unsigned int fd, struct file *file)
898{
Al Virof869e8a2012-08-15 21:06:33 -0400899 if (proc->files)
900 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900901}
902
903/*
904 * copied from sys_close
905 */
906static long task_close_fd(struct binder_proc *proc, unsigned int fd)
907{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900908 int retval;
909
Al Viro483ce1d2012-08-19 12:04:24 -0400910 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900911 return -ESRCH;
912
Al Viro483ce1d2012-08-19 12:04:24 -0400913 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900914 /* can't restart close syscall because file table entry was cleared */
915 if (unlikely(retval == -ERESTARTSYS ||
916 retval == -ERESTARTNOINTR ||
917 retval == -ERESTARTNOHAND ||
918 retval == -ERESTART_RESTARTBLOCK))
919 retval = -EINTR;
920
921 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900922}
923
924static void binder_set_nice(long nice)
925{
926 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900927
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900928 if (can_nice(current, nice)) {
929 set_user_nice(current, nice);
930 return;
931 }
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900932 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900933 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530934 "%d: nice value %ld not allowed use %ld instead\n",
935 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900936 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +0800937 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900938 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530939 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900940}
941
Todd Kjos425d23f2017-06-12 12:07:26 -0700942static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc,
943 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900944{
945 struct rb_node *n = proc->nodes.rb_node;
946 struct binder_node *node;
947
Todd Kjos425d23f2017-06-12 12:07:26 -0700948 BUG_ON(!spin_is_locked(&proc->inner_lock));
949
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900950 while (n) {
951 node = rb_entry(n, struct binder_node, rb_node);
952
953 if (ptr < node->ptr)
954 n = n->rb_left;
955 else if (ptr > node->ptr)
956 n = n->rb_right;
Todd Kjosf22abc72017-05-09 11:08:05 -0700957 else {
958 /*
959 * take an implicit weak reference
960 * to ensure node stays alive until
961 * call to binder_put_node()
962 */
Todd Kjos425d23f2017-06-12 12:07:26 -0700963 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900964 return node;
Todd Kjosf22abc72017-05-09 11:08:05 -0700965 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900966 }
967 return NULL;
968}
969
Todd Kjos425d23f2017-06-12 12:07:26 -0700970static struct binder_node *binder_get_node(struct binder_proc *proc,
971 binder_uintptr_t ptr)
972{
973 struct binder_node *node;
974
975 binder_inner_proc_lock(proc);
976 node = binder_get_node_ilocked(proc, ptr);
977 binder_inner_proc_unlock(proc);
978 return node;
979}
980
981static struct binder_node *binder_init_node_ilocked(
982 struct binder_proc *proc,
983 struct binder_node *new_node,
984 struct flat_binder_object *fp)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900985{
986 struct rb_node **p = &proc->nodes.rb_node;
987 struct rb_node *parent = NULL;
988 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -0700989 binder_uintptr_t ptr = fp ? fp->binder : 0;
990 binder_uintptr_t cookie = fp ? fp->cookie : 0;
991 __u32 flags = fp ? fp->flags : 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900992
Todd Kjos425d23f2017-06-12 12:07:26 -0700993 BUG_ON(!spin_is_locked(&proc->inner_lock));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900994 while (*p) {
Todd Kjos425d23f2017-06-12 12:07:26 -0700995
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900996 parent = *p;
997 node = rb_entry(parent, struct binder_node, rb_node);
998
999 if (ptr < node->ptr)
1000 p = &(*p)->rb_left;
1001 else if (ptr > node->ptr)
1002 p = &(*p)->rb_right;
Todd Kjos425d23f2017-06-12 12:07:26 -07001003 else {
1004 /*
1005 * A matching node is already in
1006 * the rb tree. Abandon the init
1007 * and return it.
1008 */
1009 binder_inc_node_tmpref_ilocked(node);
1010 return node;
1011 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001012 }
Todd Kjos425d23f2017-06-12 12:07:26 -07001013 node = new_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001014 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosf22abc72017-05-09 11:08:05 -07001015 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001016 rb_link_node(&node->rb_node, parent, p);
1017 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -07001018 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001019 node->proc = proc;
1020 node->ptr = ptr;
1021 node->cookie = cookie;
1022 node->work.type = BINDER_WORK_NODE;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001023 node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1024 node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
Todd Kjosfc7a7e22017-05-29 16:44:24 -07001025 spin_lock_init(&node->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001026 INIT_LIST_HEAD(&node->work.entry);
1027 INIT_LIST_HEAD(&node->async_todo);
1028 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001029 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001030 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001031 (u64)node->ptr, (u64)node->cookie);
Todd Kjos425d23f2017-06-12 12:07:26 -07001032
1033 return node;
1034}
1035
1036static struct binder_node *binder_new_node(struct binder_proc *proc,
1037 struct flat_binder_object *fp)
1038{
1039 struct binder_node *node;
1040 struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL);
1041
1042 if (!new_node)
1043 return NULL;
1044 binder_inner_proc_lock(proc);
1045 node = binder_init_node_ilocked(proc, new_node, fp);
1046 binder_inner_proc_unlock(proc);
1047 if (node != new_node)
1048 /*
1049 * The node was already added by another thread
1050 */
1051 kfree(new_node);
1052
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001053 return node;
1054}
1055
Todd Kjose7f23ed2017-03-21 13:06:01 -07001056static void binder_free_node(struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001057{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001058 kfree(node);
1059 binder_stats_deleted(BINDER_STAT_NODE);
1060}
1061
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001062static int binder_inc_node_nilocked(struct binder_node *node, int strong,
1063 int internal,
1064 struct list_head *target_list)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001065{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001066 struct binder_proc *proc = node->proc;
1067
1068 BUG_ON(!spin_is_locked(&node->lock));
1069 if (proc)
1070 BUG_ON(!spin_is_locked(&proc->inner_lock));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001071 if (strong) {
1072 if (internal) {
1073 if (target_list == NULL &&
1074 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001075 !(node->proc &&
1076 node == node->proc->context->
1077 binder_context_mgr_node &&
1078 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301079 pr_err("invalid inc strong node for %d\n",
1080 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001081 return -EINVAL;
1082 }
1083 node->internal_strong_refs++;
1084 } else
1085 node->local_strong_refs++;
1086 if (!node->has_strong_ref && target_list) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001087 binder_dequeue_work_ilocked(&node->work);
1088 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001089 }
1090 } else {
1091 if (!internal)
1092 node->local_weak_refs++;
1093 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1094 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301095 pr_err("invalid inc weak node for %d\n",
1096 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001097 return -EINVAL;
1098 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001099 binder_enqueue_work_ilocked(&node->work, target_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001100 }
1101 }
1102 return 0;
1103}
1104
Todd Kjose7f23ed2017-03-21 13:06:01 -07001105static int binder_inc_node(struct binder_node *node, int strong, int internal,
1106 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001107{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001108 int ret;
1109
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001110 binder_node_inner_lock(node);
1111 ret = binder_inc_node_nilocked(node, strong, internal, target_list);
1112 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001113
1114 return ret;
1115}
1116
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001117static bool binder_dec_node_nilocked(struct binder_node *node,
1118 int strong, int internal)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001119{
1120 struct binder_proc *proc = node->proc;
1121
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001122 BUG_ON(!spin_is_locked(&node->lock));
Todd Kjose7f23ed2017-03-21 13:06:01 -07001123 if (proc)
1124 BUG_ON(!spin_is_locked(&proc->inner_lock));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001125 if (strong) {
1126 if (internal)
1127 node->internal_strong_refs--;
1128 else
1129 node->local_strong_refs--;
1130 if (node->local_strong_refs || node->internal_strong_refs)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001131 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001132 } else {
1133 if (!internal)
1134 node->local_weak_refs--;
Todd Kjosf22abc72017-05-09 11:08:05 -07001135 if (node->local_weak_refs || node->tmp_refs ||
1136 !hlist_empty(&node->refs))
Todd Kjose7f23ed2017-03-21 13:06:01 -07001137 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001138 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001139
1140 if (proc && (node->has_strong_ref || node->has_weak_ref)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001141 if (list_empty(&node->work.entry)) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001142 binder_enqueue_work_ilocked(&node->work, &proc->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001143 wake_up_interruptible(&node->proc->wait);
1144 }
1145 } else {
1146 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosf22abc72017-05-09 11:08:05 -07001147 !node->local_weak_refs && !node->tmp_refs) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07001148 if (proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001149 binder_dequeue_work_ilocked(&node->work);
1150 rb_erase(&node->rb_node, &proc->nodes);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001151 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301152 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001153 node->debug_id);
1154 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001155 BUG_ON(!list_empty(&node->work.entry));
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001156 spin_lock(&binder_dead_nodes_lock);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001157 /*
1158 * tmp_refs could have changed so
1159 * check it again
1160 */
1161 if (node->tmp_refs) {
1162 spin_unlock(&binder_dead_nodes_lock);
1163 return false;
1164 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001165 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001166 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001167 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301168 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001169 node->debug_id);
1170 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001171 return true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001172 }
1173 }
Todd Kjose7f23ed2017-03-21 13:06:01 -07001174 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001175}
1176
Todd Kjose7f23ed2017-03-21 13:06:01 -07001177static void binder_dec_node(struct binder_node *node, int strong, int internal)
1178{
1179 bool free_node;
1180
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001181 binder_node_inner_lock(node);
1182 free_node = binder_dec_node_nilocked(node, strong, internal);
1183 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001184 if (free_node)
1185 binder_free_node(node);
1186}
1187
1188static void binder_inc_node_tmpref_ilocked(struct binder_node *node)
Todd Kjosf22abc72017-05-09 11:08:05 -07001189{
1190 /*
1191 * No call to binder_inc_node() is needed since we
1192 * don't need to inform userspace of any changes to
1193 * tmp_refs
1194 */
1195 node->tmp_refs++;
1196}
1197
1198/**
Todd Kjose7f23ed2017-03-21 13:06:01 -07001199 * binder_inc_node_tmpref() - take a temporary reference on node
1200 * @node: node to reference
1201 *
1202 * Take reference on node to prevent the node from being freed
1203 * while referenced only by a local variable. The inner lock is
1204 * needed to serialize with the node work on the queue (which
1205 * isn't needed after the node is dead). If the node is dead
1206 * (node->proc is NULL), use binder_dead_nodes_lock to protect
1207 * node->tmp_refs against dead-node-only cases where the node
1208 * lock cannot be acquired (eg traversing the dead node list to
1209 * print nodes)
1210 */
1211static void binder_inc_node_tmpref(struct binder_node *node)
1212{
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001213 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001214 if (node->proc)
1215 binder_inner_proc_lock(node->proc);
1216 else
1217 spin_lock(&binder_dead_nodes_lock);
1218 binder_inc_node_tmpref_ilocked(node);
1219 if (node->proc)
1220 binder_inner_proc_unlock(node->proc);
1221 else
1222 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001223 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001224}
1225
1226/**
Todd Kjosf22abc72017-05-09 11:08:05 -07001227 * binder_dec_node_tmpref() - remove a temporary reference on node
1228 * @node: node to reference
1229 *
1230 * Release temporary reference on node taken via binder_inc_node_tmpref()
1231 */
1232static void binder_dec_node_tmpref(struct binder_node *node)
1233{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001234 bool free_node;
1235
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001236 binder_node_inner_lock(node);
1237 if (!node->proc)
Todd Kjose7f23ed2017-03-21 13:06:01 -07001238 spin_lock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001239 node->tmp_refs--;
1240 BUG_ON(node->tmp_refs < 0);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001241 if (!node->proc)
1242 spin_unlock(&binder_dead_nodes_lock);
Todd Kjosf22abc72017-05-09 11:08:05 -07001243 /*
1244 * Call binder_dec_node() to check if all refcounts are 0
1245 * and cleanup is needed. Calling with strong=0 and internal=1
1246 * causes no actual reference to be released in binder_dec_node().
1247 * If that changes, a change is needed here too.
1248 */
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001249 free_node = binder_dec_node_nilocked(node, 0, 1);
1250 binder_node_inner_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001251 if (free_node)
1252 binder_free_node(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07001253}
1254
1255static void binder_put_node(struct binder_node *node)
1256{
1257 binder_dec_node_tmpref(node);
1258}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001259
Todd Kjos5346bf32016-10-20 16:43:34 -07001260static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc,
1261 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001262{
1263 struct rb_node *n = proc->refs_by_desc.rb_node;
1264 struct binder_ref *ref;
1265
1266 while (n) {
1267 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1268
Todd Kjosb0117bb2017-05-08 09:16:27 -07001269 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001270 n = n->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001271 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001272 n = n->rb_right;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001273 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001274 binder_user_error("tried to use weak ref as strong ref\n");
1275 return NULL;
1276 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001277 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001278 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001279 }
1280 return NULL;
1281}
1282
Todd Kjosb0117bb2017-05-08 09:16:27 -07001283/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001284 * binder_get_ref_for_node_olocked() - get the ref associated with given node
Todd Kjosb0117bb2017-05-08 09:16:27 -07001285 * @proc: binder_proc that owns the ref
1286 * @node: binder_node of target
1287 * @new_ref: newly allocated binder_ref to be initialized or %NULL
1288 *
1289 * Look up the ref for the given node and return it if it exists
1290 *
1291 * If it doesn't exist and the caller provides a newly allocated
1292 * ref, initialize the fields of the newly allocated ref and insert
1293 * into the given proc rb_trees and node refs list.
1294 *
1295 * Return: the ref for node. It is possible that another thread
1296 * allocated/initialized the ref first in which case the
1297 * returned ref would be different than the passed-in
1298 * new_ref. new_ref must be kfree'd by the caller in
1299 * this case.
1300 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001301static struct binder_ref *binder_get_ref_for_node_olocked(
1302 struct binder_proc *proc,
1303 struct binder_node *node,
1304 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001305{
Todd Kjosb0117bb2017-05-08 09:16:27 -07001306 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001307 struct rb_node **p = &proc->refs_by_node.rb_node;
1308 struct rb_node *parent = NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001309 struct binder_ref *ref;
1310 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001311
1312 while (*p) {
1313 parent = *p;
1314 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1315
1316 if (node < ref->node)
1317 p = &(*p)->rb_left;
1318 else if (node > ref->node)
1319 p = &(*p)->rb_right;
1320 else
1321 return ref;
1322 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001323 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001324 return NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001325
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001326 binder_stats_created(BINDER_STAT_REF);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001327 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001328 new_ref->proc = proc;
1329 new_ref->node = node;
1330 rb_link_node(&new_ref->rb_node_node, parent, p);
1331 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1332
Todd Kjosb0117bb2017-05-08 09:16:27 -07001333 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001334 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1335 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001336 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001337 break;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001338 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001339 }
1340
1341 p = &proc->refs_by_desc.rb_node;
1342 while (*p) {
1343 parent = *p;
1344 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1345
Todd Kjosb0117bb2017-05-08 09:16:27 -07001346 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001347 p = &(*p)->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001348 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001349 p = &(*p)->rb_right;
1350 else
1351 BUG();
1352 }
1353 rb_link_node(&new_ref->rb_node_desc, parent, p);
1354 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001355
1356 binder_node_lock(node);
Todd Kjos4cbe5752017-05-01 17:21:51 -07001357 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001358
Todd Kjos4cbe5752017-05-01 17:21:51 -07001359 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
1360 "%d new ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001361 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjos4cbe5752017-05-01 17:21:51 -07001362 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001363 binder_node_unlock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001364 return new_ref;
1365}
1366
Todd Kjos5346bf32016-10-20 16:43:34 -07001367static void binder_cleanup_ref_olocked(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001368{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001369 bool delete_node = false;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001370
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001371 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301372 "%d delete ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001373 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301374 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001375
1376 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1377 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001378
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001379 binder_node_inner_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001380 if (ref->data.strong)
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001381 binder_dec_node_nilocked(ref->node, 1, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001382
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001383 hlist_del(&ref->node_entry);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07001384 delete_node = binder_dec_node_nilocked(ref->node, 0, 1);
1385 binder_node_inner_unlock(ref->node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07001386 /*
1387 * Clear ref->node unless we want the caller to free the node
1388 */
1389 if (!delete_node) {
1390 /*
1391 * The caller uses ref->node to determine
1392 * whether the node needs to be freed. Clear
1393 * it since the node is still alive.
1394 */
1395 ref->node = NULL;
1396 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001397
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001398 if (ref->death) {
1399 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301400 "%d delete ref %d desc %d has death notification\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001401 ref->proc->pid, ref->data.debug_id,
1402 ref->data.desc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001403 binder_dequeue_work(ref->proc, &ref->death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001404 binder_stats_deleted(BINDER_STAT_DEATH);
1405 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001406 binder_stats_deleted(BINDER_STAT_REF);
1407}
1408
Todd Kjosb0117bb2017-05-08 09:16:27 -07001409/**
Todd Kjos5346bf32016-10-20 16:43:34 -07001410 * binder_inc_ref_olocked() - increment the ref for given handle
Todd Kjosb0117bb2017-05-08 09:16:27 -07001411 * @ref: ref to be incremented
1412 * @strong: if true, strong increment, else weak
1413 * @target_list: list to queue node work on
1414 *
Todd Kjos5346bf32016-10-20 16:43:34 -07001415 * Increment the ref. @ref->proc->outer_lock must be held on entry
Todd Kjosb0117bb2017-05-08 09:16:27 -07001416 *
1417 * Return: 0, if successful, else errno
1418 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001419static int binder_inc_ref_olocked(struct binder_ref *ref, int strong,
1420 struct list_head *target_list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001421{
1422 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001423
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001424 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001425 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001426 ret = binder_inc_node(ref->node, 1, 1, target_list);
1427 if (ret)
1428 return ret;
1429 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001430 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001431 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001432 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001433 ret = binder_inc_node(ref->node, 0, 1, target_list);
1434 if (ret)
1435 return ret;
1436 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001437 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001438 }
1439 return 0;
1440}
1441
Todd Kjosb0117bb2017-05-08 09:16:27 -07001442/**
1443 * binder_dec_ref() - dec the ref for given handle
1444 * @ref: ref to be decremented
1445 * @strong: if true, strong decrement, else weak
1446 *
1447 * Decrement the ref.
1448 *
Todd Kjosb0117bb2017-05-08 09:16:27 -07001449 * Return: true if ref is cleaned up and ready to be freed
1450 */
Todd Kjos5346bf32016-10-20 16:43:34 -07001451static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001452{
1453 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001454 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301455 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001456 ref->proc->pid, ref->data.debug_id,
1457 ref->data.desc, ref->data.strong,
1458 ref->data.weak);
1459 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001460 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001461 ref->data.strong--;
Todd Kjose7f23ed2017-03-21 13:06:01 -07001462 if (ref->data.strong == 0)
1463 binder_dec_node(ref->node, strong, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001464 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001465 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301466 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001467 ref->proc->pid, ref->data.debug_id,
1468 ref->data.desc, ref->data.strong,
1469 ref->data.weak);
1470 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001471 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001472 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001473 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07001474 if (ref->data.strong == 0 && ref->data.weak == 0) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001475 binder_cleanup_ref_olocked(ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001476 return true;
1477 }
1478 return false;
1479}
1480
1481/**
1482 * binder_get_node_from_ref() - get the node from the given proc/desc
1483 * @proc: proc containing the ref
1484 * @desc: the handle associated with the ref
1485 * @need_strong_ref: if true, only return node if ref is strong
1486 * @rdata: the id/refcount data for the ref
1487 *
1488 * Given a proc and ref handle, return the associated binder_node
1489 *
1490 * Return: a binder_node or NULL if not found or not strong when strong required
1491 */
1492static struct binder_node *binder_get_node_from_ref(
1493 struct binder_proc *proc,
1494 u32 desc, bool need_strong_ref,
1495 struct binder_ref_data *rdata)
1496{
1497 struct binder_node *node;
1498 struct binder_ref *ref;
1499
Todd Kjos5346bf32016-10-20 16:43:34 -07001500 binder_proc_lock(proc);
1501 ref = binder_get_ref_olocked(proc, desc, need_strong_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001502 if (!ref)
1503 goto err_no_ref;
1504 node = ref->node;
Todd Kjosf22abc72017-05-09 11:08:05 -07001505 /*
1506 * Take an implicit reference on the node to ensure
1507 * it stays alive until the call to binder_put_node()
1508 */
1509 binder_inc_node_tmpref(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001510 if (rdata)
1511 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001512 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001513
1514 return node;
1515
1516err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001517 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001518 return NULL;
1519}
1520
1521/**
1522 * binder_free_ref() - free the binder_ref
1523 * @ref: ref to free
1524 *
Todd Kjose7f23ed2017-03-21 13:06:01 -07001525 * Free the binder_ref. Free the binder_node indicated by ref->node
1526 * (if non-NULL) and the binder_ref_death indicated by ref->death.
Todd Kjosb0117bb2017-05-08 09:16:27 -07001527 */
1528static void binder_free_ref(struct binder_ref *ref)
1529{
Todd Kjose7f23ed2017-03-21 13:06:01 -07001530 if (ref->node)
1531 binder_free_node(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001532 kfree(ref->death);
1533 kfree(ref);
1534}
1535
1536/**
1537 * binder_update_ref_for_handle() - inc/dec the ref for given handle
1538 * @proc: proc containing the ref
1539 * @desc: the handle associated with the ref
1540 * @increment: true=inc reference, false=dec reference
1541 * @strong: true=strong reference, false=weak reference
1542 * @rdata: the id/refcount data for the ref
1543 *
1544 * Given a proc and ref handle, increment or decrement the ref
1545 * according to "increment" arg.
1546 *
1547 * Return: 0 if successful, else errno
1548 */
1549static int binder_update_ref_for_handle(struct binder_proc *proc,
1550 uint32_t desc, bool increment, bool strong,
1551 struct binder_ref_data *rdata)
1552{
1553 int ret = 0;
1554 struct binder_ref *ref;
1555 bool delete_ref = false;
1556
Todd Kjos5346bf32016-10-20 16:43:34 -07001557 binder_proc_lock(proc);
1558 ref = binder_get_ref_olocked(proc, desc, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001559 if (!ref) {
1560 ret = -EINVAL;
1561 goto err_no_ref;
1562 }
1563 if (increment)
Todd Kjos5346bf32016-10-20 16:43:34 -07001564 ret = binder_inc_ref_olocked(ref, strong, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001565 else
Todd Kjos5346bf32016-10-20 16:43:34 -07001566 delete_ref = binder_dec_ref_olocked(ref, strong);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001567
1568 if (rdata)
1569 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001570 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001571
1572 if (delete_ref)
1573 binder_free_ref(ref);
1574 return ret;
1575
1576err_no_ref:
Todd Kjos5346bf32016-10-20 16:43:34 -07001577 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001578 return ret;
1579}
1580
1581/**
1582 * binder_dec_ref_for_handle() - dec the ref for given handle
1583 * @proc: proc containing the ref
1584 * @desc: the handle associated with the ref
1585 * @strong: true=strong reference, false=weak reference
1586 * @rdata: the id/refcount data for the ref
1587 *
1588 * Just calls binder_update_ref_for_handle() to decrement the ref.
1589 *
1590 * Return: 0 if successful, else errno
1591 */
1592static int binder_dec_ref_for_handle(struct binder_proc *proc,
1593 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1594{
1595 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1596}
1597
1598
1599/**
1600 * binder_inc_ref_for_node() - increment the ref for given proc/node
1601 * @proc: proc containing the ref
1602 * @node: target node
1603 * @strong: true=strong reference, false=weak reference
1604 * @target_list: worklist to use if node is incremented
1605 * @rdata: the id/refcount data for the ref
1606 *
1607 * Given a proc and node, increment the ref. Create the ref if it
1608 * doesn't already exist
1609 *
1610 * Return: 0 if successful, else errno
1611 */
1612static int binder_inc_ref_for_node(struct binder_proc *proc,
1613 struct binder_node *node,
1614 bool strong,
1615 struct list_head *target_list,
1616 struct binder_ref_data *rdata)
1617{
1618 struct binder_ref *ref;
1619 struct binder_ref *new_ref = NULL;
1620 int ret = 0;
1621
Todd Kjos5346bf32016-10-20 16:43:34 -07001622 binder_proc_lock(proc);
1623 ref = binder_get_ref_for_node_olocked(proc, node, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001624 if (!ref) {
Todd Kjos5346bf32016-10-20 16:43:34 -07001625 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001626 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1627 if (!new_ref)
1628 return -ENOMEM;
Todd Kjos5346bf32016-10-20 16:43:34 -07001629 binder_proc_lock(proc);
1630 ref = binder_get_ref_for_node_olocked(proc, node, new_ref);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001631 }
Todd Kjos5346bf32016-10-20 16:43:34 -07001632 ret = binder_inc_ref_olocked(ref, strong, target_list);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001633 *rdata = ref->data;
Todd Kjos5346bf32016-10-20 16:43:34 -07001634 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001635 if (new_ref && ref != new_ref)
1636 /*
1637 * Another thread created the ref first so
1638 * free the one we allocated
1639 */
1640 kfree(new_ref);
1641 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001642}
1643
Martijn Coenen995a36e2017-06-02 13:36:52 -07001644static void binder_pop_transaction_ilocked(struct binder_thread *target_thread,
1645 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001646{
Todd Kjos21ef40a2017-03-30 18:02:13 -07001647 BUG_ON(!target_thread);
Martijn Coenen995a36e2017-06-02 13:36:52 -07001648 BUG_ON(!spin_is_locked(&target_thread->proc->inner_lock));
Todd Kjos21ef40a2017-03-30 18:02:13 -07001649 BUG_ON(target_thread->transaction_stack != t);
1650 BUG_ON(target_thread->transaction_stack->from != target_thread);
1651 target_thread->transaction_stack =
1652 target_thread->transaction_stack->from_parent;
1653 t->from = NULL;
1654}
1655
Todd Kjos2f993e22017-05-12 14:42:55 -07001656/**
1657 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1658 * @thread: thread to decrement
1659 *
1660 * A thread needs to be kept alive while being used to create or
1661 * handle a transaction. binder_get_txn_from() is used to safely
1662 * extract t->from from a binder_transaction and keep the thread
1663 * indicated by t->from from being freed. When done with that
1664 * binder_thread, this function is called to decrement the
1665 * tmp_ref and free if appropriate (thread has been released
1666 * and no transaction being processed by the driver)
1667 */
1668static void binder_thread_dec_tmpref(struct binder_thread *thread)
1669{
1670 /*
1671 * atomic is used to protect the counter value while
1672 * it cannot reach zero or thread->is_dead is false
Todd Kjos2f993e22017-05-12 14:42:55 -07001673 */
Todd Kjosb4827902017-05-25 15:52:17 -07001674 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001675 atomic_dec(&thread->tmp_ref);
1676 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
Todd Kjosb4827902017-05-25 15:52:17 -07001677 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001678 binder_free_thread(thread);
1679 return;
1680 }
Todd Kjosb4827902017-05-25 15:52:17 -07001681 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001682}
1683
1684/**
1685 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1686 * @proc: proc to decrement
1687 *
1688 * A binder_proc needs to be kept alive while being used to create or
1689 * handle a transaction. proc->tmp_ref is incremented when
1690 * creating a new transaction or the binder_proc is currently in-use
1691 * by threads that are being released. When done with the binder_proc,
1692 * this function is called to decrement the counter and free the
1693 * proc if appropriate (proc has been released, all threads have
1694 * been released and not currenly in-use to process a transaction).
1695 */
1696static void binder_proc_dec_tmpref(struct binder_proc *proc)
1697{
Todd Kjosb4827902017-05-25 15:52:17 -07001698 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001699 proc->tmp_ref--;
1700 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1701 !proc->tmp_ref) {
Todd Kjosb4827902017-05-25 15:52:17 -07001702 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001703 binder_free_proc(proc);
1704 return;
1705 }
Todd Kjosb4827902017-05-25 15:52:17 -07001706 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001707}
1708
1709/**
1710 * binder_get_txn_from() - safely extract the "from" thread in transaction
1711 * @t: binder transaction for t->from
1712 *
1713 * Atomically return the "from" thread and increment the tmp_ref
1714 * count for the thread to ensure it stays alive until
1715 * binder_thread_dec_tmpref() is called.
1716 *
1717 * Return: the value of t->from
1718 */
1719static struct binder_thread *binder_get_txn_from(
1720 struct binder_transaction *t)
1721{
1722 struct binder_thread *from;
1723
1724 spin_lock(&t->lock);
1725 from = t->from;
1726 if (from)
1727 atomic_inc(&from->tmp_ref);
1728 spin_unlock(&t->lock);
1729 return from;
1730}
1731
Martijn Coenen995a36e2017-06-02 13:36:52 -07001732/**
1733 * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock
1734 * @t: binder transaction for t->from
1735 *
1736 * Same as binder_get_txn_from() except it also acquires the proc->inner_lock
1737 * to guarantee that the thread cannot be released while operating on it.
1738 * The caller must call binder_inner_proc_unlock() to release the inner lock
1739 * as well as call binder_dec_thread_txn() to release the reference.
1740 *
1741 * Return: the value of t->from
1742 */
1743static struct binder_thread *binder_get_txn_from_and_acq_inner(
1744 struct binder_transaction *t)
1745{
1746 struct binder_thread *from;
1747
1748 from = binder_get_txn_from(t);
1749 if (!from)
1750 return NULL;
1751 binder_inner_proc_lock(from->proc);
1752 if (t->from) {
1753 BUG_ON(from != t->from);
1754 return from;
1755 }
1756 binder_inner_proc_unlock(from->proc);
1757 binder_thread_dec_tmpref(from);
1758 return NULL;
1759}
1760
Todd Kjos21ef40a2017-03-30 18:02:13 -07001761static void binder_free_transaction(struct binder_transaction *t)
1762{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001763 if (t->buffer)
1764 t->buffer->transaction = NULL;
1765 kfree(t);
1766 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1767}
1768
1769static void binder_send_failed_reply(struct binder_transaction *t,
1770 uint32_t error_code)
1771{
1772 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001773 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001774
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001775 BUG_ON(t->flags & TF_ONE_WAY);
1776 while (1) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07001777 target_thread = binder_get_txn_from_and_acq_inner(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001778 if (target_thread) {
Todd Kjos858b8da2017-04-21 17:35:12 -07001779 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1780 "send failed reply for transaction %d to %d:%d\n",
1781 t->debug_id,
1782 target_thread->proc->pid,
1783 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001784
Martijn Coenen995a36e2017-06-02 13:36:52 -07001785 binder_pop_transaction_ilocked(target_thread, t);
Todd Kjos858b8da2017-04-21 17:35:12 -07001786 if (target_thread->reply_error.cmd == BR_OK) {
1787 target_thread->reply_error.cmd = error_code;
Martijn Coenen995a36e2017-06-02 13:36:52 -07001788 binder_enqueue_work_ilocked(
Todd Kjos1c89e6b2016-10-20 10:33:00 -07001789 &target_thread->reply_error.work,
Todd Kjos858b8da2017-04-21 17:35:12 -07001790 &target_thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001791 wake_up_interruptible(&target_thread->wait);
1792 } else {
Todd Kjos858b8da2017-04-21 17:35:12 -07001793 WARN(1, "Unexpected reply error: %u\n",
1794 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001795 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07001796 binder_inner_proc_unlock(target_thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07001797 binder_thread_dec_tmpref(target_thread);
Todd Kjos858b8da2017-04-21 17:35:12 -07001798 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001799 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001800 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001801 next = t->from_parent;
1802
1803 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1804 "send failed reply for transaction %d, target dead\n",
1805 t->debug_id);
1806
Todd Kjos21ef40a2017-03-30 18:02:13 -07001807 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001808 if (next == NULL) {
1809 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1810 "reply failed, no target thread at root\n");
1811 return;
1812 }
1813 t = next;
1814 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1815 "reply failed, no target thread -- retry %d\n",
1816 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001817 }
1818}
1819
Martijn Coenen00c80372016-07-13 12:06:49 +02001820/**
1821 * binder_validate_object() - checks for a valid metadata object in a buffer.
1822 * @buffer: binder_buffer that we're parsing.
1823 * @offset: offset in the buffer at which to validate an object.
1824 *
1825 * Return: If there's a valid metadata object at @offset in @buffer, the
1826 * size of that object. Otherwise, it returns zero.
1827 */
1828static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
1829{
1830 /* Check if we can read a header first */
1831 struct binder_object_header *hdr;
1832 size_t object_size = 0;
1833
1834 if (offset > buffer->data_size - sizeof(*hdr) ||
1835 buffer->data_size < sizeof(*hdr) ||
1836 !IS_ALIGNED(offset, sizeof(u32)))
1837 return 0;
1838
1839 /* Ok, now see if we can read a complete object. */
1840 hdr = (struct binder_object_header *)(buffer->data + offset);
1841 switch (hdr->type) {
1842 case BINDER_TYPE_BINDER:
1843 case BINDER_TYPE_WEAK_BINDER:
1844 case BINDER_TYPE_HANDLE:
1845 case BINDER_TYPE_WEAK_HANDLE:
1846 object_size = sizeof(struct flat_binder_object);
1847 break;
1848 case BINDER_TYPE_FD:
1849 object_size = sizeof(struct binder_fd_object);
1850 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001851 case BINDER_TYPE_PTR:
1852 object_size = sizeof(struct binder_buffer_object);
1853 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001854 case BINDER_TYPE_FDA:
1855 object_size = sizeof(struct binder_fd_array_object);
1856 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02001857 default:
1858 return 0;
1859 }
1860 if (offset <= buffer->data_size - object_size &&
1861 buffer->data_size >= object_size)
1862 return object_size;
1863 else
1864 return 0;
1865}
1866
Martijn Coenen5a6da532016-09-30 14:10:07 +02001867/**
1868 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
1869 * @b: binder_buffer containing the object
1870 * @index: index in offset array at which the binder_buffer_object is
1871 * located
1872 * @start: points to the start of the offset array
1873 * @num_valid: the number of valid offsets in the offset array
1874 *
1875 * Return: If @index is within the valid range of the offset array
1876 * described by @start and @num_valid, and if there's a valid
1877 * binder_buffer_object at the offset found in index @index
1878 * of the offset array, that object is returned. Otherwise,
1879 * %NULL is returned.
1880 * Note that the offset found in index @index itself is not
1881 * verified; this function assumes that @num_valid elements
1882 * from @start were previously verified to have valid offsets.
1883 */
1884static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
1885 binder_size_t index,
1886 binder_size_t *start,
1887 binder_size_t num_valid)
1888{
1889 struct binder_buffer_object *buffer_obj;
1890 binder_size_t *offp;
1891
1892 if (index >= num_valid)
1893 return NULL;
1894
1895 offp = start + index;
1896 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
1897 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
1898 return NULL;
1899
1900 return buffer_obj;
1901}
1902
1903/**
1904 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
1905 * @b: transaction buffer
1906 * @objects_start start of objects buffer
1907 * @buffer: binder_buffer_object in which to fix up
1908 * @offset: start offset in @buffer to fix up
1909 * @last_obj: last binder_buffer_object that we fixed up in
1910 * @last_min_offset: minimum fixup offset in @last_obj
1911 *
1912 * Return: %true if a fixup in buffer @buffer at offset @offset is
1913 * allowed.
1914 *
1915 * For safety reasons, we only allow fixups inside a buffer to happen
1916 * at increasing offsets; additionally, we only allow fixup on the last
1917 * buffer object that was verified, or one of its parents.
1918 *
1919 * Example of what is allowed:
1920 *
1921 * A
1922 * B (parent = A, offset = 0)
1923 * C (parent = A, offset = 16)
1924 * D (parent = C, offset = 0)
1925 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
1926 *
1927 * Examples of what is not allowed:
1928 *
1929 * Decreasing offsets within the same parent:
1930 * A
1931 * C (parent = A, offset = 16)
1932 * B (parent = A, offset = 0) // decreasing offset within A
1933 *
1934 * Referring to a parent that wasn't the last object or any of its parents:
1935 * A
1936 * B (parent = A, offset = 0)
1937 * C (parent = A, offset = 0)
1938 * C (parent = A, offset = 16)
1939 * D (parent = B, offset = 0) // B is not A or any of A's parents
1940 */
1941static bool binder_validate_fixup(struct binder_buffer *b,
1942 binder_size_t *objects_start,
1943 struct binder_buffer_object *buffer,
1944 binder_size_t fixup_offset,
1945 struct binder_buffer_object *last_obj,
1946 binder_size_t last_min_offset)
1947{
1948 if (!last_obj) {
1949 /* Nothing to fix up in */
1950 return false;
1951 }
1952
1953 while (last_obj != buffer) {
1954 /*
1955 * Safe to retrieve the parent of last_obj, since it
1956 * was already previously verified by the driver.
1957 */
1958 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
1959 return false;
1960 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
1961 last_obj = (struct binder_buffer_object *)
1962 (b->data + *(objects_start + last_obj->parent));
1963 }
1964 return (fixup_offset >= last_min_offset);
1965}
1966
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001967static void binder_transaction_buffer_release(struct binder_proc *proc,
1968 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001969 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001970{
Martijn Coenen5a6da532016-09-30 14:10:07 +02001971 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001972 int debug_id = buffer->debug_id;
1973
1974 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301975 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001976 proc->pid, buffer->debug_id,
1977 buffer->data_size, buffer->offsets_size, failed_at);
1978
1979 if (buffer->target_node)
1980 binder_dec_node(buffer->target_node, 1, 0);
1981
Martijn Coenen5a6da532016-09-30 14:10:07 +02001982 off_start = (binder_size_t *)(buffer->data +
1983 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001984 if (failed_at)
1985 off_end = failed_at;
1986 else
Martijn Coenen5a6da532016-09-30 14:10:07 +02001987 off_end = (void *)off_start + buffer->offsets_size;
1988 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02001989 struct binder_object_header *hdr;
1990 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09001991
Martijn Coenen00c80372016-07-13 12:06:49 +02001992 if (object_size == 0) {
1993 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001994 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001995 continue;
1996 }
Martijn Coenen00c80372016-07-13 12:06:49 +02001997 hdr = (struct binder_object_header *)(buffer->data + *offp);
1998 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001999 case BINDER_TYPE_BINDER:
2000 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002001 struct flat_binder_object *fp;
2002 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002003
Martijn Coenen00c80372016-07-13 12:06:49 +02002004 fp = to_flat_binder_object(hdr);
2005 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002006 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002007 pr_err("transaction release %d bad node %016llx\n",
2008 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002009 break;
2010 }
2011 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002012 " node %d u%016llx\n",
2013 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02002014 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
2015 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07002016 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002017 } break;
2018 case BINDER_TYPE_HANDLE:
2019 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002020 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002021 struct binder_ref_data rdata;
2022 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002023
Martijn Coenen00c80372016-07-13 12:06:49 +02002024 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002025 ret = binder_dec_ref_for_handle(proc, fp->handle,
2026 hdr->type == BINDER_TYPE_HANDLE, &rdata);
2027
2028 if (ret) {
2029 pr_err("transaction release %d bad handle %d, ret = %d\n",
2030 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002031 break;
2032 }
2033 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002034 " ref %d desc %d\n",
2035 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002036 } break;
2037
Martijn Coenen00c80372016-07-13 12:06:49 +02002038 case BINDER_TYPE_FD: {
2039 struct binder_fd_object *fp = to_binder_fd_object(hdr);
2040
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002041 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02002042 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002043 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02002044 task_close_fd(proc, fp->fd);
2045 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002046 case BINDER_TYPE_PTR:
2047 /*
2048 * Nothing to do here, this will get cleaned up when the
2049 * transaction buffer gets freed
2050 */
2051 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002052 case BINDER_TYPE_FDA: {
2053 struct binder_fd_array_object *fda;
2054 struct binder_buffer_object *parent;
2055 uintptr_t parent_buffer;
2056 u32 *fd_array;
2057 size_t fd_index;
2058 binder_size_t fd_buf_size;
2059
2060 fda = to_binder_fd_array_object(hdr);
2061 parent = binder_validate_ptr(buffer, fda->parent,
2062 off_start,
2063 offp - off_start);
2064 if (!parent) {
2065 pr_err("transaction release %d bad parent offset",
2066 debug_id);
2067 continue;
2068 }
2069 /*
2070 * Since the parent was already fixed up, convert it
2071 * back to kernel address space to access it
2072 */
2073 parent_buffer = parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002074 binder_alloc_get_user_buffer_offset(
2075 &proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002076
2077 fd_buf_size = sizeof(u32) * fda->num_fds;
2078 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2079 pr_err("transaction release %d invalid number of fds (%lld)\n",
2080 debug_id, (u64)fda->num_fds);
2081 continue;
2082 }
2083 if (fd_buf_size > parent->length ||
2084 fda->parent_offset > parent->length - fd_buf_size) {
2085 /* No space for all file descriptors here. */
2086 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
2087 debug_id, (u64)fda->num_fds);
2088 continue;
2089 }
2090 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2091 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
2092 task_close_fd(proc, fd_array[fd_index]);
2093 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002094 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002095 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002096 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002097 break;
2098 }
2099 }
2100}
2101
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002102static int binder_translate_binder(struct flat_binder_object *fp,
2103 struct binder_transaction *t,
2104 struct binder_thread *thread)
2105{
2106 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002107 struct binder_proc *proc = thread->proc;
2108 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002109 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002110 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002111
2112 node = binder_get_node(proc, fp->binder);
2113 if (!node) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002114 node = binder_new_node(proc, fp);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002115 if (!node)
2116 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002117 }
2118 if (fp->cookie != node->cookie) {
2119 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
2120 proc->pid, thread->pid, (u64)fp->binder,
2121 node->debug_id, (u64)fp->cookie,
2122 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002123 ret = -EINVAL;
2124 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002125 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002126 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2127 ret = -EPERM;
2128 goto done;
2129 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002130
Todd Kjosb0117bb2017-05-08 09:16:27 -07002131 ret = binder_inc_ref_for_node(target_proc, node,
2132 fp->hdr.type == BINDER_TYPE_BINDER,
2133 &thread->todo, &rdata);
2134 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002135 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002136
2137 if (fp->hdr.type == BINDER_TYPE_BINDER)
2138 fp->hdr.type = BINDER_TYPE_HANDLE;
2139 else
2140 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
2141 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002142 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002143 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002144
Todd Kjosb0117bb2017-05-08 09:16:27 -07002145 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002146 binder_debug(BINDER_DEBUG_TRANSACTION,
2147 " node %d u%016llx -> ref %d desc %d\n",
2148 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002149 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07002150done:
2151 binder_put_node(node);
2152 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002153}
2154
2155static int binder_translate_handle(struct flat_binder_object *fp,
2156 struct binder_transaction *t,
2157 struct binder_thread *thread)
2158{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002159 struct binder_proc *proc = thread->proc;
2160 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002161 struct binder_node *node;
2162 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07002163 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002164
Todd Kjosb0117bb2017-05-08 09:16:27 -07002165 node = binder_get_node_from_ref(proc, fp->handle,
2166 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
2167 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002168 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
2169 proc->pid, thread->pid, fp->handle);
2170 return -EINVAL;
2171 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002172 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
2173 ret = -EPERM;
2174 goto done;
2175 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002176
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002177 binder_node_lock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002178 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002179 if (fp->hdr.type == BINDER_TYPE_HANDLE)
2180 fp->hdr.type = BINDER_TYPE_BINDER;
2181 else
2182 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002183 fp->binder = node->ptr;
2184 fp->cookie = node->cookie;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002185 if (node->proc)
2186 binder_inner_proc_lock(node->proc);
2187 binder_inc_node_nilocked(node,
2188 fp->hdr.type == BINDER_TYPE_BINDER,
2189 0, NULL);
2190 if (node->proc)
2191 binder_inner_proc_unlock(node->proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002192 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002193 binder_debug(BINDER_DEBUG_TRANSACTION,
2194 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002195 src_rdata.debug_id, src_rdata.desc, node->debug_id,
2196 (u64)node->ptr);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002197 binder_node_unlock(node);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002198 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002199 int ret;
2200 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002201
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002202 binder_node_unlock(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002203 ret = binder_inc_ref_for_node(target_proc, node,
2204 fp->hdr.type == BINDER_TYPE_HANDLE,
2205 NULL, &dest_rdata);
2206 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07002207 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002208
2209 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002210 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002211 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002212 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
2213 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002214 binder_debug(BINDER_DEBUG_TRANSACTION,
2215 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002216 src_rdata.debug_id, src_rdata.desc,
2217 dest_rdata.debug_id, dest_rdata.desc,
2218 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002219 }
Todd Kjosf22abc72017-05-09 11:08:05 -07002220done:
2221 binder_put_node(node);
2222 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002223}
2224
2225static int binder_translate_fd(int fd,
2226 struct binder_transaction *t,
2227 struct binder_thread *thread,
2228 struct binder_transaction *in_reply_to)
2229{
2230 struct binder_proc *proc = thread->proc;
2231 struct binder_proc *target_proc = t->to_proc;
2232 int target_fd;
2233 struct file *file;
2234 int ret;
2235 bool target_allows_fd;
2236
2237 if (in_reply_to)
2238 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
2239 else
2240 target_allows_fd = t->buffer->target_node->accept_fds;
2241 if (!target_allows_fd) {
2242 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
2243 proc->pid, thread->pid,
2244 in_reply_to ? "reply" : "transaction",
2245 fd);
2246 ret = -EPERM;
2247 goto err_fd_not_accepted;
2248 }
2249
2250 file = fget(fd);
2251 if (!file) {
2252 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
2253 proc->pid, thread->pid, fd);
2254 ret = -EBADF;
2255 goto err_fget;
2256 }
2257 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
2258 if (ret < 0) {
2259 ret = -EPERM;
2260 goto err_security;
2261 }
2262
2263 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
2264 if (target_fd < 0) {
2265 ret = -ENOMEM;
2266 goto err_get_unused_fd;
2267 }
2268 task_fd_install(target_proc, target_fd, file);
2269 trace_binder_transaction_fd(t, fd, target_fd);
2270 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
2271 fd, target_fd);
2272
2273 return target_fd;
2274
2275err_get_unused_fd:
2276err_security:
2277 fput(file);
2278err_fget:
2279err_fd_not_accepted:
2280 return ret;
2281}
2282
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002283static int binder_translate_fd_array(struct binder_fd_array_object *fda,
2284 struct binder_buffer_object *parent,
2285 struct binder_transaction *t,
2286 struct binder_thread *thread,
2287 struct binder_transaction *in_reply_to)
2288{
2289 binder_size_t fdi, fd_buf_size, num_installed_fds;
2290 int target_fd;
2291 uintptr_t parent_buffer;
2292 u32 *fd_array;
2293 struct binder_proc *proc = thread->proc;
2294 struct binder_proc *target_proc = t->to_proc;
2295
2296 fd_buf_size = sizeof(u32) * fda->num_fds;
2297 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
2298 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
2299 proc->pid, thread->pid, (u64)fda->num_fds);
2300 return -EINVAL;
2301 }
2302 if (fd_buf_size > parent->length ||
2303 fda->parent_offset > parent->length - fd_buf_size) {
2304 /* No space for all file descriptors here. */
2305 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
2306 proc->pid, thread->pid, (u64)fda->num_fds);
2307 return -EINVAL;
2308 }
2309 /*
2310 * Since the parent was already fixed up, convert it
2311 * back to the kernel address space to access it
2312 */
Todd Kjosd325d372016-10-10 10:40:53 -07002313 parent_buffer = parent->buffer -
2314 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002315 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
2316 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
2317 binder_user_error("%d:%d parent offset not aligned correctly.\n",
2318 proc->pid, thread->pid);
2319 return -EINVAL;
2320 }
2321 for (fdi = 0; fdi < fda->num_fds; fdi++) {
2322 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
2323 in_reply_to);
2324 if (target_fd < 0)
2325 goto err_translate_fd_failed;
2326 fd_array[fdi] = target_fd;
2327 }
2328 return 0;
2329
2330err_translate_fd_failed:
2331 /*
2332 * Failed to allocate fd or security error, free fds
2333 * installed so far.
2334 */
2335 num_installed_fds = fdi;
2336 for (fdi = 0; fdi < num_installed_fds; fdi++)
2337 task_close_fd(target_proc, fd_array[fdi]);
2338 return target_fd;
2339}
2340
Martijn Coenen5a6da532016-09-30 14:10:07 +02002341static int binder_fixup_parent(struct binder_transaction *t,
2342 struct binder_thread *thread,
2343 struct binder_buffer_object *bp,
2344 binder_size_t *off_start,
2345 binder_size_t num_valid,
2346 struct binder_buffer_object *last_fixup_obj,
2347 binder_size_t last_fixup_min_off)
2348{
2349 struct binder_buffer_object *parent;
2350 u8 *parent_buffer;
2351 struct binder_buffer *b = t->buffer;
2352 struct binder_proc *proc = thread->proc;
2353 struct binder_proc *target_proc = t->to_proc;
2354
2355 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
2356 return 0;
2357
2358 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
2359 if (!parent) {
2360 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2361 proc->pid, thread->pid);
2362 return -EINVAL;
2363 }
2364
2365 if (!binder_validate_fixup(b, off_start,
2366 parent, bp->parent_offset,
2367 last_fixup_obj,
2368 last_fixup_min_off)) {
2369 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2370 proc->pid, thread->pid);
2371 return -EINVAL;
2372 }
2373
2374 if (parent->length < sizeof(binder_uintptr_t) ||
2375 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
2376 /* No space for a pointer here! */
2377 binder_user_error("%d:%d got transaction with invalid parent offset\n",
2378 proc->pid, thread->pid);
2379 return -EINVAL;
2380 }
2381 parent_buffer = (u8 *)(parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07002382 binder_alloc_get_user_buffer_offset(
2383 &target_proc->alloc));
Martijn Coenen5a6da532016-09-30 14:10:07 +02002384 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
2385
2386 return 0;
2387}
2388
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002389static void binder_transaction(struct binder_proc *proc,
2390 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02002391 struct binder_transaction_data *tr, int reply,
2392 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002393{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002394 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002395 struct binder_transaction *t;
2396 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002397 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002398 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002399 u8 *sg_bufp, *sg_buf_end;
Todd Kjos2f993e22017-05-12 14:42:55 -07002400 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002401 struct binder_thread *target_thread = NULL;
2402 struct binder_node *target_node = NULL;
2403 struct list_head *target_list;
2404 wait_queue_head_t *target_wait;
2405 struct binder_transaction *in_reply_to = NULL;
2406 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07002407 uint32_t return_error = 0;
2408 uint32_t return_error_param = 0;
2409 uint32_t return_error_line = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002410 struct binder_buffer_object *last_fixup_obj = NULL;
2411 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002412 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07002413 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002414
2415 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07002416 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002417 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
2418 e->from_proc = proc->pid;
2419 e->from_thread = thread->pid;
2420 e->target_handle = tr->target.handle;
2421 e->data_size = tr->data_size;
2422 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02002423 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002424
2425 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002426 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002427 in_reply_to = thread->transaction_stack;
2428 if (in_reply_to == NULL) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002429 binder_inner_proc_unlock(proc);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302430 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002431 proc->pid, thread->pid);
2432 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002433 return_error_param = -EPROTO;
2434 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002435 goto err_empty_call_stack;
2436 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002437 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002438 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302439 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 +09002440 proc->pid, thread->pid, in_reply_to->debug_id,
2441 in_reply_to->to_proc ?
2442 in_reply_to->to_proc->pid : 0,
2443 in_reply_to->to_thread ?
2444 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002445 spin_unlock(&in_reply_to->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002446 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002447 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002448 return_error_param = -EPROTO;
2449 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002450 in_reply_to = NULL;
2451 goto err_bad_call_stack;
2452 }
2453 thread->transaction_stack = in_reply_to->to_parent;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002454 binder_inner_proc_unlock(proc);
2455 binder_set_nice(in_reply_to->saved_priority);
2456 target_thread = binder_get_txn_from_and_acq_inner(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002457 if (target_thread == NULL) {
2458 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002459 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002460 goto err_dead_binder;
2461 }
2462 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302463 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 +09002464 proc->pid, thread->pid,
2465 target_thread->transaction_stack ?
2466 target_thread->transaction_stack->debug_id : 0,
2467 in_reply_to->debug_id);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002468 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002469 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002470 return_error_param = -EPROTO;
2471 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002472 in_reply_to = NULL;
2473 target_thread = NULL;
2474 goto err_dead_binder;
2475 }
2476 target_proc = target_thread->proc;
Todd Kjos2f993e22017-05-12 14:42:55 -07002477 target_proc->tmp_ref++;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002478 binder_inner_proc_unlock(target_thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002479 } else {
2480 if (tr->target.handle) {
2481 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09002482
Todd Kjosc37162d2017-05-26 11:56:29 -07002483 /*
2484 * There must already be a strong ref
2485 * on this node. If so, do a strong
2486 * increment on the node to ensure it
2487 * stays alive until the transaction is
2488 * done.
2489 */
Todd Kjos5346bf32016-10-20 16:43:34 -07002490 binder_proc_lock(proc);
2491 ref = binder_get_ref_olocked(proc, tr->target.handle,
2492 true);
Todd Kjosc37162d2017-05-26 11:56:29 -07002493 if (ref) {
2494 binder_inc_node(ref->node, 1, 0, NULL);
2495 target_node = ref->node;
2496 }
Todd Kjos5346bf32016-10-20 16:43:34 -07002497 binder_proc_unlock(proc);
Todd Kjosc37162d2017-05-26 11:56:29 -07002498 if (target_node == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302499 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002500 proc->pid, thread->pid);
2501 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002502 return_error_param = -EINVAL;
2503 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002504 goto err_invalid_target_handle;
2505 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002506 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002507 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002508 target_node = context->binder_context_mgr_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002509 if (target_node == NULL) {
2510 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002511 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjose598d172017-03-22 17:19:52 -07002512 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002513 goto err_no_context_mgr_node;
2514 }
Todd Kjosc37162d2017-05-26 11:56:29 -07002515 binder_inc_node(target_node, 1, 0, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002516 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002517 }
2518 e->to_node = target_node->debug_id;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002519 binder_node_lock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002520 target_proc = target_node->proc;
2521 if (target_proc == NULL) {
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002522 binder_node_unlock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002523 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002524 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002525 goto err_dead_binder;
2526 }
Todd Kjosb4827902017-05-25 15:52:17 -07002527 binder_inner_proc_lock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002528 target_proc->tmp_ref++;
Todd Kjosb4827902017-05-25 15:52:17 -07002529 binder_inner_proc_unlock(target_proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002530 binder_node_unlock(target_node);
Stephen Smalley79af7302015-01-21 10:54:10 -05002531 if (security_binder_transaction(proc->tsk,
2532 target_proc->tsk) < 0) {
2533 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002534 return_error_param = -EPERM;
2535 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05002536 goto err_invalid_target_handle;
2537 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002538 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002539 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
2540 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002541
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002542 tmp = thread->transaction_stack;
2543 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002544 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05302545 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 +09002546 proc->pid, thread->pid, tmp->debug_id,
2547 tmp->to_proc ? tmp->to_proc->pid : 0,
2548 tmp->to_thread ?
2549 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07002550 spin_unlock(&tmp->lock);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002551 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002552 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002553 return_error_param = -EPROTO;
2554 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002555 goto err_bad_call_stack;
2556 }
2557 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002558 struct binder_thread *from;
2559
2560 spin_lock(&tmp->lock);
2561 from = tmp->from;
2562 if (from && from->proc == target_proc) {
2563 atomic_inc(&from->tmp_ref);
2564 target_thread = from;
2565 spin_unlock(&tmp->lock);
2566 break;
2567 }
2568 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002569 tmp = tmp->from_parent;
2570 }
2571 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002572 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002573 }
2574 if (target_thread) {
2575 e->to_thread = target_thread->pid;
2576 target_list = &target_thread->todo;
2577 target_wait = &target_thread->wait;
2578 } else {
2579 target_list = &target_proc->todo;
2580 target_wait = &target_proc->wait;
2581 }
2582 e->to_proc = target_proc->pid;
2583
2584 /* TODO: reuse incoming transaction for reply */
2585 t = kzalloc(sizeof(*t), GFP_KERNEL);
2586 if (t == NULL) {
2587 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002588 return_error_param = -ENOMEM;
2589 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002590 goto err_alloc_t_failed;
2591 }
2592 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07002593 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002594
2595 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
2596 if (tcomplete == NULL) {
2597 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002598 return_error_param = -ENOMEM;
2599 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002600 goto err_alloc_tcomplete_failed;
2601 }
2602 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2603
Todd Kjos1cfe6272017-05-24 13:33:28 -07002604 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002605
2606 if (reply)
2607 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02002608 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002609 proc->pid, thread->pid, t->debug_id,
2610 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002611 (u64)tr->data.ptr.buffer,
2612 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02002613 (u64)tr->data_size, (u64)tr->offsets_size,
2614 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002615 else
2616 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02002617 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002618 proc->pid, thread->pid, t->debug_id,
2619 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002620 (u64)tr->data.ptr.buffer,
2621 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02002622 (u64)tr->data_size, (u64)tr->offsets_size,
2623 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002624
2625 if (!reply && !(tr->flags & TF_ONE_WAY))
2626 t->from = thread;
2627 else
2628 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03002629 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002630 t->to_proc = target_proc;
2631 t->to_thread = target_thread;
2632 t->code = tr->code;
2633 t->flags = tr->flags;
2634 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002635
2636 trace_binder_transaction(reply, t, target_node);
2637
Todd Kjosd325d372016-10-10 10:40:53 -07002638 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02002639 tr->offsets_size, extra_buffers_size,
2640 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07002641 if (IS_ERR(t->buffer)) {
2642 /*
2643 * -ESRCH indicates VMA cleared. The target is dying.
2644 */
2645 return_error_param = PTR_ERR(t->buffer);
2646 return_error = return_error_param == -ESRCH ?
2647 BR_DEAD_REPLY : BR_FAILED_REPLY;
2648 return_error_line = __LINE__;
2649 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002650 goto err_binder_alloc_buf_failed;
2651 }
2652 t->buffer->allow_user_free = 0;
2653 t->buffer->debug_id = t->debug_id;
2654 t->buffer->transaction = t;
2655 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002656 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002657 off_start = (binder_size_t *)(t->buffer->data +
2658 ALIGN(tr->data_size, sizeof(void *)));
2659 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002660
Arve Hjønnevågda498892014-02-21 14:40:26 -08002661 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
2662 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302663 binder_user_error("%d:%d got transaction with invalid data ptr\n",
2664 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002665 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002666 return_error_param = -EFAULT;
2667 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002668 goto err_copy_data_failed;
2669 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002670 if (copy_from_user(offp, (const void __user *)(uintptr_t)
2671 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302672 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2673 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002674 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002675 return_error_param = -EFAULT;
2676 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002677 goto err_copy_data_failed;
2678 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002679 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
2680 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
2681 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002682 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002683 return_error_param = -EINVAL;
2684 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002685 goto err_bad_offset;
2686 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02002687 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
2688 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
2689 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05302690 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002691 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002692 return_error_param = -EINVAL;
2693 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002694 goto err_bad_offset;
2695 }
2696 off_end = (void *)off_start + tr->offsets_size;
2697 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
2698 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002699 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002700 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002701 struct binder_object_header *hdr;
2702 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002703
Martijn Coenen00c80372016-07-13 12:06:49 +02002704 if (object_size == 0 || *offp < off_min) {
2705 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 -08002706 proc->pid, thread->pid, (u64)*offp,
2707 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02002708 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002709 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002710 return_error_param = -EINVAL;
2711 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002712 goto err_bad_offset;
2713 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002714
2715 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
2716 off_min = *offp + object_size;
2717 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002718 case BINDER_TYPE_BINDER:
2719 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002720 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002721
Martijn Coenen00c80372016-07-13 12:06:49 +02002722 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002723 ret = binder_translate_binder(fp, t, thread);
2724 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02002725 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002726 return_error_param = ret;
2727 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002728 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002729 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002730 } break;
2731 case BINDER_TYPE_HANDLE:
2732 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002733 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002734
Martijn Coenen00c80372016-07-13 12:06:49 +02002735 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002736 ret = binder_translate_handle(fp, t, thread);
2737 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002738 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002739 return_error_param = ret;
2740 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002741 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002742 }
2743 } break;
2744
2745 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002746 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002747 int target_fd = binder_translate_fd(fp->fd, t, thread,
2748 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002749
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002750 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002751 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002752 return_error_param = target_fd;
2753 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002754 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002755 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002756 fp->pad_binder = 0;
2757 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002758 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002759 case BINDER_TYPE_FDA: {
2760 struct binder_fd_array_object *fda =
2761 to_binder_fd_array_object(hdr);
2762 struct binder_buffer_object *parent =
2763 binder_validate_ptr(t->buffer, fda->parent,
2764 off_start,
2765 offp - off_start);
2766 if (!parent) {
2767 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2768 proc->pid, thread->pid);
2769 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002770 return_error_param = -EINVAL;
2771 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002772 goto err_bad_parent;
2773 }
2774 if (!binder_validate_fixup(t->buffer, off_start,
2775 parent, fda->parent_offset,
2776 last_fixup_obj,
2777 last_fixup_min_off)) {
2778 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2779 proc->pid, thread->pid);
2780 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002781 return_error_param = -EINVAL;
2782 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002783 goto err_bad_parent;
2784 }
2785 ret = binder_translate_fd_array(fda, parent, t, thread,
2786 in_reply_to);
2787 if (ret < 0) {
2788 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002789 return_error_param = ret;
2790 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002791 goto err_translate_failed;
2792 }
2793 last_fixup_obj = parent;
2794 last_fixup_min_off =
2795 fda->parent_offset + sizeof(u32) * fda->num_fds;
2796 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002797 case BINDER_TYPE_PTR: {
2798 struct binder_buffer_object *bp =
2799 to_binder_buffer_object(hdr);
2800 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002801
Martijn Coenen5a6da532016-09-30 14:10:07 +02002802 if (bp->length > buf_left) {
2803 binder_user_error("%d:%d got transaction with too large buffer\n",
2804 proc->pid, thread->pid);
2805 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002806 return_error_param = -EINVAL;
2807 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002808 goto err_bad_offset;
2809 }
2810 if (copy_from_user(sg_bufp,
2811 (const void __user *)(uintptr_t)
2812 bp->buffer, bp->length)) {
2813 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2814 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07002815 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002816 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002817 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002818 goto err_copy_data_failed;
2819 }
2820 /* Fixup buffer pointer to target proc address space */
2821 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjosd325d372016-10-10 10:40:53 -07002822 binder_alloc_get_user_buffer_offset(
2823 &target_proc->alloc);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002824 sg_bufp += ALIGN(bp->length, sizeof(u64));
2825
2826 ret = binder_fixup_parent(t, thread, bp, off_start,
2827 offp - off_start,
2828 last_fixup_obj,
2829 last_fixup_min_off);
2830 if (ret < 0) {
2831 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002832 return_error_param = ret;
2833 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002834 goto err_translate_failed;
2835 }
2836 last_fixup_obj = bp;
2837 last_fixup_min_off = 0;
2838 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002839 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002840 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002841 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002842 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002843 return_error_param = -EINVAL;
2844 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002845 goto err_bad_object_type;
2846 }
2847 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07002848 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07002849 binder_enqueue_work(proc, tcomplete, &thread->todo);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002850 t->work.type = BINDER_WORK_TRANSACTION;
Todd Kjos8dedb0c2017-05-09 08:31:32 -07002851
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002852 if (reply) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002853 binder_inner_proc_lock(target_proc);
2854 if (target_thread->is_dead) {
2855 binder_inner_proc_unlock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002856 goto err_dead_proc_or_thread;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002857 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002858 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002859 binder_pop_transaction_ilocked(target_thread, in_reply_to);
2860 binder_enqueue_work_ilocked(&t->work, target_list);
2861 binder_inner_proc_unlock(target_proc);
Todd Kjos21ef40a2017-03-30 18:02:13 -07002862 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002863 } else if (!(t->flags & TF_ONE_WAY)) {
2864 BUG_ON(t->buffer->async_transaction != 0);
Martijn Coenen995a36e2017-06-02 13:36:52 -07002865 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002866 t->need_reply = 1;
2867 t->from_parent = thread->transaction_stack;
2868 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07002869 binder_inner_proc_unlock(proc);
2870 binder_inner_proc_lock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002871 if (target_proc->is_dead ||
2872 (target_thread && target_thread->is_dead)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002873 binder_inner_proc_unlock(target_proc);
2874 binder_inner_proc_lock(proc);
2875 binder_pop_transaction_ilocked(thread, t);
2876 binder_inner_proc_unlock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002877 goto err_dead_proc_or_thread;
2878 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002879 binder_enqueue_work_ilocked(&t->work, target_list);
2880 binder_inner_proc_unlock(target_proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002881 } else {
2882 BUG_ON(target_node == NULL);
2883 BUG_ON(t->buffer->async_transaction != 1);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002884 binder_node_lock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002885 if (target_node->has_async_transaction) {
2886 target_list = &target_node->async_todo;
2887 target_wait = NULL;
2888 } else
2889 target_node->has_async_transaction = 1;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002890 /*
2891 * Test/set of has_async_transaction
2892 * must be atomic with enqueue on
2893 * async_todo
2894 */
Martijn Coenen995a36e2017-06-02 13:36:52 -07002895 binder_inner_proc_lock(target_proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07002896 if (target_proc->is_dead ||
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002897 (target_thread && target_thread->is_dead)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07002898 binder_inner_proc_unlock(target_proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002899 binder_node_unlock(target_node);
Todd Kjos2f993e22017-05-12 14:42:55 -07002900 goto err_dead_proc_or_thread;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002901 }
Martijn Coenen995a36e2017-06-02 13:36:52 -07002902 binder_enqueue_work_ilocked(&t->work, target_list);
2903 binder_inner_proc_unlock(target_proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07002904 binder_node_unlock(target_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002905 }
Riley Andrewsb5968812015-09-01 12:42:07 -07002906 if (target_wait) {
Todd Kjos8dedb0c2017-05-09 08:31:32 -07002907 if (reply || !(tr->flags & TF_ONE_WAY))
Riley Andrewsb5968812015-09-01 12:42:07 -07002908 wake_up_interruptible_sync(target_wait);
2909 else
2910 wake_up_interruptible(target_wait);
2911 }
Todd Kjos2f993e22017-05-12 14:42:55 -07002912 if (target_thread)
2913 binder_thread_dec_tmpref(target_thread);
2914 binder_proc_dec_tmpref(target_proc);
Todd Kjos1cfe6272017-05-24 13:33:28 -07002915 /*
2916 * write barrier to synchronize with initialization
2917 * of log entry
2918 */
2919 smp_wmb();
2920 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002921 return;
2922
Todd Kjos2f993e22017-05-12 14:42:55 -07002923err_dead_proc_or_thread:
2924 return_error = BR_DEAD_REPLY;
2925 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002926err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002927err_bad_object_type:
2928err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002929err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002930err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002931 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002932 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjosc37162d2017-05-26 11:56:29 -07002933 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002934 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07002935 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002936err_binder_alloc_buf_failed:
2937 kfree(tcomplete);
2938 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2939err_alloc_tcomplete_failed:
2940 kfree(t);
2941 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2942err_alloc_t_failed:
2943err_bad_call_stack:
2944err_empty_call_stack:
2945err_dead_binder:
2946err_invalid_target_handle:
2947err_no_context_mgr_node:
Todd Kjos2f993e22017-05-12 14:42:55 -07002948 if (target_thread)
2949 binder_thread_dec_tmpref(target_thread);
2950 if (target_proc)
2951 binder_proc_dec_tmpref(target_proc);
Todd Kjosc37162d2017-05-26 11:56:29 -07002952 if (target_node)
2953 binder_dec_node(target_node, 1, 0);
2954
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002955 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07002956 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
2957 proc->pid, thread->pid, return_error, return_error_param,
2958 (u64)tr->data_size, (u64)tr->offsets_size,
2959 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002960
2961 {
2962 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09002963
Todd Kjose598d172017-03-22 17:19:52 -07002964 e->return_error = return_error;
2965 e->return_error_param = return_error_param;
2966 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002967 fe = binder_transaction_log_add(&binder_transaction_log_failed);
2968 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07002969 /*
2970 * write barrier to synchronize with initialization
2971 * of log entry
2972 */
2973 smp_wmb();
2974 WRITE_ONCE(e->debug_id_done, t_debug_id);
2975 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002976 }
2977
Todd Kjos858b8da2017-04-21 17:35:12 -07002978 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002979 if (in_reply_to) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002980 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07002981 binder_enqueue_work(thread->proc,
2982 &thread->return_error.work,
2983 &thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002984 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07002985 } else {
2986 thread->return_error.cmd = return_error;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07002987 binder_enqueue_work(thread->proc,
2988 &thread->return_error.work,
2989 &thread->todo);
Todd Kjos858b8da2017-04-21 17:35:12 -07002990 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002991}
2992
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002993static int binder_thread_write(struct binder_proc *proc,
2994 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002995 binder_uintptr_t binder_buffer, size_t size,
2996 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002997{
2998 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002999 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003000 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003001 void __user *ptr = buffer + *consumed;
3002 void __user *end = buffer + size;
3003
Todd Kjos858b8da2017-04-21 17:35:12 -07003004 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07003005 int ret;
3006
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003007 if (get_user(cmd, (uint32_t __user *)ptr))
3008 return -EFAULT;
3009 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003010 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003011 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003012 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
3013 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
3014 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003015 }
3016 switch (cmd) {
3017 case BC_INCREFS:
3018 case BC_ACQUIRE:
3019 case BC_RELEASE:
3020 case BC_DECREFS: {
3021 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003022 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003023 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
3024 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
3025 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003026
3027 if (get_user(target, (uint32_t __user *)ptr))
3028 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003029
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003030 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07003031 ret = -1;
3032 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003033 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003034 mutex_lock(&context->context_mgr_node_lock);
3035 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003036 if (ctx_mgr_node)
3037 ret = binder_inc_ref_for_node(
3038 proc, ctx_mgr_node,
3039 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003040 mutex_unlock(&context->context_mgr_node_lock);
3041 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07003042 if (ret)
3043 ret = binder_update_ref_for_handle(
3044 proc, target, increment, strong,
3045 &rdata);
3046 if (!ret && rdata.desc != target) {
3047 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
3048 proc->pid, thread->pid,
3049 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003050 }
3051 switch (cmd) {
3052 case BC_INCREFS:
3053 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003054 break;
3055 case BC_ACQUIRE:
3056 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003057 break;
3058 case BC_RELEASE:
3059 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003060 break;
3061 case BC_DECREFS:
3062 default:
3063 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07003064 break;
3065 }
3066 if (ret) {
3067 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
3068 proc->pid, thread->pid, debug_string,
3069 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003070 break;
3071 }
3072 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07003073 "%d:%d %s ref %d desc %d s %d w %d\n",
3074 proc->pid, thread->pid, debug_string,
3075 rdata.debug_id, rdata.desc, rdata.strong,
3076 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003077 break;
3078 }
3079 case BC_INCREFS_DONE:
3080 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003081 binder_uintptr_t node_ptr;
3082 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003083 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003084 bool free_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003085
Arve Hjønnevågda498892014-02-21 14:40:26 -08003086 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003087 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003088 ptr += sizeof(binder_uintptr_t);
3089 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003090 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003091 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003092 node = binder_get_node(proc, node_ptr);
3093 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003094 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003095 proc->pid, thread->pid,
3096 cmd == BC_INCREFS_DONE ?
3097 "BC_INCREFS_DONE" :
3098 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003099 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003100 break;
3101 }
3102 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003103 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003104 proc->pid, thread->pid,
3105 cmd == BC_INCREFS_DONE ?
3106 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003107 (u64)node_ptr, node->debug_id,
3108 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07003109 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003110 break;
3111 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003112 binder_node_inner_lock(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003113 if (cmd == BC_ACQUIRE_DONE) {
3114 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303115 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003116 proc->pid, thread->pid,
3117 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003118 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003119 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003120 break;
3121 }
3122 node->pending_strong_ref = 0;
3123 } else {
3124 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303125 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003126 proc->pid, thread->pid,
3127 node->debug_id);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003128 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003129 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003130 break;
3131 }
3132 node->pending_weak_ref = 0;
3133 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003134 free_node = binder_dec_node_nilocked(node,
3135 cmd == BC_ACQUIRE_DONE, 0);
3136 WARN_ON(free_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003137 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07003138 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003139 proc->pid, thread->pid,
3140 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07003141 node->debug_id, node->local_strong_refs,
3142 node->local_weak_refs, node->tmp_refs);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003143 binder_node_inner_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07003144 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003145 break;
3146 }
3147 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303148 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003149 return -EINVAL;
3150 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303151 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003152 return -EINVAL;
3153
3154 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003155 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003156 struct binder_buffer *buffer;
3157
Arve Hjønnevågda498892014-02-21 14:40:26 -08003158 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003159 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003160 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003161
Todd Kjos076072a2017-04-21 14:32:11 -07003162 buffer = binder_alloc_prepare_to_free(&proc->alloc,
3163 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003164 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003165 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
3166 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003167 break;
3168 }
3169 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003170 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
3171 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003172 break;
3173 }
3174 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003175 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
3176 proc->pid, thread->pid, (u64)data_ptr,
3177 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003178 buffer->transaction ? "active" : "finished");
3179
3180 if (buffer->transaction) {
3181 buffer->transaction->buffer = NULL;
3182 buffer->transaction = NULL;
3183 }
3184 if (buffer->async_transaction && buffer->target_node) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003185 struct binder_node *buf_node;
3186 struct binder_work *w;
3187
3188 buf_node = buffer->target_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003189 binder_node_inner_lock(buf_node);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003190 BUG_ON(!buf_node->has_async_transaction);
3191 BUG_ON(buf_node->proc != proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003192 w = binder_dequeue_work_head_ilocked(
3193 &buf_node->async_todo);
3194 if (!w)
3195 buf_node->has_async_transaction = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003196 else
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003197 binder_enqueue_work_ilocked(
3198 w, &thread->todo);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003199 binder_node_inner_unlock(buf_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003200 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003201 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003202 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07003203 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003204 break;
3205 }
3206
Martijn Coenen5a6da532016-09-30 14:10:07 +02003207 case BC_TRANSACTION_SG:
3208 case BC_REPLY_SG: {
3209 struct binder_transaction_data_sg tr;
3210
3211 if (copy_from_user(&tr, ptr, sizeof(tr)))
3212 return -EFAULT;
3213 ptr += sizeof(tr);
3214 binder_transaction(proc, thread, &tr.transaction_data,
3215 cmd == BC_REPLY_SG, tr.buffers_size);
3216 break;
3217 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003218 case BC_TRANSACTION:
3219 case BC_REPLY: {
3220 struct binder_transaction_data tr;
3221
3222 if (copy_from_user(&tr, ptr, sizeof(tr)))
3223 return -EFAULT;
3224 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02003225 binder_transaction(proc, thread, &tr,
3226 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003227 break;
3228 }
3229
3230 case BC_REGISTER_LOOPER:
3231 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303232 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003233 proc->pid, thread->pid);
Todd Kjosd600e902017-05-25 17:35:02 -07003234 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003235 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
3236 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303237 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003238 proc->pid, thread->pid);
3239 } else if (proc->requested_threads == 0) {
3240 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303241 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003242 proc->pid, thread->pid);
3243 } else {
3244 proc->requested_threads--;
3245 proc->requested_threads_started++;
3246 }
3247 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
Todd Kjosd600e902017-05-25 17:35:02 -07003248 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003249 break;
3250 case BC_ENTER_LOOPER:
3251 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303252 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003253 proc->pid, thread->pid);
3254 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
3255 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303256 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003257 proc->pid, thread->pid);
3258 }
3259 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
3260 break;
3261 case BC_EXIT_LOOPER:
3262 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303263 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003264 proc->pid, thread->pid);
3265 thread->looper |= BINDER_LOOPER_STATE_EXITED;
3266 break;
3267
3268 case BC_REQUEST_DEATH_NOTIFICATION:
3269 case BC_CLEAR_DEATH_NOTIFICATION: {
3270 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003271 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003272 struct binder_ref *ref;
Todd Kjos5346bf32016-10-20 16:43:34 -07003273 struct binder_ref_death *death = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003274
3275 if (get_user(target, (uint32_t __user *)ptr))
3276 return -EFAULT;
3277 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003278 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003279 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003280 ptr += sizeof(binder_uintptr_t);
Todd Kjos5346bf32016-10-20 16:43:34 -07003281 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3282 /*
3283 * Allocate memory for death notification
3284 * before taking lock
3285 */
3286 death = kzalloc(sizeof(*death), GFP_KERNEL);
3287 if (death == NULL) {
3288 WARN_ON(thread->return_error.cmd !=
3289 BR_OK);
3290 thread->return_error.cmd = BR_ERROR;
3291 binder_enqueue_work(
3292 thread->proc,
3293 &thread->return_error.work,
3294 &thread->todo);
3295 binder_debug(
3296 BINDER_DEBUG_FAILED_TRANSACTION,
3297 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
3298 proc->pid, thread->pid);
3299 break;
3300 }
3301 }
3302 binder_proc_lock(proc);
3303 ref = binder_get_ref_olocked(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003304 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303305 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003306 proc->pid, thread->pid,
3307 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3308 "BC_REQUEST_DEATH_NOTIFICATION" :
3309 "BC_CLEAR_DEATH_NOTIFICATION",
3310 target);
Todd Kjos5346bf32016-10-20 16:43:34 -07003311 binder_proc_unlock(proc);
3312 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003313 break;
3314 }
3315
3316 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003317 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003318 proc->pid, thread->pid,
3319 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
3320 "BC_REQUEST_DEATH_NOTIFICATION" :
3321 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07003322 (u64)cookie, ref->data.debug_id,
3323 ref->data.desc, ref->data.strong,
3324 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003325
Martijn Coenenf9eac642017-05-22 11:26:23 -07003326 binder_node_lock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003327 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
3328 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303329 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003330 proc->pid, thread->pid);
Martijn Coenenf9eac642017-05-22 11:26:23 -07003331 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003332 binder_proc_unlock(proc);
3333 kfree(death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003334 break;
3335 }
3336 binder_stats_created(BINDER_STAT_DEATH);
3337 INIT_LIST_HEAD(&death->work.entry);
3338 death->cookie = cookie;
3339 ref->death = death;
3340 if (ref->node->proc == NULL) {
3341 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003342 if (thread->looper &
3343 (BINDER_LOOPER_STATE_REGISTERED |
3344 BINDER_LOOPER_STATE_ENTERED))
3345 binder_enqueue_work(
3346 proc,
3347 &ref->death->work,
3348 &thread->todo);
3349 else {
3350 binder_enqueue_work(
3351 proc,
3352 &ref->death->work,
3353 &proc->todo);
3354 wake_up_interruptible(
3355 &proc->wait);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003356 }
3357 }
3358 } else {
3359 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303360 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003361 proc->pid, thread->pid);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003362 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003363 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003364 break;
3365 }
3366 death = ref->death;
3367 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003368 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003369 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003370 (u64)death->cookie,
3371 (u64)cookie);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003372 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003373 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003374 break;
3375 }
3376 ref->death = NULL;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003377 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003378 if (list_empty(&death->work.entry)) {
3379 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003380 if (thread->looper &
3381 (BINDER_LOOPER_STATE_REGISTERED |
3382 BINDER_LOOPER_STATE_ENTERED))
3383 binder_enqueue_work_ilocked(
3384 &death->work,
3385 &thread->todo);
3386 else {
3387 binder_enqueue_work_ilocked(
3388 &death->work,
3389 &proc->todo);
3390 wake_up_interruptible(
3391 &proc->wait);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003392 }
3393 } else {
3394 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
3395 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
3396 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003397 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003398 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07003399 binder_node_unlock(ref->node);
Todd Kjos5346bf32016-10-20 16:43:34 -07003400 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401 } break;
3402 case BC_DEAD_BINDER_DONE: {
3403 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08003404 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003405 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09003406
Arve Hjønnevågda498892014-02-21 14:40:26 -08003407 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003408 return -EFAULT;
3409
Lisa Du7a64cd82016-02-17 09:32:52 +08003410 ptr += sizeof(cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003411 binder_inner_proc_lock(proc);
3412 list_for_each_entry(w, &proc->delivered_death,
3413 entry) {
3414 struct binder_ref_death *tmp_death =
3415 container_of(w,
3416 struct binder_ref_death,
3417 work);
Seunghun Lee10f62862014-05-01 01:30:23 +09003418
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003419 if (tmp_death->cookie == cookie) {
3420 death = tmp_death;
3421 break;
3422 }
3423 }
3424 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003425 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
3426 proc->pid, thread->pid, (u64)cookie,
3427 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003428 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003429 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
3430 proc->pid, thread->pid, (u64)cookie);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003431 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003432 break;
3433 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003434 binder_dequeue_work_ilocked(&death->work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003435 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
3436 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003437 if (thread->looper &
3438 (BINDER_LOOPER_STATE_REGISTERED |
3439 BINDER_LOOPER_STATE_ENTERED))
3440 binder_enqueue_work_ilocked(
3441 &death->work, &thread->todo);
3442 else {
3443 binder_enqueue_work_ilocked(
3444 &death->work,
3445 &proc->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003446 wake_up_interruptible(&proc->wait);
3447 }
3448 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003449 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003450 } break;
3451
3452 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303453 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003454 proc->pid, thread->pid, cmd);
3455 return -EINVAL;
3456 }
3457 *consumed = ptr - buffer;
3458 }
3459 return 0;
3460}
3461
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02003462static void binder_stat_br(struct binder_proc *proc,
3463 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003464{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003465 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003466 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003467 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
3468 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
3469 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003470 }
3471}
3472
3473static int binder_has_proc_work(struct binder_proc *proc,
3474 struct binder_thread *thread)
3475{
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003476 return !binder_worklist_empty(proc, &proc->todo) ||
3477 thread->looper_need_return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003478}
3479
3480static int binder_has_thread_work(struct binder_thread *thread)
3481{
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003482 return !binder_worklist_empty(thread->proc, &thread->todo) ||
3483 thread->looper_need_return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003484}
3485
Todd Kjos60792612017-05-24 10:51:01 -07003486static int binder_put_node_cmd(struct binder_proc *proc,
3487 struct binder_thread *thread,
3488 void __user **ptrp,
3489 binder_uintptr_t node_ptr,
3490 binder_uintptr_t node_cookie,
3491 int node_debug_id,
3492 uint32_t cmd, const char *cmd_name)
3493{
3494 void __user *ptr = *ptrp;
3495
3496 if (put_user(cmd, (uint32_t __user *)ptr))
3497 return -EFAULT;
3498 ptr += sizeof(uint32_t);
3499
3500 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
3501 return -EFAULT;
3502 ptr += sizeof(binder_uintptr_t);
3503
3504 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
3505 return -EFAULT;
3506 ptr += sizeof(binder_uintptr_t);
3507
3508 binder_stat_br(proc, thread, cmd);
3509 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
3510 proc->pid, thread->pid, cmd_name, node_debug_id,
3511 (u64)node_ptr, (u64)node_cookie);
3512
3513 *ptrp = ptr;
3514 return 0;
3515}
3516
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003517static int binder_thread_read(struct binder_proc *proc,
3518 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003519 binder_uintptr_t binder_buffer, size_t size,
3520 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003521{
Arve Hjønnevågda498892014-02-21 14:40:26 -08003522 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003523 void __user *ptr = buffer + *consumed;
3524 void __user *end = buffer + size;
3525
3526 int ret = 0;
3527 int wait_for_proc_work;
3528
3529 if (*consumed == 0) {
3530 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
3531 return -EFAULT;
3532 ptr += sizeof(uint32_t);
3533 }
3534
3535retry:
Martijn Coenen995a36e2017-06-02 13:36:52 -07003536 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003537 wait_for_proc_work = thread->transaction_stack == NULL &&
Martijn Coenen995a36e2017-06-02 13:36:52 -07003538 binder_worklist_empty_ilocked(&thread->todo);
Todd Kjosd600e902017-05-25 17:35:02 -07003539 if (wait_for_proc_work)
3540 proc->ready_threads++;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003541 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003542
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003543 thread->looper |= BINDER_LOOPER_STATE_WAITING;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003544
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003545 trace_binder_wait_for_work(wait_for_proc_work,
3546 !!thread->transaction_stack,
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003547 !binder_worklist_empty(proc, &thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003548 if (wait_for_proc_work) {
3549 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3550 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05303551 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 +09003552 proc->pid, thread->pid, thread->looper);
3553 wait_event_interruptible(binder_user_error_wait,
3554 binder_stop_on_user_error < 2);
3555 }
3556 binder_set_nice(proc->default_priority);
3557 if (non_block) {
3558 if (!binder_has_proc_work(proc, thread))
3559 ret = -EAGAIN;
3560 } else
Colin Crosse2610b22013-05-06 23:50:15 +00003561 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003562 } else {
3563 if (non_block) {
3564 if (!binder_has_thread_work(thread))
3565 ret = -EAGAIN;
3566 } else
Colin Crosse2610b22013-05-06 23:50:15 +00003567 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003568 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003569
Todd Kjosd600e902017-05-25 17:35:02 -07003570 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003571 if (wait_for_proc_work)
3572 proc->ready_threads--;
Todd Kjosd600e902017-05-25 17:35:02 -07003573 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003574 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
3575
3576 if (ret)
3577 return ret;
3578
3579 while (1) {
3580 uint32_t cmd;
3581 struct binder_transaction_data tr;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003582 struct binder_work *w = NULL;
3583 struct list_head *list = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003584 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07003585 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003586
Todd Kjose7f23ed2017-03-21 13:06:01 -07003587 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003588 if (!binder_worklist_empty_ilocked(&thread->todo))
3589 list = &thread->todo;
3590 else if (!binder_worklist_empty_ilocked(&proc->todo) &&
3591 wait_for_proc_work)
3592 list = &proc->todo;
3593 else {
3594 binder_inner_proc_unlock(proc);
3595
Dmitry Voytik395262a2014-09-08 18:16:34 +04003596 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08003597 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003598 goto retry;
3599 break;
3600 }
3601
Todd Kjose7f23ed2017-03-21 13:06:01 -07003602 if (end - ptr < sizeof(tr) + 4) {
3603 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003604 break;
Todd Kjose7f23ed2017-03-21 13:06:01 -07003605 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003606 w = binder_dequeue_work_head_ilocked(list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003607
3608 switch (w->type) {
3609 case BINDER_WORK_TRANSACTION: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07003610 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003611 t = container_of(w, struct binder_transaction, work);
3612 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07003613 case BINDER_WORK_RETURN_ERROR: {
3614 struct binder_error *e = container_of(
3615 w, struct binder_error, work);
3616
3617 WARN_ON(e->cmd == BR_OK);
Todd Kjose7f23ed2017-03-21 13:06:01 -07003618 binder_inner_proc_unlock(proc);
Todd Kjos858b8da2017-04-21 17:35:12 -07003619 if (put_user(e->cmd, (uint32_t __user *)ptr))
3620 return -EFAULT;
3621 e->cmd = BR_OK;
3622 ptr += sizeof(uint32_t);
3623
3624 binder_stat_br(proc, thread, cmd);
Todd Kjos858b8da2017-04-21 17:35:12 -07003625 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003626 case BINDER_WORK_TRANSACTION_COMPLETE: {
Todd Kjose7f23ed2017-03-21 13:06:01 -07003627 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003628 cmd = BR_TRANSACTION_COMPLETE;
3629 if (put_user(cmd, (uint32_t __user *)ptr))
3630 return -EFAULT;
3631 ptr += sizeof(uint32_t);
3632
3633 binder_stat_br(proc, thread, cmd);
3634 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303635 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003636 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003637 kfree(w);
3638 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3639 } break;
3640 case BINDER_WORK_NODE: {
3641 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07003642 int strong, weak;
3643 binder_uintptr_t node_ptr = node->ptr;
3644 binder_uintptr_t node_cookie = node->cookie;
3645 int node_debug_id = node->debug_id;
3646 int has_weak_ref;
3647 int has_strong_ref;
3648 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09003649
Todd Kjos60792612017-05-24 10:51:01 -07003650 BUG_ON(proc != node->proc);
3651 strong = node->internal_strong_refs ||
3652 node->local_strong_refs;
3653 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07003654 node->local_weak_refs ||
3655 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07003656 has_strong_ref = node->has_strong_ref;
3657 has_weak_ref = node->has_weak_ref;
3658
3659 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003660 node->has_weak_ref = 1;
3661 node->pending_weak_ref = 1;
3662 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07003663 }
3664 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003665 node->has_strong_ref = 1;
3666 node->pending_strong_ref = 1;
3667 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07003668 }
3669 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003670 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07003671 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003672 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07003673 if (!weak && !strong) {
3674 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
3675 "%d:%d node %d u%016llx c%016llx deleted\n",
3676 proc->pid, thread->pid,
3677 node_debug_id,
3678 (u64)node_ptr,
3679 (u64)node_cookie);
3680 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjose7f23ed2017-03-21 13:06:01 -07003681 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07003682 binder_node_lock(node);
3683 /*
3684 * Acquire the node lock before freeing the
3685 * node to serialize with other threads that
3686 * may have been holding the node lock while
3687 * decrementing this node (avoids race where
3688 * this thread frees while the other thread
3689 * is unlocking the node after the final
3690 * decrement)
3691 */
3692 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07003693 binder_free_node(node);
3694 } else
3695 binder_inner_proc_unlock(proc);
3696
Todd Kjos60792612017-05-24 10:51:01 -07003697 if (weak && !has_weak_ref)
3698 ret = binder_put_node_cmd(
3699 proc, thread, &ptr, node_ptr,
3700 node_cookie, node_debug_id,
3701 BR_INCREFS, "BR_INCREFS");
3702 if (!ret && strong && !has_strong_ref)
3703 ret = binder_put_node_cmd(
3704 proc, thread, &ptr, node_ptr,
3705 node_cookie, node_debug_id,
3706 BR_ACQUIRE, "BR_ACQUIRE");
3707 if (!ret && !strong && has_strong_ref)
3708 ret = binder_put_node_cmd(
3709 proc, thread, &ptr, node_ptr,
3710 node_cookie, node_debug_id,
3711 BR_RELEASE, "BR_RELEASE");
3712 if (!ret && !weak && has_weak_ref)
3713 ret = binder_put_node_cmd(
3714 proc, thread, &ptr, node_ptr,
3715 node_cookie, node_debug_id,
3716 BR_DECREFS, "BR_DECREFS");
3717 if (orig_ptr == ptr)
3718 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
3719 "%d:%d node %d u%016llx c%016llx state unchanged\n",
3720 proc->pid, thread->pid,
3721 node_debug_id,
3722 (u64)node_ptr,
3723 (u64)node_cookie);
3724 if (ret)
3725 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003726 } break;
3727 case BINDER_WORK_DEAD_BINDER:
3728 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3729 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
3730 struct binder_ref_death *death;
3731 uint32_t cmd;
Martijn Coenenf9eac642017-05-22 11:26:23 -07003732 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003733
3734 death = container_of(w, struct binder_ref_death, work);
3735 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
3736 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
3737 else
3738 cmd = BR_DEAD_BINDER;
Martijn Coenenf9eac642017-05-22 11:26:23 -07003739 cookie = death->cookie;
3740
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003741 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003742 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003743 proc->pid, thread->pid,
3744 cmd == BR_DEAD_BINDER ?
3745 "BR_DEAD_BINDER" :
3746 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Martijn Coenenf9eac642017-05-22 11:26:23 -07003747 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003748 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
Martijn Coenenf9eac642017-05-22 11:26:23 -07003749 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003750 kfree(death);
3751 binder_stats_deleted(BINDER_STAT_DEATH);
Todd Kjose7f23ed2017-03-21 13:06:01 -07003752 } else {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003753 binder_enqueue_work_ilocked(
3754 w, &proc->delivered_death);
Todd Kjose7f23ed2017-03-21 13:06:01 -07003755 binder_inner_proc_unlock(proc);
3756 }
Martijn Coenenf9eac642017-05-22 11:26:23 -07003757 if (put_user(cmd, (uint32_t __user *)ptr))
3758 return -EFAULT;
3759 ptr += sizeof(uint32_t);
3760 if (put_user(cookie,
3761 (binder_uintptr_t __user *)ptr))
3762 return -EFAULT;
3763 ptr += sizeof(binder_uintptr_t);
3764 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003765 if (cmd == BR_DEAD_BINDER)
3766 goto done; /* DEAD_BINDER notifications can cause transactions */
3767 } break;
3768 }
3769
3770 if (!t)
3771 continue;
3772
3773 BUG_ON(t->buffer == NULL);
3774 if (t->buffer->target_node) {
3775 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09003776
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003777 tr.target.ptr = target_node->ptr;
3778 tr.cookie = target_node->cookie;
3779 t->saved_priority = task_nice(current);
3780 if (t->priority < target_node->min_priority &&
3781 !(t->flags & TF_ONE_WAY))
3782 binder_set_nice(t->priority);
3783 else if (!(t->flags & TF_ONE_WAY) ||
3784 t->saved_priority > target_node->min_priority)
3785 binder_set_nice(target_node->min_priority);
3786 cmd = BR_TRANSACTION;
3787 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003788 tr.target.ptr = 0;
3789 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003790 cmd = BR_REPLY;
3791 }
3792 tr.code = t->code;
3793 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06003794 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003795
Todd Kjos2f993e22017-05-12 14:42:55 -07003796 t_from = binder_get_txn_from(t);
3797 if (t_from) {
3798 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09003799
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003800 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003801 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003802 } else {
3803 tr.sender_pid = 0;
3804 }
3805
3806 tr.data_size = t->buffer->data_size;
3807 tr.offsets_size = t->buffer->offsets_size;
Todd Kjosd325d372016-10-10 10:40:53 -07003808 tr.data.ptr.buffer = (binder_uintptr_t)
3809 ((uintptr_t)t->buffer->data +
3810 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003811 tr.data.ptr.offsets = tr.data.ptr.buffer +
3812 ALIGN(t->buffer->data_size,
3813 sizeof(void *));
3814
Todd Kjos2f993e22017-05-12 14:42:55 -07003815 if (put_user(cmd, (uint32_t __user *)ptr)) {
3816 if (t_from)
3817 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003818 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07003819 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003820 ptr += sizeof(uint32_t);
Todd Kjos2f993e22017-05-12 14:42:55 -07003821 if (copy_to_user(ptr, &tr, sizeof(tr))) {
3822 if (t_from)
3823 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003824 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07003825 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003826 ptr += sizeof(tr);
3827
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003828 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003829 binder_stat_br(proc, thread, cmd);
3830 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003831 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003832 proc->pid, thread->pid,
3833 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
3834 "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07003835 t->debug_id, t_from ? t_from->proc->pid : 0,
3836 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003837 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003838 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003839
Todd Kjos2f993e22017-05-12 14:42:55 -07003840 if (t_from)
3841 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003842 t->buffer->allow_user_free = 1;
3843 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
Martijn Coenen995a36e2017-06-02 13:36:52 -07003844 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003845 t->to_parent = thread->transaction_stack;
3846 t->to_thread = thread;
3847 thread->transaction_stack = t;
Martijn Coenen995a36e2017-06-02 13:36:52 -07003848 binder_inner_proc_unlock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003849 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07003850 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003851 }
3852 break;
3853 }
3854
3855done:
3856
3857 *consumed = ptr - buffer;
Todd Kjosd600e902017-05-25 17:35:02 -07003858 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003859 if (proc->requested_threads + proc->ready_threads == 0 &&
3860 proc->requested_threads_started < proc->max_threads &&
3861 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3862 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
3863 /*spawn a new thread if we leave this out */) {
3864 proc->requested_threads++;
Todd Kjosd600e902017-05-25 17:35:02 -07003865 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003866 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303867 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003868 proc->pid, thread->pid);
3869 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
3870 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07003871 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Todd Kjosd600e902017-05-25 17:35:02 -07003872 } else
3873 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003874 return 0;
3875}
3876
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003877static void binder_release_work(struct binder_proc *proc,
3878 struct list_head *list)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003879{
3880 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09003881
Todd Kjos1c89e6b2016-10-20 10:33:00 -07003882 while (1) {
3883 w = binder_dequeue_work_head(proc, list);
3884 if (!w)
3885 return;
3886
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003887 switch (w->type) {
3888 case BINDER_WORK_TRANSACTION: {
3889 struct binder_transaction *t;
3890
3891 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003892 if (t->buffer->target_node &&
3893 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003894 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003895 } else {
3896 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303897 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003898 t->debug_id);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003899 binder_free_transaction(t);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003900 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003901 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07003902 case BINDER_WORK_RETURN_ERROR: {
3903 struct binder_error *e = container_of(
3904 w, struct binder_error, work);
3905
3906 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
3907 "undelivered TRANSACTION_ERROR: %u\n",
3908 e->cmd);
3909 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003910 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003911 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303912 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003913 kfree(w);
3914 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3915 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003916 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3917 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
3918 struct binder_ref_death *death;
3919
3920 death = container_of(w, struct binder_ref_death, work);
3921 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003922 "undelivered death notification, %016llx\n",
3923 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003924 kfree(death);
3925 binder_stats_deleted(BINDER_STAT_DEATH);
3926 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003927 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303928 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003929 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003930 break;
3931 }
3932 }
3933
3934}
3935
Todd Kjosb4827902017-05-25 15:52:17 -07003936static struct binder_thread *binder_get_thread_ilocked(
3937 struct binder_proc *proc, struct binder_thread *new_thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003938{
3939 struct binder_thread *thread = NULL;
3940 struct rb_node *parent = NULL;
3941 struct rb_node **p = &proc->threads.rb_node;
3942
3943 while (*p) {
3944 parent = *p;
3945 thread = rb_entry(parent, struct binder_thread, rb_node);
3946
3947 if (current->pid < thread->pid)
3948 p = &(*p)->rb_left;
3949 else if (current->pid > thread->pid)
3950 p = &(*p)->rb_right;
3951 else
Todd Kjosb4827902017-05-25 15:52:17 -07003952 return thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003953 }
Todd Kjosb4827902017-05-25 15:52:17 -07003954 if (!new_thread)
3955 return NULL;
3956 thread = new_thread;
3957 binder_stats_created(BINDER_STAT_THREAD);
3958 thread->proc = proc;
3959 thread->pid = current->pid;
3960 atomic_set(&thread->tmp_ref, 0);
3961 init_waitqueue_head(&thread->wait);
3962 INIT_LIST_HEAD(&thread->todo);
3963 rb_link_node(&thread->rb_node, parent, p);
3964 rb_insert_color(&thread->rb_node, &proc->threads);
3965 thread->looper_need_return = true;
3966 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
3967 thread->return_error.cmd = BR_OK;
3968 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
3969 thread->reply_error.cmd = BR_OK;
3970
3971 return thread;
3972}
3973
3974static struct binder_thread *binder_get_thread(struct binder_proc *proc)
3975{
3976 struct binder_thread *thread;
3977 struct binder_thread *new_thread;
3978
3979 binder_inner_proc_lock(proc);
3980 thread = binder_get_thread_ilocked(proc, NULL);
3981 binder_inner_proc_unlock(proc);
3982 if (!thread) {
3983 new_thread = kzalloc(sizeof(*thread), GFP_KERNEL);
3984 if (new_thread == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003985 return NULL;
Todd Kjosb4827902017-05-25 15:52:17 -07003986 binder_inner_proc_lock(proc);
3987 thread = binder_get_thread_ilocked(proc, new_thread);
3988 binder_inner_proc_unlock(proc);
3989 if (thread != new_thread)
3990 kfree(new_thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003991 }
3992 return thread;
3993}
3994
Todd Kjos2f993e22017-05-12 14:42:55 -07003995static void binder_free_proc(struct binder_proc *proc)
3996{
3997 BUG_ON(!list_empty(&proc->todo));
3998 BUG_ON(!list_empty(&proc->delivered_death));
3999 binder_alloc_deferred_release(&proc->alloc);
4000 put_task_struct(proc->tsk);
4001 binder_stats_deleted(BINDER_STAT_PROC);
4002 kfree(proc);
4003}
4004
4005static void binder_free_thread(struct binder_thread *thread)
4006{
4007 BUG_ON(!list_empty(&thread->todo));
4008 binder_stats_deleted(BINDER_STAT_THREAD);
4009 binder_proc_dec_tmpref(thread->proc);
4010 kfree(thread);
4011}
4012
4013static int binder_thread_release(struct binder_proc *proc,
4014 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004015{
4016 struct binder_transaction *t;
4017 struct binder_transaction *send_reply = NULL;
4018 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07004019 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004020
Todd Kjosb4827902017-05-25 15:52:17 -07004021 binder_inner_proc_lock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004022 /*
4023 * take a ref on the proc so it survives
4024 * after we remove this thread from proc->threads.
4025 * The corresponding dec is when we actually
4026 * free the thread in binder_free_thread()
4027 */
4028 proc->tmp_ref++;
4029 /*
4030 * take a ref on this thread to ensure it
4031 * survives while we are releasing it
4032 */
4033 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004034 rb_erase(&thread->rb_node, &proc->threads);
4035 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07004036 if (t) {
4037 spin_lock(&t->lock);
4038 if (t->to_thread == thread)
4039 send_reply = t;
4040 }
4041 thread->is_dead = true;
4042
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004043 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07004044 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004045 active_transactions++;
4046 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304047 "release %d:%d transaction %d %s, still active\n",
4048 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004049 t->debug_id,
4050 (t->to_thread == thread) ? "in" : "out");
4051
4052 if (t->to_thread == thread) {
4053 t->to_proc = NULL;
4054 t->to_thread = NULL;
4055 if (t->buffer) {
4056 t->buffer->transaction = NULL;
4057 t->buffer = NULL;
4058 }
4059 t = t->to_parent;
4060 } else if (t->from == thread) {
4061 t->from = NULL;
4062 t = t->from_parent;
4063 } else
4064 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07004065 spin_unlock(&last_t->lock);
4066 if (t)
4067 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004068 }
Todd Kjosb4827902017-05-25 15:52:17 -07004069 binder_inner_proc_unlock(thread->proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004070
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004071 if (send_reply)
4072 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004073 binder_release_work(proc, &thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07004074 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004075 return active_transactions;
4076}
4077
4078static unsigned int binder_poll(struct file *filp,
4079 struct poll_table_struct *wait)
4080{
4081 struct binder_proc *proc = filp->private_data;
4082 struct binder_thread *thread = NULL;
4083 int wait_for_proc_work;
4084
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004085 thread = binder_get_thread(proc);
4086
Martijn Coenen995a36e2017-06-02 13:36:52 -07004087 binder_inner_proc_lock(thread->proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004088 wait_for_proc_work = thread->transaction_stack == NULL &&
Martijn Coenen995a36e2017-06-02 13:36:52 -07004089 binder_worklist_empty_ilocked(&thread->todo);
4090 binder_inner_proc_unlock(thread->proc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004091
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004092 if (wait_for_proc_work) {
4093 if (binder_has_proc_work(proc, thread))
4094 return POLLIN;
4095 poll_wait(filp, &proc->wait, wait);
4096 if (binder_has_proc_work(proc, thread))
4097 return POLLIN;
4098 } else {
4099 if (binder_has_thread_work(thread))
4100 return POLLIN;
4101 poll_wait(filp, &thread->wait, wait);
4102 if (binder_has_thread_work(thread))
4103 return POLLIN;
4104 }
4105 return 0;
4106}
4107
Tair Rzayev78260ac2014-06-03 22:27:21 +03004108static int binder_ioctl_write_read(struct file *filp,
4109 unsigned int cmd, unsigned long arg,
4110 struct binder_thread *thread)
4111{
4112 int ret = 0;
4113 struct binder_proc *proc = filp->private_data;
4114 unsigned int size = _IOC_SIZE(cmd);
4115 void __user *ubuf = (void __user *)arg;
4116 struct binder_write_read bwr;
4117
4118 if (size != sizeof(struct binder_write_read)) {
4119 ret = -EINVAL;
4120 goto out;
4121 }
4122 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
4123 ret = -EFAULT;
4124 goto out;
4125 }
4126 binder_debug(BINDER_DEBUG_READ_WRITE,
4127 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
4128 proc->pid, thread->pid,
4129 (u64)bwr.write_size, (u64)bwr.write_buffer,
4130 (u64)bwr.read_size, (u64)bwr.read_buffer);
4131
4132 if (bwr.write_size > 0) {
4133 ret = binder_thread_write(proc, thread,
4134 bwr.write_buffer,
4135 bwr.write_size,
4136 &bwr.write_consumed);
4137 trace_binder_write_done(ret);
4138 if (ret < 0) {
4139 bwr.read_consumed = 0;
4140 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4141 ret = -EFAULT;
4142 goto out;
4143 }
4144 }
4145 if (bwr.read_size > 0) {
4146 ret = binder_thread_read(proc, thread, bwr.read_buffer,
4147 bwr.read_size,
4148 &bwr.read_consumed,
4149 filp->f_flags & O_NONBLOCK);
4150 trace_binder_read_done(ret);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004151 if (!binder_worklist_empty(proc, &proc->todo))
Tair Rzayev78260ac2014-06-03 22:27:21 +03004152 wake_up_interruptible(&proc->wait);
4153 if (ret < 0) {
4154 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
4155 ret = -EFAULT;
4156 goto out;
4157 }
4158 }
4159 binder_debug(BINDER_DEBUG_READ_WRITE,
4160 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
4161 proc->pid, thread->pid,
4162 (u64)bwr.write_consumed, (u64)bwr.write_size,
4163 (u64)bwr.read_consumed, (u64)bwr.read_size);
4164 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
4165 ret = -EFAULT;
4166 goto out;
4167 }
4168out:
4169 return ret;
4170}
4171
4172static int binder_ioctl_set_ctx_mgr(struct file *filp)
4173{
4174 int ret = 0;
4175 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004176 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004177 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004178 kuid_t curr_euid = current_euid();
4179
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004180 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004181 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004182 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
4183 ret = -EBUSY;
4184 goto out;
4185 }
Stephen Smalley79af7302015-01-21 10:54:10 -05004186 ret = security_binder_set_context_mgr(proc->tsk);
4187 if (ret < 0)
4188 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004189 if (uid_valid(context->binder_context_mgr_uid)) {
4190 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004191 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
4192 from_kuid(&init_user_ns, curr_euid),
4193 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004194 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03004195 ret = -EPERM;
4196 goto out;
4197 }
4198 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004199 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03004200 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004201 new_node = binder_new_node(proc, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004202 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004203 ret = -ENOMEM;
4204 goto out;
4205 }
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004206 binder_node_lock(new_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004207 new_node->local_weak_refs++;
4208 new_node->local_strong_refs++;
4209 new_node->has_strong_ref = 1;
4210 new_node->has_weak_ref = 1;
4211 context->binder_context_mgr_node = new_node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004212 binder_node_unlock(new_node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004213 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004214out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004215 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03004216 return ret;
4217}
4218
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004219static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4220{
4221 int ret;
4222 struct binder_proc *proc = filp->private_data;
4223 struct binder_thread *thread;
4224 unsigned int size = _IOC_SIZE(cmd);
4225 void __user *ubuf = (void __user *)arg;
4226
Tair Rzayev78260ac2014-06-03 22:27:21 +03004227 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
4228 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004229
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004230 trace_binder_ioctl(cmd, arg);
4231
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004232 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4233 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004234 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004235
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004236 thread = binder_get_thread(proc);
4237 if (thread == NULL) {
4238 ret = -ENOMEM;
4239 goto err;
4240 }
4241
4242 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03004243 case BINDER_WRITE_READ:
4244 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
4245 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004246 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004247 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004248 case BINDER_SET_MAX_THREADS: {
4249 int max_threads;
4250
4251 if (copy_from_user(&max_threads, ubuf,
4252 sizeof(max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004253 ret = -EINVAL;
4254 goto err;
4255 }
Todd Kjosd600e902017-05-25 17:35:02 -07004256 binder_inner_proc_lock(proc);
4257 proc->max_threads = max_threads;
4258 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004259 break;
Todd Kjosd600e902017-05-25 17:35:02 -07004260 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004261 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03004262 ret = binder_ioctl_set_ctx_mgr(filp);
4263 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004264 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004265 break;
4266 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05304267 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004268 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07004269 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004270 thread = NULL;
4271 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004272 case BINDER_VERSION: {
4273 struct binder_version __user *ver = ubuf;
4274
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004275 if (size != sizeof(struct binder_version)) {
4276 ret = -EINVAL;
4277 goto err;
4278 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02004279 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
4280 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004281 ret = -EINVAL;
4282 goto err;
4283 }
4284 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02004285 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004286 default:
4287 ret = -EINVAL;
4288 goto err;
4289 }
4290 ret = 0;
4291err:
4292 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08004293 thread->looper_need_return = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004294 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
4295 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05304296 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 -07004297err_unlocked:
4298 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004299 return ret;
4300}
4301
4302static void binder_vma_open(struct vm_area_struct *vma)
4303{
4304 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004305
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004306 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304307 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004308 proc->pid, vma->vm_start, vma->vm_end,
4309 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4310 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004311}
4312
4313static void binder_vma_close(struct vm_area_struct *vma)
4314{
4315 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004316
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004317 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05304318 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004319 proc->pid, vma->vm_start, vma->vm_end,
4320 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4321 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07004322 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004323 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
4324}
4325
Vinayak Menonddac7d52014-06-02 18:17:59 +05304326static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4327{
4328 return VM_FAULT_SIGBUS;
4329}
4330
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07004331static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004332 .open = binder_vma_open,
4333 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05304334 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004335};
4336
Todd Kjosd325d372016-10-10 10:40:53 -07004337static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
4338{
4339 int ret;
4340 struct binder_proc *proc = filp->private_data;
4341 const char *failure_string;
4342
4343 if (proc->tsk != current->group_leader)
4344 return -EINVAL;
4345
4346 if ((vma->vm_end - vma->vm_start) > SZ_4M)
4347 vma->vm_end = vma->vm_start + SZ_4M;
4348
4349 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4350 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
4351 __func__, proc->pid, vma->vm_start, vma->vm_end,
4352 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
4353 (unsigned long)pgprot_val(vma->vm_page_prot));
4354
4355 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
4356 ret = -EPERM;
4357 failure_string = "bad vm_flags";
4358 goto err_bad_arg;
4359 }
4360 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
4361 vma->vm_ops = &binder_vm_ops;
4362 vma->vm_private_data = proc;
4363
4364 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
4365 if (ret)
4366 return ret;
4367 proc->files = get_files_struct(current);
4368 return 0;
4369
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004370err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04004371 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004372 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
4373 return ret;
4374}
4375
4376static int binder_open(struct inode *nodp, struct file *filp)
4377{
4378 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004379 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004380
4381 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
4382 current->group_leader->pid, current->pid);
4383
4384 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
4385 if (proc == NULL)
4386 return -ENOMEM;
Todd Kjosfc7a7e22017-05-29 16:44:24 -07004387 spin_lock_init(&proc->inner_lock);
4388 spin_lock_init(&proc->outer_lock);
Martijn Coenen872c26e2017-03-07 15:51:18 +01004389 get_task_struct(current->group_leader);
4390 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004391 INIT_LIST_HEAD(&proc->todo);
4392 init_waitqueue_head(&proc->wait);
4393 proc->default_priority = task_nice(current);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004394 binder_dev = container_of(filp->private_data, struct binder_device,
4395 miscdev);
4396 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07004397 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004398
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004399 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004400 proc->pid = current->group_leader->pid;
4401 INIT_LIST_HEAD(&proc->delivered_death);
4402 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004403
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004404 mutex_lock(&binder_procs_lock);
4405 hlist_add_head(&proc->proc_node, &binder_procs);
4406 mutex_unlock(&binder_procs_lock);
4407
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004408 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004409 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09004410
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004411 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004412 /*
4413 * proc debug entries are shared between contexts, so
4414 * this will fail if the process tries to open the driver
4415 * again with a different context. The priting code will
4416 * anyway print all contexts that a given PID has, so this
4417 * is not a problem.
4418 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004419 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004420 binder_debugfs_dir_entry_proc,
4421 (void *)(unsigned long)proc->pid,
4422 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004423 }
4424
4425 return 0;
4426}
4427
4428static int binder_flush(struct file *filp, fl_owner_t id)
4429{
4430 struct binder_proc *proc = filp->private_data;
4431
4432 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
4433
4434 return 0;
4435}
4436
4437static void binder_deferred_flush(struct binder_proc *proc)
4438{
4439 struct rb_node *n;
4440 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09004441
Todd Kjosb4827902017-05-25 15:52:17 -07004442 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004443 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
4444 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09004445
Todd Kjos6798e6d2017-01-06 14:19:25 -08004446 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004447 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
4448 wake_up_interruptible(&thread->wait);
4449 wake_count++;
4450 }
4451 }
Todd Kjosb4827902017-05-25 15:52:17 -07004452 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004453 wake_up_interruptible_all(&proc->wait);
4454
4455 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
4456 "binder_flush: %d woke %d threads\n", proc->pid,
4457 wake_count);
4458}
4459
4460static int binder_release(struct inode *nodp, struct file *filp)
4461{
4462 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09004463
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004464 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004465 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
4466
4467 return 0;
4468}
4469
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004470static int binder_node_release(struct binder_node *node, int refs)
4471{
4472 struct binder_ref *ref;
4473 int death = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004474 struct binder_proc *proc = node->proc;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004475
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004476 binder_release_work(proc, &node->async_todo);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004477
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004478 binder_node_lock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004479 binder_inner_proc_lock(proc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004480 binder_dequeue_work_ilocked(&node->work);
Todd Kjosf22abc72017-05-09 11:08:05 -07004481 /*
4482 * The caller must have taken a temporary ref on the node,
4483 */
4484 BUG_ON(!node->tmp_refs);
4485 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Todd Kjose7f23ed2017-03-21 13:06:01 -07004486 binder_inner_proc_unlock(proc);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004487 binder_node_unlock(node);
Todd Kjose7f23ed2017-03-21 13:06:01 -07004488 binder_free_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004489
4490 return refs;
4491 }
4492
4493 node->proc = NULL;
4494 node->local_strong_refs = 0;
4495 node->local_weak_refs = 0;
Todd Kjose7f23ed2017-03-21 13:06:01 -07004496 binder_inner_proc_unlock(proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004497
4498 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004499 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004500 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004501
4502 hlist_for_each_entry(ref, &node->refs, node_entry) {
4503 refs++;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004504 /*
4505 * Need the node lock to synchronize
4506 * with new notification requests and the
4507 * inner lock to synchronize with queued
4508 * death notifications.
4509 */
4510 binder_inner_proc_lock(ref->proc);
4511 if (!ref->death) {
4512 binder_inner_proc_unlock(ref->proc);
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08004513 continue;
Martijn Coenenf9eac642017-05-22 11:26:23 -07004514 }
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004515
4516 death++;
4517
Martijn Coenenf9eac642017-05-22 11:26:23 -07004518 BUG_ON(!list_empty(&ref->death->work.entry));
4519 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
4520 binder_enqueue_work_ilocked(&ref->death->work,
4521 &ref->proc->todo);
4522 wake_up_interruptible(&ref->proc->wait);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004523 binder_inner_proc_unlock(ref->proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004524 }
4525
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004526 binder_debug(BINDER_DEBUG_DEAD_BINDER,
4527 "node %d now dead, refs %d, death %d\n",
4528 node->debug_id, refs, death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004529 binder_node_unlock(node);
Todd Kjosf22abc72017-05-09 11:08:05 -07004530 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004531
4532 return refs;
4533}
4534
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004535static void binder_deferred_release(struct binder_proc *proc)
4536{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004537 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004538 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07004539 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004540
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004541 BUG_ON(proc->files);
4542
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004543 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004544 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004545 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004546
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004547 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004548 if (context->binder_context_mgr_node &&
4549 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004550 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004551 "%s: %d context_mgr_node gone\n",
4552 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02004553 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004554 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004555 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjosb4827902017-05-25 15:52:17 -07004556 binder_inner_proc_lock(proc);
Todd Kjos2f993e22017-05-12 14:42:55 -07004557 /*
4558 * Make sure proc stays alive after we
4559 * remove all the threads
4560 */
4561 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004562
Todd Kjos2f993e22017-05-12 14:42:55 -07004563 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004564 threads = 0;
4565 active_transactions = 0;
4566 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004567 struct binder_thread *thread;
4568
4569 thread = rb_entry(n, struct binder_thread, rb_node);
Todd Kjosb4827902017-05-25 15:52:17 -07004570 binder_inner_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004571 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07004572 active_transactions += binder_thread_release(proc, thread);
Todd Kjosb4827902017-05-25 15:52:17 -07004573 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004574 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004575
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004576 nodes = 0;
4577 incoming_refs = 0;
4578 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004579 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004580
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004581 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004582 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07004583 /*
4584 * take a temporary ref on the node before
4585 * calling binder_node_release() which will either
4586 * kfree() the node or call binder_put_node()
4587 */
Todd Kjos425d23f2017-06-12 12:07:26 -07004588 binder_inc_node_tmpref_ilocked(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004589 rb_erase(&node->rb_node, &proc->nodes);
Todd Kjos425d23f2017-06-12 12:07:26 -07004590 binder_inner_proc_unlock(proc);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01004591 incoming_refs = binder_node_release(node, incoming_refs);
Todd Kjos425d23f2017-06-12 12:07:26 -07004592 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004593 }
Todd Kjos425d23f2017-06-12 12:07:26 -07004594 binder_inner_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004595
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004596 outgoing_refs = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07004597 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004598 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004599 struct binder_ref *ref;
4600
4601 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004602 outgoing_refs++;
Todd Kjos5346bf32016-10-20 16:43:34 -07004603 binder_cleanup_ref_olocked(ref);
4604 binder_proc_unlock(proc);
Todd Kjosb0117bb2017-05-08 09:16:27 -07004605 binder_free_ref(ref);
Todd Kjos5346bf32016-10-20 16:43:34 -07004606 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004607 }
Todd Kjos5346bf32016-10-20 16:43:34 -07004608 binder_proc_unlock(proc);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01004609
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004610 binder_release_work(proc, &proc->todo);
4611 binder_release_work(proc, &proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004612
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004613 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07004614 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01004615 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07004616 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004617
Todd Kjos2f993e22017-05-12 14:42:55 -07004618 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004619}
4620
4621static void binder_deferred_func(struct work_struct *work)
4622{
4623 struct binder_proc *proc;
4624 struct files_struct *files;
4625
4626 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09004627
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004628 do {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004629 mutex_lock(&binder_deferred_lock);
4630 if (!hlist_empty(&binder_deferred_list)) {
4631 proc = hlist_entry(binder_deferred_list.first,
4632 struct binder_proc, deferred_work_node);
4633 hlist_del_init(&proc->deferred_work_node);
4634 defer = proc->deferred_work;
4635 proc->deferred_work = 0;
4636 } else {
4637 proc = NULL;
4638 defer = 0;
4639 }
4640 mutex_unlock(&binder_deferred_lock);
4641
4642 files = NULL;
4643 if (defer & BINDER_DEFERRED_PUT_FILES) {
4644 files = proc->files;
4645 if (files)
4646 proc->files = NULL;
4647 }
4648
4649 if (defer & BINDER_DEFERRED_FLUSH)
4650 binder_deferred_flush(proc);
4651
4652 if (defer & BINDER_DEFERRED_RELEASE)
4653 binder_deferred_release(proc); /* frees proc */
4654
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004655 if (files)
4656 put_files_struct(files);
4657 } while (proc);
4658}
4659static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
4660
4661static void
4662binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
4663{
4664 mutex_lock(&binder_deferred_lock);
4665 proc->deferred_work |= defer;
4666 if (hlist_unhashed(&proc->deferred_work_node)) {
4667 hlist_add_head(&proc->deferred_work_node,
4668 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05304669 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004670 }
4671 mutex_unlock(&binder_deferred_lock);
4672}
4673
Todd Kjos6d241a42017-04-21 14:32:11 -07004674static void print_binder_transaction_ilocked(struct seq_file *m,
4675 struct binder_proc *proc,
4676 const char *prefix,
4677 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004678{
Todd Kjos6d241a42017-04-21 14:32:11 -07004679 struct binder_proc *to_proc;
4680 struct binder_buffer *buffer = t->buffer;
4681
4682 WARN_ON(!spin_is_locked(&proc->inner_lock));
Todd Kjos2f993e22017-05-12 14:42:55 -07004683 spin_lock(&t->lock);
Todd Kjos6d241a42017-04-21 14:32:11 -07004684 to_proc = t->to_proc;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004685 seq_printf(m,
4686 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
4687 prefix, t->debug_id, t,
4688 t->from ? t->from->proc->pid : 0,
4689 t->from ? t->from->pid : 0,
Todd Kjos6d241a42017-04-21 14:32:11 -07004690 to_proc ? to_proc->pid : 0,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004691 t->to_thread ? t->to_thread->pid : 0,
4692 t->code, t->flags, t->priority, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07004693 spin_unlock(&t->lock);
4694
Todd Kjos6d241a42017-04-21 14:32:11 -07004695 if (proc != to_proc) {
4696 /*
4697 * Can only safely deref buffer if we are holding the
4698 * correct proc inner lock for this node
4699 */
4700 seq_puts(m, "\n");
4701 return;
4702 }
4703
4704 if (buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004705 seq_puts(m, " buffer free\n");
4706 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004707 }
Todd Kjos6d241a42017-04-21 14:32:11 -07004708 if (buffer->target_node)
4709 seq_printf(m, " node %d", buffer->target_node->debug_id);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004710 seq_printf(m, " size %zd:%zd data %p\n",
Todd Kjos6d241a42017-04-21 14:32:11 -07004711 buffer->data_size, buffer->offsets_size,
4712 buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004713}
4714
Todd Kjos6d241a42017-04-21 14:32:11 -07004715static void print_binder_work_ilocked(struct seq_file *m,
4716 struct binder_proc *proc,
4717 const char *prefix,
4718 const char *transaction_prefix,
4719 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004720{
4721 struct binder_node *node;
4722 struct binder_transaction *t;
4723
4724 switch (w->type) {
4725 case BINDER_WORK_TRANSACTION:
4726 t = container_of(w, struct binder_transaction, work);
Todd Kjos6d241a42017-04-21 14:32:11 -07004727 print_binder_transaction_ilocked(
4728 m, proc, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004729 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07004730 case BINDER_WORK_RETURN_ERROR: {
4731 struct binder_error *e = container_of(
4732 w, struct binder_error, work);
4733
4734 seq_printf(m, "%stransaction error: %u\n",
4735 prefix, e->cmd);
4736 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004737 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004738 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004739 break;
4740 case BINDER_WORK_NODE:
4741 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08004742 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
4743 prefix, node->debug_id,
4744 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004745 break;
4746 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004747 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004748 break;
4749 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004750 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004751 break;
4752 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004753 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004754 break;
4755 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004756 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004757 break;
4758 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004759}
4760
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004761static void print_binder_thread_ilocked(struct seq_file *m,
4762 struct binder_thread *thread,
4763 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004764{
4765 struct binder_transaction *t;
4766 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004767 size_t start_pos = m->count;
4768 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004769
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004770 WARN_ON(!spin_is_locked(&thread->proc->inner_lock));
Todd Kjos2f993e22017-05-12 14:42:55 -07004771 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08004772 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07004773 thread->looper_need_return,
4774 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004775 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004776 t = thread->transaction_stack;
4777 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004778 if (t->from == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07004779 print_binder_transaction_ilocked(m, thread->proc,
4780 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004781 t = t->from_parent;
4782 } else if (t->to_thread == thread) {
Todd Kjos6d241a42017-04-21 14:32:11 -07004783 print_binder_transaction_ilocked(m, thread->proc,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004784 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004785 t = t->to_parent;
4786 } else {
Todd Kjos6d241a42017-04-21 14:32:11 -07004787 print_binder_transaction_ilocked(m, thread->proc,
4788 " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004789 t = NULL;
4790 }
4791 }
4792 list_for_each_entry(w, &thread->todo, entry) {
Todd Kjos6d241a42017-04-21 14:32:11 -07004793 print_binder_work_ilocked(m, thread->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004794 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004795 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004796 if (!print_always && m->count == header_pos)
4797 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004798}
4799
Todd Kjos425d23f2017-06-12 12:07:26 -07004800static void print_binder_node_nilocked(struct seq_file *m,
4801 struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004802{
4803 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004804 struct binder_work *w;
4805 int count;
4806
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004807 WARN_ON(!spin_is_locked(&node->lock));
Todd Kjos425d23f2017-06-12 12:07:26 -07004808 if (node->proc)
4809 WARN_ON(!spin_is_locked(&node->proc->inner_lock));
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004810
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004811 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004812 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004813 count++;
4814
Todd Kjosf22abc72017-05-09 11:08:05 -07004815 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d",
Arve Hjønnevågda498892014-02-21 14:40:26 -08004816 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004817 node->has_strong_ref, node->has_weak_ref,
4818 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07004819 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004820 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004821 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08004822 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004823 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004824 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004825 seq_puts(m, "\n");
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004826 if (node->proc) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004827 list_for_each_entry(w, &node->async_todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07004828 print_binder_work_ilocked(m, node->proc, " ",
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004829 " pending async transaction", w);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004830 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004831}
4832
Todd Kjos5346bf32016-10-20 16:43:34 -07004833static void print_binder_ref_olocked(struct seq_file *m,
4834 struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004835{
Todd Kjos5346bf32016-10-20 16:43:34 -07004836 WARN_ON(!spin_is_locked(&ref->proc->outer_lock));
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004837 binder_node_lock(ref->node);
Todd Kjosb0117bb2017-05-08 09:16:27 -07004838 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
4839 ref->data.debug_id, ref->data.desc,
4840 ref->node->proc ? "" : "dead ",
4841 ref->node->debug_id, ref->data.strong,
4842 ref->data.weak, ref->death);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07004843 binder_node_unlock(ref->node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004844}
4845
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004846static void print_binder_proc(struct seq_file *m,
4847 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004848{
4849 struct binder_work *w;
4850 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004851 size_t start_pos = m->count;
4852 size_t header_pos;
Todd Kjos425d23f2017-06-12 12:07:26 -07004853 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004854
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004855 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004856 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004857 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004858
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004859 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004860 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004861 print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004862 rb_node), print_all);
Todd Kjos425d23f2017-06-12 12:07:26 -07004863
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004864 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004865 struct binder_node *node = rb_entry(n, struct binder_node,
4866 rb_node);
Todd Kjos425d23f2017-06-12 12:07:26 -07004867 /*
4868 * take a temporary reference on the node so it
4869 * survives and isn't removed from the tree
4870 * while we print it.
4871 */
4872 binder_inc_node_tmpref_ilocked(node);
4873 /* Need to drop inner lock to take node lock */
4874 binder_inner_proc_unlock(proc);
4875 if (last_node)
4876 binder_put_node(last_node);
4877 binder_node_inner_lock(node);
4878 print_binder_node_nilocked(m, node);
4879 binder_node_inner_unlock(node);
4880 last_node = node;
4881 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004882 }
Todd Kjos425d23f2017-06-12 12:07:26 -07004883 binder_inner_proc_unlock(proc);
4884 if (last_node)
4885 binder_put_node(last_node);
4886
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004887 if (print_all) {
Todd Kjos5346bf32016-10-20 16:43:34 -07004888 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004889 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004890 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004891 n = rb_next(n))
Todd Kjos5346bf32016-10-20 16:43:34 -07004892 print_binder_ref_olocked(m, rb_entry(n,
4893 struct binder_ref,
4894 rb_node_desc));
4895 binder_proc_unlock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004896 }
Todd Kjosd325d372016-10-10 10:40:53 -07004897 binder_alloc_print_allocated(m, &proc->alloc);
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004898 binder_inner_proc_lock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004899 list_for_each_entry(w, &proc->todo, entry)
Todd Kjos6d241a42017-04-21 14:32:11 -07004900 print_binder_work_ilocked(m, proc, " ",
4901 " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004902 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004903 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004904 break;
4905 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07004906 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004907 if (!print_all && m->count == header_pos)
4908 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004909}
4910
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10004911static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004912 "BR_ERROR",
4913 "BR_OK",
4914 "BR_TRANSACTION",
4915 "BR_REPLY",
4916 "BR_ACQUIRE_RESULT",
4917 "BR_DEAD_REPLY",
4918 "BR_TRANSACTION_COMPLETE",
4919 "BR_INCREFS",
4920 "BR_ACQUIRE",
4921 "BR_RELEASE",
4922 "BR_DECREFS",
4923 "BR_ATTEMPT_ACQUIRE",
4924 "BR_NOOP",
4925 "BR_SPAWN_LOOPER",
4926 "BR_FINISHED",
4927 "BR_DEAD_BINDER",
4928 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4929 "BR_FAILED_REPLY"
4930};
4931
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10004932static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004933 "BC_TRANSACTION",
4934 "BC_REPLY",
4935 "BC_ACQUIRE_RESULT",
4936 "BC_FREE_BUFFER",
4937 "BC_INCREFS",
4938 "BC_ACQUIRE",
4939 "BC_RELEASE",
4940 "BC_DECREFS",
4941 "BC_INCREFS_DONE",
4942 "BC_ACQUIRE_DONE",
4943 "BC_ATTEMPT_ACQUIRE",
4944 "BC_REGISTER_LOOPER",
4945 "BC_ENTER_LOOPER",
4946 "BC_EXIT_LOOPER",
4947 "BC_REQUEST_DEATH_NOTIFICATION",
4948 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02004949 "BC_DEAD_BINDER_DONE",
4950 "BC_TRANSACTION_SG",
4951 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004952};
4953
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10004954static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004955 "proc",
4956 "thread",
4957 "node",
4958 "ref",
4959 "death",
4960 "transaction",
4961 "transaction_complete"
4962};
4963
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004964static void print_binder_stats(struct seq_file *m, const char *prefix,
4965 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004966{
4967 int i;
4968
4969 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004970 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004971 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004972 int temp = atomic_read(&stats->bc[i]);
4973
4974 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004975 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004976 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004977 }
4978
4979 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004980 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004981 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004982 int temp = atomic_read(&stats->br[i]);
4983
4984 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004985 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004986 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004987 }
4988
4989 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004990 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004991 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004992 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004993 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004994 int created = atomic_read(&stats->obj_created[i]);
4995 int deleted = atomic_read(&stats->obj_deleted[i]);
4996
4997 if (created || deleted)
4998 seq_printf(m, "%s%s: active %d total %d\n",
4999 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005000 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07005001 created - deleted,
5002 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005003 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005004}
5005
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005006static void print_binder_proc_stats(struct seq_file *m,
5007 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005008{
5009 struct binder_work *w;
5010 struct rb_node *n;
5011 int count, strong, weak;
Todd Kjosb4827902017-05-25 15:52:17 -07005012 size_t free_async_space =
5013 binder_alloc_get_free_async_space(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005014
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005015 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005016 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005017 count = 0;
Todd Kjosb4827902017-05-25 15:52:17 -07005018 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005019 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
5020 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005021 seq_printf(m, " threads: %d\n", count);
5022 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005023 " ready threads %d\n"
5024 " free async space %zd\n", proc->requested_threads,
5025 proc->requested_threads_started, proc->max_threads,
Todd Kjosd325d372016-10-10 10:40:53 -07005026 proc->ready_threads,
Todd Kjosb4827902017-05-25 15:52:17 -07005027 free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005028 count = 0;
5029 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
5030 count++;
Todd Kjos425d23f2017-06-12 12:07:26 -07005031 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005032 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005033 count = 0;
5034 strong = 0;
5035 weak = 0;
Todd Kjos5346bf32016-10-20 16:43:34 -07005036 binder_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005037 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
5038 struct binder_ref *ref = rb_entry(n, struct binder_ref,
5039 rb_node_desc);
5040 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07005041 strong += ref->data.strong;
5042 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005043 }
Todd Kjos5346bf32016-10-20 16:43:34 -07005044 binder_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005045 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005046
Todd Kjosd325d372016-10-10 10:40:53 -07005047 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005048 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005049
5050 count = 0;
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005051 binder_inner_proc_lock(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005052 list_for_each_entry(w, &proc->todo, entry) {
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005053 if (w->type == BINDER_WORK_TRANSACTION)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005054 count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005055 }
Todd Kjos1c89e6b2016-10-20 10:33:00 -07005056 binder_inner_proc_unlock(proc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005057 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005058
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005059 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005060}
5061
5062
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005063static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005064{
5065 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005066 struct binder_node *node;
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005067 struct binder_node *last_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005068
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005069 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005070
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005071 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005072 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005073 seq_puts(m, "dead nodes:\n");
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005074 hlist_for_each_entry(node, &binder_dead_nodes, dead_node) {
5075 /*
5076 * take a temporary reference on the node so it
5077 * survives and isn't removed from the list
5078 * while we print it.
5079 */
5080 node->tmp_refs++;
5081 spin_unlock(&binder_dead_nodes_lock);
5082 if (last_node)
5083 binder_put_node(last_node);
5084 binder_node_lock(node);
Todd Kjos425d23f2017-06-12 12:07:26 -07005085 print_binder_node_nilocked(m, node);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005086 binder_node_unlock(node);
5087 last_node = node;
5088 spin_lock(&binder_dead_nodes_lock);
5089 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005090 spin_unlock(&binder_dead_nodes_lock);
Todd Kjoscbcbbd62017-06-08 13:45:59 -07005091 if (last_node)
5092 binder_put_node(last_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005093
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005094 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005095 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005096 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005097 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005098
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005099 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005100}
5101
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005102static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005103{
5104 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005105
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005106 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005107
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005108 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005109
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005110 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005111 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005112 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005113 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005114
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005115 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005116}
5117
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005118static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005119{
5120 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005121
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005122 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005123 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08005124 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005125 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005126 mutex_unlock(&binder_procs_lock);
Todd Kjos218b6972016-11-14 11:37:41 -08005127
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005128 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005129}
5130
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005131static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005132{
Riley Andrews83050a42016-02-09 21:05:33 -08005133 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005134 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005135
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005136 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08005137 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005138 if (itr->pid == pid) {
5139 seq_puts(m, "binder proc state:\n");
5140 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08005141 }
5142 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005143 mutex_unlock(&binder_procs_lock);
5144
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005145 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005146}
5147
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005148static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005149 struct binder_transaction_log_entry *e)
5150{
Todd Kjos1cfe6272017-05-24 13:33:28 -07005151 int debug_id = READ_ONCE(e->debug_id_done);
5152 /*
5153 * read barrier to guarantee debug_id_done read before
5154 * we print the log values
5155 */
5156 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005157 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07005158 "%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 -07005159 e->debug_id, (e->call_type == 2) ? "reply" :
5160 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02005161 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07005162 e->to_node, e->target_handle, e->data_size, e->offsets_size,
5163 e->return_error, e->return_error_param,
5164 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07005165 /*
5166 * read-barrier to guarantee read of debug_id_done after
5167 * done printing the fields of the entry
5168 */
5169 smp_rmb();
5170 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
5171 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005172}
5173
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005174static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005175{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005176 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07005177 unsigned int log_cur = atomic_read(&log->cur);
5178 unsigned int count;
5179 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005180 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005181
Todd Kjos1cfe6272017-05-24 13:33:28 -07005182 count = log_cur + 1;
5183 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
5184 0 : count % ARRAY_SIZE(log->entry);
5185 if (count > ARRAY_SIZE(log->entry) || log->full)
5186 count = ARRAY_SIZE(log->entry);
5187 for (i = 0; i < count; i++) {
5188 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
5189
5190 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005191 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005192 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005193}
5194
5195static const struct file_operations binder_fops = {
5196 .owner = THIS_MODULE,
5197 .poll = binder_poll,
5198 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08005199 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005200 .mmap = binder_mmap,
5201 .open = binder_open,
5202 .flush = binder_flush,
5203 .release = binder_release,
5204};
5205
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07005206BINDER_DEBUG_ENTRY(state);
5207BINDER_DEBUG_ENTRY(stats);
5208BINDER_DEBUG_ENTRY(transactions);
5209BINDER_DEBUG_ENTRY(transaction_log);
5210
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005211static int __init init_binder_device(const char *name)
5212{
5213 int ret;
5214 struct binder_device *binder_device;
5215
5216 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
5217 if (!binder_device)
5218 return -ENOMEM;
5219
5220 binder_device->miscdev.fops = &binder_fops;
5221 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
5222 binder_device->miscdev.name = name;
5223
5224 binder_device->context.binder_context_mgr_uid = INVALID_UID;
5225 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07005226 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005227
5228 ret = misc_register(&binder_device->miscdev);
5229 if (ret < 0) {
5230 kfree(binder_device);
5231 return ret;
5232 }
5233
5234 hlist_add_head(&binder_device->hlist, &binder_devices);
5235
5236 return ret;
5237}
5238
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005239static int __init binder_init(void)
5240{
5241 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005242 char *device_name, *device_names;
5243 struct binder_device *device;
5244 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005245
Todd Kjos1cfe6272017-05-24 13:33:28 -07005246 atomic_set(&binder_transaction_log.cur, ~0U);
5247 atomic_set(&binder_transaction_log_failed.cur, ~0U);
5248
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005249 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
5250 if (binder_debugfs_dir_entry_root)
5251 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
5252 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005253
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07005254 if (binder_debugfs_dir_entry_root) {
5255 debugfs_create_file("state",
5256 S_IRUGO,
5257 binder_debugfs_dir_entry_root,
5258 NULL,
5259 &binder_state_fops);
5260 debugfs_create_file("stats",
5261 S_IRUGO,
5262 binder_debugfs_dir_entry_root,
5263 NULL,
5264 &binder_stats_fops);
5265 debugfs_create_file("transactions",
5266 S_IRUGO,
5267 binder_debugfs_dir_entry_root,
5268 NULL,
5269 &binder_transactions_fops);
5270 debugfs_create_file("transaction_log",
5271 S_IRUGO,
5272 binder_debugfs_dir_entry_root,
5273 &binder_transaction_log,
5274 &binder_transaction_log_fops);
5275 debugfs_create_file("failed_transaction_log",
5276 S_IRUGO,
5277 binder_debugfs_dir_entry_root,
5278 &binder_transaction_log_failed,
5279 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005280 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02005281
5282 /*
5283 * Copy the module_parameter string, because we don't want to
5284 * tokenize it in-place.
5285 */
5286 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
5287 if (!device_names) {
5288 ret = -ENOMEM;
5289 goto err_alloc_device_names_failed;
5290 }
5291 strcpy(device_names, binder_devices_param);
5292
5293 while ((device_name = strsep(&device_names, ","))) {
5294 ret = init_binder_device(device_name);
5295 if (ret)
5296 goto err_init_binder_device_failed;
5297 }
5298
5299 return ret;
5300
5301err_init_binder_device_failed:
5302 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
5303 misc_deregister(&device->miscdev);
5304 hlist_del(&device->hlist);
5305 kfree(device);
5306 }
5307err_alloc_device_names_failed:
5308 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
5309
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005310 return ret;
5311}
5312
5313device_initcall(binder_init);
5314
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07005315#define CREATE_TRACE_POINTS
5316#include "binder_trace.h"
5317
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09005318MODULE_LICENSE("GPL v2");