blob: cb78a4e6872de0604107bbbd59aefccd77d3f3f0 [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010033#include <linux/sched/signal.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010034#include <linux/sched/mm.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070035#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090036#include <linux/uaccess.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080037#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050038#include <linux/security.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090039
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020040#ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
41#define BINDER_IPC_32BIT 1
42#endif
43
44#include <uapi/linux/android/binder.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070045#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070046#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090047
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070048static DEFINE_MUTEX(binder_main_lock);
Todd Kjosc44b1232017-06-29 12:01:43 -070049
50static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090051static DEFINE_MUTEX(binder_deferred_lock);
52
Martijn Coenenac4812c2017-02-03 14:40:48 -080053static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090054static HLIST_HEAD(binder_procs);
Todd Kjosc44b1232017-06-29 12:01:43 -070055static DEFINE_MUTEX(binder_procs_lock);
56
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090057static HLIST_HEAD(binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -070058static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090059
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070060static struct dentry *binder_debugfs_dir_entry_root;
61static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjos656a8002017-06-29 12:01:45 -070062static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090063
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070064#define BINDER_DEBUG_ENTRY(name) \
65static int binder_##name##_open(struct inode *inode, struct file *file) \
66{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070067 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070068} \
69\
70static const struct file_operations binder_##name##_fops = { \
71 .owner = THIS_MODULE, \
72 .open = binder_##name##_open, \
73 .read = seq_read, \
74 .llseek = seq_lseek, \
75 .release = single_release, \
76}
77
78static int binder_proc_show(struct seq_file *m, void *unused);
79BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090080
81/* This is only defined in include/asm-arm/sizes.h */
82#ifndef SZ_1K
83#define SZ_1K 0x400
84#endif
85
86#ifndef SZ_4M
87#define SZ_4M 0x400000
88#endif
89
90#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
91
92#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
93
94enum {
95 BINDER_DEBUG_USER_ERROR = 1U << 0,
96 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
97 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
98 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
99 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
100 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
101 BINDER_DEBUG_READ_WRITE = 1U << 6,
102 BINDER_DEBUG_USER_REFS = 1U << 7,
103 BINDER_DEBUG_THREADS = 1U << 8,
104 BINDER_DEBUG_TRANSACTION = 1U << 9,
105 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
106 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
107 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjos19c98722017-06-29 12:01:40 -0700108 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900109};
110static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
111 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
112module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
113
Martijn Coenenac4812c2017-02-03 14:40:48 -0800114static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
115module_param_named(devices, binder_devices_param, charp, 0444);
116
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900117static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
118static int binder_stop_on_user_error;
119
120static int binder_set_stop_on_user_error(const char *val,
121 struct kernel_param *kp)
122{
123 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900124
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900125 ret = param_set_int(val, kp);
126 if (binder_stop_on_user_error < 2)
127 wake_up(&binder_user_error_wait);
128 return ret;
129}
130module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
131 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
132
133#define binder_debug(mask, x...) \
134 do { \
135 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400136 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900137 } while (0)
138
139#define binder_user_error(x...) \
140 do { \
141 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400142 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900143 if (binder_stop_on_user_error) \
144 binder_stop_on_user_error = 2; \
145 } while (0)
146
Martijn Coenenfeba3902017-02-03 14:40:45 -0800147#define to_flat_binder_object(hdr) \
148 container_of(hdr, struct flat_binder_object, hdr)
149
150#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
151
Martijn Coenen79802402017-02-03 14:40:51 -0800152#define to_binder_buffer_object(hdr) \
153 container_of(hdr, struct binder_buffer_object, hdr)
154
Martijn Coenendef95c72017-02-03 14:40:52 -0800155#define to_binder_fd_array_object(hdr) \
156 container_of(hdr, struct binder_fd_array_object, hdr)
157
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900158enum binder_stat_types {
159 BINDER_STAT_PROC,
160 BINDER_STAT_THREAD,
161 BINDER_STAT_NODE,
162 BINDER_STAT_REF,
163 BINDER_STAT_DEATH,
164 BINDER_STAT_TRANSACTION,
165 BINDER_STAT_TRANSACTION_COMPLETE,
166 BINDER_STAT_COUNT
167};
168
169struct binder_stats {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700170 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
171 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
172 atomic_t obj_created[BINDER_STAT_COUNT];
173 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900174};
175
176static struct binder_stats binder_stats;
177
178static inline void binder_stats_deleted(enum binder_stat_types type)
179{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700180 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900181}
182
183static inline void binder_stats_created(enum binder_stat_types type)
184{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700185 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900186}
187
188struct binder_transaction_log_entry {
189 int debug_id;
Todd Kjosd99c7332017-06-29 12:01:53 -0700190 int debug_id_done;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900191 int call_type;
192 int from_proc;
193 int from_thread;
194 int target_handle;
195 int to_proc;
196 int to_thread;
197 int to_node;
198 int data_size;
199 int offsets_size;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700200 int return_error_line;
201 uint32_t return_error;
202 uint32_t return_error_param;
Martijn Coenen14db3182017-02-03 14:40:47 -0800203 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900204};
205struct binder_transaction_log {
Todd Kjosd99c7332017-06-29 12:01:53 -0700206 atomic_t cur;
207 bool full;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900208 struct binder_transaction_log_entry entry[32];
209};
210static struct binder_transaction_log binder_transaction_log;
211static struct binder_transaction_log binder_transaction_log_failed;
212
213static struct binder_transaction_log_entry *binder_transaction_log_add(
214 struct binder_transaction_log *log)
215{
216 struct binder_transaction_log_entry *e;
Todd Kjosd99c7332017-06-29 12:01:53 -0700217 unsigned int cur = atomic_inc_return(&log->cur);
Seunghun Lee10f62862014-05-01 01:30:23 +0900218
Todd Kjosd99c7332017-06-29 12:01:53 -0700219 if (cur >= ARRAY_SIZE(log->entry))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900220 log->full = 1;
Todd Kjosd99c7332017-06-29 12:01:53 -0700221 e = &log->entry[cur % ARRAY_SIZE(log->entry)];
222 WRITE_ONCE(e->debug_id_done, 0);
223 /*
224 * write-barrier to synchronize access to e->debug_id_done.
225 * We make sure the initialized 0 value is seen before
226 * memset() other fields are zeroed by memset.
227 */
228 smp_wmb();
229 memset(e, 0, sizeof(*e));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900230 return e;
231}
232
Martijn Coenen342e5c92017-02-03 14:40:46 -0800233struct binder_context {
234 struct binder_node *binder_context_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -0700235 struct mutex context_mgr_node_lock;
236
Martijn Coenen342e5c92017-02-03 14:40:46 -0800237 kuid_t binder_context_mgr_uid;
Martijn Coenen14db3182017-02-03 14:40:47 -0800238 const char *name;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800239};
240
Martijn Coenenac4812c2017-02-03 14:40:48 -0800241struct binder_device {
242 struct hlist_node hlist;
243 struct miscdevice miscdev;
244 struct binder_context context;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800245};
246
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900247struct binder_work {
248 struct list_head entry;
249 enum {
250 BINDER_WORK_TRANSACTION = 1,
251 BINDER_WORK_TRANSACTION_COMPLETE,
252 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
259struct binder_node {
260 int debug_id;
261 struct binder_work work;
262 union {
263 struct rb_node rb_node;
264 struct hlist_node dead_node;
265 };
266 struct binder_proc *proc;
267 struct hlist_head refs;
268 int internal_strong_refs;
269 int local_weak_refs;
270 int local_strong_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800271 binder_uintptr_t ptr;
272 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900273 unsigned has_strong_ref:1;
274 unsigned pending_strong_ref:1;
275 unsigned has_weak_ref:1;
276 unsigned pending_weak_ref:1;
277 unsigned has_async_transaction:1;
278 unsigned accept_fds:1;
279 unsigned min_priority:8;
280 struct list_head async_todo;
281};
282
283struct binder_ref_death {
284 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800285 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900286};
287
288struct binder_ref {
289 /* Lookups needed: */
290 /* node + proc => ref (transaction) */
291 /* desc + proc => ref (transaction, inc/dec ref) */
292 /* node => refs + procs (proc exit) */
293 int debug_id;
294 struct rb_node rb_node_desc;
295 struct rb_node rb_node_node;
296 struct hlist_node node_entry;
297 struct binder_proc *proc;
298 struct binder_node *node;
299 uint32_t desc;
300 int strong;
301 int weak;
302 struct binder_ref_death *death;
303};
304
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900305enum binder_deferred_state {
306 BINDER_DEFERRED_PUT_FILES = 0x01,
307 BINDER_DEFERRED_FLUSH = 0x02,
308 BINDER_DEFERRED_RELEASE = 0x04,
309};
310
311struct binder_proc {
312 struct hlist_node proc_node;
313 struct rb_root threads;
314 struct rb_root nodes;
315 struct rb_root refs_by_desc;
316 struct rb_root refs_by_node;
317 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900318 struct task_struct *tsk;
319 struct files_struct *files;
320 struct hlist_node deferred_work_node;
321 int deferred_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900322
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900323 struct list_head todo;
324 wait_queue_head_t wait;
325 struct binder_stats stats;
326 struct list_head delivered_death;
327 int max_threads;
328 int requested_threads;
329 int requested_threads_started;
330 int ready_threads;
331 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700332 struct dentry *debugfs_entry;
Todd Kjosfdfb4a92017-06-29 12:01:38 -0700333 struct binder_alloc alloc;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800334 struct binder_context *context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900335};
336
337enum {
338 BINDER_LOOPER_STATE_REGISTERED = 0x01,
339 BINDER_LOOPER_STATE_ENTERED = 0x02,
340 BINDER_LOOPER_STATE_EXITED = 0x04,
341 BINDER_LOOPER_STATE_INVALID = 0x08,
342 BINDER_LOOPER_STATE_WAITING = 0x10,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900343};
344
345struct binder_thread {
346 struct binder_proc *proc;
347 struct rb_node rb_node;
348 int pid;
Todd Kjos08dabce2017-06-29 12:01:49 -0700349 int looper; /* only modified by this thread */
350 bool looper_need_return; /* can be written by other thread */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900351 struct binder_transaction *transaction_stack;
352 struct list_head todo;
353 uint32_t return_error; /* Write failed, return error code in read buf */
354 uint32_t return_error2; /* Write failed, return error code in read */
355 /* buffer. Used when sending a reply to a dead process that */
356 /* we are also waiting on */
357 wait_queue_head_t wait;
358 struct binder_stats stats;
359};
360
361struct binder_transaction {
362 int debug_id;
363 struct binder_work work;
364 struct binder_thread *from;
365 struct binder_transaction *from_parent;
366 struct binder_proc *to_proc;
367 struct binder_thread *to_thread;
368 struct binder_transaction *to_parent;
369 unsigned need_reply:1;
370 /* unsigned is_dead:1; */ /* not used at the moment */
371
372 struct binder_buffer *buffer;
373 unsigned int code;
374 unsigned int flags;
375 long priority;
376 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600377 kuid_t sender_euid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900378};
379
380static void
381binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
382
Sachin Kamatefde99c2012-08-17 16:39:36 +0530383static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900384{
385 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900386 unsigned long rlim_cur;
387 unsigned long irqs;
388
389 if (files == NULL)
390 return -ESRCH;
391
Al Virodcfadfa2012-08-12 17:27:30 -0400392 if (!lock_task_sighand(proc->tsk, &irqs))
393 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900394
Al Virodcfadfa2012-08-12 17:27:30 -0400395 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
396 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900397
Al Virodcfadfa2012-08-12 17:27:30 -0400398 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900399}
400
401/*
402 * copied from fd_install
403 */
404static void task_fd_install(
405 struct binder_proc *proc, unsigned int fd, struct file *file)
406{
Al Virof869e8a2012-08-15 21:06:33 -0400407 if (proc->files)
408 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900409}
410
411/*
412 * copied from sys_close
413 */
414static long task_close_fd(struct binder_proc *proc, unsigned int fd)
415{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900416 int retval;
417
Al Viro483ce1d2012-08-19 12:04:24 -0400418 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900419 return -ESRCH;
420
Al Viro483ce1d2012-08-19 12:04:24 -0400421 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900422 /* can't restart close syscall because file table entry was cleared */
423 if (unlikely(retval == -ERESTARTSYS ||
424 retval == -ERESTARTNOINTR ||
425 retval == -ERESTARTNOHAND ||
426 retval == -ERESTART_RESTARTBLOCK))
427 retval = -EINTR;
428
429 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900430}
431
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700432static inline void binder_lock(const char *tag)
433{
434 trace_binder_lock(tag);
435 mutex_lock(&binder_main_lock);
436 trace_binder_locked(tag);
437}
438
439static inline void binder_unlock(const char *tag)
440{
441 trace_binder_unlock(tag);
442 mutex_unlock(&binder_main_lock);
443}
444
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900445static void binder_set_nice(long nice)
446{
447 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900448
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900449 if (can_nice(current, nice)) {
450 set_user_nice(current, nice);
451 return;
452 }
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900453 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900454 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530455 "%d: nice value %ld not allowed use %ld instead\n",
456 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900457 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +0800458 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900459 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530460 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900461}
462
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900463static struct binder_node *binder_get_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800464 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900465{
466 struct rb_node *n = proc->nodes.rb_node;
467 struct binder_node *node;
468
469 while (n) {
470 node = rb_entry(n, struct binder_node, rb_node);
471
472 if (ptr < node->ptr)
473 n = n->rb_left;
474 else if (ptr > node->ptr)
475 n = n->rb_right;
476 else
477 return node;
478 }
479 return NULL;
480}
481
482static struct binder_node *binder_new_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800483 binder_uintptr_t ptr,
484 binder_uintptr_t cookie)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900485{
486 struct rb_node **p = &proc->nodes.rb_node;
487 struct rb_node *parent = NULL;
488 struct binder_node *node;
489
490 while (*p) {
491 parent = *p;
492 node = rb_entry(parent, struct binder_node, rb_node);
493
494 if (ptr < node->ptr)
495 p = &(*p)->rb_left;
496 else if (ptr > node->ptr)
497 p = &(*p)->rb_right;
498 else
499 return NULL;
500 }
501
502 node = kzalloc(sizeof(*node), GFP_KERNEL);
503 if (node == NULL)
504 return NULL;
505 binder_stats_created(BINDER_STAT_NODE);
506 rb_link_node(&node->rb_node, parent, p);
507 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjos656a8002017-06-29 12:01:45 -0700508 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900509 node->proc = proc;
510 node->ptr = ptr;
511 node->cookie = cookie;
512 node->work.type = BINDER_WORK_NODE;
513 INIT_LIST_HEAD(&node->work.entry);
514 INIT_LIST_HEAD(&node->async_todo);
515 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800516 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900517 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800518 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900519 return node;
520}
521
522static int binder_inc_node(struct binder_node *node, int strong, int internal,
523 struct list_head *target_list)
524{
525 if (strong) {
526 if (internal) {
527 if (target_list == NULL &&
528 node->internal_strong_refs == 0 &&
Martijn Coenen342e5c92017-02-03 14:40:46 -0800529 !(node->proc &&
530 node == node->proc->context->binder_context_mgr_node &&
531 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530532 pr_err("invalid inc strong node for %d\n",
533 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900534 return -EINVAL;
535 }
536 node->internal_strong_refs++;
537 } else
538 node->local_strong_refs++;
539 if (!node->has_strong_ref && target_list) {
540 list_del_init(&node->work.entry);
541 list_add_tail(&node->work.entry, target_list);
542 }
543 } else {
544 if (!internal)
545 node->local_weak_refs++;
546 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
547 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530548 pr_err("invalid inc weak node for %d\n",
549 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900550 return -EINVAL;
551 }
552 list_add_tail(&node->work.entry, target_list);
553 }
554 }
555 return 0;
556}
557
558static int binder_dec_node(struct binder_node *node, int strong, int internal)
559{
560 if (strong) {
561 if (internal)
562 node->internal_strong_refs--;
563 else
564 node->local_strong_refs--;
565 if (node->local_strong_refs || node->internal_strong_refs)
566 return 0;
567 } else {
568 if (!internal)
569 node->local_weak_refs--;
570 if (node->local_weak_refs || !hlist_empty(&node->refs))
571 return 0;
572 }
573 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
574 if (list_empty(&node->work.entry)) {
575 list_add_tail(&node->work.entry, &node->proc->todo);
576 wake_up_interruptible(&node->proc->wait);
577 }
578 } else {
579 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
580 !node->local_weak_refs) {
581 list_del_init(&node->work.entry);
582 if (node->proc) {
583 rb_erase(&node->rb_node, &node->proc->nodes);
584 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530585 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900586 node->debug_id);
587 } else {
Todd Kjosc44b1232017-06-29 12:01:43 -0700588 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900589 hlist_del(&node->dead_node);
Todd Kjosc44b1232017-06-29 12:01:43 -0700590 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900591 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530592 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900593 node->debug_id);
594 }
595 kfree(node);
596 binder_stats_deleted(BINDER_STAT_NODE);
597 }
598 }
599
600 return 0;
601}
602
603
604static struct binder_ref *binder_get_ref(struct binder_proc *proc,
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200605 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900606{
607 struct rb_node *n = proc->refs_by_desc.rb_node;
608 struct binder_ref *ref;
609
610 while (n) {
611 ref = rb_entry(n, struct binder_ref, rb_node_desc);
612
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200613 if (desc < ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900614 n = n->rb_left;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200615 } else if (desc > ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900616 n = n->rb_right;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200617 } else if (need_strong_ref && !ref->strong) {
618 binder_user_error("tried to use weak ref as strong ref\n");
619 return NULL;
620 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900621 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200622 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900623 }
624 return NULL;
625}
626
627static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
628 struct binder_node *node)
629{
630 struct rb_node *n;
631 struct rb_node **p = &proc->refs_by_node.rb_node;
632 struct rb_node *parent = NULL;
633 struct binder_ref *ref, *new_ref;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800634 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900635
636 while (*p) {
637 parent = *p;
638 ref = rb_entry(parent, struct binder_ref, rb_node_node);
639
640 if (node < ref->node)
641 p = &(*p)->rb_left;
642 else if (node > ref->node)
643 p = &(*p)->rb_right;
644 else
645 return ref;
646 }
647 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
648 if (new_ref == NULL)
649 return NULL;
650 binder_stats_created(BINDER_STAT_REF);
Todd Kjos656a8002017-06-29 12:01:45 -0700651 new_ref->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900652 new_ref->proc = proc;
653 new_ref->node = node;
654 rb_link_node(&new_ref->rb_node_node, parent, p);
655 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
656
Martijn Coenen342e5c92017-02-03 14:40:46 -0800657 new_ref->desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900658 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
659 ref = rb_entry(n, struct binder_ref, rb_node_desc);
660 if (ref->desc > new_ref->desc)
661 break;
662 new_ref->desc = ref->desc + 1;
663 }
664
665 p = &proc->refs_by_desc.rb_node;
666 while (*p) {
667 parent = *p;
668 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
669
670 if (new_ref->desc < ref->desc)
671 p = &(*p)->rb_left;
672 else if (new_ref->desc > ref->desc)
673 p = &(*p)->rb_right;
674 else
675 BUG();
676 }
677 rb_link_node(&new_ref->rb_node_desc, parent, p);
678 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
Todd Kjose4cffcf2017-06-29 12:01:50 -0700679 hlist_add_head(&new_ref->node_entry, &node->refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900680
Todd Kjose4cffcf2017-06-29 12:01:50 -0700681 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
682 "%d new ref %d desc %d for node %d\n",
683 proc->pid, new_ref->debug_id, new_ref->desc,
684 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900685 return new_ref;
686}
687
688static void binder_delete_ref(struct binder_ref *ref)
689{
690 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530691 "%d delete ref %d desc %d for node %d\n",
692 ref->proc->pid, ref->debug_id, ref->desc,
693 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900694
695 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
696 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
697 if (ref->strong)
698 binder_dec_node(ref->node, 1, 1);
699 hlist_del(&ref->node_entry);
700 binder_dec_node(ref->node, 0, 1);
701 if (ref->death) {
702 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530703 "%d delete ref %d desc %d has death notification\n",
704 ref->proc->pid, ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900705 list_del(&ref->death->work.entry);
706 kfree(ref->death);
707 binder_stats_deleted(BINDER_STAT_DEATH);
708 }
709 kfree(ref);
710 binder_stats_deleted(BINDER_STAT_REF);
711}
712
713static int binder_inc_ref(struct binder_ref *ref, int strong,
714 struct list_head *target_list)
715{
716 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900717
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900718 if (strong) {
719 if (ref->strong == 0) {
720 ret = binder_inc_node(ref->node, 1, 1, target_list);
721 if (ret)
722 return ret;
723 }
724 ref->strong++;
725 } else {
726 if (ref->weak == 0) {
727 ret = binder_inc_node(ref->node, 0, 1, target_list);
728 if (ret)
729 return ret;
730 }
731 ref->weak++;
732 }
733 return 0;
734}
735
736
737static int binder_dec_ref(struct binder_ref *ref, int strong)
738{
739 if (strong) {
740 if (ref->strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530741 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900742 ref->proc->pid, ref->debug_id,
743 ref->desc, ref->strong, ref->weak);
744 return -EINVAL;
745 }
746 ref->strong--;
747 if (ref->strong == 0) {
748 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900749
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900750 ret = binder_dec_node(ref->node, strong, 1);
751 if (ret)
752 return ret;
753 }
754 } else {
755 if (ref->weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530756 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900757 ref->proc->pid, ref->debug_id,
758 ref->desc, ref->strong, ref->weak);
759 return -EINVAL;
760 }
761 ref->weak--;
762 }
763 if (ref->strong == 0 && ref->weak == 0)
764 binder_delete_ref(ref);
765 return 0;
766}
767
768static void binder_pop_transaction(struct binder_thread *target_thread,
769 struct binder_transaction *t)
770{
771 if (target_thread) {
772 BUG_ON(target_thread->transaction_stack != t);
773 BUG_ON(target_thread->transaction_stack->from != target_thread);
774 target_thread->transaction_stack =
775 target_thread->transaction_stack->from_parent;
776 t->from = NULL;
777 }
778 t->need_reply = 0;
779 if (t->buffer)
780 t->buffer->transaction = NULL;
781 kfree(t);
782 binder_stats_deleted(BINDER_STAT_TRANSACTION);
783}
784
785static void binder_send_failed_reply(struct binder_transaction *t,
786 uint32_t error_code)
787{
788 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -0300789 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +0900790
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900791 BUG_ON(t->flags & TF_ONE_WAY);
792 while (1) {
793 target_thread = t->from;
794 if (target_thread) {
795 if (target_thread->return_error != BR_OK &&
796 target_thread->return_error2 == BR_OK) {
797 target_thread->return_error2 =
798 target_thread->return_error;
799 target_thread->return_error = BR_OK;
800 }
801 if (target_thread->return_error == BR_OK) {
802 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530803 "send failed reply for transaction %d to %d:%d\n",
William Panlener0232a422014-09-03 22:44:03 -0500804 t->debug_id,
805 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900806 target_thread->pid);
807
808 binder_pop_transaction(target_thread, t);
809 target_thread->return_error = error_code;
810 wake_up_interruptible(&target_thread->wait);
811 } else {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530812 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
813 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900814 target_thread->pid,
815 target_thread->return_error);
816 }
817 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900818 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -0300819 next = t->from_parent;
820
821 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
822 "send failed reply for transaction %d, target dead\n",
823 t->debug_id);
824
825 binder_pop_transaction(target_thread, t);
826 if (next == NULL) {
827 binder_debug(BINDER_DEBUG_DEAD_BINDER,
828 "reply failed, no target thread at root\n");
829 return;
830 }
831 t = next;
832 binder_debug(BINDER_DEBUG_DEAD_BINDER,
833 "reply failed, no target thread -- retry %d\n",
834 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900835 }
836}
837
Martijn Coenenfeba3902017-02-03 14:40:45 -0800838/**
839 * binder_validate_object() - checks for a valid metadata object in a buffer.
840 * @buffer: binder_buffer that we're parsing.
841 * @offset: offset in the buffer at which to validate an object.
842 *
843 * Return: If there's a valid metadata object at @offset in @buffer, the
844 * size of that object. Otherwise, it returns zero.
845 */
846static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
847{
848 /* Check if we can read a header first */
849 struct binder_object_header *hdr;
850 size_t object_size = 0;
851
852 if (offset > buffer->data_size - sizeof(*hdr) ||
853 buffer->data_size < sizeof(*hdr) ||
854 !IS_ALIGNED(offset, sizeof(u32)))
855 return 0;
856
857 /* Ok, now see if we can read a complete object. */
858 hdr = (struct binder_object_header *)(buffer->data + offset);
859 switch (hdr->type) {
860 case BINDER_TYPE_BINDER:
861 case BINDER_TYPE_WEAK_BINDER:
862 case BINDER_TYPE_HANDLE:
863 case BINDER_TYPE_WEAK_HANDLE:
864 object_size = sizeof(struct flat_binder_object);
865 break;
866 case BINDER_TYPE_FD:
867 object_size = sizeof(struct binder_fd_object);
868 break;
Martijn Coenen79802402017-02-03 14:40:51 -0800869 case BINDER_TYPE_PTR:
870 object_size = sizeof(struct binder_buffer_object);
871 break;
Martijn Coenendef95c72017-02-03 14:40:52 -0800872 case BINDER_TYPE_FDA:
873 object_size = sizeof(struct binder_fd_array_object);
874 break;
Martijn Coenenfeba3902017-02-03 14:40:45 -0800875 default:
876 return 0;
877 }
878 if (offset <= buffer->data_size - object_size &&
879 buffer->data_size >= object_size)
880 return object_size;
881 else
882 return 0;
883}
884
Martijn Coenen79802402017-02-03 14:40:51 -0800885/**
886 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
887 * @b: binder_buffer containing the object
888 * @index: index in offset array at which the binder_buffer_object is
889 * located
890 * @start: points to the start of the offset array
891 * @num_valid: the number of valid offsets in the offset array
892 *
893 * Return: If @index is within the valid range of the offset array
894 * described by @start and @num_valid, and if there's a valid
895 * binder_buffer_object at the offset found in index @index
896 * of the offset array, that object is returned. Otherwise,
897 * %NULL is returned.
898 * Note that the offset found in index @index itself is not
899 * verified; this function assumes that @num_valid elements
900 * from @start were previously verified to have valid offsets.
901 */
902static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
903 binder_size_t index,
904 binder_size_t *start,
905 binder_size_t num_valid)
906{
907 struct binder_buffer_object *buffer_obj;
908 binder_size_t *offp;
909
910 if (index >= num_valid)
911 return NULL;
912
913 offp = start + index;
914 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
915 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
916 return NULL;
917
918 return buffer_obj;
919}
920
921/**
922 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
923 * @b: transaction buffer
924 * @objects_start start of objects buffer
925 * @buffer: binder_buffer_object in which to fix up
926 * @offset: start offset in @buffer to fix up
927 * @last_obj: last binder_buffer_object that we fixed up in
928 * @last_min_offset: minimum fixup offset in @last_obj
929 *
930 * Return: %true if a fixup in buffer @buffer at offset @offset is
931 * allowed.
932 *
933 * For safety reasons, we only allow fixups inside a buffer to happen
934 * at increasing offsets; additionally, we only allow fixup on the last
935 * buffer object that was verified, or one of its parents.
936 *
937 * Example of what is allowed:
938 *
939 * A
940 * B (parent = A, offset = 0)
941 * C (parent = A, offset = 16)
942 * D (parent = C, offset = 0)
943 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
944 *
945 * Examples of what is not allowed:
946 *
947 * Decreasing offsets within the same parent:
948 * A
949 * C (parent = A, offset = 16)
950 * B (parent = A, offset = 0) // decreasing offset within A
951 *
952 * Referring to a parent that wasn't the last object or any of its parents:
953 * A
954 * B (parent = A, offset = 0)
955 * C (parent = A, offset = 0)
956 * C (parent = A, offset = 16)
957 * D (parent = B, offset = 0) // B is not A or any of A's parents
958 */
959static bool binder_validate_fixup(struct binder_buffer *b,
960 binder_size_t *objects_start,
961 struct binder_buffer_object *buffer,
962 binder_size_t fixup_offset,
963 struct binder_buffer_object *last_obj,
964 binder_size_t last_min_offset)
965{
966 if (!last_obj) {
967 /* Nothing to fix up in */
968 return false;
969 }
970
971 while (last_obj != buffer) {
972 /*
973 * Safe to retrieve the parent of last_obj, since it
974 * was already previously verified by the driver.
975 */
976 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
977 return false;
978 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
979 last_obj = (struct binder_buffer_object *)
980 (b->data + *(objects_start + last_obj->parent));
981 }
982 return (fixup_offset >= last_min_offset);
983}
984
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900985static void binder_transaction_buffer_release(struct binder_proc *proc,
986 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800987 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900988{
Martijn Coenen79802402017-02-03 14:40:51 -0800989 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900990 int debug_id = buffer->debug_id;
991
992 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530993 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900994 proc->pid, buffer->debug_id,
995 buffer->data_size, buffer->offsets_size, failed_at);
996
997 if (buffer->target_node)
998 binder_dec_node(buffer->target_node, 1, 0);
999
Martijn Coenen79802402017-02-03 14:40:51 -08001000 off_start = (binder_size_t *)(buffer->data +
1001 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001002 if (failed_at)
1003 off_end = failed_at;
1004 else
Martijn Coenen79802402017-02-03 14:40:51 -08001005 off_end = (void *)off_start + buffer->offsets_size;
1006 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001007 struct binder_object_header *hdr;
1008 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09001009
Martijn Coenenfeba3902017-02-03 14:40:45 -08001010 if (object_size == 0) {
1011 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001012 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001013 continue;
1014 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08001015 hdr = (struct binder_object_header *)(buffer->data + *offp);
1016 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001017 case BINDER_TYPE_BINDER:
1018 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001019 struct flat_binder_object *fp;
1020 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09001021
Martijn Coenenfeba3902017-02-03 14:40:45 -08001022 fp = to_flat_binder_object(hdr);
1023 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001024 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001025 pr_err("transaction release %d bad node %016llx\n",
1026 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001027 break;
1028 }
1029 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001030 " node %d u%016llx\n",
1031 node->debug_id, (u64)node->ptr);
Martijn Coenenfeba3902017-02-03 14:40:45 -08001032 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
1033 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001034 } break;
1035 case BINDER_TYPE_HANDLE:
1036 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001037 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001038 struct binder_ref *ref;
1039
Martijn Coenenfeba3902017-02-03 14:40:45 -08001040 fp = to_flat_binder_object(hdr);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001041 ref = binder_get_ref(proc, fp->handle,
Martijn Coenenfeba3902017-02-03 14:40:45 -08001042 hdr->type == BINDER_TYPE_HANDLE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001043 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001044 pr_err("transaction release %d bad handle %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301045 debug_id, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001046 break;
1047 }
1048 binder_debug(BINDER_DEBUG_TRANSACTION,
1049 " ref %d desc %d (node %d)\n",
1050 ref->debug_id, ref->desc, ref->node->debug_id);
Martijn Coenenfeba3902017-02-03 14:40:45 -08001051 binder_dec_ref(ref, hdr->type == BINDER_TYPE_HANDLE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001052 } break;
1053
Martijn Coenenfeba3902017-02-03 14:40:45 -08001054 case BINDER_TYPE_FD: {
1055 struct binder_fd_object *fp = to_binder_fd_object(hdr);
1056
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001057 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenenfeba3902017-02-03 14:40:45 -08001058 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001059 if (failed_at)
Martijn Coenenfeba3902017-02-03 14:40:45 -08001060 task_close_fd(proc, fp->fd);
1061 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08001062 case BINDER_TYPE_PTR:
1063 /*
1064 * Nothing to do here, this will get cleaned up when the
1065 * transaction buffer gets freed
1066 */
1067 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08001068 case BINDER_TYPE_FDA: {
1069 struct binder_fd_array_object *fda;
1070 struct binder_buffer_object *parent;
1071 uintptr_t parent_buffer;
1072 u32 *fd_array;
1073 size_t fd_index;
1074 binder_size_t fd_buf_size;
1075
1076 fda = to_binder_fd_array_object(hdr);
1077 parent = binder_validate_ptr(buffer, fda->parent,
1078 off_start,
1079 offp - off_start);
1080 if (!parent) {
1081 pr_err("transaction release %d bad parent offset",
1082 debug_id);
1083 continue;
1084 }
1085 /*
1086 * Since the parent was already fixed up, convert it
1087 * back to kernel address space to access it
1088 */
1089 parent_buffer = parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07001090 binder_alloc_get_user_buffer_offset(
1091 &proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08001092
1093 fd_buf_size = sizeof(u32) * fda->num_fds;
1094 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
1095 pr_err("transaction release %d invalid number of fds (%lld)\n",
1096 debug_id, (u64)fda->num_fds);
1097 continue;
1098 }
1099 if (fd_buf_size > parent->length ||
1100 fda->parent_offset > parent->length - fd_buf_size) {
1101 /* No space for all file descriptors here. */
1102 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
1103 debug_id, (u64)fda->num_fds);
1104 continue;
1105 }
1106 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
1107 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
1108 task_close_fd(proc, fd_array[fd_index]);
1109 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001110 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001111 pr_err("transaction release %d bad object type %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08001112 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001113 break;
1114 }
1115 }
1116}
1117
Martijn Coenena056af42017-02-03 14:40:49 -08001118static int binder_translate_binder(struct flat_binder_object *fp,
1119 struct binder_transaction *t,
1120 struct binder_thread *thread)
1121{
1122 struct binder_node *node;
1123 struct binder_ref *ref;
1124 struct binder_proc *proc = thread->proc;
1125 struct binder_proc *target_proc = t->to_proc;
1126
1127 node = binder_get_node(proc, fp->binder);
1128 if (!node) {
1129 node = binder_new_node(proc, fp->binder, fp->cookie);
1130 if (!node)
1131 return -ENOMEM;
1132
1133 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1134 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1135 }
1136 if (fp->cookie != node->cookie) {
1137 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1138 proc->pid, thread->pid, (u64)fp->binder,
1139 node->debug_id, (u64)fp->cookie,
1140 (u64)node->cookie);
1141 return -EINVAL;
1142 }
1143 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk))
1144 return -EPERM;
1145
1146 ref = binder_get_ref_for_node(target_proc, node);
1147 if (!ref)
Todd Kjos57ada2f2017-06-29 12:01:46 -07001148 return -ENOMEM;
Martijn Coenena056af42017-02-03 14:40:49 -08001149
1150 if (fp->hdr.type == BINDER_TYPE_BINDER)
1151 fp->hdr.type = BINDER_TYPE_HANDLE;
1152 else
1153 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
1154 fp->binder = 0;
1155 fp->handle = ref->desc;
1156 fp->cookie = 0;
1157 binder_inc_ref(ref, fp->hdr.type == BINDER_TYPE_HANDLE, &thread->todo);
1158
1159 trace_binder_transaction_node_to_ref(t, node, ref);
1160 binder_debug(BINDER_DEBUG_TRANSACTION,
1161 " node %d u%016llx -> ref %d desc %d\n",
1162 node->debug_id, (u64)node->ptr,
1163 ref->debug_id, ref->desc);
1164
1165 return 0;
1166}
1167
1168static int binder_translate_handle(struct flat_binder_object *fp,
1169 struct binder_transaction *t,
1170 struct binder_thread *thread)
1171{
1172 struct binder_ref *ref;
1173 struct binder_proc *proc = thread->proc;
1174 struct binder_proc *target_proc = t->to_proc;
1175
1176 ref = binder_get_ref(proc, fp->handle,
1177 fp->hdr.type == BINDER_TYPE_HANDLE);
1178 if (!ref) {
1179 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
1180 proc->pid, thread->pid, fp->handle);
1181 return -EINVAL;
1182 }
1183 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk))
1184 return -EPERM;
1185
1186 if (ref->node->proc == target_proc) {
1187 if (fp->hdr.type == BINDER_TYPE_HANDLE)
1188 fp->hdr.type = BINDER_TYPE_BINDER;
1189 else
1190 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
1191 fp->binder = ref->node->ptr;
1192 fp->cookie = ref->node->cookie;
1193 binder_inc_node(ref->node, fp->hdr.type == BINDER_TYPE_BINDER,
1194 0, NULL);
1195 trace_binder_transaction_ref_to_node(t, ref);
1196 binder_debug(BINDER_DEBUG_TRANSACTION,
1197 " ref %d desc %d -> node %d u%016llx\n",
1198 ref->debug_id, ref->desc, ref->node->debug_id,
1199 (u64)ref->node->ptr);
1200 } else {
1201 struct binder_ref *new_ref;
1202
1203 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1204 if (!new_ref)
Todd Kjos57ada2f2017-06-29 12:01:46 -07001205 return -ENOMEM;
Martijn Coenena056af42017-02-03 14:40:49 -08001206
1207 fp->binder = 0;
1208 fp->handle = new_ref->desc;
1209 fp->cookie = 0;
1210 binder_inc_ref(new_ref, fp->hdr.type == BINDER_TYPE_HANDLE,
1211 NULL);
1212 trace_binder_transaction_ref_to_ref(t, ref, new_ref);
1213 binder_debug(BINDER_DEBUG_TRANSACTION,
1214 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1215 ref->debug_id, ref->desc, new_ref->debug_id,
1216 new_ref->desc, ref->node->debug_id);
1217 }
1218 return 0;
1219}
1220
1221static int binder_translate_fd(int fd,
1222 struct binder_transaction *t,
1223 struct binder_thread *thread,
1224 struct binder_transaction *in_reply_to)
1225{
1226 struct binder_proc *proc = thread->proc;
1227 struct binder_proc *target_proc = t->to_proc;
1228 int target_fd;
1229 struct file *file;
1230 int ret;
1231 bool target_allows_fd;
1232
1233 if (in_reply_to)
1234 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
1235 else
1236 target_allows_fd = t->buffer->target_node->accept_fds;
1237 if (!target_allows_fd) {
1238 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
1239 proc->pid, thread->pid,
1240 in_reply_to ? "reply" : "transaction",
1241 fd);
1242 ret = -EPERM;
1243 goto err_fd_not_accepted;
1244 }
1245
1246 file = fget(fd);
1247 if (!file) {
1248 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1249 proc->pid, thread->pid, fd);
1250 ret = -EBADF;
1251 goto err_fget;
1252 }
1253 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
1254 if (ret < 0) {
1255 ret = -EPERM;
1256 goto err_security;
1257 }
1258
1259 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1260 if (target_fd < 0) {
1261 ret = -ENOMEM;
1262 goto err_get_unused_fd;
1263 }
1264 task_fd_install(target_proc, target_fd, file);
1265 trace_binder_transaction_fd(t, fd, target_fd);
1266 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
1267 fd, target_fd);
1268
1269 return target_fd;
1270
1271err_get_unused_fd:
1272err_security:
1273 fput(file);
1274err_fget:
1275err_fd_not_accepted:
1276 return ret;
1277}
1278
Martijn Coenendef95c72017-02-03 14:40:52 -08001279static int binder_translate_fd_array(struct binder_fd_array_object *fda,
1280 struct binder_buffer_object *parent,
1281 struct binder_transaction *t,
1282 struct binder_thread *thread,
1283 struct binder_transaction *in_reply_to)
1284{
1285 binder_size_t fdi, fd_buf_size, num_installed_fds;
1286 int target_fd;
1287 uintptr_t parent_buffer;
1288 u32 *fd_array;
1289 struct binder_proc *proc = thread->proc;
1290 struct binder_proc *target_proc = t->to_proc;
1291
1292 fd_buf_size = sizeof(u32) * fda->num_fds;
1293 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
1294 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
1295 proc->pid, thread->pid, (u64)fda->num_fds);
1296 return -EINVAL;
1297 }
1298 if (fd_buf_size > parent->length ||
1299 fda->parent_offset > parent->length - fd_buf_size) {
1300 /* No space for all file descriptors here. */
1301 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
1302 proc->pid, thread->pid, (u64)fda->num_fds);
1303 return -EINVAL;
1304 }
1305 /*
1306 * Since the parent was already fixed up, convert it
1307 * back to the kernel address space to access it
1308 */
Todd Kjos19c98722017-06-29 12:01:40 -07001309 parent_buffer = parent->buffer -
1310 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08001311 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
1312 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
1313 binder_user_error("%d:%d parent offset not aligned correctly.\n",
1314 proc->pid, thread->pid);
1315 return -EINVAL;
1316 }
1317 for (fdi = 0; fdi < fda->num_fds; fdi++) {
1318 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
1319 in_reply_to);
1320 if (target_fd < 0)
1321 goto err_translate_fd_failed;
1322 fd_array[fdi] = target_fd;
1323 }
1324 return 0;
1325
1326err_translate_fd_failed:
1327 /*
1328 * Failed to allocate fd or security error, free fds
1329 * installed so far.
1330 */
1331 num_installed_fds = fdi;
1332 for (fdi = 0; fdi < num_installed_fds; fdi++)
1333 task_close_fd(target_proc, fd_array[fdi]);
1334 return target_fd;
1335}
1336
Martijn Coenen79802402017-02-03 14:40:51 -08001337static int binder_fixup_parent(struct binder_transaction *t,
1338 struct binder_thread *thread,
1339 struct binder_buffer_object *bp,
1340 binder_size_t *off_start,
1341 binder_size_t num_valid,
1342 struct binder_buffer_object *last_fixup_obj,
1343 binder_size_t last_fixup_min_off)
1344{
1345 struct binder_buffer_object *parent;
1346 u8 *parent_buffer;
1347 struct binder_buffer *b = t->buffer;
1348 struct binder_proc *proc = thread->proc;
1349 struct binder_proc *target_proc = t->to_proc;
1350
1351 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
1352 return 0;
1353
1354 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
1355 if (!parent) {
1356 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
1357 proc->pid, thread->pid);
1358 return -EINVAL;
1359 }
1360
1361 if (!binder_validate_fixup(b, off_start,
1362 parent, bp->parent_offset,
1363 last_fixup_obj,
1364 last_fixup_min_off)) {
1365 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
1366 proc->pid, thread->pid);
1367 return -EINVAL;
1368 }
1369
1370 if (parent->length < sizeof(binder_uintptr_t) ||
1371 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
1372 /* No space for a pointer here! */
1373 binder_user_error("%d:%d got transaction with invalid parent offset\n",
1374 proc->pid, thread->pid);
1375 return -EINVAL;
1376 }
1377 parent_buffer = (u8 *)(parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07001378 binder_alloc_get_user_buffer_offset(
1379 &target_proc->alloc));
Martijn Coenen79802402017-02-03 14:40:51 -08001380 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
1381
1382 return 0;
1383}
1384
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001385static void binder_transaction(struct binder_proc *proc,
1386 struct binder_thread *thread,
Martijn Coenen4bfac802017-02-03 14:40:50 -08001387 struct binder_transaction_data *tr, int reply,
1388 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001389{
Martijn Coenena056af42017-02-03 14:40:49 -08001390 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001391 struct binder_transaction *t;
1392 struct binder_work *tcomplete;
Martijn Coenen79802402017-02-03 14:40:51 -08001393 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001394 binder_size_t off_min;
Martijn Coenen79802402017-02-03 14:40:51 -08001395 u8 *sg_bufp, *sg_buf_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001396 struct binder_proc *target_proc;
1397 struct binder_thread *target_thread = NULL;
1398 struct binder_node *target_node = NULL;
1399 struct list_head *target_list;
1400 wait_queue_head_t *target_wait;
1401 struct binder_transaction *in_reply_to = NULL;
1402 struct binder_transaction_log_entry *e;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001403 uint32_t return_error = 0;
1404 uint32_t return_error_param = 0;
1405 uint32_t return_error_line = 0;
Martijn Coenen79802402017-02-03 14:40:51 -08001406 struct binder_buffer_object *last_fixup_obj = NULL;
1407 binder_size_t last_fixup_min_off = 0;
Martijn Coenen342e5c92017-02-03 14:40:46 -08001408 struct binder_context *context = proc->context;
Todd Kjosd99c7332017-06-29 12:01:53 -07001409 int t_debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001410
1411 e = binder_transaction_log_add(&binder_transaction_log);
Todd Kjosd99c7332017-06-29 12:01:53 -07001412 e->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001413 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1414 e->from_proc = proc->pid;
1415 e->from_thread = thread->pid;
1416 e->target_handle = tr->target.handle;
1417 e->data_size = tr->data_size;
1418 e->offsets_size = tr->offsets_size;
Martijn Coenen14db3182017-02-03 14:40:47 -08001419 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001420
1421 if (reply) {
1422 in_reply_to = thread->transaction_stack;
1423 if (in_reply_to == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301424 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001425 proc->pid, thread->pid);
1426 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001427 return_error_param = -EPROTO;
1428 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001429 goto err_empty_call_stack;
1430 }
1431 binder_set_nice(in_reply_to->saved_priority);
1432 if (in_reply_to->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301433 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 +09001434 proc->pid, thread->pid, in_reply_to->debug_id,
1435 in_reply_to->to_proc ?
1436 in_reply_to->to_proc->pid : 0,
1437 in_reply_to->to_thread ?
1438 in_reply_to->to_thread->pid : 0);
1439 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001440 return_error_param = -EPROTO;
1441 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001442 in_reply_to = NULL;
1443 goto err_bad_call_stack;
1444 }
1445 thread->transaction_stack = in_reply_to->to_parent;
1446 target_thread = in_reply_to->from;
1447 if (target_thread == NULL) {
1448 return_error = BR_DEAD_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001449 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001450 goto err_dead_binder;
1451 }
1452 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301453 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 +09001454 proc->pid, thread->pid,
1455 target_thread->transaction_stack ?
1456 target_thread->transaction_stack->debug_id : 0,
1457 in_reply_to->debug_id);
1458 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001459 return_error_param = -EPROTO;
1460 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001461 in_reply_to = NULL;
1462 target_thread = NULL;
1463 goto err_dead_binder;
1464 }
1465 target_proc = target_thread->proc;
1466 } else {
1467 if (tr->target.handle) {
1468 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001469
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001470 ref = binder_get_ref(proc, tr->target.handle, true);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001471 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301472 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001473 proc->pid, thread->pid);
1474 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001475 return_error_param = -EINVAL;
1476 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001477 goto err_invalid_target_handle;
1478 }
1479 target_node = ref->node;
1480 } else {
Todd Kjosc44b1232017-06-29 12:01:43 -07001481 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08001482 target_node = context->binder_context_mgr_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001483 if (target_node == NULL) {
1484 return_error = BR_DEAD_REPLY;
Todd Kjosc44b1232017-06-29 12:01:43 -07001485 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos57ada2f2017-06-29 12:01:46 -07001486 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001487 goto err_no_context_mgr_node;
1488 }
Todd Kjosc44b1232017-06-29 12:01:43 -07001489 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001490 }
1491 e->to_node = target_node->debug_id;
1492 target_proc = target_node->proc;
1493 if (target_proc == NULL) {
1494 return_error = BR_DEAD_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001495 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001496 goto err_dead_binder;
1497 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001498 if (security_binder_transaction(proc->tsk,
1499 target_proc->tsk) < 0) {
1500 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001501 return_error_param = -EPERM;
1502 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05001503 goto err_invalid_target_handle;
1504 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001505 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1506 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001507
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001508 tmp = thread->transaction_stack;
1509 if (tmp->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301510 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 +09001511 proc->pid, thread->pid, tmp->debug_id,
1512 tmp->to_proc ? tmp->to_proc->pid : 0,
1513 tmp->to_thread ?
1514 tmp->to_thread->pid : 0);
1515 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001516 return_error_param = -EPROTO;
1517 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001518 goto err_bad_call_stack;
1519 }
1520 while (tmp) {
1521 if (tmp->from && tmp->from->proc == target_proc)
1522 target_thread = tmp->from;
1523 tmp = tmp->from_parent;
1524 }
1525 }
1526 }
1527 if (target_thread) {
1528 e->to_thread = target_thread->pid;
1529 target_list = &target_thread->todo;
1530 target_wait = &target_thread->wait;
1531 } else {
1532 target_list = &target_proc->todo;
1533 target_wait = &target_proc->wait;
1534 }
1535 e->to_proc = target_proc->pid;
1536
1537 /* TODO: reuse incoming transaction for reply */
1538 t = kzalloc(sizeof(*t), GFP_KERNEL);
1539 if (t == NULL) {
1540 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001541 return_error_param = -ENOMEM;
1542 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001543 goto err_alloc_t_failed;
1544 }
1545 binder_stats_created(BINDER_STAT_TRANSACTION);
1546
1547 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1548 if (tcomplete == NULL) {
1549 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001550 return_error_param = -ENOMEM;
1551 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001552 goto err_alloc_tcomplete_failed;
1553 }
1554 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1555
Todd Kjosd99c7332017-06-29 12:01:53 -07001556 t->debug_id = t_debug_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001557
1558 if (reply)
1559 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08001560 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001561 proc->pid, thread->pid, t->debug_id,
1562 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001563 (u64)tr->data.ptr.buffer,
1564 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08001565 (u64)tr->data_size, (u64)tr->offsets_size,
1566 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001567 else
1568 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen4bfac802017-02-03 14:40:50 -08001569 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001570 proc->pid, thread->pid, t->debug_id,
1571 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001572 (u64)tr->data.ptr.buffer,
1573 (u64)tr->data.ptr.offsets,
Martijn Coenen4bfac802017-02-03 14:40:50 -08001574 (u64)tr->data_size, (u64)tr->offsets_size,
1575 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001576
1577 if (!reply && !(tr->flags & TF_ONE_WAY))
1578 t->from = thread;
1579 else
1580 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03001581 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001582 t->to_proc = target_proc;
1583 t->to_thread = target_thread;
1584 t->code = tr->code;
1585 t->flags = tr->flags;
1586 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001587
1588 trace_binder_transaction(reply, t, target_node);
1589
Todd Kjos19c98722017-06-29 12:01:40 -07001590 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen4bfac802017-02-03 14:40:50 -08001591 tr->offsets_size, extra_buffers_size,
1592 !reply && (t->flags & TF_ONE_WAY));
Todd Kjos57ada2f2017-06-29 12:01:46 -07001593 if (IS_ERR(t->buffer)) {
1594 /*
1595 * -ESRCH indicates VMA cleared. The target is dying.
1596 */
1597 return_error_param = PTR_ERR(t->buffer);
1598 return_error = return_error_param == -ESRCH ?
1599 BR_DEAD_REPLY : BR_FAILED_REPLY;
1600 return_error_line = __LINE__;
1601 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001602 goto err_binder_alloc_buf_failed;
1603 }
1604 t->buffer->allow_user_free = 0;
1605 t->buffer->debug_id = t->debug_id;
1606 t->buffer->transaction = t;
1607 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001608 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001609 if (target_node)
1610 binder_inc_node(target_node, 1, 0, NULL);
1611
Martijn Coenen79802402017-02-03 14:40:51 -08001612 off_start = (binder_size_t *)(t->buffer->data +
1613 ALIGN(tr->data_size, sizeof(void *)));
1614 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001615
Arve Hjønnevågda498892014-02-21 14:40:26 -08001616 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1617 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301618 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1619 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001620 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001621 return_error_param = -EFAULT;
1622 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001623 goto err_copy_data_failed;
1624 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001625 if (copy_from_user(offp, (const void __user *)(uintptr_t)
1626 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301627 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1628 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001629 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001630 return_error_param = -EFAULT;
1631 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001632 goto err_copy_data_failed;
1633 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001634 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1635 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1636 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001637 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001638 return_error_param = -EINVAL;
1639 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001640 goto err_bad_offset;
1641 }
Martijn Coenen79802402017-02-03 14:40:51 -08001642 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
1643 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
1644 proc->pid, thread->pid,
1645 (u64)extra_buffers_size);
1646 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001647 return_error_param = -EINVAL;
1648 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08001649 goto err_bad_offset;
1650 }
1651 off_end = (void *)off_start + tr->offsets_size;
1652 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
1653 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001654 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001655 for (; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001656 struct binder_object_header *hdr;
1657 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09001658
Martijn Coenenfeba3902017-02-03 14:40:45 -08001659 if (object_size == 0 || *offp < off_min) {
1660 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 -08001661 proc->pid, thread->pid, (u64)*offp,
1662 (u64)off_min,
Martijn Coenenfeba3902017-02-03 14:40:45 -08001663 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001664 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001665 return_error_param = -EINVAL;
1666 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001667 goto err_bad_offset;
1668 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08001669
1670 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
1671 off_min = *offp + object_size;
1672 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001673 case BINDER_TYPE_BINDER:
1674 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001675 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001676
Martijn Coenenfeba3902017-02-03 14:40:45 -08001677 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08001678 ret = binder_translate_binder(fp, t, thread);
1679 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02001680 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001681 return_error_param = ret;
1682 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08001683 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001684 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001685 } break;
1686 case BINDER_TYPE_HANDLE:
1687 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001688 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001689
Martijn Coenenfeba3902017-02-03 14:40:45 -08001690 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08001691 ret = binder_translate_handle(fp, t, thread);
1692 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001693 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001694 return_error_param = ret;
1695 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08001696 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001697 }
1698 } break;
1699
1700 case BINDER_TYPE_FD: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001701 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08001702 int target_fd = binder_translate_fd(fp->fd, t, thread,
1703 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001704
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001705 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001706 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001707 return_error_param = target_fd;
1708 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08001709 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001710 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08001711 fp->pad_binder = 0;
1712 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001713 } break;
Martijn Coenendef95c72017-02-03 14:40:52 -08001714 case BINDER_TYPE_FDA: {
1715 struct binder_fd_array_object *fda =
1716 to_binder_fd_array_object(hdr);
1717 struct binder_buffer_object *parent =
1718 binder_validate_ptr(t->buffer, fda->parent,
1719 off_start,
1720 offp - off_start);
1721 if (!parent) {
1722 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
1723 proc->pid, thread->pid);
1724 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001725 return_error_param = -EINVAL;
1726 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08001727 goto err_bad_parent;
1728 }
1729 if (!binder_validate_fixup(t->buffer, off_start,
1730 parent, fda->parent_offset,
1731 last_fixup_obj,
1732 last_fixup_min_off)) {
1733 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
1734 proc->pid, thread->pid);
1735 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001736 return_error_param = -EINVAL;
1737 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08001738 goto err_bad_parent;
1739 }
1740 ret = binder_translate_fd_array(fda, parent, t, thread,
1741 in_reply_to);
1742 if (ret < 0) {
1743 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001744 return_error_param = ret;
1745 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08001746 goto err_translate_failed;
1747 }
1748 last_fixup_obj = parent;
1749 last_fixup_min_off =
1750 fda->parent_offset + sizeof(u32) * fda->num_fds;
1751 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08001752 case BINDER_TYPE_PTR: {
1753 struct binder_buffer_object *bp =
1754 to_binder_buffer_object(hdr);
1755 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001756
Martijn Coenen79802402017-02-03 14:40:51 -08001757 if (bp->length > buf_left) {
1758 binder_user_error("%d:%d got transaction with too large buffer\n",
1759 proc->pid, thread->pid);
1760 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001761 return_error_param = -EINVAL;
1762 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08001763 goto err_bad_offset;
1764 }
1765 if (copy_from_user(sg_bufp,
1766 (const void __user *)(uintptr_t)
1767 bp->buffer, bp->length)) {
1768 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1769 proc->pid, thread->pid);
Todd Kjos57ada2f2017-06-29 12:01:46 -07001770 return_error_param = -EFAULT;
Martijn Coenen79802402017-02-03 14:40:51 -08001771 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001772 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08001773 goto err_copy_data_failed;
1774 }
1775 /* Fixup buffer pointer to target proc address space */
1776 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjos19c98722017-06-29 12:01:40 -07001777 binder_alloc_get_user_buffer_offset(
1778 &target_proc->alloc);
Martijn Coenen79802402017-02-03 14:40:51 -08001779 sg_bufp += ALIGN(bp->length, sizeof(u64));
1780
1781 ret = binder_fixup_parent(t, thread, bp, off_start,
1782 offp - off_start,
1783 last_fixup_obj,
1784 last_fixup_min_off);
1785 if (ret < 0) {
1786 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001787 return_error_param = ret;
1788 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08001789 goto err_translate_failed;
1790 }
1791 last_fixup_obj = bp;
1792 last_fixup_min_off = 0;
1793 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001794 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001795 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08001796 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001797 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001798 return_error_param = -EINVAL;
1799 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001800 goto err_bad_object_type;
1801 }
1802 }
Todd Kjosccae6f62017-06-29 12:01:48 -07001803 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1804 list_add_tail(&tcomplete->entry, &thread->todo);
1805
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001806 if (reply) {
1807 BUG_ON(t->buffer->async_transaction != 0);
1808 binder_pop_transaction(target_thread, in_reply_to);
1809 } else if (!(t->flags & TF_ONE_WAY)) {
1810 BUG_ON(t->buffer->async_transaction != 0);
1811 t->need_reply = 1;
1812 t->from_parent = thread->transaction_stack;
1813 thread->transaction_stack = t;
1814 } else {
1815 BUG_ON(target_node == NULL);
1816 BUG_ON(t->buffer->async_transaction != 1);
1817 if (target_node->has_async_transaction) {
1818 target_list = &target_node->async_todo;
1819 target_wait = NULL;
1820 } else
1821 target_node->has_async_transaction = 1;
1822 }
1823 t->work.type = BINDER_WORK_TRANSACTION;
1824 list_add_tail(&t->work.entry, target_list);
Riley Andrews00b40d62017-06-29 12:01:37 -07001825 if (target_wait) {
Todd Kjosccae6f62017-06-29 12:01:48 -07001826 if (reply || !(tr->flags & TF_ONE_WAY))
Riley Andrews00b40d62017-06-29 12:01:37 -07001827 wake_up_interruptible_sync(target_wait);
1828 else
1829 wake_up_interruptible(target_wait);
1830 }
Todd Kjosd99c7332017-06-29 12:01:53 -07001831 /*
1832 * write barrier to synchronize with initialization
1833 * of log entry
1834 */
1835 smp_wmb();
1836 WRITE_ONCE(e->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001837 return;
1838
Martijn Coenena056af42017-02-03 14:40:49 -08001839err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001840err_bad_object_type:
1841err_bad_offset:
Martijn Coenendef95c72017-02-03 14:40:52 -08001842err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001843err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001844 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001845 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1846 t->buffer->transaction = NULL;
Todd Kjos19c98722017-06-29 12:01:40 -07001847 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001848err_binder_alloc_buf_failed:
1849 kfree(tcomplete);
1850 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1851err_alloc_tcomplete_failed:
1852 kfree(t);
1853 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1854err_alloc_t_failed:
1855err_bad_call_stack:
1856err_empty_call_stack:
1857err_dead_binder:
1858err_invalid_target_handle:
1859err_no_context_mgr_node:
1860 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjos57ada2f2017-06-29 12:01:46 -07001861 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
1862 proc->pid, thread->pid, return_error, return_error_param,
1863 (u64)tr->data_size, (u64)tr->offsets_size,
1864 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001865
1866 {
1867 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09001868
Todd Kjos57ada2f2017-06-29 12:01:46 -07001869 e->return_error = return_error;
1870 e->return_error_param = return_error_param;
1871 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001872 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1873 *fe = *e;
Todd Kjosd99c7332017-06-29 12:01:53 -07001874 /*
1875 * write barrier to synchronize with initialization
1876 * of log entry
1877 */
1878 smp_wmb();
1879 WRITE_ONCE(e->debug_id_done, t_debug_id);
1880 WRITE_ONCE(fe->debug_id_done, t_debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001881 }
1882
1883 BUG_ON(thread->return_error != BR_OK);
1884 if (in_reply_to) {
1885 thread->return_error = BR_TRANSACTION_COMPLETE;
1886 binder_send_failed_reply(in_reply_to, return_error);
1887 } else
1888 thread->return_error = return_error;
1889}
1890
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02001891static int binder_thread_write(struct binder_proc *proc,
1892 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001893 binder_uintptr_t binder_buffer, size_t size,
1894 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001895{
1896 uint32_t cmd;
Martijn Coenen342e5c92017-02-03 14:40:46 -08001897 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001898 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001899 void __user *ptr = buffer + *consumed;
1900 void __user *end = buffer + size;
1901
1902 while (ptr < end && thread->return_error == BR_OK) {
1903 if (get_user(cmd, (uint32_t __user *)ptr))
1904 return -EFAULT;
1905 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001906 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001907 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07001908 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
1909 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
1910 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001911 }
1912 switch (cmd) {
1913 case BC_INCREFS:
1914 case BC_ACQUIRE:
1915 case BC_RELEASE:
1916 case BC_DECREFS: {
1917 uint32_t target;
Todd Kjosc44b1232017-06-29 12:01:43 -07001918 struct binder_ref *ref = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001919 const char *debug_string;
1920
1921 if (get_user(target, (uint32_t __user *)ptr))
1922 return -EFAULT;
Todd Kjosc44b1232017-06-29 12:01:43 -07001923
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001924 ptr += sizeof(uint32_t);
Todd Kjosc44b1232017-06-29 12:01:43 -07001925 if (target == 0 &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001926 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
Todd Kjosc44b1232017-06-29 12:01:43 -07001927 struct binder_node *ctx_mgr_node;
1928
1929 mutex_lock(&context->context_mgr_node_lock);
1930 ctx_mgr_node = context->binder_context_mgr_node;
1931 if (ctx_mgr_node) {
1932 ref = binder_get_ref_for_node(proc,
1933 ctx_mgr_node);
1934 if (ref && ref->desc != target) {
1935 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
1936 proc->pid, thread->pid,
1937 ref->desc);
1938 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001939 }
Todd Kjosc44b1232017-06-29 12:01:43 -07001940 mutex_unlock(&context->context_mgr_node_lock);
1941 }
1942 if (ref == NULL)
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001943 ref = binder_get_ref(proc, target,
1944 cmd == BC_ACQUIRE ||
1945 cmd == BC_RELEASE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001946 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301947 binder_user_error("%d:%d refcount change on invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001948 proc->pid, thread->pid, target);
1949 break;
1950 }
1951 switch (cmd) {
1952 case BC_INCREFS:
1953 debug_string = "IncRefs";
1954 binder_inc_ref(ref, 0, NULL);
1955 break;
1956 case BC_ACQUIRE:
1957 debug_string = "Acquire";
1958 binder_inc_ref(ref, 1, NULL);
1959 break;
1960 case BC_RELEASE:
1961 debug_string = "Release";
1962 binder_dec_ref(ref, 1);
1963 break;
1964 case BC_DECREFS:
1965 default:
1966 debug_string = "DecRefs";
1967 binder_dec_ref(ref, 0);
1968 break;
1969 }
1970 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301971 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001972 proc->pid, thread->pid, debug_string, ref->debug_id,
1973 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1974 break;
1975 }
1976 case BC_INCREFS_DONE:
1977 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001978 binder_uintptr_t node_ptr;
1979 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001980 struct binder_node *node;
1981
Arve Hjønnevågda498892014-02-21 14:40:26 -08001982 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001983 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001984 ptr += sizeof(binder_uintptr_t);
1985 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001986 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001987 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001988 node = binder_get_node(proc, node_ptr);
1989 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001990 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001991 proc->pid, thread->pid,
1992 cmd == BC_INCREFS_DONE ?
1993 "BC_INCREFS_DONE" :
1994 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001995 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001996 break;
1997 }
1998 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001999 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002000 proc->pid, thread->pid,
2001 cmd == BC_INCREFS_DONE ?
2002 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002003 (u64)node_ptr, node->debug_id,
2004 (u64)cookie, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002005 break;
2006 }
2007 if (cmd == BC_ACQUIRE_DONE) {
2008 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302009 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002010 proc->pid, thread->pid,
2011 node->debug_id);
2012 break;
2013 }
2014 node->pending_strong_ref = 0;
2015 } else {
2016 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302017 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002018 proc->pid, thread->pid,
2019 node->debug_id);
2020 break;
2021 }
2022 node->pending_weak_ref = 0;
2023 }
2024 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
2025 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302026 "%d:%d %s node %d ls %d lw %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002027 proc->pid, thread->pid,
2028 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
2029 node->debug_id, node->local_strong_refs, node->local_weak_refs);
2030 break;
2031 }
2032 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302033 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002034 return -EINVAL;
2035 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302036 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002037 return -EINVAL;
2038
2039 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002040 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002041 struct binder_buffer *buffer;
2042
Arve Hjønnevågda498892014-02-21 14:40:26 -08002043 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002044 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002045 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002046
Todd Kjos53d311cf2017-06-29 12:01:51 -07002047 buffer = binder_alloc_prepare_to_free(&proc->alloc,
2048 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002049 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002050 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
2051 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002052 break;
2053 }
2054 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002055 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
2056 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002057 break;
2058 }
2059 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002060 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
2061 proc->pid, thread->pid, (u64)data_ptr,
2062 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002063 buffer->transaction ? "active" : "finished");
2064
2065 if (buffer->transaction) {
2066 buffer->transaction->buffer = NULL;
2067 buffer->transaction = NULL;
2068 }
2069 if (buffer->async_transaction && buffer->target_node) {
2070 BUG_ON(!buffer->target_node->has_async_transaction);
2071 if (list_empty(&buffer->target_node->async_todo))
2072 buffer->target_node->has_async_transaction = 0;
2073 else
2074 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
2075 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002076 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002077 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjos19c98722017-06-29 12:01:40 -07002078 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002079 break;
2080 }
2081
Martijn Coenen79802402017-02-03 14:40:51 -08002082 case BC_TRANSACTION_SG:
2083 case BC_REPLY_SG: {
2084 struct binder_transaction_data_sg tr;
2085
2086 if (copy_from_user(&tr, ptr, sizeof(tr)))
2087 return -EFAULT;
2088 ptr += sizeof(tr);
2089 binder_transaction(proc, thread, &tr.transaction_data,
2090 cmd == BC_REPLY_SG, tr.buffers_size);
2091 break;
2092 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002093 case BC_TRANSACTION:
2094 case BC_REPLY: {
2095 struct binder_transaction_data tr;
2096
2097 if (copy_from_user(&tr, ptr, sizeof(tr)))
2098 return -EFAULT;
2099 ptr += sizeof(tr);
Martijn Coenen4bfac802017-02-03 14:40:50 -08002100 binder_transaction(proc, thread, &tr,
2101 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002102 break;
2103 }
2104
2105 case BC_REGISTER_LOOPER:
2106 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302107 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002108 proc->pid, thread->pid);
2109 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
2110 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302111 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002112 proc->pid, thread->pid);
2113 } else if (proc->requested_threads == 0) {
2114 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302115 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002116 proc->pid, thread->pid);
2117 } else {
2118 proc->requested_threads--;
2119 proc->requested_threads_started++;
2120 }
2121 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
2122 break;
2123 case BC_ENTER_LOOPER:
2124 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302125 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002126 proc->pid, thread->pid);
2127 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
2128 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302129 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002130 proc->pid, thread->pid);
2131 }
2132 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
2133 break;
2134 case BC_EXIT_LOOPER:
2135 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302136 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002137 proc->pid, thread->pid);
2138 thread->looper |= BINDER_LOOPER_STATE_EXITED;
2139 break;
2140
2141 case BC_REQUEST_DEATH_NOTIFICATION:
2142 case BC_CLEAR_DEATH_NOTIFICATION: {
2143 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002144 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002145 struct binder_ref *ref;
2146 struct binder_ref_death *death;
2147
2148 if (get_user(target, (uint32_t __user *)ptr))
2149 return -EFAULT;
2150 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002151 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002152 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002153 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002154 ref = binder_get_ref(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002155 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302156 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002157 proc->pid, thread->pid,
2158 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2159 "BC_REQUEST_DEATH_NOTIFICATION" :
2160 "BC_CLEAR_DEATH_NOTIFICATION",
2161 target);
2162 break;
2163 }
2164
2165 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002166 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002167 proc->pid, thread->pid,
2168 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2169 "BC_REQUEST_DEATH_NOTIFICATION" :
2170 "BC_CLEAR_DEATH_NOTIFICATION",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002171 (u64)cookie, ref->debug_id, ref->desc,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002172 ref->strong, ref->weak, ref->node->debug_id);
2173
2174 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2175 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302176 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002177 proc->pid, thread->pid);
2178 break;
2179 }
2180 death = kzalloc(sizeof(*death), GFP_KERNEL);
2181 if (death == NULL) {
2182 thread->return_error = BR_ERROR;
2183 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302184 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002185 proc->pid, thread->pid);
2186 break;
2187 }
2188 binder_stats_created(BINDER_STAT_DEATH);
2189 INIT_LIST_HEAD(&death->work.entry);
2190 death->cookie = cookie;
2191 ref->death = death;
2192 if (ref->node->proc == NULL) {
2193 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2194 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2195 list_add_tail(&ref->death->work.entry, &thread->todo);
2196 } else {
2197 list_add_tail(&ref->death->work.entry, &proc->todo);
2198 wake_up_interruptible(&proc->wait);
2199 }
2200 }
2201 } else {
2202 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302203 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002204 proc->pid, thread->pid);
2205 break;
2206 }
2207 death = ref->death;
2208 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002209 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002210 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002211 (u64)death->cookie,
2212 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002213 break;
2214 }
2215 ref->death = NULL;
2216 if (list_empty(&death->work.entry)) {
2217 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2218 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2219 list_add_tail(&death->work.entry, &thread->todo);
2220 } else {
2221 list_add_tail(&death->work.entry, &proc->todo);
2222 wake_up_interruptible(&proc->wait);
2223 }
2224 } else {
2225 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2226 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2227 }
2228 }
2229 } break;
2230 case BC_DEAD_BINDER_DONE: {
2231 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002232 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002233 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09002234
Arve Hjønnevågda498892014-02-21 14:40:26 -08002235 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002236 return -EFAULT;
2237
Lisa Du7a64cd82016-02-17 09:32:52 +08002238 ptr += sizeof(cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002239 list_for_each_entry(w, &proc->delivered_death, entry) {
2240 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
Seunghun Lee10f62862014-05-01 01:30:23 +09002241
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002242 if (tmp_death->cookie == cookie) {
2243 death = tmp_death;
2244 break;
2245 }
2246 }
2247 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002248 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2249 proc->pid, thread->pid, (u64)cookie,
2250 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002251 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002252 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2253 proc->pid, thread->pid, (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002254 break;
2255 }
2256
2257 list_del_init(&death->work.entry);
2258 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2259 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2260 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2261 list_add_tail(&death->work.entry, &thread->todo);
2262 } else {
2263 list_add_tail(&death->work.entry, &proc->todo);
2264 wake_up_interruptible(&proc->wait);
2265 }
2266 }
2267 } break;
2268
2269 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302270 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002271 proc->pid, thread->pid, cmd);
2272 return -EINVAL;
2273 }
2274 *consumed = ptr - buffer;
2275 }
2276 return 0;
2277}
2278
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002279static void binder_stat_br(struct binder_proc *proc,
2280 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002281{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002282 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002283 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07002284 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
2285 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
2286 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002287 }
2288}
2289
2290static int binder_has_proc_work(struct binder_proc *proc,
2291 struct binder_thread *thread)
2292{
Todd Kjos08dabce2017-06-29 12:01:49 -07002293 return !list_empty(&proc->todo) || thread->looper_need_return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002294}
2295
2296static int binder_has_thread_work(struct binder_thread *thread)
2297{
2298 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
Todd Kjos08dabce2017-06-29 12:01:49 -07002299 thread->looper_need_return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002300}
2301
Todd Kjos26b47d82017-06-29 12:01:47 -07002302static int binder_put_node_cmd(struct binder_proc *proc,
2303 struct binder_thread *thread,
2304 void __user **ptrp,
2305 binder_uintptr_t node_ptr,
2306 binder_uintptr_t node_cookie,
2307 int node_debug_id,
2308 uint32_t cmd, const char *cmd_name)
2309{
2310 void __user *ptr = *ptrp;
2311
2312 if (put_user(cmd, (uint32_t __user *)ptr))
2313 return -EFAULT;
2314 ptr += sizeof(uint32_t);
2315
2316 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
2317 return -EFAULT;
2318 ptr += sizeof(binder_uintptr_t);
2319
2320 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
2321 return -EFAULT;
2322 ptr += sizeof(binder_uintptr_t);
2323
2324 binder_stat_br(proc, thread, cmd);
2325 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
2326 proc->pid, thread->pid, cmd_name, node_debug_id,
2327 (u64)node_ptr, (u64)node_cookie);
2328
2329 *ptrp = ptr;
2330 return 0;
2331}
2332
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002333static int binder_thread_read(struct binder_proc *proc,
2334 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002335 binder_uintptr_t binder_buffer, size_t size,
2336 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002337{
Arve Hjønnevågda498892014-02-21 14:40:26 -08002338 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002339 void __user *ptr = buffer + *consumed;
2340 void __user *end = buffer + size;
2341
2342 int ret = 0;
2343 int wait_for_proc_work;
2344
2345 if (*consumed == 0) {
2346 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2347 return -EFAULT;
2348 ptr += sizeof(uint32_t);
2349 }
2350
2351retry:
2352 wait_for_proc_work = thread->transaction_stack == NULL &&
2353 list_empty(&thread->todo);
2354
2355 if (thread->return_error != BR_OK && ptr < end) {
2356 if (thread->return_error2 != BR_OK) {
2357 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2358 return -EFAULT;
2359 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002360 binder_stat_br(proc, thread, thread->return_error2);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002361 if (ptr == end)
2362 goto done;
2363 thread->return_error2 = BR_OK;
2364 }
2365 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2366 return -EFAULT;
2367 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002368 binder_stat_br(proc, thread, thread->return_error);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002369 thread->return_error = BR_OK;
2370 goto done;
2371 }
2372
2373
2374 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2375 if (wait_for_proc_work)
2376 proc->ready_threads++;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002377
2378 binder_unlock(__func__);
2379
2380 trace_binder_wait_for_work(wait_for_proc_work,
2381 !!thread->transaction_stack,
2382 !list_empty(&thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002383 if (wait_for_proc_work) {
2384 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2385 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302386 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 +09002387 proc->pid, thread->pid, thread->looper);
2388 wait_event_interruptible(binder_user_error_wait,
2389 binder_stop_on_user_error < 2);
2390 }
2391 binder_set_nice(proc->default_priority);
2392 if (non_block) {
2393 if (!binder_has_proc_work(proc, thread))
2394 ret = -EAGAIN;
2395 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002396 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002397 } else {
2398 if (non_block) {
2399 if (!binder_has_thread_work(thread))
2400 ret = -EAGAIN;
2401 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002402 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002403 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002404
2405 binder_lock(__func__);
2406
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002407 if (wait_for_proc_work)
2408 proc->ready_threads--;
2409 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2410
2411 if (ret)
2412 return ret;
2413
2414 while (1) {
2415 uint32_t cmd;
2416 struct binder_transaction_data tr;
2417 struct binder_work *w;
2418 struct binder_transaction *t = NULL;
2419
Dmitry Voytik395262a2014-09-08 18:16:34 +04002420 if (!list_empty(&thread->todo)) {
2421 w = list_first_entry(&thread->todo, struct binder_work,
2422 entry);
2423 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2424 w = list_first_entry(&proc->todo, struct binder_work,
2425 entry);
2426 } else {
2427 /* no data added */
Todd Kjos08dabce2017-06-29 12:01:49 -07002428 if (ptr - buffer == 4 && !thread->looper_need_return)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002429 goto retry;
2430 break;
2431 }
2432
2433 if (end - ptr < sizeof(tr) + 4)
2434 break;
2435
2436 switch (w->type) {
2437 case BINDER_WORK_TRANSACTION: {
2438 t = container_of(w, struct binder_transaction, work);
2439 } break;
2440 case BINDER_WORK_TRANSACTION_COMPLETE: {
2441 cmd = BR_TRANSACTION_COMPLETE;
2442 if (put_user(cmd, (uint32_t __user *)ptr))
2443 return -EFAULT;
2444 ptr += sizeof(uint32_t);
2445
2446 binder_stat_br(proc, thread, cmd);
2447 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302448 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002449 proc->pid, thread->pid);
2450
2451 list_del(&w->entry);
2452 kfree(w);
2453 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2454 } break;
2455 case BINDER_WORK_NODE: {
2456 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos26b47d82017-06-29 12:01:47 -07002457 int strong, weak;
2458 binder_uintptr_t node_ptr = node->ptr;
2459 binder_uintptr_t node_cookie = node->cookie;
2460 int node_debug_id = node->debug_id;
2461 int has_weak_ref;
2462 int has_strong_ref;
2463 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09002464
Todd Kjos26b47d82017-06-29 12:01:47 -07002465 BUG_ON(proc != node->proc);
2466 strong = node->internal_strong_refs ||
2467 node->local_strong_refs;
2468 weak = !hlist_empty(&node->refs) ||
2469 node->local_weak_refs || strong;
2470 has_strong_ref = node->has_strong_ref;
2471 has_weak_ref = node->has_weak_ref;
2472
2473 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002474 node->has_weak_ref = 1;
2475 node->pending_weak_ref = 1;
2476 node->local_weak_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07002477 }
2478 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002479 node->has_strong_ref = 1;
2480 node->pending_strong_ref = 1;
2481 node->local_strong_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07002482 }
2483 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002484 node->has_strong_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07002485 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002486 node->has_weak_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07002487 list_del(&w->entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002488
Todd Kjos26b47d82017-06-29 12:01:47 -07002489 if (!weak && !strong) {
2490 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2491 "%d:%d node %d u%016llx c%016llx deleted\n",
2492 proc->pid, thread->pid,
2493 node_debug_id,
2494 (u64)node_ptr,
2495 (u64)node_cookie);
2496 rb_erase(&node->rb_node, &proc->nodes);
2497 kfree(node);
2498 binder_stats_deleted(BINDER_STAT_NODE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002499 }
Todd Kjos26b47d82017-06-29 12:01:47 -07002500 if (weak && !has_weak_ref)
2501 ret = binder_put_node_cmd(
2502 proc, thread, &ptr, node_ptr,
2503 node_cookie, node_debug_id,
2504 BR_INCREFS, "BR_INCREFS");
2505 if (!ret && strong && !has_strong_ref)
2506 ret = binder_put_node_cmd(
2507 proc, thread, &ptr, node_ptr,
2508 node_cookie, node_debug_id,
2509 BR_ACQUIRE, "BR_ACQUIRE");
2510 if (!ret && !strong && has_strong_ref)
2511 ret = binder_put_node_cmd(
2512 proc, thread, &ptr, node_ptr,
2513 node_cookie, node_debug_id,
2514 BR_RELEASE, "BR_RELEASE");
2515 if (!ret && !weak && has_weak_ref)
2516 ret = binder_put_node_cmd(
2517 proc, thread, &ptr, node_ptr,
2518 node_cookie, node_debug_id,
2519 BR_DECREFS, "BR_DECREFS");
2520 if (orig_ptr == ptr)
2521 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2522 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2523 proc->pid, thread->pid,
2524 node_debug_id,
2525 (u64)node_ptr,
2526 (u64)node_cookie);
2527 if (ret)
2528 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002529 } break;
2530 case BINDER_WORK_DEAD_BINDER:
2531 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2532 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2533 struct binder_ref_death *death;
2534 uint32_t cmd;
2535
2536 death = container_of(w, struct binder_ref_death, work);
2537 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2538 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2539 else
2540 cmd = BR_DEAD_BINDER;
2541 if (put_user(cmd, (uint32_t __user *)ptr))
2542 return -EFAULT;
2543 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002544 if (put_user(death->cookie,
2545 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002546 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002547 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002548 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002549 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002550 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002551 proc->pid, thread->pid,
2552 cmd == BR_DEAD_BINDER ?
2553 "BR_DEAD_BINDER" :
2554 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002555 (u64)death->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002556
2557 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2558 list_del(&w->entry);
2559 kfree(death);
2560 binder_stats_deleted(BINDER_STAT_DEATH);
2561 } else
2562 list_move(&w->entry, &proc->delivered_death);
2563 if (cmd == BR_DEAD_BINDER)
2564 goto done; /* DEAD_BINDER notifications can cause transactions */
2565 } break;
2566 }
2567
2568 if (!t)
2569 continue;
2570
2571 BUG_ON(t->buffer == NULL);
2572 if (t->buffer->target_node) {
2573 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002574
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002575 tr.target.ptr = target_node->ptr;
2576 tr.cookie = target_node->cookie;
2577 t->saved_priority = task_nice(current);
2578 if (t->priority < target_node->min_priority &&
2579 !(t->flags & TF_ONE_WAY))
2580 binder_set_nice(t->priority);
2581 else if (!(t->flags & TF_ONE_WAY) ||
2582 t->saved_priority > target_node->min_priority)
2583 binder_set_nice(target_node->min_priority);
2584 cmd = BR_TRANSACTION;
2585 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002586 tr.target.ptr = 0;
2587 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002588 cmd = BR_REPLY;
2589 }
2590 tr.code = t->code;
2591 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002592 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002593
2594 if (t->from) {
2595 struct task_struct *sender = t->from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09002596
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002597 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002598 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002599 } else {
2600 tr.sender_pid = 0;
2601 }
2602
2603 tr.data_size = t->buffer->data_size;
2604 tr.offsets_size = t->buffer->offsets_size;
Todd Kjos19c98722017-06-29 12:01:40 -07002605 tr.data.ptr.buffer = (binder_uintptr_t)
2606 ((uintptr_t)t->buffer->data +
2607 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002608 tr.data.ptr.offsets = tr.data.ptr.buffer +
2609 ALIGN(t->buffer->data_size,
2610 sizeof(void *));
2611
2612 if (put_user(cmd, (uint32_t __user *)ptr))
2613 return -EFAULT;
2614 ptr += sizeof(uint32_t);
2615 if (copy_to_user(ptr, &tr, sizeof(tr)))
2616 return -EFAULT;
2617 ptr += sizeof(tr);
2618
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002619 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002620 binder_stat_br(proc, thread, cmd);
2621 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002622 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002623 proc->pid, thread->pid,
2624 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2625 "BR_REPLY",
2626 t->debug_id, t->from ? t->from->proc->pid : 0,
2627 t->from ? t->from->pid : 0, cmd,
2628 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002629 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002630
2631 list_del(&t->work.entry);
2632 t->buffer->allow_user_free = 1;
2633 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2634 t->to_parent = thread->transaction_stack;
2635 t->to_thread = thread;
2636 thread->transaction_stack = t;
2637 } else {
2638 t->buffer->transaction = NULL;
2639 kfree(t);
2640 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2641 }
2642 break;
2643 }
2644
2645done:
2646
2647 *consumed = ptr - buffer;
2648 if (proc->requested_threads + proc->ready_threads == 0 &&
2649 proc->requested_threads_started < proc->max_threads &&
2650 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2651 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2652 /*spawn a new thread if we leave this out */) {
2653 proc->requested_threads++;
2654 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302655 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002656 proc->pid, thread->pid);
2657 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2658 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002659 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002660 }
2661 return 0;
2662}
2663
2664static void binder_release_work(struct list_head *list)
2665{
2666 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09002667
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002668 while (!list_empty(list)) {
2669 w = list_first_entry(list, struct binder_work, entry);
2670 list_del_init(&w->entry);
2671 switch (w->type) {
2672 case BINDER_WORK_TRANSACTION: {
2673 struct binder_transaction *t;
2674
2675 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002676 if (t->buffer->target_node &&
2677 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002678 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002679 } else {
2680 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302681 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002682 t->debug_id);
2683 t->buffer->transaction = NULL;
2684 kfree(t);
2685 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2686 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002687 } break;
2688 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002689 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302690 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002691 kfree(w);
2692 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2693 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002694 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2695 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2696 struct binder_ref_death *death;
2697
2698 death = container_of(w, struct binder_ref_death, work);
2699 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002700 "undelivered death notification, %016llx\n",
2701 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002702 kfree(death);
2703 binder_stats_deleted(BINDER_STAT_DEATH);
2704 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002705 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302706 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002707 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002708 break;
2709 }
2710 }
2711
2712}
2713
2714static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2715{
2716 struct binder_thread *thread = NULL;
2717 struct rb_node *parent = NULL;
2718 struct rb_node **p = &proc->threads.rb_node;
2719
2720 while (*p) {
2721 parent = *p;
2722 thread = rb_entry(parent, struct binder_thread, rb_node);
2723
2724 if (current->pid < thread->pid)
2725 p = &(*p)->rb_left;
2726 else if (current->pid > thread->pid)
2727 p = &(*p)->rb_right;
2728 else
2729 break;
2730 }
2731 if (*p == NULL) {
2732 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2733 if (thread == NULL)
2734 return NULL;
2735 binder_stats_created(BINDER_STAT_THREAD);
2736 thread->proc = proc;
2737 thread->pid = current->pid;
2738 init_waitqueue_head(&thread->wait);
2739 INIT_LIST_HEAD(&thread->todo);
2740 rb_link_node(&thread->rb_node, parent, p);
2741 rb_insert_color(&thread->rb_node, &proc->threads);
Todd Kjos08dabce2017-06-29 12:01:49 -07002742 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002743 thread->return_error = BR_OK;
2744 thread->return_error2 = BR_OK;
2745 }
2746 return thread;
2747}
2748
2749static int binder_free_thread(struct binder_proc *proc,
2750 struct binder_thread *thread)
2751{
2752 struct binder_transaction *t;
2753 struct binder_transaction *send_reply = NULL;
2754 int active_transactions = 0;
2755
2756 rb_erase(&thread->rb_node, &proc->threads);
2757 t = thread->transaction_stack;
2758 if (t && t->to_thread == thread)
2759 send_reply = t;
2760 while (t) {
2761 active_transactions++;
2762 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302763 "release %d:%d transaction %d %s, still active\n",
2764 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002765 t->debug_id,
2766 (t->to_thread == thread) ? "in" : "out");
2767
2768 if (t->to_thread == thread) {
2769 t->to_proc = NULL;
2770 t->to_thread = NULL;
2771 if (t->buffer) {
2772 t->buffer->transaction = NULL;
2773 t->buffer = NULL;
2774 }
2775 t = t->to_parent;
2776 } else if (t->from == thread) {
2777 t->from = NULL;
2778 t = t->from_parent;
2779 } else
2780 BUG();
2781 }
2782 if (send_reply)
2783 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2784 binder_release_work(&thread->todo);
2785 kfree(thread);
2786 binder_stats_deleted(BINDER_STAT_THREAD);
2787 return active_transactions;
2788}
2789
2790static unsigned int binder_poll(struct file *filp,
2791 struct poll_table_struct *wait)
2792{
2793 struct binder_proc *proc = filp->private_data;
2794 struct binder_thread *thread = NULL;
2795 int wait_for_proc_work;
2796
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002797 binder_lock(__func__);
2798
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002799 thread = binder_get_thread(proc);
2800
2801 wait_for_proc_work = thread->transaction_stack == NULL &&
2802 list_empty(&thread->todo) && thread->return_error == BR_OK;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002803
2804 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002805
2806 if (wait_for_proc_work) {
2807 if (binder_has_proc_work(proc, thread))
2808 return POLLIN;
2809 poll_wait(filp, &proc->wait, wait);
2810 if (binder_has_proc_work(proc, thread))
2811 return POLLIN;
2812 } else {
2813 if (binder_has_thread_work(thread))
2814 return POLLIN;
2815 poll_wait(filp, &thread->wait, wait);
2816 if (binder_has_thread_work(thread))
2817 return POLLIN;
2818 }
2819 return 0;
2820}
2821
Tair Rzayev78260ac2014-06-03 22:27:21 +03002822static int binder_ioctl_write_read(struct file *filp,
2823 unsigned int cmd, unsigned long arg,
2824 struct binder_thread *thread)
2825{
2826 int ret = 0;
2827 struct binder_proc *proc = filp->private_data;
2828 unsigned int size = _IOC_SIZE(cmd);
2829 void __user *ubuf = (void __user *)arg;
2830 struct binder_write_read bwr;
2831
2832 if (size != sizeof(struct binder_write_read)) {
2833 ret = -EINVAL;
2834 goto out;
2835 }
2836 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2837 ret = -EFAULT;
2838 goto out;
2839 }
2840 binder_debug(BINDER_DEBUG_READ_WRITE,
2841 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2842 proc->pid, thread->pid,
2843 (u64)bwr.write_size, (u64)bwr.write_buffer,
2844 (u64)bwr.read_size, (u64)bwr.read_buffer);
2845
2846 if (bwr.write_size > 0) {
2847 ret = binder_thread_write(proc, thread,
2848 bwr.write_buffer,
2849 bwr.write_size,
2850 &bwr.write_consumed);
2851 trace_binder_write_done(ret);
2852 if (ret < 0) {
2853 bwr.read_consumed = 0;
2854 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2855 ret = -EFAULT;
2856 goto out;
2857 }
2858 }
2859 if (bwr.read_size > 0) {
2860 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2861 bwr.read_size,
2862 &bwr.read_consumed,
2863 filp->f_flags & O_NONBLOCK);
2864 trace_binder_read_done(ret);
2865 if (!list_empty(&proc->todo))
2866 wake_up_interruptible(&proc->wait);
2867 if (ret < 0) {
2868 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2869 ret = -EFAULT;
2870 goto out;
2871 }
2872 }
2873 binder_debug(BINDER_DEBUG_READ_WRITE,
2874 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2875 proc->pid, thread->pid,
2876 (u64)bwr.write_consumed, (u64)bwr.write_size,
2877 (u64)bwr.read_consumed, (u64)bwr.read_size);
2878 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2879 ret = -EFAULT;
2880 goto out;
2881 }
2882out:
2883 return ret;
2884}
2885
2886static int binder_ioctl_set_ctx_mgr(struct file *filp)
2887{
2888 int ret = 0;
2889 struct binder_proc *proc = filp->private_data;
Martijn Coenen342e5c92017-02-03 14:40:46 -08002890 struct binder_context *context = proc->context;
Todd Kjosc44b1232017-06-29 12:01:43 -07002891 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002892 kuid_t curr_euid = current_euid();
2893
Todd Kjosc44b1232017-06-29 12:01:43 -07002894 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08002895 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002896 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2897 ret = -EBUSY;
2898 goto out;
2899 }
Stephen Smalley79af7302015-01-21 10:54:10 -05002900 ret = security_binder_set_context_mgr(proc->tsk);
2901 if (ret < 0)
2902 goto out;
Martijn Coenen342e5c92017-02-03 14:40:46 -08002903 if (uid_valid(context->binder_context_mgr_uid)) {
2904 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002905 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2906 from_kuid(&init_user_ns, curr_euid),
2907 from_kuid(&init_user_ns,
Martijn Coenen342e5c92017-02-03 14:40:46 -08002908 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03002909 ret = -EPERM;
2910 goto out;
2911 }
2912 } else {
Martijn Coenen342e5c92017-02-03 14:40:46 -08002913 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002914 }
Todd Kjosc44b1232017-06-29 12:01:43 -07002915 new_node = binder_new_node(proc, 0, 0);
2916 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002917 ret = -ENOMEM;
2918 goto out;
2919 }
Todd Kjosc44b1232017-06-29 12:01:43 -07002920 new_node->local_weak_refs++;
2921 new_node->local_strong_refs++;
2922 new_node->has_strong_ref = 1;
2923 new_node->has_weak_ref = 1;
2924 context->binder_context_mgr_node = new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002925out:
Todd Kjosc44b1232017-06-29 12:01:43 -07002926 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03002927 return ret;
2928}
2929
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002930static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2931{
2932 int ret;
2933 struct binder_proc *proc = filp->private_data;
2934 struct binder_thread *thread;
2935 unsigned int size = _IOC_SIZE(cmd);
2936 void __user *ubuf = (void __user *)arg;
2937
Tair Rzayev78260ac2014-06-03 22:27:21 +03002938 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2939 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002940
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002941 trace_binder_ioctl(cmd, arg);
2942
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002943 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2944 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002945 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002946
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002947 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002948 thread = binder_get_thread(proc);
2949 if (thread == NULL) {
2950 ret = -ENOMEM;
2951 goto err;
2952 }
2953
2954 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002955 case BINDER_WRITE_READ:
2956 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
2957 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002958 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002959 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002960 case BINDER_SET_MAX_THREADS:
2961 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2962 ret = -EINVAL;
2963 goto err;
2964 }
2965 break;
2966 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03002967 ret = binder_ioctl_set_ctx_mgr(filp);
2968 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002969 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002970 break;
2971 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302972 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002973 proc->pid, thread->pid);
2974 binder_free_thread(proc, thread);
2975 thread = NULL;
2976 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002977 case BINDER_VERSION: {
2978 struct binder_version __user *ver = ubuf;
2979
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002980 if (size != sizeof(struct binder_version)) {
2981 ret = -EINVAL;
2982 goto err;
2983 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02002984 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2985 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002986 ret = -EINVAL;
2987 goto err;
2988 }
2989 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002990 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002991 default:
2992 ret = -EINVAL;
2993 goto err;
2994 }
2995 ret = 0;
2996err:
2997 if (thread)
Todd Kjos08dabce2017-06-29 12:01:49 -07002998 thread->looper_need_return = false;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002999 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003000 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
3001 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05303002 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 -07003003err_unlocked:
3004 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003005 return ret;
3006}
3007
3008static void binder_vma_open(struct vm_area_struct *vma)
3009{
3010 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003011
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003012 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303013 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003014 proc->pid, vma->vm_start, vma->vm_end,
3015 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3016 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003017}
3018
3019static void binder_vma_close(struct vm_area_struct *vma)
3020{
3021 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003022
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003023 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303024 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003025 proc->pid, vma->vm_start, vma->vm_end,
3026 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3027 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjos19c98722017-06-29 12:01:40 -07003028 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003029 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
3030}
3031
Dave Jiang11bac802017-02-24 14:56:41 -08003032static int binder_vm_fault(struct vm_fault *vmf)
Vinayak Menonddac7d52014-06-02 18:17:59 +05303033{
3034 return VM_FAULT_SIGBUS;
3035}
3036
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07003037static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003038 .open = binder_vma_open,
3039 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05303040 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003041};
3042
Todd Kjos19c98722017-06-29 12:01:40 -07003043static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
3044{
3045 int ret;
3046 struct binder_proc *proc = filp->private_data;
3047 const char *failure_string;
3048
3049 if (proc->tsk != current->group_leader)
3050 return -EINVAL;
3051
3052 if ((vma->vm_end - vma->vm_start) > SZ_4M)
3053 vma->vm_end = vma->vm_start + SZ_4M;
3054
3055 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3056 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
3057 __func__, proc->pid, vma->vm_start, vma->vm_end,
3058 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3059 (unsigned long)pgprot_val(vma->vm_page_prot));
3060
3061 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
3062 ret = -EPERM;
3063 failure_string = "bad vm_flags";
3064 goto err_bad_arg;
3065 }
3066 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
3067 vma->vm_ops = &binder_vm_ops;
3068 vma->vm_private_data = proc;
3069
3070 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
3071 if (ret)
3072 return ret;
3073 proc->files = get_files_struct(current);
3074 return 0;
3075
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003076err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04003077 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003078 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
3079 return ret;
3080}
3081
3082static int binder_open(struct inode *nodp, struct file *filp)
3083{
3084 struct binder_proc *proc;
Martijn Coenenac4812c2017-02-03 14:40:48 -08003085 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003086
3087 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
3088 current->group_leader->pid, current->pid);
3089
3090 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
3091 if (proc == NULL)
3092 return -ENOMEM;
Todd Kjosc4ea41b2017-06-29 12:01:36 -07003093 get_task_struct(current->group_leader);
3094 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003095 INIT_LIST_HEAD(&proc->todo);
3096 init_waitqueue_head(&proc->wait);
3097 proc->default_priority = task_nice(current);
Martijn Coenenac4812c2017-02-03 14:40:48 -08003098 binder_dev = container_of(filp->private_data, struct binder_device,
3099 miscdev);
3100 proc->context = &binder_dev->context;
Todd Kjos19c98722017-06-29 12:01:40 -07003101 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003102
3103 binder_lock(__func__);
3104
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003105 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003106 proc->pid = current->group_leader->pid;
3107 INIT_LIST_HEAD(&proc->delivered_death);
3108 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003109
3110 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003111
Todd Kjosc44b1232017-06-29 12:01:43 -07003112 mutex_lock(&binder_procs_lock);
3113 hlist_add_head(&proc->proc_node, &binder_procs);
3114 mutex_unlock(&binder_procs_lock);
3115
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003116 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003117 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09003118
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003119 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08003120 /*
3121 * proc debug entries are shared between contexts, so
3122 * this will fail if the process tries to open the driver
3123 * again with a different context. The priting code will
3124 * anyway print all contexts that a given PID has, so this
3125 * is not a problem.
3126 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003127 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen14db3182017-02-03 14:40:47 -08003128 binder_debugfs_dir_entry_proc,
3129 (void *)(unsigned long)proc->pid,
3130 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003131 }
3132
3133 return 0;
3134}
3135
3136static int binder_flush(struct file *filp, fl_owner_t id)
3137{
3138 struct binder_proc *proc = filp->private_data;
3139
3140 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3141
3142 return 0;
3143}
3144
3145static void binder_deferred_flush(struct binder_proc *proc)
3146{
3147 struct rb_node *n;
3148 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09003149
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003150 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3151 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09003152
Todd Kjos08dabce2017-06-29 12:01:49 -07003153 thread->looper_need_return = true;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003154 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3155 wake_up_interruptible(&thread->wait);
3156 wake_count++;
3157 }
3158 }
3159 wake_up_interruptible_all(&proc->wait);
3160
3161 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3162 "binder_flush: %d woke %d threads\n", proc->pid,
3163 wake_count);
3164}
3165
3166static int binder_release(struct inode *nodp, struct file *filp)
3167{
3168 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003169
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003170 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003171 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3172
3173 return 0;
3174}
3175
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003176static int binder_node_release(struct binder_node *node, int refs)
3177{
3178 struct binder_ref *ref;
3179 int death = 0;
3180
3181 list_del_init(&node->work.entry);
3182 binder_release_work(&node->async_todo);
3183
3184 if (hlist_empty(&node->refs)) {
3185 kfree(node);
3186 binder_stats_deleted(BINDER_STAT_NODE);
3187
3188 return refs;
3189 }
3190
3191 node->proc = NULL;
3192 node->local_strong_refs = 0;
3193 node->local_weak_refs = 0;
Todd Kjosc44b1232017-06-29 12:01:43 -07003194
3195 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003196 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -07003197 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003198
3199 hlist_for_each_entry(ref, &node->refs, node_entry) {
3200 refs++;
3201
3202 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08003203 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003204
3205 death++;
3206
3207 if (list_empty(&ref->death->work.entry)) {
3208 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3209 list_add_tail(&ref->death->work.entry,
3210 &ref->proc->todo);
3211 wake_up_interruptible(&ref->proc->wait);
3212 } else
3213 BUG();
3214 }
3215
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003216 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3217 "node %d now dead, refs %d, death %d\n",
3218 node->debug_id, refs, death);
3219
3220 return refs;
3221}
3222
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003223static void binder_deferred_release(struct binder_proc *proc)
3224{
Martijn Coenen342e5c92017-02-03 14:40:46 -08003225 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003226 struct rb_node *n;
Todd Kjos19c98722017-06-29 12:01:40 -07003227 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003228
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003229 BUG_ON(proc->files);
3230
Todd Kjosc44b1232017-06-29 12:01:43 -07003231 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003232 hlist_del(&proc->proc_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07003233 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003234
Todd Kjosc44b1232017-06-29 12:01:43 -07003235 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08003236 if (context->binder_context_mgr_node &&
3237 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003238 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003239 "%s: %d context_mgr_node gone\n",
3240 __func__, proc->pid);
Martijn Coenen342e5c92017-02-03 14:40:46 -08003241 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003242 }
Todd Kjosc44b1232017-06-29 12:01:43 -07003243 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003244
3245 threads = 0;
3246 active_transactions = 0;
3247 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003248 struct binder_thread *thread;
3249
3250 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003251 threads++;
3252 active_transactions += binder_free_thread(proc, thread);
3253 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003254
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003255 nodes = 0;
3256 incoming_refs = 0;
3257 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003258 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003259
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003260 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003261 nodes++;
3262 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003263 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003264 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003265
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003266 outgoing_refs = 0;
3267 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003268 struct binder_ref *ref;
3269
3270 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003271 outgoing_refs++;
3272 binder_delete_ref(ref);
3273 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003274
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003275 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003276 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003277
Todd Kjos19c98722017-06-29 12:01:40 -07003278 binder_alloc_deferred_release(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003279 binder_stats_deleted(BINDER_STAT_PROC);
3280
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003281 put_task_struct(proc->tsk);
3282
3283 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjos19c98722017-06-29 12:01:40 -07003284 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003285 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjos19c98722017-06-29 12:01:40 -07003286 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003287
3288 kfree(proc);
3289}
3290
3291static void binder_deferred_func(struct work_struct *work)
3292{
3293 struct binder_proc *proc;
3294 struct files_struct *files;
3295
3296 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003297
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003298 do {
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003299 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003300 mutex_lock(&binder_deferred_lock);
3301 if (!hlist_empty(&binder_deferred_list)) {
3302 proc = hlist_entry(binder_deferred_list.first,
3303 struct binder_proc, deferred_work_node);
3304 hlist_del_init(&proc->deferred_work_node);
3305 defer = proc->deferred_work;
3306 proc->deferred_work = 0;
3307 } else {
3308 proc = NULL;
3309 defer = 0;
3310 }
3311 mutex_unlock(&binder_deferred_lock);
3312
3313 files = NULL;
3314 if (defer & BINDER_DEFERRED_PUT_FILES) {
3315 files = proc->files;
3316 if (files)
3317 proc->files = NULL;
3318 }
3319
3320 if (defer & BINDER_DEFERRED_FLUSH)
3321 binder_deferred_flush(proc);
3322
3323 if (defer & BINDER_DEFERRED_RELEASE)
3324 binder_deferred_release(proc); /* frees proc */
3325
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003326 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003327 if (files)
3328 put_files_struct(files);
3329 } while (proc);
3330}
3331static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3332
3333static void
3334binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3335{
3336 mutex_lock(&binder_deferred_lock);
3337 proc->deferred_work |= defer;
3338 if (hlist_unhashed(&proc->deferred_work_node)) {
3339 hlist_add_head(&proc->deferred_work_node,
3340 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05303341 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003342 }
3343 mutex_unlock(&binder_deferred_lock);
3344}
3345
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003346static void print_binder_transaction(struct seq_file *m, const char *prefix,
3347 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003348{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003349 seq_printf(m,
3350 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3351 prefix, t->debug_id, t,
3352 t->from ? t->from->proc->pid : 0,
3353 t->from ? t->from->pid : 0,
3354 t->to_proc ? t->to_proc->pid : 0,
3355 t->to_thread ? t->to_thread->pid : 0,
3356 t->code, t->flags, t->priority, t->need_reply);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003357 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003358 seq_puts(m, " buffer free\n");
3359 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003360 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003361 if (t->buffer->target_node)
3362 seq_printf(m, " node %d",
3363 t->buffer->target_node->debug_id);
3364 seq_printf(m, " size %zd:%zd data %p\n",
3365 t->buffer->data_size, t->buffer->offsets_size,
3366 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003367}
3368
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003369static void print_binder_work(struct seq_file *m, const char *prefix,
3370 const char *transaction_prefix,
3371 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003372{
3373 struct binder_node *node;
3374 struct binder_transaction *t;
3375
3376 switch (w->type) {
3377 case BINDER_WORK_TRANSACTION:
3378 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003379 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003380 break;
3381 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003382 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003383 break;
3384 case BINDER_WORK_NODE:
3385 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003386 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3387 prefix, node->debug_id,
3388 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003389 break;
3390 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003391 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003392 break;
3393 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003394 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003395 break;
3396 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003397 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003398 break;
3399 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003400 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401 break;
3402 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003403}
3404
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003405static void print_binder_thread(struct seq_file *m,
3406 struct binder_thread *thread,
3407 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003408{
3409 struct binder_transaction *t;
3410 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003411 size_t start_pos = m->count;
3412 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003413
Todd Kjos08dabce2017-06-29 12:01:49 -07003414 seq_printf(m, " thread %d: l %02x need_return %d\n",
3415 thread->pid, thread->looper,
3416 thread->looper_need_return);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003417 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003418 t = thread->transaction_stack;
3419 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003420 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003421 print_binder_transaction(m,
3422 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003423 t = t->from_parent;
3424 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003425 print_binder_transaction(m,
3426 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003427 t = t->to_parent;
3428 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003429 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003430 t = NULL;
3431 }
3432 }
3433 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003434 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003435 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003436 if (!print_always && m->count == header_pos)
3437 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003438}
3439
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003440static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003441{
3442 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003443 struct binder_work *w;
3444 int count;
3445
3446 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003447 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003448 count++;
3449
Arve Hjønnevågda498892014-02-21 14:40:26 -08003450 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3451 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003452 node->has_strong_ref, node->has_weak_ref,
3453 node->local_strong_refs, node->local_weak_refs,
3454 node->internal_strong_refs, count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003455 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003456 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003457 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003458 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003459 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003460 seq_puts(m, "\n");
3461 list_for_each_entry(w, &node->async_todo, entry)
3462 print_binder_work(m, " ",
3463 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003464}
3465
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003466static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003467{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003468 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3469 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3470 ref->node->debug_id, ref->strong, ref->weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003471}
3472
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003473static void print_binder_proc(struct seq_file *m,
3474 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003475{
3476 struct binder_work *w;
3477 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003478 size_t start_pos = m->count;
3479 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003480
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003481 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08003482 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003483 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003484
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003485 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3486 print_binder_thread(m, rb_entry(n, struct binder_thread,
3487 rb_node), print_all);
3488 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003489 struct binder_node *node = rb_entry(n, struct binder_node,
3490 rb_node);
3491 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003492 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003493 }
3494 if (print_all) {
3495 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003496 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003497 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003498 print_binder_ref(m, rb_entry(n, struct binder_ref,
3499 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003500 }
Todd Kjos19c98722017-06-29 12:01:40 -07003501 binder_alloc_print_allocated(m, &proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003502 list_for_each_entry(w, &proc->todo, entry)
3503 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003504 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003505 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003506 break;
3507 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003508 if (!print_all && m->count == header_pos)
3509 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003510}
3511
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003512static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003513 "BR_ERROR",
3514 "BR_OK",
3515 "BR_TRANSACTION",
3516 "BR_REPLY",
3517 "BR_ACQUIRE_RESULT",
3518 "BR_DEAD_REPLY",
3519 "BR_TRANSACTION_COMPLETE",
3520 "BR_INCREFS",
3521 "BR_ACQUIRE",
3522 "BR_RELEASE",
3523 "BR_DECREFS",
3524 "BR_ATTEMPT_ACQUIRE",
3525 "BR_NOOP",
3526 "BR_SPAWN_LOOPER",
3527 "BR_FINISHED",
3528 "BR_DEAD_BINDER",
3529 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3530 "BR_FAILED_REPLY"
3531};
3532
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003533static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003534 "BC_TRANSACTION",
3535 "BC_REPLY",
3536 "BC_ACQUIRE_RESULT",
3537 "BC_FREE_BUFFER",
3538 "BC_INCREFS",
3539 "BC_ACQUIRE",
3540 "BC_RELEASE",
3541 "BC_DECREFS",
3542 "BC_INCREFS_DONE",
3543 "BC_ACQUIRE_DONE",
3544 "BC_ATTEMPT_ACQUIRE",
3545 "BC_REGISTER_LOOPER",
3546 "BC_ENTER_LOOPER",
3547 "BC_EXIT_LOOPER",
3548 "BC_REQUEST_DEATH_NOTIFICATION",
3549 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen79802402017-02-03 14:40:51 -08003550 "BC_DEAD_BINDER_DONE",
3551 "BC_TRANSACTION_SG",
3552 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003553};
3554
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003555static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003556 "proc",
3557 "thread",
3558 "node",
3559 "ref",
3560 "death",
3561 "transaction",
3562 "transaction_complete"
3563};
3564
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003565static void print_binder_stats(struct seq_file *m, const char *prefix,
3566 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003567{
3568 int i;
3569
3570 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003571 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003572 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003573 int temp = atomic_read(&stats->bc[i]);
3574
3575 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003576 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003577 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003578 }
3579
3580 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003581 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003582 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003583 int temp = atomic_read(&stats->br[i]);
3584
3585 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003586 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003587 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003588 }
3589
3590 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003591 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003592 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003593 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003594 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003595 int created = atomic_read(&stats->obj_created[i]);
3596 int deleted = atomic_read(&stats->obj_deleted[i]);
3597
3598 if (created || deleted)
3599 seq_printf(m, "%s%s: active %d total %d\n",
3600 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003601 binder_objstat_strings[i],
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003602 created - deleted,
3603 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003604 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003605}
3606
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003607static void print_binder_proc_stats(struct seq_file *m,
3608 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003609{
3610 struct binder_work *w;
3611 struct rb_node *n;
3612 int count, strong, weak;
3613
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003614 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08003615 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003616 count = 0;
3617 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3618 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003619 seq_printf(m, " threads: %d\n", count);
3620 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003621 " ready threads %d\n"
3622 " free async space %zd\n", proc->requested_threads,
3623 proc->requested_threads_started, proc->max_threads,
Todd Kjos19c98722017-06-29 12:01:40 -07003624 proc->ready_threads,
3625 binder_alloc_get_free_async_space(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003626 count = 0;
3627 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3628 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003629 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003630 count = 0;
3631 strong = 0;
3632 weak = 0;
3633 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3634 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3635 rb_node_desc);
3636 count++;
3637 strong += ref->strong;
3638 weak += ref->weak;
3639 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003640 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003641
Todd Kjos19c98722017-06-29 12:01:40 -07003642 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003643 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003644
3645 count = 0;
3646 list_for_each_entry(w, &proc->todo, entry) {
3647 switch (w->type) {
3648 case BINDER_WORK_TRANSACTION:
3649 count++;
3650 break;
3651 default:
3652 break;
3653 }
3654 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003655 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003656
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003657 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003658}
3659
3660
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003661static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003662{
3663 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003664 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003665
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003666 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003667
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003668 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003669
Todd Kjosc44b1232017-06-29 12:01:43 -07003670 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003671 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003672 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003673 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003674 print_binder_node(m, node);
Todd Kjosc44b1232017-06-29 12:01:43 -07003675 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003676
Todd Kjosc44b1232017-06-29 12:01:43 -07003677 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003678 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003679 print_binder_proc(m, proc, 1);
Todd Kjosc44b1232017-06-29 12:01:43 -07003680 mutex_unlock(&binder_procs_lock);
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003681 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003682 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003683}
3684
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003685static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003686{
3687 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003688
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003689 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003690
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003691 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003692
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003693 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003694
Todd Kjosc44b1232017-06-29 12:01:43 -07003695 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003696 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003697 print_binder_proc_stats(m, proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07003698 mutex_unlock(&binder_procs_lock);
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003699 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003700 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003701}
3702
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003703static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003704{
3705 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003706
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003707 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003708
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003709 seq_puts(m, "binder transactions:\n");
Todd Kjosc44b1232017-06-29 12:01:43 -07003710 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003711 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003712 print_binder_proc(m, proc, 0);
Todd Kjosc44b1232017-06-29 12:01:43 -07003713 mutex_unlock(&binder_procs_lock);
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003714 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003715 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003716}
3717
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003718static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003719{
Riley Andrews83050a42016-02-09 21:05:33 -08003720 struct binder_proc *itr;
Martijn Coenen14db3182017-02-03 14:40:47 -08003721 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003722
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003723 binder_lock(__func__);
Riley Andrews83050a42016-02-09 21:05:33 -08003724
Todd Kjosc44b1232017-06-29 12:01:43 -07003725 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08003726 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen14db3182017-02-03 14:40:47 -08003727 if (itr->pid == pid) {
3728 seq_puts(m, "binder proc state:\n");
3729 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08003730 }
3731 }
Todd Kjosc44b1232017-06-29 12:01:43 -07003732 mutex_unlock(&binder_procs_lock);
3733
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003734 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003735 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003736}
3737
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003738static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003739 struct binder_transaction_log_entry *e)
3740{
Todd Kjosd99c7332017-06-29 12:01:53 -07003741 int debug_id = READ_ONCE(e->debug_id_done);
3742 /*
3743 * read barrier to guarantee debug_id_done read before
3744 * we print the log values
3745 */
3746 smp_rmb();
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003747 seq_printf(m,
Todd Kjosd99c7332017-06-29 12:01:53 -07003748 "%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 -07003749 e->debug_id, (e->call_type == 2) ? "reply" :
3750 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen14db3182017-02-03 14:40:47 -08003751 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjos57ada2f2017-06-29 12:01:46 -07003752 e->to_node, e->target_handle, e->data_size, e->offsets_size,
3753 e->return_error, e->return_error_param,
3754 e->return_error_line);
Todd Kjosd99c7332017-06-29 12:01:53 -07003755 /*
3756 * read-barrier to guarantee read of debug_id_done after
3757 * done printing the fields of the entry
3758 */
3759 smp_rmb();
3760 seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ?
3761 "\n" : " (incomplete)\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003762}
3763
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003764static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003765{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003766 struct binder_transaction_log *log = m->private;
Todd Kjosd99c7332017-06-29 12:01:53 -07003767 unsigned int log_cur = atomic_read(&log->cur);
3768 unsigned int count;
3769 unsigned int cur;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003770 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003771
Todd Kjosd99c7332017-06-29 12:01:53 -07003772 count = log_cur + 1;
3773 cur = count < ARRAY_SIZE(log->entry) && !log->full ?
3774 0 : count % ARRAY_SIZE(log->entry);
3775 if (count > ARRAY_SIZE(log->entry) || log->full)
3776 count = ARRAY_SIZE(log->entry);
3777 for (i = 0; i < count; i++) {
3778 unsigned int index = cur++ % ARRAY_SIZE(log->entry);
3779
3780 print_binder_transaction_log_entry(m, &log->entry[index]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003781 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003782 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003783}
3784
3785static const struct file_operations binder_fops = {
3786 .owner = THIS_MODULE,
3787 .poll = binder_poll,
3788 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003789 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003790 .mmap = binder_mmap,
3791 .open = binder_open,
3792 .flush = binder_flush,
3793 .release = binder_release,
3794};
3795
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003796BINDER_DEBUG_ENTRY(state);
3797BINDER_DEBUG_ENTRY(stats);
3798BINDER_DEBUG_ENTRY(transactions);
3799BINDER_DEBUG_ENTRY(transaction_log);
3800
Martijn Coenenac4812c2017-02-03 14:40:48 -08003801static int __init init_binder_device(const char *name)
3802{
3803 int ret;
3804 struct binder_device *binder_device;
3805
3806 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
3807 if (!binder_device)
3808 return -ENOMEM;
3809
3810 binder_device->miscdev.fops = &binder_fops;
3811 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
3812 binder_device->miscdev.name = name;
3813
3814 binder_device->context.binder_context_mgr_uid = INVALID_UID;
3815 binder_device->context.name = name;
Todd Kjosc44b1232017-06-29 12:01:43 -07003816 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenenac4812c2017-02-03 14:40:48 -08003817
3818 ret = misc_register(&binder_device->miscdev);
3819 if (ret < 0) {
3820 kfree(binder_device);
3821 return ret;
3822 }
3823
3824 hlist_add_head(&binder_device->hlist, &binder_devices);
3825
3826 return ret;
3827}
3828
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003829static int __init binder_init(void)
3830{
3831 int ret;
Martijn Coenenac4812c2017-02-03 14:40:48 -08003832 char *device_name, *device_names;
3833 struct binder_device *device;
3834 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003835
Todd Kjosd99c7332017-06-29 12:01:53 -07003836 atomic_set(&binder_transaction_log.cur, ~0U);
3837 atomic_set(&binder_transaction_log_failed.cur, ~0U);
3838
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003839 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3840 if (binder_debugfs_dir_entry_root)
3841 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3842 binder_debugfs_dir_entry_root);
Martijn Coenenac4812c2017-02-03 14:40:48 -08003843
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003844 if (binder_debugfs_dir_entry_root) {
3845 debugfs_create_file("state",
3846 S_IRUGO,
3847 binder_debugfs_dir_entry_root,
3848 NULL,
3849 &binder_state_fops);
3850 debugfs_create_file("stats",
3851 S_IRUGO,
3852 binder_debugfs_dir_entry_root,
3853 NULL,
3854 &binder_stats_fops);
3855 debugfs_create_file("transactions",
3856 S_IRUGO,
3857 binder_debugfs_dir_entry_root,
3858 NULL,
3859 &binder_transactions_fops);
3860 debugfs_create_file("transaction_log",
3861 S_IRUGO,
3862 binder_debugfs_dir_entry_root,
3863 &binder_transaction_log,
3864 &binder_transaction_log_fops);
3865 debugfs_create_file("failed_transaction_log",
3866 S_IRUGO,
3867 binder_debugfs_dir_entry_root,
3868 &binder_transaction_log_failed,
3869 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003870 }
Martijn Coenenac4812c2017-02-03 14:40:48 -08003871
3872 /*
3873 * Copy the module_parameter string, because we don't want to
3874 * tokenize it in-place.
3875 */
3876 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
3877 if (!device_names) {
3878 ret = -ENOMEM;
3879 goto err_alloc_device_names_failed;
3880 }
3881 strcpy(device_names, binder_devices_param);
3882
3883 while ((device_name = strsep(&device_names, ","))) {
3884 ret = init_binder_device(device_name);
3885 if (ret)
3886 goto err_init_binder_device_failed;
3887 }
3888
3889 return ret;
3890
3891err_init_binder_device_failed:
3892 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
3893 misc_deregister(&device->miscdev);
3894 hlist_del(&device->hlist);
3895 kfree(device);
3896 }
3897err_alloc_device_names_failed:
3898 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
3899
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003900 return ret;
3901}
3902
3903device_initcall(binder_init);
3904
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003905#define CREATE_TRACE_POINTS
3906#include "binder_trace.h"
3907
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003908MODULE_LICENSE("GPL v2");