blob: 9db9f4acad9adba80eabdac750cce08e0f3b2d38 [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
Anmol Sarma56b468f2012-10-30 22:35:43 +053018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090020#include <asm/cacheflush.h>
21#include <linux/fdtable.h>
22#include <linux/file.h>
Colin Crosse2610b22013-05-06 23:50:15 +000023#include <linux/freezer.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090024#include <linux/fs.h>
25#include <linux/list.h>
26#include <linux/miscdevice.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090027#include <linux/module.h>
28#include <linux/mutex.h>
29#include <linux/nsproxy.h>
30#include <linux/poll.h>
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070031#include <linux/debugfs.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090032#include <linux/rbtree.h>
33#include <linux/sched.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070034#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090035#include <linux/uaccess.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080036#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050037#include <linux/security.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090038
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020039#ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
40#define BINDER_IPC_32BIT 1
41#endif
42
43#include <uapi/linux/android/binder.h>
Todd Kjosb9341022016-10-10 10:40:53 -070044#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070045#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090046
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070047static DEFINE_MUTEX(binder_main_lock);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070048
49static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090050static DEFINE_MUTEX(binder_deferred_lock);
51
Martijn Coenen6b7c7122016-09-30 16:08:09 +020052static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090053static HLIST_HEAD(binder_procs);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070054static DEFINE_MUTEX(binder_procs_lock);
55
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090056static HLIST_HEAD(binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070057static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090058
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070059static struct dentry *binder_debugfs_dir_entry_root;
60static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjosc4bd08b2017-05-25 10:56:00 -070061static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090062
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070063#define BINDER_DEBUG_ENTRY(name) \
64static int binder_##name##_open(struct inode *inode, struct file *file) \
65{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070066 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070067} \
68\
69static const struct file_operations binder_##name##_fops = { \
70 .owner = THIS_MODULE, \
71 .open = binder_##name##_open, \
72 .read = seq_read, \
73 .llseek = seq_lseek, \
74 .release = single_release, \
75}
76
77static int binder_proc_show(struct seq_file *m, void *unused);
78BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090079
80/* This is only defined in include/asm-arm/sizes.h */
81#ifndef SZ_1K
82#define SZ_1K 0x400
83#endif
84
85#ifndef SZ_4M
86#define SZ_4M 0x400000
87#endif
88
89#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
90
91#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
92
93enum {
94 BINDER_DEBUG_USER_ERROR = 1U << 0,
95 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
96 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
97 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
98 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
99 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
100 BINDER_DEBUG_READ_WRITE = 1U << 6,
101 BINDER_DEBUG_USER_REFS = 1U << 7,
102 BINDER_DEBUG_THREADS = 1U << 8,
103 BINDER_DEBUG_TRANSACTION = 1U << 9,
104 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
105 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
106 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjosd325d372016-10-10 10:40:53 -0700107 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900108};
109static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
110 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
111module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
112
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200113static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
114module_param_named(devices, binder_devices_param, charp, S_IRUGO);
115
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900116static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
117static int binder_stop_on_user_error;
118
119static int binder_set_stop_on_user_error(const char *val,
120 struct kernel_param *kp)
121{
122 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900123
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900124 ret = param_set_int(val, kp);
125 if (binder_stop_on_user_error < 2)
126 wake_up(&binder_user_error_wait);
127 return ret;
128}
129module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
130 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
131
132#define binder_debug(mask, x...) \
133 do { \
134 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400135 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900136 } while (0)
137
138#define binder_user_error(x...) \
139 do { \
140 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400141 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900142 if (binder_stop_on_user_error) \
143 binder_stop_on_user_error = 2; \
144 } while (0)
145
Martijn Coenen00c80372016-07-13 12:06:49 +0200146#define to_flat_binder_object(hdr) \
147 container_of(hdr, struct flat_binder_object, hdr)
148
149#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
150
Martijn Coenen5a6da532016-09-30 14:10:07 +0200151#define to_binder_buffer_object(hdr) \
152 container_of(hdr, struct binder_buffer_object, hdr)
153
Martijn Coenene3e0f4802016-10-18 13:58:55 +0200154#define to_binder_fd_array_object(hdr) \
155 container_of(hdr, struct binder_fd_array_object, hdr)
156
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900157enum binder_stat_types {
158 BINDER_STAT_PROC,
159 BINDER_STAT_THREAD,
160 BINDER_STAT_NODE,
161 BINDER_STAT_REF,
162 BINDER_STAT_DEATH,
163 BINDER_STAT_TRANSACTION,
164 BINDER_STAT_TRANSACTION_COMPLETE,
165 BINDER_STAT_COUNT
166};
167
168struct binder_stats {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700169 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
170 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
171 atomic_t obj_created[BINDER_STAT_COUNT];
172 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900173};
174
175static struct binder_stats binder_stats;
176
177static inline void binder_stats_deleted(enum binder_stat_types type)
178{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700179 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900180}
181
182static inline void binder_stats_created(enum binder_stat_types type)
183{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700184 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900185}
186
187struct binder_transaction_log_entry {
188 int debug_id;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700189 int debug_id_done;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900190 int call_type;
191 int from_proc;
192 int from_thread;
193 int target_handle;
194 int to_proc;
195 int to_thread;
196 int to_node;
197 int data_size;
198 int offsets_size;
Todd Kjose598d172017-03-22 17:19:52 -0700199 int return_error_line;
200 uint32_t return_error;
201 uint32_t return_error_param;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200202 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900203};
204struct binder_transaction_log {
Todd Kjos1cfe6272017-05-24 13:33:28 -0700205 atomic_t cur;
206 bool full;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900207 struct binder_transaction_log_entry entry[32];
208};
209static struct binder_transaction_log binder_transaction_log;
210static struct binder_transaction_log binder_transaction_log_failed;
211
212static struct binder_transaction_log_entry *binder_transaction_log_add(
213 struct binder_transaction_log *log)
214{
215 struct binder_transaction_log_entry *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700216 unsigned int cur = atomic_inc_return(&log->cur);
Seunghun Lee10f62862014-05-01 01:30:23 +0900217
Todd Kjos1cfe6272017-05-24 13:33:28 -0700218 if (cur >= ARRAY_SIZE(log->entry))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900219 log->full = 1;
Todd Kjos1cfe6272017-05-24 13:33:28 -0700220 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
221 WRITE_ONCE(e->debug_id_done, 0);
222 /*
223 * write-barrier to synchronize access to e->debug_id_done.
224 * We make sure the initialized 0 value is seen before
225 * memset() other fields are zeroed by memset.
226 */
227 smp_wmb();
228 memset(e, 0, sizeof(*e));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900229 return e;
230}
231
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200232struct binder_context {
233 struct binder_node *binder_context_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -0700234 struct mutex context_mgr_node_lock;
235
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200236 kuid_t binder_context_mgr_uid;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200237 const char *name;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200238};
239
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200240struct binder_device {
241 struct hlist_node hlist;
242 struct miscdevice miscdev;
243 struct binder_context context;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200244};
245
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900246struct binder_work {
247 struct list_head entry;
248 enum {
249 BINDER_WORK_TRANSACTION = 1,
250 BINDER_WORK_TRANSACTION_COMPLETE,
Todd Kjos858b8da2017-04-21 17:35:12 -0700251 BINDER_WORK_RETURN_ERROR,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900252 BINDER_WORK_NODE,
253 BINDER_WORK_DEAD_BINDER,
254 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
255 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
256 } type;
257};
258
Todd Kjos858b8da2017-04-21 17:35:12 -0700259struct binder_error {
260 struct binder_work work;
261 uint32_t cmd;
262};
263
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900264struct binder_node {
265 int debug_id;
266 struct binder_work work;
267 union {
268 struct rb_node rb_node;
269 struct hlist_node dead_node;
270 };
271 struct binder_proc *proc;
272 struct hlist_head refs;
273 int internal_strong_refs;
274 int local_weak_refs;
275 int local_strong_refs;
Todd Kjosf22abc72017-05-09 11:08:05 -0700276 int tmp_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800277 binder_uintptr_t ptr;
278 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900279 unsigned has_strong_ref:1;
280 unsigned pending_strong_ref:1;
281 unsigned has_weak_ref:1;
282 unsigned pending_weak_ref:1;
283 unsigned has_async_transaction:1;
284 unsigned accept_fds:1;
285 unsigned min_priority:8;
286 struct list_head async_todo;
287};
288
289struct binder_ref_death {
290 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800291 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900292};
293
Todd Kjosb0117bb2017-05-08 09:16:27 -0700294/**
295 * struct binder_ref_data - binder_ref counts and id
296 * @debug_id: unique ID for the ref
297 * @desc: unique userspace handle for ref
298 * @strong: strong ref count (debugging only if not locked)
299 * @weak: weak ref count (debugging only if not locked)
300 *
301 * Structure to hold ref count and ref id information. Since
302 * the actual ref can only be accessed with a lock, this structure
303 * is used to return information about the ref to callers of
304 * ref inc/dec functions.
305 */
306struct binder_ref_data {
307 int debug_id;
308 uint32_t desc;
309 int strong;
310 int weak;
311};
312
313/**
314 * struct binder_ref - struct to track references on nodes
315 * @data: binder_ref_data containing id, handle, and current refcounts
316 * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree
317 * @rb_node_node: node for lookup by @node in proc's rb_tree
318 * @node_entry: list entry for node->refs list in target node
319 * @proc: binder_proc containing ref
320 * @node: binder_node of target node. When cleaning up a
321 * ref for deletion in binder_cleanup_ref, a non-NULL
322 * @node indicates the node must be freed
323 * @death: pointer to death notification (ref_death) if requested
324 *
325 * Structure to track references from procA to target node (on procB). This
326 * structure is unsafe to access without holding @proc->outer_lock.
327 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900328struct binder_ref {
329 /* Lookups needed: */
330 /* node + proc => ref (transaction) */
331 /* desc + proc => ref (transaction, inc/dec ref) */
332 /* node => refs + procs (proc exit) */
Todd Kjosb0117bb2017-05-08 09:16:27 -0700333 struct binder_ref_data data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900334 struct rb_node rb_node_desc;
335 struct rb_node rb_node_node;
336 struct hlist_node node_entry;
337 struct binder_proc *proc;
338 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900339 struct binder_ref_death *death;
340};
341
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900342enum binder_deferred_state {
343 BINDER_DEFERRED_PUT_FILES = 0x01,
344 BINDER_DEFERRED_FLUSH = 0x02,
345 BINDER_DEFERRED_RELEASE = 0x04,
346};
347
348struct binder_proc {
349 struct hlist_node proc_node;
350 struct rb_root threads;
351 struct rb_root nodes;
352 struct rb_root refs_by_desc;
353 struct rb_root refs_by_node;
354 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900355 struct task_struct *tsk;
356 struct files_struct *files;
357 struct hlist_node deferred_work_node;
358 int deferred_work;
Todd Kjos2f993e22017-05-12 14:42:55 -0700359 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900360
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900361 struct list_head todo;
362 wait_queue_head_t wait;
363 struct binder_stats stats;
364 struct list_head delivered_death;
365 int max_threads;
366 int requested_threads;
367 int requested_threads_started;
368 int ready_threads;
Todd Kjos2f993e22017-05-12 14:42:55 -0700369 int tmp_ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900370 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700371 struct dentry *debugfs_entry;
Todd Kjosf85d2292016-10-10 10:39:59 -0700372 struct binder_alloc alloc;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200373 struct binder_context *context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900374};
375
376enum {
377 BINDER_LOOPER_STATE_REGISTERED = 0x01,
378 BINDER_LOOPER_STATE_ENTERED = 0x02,
379 BINDER_LOOPER_STATE_EXITED = 0x04,
380 BINDER_LOOPER_STATE_INVALID = 0x08,
381 BINDER_LOOPER_STATE_WAITING = 0x10,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900382};
383
384struct binder_thread {
385 struct binder_proc *proc;
386 struct rb_node rb_node;
387 int pid;
Todd Kjos6798e6d2017-01-06 14:19:25 -0800388 int looper; /* only modified by this thread */
389 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900390 struct binder_transaction *transaction_stack;
391 struct list_head todo;
Todd Kjos858b8da2017-04-21 17:35:12 -0700392 struct binder_error return_error;
393 struct binder_error reply_error;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900394 wait_queue_head_t wait;
395 struct binder_stats stats;
Todd Kjos2f993e22017-05-12 14:42:55 -0700396 atomic_t tmp_ref;
397 bool is_dead;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900398};
399
400struct binder_transaction {
401 int debug_id;
402 struct binder_work work;
403 struct binder_thread *from;
404 struct binder_transaction *from_parent;
405 struct binder_proc *to_proc;
406 struct binder_thread *to_thread;
407 struct binder_transaction *to_parent;
408 unsigned need_reply:1;
409 /* unsigned is_dead:1; */ /* not used at the moment */
410
411 struct binder_buffer *buffer;
412 unsigned int code;
413 unsigned int flags;
414 long priority;
415 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600416 kuid_t sender_euid;
Todd Kjos2f993e22017-05-12 14:42:55 -0700417 /**
418 * @lock: protects @from, @to_proc, and @to_thread
419 *
420 * @from, @to_proc, and @to_thread can be set to NULL
421 * during thread teardown
422 */
423 spinlock_t lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900424};
425
426static void
427binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
Todd Kjos2f993e22017-05-12 14:42:55 -0700428static void binder_free_thread(struct binder_thread *thread);
429static void binder_free_proc(struct binder_proc *proc);
Todd Kjosf22abc72017-05-09 11:08:05 -0700430static void binder_inc_node_tmpref(struct binder_node *node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900431
Sachin Kamatefde99c2012-08-17 16:39:36 +0530432static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900433{
434 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900435 unsigned long rlim_cur;
436 unsigned long irqs;
437
438 if (files == NULL)
439 return -ESRCH;
440
Al Virodcfadfa2012-08-12 17:27:30 -0400441 if (!lock_task_sighand(proc->tsk, &irqs))
442 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900443
Al Virodcfadfa2012-08-12 17:27:30 -0400444 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
445 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900446
Al Virodcfadfa2012-08-12 17:27:30 -0400447 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900448}
449
450/*
451 * copied from fd_install
452 */
453static void task_fd_install(
454 struct binder_proc *proc, unsigned int fd, struct file *file)
455{
Al Virof869e8a2012-08-15 21:06:33 -0400456 if (proc->files)
457 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900458}
459
460/*
461 * copied from sys_close
462 */
463static long task_close_fd(struct binder_proc *proc, unsigned int fd)
464{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900465 int retval;
466
Al Viro483ce1d2012-08-19 12:04:24 -0400467 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900468 return -ESRCH;
469
Al Viro483ce1d2012-08-19 12:04:24 -0400470 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900471 /* can't restart close syscall because file table entry was cleared */
472 if (unlikely(retval == -ERESTARTSYS ||
473 retval == -ERESTARTNOINTR ||
474 retval == -ERESTARTNOHAND ||
475 retval == -ERESTART_RESTARTBLOCK))
476 retval = -EINTR;
477
478 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900479}
480
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700481static inline void binder_lock(const char *tag)
482{
483 trace_binder_lock(tag);
484 mutex_lock(&binder_main_lock);
485 trace_binder_locked(tag);
486}
487
488static inline void binder_unlock(const char *tag)
489{
490 trace_binder_unlock(tag);
491 mutex_unlock(&binder_main_lock);
492}
493
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900494static void binder_set_nice(long nice)
495{
496 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900497
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900498 if (can_nice(current, nice)) {
499 set_user_nice(current, nice);
500 return;
501 }
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900502 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900503 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530504 "%d: nice value %ld not allowed use %ld instead\n",
505 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900506 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +0800507 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900508 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530509 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900510}
511
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900512static struct binder_node *binder_get_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800513 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900514{
515 struct rb_node *n = proc->nodes.rb_node;
516 struct binder_node *node;
517
518 while (n) {
519 node = rb_entry(n, struct binder_node, rb_node);
520
521 if (ptr < node->ptr)
522 n = n->rb_left;
523 else if (ptr > node->ptr)
524 n = n->rb_right;
Todd Kjosf22abc72017-05-09 11:08:05 -0700525 else {
526 /*
527 * take an implicit weak reference
528 * to ensure node stays alive until
529 * call to binder_put_node()
530 */
531 binder_inc_node_tmpref(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900532 return node;
Todd Kjosf22abc72017-05-09 11:08:05 -0700533 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900534 }
535 return NULL;
536}
537
538static struct binder_node *binder_new_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800539 binder_uintptr_t ptr,
540 binder_uintptr_t cookie)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900541{
542 struct rb_node **p = &proc->nodes.rb_node;
543 struct rb_node *parent = NULL;
544 struct binder_node *node;
545
546 while (*p) {
547 parent = *p;
548 node = rb_entry(parent, struct binder_node, rb_node);
549
550 if (ptr < node->ptr)
551 p = &(*p)->rb_left;
552 else if (ptr > node->ptr)
553 p = &(*p)->rb_right;
554 else
555 return NULL;
556 }
557
558 node = kzalloc(sizeof(*node), GFP_KERNEL);
559 if (node == NULL)
560 return NULL;
561 binder_stats_created(BINDER_STAT_NODE);
Todd Kjosf22abc72017-05-09 11:08:05 -0700562 node->tmp_refs++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900563 rb_link_node(&node->rb_node, parent, p);
564 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -0700565 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900566 node->proc = proc;
567 node->ptr = ptr;
568 node->cookie = cookie;
569 node->work.type = BINDER_WORK_NODE;
570 INIT_LIST_HEAD(&node->work.entry);
571 INIT_LIST_HEAD(&node->async_todo);
572 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800573 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900574 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800575 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900576 return node;
577}
578
579static int binder_inc_node(struct binder_node *node, int strong, int internal,
580 struct list_head *target_list)
581{
582 if (strong) {
583 if (internal) {
584 if (target_list == NULL &&
585 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200586 !(node->proc &&
587 node == node->proc->context->
588 binder_context_mgr_node &&
589 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530590 pr_err("invalid inc strong node for %d\n",
591 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900592 return -EINVAL;
593 }
594 node->internal_strong_refs++;
595 } else
596 node->local_strong_refs++;
597 if (!node->has_strong_ref && target_list) {
598 list_del_init(&node->work.entry);
599 list_add_tail(&node->work.entry, target_list);
600 }
601 } else {
602 if (!internal)
603 node->local_weak_refs++;
604 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
605 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530606 pr_err("invalid inc weak node for %d\n",
607 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900608 return -EINVAL;
609 }
610 list_add_tail(&node->work.entry, target_list);
611 }
612 }
613 return 0;
614}
615
616static int binder_dec_node(struct binder_node *node, int strong, int internal)
617{
618 if (strong) {
619 if (internal)
620 node->internal_strong_refs--;
621 else
622 node->local_strong_refs--;
623 if (node->local_strong_refs || node->internal_strong_refs)
624 return 0;
625 } else {
626 if (!internal)
627 node->local_weak_refs--;
Todd Kjosf22abc72017-05-09 11:08:05 -0700628 if (node->local_weak_refs || node->tmp_refs ||
629 !hlist_empty(&node->refs))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900630 return 0;
631 }
632 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
633 if (list_empty(&node->work.entry)) {
634 list_add_tail(&node->work.entry, &node->proc->todo);
635 wake_up_interruptible(&node->proc->wait);
636 }
637 } else {
638 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
Todd Kjosf22abc72017-05-09 11:08:05 -0700639 !node->local_weak_refs && !node->tmp_refs) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900640 list_del_init(&node->work.entry);
641 if (node->proc) {
642 rb_erase(&node->rb_node, &node->proc->nodes);
643 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530644 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900645 node->debug_id);
646 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -0700647 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900648 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -0700649 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900650 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530651 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900652 node->debug_id);
653 }
654 kfree(node);
655 binder_stats_deleted(BINDER_STAT_NODE);
656 }
657 }
658
659 return 0;
660}
661
Todd Kjosf22abc72017-05-09 11:08:05 -0700662/**
663 * binder_inc_node_tmpref() - take a temporary reference on node
664 * @node: node to reference
665 *
666 * Take reference on node to prevent the node from being freed
667 * while referenced only by a local variable
668 */
669static void binder_inc_node_tmpref(struct binder_node *node)
670{
671 /*
672 * No call to binder_inc_node() is needed since we
673 * don't need to inform userspace of any changes to
674 * tmp_refs
675 */
676 node->tmp_refs++;
677}
678
679/**
680 * binder_dec_node_tmpref() - remove a temporary reference on node
681 * @node: node to reference
682 *
683 * Release temporary reference on node taken via binder_inc_node_tmpref()
684 */
685static void binder_dec_node_tmpref(struct binder_node *node)
686{
687 node->tmp_refs--;
688 BUG_ON(node->tmp_refs < 0);
689 /*
690 * Call binder_dec_node() to check if all refcounts are 0
691 * and cleanup is needed. Calling with strong=0 and internal=1
692 * causes no actual reference to be released in binder_dec_node().
693 * If that changes, a change is needed here too.
694 */
695 binder_dec_node(node, 0, 1);
696}
697
698static void binder_put_node(struct binder_node *node)
699{
700 binder_dec_node_tmpref(node);
701}
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900702
703static struct binder_ref *binder_get_ref(struct binder_proc *proc,
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200704 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900705{
706 struct rb_node *n = proc->refs_by_desc.rb_node;
707 struct binder_ref *ref;
708
709 while (n) {
710 ref = rb_entry(n, struct binder_ref, rb_node_desc);
711
Todd Kjosb0117bb2017-05-08 09:16:27 -0700712 if (desc < ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900713 n = n->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -0700714 } else if (desc > ref->data.desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900715 n = n->rb_right;
Todd Kjosb0117bb2017-05-08 09:16:27 -0700716 } else if (need_strong_ref && !ref->data.strong) {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200717 binder_user_error("tried to use weak ref as strong ref\n");
718 return NULL;
719 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900720 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200721 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900722 }
723 return NULL;
724}
725
Todd Kjosb0117bb2017-05-08 09:16:27 -0700726/**
727 * binder_get_ref_for_node() - get the ref associated with given node
728 * @proc: binder_proc that owns the ref
729 * @node: binder_node of target
730 * @new_ref: newly allocated binder_ref to be initialized or %NULL
731 *
732 * Look up the ref for the given node and return it if it exists
733 *
734 * If it doesn't exist and the caller provides a newly allocated
735 * ref, initialize the fields of the newly allocated ref and insert
736 * into the given proc rb_trees and node refs list.
737 *
738 * Return: the ref for node. It is possible that another thread
739 * allocated/initialized the ref first in which case the
740 * returned ref would be different than the passed-in
741 * new_ref. new_ref must be kfree'd by the caller in
742 * this case.
743 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900744static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
Todd Kjosb0117bb2017-05-08 09:16:27 -0700745 struct binder_node *node,
746 struct binder_ref *new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900747{
Todd Kjosb0117bb2017-05-08 09:16:27 -0700748 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900749 struct rb_node **p = &proc->refs_by_node.rb_node;
750 struct rb_node *parent = NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -0700751 struct binder_ref *ref;
752 struct rb_node *n;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900753
754 while (*p) {
755 parent = *p;
756 ref = rb_entry(parent, struct binder_ref, rb_node_node);
757
758 if (node < ref->node)
759 p = &(*p)->rb_left;
760 else if (node > ref->node)
761 p = &(*p)->rb_right;
762 else
763 return ref;
764 }
Todd Kjosb0117bb2017-05-08 09:16:27 -0700765 if (!new_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900766 return NULL;
Todd Kjosb0117bb2017-05-08 09:16:27 -0700767
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900768 binder_stats_created(BINDER_STAT_REF);
Todd Kjosb0117bb2017-05-08 09:16:27 -0700769 new_ref->data.debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900770 new_ref->proc = proc;
771 new_ref->node = node;
772 rb_link_node(&new_ref->rb_node_node, parent, p);
773 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
774
Todd Kjosb0117bb2017-05-08 09:16:27 -0700775 new_ref->data.desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900776 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
777 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Todd Kjosb0117bb2017-05-08 09:16:27 -0700778 if (ref->data.desc > new_ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900779 break;
Todd Kjosb0117bb2017-05-08 09:16:27 -0700780 new_ref->data.desc = ref->data.desc + 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900781 }
782
783 p = &proc->refs_by_desc.rb_node;
784 while (*p) {
785 parent = *p;
786 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
787
Todd Kjosb0117bb2017-05-08 09:16:27 -0700788 if (new_ref->data.desc < ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900789 p = &(*p)->rb_left;
Todd Kjosb0117bb2017-05-08 09:16:27 -0700790 else if (new_ref->data.desc > ref->data.desc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900791 p = &(*p)->rb_right;
792 else
793 BUG();
794 }
795 rb_link_node(&new_ref->rb_node_desc, parent, p);
796 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjos4cbe5752017-05-01 17:21:51 -0700797 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900798
Todd Kjos4cbe5752017-05-01 17:21:51 -0700799 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
800 "%d new ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -0700801 proc->pid, new_ref->data.debug_id, new_ref->data.desc,
Todd Kjos4cbe5752017-05-01 17:21:51 -0700802 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900803 return new_ref;
804}
805
Todd Kjosb0117bb2017-05-08 09:16:27 -0700806static void binder_cleanup_ref(struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900807{
808 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530809 "%d delete ref %d desc %d for node %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -0700810 ref->proc->pid, ref->data.debug_id, ref->data.desc,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530811 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900812
813 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
814 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
Todd Kjosb0117bb2017-05-08 09:16:27 -0700815
816 if (ref->data.strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900817 binder_dec_node(ref->node, 1, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -0700818
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900819 hlist_del(&ref->node_entry);
820 binder_dec_node(ref->node, 0, 1);
Todd Kjosb0117bb2017-05-08 09:16:27 -0700821
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900822 if (ref->death) {
823 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530824 "%d delete ref %d desc %d has death notification\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -0700825 ref->proc->pid, ref->data.debug_id,
826 ref->data.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900827 list_del(&ref->death->work.entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900828 binder_stats_deleted(BINDER_STAT_DEATH);
829 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900830 binder_stats_deleted(BINDER_STAT_REF);
831}
832
Todd Kjosb0117bb2017-05-08 09:16:27 -0700833/**
834 * binder_inc_ref() - increment the ref for given handle
835 * @ref: ref to be incremented
836 * @strong: if true, strong increment, else weak
837 * @target_list: list to queue node work on
838 *
839 * Increment the ref.
840 *
841 * Return: 0, if successful, else errno
842 */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900843static int binder_inc_ref(struct binder_ref *ref, int strong,
844 struct list_head *target_list)
845{
846 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900847
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900848 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -0700849 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900850 ret = binder_inc_node(ref->node, 1, 1, target_list);
851 if (ret)
852 return ret;
853 }
Todd Kjosb0117bb2017-05-08 09:16:27 -0700854 ref->data.strong++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900855 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -0700856 if (ref->data.weak == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900857 ret = binder_inc_node(ref->node, 0, 1, target_list);
858 if (ret)
859 return ret;
860 }
Todd Kjosb0117bb2017-05-08 09:16:27 -0700861 ref->data.weak++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900862 }
863 return 0;
864}
865
Todd Kjosb0117bb2017-05-08 09:16:27 -0700866/**
867 * binder_dec_ref() - dec the ref for given handle
868 * @ref: ref to be decremented
869 * @strong: if true, strong decrement, else weak
870 *
871 * Decrement the ref.
872 *
873 * TODO: kfree is avoided here since an upcoming patch
874 * will put this under a lock.
875 *
876 * Return: true if ref is cleaned up and ready to be freed
877 */
878static bool binder_dec_ref(struct binder_ref *ref, int strong)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900879{
880 if (strong) {
Todd Kjosb0117bb2017-05-08 09:16:27 -0700881 if (ref->data.strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530882 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -0700883 ref->proc->pid, ref->data.debug_id,
884 ref->data.desc, ref->data.strong,
885 ref->data.weak);
886 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900887 }
Todd Kjosb0117bb2017-05-08 09:16:27 -0700888 ref->data.strong--;
889 if (ref->data.strong == 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900890 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900891
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900892 ret = binder_dec_node(ref->node, strong, 1);
893 if (ret)
Todd Kjosb0117bb2017-05-08 09:16:27 -0700894 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900895 }
896 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -0700897 if (ref->data.weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530898 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -0700899 ref->proc->pid, ref->data.debug_id,
900 ref->data.desc, ref->data.strong,
901 ref->data.weak);
902 return false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900903 }
Todd Kjosb0117bb2017-05-08 09:16:27 -0700904 ref->data.weak--;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900905 }
Todd Kjosb0117bb2017-05-08 09:16:27 -0700906 if (ref->data.strong == 0 && ref->data.weak == 0) {
907 binder_cleanup_ref(ref);
908 /*
909 * TODO: we could kfree(ref) here, but an upcoming
910 * patch will call this with a lock held, so we
911 * return an indication that the ref should be
912 * freed.
913 */
914 return true;
915 }
916 return false;
917}
918
919/**
920 * binder_get_node_from_ref() - get the node from the given proc/desc
921 * @proc: proc containing the ref
922 * @desc: the handle associated with the ref
923 * @need_strong_ref: if true, only return node if ref is strong
924 * @rdata: the id/refcount data for the ref
925 *
926 * Given a proc and ref handle, return the associated binder_node
927 *
928 * Return: a binder_node or NULL if not found or not strong when strong required
929 */
930static struct binder_node *binder_get_node_from_ref(
931 struct binder_proc *proc,
932 u32 desc, bool need_strong_ref,
933 struct binder_ref_data *rdata)
934{
935 struct binder_node *node;
936 struct binder_ref *ref;
937
938 ref = binder_get_ref(proc, desc, need_strong_ref);
939 if (!ref)
940 goto err_no_ref;
941 node = ref->node;
Todd Kjosf22abc72017-05-09 11:08:05 -0700942 /*
943 * Take an implicit reference on the node to ensure
944 * it stays alive until the call to binder_put_node()
945 */
946 binder_inc_node_tmpref(node);
Todd Kjosb0117bb2017-05-08 09:16:27 -0700947 if (rdata)
948 *rdata = ref->data;
949
950 return node;
951
952err_no_ref:
953 return NULL;
954}
955
956/**
957 * binder_free_ref() - free the binder_ref
958 * @ref: ref to free
959 *
960 * Free the binder_ref and the binder_ref_death indicated by ref->death.
961 */
962static void binder_free_ref(struct binder_ref *ref)
963{
964 kfree(ref->death);
965 kfree(ref);
966}
967
968/**
969 * binder_update_ref_for_handle() - inc/dec the ref for given handle
970 * @proc: proc containing the ref
971 * @desc: the handle associated with the ref
972 * @increment: true=inc reference, false=dec reference
973 * @strong: true=strong reference, false=weak reference
974 * @rdata: the id/refcount data for the ref
975 *
976 * Given a proc and ref handle, increment or decrement the ref
977 * according to "increment" arg.
978 *
979 * Return: 0 if successful, else errno
980 */
981static int binder_update_ref_for_handle(struct binder_proc *proc,
982 uint32_t desc, bool increment, bool strong,
983 struct binder_ref_data *rdata)
984{
985 int ret = 0;
986 struct binder_ref *ref;
987 bool delete_ref = false;
988
989 ref = binder_get_ref(proc, desc, strong);
990 if (!ref) {
991 ret = -EINVAL;
992 goto err_no_ref;
993 }
994 if (increment)
995 ret = binder_inc_ref(ref, strong, NULL);
996 else
997 delete_ref = binder_dec_ref(ref, strong);
998
999 if (rdata)
1000 *rdata = ref->data;
1001
1002 if (delete_ref)
1003 binder_free_ref(ref);
1004 return ret;
1005
1006err_no_ref:
1007 return ret;
1008}
1009
1010/**
1011 * binder_dec_ref_for_handle() - dec the ref for given handle
1012 * @proc: proc containing the ref
1013 * @desc: the handle associated with the ref
1014 * @strong: true=strong reference, false=weak reference
1015 * @rdata: the id/refcount data for the ref
1016 *
1017 * Just calls binder_update_ref_for_handle() to decrement the ref.
1018 *
1019 * Return: 0 if successful, else errno
1020 */
1021static int binder_dec_ref_for_handle(struct binder_proc *proc,
1022 uint32_t desc, bool strong, struct binder_ref_data *rdata)
1023{
1024 return binder_update_ref_for_handle(proc, desc, false, strong, rdata);
1025}
1026
1027
1028/**
1029 * binder_inc_ref_for_node() - increment the ref for given proc/node
1030 * @proc: proc containing the ref
1031 * @node: target node
1032 * @strong: true=strong reference, false=weak reference
1033 * @target_list: worklist to use if node is incremented
1034 * @rdata: the id/refcount data for the ref
1035 *
1036 * Given a proc and node, increment the ref. Create the ref if it
1037 * doesn't already exist
1038 *
1039 * Return: 0 if successful, else errno
1040 */
1041static int binder_inc_ref_for_node(struct binder_proc *proc,
1042 struct binder_node *node,
1043 bool strong,
1044 struct list_head *target_list,
1045 struct binder_ref_data *rdata)
1046{
1047 struct binder_ref *ref;
1048 struct binder_ref *new_ref = NULL;
1049 int ret = 0;
1050
1051 ref = binder_get_ref_for_node(proc, node, NULL);
1052 if (!ref) {
1053 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1054 if (!new_ref)
1055 return -ENOMEM;
1056 ref = binder_get_ref_for_node(proc, node, new_ref);
1057 }
1058 ret = binder_inc_ref(ref, strong, target_list);
1059 *rdata = ref->data;
1060 if (new_ref && ref != new_ref)
1061 /*
1062 * Another thread created the ref first so
1063 * free the one we allocated
1064 */
1065 kfree(new_ref);
1066 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001067}
1068
1069static void binder_pop_transaction(struct binder_thread *target_thread,
1070 struct binder_transaction *t)
1071{
Todd Kjos21ef40a2017-03-30 18:02:13 -07001072 BUG_ON(!target_thread);
1073 BUG_ON(target_thread->transaction_stack != t);
1074 BUG_ON(target_thread->transaction_stack->from != target_thread);
1075 target_thread->transaction_stack =
1076 target_thread->transaction_stack->from_parent;
1077 t->from = NULL;
1078}
1079
Todd Kjos2f993e22017-05-12 14:42:55 -07001080/**
1081 * binder_thread_dec_tmpref() - decrement thread->tmp_ref
1082 * @thread: thread to decrement
1083 *
1084 * A thread needs to be kept alive while being used to create or
1085 * handle a transaction. binder_get_txn_from() is used to safely
1086 * extract t->from from a binder_transaction and keep the thread
1087 * indicated by t->from from being freed. When done with that
1088 * binder_thread, this function is called to decrement the
1089 * tmp_ref and free if appropriate (thread has been released
1090 * and no transaction being processed by the driver)
1091 */
1092static void binder_thread_dec_tmpref(struct binder_thread *thread)
1093{
1094 /*
1095 * atomic is used to protect the counter value while
1096 * it cannot reach zero or thread->is_dead is false
1097 *
1098 * TODO: future patch adds locking to ensure that the
1099 * check of tmp_ref and is_dead is done with a lock held
1100 */
1101 atomic_dec(&thread->tmp_ref);
1102 if (thread->is_dead && !atomic_read(&thread->tmp_ref)) {
1103 binder_free_thread(thread);
1104 return;
1105 }
1106}
1107
1108/**
1109 * binder_proc_dec_tmpref() - decrement proc->tmp_ref
1110 * @proc: proc to decrement
1111 *
1112 * A binder_proc needs to be kept alive while being used to create or
1113 * handle a transaction. proc->tmp_ref is incremented when
1114 * creating a new transaction or the binder_proc is currently in-use
1115 * by threads that are being released. When done with the binder_proc,
1116 * this function is called to decrement the counter and free the
1117 * proc if appropriate (proc has been released, all threads have
1118 * been released and not currenly in-use to process a transaction).
1119 */
1120static void binder_proc_dec_tmpref(struct binder_proc *proc)
1121{
1122 proc->tmp_ref--;
1123 if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) &&
1124 !proc->tmp_ref) {
1125 binder_free_proc(proc);
1126 return;
1127 }
1128}
1129
1130/**
1131 * binder_get_txn_from() - safely extract the "from" thread in transaction
1132 * @t: binder transaction for t->from
1133 *
1134 * Atomically return the "from" thread and increment the tmp_ref
1135 * count for the thread to ensure it stays alive until
1136 * binder_thread_dec_tmpref() is called.
1137 *
1138 * Return: the value of t->from
1139 */
1140static struct binder_thread *binder_get_txn_from(
1141 struct binder_transaction *t)
1142{
1143 struct binder_thread *from;
1144
1145 spin_lock(&t->lock);
1146 from = t->from;
1147 if (from)
1148 atomic_inc(&from->tmp_ref);
1149 spin_unlock(&t->lock);
1150 return from;
1151}
1152
Todd Kjos21ef40a2017-03-30 18:02:13 -07001153static void binder_free_transaction(struct binder_transaction *t)
1154{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001155 if (t->buffer)
1156 t->buffer->transaction = NULL;
1157 kfree(t);
1158 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1159}
1160
1161static void binder_send_failed_reply(struct binder_transaction *t,
1162 uint32_t error_code)
1163{
1164 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001165 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001166
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001167 BUG_ON(t->flags & TF_ONE_WAY);
1168 while (1) {
Todd Kjos2f993e22017-05-12 14:42:55 -07001169 target_thread = binder_get_txn_from(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001170 if (target_thread) {
Todd Kjos858b8da2017-04-21 17:35:12 -07001171 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1172 "send failed reply for transaction %d to %d:%d\n",
1173 t->debug_id,
1174 target_thread->proc->pid,
1175 target_thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001176
Todd Kjos858b8da2017-04-21 17:35:12 -07001177 binder_pop_transaction(target_thread, t);
1178 if (target_thread->reply_error.cmd == BR_OK) {
1179 target_thread->reply_error.cmd = error_code;
1180 list_add_tail(
1181 &target_thread->reply_error.work.entry,
1182 &target_thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001183 wake_up_interruptible(&target_thread->wait);
1184 } else {
Todd Kjos858b8da2017-04-21 17:35:12 -07001185 WARN(1, "Unexpected reply error: %u\n",
1186 target_thread->reply_error.cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001187 }
Todd Kjos2f993e22017-05-12 14:42:55 -07001188 binder_thread_dec_tmpref(target_thread);
Todd Kjos858b8da2017-04-21 17:35:12 -07001189 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001190 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001191 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001192 next = t->from_parent;
1193
1194 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1195 "send failed reply for transaction %d, target dead\n",
1196 t->debug_id);
1197
Todd Kjos21ef40a2017-03-30 18:02:13 -07001198 binder_free_transaction(t);
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001199 if (next == NULL) {
1200 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1201 "reply failed, no target thread at root\n");
1202 return;
1203 }
1204 t = next;
1205 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1206 "reply failed, no target thread -- retry %d\n",
1207 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001208 }
1209}
1210
Martijn Coenen00c80372016-07-13 12:06:49 +02001211/**
1212 * binder_validate_object() - checks for a valid metadata object in a buffer.
1213 * @buffer: binder_buffer that we're parsing.
1214 * @offset: offset in the buffer at which to validate an object.
1215 *
1216 * Return: If there's a valid metadata object at @offset in @buffer, the
1217 * size of that object. Otherwise, it returns zero.
1218 */
1219static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
1220{
1221 /* Check if we can read a header first */
1222 struct binder_object_header *hdr;
1223 size_t object_size = 0;
1224
1225 if (offset > buffer->data_size - sizeof(*hdr) ||
1226 buffer->data_size < sizeof(*hdr) ||
1227 !IS_ALIGNED(offset, sizeof(u32)))
1228 return 0;
1229
1230 /* Ok, now see if we can read a complete object. */
1231 hdr = (struct binder_object_header *)(buffer->data + offset);
1232 switch (hdr->type) {
1233 case BINDER_TYPE_BINDER:
1234 case BINDER_TYPE_WEAK_BINDER:
1235 case BINDER_TYPE_HANDLE:
1236 case BINDER_TYPE_WEAK_HANDLE:
1237 object_size = sizeof(struct flat_binder_object);
1238 break;
1239 case BINDER_TYPE_FD:
1240 object_size = sizeof(struct binder_fd_object);
1241 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001242 case BINDER_TYPE_PTR:
1243 object_size = sizeof(struct binder_buffer_object);
1244 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001245 case BINDER_TYPE_FDA:
1246 object_size = sizeof(struct binder_fd_array_object);
1247 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02001248 default:
1249 return 0;
1250 }
1251 if (offset <= buffer->data_size - object_size &&
1252 buffer->data_size >= object_size)
1253 return object_size;
1254 else
1255 return 0;
1256}
1257
Martijn Coenen5a6da532016-09-30 14:10:07 +02001258/**
1259 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
1260 * @b: binder_buffer containing the object
1261 * @index: index in offset array at which the binder_buffer_object is
1262 * located
1263 * @start: points to the start of the offset array
1264 * @num_valid: the number of valid offsets in the offset array
1265 *
1266 * Return: If @index is within the valid range of the offset array
1267 * described by @start and @num_valid, and if there's a valid
1268 * binder_buffer_object at the offset found in index @index
1269 * of the offset array, that object is returned. Otherwise,
1270 * %NULL is returned.
1271 * Note that the offset found in index @index itself is not
1272 * verified; this function assumes that @num_valid elements
1273 * from @start were previously verified to have valid offsets.
1274 */
1275static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
1276 binder_size_t index,
1277 binder_size_t *start,
1278 binder_size_t num_valid)
1279{
1280 struct binder_buffer_object *buffer_obj;
1281 binder_size_t *offp;
1282
1283 if (index >= num_valid)
1284 return NULL;
1285
1286 offp = start + index;
1287 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
1288 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
1289 return NULL;
1290
1291 return buffer_obj;
1292}
1293
1294/**
1295 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
1296 * @b: transaction buffer
1297 * @objects_start start of objects buffer
1298 * @buffer: binder_buffer_object in which to fix up
1299 * @offset: start offset in @buffer to fix up
1300 * @last_obj: last binder_buffer_object that we fixed up in
1301 * @last_min_offset: minimum fixup offset in @last_obj
1302 *
1303 * Return: %true if a fixup in buffer @buffer at offset @offset is
1304 * allowed.
1305 *
1306 * For safety reasons, we only allow fixups inside a buffer to happen
1307 * at increasing offsets; additionally, we only allow fixup on the last
1308 * buffer object that was verified, or one of its parents.
1309 *
1310 * Example of what is allowed:
1311 *
1312 * A
1313 * B (parent = A, offset = 0)
1314 * C (parent = A, offset = 16)
1315 * D (parent = C, offset = 0)
1316 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
1317 *
1318 * Examples of what is not allowed:
1319 *
1320 * Decreasing offsets within the same parent:
1321 * A
1322 * C (parent = A, offset = 16)
1323 * B (parent = A, offset = 0) // decreasing offset within A
1324 *
1325 * Referring to a parent that wasn't the last object or any of its parents:
1326 * A
1327 * B (parent = A, offset = 0)
1328 * C (parent = A, offset = 0)
1329 * C (parent = A, offset = 16)
1330 * D (parent = B, offset = 0) // B is not A or any of A's parents
1331 */
1332static bool binder_validate_fixup(struct binder_buffer *b,
1333 binder_size_t *objects_start,
1334 struct binder_buffer_object *buffer,
1335 binder_size_t fixup_offset,
1336 struct binder_buffer_object *last_obj,
1337 binder_size_t last_min_offset)
1338{
1339 if (!last_obj) {
1340 /* Nothing to fix up in */
1341 return false;
1342 }
1343
1344 while (last_obj != buffer) {
1345 /*
1346 * Safe to retrieve the parent of last_obj, since it
1347 * was already previously verified by the driver.
1348 */
1349 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
1350 return false;
1351 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
1352 last_obj = (struct binder_buffer_object *)
1353 (b->data + *(objects_start + last_obj->parent));
1354 }
1355 return (fixup_offset >= last_min_offset);
1356}
1357
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001358static void binder_transaction_buffer_release(struct binder_proc *proc,
1359 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001360 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001361{
Martijn Coenen5a6da532016-09-30 14:10:07 +02001362 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001363 int debug_id = buffer->debug_id;
1364
1365 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301366 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001367 proc->pid, buffer->debug_id,
1368 buffer->data_size, buffer->offsets_size, failed_at);
1369
1370 if (buffer->target_node)
1371 binder_dec_node(buffer->target_node, 1, 0);
1372
Martijn Coenen5a6da532016-09-30 14:10:07 +02001373 off_start = (binder_size_t *)(buffer->data +
1374 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001375 if (failed_at)
1376 off_end = failed_at;
1377 else
Martijn Coenen5a6da532016-09-30 14:10:07 +02001378 off_end = (void *)off_start + buffer->offsets_size;
1379 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02001380 struct binder_object_header *hdr;
1381 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09001382
Martijn Coenen00c80372016-07-13 12:06:49 +02001383 if (object_size == 0) {
1384 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001385 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001386 continue;
1387 }
Martijn Coenen00c80372016-07-13 12:06:49 +02001388 hdr = (struct binder_object_header *)(buffer->data + *offp);
1389 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001390 case BINDER_TYPE_BINDER:
1391 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02001392 struct flat_binder_object *fp;
1393 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09001394
Martijn Coenen00c80372016-07-13 12:06:49 +02001395 fp = to_flat_binder_object(hdr);
1396 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001397 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001398 pr_err("transaction release %d bad node %016llx\n",
1399 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001400 break;
1401 }
1402 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001403 " node %d u%016llx\n",
1404 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02001405 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
1406 0);
Todd Kjosf22abc72017-05-09 11:08:05 -07001407 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001408 } break;
1409 case BINDER_TYPE_HANDLE:
1410 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02001411 struct flat_binder_object *fp;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001412 struct binder_ref_data rdata;
1413 int ret;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001414
Martijn Coenen00c80372016-07-13 12:06:49 +02001415 fp = to_flat_binder_object(hdr);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001416 ret = binder_dec_ref_for_handle(proc, fp->handle,
1417 hdr->type == BINDER_TYPE_HANDLE, &rdata);
1418
1419 if (ret) {
1420 pr_err("transaction release %d bad handle %d, ret = %d\n",
1421 debug_id, fp->handle, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001422 break;
1423 }
1424 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjosb0117bb2017-05-08 09:16:27 -07001425 " ref %d desc %d\n",
1426 rdata.debug_id, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001427 } break;
1428
Martijn Coenen00c80372016-07-13 12:06:49 +02001429 case BINDER_TYPE_FD: {
1430 struct binder_fd_object *fp = to_binder_fd_object(hdr);
1431
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001432 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02001433 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001434 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02001435 task_close_fd(proc, fp->fd);
1436 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001437 case BINDER_TYPE_PTR:
1438 /*
1439 * Nothing to do here, this will get cleaned up when the
1440 * transaction buffer gets freed
1441 */
1442 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001443 case BINDER_TYPE_FDA: {
1444 struct binder_fd_array_object *fda;
1445 struct binder_buffer_object *parent;
1446 uintptr_t parent_buffer;
1447 u32 *fd_array;
1448 size_t fd_index;
1449 binder_size_t fd_buf_size;
1450
1451 fda = to_binder_fd_array_object(hdr);
1452 parent = binder_validate_ptr(buffer, fda->parent,
1453 off_start,
1454 offp - off_start);
1455 if (!parent) {
1456 pr_err("transaction release %d bad parent offset",
1457 debug_id);
1458 continue;
1459 }
1460 /*
1461 * Since the parent was already fixed up, convert it
1462 * back to kernel address space to access it
1463 */
1464 parent_buffer = parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07001465 binder_alloc_get_user_buffer_offset(
1466 &proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001467
1468 fd_buf_size = sizeof(u32) * fda->num_fds;
1469 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
1470 pr_err("transaction release %d invalid number of fds (%lld)\n",
1471 debug_id, (u64)fda->num_fds);
1472 continue;
1473 }
1474 if (fd_buf_size > parent->length ||
1475 fda->parent_offset > parent->length - fd_buf_size) {
1476 /* No space for all file descriptors here. */
1477 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
1478 debug_id, (u64)fda->num_fds);
1479 continue;
1480 }
1481 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
1482 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
1483 task_close_fd(proc, fd_array[fd_index]);
1484 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001485 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001486 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02001487 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001488 break;
1489 }
1490 }
1491}
1492
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001493static int binder_translate_binder(struct flat_binder_object *fp,
1494 struct binder_transaction *t,
1495 struct binder_thread *thread)
1496{
1497 struct binder_node *node;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001498 struct binder_proc *proc = thread->proc;
1499 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001500 struct binder_ref_data rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07001501 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001502
1503 node = binder_get_node(proc, fp->binder);
1504 if (!node) {
1505 node = binder_new_node(proc, fp->binder, fp->cookie);
1506 if (!node)
1507 return -ENOMEM;
1508
1509 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1510 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1511 }
1512 if (fp->cookie != node->cookie) {
1513 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1514 proc->pid, thread->pid, (u64)fp->binder,
1515 node->debug_id, (u64)fp->cookie,
1516 (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07001517 ret = -EINVAL;
1518 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001519 }
Todd Kjosf22abc72017-05-09 11:08:05 -07001520 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
1521 ret = -EPERM;
1522 goto done;
1523 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001524
Todd Kjosb0117bb2017-05-08 09:16:27 -07001525 ret = binder_inc_ref_for_node(target_proc, node,
1526 fp->hdr.type == BINDER_TYPE_BINDER,
1527 &thread->todo, &rdata);
1528 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07001529 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001530
1531 if (fp->hdr.type == BINDER_TYPE_BINDER)
1532 fp->hdr.type = BINDER_TYPE_HANDLE;
1533 else
1534 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
1535 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001536 fp->handle = rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001537 fp->cookie = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001538
Todd Kjosb0117bb2017-05-08 09:16:27 -07001539 trace_binder_transaction_node_to_ref(t, node, &rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001540 binder_debug(BINDER_DEBUG_TRANSACTION,
1541 " node %d u%016llx -> ref %d desc %d\n",
1542 node->debug_id, (u64)node->ptr,
Todd Kjosb0117bb2017-05-08 09:16:27 -07001543 rdata.debug_id, rdata.desc);
Todd Kjosf22abc72017-05-09 11:08:05 -07001544done:
1545 binder_put_node(node);
1546 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001547}
1548
1549static int binder_translate_handle(struct flat_binder_object *fp,
1550 struct binder_transaction *t,
1551 struct binder_thread *thread)
1552{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001553 struct binder_proc *proc = thread->proc;
1554 struct binder_proc *target_proc = t->to_proc;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001555 struct binder_node *node;
1556 struct binder_ref_data src_rdata;
Todd Kjosf22abc72017-05-09 11:08:05 -07001557 int ret = 0;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001558
Todd Kjosb0117bb2017-05-08 09:16:27 -07001559 node = binder_get_node_from_ref(proc, fp->handle,
1560 fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata);
1561 if (!node) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001562 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
1563 proc->pid, thread->pid, fp->handle);
1564 return -EINVAL;
1565 }
Todd Kjosf22abc72017-05-09 11:08:05 -07001566 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) {
1567 ret = -EPERM;
1568 goto done;
1569 }
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001570
Todd Kjosb0117bb2017-05-08 09:16:27 -07001571 if (node->proc == target_proc) {
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001572 if (fp->hdr.type == BINDER_TYPE_HANDLE)
1573 fp->hdr.type = BINDER_TYPE_BINDER;
1574 else
1575 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001576 fp->binder = node->ptr;
1577 fp->cookie = node->cookie;
1578 binder_inc_node(node,
1579 fp->hdr.type == BINDER_TYPE_BINDER,
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001580 0, NULL);
Todd Kjosb0117bb2017-05-08 09:16:27 -07001581 trace_binder_transaction_ref_to_node(t, node, &src_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001582 binder_debug(BINDER_DEBUG_TRANSACTION,
1583 " ref %d desc %d -> node %d u%016llx\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001584 src_rdata.debug_id, src_rdata.desc, node->debug_id,
1585 (u64)node->ptr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001586 } else {
Todd Kjosb0117bb2017-05-08 09:16:27 -07001587 int ret;
1588 struct binder_ref_data dest_rdata;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001589
Todd Kjosb0117bb2017-05-08 09:16:27 -07001590 ret = binder_inc_ref_for_node(target_proc, node,
1591 fp->hdr.type == BINDER_TYPE_HANDLE,
1592 NULL, &dest_rdata);
1593 if (ret)
Todd Kjosf22abc72017-05-09 11:08:05 -07001594 goto done;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001595
1596 fp->binder = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001597 fp->handle = dest_rdata.desc;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001598 fp->cookie = 0;
Todd Kjosb0117bb2017-05-08 09:16:27 -07001599 trace_binder_transaction_ref_to_ref(t, node, &src_rdata,
1600 &dest_rdata);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001601 binder_debug(BINDER_DEBUG_TRANSACTION,
1602 " ref %d desc %d -> ref %d desc %d (node %d)\n",
Todd Kjosb0117bb2017-05-08 09:16:27 -07001603 src_rdata.debug_id, src_rdata.desc,
1604 dest_rdata.debug_id, dest_rdata.desc,
1605 node->debug_id);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001606 }
Todd Kjosf22abc72017-05-09 11:08:05 -07001607done:
1608 binder_put_node(node);
1609 return ret;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001610}
1611
1612static int binder_translate_fd(int fd,
1613 struct binder_transaction *t,
1614 struct binder_thread *thread,
1615 struct binder_transaction *in_reply_to)
1616{
1617 struct binder_proc *proc = thread->proc;
1618 struct binder_proc *target_proc = t->to_proc;
1619 int target_fd;
1620 struct file *file;
1621 int ret;
1622 bool target_allows_fd;
1623
1624 if (in_reply_to)
1625 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
1626 else
1627 target_allows_fd = t->buffer->target_node->accept_fds;
1628 if (!target_allows_fd) {
1629 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
1630 proc->pid, thread->pid,
1631 in_reply_to ? "reply" : "transaction",
1632 fd);
1633 ret = -EPERM;
1634 goto err_fd_not_accepted;
1635 }
1636
1637 file = fget(fd);
1638 if (!file) {
1639 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1640 proc->pid, thread->pid, fd);
1641 ret = -EBADF;
1642 goto err_fget;
1643 }
1644 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
1645 if (ret < 0) {
1646 ret = -EPERM;
1647 goto err_security;
1648 }
1649
1650 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1651 if (target_fd < 0) {
1652 ret = -ENOMEM;
1653 goto err_get_unused_fd;
1654 }
1655 task_fd_install(target_proc, target_fd, file);
1656 trace_binder_transaction_fd(t, fd, target_fd);
1657 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
1658 fd, target_fd);
1659
1660 return target_fd;
1661
1662err_get_unused_fd:
1663err_security:
1664 fput(file);
1665err_fget:
1666err_fd_not_accepted:
1667 return ret;
1668}
1669
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001670static int binder_translate_fd_array(struct binder_fd_array_object *fda,
1671 struct binder_buffer_object *parent,
1672 struct binder_transaction *t,
1673 struct binder_thread *thread,
1674 struct binder_transaction *in_reply_to)
1675{
1676 binder_size_t fdi, fd_buf_size, num_installed_fds;
1677 int target_fd;
1678 uintptr_t parent_buffer;
1679 u32 *fd_array;
1680 struct binder_proc *proc = thread->proc;
1681 struct binder_proc *target_proc = t->to_proc;
1682
1683 fd_buf_size = sizeof(u32) * fda->num_fds;
1684 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
1685 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
1686 proc->pid, thread->pid, (u64)fda->num_fds);
1687 return -EINVAL;
1688 }
1689 if (fd_buf_size > parent->length ||
1690 fda->parent_offset > parent->length - fd_buf_size) {
1691 /* No space for all file descriptors here. */
1692 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
1693 proc->pid, thread->pid, (u64)fda->num_fds);
1694 return -EINVAL;
1695 }
1696 /*
1697 * Since the parent was already fixed up, convert it
1698 * back to the kernel address space to access it
1699 */
Todd Kjosd325d372016-10-10 10:40:53 -07001700 parent_buffer = parent->buffer -
1701 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001702 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
1703 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
1704 binder_user_error("%d:%d parent offset not aligned correctly.\n",
1705 proc->pid, thread->pid);
1706 return -EINVAL;
1707 }
1708 for (fdi = 0; fdi < fda->num_fds; fdi++) {
1709 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
1710 in_reply_to);
1711 if (target_fd < 0)
1712 goto err_translate_fd_failed;
1713 fd_array[fdi] = target_fd;
1714 }
1715 return 0;
1716
1717err_translate_fd_failed:
1718 /*
1719 * Failed to allocate fd or security error, free fds
1720 * installed so far.
1721 */
1722 num_installed_fds = fdi;
1723 for (fdi = 0; fdi < num_installed_fds; fdi++)
1724 task_close_fd(target_proc, fd_array[fdi]);
1725 return target_fd;
1726}
1727
Martijn Coenen5a6da532016-09-30 14:10:07 +02001728static int binder_fixup_parent(struct binder_transaction *t,
1729 struct binder_thread *thread,
1730 struct binder_buffer_object *bp,
1731 binder_size_t *off_start,
1732 binder_size_t num_valid,
1733 struct binder_buffer_object *last_fixup_obj,
1734 binder_size_t last_fixup_min_off)
1735{
1736 struct binder_buffer_object *parent;
1737 u8 *parent_buffer;
1738 struct binder_buffer *b = t->buffer;
1739 struct binder_proc *proc = thread->proc;
1740 struct binder_proc *target_proc = t->to_proc;
1741
1742 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
1743 return 0;
1744
1745 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
1746 if (!parent) {
1747 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
1748 proc->pid, thread->pid);
1749 return -EINVAL;
1750 }
1751
1752 if (!binder_validate_fixup(b, off_start,
1753 parent, bp->parent_offset,
1754 last_fixup_obj,
1755 last_fixup_min_off)) {
1756 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
1757 proc->pid, thread->pid);
1758 return -EINVAL;
1759 }
1760
1761 if (parent->length < sizeof(binder_uintptr_t) ||
1762 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
1763 /* No space for a pointer here! */
1764 binder_user_error("%d:%d got transaction with invalid parent offset\n",
1765 proc->pid, thread->pid);
1766 return -EINVAL;
1767 }
1768 parent_buffer = (u8 *)(parent->buffer -
Todd Kjosd325d372016-10-10 10:40:53 -07001769 binder_alloc_get_user_buffer_offset(
1770 &target_proc->alloc));
Martijn Coenen5a6da532016-09-30 14:10:07 +02001771 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
1772
1773 return 0;
1774}
1775
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001776static void binder_transaction(struct binder_proc *proc,
1777 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02001778 struct binder_transaction_data *tr, int reply,
1779 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001780{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001781 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001782 struct binder_transaction *t;
1783 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001784 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001785 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001786 u8 *sg_bufp, *sg_buf_end;
Todd Kjos2f993e22017-05-12 14:42:55 -07001787 struct binder_proc *target_proc = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001788 struct binder_thread *target_thread = NULL;
1789 struct binder_node *target_node = NULL;
1790 struct list_head *target_list;
1791 wait_queue_head_t *target_wait;
1792 struct binder_transaction *in_reply_to = NULL;
1793 struct binder_transaction_log_entry *e;
Todd Kjose598d172017-03-22 17:19:52 -07001794 uint32_t return_error = 0;
1795 uint32_t return_error_param = 0;
1796 uint32_t return_error_line = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001797 struct binder_buffer_object *last_fixup_obj = NULL;
1798 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001799 struct binder_context *context = proc->context;
Todd Kjos1cfe6272017-05-24 13:33:28 -07001800 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001801
1802 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjos1cfe6272017-05-24 13:33:28 -07001803 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001804 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1805 e->from_proc = proc->pid;
1806 e->from_thread = thread->pid;
1807 e->target_handle = tr->target.handle;
1808 e->data_size = tr->data_size;
1809 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02001810 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001811
1812 if (reply) {
1813 in_reply_to = thread->transaction_stack;
1814 if (in_reply_to == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301815 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001816 proc->pid, thread->pid);
1817 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001818 return_error_param = -EPROTO;
1819 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001820 goto err_empty_call_stack;
1821 }
1822 binder_set_nice(in_reply_to->saved_priority);
1823 if (in_reply_to->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07001824 spin_lock(&in_reply_to->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05301825 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 +09001826 proc->pid, thread->pid, in_reply_to->debug_id,
1827 in_reply_to->to_proc ?
1828 in_reply_to->to_proc->pid : 0,
1829 in_reply_to->to_thread ?
1830 in_reply_to->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07001831 spin_unlock(&in_reply_to->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001832 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001833 return_error_param = -EPROTO;
1834 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001835 in_reply_to = NULL;
1836 goto err_bad_call_stack;
1837 }
1838 thread->transaction_stack = in_reply_to->to_parent;
Todd Kjos2f993e22017-05-12 14:42:55 -07001839 target_thread = binder_get_txn_from(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001840 if (target_thread == NULL) {
1841 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001842 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001843 goto err_dead_binder;
1844 }
1845 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301846 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 +09001847 proc->pid, thread->pid,
1848 target_thread->transaction_stack ?
1849 target_thread->transaction_stack->debug_id : 0,
1850 in_reply_to->debug_id);
1851 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001852 return_error_param = -EPROTO;
1853 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001854 in_reply_to = NULL;
1855 target_thread = NULL;
1856 goto err_dead_binder;
1857 }
1858 target_proc = target_thread->proc;
Todd Kjos2f993e22017-05-12 14:42:55 -07001859 target_proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001860 } else {
1861 if (tr->target.handle) {
1862 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001863
Todd Kjosc37162d2017-05-26 11:56:29 -07001864 /*
1865 * There must already be a strong ref
1866 * on this node. If so, do a strong
1867 * increment on the node to ensure it
1868 * stays alive until the transaction is
1869 * done.
1870 */
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001871 ref = binder_get_ref(proc, tr->target.handle, true);
Todd Kjosc37162d2017-05-26 11:56:29 -07001872 if (ref) {
1873 binder_inc_node(ref->node, 1, 0, NULL);
1874 target_node = ref->node;
1875 }
1876 if (target_node == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301877 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001878 proc->pid, thread->pid);
1879 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001880 return_error_param = -EINVAL;
1881 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001882 goto err_invalid_target_handle;
1883 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001884 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001885 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001886 target_node = context->binder_context_mgr_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001887 if (target_node == NULL) {
1888 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001889 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjose598d172017-03-22 17:19:52 -07001890 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001891 goto err_no_context_mgr_node;
1892 }
Todd Kjosc37162d2017-05-26 11:56:29 -07001893 binder_inc_node(target_node, 1, 0, NULL);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001894 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001895 }
1896 e->to_node = target_node->debug_id;
1897 target_proc = target_node->proc;
1898 if (target_proc == NULL) {
1899 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001900 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001901 goto err_dead_binder;
1902 }
Todd Kjos2f993e22017-05-12 14:42:55 -07001903 target_proc->tmp_ref++;
Stephen Smalley79af7302015-01-21 10:54:10 -05001904 if (security_binder_transaction(proc->tsk,
1905 target_proc->tsk) < 0) {
1906 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001907 return_error_param = -EPERM;
1908 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05001909 goto err_invalid_target_handle;
1910 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001911 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1912 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001913
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001914 tmp = thread->transaction_stack;
1915 if (tmp->to_thread != thread) {
Todd Kjos2f993e22017-05-12 14:42:55 -07001916 spin_lock(&tmp->lock);
Anmol Sarma56b468f2012-10-30 22:35:43 +05301917 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 +09001918 proc->pid, thread->pid, tmp->debug_id,
1919 tmp->to_proc ? tmp->to_proc->pid : 0,
1920 tmp->to_thread ?
1921 tmp->to_thread->pid : 0);
Todd Kjos2f993e22017-05-12 14:42:55 -07001922 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001923 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001924 return_error_param = -EPROTO;
1925 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001926 goto err_bad_call_stack;
1927 }
1928 while (tmp) {
Todd Kjos2f993e22017-05-12 14:42:55 -07001929 struct binder_thread *from;
1930
1931 spin_lock(&tmp->lock);
1932 from = tmp->from;
1933 if (from && from->proc == target_proc) {
1934 atomic_inc(&from->tmp_ref);
1935 target_thread = from;
1936 spin_unlock(&tmp->lock);
1937 break;
1938 }
1939 spin_unlock(&tmp->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001940 tmp = tmp->from_parent;
1941 }
1942 }
1943 }
1944 if (target_thread) {
1945 e->to_thread = target_thread->pid;
1946 target_list = &target_thread->todo;
1947 target_wait = &target_thread->wait;
1948 } else {
1949 target_list = &target_proc->todo;
1950 target_wait = &target_proc->wait;
1951 }
1952 e->to_proc = target_proc->pid;
1953
1954 /* TODO: reuse incoming transaction for reply */
1955 t = kzalloc(sizeof(*t), GFP_KERNEL);
1956 if (t == NULL) {
1957 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001958 return_error_param = -ENOMEM;
1959 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001960 goto err_alloc_t_failed;
1961 }
1962 binder_stats_created(BINDER_STAT_TRANSACTION);
Todd Kjos2f993e22017-05-12 14:42:55 -07001963 spin_lock_init(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001964
1965 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1966 if (tcomplete == NULL) {
1967 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001968 return_error_param = -ENOMEM;
1969 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001970 goto err_alloc_tcomplete_failed;
1971 }
1972 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1973
Todd Kjos1cfe6272017-05-24 13:33:28 -07001974 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001975
1976 if (reply)
1977 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02001978 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001979 proc->pid, thread->pid, t->debug_id,
1980 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001981 (u64)tr->data.ptr.buffer,
1982 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02001983 (u64)tr->data_size, (u64)tr->offsets_size,
1984 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001985 else
1986 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02001987 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001988 proc->pid, thread->pid, t->debug_id,
1989 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001990 (u64)tr->data.ptr.buffer,
1991 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02001992 (u64)tr->data_size, (u64)tr->offsets_size,
1993 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001994
1995 if (!reply && !(tr->flags & TF_ONE_WAY))
1996 t->from = thread;
1997 else
1998 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03001999 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002000 t->to_proc = target_proc;
2001 t->to_thread = target_thread;
2002 t->code = tr->code;
2003 t->flags = tr->flags;
2004 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002005
2006 trace_binder_transaction(reply, t, target_node);
2007
Todd Kjosd325d372016-10-10 10:40:53 -07002008 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02002009 tr->offsets_size, extra_buffers_size,
2010 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07002011 if (IS_ERR(t->buffer)) {
2012 /*
2013 * -ESRCH indicates VMA cleared. The target is dying.
2014 */
2015 return_error_param = PTR_ERR(t->buffer);
2016 return_error = return_error_param == -ESRCH ?
2017 BR_DEAD_REPLY : BR_FAILED_REPLY;
2018 return_error_line = __LINE__;
2019 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002020 goto err_binder_alloc_buf_failed;
2021 }
2022 t->buffer->allow_user_free = 0;
2023 t->buffer->debug_id = t->debug_id;
2024 t->buffer->transaction = t;
2025 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002026 trace_binder_transaction_alloc_buf(t->buffer);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002027 off_start = (binder_size_t *)(t->buffer->data +
2028 ALIGN(tr->data_size, sizeof(void *)));
2029 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002030
Arve Hjønnevågda498892014-02-21 14:40:26 -08002031 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
2032 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302033 binder_user_error("%d:%d got transaction with invalid data ptr\n",
2034 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002035 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002036 return_error_param = -EFAULT;
2037 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002038 goto err_copy_data_failed;
2039 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002040 if (copy_from_user(offp, (const void __user *)(uintptr_t)
2041 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302042 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2043 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002044 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002045 return_error_param = -EFAULT;
2046 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002047 goto err_copy_data_failed;
2048 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002049 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
2050 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
2051 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002052 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002053 return_error_param = -EINVAL;
2054 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002055 goto err_bad_offset;
2056 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02002057 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
2058 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
2059 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05302060 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002061 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002062 return_error_param = -EINVAL;
2063 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002064 goto err_bad_offset;
2065 }
2066 off_end = (void *)off_start + tr->offsets_size;
2067 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
2068 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002069 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002070 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002071 struct binder_object_header *hdr;
2072 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002073
Martijn Coenen00c80372016-07-13 12:06:49 +02002074 if (object_size == 0 || *offp < off_min) {
2075 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 -08002076 proc->pid, thread->pid, (u64)*offp,
2077 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02002078 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002079 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002080 return_error_param = -EINVAL;
2081 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002082 goto err_bad_offset;
2083 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002084
2085 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
2086 off_min = *offp + object_size;
2087 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002088 case BINDER_TYPE_BINDER:
2089 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002090 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002091
Martijn Coenen00c80372016-07-13 12:06:49 +02002092 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002093 ret = binder_translate_binder(fp, t, thread);
2094 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02002095 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002096 return_error_param = ret;
2097 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002098 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002099 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002100 } break;
2101 case BINDER_TYPE_HANDLE:
2102 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002103 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002104
Martijn Coenen00c80372016-07-13 12:06:49 +02002105 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002106 ret = binder_translate_handle(fp, t, thread);
2107 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002108 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002109 return_error_param = ret;
2110 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002111 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002112 }
2113 } break;
2114
2115 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002116 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002117 int target_fd = binder_translate_fd(fp->fd, t, thread,
2118 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002119
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002120 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002121 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002122 return_error_param = target_fd;
2123 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002124 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002125 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002126 fp->pad_binder = 0;
2127 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002128 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002129 case BINDER_TYPE_FDA: {
2130 struct binder_fd_array_object *fda =
2131 to_binder_fd_array_object(hdr);
2132 struct binder_buffer_object *parent =
2133 binder_validate_ptr(t->buffer, fda->parent,
2134 off_start,
2135 offp - off_start);
2136 if (!parent) {
2137 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2138 proc->pid, thread->pid);
2139 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002140 return_error_param = -EINVAL;
2141 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002142 goto err_bad_parent;
2143 }
2144 if (!binder_validate_fixup(t->buffer, off_start,
2145 parent, fda->parent_offset,
2146 last_fixup_obj,
2147 last_fixup_min_off)) {
2148 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2149 proc->pid, thread->pid);
2150 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002151 return_error_param = -EINVAL;
2152 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002153 goto err_bad_parent;
2154 }
2155 ret = binder_translate_fd_array(fda, parent, t, thread,
2156 in_reply_to);
2157 if (ret < 0) {
2158 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002159 return_error_param = ret;
2160 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002161 goto err_translate_failed;
2162 }
2163 last_fixup_obj = parent;
2164 last_fixup_min_off =
2165 fda->parent_offset + sizeof(u32) * fda->num_fds;
2166 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002167 case BINDER_TYPE_PTR: {
2168 struct binder_buffer_object *bp =
2169 to_binder_buffer_object(hdr);
2170 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002171
Martijn Coenen5a6da532016-09-30 14:10:07 +02002172 if (bp->length > buf_left) {
2173 binder_user_error("%d:%d got transaction with too large buffer\n",
2174 proc->pid, thread->pid);
2175 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002176 return_error_param = -EINVAL;
2177 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002178 goto err_bad_offset;
2179 }
2180 if (copy_from_user(sg_bufp,
2181 (const void __user *)(uintptr_t)
2182 bp->buffer, bp->length)) {
2183 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2184 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07002185 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002186 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002187 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002188 goto err_copy_data_failed;
2189 }
2190 /* Fixup buffer pointer to target proc address space */
2191 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjosd325d372016-10-10 10:40:53 -07002192 binder_alloc_get_user_buffer_offset(
2193 &target_proc->alloc);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002194 sg_bufp += ALIGN(bp->length, sizeof(u64));
2195
2196 ret = binder_fixup_parent(t, thread, bp, off_start,
2197 offp - off_start,
2198 last_fixup_obj,
2199 last_fixup_min_off);
2200 if (ret < 0) {
2201 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002202 return_error_param = ret;
2203 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002204 goto err_translate_failed;
2205 }
2206 last_fixup_obj = bp;
2207 last_fixup_min_off = 0;
2208 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002209 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002210 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002211 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002212 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07002213 return_error_param = -EINVAL;
2214 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002215 goto err_bad_object_type;
2216 }
2217 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07002218 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
2219 list_add_tail(&tcomplete->entry, &thread->todo);
2220
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002221 if (reply) {
Todd Kjos2f993e22017-05-12 14:42:55 -07002222 if (target_thread->is_dead)
2223 goto err_dead_proc_or_thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002224 BUG_ON(t->buffer->async_transaction != 0);
2225 binder_pop_transaction(target_thread, in_reply_to);
Todd Kjos21ef40a2017-03-30 18:02:13 -07002226 binder_free_transaction(in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002227 } else if (!(t->flags & TF_ONE_WAY)) {
2228 BUG_ON(t->buffer->async_transaction != 0);
2229 t->need_reply = 1;
2230 t->from_parent = thread->transaction_stack;
2231 thread->transaction_stack = t;
Todd Kjos2f993e22017-05-12 14:42:55 -07002232 if (target_proc->is_dead ||
2233 (target_thread && target_thread->is_dead)) {
2234 binder_pop_transaction(thread, t);
2235 goto err_dead_proc_or_thread;
2236 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002237 } else {
2238 BUG_ON(target_node == NULL);
2239 BUG_ON(t->buffer->async_transaction != 1);
2240 if (target_node->has_async_transaction) {
2241 target_list = &target_node->async_todo;
2242 target_wait = NULL;
2243 } else
2244 target_node->has_async_transaction = 1;
Todd Kjos2f993e22017-05-12 14:42:55 -07002245 if (target_proc->is_dead ||
2246 (target_thread && target_thread->is_dead))
2247 goto err_dead_proc_or_thread;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002248 }
2249 t->work.type = BINDER_WORK_TRANSACTION;
2250 list_add_tail(&t->work.entry, target_list);
Riley Andrewsb5968812015-09-01 12:42:07 -07002251 if (target_wait) {
Todd Kjos8dedb0c2017-05-09 08:31:32 -07002252 if (reply || !(tr->flags & TF_ONE_WAY))
Riley Andrewsb5968812015-09-01 12:42:07 -07002253 wake_up_interruptible_sync(target_wait);
2254 else
2255 wake_up_interruptible(target_wait);
2256 }
Todd Kjos2f993e22017-05-12 14:42:55 -07002257 if (target_thread)
2258 binder_thread_dec_tmpref(target_thread);
2259 binder_proc_dec_tmpref(target_proc);
Todd Kjos1cfe6272017-05-24 13:33:28 -07002260 /*
2261 * write barrier to synchronize with initialization
2262 * of log entry
2263 */
2264 smp_wmb();
2265 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002266 return;
2267
Todd Kjos2f993e22017-05-12 14:42:55 -07002268err_dead_proc_or_thread:
2269 return_error = BR_DEAD_REPLY;
2270 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002271err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002272err_bad_object_type:
2273err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002274err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002275err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002276 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002277 binder_transaction_buffer_release(target_proc, t->buffer, offp);
Todd Kjosc37162d2017-05-26 11:56:29 -07002278 target_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002279 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07002280 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002281err_binder_alloc_buf_failed:
2282 kfree(tcomplete);
2283 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2284err_alloc_tcomplete_failed:
2285 kfree(t);
2286 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2287err_alloc_t_failed:
2288err_bad_call_stack:
2289err_empty_call_stack:
2290err_dead_binder:
2291err_invalid_target_handle:
2292err_no_context_mgr_node:
Todd Kjos2f993e22017-05-12 14:42:55 -07002293 if (target_thread)
2294 binder_thread_dec_tmpref(target_thread);
2295 if (target_proc)
2296 binder_proc_dec_tmpref(target_proc);
Todd Kjosc37162d2017-05-26 11:56:29 -07002297 if (target_node)
2298 binder_dec_node(target_node, 1, 0);
2299
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002300 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07002301 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
2302 proc->pid, thread->pid, return_error, return_error_param,
2303 (u64)tr->data_size, (u64)tr->offsets_size,
2304 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002305
2306 {
2307 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09002308
Todd Kjose598d172017-03-22 17:19:52 -07002309 e->return_error = return_error;
2310 e->return_error_param = return_error_param;
2311 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002312 fe = binder_transaction_log_add(&binder_transaction_log_failed);
2313 *fe = *e;
Todd Kjos1cfe6272017-05-24 13:33:28 -07002314 /*
2315 * write barrier to synchronize with initialization
2316 * of log entry
2317 */
2318 smp_wmb();
2319 WRITE_ONCE(e->debug_id_done, t_debug_id);
2320 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002321 }
2322
Todd Kjos858b8da2017-04-21 17:35:12 -07002323 BUG_ON(thread->return_error.cmd != BR_OK);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002324 if (in_reply_to) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002325 thread->return_error.cmd = BR_TRANSACTION_COMPLETE;
2326 list_add_tail(&thread->return_error.work.entry,
2327 &thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002328 binder_send_failed_reply(in_reply_to, return_error);
Todd Kjos858b8da2017-04-21 17:35:12 -07002329 } else {
2330 thread->return_error.cmd = return_error;
2331 list_add_tail(&thread->return_error.work.entry,
2332 &thread->todo);
2333 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002334}
2335
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002336static int binder_thread_write(struct binder_proc *proc,
2337 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002338 binder_uintptr_t binder_buffer, size_t size,
2339 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002340{
2341 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002342 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002343 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002344 void __user *ptr = buffer + *consumed;
2345 void __user *end = buffer + size;
2346
Todd Kjos858b8da2017-04-21 17:35:12 -07002347 while (ptr < end && thread->return_error.cmd == BR_OK) {
Todd Kjosb0117bb2017-05-08 09:16:27 -07002348 int ret;
2349
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002350 if (get_user(cmd, (uint32_t __user *)ptr))
2351 return -EFAULT;
2352 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002353 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002354 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07002355 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
2356 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
2357 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002358 }
2359 switch (cmd) {
2360 case BC_INCREFS:
2361 case BC_ACQUIRE:
2362 case BC_RELEASE:
2363 case BC_DECREFS: {
2364 uint32_t target;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002365 const char *debug_string;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002366 bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE;
2367 bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE;
2368 struct binder_ref_data rdata;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002369
2370 if (get_user(target, (uint32_t __user *)ptr))
2371 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002372
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002373 ptr += sizeof(uint32_t);
Todd Kjosb0117bb2017-05-08 09:16:27 -07002374 ret = -1;
2375 if (increment && !target) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002376 struct binder_node *ctx_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002377 mutex_lock(&context->context_mgr_node_lock);
2378 ctx_mgr_node = context->binder_context_mgr_node;
Todd Kjosb0117bb2017-05-08 09:16:27 -07002379 if (ctx_mgr_node)
2380 ret = binder_inc_ref_for_node(
2381 proc, ctx_mgr_node,
2382 strong, NULL, &rdata);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002383 mutex_unlock(&context->context_mgr_node_lock);
2384 }
Todd Kjosb0117bb2017-05-08 09:16:27 -07002385 if (ret)
2386 ret = binder_update_ref_for_handle(
2387 proc, target, increment, strong,
2388 &rdata);
2389 if (!ret && rdata.desc != target) {
2390 binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n",
2391 proc->pid, thread->pid,
2392 target, rdata.desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002393 }
2394 switch (cmd) {
2395 case BC_INCREFS:
2396 debug_string = "IncRefs";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002397 break;
2398 case BC_ACQUIRE:
2399 debug_string = "Acquire";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002400 break;
2401 case BC_RELEASE:
2402 debug_string = "Release";
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002403 break;
2404 case BC_DECREFS:
2405 default:
2406 debug_string = "DecRefs";
Todd Kjosb0117bb2017-05-08 09:16:27 -07002407 break;
2408 }
2409 if (ret) {
2410 binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n",
2411 proc->pid, thread->pid, debug_string,
2412 strong, target, ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002413 break;
2414 }
2415 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosb0117bb2017-05-08 09:16:27 -07002416 "%d:%d %s ref %d desc %d s %d w %d\n",
2417 proc->pid, thread->pid, debug_string,
2418 rdata.debug_id, rdata.desc, rdata.strong,
2419 rdata.weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002420 break;
2421 }
2422 case BC_INCREFS_DONE:
2423 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002424 binder_uintptr_t node_ptr;
2425 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002426 struct binder_node *node;
2427
Arve Hjønnevågda498892014-02-21 14:40:26 -08002428 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002429 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002430 ptr += sizeof(binder_uintptr_t);
2431 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002432 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002433 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002434 node = binder_get_node(proc, node_ptr);
2435 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002436 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002437 proc->pid, thread->pid,
2438 cmd == BC_INCREFS_DONE ?
2439 "BC_INCREFS_DONE" :
2440 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002441 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002442 break;
2443 }
2444 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002445 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002446 proc->pid, thread->pid,
2447 cmd == BC_INCREFS_DONE ?
2448 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002449 (u64)node_ptr, node->debug_id,
2450 (u64)cookie, (u64)node->cookie);
Todd Kjosf22abc72017-05-09 11:08:05 -07002451 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002452 break;
2453 }
2454 if (cmd == BC_ACQUIRE_DONE) {
2455 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302456 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002457 proc->pid, thread->pid,
2458 node->debug_id);
Todd Kjosf22abc72017-05-09 11:08:05 -07002459 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002460 break;
2461 }
2462 node->pending_strong_ref = 0;
2463 } else {
2464 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302465 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002466 proc->pid, thread->pid,
2467 node->debug_id);
Todd Kjosf22abc72017-05-09 11:08:05 -07002468 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002469 break;
2470 }
2471 node->pending_weak_ref = 0;
2472 }
2473 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
2474 binder_debug(BINDER_DEBUG_USER_REFS,
Todd Kjosf22abc72017-05-09 11:08:05 -07002475 "%d:%d %s node %d ls %d lw %d tr %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002476 proc->pid, thread->pid,
2477 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Todd Kjosf22abc72017-05-09 11:08:05 -07002478 node->debug_id, node->local_strong_refs,
2479 node->local_weak_refs, node->tmp_refs);
2480 binder_put_node(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002481 break;
2482 }
2483 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302484 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002485 return -EINVAL;
2486 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302487 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002488 return -EINVAL;
2489
2490 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002491 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002492 struct binder_buffer *buffer;
2493
Arve Hjønnevågda498892014-02-21 14:40:26 -08002494 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002495 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002496 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002497
Todd Kjos076072a2017-04-21 14:32:11 -07002498 buffer = binder_alloc_prepare_to_free(&proc->alloc,
2499 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002500 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002501 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
2502 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002503 break;
2504 }
2505 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002506 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
2507 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002508 break;
2509 }
2510 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002511 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
2512 proc->pid, thread->pid, (u64)data_ptr,
2513 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002514 buffer->transaction ? "active" : "finished");
2515
2516 if (buffer->transaction) {
2517 buffer->transaction->buffer = NULL;
2518 buffer->transaction = NULL;
2519 }
2520 if (buffer->async_transaction && buffer->target_node) {
2521 BUG_ON(!buffer->target_node->has_async_transaction);
2522 if (list_empty(&buffer->target_node->async_todo))
2523 buffer->target_node->has_async_transaction = 0;
2524 else
2525 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
2526 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002527 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002528 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07002529 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002530 break;
2531 }
2532
Martijn Coenen5a6da532016-09-30 14:10:07 +02002533 case BC_TRANSACTION_SG:
2534 case BC_REPLY_SG: {
2535 struct binder_transaction_data_sg tr;
2536
2537 if (copy_from_user(&tr, ptr, sizeof(tr)))
2538 return -EFAULT;
2539 ptr += sizeof(tr);
2540 binder_transaction(proc, thread, &tr.transaction_data,
2541 cmd == BC_REPLY_SG, tr.buffers_size);
2542 break;
2543 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002544 case BC_TRANSACTION:
2545 case BC_REPLY: {
2546 struct binder_transaction_data tr;
2547
2548 if (copy_from_user(&tr, ptr, sizeof(tr)))
2549 return -EFAULT;
2550 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02002551 binder_transaction(proc, thread, &tr,
2552 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002553 break;
2554 }
2555
2556 case BC_REGISTER_LOOPER:
2557 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302558 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002559 proc->pid, thread->pid);
2560 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
2561 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302562 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002563 proc->pid, thread->pid);
2564 } else if (proc->requested_threads == 0) {
2565 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302566 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002567 proc->pid, thread->pid);
2568 } else {
2569 proc->requested_threads--;
2570 proc->requested_threads_started++;
2571 }
2572 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
2573 break;
2574 case BC_ENTER_LOOPER:
2575 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302576 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002577 proc->pid, thread->pid);
2578 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
2579 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302580 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002581 proc->pid, thread->pid);
2582 }
2583 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
2584 break;
2585 case BC_EXIT_LOOPER:
2586 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302587 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002588 proc->pid, thread->pid);
2589 thread->looper |= BINDER_LOOPER_STATE_EXITED;
2590 break;
2591
2592 case BC_REQUEST_DEATH_NOTIFICATION:
2593 case BC_CLEAR_DEATH_NOTIFICATION: {
2594 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002595 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002596 struct binder_ref *ref;
2597 struct binder_ref_death *death;
2598
2599 if (get_user(target, (uint32_t __user *)ptr))
2600 return -EFAULT;
2601 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002602 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002603 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002604 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002605 ref = binder_get_ref(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002606 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302607 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002608 proc->pid, thread->pid,
2609 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2610 "BC_REQUEST_DEATH_NOTIFICATION" :
2611 "BC_CLEAR_DEATH_NOTIFICATION",
2612 target);
2613 break;
2614 }
2615
2616 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002617 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002618 proc->pid, thread->pid,
2619 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2620 "BC_REQUEST_DEATH_NOTIFICATION" :
2621 "BC_CLEAR_DEATH_NOTIFICATION",
Todd Kjosb0117bb2017-05-08 09:16:27 -07002622 (u64)cookie, ref->data.debug_id,
2623 ref->data.desc, ref->data.strong,
2624 ref->data.weak, ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002625
2626 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2627 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302628 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002629 proc->pid, thread->pid);
2630 break;
2631 }
2632 death = kzalloc(sizeof(*death), GFP_KERNEL);
2633 if (death == NULL) {
Todd Kjos858b8da2017-04-21 17:35:12 -07002634 WARN_ON(thread->return_error.cmd !=
2635 BR_OK);
2636 thread->return_error.cmd = BR_ERROR;
2637 list_add_tail(
2638 &thread->return_error.work.entry,
2639 &thread->todo);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002640 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302641 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002642 proc->pid, thread->pid);
2643 break;
2644 }
2645 binder_stats_created(BINDER_STAT_DEATH);
2646 INIT_LIST_HEAD(&death->work.entry);
2647 death->cookie = cookie;
2648 ref->death = death;
2649 if (ref->node->proc == NULL) {
2650 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2651 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2652 list_add_tail(&ref->death->work.entry, &thread->todo);
2653 } else {
2654 list_add_tail(&ref->death->work.entry, &proc->todo);
2655 wake_up_interruptible(&proc->wait);
2656 }
2657 }
2658 } else {
2659 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302660 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002661 proc->pid, thread->pid);
2662 break;
2663 }
2664 death = ref->death;
2665 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002666 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002667 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002668 (u64)death->cookie,
2669 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002670 break;
2671 }
2672 ref->death = NULL;
2673 if (list_empty(&death->work.entry)) {
2674 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2675 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2676 list_add_tail(&death->work.entry, &thread->todo);
2677 } else {
2678 list_add_tail(&death->work.entry, &proc->todo);
2679 wake_up_interruptible(&proc->wait);
2680 }
2681 } else {
2682 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2683 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2684 }
2685 }
2686 } break;
2687 case BC_DEAD_BINDER_DONE: {
2688 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002689 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002690 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09002691
Arve Hjønnevågda498892014-02-21 14:40:26 -08002692 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002693 return -EFAULT;
2694
Lisa Du7a64cd82016-02-17 09:32:52 +08002695 ptr += sizeof(cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002696 list_for_each_entry(w, &proc->delivered_death, entry) {
2697 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
Seunghun Lee10f62862014-05-01 01:30:23 +09002698
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002699 if (tmp_death->cookie == cookie) {
2700 death = tmp_death;
2701 break;
2702 }
2703 }
2704 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002705 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2706 proc->pid, thread->pid, (u64)cookie,
2707 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002708 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002709 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2710 proc->pid, thread->pid, (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002711 break;
2712 }
2713
2714 list_del_init(&death->work.entry);
2715 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2716 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2717 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2718 list_add_tail(&death->work.entry, &thread->todo);
2719 } else {
2720 list_add_tail(&death->work.entry, &proc->todo);
2721 wake_up_interruptible(&proc->wait);
2722 }
2723 }
2724 } break;
2725
2726 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302727 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002728 proc->pid, thread->pid, cmd);
2729 return -EINVAL;
2730 }
2731 *consumed = ptr - buffer;
2732 }
2733 return 0;
2734}
2735
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002736static void binder_stat_br(struct binder_proc *proc,
2737 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002738{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002739 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002740 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07002741 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
2742 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
2743 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002744 }
2745}
2746
2747static int binder_has_proc_work(struct binder_proc *proc,
2748 struct binder_thread *thread)
2749{
Todd Kjos6798e6d2017-01-06 14:19:25 -08002750 return !list_empty(&proc->todo) || thread->looper_need_return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002751}
2752
2753static int binder_has_thread_work(struct binder_thread *thread)
2754{
Todd Kjos858b8da2017-04-21 17:35:12 -07002755 return !list_empty(&thread->todo) || thread->looper_need_return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002756}
2757
Todd Kjos60792612017-05-24 10:51:01 -07002758static int binder_put_node_cmd(struct binder_proc *proc,
2759 struct binder_thread *thread,
2760 void __user **ptrp,
2761 binder_uintptr_t node_ptr,
2762 binder_uintptr_t node_cookie,
2763 int node_debug_id,
2764 uint32_t cmd, const char *cmd_name)
2765{
2766 void __user *ptr = *ptrp;
2767
2768 if (put_user(cmd, (uint32_t __user *)ptr))
2769 return -EFAULT;
2770 ptr += sizeof(uint32_t);
2771
2772 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
2773 return -EFAULT;
2774 ptr += sizeof(binder_uintptr_t);
2775
2776 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
2777 return -EFAULT;
2778 ptr += sizeof(binder_uintptr_t);
2779
2780 binder_stat_br(proc, thread, cmd);
2781 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
2782 proc->pid, thread->pid, cmd_name, node_debug_id,
2783 (u64)node_ptr, (u64)node_cookie);
2784
2785 *ptrp = ptr;
2786 return 0;
2787}
2788
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002789static int binder_thread_read(struct binder_proc *proc,
2790 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002791 binder_uintptr_t binder_buffer, size_t size,
2792 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002793{
Arve Hjønnevågda498892014-02-21 14:40:26 -08002794 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002795 void __user *ptr = buffer + *consumed;
2796 void __user *end = buffer + size;
2797
2798 int ret = 0;
2799 int wait_for_proc_work;
2800
2801 if (*consumed == 0) {
2802 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2803 return -EFAULT;
2804 ptr += sizeof(uint32_t);
2805 }
2806
2807retry:
2808 wait_for_proc_work = thread->transaction_stack == NULL &&
2809 list_empty(&thread->todo);
2810
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002811 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2812 if (wait_for_proc_work)
2813 proc->ready_threads++;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002814
2815 binder_unlock(__func__);
2816
2817 trace_binder_wait_for_work(wait_for_proc_work,
2818 !!thread->transaction_stack,
2819 !list_empty(&thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002820 if (wait_for_proc_work) {
2821 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2822 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302823 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 +09002824 proc->pid, thread->pid, thread->looper);
2825 wait_event_interruptible(binder_user_error_wait,
2826 binder_stop_on_user_error < 2);
2827 }
2828 binder_set_nice(proc->default_priority);
2829 if (non_block) {
2830 if (!binder_has_proc_work(proc, thread))
2831 ret = -EAGAIN;
2832 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002833 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002834 } else {
2835 if (non_block) {
2836 if (!binder_has_thread_work(thread))
2837 ret = -EAGAIN;
2838 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002839 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002840 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002841
2842 binder_lock(__func__);
2843
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002844 if (wait_for_proc_work)
2845 proc->ready_threads--;
2846 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2847
2848 if (ret)
2849 return ret;
2850
2851 while (1) {
2852 uint32_t cmd;
2853 struct binder_transaction_data tr;
2854 struct binder_work *w;
2855 struct binder_transaction *t = NULL;
Todd Kjos2f993e22017-05-12 14:42:55 -07002856 struct binder_thread *t_from;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002857
Dmitry Voytik395262a2014-09-08 18:16:34 +04002858 if (!list_empty(&thread->todo)) {
2859 w = list_first_entry(&thread->todo, struct binder_work,
2860 entry);
2861 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2862 w = list_first_entry(&proc->todo, struct binder_work,
2863 entry);
2864 } else {
2865 /* no data added */
Todd Kjos6798e6d2017-01-06 14:19:25 -08002866 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002867 goto retry;
2868 break;
2869 }
2870
2871 if (end - ptr < sizeof(tr) + 4)
2872 break;
2873
2874 switch (w->type) {
2875 case BINDER_WORK_TRANSACTION: {
2876 t = container_of(w, struct binder_transaction, work);
2877 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07002878 case BINDER_WORK_RETURN_ERROR: {
2879 struct binder_error *e = container_of(
2880 w, struct binder_error, work);
2881
2882 WARN_ON(e->cmd == BR_OK);
2883 if (put_user(e->cmd, (uint32_t __user *)ptr))
2884 return -EFAULT;
2885 e->cmd = BR_OK;
2886 ptr += sizeof(uint32_t);
2887
2888 binder_stat_br(proc, thread, cmd);
2889 list_del(&w->entry);
2890 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002891 case BINDER_WORK_TRANSACTION_COMPLETE: {
2892 cmd = BR_TRANSACTION_COMPLETE;
2893 if (put_user(cmd, (uint32_t __user *)ptr))
2894 return -EFAULT;
2895 ptr += sizeof(uint32_t);
2896
2897 binder_stat_br(proc, thread, cmd);
2898 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302899 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002900 proc->pid, thread->pid);
2901
2902 list_del(&w->entry);
2903 kfree(w);
2904 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2905 } break;
2906 case BINDER_WORK_NODE: {
2907 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07002908 int strong, weak;
2909 binder_uintptr_t node_ptr = node->ptr;
2910 binder_uintptr_t node_cookie = node->cookie;
2911 int node_debug_id = node->debug_id;
2912 int has_weak_ref;
2913 int has_strong_ref;
2914 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09002915
Todd Kjos60792612017-05-24 10:51:01 -07002916 BUG_ON(proc != node->proc);
2917 strong = node->internal_strong_refs ||
2918 node->local_strong_refs;
2919 weak = !hlist_empty(&node->refs) ||
Todd Kjosf22abc72017-05-09 11:08:05 -07002920 node->local_weak_refs ||
2921 node->tmp_refs || strong;
Todd Kjos60792612017-05-24 10:51:01 -07002922 has_strong_ref = node->has_strong_ref;
2923 has_weak_ref = node->has_weak_ref;
2924
2925 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002926 node->has_weak_ref = 1;
2927 node->pending_weak_ref = 1;
2928 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07002929 }
2930 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002931 node->has_strong_ref = 1;
2932 node->pending_strong_ref = 1;
2933 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07002934 }
2935 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002936 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07002937 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002938 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07002939 list_del(&w->entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002940
Todd Kjos60792612017-05-24 10:51:01 -07002941 if (!weak && !strong) {
2942 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2943 "%d:%d node %d u%016llx c%016llx deleted\n",
2944 proc->pid, thread->pid,
2945 node_debug_id,
2946 (u64)node_ptr,
2947 (u64)node_cookie);
2948 rb_erase(&node->rb_node, &proc->nodes);
2949 kfree(node);
2950 binder_stats_deleted(BINDER_STAT_NODE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002951 }
Todd Kjos60792612017-05-24 10:51:01 -07002952 if (weak && !has_weak_ref)
2953 ret = binder_put_node_cmd(
2954 proc, thread, &ptr, node_ptr,
2955 node_cookie, node_debug_id,
2956 BR_INCREFS, "BR_INCREFS");
2957 if (!ret && strong && !has_strong_ref)
2958 ret = binder_put_node_cmd(
2959 proc, thread, &ptr, node_ptr,
2960 node_cookie, node_debug_id,
2961 BR_ACQUIRE, "BR_ACQUIRE");
2962 if (!ret && !strong && has_strong_ref)
2963 ret = binder_put_node_cmd(
2964 proc, thread, &ptr, node_ptr,
2965 node_cookie, node_debug_id,
2966 BR_RELEASE, "BR_RELEASE");
2967 if (!ret && !weak && has_weak_ref)
2968 ret = binder_put_node_cmd(
2969 proc, thread, &ptr, node_ptr,
2970 node_cookie, node_debug_id,
2971 BR_DECREFS, "BR_DECREFS");
2972 if (orig_ptr == ptr)
2973 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2974 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2975 proc->pid, thread->pid,
2976 node_debug_id,
2977 (u64)node_ptr,
2978 (u64)node_cookie);
2979 if (ret)
2980 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002981 } break;
2982 case BINDER_WORK_DEAD_BINDER:
2983 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2984 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2985 struct binder_ref_death *death;
2986 uint32_t cmd;
2987
2988 death = container_of(w, struct binder_ref_death, work);
2989 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2990 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2991 else
2992 cmd = BR_DEAD_BINDER;
2993 if (put_user(cmd, (uint32_t __user *)ptr))
2994 return -EFAULT;
2995 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002996 if (put_user(death->cookie,
2997 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002998 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002999 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07003000 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003001 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003002 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003003 proc->pid, thread->pid,
3004 cmd == BR_DEAD_BINDER ?
3005 "BR_DEAD_BINDER" :
3006 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08003007 (u64)death->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003008
3009 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
3010 list_del(&w->entry);
3011 kfree(death);
3012 binder_stats_deleted(BINDER_STAT_DEATH);
3013 } else
3014 list_move(&w->entry, &proc->delivered_death);
3015 if (cmd == BR_DEAD_BINDER)
3016 goto done; /* DEAD_BINDER notifications can cause transactions */
3017 } break;
3018 }
3019
3020 if (!t)
3021 continue;
3022
3023 BUG_ON(t->buffer == NULL);
3024 if (t->buffer->target_node) {
3025 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09003026
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003027 tr.target.ptr = target_node->ptr;
3028 tr.cookie = target_node->cookie;
3029 t->saved_priority = task_nice(current);
3030 if (t->priority < target_node->min_priority &&
3031 !(t->flags & TF_ONE_WAY))
3032 binder_set_nice(t->priority);
3033 else if (!(t->flags & TF_ONE_WAY) ||
3034 t->saved_priority > target_node->min_priority)
3035 binder_set_nice(target_node->min_priority);
3036 cmd = BR_TRANSACTION;
3037 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08003038 tr.target.ptr = 0;
3039 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003040 cmd = BR_REPLY;
3041 }
3042 tr.code = t->code;
3043 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06003044 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003045
Todd Kjos2f993e22017-05-12 14:42:55 -07003046 t_from = binder_get_txn_from(t);
3047 if (t_from) {
3048 struct task_struct *sender = t_from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09003049
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003050 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003051 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003052 } else {
3053 tr.sender_pid = 0;
3054 }
3055
3056 tr.data_size = t->buffer->data_size;
3057 tr.offsets_size = t->buffer->offsets_size;
Todd Kjosd325d372016-10-10 10:40:53 -07003058 tr.data.ptr.buffer = (binder_uintptr_t)
3059 ((uintptr_t)t->buffer->data +
3060 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003061 tr.data.ptr.offsets = tr.data.ptr.buffer +
3062 ALIGN(t->buffer->data_size,
3063 sizeof(void *));
3064
Todd Kjos2f993e22017-05-12 14:42:55 -07003065 if (put_user(cmd, (uint32_t __user *)ptr)) {
3066 if (t_from)
3067 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003068 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07003069 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003070 ptr += sizeof(uint32_t);
Todd Kjos2f993e22017-05-12 14:42:55 -07003071 if (copy_to_user(ptr, &tr, sizeof(tr))) {
3072 if (t_from)
3073 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003074 return -EFAULT;
Todd Kjos2f993e22017-05-12 14:42:55 -07003075 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003076 ptr += sizeof(tr);
3077
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003078 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003079 binder_stat_br(proc, thread, cmd);
3080 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003081 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003082 proc->pid, thread->pid,
3083 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
3084 "BR_REPLY",
Todd Kjos2f993e22017-05-12 14:42:55 -07003085 t->debug_id, t_from ? t_from->proc->pid : 0,
3086 t_from ? t_from->pid : 0, cmd,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003087 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003088 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003089
Todd Kjos2f993e22017-05-12 14:42:55 -07003090 if (t_from)
3091 binder_thread_dec_tmpref(t_from);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003092 list_del(&t->work.entry);
3093 t->buffer->allow_user_free = 1;
3094 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
3095 t->to_parent = thread->transaction_stack;
3096 t->to_thread = thread;
3097 thread->transaction_stack = t;
3098 } else {
Todd Kjos21ef40a2017-03-30 18:02:13 -07003099 binder_free_transaction(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003100 }
3101 break;
3102 }
3103
3104done:
3105
3106 *consumed = ptr - buffer;
3107 if (proc->requested_threads + proc->ready_threads == 0 &&
3108 proc->requested_threads_started < proc->max_threads &&
3109 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3110 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
3111 /*spawn a new thread if we leave this out */) {
3112 proc->requested_threads++;
3113 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303114 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003115 proc->pid, thread->pid);
3116 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
3117 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07003118 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003119 }
3120 return 0;
3121}
3122
3123static void binder_release_work(struct list_head *list)
3124{
3125 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09003126
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003127 while (!list_empty(list)) {
3128 w = list_first_entry(list, struct binder_work, entry);
3129 list_del_init(&w->entry);
3130 switch (w->type) {
3131 case BINDER_WORK_TRANSACTION: {
3132 struct binder_transaction *t;
3133
3134 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003135 if (t->buffer->target_node &&
3136 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003137 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003138 } else {
3139 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303140 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003141 t->debug_id);
Todd Kjos21ef40a2017-03-30 18:02:13 -07003142 binder_free_transaction(t);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003143 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003144 } break;
Todd Kjos858b8da2017-04-21 17:35:12 -07003145 case BINDER_WORK_RETURN_ERROR: {
3146 struct binder_error *e = container_of(
3147 w, struct binder_error, work);
3148
3149 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
3150 "undelivered TRANSACTION_ERROR: %u\n",
3151 e->cmd);
3152 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003153 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003154 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303155 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003156 kfree(w);
3157 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3158 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003159 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3160 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
3161 struct binder_ref_death *death;
3162
3163 death = container_of(w, struct binder_ref_death, work);
3164 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003165 "undelivered death notification, %016llx\n",
3166 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003167 kfree(death);
3168 binder_stats_deleted(BINDER_STAT_DEATH);
3169 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003170 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303171 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003172 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003173 break;
3174 }
3175 }
3176
3177}
3178
3179static struct binder_thread *binder_get_thread(struct binder_proc *proc)
3180{
3181 struct binder_thread *thread = NULL;
3182 struct rb_node *parent = NULL;
3183 struct rb_node **p = &proc->threads.rb_node;
3184
3185 while (*p) {
3186 parent = *p;
3187 thread = rb_entry(parent, struct binder_thread, rb_node);
3188
3189 if (current->pid < thread->pid)
3190 p = &(*p)->rb_left;
3191 else if (current->pid > thread->pid)
3192 p = &(*p)->rb_right;
3193 else
3194 break;
3195 }
3196 if (*p == NULL) {
3197 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
3198 if (thread == NULL)
3199 return NULL;
3200 binder_stats_created(BINDER_STAT_THREAD);
3201 thread->proc = proc;
3202 thread->pid = current->pid;
Todd Kjos2f993e22017-05-12 14:42:55 -07003203 atomic_set(&thread->tmp_ref, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003204 init_waitqueue_head(&thread->wait);
3205 INIT_LIST_HEAD(&thread->todo);
3206 rb_link_node(&thread->rb_node, parent, p);
3207 rb_insert_color(&thread->rb_node, &proc->threads);
Todd Kjos6798e6d2017-01-06 14:19:25 -08003208 thread->looper_need_return = true;
Todd Kjos858b8da2017-04-21 17:35:12 -07003209 thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
3210 thread->return_error.cmd = BR_OK;
3211 thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
3212 thread->reply_error.cmd = BR_OK;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003213 }
3214 return thread;
3215}
3216
Todd Kjos2f993e22017-05-12 14:42:55 -07003217static void binder_free_proc(struct binder_proc *proc)
3218{
3219 BUG_ON(!list_empty(&proc->todo));
3220 BUG_ON(!list_empty(&proc->delivered_death));
3221 binder_alloc_deferred_release(&proc->alloc);
3222 put_task_struct(proc->tsk);
3223 binder_stats_deleted(BINDER_STAT_PROC);
3224 kfree(proc);
3225}
3226
3227static void binder_free_thread(struct binder_thread *thread)
3228{
3229 BUG_ON(!list_empty(&thread->todo));
3230 binder_stats_deleted(BINDER_STAT_THREAD);
3231 binder_proc_dec_tmpref(thread->proc);
3232 kfree(thread);
3233}
3234
3235static int binder_thread_release(struct binder_proc *proc,
3236 struct binder_thread *thread)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003237{
3238 struct binder_transaction *t;
3239 struct binder_transaction *send_reply = NULL;
3240 int active_transactions = 0;
Todd Kjos2f993e22017-05-12 14:42:55 -07003241 struct binder_transaction *last_t = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003242
Todd Kjos2f993e22017-05-12 14:42:55 -07003243 /*
3244 * take a ref on the proc so it survives
3245 * after we remove this thread from proc->threads.
3246 * The corresponding dec is when we actually
3247 * free the thread in binder_free_thread()
3248 */
3249 proc->tmp_ref++;
3250 /*
3251 * take a ref on this thread to ensure it
3252 * survives while we are releasing it
3253 */
3254 atomic_inc(&thread->tmp_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003255 rb_erase(&thread->rb_node, &proc->threads);
3256 t = thread->transaction_stack;
Todd Kjos2f993e22017-05-12 14:42:55 -07003257 if (t) {
3258 spin_lock(&t->lock);
3259 if (t->to_thread == thread)
3260 send_reply = t;
3261 }
3262 thread->is_dead = true;
3263
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003264 while (t) {
Todd Kjos2f993e22017-05-12 14:42:55 -07003265 last_t = t;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003266 active_transactions++;
3267 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303268 "release %d:%d transaction %d %s, still active\n",
3269 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003270 t->debug_id,
3271 (t->to_thread == thread) ? "in" : "out");
3272
3273 if (t->to_thread == thread) {
3274 t->to_proc = NULL;
3275 t->to_thread = NULL;
3276 if (t->buffer) {
3277 t->buffer->transaction = NULL;
3278 t->buffer = NULL;
3279 }
3280 t = t->to_parent;
3281 } else if (t->from == thread) {
3282 t->from = NULL;
3283 t = t->from_parent;
3284 } else
3285 BUG();
Todd Kjos2f993e22017-05-12 14:42:55 -07003286 spin_unlock(&last_t->lock);
3287 if (t)
3288 spin_lock(&t->lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003289 }
Todd Kjos2f993e22017-05-12 14:42:55 -07003290
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003291 if (send_reply)
3292 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
3293 binder_release_work(&thread->todo);
Todd Kjos2f993e22017-05-12 14:42:55 -07003294 binder_thread_dec_tmpref(thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003295 return active_transactions;
3296}
3297
3298static unsigned int binder_poll(struct file *filp,
3299 struct poll_table_struct *wait)
3300{
3301 struct binder_proc *proc = filp->private_data;
3302 struct binder_thread *thread = NULL;
3303 int wait_for_proc_work;
3304
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003305 binder_lock(__func__);
3306
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003307 thread = binder_get_thread(proc);
3308
3309 wait_for_proc_work = thread->transaction_stack == NULL &&
Todd Kjos858b8da2017-04-21 17:35:12 -07003310 list_empty(&thread->todo);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003311
3312 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003313
3314 if (wait_for_proc_work) {
3315 if (binder_has_proc_work(proc, thread))
3316 return POLLIN;
3317 poll_wait(filp, &proc->wait, wait);
3318 if (binder_has_proc_work(proc, thread))
3319 return POLLIN;
3320 } else {
3321 if (binder_has_thread_work(thread))
3322 return POLLIN;
3323 poll_wait(filp, &thread->wait, wait);
3324 if (binder_has_thread_work(thread))
3325 return POLLIN;
3326 }
3327 return 0;
3328}
3329
Tair Rzayev78260ac2014-06-03 22:27:21 +03003330static int binder_ioctl_write_read(struct file *filp,
3331 unsigned int cmd, unsigned long arg,
3332 struct binder_thread *thread)
3333{
3334 int ret = 0;
3335 struct binder_proc *proc = filp->private_data;
3336 unsigned int size = _IOC_SIZE(cmd);
3337 void __user *ubuf = (void __user *)arg;
3338 struct binder_write_read bwr;
3339
3340 if (size != sizeof(struct binder_write_read)) {
3341 ret = -EINVAL;
3342 goto out;
3343 }
3344 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
3345 ret = -EFAULT;
3346 goto out;
3347 }
3348 binder_debug(BINDER_DEBUG_READ_WRITE,
3349 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
3350 proc->pid, thread->pid,
3351 (u64)bwr.write_size, (u64)bwr.write_buffer,
3352 (u64)bwr.read_size, (u64)bwr.read_buffer);
3353
3354 if (bwr.write_size > 0) {
3355 ret = binder_thread_write(proc, thread,
3356 bwr.write_buffer,
3357 bwr.write_size,
3358 &bwr.write_consumed);
3359 trace_binder_write_done(ret);
3360 if (ret < 0) {
3361 bwr.read_consumed = 0;
3362 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
3363 ret = -EFAULT;
3364 goto out;
3365 }
3366 }
3367 if (bwr.read_size > 0) {
3368 ret = binder_thread_read(proc, thread, bwr.read_buffer,
3369 bwr.read_size,
3370 &bwr.read_consumed,
3371 filp->f_flags & O_NONBLOCK);
3372 trace_binder_read_done(ret);
3373 if (!list_empty(&proc->todo))
3374 wake_up_interruptible(&proc->wait);
3375 if (ret < 0) {
3376 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
3377 ret = -EFAULT;
3378 goto out;
3379 }
3380 }
3381 binder_debug(BINDER_DEBUG_READ_WRITE,
3382 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
3383 proc->pid, thread->pid,
3384 (u64)bwr.write_consumed, (u64)bwr.write_size,
3385 (u64)bwr.read_consumed, (u64)bwr.read_size);
3386 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
3387 ret = -EFAULT;
3388 goto out;
3389 }
3390out:
3391 return ret;
3392}
3393
3394static int binder_ioctl_set_ctx_mgr(struct file *filp)
3395{
3396 int ret = 0;
3397 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003398 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003399 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03003400 kuid_t curr_euid = current_euid();
3401
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003402 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003403 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003404 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
3405 ret = -EBUSY;
3406 goto out;
3407 }
Stephen Smalley79af7302015-01-21 10:54:10 -05003408 ret = security_binder_set_context_mgr(proc->tsk);
3409 if (ret < 0)
3410 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003411 if (uid_valid(context->binder_context_mgr_uid)) {
3412 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003413 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
3414 from_kuid(&init_user_ns, curr_euid),
3415 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003416 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03003417 ret = -EPERM;
3418 goto out;
3419 }
3420 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003421 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03003422 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003423 new_node = binder_new_node(proc, 0, 0);
3424 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003425 ret = -ENOMEM;
3426 goto out;
3427 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003428 new_node->local_weak_refs++;
3429 new_node->local_strong_refs++;
3430 new_node->has_strong_ref = 1;
3431 new_node->has_weak_ref = 1;
3432 context->binder_context_mgr_node = new_node;
Todd Kjosf22abc72017-05-09 11:08:05 -07003433 binder_put_node(new_node);
Tair Rzayev78260ac2014-06-03 22:27:21 +03003434out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003435 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03003436 return ret;
3437}
3438
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003439static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3440{
3441 int ret;
3442 struct binder_proc *proc = filp->private_data;
3443 struct binder_thread *thread;
3444 unsigned int size = _IOC_SIZE(cmd);
3445 void __user *ubuf = (void __user *)arg;
3446
Tair Rzayev78260ac2014-06-03 22:27:21 +03003447 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
3448 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003449
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003450 trace_binder_ioctl(cmd, arg);
3451
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003452 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
3453 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003454 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003455
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003456 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003457 thread = binder_get_thread(proc);
3458 if (thread == NULL) {
3459 ret = -ENOMEM;
3460 goto err;
3461 }
3462
3463 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003464 case BINDER_WRITE_READ:
3465 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
3466 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003467 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003468 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003469 case BINDER_SET_MAX_THREADS:
3470 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
3471 ret = -EINVAL;
3472 goto err;
3473 }
3474 break;
3475 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03003476 ret = binder_ioctl_set_ctx_mgr(filp);
3477 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003478 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003479 break;
3480 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303481 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003482 proc->pid, thread->pid);
Todd Kjos2f993e22017-05-12 14:42:55 -07003483 binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003484 thread = NULL;
3485 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02003486 case BINDER_VERSION: {
3487 struct binder_version __user *ver = ubuf;
3488
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003489 if (size != sizeof(struct binder_version)) {
3490 ret = -EINVAL;
3491 goto err;
3492 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02003493 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
3494 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003495 ret = -EINVAL;
3496 goto err;
3497 }
3498 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02003499 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003500 default:
3501 ret = -EINVAL;
3502 goto err;
3503 }
3504 ret = 0;
3505err:
3506 if (thread)
Todd Kjos6798e6d2017-01-06 14:19:25 -08003507 thread->looper_need_return = false;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003508 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003509 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
3510 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05303511 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 -07003512err_unlocked:
3513 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003514 return ret;
3515}
3516
3517static void binder_vma_open(struct vm_area_struct *vma)
3518{
3519 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003520
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003521 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303522 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003523 proc->pid, vma->vm_start, vma->vm_end,
3524 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3525 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003526}
3527
3528static void binder_vma_close(struct vm_area_struct *vma)
3529{
3530 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003531
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003532 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303533 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003534 proc->pid, vma->vm_start, vma->vm_end,
3535 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3536 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07003537 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003538 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
3539}
3540
Vinayak Menonddac7d52014-06-02 18:17:59 +05303541static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3542{
3543 return VM_FAULT_SIGBUS;
3544}
3545
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07003546static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003547 .open = binder_vma_open,
3548 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05303549 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003550};
3551
Todd Kjosd325d372016-10-10 10:40:53 -07003552static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
3553{
3554 int ret;
3555 struct binder_proc *proc = filp->private_data;
3556 const char *failure_string;
3557
3558 if (proc->tsk != current->group_leader)
3559 return -EINVAL;
3560
3561 if ((vma->vm_end - vma->vm_start) > SZ_4M)
3562 vma->vm_end = vma->vm_start + SZ_4M;
3563
3564 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3565 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
3566 __func__, proc->pid, vma->vm_start, vma->vm_end,
3567 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3568 (unsigned long)pgprot_val(vma->vm_page_prot));
3569
3570 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
3571 ret = -EPERM;
3572 failure_string = "bad vm_flags";
3573 goto err_bad_arg;
3574 }
3575 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
3576 vma->vm_ops = &binder_vm_ops;
3577 vma->vm_private_data = proc;
3578
3579 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
3580 if (ret)
3581 return ret;
3582 proc->files = get_files_struct(current);
3583 return 0;
3584
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003585err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04003586 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003587 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
3588 return ret;
3589}
3590
3591static int binder_open(struct inode *nodp, struct file *filp)
3592{
3593 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003594 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003595
3596 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
3597 current->group_leader->pid, current->pid);
3598
3599 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
3600 if (proc == NULL)
3601 return -ENOMEM;
Martijn Coenen872c26e2017-03-07 15:51:18 +01003602 get_task_struct(current->group_leader);
3603 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003604 INIT_LIST_HEAD(&proc->todo);
3605 init_waitqueue_head(&proc->wait);
3606 proc->default_priority = task_nice(current);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003607 binder_dev = container_of(filp->private_data, struct binder_device,
3608 miscdev);
3609 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07003610 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003611
3612 binder_lock(__func__);
3613
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003614 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003615 proc->pid = current->group_leader->pid;
3616 INIT_LIST_HEAD(&proc->delivered_death);
3617 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003618
3619 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003620
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003621 mutex_lock(&binder_procs_lock);
3622 hlist_add_head(&proc->proc_node, &binder_procs);
3623 mutex_unlock(&binder_procs_lock);
3624
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003625 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003626 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09003627
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003628 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003629 /*
3630 * proc debug entries are shared between contexts, so
3631 * this will fail if the process tries to open the driver
3632 * again with a different context. The priting code will
3633 * anyway print all contexts that a given PID has, so this
3634 * is not a problem.
3635 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003636 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003637 binder_debugfs_dir_entry_proc,
3638 (void *)(unsigned long)proc->pid,
3639 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003640 }
3641
3642 return 0;
3643}
3644
3645static int binder_flush(struct file *filp, fl_owner_t id)
3646{
3647 struct binder_proc *proc = filp->private_data;
3648
3649 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3650
3651 return 0;
3652}
3653
3654static void binder_deferred_flush(struct binder_proc *proc)
3655{
3656 struct rb_node *n;
3657 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09003658
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003659 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3660 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09003661
Todd Kjos6798e6d2017-01-06 14:19:25 -08003662 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003663 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3664 wake_up_interruptible(&thread->wait);
3665 wake_count++;
3666 }
3667 }
3668 wake_up_interruptible_all(&proc->wait);
3669
3670 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3671 "binder_flush: %d woke %d threads\n", proc->pid,
3672 wake_count);
3673}
3674
3675static int binder_release(struct inode *nodp, struct file *filp)
3676{
3677 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003678
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003679 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003680 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3681
3682 return 0;
3683}
3684
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003685static int binder_node_release(struct binder_node *node, int refs)
3686{
3687 struct binder_ref *ref;
3688 int death = 0;
3689
3690 list_del_init(&node->work.entry);
3691 binder_release_work(&node->async_todo);
Todd Kjosf22abc72017-05-09 11:08:05 -07003692 /*
3693 * The caller must have taken a temporary ref on the node,
3694 */
3695 BUG_ON(!node->tmp_refs);
3696 if (hlist_empty(&node->refs) && node->tmp_refs == 1) {
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003697 kfree(node);
3698 binder_stats_deleted(BINDER_STAT_NODE);
3699
3700 return refs;
3701 }
3702
3703 node->proc = NULL;
3704 node->local_strong_refs = 0;
3705 node->local_weak_refs = 0;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003706
3707 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003708 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003709 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003710
3711 hlist_for_each_entry(ref, &node->refs, node_entry) {
3712 refs++;
3713
3714 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08003715 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003716
3717 death++;
3718
3719 if (list_empty(&ref->death->work.entry)) {
3720 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3721 list_add_tail(&ref->death->work.entry,
3722 &ref->proc->todo);
3723 wake_up_interruptible(&ref->proc->wait);
3724 } else
3725 BUG();
3726 }
3727
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003728 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3729 "node %d now dead, refs %d, death %d\n",
3730 node->debug_id, refs, death);
Todd Kjosf22abc72017-05-09 11:08:05 -07003731 binder_put_node(node);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003732
3733 return refs;
3734}
3735
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003736static void binder_deferred_release(struct binder_proc *proc)
3737{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003738 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003739 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07003740 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003741
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003742 BUG_ON(proc->files);
3743
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003744 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003745 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003746 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003747
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003748 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003749 if (context->binder_context_mgr_node &&
3750 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003751 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003752 "%s: %d context_mgr_node gone\n",
3753 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003754 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003755 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003756 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos2f993e22017-05-12 14:42:55 -07003757 /*
3758 * Make sure proc stays alive after we
3759 * remove all the threads
3760 */
3761 proc->tmp_ref++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003762
Todd Kjos2f993e22017-05-12 14:42:55 -07003763 proc->is_dead = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003764 threads = 0;
3765 active_transactions = 0;
3766 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003767 struct binder_thread *thread;
3768
3769 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003770 threads++;
Todd Kjos2f993e22017-05-12 14:42:55 -07003771 active_transactions += binder_thread_release(proc, thread);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003772 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003773
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003774 nodes = 0;
3775 incoming_refs = 0;
3776 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003777 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003778
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003779 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003780 nodes++;
Todd Kjosf22abc72017-05-09 11:08:05 -07003781 /*
3782 * take a temporary ref on the node before
3783 * calling binder_node_release() which will either
3784 * kfree() the node or call binder_put_node()
3785 */
3786 binder_inc_node_tmpref(node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003787 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003788 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003789 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003790
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003791 outgoing_refs = 0;
3792 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003793 struct binder_ref *ref;
3794
3795 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003796 outgoing_refs++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07003797 binder_cleanup_ref(ref);
3798 binder_free_ref(ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003799 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003800
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003801 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003802 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003803
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003804 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07003805 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003806 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07003807 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003808
Todd Kjos2f993e22017-05-12 14:42:55 -07003809 binder_proc_dec_tmpref(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003810}
3811
3812static void binder_deferred_func(struct work_struct *work)
3813{
3814 struct binder_proc *proc;
3815 struct files_struct *files;
3816
3817 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003818
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003819 do {
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003820 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003821 mutex_lock(&binder_deferred_lock);
3822 if (!hlist_empty(&binder_deferred_list)) {
3823 proc = hlist_entry(binder_deferred_list.first,
3824 struct binder_proc, deferred_work_node);
3825 hlist_del_init(&proc->deferred_work_node);
3826 defer = proc->deferred_work;
3827 proc->deferred_work = 0;
3828 } else {
3829 proc = NULL;
3830 defer = 0;
3831 }
3832 mutex_unlock(&binder_deferred_lock);
3833
3834 files = NULL;
3835 if (defer & BINDER_DEFERRED_PUT_FILES) {
3836 files = proc->files;
3837 if (files)
3838 proc->files = NULL;
3839 }
3840
3841 if (defer & BINDER_DEFERRED_FLUSH)
3842 binder_deferred_flush(proc);
3843
3844 if (defer & BINDER_DEFERRED_RELEASE)
3845 binder_deferred_release(proc); /* frees proc */
3846
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003847 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003848 if (files)
3849 put_files_struct(files);
3850 } while (proc);
3851}
3852static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3853
3854static void
3855binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3856{
3857 mutex_lock(&binder_deferred_lock);
3858 proc->deferred_work |= defer;
3859 if (hlist_unhashed(&proc->deferred_work_node)) {
3860 hlist_add_head(&proc->deferred_work_node,
3861 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05303862 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003863 }
3864 mutex_unlock(&binder_deferred_lock);
3865}
3866
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003867static void print_binder_transaction(struct seq_file *m, const char *prefix,
3868 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003869{
Todd Kjos2f993e22017-05-12 14:42:55 -07003870 spin_lock(&t->lock);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003871 seq_printf(m,
3872 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3873 prefix, t->debug_id, t,
3874 t->from ? t->from->proc->pid : 0,
3875 t->from ? t->from->pid : 0,
3876 t->to_proc ? t->to_proc->pid : 0,
3877 t->to_thread ? t->to_thread->pid : 0,
3878 t->code, t->flags, t->priority, t->need_reply);
Todd Kjos2f993e22017-05-12 14:42:55 -07003879 spin_unlock(&t->lock);
3880
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003881 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003882 seq_puts(m, " buffer free\n");
3883 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003884 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003885 if (t->buffer->target_node)
3886 seq_printf(m, " node %d",
3887 t->buffer->target_node->debug_id);
3888 seq_printf(m, " size %zd:%zd data %p\n",
3889 t->buffer->data_size, t->buffer->offsets_size,
3890 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003891}
3892
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003893static void print_binder_work(struct seq_file *m, const char *prefix,
3894 const char *transaction_prefix,
3895 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003896{
3897 struct binder_node *node;
3898 struct binder_transaction *t;
3899
3900 switch (w->type) {
3901 case BINDER_WORK_TRANSACTION:
3902 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003903 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003904 break;
Todd Kjos858b8da2017-04-21 17:35:12 -07003905 case BINDER_WORK_RETURN_ERROR: {
3906 struct binder_error *e = container_of(
3907 w, struct binder_error, work);
3908
3909 seq_printf(m, "%stransaction error: %u\n",
3910 prefix, e->cmd);
3911 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003912 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003913 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003914 break;
3915 case BINDER_WORK_NODE:
3916 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003917 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3918 prefix, node->debug_id,
3919 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003920 break;
3921 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003922 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003923 break;
3924 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003925 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003926 break;
3927 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003928 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003929 break;
3930 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003931 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003932 break;
3933 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003934}
3935
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003936static void print_binder_thread(struct seq_file *m,
3937 struct binder_thread *thread,
3938 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003939{
3940 struct binder_transaction *t;
3941 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003942 size_t start_pos = m->count;
3943 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003944
Todd Kjos2f993e22017-05-12 14:42:55 -07003945 seq_printf(m, " thread %d: l %02x need_return %d tr %d\n",
Todd Kjos6798e6d2017-01-06 14:19:25 -08003946 thread->pid, thread->looper,
Todd Kjos2f993e22017-05-12 14:42:55 -07003947 thread->looper_need_return,
3948 atomic_read(&thread->tmp_ref));
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003949 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003950 t = thread->transaction_stack;
3951 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003952 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003953 print_binder_transaction(m,
3954 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003955 t = t->from_parent;
3956 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003957 print_binder_transaction(m,
3958 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003959 t = t->to_parent;
3960 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003961 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003962 t = NULL;
3963 }
3964 }
3965 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003966 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003967 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003968 if (!print_always && m->count == header_pos)
3969 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003970}
3971
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003972static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003973{
3974 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003975 struct binder_work *w;
3976 int count;
3977
3978 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003979 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003980 count++;
3981
Todd Kjosf22abc72017-05-09 11:08:05 -07003982 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 -08003983 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003984 node->has_strong_ref, node->has_weak_ref,
3985 node->local_strong_refs, node->local_weak_refs,
Todd Kjosf22abc72017-05-09 11:08:05 -07003986 node->internal_strong_refs, count, node->tmp_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003987 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003988 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003989 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003990 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003991 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003992 seq_puts(m, "\n");
3993 list_for_each_entry(w, &node->async_todo, entry)
3994 print_binder_work(m, " ",
3995 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003996}
3997
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003998static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003999{
Todd Kjosb0117bb2017-05-08 09:16:27 -07004000 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
4001 ref->data.debug_id, ref->data.desc,
4002 ref->node->proc ? "" : "dead ",
4003 ref->node->debug_id, ref->data.strong,
4004 ref->data.weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004005}
4006
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004007static void print_binder_proc(struct seq_file *m,
4008 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004009{
4010 struct binder_work *w;
4011 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004012 size_t start_pos = m->count;
4013 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004014
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004015 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004016 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004017 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004018
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004019 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
4020 print_binder_thread(m, rb_entry(n, struct binder_thread,
4021 rb_node), print_all);
4022 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004023 struct binder_node *node = rb_entry(n, struct binder_node,
4024 rb_node);
4025 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004026 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004027 }
4028 if (print_all) {
4029 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004030 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004031 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004032 print_binder_ref(m, rb_entry(n, struct binder_ref,
4033 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004034 }
Todd Kjosd325d372016-10-10 10:40:53 -07004035 binder_alloc_print_allocated(m, &proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004036 list_for_each_entry(w, &proc->todo, entry)
4037 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004038 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004039 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004040 break;
4041 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004042 if (!print_all && m->count == header_pos)
4043 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004044}
4045
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10004046static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004047 "BR_ERROR",
4048 "BR_OK",
4049 "BR_TRANSACTION",
4050 "BR_REPLY",
4051 "BR_ACQUIRE_RESULT",
4052 "BR_DEAD_REPLY",
4053 "BR_TRANSACTION_COMPLETE",
4054 "BR_INCREFS",
4055 "BR_ACQUIRE",
4056 "BR_RELEASE",
4057 "BR_DECREFS",
4058 "BR_ATTEMPT_ACQUIRE",
4059 "BR_NOOP",
4060 "BR_SPAWN_LOOPER",
4061 "BR_FINISHED",
4062 "BR_DEAD_BINDER",
4063 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4064 "BR_FAILED_REPLY"
4065};
4066
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10004067static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004068 "BC_TRANSACTION",
4069 "BC_REPLY",
4070 "BC_ACQUIRE_RESULT",
4071 "BC_FREE_BUFFER",
4072 "BC_INCREFS",
4073 "BC_ACQUIRE",
4074 "BC_RELEASE",
4075 "BC_DECREFS",
4076 "BC_INCREFS_DONE",
4077 "BC_ACQUIRE_DONE",
4078 "BC_ATTEMPT_ACQUIRE",
4079 "BC_REGISTER_LOOPER",
4080 "BC_ENTER_LOOPER",
4081 "BC_EXIT_LOOPER",
4082 "BC_REQUEST_DEATH_NOTIFICATION",
4083 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02004084 "BC_DEAD_BINDER_DONE",
4085 "BC_TRANSACTION_SG",
4086 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004087};
4088
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10004089static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004090 "proc",
4091 "thread",
4092 "node",
4093 "ref",
4094 "death",
4095 "transaction",
4096 "transaction_complete"
4097};
4098
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004099static void print_binder_stats(struct seq_file *m, const char *prefix,
4100 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004101{
4102 int i;
4103
4104 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004105 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004106 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004107 int temp = atomic_read(&stats->bc[i]);
4108
4109 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004110 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004111 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004112 }
4113
4114 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004115 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004116 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004117 int temp = atomic_read(&stats->br[i]);
4118
4119 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004120 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004121 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004122 }
4123
4124 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004125 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004126 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004127 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004128 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004129 int created = atomic_read(&stats->obj_created[i]);
4130 int deleted = atomic_read(&stats->obj_deleted[i]);
4131
4132 if (created || deleted)
4133 seq_printf(m, "%s%s: active %d total %d\n",
4134 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004135 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07004136 created - deleted,
4137 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004138 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004139}
4140
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004141static void print_binder_proc_stats(struct seq_file *m,
4142 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004143{
4144 struct binder_work *w;
4145 struct rb_node *n;
4146 int count, strong, weak;
4147
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004148 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004149 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004150 count = 0;
4151 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
4152 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004153 seq_printf(m, " threads: %d\n", count);
4154 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004155 " ready threads %d\n"
4156 " free async space %zd\n", proc->requested_threads,
4157 proc->requested_threads_started, proc->max_threads,
Todd Kjosd325d372016-10-10 10:40:53 -07004158 proc->ready_threads,
4159 binder_alloc_get_free_async_space(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004160 count = 0;
4161 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
4162 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004163 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004164 count = 0;
4165 strong = 0;
4166 weak = 0;
4167 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
4168 struct binder_ref *ref = rb_entry(n, struct binder_ref,
4169 rb_node_desc);
4170 count++;
Todd Kjosb0117bb2017-05-08 09:16:27 -07004171 strong += ref->data.strong;
4172 weak += ref->data.weak;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004173 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004174 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004175
Todd Kjosd325d372016-10-10 10:40:53 -07004176 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004177 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004178
4179 count = 0;
4180 list_for_each_entry(w, &proc->todo, entry) {
4181 switch (w->type) {
4182 case BINDER_WORK_TRANSACTION:
4183 count++;
4184 break;
4185 default:
4186 break;
4187 }
4188 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004189 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004190
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004191 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004192}
4193
4194
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004195static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004196{
4197 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004198 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004199
Todd Kjos48b33212017-05-24 11:53:13 -07004200 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004201
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004202 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004203
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004204 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004205 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004206 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08004207 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004208 print_binder_node(m, node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004209 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004210
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004211 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004212 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004213 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004214 mutex_unlock(&binder_procs_lock);
Todd Kjos48b33212017-05-24 11:53:13 -07004215 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004216 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004217}
4218
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004219static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004220{
4221 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004222
Todd Kjos48b33212017-05-24 11:53:13 -07004223 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004224
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004225 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004226
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004227 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004228
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004229 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004230 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004231 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004232 mutex_unlock(&binder_procs_lock);
Todd Kjos48b33212017-05-24 11:53:13 -07004233 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004234 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004235}
4236
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004237static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004238{
4239 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004240
Todd Kjos48b33212017-05-24 11:53:13 -07004241 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004242
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004243 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004244 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004245 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004246 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004247 mutex_unlock(&binder_procs_lock);
Todd Kjos48b33212017-05-24 11:53:13 -07004248 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004249 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004250}
4251
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004252static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004253{
Riley Andrews83050a42016-02-09 21:05:33 -08004254 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004255 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004256
Todd Kjos48b33212017-05-24 11:53:13 -07004257 binder_lock(__func__);
Riley Andrews83050a42016-02-09 21:05:33 -08004258
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004259 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08004260 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004261 if (itr->pid == pid) {
4262 seq_puts(m, "binder proc state:\n");
4263 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08004264 }
4265 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004266 mutex_unlock(&binder_procs_lock);
4267
Todd Kjos48b33212017-05-24 11:53:13 -07004268 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004269 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004270}
4271
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004272static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004273 struct binder_transaction_log_entry *e)
4274{
Todd Kjos1cfe6272017-05-24 13:33:28 -07004275 int debug_id = READ_ONCE(e->debug_id_done);
4276 /*
4277 * read barrier to guarantee debug_id_done read before
4278 * we print the log values
4279 */
4280 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004281 seq_printf(m,
Todd Kjos1cfe6272017-05-24 13:33:28 -07004282 "%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 -07004283 e->debug_id, (e->call_type == 2) ? "reply" :
4284 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004285 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07004286 e->to_node, e->target_handle, e->data_size, e->offsets_size,
4287 e->return_error, e->return_error_param,
4288 e->return_error_line);
Todd Kjos1cfe6272017-05-24 13:33:28 -07004289 /*
4290 * read-barrier to guarantee read of debug_id_done after
4291 * done printing the fields of the entry
4292 */
4293 smp_rmb();
4294 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
4295 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004296}
4297
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004298static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004299{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004300 struct binder_transaction_log *log = m->private;
Todd Kjos1cfe6272017-05-24 13:33:28 -07004301 unsigned int log_cur = atomic_read(&log->cur);
4302 unsigned int count;
4303 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004304 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004305
Todd Kjos1cfe6272017-05-24 13:33:28 -07004306 count = log_cur + 1;
4307 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
4308 0 : count % ARRAY_SIZE(log->entry);
4309 if (count > ARRAY_SIZE(log->entry) || log->full)
4310 count = ARRAY_SIZE(log->entry);
4311 for (i = 0; i < count; i++) {
4312 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
4313
4314 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004315 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004316 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004317}
4318
4319static const struct file_operations binder_fops = {
4320 .owner = THIS_MODULE,
4321 .poll = binder_poll,
4322 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004323 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004324 .mmap = binder_mmap,
4325 .open = binder_open,
4326 .flush = binder_flush,
4327 .release = binder_release,
4328};
4329
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004330BINDER_DEBUG_ENTRY(state);
4331BINDER_DEBUG_ENTRY(stats);
4332BINDER_DEBUG_ENTRY(transactions);
4333BINDER_DEBUG_ENTRY(transaction_log);
4334
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004335static int __init init_binder_device(const char *name)
4336{
4337 int ret;
4338 struct binder_device *binder_device;
4339
4340 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
4341 if (!binder_device)
4342 return -ENOMEM;
4343
4344 binder_device->miscdev.fops = &binder_fops;
4345 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
4346 binder_device->miscdev.name = name;
4347
4348 binder_device->context.binder_context_mgr_uid = INVALID_UID;
4349 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07004350 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004351
4352 ret = misc_register(&binder_device->miscdev);
4353 if (ret < 0) {
4354 kfree(binder_device);
4355 return ret;
4356 }
4357
4358 hlist_add_head(&binder_device->hlist, &binder_devices);
4359
4360 return ret;
4361}
4362
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004363static int __init binder_init(void)
4364{
4365 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004366 char *device_name, *device_names;
4367 struct binder_device *device;
4368 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004369
Todd Kjos1cfe6272017-05-24 13:33:28 -07004370 atomic_set(&binder_transaction_log.cur, ~0U);
4371 atomic_set(&binder_transaction_log_failed.cur, ~0U);
4372
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004373 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
4374 if (binder_debugfs_dir_entry_root)
4375 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
4376 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004377
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004378 if (binder_debugfs_dir_entry_root) {
4379 debugfs_create_file("state",
4380 S_IRUGO,
4381 binder_debugfs_dir_entry_root,
4382 NULL,
4383 &binder_state_fops);
4384 debugfs_create_file("stats",
4385 S_IRUGO,
4386 binder_debugfs_dir_entry_root,
4387 NULL,
4388 &binder_stats_fops);
4389 debugfs_create_file("transactions",
4390 S_IRUGO,
4391 binder_debugfs_dir_entry_root,
4392 NULL,
4393 &binder_transactions_fops);
4394 debugfs_create_file("transaction_log",
4395 S_IRUGO,
4396 binder_debugfs_dir_entry_root,
4397 &binder_transaction_log,
4398 &binder_transaction_log_fops);
4399 debugfs_create_file("failed_transaction_log",
4400 S_IRUGO,
4401 binder_debugfs_dir_entry_root,
4402 &binder_transaction_log_failed,
4403 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004404 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004405
4406 /*
4407 * Copy the module_parameter string, because we don't want to
4408 * tokenize it in-place.
4409 */
4410 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
4411 if (!device_names) {
4412 ret = -ENOMEM;
4413 goto err_alloc_device_names_failed;
4414 }
4415 strcpy(device_names, binder_devices_param);
4416
4417 while ((device_name = strsep(&device_names, ","))) {
4418 ret = init_binder_device(device_name);
4419 if (ret)
4420 goto err_init_binder_device_failed;
4421 }
4422
4423 return ret;
4424
4425err_init_binder_device_failed:
4426 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
4427 misc_deregister(&device->miscdev);
4428 hlist_del(&device->hlist);
4429 kfree(device);
4430 }
4431err_alloc_device_names_failed:
4432 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
4433
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004434 return ret;
4435}
4436
4437device_initcall(binder_init);
4438
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004439#define CREATE_TRACE_POINTS
4440#include "binder_trace.h"
4441
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004442MODULE_LICENSE("GPL v2");