blob: 0cd37910ff83c058ebd787460d04186080729462 [file] [log] [blame]
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001/* binder.c
2 *
3 * Android IPC Subsystem
4 *
5 * Copyright (C) 2007-2008 Google, Inc.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Anmol Sarma56b468f2012-10-30 22:35:43 +053018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090020#include <asm/cacheflush.h>
21#include <linux/fdtable.h>
22#include <linux/file.h>
Colin Crosse2610b22013-05-06 23:50:15 +000023#include <linux/freezer.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090024#include <linux/fs.h>
25#include <linux/list.h>
26#include <linux/miscdevice.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090027#include <linux/module.h>
28#include <linux/mutex.h>
29#include <linux/nsproxy.h>
30#include <linux/poll.h>
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070031#include <linux/debugfs.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090032#include <linux/rbtree.h>
33#include <linux/sched.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070034#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090035#include <linux/uaccess.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080036#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050037#include <linux/security.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090038
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020039#ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
40#define BINDER_IPC_32BIT 1
41#endif
42
43#include <uapi/linux/android/binder.h>
Todd Kjosb9341022016-10-10 10:40:53 -070044#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070045#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090046
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070047static DEFINE_MUTEX(binder_main_lock);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070048
49static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090050static DEFINE_MUTEX(binder_deferred_lock);
51
Martijn Coenen6b7c7122016-09-30 16:08:09 +020052static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090053static HLIST_HEAD(binder_procs);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070054static DEFINE_MUTEX(binder_procs_lock);
55
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090056static HLIST_HEAD(binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -070057static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090058
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070059static struct dentry *binder_debugfs_dir_entry_root;
60static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjosc4bd08b2017-05-25 10:56:00 -070061static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090062
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070063#define BINDER_DEBUG_ENTRY(name) \
64static int binder_##name##_open(struct inode *inode, struct file *file) \
65{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070066 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070067} \
68\
69static const struct file_operations binder_##name##_fops = { \
70 .owner = THIS_MODULE, \
71 .open = binder_##name##_open, \
72 .read = seq_read, \
73 .llseek = seq_lseek, \
74 .release = single_release, \
75}
76
77static int binder_proc_show(struct seq_file *m, void *unused);
78BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090079
80/* This is only defined in include/asm-arm/sizes.h */
81#ifndef SZ_1K
82#define SZ_1K 0x400
83#endif
84
85#ifndef SZ_4M
86#define SZ_4M 0x400000
87#endif
88
89#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
90
91#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
92
93enum {
94 BINDER_DEBUG_USER_ERROR = 1U << 0,
95 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
96 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
97 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
98 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
99 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
100 BINDER_DEBUG_READ_WRITE = 1U << 6,
101 BINDER_DEBUG_USER_REFS = 1U << 7,
102 BINDER_DEBUG_THREADS = 1U << 8,
103 BINDER_DEBUG_TRANSACTION = 1U << 9,
104 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
105 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
106 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjosd325d372016-10-10 10:40:53 -0700107 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900108};
109static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
110 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
111module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
112
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200113static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
114module_param_named(devices, binder_devices_param, charp, S_IRUGO);
115
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900116static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
117static int binder_stop_on_user_error;
118
119static int binder_set_stop_on_user_error(const char *val,
120 struct kernel_param *kp)
121{
122 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900123
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900124 ret = param_set_int(val, kp);
125 if (binder_stop_on_user_error < 2)
126 wake_up(&binder_user_error_wait);
127 return ret;
128}
129module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
130 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
131
132#define binder_debug(mask, x...) \
133 do { \
134 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400135 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900136 } while (0)
137
138#define binder_user_error(x...) \
139 do { \
140 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400141 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900142 if (binder_stop_on_user_error) \
143 binder_stop_on_user_error = 2; \
144 } while (0)
145
Martijn Coenen00c80372016-07-13 12:06:49 +0200146#define to_flat_binder_object(hdr) \
147 container_of(hdr, struct flat_binder_object, hdr)
148
149#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
150
Martijn Coenen5a6da532016-09-30 14:10:07 +0200151#define to_binder_buffer_object(hdr) \
152 container_of(hdr, struct binder_buffer_object, hdr)
153
Martijn Coenene3e0f4802016-10-18 13:58:55 +0200154#define to_binder_fd_array_object(hdr) \
155 container_of(hdr, struct binder_fd_array_object, hdr)
156
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900157enum binder_stat_types {
158 BINDER_STAT_PROC,
159 BINDER_STAT_THREAD,
160 BINDER_STAT_NODE,
161 BINDER_STAT_REF,
162 BINDER_STAT_DEATH,
163 BINDER_STAT_TRANSACTION,
164 BINDER_STAT_TRANSACTION_COMPLETE,
165 BINDER_STAT_COUNT
166};
167
168struct binder_stats {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700169 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
170 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
171 atomic_t obj_created[BINDER_STAT_COUNT];
172 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900173};
174
175static struct binder_stats binder_stats;
176
177static inline void binder_stats_deleted(enum binder_stat_types type)
178{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700179 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900180}
181
182static inline void binder_stats_created(enum binder_stat_types type)
183{
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -0700184 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900185}
186
187struct binder_transaction_log_entry {
188 int debug_id;
189 int call_type;
190 int from_proc;
191 int from_thread;
192 int target_handle;
193 int to_proc;
194 int to_thread;
195 int to_node;
196 int data_size;
197 int offsets_size;
Todd Kjose598d172017-03-22 17:19:52 -0700198 int return_error_line;
199 uint32_t return_error;
200 uint32_t return_error_param;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200201 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900202};
203struct binder_transaction_log {
204 int next;
205 int full;
206 struct binder_transaction_log_entry entry[32];
207};
208static struct binder_transaction_log binder_transaction_log;
209static struct binder_transaction_log binder_transaction_log_failed;
210
211static struct binder_transaction_log_entry *binder_transaction_log_add(
212 struct binder_transaction_log *log)
213{
214 struct binder_transaction_log_entry *e;
Seunghun Lee10f62862014-05-01 01:30:23 +0900215
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900216 e = &log->entry[log->next];
217 memset(e, 0, sizeof(*e));
218 log->next++;
219 if (log->next == ARRAY_SIZE(log->entry)) {
220 log->next = 0;
221 log->full = 1;
222 }
223 return e;
224}
225
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200226struct binder_context {
227 struct binder_node *binder_context_mgr_node;
Todd Kjos8d9f6f32016-10-17 12:33:15 -0700228 struct mutex context_mgr_node_lock;
229
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200230 kuid_t binder_context_mgr_uid;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200231 const char *name;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200232};
233
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200234struct binder_device {
235 struct hlist_node hlist;
236 struct miscdevice miscdev;
237 struct binder_context context;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200238};
239
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900240struct binder_work {
241 struct list_head entry;
242 enum {
243 BINDER_WORK_TRANSACTION = 1,
244 BINDER_WORK_TRANSACTION_COMPLETE,
245 BINDER_WORK_NODE,
246 BINDER_WORK_DEAD_BINDER,
247 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
248 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
249 } type;
250};
251
252struct binder_node {
253 int debug_id;
254 struct binder_work work;
255 union {
256 struct rb_node rb_node;
257 struct hlist_node dead_node;
258 };
259 struct binder_proc *proc;
260 struct hlist_head refs;
261 int internal_strong_refs;
262 int local_weak_refs;
263 int local_strong_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800264 binder_uintptr_t ptr;
265 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900266 unsigned has_strong_ref:1;
267 unsigned pending_strong_ref:1;
268 unsigned has_weak_ref:1;
269 unsigned pending_weak_ref:1;
270 unsigned has_async_transaction:1;
271 unsigned accept_fds:1;
272 unsigned min_priority:8;
273 struct list_head async_todo;
274};
275
276struct binder_ref_death {
277 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800278 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900279};
280
281struct binder_ref {
282 /* Lookups needed: */
283 /* node + proc => ref (transaction) */
284 /* desc + proc => ref (transaction, inc/dec ref) */
285 /* node => refs + procs (proc exit) */
286 int debug_id;
287 struct rb_node rb_node_desc;
288 struct rb_node rb_node_node;
289 struct hlist_node node_entry;
290 struct binder_proc *proc;
291 struct binder_node *node;
292 uint32_t desc;
293 int strong;
294 int weak;
295 struct binder_ref_death *death;
296};
297
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900298enum binder_deferred_state {
299 BINDER_DEFERRED_PUT_FILES = 0x01,
300 BINDER_DEFERRED_FLUSH = 0x02,
301 BINDER_DEFERRED_RELEASE = 0x04,
302};
303
304struct binder_proc {
305 struct hlist_node proc_node;
306 struct rb_root threads;
307 struct rb_root nodes;
308 struct rb_root refs_by_desc;
309 struct rb_root refs_by_node;
310 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900311 struct task_struct *tsk;
312 struct files_struct *files;
313 struct hlist_node deferred_work_node;
314 int deferred_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900315
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900316 struct list_head todo;
317 wait_queue_head_t wait;
318 struct binder_stats stats;
319 struct list_head delivered_death;
320 int max_threads;
321 int requested_threads;
322 int requested_threads_started;
323 int ready_threads;
324 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700325 struct dentry *debugfs_entry;
Todd Kjosf85d2292016-10-10 10:39:59 -0700326 struct binder_alloc alloc;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200327 struct binder_context *context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900328};
329
330enum {
331 BINDER_LOOPER_STATE_REGISTERED = 0x01,
332 BINDER_LOOPER_STATE_ENTERED = 0x02,
333 BINDER_LOOPER_STATE_EXITED = 0x04,
334 BINDER_LOOPER_STATE_INVALID = 0x08,
335 BINDER_LOOPER_STATE_WAITING = 0x10,
336 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
337};
338
339struct binder_thread {
340 struct binder_proc *proc;
341 struct rb_node rb_node;
342 int pid;
343 int looper;
344 struct binder_transaction *transaction_stack;
345 struct list_head todo;
346 uint32_t return_error; /* Write failed, return error code in read buf */
347 uint32_t return_error2; /* Write failed, return error code in read */
348 /* buffer. Used when sending a reply to a dead process that */
349 /* we are also waiting on */
350 wait_queue_head_t wait;
351 struct binder_stats stats;
352};
353
354struct binder_transaction {
355 int debug_id;
356 struct binder_work work;
357 struct binder_thread *from;
358 struct binder_transaction *from_parent;
359 struct binder_proc *to_proc;
360 struct binder_thread *to_thread;
361 struct binder_transaction *to_parent;
362 unsigned need_reply:1;
363 /* unsigned is_dead:1; */ /* not used at the moment */
364
365 struct binder_buffer *buffer;
366 unsigned int code;
367 unsigned int flags;
368 long priority;
369 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600370 kuid_t sender_euid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900371};
372
373static void
374binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
375
Sachin Kamatefde99c2012-08-17 16:39:36 +0530376static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900377{
378 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900379 unsigned long rlim_cur;
380 unsigned long irqs;
381
382 if (files == NULL)
383 return -ESRCH;
384
Al Virodcfadfa2012-08-12 17:27:30 -0400385 if (!lock_task_sighand(proc->tsk, &irqs))
386 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900387
Al Virodcfadfa2012-08-12 17:27:30 -0400388 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
389 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900390
Al Virodcfadfa2012-08-12 17:27:30 -0400391 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900392}
393
394/*
395 * copied from fd_install
396 */
397static void task_fd_install(
398 struct binder_proc *proc, unsigned int fd, struct file *file)
399{
Al Virof869e8a2012-08-15 21:06:33 -0400400 if (proc->files)
401 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900402}
403
404/*
405 * copied from sys_close
406 */
407static long task_close_fd(struct binder_proc *proc, unsigned int fd)
408{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900409 int retval;
410
Al Viro483ce1d2012-08-19 12:04:24 -0400411 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900412 return -ESRCH;
413
Al Viro483ce1d2012-08-19 12:04:24 -0400414 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900415 /* can't restart close syscall because file table entry was cleared */
416 if (unlikely(retval == -ERESTARTSYS ||
417 retval == -ERESTARTNOINTR ||
418 retval == -ERESTARTNOHAND ||
419 retval == -ERESTART_RESTARTBLOCK))
420 retval = -EINTR;
421
422 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900423}
424
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700425static inline void binder_lock(const char *tag)
426{
427 trace_binder_lock(tag);
428 mutex_lock(&binder_main_lock);
429 trace_binder_locked(tag);
430}
431
432static inline void binder_unlock(const char *tag)
433{
434 trace_binder_unlock(tag);
435 mutex_unlock(&binder_main_lock);
436}
437
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900438static void binder_set_nice(long nice)
439{
440 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900441
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900442 if (can_nice(current, nice)) {
443 set_user_nice(current, nice);
444 return;
445 }
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900446 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900447 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530448 "%d: nice value %ld not allowed use %ld instead\n",
449 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900450 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +0800451 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900452 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530453 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900454}
455
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900456static struct binder_node *binder_get_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800457 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900458{
459 struct rb_node *n = proc->nodes.rb_node;
460 struct binder_node *node;
461
462 while (n) {
463 node = rb_entry(n, struct binder_node, rb_node);
464
465 if (ptr < node->ptr)
466 n = n->rb_left;
467 else if (ptr > node->ptr)
468 n = n->rb_right;
469 else
470 return node;
471 }
472 return NULL;
473}
474
475static struct binder_node *binder_new_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800476 binder_uintptr_t ptr,
477 binder_uintptr_t cookie)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900478{
479 struct rb_node **p = &proc->nodes.rb_node;
480 struct rb_node *parent = NULL;
481 struct binder_node *node;
482
483 while (*p) {
484 parent = *p;
485 node = rb_entry(parent, struct binder_node, rb_node);
486
487 if (ptr < node->ptr)
488 p = &(*p)->rb_left;
489 else if (ptr > node->ptr)
490 p = &(*p)->rb_right;
491 else
492 return NULL;
493 }
494
495 node = kzalloc(sizeof(*node), GFP_KERNEL);
496 if (node == NULL)
497 return NULL;
498 binder_stats_created(BINDER_STAT_NODE);
499 rb_link_node(&node->rb_node, parent, p);
500 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjosc4bd08b2017-05-25 10:56:00 -0700501 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900502 node->proc = proc;
503 node->ptr = ptr;
504 node->cookie = cookie;
505 node->work.type = BINDER_WORK_NODE;
506 INIT_LIST_HEAD(&node->work.entry);
507 INIT_LIST_HEAD(&node->async_todo);
508 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800509 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900510 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800511 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900512 return node;
513}
514
515static int binder_inc_node(struct binder_node *node, int strong, int internal,
516 struct list_head *target_list)
517{
518 if (strong) {
519 if (internal) {
520 if (target_list == NULL &&
521 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200522 !(node->proc &&
523 node == node->proc->context->
524 binder_context_mgr_node &&
525 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530526 pr_err("invalid inc strong node for %d\n",
527 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900528 return -EINVAL;
529 }
530 node->internal_strong_refs++;
531 } else
532 node->local_strong_refs++;
533 if (!node->has_strong_ref && target_list) {
534 list_del_init(&node->work.entry);
535 list_add_tail(&node->work.entry, target_list);
536 }
537 } else {
538 if (!internal)
539 node->local_weak_refs++;
540 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
541 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530542 pr_err("invalid inc weak node for %d\n",
543 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900544 return -EINVAL;
545 }
546 list_add_tail(&node->work.entry, target_list);
547 }
548 }
549 return 0;
550}
551
552static int binder_dec_node(struct binder_node *node, int strong, int internal)
553{
554 if (strong) {
555 if (internal)
556 node->internal_strong_refs--;
557 else
558 node->local_strong_refs--;
559 if (node->local_strong_refs || node->internal_strong_refs)
560 return 0;
561 } else {
562 if (!internal)
563 node->local_weak_refs--;
564 if (node->local_weak_refs || !hlist_empty(&node->refs))
565 return 0;
566 }
567 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
568 if (list_empty(&node->work.entry)) {
569 list_add_tail(&node->work.entry, &node->proc->todo);
570 wake_up_interruptible(&node->proc->wait);
571 }
572 } else {
573 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
574 !node->local_weak_refs) {
575 list_del_init(&node->work.entry);
576 if (node->proc) {
577 rb_erase(&node->rb_node, &node->proc->nodes);
578 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530579 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900580 node->debug_id);
581 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -0700582 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900583 hlist_del(&node->dead_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -0700584 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900585 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530586 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900587 node->debug_id);
588 }
589 kfree(node);
590 binder_stats_deleted(BINDER_STAT_NODE);
591 }
592 }
593
594 return 0;
595}
596
597
598static struct binder_ref *binder_get_ref(struct binder_proc *proc,
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200599 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900600{
601 struct rb_node *n = proc->refs_by_desc.rb_node;
602 struct binder_ref *ref;
603
604 while (n) {
605 ref = rb_entry(n, struct binder_ref, rb_node_desc);
606
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200607 if (desc < ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900608 n = n->rb_left;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200609 } else if (desc > ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900610 n = n->rb_right;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200611 } else if (need_strong_ref && !ref->strong) {
612 binder_user_error("tried to use weak ref as strong ref\n");
613 return NULL;
614 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900615 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +0200616 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900617 }
618 return NULL;
619}
620
621static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
622 struct binder_node *node)
623{
624 struct rb_node *n;
625 struct rb_node **p = &proc->refs_by_node.rb_node;
626 struct rb_node *parent = NULL;
627 struct binder_ref *ref, *new_ref;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200628 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900629
630 while (*p) {
631 parent = *p;
632 ref = rb_entry(parent, struct binder_ref, rb_node_node);
633
634 if (node < ref->node)
635 p = &(*p)->rb_left;
636 else if (node > ref->node)
637 p = &(*p)->rb_right;
638 else
639 return ref;
640 }
641 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
642 if (new_ref == NULL)
643 return NULL;
644 binder_stats_created(BINDER_STAT_REF);
Todd Kjosc4bd08b2017-05-25 10:56:00 -0700645 new_ref->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900646 new_ref->proc = proc;
647 new_ref->node = node;
648 rb_link_node(&new_ref->rb_node_node, parent, p);
649 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
650
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200651 new_ref->desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900652 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
653 ref = rb_entry(n, struct binder_ref, rb_node_desc);
654 if (ref->desc > new_ref->desc)
655 break;
656 new_ref->desc = ref->desc + 1;
657 }
658
659 p = &proc->refs_by_desc.rb_node;
660 while (*p) {
661 parent = *p;
662 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
663
664 if (new_ref->desc < ref->desc)
665 p = &(*p)->rb_left;
666 else if (new_ref->desc > ref->desc)
667 p = &(*p)->rb_right;
668 else
669 BUG();
670 }
671 rb_link_node(&new_ref->rb_node_desc, parent, p);
672 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
673 if (node) {
674 hlist_add_head(&new_ref->node_entry, &node->refs);
675
676 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530677 "%d new ref %d desc %d for node %d\n",
678 proc->pid, new_ref->debug_id, new_ref->desc,
679 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900680 } else {
681 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530682 "%d new ref %d desc %d for dead node\n",
683 proc->pid, new_ref->debug_id, new_ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900684 }
685 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 Coenen00c80372016-07-13 12:06:49 +0200838/**
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 Coenen5a6da532016-09-30 14:10:07 +0200869 case BINDER_TYPE_PTR:
870 object_size = sizeof(struct binder_buffer_object);
871 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +0200872 case BINDER_TYPE_FDA:
873 object_size = sizeof(struct binder_fd_array_object);
874 break;
Martijn Coenen00c80372016-07-13 12:06:49 +0200875 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 Coenen5a6da532016-09-30 14:10:07 +0200885/**
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 Coenen5a6da532016-09-30 14:10:07 +0200989 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 Coenen5a6da532016-09-30 14:10:07 +02001000 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 Coenen5a6da532016-09-30 14:10:07 +02001005 off_end = (void *)off_start + buffer->offsets_size;
1006 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02001007 struct binder_object_header *hdr;
1008 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09001009
Martijn Coenen00c80372016-07-13 12:06:49 +02001010 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 Coenen00c80372016-07-13 12:06:49 +02001015 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 Coenen00c80372016-07-13 12:06:49 +02001019 struct flat_binder_object *fp;
1020 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09001021
Martijn Coenen00c80372016-07-13 12:06:49 +02001022 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 Coenen00c80372016-07-13 12:06:49 +02001032 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 Coenen00c80372016-07-13 12:06:49 +02001037 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001038 struct binder_ref *ref;
1039
Martijn Coenen00c80372016-07-13 12:06:49 +02001040 fp = to_flat_binder_object(hdr);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001041 ref = binder_get_ref(proc, fp->handle,
Martijn Coenen00c80372016-07-13 12:06:49 +02001042 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 Coenen00c80372016-07-13 12:06:49 +02001051 binder_dec_ref(ref, hdr->type == BINDER_TYPE_HANDLE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001052 } break;
1053
Martijn Coenen00c80372016-07-13 12:06:49 +02001054 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 Coenen00c80372016-07-13 12:06:49 +02001058 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001059 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02001060 task_close_fd(proc, fp->fd);
1061 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001062 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 Coenene3e0f4802016-10-18 13:58:55 +02001068 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 Kjosd325d372016-10-10 10:40:53 -07001090 binder_alloc_get_user_buffer_offset(
1091 &proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001092
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 Coenen00c80372016-07-13 12:06:49 +02001112 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001113 break;
1114 }
1115 }
1116}
1117
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001118static 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 Kjose598d172017-03-22 17:19:52 -07001148 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001149
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 Kjose598d172017-03-22 17:19:52 -07001205 return -ENOMEM;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001206
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 Coenene3e0f4802016-10-18 13:58:55 +02001279static 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 Kjosd325d372016-10-10 10:40:53 -07001309 parent_buffer = parent->buffer -
1310 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001311 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 Coenen5a6da532016-09-30 14:10:07 +02001337static 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 Kjosd325d372016-10-10 10:40:53 -07001378 binder_alloc_get_user_buffer_offset(
1379 &target_proc->alloc));
Martijn Coenen5a6da532016-09-30 14:10:07 +02001380 *(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 Coenen59878d72016-09-30 14:05:40 +02001387 struct binder_transaction_data *tr, int reply,
1388 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001389{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001390 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001391 struct binder_transaction *t;
1392 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001393 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001394 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001395 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 Kjose598d172017-03-22 17:19:52 -07001403 uint32_t return_error = 0;
1404 uint32_t return_error_param = 0;
1405 uint32_t return_error_line = 0;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001406 struct binder_buffer_object *last_fixup_obj = NULL;
1407 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001408 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001409
1410 e = binder_transaction_log_add(&binder_transaction_log);
1411 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1412 e->from_proc = proc->pid;
1413 e->from_thread = thread->pid;
1414 e->target_handle = tr->target.handle;
1415 e->data_size = tr->data_size;
1416 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02001417 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001418
1419 if (reply) {
1420 in_reply_to = thread->transaction_stack;
1421 if (in_reply_to == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301422 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001423 proc->pid, thread->pid);
1424 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001425 return_error_param = -EPROTO;
1426 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001427 goto err_empty_call_stack;
1428 }
1429 binder_set_nice(in_reply_to->saved_priority);
1430 if (in_reply_to->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301431 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 +09001432 proc->pid, thread->pid, in_reply_to->debug_id,
1433 in_reply_to->to_proc ?
1434 in_reply_to->to_proc->pid : 0,
1435 in_reply_to->to_thread ?
1436 in_reply_to->to_thread->pid : 0);
1437 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001438 return_error_param = -EPROTO;
1439 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001440 in_reply_to = NULL;
1441 goto err_bad_call_stack;
1442 }
1443 thread->transaction_stack = in_reply_to->to_parent;
1444 target_thread = in_reply_to->from;
1445 if (target_thread == NULL) {
1446 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001447 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001448 goto err_dead_binder;
1449 }
1450 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301451 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 +09001452 proc->pid, thread->pid,
1453 target_thread->transaction_stack ?
1454 target_thread->transaction_stack->debug_id : 0,
1455 in_reply_to->debug_id);
1456 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001457 return_error_param = -EPROTO;
1458 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001459 in_reply_to = NULL;
1460 target_thread = NULL;
1461 goto err_dead_binder;
1462 }
1463 target_proc = target_thread->proc;
1464 } else {
1465 if (tr->target.handle) {
1466 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001467
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001468 ref = binder_get_ref(proc, tr->target.handle, true);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001469 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301470 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001471 proc->pid, thread->pid);
1472 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001473 return_error_param = -EINVAL;
1474 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001475 goto err_invalid_target_handle;
1476 }
1477 target_node = ref->node;
1478 } else {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001479 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001480 target_node = context->binder_context_mgr_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001481 if (target_node == NULL) {
1482 return_error = BR_DEAD_REPLY;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001483 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjose598d172017-03-22 17:19:52 -07001484 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001485 goto err_no_context_mgr_node;
1486 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001487 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001488 }
1489 e->to_node = target_node->debug_id;
1490 target_proc = target_node->proc;
1491 if (target_proc == NULL) {
1492 return_error = BR_DEAD_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001493 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001494 goto err_dead_binder;
1495 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001496 if (security_binder_transaction(proc->tsk,
1497 target_proc->tsk) < 0) {
1498 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001499 return_error_param = -EPERM;
1500 return_error_line = __LINE__;
Stephen Smalley79af7302015-01-21 10:54:10 -05001501 goto err_invalid_target_handle;
1502 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001503 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1504 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001505
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001506 tmp = thread->transaction_stack;
1507 if (tmp->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301508 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 +09001509 proc->pid, thread->pid, tmp->debug_id,
1510 tmp->to_proc ? tmp->to_proc->pid : 0,
1511 tmp->to_thread ?
1512 tmp->to_thread->pid : 0);
1513 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001514 return_error_param = -EPROTO;
1515 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001516 goto err_bad_call_stack;
1517 }
1518 while (tmp) {
1519 if (tmp->from && tmp->from->proc == target_proc)
1520 target_thread = tmp->from;
1521 tmp = tmp->from_parent;
1522 }
1523 }
1524 }
1525 if (target_thread) {
1526 e->to_thread = target_thread->pid;
1527 target_list = &target_thread->todo;
1528 target_wait = &target_thread->wait;
1529 } else {
1530 target_list = &target_proc->todo;
1531 target_wait = &target_proc->wait;
1532 }
1533 e->to_proc = target_proc->pid;
1534
1535 /* TODO: reuse incoming transaction for reply */
1536 t = kzalloc(sizeof(*t), GFP_KERNEL);
1537 if (t == NULL) {
1538 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001539 return_error_param = -ENOMEM;
1540 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001541 goto err_alloc_t_failed;
1542 }
1543 binder_stats_created(BINDER_STAT_TRANSACTION);
1544
1545 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1546 if (tcomplete == NULL) {
1547 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001548 return_error_param = -ENOMEM;
1549 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001550 goto err_alloc_tcomplete_failed;
1551 }
1552 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1553
Todd Kjosc4bd08b2017-05-25 10:56:00 -07001554 t->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001555 e->debug_id = t->debug_id;
1556
1557 if (reply)
1558 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02001559 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001560 proc->pid, thread->pid, t->debug_id,
1561 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001562 (u64)tr->data.ptr.buffer,
1563 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02001564 (u64)tr->data_size, (u64)tr->offsets_size,
1565 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001566 else
1567 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02001568 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001569 proc->pid, thread->pid, t->debug_id,
1570 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001571 (u64)tr->data.ptr.buffer,
1572 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02001573 (u64)tr->data_size, (u64)tr->offsets_size,
1574 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001575
1576 if (!reply && !(tr->flags & TF_ONE_WAY))
1577 t->from = thread;
1578 else
1579 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03001580 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001581 t->to_proc = target_proc;
1582 t->to_thread = target_thread;
1583 t->code = tr->code;
1584 t->flags = tr->flags;
1585 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001586
1587 trace_binder_transaction(reply, t, target_node);
1588
Todd Kjosd325d372016-10-10 10:40:53 -07001589 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02001590 tr->offsets_size, extra_buffers_size,
1591 !reply && (t->flags & TF_ONE_WAY));
Todd Kjose598d172017-03-22 17:19:52 -07001592 if (IS_ERR(t->buffer)) {
1593 /*
1594 * -ESRCH indicates VMA cleared. The target is dying.
1595 */
1596 return_error_param = PTR_ERR(t->buffer);
1597 return_error = return_error_param == -ESRCH ?
1598 BR_DEAD_REPLY : BR_FAILED_REPLY;
1599 return_error_line = __LINE__;
1600 t->buffer = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001601 goto err_binder_alloc_buf_failed;
1602 }
1603 t->buffer->allow_user_free = 0;
1604 t->buffer->debug_id = t->debug_id;
1605 t->buffer->transaction = t;
1606 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001607 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001608 if (target_node)
1609 binder_inc_node(target_node, 1, 0, NULL);
1610
Martijn Coenen5a6da532016-09-30 14:10:07 +02001611 off_start = (binder_size_t *)(t->buffer->data +
1612 ALIGN(tr->data_size, sizeof(void *)));
1613 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001614
Arve Hjønnevågda498892014-02-21 14:40:26 -08001615 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1616 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301617 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1618 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001619 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001620 return_error_param = -EFAULT;
1621 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001622 goto err_copy_data_failed;
1623 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001624 if (copy_from_user(offp, (const void __user *)(uintptr_t)
1625 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301626 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1627 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001628 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001629 return_error_param = -EFAULT;
1630 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001631 goto err_copy_data_failed;
1632 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001633 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1634 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1635 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001636 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001637 return_error_param = -EINVAL;
1638 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001639 goto err_bad_offset;
1640 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02001641 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
1642 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
1643 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05301644 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02001645 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001646 return_error_param = -EINVAL;
1647 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001648 goto err_bad_offset;
1649 }
1650 off_end = (void *)off_start + tr->offsets_size;
1651 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
1652 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001653 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001654 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02001655 struct binder_object_header *hdr;
1656 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09001657
Martijn Coenen00c80372016-07-13 12:06:49 +02001658 if (object_size == 0 || *offp < off_min) {
1659 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 -08001660 proc->pid, thread->pid, (u64)*offp,
1661 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02001662 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001663 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001664 return_error_param = -EINVAL;
1665 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001666 goto err_bad_offset;
1667 }
Martijn Coenen00c80372016-07-13 12:06:49 +02001668
1669 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
1670 off_min = *offp + object_size;
1671 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001672 case BINDER_TYPE_BINDER:
1673 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02001674 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001675
Martijn Coenen00c80372016-07-13 12:06:49 +02001676 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001677 ret = binder_translate_binder(fp, t, thread);
1678 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02001679 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001680 return_error_param = ret;
1681 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001682 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001683 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001684 } break;
1685 case BINDER_TYPE_HANDLE:
1686 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02001687 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001688
Martijn Coenen00c80372016-07-13 12:06:49 +02001689 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001690 ret = binder_translate_handle(fp, t, thread);
1691 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001692 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001693 return_error_param = ret;
1694 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001695 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001696 }
1697 } break;
1698
1699 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02001700 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001701 int target_fd = binder_translate_fd(fp->fd, t, thread,
1702 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001703
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001704 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001705 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001706 return_error_param = target_fd;
1707 return_error_line = __LINE__;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001708 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001709 }
Martijn Coenen00c80372016-07-13 12:06:49 +02001710 fp->pad_binder = 0;
1711 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001712 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001713 case BINDER_TYPE_FDA: {
1714 struct binder_fd_array_object *fda =
1715 to_binder_fd_array_object(hdr);
1716 struct binder_buffer_object *parent =
1717 binder_validate_ptr(t->buffer, fda->parent,
1718 off_start,
1719 offp - off_start);
1720 if (!parent) {
1721 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
1722 proc->pid, thread->pid);
1723 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001724 return_error_param = -EINVAL;
1725 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001726 goto err_bad_parent;
1727 }
1728 if (!binder_validate_fixup(t->buffer, off_start,
1729 parent, fda->parent_offset,
1730 last_fixup_obj,
1731 last_fixup_min_off)) {
1732 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
1733 proc->pid, thread->pid);
1734 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001735 return_error_param = -EINVAL;
1736 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001737 goto err_bad_parent;
1738 }
1739 ret = binder_translate_fd_array(fda, parent, t, thread,
1740 in_reply_to);
1741 if (ret < 0) {
1742 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001743 return_error_param = ret;
1744 return_error_line = __LINE__;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001745 goto err_translate_failed;
1746 }
1747 last_fixup_obj = parent;
1748 last_fixup_min_off =
1749 fda->parent_offset + sizeof(u32) * fda->num_fds;
1750 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001751 case BINDER_TYPE_PTR: {
1752 struct binder_buffer_object *bp =
1753 to_binder_buffer_object(hdr);
1754 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001755
Martijn Coenen5a6da532016-09-30 14:10:07 +02001756 if (bp->length > buf_left) {
1757 binder_user_error("%d:%d got transaction with too large buffer\n",
1758 proc->pid, thread->pid);
1759 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001760 return_error_param = -EINVAL;
1761 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001762 goto err_bad_offset;
1763 }
1764 if (copy_from_user(sg_bufp,
1765 (const void __user *)(uintptr_t)
1766 bp->buffer, bp->length)) {
1767 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1768 proc->pid, thread->pid);
Todd Kjose598d172017-03-22 17:19:52 -07001769 return_error_param = -EFAULT;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001770 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001771 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001772 goto err_copy_data_failed;
1773 }
1774 /* Fixup buffer pointer to target proc address space */
1775 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjosd325d372016-10-10 10:40:53 -07001776 binder_alloc_get_user_buffer_offset(
1777 &target_proc->alloc);
Martijn Coenen5a6da532016-09-30 14:10:07 +02001778 sg_bufp += ALIGN(bp->length, sizeof(u64));
1779
1780 ret = binder_fixup_parent(t, thread, bp, off_start,
1781 offp - off_start,
1782 last_fixup_obj,
1783 last_fixup_min_off);
1784 if (ret < 0) {
1785 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001786 return_error_param = ret;
1787 return_error_line = __LINE__;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001788 goto err_translate_failed;
1789 }
1790 last_fixup_obj = bp;
1791 last_fixup_min_off = 0;
1792 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001793 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001794 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02001795 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001796 return_error = BR_FAILED_REPLY;
Todd Kjose598d172017-03-22 17:19:52 -07001797 return_error_param = -EINVAL;
1798 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001799 goto err_bad_object_type;
1800 }
1801 }
Todd Kjos8dedb0c2017-05-09 08:31:32 -07001802 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1803 list_add_tail(&tcomplete->entry, &thread->todo);
1804
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001805 if (reply) {
1806 BUG_ON(t->buffer->async_transaction != 0);
1807 binder_pop_transaction(target_thread, in_reply_to);
1808 } else if (!(t->flags & TF_ONE_WAY)) {
1809 BUG_ON(t->buffer->async_transaction != 0);
1810 t->need_reply = 1;
1811 t->from_parent = thread->transaction_stack;
1812 thread->transaction_stack = t;
1813 } else {
1814 BUG_ON(target_node == NULL);
1815 BUG_ON(t->buffer->async_transaction != 1);
1816 if (target_node->has_async_transaction) {
1817 target_list = &target_node->async_todo;
1818 target_wait = NULL;
1819 } else
1820 target_node->has_async_transaction = 1;
1821 }
1822 t->work.type = BINDER_WORK_TRANSACTION;
1823 list_add_tail(&t->work.entry, target_list);
Riley Andrewsb5968812015-09-01 12:42:07 -07001824 if (target_wait) {
Todd Kjos8dedb0c2017-05-09 08:31:32 -07001825 if (reply || !(tr->flags & TF_ONE_WAY))
Riley Andrewsb5968812015-09-01 12:42:07 -07001826 wake_up_interruptible_sync(target_wait);
1827 else
1828 wake_up_interruptible(target_wait);
1829 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001830 return;
1831
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001832err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001833err_bad_object_type:
1834err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001835err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001836err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001837 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001838 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1839 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07001840 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001841err_binder_alloc_buf_failed:
1842 kfree(tcomplete);
1843 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1844err_alloc_tcomplete_failed:
1845 kfree(t);
1846 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1847err_alloc_t_failed:
1848err_bad_call_stack:
1849err_empty_call_stack:
1850err_dead_binder:
1851err_invalid_target_handle:
1852err_no_context_mgr_node:
1853 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07001854 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
1855 proc->pid, thread->pid, return_error, return_error_param,
1856 (u64)tr->data_size, (u64)tr->offsets_size,
1857 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001858
1859 {
1860 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09001861
Todd Kjose598d172017-03-22 17:19:52 -07001862 e->return_error = return_error;
1863 e->return_error_param = return_error_param;
1864 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001865 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1866 *fe = *e;
1867 }
1868
1869 BUG_ON(thread->return_error != BR_OK);
1870 if (in_reply_to) {
1871 thread->return_error = BR_TRANSACTION_COMPLETE;
1872 binder_send_failed_reply(in_reply_to, return_error);
1873 } else
1874 thread->return_error = return_error;
1875}
1876
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02001877static int binder_thread_write(struct binder_proc *proc,
1878 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001879 binder_uintptr_t binder_buffer, size_t size,
1880 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001881{
1882 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001883 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001884 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001885 void __user *ptr = buffer + *consumed;
1886 void __user *end = buffer + size;
1887
1888 while (ptr < end && thread->return_error == BR_OK) {
1889 if (get_user(cmd, (uint32_t __user *)ptr))
1890 return -EFAULT;
1891 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001892 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001893 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07001894 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
1895 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
1896 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001897 }
1898 switch (cmd) {
1899 case BC_INCREFS:
1900 case BC_ACQUIRE:
1901 case BC_RELEASE:
1902 case BC_DECREFS: {
1903 uint32_t target;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001904 struct binder_ref *ref = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001905 const char *debug_string;
1906
1907 if (get_user(target, (uint32_t __user *)ptr))
1908 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001909
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001910 ptr += sizeof(uint32_t);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001911 if (target == 0 &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001912 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001913 struct binder_node *ctx_mgr_node;
1914
1915 mutex_lock(&context->context_mgr_node_lock);
1916 ctx_mgr_node = context->binder_context_mgr_node;
1917 if (ctx_mgr_node) {
1918 ref = binder_get_ref_for_node(proc,
1919 ctx_mgr_node);
1920 if (ref && ref->desc != target) {
1921 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
1922 proc->pid, thread->pid,
1923 ref->desc);
1924 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001925 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001926 mutex_unlock(&context->context_mgr_node_lock);
1927 }
1928 if (ref == NULL)
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001929 ref = binder_get_ref(proc, target,
1930 cmd == BC_ACQUIRE ||
1931 cmd == BC_RELEASE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001932 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301933 binder_user_error("%d:%d refcount change on invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001934 proc->pid, thread->pid, target);
1935 break;
1936 }
1937 switch (cmd) {
1938 case BC_INCREFS:
1939 debug_string = "IncRefs";
1940 binder_inc_ref(ref, 0, NULL);
1941 break;
1942 case BC_ACQUIRE:
1943 debug_string = "Acquire";
1944 binder_inc_ref(ref, 1, NULL);
1945 break;
1946 case BC_RELEASE:
1947 debug_string = "Release";
1948 binder_dec_ref(ref, 1);
1949 break;
1950 case BC_DECREFS:
1951 default:
1952 debug_string = "DecRefs";
1953 binder_dec_ref(ref, 0);
1954 break;
1955 }
1956 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301957 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001958 proc->pid, thread->pid, debug_string, ref->debug_id,
1959 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1960 break;
1961 }
1962 case BC_INCREFS_DONE:
1963 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001964 binder_uintptr_t node_ptr;
1965 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001966 struct binder_node *node;
1967
Arve Hjønnevågda498892014-02-21 14:40:26 -08001968 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001969 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001970 ptr += sizeof(binder_uintptr_t);
1971 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001972 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001973 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001974 node = binder_get_node(proc, node_ptr);
1975 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001976 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001977 proc->pid, thread->pid,
1978 cmd == BC_INCREFS_DONE ?
1979 "BC_INCREFS_DONE" :
1980 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001981 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001982 break;
1983 }
1984 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001985 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001986 proc->pid, thread->pid,
1987 cmd == BC_INCREFS_DONE ?
1988 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001989 (u64)node_ptr, node->debug_id,
1990 (u64)cookie, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001991 break;
1992 }
1993 if (cmd == BC_ACQUIRE_DONE) {
1994 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301995 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001996 proc->pid, thread->pid,
1997 node->debug_id);
1998 break;
1999 }
2000 node->pending_strong_ref = 0;
2001 } else {
2002 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302003 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002004 proc->pid, thread->pid,
2005 node->debug_id);
2006 break;
2007 }
2008 node->pending_weak_ref = 0;
2009 }
2010 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
2011 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302012 "%d:%d %s node %d ls %d lw %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002013 proc->pid, thread->pid,
2014 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
2015 node->debug_id, node->local_strong_refs, node->local_weak_refs);
2016 break;
2017 }
2018 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302019 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002020 return -EINVAL;
2021 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302022 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002023 return -EINVAL;
2024
2025 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002026 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002027 struct binder_buffer *buffer;
2028
Arve Hjønnevågda498892014-02-21 14:40:26 -08002029 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002030 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002031 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002032
Todd Kjosd325d372016-10-10 10:40:53 -07002033 buffer = binder_alloc_buffer_lookup(&proc->alloc,
2034 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002035 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002036 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
2037 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002038 break;
2039 }
2040 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002041 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
2042 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002043 break;
2044 }
2045 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002046 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
2047 proc->pid, thread->pid, (u64)data_ptr,
2048 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002049 buffer->transaction ? "active" : "finished");
2050
2051 if (buffer->transaction) {
2052 buffer->transaction->buffer = NULL;
2053 buffer->transaction = NULL;
2054 }
2055 if (buffer->async_transaction && buffer->target_node) {
2056 BUG_ON(!buffer->target_node->has_async_transaction);
2057 if (list_empty(&buffer->target_node->async_todo))
2058 buffer->target_node->has_async_transaction = 0;
2059 else
2060 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
2061 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002062 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002063 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07002064 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002065 break;
2066 }
2067
Martijn Coenen5a6da532016-09-30 14:10:07 +02002068 case BC_TRANSACTION_SG:
2069 case BC_REPLY_SG: {
2070 struct binder_transaction_data_sg tr;
2071
2072 if (copy_from_user(&tr, ptr, sizeof(tr)))
2073 return -EFAULT;
2074 ptr += sizeof(tr);
2075 binder_transaction(proc, thread, &tr.transaction_data,
2076 cmd == BC_REPLY_SG, tr.buffers_size);
2077 break;
2078 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002079 case BC_TRANSACTION:
2080 case BC_REPLY: {
2081 struct binder_transaction_data tr;
2082
2083 if (copy_from_user(&tr, ptr, sizeof(tr)))
2084 return -EFAULT;
2085 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02002086 binder_transaction(proc, thread, &tr,
2087 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002088 break;
2089 }
2090
2091 case BC_REGISTER_LOOPER:
2092 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302093 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002094 proc->pid, thread->pid);
2095 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
2096 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302097 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002098 proc->pid, thread->pid);
2099 } else if (proc->requested_threads == 0) {
2100 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302101 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002102 proc->pid, thread->pid);
2103 } else {
2104 proc->requested_threads--;
2105 proc->requested_threads_started++;
2106 }
2107 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
2108 break;
2109 case BC_ENTER_LOOPER:
2110 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302111 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002112 proc->pid, thread->pid);
2113 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
2114 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302115 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002116 proc->pid, thread->pid);
2117 }
2118 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
2119 break;
2120 case BC_EXIT_LOOPER:
2121 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302122 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002123 proc->pid, thread->pid);
2124 thread->looper |= BINDER_LOOPER_STATE_EXITED;
2125 break;
2126
2127 case BC_REQUEST_DEATH_NOTIFICATION:
2128 case BC_CLEAR_DEATH_NOTIFICATION: {
2129 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002130 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002131 struct binder_ref *ref;
2132 struct binder_ref_death *death;
2133
2134 if (get_user(target, (uint32_t __user *)ptr))
2135 return -EFAULT;
2136 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002137 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002138 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002139 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002140 ref = binder_get_ref(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002141 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302142 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002143 proc->pid, thread->pid,
2144 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2145 "BC_REQUEST_DEATH_NOTIFICATION" :
2146 "BC_CLEAR_DEATH_NOTIFICATION",
2147 target);
2148 break;
2149 }
2150
2151 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002152 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002153 proc->pid, thread->pid,
2154 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2155 "BC_REQUEST_DEATH_NOTIFICATION" :
2156 "BC_CLEAR_DEATH_NOTIFICATION",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002157 (u64)cookie, ref->debug_id, ref->desc,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002158 ref->strong, ref->weak, ref->node->debug_id);
2159
2160 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2161 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302162 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002163 proc->pid, thread->pid);
2164 break;
2165 }
2166 death = kzalloc(sizeof(*death), GFP_KERNEL);
2167 if (death == NULL) {
2168 thread->return_error = BR_ERROR;
2169 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302170 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002171 proc->pid, thread->pid);
2172 break;
2173 }
2174 binder_stats_created(BINDER_STAT_DEATH);
2175 INIT_LIST_HEAD(&death->work.entry);
2176 death->cookie = cookie;
2177 ref->death = death;
2178 if (ref->node->proc == NULL) {
2179 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2180 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2181 list_add_tail(&ref->death->work.entry, &thread->todo);
2182 } else {
2183 list_add_tail(&ref->death->work.entry, &proc->todo);
2184 wake_up_interruptible(&proc->wait);
2185 }
2186 }
2187 } else {
2188 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302189 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002190 proc->pid, thread->pid);
2191 break;
2192 }
2193 death = ref->death;
2194 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002195 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002196 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002197 (u64)death->cookie,
2198 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002199 break;
2200 }
2201 ref->death = NULL;
2202 if (list_empty(&death->work.entry)) {
2203 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2204 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2205 list_add_tail(&death->work.entry, &thread->todo);
2206 } else {
2207 list_add_tail(&death->work.entry, &proc->todo);
2208 wake_up_interruptible(&proc->wait);
2209 }
2210 } else {
2211 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2212 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2213 }
2214 }
2215 } break;
2216 case BC_DEAD_BINDER_DONE: {
2217 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002218 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002219 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09002220
Arve Hjønnevågda498892014-02-21 14:40:26 -08002221 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002222 return -EFAULT;
2223
Lisa Du7a64cd82016-02-17 09:32:52 +08002224 ptr += sizeof(cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002225 list_for_each_entry(w, &proc->delivered_death, entry) {
2226 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
Seunghun Lee10f62862014-05-01 01:30:23 +09002227
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002228 if (tmp_death->cookie == cookie) {
2229 death = tmp_death;
2230 break;
2231 }
2232 }
2233 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002234 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2235 proc->pid, thread->pid, (u64)cookie,
2236 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002237 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002238 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2239 proc->pid, thread->pid, (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002240 break;
2241 }
2242
2243 list_del_init(&death->work.entry);
2244 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2245 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2246 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2247 list_add_tail(&death->work.entry, &thread->todo);
2248 } else {
2249 list_add_tail(&death->work.entry, &proc->todo);
2250 wake_up_interruptible(&proc->wait);
2251 }
2252 }
2253 } break;
2254
2255 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302256 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002257 proc->pid, thread->pid, cmd);
2258 return -EINVAL;
2259 }
2260 *consumed = ptr - buffer;
2261 }
2262 return 0;
2263}
2264
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002265static void binder_stat_br(struct binder_proc *proc,
2266 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002267{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002268 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002269 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07002270 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
2271 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
2272 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002273 }
2274}
2275
2276static int binder_has_proc_work(struct binder_proc *proc,
2277 struct binder_thread *thread)
2278{
2279 return !list_empty(&proc->todo) ||
2280 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2281}
2282
2283static int binder_has_thread_work(struct binder_thread *thread)
2284{
2285 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2286 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2287}
2288
Todd Kjos60792612017-05-24 10:51:01 -07002289static int binder_put_node_cmd(struct binder_proc *proc,
2290 struct binder_thread *thread,
2291 void __user **ptrp,
2292 binder_uintptr_t node_ptr,
2293 binder_uintptr_t node_cookie,
2294 int node_debug_id,
2295 uint32_t cmd, const char *cmd_name)
2296{
2297 void __user *ptr = *ptrp;
2298
2299 if (put_user(cmd, (uint32_t __user *)ptr))
2300 return -EFAULT;
2301 ptr += sizeof(uint32_t);
2302
2303 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
2304 return -EFAULT;
2305 ptr += sizeof(binder_uintptr_t);
2306
2307 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
2308 return -EFAULT;
2309 ptr += sizeof(binder_uintptr_t);
2310
2311 binder_stat_br(proc, thread, cmd);
2312 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
2313 proc->pid, thread->pid, cmd_name, node_debug_id,
2314 (u64)node_ptr, (u64)node_cookie);
2315
2316 *ptrp = ptr;
2317 return 0;
2318}
2319
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002320static int binder_thread_read(struct binder_proc *proc,
2321 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002322 binder_uintptr_t binder_buffer, size_t size,
2323 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002324{
Arve Hjønnevågda498892014-02-21 14:40:26 -08002325 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002326 void __user *ptr = buffer + *consumed;
2327 void __user *end = buffer + size;
2328
2329 int ret = 0;
2330 int wait_for_proc_work;
2331
2332 if (*consumed == 0) {
2333 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2334 return -EFAULT;
2335 ptr += sizeof(uint32_t);
2336 }
2337
2338retry:
2339 wait_for_proc_work = thread->transaction_stack == NULL &&
2340 list_empty(&thread->todo);
2341
2342 if (thread->return_error != BR_OK && ptr < end) {
2343 if (thread->return_error2 != BR_OK) {
2344 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2345 return -EFAULT;
2346 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002347 binder_stat_br(proc, thread, thread->return_error2);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002348 if (ptr == end)
2349 goto done;
2350 thread->return_error2 = BR_OK;
2351 }
2352 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2353 return -EFAULT;
2354 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002355 binder_stat_br(proc, thread, thread->return_error);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002356 thread->return_error = BR_OK;
2357 goto done;
2358 }
2359
2360
2361 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2362 if (wait_for_proc_work)
2363 proc->ready_threads++;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002364
2365 binder_unlock(__func__);
2366
2367 trace_binder_wait_for_work(wait_for_proc_work,
2368 !!thread->transaction_stack,
2369 !list_empty(&thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002370 if (wait_for_proc_work) {
2371 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2372 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302373 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 +09002374 proc->pid, thread->pid, thread->looper);
2375 wait_event_interruptible(binder_user_error_wait,
2376 binder_stop_on_user_error < 2);
2377 }
2378 binder_set_nice(proc->default_priority);
2379 if (non_block) {
2380 if (!binder_has_proc_work(proc, thread))
2381 ret = -EAGAIN;
2382 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002383 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002384 } else {
2385 if (non_block) {
2386 if (!binder_has_thread_work(thread))
2387 ret = -EAGAIN;
2388 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002389 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002390 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002391
2392 binder_lock(__func__);
2393
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002394 if (wait_for_proc_work)
2395 proc->ready_threads--;
2396 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2397
2398 if (ret)
2399 return ret;
2400
2401 while (1) {
2402 uint32_t cmd;
2403 struct binder_transaction_data tr;
2404 struct binder_work *w;
2405 struct binder_transaction *t = NULL;
2406
Dmitry Voytik395262a2014-09-08 18:16:34 +04002407 if (!list_empty(&thread->todo)) {
2408 w = list_first_entry(&thread->todo, struct binder_work,
2409 entry);
2410 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2411 w = list_first_entry(&proc->todo, struct binder_work,
2412 entry);
2413 } else {
2414 /* no data added */
2415 if (ptr - buffer == 4 &&
2416 !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002417 goto retry;
2418 break;
2419 }
2420
2421 if (end - ptr < sizeof(tr) + 4)
2422 break;
2423
2424 switch (w->type) {
2425 case BINDER_WORK_TRANSACTION: {
2426 t = container_of(w, struct binder_transaction, work);
2427 } break;
2428 case BINDER_WORK_TRANSACTION_COMPLETE: {
2429 cmd = BR_TRANSACTION_COMPLETE;
2430 if (put_user(cmd, (uint32_t __user *)ptr))
2431 return -EFAULT;
2432 ptr += sizeof(uint32_t);
2433
2434 binder_stat_br(proc, thread, cmd);
2435 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302436 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002437 proc->pid, thread->pid);
2438
2439 list_del(&w->entry);
2440 kfree(w);
2441 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2442 } break;
2443 case BINDER_WORK_NODE: {
2444 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos60792612017-05-24 10:51:01 -07002445 int strong, weak;
2446 binder_uintptr_t node_ptr = node->ptr;
2447 binder_uintptr_t node_cookie = node->cookie;
2448 int node_debug_id = node->debug_id;
2449 int has_weak_ref;
2450 int has_strong_ref;
2451 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09002452
Todd Kjos60792612017-05-24 10:51:01 -07002453 BUG_ON(proc != node->proc);
2454 strong = node->internal_strong_refs ||
2455 node->local_strong_refs;
2456 weak = !hlist_empty(&node->refs) ||
2457 node->local_weak_refs || strong;
2458 has_strong_ref = node->has_strong_ref;
2459 has_weak_ref = node->has_weak_ref;
2460
2461 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002462 node->has_weak_ref = 1;
2463 node->pending_weak_ref = 1;
2464 node->local_weak_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07002465 }
2466 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002467 node->has_strong_ref = 1;
2468 node->pending_strong_ref = 1;
2469 node->local_strong_refs++;
Todd Kjos60792612017-05-24 10:51:01 -07002470 }
2471 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002472 node->has_strong_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07002473 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002474 node->has_weak_ref = 0;
Todd Kjos60792612017-05-24 10:51:01 -07002475 list_del(&w->entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002476
Todd Kjos60792612017-05-24 10:51:01 -07002477 if (!weak && !strong) {
2478 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2479 "%d:%d node %d u%016llx c%016llx deleted\n",
2480 proc->pid, thread->pid,
2481 node_debug_id,
2482 (u64)node_ptr,
2483 (u64)node_cookie);
2484 rb_erase(&node->rb_node, &proc->nodes);
2485 kfree(node);
2486 binder_stats_deleted(BINDER_STAT_NODE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002487 }
Todd Kjos60792612017-05-24 10:51:01 -07002488 if (weak && !has_weak_ref)
2489 ret = binder_put_node_cmd(
2490 proc, thread, &ptr, node_ptr,
2491 node_cookie, node_debug_id,
2492 BR_INCREFS, "BR_INCREFS");
2493 if (!ret && strong && !has_strong_ref)
2494 ret = binder_put_node_cmd(
2495 proc, thread, &ptr, node_ptr,
2496 node_cookie, node_debug_id,
2497 BR_ACQUIRE, "BR_ACQUIRE");
2498 if (!ret && !strong && has_strong_ref)
2499 ret = binder_put_node_cmd(
2500 proc, thread, &ptr, node_ptr,
2501 node_cookie, node_debug_id,
2502 BR_RELEASE, "BR_RELEASE");
2503 if (!ret && !weak && has_weak_ref)
2504 ret = binder_put_node_cmd(
2505 proc, thread, &ptr, node_ptr,
2506 node_cookie, node_debug_id,
2507 BR_DECREFS, "BR_DECREFS");
2508 if (orig_ptr == ptr)
2509 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2510 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2511 proc->pid, thread->pid,
2512 node_debug_id,
2513 (u64)node_ptr,
2514 (u64)node_cookie);
2515 if (ret)
2516 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002517 } break;
2518 case BINDER_WORK_DEAD_BINDER:
2519 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2520 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2521 struct binder_ref_death *death;
2522 uint32_t cmd;
2523
2524 death = container_of(w, struct binder_ref_death, work);
2525 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2526 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2527 else
2528 cmd = BR_DEAD_BINDER;
2529 if (put_user(cmd, (uint32_t __user *)ptr))
2530 return -EFAULT;
2531 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002532 if (put_user(death->cookie,
2533 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002534 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002535 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002536 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002537 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002538 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002539 proc->pid, thread->pid,
2540 cmd == BR_DEAD_BINDER ?
2541 "BR_DEAD_BINDER" :
2542 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002543 (u64)death->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002544
2545 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2546 list_del(&w->entry);
2547 kfree(death);
2548 binder_stats_deleted(BINDER_STAT_DEATH);
2549 } else
2550 list_move(&w->entry, &proc->delivered_death);
2551 if (cmd == BR_DEAD_BINDER)
2552 goto done; /* DEAD_BINDER notifications can cause transactions */
2553 } break;
2554 }
2555
2556 if (!t)
2557 continue;
2558
2559 BUG_ON(t->buffer == NULL);
2560 if (t->buffer->target_node) {
2561 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002562
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002563 tr.target.ptr = target_node->ptr;
2564 tr.cookie = target_node->cookie;
2565 t->saved_priority = task_nice(current);
2566 if (t->priority < target_node->min_priority &&
2567 !(t->flags & TF_ONE_WAY))
2568 binder_set_nice(t->priority);
2569 else if (!(t->flags & TF_ONE_WAY) ||
2570 t->saved_priority > target_node->min_priority)
2571 binder_set_nice(target_node->min_priority);
2572 cmd = BR_TRANSACTION;
2573 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002574 tr.target.ptr = 0;
2575 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002576 cmd = BR_REPLY;
2577 }
2578 tr.code = t->code;
2579 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002580 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002581
2582 if (t->from) {
2583 struct task_struct *sender = t->from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09002584
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002585 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002586 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002587 } else {
2588 tr.sender_pid = 0;
2589 }
2590
2591 tr.data_size = t->buffer->data_size;
2592 tr.offsets_size = t->buffer->offsets_size;
Todd Kjosd325d372016-10-10 10:40:53 -07002593 tr.data.ptr.buffer = (binder_uintptr_t)
2594 ((uintptr_t)t->buffer->data +
2595 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002596 tr.data.ptr.offsets = tr.data.ptr.buffer +
2597 ALIGN(t->buffer->data_size,
2598 sizeof(void *));
2599
2600 if (put_user(cmd, (uint32_t __user *)ptr))
2601 return -EFAULT;
2602 ptr += sizeof(uint32_t);
2603 if (copy_to_user(ptr, &tr, sizeof(tr)))
2604 return -EFAULT;
2605 ptr += sizeof(tr);
2606
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002607 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002608 binder_stat_br(proc, thread, cmd);
2609 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002610 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002611 proc->pid, thread->pid,
2612 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2613 "BR_REPLY",
2614 t->debug_id, t->from ? t->from->proc->pid : 0,
2615 t->from ? t->from->pid : 0, cmd,
2616 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002617 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002618
2619 list_del(&t->work.entry);
2620 t->buffer->allow_user_free = 1;
2621 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2622 t->to_parent = thread->transaction_stack;
2623 t->to_thread = thread;
2624 thread->transaction_stack = t;
2625 } else {
2626 t->buffer->transaction = NULL;
2627 kfree(t);
2628 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2629 }
2630 break;
2631 }
2632
2633done:
2634
2635 *consumed = ptr - buffer;
2636 if (proc->requested_threads + proc->ready_threads == 0 &&
2637 proc->requested_threads_started < proc->max_threads &&
2638 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2639 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2640 /*spawn a new thread if we leave this out */) {
2641 proc->requested_threads++;
2642 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302643 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002644 proc->pid, thread->pid);
2645 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2646 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002647 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002648 }
2649 return 0;
2650}
2651
2652static void binder_release_work(struct list_head *list)
2653{
2654 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09002655
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002656 while (!list_empty(list)) {
2657 w = list_first_entry(list, struct binder_work, entry);
2658 list_del_init(&w->entry);
2659 switch (w->type) {
2660 case BINDER_WORK_TRANSACTION: {
2661 struct binder_transaction *t;
2662
2663 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002664 if (t->buffer->target_node &&
2665 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002666 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002667 } else {
2668 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302669 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002670 t->debug_id);
2671 t->buffer->transaction = NULL;
2672 kfree(t);
2673 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2674 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002675 } break;
2676 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002677 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302678 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002679 kfree(w);
2680 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2681 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002682 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2683 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2684 struct binder_ref_death *death;
2685
2686 death = container_of(w, struct binder_ref_death, work);
2687 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002688 "undelivered death notification, %016llx\n",
2689 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002690 kfree(death);
2691 binder_stats_deleted(BINDER_STAT_DEATH);
2692 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002693 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302694 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002695 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002696 break;
2697 }
2698 }
2699
2700}
2701
2702static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2703{
2704 struct binder_thread *thread = NULL;
2705 struct rb_node *parent = NULL;
2706 struct rb_node **p = &proc->threads.rb_node;
2707
2708 while (*p) {
2709 parent = *p;
2710 thread = rb_entry(parent, struct binder_thread, rb_node);
2711
2712 if (current->pid < thread->pid)
2713 p = &(*p)->rb_left;
2714 else if (current->pid > thread->pid)
2715 p = &(*p)->rb_right;
2716 else
2717 break;
2718 }
2719 if (*p == NULL) {
2720 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2721 if (thread == NULL)
2722 return NULL;
2723 binder_stats_created(BINDER_STAT_THREAD);
2724 thread->proc = proc;
2725 thread->pid = current->pid;
2726 init_waitqueue_head(&thread->wait);
2727 INIT_LIST_HEAD(&thread->todo);
2728 rb_link_node(&thread->rb_node, parent, p);
2729 rb_insert_color(&thread->rb_node, &proc->threads);
2730 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2731 thread->return_error = BR_OK;
2732 thread->return_error2 = BR_OK;
2733 }
2734 return thread;
2735}
2736
2737static int binder_free_thread(struct binder_proc *proc,
2738 struct binder_thread *thread)
2739{
2740 struct binder_transaction *t;
2741 struct binder_transaction *send_reply = NULL;
2742 int active_transactions = 0;
2743
2744 rb_erase(&thread->rb_node, &proc->threads);
2745 t = thread->transaction_stack;
2746 if (t && t->to_thread == thread)
2747 send_reply = t;
2748 while (t) {
2749 active_transactions++;
2750 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302751 "release %d:%d transaction %d %s, still active\n",
2752 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002753 t->debug_id,
2754 (t->to_thread == thread) ? "in" : "out");
2755
2756 if (t->to_thread == thread) {
2757 t->to_proc = NULL;
2758 t->to_thread = NULL;
2759 if (t->buffer) {
2760 t->buffer->transaction = NULL;
2761 t->buffer = NULL;
2762 }
2763 t = t->to_parent;
2764 } else if (t->from == thread) {
2765 t->from = NULL;
2766 t = t->from_parent;
2767 } else
2768 BUG();
2769 }
2770 if (send_reply)
2771 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2772 binder_release_work(&thread->todo);
2773 kfree(thread);
2774 binder_stats_deleted(BINDER_STAT_THREAD);
2775 return active_transactions;
2776}
2777
2778static unsigned int binder_poll(struct file *filp,
2779 struct poll_table_struct *wait)
2780{
2781 struct binder_proc *proc = filp->private_data;
2782 struct binder_thread *thread = NULL;
2783 int wait_for_proc_work;
2784
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002785 binder_lock(__func__);
2786
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002787 thread = binder_get_thread(proc);
2788
2789 wait_for_proc_work = thread->transaction_stack == NULL &&
2790 list_empty(&thread->todo) && thread->return_error == BR_OK;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002791
2792 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002793
2794 if (wait_for_proc_work) {
2795 if (binder_has_proc_work(proc, thread))
2796 return POLLIN;
2797 poll_wait(filp, &proc->wait, wait);
2798 if (binder_has_proc_work(proc, thread))
2799 return POLLIN;
2800 } else {
2801 if (binder_has_thread_work(thread))
2802 return POLLIN;
2803 poll_wait(filp, &thread->wait, wait);
2804 if (binder_has_thread_work(thread))
2805 return POLLIN;
2806 }
2807 return 0;
2808}
2809
Tair Rzayev78260ac2014-06-03 22:27:21 +03002810static int binder_ioctl_write_read(struct file *filp,
2811 unsigned int cmd, unsigned long arg,
2812 struct binder_thread *thread)
2813{
2814 int ret = 0;
2815 struct binder_proc *proc = filp->private_data;
2816 unsigned int size = _IOC_SIZE(cmd);
2817 void __user *ubuf = (void __user *)arg;
2818 struct binder_write_read bwr;
2819
2820 if (size != sizeof(struct binder_write_read)) {
2821 ret = -EINVAL;
2822 goto out;
2823 }
2824 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2825 ret = -EFAULT;
2826 goto out;
2827 }
2828 binder_debug(BINDER_DEBUG_READ_WRITE,
2829 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2830 proc->pid, thread->pid,
2831 (u64)bwr.write_size, (u64)bwr.write_buffer,
2832 (u64)bwr.read_size, (u64)bwr.read_buffer);
2833
2834 if (bwr.write_size > 0) {
2835 ret = binder_thread_write(proc, thread,
2836 bwr.write_buffer,
2837 bwr.write_size,
2838 &bwr.write_consumed);
2839 trace_binder_write_done(ret);
2840 if (ret < 0) {
2841 bwr.read_consumed = 0;
2842 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2843 ret = -EFAULT;
2844 goto out;
2845 }
2846 }
2847 if (bwr.read_size > 0) {
2848 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2849 bwr.read_size,
2850 &bwr.read_consumed,
2851 filp->f_flags & O_NONBLOCK);
2852 trace_binder_read_done(ret);
2853 if (!list_empty(&proc->todo))
2854 wake_up_interruptible(&proc->wait);
2855 if (ret < 0) {
2856 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2857 ret = -EFAULT;
2858 goto out;
2859 }
2860 }
2861 binder_debug(BINDER_DEBUG_READ_WRITE,
2862 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2863 proc->pid, thread->pid,
2864 (u64)bwr.write_consumed, (u64)bwr.write_size,
2865 (u64)bwr.read_consumed, (u64)bwr.read_size);
2866 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2867 ret = -EFAULT;
2868 goto out;
2869 }
2870out:
2871 return ret;
2872}
2873
2874static int binder_ioctl_set_ctx_mgr(struct file *filp)
2875{
2876 int ret = 0;
2877 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002878 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002879 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002880 kuid_t curr_euid = current_euid();
2881
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002882 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002883 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002884 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2885 ret = -EBUSY;
2886 goto out;
2887 }
Stephen Smalley79af7302015-01-21 10:54:10 -05002888 ret = security_binder_set_context_mgr(proc->tsk);
2889 if (ret < 0)
2890 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002891 if (uid_valid(context->binder_context_mgr_uid)) {
2892 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002893 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2894 from_kuid(&init_user_ns, curr_euid),
2895 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002896 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03002897 ret = -EPERM;
2898 goto out;
2899 }
2900 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002901 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002902 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002903 new_node = binder_new_node(proc, 0, 0);
2904 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002905 ret = -ENOMEM;
2906 goto out;
2907 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002908 new_node->local_weak_refs++;
2909 new_node->local_strong_refs++;
2910 new_node->has_strong_ref = 1;
2911 new_node->has_weak_ref = 1;
2912 context->binder_context_mgr_node = new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002913out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002914 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03002915 return ret;
2916}
2917
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002918static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2919{
2920 int ret;
2921 struct binder_proc *proc = filp->private_data;
2922 struct binder_thread *thread;
2923 unsigned int size = _IOC_SIZE(cmd);
2924 void __user *ubuf = (void __user *)arg;
2925
Tair Rzayev78260ac2014-06-03 22:27:21 +03002926 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2927 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002928
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002929 trace_binder_ioctl(cmd, arg);
2930
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002931 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2932 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002933 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002934
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002935 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002936 thread = binder_get_thread(proc);
2937 if (thread == NULL) {
2938 ret = -ENOMEM;
2939 goto err;
2940 }
2941
2942 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002943 case BINDER_WRITE_READ:
2944 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
2945 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002946 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002947 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002948 case BINDER_SET_MAX_THREADS:
2949 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2950 ret = -EINVAL;
2951 goto err;
2952 }
2953 break;
2954 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03002955 ret = binder_ioctl_set_ctx_mgr(filp);
2956 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002957 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002958 break;
2959 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302960 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002961 proc->pid, thread->pid);
2962 binder_free_thread(proc, thread);
2963 thread = NULL;
2964 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002965 case BINDER_VERSION: {
2966 struct binder_version __user *ver = ubuf;
2967
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002968 if (size != sizeof(struct binder_version)) {
2969 ret = -EINVAL;
2970 goto err;
2971 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02002972 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2973 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002974 ret = -EINVAL;
2975 goto err;
2976 }
2977 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002978 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002979 default:
2980 ret = -EINVAL;
2981 goto err;
2982 }
2983 ret = 0;
2984err:
2985 if (thread)
2986 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002987 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002988 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2989 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05302990 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 -07002991err_unlocked:
2992 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002993 return ret;
2994}
2995
2996static void binder_vma_open(struct vm_area_struct *vma)
2997{
2998 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002999
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003000 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303001 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003002 proc->pid, vma->vm_start, vma->vm_end,
3003 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3004 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003005}
3006
3007static void binder_vma_close(struct vm_area_struct *vma)
3008{
3009 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003010
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003011 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303012 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003013 proc->pid, vma->vm_start, vma->vm_end,
3014 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3015 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07003016 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003017 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
3018}
3019
Vinayak Menonddac7d52014-06-02 18:17:59 +05303020static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3021{
3022 return VM_FAULT_SIGBUS;
3023}
3024
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07003025static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003026 .open = binder_vma_open,
3027 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05303028 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003029};
3030
Todd Kjosd325d372016-10-10 10:40:53 -07003031static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
3032{
3033 int ret;
3034 struct binder_proc *proc = filp->private_data;
3035 const char *failure_string;
3036
3037 if (proc->tsk != current->group_leader)
3038 return -EINVAL;
3039
3040 if ((vma->vm_end - vma->vm_start) > SZ_4M)
3041 vma->vm_end = vma->vm_start + SZ_4M;
3042
3043 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3044 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
3045 __func__, proc->pid, vma->vm_start, vma->vm_end,
3046 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3047 (unsigned long)pgprot_val(vma->vm_page_prot));
3048
3049 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
3050 ret = -EPERM;
3051 failure_string = "bad vm_flags";
3052 goto err_bad_arg;
3053 }
3054 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
3055 vma->vm_ops = &binder_vm_ops;
3056 vma->vm_private_data = proc;
3057
3058 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
3059 if (ret)
3060 return ret;
3061 proc->files = get_files_struct(current);
3062 return 0;
3063
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003064err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04003065 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003066 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
3067 return ret;
3068}
3069
3070static int binder_open(struct inode *nodp, struct file *filp)
3071{
3072 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003073 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003074
3075 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
3076 current->group_leader->pid, current->pid);
3077
3078 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
3079 if (proc == NULL)
3080 return -ENOMEM;
Martijn Coenen872c26e2017-03-07 15:51:18 +01003081 get_task_struct(current->group_leader);
3082 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003083 INIT_LIST_HEAD(&proc->todo);
3084 init_waitqueue_head(&proc->wait);
3085 proc->default_priority = task_nice(current);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003086 binder_dev = container_of(filp->private_data, struct binder_device,
3087 miscdev);
3088 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07003089 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003090
3091 binder_lock(__func__);
3092
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003093 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003094 proc->pid = current->group_leader->pid;
3095 INIT_LIST_HEAD(&proc->delivered_death);
3096 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003097
3098 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003099
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003100 mutex_lock(&binder_procs_lock);
3101 hlist_add_head(&proc->proc_node, &binder_procs);
3102 mutex_unlock(&binder_procs_lock);
3103
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003104 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003105 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09003106
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003107 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003108 /*
3109 * proc debug entries are shared between contexts, so
3110 * this will fail if the process tries to open the driver
3111 * again with a different context. The priting code will
3112 * anyway print all contexts that a given PID has, so this
3113 * is not a problem.
3114 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003115 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003116 binder_debugfs_dir_entry_proc,
3117 (void *)(unsigned long)proc->pid,
3118 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003119 }
3120
3121 return 0;
3122}
3123
3124static int binder_flush(struct file *filp, fl_owner_t id)
3125{
3126 struct binder_proc *proc = filp->private_data;
3127
3128 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3129
3130 return 0;
3131}
3132
3133static void binder_deferred_flush(struct binder_proc *proc)
3134{
3135 struct rb_node *n;
3136 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09003137
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003138 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3139 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09003140
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003141 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3142 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3143 wake_up_interruptible(&thread->wait);
3144 wake_count++;
3145 }
3146 }
3147 wake_up_interruptible_all(&proc->wait);
3148
3149 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3150 "binder_flush: %d woke %d threads\n", proc->pid,
3151 wake_count);
3152}
3153
3154static int binder_release(struct inode *nodp, struct file *filp)
3155{
3156 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003157
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003158 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003159 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3160
3161 return 0;
3162}
3163
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003164static int binder_node_release(struct binder_node *node, int refs)
3165{
3166 struct binder_ref *ref;
3167 int death = 0;
3168
3169 list_del_init(&node->work.entry);
3170 binder_release_work(&node->async_todo);
3171
3172 if (hlist_empty(&node->refs)) {
3173 kfree(node);
3174 binder_stats_deleted(BINDER_STAT_NODE);
3175
3176 return refs;
3177 }
3178
3179 node->proc = NULL;
3180 node->local_strong_refs = 0;
3181 node->local_weak_refs = 0;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003182
3183 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003184 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003185 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003186
3187 hlist_for_each_entry(ref, &node->refs, node_entry) {
3188 refs++;
3189
3190 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08003191 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003192
3193 death++;
3194
3195 if (list_empty(&ref->death->work.entry)) {
3196 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3197 list_add_tail(&ref->death->work.entry,
3198 &ref->proc->todo);
3199 wake_up_interruptible(&ref->proc->wait);
3200 } else
3201 BUG();
3202 }
3203
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003204 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3205 "node %d now dead, refs %d, death %d\n",
3206 node->debug_id, refs, death);
3207
3208 return refs;
3209}
3210
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003211static void binder_deferred_release(struct binder_proc *proc)
3212{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003213 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003214 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07003215 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003216
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003217 BUG_ON(proc->files);
3218
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003219 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003220 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003221 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003222
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003223 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003224 if (context->binder_context_mgr_node &&
3225 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003226 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003227 "%s: %d context_mgr_node gone\n",
3228 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003229 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003230 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003231 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003232
3233 threads = 0;
3234 active_transactions = 0;
3235 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003236 struct binder_thread *thread;
3237
3238 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003239 threads++;
3240 active_transactions += binder_free_thread(proc, thread);
3241 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003242
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003243 nodes = 0;
3244 incoming_refs = 0;
3245 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003246 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003247
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003248 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003249 nodes++;
3250 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003251 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003252 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003253
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003254 outgoing_refs = 0;
3255 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003256 struct binder_ref *ref;
3257
3258 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003259 outgoing_refs++;
3260 binder_delete_ref(ref);
3261 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003262
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003263 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003264 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003265
Todd Kjosd325d372016-10-10 10:40:53 -07003266 binder_alloc_deferred_release(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003267 binder_stats_deleted(BINDER_STAT_PROC);
3268
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003269 put_task_struct(proc->tsk);
3270
3271 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07003272 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003273 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07003274 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003275
3276 kfree(proc);
3277}
3278
3279static void binder_deferred_func(struct work_struct *work)
3280{
3281 struct binder_proc *proc;
3282 struct files_struct *files;
3283
3284 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003285
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003286 do {
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003287 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003288 mutex_lock(&binder_deferred_lock);
3289 if (!hlist_empty(&binder_deferred_list)) {
3290 proc = hlist_entry(binder_deferred_list.first,
3291 struct binder_proc, deferred_work_node);
3292 hlist_del_init(&proc->deferred_work_node);
3293 defer = proc->deferred_work;
3294 proc->deferred_work = 0;
3295 } else {
3296 proc = NULL;
3297 defer = 0;
3298 }
3299 mutex_unlock(&binder_deferred_lock);
3300
3301 files = NULL;
3302 if (defer & BINDER_DEFERRED_PUT_FILES) {
3303 files = proc->files;
3304 if (files)
3305 proc->files = NULL;
3306 }
3307
3308 if (defer & BINDER_DEFERRED_FLUSH)
3309 binder_deferred_flush(proc);
3310
3311 if (defer & BINDER_DEFERRED_RELEASE)
3312 binder_deferred_release(proc); /* frees proc */
3313
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003314 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003315 if (files)
3316 put_files_struct(files);
3317 } while (proc);
3318}
3319static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3320
3321static void
3322binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3323{
3324 mutex_lock(&binder_deferred_lock);
3325 proc->deferred_work |= defer;
3326 if (hlist_unhashed(&proc->deferred_work_node)) {
3327 hlist_add_head(&proc->deferred_work_node,
3328 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05303329 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003330 }
3331 mutex_unlock(&binder_deferred_lock);
3332}
3333
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003334static void print_binder_transaction(struct seq_file *m, const char *prefix,
3335 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003336{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003337 seq_printf(m,
3338 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3339 prefix, t->debug_id, t,
3340 t->from ? t->from->proc->pid : 0,
3341 t->from ? t->from->pid : 0,
3342 t->to_proc ? t->to_proc->pid : 0,
3343 t->to_thread ? t->to_thread->pid : 0,
3344 t->code, t->flags, t->priority, t->need_reply);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003345 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003346 seq_puts(m, " buffer free\n");
3347 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003348 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003349 if (t->buffer->target_node)
3350 seq_printf(m, " node %d",
3351 t->buffer->target_node->debug_id);
3352 seq_printf(m, " size %zd:%zd data %p\n",
3353 t->buffer->data_size, t->buffer->offsets_size,
3354 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003355}
3356
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003357static void print_binder_work(struct seq_file *m, const char *prefix,
3358 const char *transaction_prefix,
3359 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003360{
3361 struct binder_node *node;
3362 struct binder_transaction *t;
3363
3364 switch (w->type) {
3365 case BINDER_WORK_TRANSACTION:
3366 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003367 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003368 break;
3369 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003370 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003371 break;
3372 case BINDER_WORK_NODE:
3373 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003374 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3375 prefix, node->debug_id,
3376 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003377 break;
3378 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003379 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003380 break;
3381 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003382 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003383 break;
3384 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003385 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003386 break;
3387 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003388 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003389 break;
3390 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003391}
3392
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003393static void print_binder_thread(struct seq_file *m,
3394 struct binder_thread *thread,
3395 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396{
3397 struct binder_transaction *t;
3398 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003399 size_t start_pos = m->count;
3400 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003402 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3403 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003404 t = thread->transaction_stack;
3405 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003406 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003407 print_binder_transaction(m,
3408 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003409 t = t->from_parent;
3410 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003411 print_binder_transaction(m,
3412 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003413 t = t->to_parent;
3414 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003415 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003416 t = NULL;
3417 }
3418 }
3419 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003420 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003421 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003422 if (!print_always && m->count == header_pos)
3423 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003424}
3425
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003426static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003427{
3428 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003429 struct binder_work *w;
3430 int count;
3431
3432 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003433 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003434 count++;
3435
Arve Hjønnevågda498892014-02-21 14:40:26 -08003436 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3437 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003438 node->has_strong_ref, node->has_weak_ref,
3439 node->local_strong_refs, node->local_weak_refs,
3440 node->internal_strong_refs, count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003441 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003442 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003443 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003444 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003445 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003446 seq_puts(m, "\n");
3447 list_for_each_entry(w, &node->async_todo, entry)
3448 print_binder_work(m, " ",
3449 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003450}
3451
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003452static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003453{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003454 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3455 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3456 ref->node->debug_id, ref->strong, ref->weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003457}
3458
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003459static void print_binder_proc(struct seq_file *m,
3460 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003461{
3462 struct binder_work *w;
3463 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003464 size_t start_pos = m->count;
3465 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003466
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003467 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003468 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003469 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003470
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003471 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3472 print_binder_thread(m, rb_entry(n, struct binder_thread,
3473 rb_node), print_all);
3474 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003475 struct binder_node *node = rb_entry(n, struct binder_node,
3476 rb_node);
3477 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003478 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003479 }
3480 if (print_all) {
3481 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003482 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003483 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003484 print_binder_ref(m, rb_entry(n, struct binder_ref,
3485 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003486 }
Todd Kjosd325d372016-10-10 10:40:53 -07003487 binder_alloc_print_allocated(m, &proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003488 list_for_each_entry(w, &proc->todo, entry)
3489 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003490 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003491 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003492 break;
3493 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003494 if (!print_all && m->count == header_pos)
3495 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003496}
3497
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003498static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003499 "BR_ERROR",
3500 "BR_OK",
3501 "BR_TRANSACTION",
3502 "BR_REPLY",
3503 "BR_ACQUIRE_RESULT",
3504 "BR_DEAD_REPLY",
3505 "BR_TRANSACTION_COMPLETE",
3506 "BR_INCREFS",
3507 "BR_ACQUIRE",
3508 "BR_RELEASE",
3509 "BR_DECREFS",
3510 "BR_ATTEMPT_ACQUIRE",
3511 "BR_NOOP",
3512 "BR_SPAWN_LOOPER",
3513 "BR_FINISHED",
3514 "BR_DEAD_BINDER",
3515 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3516 "BR_FAILED_REPLY"
3517};
3518
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003519static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003520 "BC_TRANSACTION",
3521 "BC_REPLY",
3522 "BC_ACQUIRE_RESULT",
3523 "BC_FREE_BUFFER",
3524 "BC_INCREFS",
3525 "BC_ACQUIRE",
3526 "BC_RELEASE",
3527 "BC_DECREFS",
3528 "BC_INCREFS_DONE",
3529 "BC_ACQUIRE_DONE",
3530 "BC_ATTEMPT_ACQUIRE",
3531 "BC_REGISTER_LOOPER",
3532 "BC_ENTER_LOOPER",
3533 "BC_EXIT_LOOPER",
3534 "BC_REQUEST_DEATH_NOTIFICATION",
3535 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02003536 "BC_DEAD_BINDER_DONE",
3537 "BC_TRANSACTION_SG",
3538 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003539};
3540
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003541static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003542 "proc",
3543 "thread",
3544 "node",
3545 "ref",
3546 "death",
3547 "transaction",
3548 "transaction_complete"
3549};
3550
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003551static void print_binder_stats(struct seq_file *m, const char *prefix,
3552 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003553{
3554 int i;
3555
3556 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003557 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003558 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003559 int temp = atomic_read(&stats->bc[i]);
3560
3561 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003562 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003563 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003564 }
3565
3566 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003567 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003568 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003569 int temp = atomic_read(&stats->br[i]);
3570
3571 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003572 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003573 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003574 }
3575
3576 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003577 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003578 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003579 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003580 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003581 int created = atomic_read(&stats->obj_created[i]);
3582 int deleted = atomic_read(&stats->obj_deleted[i]);
3583
3584 if (created || deleted)
3585 seq_printf(m, "%s%s: active %d total %d\n",
3586 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003587 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003588 created - deleted,
3589 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003590 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003591}
3592
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003593static void print_binder_proc_stats(struct seq_file *m,
3594 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003595{
3596 struct binder_work *w;
3597 struct rb_node *n;
3598 int count, strong, weak;
3599
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003600 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003601 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003602 count = 0;
3603 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3604 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003605 seq_printf(m, " threads: %d\n", count);
3606 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003607 " ready threads %d\n"
3608 " free async space %zd\n", proc->requested_threads,
3609 proc->requested_threads_started, proc->max_threads,
Todd Kjosd325d372016-10-10 10:40:53 -07003610 proc->ready_threads,
3611 binder_alloc_get_free_async_space(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003612 count = 0;
3613 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3614 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003615 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003616 count = 0;
3617 strong = 0;
3618 weak = 0;
3619 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3620 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3621 rb_node_desc);
3622 count++;
3623 strong += ref->strong;
3624 weak += ref->weak;
3625 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003626 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003627
Todd Kjosd325d372016-10-10 10:40:53 -07003628 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003629 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003630
3631 count = 0;
3632 list_for_each_entry(w, &proc->todo, entry) {
3633 switch (w->type) {
3634 case BINDER_WORK_TRANSACTION:
3635 count++;
3636 break;
3637 default:
3638 break;
3639 }
3640 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003641 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003642
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003643 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003644}
3645
3646
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003647static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003648{
3649 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003650 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003651
Todd Kjos48b33212017-05-24 11:53:13 -07003652 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003653
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003654 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003655
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003656 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003657 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003658 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003659 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003660 print_binder_node(m, node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003661 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003662
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003663 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003664 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003665 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003666 mutex_unlock(&binder_procs_lock);
Todd Kjos48b33212017-05-24 11:53:13 -07003667 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003668 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003669}
3670
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003671static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003672{
3673 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003674
Todd Kjos48b33212017-05-24 11:53:13 -07003675 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003676
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003677 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003678
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003679 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003680
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003681 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003682 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003683 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003684 mutex_unlock(&binder_procs_lock);
Todd Kjos48b33212017-05-24 11:53:13 -07003685 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003686 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003687}
3688
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003689static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003690{
3691 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003692
Todd Kjos48b33212017-05-24 11:53:13 -07003693 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003694
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003695 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003696 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003697 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003698 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003699 mutex_unlock(&binder_procs_lock);
Todd Kjos48b33212017-05-24 11:53:13 -07003700 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003701 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003702}
3703
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003704static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003705{
Riley Andrews83050a42016-02-09 21:05:33 -08003706 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003707 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003708
Todd Kjos48b33212017-05-24 11:53:13 -07003709 binder_lock(__func__);
Riley Andrews83050a42016-02-09 21:05:33 -08003710
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003711 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08003712 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003713 if (itr->pid == pid) {
3714 seq_puts(m, "binder proc state:\n");
3715 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08003716 }
3717 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003718 mutex_unlock(&binder_procs_lock);
3719
Todd Kjos48b33212017-05-24 11:53:13 -07003720 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003721 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003722}
3723
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003724static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003725 struct binder_transaction_log_entry *e)
3726{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003727 seq_printf(m,
Todd Kjose598d172017-03-22 17:19:52 -07003728 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d ret %d/%d l=%d\n",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003729 e->debug_id, (e->call_type == 2) ? "reply" :
3730 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003731 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07003732 e->to_node, e->target_handle, e->data_size, e->offsets_size,
3733 e->return_error, e->return_error_param,
3734 e->return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003735}
3736
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003737static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003738{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003739 struct binder_transaction_log *log = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003740 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003741
3742 if (log->full) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003743 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3744 print_binder_transaction_log_entry(m, &log->entry[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003745 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003746 for (i = 0; i < log->next; i++)
3747 print_binder_transaction_log_entry(m, &log->entry[i]);
3748 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003749}
3750
3751static const struct file_operations binder_fops = {
3752 .owner = THIS_MODULE,
3753 .poll = binder_poll,
3754 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003755 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003756 .mmap = binder_mmap,
3757 .open = binder_open,
3758 .flush = binder_flush,
3759 .release = binder_release,
3760};
3761
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003762BINDER_DEBUG_ENTRY(state);
3763BINDER_DEBUG_ENTRY(stats);
3764BINDER_DEBUG_ENTRY(transactions);
3765BINDER_DEBUG_ENTRY(transaction_log);
3766
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003767static int __init init_binder_device(const char *name)
3768{
3769 int ret;
3770 struct binder_device *binder_device;
3771
3772 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
3773 if (!binder_device)
3774 return -ENOMEM;
3775
3776 binder_device->miscdev.fops = &binder_fops;
3777 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
3778 binder_device->miscdev.name = name;
3779
3780 binder_device->context.binder_context_mgr_uid = INVALID_UID;
3781 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003782 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003783
3784 ret = misc_register(&binder_device->miscdev);
3785 if (ret < 0) {
3786 kfree(binder_device);
3787 return ret;
3788 }
3789
3790 hlist_add_head(&binder_device->hlist, &binder_devices);
3791
3792 return ret;
3793}
3794
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003795static int __init binder_init(void)
3796{
3797 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003798 char *device_name, *device_names;
3799 struct binder_device *device;
3800 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003801
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003802 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3803 if (binder_debugfs_dir_entry_root)
3804 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3805 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003806
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003807 if (binder_debugfs_dir_entry_root) {
3808 debugfs_create_file("state",
3809 S_IRUGO,
3810 binder_debugfs_dir_entry_root,
3811 NULL,
3812 &binder_state_fops);
3813 debugfs_create_file("stats",
3814 S_IRUGO,
3815 binder_debugfs_dir_entry_root,
3816 NULL,
3817 &binder_stats_fops);
3818 debugfs_create_file("transactions",
3819 S_IRUGO,
3820 binder_debugfs_dir_entry_root,
3821 NULL,
3822 &binder_transactions_fops);
3823 debugfs_create_file("transaction_log",
3824 S_IRUGO,
3825 binder_debugfs_dir_entry_root,
3826 &binder_transaction_log,
3827 &binder_transaction_log_fops);
3828 debugfs_create_file("failed_transaction_log",
3829 S_IRUGO,
3830 binder_debugfs_dir_entry_root,
3831 &binder_transaction_log_failed,
3832 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003833 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003834
3835 /*
3836 * Copy the module_parameter string, because we don't want to
3837 * tokenize it in-place.
3838 */
3839 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
3840 if (!device_names) {
3841 ret = -ENOMEM;
3842 goto err_alloc_device_names_failed;
3843 }
3844 strcpy(device_names, binder_devices_param);
3845
3846 while ((device_name = strsep(&device_names, ","))) {
3847 ret = init_binder_device(device_name);
3848 if (ret)
3849 goto err_init_binder_device_failed;
3850 }
3851
3852 return ret;
3853
3854err_init_binder_device_failed:
3855 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
3856 misc_deregister(&device->miscdev);
3857 hlist_del(&device->hlist);
3858 kfree(device);
3859 }
3860err_alloc_device_names_failed:
3861 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
3862
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003863 return ret;
3864}
3865
3866device_initcall(binder_init);
3867
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003868#define CREATE_TRACE_POINTS
3869#include "binder_trace.h"
3870
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003871MODULE_LICENSE("GPL v2");