blob: f17d1dfa5b027f3792e32aa233532bb6de544541 [file] [log] [blame]
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001/* binder.c
2 *
3 * Android IPC Subsystem
4 *
5 * Copyright (C) 2007-2008 Google, Inc.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Anmol Sarma56b468f2012-10-30 22:35:43 +053018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090020#include <asm/cacheflush.h>
21#include <linux/fdtable.h>
22#include <linux/file.h>
Colin Crosse2610b22013-05-06 23:50:15 +000023#include <linux/freezer.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090024#include <linux/fs.h>
25#include <linux/list.h>
26#include <linux/miscdevice.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090027#include <linux/module.h>
28#include <linux/mutex.h>
29#include <linux/nsproxy.h>
30#include <linux/poll.h>
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070031#include <linux/debugfs.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090032#include <linux/rbtree.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010033#include <linux/sched/signal.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010034#include <linux/sched/mm.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070035#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090036#include <linux/uaccess.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080037#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050038#include <linux/security.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090039
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020040#ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
41#define BINDER_IPC_32BIT 1
42#endif
43
44#include <uapi/linux/android/binder.h>
Todd Kjos0c972a02017-06-29 12:01:41 -070045#include "binder_alloc.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070046#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090047
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070048static DEFINE_MUTEX(binder_main_lock);
Todd Kjosc44b1232017-06-29 12:01:43 -070049
50static HLIST_HEAD(binder_deferred_list);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090051static DEFINE_MUTEX(binder_deferred_lock);
52
Martijn Coenenac4812c2017-02-03 14:40:48 -080053static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090054static HLIST_HEAD(binder_procs);
Todd Kjosc44b1232017-06-29 12:01:43 -070055static DEFINE_MUTEX(binder_procs_lock);
56
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090057static HLIST_HEAD(binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -070058static DEFINE_SPINLOCK(binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090059
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070060static struct dentry *binder_debugfs_dir_entry_root;
61static struct dentry *binder_debugfs_dir_entry_proc;
Todd Kjos656a8002017-06-29 12:01:45 -070062static atomic_t binder_last_id;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090063
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070064#define BINDER_DEBUG_ENTRY(name) \
65static int binder_##name##_open(struct inode *inode, struct file *file) \
66{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070067 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070068} \
69\
70static const struct file_operations binder_##name##_fops = { \
71 .owner = THIS_MODULE, \
72 .open = binder_##name##_open, \
73 .read = seq_read, \
74 .llseek = seq_lseek, \
75 .release = single_release, \
76}
77
78static int binder_proc_show(struct seq_file *m, void *unused);
79BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090080
81/* This is only defined in include/asm-arm/sizes.h */
82#ifndef SZ_1K
83#define SZ_1K 0x400
84#endif
85
86#ifndef SZ_4M
87#define SZ_4M 0x400000
88#endif
89
90#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
91
92#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
93
94enum {
95 BINDER_DEBUG_USER_ERROR = 1U << 0,
96 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
97 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
98 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
99 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
100 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
101 BINDER_DEBUG_READ_WRITE = 1U << 6,
102 BINDER_DEBUG_USER_REFS = 1U << 7,
103 BINDER_DEBUG_THREADS = 1U << 8,
104 BINDER_DEBUG_TRANSACTION = 1U << 9,
105 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
106 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
107 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
Todd Kjos19c98722017-06-29 12:01:40 -0700108 BINDER_DEBUG_PRIORITY_CAP = 1U << 13,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900109};
110static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
111 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
112module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
113
Martijn Coenenac4812c2017-02-03 14:40:48 -0800114static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
115module_param_named(devices, binder_devices_param, charp, 0444);
116
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900117static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
118static int binder_stop_on_user_error;
119
120static int binder_set_stop_on_user_error(const char *val,
121 struct kernel_param *kp)
122{
123 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900124
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900125 ret = param_set_int(val, kp);
126 if (binder_stop_on_user_error < 2)
127 wake_up(&binder_user_error_wait);
128 return ret;
129}
130module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
131 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
132
133#define binder_debug(mask, x...) \
134 do { \
135 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400136 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900137 } while (0)
138
139#define binder_user_error(x...) \
140 do { \
141 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400142 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900143 if (binder_stop_on_user_error) \
144 binder_stop_on_user_error = 2; \
145 } while (0)
146
Martijn Coenenfeba3902017-02-03 14:40:45 -0800147#define to_flat_binder_object(hdr) \
148 container_of(hdr, struct flat_binder_object, hdr)
149
150#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
151
Martijn Coenen79802402017-02-03 14:40:51 -0800152#define to_binder_buffer_object(hdr) \
153 container_of(hdr, struct binder_buffer_object, hdr)
154
Martijn Coenendef95c72017-02-03 14:40:52 -0800155#define to_binder_fd_array_object(hdr) \
156 container_of(hdr, struct binder_fd_array_object, hdr)
157
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900158enum binder_stat_types {
159 BINDER_STAT_PROC,
160 BINDER_STAT_THREAD,
161 BINDER_STAT_NODE,
162 BINDER_STAT_REF,
163 BINDER_STAT_DEATH,
164 BINDER_STAT_TRANSACTION,
165 BINDER_STAT_TRANSACTION_COMPLETE,
166 BINDER_STAT_COUNT
167};
168
169struct binder_stats {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700170 atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1];
171 atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1];
172 atomic_t obj_created[BINDER_STAT_COUNT];
173 atomic_t obj_deleted[BINDER_STAT_COUNT];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900174};
175
176static struct binder_stats binder_stats;
177
178static inline void binder_stats_deleted(enum binder_stat_types type)
179{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700180 atomic_inc(&binder_stats.obj_deleted[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900181}
182
183static inline void binder_stats_created(enum binder_stat_types type)
184{
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -0700185 atomic_inc(&binder_stats.obj_created[type]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900186}
187
188struct binder_transaction_log_entry {
189 int debug_id;
190 int call_type;
191 int from_proc;
192 int from_thread;
193 int target_handle;
194 int to_proc;
195 int to_thread;
196 int to_node;
197 int data_size;
198 int offsets_size;
Todd Kjos57ada2f2017-06-29 12:01:46 -0700199 int return_error_line;
200 uint32_t return_error;
201 uint32_t return_error_param;
Martijn Coenen14db3182017-02-03 14:40:47 -0800202 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900203};
204struct binder_transaction_log {
205 int next;
206 int full;
207 struct binder_transaction_log_entry entry[32];
208};
209static struct binder_transaction_log binder_transaction_log;
210static struct binder_transaction_log binder_transaction_log_failed;
211
212static struct binder_transaction_log_entry *binder_transaction_log_add(
213 struct binder_transaction_log *log)
214{
215 struct binder_transaction_log_entry *e;
Seunghun Lee10f62862014-05-01 01:30:23 +0900216
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900217 e = &log->entry[log->next];
218 memset(e, 0, sizeof(*e));
219 log->next++;
220 if (log->next == ARRAY_SIZE(log->entry)) {
221 log->next = 0;
222 log->full = 1;
223 }
224 return e;
225}
226
Martijn Coenen342e5c92017-02-03 14:40:46 -0800227struct binder_context {
228 struct binder_node *binder_context_mgr_node;
Todd Kjosc44b1232017-06-29 12:01:43 -0700229 struct mutex context_mgr_node_lock;
230
Martijn Coenen342e5c92017-02-03 14:40:46 -0800231 kuid_t binder_context_mgr_uid;
Martijn Coenen14db3182017-02-03 14:40:47 -0800232 const char *name;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800233};
234
Martijn Coenenac4812c2017-02-03 14:40:48 -0800235struct binder_device {
236 struct hlist_node hlist;
237 struct miscdevice miscdev;
238 struct binder_context context;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800239};
240
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900241struct binder_work {
242 struct list_head entry;
243 enum {
244 BINDER_WORK_TRANSACTION = 1,
245 BINDER_WORK_TRANSACTION_COMPLETE,
246 BINDER_WORK_NODE,
247 BINDER_WORK_DEAD_BINDER,
248 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
249 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
250 } type;
251};
252
253struct binder_node {
254 int debug_id;
255 struct binder_work work;
256 union {
257 struct rb_node rb_node;
258 struct hlist_node dead_node;
259 };
260 struct binder_proc *proc;
261 struct hlist_head refs;
262 int internal_strong_refs;
263 int local_weak_refs;
264 int local_strong_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800265 binder_uintptr_t ptr;
266 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900267 unsigned has_strong_ref:1;
268 unsigned pending_strong_ref:1;
269 unsigned has_weak_ref:1;
270 unsigned pending_weak_ref:1;
271 unsigned has_async_transaction:1;
272 unsigned accept_fds:1;
273 unsigned min_priority:8;
274 struct list_head async_todo;
275};
276
277struct binder_ref_death {
278 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800279 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900280};
281
282struct binder_ref {
283 /* Lookups needed: */
284 /* node + proc => ref (transaction) */
285 /* desc + proc => ref (transaction, inc/dec ref) */
286 /* node => refs + procs (proc exit) */
287 int debug_id;
288 struct rb_node rb_node_desc;
289 struct rb_node rb_node_node;
290 struct hlist_node node_entry;
291 struct binder_proc *proc;
292 struct binder_node *node;
293 uint32_t desc;
294 int strong;
295 int weak;
296 struct binder_ref_death *death;
297};
298
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900299enum binder_deferred_state {
300 BINDER_DEFERRED_PUT_FILES = 0x01,
301 BINDER_DEFERRED_FLUSH = 0x02,
302 BINDER_DEFERRED_RELEASE = 0x04,
303};
304
305struct binder_proc {
306 struct hlist_node proc_node;
307 struct rb_root threads;
308 struct rb_root nodes;
309 struct rb_root refs_by_desc;
310 struct rb_root refs_by_node;
311 int pid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900312 struct task_struct *tsk;
313 struct files_struct *files;
314 struct hlist_node deferred_work_node;
315 int deferred_work;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900316
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900317 struct list_head todo;
318 wait_queue_head_t wait;
319 struct binder_stats stats;
320 struct list_head delivered_death;
321 int max_threads;
322 int requested_threads;
323 int requested_threads_started;
324 int ready_threads;
325 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700326 struct dentry *debugfs_entry;
Todd Kjosfdfb4a92017-06-29 12:01:38 -0700327 struct binder_alloc alloc;
Martijn Coenen342e5c92017-02-03 14:40:46 -0800328 struct binder_context *context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900329};
330
331enum {
332 BINDER_LOOPER_STATE_REGISTERED = 0x01,
333 BINDER_LOOPER_STATE_ENTERED = 0x02,
334 BINDER_LOOPER_STATE_EXITED = 0x04,
335 BINDER_LOOPER_STATE_INVALID = 0x08,
336 BINDER_LOOPER_STATE_WAITING = 0x10,
337 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
338};
339
340struct binder_thread {
341 struct binder_proc *proc;
342 struct rb_node rb_node;
343 int pid;
344 int looper;
345 struct binder_transaction *transaction_stack;
346 struct list_head todo;
347 uint32_t return_error; /* Write failed, return error code in read buf */
348 uint32_t return_error2; /* Write failed, return error code in read */
349 /* buffer. Used when sending a reply to a dead process that */
350 /* we are also waiting on */
351 wait_queue_head_t wait;
352 struct binder_stats stats;
353};
354
355struct binder_transaction {
356 int debug_id;
357 struct binder_work work;
358 struct binder_thread *from;
359 struct binder_transaction *from_parent;
360 struct binder_proc *to_proc;
361 struct binder_thread *to_thread;
362 struct binder_transaction *to_parent;
363 unsigned need_reply:1;
364 /* unsigned is_dead:1; */ /* not used at the moment */
365
366 struct binder_buffer *buffer;
367 unsigned int code;
368 unsigned int flags;
369 long priority;
370 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600371 kuid_t sender_euid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900372};
373
374static void
375binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
376
Sachin Kamatefde99c2012-08-17 16:39:36 +0530377static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900378{
379 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900380 unsigned long rlim_cur;
381 unsigned long irqs;
382
383 if (files == NULL)
384 return -ESRCH;
385
Al Virodcfadfa2012-08-12 17:27:30 -0400386 if (!lock_task_sighand(proc->tsk, &irqs))
387 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900388
Al Virodcfadfa2012-08-12 17:27:30 -0400389 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
390 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900391
Al Virodcfadfa2012-08-12 17:27:30 -0400392 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900393}
394
395/*
396 * copied from fd_install
397 */
398static void task_fd_install(
399 struct binder_proc *proc, unsigned int fd, struct file *file)
400{
Al Virof869e8a2012-08-15 21:06:33 -0400401 if (proc->files)
402 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900403}
404
405/*
406 * copied from sys_close
407 */
408static long task_close_fd(struct binder_proc *proc, unsigned int fd)
409{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900410 int retval;
411
Al Viro483ce1d2012-08-19 12:04:24 -0400412 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900413 return -ESRCH;
414
Al Viro483ce1d2012-08-19 12:04:24 -0400415 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900416 /* can't restart close syscall because file table entry was cleared */
417 if (unlikely(retval == -ERESTARTSYS ||
418 retval == -ERESTARTNOINTR ||
419 retval == -ERESTARTNOHAND ||
420 retval == -ERESTART_RESTARTBLOCK))
421 retval = -EINTR;
422
423 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900424}
425
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700426static inline void binder_lock(const char *tag)
427{
428 trace_binder_lock(tag);
429 mutex_lock(&binder_main_lock);
430 trace_binder_locked(tag);
431}
432
433static inline void binder_unlock(const char *tag)
434{
435 trace_binder_unlock(tag);
436 mutex_unlock(&binder_main_lock);
437}
438
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900439static void binder_set_nice(long nice)
440{
441 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900442
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900443 if (can_nice(current, nice)) {
444 set_user_nice(current, nice);
445 return;
446 }
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900447 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900448 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530449 "%d: nice value %ld not allowed use %ld instead\n",
450 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900451 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +0800452 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900453 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530454 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900455}
456
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900457static struct binder_node *binder_get_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800458 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900459{
460 struct rb_node *n = proc->nodes.rb_node;
461 struct binder_node *node;
462
463 while (n) {
464 node = rb_entry(n, struct binder_node, rb_node);
465
466 if (ptr < node->ptr)
467 n = n->rb_left;
468 else if (ptr > node->ptr)
469 n = n->rb_right;
470 else
471 return node;
472 }
473 return NULL;
474}
475
476static struct binder_node *binder_new_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800477 binder_uintptr_t ptr,
478 binder_uintptr_t cookie)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900479{
480 struct rb_node **p = &proc->nodes.rb_node;
481 struct rb_node *parent = NULL;
482 struct binder_node *node;
483
484 while (*p) {
485 parent = *p;
486 node = rb_entry(parent, struct binder_node, rb_node);
487
488 if (ptr < node->ptr)
489 p = &(*p)->rb_left;
490 else if (ptr > node->ptr)
491 p = &(*p)->rb_right;
492 else
493 return NULL;
494 }
495
496 node = kzalloc(sizeof(*node), GFP_KERNEL);
497 if (node == NULL)
498 return NULL;
499 binder_stats_created(BINDER_STAT_NODE);
500 rb_link_node(&node->rb_node, parent, p);
501 rb_insert_color(&node->rb_node, &proc->nodes);
Todd Kjos656a8002017-06-29 12:01:45 -0700502 node->debug_id = atomic_inc_return(&binder_last_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900503 node->proc = proc;
504 node->ptr = ptr;
505 node->cookie = cookie;
506 node->work.type = BINDER_WORK_NODE;
507 INIT_LIST_HEAD(&node->work.entry);
508 INIT_LIST_HEAD(&node->async_todo);
509 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800510 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900511 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800512 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900513 return node;
514}
515
516static int binder_inc_node(struct binder_node *node, int strong, int internal,
517 struct list_head *target_list)
518{
519 if (strong) {
520 if (internal) {
521 if (target_list == NULL &&
522 node->internal_strong_refs == 0 &&
Martijn Coenen342e5c92017-02-03 14:40:46 -0800523 !(node->proc &&
524 node == node->proc->context->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 Kjosc44b1232017-06-29 12:01:43 -0700582 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900583 hlist_del(&node->dead_node);
Todd Kjosc44b1232017-06-29 12:01:43 -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 Coenen342e5c92017-02-03 14:40:46 -0800628 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 Kjos656a8002017-06-29 12:01:45 -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 Coenen342e5c92017-02-03 14:40:46 -0800651 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 Coenenfeba3902017-02-03 14:40:45 -0800838/**
839 * binder_validate_object() - checks for a valid metadata object in a buffer.
840 * @buffer: binder_buffer that we're parsing.
841 * @offset: offset in the buffer at which to validate an object.
842 *
843 * Return: If there's a valid metadata object at @offset in @buffer, the
844 * size of that object. Otherwise, it returns zero.
845 */
846static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
847{
848 /* Check if we can read a header first */
849 struct binder_object_header *hdr;
850 size_t object_size = 0;
851
852 if (offset > buffer->data_size - sizeof(*hdr) ||
853 buffer->data_size < sizeof(*hdr) ||
854 !IS_ALIGNED(offset, sizeof(u32)))
855 return 0;
856
857 /* Ok, now see if we can read a complete object. */
858 hdr = (struct binder_object_header *)(buffer->data + offset);
859 switch (hdr->type) {
860 case BINDER_TYPE_BINDER:
861 case BINDER_TYPE_WEAK_BINDER:
862 case BINDER_TYPE_HANDLE:
863 case BINDER_TYPE_WEAK_HANDLE:
864 object_size = sizeof(struct flat_binder_object);
865 break;
866 case BINDER_TYPE_FD:
867 object_size = sizeof(struct binder_fd_object);
868 break;
Martijn Coenen79802402017-02-03 14:40:51 -0800869 case BINDER_TYPE_PTR:
870 object_size = sizeof(struct binder_buffer_object);
871 break;
Martijn Coenendef95c72017-02-03 14:40:52 -0800872 case BINDER_TYPE_FDA:
873 object_size = sizeof(struct binder_fd_array_object);
874 break;
Martijn Coenenfeba3902017-02-03 14:40:45 -0800875 default:
876 return 0;
877 }
878 if (offset <= buffer->data_size - object_size &&
879 buffer->data_size >= object_size)
880 return object_size;
881 else
882 return 0;
883}
884
Martijn Coenen79802402017-02-03 14:40:51 -0800885/**
886 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
887 * @b: binder_buffer containing the object
888 * @index: index in offset array at which the binder_buffer_object is
889 * located
890 * @start: points to the start of the offset array
891 * @num_valid: the number of valid offsets in the offset array
892 *
893 * Return: If @index is within the valid range of the offset array
894 * described by @start and @num_valid, and if there's a valid
895 * binder_buffer_object at the offset found in index @index
896 * of the offset array, that object is returned. Otherwise,
897 * %NULL is returned.
898 * Note that the offset found in index @index itself is not
899 * verified; this function assumes that @num_valid elements
900 * from @start were previously verified to have valid offsets.
901 */
902static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
903 binder_size_t index,
904 binder_size_t *start,
905 binder_size_t num_valid)
906{
907 struct binder_buffer_object *buffer_obj;
908 binder_size_t *offp;
909
910 if (index >= num_valid)
911 return NULL;
912
913 offp = start + index;
914 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
915 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
916 return NULL;
917
918 return buffer_obj;
919}
920
921/**
922 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
923 * @b: transaction buffer
924 * @objects_start start of objects buffer
925 * @buffer: binder_buffer_object in which to fix up
926 * @offset: start offset in @buffer to fix up
927 * @last_obj: last binder_buffer_object that we fixed up in
928 * @last_min_offset: minimum fixup offset in @last_obj
929 *
930 * Return: %true if a fixup in buffer @buffer at offset @offset is
931 * allowed.
932 *
933 * For safety reasons, we only allow fixups inside a buffer to happen
934 * at increasing offsets; additionally, we only allow fixup on the last
935 * buffer object that was verified, or one of its parents.
936 *
937 * Example of what is allowed:
938 *
939 * A
940 * B (parent = A, offset = 0)
941 * C (parent = A, offset = 16)
942 * D (parent = C, offset = 0)
943 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
944 *
945 * Examples of what is not allowed:
946 *
947 * Decreasing offsets within the same parent:
948 * A
949 * C (parent = A, offset = 16)
950 * B (parent = A, offset = 0) // decreasing offset within A
951 *
952 * Referring to a parent that wasn't the last object or any of its parents:
953 * A
954 * B (parent = A, offset = 0)
955 * C (parent = A, offset = 0)
956 * C (parent = A, offset = 16)
957 * D (parent = B, offset = 0) // B is not A or any of A's parents
958 */
959static bool binder_validate_fixup(struct binder_buffer *b,
960 binder_size_t *objects_start,
961 struct binder_buffer_object *buffer,
962 binder_size_t fixup_offset,
963 struct binder_buffer_object *last_obj,
964 binder_size_t last_min_offset)
965{
966 if (!last_obj) {
967 /* Nothing to fix up in */
968 return false;
969 }
970
971 while (last_obj != buffer) {
972 /*
973 * Safe to retrieve the parent of last_obj, since it
974 * was already previously verified by the driver.
975 */
976 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
977 return false;
978 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
979 last_obj = (struct binder_buffer_object *)
980 (b->data + *(objects_start + last_obj->parent));
981 }
982 return (fixup_offset >= last_min_offset);
983}
984
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900985static void binder_transaction_buffer_release(struct binder_proc *proc,
986 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800987 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900988{
Martijn Coenen79802402017-02-03 14:40:51 -0800989 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900990 int debug_id = buffer->debug_id;
991
992 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530993 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900994 proc->pid, buffer->debug_id,
995 buffer->data_size, buffer->offsets_size, failed_at);
996
997 if (buffer->target_node)
998 binder_dec_node(buffer->target_node, 1, 0);
999
Martijn Coenen79802402017-02-03 14:40:51 -08001000 off_start = (binder_size_t *)(buffer->data +
1001 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001002 if (failed_at)
1003 off_end = failed_at;
1004 else
Martijn Coenen79802402017-02-03 14:40:51 -08001005 off_end = (void *)off_start + buffer->offsets_size;
1006 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001007 struct binder_object_header *hdr;
1008 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09001009
Martijn Coenenfeba3902017-02-03 14:40:45 -08001010 if (object_size == 0) {
1011 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001012 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001013 continue;
1014 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08001015 hdr = (struct binder_object_header *)(buffer->data + *offp);
1016 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001017 case BINDER_TYPE_BINDER:
1018 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001019 struct flat_binder_object *fp;
1020 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09001021
Martijn Coenenfeba3902017-02-03 14:40:45 -08001022 fp = to_flat_binder_object(hdr);
1023 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001024 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001025 pr_err("transaction release %d bad node %016llx\n",
1026 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001027 break;
1028 }
1029 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001030 " node %d u%016llx\n",
1031 node->debug_id, (u64)node->ptr);
Martijn Coenenfeba3902017-02-03 14:40:45 -08001032 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
1033 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001034 } break;
1035 case BINDER_TYPE_HANDLE:
1036 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001037 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001038 struct binder_ref *ref;
1039
Martijn Coenenfeba3902017-02-03 14:40:45 -08001040 fp = to_flat_binder_object(hdr);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001041 ref = binder_get_ref(proc, fp->handle,
Martijn Coenenfeba3902017-02-03 14:40:45 -08001042 hdr->type == BINDER_TYPE_HANDLE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001043 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001044 pr_err("transaction release %d bad handle %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301045 debug_id, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001046 break;
1047 }
1048 binder_debug(BINDER_DEBUG_TRANSACTION,
1049 " ref %d desc %d (node %d)\n",
1050 ref->debug_id, ref->desc, ref->node->debug_id);
Martijn Coenenfeba3902017-02-03 14:40:45 -08001051 binder_dec_ref(ref, hdr->type == BINDER_TYPE_HANDLE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001052 } break;
1053
Martijn Coenenfeba3902017-02-03 14:40:45 -08001054 case BINDER_TYPE_FD: {
1055 struct binder_fd_object *fp = to_binder_fd_object(hdr);
1056
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001057 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenenfeba3902017-02-03 14:40:45 -08001058 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001059 if (failed_at)
Martijn Coenenfeba3902017-02-03 14:40:45 -08001060 task_close_fd(proc, fp->fd);
1061 } break;
Martijn Coenen79802402017-02-03 14:40:51 -08001062 case BINDER_TYPE_PTR:
1063 /*
1064 * Nothing to do here, this will get cleaned up when the
1065 * transaction buffer gets freed
1066 */
1067 break;
Martijn Coenendef95c72017-02-03 14:40:52 -08001068 case BINDER_TYPE_FDA: {
1069 struct binder_fd_array_object *fda;
1070 struct binder_buffer_object *parent;
1071 uintptr_t parent_buffer;
1072 u32 *fd_array;
1073 size_t fd_index;
1074 binder_size_t fd_buf_size;
1075
1076 fda = to_binder_fd_array_object(hdr);
1077 parent = binder_validate_ptr(buffer, fda->parent,
1078 off_start,
1079 offp - off_start);
1080 if (!parent) {
1081 pr_err("transaction release %d bad parent offset",
1082 debug_id);
1083 continue;
1084 }
1085 /*
1086 * Since the parent was already fixed up, convert it
1087 * back to kernel address space to access it
1088 */
1089 parent_buffer = parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07001090 binder_alloc_get_user_buffer_offset(
1091 &proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08001092
1093 fd_buf_size = sizeof(u32) * fda->num_fds;
1094 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
1095 pr_err("transaction release %d invalid number of fds (%lld)\n",
1096 debug_id, (u64)fda->num_fds);
1097 continue;
1098 }
1099 if (fd_buf_size > parent->length ||
1100 fda->parent_offset > parent->length - fd_buf_size) {
1101 /* No space for all file descriptors here. */
1102 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
1103 debug_id, (u64)fda->num_fds);
1104 continue;
1105 }
1106 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
1107 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
1108 task_close_fd(proc, fd_array[fd_index]);
1109 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001110 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001111 pr_err("transaction release %d bad object type %x\n",
Martijn Coenenfeba3902017-02-03 14:40:45 -08001112 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001113 break;
1114 }
1115 }
1116}
1117
Martijn Coenena056af42017-02-03 14:40:49 -08001118static int binder_translate_binder(struct flat_binder_object *fp,
1119 struct binder_transaction *t,
1120 struct binder_thread *thread)
1121{
1122 struct binder_node *node;
1123 struct binder_ref *ref;
1124 struct binder_proc *proc = thread->proc;
1125 struct binder_proc *target_proc = t->to_proc;
1126
1127 node = binder_get_node(proc, fp->binder);
1128 if (!node) {
1129 node = binder_new_node(proc, fp->binder, fp->cookie);
1130 if (!node)
1131 return -ENOMEM;
1132
1133 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1134 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1135 }
1136 if (fp->cookie != node->cookie) {
1137 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1138 proc->pid, thread->pid, (u64)fp->binder,
1139 node->debug_id, (u64)fp->cookie,
1140 (u64)node->cookie);
1141 return -EINVAL;
1142 }
1143 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk))
1144 return -EPERM;
1145
1146 ref = binder_get_ref_for_node(target_proc, node);
1147 if (!ref)
Todd Kjos57ada2f2017-06-29 12:01:46 -07001148 return -ENOMEM;
Martijn Coenena056af42017-02-03 14:40:49 -08001149
1150 if (fp->hdr.type == BINDER_TYPE_BINDER)
1151 fp->hdr.type = BINDER_TYPE_HANDLE;
1152 else
1153 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
1154 fp->binder = 0;
1155 fp->handle = ref->desc;
1156 fp->cookie = 0;
1157 binder_inc_ref(ref, fp->hdr.type == BINDER_TYPE_HANDLE, &thread->todo);
1158
1159 trace_binder_transaction_node_to_ref(t, node, ref);
1160 binder_debug(BINDER_DEBUG_TRANSACTION,
1161 " node %d u%016llx -> ref %d desc %d\n",
1162 node->debug_id, (u64)node->ptr,
1163 ref->debug_id, ref->desc);
1164
1165 return 0;
1166}
1167
1168static int binder_translate_handle(struct flat_binder_object *fp,
1169 struct binder_transaction *t,
1170 struct binder_thread *thread)
1171{
1172 struct binder_ref *ref;
1173 struct binder_proc *proc = thread->proc;
1174 struct binder_proc *target_proc = t->to_proc;
1175
1176 ref = binder_get_ref(proc, fp->handle,
1177 fp->hdr.type == BINDER_TYPE_HANDLE);
1178 if (!ref) {
1179 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
1180 proc->pid, thread->pid, fp->handle);
1181 return -EINVAL;
1182 }
1183 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk))
1184 return -EPERM;
1185
1186 if (ref->node->proc == target_proc) {
1187 if (fp->hdr.type == BINDER_TYPE_HANDLE)
1188 fp->hdr.type = BINDER_TYPE_BINDER;
1189 else
1190 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
1191 fp->binder = ref->node->ptr;
1192 fp->cookie = ref->node->cookie;
1193 binder_inc_node(ref->node, fp->hdr.type == BINDER_TYPE_BINDER,
1194 0, NULL);
1195 trace_binder_transaction_ref_to_node(t, ref);
1196 binder_debug(BINDER_DEBUG_TRANSACTION,
1197 " ref %d desc %d -> node %d u%016llx\n",
1198 ref->debug_id, ref->desc, ref->node->debug_id,
1199 (u64)ref->node->ptr);
1200 } else {
1201 struct binder_ref *new_ref;
1202
1203 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1204 if (!new_ref)
Todd Kjos57ada2f2017-06-29 12:01:46 -07001205 return -ENOMEM;
Martijn Coenena056af42017-02-03 14:40:49 -08001206
1207 fp->binder = 0;
1208 fp->handle = new_ref->desc;
1209 fp->cookie = 0;
1210 binder_inc_ref(new_ref, fp->hdr.type == BINDER_TYPE_HANDLE,
1211 NULL);
1212 trace_binder_transaction_ref_to_ref(t, ref, new_ref);
1213 binder_debug(BINDER_DEBUG_TRANSACTION,
1214 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1215 ref->debug_id, ref->desc, new_ref->debug_id,
1216 new_ref->desc, ref->node->debug_id);
1217 }
1218 return 0;
1219}
1220
1221static int binder_translate_fd(int fd,
1222 struct binder_transaction *t,
1223 struct binder_thread *thread,
1224 struct binder_transaction *in_reply_to)
1225{
1226 struct binder_proc *proc = thread->proc;
1227 struct binder_proc *target_proc = t->to_proc;
1228 int target_fd;
1229 struct file *file;
1230 int ret;
1231 bool target_allows_fd;
1232
1233 if (in_reply_to)
1234 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
1235 else
1236 target_allows_fd = t->buffer->target_node->accept_fds;
1237 if (!target_allows_fd) {
1238 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
1239 proc->pid, thread->pid,
1240 in_reply_to ? "reply" : "transaction",
1241 fd);
1242 ret = -EPERM;
1243 goto err_fd_not_accepted;
1244 }
1245
1246 file = fget(fd);
1247 if (!file) {
1248 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1249 proc->pid, thread->pid, fd);
1250 ret = -EBADF;
1251 goto err_fget;
1252 }
1253 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
1254 if (ret < 0) {
1255 ret = -EPERM;
1256 goto err_security;
1257 }
1258
1259 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1260 if (target_fd < 0) {
1261 ret = -ENOMEM;
1262 goto err_get_unused_fd;
1263 }
1264 task_fd_install(target_proc, target_fd, file);
1265 trace_binder_transaction_fd(t, fd, target_fd);
1266 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
1267 fd, target_fd);
1268
1269 return target_fd;
1270
1271err_get_unused_fd:
1272err_security:
1273 fput(file);
1274err_fget:
1275err_fd_not_accepted:
1276 return ret;
1277}
1278
Martijn Coenendef95c72017-02-03 14:40:52 -08001279static int binder_translate_fd_array(struct binder_fd_array_object *fda,
1280 struct binder_buffer_object *parent,
1281 struct binder_transaction *t,
1282 struct binder_thread *thread,
1283 struct binder_transaction *in_reply_to)
1284{
1285 binder_size_t fdi, fd_buf_size, num_installed_fds;
1286 int target_fd;
1287 uintptr_t parent_buffer;
1288 u32 *fd_array;
1289 struct binder_proc *proc = thread->proc;
1290 struct binder_proc *target_proc = t->to_proc;
1291
1292 fd_buf_size = sizeof(u32) * fda->num_fds;
1293 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
1294 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
1295 proc->pid, thread->pid, (u64)fda->num_fds);
1296 return -EINVAL;
1297 }
1298 if (fd_buf_size > parent->length ||
1299 fda->parent_offset > parent->length - fd_buf_size) {
1300 /* No space for all file descriptors here. */
1301 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
1302 proc->pid, thread->pid, (u64)fda->num_fds);
1303 return -EINVAL;
1304 }
1305 /*
1306 * Since the parent was already fixed up, convert it
1307 * back to the kernel address space to access it
1308 */
Todd Kjos19c98722017-06-29 12:01:40 -07001309 parent_buffer = parent->buffer -
1310 binder_alloc_get_user_buffer_offset(&target_proc->alloc);
Martijn Coenendef95c72017-02-03 14:40:52 -08001311 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
1312 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
1313 binder_user_error("%d:%d parent offset not aligned correctly.\n",
1314 proc->pid, thread->pid);
1315 return -EINVAL;
1316 }
1317 for (fdi = 0; fdi < fda->num_fds; fdi++) {
1318 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
1319 in_reply_to);
1320 if (target_fd < 0)
1321 goto err_translate_fd_failed;
1322 fd_array[fdi] = target_fd;
1323 }
1324 return 0;
1325
1326err_translate_fd_failed:
1327 /*
1328 * Failed to allocate fd or security error, free fds
1329 * installed so far.
1330 */
1331 num_installed_fds = fdi;
1332 for (fdi = 0; fdi < num_installed_fds; fdi++)
1333 task_close_fd(target_proc, fd_array[fdi]);
1334 return target_fd;
1335}
1336
Martijn Coenen79802402017-02-03 14:40:51 -08001337static int binder_fixup_parent(struct binder_transaction *t,
1338 struct binder_thread *thread,
1339 struct binder_buffer_object *bp,
1340 binder_size_t *off_start,
1341 binder_size_t num_valid,
1342 struct binder_buffer_object *last_fixup_obj,
1343 binder_size_t last_fixup_min_off)
1344{
1345 struct binder_buffer_object *parent;
1346 u8 *parent_buffer;
1347 struct binder_buffer *b = t->buffer;
1348 struct binder_proc *proc = thread->proc;
1349 struct binder_proc *target_proc = t->to_proc;
1350
1351 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
1352 return 0;
1353
1354 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
1355 if (!parent) {
1356 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
1357 proc->pid, thread->pid);
1358 return -EINVAL;
1359 }
1360
1361 if (!binder_validate_fixup(b, off_start,
1362 parent, bp->parent_offset,
1363 last_fixup_obj,
1364 last_fixup_min_off)) {
1365 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
1366 proc->pid, thread->pid);
1367 return -EINVAL;
1368 }
1369
1370 if (parent->length < sizeof(binder_uintptr_t) ||
1371 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
1372 /* No space for a pointer here! */
1373 binder_user_error("%d:%d got transaction with invalid parent offset\n",
1374 proc->pid, thread->pid);
1375 return -EINVAL;
1376 }
1377 parent_buffer = (u8 *)(parent->buffer -
Todd Kjos19c98722017-06-29 12:01:40 -07001378 binder_alloc_get_user_buffer_offset(
1379 &target_proc->alloc));
Martijn Coenen79802402017-02-03 14:40:51 -08001380 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
1381
1382 return 0;
1383}
1384
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001385static void binder_transaction(struct binder_proc *proc,
1386 struct binder_thread *thread,
Martijn Coenen4bfac802017-02-03 14:40:50 -08001387 struct binder_transaction_data *tr, int reply,
1388 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001389{
Martijn Coenena056af42017-02-03 14:40:49 -08001390 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001391 struct binder_transaction *t;
1392 struct binder_work *tcomplete;
Martijn Coenen79802402017-02-03 14:40:51 -08001393 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001394 binder_size_t off_min;
Martijn Coenen79802402017-02-03 14:40:51 -08001395 u8 *sg_bufp, *sg_buf_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001396 struct binder_proc *target_proc;
1397 struct binder_thread *target_thread = NULL;
1398 struct binder_node *target_node = NULL;
1399 struct list_head *target_list;
1400 wait_queue_head_t *target_wait;
1401 struct binder_transaction *in_reply_to = NULL;
1402 struct binder_transaction_log_entry *e;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001403 uint32_t return_error = 0;
1404 uint32_t return_error_param = 0;
1405 uint32_t return_error_line = 0;
Martijn Coenen79802402017-02-03 14:40:51 -08001406 struct binder_buffer_object *last_fixup_obj = NULL;
1407 binder_size_t last_fixup_min_off = 0;
Martijn Coenen342e5c92017-02-03 14:40:46 -08001408 struct binder_context *context = proc->context;
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 Coenen14db3182017-02-03 14:40:47 -08001417 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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjosc44b1232017-06-29 12:01:43 -07001479 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08001480 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 Kjosc44b1232017-06-29 12:01:43 -07001483 mutex_unlock(&context->context_mgr_node_lock);
Todd Kjos57ada2f2017-06-29 12:01:46 -07001484 return_error_line = __LINE__;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001485 goto err_no_context_mgr_node;
1486 }
Todd Kjosc44b1232017-06-29 12:01:43 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjos656a8002017-06-29 12:01:45 -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 Coenen4bfac802017-02-03 14:40:50 -08001559 "%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 Coenen4bfac802017-02-03 14:40:50 -08001564 (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 Coenen4bfac802017-02-03 14:40:50 -08001568 "%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 Coenen4bfac802017-02-03 14:40:50 -08001573 (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 Kjos19c98722017-06-29 12:01:40 -07001589 t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size,
Martijn Coenen4bfac802017-02-03 14:40:50 -08001590 tr->offsets_size, extra_buffers_size,
1591 !reply && (t->flags & TF_ONE_WAY));
Todd Kjos57ada2f2017-06-29 12:01:46 -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 Coenen79802402017-02-03 14:40:51 -08001611 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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Coenen79802402017-02-03 14:40:51 -08001641 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,
1644 (u64)extra_buffers_size);
1645 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001646 return_error_param = -EINVAL;
1647 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08001648 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 Coenenfeba3902017-02-03 14:40:45 -08001655 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 Coenenfeba3902017-02-03 14:40:45 -08001658 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 Coenenfeba3902017-02-03 14:40:45 -08001662 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001663 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -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 Coenenfeba3902017-02-03 14:40:45 -08001668
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 Coenenfeba3902017-02-03 14:40:45 -08001674 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001675
Martijn Coenenfeba3902017-02-03 14:40:45 -08001676 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08001677 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 Kjos57ada2f2017-06-29 12:01:46 -07001680 return_error_param = ret;
1681 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08001682 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 Coenenfeba3902017-02-03 14:40:45 -08001687 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001688
Martijn Coenenfeba3902017-02-03 14:40:45 -08001689 fp = to_flat_binder_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08001690 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 Kjos57ada2f2017-06-29 12:01:46 -07001693 return_error_param = ret;
1694 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08001695 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001696 }
1697 } break;
1698
1699 case BINDER_TYPE_FD: {
Martijn Coenenfeba3902017-02-03 14:40:45 -08001700 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenena056af42017-02-03 14:40:49 -08001701 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 Kjos57ada2f2017-06-29 12:01:46 -07001706 return_error_param = target_fd;
1707 return_error_line = __LINE__;
Martijn Coenena056af42017-02-03 14:40:49 -08001708 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001709 }
Martijn Coenenfeba3902017-02-03 14:40:45 -08001710 fp->pad_binder = 0;
1711 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001712 } break;
Martijn Coenendef95c72017-02-03 14:40:52 -08001713 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 Kjos57ada2f2017-06-29 12:01:46 -07001724 return_error_param = -EINVAL;
1725 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08001726 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 Kjos57ada2f2017-06-29 12:01:46 -07001735 return_error_param = -EINVAL;
1736 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08001737 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 Kjos57ada2f2017-06-29 12:01:46 -07001743 return_error_param = ret;
1744 return_error_line = __LINE__;
Martijn Coenendef95c72017-02-03 14:40:52 -08001745 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 Coenen79802402017-02-03 14:40:51 -08001751 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 Coenen79802402017-02-03 14:40:51 -08001756 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 Kjos57ada2f2017-06-29 12:01:46 -07001760 return_error_param = -EINVAL;
1761 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08001762 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 Kjos57ada2f2017-06-29 12:01:46 -07001769 return_error_param = -EFAULT;
Martijn Coenen79802402017-02-03 14:40:51 -08001770 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -07001771 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08001772 goto err_copy_data_failed;
1773 }
1774 /* Fixup buffer pointer to target proc address space */
1775 bp->buffer = (uintptr_t)sg_bufp +
Todd Kjos19c98722017-06-29 12:01:40 -07001776 binder_alloc_get_user_buffer_offset(
1777 &target_proc->alloc);
Martijn Coenen79802402017-02-03 14:40:51 -08001778 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 Kjos57ada2f2017-06-29 12:01:46 -07001786 return_error_param = ret;
1787 return_error_line = __LINE__;
Martijn Coenen79802402017-02-03 14:40:51 -08001788 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 Coenenfeba3902017-02-03 14:40:45 -08001795 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001796 return_error = BR_FAILED_REPLY;
Todd Kjos57ada2f2017-06-29 12:01:46 -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 Andrews00b40d62017-06-29 12:01:37 -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 Coenena056af42017-02-03 14:40:49 -08001831err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001832err_bad_object_type:
1833err_bad_offset:
Martijn Coenendef95c72017-02-03 14:40:52 -08001834err_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 Kjos19c98722017-06-29 12:01:40 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Kjos57ada2f2017-06-29 12:01:46 -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 Coenen342e5c92017-02-03 14:40:46 -08001882 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 Sridharan0953c792017-06-29 12:01:44 -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 Kjosc44b1232017-06-29 12:01:43 -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 Kjosc44b1232017-06-29 12:01:43 -07001908
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001909 ptr += sizeof(uint32_t);
Todd Kjosc44b1232017-06-29 12:01:43 -07001910 if (target == 0 &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001911 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
Todd Kjosc44b1232017-06-29 12:01:43 -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 Kjosc44b1232017-06-29 12:01:43 -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 Kjos19c98722017-06-29 12:01:40 -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 Kjos19c98722017-06-29 12:01:40 -07002063 binder_alloc_free_buf(&proc->alloc, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002064 break;
2065 }
2066
Martijn Coenen79802402017-02-03 14:40:51 -08002067 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 Coenen4bfac802017-02-03 14:40:50 -08002085 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 Sridharan0953c792017-06-29 12:01:44 -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
Todd Kjos26b47d82017-06-29 12:01:47 -07002288static int binder_put_node_cmd(struct binder_proc *proc,
2289 struct binder_thread *thread,
2290 void __user **ptrp,
2291 binder_uintptr_t node_ptr,
2292 binder_uintptr_t node_cookie,
2293 int node_debug_id,
2294 uint32_t cmd, const char *cmd_name)
2295{
2296 void __user *ptr = *ptrp;
2297
2298 if (put_user(cmd, (uint32_t __user *)ptr))
2299 return -EFAULT;
2300 ptr += sizeof(uint32_t);
2301
2302 if (put_user(node_ptr, (binder_uintptr_t __user *)ptr))
2303 return -EFAULT;
2304 ptr += sizeof(binder_uintptr_t);
2305
2306 if (put_user(node_cookie, (binder_uintptr_t __user *)ptr))
2307 return -EFAULT;
2308 ptr += sizeof(binder_uintptr_t);
2309
2310 binder_stat_br(proc, thread, cmd);
2311 binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n",
2312 proc->pid, thread->pid, cmd_name, node_debug_id,
2313 (u64)node_ptr, (u64)node_cookie);
2314
2315 *ptrp = ptr;
2316 return 0;
2317}
2318
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002319static int binder_thread_read(struct binder_proc *proc,
2320 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002321 binder_uintptr_t binder_buffer, size_t size,
2322 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002323{
Arve Hjønnevågda498892014-02-21 14:40:26 -08002324 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002325 void __user *ptr = buffer + *consumed;
2326 void __user *end = buffer + size;
2327
2328 int ret = 0;
2329 int wait_for_proc_work;
2330
2331 if (*consumed == 0) {
2332 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2333 return -EFAULT;
2334 ptr += sizeof(uint32_t);
2335 }
2336
2337retry:
2338 wait_for_proc_work = thread->transaction_stack == NULL &&
2339 list_empty(&thread->todo);
2340
2341 if (thread->return_error != BR_OK && ptr < end) {
2342 if (thread->return_error2 != BR_OK) {
2343 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2344 return -EFAULT;
2345 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002346 binder_stat_br(proc, thread, thread->return_error2);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002347 if (ptr == end)
2348 goto done;
2349 thread->return_error2 = BR_OK;
2350 }
2351 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2352 return -EFAULT;
2353 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002354 binder_stat_br(proc, thread, thread->return_error);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002355 thread->return_error = BR_OK;
2356 goto done;
2357 }
2358
2359
2360 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2361 if (wait_for_proc_work)
2362 proc->ready_threads++;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002363
2364 binder_unlock(__func__);
2365
2366 trace_binder_wait_for_work(wait_for_proc_work,
2367 !!thread->transaction_stack,
2368 !list_empty(&thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002369 if (wait_for_proc_work) {
2370 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2371 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302372 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 +09002373 proc->pid, thread->pid, thread->looper);
2374 wait_event_interruptible(binder_user_error_wait,
2375 binder_stop_on_user_error < 2);
2376 }
2377 binder_set_nice(proc->default_priority);
2378 if (non_block) {
2379 if (!binder_has_proc_work(proc, thread))
2380 ret = -EAGAIN;
2381 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002382 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002383 } else {
2384 if (non_block) {
2385 if (!binder_has_thread_work(thread))
2386 ret = -EAGAIN;
2387 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002388 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002389 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002390
2391 binder_lock(__func__);
2392
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002393 if (wait_for_proc_work)
2394 proc->ready_threads--;
2395 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2396
2397 if (ret)
2398 return ret;
2399
2400 while (1) {
2401 uint32_t cmd;
2402 struct binder_transaction_data tr;
2403 struct binder_work *w;
2404 struct binder_transaction *t = NULL;
2405
Dmitry Voytik395262a2014-09-08 18:16:34 +04002406 if (!list_empty(&thread->todo)) {
2407 w = list_first_entry(&thread->todo, struct binder_work,
2408 entry);
2409 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2410 w = list_first_entry(&proc->todo, struct binder_work,
2411 entry);
2412 } else {
2413 /* no data added */
2414 if (ptr - buffer == 4 &&
2415 !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002416 goto retry;
2417 break;
2418 }
2419
2420 if (end - ptr < sizeof(tr) + 4)
2421 break;
2422
2423 switch (w->type) {
2424 case BINDER_WORK_TRANSACTION: {
2425 t = container_of(w, struct binder_transaction, work);
2426 } break;
2427 case BINDER_WORK_TRANSACTION_COMPLETE: {
2428 cmd = BR_TRANSACTION_COMPLETE;
2429 if (put_user(cmd, (uint32_t __user *)ptr))
2430 return -EFAULT;
2431 ptr += sizeof(uint32_t);
2432
2433 binder_stat_br(proc, thread, cmd);
2434 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302435 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002436 proc->pid, thread->pid);
2437
2438 list_del(&w->entry);
2439 kfree(w);
2440 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2441 } break;
2442 case BINDER_WORK_NODE: {
2443 struct binder_node *node = container_of(w, struct binder_node, work);
Todd Kjos26b47d82017-06-29 12:01:47 -07002444 int strong, weak;
2445 binder_uintptr_t node_ptr = node->ptr;
2446 binder_uintptr_t node_cookie = node->cookie;
2447 int node_debug_id = node->debug_id;
2448 int has_weak_ref;
2449 int has_strong_ref;
2450 void __user *orig_ptr = ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +09002451
Todd Kjos26b47d82017-06-29 12:01:47 -07002452 BUG_ON(proc != node->proc);
2453 strong = node->internal_strong_refs ||
2454 node->local_strong_refs;
2455 weak = !hlist_empty(&node->refs) ||
2456 node->local_weak_refs || strong;
2457 has_strong_ref = node->has_strong_ref;
2458 has_weak_ref = node->has_weak_ref;
2459
2460 if (weak && !has_weak_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002461 node->has_weak_ref = 1;
2462 node->pending_weak_ref = 1;
2463 node->local_weak_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07002464 }
2465 if (strong && !has_strong_ref) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002466 node->has_strong_ref = 1;
2467 node->pending_strong_ref = 1;
2468 node->local_strong_refs++;
Todd Kjos26b47d82017-06-29 12:01:47 -07002469 }
2470 if (!strong && has_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002471 node->has_strong_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07002472 if (!weak && has_weak_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002473 node->has_weak_ref = 0;
Todd Kjos26b47d82017-06-29 12:01:47 -07002474 list_del(&w->entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002475
Todd Kjos26b47d82017-06-29 12:01:47 -07002476 if (!weak && !strong) {
2477 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2478 "%d:%d node %d u%016llx c%016llx deleted\n",
2479 proc->pid, thread->pid,
2480 node_debug_id,
2481 (u64)node_ptr,
2482 (u64)node_cookie);
2483 rb_erase(&node->rb_node, &proc->nodes);
2484 kfree(node);
2485 binder_stats_deleted(BINDER_STAT_NODE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002486 }
Todd Kjos26b47d82017-06-29 12:01:47 -07002487 if (weak && !has_weak_ref)
2488 ret = binder_put_node_cmd(
2489 proc, thread, &ptr, node_ptr,
2490 node_cookie, node_debug_id,
2491 BR_INCREFS, "BR_INCREFS");
2492 if (!ret && strong && !has_strong_ref)
2493 ret = binder_put_node_cmd(
2494 proc, thread, &ptr, node_ptr,
2495 node_cookie, node_debug_id,
2496 BR_ACQUIRE, "BR_ACQUIRE");
2497 if (!ret && !strong && has_strong_ref)
2498 ret = binder_put_node_cmd(
2499 proc, thread, &ptr, node_ptr,
2500 node_cookie, node_debug_id,
2501 BR_RELEASE, "BR_RELEASE");
2502 if (!ret && !weak && has_weak_ref)
2503 ret = binder_put_node_cmd(
2504 proc, thread, &ptr, node_ptr,
2505 node_cookie, node_debug_id,
2506 BR_DECREFS, "BR_DECREFS");
2507 if (orig_ptr == ptr)
2508 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
2509 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2510 proc->pid, thread->pid,
2511 node_debug_id,
2512 (u64)node_ptr,
2513 (u64)node_cookie);
2514 if (ret)
2515 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002516 } break;
2517 case BINDER_WORK_DEAD_BINDER:
2518 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2519 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2520 struct binder_ref_death *death;
2521 uint32_t cmd;
2522
2523 death = container_of(w, struct binder_ref_death, work);
2524 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2525 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2526 else
2527 cmd = BR_DEAD_BINDER;
2528 if (put_user(cmd, (uint32_t __user *)ptr))
2529 return -EFAULT;
2530 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002531 if (put_user(death->cookie,
2532 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002533 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002534 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002535 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002536 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002537 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002538 proc->pid, thread->pid,
2539 cmd == BR_DEAD_BINDER ?
2540 "BR_DEAD_BINDER" :
2541 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002542 (u64)death->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002543
2544 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2545 list_del(&w->entry);
2546 kfree(death);
2547 binder_stats_deleted(BINDER_STAT_DEATH);
2548 } else
2549 list_move(&w->entry, &proc->delivered_death);
2550 if (cmd == BR_DEAD_BINDER)
2551 goto done; /* DEAD_BINDER notifications can cause transactions */
2552 } break;
2553 }
2554
2555 if (!t)
2556 continue;
2557
2558 BUG_ON(t->buffer == NULL);
2559 if (t->buffer->target_node) {
2560 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002561
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002562 tr.target.ptr = target_node->ptr;
2563 tr.cookie = target_node->cookie;
2564 t->saved_priority = task_nice(current);
2565 if (t->priority < target_node->min_priority &&
2566 !(t->flags & TF_ONE_WAY))
2567 binder_set_nice(t->priority);
2568 else if (!(t->flags & TF_ONE_WAY) ||
2569 t->saved_priority > target_node->min_priority)
2570 binder_set_nice(target_node->min_priority);
2571 cmd = BR_TRANSACTION;
2572 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002573 tr.target.ptr = 0;
2574 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002575 cmd = BR_REPLY;
2576 }
2577 tr.code = t->code;
2578 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002579 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002580
2581 if (t->from) {
2582 struct task_struct *sender = t->from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09002583
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002584 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002585 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002586 } else {
2587 tr.sender_pid = 0;
2588 }
2589
2590 tr.data_size = t->buffer->data_size;
2591 tr.offsets_size = t->buffer->offsets_size;
Todd Kjos19c98722017-06-29 12:01:40 -07002592 tr.data.ptr.buffer = (binder_uintptr_t)
2593 ((uintptr_t)t->buffer->data +
2594 binder_alloc_get_user_buffer_offset(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002595 tr.data.ptr.offsets = tr.data.ptr.buffer +
2596 ALIGN(t->buffer->data_size,
2597 sizeof(void *));
2598
2599 if (put_user(cmd, (uint32_t __user *)ptr))
2600 return -EFAULT;
2601 ptr += sizeof(uint32_t);
2602 if (copy_to_user(ptr, &tr, sizeof(tr)))
2603 return -EFAULT;
2604 ptr += sizeof(tr);
2605
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002606 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002607 binder_stat_br(proc, thread, cmd);
2608 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002609 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002610 proc->pid, thread->pid,
2611 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2612 "BR_REPLY",
2613 t->debug_id, t->from ? t->from->proc->pid : 0,
2614 t->from ? t->from->pid : 0, cmd,
2615 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002616 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002617
2618 list_del(&t->work.entry);
2619 t->buffer->allow_user_free = 1;
2620 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2621 t->to_parent = thread->transaction_stack;
2622 t->to_thread = thread;
2623 thread->transaction_stack = t;
2624 } else {
2625 t->buffer->transaction = NULL;
2626 kfree(t);
2627 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2628 }
2629 break;
2630 }
2631
2632done:
2633
2634 *consumed = ptr - buffer;
2635 if (proc->requested_threads + proc->ready_threads == 0 &&
2636 proc->requested_threads_started < proc->max_threads &&
2637 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2638 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2639 /*spawn a new thread if we leave this out */) {
2640 proc->requested_threads++;
2641 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302642 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002643 proc->pid, thread->pid);
2644 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2645 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002646 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002647 }
2648 return 0;
2649}
2650
2651static void binder_release_work(struct list_head *list)
2652{
2653 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09002654
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002655 while (!list_empty(list)) {
2656 w = list_first_entry(list, struct binder_work, entry);
2657 list_del_init(&w->entry);
2658 switch (w->type) {
2659 case BINDER_WORK_TRANSACTION: {
2660 struct binder_transaction *t;
2661
2662 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002663 if (t->buffer->target_node &&
2664 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002665 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002666 } else {
2667 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302668 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002669 t->debug_id);
2670 t->buffer->transaction = NULL;
2671 kfree(t);
2672 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2673 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002674 } break;
2675 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002676 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302677 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002678 kfree(w);
2679 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2680 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002681 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2682 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2683 struct binder_ref_death *death;
2684
2685 death = container_of(w, struct binder_ref_death, work);
2686 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002687 "undelivered death notification, %016llx\n",
2688 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002689 kfree(death);
2690 binder_stats_deleted(BINDER_STAT_DEATH);
2691 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002692 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302693 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002694 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002695 break;
2696 }
2697 }
2698
2699}
2700
2701static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2702{
2703 struct binder_thread *thread = NULL;
2704 struct rb_node *parent = NULL;
2705 struct rb_node **p = &proc->threads.rb_node;
2706
2707 while (*p) {
2708 parent = *p;
2709 thread = rb_entry(parent, struct binder_thread, rb_node);
2710
2711 if (current->pid < thread->pid)
2712 p = &(*p)->rb_left;
2713 else if (current->pid > thread->pid)
2714 p = &(*p)->rb_right;
2715 else
2716 break;
2717 }
2718 if (*p == NULL) {
2719 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2720 if (thread == NULL)
2721 return NULL;
2722 binder_stats_created(BINDER_STAT_THREAD);
2723 thread->proc = proc;
2724 thread->pid = current->pid;
2725 init_waitqueue_head(&thread->wait);
2726 INIT_LIST_HEAD(&thread->todo);
2727 rb_link_node(&thread->rb_node, parent, p);
2728 rb_insert_color(&thread->rb_node, &proc->threads);
2729 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2730 thread->return_error = BR_OK;
2731 thread->return_error2 = BR_OK;
2732 }
2733 return thread;
2734}
2735
2736static int binder_free_thread(struct binder_proc *proc,
2737 struct binder_thread *thread)
2738{
2739 struct binder_transaction *t;
2740 struct binder_transaction *send_reply = NULL;
2741 int active_transactions = 0;
2742
2743 rb_erase(&thread->rb_node, &proc->threads);
2744 t = thread->transaction_stack;
2745 if (t && t->to_thread == thread)
2746 send_reply = t;
2747 while (t) {
2748 active_transactions++;
2749 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302750 "release %d:%d transaction %d %s, still active\n",
2751 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002752 t->debug_id,
2753 (t->to_thread == thread) ? "in" : "out");
2754
2755 if (t->to_thread == thread) {
2756 t->to_proc = NULL;
2757 t->to_thread = NULL;
2758 if (t->buffer) {
2759 t->buffer->transaction = NULL;
2760 t->buffer = NULL;
2761 }
2762 t = t->to_parent;
2763 } else if (t->from == thread) {
2764 t->from = NULL;
2765 t = t->from_parent;
2766 } else
2767 BUG();
2768 }
2769 if (send_reply)
2770 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2771 binder_release_work(&thread->todo);
2772 kfree(thread);
2773 binder_stats_deleted(BINDER_STAT_THREAD);
2774 return active_transactions;
2775}
2776
2777static unsigned int binder_poll(struct file *filp,
2778 struct poll_table_struct *wait)
2779{
2780 struct binder_proc *proc = filp->private_data;
2781 struct binder_thread *thread = NULL;
2782 int wait_for_proc_work;
2783
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002784 binder_lock(__func__);
2785
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002786 thread = binder_get_thread(proc);
2787
2788 wait_for_proc_work = thread->transaction_stack == NULL &&
2789 list_empty(&thread->todo) && thread->return_error == BR_OK;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002790
2791 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002792
2793 if (wait_for_proc_work) {
2794 if (binder_has_proc_work(proc, thread))
2795 return POLLIN;
2796 poll_wait(filp, &proc->wait, wait);
2797 if (binder_has_proc_work(proc, thread))
2798 return POLLIN;
2799 } else {
2800 if (binder_has_thread_work(thread))
2801 return POLLIN;
2802 poll_wait(filp, &thread->wait, wait);
2803 if (binder_has_thread_work(thread))
2804 return POLLIN;
2805 }
2806 return 0;
2807}
2808
Tair Rzayev78260ac2014-06-03 22:27:21 +03002809static int binder_ioctl_write_read(struct file *filp,
2810 unsigned int cmd, unsigned long arg,
2811 struct binder_thread *thread)
2812{
2813 int ret = 0;
2814 struct binder_proc *proc = filp->private_data;
2815 unsigned int size = _IOC_SIZE(cmd);
2816 void __user *ubuf = (void __user *)arg;
2817 struct binder_write_read bwr;
2818
2819 if (size != sizeof(struct binder_write_read)) {
2820 ret = -EINVAL;
2821 goto out;
2822 }
2823 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2824 ret = -EFAULT;
2825 goto out;
2826 }
2827 binder_debug(BINDER_DEBUG_READ_WRITE,
2828 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2829 proc->pid, thread->pid,
2830 (u64)bwr.write_size, (u64)bwr.write_buffer,
2831 (u64)bwr.read_size, (u64)bwr.read_buffer);
2832
2833 if (bwr.write_size > 0) {
2834 ret = binder_thread_write(proc, thread,
2835 bwr.write_buffer,
2836 bwr.write_size,
2837 &bwr.write_consumed);
2838 trace_binder_write_done(ret);
2839 if (ret < 0) {
2840 bwr.read_consumed = 0;
2841 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2842 ret = -EFAULT;
2843 goto out;
2844 }
2845 }
2846 if (bwr.read_size > 0) {
2847 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2848 bwr.read_size,
2849 &bwr.read_consumed,
2850 filp->f_flags & O_NONBLOCK);
2851 trace_binder_read_done(ret);
2852 if (!list_empty(&proc->todo))
2853 wake_up_interruptible(&proc->wait);
2854 if (ret < 0) {
2855 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2856 ret = -EFAULT;
2857 goto out;
2858 }
2859 }
2860 binder_debug(BINDER_DEBUG_READ_WRITE,
2861 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2862 proc->pid, thread->pid,
2863 (u64)bwr.write_consumed, (u64)bwr.write_size,
2864 (u64)bwr.read_consumed, (u64)bwr.read_size);
2865 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2866 ret = -EFAULT;
2867 goto out;
2868 }
2869out:
2870 return ret;
2871}
2872
2873static int binder_ioctl_set_ctx_mgr(struct file *filp)
2874{
2875 int ret = 0;
2876 struct binder_proc *proc = filp->private_data;
Martijn Coenen342e5c92017-02-03 14:40:46 -08002877 struct binder_context *context = proc->context;
Todd Kjosc44b1232017-06-29 12:01:43 -07002878 struct binder_node *new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002879 kuid_t curr_euid = current_euid();
2880
Todd Kjosc44b1232017-06-29 12:01:43 -07002881 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08002882 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002883 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2884 ret = -EBUSY;
2885 goto out;
2886 }
Stephen Smalley79af7302015-01-21 10:54:10 -05002887 ret = security_binder_set_context_mgr(proc->tsk);
2888 if (ret < 0)
2889 goto out;
Martijn Coenen342e5c92017-02-03 14:40:46 -08002890 if (uid_valid(context->binder_context_mgr_uid)) {
2891 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002892 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2893 from_kuid(&init_user_ns, curr_euid),
2894 from_kuid(&init_user_ns,
Martijn Coenen342e5c92017-02-03 14:40:46 -08002895 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03002896 ret = -EPERM;
2897 goto out;
2898 }
2899 } else {
Martijn Coenen342e5c92017-02-03 14:40:46 -08002900 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002901 }
Todd Kjosc44b1232017-06-29 12:01:43 -07002902 new_node = binder_new_node(proc, 0, 0);
2903 if (!new_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002904 ret = -ENOMEM;
2905 goto out;
2906 }
Todd Kjosc44b1232017-06-29 12:01:43 -07002907 new_node->local_weak_refs++;
2908 new_node->local_strong_refs++;
2909 new_node->has_strong_ref = 1;
2910 new_node->has_weak_ref = 1;
2911 context->binder_context_mgr_node = new_node;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002912out:
Todd Kjosc44b1232017-06-29 12:01:43 -07002913 mutex_unlock(&context->context_mgr_node_lock);
Tair Rzayev78260ac2014-06-03 22:27:21 +03002914 return ret;
2915}
2916
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002917static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2918{
2919 int ret;
2920 struct binder_proc *proc = filp->private_data;
2921 struct binder_thread *thread;
2922 unsigned int size = _IOC_SIZE(cmd);
2923 void __user *ubuf = (void __user *)arg;
2924
Tair Rzayev78260ac2014-06-03 22:27:21 +03002925 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2926 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002927
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002928 trace_binder_ioctl(cmd, arg);
2929
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002930 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2931 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002932 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002933
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002934 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002935 thread = binder_get_thread(proc);
2936 if (thread == NULL) {
2937 ret = -ENOMEM;
2938 goto err;
2939 }
2940
2941 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002942 case BINDER_WRITE_READ:
2943 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
2944 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002945 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002946 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002947 case BINDER_SET_MAX_THREADS:
2948 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2949 ret = -EINVAL;
2950 goto err;
2951 }
2952 break;
2953 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03002954 ret = binder_ioctl_set_ctx_mgr(filp);
2955 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002956 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002957 break;
2958 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302959 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002960 proc->pid, thread->pid);
2961 binder_free_thread(proc, thread);
2962 thread = NULL;
2963 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002964 case BINDER_VERSION: {
2965 struct binder_version __user *ver = ubuf;
2966
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002967 if (size != sizeof(struct binder_version)) {
2968 ret = -EINVAL;
2969 goto err;
2970 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02002971 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2972 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002973 ret = -EINVAL;
2974 goto err;
2975 }
2976 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002977 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002978 default:
2979 ret = -EINVAL;
2980 goto err;
2981 }
2982 ret = 0;
2983err:
2984 if (thread)
2985 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002986 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002987 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2988 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05302989 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 -07002990err_unlocked:
2991 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002992 return ret;
2993}
2994
2995static void binder_vma_open(struct vm_area_struct *vma)
2996{
2997 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002998
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002999 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303000 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003001 proc->pid, vma->vm_start, vma->vm_end,
3002 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3003 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003004}
3005
3006static void binder_vma_close(struct vm_area_struct *vma)
3007{
3008 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003009
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003010 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303011 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003012 proc->pid, vma->vm_start, vma->vm_end,
3013 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3014 (unsigned long)pgprot_val(vma->vm_page_prot));
Todd Kjos19c98722017-06-29 12:01:40 -07003015 binder_alloc_vma_close(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003016 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
3017}
3018
Dave Jiang11bac802017-02-24 14:56:41 -08003019static int binder_vm_fault(struct vm_fault *vmf)
Vinayak Menonddac7d52014-06-02 18:17:59 +05303020{
3021 return VM_FAULT_SIGBUS;
3022}
3023
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07003024static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003025 .open = binder_vma_open,
3026 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05303027 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003028};
3029
Todd Kjos19c98722017-06-29 12:01:40 -07003030static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
3031{
3032 int ret;
3033 struct binder_proc *proc = filp->private_data;
3034 const char *failure_string;
3035
3036 if (proc->tsk != current->group_leader)
3037 return -EINVAL;
3038
3039 if ((vma->vm_end - vma->vm_start) > SZ_4M)
3040 vma->vm_end = vma->vm_start + SZ_4M;
3041
3042 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3043 "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
3044 __func__, proc->pid, vma->vm_start, vma->vm_end,
3045 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3046 (unsigned long)pgprot_val(vma->vm_page_prot));
3047
3048 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
3049 ret = -EPERM;
3050 failure_string = "bad vm_flags";
3051 goto err_bad_arg;
3052 }
3053 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
3054 vma->vm_ops = &binder_vm_ops;
3055 vma->vm_private_data = proc;
3056
3057 ret = binder_alloc_mmap_handler(&proc->alloc, vma);
3058 if (ret)
3059 return ret;
3060 proc->files = get_files_struct(current);
3061 return 0;
3062
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003063err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04003064 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003065 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
3066 return ret;
3067}
3068
3069static int binder_open(struct inode *nodp, struct file *filp)
3070{
3071 struct binder_proc *proc;
Martijn Coenenac4812c2017-02-03 14:40:48 -08003072 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003073
3074 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
3075 current->group_leader->pid, current->pid);
3076
3077 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
3078 if (proc == NULL)
3079 return -ENOMEM;
Todd Kjosc4ea41b2017-06-29 12:01:36 -07003080 get_task_struct(current->group_leader);
3081 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003082 INIT_LIST_HEAD(&proc->todo);
3083 init_waitqueue_head(&proc->wait);
3084 proc->default_priority = task_nice(current);
Martijn Coenenac4812c2017-02-03 14:40:48 -08003085 binder_dev = container_of(filp->private_data, struct binder_device,
3086 miscdev);
3087 proc->context = &binder_dev->context;
Todd Kjos19c98722017-06-29 12:01:40 -07003088 binder_alloc_init(&proc->alloc);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003089
3090 binder_lock(__func__);
3091
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003092 binder_stats_created(BINDER_STAT_PROC);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003093 proc->pid = current->group_leader->pid;
3094 INIT_LIST_HEAD(&proc->delivered_death);
3095 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003096
3097 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003098
Todd Kjosc44b1232017-06-29 12:01:43 -07003099 mutex_lock(&binder_procs_lock);
3100 hlist_add_head(&proc->proc_node, &binder_procs);
3101 mutex_unlock(&binder_procs_lock);
3102
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003103 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003104 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09003105
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003106 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08003107 /*
3108 * proc debug entries are shared between contexts, so
3109 * this will fail if the process tries to open the driver
3110 * again with a different context. The priting code will
3111 * anyway print all contexts that a given PID has, so this
3112 * is not a problem.
3113 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003114 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen14db3182017-02-03 14:40:47 -08003115 binder_debugfs_dir_entry_proc,
3116 (void *)(unsigned long)proc->pid,
3117 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003118 }
3119
3120 return 0;
3121}
3122
3123static int binder_flush(struct file *filp, fl_owner_t id)
3124{
3125 struct binder_proc *proc = filp->private_data;
3126
3127 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3128
3129 return 0;
3130}
3131
3132static void binder_deferred_flush(struct binder_proc *proc)
3133{
3134 struct rb_node *n;
3135 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09003136
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003137 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3138 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09003139
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003140 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3141 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3142 wake_up_interruptible(&thread->wait);
3143 wake_count++;
3144 }
3145 }
3146 wake_up_interruptible_all(&proc->wait);
3147
3148 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3149 "binder_flush: %d woke %d threads\n", proc->pid,
3150 wake_count);
3151}
3152
3153static int binder_release(struct inode *nodp, struct file *filp)
3154{
3155 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003156
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003157 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003158 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3159
3160 return 0;
3161}
3162
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003163static int binder_node_release(struct binder_node *node, int refs)
3164{
3165 struct binder_ref *ref;
3166 int death = 0;
3167
3168 list_del_init(&node->work.entry);
3169 binder_release_work(&node->async_todo);
3170
3171 if (hlist_empty(&node->refs)) {
3172 kfree(node);
3173 binder_stats_deleted(BINDER_STAT_NODE);
3174
3175 return refs;
3176 }
3177
3178 node->proc = NULL;
3179 node->local_strong_refs = 0;
3180 node->local_weak_refs = 0;
Todd Kjosc44b1232017-06-29 12:01:43 -07003181
3182 spin_lock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003183 hlist_add_head(&node->dead_node, &binder_dead_nodes);
Todd Kjosc44b1232017-06-29 12:01:43 -07003184 spin_unlock(&binder_dead_nodes_lock);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003185
3186 hlist_for_each_entry(ref, &node->refs, node_entry) {
3187 refs++;
3188
3189 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08003190 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003191
3192 death++;
3193
3194 if (list_empty(&ref->death->work.entry)) {
3195 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3196 list_add_tail(&ref->death->work.entry,
3197 &ref->proc->todo);
3198 wake_up_interruptible(&ref->proc->wait);
3199 } else
3200 BUG();
3201 }
3202
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003203 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3204 "node %d now dead, refs %d, death %d\n",
3205 node->debug_id, refs, death);
3206
3207 return refs;
3208}
3209
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003210static void binder_deferred_release(struct binder_proc *proc)
3211{
Martijn Coenen342e5c92017-02-03 14:40:46 -08003212 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003213 struct rb_node *n;
Todd Kjos19c98722017-06-29 12:01:40 -07003214 int threads, nodes, incoming_refs, outgoing_refs, active_transactions;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003215
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003216 BUG_ON(proc->files);
3217
Todd Kjosc44b1232017-06-29 12:01:43 -07003218 mutex_lock(&binder_procs_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003219 hlist_del(&proc->proc_node);
Todd Kjosc44b1232017-06-29 12:01:43 -07003220 mutex_unlock(&binder_procs_lock);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003221
Todd Kjosc44b1232017-06-29 12:01:43 -07003222 mutex_lock(&context->context_mgr_node_lock);
Martijn Coenen342e5c92017-02-03 14:40:46 -08003223 if (context->binder_context_mgr_node &&
3224 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003225 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003226 "%s: %d context_mgr_node gone\n",
3227 __func__, proc->pid);
Martijn Coenen342e5c92017-02-03 14:40:46 -08003228 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003229 }
Todd Kjosc44b1232017-06-29 12:01:43 -07003230 mutex_unlock(&context->context_mgr_node_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003231
3232 threads = 0;
3233 active_transactions = 0;
3234 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003235 struct binder_thread *thread;
3236
3237 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003238 threads++;
3239 active_transactions += binder_free_thread(proc, thread);
3240 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003241
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003242 nodes = 0;
3243 incoming_refs = 0;
3244 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003245 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003246
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003247 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003248 nodes++;
3249 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003250 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003251 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003252
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003253 outgoing_refs = 0;
3254 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003255 struct binder_ref *ref;
3256
3257 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003258 outgoing_refs++;
3259 binder_delete_ref(ref);
3260 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003261
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003262 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003263 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003264
Todd Kjos19c98722017-06-29 12:01:40 -07003265 binder_alloc_deferred_release(&proc->alloc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003266 binder_stats_deleted(BINDER_STAT_PROC);
3267
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003268 put_task_struct(proc->tsk);
3269
3270 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Todd Kjos19c98722017-06-29 12:01:40 -07003271 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003272 __func__, proc->pid, threads, nodes, incoming_refs,
Todd Kjos19c98722017-06-29 12:01:40 -07003273 outgoing_refs, active_transactions);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003274
3275 kfree(proc);
3276}
3277
3278static void binder_deferred_func(struct work_struct *work)
3279{
3280 struct binder_proc *proc;
3281 struct files_struct *files;
3282
3283 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003284
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003285 do {
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003286 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003287 mutex_lock(&binder_deferred_lock);
3288 if (!hlist_empty(&binder_deferred_list)) {
3289 proc = hlist_entry(binder_deferred_list.first,
3290 struct binder_proc, deferred_work_node);
3291 hlist_del_init(&proc->deferred_work_node);
3292 defer = proc->deferred_work;
3293 proc->deferred_work = 0;
3294 } else {
3295 proc = NULL;
3296 defer = 0;
3297 }
3298 mutex_unlock(&binder_deferred_lock);
3299
3300 files = NULL;
3301 if (defer & BINDER_DEFERRED_PUT_FILES) {
3302 files = proc->files;
3303 if (files)
3304 proc->files = NULL;
3305 }
3306
3307 if (defer & BINDER_DEFERRED_FLUSH)
3308 binder_deferred_flush(proc);
3309
3310 if (defer & BINDER_DEFERRED_RELEASE)
3311 binder_deferred_release(proc); /* frees proc */
3312
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003313 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003314 if (files)
3315 put_files_struct(files);
3316 } while (proc);
3317}
3318static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3319
3320static void
3321binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3322{
3323 mutex_lock(&binder_deferred_lock);
3324 proc->deferred_work |= defer;
3325 if (hlist_unhashed(&proc->deferred_work_node)) {
3326 hlist_add_head(&proc->deferred_work_node,
3327 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05303328 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003329 }
3330 mutex_unlock(&binder_deferred_lock);
3331}
3332
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003333static void print_binder_transaction(struct seq_file *m, const char *prefix,
3334 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003335{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003336 seq_printf(m,
3337 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3338 prefix, t->debug_id, t,
3339 t->from ? t->from->proc->pid : 0,
3340 t->from ? t->from->pid : 0,
3341 t->to_proc ? t->to_proc->pid : 0,
3342 t->to_thread ? t->to_thread->pid : 0,
3343 t->code, t->flags, t->priority, t->need_reply);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003344 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003345 seq_puts(m, " buffer free\n");
3346 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003347 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003348 if (t->buffer->target_node)
3349 seq_printf(m, " node %d",
3350 t->buffer->target_node->debug_id);
3351 seq_printf(m, " size %zd:%zd data %p\n",
3352 t->buffer->data_size, t->buffer->offsets_size,
3353 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003354}
3355
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003356static void print_binder_work(struct seq_file *m, const char *prefix,
3357 const char *transaction_prefix,
3358 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003359{
3360 struct binder_node *node;
3361 struct binder_transaction *t;
3362
3363 switch (w->type) {
3364 case BINDER_WORK_TRANSACTION:
3365 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003366 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003367 break;
3368 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003369 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003370 break;
3371 case BINDER_WORK_NODE:
3372 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003373 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3374 prefix, node->debug_id,
3375 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003376 break;
3377 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003378 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003379 break;
3380 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003381 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003382 break;
3383 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003384 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003385 break;
3386 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003387 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003388 break;
3389 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003390}
3391
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003392static void print_binder_thread(struct seq_file *m,
3393 struct binder_thread *thread,
3394 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003395{
3396 struct binder_transaction *t;
3397 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003398 size_t start_pos = m->count;
3399 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003400
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003401 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3402 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003403 t = thread->transaction_stack;
3404 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003405 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003406 print_binder_transaction(m,
3407 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003408 t = t->from_parent;
3409 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003410 print_binder_transaction(m,
3411 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003412 t = t->to_parent;
3413 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003414 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003415 t = NULL;
3416 }
3417 }
3418 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003419 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003420 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003421 if (!print_always && m->count == header_pos)
3422 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003423}
3424
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003425static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003426{
3427 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003428 struct binder_work *w;
3429 int count;
3430
3431 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003432 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003433 count++;
3434
Arve Hjønnevågda498892014-02-21 14:40:26 -08003435 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3436 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003437 node->has_strong_ref, node->has_weak_ref,
3438 node->local_strong_refs, node->local_weak_refs,
3439 node->internal_strong_refs, count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003440 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003441 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003442 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003443 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003444 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003445 seq_puts(m, "\n");
3446 list_for_each_entry(w, &node->async_todo, entry)
3447 print_binder_work(m, " ",
3448 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003449}
3450
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003451static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003452{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003453 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3454 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3455 ref->node->debug_id, ref->strong, ref->weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003456}
3457
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003458static void print_binder_proc(struct seq_file *m,
3459 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003460{
3461 struct binder_work *w;
3462 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003463 size_t start_pos = m->count;
3464 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003465
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003466 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08003467 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003468 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003469
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003470 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3471 print_binder_thread(m, rb_entry(n, struct binder_thread,
3472 rb_node), print_all);
3473 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003474 struct binder_node *node = rb_entry(n, struct binder_node,
3475 rb_node);
3476 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003477 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003478 }
3479 if (print_all) {
3480 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003481 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003482 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003483 print_binder_ref(m, rb_entry(n, struct binder_ref,
3484 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003485 }
Todd Kjos19c98722017-06-29 12:01:40 -07003486 binder_alloc_print_allocated(m, &proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003487 list_for_each_entry(w, &proc->todo, entry)
3488 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003489 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003490 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003491 break;
3492 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003493 if (!print_all && m->count == header_pos)
3494 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003495}
3496
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003497static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003498 "BR_ERROR",
3499 "BR_OK",
3500 "BR_TRANSACTION",
3501 "BR_REPLY",
3502 "BR_ACQUIRE_RESULT",
3503 "BR_DEAD_REPLY",
3504 "BR_TRANSACTION_COMPLETE",
3505 "BR_INCREFS",
3506 "BR_ACQUIRE",
3507 "BR_RELEASE",
3508 "BR_DECREFS",
3509 "BR_ATTEMPT_ACQUIRE",
3510 "BR_NOOP",
3511 "BR_SPAWN_LOOPER",
3512 "BR_FINISHED",
3513 "BR_DEAD_BINDER",
3514 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3515 "BR_FAILED_REPLY"
3516};
3517
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003518static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003519 "BC_TRANSACTION",
3520 "BC_REPLY",
3521 "BC_ACQUIRE_RESULT",
3522 "BC_FREE_BUFFER",
3523 "BC_INCREFS",
3524 "BC_ACQUIRE",
3525 "BC_RELEASE",
3526 "BC_DECREFS",
3527 "BC_INCREFS_DONE",
3528 "BC_ACQUIRE_DONE",
3529 "BC_ATTEMPT_ACQUIRE",
3530 "BC_REGISTER_LOOPER",
3531 "BC_ENTER_LOOPER",
3532 "BC_EXIT_LOOPER",
3533 "BC_REQUEST_DEATH_NOTIFICATION",
3534 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen79802402017-02-03 14:40:51 -08003535 "BC_DEAD_BINDER_DONE",
3536 "BC_TRANSACTION_SG",
3537 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003538};
3539
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003540static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003541 "proc",
3542 "thread",
3543 "node",
3544 "ref",
3545 "death",
3546 "transaction",
3547 "transaction_complete"
3548};
3549
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003550static void print_binder_stats(struct seq_file *m, const char *prefix,
3551 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003552{
3553 int i;
3554
3555 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003556 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003557 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003558 int temp = atomic_read(&stats->bc[i]);
3559
3560 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003561 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003562 binder_command_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003563 }
3564
3565 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003566 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003567 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003568 int temp = atomic_read(&stats->br[i]);
3569
3570 if (temp)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003571 seq_printf(m, "%s%s: %d\n", prefix,
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003572 binder_return_strings[i], temp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003573 }
3574
3575 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003576 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003577 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003578 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003579 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003580 int created = atomic_read(&stats->obj_created[i]);
3581 int deleted = atomic_read(&stats->obj_deleted[i]);
3582
3583 if (created || deleted)
3584 seq_printf(m, "%s%s: active %d total %d\n",
3585 prefix,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003586 binder_objstat_strings[i],
Badhri Jagan Sridharan0953c792017-06-29 12:01:44 -07003587 created - deleted,
3588 created);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003589 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003590}
3591
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003592static void print_binder_proc_stats(struct seq_file *m,
3593 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003594{
3595 struct binder_work *w;
3596 struct rb_node *n;
3597 int count, strong, weak;
3598
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003599 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen14db3182017-02-03 14:40:47 -08003600 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003601 count = 0;
3602 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3603 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003604 seq_printf(m, " threads: %d\n", count);
3605 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003606 " ready threads %d\n"
3607 " free async space %zd\n", proc->requested_threads,
3608 proc->requested_threads_started, proc->max_threads,
Todd Kjos19c98722017-06-29 12:01:40 -07003609 proc->ready_threads,
3610 binder_alloc_get_free_async_space(&proc->alloc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003611 count = 0;
3612 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3613 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003614 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003615 count = 0;
3616 strong = 0;
3617 weak = 0;
3618 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3619 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3620 rb_node_desc);
3621 count++;
3622 strong += ref->strong;
3623 weak += ref->weak;
3624 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003625 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003626
Todd Kjos19c98722017-06-29 12:01:40 -07003627 count = binder_alloc_get_allocated_count(&proc->alloc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003628 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003629
3630 count = 0;
3631 list_for_each_entry(w, &proc->todo, entry) {
3632 switch (w->type) {
3633 case BINDER_WORK_TRANSACTION:
3634 count++;
3635 break;
3636 default:
3637 break;
3638 }
3639 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003640 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003641
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003642 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003643}
3644
3645
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003646static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003647{
3648 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003649 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003650
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003651 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003652
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003653 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003654
Todd Kjosc44b1232017-06-29 12:01:43 -07003655 spin_lock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003656 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003657 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003658 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003659 print_binder_node(m, node);
Todd Kjosc44b1232017-06-29 12:01:43 -07003660 spin_unlock(&binder_dead_nodes_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003661
Todd Kjosc44b1232017-06-29 12:01:43 -07003662 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003663 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003664 print_binder_proc(m, proc, 1);
Todd Kjosc44b1232017-06-29 12:01:43 -07003665 mutex_unlock(&binder_procs_lock);
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003666 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003667 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003668}
3669
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003670static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003671{
3672 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003673
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003674 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003675
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003676 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003677
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003678 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003679
Todd Kjosc44b1232017-06-29 12:01:43 -07003680 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003681 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003682 print_binder_proc_stats(m, proc);
Todd Kjosc44b1232017-06-29 12:01:43 -07003683 mutex_unlock(&binder_procs_lock);
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003684 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003685 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003686}
3687
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003688static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003689{
3690 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003691
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003692 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003693
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003694 seq_puts(m, "binder transactions:\n");
Todd Kjosc44b1232017-06-29 12:01:43 -07003695 mutex_lock(&binder_procs_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003696 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003697 print_binder_proc(m, proc, 0);
Todd Kjosc44b1232017-06-29 12:01:43 -07003698 mutex_unlock(&binder_procs_lock);
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003699 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003700 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003701}
3702
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003703static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003704{
Riley Andrews83050a42016-02-09 21:05:33 -08003705 struct binder_proc *itr;
Martijn Coenen14db3182017-02-03 14:40:47 -08003706 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003707
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003708 binder_lock(__func__);
Riley Andrews83050a42016-02-09 21:05:33 -08003709
Todd Kjosc44b1232017-06-29 12:01:43 -07003710 mutex_lock(&binder_procs_lock);
Riley Andrews83050a42016-02-09 21:05:33 -08003711 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen14db3182017-02-03 14:40:47 -08003712 if (itr->pid == pid) {
3713 seq_puts(m, "binder proc state:\n");
3714 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08003715 }
3716 }
Todd Kjosc44b1232017-06-29 12:01:43 -07003717 mutex_unlock(&binder_procs_lock);
3718
Todd Kjos1cf29cf2017-06-29 12:01:42 -07003719 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003720 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003721}
3722
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003723static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003724 struct binder_transaction_log_entry *e)
3725{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003726 seq_printf(m,
Todd Kjos57ada2f2017-06-29 12:01:46 -07003727 "%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 -07003728 e->debug_id, (e->call_type == 2) ? "reply" :
3729 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen14db3182017-02-03 14:40:47 -08003730 e->from_thread, e->to_proc, e->to_thread, e->context_name,
Todd Kjos57ada2f2017-06-29 12:01:46 -07003731 e->to_node, e->target_handle, e->data_size, e->offsets_size,
3732 e->return_error, e->return_error_param,
3733 e->return_error_line);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003734}
3735
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003736static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003737{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003738 struct binder_transaction_log *log = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003739 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003740
3741 if (log->full) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003742 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3743 print_binder_transaction_log_entry(m, &log->entry[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003744 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003745 for (i = 0; i < log->next; i++)
3746 print_binder_transaction_log_entry(m, &log->entry[i]);
3747 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003748}
3749
3750static const struct file_operations binder_fops = {
3751 .owner = THIS_MODULE,
3752 .poll = binder_poll,
3753 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003754 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003755 .mmap = binder_mmap,
3756 .open = binder_open,
3757 .flush = binder_flush,
3758 .release = binder_release,
3759};
3760
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003761BINDER_DEBUG_ENTRY(state);
3762BINDER_DEBUG_ENTRY(stats);
3763BINDER_DEBUG_ENTRY(transactions);
3764BINDER_DEBUG_ENTRY(transaction_log);
3765
Martijn Coenenac4812c2017-02-03 14:40:48 -08003766static int __init init_binder_device(const char *name)
3767{
3768 int ret;
3769 struct binder_device *binder_device;
3770
3771 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
3772 if (!binder_device)
3773 return -ENOMEM;
3774
3775 binder_device->miscdev.fops = &binder_fops;
3776 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
3777 binder_device->miscdev.name = name;
3778
3779 binder_device->context.binder_context_mgr_uid = INVALID_UID;
3780 binder_device->context.name = name;
Todd Kjosc44b1232017-06-29 12:01:43 -07003781 mutex_init(&binder_device->context.context_mgr_node_lock);
Martijn Coenenac4812c2017-02-03 14:40:48 -08003782
3783 ret = misc_register(&binder_device->miscdev);
3784 if (ret < 0) {
3785 kfree(binder_device);
3786 return ret;
3787 }
3788
3789 hlist_add_head(&binder_device->hlist, &binder_devices);
3790
3791 return ret;
3792}
3793
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003794static int __init binder_init(void)
3795{
3796 int ret;
Martijn Coenenac4812c2017-02-03 14:40:48 -08003797 char *device_name, *device_names;
3798 struct binder_device *device;
3799 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003800
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003801 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3802 if (binder_debugfs_dir_entry_root)
3803 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3804 binder_debugfs_dir_entry_root);
Martijn Coenenac4812c2017-02-03 14:40:48 -08003805
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003806 if (binder_debugfs_dir_entry_root) {
3807 debugfs_create_file("state",
3808 S_IRUGO,
3809 binder_debugfs_dir_entry_root,
3810 NULL,
3811 &binder_state_fops);
3812 debugfs_create_file("stats",
3813 S_IRUGO,
3814 binder_debugfs_dir_entry_root,
3815 NULL,
3816 &binder_stats_fops);
3817 debugfs_create_file("transactions",
3818 S_IRUGO,
3819 binder_debugfs_dir_entry_root,
3820 NULL,
3821 &binder_transactions_fops);
3822 debugfs_create_file("transaction_log",
3823 S_IRUGO,
3824 binder_debugfs_dir_entry_root,
3825 &binder_transaction_log,
3826 &binder_transaction_log_fops);
3827 debugfs_create_file("failed_transaction_log",
3828 S_IRUGO,
3829 binder_debugfs_dir_entry_root,
3830 &binder_transaction_log_failed,
3831 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003832 }
Martijn Coenenac4812c2017-02-03 14:40:48 -08003833
3834 /*
3835 * Copy the module_parameter string, because we don't want to
3836 * tokenize it in-place.
3837 */
3838 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
3839 if (!device_names) {
3840 ret = -ENOMEM;
3841 goto err_alloc_device_names_failed;
3842 }
3843 strcpy(device_names, binder_devices_param);
3844
3845 while ((device_name = strsep(&device_names, ","))) {
3846 ret = init_binder_device(device_name);
3847 if (ret)
3848 goto err_init_binder_device_failed;
3849 }
3850
3851 return ret;
3852
3853err_init_binder_device_failed:
3854 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
3855 misc_deregister(&device->miscdev);
3856 hlist_del(&device->hlist);
3857 kfree(device);
3858 }
3859err_alloc_device_names_failed:
3860 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
3861
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003862 return ret;
3863}
3864
3865device_initcall(binder_init);
3866
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003867#define CREATE_TRACE_POINTS
3868#include "binder_trace.h"
3869
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003870MODULE_LICENSE("GPL v2");