blob: f12913303827d046ea0f1d54a879c3aa34ddb749 [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 }
1802 if (reply) {
1803 BUG_ON(t->buffer->async_transaction != 0);
1804 binder_pop_transaction(target_thread, in_reply_to);
1805 } else if (!(t->flags & TF_ONE_WAY)) {
1806 BUG_ON(t->buffer->async_transaction != 0);
1807 t->need_reply = 1;
1808 t->from_parent = thread->transaction_stack;
1809 thread->transaction_stack = t;
1810 } else {
1811 BUG_ON(target_node == NULL);
1812 BUG_ON(t->buffer->async_transaction != 1);
1813 if (target_node->has_async_transaction) {
1814 target_list = &target_node->async_todo;
1815 target_wait = NULL;
1816 } else
1817 target_node->has_async_transaction = 1;
1818 }
1819 t->work.type = BINDER_WORK_TRANSACTION;
1820 list_add_tail(&t->work.entry, target_list);
1821 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1822 list_add_tail(&tcomplete->entry, &thread->todo);
Riley Andrewsb5968812015-09-01 12:42:07 -07001823 if (target_wait) {
1824 if (reply || !(t->flags & TF_ONE_WAY))
1825 wake_up_interruptible_sync(target_wait);
1826 else
1827 wake_up_interruptible(target_wait);
1828 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001829 return;
1830
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001831err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001832err_bad_object_type:
1833err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001834err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001835err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001836 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001837 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1838 t->buffer->transaction = NULL;
Todd Kjosd325d372016-10-10 10:40:53 -07001839 binder_alloc_free_buf(&target_proc->alloc, t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001840err_binder_alloc_buf_failed:
1841 kfree(tcomplete);
1842 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1843err_alloc_tcomplete_failed:
1844 kfree(t);
1845 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1846err_alloc_t_failed:
1847err_bad_call_stack:
1848err_empty_call_stack:
1849err_dead_binder:
1850err_invalid_target_handle:
1851err_no_context_mgr_node:
1852 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Todd Kjose598d172017-03-22 17:19:52 -07001853 "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n",
1854 proc->pid, thread->pid, return_error, return_error_param,
1855 (u64)tr->data_size, (u64)tr->offsets_size,
1856 return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001857
1858 {
1859 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09001860
Todd Kjose598d172017-03-22 17:19:52 -07001861 e->return_error = return_error;
1862 e->return_error_param = return_error_param;
1863 e->return_error_line = return_error_line;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001864 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1865 *fe = *e;
1866 }
1867
1868 BUG_ON(thread->return_error != BR_OK);
1869 if (in_reply_to) {
1870 thread->return_error = BR_TRANSACTION_COMPLETE;
1871 binder_send_failed_reply(in_reply_to, return_error);
1872 } else
1873 thread->return_error = return_error;
1874}
1875
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02001876static int binder_thread_write(struct binder_proc *proc,
1877 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001878 binder_uintptr_t binder_buffer, size_t size,
1879 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001880{
1881 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001882 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001883 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001884 void __user *ptr = buffer + *consumed;
1885 void __user *end = buffer + size;
1886
1887 while (ptr < end && thread->return_error == BR_OK) {
1888 if (get_user(cmd, (uint32_t __user *)ptr))
1889 return -EFAULT;
1890 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001891 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001892 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07001893 atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]);
1894 atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]);
1895 atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001896 }
1897 switch (cmd) {
1898 case BC_INCREFS:
1899 case BC_ACQUIRE:
1900 case BC_RELEASE:
1901 case BC_DECREFS: {
1902 uint32_t target;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001903 struct binder_ref *ref = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001904 const char *debug_string;
1905
1906 if (get_user(target, (uint32_t __user *)ptr))
1907 return -EFAULT;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001908
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001909 ptr += sizeof(uint32_t);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001910 if (target == 0 &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001911 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001912 struct binder_node *ctx_mgr_node;
1913
1914 mutex_lock(&context->context_mgr_node_lock);
1915 ctx_mgr_node = context->binder_context_mgr_node;
1916 if (ctx_mgr_node) {
1917 ref = binder_get_ref_for_node(proc,
1918 ctx_mgr_node);
1919 if (ref && ref->desc != target) {
1920 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
1921 proc->pid, thread->pid,
1922 ref->desc);
1923 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001924 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07001925 mutex_unlock(&context->context_mgr_node_lock);
1926 }
1927 if (ref == NULL)
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001928 ref = binder_get_ref(proc, target,
1929 cmd == BC_ACQUIRE ||
1930 cmd == BC_RELEASE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001931 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301932 binder_user_error("%d:%d refcount change on invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001933 proc->pid, thread->pid, target);
1934 break;
1935 }
1936 switch (cmd) {
1937 case BC_INCREFS:
1938 debug_string = "IncRefs";
1939 binder_inc_ref(ref, 0, NULL);
1940 break;
1941 case BC_ACQUIRE:
1942 debug_string = "Acquire";
1943 binder_inc_ref(ref, 1, NULL);
1944 break;
1945 case BC_RELEASE:
1946 debug_string = "Release";
1947 binder_dec_ref(ref, 1);
1948 break;
1949 case BC_DECREFS:
1950 default:
1951 debug_string = "DecRefs";
1952 binder_dec_ref(ref, 0);
1953 break;
1954 }
1955 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301956 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001957 proc->pid, thread->pid, debug_string, ref->debug_id,
1958 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1959 break;
1960 }
1961 case BC_INCREFS_DONE:
1962 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001963 binder_uintptr_t node_ptr;
1964 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001965 struct binder_node *node;
1966
Arve Hjønnevågda498892014-02-21 14:40:26 -08001967 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001968 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001969 ptr += sizeof(binder_uintptr_t);
1970 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001971 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001972 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001973 node = binder_get_node(proc, node_ptr);
1974 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001975 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001976 proc->pid, thread->pid,
1977 cmd == BC_INCREFS_DONE ?
1978 "BC_INCREFS_DONE" :
1979 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001980 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001981 break;
1982 }
1983 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001984 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001985 proc->pid, thread->pid,
1986 cmd == BC_INCREFS_DONE ?
1987 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001988 (u64)node_ptr, node->debug_id,
1989 (u64)cookie, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001990 break;
1991 }
1992 if (cmd == BC_ACQUIRE_DONE) {
1993 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301994 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001995 proc->pid, thread->pid,
1996 node->debug_id);
1997 break;
1998 }
1999 node->pending_strong_ref = 0;
2000 } else {
2001 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302002 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002003 proc->pid, thread->pid,
2004 node->debug_id);
2005 break;
2006 }
2007 node->pending_weak_ref = 0;
2008 }
2009 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
2010 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302011 "%d:%d %s node %d ls %d lw %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002012 proc->pid, thread->pid,
2013 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
2014 node->debug_id, node->local_strong_refs, node->local_weak_refs);
2015 break;
2016 }
2017 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302018 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002019 return -EINVAL;
2020 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302021 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002022 return -EINVAL;
2023
2024 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002025 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002026 struct binder_buffer *buffer;
2027
Arve Hjønnevågda498892014-02-21 14:40:26 -08002028 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002029 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002030 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002031
Todd Kjosd325d372016-10-10 10:40:53 -07002032 buffer = binder_alloc_buffer_lookup(&proc->alloc,
2033 data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002034 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002035 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
2036 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002037 break;
2038 }
2039 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002040 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
2041 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002042 break;
2043 }
2044 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002045 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
2046 proc->pid, thread->pid, (u64)data_ptr,
2047 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002048 buffer->transaction ? "active" : "finished");
2049
2050 if (buffer->transaction) {
2051 buffer->transaction->buffer = NULL;
2052 buffer->transaction = NULL;
2053 }
2054 if (buffer->async_transaction && buffer->target_node) {
2055 BUG_ON(!buffer->target_node->has_async_transaction);
2056 if (list_empty(&buffer->target_node->async_todo))
2057 buffer->target_node->has_async_transaction = 0;
2058 else
2059 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
2060 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002061 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002062 binder_transaction_buffer_release(proc, buffer, NULL);
Todd Kjosd325d372016-10-10 10:40:53 -07002063 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002064 break;
2065 }
2066
Martijn Coenen5a6da532016-09-30 14:10:07 +02002067 case BC_TRANSACTION_SG:
2068 case BC_REPLY_SG: {
2069 struct binder_transaction_data_sg tr;
2070
2071 if (copy_from_user(&tr, ptr, sizeof(tr)))
2072 return -EFAULT;
2073 ptr += sizeof(tr);
2074 binder_transaction(proc, thread, &tr.transaction_data,
2075 cmd == BC_REPLY_SG, tr.buffers_size);
2076 break;
2077 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002078 case BC_TRANSACTION:
2079 case BC_REPLY: {
2080 struct binder_transaction_data tr;
2081
2082 if (copy_from_user(&tr, ptr, sizeof(tr)))
2083 return -EFAULT;
2084 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02002085 binder_transaction(proc, thread, &tr,
2086 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002087 break;
2088 }
2089
2090 case BC_REGISTER_LOOPER:
2091 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302092 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002093 proc->pid, thread->pid);
2094 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
2095 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302096 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002097 proc->pid, thread->pid);
2098 } else if (proc->requested_threads == 0) {
2099 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302100 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002101 proc->pid, thread->pid);
2102 } else {
2103 proc->requested_threads--;
2104 proc->requested_threads_started++;
2105 }
2106 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
2107 break;
2108 case BC_ENTER_LOOPER:
2109 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302110 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002111 proc->pid, thread->pid);
2112 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
2113 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302114 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002115 proc->pid, thread->pid);
2116 }
2117 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
2118 break;
2119 case BC_EXIT_LOOPER:
2120 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302121 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002122 proc->pid, thread->pid);
2123 thread->looper |= BINDER_LOOPER_STATE_EXITED;
2124 break;
2125
2126 case BC_REQUEST_DEATH_NOTIFICATION:
2127 case BC_CLEAR_DEATH_NOTIFICATION: {
2128 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002129 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002130 struct binder_ref *ref;
2131 struct binder_ref_death *death;
2132
2133 if (get_user(target, (uint32_t __user *)ptr))
2134 return -EFAULT;
2135 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002136 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002137 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002138 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002139 ref = binder_get_ref(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002140 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302141 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002142 proc->pid, thread->pid,
2143 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2144 "BC_REQUEST_DEATH_NOTIFICATION" :
2145 "BC_CLEAR_DEATH_NOTIFICATION",
2146 target);
2147 break;
2148 }
2149
2150 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002151 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002152 proc->pid, thread->pid,
2153 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2154 "BC_REQUEST_DEATH_NOTIFICATION" :
2155 "BC_CLEAR_DEATH_NOTIFICATION",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002156 (u64)cookie, ref->debug_id, ref->desc,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002157 ref->strong, ref->weak, ref->node->debug_id);
2158
2159 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2160 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302161 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002162 proc->pid, thread->pid);
2163 break;
2164 }
2165 death = kzalloc(sizeof(*death), GFP_KERNEL);
2166 if (death == NULL) {
2167 thread->return_error = BR_ERROR;
2168 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302169 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002170 proc->pid, thread->pid);
2171 break;
2172 }
2173 binder_stats_created(BINDER_STAT_DEATH);
2174 INIT_LIST_HEAD(&death->work.entry);
2175 death->cookie = cookie;
2176 ref->death = death;
2177 if (ref->node->proc == NULL) {
2178 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2179 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2180 list_add_tail(&ref->death->work.entry, &thread->todo);
2181 } else {
2182 list_add_tail(&ref->death->work.entry, &proc->todo);
2183 wake_up_interruptible(&proc->wait);
2184 }
2185 }
2186 } else {
2187 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302188 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002189 proc->pid, thread->pid);
2190 break;
2191 }
2192 death = ref->death;
2193 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002194 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002195 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002196 (u64)death->cookie,
2197 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002198 break;
2199 }
2200 ref->death = NULL;
2201 if (list_empty(&death->work.entry)) {
2202 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2203 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2204 list_add_tail(&death->work.entry, &thread->todo);
2205 } else {
2206 list_add_tail(&death->work.entry, &proc->todo);
2207 wake_up_interruptible(&proc->wait);
2208 }
2209 } else {
2210 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2211 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2212 }
2213 }
2214 } break;
2215 case BC_DEAD_BINDER_DONE: {
2216 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002217 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002218 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09002219
Arve Hjønnevågda498892014-02-21 14:40:26 -08002220 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002221 return -EFAULT;
2222
Lisa Du7a64cd82016-02-17 09:32:52 +08002223 ptr += sizeof(cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002224 list_for_each_entry(w, &proc->delivered_death, entry) {
2225 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
Seunghun Lee10f62862014-05-01 01:30:23 +09002226
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002227 if (tmp_death->cookie == cookie) {
2228 death = tmp_death;
2229 break;
2230 }
2231 }
2232 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002233 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2234 proc->pid, thread->pid, (u64)cookie,
2235 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002236 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002237 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2238 proc->pid, thread->pid, (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002239 break;
2240 }
2241
2242 list_del_init(&death->work.entry);
2243 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2244 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2245 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2246 list_add_tail(&death->work.entry, &thread->todo);
2247 } else {
2248 list_add_tail(&death->work.entry, &proc->todo);
2249 wake_up_interruptible(&proc->wait);
2250 }
2251 }
2252 } break;
2253
2254 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302255 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002256 proc->pid, thread->pid, cmd);
2257 return -EINVAL;
2258 }
2259 *consumed = ptr - buffer;
2260 }
2261 return 0;
2262}
2263
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002264static void binder_stat_br(struct binder_proc *proc,
2265 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002266{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002267 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002268 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07002269 atomic_inc(&binder_stats.br[_IOC_NR(cmd)]);
2270 atomic_inc(&proc->stats.br[_IOC_NR(cmd)]);
2271 atomic_inc(&thread->stats.br[_IOC_NR(cmd)]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002272 }
2273}
2274
2275static int binder_has_proc_work(struct binder_proc *proc,
2276 struct binder_thread *thread)
2277{
2278 return !list_empty(&proc->todo) ||
2279 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2280}
2281
2282static int binder_has_thread_work(struct binder_thread *thread)
2283{
2284 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2285 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2286}
2287
2288static int binder_thread_read(struct binder_proc *proc,
2289 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002290 binder_uintptr_t binder_buffer, size_t size,
2291 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002292{
Arve Hjønnevågda498892014-02-21 14:40:26 -08002293 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002294 void __user *ptr = buffer + *consumed;
2295 void __user *end = buffer + size;
2296
2297 int ret = 0;
2298 int wait_for_proc_work;
2299
2300 if (*consumed == 0) {
2301 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2302 return -EFAULT;
2303 ptr += sizeof(uint32_t);
2304 }
2305
2306retry:
2307 wait_for_proc_work = thread->transaction_stack == NULL &&
2308 list_empty(&thread->todo);
2309
2310 if (thread->return_error != BR_OK && ptr < end) {
2311 if (thread->return_error2 != BR_OK) {
2312 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2313 return -EFAULT;
2314 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002315 binder_stat_br(proc, thread, thread->return_error2);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002316 if (ptr == end)
2317 goto done;
2318 thread->return_error2 = BR_OK;
2319 }
2320 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2321 return -EFAULT;
2322 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002323 binder_stat_br(proc, thread, thread->return_error);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002324 thread->return_error = BR_OK;
2325 goto done;
2326 }
2327
2328
2329 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2330 if (wait_for_proc_work)
2331 proc->ready_threads++;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002332
2333 binder_unlock(__func__);
2334
2335 trace_binder_wait_for_work(wait_for_proc_work,
2336 !!thread->transaction_stack,
2337 !list_empty(&thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002338 if (wait_for_proc_work) {
2339 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2340 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302341 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 +09002342 proc->pid, thread->pid, thread->looper);
2343 wait_event_interruptible(binder_user_error_wait,
2344 binder_stop_on_user_error < 2);
2345 }
2346 binder_set_nice(proc->default_priority);
2347 if (non_block) {
2348 if (!binder_has_proc_work(proc, thread))
2349 ret = -EAGAIN;
2350 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002351 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002352 } else {
2353 if (non_block) {
2354 if (!binder_has_thread_work(thread))
2355 ret = -EAGAIN;
2356 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002357 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002358 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002359
2360 binder_lock(__func__);
2361
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002362 if (wait_for_proc_work)
2363 proc->ready_threads--;
2364 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2365
2366 if (ret)
2367 return ret;
2368
2369 while (1) {
2370 uint32_t cmd;
2371 struct binder_transaction_data tr;
2372 struct binder_work *w;
2373 struct binder_transaction *t = NULL;
2374
Dmitry Voytik395262a2014-09-08 18:16:34 +04002375 if (!list_empty(&thread->todo)) {
2376 w = list_first_entry(&thread->todo, struct binder_work,
2377 entry);
2378 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2379 w = list_first_entry(&proc->todo, struct binder_work,
2380 entry);
2381 } else {
2382 /* no data added */
2383 if (ptr - buffer == 4 &&
2384 !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002385 goto retry;
2386 break;
2387 }
2388
2389 if (end - ptr < sizeof(tr) + 4)
2390 break;
2391
2392 switch (w->type) {
2393 case BINDER_WORK_TRANSACTION: {
2394 t = container_of(w, struct binder_transaction, work);
2395 } break;
2396 case BINDER_WORK_TRANSACTION_COMPLETE: {
2397 cmd = BR_TRANSACTION_COMPLETE;
2398 if (put_user(cmd, (uint32_t __user *)ptr))
2399 return -EFAULT;
2400 ptr += sizeof(uint32_t);
2401
2402 binder_stat_br(proc, thread, cmd);
2403 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302404 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002405 proc->pid, thread->pid);
2406
2407 list_del(&w->entry);
2408 kfree(w);
2409 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2410 } break;
2411 case BINDER_WORK_NODE: {
2412 struct binder_node *node = container_of(w, struct binder_node, work);
2413 uint32_t cmd = BR_NOOP;
2414 const char *cmd_name;
2415 int strong = node->internal_strong_refs || node->local_strong_refs;
2416 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
Seunghun Lee10f62862014-05-01 01:30:23 +09002417
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002418 if (weak && !node->has_weak_ref) {
2419 cmd = BR_INCREFS;
2420 cmd_name = "BR_INCREFS";
2421 node->has_weak_ref = 1;
2422 node->pending_weak_ref = 1;
2423 node->local_weak_refs++;
2424 } else if (strong && !node->has_strong_ref) {
2425 cmd = BR_ACQUIRE;
2426 cmd_name = "BR_ACQUIRE";
2427 node->has_strong_ref = 1;
2428 node->pending_strong_ref = 1;
2429 node->local_strong_refs++;
2430 } else if (!strong && node->has_strong_ref) {
2431 cmd = BR_RELEASE;
2432 cmd_name = "BR_RELEASE";
2433 node->has_strong_ref = 0;
2434 } else if (!weak && node->has_weak_ref) {
2435 cmd = BR_DECREFS;
2436 cmd_name = "BR_DECREFS";
2437 node->has_weak_ref = 0;
2438 }
2439 if (cmd != BR_NOOP) {
2440 if (put_user(cmd, (uint32_t __user *)ptr))
2441 return -EFAULT;
2442 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002443 if (put_user(node->ptr,
2444 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002445 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002446 ptr += sizeof(binder_uintptr_t);
2447 if (put_user(node->cookie,
2448 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002449 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002450 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002451
2452 binder_stat_br(proc, thread, cmd);
2453 binder_debug(BINDER_DEBUG_USER_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002454 "%d:%d %s %d u%016llx c%016llx\n",
2455 proc->pid, thread->pid, cmd_name,
2456 node->debug_id,
2457 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002458 } else {
2459 list_del_init(&w->entry);
2460 if (!weak && !strong) {
2461 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002462 "%d:%d node %d u%016llx c%016llx deleted\n",
2463 proc->pid, thread->pid,
2464 node->debug_id,
2465 (u64)node->ptr,
2466 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002467 rb_erase(&node->rb_node, &proc->nodes);
2468 kfree(node);
2469 binder_stats_deleted(BINDER_STAT_NODE);
2470 } else {
2471 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002472 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2473 proc->pid, thread->pid,
2474 node->debug_id,
2475 (u64)node->ptr,
2476 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002477 }
2478 }
2479 } break;
2480 case BINDER_WORK_DEAD_BINDER:
2481 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2482 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2483 struct binder_ref_death *death;
2484 uint32_t cmd;
2485
2486 death = container_of(w, struct binder_ref_death, work);
2487 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2488 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2489 else
2490 cmd = BR_DEAD_BINDER;
2491 if (put_user(cmd, (uint32_t __user *)ptr))
2492 return -EFAULT;
2493 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002494 if (put_user(death->cookie,
2495 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002496 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002497 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002498 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002499 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002500 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002501 proc->pid, thread->pid,
2502 cmd == BR_DEAD_BINDER ?
2503 "BR_DEAD_BINDER" :
2504 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002505 (u64)death->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002506
2507 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2508 list_del(&w->entry);
2509 kfree(death);
2510 binder_stats_deleted(BINDER_STAT_DEATH);
2511 } else
2512 list_move(&w->entry, &proc->delivered_death);
2513 if (cmd == BR_DEAD_BINDER)
2514 goto done; /* DEAD_BINDER notifications can cause transactions */
2515 } break;
2516 }
2517
2518 if (!t)
2519 continue;
2520
2521 BUG_ON(t->buffer == NULL);
2522 if (t->buffer->target_node) {
2523 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002524
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002525 tr.target.ptr = target_node->ptr;
2526 tr.cookie = target_node->cookie;
2527 t->saved_priority = task_nice(current);
2528 if (t->priority < target_node->min_priority &&
2529 !(t->flags & TF_ONE_WAY))
2530 binder_set_nice(t->priority);
2531 else if (!(t->flags & TF_ONE_WAY) ||
2532 t->saved_priority > target_node->min_priority)
2533 binder_set_nice(target_node->min_priority);
2534 cmd = BR_TRANSACTION;
2535 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002536 tr.target.ptr = 0;
2537 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002538 cmd = BR_REPLY;
2539 }
2540 tr.code = t->code;
2541 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002542 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002543
2544 if (t->from) {
2545 struct task_struct *sender = t->from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09002546
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002547 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002548 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002549 } else {
2550 tr.sender_pid = 0;
2551 }
2552
2553 tr.data_size = t->buffer->data_size;
2554 tr.offsets_size = t->buffer->offsets_size;
Todd Kjosd325d372016-10-10 10:40:53 -07002555 tr.data.ptr.buffer = (binder_uintptr_t)
2556 ((uintptr_t)t->buffer->data +
2557 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002558 tr.data.ptr.offsets = tr.data.ptr.buffer +
2559 ALIGN(t->buffer->data_size,
2560 sizeof(void *));
2561
2562 if (put_user(cmd, (uint32_t __user *)ptr))
2563 return -EFAULT;
2564 ptr += sizeof(uint32_t);
2565 if (copy_to_user(ptr, &tr, sizeof(tr)))
2566 return -EFAULT;
2567 ptr += sizeof(tr);
2568
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002569 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002570 binder_stat_br(proc, thread, cmd);
2571 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002572 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002573 proc->pid, thread->pid,
2574 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2575 "BR_REPLY",
2576 t->debug_id, t->from ? t->from->proc->pid : 0,
2577 t->from ? t->from->pid : 0, cmd,
2578 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002579 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002580
2581 list_del(&t->work.entry);
2582 t->buffer->allow_user_free = 1;
2583 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2584 t->to_parent = thread->transaction_stack;
2585 t->to_thread = thread;
2586 thread->transaction_stack = t;
2587 } else {
2588 t->buffer->transaction = NULL;
2589 kfree(t);
2590 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2591 }
2592 break;
2593 }
2594
2595done:
2596
2597 *consumed = ptr - buffer;
2598 if (proc->requested_threads + proc->ready_threads == 0 &&
2599 proc->requested_threads_started < proc->max_threads &&
2600 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2601 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2602 /*spawn a new thread if we leave this out */) {
2603 proc->requested_threads++;
2604 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302605 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002606 proc->pid, thread->pid);
2607 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2608 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002609 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002610 }
2611 return 0;
2612}
2613
2614static void binder_release_work(struct list_head *list)
2615{
2616 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09002617
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002618 while (!list_empty(list)) {
2619 w = list_first_entry(list, struct binder_work, entry);
2620 list_del_init(&w->entry);
2621 switch (w->type) {
2622 case BINDER_WORK_TRANSACTION: {
2623 struct binder_transaction *t;
2624
2625 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002626 if (t->buffer->target_node &&
2627 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002628 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002629 } else {
2630 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302631 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002632 t->debug_id);
2633 t->buffer->transaction = NULL;
2634 kfree(t);
2635 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2636 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002637 } break;
2638 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002639 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302640 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002641 kfree(w);
2642 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2643 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002644 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2645 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2646 struct binder_ref_death *death;
2647
2648 death = container_of(w, struct binder_ref_death, work);
2649 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002650 "undelivered death notification, %016llx\n",
2651 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002652 kfree(death);
2653 binder_stats_deleted(BINDER_STAT_DEATH);
2654 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002655 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302656 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002657 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002658 break;
2659 }
2660 }
2661
2662}
2663
2664static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2665{
2666 struct binder_thread *thread = NULL;
2667 struct rb_node *parent = NULL;
2668 struct rb_node **p = &proc->threads.rb_node;
2669
2670 while (*p) {
2671 parent = *p;
2672 thread = rb_entry(parent, struct binder_thread, rb_node);
2673
2674 if (current->pid < thread->pid)
2675 p = &(*p)->rb_left;
2676 else if (current->pid > thread->pid)
2677 p = &(*p)->rb_right;
2678 else
2679 break;
2680 }
2681 if (*p == NULL) {
2682 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2683 if (thread == NULL)
2684 return NULL;
2685 binder_stats_created(BINDER_STAT_THREAD);
2686 thread->proc = proc;
2687 thread->pid = current->pid;
2688 init_waitqueue_head(&thread->wait);
2689 INIT_LIST_HEAD(&thread->todo);
2690 rb_link_node(&thread->rb_node, parent, p);
2691 rb_insert_color(&thread->rb_node, &proc->threads);
2692 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2693 thread->return_error = BR_OK;
2694 thread->return_error2 = BR_OK;
2695 }
2696 return thread;
2697}
2698
2699static int binder_free_thread(struct binder_proc *proc,
2700 struct binder_thread *thread)
2701{
2702 struct binder_transaction *t;
2703 struct binder_transaction *send_reply = NULL;
2704 int active_transactions = 0;
2705
2706 rb_erase(&thread->rb_node, &proc->threads);
2707 t = thread->transaction_stack;
2708 if (t && t->to_thread == thread)
2709 send_reply = t;
2710 while (t) {
2711 active_transactions++;
2712 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302713 "release %d:%d transaction %d %s, still active\n",
2714 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002715 t->debug_id,
2716 (t->to_thread == thread) ? "in" : "out");
2717
2718 if (t->to_thread == thread) {
2719 t->to_proc = NULL;
2720 t->to_thread = NULL;
2721 if (t->buffer) {
2722 t->buffer->transaction = NULL;
2723 t->buffer = NULL;
2724 }
2725 t = t->to_parent;
2726 } else if (t->from == thread) {
2727 t->from = NULL;
2728 t = t->from_parent;
2729 } else
2730 BUG();
2731 }
2732 if (send_reply)
2733 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2734 binder_release_work(&thread->todo);
2735 kfree(thread);
2736 binder_stats_deleted(BINDER_STAT_THREAD);
2737 return active_transactions;
2738}
2739
2740static unsigned int binder_poll(struct file *filp,
2741 struct poll_table_struct *wait)
2742{
2743 struct binder_proc *proc = filp->private_data;
2744 struct binder_thread *thread = NULL;
2745 int wait_for_proc_work;
2746
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002747 binder_lock(__func__);
2748
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002749 thread = binder_get_thread(proc);
2750
2751 wait_for_proc_work = thread->transaction_stack == NULL &&
2752 list_empty(&thread->todo) && thread->return_error == BR_OK;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002753
2754 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002755
2756 if (wait_for_proc_work) {
2757 if (binder_has_proc_work(proc, thread))
2758 return POLLIN;
2759 poll_wait(filp, &proc->wait, wait);
2760 if (binder_has_proc_work(proc, thread))
2761 return POLLIN;
2762 } else {
2763 if (binder_has_thread_work(thread))
2764 return POLLIN;
2765 poll_wait(filp, &thread->wait, wait);
2766 if (binder_has_thread_work(thread))
2767 return POLLIN;
2768 }
2769 return 0;
2770}
2771
Tair Rzayev78260ac2014-06-03 22:27:21 +03002772static int binder_ioctl_write_read(struct file *filp,
2773 unsigned int cmd, unsigned long arg,
2774 struct binder_thread *thread)
2775{
2776 int ret = 0;
2777 struct binder_proc *proc = filp->private_data;
2778 unsigned int size = _IOC_SIZE(cmd);
2779 void __user *ubuf = (void __user *)arg;
2780 struct binder_write_read bwr;
2781
2782 if (size != sizeof(struct binder_write_read)) {
2783 ret = -EINVAL;
2784 goto out;
2785 }
2786 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2787 ret = -EFAULT;
2788 goto out;
2789 }
2790 binder_debug(BINDER_DEBUG_READ_WRITE,
2791 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2792 proc->pid, thread->pid,
2793 (u64)bwr.write_size, (u64)bwr.write_buffer,
2794 (u64)bwr.read_size, (u64)bwr.read_buffer);
2795
2796 if (bwr.write_size > 0) {
2797 ret = binder_thread_write(proc, thread,
2798 bwr.write_buffer,
2799 bwr.write_size,
2800 &bwr.write_consumed);
2801 trace_binder_write_done(ret);
2802 if (ret < 0) {
2803 bwr.read_consumed = 0;
2804 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2805 ret = -EFAULT;
2806 goto out;
2807 }
2808 }
2809 if (bwr.read_size > 0) {
2810 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2811 bwr.read_size,
2812 &bwr.read_consumed,
2813 filp->f_flags & O_NONBLOCK);
2814 trace_binder_read_done(ret);
2815 if (!list_empty(&proc->todo))
2816 wake_up_interruptible(&proc->wait);
2817 if (ret < 0) {
2818 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2819 ret = -EFAULT;
2820 goto out;
2821 }
2822 }
2823 binder_debug(BINDER_DEBUG_READ_WRITE,
2824 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2825 proc->pid, thread->pid,
2826 (u64)bwr.write_consumed, (u64)bwr.write_size,
2827 (u64)bwr.read_consumed, (u64)bwr.read_size);
2828 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2829 ret = -EFAULT;
2830 goto out;
2831 }
2832out:
2833 return ret;
2834}
2835
2836static int binder_ioctl_set_ctx_mgr(struct file *filp)
2837{
2838 int ret = 0;
2839 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002840 struct binder_context *context = proc->context;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002841 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002842 kuid_t curr_euid = current_euid();
2843
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002844 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002845 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002846 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2847 ret = -EBUSY;
2848 goto out;
2849 }
Stephen Smalley79af7302015-01-21 10:54:10 -05002850 ret = security_binder_set_context_mgr(proc->tsk);
2851 if (ret < 0)
2852 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002853 if (uid_valid(context->binder_context_mgr_uid)) {
2854 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002855 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2856 from_kuid(&init_user_ns, curr_euid),
2857 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002858 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03002859 ret = -EPERM;
2860 goto out;
2861 }
2862 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002863 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002864 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002865 new_node = binder_new_node(proc, 0, 0);
2866 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002867 ret = -ENOMEM;
2868 goto out;
2869 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002870 new_node->local_weak_refs++;
2871 new_node->local_strong_refs++;
2872 new_node->has_strong_ref = 1;
2873 new_node->has_weak_ref = 1;
2874 context->binder_context_mgr_node = new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002875out:
Todd Kjos8d9f6f32016-10-17 12:33:15 -07002876 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03002877 return ret;
2878}
2879
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002880static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2881{
2882 int ret;
2883 struct binder_proc *proc = filp->private_data;
2884 struct binder_thread *thread;
2885 unsigned int size = _IOC_SIZE(cmd);
2886 void __user *ubuf = (void __user *)arg;
2887
Tair Rzayev78260ac2014-06-03 22:27:21 +03002888 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2889 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002890
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002891 trace_binder_ioctl(cmd, arg);
2892
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002893 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2894 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002895 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002896
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002897 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002898 thread = binder_get_thread(proc);
2899 if (thread == NULL) {
2900 ret = -ENOMEM;
2901 goto err;
2902 }
2903
2904 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002905 case BINDER_WRITE_READ:
2906 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
2907 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002908 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002909 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002910 case BINDER_SET_MAX_THREADS:
2911 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2912 ret = -EINVAL;
2913 goto err;
2914 }
2915 break;
2916 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03002917 ret = binder_ioctl_set_ctx_mgr(filp);
2918 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002919 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002920 break;
2921 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302922 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002923 proc->pid, thread->pid);
2924 binder_free_thread(proc, thread);
2925 thread = NULL;
2926 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002927 case BINDER_VERSION: {
2928 struct binder_version __user *ver = ubuf;
2929
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002930 if (size != sizeof(struct binder_version)) {
2931 ret = -EINVAL;
2932 goto err;
2933 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02002934 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2935 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002936 ret = -EINVAL;
2937 goto err;
2938 }
2939 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002940 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002941 default:
2942 ret = -EINVAL;
2943 goto err;
2944 }
2945 ret = 0;
2946err:
2947 if (thread)
2948 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002949 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002950 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2951 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05302952 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 -07002953err_unlocked:
2954 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002955 return ret;
2956}
2957
2958static void binder_vma_open(struct vm_area_struct *vma)
2959{
2960 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002961
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002962 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302963 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002964 proc->pid, vma->vm_start, vma->vm_end,
2965 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2966 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002967}
2968
2969static void binder_vma_close(struct vm_area_struct *vma)
2970{
2971 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002972
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002973 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302974 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002975 proc->pid, vma->vm_start, vma->vm_end,
2976 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2977 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjosd325d372016-10-10 10:40:53 -07002978 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002979 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2980}
2981
Vinayak Menonddac7d52014-06-02 18:17:59 +05302982static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2983{
2984 return VM_FAULT_SIGBUS;
2985}
2986
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07002987static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002988 .open = binder_vma_open,
2989 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05302990 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002991};
2992
Todd Kjosd325d372016-10-10 10:40:53 -07002993static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2994{
2995 int ret;
2996 struct binder_proc *proc = filp->private_data;
2997 const char *failure_string;
2998
2999 if (proc->tsk != current->group_leader)
3000 return -EINVAL;
3001
3002 if ((vma->vm_end - vma->vm_start) > SZ_4M)
3003 vma->vm_end = vma->vm_start + SZ_4M;
3004
3005 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3006 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
3007 __func__, proc->pid, vma->vm_start, vma->vm_end,
3008 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3009 (unsigned long)pgprot_val(vma->vm_page_prot));
3010
3011 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
3012 ret = -EPERM;
3013 failure_string = "bad vm_flags";
3014 goto err_bad_arg;
3015 }
3016 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
3017 vma->vm_ops = &binder_vm_ops;
3018 vma->vm_private_data = proc;
3019
3020 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
3021 if (ret)
3022 return ret;
3023 proc->files = get_files_struct(current);
3024 return 0;
3025
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003026err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04003027 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003028 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
3029 return ret;
3030}
3031
3032static int binder_open(struct inode *nodp, struct file *filp)
3033{
3034 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003035 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003036
3037 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
3038 current->group_leader->pid, current->pid);
3039
3040 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
3041 if (proc == NULL)
3042 return -ENOMEM;
Martijn Coenen872c26e2017-03-07 15:51:18 +01003043 get_task_struct(current->group_leader);
3044 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003045 INIT_LIST_HEAD(&proc->todo);
3046 init_waitqueue_head(&proc->wait);
3047 proc->default_priority = task_nice(current);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003048 binder_dev = container_of(filp->private_data, struct binder_device,
3049 miscdev);
3050 proc->context = &binder_dev->context;
Todd Kjosd325d372016-10-10 10:40:53 -07003051 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003052
3053 binder_lock(__func__);
3054
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003055 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003056 proc->pid = current->group_leader->pid;
3057 INIT_LIST_HEAD(&proc->delivered_death);
3058 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003059
3060 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003061
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003062 mutex_lock(&binder_procs_lock);
3063 hlist_add_head(&proc->proc_node, &binder_procs);
3064 mutex_unlock(&binder_procs_lock);
3065
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003066 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003067 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09003068
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003069 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003070 /*
3071 * proc debug entries are shared between contexts, so
3072 * this will fail if the process tries to open the driver
3073 * again with a different context. The priting code will
3074 * anyway print all contexts that a given PID has, so this
3075 * is not a problem.
3076 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003077 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003078 binder_debugfs_dir_entry_proc,
3079 (void *)(unsigned long)proc->pid,
3080 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003081 }
3082
3083 return 0;
3084}
3085
3086static int binder_flush(struct file *filp, fl_owner_t id)
3087{
3088 struct binder_proc *proc = filp->private_data;
3089
3090 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3091
3092 return 0;
3093}
3094
3095static void binder_deferred_flush(struct binder_proc *proc)
3096{
3097 struct rb_node *n;
3098 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09003099
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003100 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3101 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09003102
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003103 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3104 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3105 wake_up_interruptible(&thread->wait);
3106 wake_count++;
3107 }
3108 }
3109 wake_up_interruptible_all(&proc->wait);
3110
3111 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3112 "binder_flush: %d woke %d threads\n", proc->pid,
3113 wake_count);
3114}
3115
3116static int binder_release(struct inode *nodp, struct file *filp)
3117{
3118 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003119
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003120 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003121 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3122
3123 return 0;
3124}
3125
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003126static int binder_node_release(struct binder_node *node, int refs)
3127{
3128 struct binder_ref *ref;
3129 int death = 0;
3130
3131 list_del_init(&node->work.entry);
3132 binder_release_work(&node->async_todo);
3133
3134 if (hlist_empty(&node->refs)) {
3135 kfree(node);
3136 binder_stats_deleted(BINDER_STAT_NODE);
3137
3138 return refs;
3139 }
3140
3141 node->proc = NULL;
3142 node->local_strong_refs = 0;
3143 node->local_weak_refs = 0;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003144
3145 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003146 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003147 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003148
3149 hlist_for_each_entry(ref, &node->refs, node_entry) {
3150 refs++;
3151
3152 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08003153 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003154
3155 death++;
3156
3157 if (list_empty(&ref->death->work.entry)) {
3158 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3159 list_add_tail(&ref->death->work.entry,
3160 &ref->proc->todo);
3161 wake_up_interruptible(&ref->proc->wait);
3162 } else
3163 BUG();
3164 }
3165
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003166 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3167 "node %d now dead, refs %d, death %d\n",
3168 node->debug_id, refs, death);
3169
3170 return refs;
3171}
3172
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003173static void binder_deferred_release(struct binder_proc *proc)
3174{
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003175 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003176 struct rb_node *n;
Todd Kjosd325d372016-10-10 10:40:53 -07003177 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003178
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003179 BUG_ON(proc->files);
3180
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003181 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003182 hlist_del(&proc->proc_node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003183 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003184
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003185 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003186 if (context->binder_context_mgr_node &&
3187 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003188 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003189 "%s: %d context_mgr_node gone\n",
3190 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003191 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003192 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003193 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003194
3195 threads = 0;
3196 active_transactions = 0;
3197 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003198 struct binder_thread *thread;
3199
3200 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003201 threads++;
3202 active_transactions += binder_free_thread(proc, thread);
3203 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003204
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003205 nodes = 0;
3206 incoming_refs = 0;
3207 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003208 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003209
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003210 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003211 nodes++;
3212 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003213 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003214 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003215
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003216 outgoing_refs = 0;
3217 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003218 struct binder_ref *ref;
3219
3220 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003221 outgoing_refs++;
3222 binder_delete_ref(ref);
3223 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003224
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003225 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003226 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003227
Todd Kjosd325d372016-10-10 10:40:53 -07003228 binder_alloc_deferred_release(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003229 binder_stats_deleted(BINDER_STAT_PROC);
3230
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003231 put_task_struct(proc->tsk);
3232
3233 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjosd325d372016-10-10 10:40:53 -07003234 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003235 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjosd325d372016-10-10 10:40:53 -07003236 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003237
3238 kfree(proc);
3239}
3240
3241static void binder_deferred_func(struct work_struct *work)
3242{
3243 struct binder_proc *proc;
3244 struct files_struct *files;
3245
3246 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003247
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003248 do {
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003249 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003250 mutex_lock(&binder_deferred_lock);
3251 if (!hlist_empty(&binder_deferred_list)) {
3252 proc = hlist_entry(binder_deferred_list.first,
3253 struct binder_proc, deferred_work_node);
3254 hlist_del_init(&proc->deferred_work_node);
3255 defer = proc->deferred_work;
3256 proc->deferred_work = 0;
3257 } else {
3258 proc = NULL;
3259 defer = 0;
3260 }
3261 mutex_unlock(&binder_deferred_lock);
3262
3263 files = NULL;
3264 if (defer & BINDER_DEFERRED_PUT_FILES) {
3265 files = proc->files;
3266 if (files)
3267 proc->files = NULL;
3268 }
3269
3270 if (defer & BINDER_DEFERRED_FLUSH)
3271 binder_deferred_flush(proc);
3272
3273 if (defer & BINDER_DEFERRED_RELEASE)
3274 binder_deferred_release(proc); /* frees proc */
3275
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003276 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003277 if (files)
3278 put_files_struct(files);
3279 } while (proc);
3280}
3281static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3282
3283static void
3284binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3285{
3286 mutex_lock(&binder_deferred_lock);
3287 proc->deferred_work |= defer;
3288 if (hlist_unhashed(&proc->deferred_work_node)) {
3289 hlist_add_head(&proc->deferred_work_node,
3290 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05303291 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003292 }
3293 mutex_unlock(&binder_deferred_lock);
3294}
3295
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003296static void print_binder_transaction(struct seq_file *m, const char *prefix,
3297 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003298{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003299 seq_printf(m,
3300 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3301 prefix, t->debug_id, t,
3302 t->from ? t->from->proc->pid : 0,
3303 t->from ? t->from->pid : 0,
3304 t->to_proc ? t->to_proc->pid : 0,
3305 t->to_thread ? t->to_thread->pid : 0,
3306 t->code, t->flags, t->priority, t->need_reply);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003307 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003308 seq_puts(m, " buffer free\n");
3309 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003310 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003311 if (t->buffer->target_node)
3312 seq_printf(m, " node %d",
3313 t->buffer->target_node->debug_id);
3314 seq_printf(m, " size %zd:%zd data %p\n",
3315 t->buffer->data_size, t->buffer->offsets_size,
3316 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003317}
3318
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003319static void print_binder_work(struct seq_file *m, const char *prefix,
3320 const char *transaction_prefix,
3321 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003322{
3323 struct binder_node *node;
3324 struct binder_transaction *t;
3325
3326 switch (w->type) {
3327 case BINDER_WORK_TRANSACTION:
3328 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003329 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003330 break;
3331 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003332 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003333 break;
3334 case BINDER_WORK_NODE:
3335 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003336 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3337 prefix, node->debug_id,
3338 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003339 break;
3340 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003341 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003342 break;
3343 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003344 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003345 break;
3346 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003347 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003348 break;
3349 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003350 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003351 break;
3352 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003353}
3354
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003355static void print_binder_thread(struct seq_file *m,
3356 struct binder_thread *thread,
3357 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003358{
3359 struct binder_transaction *t;
3360 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003361 size_t start_pos = m->count;
3362 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003363
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003364 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3365 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003366 t = thread->transaction_stack;
3367 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003368 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003369 print_binder_transaction(m,
3370 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003371 t = t->from_parent;
3372 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003373 print_binder_transaction(m,
3374 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003375 t = t->to_parent;
3376 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003377 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003378 t = NULL;
3379 }
3380 }
3381 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003382 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003383 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003384 if (!print_always && m->count == header_pos)
3385 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003386}
3387
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003388static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003389{
3390 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003391 struct binder_work *w;
3392 int count;
3393
3394 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003395 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396 count++;
3397
Arve Hjønnevågda498892014-02-21 14:40:26 -08003398 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3399 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003400 node->has_strong_ref, node->has_weak_ref,
3401 node->local_strong_refs, node->local_weak_refs,
3402 node->internal_strong_refs, count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003403 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003404 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003405 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003406 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003407 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003408 seq_puts(m, "\n");
3409 list_for_each_entry(w, &node->async_todo, entry)
3410 print_binder_work(m, " ",
3411 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003412}
3413
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003414static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003415{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003416 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3417 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3418 ref->node->debug_id, ref->strong, ref->weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003419}
3420
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003421static void print_binder_proc(struct seq_file *m,
3422 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003423{
3424 struct binder_work *w;
3425 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003426 size_t start_pos = m->count;
3427 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003428
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003429 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003430 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003431 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003432
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003433 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3434 print_binder_thread(m, rb_entry(n, struct binder_thread,
3435 rb_node), print_all);
3436 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003437 struct binder_node *node = rb_entry(n, struct binder_node,
3438 rb_node);
3439 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003440 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003441 }
3442 if (print_all) {
3443 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003444 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003445 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003446 print_binder_ref(m, rb_entry(n, struct binder_ref,
3447 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003448 }
Todd Kjosd325d372016-10-10 10:40:53 -07003449 binder_alloc_print_allocated(m, &proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003450 list_for_each_entry(w, &proc->todo, entry)
3451 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003452 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003453 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003454 break;
3455 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003456 if (!print_all && m->count == header_pos)
3457 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003458}
3459
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003460static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003461 "BR_ERROR",
3462 "BR_OK",
3463 "BR_TRANSACTION",
3464 "BR_REPLY",
3465 "BR_ACQUIRE_RESULT",
3466 "BR_DEAD_REPLY",
3467 "BR_TRANSACTION_COMPLETE",
3468 "BR_INCREFS",
3469 "BR_ACQUIRE",
3470 "BR_RELEASE",
3471 "BR_DECREFS",
3472 "BR_ATTEMPT_ACQUIRE",
3473 "BR_NOOP",
3474 "BR_SPAWN_LOOPER",
3475 "BR_FINISHED",
3476 "BR_DEAD_BINDER",
3477 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3478 "BR_FAILED_REPLY"
3479};
3480
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003481static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003482 "BC_TRANSACTION",
3483 "BC_REPLY",
3484 "BC_ACQUIRE_RESULT",
3485 "BC_FREE_BUFFER",
3486 "BC_INCREFS",
3487 "BC_ACQUIRE",
3488 "BC_RELEASE",
3489 "BC_DECREFS",
3490 "BC_INCREFS_DONE",
3491 "BC_ACQUIRE_DONE",
3492 "BC_ATTEMPT_ACQUIRE",
3493 "BC_REGISTER_LOOPER",
3494 "BC_ENTER_LOOPER",
3495 "BC_EXIT_LOOPER",
3496 "BC_REQUEST_DEATH_NOTIFICATION",
3497 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02003498 "BC_DEAD_BINDER_DONE",
3499 "BC_TRANSACTION_SG",
3500 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003501};
3502
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003503static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003504 "proc",
3505 "thread",
3506 "node",
3507 "ref",
3508 "death",
3509 "transaction",
3510 "transaction_complete"
3511};
3512
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003513static void print_binder_stats(struct seq_file *m, const char *prefix,
3514 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003515{
3516 int i;
3517
3518 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003519 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003520 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003521 int temp = atomic_read(&stats->bc[i]);
3522
3523 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003524 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003525 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003526 }
3527
3528 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003529 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003530 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003531 int temp = atomic_read(&stats->br[i]);
3532
3533 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003534 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003535 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003536 }
3537
3538 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003539 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003540 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003541 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003542 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003543 int created = atomic_read(&stats->obj_created[i]);
3544 int deleted = atomic_read(&stats->obj_deleted[i]);
3545
3546 if (created || deleted)
3547 seq_printf(m, "%s%s: active %d total %d\n",
3548 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003549 binder_objstat_strings[i],
Badhri Jagan Sridharan5551ff22016-10-13 16:36:15 -07003550 created - deleted,
3551 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003552 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003553}
3554
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003555static void print_binder_proc_stats(struct seq_file *m,
3556 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003557{
3558 struct binder_work *w;
3559 struct rb_node *n;
3560 int count, strong, weak;
3561
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003562 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003563 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003564 count = 0;
3565 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3566 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003567 seq_printf(m, " threads: %d\n", count);
3568 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003569 " ready threads %d\n"
3570 " free async space %zd\n", proc->requested_threads,
3571 proc->requested_threads_started, proc->max_threads,
Todd Kjosd325d372016-10-10 10:40:53 -07003572 proc->ready_threads,
3573 binder_alloc_get_free_async_space(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003574 count = 0;
3575 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3576 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003577 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003578 count = 0;
3579 strong = 0;
3580 weak = 0;
3581 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3582 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3583 rb_node_desc);
3584 count++;
3585 strong += ref->strong;
3586 weak += ref->weak;
3587 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003588 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003589
Todd Kjosd325d372016-10-10 10:40:53 -07003590 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003591 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003592
3593 count = 0;
3594 list_for_each_entry(w, &proc->todo, entry) {
3595 switch (w->type) {
3596 case BINDER_WORK_TRANSACTION:
3597 count++;
3598 break;
3599 default:
3600 break;
3601 }
3602 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003603 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003604
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003605 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003606}
3607
3608
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003609static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003610{
3611 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003612 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003613
Todd Kjos48b33212017-05-24 11:53:13 -07003614 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003615
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003616 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003617
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003618 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003619 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003620 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003621 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003622 print_binder_node(m, node);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003623 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003624
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003625 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003626 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003627 print_binder_proc(m, proc, 1);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003628 mutex_unlock(&binder_procs_lock);
Todd Kjos48b33212017-05-24 11:53:13 -07003629 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003630 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003631}
3632
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003633static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003634{
3635 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003636
Todd Kjos48b33212017-05-24 11:53:13 -07003637 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003638
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003639 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003640
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003641 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003642
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003643 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003644 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003645 print_binder_proc_stats(m, proc);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003646 mutex_unlock(&binder_procs_lock);
Todd Kjos48b33212017-05-24 11:53:13 -07003647 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003648 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003649}
3650
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003651static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003652{
3653 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003654
Todd Kjos48b33212017-05-24 11:53:13 -07003655 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003656
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003657 seq_puts(m, "binder transactions:\n");
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003658 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003659 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003660 print_binder_proc(m, proc, 0);
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003661 mutex_unlock(&binder_procs_lock);
Todd Kjos48b33212017-05-24 11:53:13 -07003662 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003663 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003664}
3665
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003666static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003667{
Riley Andrews83050a42016-02-09 21:05:33 -08003668 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003669 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003670
Todd Kjos48b33212017-05-24 11:53:13 -07003671 binder_lock(__func__);
Riley Andrews83050a42016-02-09 21:05:33 -08003672
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003673 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08003674 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003675 if (itr->pid == pid) {
3676 seq_puts(m, "binder proc state:\n");
3677 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08003678 }
3679 }
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003680 mutex_unlock(&binder_procs_lock);
3681
Todd Kjos48b33212017-05-24 11:53:13 -07003682 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003683 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003684}
3685
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003686static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003687 struct binder_transaction_log_entry *e)
3688{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003689 seq_printf(m,
Todd Kjose598d172017-03-22 17:19:52 -07003690 "%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 -07003691 e->debug_id, (e->call_type == 2) ? "reply" :
3692 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003693 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjose598d172017-03-22 17:19:52 -07003694 e->to_node, e->target_handle, e->data_size, e->offsets_size,
3695 e->return_error, e->return_error_param,
3696 e->return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003697}
3698
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003699static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003700{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003701 struct binder_transaction_log *log = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003702 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003703
3704 if (log->full) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003705 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3706 print_binder_transaction_log_entry(m, &log->entry[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003707 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003708 for (i = 0; i < log->next; i++)
3709 print_binder_transaction_log_entry(m, &log->entry[i]);
3710 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003711}
3712
3713static const struct file_operations binder_fops = {
3714 .owner = THIS_MODULE,
3715 .poll = binder_poll,
3716 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003717 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003718 .mmap = binder_mmap,
3719 .open = binder_open,
3720 .flush = binder_flush,
3721 .release = binder_release,
3722};
3723
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003724BINDER_DEBUG_ENTRY(state);
3725BINDER_DEBUG_ENTRY(stats);
3726BINDER_DEBUG_ENTRY(transactions);
3727BINDER_DEBUG_ENTRY(transaction_log);
3728
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003729static int __init init_binder_device(const char *name)
3730{
3731 int ret;
3732 struct binder_device *binder_device;
3733
3734 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
3735 if (!binder_device)
3736 return -ENOMEM;
3737
3738 binder_device->miscdev.fops = &binder_fops;
3739 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
3740 binder_device->miscdev.name = name;
3741
3742 binder_device->context.binder_context_mgr_uid = INVALID_UID;
3743 binder_device->context.name = name;
Todd Kjos8d9f6f32016-10-17 12:33:15 -07003744 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003745
3746 ret = misc_register(&binder_device->miscdev);
3747 if (ret < 0) {
3748 kfree(binder_device);
3749 return ret;
3750 }
3751
3752 hlist_add_head(&binder_device->hlist, &binder_devices);
3753
3754 return ret;
3755}
3756
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003757static int __init binder_init(void)
3758{
3759 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003760 char *device_name, *device_names;
3761 struct binder_device *device;
3762 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003763
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003764 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3765 if (binder_debugfs_dir_entry_root)
3766 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3767 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003768
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003769 if (binder_debugfs_dir_entry_root) {
3770 debugfs_create_file("state",
3771 S_IRUGO,
3772 binder_debugfs_dir_entry_root,
3773 NULL,
3774 &binder_state_fops);
3775 debugfs_create_file("stats",
3776 S_IRUGO,
3777 binder_debugfs_dir_entry_root,
3778 NULL,
3779 &binder_stats_fops);
3780 debugfs_create_file("transactions",
3781 S_IRUGO,
3782 binder_debugfs_dir_entry_root,
3783 NULL,
3784 &binder_transactions_fops);
3785 debugfs_create_file("transaction_log",
3786 S_IRUGO,
3787 binder_debugfs_dir_entry_root,
3788 &binder_transaction_log,
3789 &binder_transaction_log_fops);
3790 debugfs_create_file("failed_transaction_log",
3791 S_IRUGO,
3792 binder_debugfs_dir_entry_root,
3793 &binder_transaction_log_failed,
3794 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003795 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003796
3797 /*
3798 * Copy the module_parameter string, because we don't want to
3799 * tokenize it in-place.
3800 */
3801 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
3802 if (!device_names) {
3803 ret = -ENOMEM;
3804 goto err_alloc_device_names_failed;
3805 }
3806 strcpy(device_names, binder_devices_param);
3807
3808 while ((device_name = strsep(&device_names, ","))) {
3809 ret = init_binder_device(device_name);
3810 if (ret)
3811 goto err_init_binder_device_failed;
3812 }
3813
3814 return ret;
3815
3816err_init_binder_device_failed:
3817 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
3818 misc_deregister(&device->miscdev);
3819 hlist_del(&device->hlist);
3820 kfree(device);
3821 }
3822err_alloc_device_names_failed:
3823 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
3824
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003825 return ret;
3826}
3827
3828device_initcall(binder_init);
3829
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003830#define CREATE_TRACE_POINTS
3831#include "binder_trace.h"
3832
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003833MODULE_LICENSE("GPL v2");