blob: 79902e7f8fa6861daa110233abc297078f0a943d [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>
27#include <linux/mm.h>
28#include <linux/module.h>
29#include <linux/mutex.h>
30#include <linux/nsproxy.h>
31#include <linux/poll.h>
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070032#include <linux/debugfs.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090033#include <linux/rbtree.h>
34#include <linux/sched.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>
37#include <linux/vmalloc.h>
Colin Crossc11a1662010-04-15 15:21:51 -070038#include <linux/slab.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080039#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050040#include <linux/security.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090041
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020042#ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
43#define BINDER_IPC_32BIT 1
44#endif
45
46#include <uapi/linux/android/binder.h>
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070047#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090048
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070049static DEFINE_MUTEX(binder_main_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090050static DEFINE_MUTEX(binder_deferred_lock);
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -080051static DEFINE_MUTEX(binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090052
Martijn Coenen6b7c7122016-09-30 16:08:09 +020053static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090054static HLIST_HEAD(binder_procs);
55static HLIST_HEAD(binder_deferred_list);
56static HLIST_HEAD(binder_dead_nodes);
57
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070058static struct dentry *binder_debugfs_dir_entry_root;
59static struct dentry *binder_debugfs_dir_entry_proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090060static int binder_last_id;
61
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070062#define BINDER_DEBUG_ENTRY(name) \
63static int binder_##name##_open(struct inode *inode, struct file *file) \
64{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070065 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070066} \
67\
68static const struct file_operations binder_##name##_fops = { \
69 .owner = THIS_MODULE, \
70 .open = binder_##name##_open, \
71 .read = seq_read, \
72 .llseek = seq_lseek, \
73 .release = single_release, \
74}
75
76static int binder_proc_show(struct seq_file *m, void *unused);
77BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090078
79/* This is only defined in include/asm-arm/sizes.h */
80#ifndef SZ_1K
81#define SZ_1K 0x400
82#endif
83
84#ifndef SZ_4M
85#define SZ_4M 0x400000
86#endif
87
88#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
89
90#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
91
92enum {
93 BINDER_DEBUG_USER_ERROR = 1U << 0,
94 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
95 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
96 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
97 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
98 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
99 BINDER_DEBUG_READ_WRITE = 1U << 6,
100 BINDER_DEBUG_USER_REFS = 1U << 7,
101 BINDER_DEBUG_THREADS = 1U << 8,
102 BINDER_DEBUG_TRANSACTION = 1U << 9,
103 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
104 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
105 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
106 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
107 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
108 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
109};
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
Zhengwang Ruan2c523252012-03-07 10:36:57 +0800114static bool binder_debug_no_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900115module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
116
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200117static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
118module_param_named(devices, binder_devices_param, charp, S_IRUGO);
119
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900120static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
121static int binder_stop_on_user_error;
122
123static int binder_set_stop_on_user_error(const char *val,
124 struct kernel_param *kp)
125{
126 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900127
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900128 ret = param_set_int(val, kp);
129 if (binder_stop_on_user_error < 2)
130 wake_up(&binder_user_error_wait);
131 return ret;
132}
133module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
134 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
135
136#define binder_debug(mask, x...) \
137 do { \
138 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400139 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900140 } while (0)
141
142#define binder_user_error(x...) \
143 do { \
144 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400145 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900146 if (binder_stop_on_user_error) \
147 binder_stop_on_user_error = 2; \
148 } while (0)
149
Martijn Coenen00c80372016-07-13 12:06:49 +0200150#define to_flat_binder_object(hdr) \
151 container_of(hdr, struct flat_binder_object, hdr)
152
153#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
154
Martijn Coenen5a6da532016-09-30 14:10:07 +0200155#define to_binder_buffer_object(hdr) \
156 container_of(hdr, struct binder_buffer_object, hdr)
157
Martijn Coenene3e0f4802016-10-18 13:58:55 +0200158#define to_binder_fd_array_object(hdr) \
159 container_of(hdr, struct binder_fd_array_object, hdr)
160
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900161enum binder_stat_types {
162 BINDER_STAT_PROC,
163 BINDER_STAT_THREAD,
164 BINDER_STAT_NODE,
165 BINDER_STAT_REF,
166 BINDER_STAT_DEATH,
167 BINDER_STAT_TRANSACTION,
168 BINDER_STAT_TRANSACTION_COMPLETE,
169 BINDER_STAT_COUNT
170};
171
172struct binder_stats {
173 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
Martijn Coenen5a6da532016-09-30 14:10:07 +0200174 int bc[_IOC_NR(BC_REPLY_SG) + 1];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900175 int obj_created[BINDER_STAT_COUNT];
176 int obj_deleted[BINDER_STAT_COUNT];
177};
178
179static struct binder_stats binder_stats;
180
181static inline void binder_stats_deleted(enum binder_stat_types type)
182{
183 binder_stats.obj_deleted[type]++;
184}
185
186static inline void binder_stats_created(enum binder_stat_types type)
187{
188 binder_stats.obj_created[type]++;
189}
190
191struct binder_transaction_log_entry {
192 int debug_id;
193 int call_type;
194 int from_proc;
195 int from_thread;
196 int target_handle;
197 int to_proc;
198 int to_thread;
199 int to_node;
200 int data_size;
201 int offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200202 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900203};
204struct binder_transaction_log {
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 Coenen0b3311e2016-09-30 15:51:48 +0200227struct binder_context {
228 struct binder_node *binder_context_mgr_node;
229 kuid_t binder_context_mgr_uid;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200230 const char *name;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200231};
232
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200233struct binder_device {
234 struct hlist_node hlist;
235 struct miscdevice miscdev;
236 struct binder_context context;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200237};
238
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900239struct binder_work {
240 struct list_head entry;
241 enum {
242 BINDER_WORK_TRANSACTION = 1,
243 BINDER_WORK_TRANSACTION_COMPLETE,
244 BINDER_WORK_NODE,
245 BINDER_WORK_DEAD_BINDER,
246 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
247 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
248 } type;
249};
250
251struct binder_node {
252 int debug_id;
253 struct binder_work work;
254 union {
255 struct rb_node rb_node;
256 struct hlist_node dead_node;
257 };
258 struct binder_proc *proc;
259 struct hlist_head refs;
260 int internal_strong_refs;
261 int local_weak_refs;
262 int local_strong_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800263 binder_uintptr_t ptr;
264 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900265 unsigned has_strong_ref:1;
266 unsigned pending_strong_ref:1;
267 unsigned has_weak_ref:1;
268 unsigned pending_weak_ref:1;
269 unsigned has_async_transaction:1;
270 unsigned accept_fds:1;
271 unsigned min_priority:8;
272 struct list_head async_todo;
273};
274
275struct binder_ref_death {
276 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800277 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900278};
279
280struct binder_ref {
281 /* Lookups needed: */
282 /* node + proc => ref (transaction) */
283 /* desc + proc => ref (transaction, inc/dec ref) */
284 /* node => refs + procs (proc exit) */
285 int debug_id;
286 struct rb_node rb_node_desc;
287 struct rb_node rb_node_node;
288 struct hlist_node node_entry;
289 struct binder_proc *proc;
290 struct binder_node *node;
291 uint32_t desc;
292 int strong;
293 int weak;
294 struct binder_ref_death *death;
295};
296
297struct binder_buffer {
Justin P. Mattock217218f2012-01-12 06:51:31 -0800298 struct list_head entry; /* free and allocated entries by address */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900299 struct rb_node rb_node; /* free entry by size or allocated entry */
300 /* by address */
301 unsigned free:1;
302 unsigned allow_user_free:1;
303 unsigned async_transaction:1;
304 unsigned debug_id:29;
305
306 struct binder_transaction *transaction;
307
308 struct binder_node *target_node;
309 size_t data_size;
310 size_t offsets_size;
Martijn Coenen59878d72016-09-30 14:05:40 +0200311 size_t extra_buffers_size;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900312 uint8_t data[0];
313};
314
315enum binder_deferred_state {
316 BINDER_DEFERRED_PUT_FILES = 0x01,
317 BINDER_DEFERRED_FLUSH = 0x02,
318 BINDER_DEFERRED_RELEASE = 0x04,
319};
320
321struct binder_proc {
322 struct hlist_node proc_node;
323 struct rb_root threads;
324 struct rb_root nodes;
325 struct rb_root refs_by_desc;
326 struct rb_root refs_by_node;
327 int pid;
328 struct vm_area_struct *vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800329 struct mm_struct *vma_vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900330 struct task_struct *tsk;
331 struct files_struct *files;
332 struct hlist_node deferred_work_node;
333 int deferred_work;
334 void *buffer;
335 ptrdiff_t user_buffer_offset;
336
337 struct list_head buffers;
338 struct rb_root free_buffers;
339 struct rb_root allocated_buffers;
340 size_t free_async_space;
341
342 struct page **pages;
343 size_t buffer_size;
344 uint32_t buffer_free;
345 struct list_head todo;
346 wait_queue_head_t wait;
347 struct binder_stats stats;
348 struct list_head delivered_death;
349 int max_threads;
350 int requested_threads;
351 int requested_threads_started;
352 int ready_threads;
353 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700354 struct dentry *debugfs_entry;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200355 struct binder_context *context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900356};
357
358enum {
359 BINDER_LOOPER_STATE_REGISTERED = 0x01,
360 BINDER_LOOPER_STATE_ENTERED = 0x02,
361 BINDER_LOOPER_STATE_EXITED = 0x04,
362 BINDER_LOOPER_STATE_INVALID = 0x08,
363 BINDER_LOOPER_STATE_WAITING = 0x10,
364 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
365};
366
367struct binder_thread {
368 struct binder_proc *proc;
369 struct rb_node rb_node;
370 int pid;
371 int looper;
372 struct binder_transaction *transaction_stack;
373 struct list_head todo;
374 uint32_t return_error; /* Write failed, return error code in read buf */
375 uint32_t return_error2; /* Write failed, return error code in read */
376 /* buffer. Used when sending a reply to a dead process that */
377 /* we are also waiting on */
378 wait_queue_head_t wait;
379 struct binder_stats stats;
380};
381
382struct binder_transaction {
383 int debug_id;
384 struct binder_work work;
385 struct binder_thread *from;
386 struct binder_transaction *from_parent;
387 struct binder_proc *to_proc;
388 struct binder_thread *to_thread;
389 struct binder_transaction *to_parent;
390 unsigned need_reply:1;
391 /* unsigned is_dead:1; */ /* not used at the moment */
392
393 struct binder_buffer *buffer;
394 unsigned int code;
395 unsigned int flags;
396 long priority;
397 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600398 kuid_t sender_euid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900399};
400
401static void
402binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
403
Sachin Kamatefde99c2012-08-17 16:39:36 +0530404static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900405{
406 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900407 unsigned long rlim_cur;
408 unsigned long irqs;
Riley Andrewsf48e2692015-09-15 10:49:46 -0700409 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900410
411 if (files == NULL)
412 return -ESRCH;
413
Al Virodcfadfa2012-08-12 17:27:30 -0400414 if (!lock_task_sighand(proc->tsk, &irqs))
415 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900416
Al Virodcfadfa2012-08-12 17:27:30 -0400417 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
418 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900419
Riley Andrewsf48e2692015-09-15 10:49:46 -0700420 preempt_enable_no_resched();
421 ret = __alloc_fd(files, 0, rlim_cur, flags);
422 preempt_disable();
423
424 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900425}
426
427/*
428 * copied from fd_install
429 */
430static void task_fd_install(
431 struct binder_proc *proc, unsigned int fd, struct file *file)
432{
Riley Andrewsf48e2692015-09-15 10:49:46 -0700433 if (proc->files) {
434 preempt_enable_no_resched();
Al Virof869e8a2012-08-15 21:06:33 -0400435 __fd_install(proc->files, fd, file);
Riley Andrewsf48e2692015-09-15 10:49:46 -0700436 preempt_disable();
437 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900438}
439
440/*
441 * copied from sys_close
442 */
443static long task_close_fd(struct binder_proc *proc, unsigned int fd)
444{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900445 int retval;
446
Al Viro483ce1d2012-08-19 12:04:24 -0400447 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900448 return -ESRCH;
449
Al Viro483ce1d2012-08-19 12:04:24 -0400450 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900451 /* can't restart close syscall because file table entry was cleared */
452 if (unlikely(retval == -ERESTARTSYS ||
453 retval == -ERESTARTNOINTR ||
454 retval == -ERESTARTNOHAND ||
455 retval == -ERESTART_RESTARTBLOCK))
456 retval = -EINTR;
457
458 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900459}
460
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700461static inline void binder_lock(const char *tag)
462{
463 trace_binder_lock(tag);
464 mutex_lock(&binder_main_lock);
Riley Andrewsf48e2692015-09-15 10:49:46 -0700465 preempt_disable();
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700466 trace_binder_locked(tag);
467}
468
469static inline void binder_unlock(const char *tag)
470{
471 trace_binder_unlock(tag);
472 mutex_unlock(&binder_main_lock);
Riley Andrewsf48e2692015-09-15 10:49:46 -0700473 preempt_enable();
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700474}
475
Riley Andrewsf48e2692015-09-15 10:49:46 -0700476static inline void *kzalloc_preempt_disabled(size_t size)
477{
478 void *ptr;
479
480 ptr = kzalloc(size, GFP_NOWAIT);
481 if (ptr)
482 return ptr;
483
484 preempt_enable_no_resched();
485 ptr = kzalloc(size, GFP_KERNEL);
486 preempt_disable();
487
488 return ptr;
489}
490
491static inline long copy_to_user_preempt_disabled(void __user *to, const void *from, long n)
492{
493 long ret;
494
495 preempt_enable_no_resched();
496 ret = copy_to_user(to, from, n);
497 preempt_disable();
498 return ret;
499}
500
501static inline long copy_from_user_preempt_disabled(void *to, const void __user *from, long n)
502{
503 long ret;
504
505 preempt_enable_no_resched();
506 ret = copy_from_user(to, from, n);
507 preempt_disable();
508 return ret;
509}
510
511#define get_user_preempt_disabled(x, ptr) \
512({ \
513 int __ret; \
514 preempt_enable_no_resched(); \
515 __ret = get_user(x, ptr); \
516 preempt_disable(); \
517 __ret; \
518})
519
520#define put_user_preempt_disabled(x, ptr) \
521({ \
522 int __ret; \
523 preempt_enable_no_resched(); \
524 __ret = put_user(x, ptr); \
525 preempt_disable(); \
526 __ret; \
527})
528
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900529static void binder_set_nice(long nice)
530{
531 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900532
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900533 if (can_nice(current, nice)) {
534 set_user_nice(current, nice);
535 return;
536 }
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900537 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900538 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530539 "%d: nice value %ld not allowed use %ld instead\n",
540 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900541 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +0800542 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900543 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530544 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900545}
546
547static size_t binder_buffer_size(struct binder_proc *proc,
548 struct binder_buffer *buffer)
549{
550 if (list_is_last(&buffer->entry, &proc->buffers))
551 return proc->buffer + proc->buffer_size - (void *)buffer->data;
Karthik Nayak78733112014-06-21 20:23:16 +0530552 return (size_t)list_entry(buffer->entry.next,
553 struct binder_buffer, entry) - (size_t)buffer->data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900554}
555
556static void binder_insert_free_buffer(struct binder_proc *proc,
557 struct binder_buffer *new_buffer)
558{
559 struct rb_node **p = &proc->free_buffers.rb_node;
560 struct rb_node *parent = NULL;
561 struct binder_buffer *buffer;
562 size_t buffer_size;
563 size_t new_buffer_size;
564
565 BUG_ON(!new_buffer->free);
566
567 new_buffer_size = binder_buffer_size(proc, new_buffer);
568
569 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530570 "%d: add free buffer, size %zd, at %p\n",
571 proc->pid, new_buffer_size, new_buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900572
573 while (*p) {
574 parent = *p;
575 buffer = rb_entry(parent, struct binder_buffer, rb_node);
576 BUG_ON(!buffer->free);
577
578 buffer_size = binder_buffer_size(proc, buffer);
579
580 if (new_buffer_size < buffer_size)
581 p = &parent->rb_left;
582 else
583 p = &parent->rb_right;
584 }
585 rb_link_node(&new_buffer->rb_node, parent, p);
586 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
587}
588
589static void binder_insert_allocated_buffer(struct binder_proc *proc,
590 struct binder_buffer *new_buffer)
591{
592 struct rb_node **p = &proc->allocated_buffers.rb_node;
593 struct rb_node *parent = NULL;
594 struct binder_buffer *buffer;
595
596 BUG_ON(new_buffer->free);
597
598 while (*p) {
599 parent = *p;
600 buffer = rb_entry(parent, struct binder_buffer, rb_node);
601 BUG_ON(buffer->free);
602
603 if (new_buffer < buffer)
604 p = &parent->rb_left;
605 else if (new_buffer > buffer)
606 p = &parent->rb_right;
607 else
608 BUG();
609 }
610 rb_link_node(&new_buffer->rb_node, parent, p);
611 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
612}
613
614static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800615 uintptr_t user_ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900616{
617 struct rb_node *n = proc->allocated_buffers.rb_node;
618 struct binder_buffer *buffer;
619 struct binder_buffer *kern_ptr;
620
Arve Hjønnevågda498892014-02-21 14:40:26 -0800621 kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
622 - offsetof(struct binder_buffer, data));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900623
624 while (n) {
625 buffer = rb_entry(n, struct binder_buffer, rb_node);
626 BUG_ON(buffer->free);
627
628 if (kern_ptr < buffer)
629 n = n->rb_left;
630 else if (kern_ptr > buffer)
631 n = n->rb_right;
632 else
633 return buffer;
634 }
635 return NULL;
636}
637
638static int binder_update_page_range(struct binder_proc *proc, int allocate,
639 void *start, void *end,
640 struct vm_area_struct *vma)
641{
642 void *page_addr;
643 unsigned long user_page_addr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900644 struct page **page;
645 struct mm_struct *mm;
646
647 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530648 "%d: %s pages %p-%p\n", proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900649 allocate ? "allocate" : "free", start, end);
650
651 if (end <= start)
652 return 0;
653
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700654 trace_binder_update_page_range(proc, allocate, start, end);
655
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900656 if (vma)
657 mm = NULL;
658 else
659 mm = get_task_mm(proc->tsk);
660
Riley Andrewsf48e2692015-09-15 10:49:46 -0700661 preempt_enable_no_resched();
662
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900663 if (mm) {
664 down_write(&mm->mmap_sem);
665 vma = proc->vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800666 if (vma && mm != proc->vma_vm_mm) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530667 pr_err("%d: vma mm and task mm mismatch\n",
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -0800668 proc->pid);
669 vma = NULL;
670 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900671 }
672
673 if (allocate == 0)
674 goto free_range;
675
676 if (vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530677 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
678 proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900679 goto err_no_vma;
680 }
681
682 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
683 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900684
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900685 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
686
687 BUG_ON(*page);
Arve Hjønnevåg585650d2012-10-16 15:29:55 -0700688 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900689 if (*page == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530690 pr_err("%d: binder_alloc_buf failed for page at %p\n",
691 proc->pid, page_addr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900692 goto err_alloc_page_failed;
693 }
Andrey Ryabininf4c72c72015-02-27 20:44:21 +0300694 ret = map_kernel_range_noflush((unsigned long)page_addr,
695 PAGE_SIZE, PAGE_KERNEL, page);
696 flush_cache_vmap((unsigned long)page_addr,
697 (unsigned long)page_addr + PAGE_SIZE);
698 if (ret != 1) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530699 pr_err("%d: binder_alloc_buf failed to map page at %p in kernel\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900700 proc->pid, page_addr);
701 goto err_map_kernel_failed;
702 }
703 user_page_addr =
704 (uintptr_t)page_addr + proc->user_buffer_offset;
705 ret = vm_insert_page(vma, user_page_addr, page[0]);
706 if (ret) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530707 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900708 proc->pid, user_page_addr);
709 goto err_vm_insert_page_failed;
710 }
711 /* vm_insert_page does not seem to increment the refcount */
712 }
713 if (mm) {
714 up_write(&mm->mmap_sem);
715 mmput(mm);
716 }
Riley Andrewsf48e2692015-09-15 10:49:46 -0700717
718 preempt_disable();
719
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900720 return 0;
721
722free_range:
723 for (page_addr = end - PAGE_SIZE; page_addr >= start;
724 page_addr -= PAGE_SIZE) {
725 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
726 if (vma)
727 zap_page_range(vma, (uintptr_t)page_addr +
728 proc->user_buffer_offset, PAGE_SIZE, NULL);
729err_vm_insert_page_failed:
730 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
731err_map_kernel_failed:
732 __free_page(*page);
733 *page = NULL;
734err_alloc_page_failed:
735 ;
736 }
737err_no_vma:
738 if (mm) {
739 up_write(&mm->mmap_sem);
740 mmput(mm);
741 }
Riley Andrewsf48e2692015-09-15 10:49:46 -0700742
743 preempt_disable();
744
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900745 return -ENOMEM;
746}
747
748static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
749 size_t data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +0200750 size_t offsets_size,
751 size_t extra_buffers_size,
752 int is_async)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900753{
754 struct rb_node *n = proc->free_buffers.rb_node;
755 struct binder_buffer *buffer;
756 size_t buffer_size;
757 struct rb_node *best_fit = NULL;
758 void *has_page_addr;
759 void *end_page_addr;
Martijn Coenen59878d72016-09-30 14:05:40 +0200760 size_t size, data_offsets_size;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900761
762 if (proc->vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530763 pr_err("%d: binder_alloc_buf, no vma\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900764 proc->pid);
765 return NULL;
766 }
767
Martijn Coenen59878d72016-09-30 14:05:40 +0200768 data_offsets_size = ALIGN(data_size, sizeof(void *)) +
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900769 ALIGN(offsets_size, sizeof(void *));
770
Martijn Coenen59878d72016-09-30 14:05:40 +0200771 if (data_offsets_size < data_size || data_offsets_size < offsets_size) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530772 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
773 proc->pid, data_size, offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900774 return NULL;
775 }
Martijn Coenen59878d72016-09-30 14:05:40 +0200776 size = data_offsets_size + ALIGN(extra_buffers_size, sizeof(void *));
777 if (size < data_offsets_size || size < extra_buffers_size) {
778 binder_user_error("%d: got transaction with invalid extra_buffers_size %zd\n",
779 proc->pid, extra_buffers_size);
780 return NULL;
781 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900782 if (is_async &&
783 proc->free_async_space < size + sizeof(struct binder_buffer)) {
784 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530785 "%d: binder_alloc_buf size %zd failed, no async space left\n",
786 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900787 return NULL;
788 }
789
790 while (n) {
791 buffer = rb_entry(n, struct binder_buffer, rb_node);
792 BUG_ON(!buffer->free);
793 buffer_size = binder_buffer_size(proc, buffer);
794
795 if (size < buffer_size) {
796 best_fit = n;
797 n = n->rb_left;
798 } else if (size > buffer_size)
799 n = n->rb_right;
800 else {
801 best_fit = n;
802 break;
803 }
804 }
805 if (best_fit == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530806 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
807 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900808 return NULL;
809 }
810 if (n == NULL) {
811 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
812 buffer_size = binder_buffer_size(proc, buffer);
813 }
814
815 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530816 "%d: binder_alloc_buf size %zd got buffer %p size %zd\n",
817 proc->pid, size, buffer, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900818
819 has_page_addr =
820 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
821 if (n == NULL) {
822 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
823 buffer_size = size; /* no room for other buffers */
824 else
825 buffer_size = size + sizeof(struct binder_buffer);
826 }
827 end_page_addr =
828 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
829 if (end_page_addr > has_page_addr)
830 end_page_addr = has_page_addr;
831 if (binder_update_page_range(proc, 1,
832 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
833 return NULL;
834
835 rb_erase(best_fit, &proc->free_buffers);
836 buffer->free = 0;
837 binder_insert_allocated_buffer(proc, buffer);
838 if (buffer_size != size) {
839 struct binder_buffer *new_buffer = (void *)buffer->data + size;
Seunghun Lee10f62862014-05-01 01:30:23 +0900840
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900841 list_add(&new_buffer->entry, &buffer->entry);
842 new_buffer->free = 1;
843 binder_insert_free_buffer(proc, new_buffer);
844 }
845 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530846 "%d: binder_alloc_buf size %zd got %p\n",
847 proc->pid, size, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900848 buffer->data_size = data_size;
849 buffer->offsets_size = offsets_size;
Martijn Coenen59878d72016-09-30 14:05:40 +0200850 buffer->extra_buffers_size = extra_buffers_size;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900851 buffer->async_transaction = is_async;
852 if (is_async) {
853 proc->free_async_space -= size + sizeof(struct binder_buffer);
854 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530855 "%d: binder_alloc_buf size %zd async free %zd\n",
856 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900857 }
858
859 return buffer;
860}
861
862static void *buffer_start_page(struct binder_buffer *buffer)
863{
864 return (void *)((uintptr_t)buffer & PAGE_MASK);
865}
866
867static void *buffer_end_page(struct binder_buffer *buffer)
868{
869 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
870}
871
872static void binder_delete_free_buffer(struct binder_proc *proc,
873 struct binder_buffer *buffer)
874{
875 struct binder_buffer *prev, *next = NULL;
876 int free_page_end = 1;
877 int free_page_start = 1;
878
879 BUG_ON(proc->buffers.next == &buffer->entry);
880 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
881 BUG_ON(!prev->free);
882 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
883 free_page_start = 0;
884 if (buffer_end_page(prev) == buffer_end_page(buffer))
885 free_page_end = 0;
886 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530887 "%d: merge free, buffer %p share page with %p\n",
888 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900889 }
890
891 if (!list_is_last(&buffer->entry, &proc->buffers)) {
892 next = list_entry(buffer->entry.next,
893 struct binder_buffer, entry);
894 if (buffer_start_page(next) == buffer_end_page(buffer)) {
895 free_page_end = 0;
896 if (buffer_start_page(next) ==
897 buffer_start_page(buffer))
898 free_page_start = 0;
899 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530900 "%d: merge free, buffer %p share page with %p\n",
901 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900902 }
903 }
904 list_del(&buffer->entry);
905 if (free_page_start || free_page_end) {
906 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Masanari Iida1dcdbfd2013-06-23 23:47:15 +0900907 "%d: merge free, buffer %p do not share page%s%s with %p or %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900908 proc->pid, buffer, free_page_start ? "" : " end",
909 free_page_end ? "" : " start", prev, next);
910 binder_update_page_range(proc, 0, free_page_start ?
911 buffer_start_page(buffer) : buffer_end_page(buffer),
912 (free_page_end ? buffer_end_page(buffer) :
913 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
914 }
915}
916
917static void binder_free_buf(struct binder_proc *proc,
918 struct binder_buffer *buffer)
919{
920 size_t size, buffer_size;
921
922 buffer_size = binder_buffer_size(proc, buffer);
923
924 size = ALIGN(buffer->data_size, sizeof(void *)) +
Martijn Coenen59878d72016-09-30 14:05:40 +0200925 ALIGN(buffer->offsets_size, sizeof(void *)) +
926 ALIGN(buffer->extra_buffers_size, sizeof(void *));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900927
928 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530929 "%d: binder_free_buf %p size %zd buffer_size %zd\n",
930 proc->pid, buffer, size, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900931
932 BUG_ON(buffer->free);
933 BUG_ON(size > buffer_size);
934 BUG_ON(buffer->transaction != NULL);
935 BUG_ON((void *)buffer < proc->buffer);
936 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
937
938 if (buffer->async_transaction) {
939 proc->free_async_space += size + sizeof(struct binder_buffer);
940
941 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530942 "%d: binder_free_buf size %zd async free %zd\n",
943 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900944 }
945
946 binder_update_page_range(proc, 0,
947 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
948 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
949 NULL);
950 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
951 buffer->free = 1;
952 if (!list_is_last(&buffer->entry, &proc->buffers)) {
953 struct binder_buffer *next = list_entry(buffer->entry.next,
954 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900955
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900956 if (next->free) {
957 rb_erase(&next->rb_node, &proc->free_buffers);
958 binder_delete_free_buffer(proc, next);
959 }
960 }
961 if (proc->buffers.next != &buffer->entry) {
962 struct binder_buffer *prev = list_entry(buffer->entry.prev,
963 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900964
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900965 if (prev->free) {
966 binder_delete_free_buffer(proc, buffer);
967 rb_erase(&prev->rb_node, &proc->free_buffers);
968 buffer = prev;
969 }
970 }
971 binder_insert_free_buffer(proc, buffer);
972}
973
974static struct binder_node *binder_get_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800975 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900976{
977 struct rb_node *n = proc->nodes.rb_node;
978 struct binder_node *node;
979
980 while (n) {
981 node = rb_entry(n, struct binder_node, rb_node);
982
983 if (ptr < node->ptr)
984 n = n->rb_left;
985 else if (ptr > node->ptr)
986 n = n->rb_right;
987 else
988 return node;
989 }
990 return NULL;
991}
992
993static struct binder_node *binder_new_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800994 binder_uintptr_t ptr,
995 binder_uintptr_t cookie)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900996{
997 struct rb_node **p = &proc->nodes.rb_node;
998 struct rb_node *parent = NULL;
999 struct binder_node *node;
1000
1001 while (*p) {
1002 parent = *p;
1003 node = rb_entry(parent, struct binder_node, rb_node);
1004
1005 if (ptr < node->ptr)
1006 p = &(*p)->rb_left;
1007 else if (ptr > node->ptr)
1008 p = &(*p)->rb_right;
1009 else
1010 return NULL;
1011 }
1012
Riley Andrewsf48e2692015-09-15 10:49:46 -07001013 node = kzalloc_preempt_disabled(sizeof(*node));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001014 if (node == NULL)
1015 return NULL;
1016 binder_stats_created(BINDER_STAT_NODE);
1017 rb_link_node(&node->rb_node, parent, p);
1018 rb_insert_color(&node->rb_node, &proc->nodes);
1019 node->debug_id = ++binder_last_id;
1020 node->proc = proc;
1021 node->ptr = ptr;
1022 node->cookie = cookie;
1023 node->work.type = BINDER_WORK_NODE;
1024 INIT_LIST_HEAD(&node->work.entry);
1025 INIT_LIST_HEAD(&node->async_todo);
1026 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001027 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001028 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001029 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001030 return node;
1031}
1032
1033static int binder_inc_node(struct binder_node *node, int strong, int internal,
1034 struct list_head *target_list)
1035{
1036 if (strong) {
1037 if (internal) {
1038 if (target_list == NULL &&
1039 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001040 !(node->proc &&
1041 node == node->proc->context->
1042 binder_context_mgr_node &&
1043 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301044 pr_err("invalid inc strong node for %d\n",
1045 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001046 return -EINVAL;
1047 }
1048 node->internal_strong_refs++;
1049 } else
1050 node->local_strong_refs++;
1051 if (!node->has_strong_ref && target_list) {
1052 list_del_init(&node->work.entry);
1053 list_add_tail(&node->work.entry, target_list);
1054 }
1055 } else {
1056 if (!internal)
1057 node->local_weak_refs++;
1058 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
1059 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301060 pr_err("invalid inc weak node for %d\n",
1061 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001062 return -EINVAL;
1063 }
1064 list_add_tail(&node->work.entry, target_list);
1065 }
1066 }
1067 return 0;
1068}
1069
1070static int binder_dec_node(struct binder_node *node, int strong, int internal)
1071{
1072 if (strong) {
1073 if (internal)
1074 node->internal_strong_refs--;
1075 else
1076 node->local_strong_refs--;
1077 if (node->local_strong_refs || node->internal_strong_refs)
1078 return 0;
1079 } else {
1080 if (!internal)
1081 node->local_weak_refs--;
1082 if (node->local_weak_refs || !hlist_empty(&node->refs))
1083 return 0;
1084 }
1085 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
1086 if (list_empty(&node->work.entry)) {
1087 list_add_tail(&node->work.entry, &node->proc->todo);
1088 wake_up_interruptible(&node->proc->wait);
1089 }
1090 } else {
1091 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1092 !node->local_weak_refs) {
1093 list_del_init(&node->work.entry);
1094 if (node->proc) {
1095 rb_erase(&node->rb_node, &node->proc->nodes);
1096 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301097 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001098 node->debug_id);
1099 } else {
1100 hlist_del(&node->dead_node);
1101 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301102 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001103 node->debug_id);
1104 }
1105 kfree(node);
1106 binder_stats_deleted(BINDER_STAT_NODE);
1107 }
1108 }
1109
1110 return 0;
1111}
1112
1113
1114static struct binder_ref *binder_get_ref(struct binder_proc *proc,
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001115 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001116{
1117 struct rb_node *n = proc->refs_by_desc.rb_node;
1118 struct binder_ref *ref;
1119
1120 while (n) {
1121 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1122
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001123 if (desc < ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001124 n = n->rb_left;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001125 } else if (desc > ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001126 n = n->rb_right;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001127 } else if (need_strong_ref && !ref->strong) {
1128 binder_user_error("tried to use weak ref as strong ref\n");
1129 return NULL;
1130 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001131 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001132 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001133 }
1134 return NULL;
1135}
1136
1137static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1138 struct binder_node *node)
1139{
1140 struct rb_node *n;
1141 struct rb_node **p = &proc->refs_by_node.rb_node;
1142 struct rb_node *parent = NULL;
1143 struct binder_ref *ref, *new_ref;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001144 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001145
1146 while (*p) {
1147 parent = *p;
1148 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1149
1150 if (node < ref->node)
1151 p = &(*p)->rb_left;
1152 else if (node > ref->node)
1153 p = &(*p)->rb_right;
1154 else
1155 return ref;
1156 }
Riley Andrewsf48e2692015-09-15 10:49:46 -07001157 new_ref = kzalloc_preempt_disabled(sizeof(*ref));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001158 if (new_ref == NULL)
1159 return NULL;
1160 binder_stats_created(BINDER_STAT_REF);
1161 new_ref->debug_id = ++binder_last_id;
1162 new_ref->proc = proc;
1163 new_ref->node = node;
1164 rb_link_node(&new_ref->rb_node_node, parent, p);
1165 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1166
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001167 new_ref->desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001168 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1169 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1170 if (ref->desc > new_ref->desc)
1171 break;
1172 new_ref->desc = ref->desc + 1;
1173 }
1174
1175 p = &proc->refs_by_desc.rb_node;
1176 while (*p) {
1177 parent = *p;
1178 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1179
1180 if (new_ref->desc < ref->desc)
1181 p = &(*p)->rb_left;
1182 else if (new_ref->desc > ref->desc)
1183 p = &(*p)->rb_right;
1184 else
1185 BUG();
1186 }
1187 rb_link_node(&new_ref->rb_node_desc, parent, p);
1188 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1189 if (node) {
1190 hlist_add_head(&new_ref->node_entry, &node->refs);
1191
1192 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301193 "%d new ref %d desc %d for node %d\n",
1194 proc->pid, new_ref->debug_id, new_ref->desc,
1195 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001196 } else {
1197 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301198 "%d new ref %d desc %d for dead node\n",
1199 proc->pid, new_ref->debug_id, new_ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001200 }
1201 return new_ref;
1202}
1203
1204static void binder_delete_ref(struct binder_ref *ref)
1205{
1206 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301207 "%d delete ref %d desc %d for node %d\n",
1208 ref->proc->pid, ref->debug_id, ref->desc,
1209 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001210
1211 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1212 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1213 if (ref->strong)
1214 binder_dec_node(ref->node, 1, 1);
1215 hlist_del(&ref->node_entry);
1216 binder_dec_node(ref->node, 0, 1);
1217 if (ref->death) {
1218 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301219 "%d delete ref %d desc %d has death notification\n",
1220 ref->proc->pid, ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001221 list_del(&ref->death->work.entry);
1222 kfree(ref->death);
1223 binder_stats_deleted(BINDER_STAT_DEATH);
1224 }
1225 kfree(ref);
1226 binder_stats_deleted(BINDER_STAT_REF);
1227}
1228
1229static int binder_inc_ref(struct binder_ref *ref, int strong,
1230 struct list_head *target_list)
1231{
1232 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001233
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001234 if (strong) {
1235 if (ref->strong == 0) {
1236 ret = binder_inc_node(ref->node, 1, 1, target_list);
1237 if (ret)
1238 return ret;
1239 }
1240 ref->strong++;
1241 } else {
1242 if (ref->weak == 0) {
1243 ret = binder_inc_node(ref->node, 0, 1, target_list);
1244 if (ret)
1245 return ret;
1246 }
1247 ref->weak++;
1248 }
1249 return 0;
1250}
1251
1252
1253static int binder_dec_ref(struct binder_ref *ref, int strong)
1254{
1255 if (strong) {
1256 if (ref->strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301257 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001258 ref->proc->pid, ref->debug_id,
1259 ref->desc, ref->strong, ref->weak);
1260 return -EINVAL;
1261 }
1262 ref->strong--;
1263 if (ref->strong == 0) {
1264 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001265
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001266 ret = binder_dec_node(ref->node, strong, 1);
1267 if (ret)
1268 return ret;
1269 }
1270 } else {
1271 if (ref->weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301272 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001273 ref->proc->pid, ref->debug_id,
1274 ref->desc, ref->strong, ref->weak);
1275 return -EINVAL;
1276 }
1277 ref->weak--;
1278 }
1279 if (ref->strong == 0 && ref->weak == 0)
1280 binder_delete_ref(ref);
1281 return 0;
1282}
1283
1284static void binder_pop_transaction(struct binder_thread *target_thread,
1285 struct binder_transaction *t)
1286{
1287 if (target_thread) {
1288 BUG_ON(target_thread->transaction_stack != t);
1289 BUG_ON(target_thread->transaction_stack->from != target_thread);
1290 target_thread->transaction_stack =
1291 target_thread->transaction_stack->from_parent;
1292 t->from = NULL;
1293 }
1294 t->need_reply = 0;
1295 if (t->buffer)
1296 t->buffer->transaction = NULL;
1297 kfree(t);
1298 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1299}
1300
1301static void binder_send_failed_reply(struct binder_transaction *t,
1302 uint32_t error_code)
1303{
1304 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001305 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001306
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001307 BUG_ON(t->flags & TF_ONE_WAY);
1308 while (1) {
1309 target_thread = t->from;
1310 if (target_thread) {
1311 if (target_thread->return_error != BR_OK &&
1312 target_thread->return_error2 == BR_OK) {
1313 target_thread->return_error2 =
1314 target_thread->return_error;
1315 target_thread->return_error = BR_OK;
1316 }
1317 if (target_thread->return_error == BR_OK) {
1318 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301319 "send failed reply for transaction %d to %d:%d\n",
William Panlener0232a422014-09-03 22:44:03 -05001320 t->debug_id,
1321 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001322 target_thread->pid);
1323
1324 binder_pop_transaction(target_thread, t);
1325 target_thread->return_error = error_code;
1326 wake_up_interruptible(&target_thread->wait);
1327 } else {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301328 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1329 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001330 target_thread->pid,
1331 target_thread->return_error);
1332 }
1333 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001334 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001335 next = t->from_parent;
1336
1337 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1338 "send failed reply for transaction %d, target dead\n",
1339 t->debug_id);
1340
1341 binder_pop_transaction(target_thread, t);
1342 if (next == NULL) {
1343 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1344 "reply failed, no target thread at root\n");
1345 return;
1346 }
1347 t = next;
1348 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1349 "reply failed, no target thread -- retry %d\n",
1350 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001351 }
1352}
1353
Martijn Coenen00c80372016-07-13 12:06:49 +02001354/**
1355 * binder_validate_object() - checks for a valid metadata object in a buffer.
1356 * @buffer: binder_buffer that we're parsing.
1357 * @offset: offset in the buffer at which to validate an object.
1358 *
1359 * Return: If there's a valid metadata object at @offset in @buffer, the
1360 * size of that object. Otherwise, it returns zero.
1361 */
1362static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
1363{
1364 /* Check if we can read a header first */
1365 struct binder_object_header *hdr;
1366 size_t object_size = 0;
1367
1368 if (offset > buffer->data_size - sizeof(*hdr) ||
1369 buffer->data_size < sizeof(*hdr) ||
1370 !IS_ALIGNED(offset, sizeof(u32)))
1371 return 0;
1372
1373 /* Ok, now see if we can read a complete object. */
1374 hdr = (struct binder_object_header *)(buffer->data + offset);
1375 switch (hdr->type) {
1376 case BINDER_TYPE_BINDER:
1377 case BINDER_TYPE_WEAK_BINDER:
1378 case BINDER_TYPE_HANDLE:
1379 case BINDER_TYPE_WEAK_HANDLE:
1380 object_size = sizeof(struct flat_binder_object);
1381 break;
1382 case BINDER_TYPE_FD:
1383 object_size = sizeof(struct binder_fd_object);
1384 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001385 case BINDER_TYPE_PTR:
1386 object_size = sizeof(struct binder_buffer_object);
1387 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001388 case BINDER_TYPE_FDA:
1389 object_size = sizeof(struct binder_fd_array_object);
1390 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02001391 default:
1392 return 0;
1393 }
1394 if (offset <= buffer->data_size - object_size &&
1395 buffer->data_size >= object_size)
1396 return object_size;
1397 else
1398 return 0;
1399}
1400
Martijn Coenen5a6da532016-09-30 14:10:07 +02001401/**
1402 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
1403 * @b: binder_buffer containing the object
1404 * @index: index in offset array at which the binder_buffer_object is
1405 * located
1406 * @start: points to the start of the offset array
1407 * @num_valid: the number of valid offsets in the offset array
1408 *
1409 * Return: If @index is within the valid range of the offset array
1410 * described by @start and @num_valid, and if there's a valid
1411 * binder_buffer_object at the offset found in index @index
1412 * of the offset array, that object is returned. Otherwise,
1413 * %NULL is returned.
1414 * Note that the offset found in index @index itself is not
1415 * verified; this function assumes that @num_valid elements
1416 * from @start were previously verified to have valid offsets.
1417 */
1418static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
1419 binder_size_t index,
1420 binder_size_t *start,
1421 binder_size_t num_valid)
1422{
1423 struct binder_buffer_object *buffer_obj;
1424 binder_size_t *offp;
1425
1426 if (index >= num_valid)
1427 return NULL;
1428
1429 offp = start + index;
1430 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
1431 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
1432 return NULL;
1433
1434 return buffer_obj;
1435}
1436
1437/**
1438 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
1439 * @b: transaction buffer
1440 * @objects_start start of objects buffer
1441 * @buffer: binder_buffer_object in which to fix up
1442 * @offset: start offset in @buffer to fix up
1443 * @last_obj: last binder_buffer_object that we fixed up in
1444 * @last_min_offset: minimum fixup offset in @last_obj
1445 *
1446 * Return: %true if a fixup in buffer @buffer at offset @offset is
1447 * allowed.
1448 *
1449 * For safety reasons, we only allow fixups inside a buffer to happen
1450 * at increasing offsets; additionally, we only allow fixup on the last
1451 * buffer object that was verified, or one of its parents.
1452 *
1453 * Example of what is allowed:
1454 *
1455 * A
1456 * B (parent = A, offset = 0)
1457 * C (parent = A, offset = 16)
1458 * D (parent = C, offset = 0)
1459 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
1460 *
1461 * Examples of what is not allowed:
1462 *
1463 * Decreasing offsets within the same parent:
1464 * A
1465 * C (parent = A, offset = 16)
1466 * B (parent = A, offset = 0) // decreasing offset within A
1467 *
1468 * Referring to a parent that wasn't the last object or any of its parents:
1469 * A
1470 * B (parent = A, offset = 0)
1471 * C (parent = A, offset = 0)
1472 * C (parent = A, offset = 16)
1473 * D (parent = B, offset = 0) // B is not A or any of A's parents
1474 */
1475static bool binder_validate_fixup(struct binder_buffer *b,
1476 binder_size_t *objects_start,
1477 struct binder_buffer_object *buffer,
1478 binder_size_t fixup_offset,
1479 struct binder_buffer_object *last_obj,
1480 binder_size_t last_min_offset)
1481{
1482 if (!last_obj) {
1483 /* Nothing to fix up in */
1484 return false;
1485 }
1486
1487 while (last_obj != buffer) {
1488 /*
1489 * Safe to retrieve the parent of last_obj, since it
1490 * was already previously verified by the driver.
1491 */
1492 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
1493 return false;
1494 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
1495 last_obj = (struct binder_buffer_object *)
1496 (b->data + *(objects_start + last_obj->parent));
1497 }
1498 return (fixup_offset >= last_min_offset);
1499}
1500
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001501static void binder_transaction_buffer_release(struct binder_proc *proc,
1502 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001503 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001504{
Martijn Coenen5a6da532016-09-30 14:10:07 +02001505 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001506 int debug_id = buffer->debug_id;
1507
1508 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301509 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001510 proc->pid, buffer->debug_id,
1511 buffer->data_size, buffer->offsets_size, failed_at);
1512
1513 if (buffer->target_node)
1514 binder_dec_node(buffer->target_node, 1, 0);
1515
Martijn Coenen5a6da532016-09-30 14:10:07 +02001516 off_start = (binder_size_t *)(buffer->data +
1517 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001518 if (failed_at)
1519 off_end = failed_at;
1520 else
Martijn Coenen5a6da532016-09-30 14:10:07 +02001521 off_end = (void *)off_start + buffer->offsets_size;
1522 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02001523 struct binder_object_header *hdr;
1524 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09001525
Martijn Coenen00c80372016-07-13 12:06:49 +02001526 if (object_size == 0) {
1527 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001528 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001529 continue;
1530 }
Martijn Coenen00c80372016-07-13 12:06:49 +02001531 hdr = (struct binder_object_header *)(buffer->data + *offp);
1532 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001533 case BINDER_TYPE_BINDER:
1534 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02001535 struct flat_binder_object *fp;
1536 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09001537
Martijn Coenen00c80372016-07-13 12:06:49 +02001538 fp = to_flat_binder_object(hdr);
1539 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001540 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001541 pr_err("transaction release %d bad node %016llx\n",
1542 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001543 break;
1544 }
1545 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001546 " node %d u%016llx\n",
1547 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02001548 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
1549 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001550 } break;
1551 case BINDER_TYPE_HANDLE:
1552 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02001553 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001554 struct binder_ref *ref;
1555
Martijn Coenen00c80372016-07-13 12:06:49 +02001556 fp = to_flat_binder_object(hdr);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001557 ref = binder_get_ref(proc, fp->handle,
Martijn Coenen00c80372016-07-13 12:06:49 +02001558 hdr->type == BINDER_TYPE_HANDLE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001559 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001560 pr_err("transaction release %d bad handle %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301561 debug_id, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001562 break;
1563 }
1564 binder_debug(BINDER_DEBUG_TRANSACTION,
1565 " ref %d desc %d (node %d)\n",
1566 ref->debug_id, ref->desc, ref->node->debug_id);
Martijn Coenen00c80372016-07-13 12:06:49 +02001567 binder_dec_ref(ref, hdr->type == BINDER_TYPE_HANDLE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001568 } break;
1569
Martijn Coenen00c80372016-07-13 12:06:49 +02001570 case BINDER_TYPE_FD: {
1571 struct binder_fd_object *fp = to_binder_fd_object(hdr);
1572
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001573 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02001574 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001575 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02001576 task_close_fd(proc, fp->fd);
1577 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001578 case BINDER_TYPE_PTR:
1579 /*
1580 * Nothing to do here, this will get cleaned up when the
1581 * transaction buffer gets freed
1582 */
1583 break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001584 case BINDER_TYPE_FDA: {
1585 struct binder_fd_array_object *fda;
1586 struct binder_buffer_object *parent;
1587 uintptr_t parent_buffer;
1588 u32 *fd_array;
1589 size_t fd_index;
1590 binder_size_t fd_buf_size;
1591
1592 fda = to_binder_fd_array_object(hdr);
1593 parent = binder_validate_ptr(buffer, fda->parent,
1594 off_start,
1595 offp - off_start);
1596 if (!parent) {
1597 pr_err("transaction release %d bad parent offset",
1598 debug_id);
1599 continue;
1600 }
1601 /*
1602 * Since the parent was already fixed up, convert it
1603 * back to kernel address space to access it
1604 */
1605 parent_buffer = parent->buffer -
1606 proc->user_buffer_offset;
1607
1608 fd_buf_size = sizeof(u32) * fda->num_fds;
1609 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
1610 pr_err("transaction release %d invalid number of fds (%lld)\n",
1611 debug_id, (u64)fda->num_fds);
1612 continue;
1613 }
1614 if (fd_buf_size > parent->length ||
1615 fda->parent_offset > parent->length - fd_buf_size) {
1616 /* No space for all file descriptors here. */
1617 pr_err("transaction release %d not enough space for %lld fds in buffer\n",
1618 debug_id, (u64)fda->num_fds);
1619 continue;
1620 }
1621 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
1622 for (fd_index = 0; fd_index < fda->num_fds; fd_index++)
1623 task_close_fd(proc, fd_array[fd_index]);
1624 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001625 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001626 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02001627 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001628 break;
1629 }
1630 }
1631}
1632
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001633static int binder_translate_binder(struct flat_binder_object *fp,
1634 struct binder_transaction *t,
1635 struct binder_thread *thread)
1636{
1637 struct binder_node *node;
1638 struct binder_ref *ref;
1639 struct binder_proc *proc = thread->proc;
1640 struct binder_proc *target_proc = t->to_proc;
1641
1642 node = binder_get_node(proc, fp->binder);
1643 if (!node) {
1644 node = binder_new_node(proc, fp->binder, fp->cookie);
1645 if (!node)
1646 return -ENOMEM;
1647
1648 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1649 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1650 }
1651 if (fp->cookie != node->cookie) {
1652 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1653 proc->pid, thread->pid, (u64)fp->binder,
1654 node->debug_id, (u64)fp->cookie,
1655 (u64)node->cookie);
1656 return -EINVAL;
1657 }
1658 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk))
1659 return -EPERM;
1660
1661 ref = binder_get_ref_for_node(target_proc, node);
1662 if (!ref)
1663 return -EINVAL;
1664
1665 if (fp->hdr.type == BINDER_TYPE_BINDER)
1666 fp->hdr.type = BINDER_TYPE_HANDLE;
1667 else
1668 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
1669 fp->binder = 0;
1670 fp->handle = ref->desc;
1671 fp->cookie = 0;
1672 binder_inc_ref(ref, fp->hdr.type == BINDER_TYPE_HANDLE, &thread->todo);
1673
1674 trace_binder_transaction_node_to_ref(t, node, ref);
1675 binder_debug(BINDER_DEBUG_TRANSACTION,
1676 " node %d u%016llx -> ref %d desc %d\n",
1677 node->debug_id, (u64)node->ptr,
1678 ref->debug_id, ref->desc);
1679
1680 return 0;
1681}
1682
1683static int binder_translate_handle(struct flat_binder_object *fp,
1684 struct binder_transaction *t,
1685 struct binder_thread *thread)
1686{
1687 struct binder_ref *ref;
1688 struct binder_proc *proc = thread->proc;
1689 struct binder_proc *target_proc = t->to_proc;
1690
1691 ref = binder_get_ref(proc, fp->handle,
1692 fp->hdr.type == BINDER_TYPE_HANDLE);
1693 if (!ref) {
1694 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
1695 proc->pid, thread->pid, fp->handle);
1696 return -EINVAL;
1697 }
1698 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk))
1699 return -EPERM;
1700
1701 if (ref->node->proc == target_proc) {
1702 if (fp->hdr.type == BINDER_TYPE_HANDLE)
1703 fp->hdr.type = BINDER_TYPE_BINDER;
1704 else
1705 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
1706 fp->binder = ref->node->ptr;
1707 fp->cookie = ref->node->cookie;
1708 binder_inc_node(ref->node, fp->hdr.type == BINDER_TYPE_BINDER,
1709 0, NULL);
1710 trace_binder_transaction_ref_to_node(t, ref);
1711 binder_debug(BINDER_DEBUG_TRANSACTION,
1712 " ref %d desc %d -> node %d u%016llx\n",
1713 ref->debug_id, ref->desc, ref->node->debug_id,
1714 (u64)ref->node->ptr);
1715 } else {
1716 struct binder_ref *new_ref;
1717
1718 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1719 if (!new_ref)
1720 return -EINVAL;
1721
1722 fp->binder = 0;
1723 fp->handle = new_ref->desc;
1724 fp->cookie = 0;
1725 binder_inc_ref(new_ref, fp->hdr.type == BINDER_TYPE_HANDLE,
1726 NULL);
1727 trace_binder_transaction_ref_to_ref(t, ref, new_ref);
1728 binder_debug(BINDER_DEBUG_TRANSACTION,
1729 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1730 ref->debug_id, ref->desc, new_ref->debug_id,
1731 new_ref->desc, ref->node->debug_id);
1732 }
1733 return 0;
1734}
1735
1736static int binder_translate_fd(int fd,
1737 struct binder_transaction *t,
1738 struct binder_thread *thread,
1739 struct binder_transaction *in_reply_to)
1740{
1741 struct binder_proc *proc = thread->proc;
1742 struct binder_proc *target_proc = t->to_proc;
1743 int target_fd;
1744 struct file *file;
1745 int ret;
1746 bool target_allows_fd;
1747
1748 if (in_reply_to)
1749 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
1750 else
1751 target_allows_fd = t->buffer->target_node->accept_fds;
1752 if (!target_allows_fd) {
1753 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
1754 proc->pid, thread->pid,
1755 in_reply_to ? "reply" : "transaction",
1756 fd);
1757 ret = -EPERM;
1758 goto err_fd_not_accepted;
1759 }
1760
1761 file = fget(fd);
1762 if (!file) {
1763 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1764 proc->pid, thread->pid, fd);
1765 ret = -EBADF;
1766 goto err_fget;
1767 }
1768 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
1769 if (ret < 0) {
1770 ret = -EPERM;
1771 goto err_security;
1772 }
1773
1774 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1775 if (target_fd < 0) {
1776 ret = -ENOMEM;
1777 goto err_get_unused_fd;
1778 }
1779 task_fd_install(target_proc, target_fd, file);
1780 trace_binder_transaction_fd(t, fd, target_fd);
1781 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
1782 fd, target_fd);
1783
1784 return target_fd;
1785
1786err_get_unused_fd:
1787err_security:
1788 fput(file);
1789err_fget:
1790err_fd_not_accepted:
1791 return ret;
1792}
1793
Martijn Coenene3e0f4802016-10-18 13:58:55 +02001794static int binder_translate_fd_array(struct binder_fd_array_object *fda,
1795 struct binder_buffer_object *parent,
1796 struct binder_transaction *t,
1797 struct binder_thread *thread,
1798 struct binder_transaction *in_reply_to)
1799{
1800 binder_size_t fdi, fd_buf_size, num_installed_fds;
1801 int target_fd;
1802 uintptr_t parent_buffer;
1803 u32 *fd_array;
1804 struct binder_proc *proc = thread->proc;
1805 struct binder_proc *target_proc = t->to_proc;
1806
1807 fd_buf_size = sizeof(u32) * fda->num_fds;
1808 if (fda->num_fds >= SIZE_MAX / sizeof(u32)) {
1809 binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n",
1810 proc->pid, thread->pid, (u64)fda->num_fds);
1811 return -EINVAL;
1812 }
1813 if (fd_buf_size > parent->length ||
1814 fda->parent_offset > parent->length - fd_buf_size) {
1815 /* No space for all file descriptors here. */
1816 binder_user_error("%d:%d not enough space to store %lld fds in buffer\n",
1817 proc->pid, thread->pid, (u64)fda->num_fds);
1818 return -EINVAL;
1819 }
1820 /*
1821 * Since the parent was already fixed up, convert it
1822 * back to the kernel address space to access it
1823 */
1824 parent_buffer = parent->buffer - target_proc->user_buffer_offset;
1825 fd_array = (u32 *)(parent_buffer + fda->parent_offset);
1826 if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) {
1827 binder_user_error("%d:%d parent offset not aligned correctly.\n",
1828 proc->pid, thread->pid);
1829 return -EINVAL;
1830 }
1831 for (fdi = 0; fdi < fda->num_fds; fdi++) {
1832 target_fd = binder_translate_fd(fd_array[fdi], t, thread,
1833 in_reply_to);
1834 if (target_fd < 0)
1835 goto err_translate_fd_failed;
1836 fd_array[fdi] = target_fd;
1837 }
1838 return 0;
1839
1840err_translate_fd_failed:
1841 /*
1842 * Failed to allocate fd or security error, free fds
1843 * installed so far.
1844 */
1845 num_installed_fds = fdi;
1846 for (fdi = 0; fdi < num_installed_fds; fdi++)
1847 task_close_fd(target_proc, fd_array[fdi]);
1848 return target_fd;
1849}
1850
Martijn Coenen5a6da532016-09-30 14:10:07 +02001851static int binder_fixup_parent(struct binder_transaction *t,
1852 struct binder_thread *thread,
1853 struct binder_buffer_object *bp,
1854 binder_size_t *off_start,
1855 binder_size_t num_valid,
1856 struct binder_buffer_object *last_fixup_obj,
1857 binder_size_t last_fixup_min_off)
1858{
1859 struct binder_buffer_object *parent;
1860 u8 *parent_buffer;
1861 struct binder_buffer *b = t->buffer;
1862 struct binder_proc *proc = thread->proc;
1863 struct binder_proc *target_proc = t->to_proc;
1864
1865 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
1866 return 0;
1867
1868 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
1869 if (!parent) {
1870 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
1871 proc->pid, thread->pid);
1872 return -EINVAL;
1873 }
1874
1875 if (!binder_validate_fixup(b, off_start,
1876 parent, bp->parent_offset,
1877 last_fixup_obj,
1878 last_fixup_min_off)) {
1879 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
1880 proc->pid, thread->pid);
1881 return -EINVAL;
1882 }
1883
1884 if (parent->length < sizeof(binder_uintptr_t) ||
1885 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
1886 /* No space for a pointer here! */
1887 binder_user_error("%d:%d got transaction with invalid parent offset\n",
1888 proc->pid, thread->pid);
1889 return -EINVAL;
1890 }
1891 parent_buffer = (u8 *)(parent->buffer -
1892 target_proc->user_buffer_offset);
1893 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
1894
1895 return 0;
1896}
1897
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001898static void binder_transaction(struct binder_proc *proc,
1899 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02001900 struct binder_transaction_data *tr, int reply,
1901 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001902{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001903 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001904 struct binder_transaction *t;
1905 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001906 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001907 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001908 u8 *sg_bufp, *sg_buf_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001909 struct binder_proc *target_proc;
1910 struct binder_thread *target_thread = NULL;
1911 struct binder_node *target_node = NULL;
1912 struct list_head *target_list;
1913 wait_queue_head_t *target_wait;
1914 struct binder_transaction *in_reply_to = NULL;
1915 struct binder_transaction_log_entry *e;
1916 uint32_t return_error;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001917 struct binder_buffer_object *last_fixup_obj = NULL;
1918 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001919 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001920
1921 e = binder_transaction_log_add(&binder_transaction_log);
1922 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1923 e->from_proc = proc->pid;
1924 e->from_thread = thread->pid;
1925 e->target_handle = tr->target.handle;
1926 e->data_size = tr->data_size;
1927 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02001928 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001929
1930 if (reply) {
1931 in_reply_to = thread->transaction_stack;
1932 if (in_reply_to == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301933 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001934 proc->pid, thread->pid);
1935 return_error = BR_FAILED_REPLY;
1936 goto err_empty_call_stack;
1937 }
1938 binder_set_nice(in_reply_to->saved_priority);
1939 if (in_reply_to->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301940 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 +09001941 proc->pid, thread->pid, in_reply_to->debug_id,
1942 in_reply_to->to_proc ?
1943 in_reply_to->to_proc->pid : 0,
1944 in_reply_to->to_thread ?
1945 in_reply_to->to_thread->pid : 0);
1946 return_error = BR_FAILED_REPLY;
1947 in_reply_to = NULL;
1948 goto err_bad_call_stack;
1949 }
1950 thread->transaction_stack = in_reply_to->to_parent;
1951 target_thread = in_reply_to->from;
1952 if (target_thread == NULL) {
1953 return_error = BR_DEAD_REPLY;
1954 goto err_dead_binder;
1955 }
1956 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301957 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 +09001958 proc->pid, thread->pid,
1959 target_thread->transaction_stack ?
1960 target_thread->transaction_stack->debug_id : 0,
1961 in_reply_to->debug_id);
1962 return_error = BR_FAILED_REPLY;
1963 in_reply_to = NULL;
1964 target_thread = NULL;
1965 goto err_dead_binder;
1966 }
1967 target_proc = target_thread->proc;
1968 } else {
1969 if (tr->target.handle) {
1970 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001971
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001972 ref = binder_get_ref(proc, tr->target.handle, true);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001973 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301974 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001975 proc->pid, thread->pid);
1976 return_error = BR_FAILED_REPLY;
1977 goto err_invalid_target_handle;
1978 }
1979 target_node = ref->node;
1980 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001981 target_node = context->binder_context_mgr_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001982 if (target_node == NULL) {
1983 return_error = BR_DEAD_REPLY;
1984 goto err_no_context_mgr_node;
1985 }
1986 }
1987 e->to_node = target_node->debug_id;
1988 target_proc = target_node->proc;
1989 if (target_proc == NULL) {
1990 return_error = BR_DEAD_REPLY;
1991 goto err_dead_binder;
1992 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001993 if (security_binder_transaction(proc->tsk,
1994 target_proc->tsk) < 0) {
1995 return_error = BR_FAILED_REPLY;
1996 goto err_invalid_target_handle;
1997 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001998 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1999 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002000
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002001 tmp = thread->transaction_stack;
2002 if (tmp->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302003 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 +09002004 proc->pid, thread->pid, tmp->debug_id,
2005 tmp->to_proc ? tmp->to_proc->pid : 0,
2006 tmp->to_thread ?
2007 tmp->to_thread->pid : 0);
2008 return_error = BR_FAILED_REPLY;
2009 goto err_bad_call_stack;
2010 }
2011 while (tmp) {
2012 if (tmp->from && tmp->from->proc == target_proc)
2013 target_thread = tmp->from;
2014 tmp = tmp->from_parent;
2015 }
2016 }
2017 }
2018 if (target_thread) {
2019 e->to_thread = target_thread->pid;
2020 target_list = &target_thread->todo;
2021 target_wait = &target_thread->wait;
2022 } else {
2023 target_list = &target_proc->todo;
2024 target_wait = &target_proc->wait;
2025 }
2026 e->to_proc = target_proc->pid;
2027
2028 /* TODO: reuse incoming transaction for reply */
Riley Andrewsf48e2692015-09-15 10:49:46 -07002029 t = kzalloc_preempt_disabled(sizeof(*t));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002030 if (t == NULL) {
2031 return_error = BR_FAILED_REPLY;
2032 goto err_alloc_t_failed;
2033 }
2034 binder_stats_created(BINDER_STAT_TRANSACTION);
2035
Riley Andrewsf48e2692015-09-15 10:49:46 -07002036 tcomplete = kzalloc_preempt_disabled(sizeof(*tcomplete));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002037 if (tcomplete == NULL) {
2038 return_error = BR_FAILED_REPLY;
2039 goto err_alloc_tcomplete_failed;
2040 }
2041 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
2042
2043 t->debug_id = ++binder_last_id;
2044 e->debug_id = t->debug_id;
2045
2046 if (reply)
2047 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02002048 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002049 proc->pid, thread->pid, t->debug_id,
2050 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002051 (u64)tr->data.ptr.buffer,
2052 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02002053 (u64)tr->data_size, (u64)tr->offsets_size,
2054 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002055 else
2056 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02002057 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002058 proc->pid, thread->pid, t->debug_id,
2059 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002060 (u64)tr->data.ptr.buffer,
2061 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02002062 (u64)tr->data_size, (u64)tr->offsets_size,
2063 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002064
2065 if (!reply && !(tr->flags & TF_ONE_WAY))
2066 t->from = thread;
2067 else
2068 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03002069 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002070 t->to_proc = target_proc;
2071 t->to_thread = target_thread;
2072 t->code = tr->code;
2073 t->flags = tr->flags;
2074 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002075
2076 trace_binder_transaction(reply, t, target_node);
2077
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002078 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02002079 tr->offsets_size, extra_buffers_size,
2080 !reply && (t->flags & TF_ONE_WAY));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002081 if (t->buffer == NULL) {
2082 return_error = BR_FAILED_REPLY;
2083 goto err_binder_alloc_buf_failed;
2084 }
2085 t->buffer->allow_user_free = 0;
2086 t->buffer->debug_id = t->debug_id;
2087 t->buffer->transaction = t;
2088 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002089 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002090 if (target_node)
2091 binder_inc_node(target_node, 1, 0, NULL);
2092
Martijn Coenen5a6da532016-09-30 14:10:07 +02002093 off_start = (binder_size_t *)(t->buffer->data +
2094 ALIGN(tr->data_size, sizeof(void *)));
2095 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002096
Riley Andrewsf48e2692015-09-15 10:49:46 -07002097 if (copy_from_user_preempt_disabled(t->buffer->data, (const void __user *)(uintptr_t)
Arve Hjønnevågda498892014-02-21 14:40:26 -08002098 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302099 binder_user_error("%d:%d got transaction with invalid data ptr\n",
2100 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002101 return_error = BR_FAILED_REPLY;
2102 goto err_copy_data_failed;
2103 }
Riley Andrewsf48e2692015-09-15 10:49:46 -07002104 if (copy_from_user_preempt_disabled(offp, (const void __user *)(uintptr_t)
Arve Hjønnevågda498892014-02-21 14:40:26 -08002105 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302106 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2107 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002108 return_error = BR_FAILED_REPLY;
2109 goto err_copy_data_failed;
2110 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08002111 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
2112 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
2113 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002114 return_error = BR_FAILED_REPLY;
2115 goto err_bad_offset;
2116 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02002117 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
2118 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
2119 proc->pid, thread->pid,
Amit Pundir44cbb182017-02-01 12:53:45 +05302120 (u64)extra_buffers_size);
Martijn Coenen5a6da532016-09-30 14:10:07 +02002121 return_error = BR_FAILED_REPLY;
2122 goto err_bad_offset;
2123 }
2124 off_end = (void *)off_start + tr->offsets_size;
2125 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
2126 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08002127 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002128 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02002129 struct binder_object_header *hdr;
2130 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09002131
Martijn Coenen00c80372016-07-13 12:06:49 +02002132 if (object_size == 0 || *offp < off_min) {
2133 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 -08002134 proc->pid, thread->pid, (u64)*offp,
2135 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02002136 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002137 return_error = BR_FAILED_REPLY;
2138 goto err_bad_offset;
2139 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002140
2141 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
2142 off_min = *offp + object_size;
2143 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002144 case BINDER_TYPE_BINDER:
2145 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002146 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09002147
Martijn Coenen00c80372016-07-13 12:06:49 +02002148 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002149 ret = binder_translate_binder(fp, t, thread);
2150 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02002151 return_error = BR_FAILED_REPLY;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002152 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002153 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002154 } break;
2155 case BINDER_TYPE_HANDLE:
2156 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002157 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002158
Martijn Coenen00c80372016-07-13 12:06:49 +02002159 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002160 ret = binder_translate_handle(fp, t, thread);
2161 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002162 return_error = BR_FAILED_REPLY;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002163 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002164 }
2165 } break;
2166
2167 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02002168 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002169 int target_fd = binder_translate_fd(fp->fd, t, thread,
2170 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002171
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002172 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002173 return_error = BR_FAILED_REPLY;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002174 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002175 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002176 fp->pad_binder = 0;
2177 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002178 } break;
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002179 case BINDER_TYPE_FDA: {
2180 struct binder_fd_array_object *fda =
2181 to_binder_fd_array_object(hdr);
2182 struct binder_buffer_object *parent =
2183 binder_validate_ptr(t->buffer, fda->parent,
2184 off_start,
2185 offp - off_start);
2186 if (!parent) {
2187 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
2188 proc->pid, thread->pid);
2189 return_error = BR_FAILED_REPLY;
2190 goto err_bad_parent;
2191 }
2192 if (!binder_validate_fixup(t->buffer, off_start,
2193 parent, fda->parent_offset,
2194 last_fixup_obj,
2195 last_fixup_min_off)) {
2196 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
2197 proc->pid, thread->pid);
2198 return_error = BR_FAILED_REPLY;
2199 goto err_bad_parent;
2200 }
2201 ret = binder_translate_fd_array(fda, parent, t, thread,
2202 in_reply_to);
2203 if (ret < 0) {
2204 return_error = BR_FAILED_REPLY;
2205 goto err_translate_failed;
2206 }
2207 last_fixup_obj = parent;
2208 last_fixup_min_off =
2209 fda->parent_offset + sizeof(u32) * fda->num_fds;
2210 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002211 case BINDER_TYPE_PTR: {
2212 struct binder_buffer_object *bp =
2213 to_binder_buffer_object(hdr);
2214 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002215
Martijn Coenen5a6da532016-09-30 14:10:07 +02002216 if (bp->length > buf_left) {
2217 binder_user_error("%d:%d got transaction with too large buffer\n",
2218 proc->pid, thread->pid);
2219 return_error = BR_FAILED_REPLY;
2220 goto err_bad_offset;
2221 }
Riley Andrewsf48e2692015-09-15 10:49:46 -07002222 if (copy_from_user_preempt_disabled(
2223 sg_bufp,
2224 (const void __user *)(uintptr_t)
2225 bp->buffer, bp->length)) {
Martijn Coenen5a6da532016-09-30 14:10:07 +02002226 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2227 proc->pid, thread->pid);
2228 return_error = BR_FAILED_REPLY;
2229 goto err_copy_data_failed;
2230 }
2231 /* Fixup buffer pointer to target proc address space */
2232 bp->buffer = (uintptr_t)sg_bufp +
2233 target_proc->user_buffer_offset;
2234 sg_bufp += ALIGN(bp->length, sizeof(u64));
2235
2236 ret = binder_fixup_parent(t, thread, bp, off_start,
2237 offp - off_start,
2238 last_fixup_obj,
2239 last_fixup_min_off);
2240 if (ret < 0) {
2241 return_error = BR_FAILED_REPLY;
2242 goto err_translate_failed;
2243 }
2244 last_fixup_obj = bp;
2245 last_fixup_min_off = 0;
2246 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002247 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002248 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002249 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002250 return_error = BR_FAILED_REPLY;
2251 goto err_bad_object_type;
2252 }
2253 }
2254 if (reply) {
2255 BUG_ON(t->buffer->async_transaction != 0);
2256 binder_pop_transaction(target_thread, in_reply_to);
2257 } else if (!(t->flags & TF_ONE_WAY)) {
2258 BUG_ON(t->buffer->async_transaction != 0);
2259 t->need_reply = 1;
2260 t->from_parent = thread->transaction_stack;
2261 thread->transaction_stack = t;
2262 } else {
2263 BUG_ON(target_node == NULL);
2264 BUG_ON(t->buffer->async_transaction != 1);
2265 if (target_node->has_async_transaction) {
2266 target_list = &target_node->async_todo;
2267 target_wait = NULL;
2268 } else
2269 target_node->has_async_transaction = 1;
2270 }
2271 t->work.type = BINDER_WORK_TRANSACTION;
2272 list_add_tail(&t->work.entry, target_list);
2273 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
2274 list_add_tail(&tcomplete->entry, &thread->todo);
2275 if (target_wait)
2276 wake_up_interruptible(target_wait);
2277 return;
2278
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002279err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002280err_bad_object_type:
2281err_bad_offset:
Martijn Coenene3e0f4802016-10-18 13:58:55 +02002282err_bad_parent:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002283err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002284 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002285 binder_transaction_buffer_release(target_proc, t->buffer, offp);
2286 t->buffer->transaction = NULL;
2287 binder_free_buf(target_proc, t->buffer);
2288err_binder_alloc_buf_failed:
2289 kfree(tcomplete);
2290 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2291err_alloc_tcomplete_failed:
2292 kfree(t);
2293 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2294err_alloc_t_failed:
2295err_bad_call_stack:
2296err_empty_call_stack:
2297err_dead_binder:
2298err_invalid_target_handle:
2299err_no_context_mgr_node:
2300 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002301 "%d:%d transaction failed %d, size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002302 proc->pid, thread->pid, return_error,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002303 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002304
2305 {
2306 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09002307
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002308 fe = binder_transaction_log_add(&binder_transaction_log_failed);
2309 *fe = *e;
2310 }
2311
2312 BUG_ON(thread->return_error != BR_OK);
2313 if (in_reply_to) {
2314 thread->return_error = BR_TRANSACTION_COMPLETE;
2315 binder_send_failed_reply(in_reply_to, return_error);
2316 } else
2317 thread->return_error = return_error;
2318}
2319
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002320static int binder_thread_write(struct binder_proc *proc,
2321 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002322 binder_uintptr_t binder_buffer, size_t size,
2323 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002324{
2325 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002326 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002327 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002328 void __user *ptr = buffer + *consumed;
2329 void __user *end = buffer + size;
2330
2331 while (ptr < end && thread->return_error == BR_OK) {
Riley Andrewsf48e2692015-09-15 10:49:46 -07002332 if (get_user_preempt_disabled(cmd, (uint32_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002333 return -EFAULT;
2334 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002335 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002336 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
2337 binder_stats.bc[_IOC_NR(cmd)]++;
2338 proc->stats.bc[_IOC_NR(cmd)]++;
2339 thread->stats.bc[_IOC_NR(cmd)]++;
2340 }
2341 switch (cmd) {
2342 case BC_INCREFS:
2343 case BC_ACQUIRE:
2344 case BC_RELEASE:
2345 case BC_DECREFS: {
2346 uint32_t target;
2347 struct binder_ref *ref;
2348 const char *debug_string;
2349
Riley Andrewsf48e2692015-09-15 10:49:46 -07002350 if (get_user_preempt_disabled(target, (uint32_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002351 return -EFAULT;
2352 ptr += sizeof(uint32_t);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002353 if (target == 0 && context->binder_context_mgr_node &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002354 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
2355 ref = binder_get_ref_for_node(proc,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002356 context->binder_context_mgr_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002357 if (ref->desc != target) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302358 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002359 proc->pid, thread->pid,
2360 ref->desc);
2361 }
2362 } else
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002363 ref = binder_get_ref(proc, target,
2364 cmd == BC_ACQUIRE ||
2365 cmd == BC_RELEASE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002366 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302367 binder_user_error("%d:%d refcount change on invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002368 proc->pid, thread->pid, target);
2369 break;
2370 }
2371 switch (cmd) {
2372 case BC_INCREFS:
2373 debug_string = "IncRefs";
2374 binder_inc_ref(ref, 0, NULL);
2375 break;
2376 case BC_ACQUIRE:
2377 debug_string = "Acquire";
2378 binder_inc_ref(ref, 1, NULL);
2379 break;
2380 case BC_RELEASE:
2381 debug_string = "Release";
2382 binder_dec_ref(ref, 1);
2383 break;
2384 case BC_DECREFS:
2385 default:
2386 debug_string = "DecRefs";
2387 binder_dec_ref(ref, 0);
2388 break;
2389 }
2390 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302391 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002392 proc->pid, thread->pid, debug_string, ref->debug_id,
2393 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
2394 break;
2395 }
2396 case BC_INCREFS_DONE:
2397 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002398 binder_uintptr_t node_ptr;
2399 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002400 struct binder_node *node;
2401
Riley Andrewsf48e2692015-09-15 10:49:46 -07002402 if (get_user_preempt_disabled(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002403 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002404 ptr += sizeof(binder_uintptr_t);
Riley Andrewsf48e2692015-09-15 10:49:46 -07002405 if (get_user_preempt_disabled(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002406 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002407 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002408 node = binder_get_node(proc, node_ptr);
2409 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002410 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002411 proc->pid, thread->pid,
2412 cmd == BC_INCREFS_DONE ?
2413 "BC_INCREFS_DONE" :
2414 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002415 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002416 break;
2417 }
2418 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002419 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002420 proc->pid, thread->pid,
2421 cmd == BC_INCREFS_DONE ?
2422 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002423 (u64)node_ptr, node->debug_id,
2424 (u64)cookie, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002425 break;
2426 }
2427 if (cmd == BC_ACQUIRE_DONE) {
2428 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302429 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002430 proc->pid, thread->pid,
2431 node->debug_id);
2432 break;
2433 }
2434 node->pending_strong_ref = 0;
2435 } else {
2436 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302437 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002438 proc->pid, thread->pid,
2439 node->debug_id);
2440 break;
2441 }
2442 node->pending_weak_ref = 0;
2443 }
2444 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
2445 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302446 "%d:%d %s node %d ls %d lw %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002447 proc->pid, thread->pid,
2448 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
2449 node->debug_id, node->local_strong_refs, node->local_weak_refs);
2450 break;
2451 }
2452 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302453 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002454 return -EINVAL;
2455 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302456 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002457 return -EINVAL;
2458
2459 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002460 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002461 struct binder_buffer *buffer;
2462
Riley Andrewsf48e2692015-09-15 10:49:46 -07002463 if (get_user_preempt_disabled(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002464 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002465 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002466
2467 buffer = binder_buffer_lookup(proc, data_ptr);
2468 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002469 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
2470 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002471 break;
2472 }
2473 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002474 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
2475 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002476 break;
2477 }
2478 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002479 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
2480 proc->pid, thread->pid, (u64)data_ptr,
2481 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002482 buffer->transaction ? "active" : "finished");
2483
2484 if (buffer->transaction) {
2485 buffer->transaction->buffer = NULL;
2486 buffer->transaction = NULL;
2487 }
2488 if (buffer->async_transaction && buffer->target_node) {
2489 BUG_ON(!buffer->target_node->has_async_transaction);
2490 if (list_empty(&buffer->target_node->async_todo))
2491 buffer->target_node->has_async_transaction = 0;
2492 else
2493 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
2494 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002495 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002496 binder_transaction_buffer_release(proc, buffer, NULL);
2497 binder_free_buf(proc, buffer);
2498 break;
2499 }
2500
Martijn Coenen5a6da532016-09-30 14:10:07 +02002501 case BC_TRANSACTION_SG:
2502 case BC_REPLY_SG: {
2503 struct binder_transaction_data_sg tr;
2504
Riley Andrewsf48e2692015-09-15 10:49:46 -07002505 if (copy_from_user_preempt_disabled(&tr, ptr,
2506 sizeof(tr)))
Martijn Coenen5a6da532016-09-30 14:10:07 +02002507 return -EFAULT;
2508 ptr += sizeof(tr);
2509 binder_transaction(proc, thread, &tr.transaction_data,
2510 cmd == BC_REPLY_SG, tr.buffers_size);
2511 break;
2512 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002513 case BC_TRANSACTION:
2514 case BC_REPLY: {
2515 struct binder_transaction_data tr;
2516
Riley Andrewsf48e2692015-09-15 10:49:46 -07002517 if (copy_from_user_preempt_disabled(&tr, ptr, sizeof(tr)))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002518 return -EFAULT;
2519 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02002520 binder_transaction(proc, thread, &tr,
2521 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002522 break;
2523 }
2524
2525 case BC_REGISTER_LOOPER:
2526 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302527 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002528 proc->pid, thread->pid);
2529 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
2530 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302531 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002532 proc->pid, thread->pid);
2533 } else if (proc->requested_threads == 0) {
2534 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302535 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002536 proc->pid, thread->pid);
2537 } else {
2538 proc->requested_threads--;
2539 proc->requested_threads_started++;
2540 }
2541 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
2542 break;
2543 case BC_ENTER_LOOPER:
2544 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302545 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002546 proc->pid, thread->pid);
2547 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
2548 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302549 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002550 proc->pid, thread->pid);
2551 }
2552 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
2553 break;
2554 case BC_EXIT_LOOPER:
2555 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302556 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002557 proc->pid, thread->pid);
2558 thread->looper |= BINDER_LOOPER_STATE_EXITED;
2559 break;
2560
2561 case BC_REQUEST_DEATH_NOTIFICATION:
2562 case BC_CLEAR_DEATH_NOTIFICATION: {
2563 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002564 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002565 struct binder_ref *ref;
2566 struct binder_ref_death *death;
2567
Riley Andrewsf48e2692015-09-15 10:49:46 -07002568 if (get_user_preempt_disabled(target, (uint32_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002569 return -EFAULT;
2570 ptr += sizeof(uint32_t);
Riley Andrewsf48e2692015-09-15 10:49:46 -07002571 if (get_user_preempt_disabled(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002572 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002573 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002574 ref = binder_get_ref(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002575 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302576 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002577 proc->pid, thread->pid,
2578 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2579 "BC_REQUEST_DEATH_NOTIFICATION" :
2580 "BC_CLEAR_DEATH_NOTIFICATION",
2581 target);
2582 break;
2583 }
2584
2585 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002586 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002587 proc->pid, thread->pid,
2588 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2589 "BC_REQUEST_DEATH_NOTIFICATION" :
2590 "BC_CLEAR_DEATH_NOTIFICATION",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002591 (u64)cookie, ref->debug_id, ref->desc,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002592 ref->strong, ref->weak, ref->node->debug_id);
2593
2594 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2595 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302596 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002597 proc->pid, thread->pid);
2598 break;
2599 }
Riley Andrewsf48e2692015-09-15 10:49:46 -07002600 death = kzalloc_preempt_disabled(sizeof(*death));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002601 if (death == NULL) {
2602 thread->return_error = BR_ERROR;
2603 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302604 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002605 proc->pid, thread->pid);
2606 break;
2607 }
2608 binder_stats_created(BINDER_STAT_DEATH);
2609 INIT_LIST_HEAD(&death->work.entry);
2610 death->cookie = cookie;
2611 ref->death = death;
2612 if (ref->node->proc == NULL) {
2613 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2614 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2615 list_add_tail(&ref->death->work.entry, &thread->todo);
2616 } else {
2617 list_add_tail(&ref->death->work.entry, &proc->todo);
2618 wake_up_interruptible(&proc->wait);
2619 }
2620 }
2621 } else {
2622 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302623 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002624 proc->pid, thread->pid);
2625 break;
2626 }
2627 death = ref->death;
2628 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002629 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002630 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002631 (u64)death->cookie,
2632 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002633 break;
2634 }
2635 ref->death = NULL;
2636 if (list_empty(&death->work.entry)) {
2637 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2638 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2639 list_add_tail(&death->work.entry, &thread->todo);
2640 } else {
2641 list_add_tail(&death->work.entry, &proc->todo);
2642 wake_up_interruptible(&proc->wait);
2643 }
2644 } else {
2645 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2646 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2647 }
2648 }
2649 } break;
2650 case BC_DEAD_BINDER_DONE: {
2651 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002652 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002653 struct binder_ref_death *death = NULL;
Riley Andrewsf48e2692015-09-15 10:49:46 -07002654 if (get_user_preempt_disabled(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002655 return -EFAULT;
2656
Lisa Du7a64cd82016-02-17 09:32:52 +08002657 ptr += sizeof(cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002658 list_for_each_entry(w, &proc->delivered_death, entry) {
2659 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
Seunghun Lee10f62862014-05-01 01:30:23 +09002660
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002661 if (tmp_death->cookie == cookie) {
2662 death = tmp_death;
2663 break;
2664 }
2665 }
2666 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002667 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2668 proc->pid, thread->pid, (u64)cookie,
2669 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002670 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002671 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2672 proc->pid, thread->pid, (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002673 break;
2674 }
2675
2676 list_del_init(&death->work.entry);
2677 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2678 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2679 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2680 list_add_tail(&death->work.entry, &thread->todo);
2681 } else {
2682 list_add_tail(&death->work.entry, &proc->todo);
2683 wake_up_interruptible(&proc->wait);
2684 }
2685 }
Riley Andrewsf48e2692015-09-15 10:49:46 -07002686 }
2687 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002688
2689 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302690 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002691 proc->pid, thread->pid, cmd);
2692 return -EINVAL;
2693 }
2694 *consumed = ptr - buffer;
2695 }
2696 return 0;
2697}
2698
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002699static void binder_stat_br(struct binder_proc *proc,
2700 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002701{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002702 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002703 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2704 binder_stats.br[_IOC_NR(cmd)]++;
2705 proc->stats.br[_IOC_NR(cmd)]++;
2706 thread->stats.br[_IOC_NR(cmd)]++;
2707 }
2708}
2709
2710static int binder_has_proc_work(struct binder_proc *proc,
2711 struct binder_thread *thread)
2712{
2713 return !list_empty(&proc->todo) ||
2714 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2715}
2716
2717static int binder_has_thread_work(struct binder_thread *thread)
2718{
2719 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2720 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2721}
2722
2723static int binder_thread_read(struct binder_proc *proc,
2724 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002725 binder_uintptr_t binder_buffer, size_t size,
2726 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002727{
Arve Hjønnevågda498892014-02-21 14:40:26 -08002728 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002729 void __user *ptr = buffer + *consumed;
2730 void __user *end = buffer + size;
2731
2732 int ret = 0;
2733 int wait_for_proc_work;
2734
2735 if (*consumed == 0) {
Riley Andrewsf48e2692015-09-15 10:49:46 -07002736 if (put_user_preempt_disabled(BR_NOOP, (uint32_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002737 return -EFAULT;
2738 ptr += sizeof(uint32_t);
2739 }
2740
2741retry:
2742 wait_for_proc_work = thread->transaction_stack == NULL &&
2743 list_empty(&thread->todo);
2744
2745 if (thread->return_error != BR_OK && ptr < end) {
2746 if (thread->return_error2 != BR_OK) {
Riley Andrewsf48e2692015-09-15 10:49:46 -07002747 if (put_user_preempt_disabled(thread->return_error2, (uint32_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002748 return -EFAULT;
2749 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002750 binder_stat_br(proc, thread, thread->return_error2);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002751 if (ptr == end)
2752 goto done;
2753 thread->return_error2 = BR_OK;
2754 }
Riley Andrewsf48e2692015-09-15 10:49:46 -07002755 if (put_user_preempt_disabled(thread->return_error, (uint32_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002756 return -EFAULT;
2757 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002758 binder_stat_br(proc, thread, thread->return_error);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002759 thread->return_error = BR_OK;
2760 goto done;
2761 }
2762
2763
2764 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2765 if (wait_for_proc_work)
2766 proc->ready_threads++;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002767
2768 binder_unlock(__func__);
2769
2770 trace_binder_wait_for_work(wait_for_proc_work,
2771 !!thread->transaction_stack,
2772 !list_empty(&thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002773 if (wait_for_proc_work) {
2774 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2775 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302776 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 +09002777 proc->pid, thread->pid, thread->looper);
2778 wait_event_interruptible(binder_user_error_wait,
2779 binder_stop_on_user_error < 2);
2780 }
2781 binder_set_nice(proc->default_priority);
2782 if (non_block) {
2783 if (!binder_has_proc_work(proc, thread))
2784 ret = -EAGAIN;
2785 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002786 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002787 } else {
2788 if (non_block) {
2789 if (!binder_has_thread_work(thread))
2790 ret = -EAGAIN;
2791 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002792 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002793 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002794
2795 binder_lock(__func__);
2796
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002797 if (wait_for_proc_work)
2798 proc->ready_threads--;
2799 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2800
2801 if (ret)
2802 return ret;
2803
2804 while (1) {
2805 uint32_t cmd;
2806 struct binder_transaction_data tr;
2807 struct binder_work *w;
2808 struct binder_transaction *t = NULL;
2809
Dmitry Voytik395262a2014-09-08 18:16:34 +04002810 if (!list_empty(&thread->todo)) {
2811 w = list_first_entry(&thread->todo, struct binder_work,
2812 entry);
2813 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2814 w = list_first_entry(&proc->todo, struct binder_work,
2815 entry);
2816 } else {
2817 /* no data added */
2818 if (ptr - buffer == 4 &&
2819 !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002820 goto retry;
2821 break;
2822 }
2823
2824 if (end - ptr < sizeof(tr) + 4)
2825 break;
2826
2827 switch (w->type) {
2828 case BINDER_WORK_TRANSACTION: {
2829 t = container_of(w, struct binder_transaction, work);
2830 } break;
2831 case BINDER_WORK_TRANSACTION_COMPLETE: {
2832 cmd = BR_TRANSACTION_COMPLETE;
Riley Andrewsf48e2692015-09-15 10:49:46 -07002833 if (put_user_preempt_disabled(cmd, (uint32_t __user *) ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002834 return -EFAULT;
2835 ptr += sizeof(uint32_t);
2836
2837 binder_stat_br(proc, thread, cmd);
2838 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302839 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002840 proc->pid, thread->pid);
2841
2842 list_del(&w->entry);
2843 kfree(w);
2844 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2845 } break;
2846 case BINDER_WORK_NODE: {
2847 struct binder_node *node = container_of(w, struct binder_node, work);
2848 uint32_t cmd = BR_NOOP;
2849 const char *cmd_name;
2850 int strong = node->internal_strong_refs || node->local_strong_refs;
2851 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
Seunghun Lee10f62862014-05-01 01:30:23 +09002852
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002853 if (weak && !node->has_weak_ref) {
2854 cmd = BR_INCREFS;
2855 cmd_name = "BR_INCREFS";
2856 node->has_weak_ref = 1;
2857 node->pending_weak_ref = 1;
2858 node->local_weak_refs++;
2859 } else if (strong && !node->has_strong_ref) {
2860 cmd = BR_ACQUIRE;
2861 cmd_name = "BR_ACQUIRE";
2862 node->has_strong_ref = 1;
2863 node->pending_strong_ref = 1;
2864 node->local_strong_refs++;
2865 } else if (!strong && node->has_strong_ref) {
2866 cmd = BR_RELEASE;
2867 cmd_name = "BR_RELEASE";
2868 node->has_strong_ref = 0;
2869 } else if (!weak && node->has_weak_ref) {
2870 cmd = BR_DECREFS;
2871 cmd_name = "BR_DECREFS";
2872 node->has_weak_ref = 0;
2873 }
2874 if (cmd != BR_NOOP) {
Riley Andrewsf48e2692015-09-15 10:49:46 -07002875 if (put_user_preempt_disabled(cmd, (uint32_t __user *) ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002876 return -EFAULT;
2877 ptr += sizeof(uint32_t);
Riley Andrewsf48e2692015-09-15 10:49:46 -07002878 if (put_user_preempt_disabled(node->ptr, (binder_uintptr_t __user *)
Arve Hjønnevågda498892014-02-21 14:40:26 -08002879 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002880 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002881 ptr += sizeof(binder_uintptr_t);
Riley Andrewsf48e2692015-09-15 10:49:46 -07002882 if (put_user_preempt_disabled(node->cookie, (binder_uintptr_t __user *)
Arve Hjønnevågda498892014-02-21 14:40:26 -08002883 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002884 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002885 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002886
2887 binder_stat_br(proc, thread, cmd);
2888 binder_debug(BINDER_DEBUG_USER_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002889 "%d:%d %s %d u%016llx c%016llx\n",
2890 proc->pid, thread->pid, cmd_name,
2891 node->debug_id,
2892 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002893 } else {
2894 list_del_init(&w->entry);
2895 if (!weak && !strong) {
2896 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002897 "%d:%d node %d u%016llx c%016llx deleted\n",
2898 proc->pid, thread->pid,
2899 node->debug_id,
2900 (u64)node->ptr,
2901 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002902 rb_erase(&node->rb_node, &proc->nodes);
2903 kfree(node);
2904 binder_stats_deleted(BINDER_STAT_NODE);
2905 } else {
2906 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002907 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2908 proc->pid, thread->pid,
2909 node->debug_id,
2910 (u64)node->ptr,
2911 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002912 }
2913 }
2914 } break;
2915 case BINDER_WORK_DEAD_BINDER:
2916 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2917 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2918 struct binder_ref_death *death;
2919 uint32_t cmd;
2920
2921 death = container_of(w, struct binder_ref_death, work);
2922 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2923 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2924 else
2925 cmd = BR_DEAD_BINDER;
Riley Andrewsf48e2692015-09-15 10:49:46 -07002926 if (put_user_preempt_disabled(cmd, (uint32_t __user *) ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002927 return -EFAULT;
2928 ptr += sizeof(uint32_t);
Riley Andrewsf48e2692015-09-15 10:49:46 -07002929 if (put_user_preempt_disabled(death->cookie, (binder_uintptr_t __user *) ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002930 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002931 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002932 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002933 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002934 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002935 proc->pid, thread->pid,
2936 cmd == BR_DEAD_BINDER ?
2937 "BR_DEAD_BINDER" :
2938 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002939 (u64)death->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002940
2941 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2942 list_del(&w->entry);
2943 kfree(death);
2944 binder_stats_deleted(BINDER_STAT_DEATH);
2945 } else
2946 list_move(&w->entry, &proc->delivered_death);
2947 if (cmd == BR_DEAD_BINDER)
2948 goto done; /* DEAD_BINDER notifications can cause transactions */
2949 } break;
2950 }
2951
2952 if (!t)
2953 continue;
2954
2955 BUG_ON(t->buffer == NULL);
2956 if (t->buffer->target_node) {
2957 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002958
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002959 tr.target.ptr = target_node->ptr;
2960 tr.cookie = target_node->cookie;
2961 t->saved_priority = task_nice(current);
2962 if (t->priority < target_node->min_priority &&
2963 !(t->flags & TF_ONE_WAY))
2964 binder_set_nice(t->priority);
2965 else if (!(t->flags & TF_ONE_WAY) ||
2966 t->saved_priority > target_node->min_priority)
2967 binder_set_nice(target_node->min_priority);
2968 cmd = BR_TRANSACTION;
2969 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002970 tr.target.ptr = 0;
2971 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002972 cmd = BR_REPLY;
2973 }
2974 tr.code = t->code;
2975 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002976 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002977
2978 if (t->from) {
2979 struct task_struct *sender = t->from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09002980
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002981 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002982 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002983 } else {
2984 tr.sender_pid = 0;
2985 }
2986
2987 tr.data_size = t->buffer->data_size;
2988 tr.offsets_size = t->buffer->offsets_size;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002989 tr.data.ptr.buffer = (binder_uintptr_t)(
2990 (uintptr_t)t->buffer->data +
2991 proc->user_buffer_offset);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002992 tr.data.ptr.offsets = tr.data.ptr.buffer +
2993 ALIGN(t->buffer->data_size,
2994 sizeof(void *));
2995
Riley Andrewsf48e2692015-09-15 10:49:46 -07002996 if (put_user_preempt_disabled(cmd, (uint32_t __user *) ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002997 return -EFAULT;
2998 ptr += sizeof(uint32_t);
Riley Andrewsf48e2692015-09-15 10:49:46 -07002999 if (copy_to_user_preempt_disabled(ptr, &tr, sizeof(tr)))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003000 return -EFAULT;
3001 ptr += sizeof(tr);
3002
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003003 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003004 binder_stat_br(proc, thread, cmd);
3005 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003006 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003007 proc->pid, thread->pid,
3008 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
3009 "BR_REPLY",
3010 t->debug_id, t->from ? t->from->proc->pid : 0,
3011 t->from ? t->from->pid : 0, cmd,
3012 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003013 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003014
3015 list_del(&t->work.entry);
3016 t->buffer->allow_user_free = 1;
3017 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
3018 t->to_parent = thread->transaction_stack;
3019 t->to_thread = thread;
3020 thread->transaction_stack = t;
3021 } else {
3022 t->buffer->transaction = NULL;
3023 kfree(t);
3024 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3025 }
3026 break;
3027 }
3028
3029done:
3030
3031 *consumed = ptr - buffer;
3032 if (proc->requested_threads + proc->ready_threads == 0 &&
3033 proc->requested_threads_started < proc->max_threads &&
3034 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
3035 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
3036 /*spawn a new thread if we leave this out */) {
3037 proc->requested_threads++;
3038 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303039 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003040 proc->pid, thread->pid);
Riley Andrewsf48e2692015-09-15 10:49:46 -07003041 if (put_user_preempt_disabled(BR_SPAWN_LOOPER, (uint32_t __user *) buffer))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003042 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07003043 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003044 }
3045 return 0;
3046}
3047
3048static void binder_release_work(struct list_head *list)
3049{
3050 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09003051
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003052 while (!list_empty(list)) {
3053 w = list_first_entry(list, struct binder_work, entry);
3054 list_del_init(&w->entry);
3055 switch (w->type) {
3056 case BINDER_WORK_TRANSACTION: {
3057 struct binder_transaction *t;
3058
3059 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003060 if (t->buffer->target_node &&
3061 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003062 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003063 } else {
3064 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303065 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003066 t->debug_id);
3067 t->buffer->transaction = NULL;
3068 kfree(t);
3069 binder_stats_deleted(BINDER_STAT_TRANSACTION);
3070 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003071 } break;
3072 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003073 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303074 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003075 kfree(w);
3076 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
3077 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003078 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
3079 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
3080 struct binder_ref_death *death;
3081
3082 death = container_of(w, struct binder_ref_death, work);
3083 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003084 "undelivered death notification, %016llx\n",
3085 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003086 kfree(death);
3087 binder_stats_deleted(BINDER_STAT_DEATH);
3088 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003089 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303090 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003091 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003092 break;
3093 }
3094 }
3095
3096}
3097
3098static struct binder_thread *binder_get_thread(struct binder_proc *proc)
3099{
3100 struct binder_thread *thread = NULL;
3101 struct rb_node *parent = NULL;
3102 struct rb_node **p = &proc->threads.rb_node;
3103
3104 while (*p) {
3105 parent = *p;
3106 thread = rb_entry(parent, struct binder_thread, rb_node);
3107
3108 if (current->pid < thread->pid)
3109 p = &(*p)->rb_left;
3110 else if (current->pid > thread->pid)
3111 p = &(*p)->rb_right;
3112 else
3113 break;
3114 }
3115 if (*p == NULL) {
Riley Andrewsf48e2692015-09-15 10:49:46 -07003116 thread = kzalloc_preempt_disabled(sizeof(*thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003117 if (thread == NULL)
3118 return NULL;
3119 binder_stats_created(BINDER_STAT_THREAD);
3120 thread->proc = proc;
3121 thread->pid = current->pid;
3122 init_waitqueue_head(&thread->wait);
3123 INIT_LIST_HEAD(&thread->todo);
3124 rb_link_node(&thread->rb_node, parent, p);
3125 rb_insert_color(&thread->rb_node, &proc->threads);
3126 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3127 thread->return_error = BR_OK;
3128 thread->return_error2 = BR_OK;
3129 }
3130 return thread;
3131}
3132
3133static int binder_free_thread(struct binder_proc *proc,
3134 struct binder_thread *thread)
3135{
3136 struct binder_transaction *t;
3137 struct binder_transaction *send_reply = NULL;
3138 int active_transactions = 0;
3139
3140 rb_erase(&thread->rb_node, &proc->threads);
3141 t = thread->transaction_stack;
3142 if (t && t->to_thread == thread)
3143 send_reply = t;
3144 while (t) {
3145 active_transactions++;
3146 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303147 "release %d:%d transaction %d %s, still active\n",
3148 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003149 t->debug_id,
3150 (t->to_thread == thread) ? "in" : "out");
3151
3152 if (t->to_thread == thread) {
3153 t->to_proc = NULL;
3154 t->to_thread = NULL;
3155 if (t->buffer) {
3156 t->buffer->transaction = NULL;
3157 t->buffer = NULL;
3158 }
3159 t = t->to_parent;
3160 } else if (t->from == thread) {
3161 t->from = NULL;
3162 t = t->from_parent;
3163 } else
3164 BUG();
3165 }
3166 if (send_reply)
3167 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
3168 binder_release_work(&thread->todo);
3169 kfree(thread);
3170 binder_stats_deleted(BINDER_STAT_THREAD);
3171 return active_transactions;
3172}
3173
3174static unsigned int binder_poll(struct file *filp,
3175 struct poll_table_struct *wait)
3176{
3177 struct binder_proc *proc = filp->private_data;
3178 struct binder_thread *thread = NULL;
3179 int wait_for_proc_work;
3180
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003181 binder_lock(__func__);
3182
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003183 thread = binder_get_thread(proc);
3184
3185 wait_for_proc_work = thread->transaction_stack == NULL &&
3186 list_empty(&thread->todo) && thread->return_error == BR_OK;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003187
3188 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003189
3190 if (wait_for_proc_work) {
3191 if (binder_has_proc_work(proc, thread))
3192 return POLLIN;
3193 poll_wait(filp, &proc->wait, wait);
3194 if (binder_has_proc_work(proc, thread))
3195 return POLLIN;
3196 } else {
3197 if (binder_has_thread_work(thread))
3198 return POLLIN;
3199 poll_wait(filp, &thread->wait, wait);
3200 if (binder_has_thread_work(thread))
3201 return POLLIN;
3202 }
3203 return 0;
3204}
3205
Tair Rzayev78260ac2014-06-03 22:27:21 +03003206static int binder_ioctl_write_read(struct file *filp,
3207 unsigned int cmd, unsigned long arg,
3208 struct binder_thread *thread)
3209{
3210 int ret = 0;
3211 struct binder_proc *proc = filp->private_data;
3212 unsigned int size = _IOC_SIZE(cmd);
3213 void __user *ubuf = (void __user *)arg;
3214 struct binder_write_read bwr;
3215
3216 if (size != sizeof(struct binder_write_read)) {
3217 ret = -EINVAL;
3218 goto out;
3219 }
Riley Andrewsf48e2692015-09-15 10:49:46 -07003220 if (copy_from_user_preempt_disabled(&bwr, ubuf, sizeof(bwr))) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003221 ret = -EFAULT;
3222 goto out;
3223 }
3224 binder_debug(BINDER_DEBUG_READ_WRITE,
3225 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
3226 proc->pid, thread->pid,
3227 (u64)bwr.write_size, (u64)bwr.write_buffer,
3228 (u64)bwr.read_size, (u64)bwr.read_buffer);
3229
3230 if (bwr.write_size > 0) {
3231 ret = binder_thread_write(proc, thread,
3232 bwr.write_buffer,
3233 bwr.write_size,
3234 &bwr.write_consumed);
3235 trace_binder_write_done(ret);
3236 if (ret < 0) {
3237 bwr.read_consumed = 0;
Riley Andrewsf48e2692015-09-15 10:49:46 -07003238 if (copy_to_user_preempt_disabled(ubuf, &bwr, sizeof(bwr)))
Tair Rzayev78260ac2014-06-03 22:27:21 +03003239 ret = -EFAULT;
3240 goto out;
3241 }
3242 }
3243 if (bwr.read_size > 0) {
3244 ret = binder_thread_read(proc, thread, bwr.read_buffer,
3245 bwr.read_size,
3246 &bwr.read_consumed,
3247 filp->f_flags & O_NONBLOCK);
3248 trace_binder_read_done(ret);
3249 if (!list_empty(&proc->todo))
3250 wake_up_interruptible(&proc->wait);
3251 if (ret < 0) {
Riley Andrewsf48e2692015-09-15 10:49:46 -07003252 if (copy_to_user_preempt_disabled(ubuf, &bwr, sizeof(bwr)))
Tair Rzayev78260ac2014-06-03 22:27:21 +03003253 ret = -EFAULT;
3254 goto out;
3255 }
3256 }
3257 binder_debug(BINDER_DEBUG_READ_WRITE,
3258 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
3259 proc->pid, thread->pid,
3260 (u64)bwr.write_consumed, (u64)bwr.write_size,
3261 (u64)bwr.read_consumed, (u64)bwr.read_size);
Riley Andrewsf48e2692015-09-15 10:49:46 -07003262 if (copy_to_user_preempt_disabled(ubuf, &bwr, sizeof(bwr))) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003263 ret = -EFAULT;
3264 goto out;
3265 }
3266out:
3267 return ret;
3268}
3269
3270static int binder_ioctl_set_ctx_mgr(struct file *filp)
3271{
3272 int ret = 0;
3273 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003274 struct binder_context *context = proc->context;
3275
Tair Rzayev78260ac2014-06-03 22:27:21 +03003276 kuid_t curr_euid = current_euid();
3277
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003278 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003279 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
3280 ret = -EBUSY;
3281 goto out;
3282 }
Stephen Smalley79af7302015-01-21 10:54:10 -05003283 ret = security_binder_set_context_mgr(proc->tsk);
3284 if (ret < 0)
3285 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003286 if (uid_valid(context->binder_context_mgr_uid)) {
3287 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003288 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
3289 from_kuid(&init_user_ns, curr_euid),
3290 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003291 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03003292 ret = -EPERM;
3293 goto out;
3294 }
3295 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003296 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03003297 }
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003298 context->binder_context_mgr_node = binder_new_node(proc, 0, 0);
3299 if (!context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003300 ret = -ENOMEM;
3301 goto out;
3302 }
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003303 context->binder_context_mgr_node->local_weak_refs++;
3304 context->binder_context_mgr_node->local_strong_refs++;
3305 context->binder_context_mgr_node->has_strong_ref = 1;
3306 context->binder_context_mgr_node->has_weak_ref = 1;
Tair Rzayev78260ac2014-06-03 22:27:21 +03003307out:
3308 return ret;
3309}
3310
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003311static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3312{
3313 int ret;
3314 struct binder_proc *proc = filp->private_data;
3315 struct binder_thread *thread;
3316 unsigned int size = _IOC_SIZE(cmd);
3317 void __user *ubuf = (void __user *)arg;
3318
Tair Rzayev78260ac2014-06-03 22:27:21 +03003319 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
3320 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003321
Chen Fenga906d692016-02-01 14:04:02 +08003322 if (unlikely(current->mm != proc->vma_vm_mm)) {
3323 pr_err("current mm mismatch proc mm\n");
3324 return -EINVAL;
3325 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003326 trace_binder_ioctl(cmd, arg);
3327
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003328 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
3329 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003330 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003331
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003332 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003333 thread = binder_get_thread(proc);
3334 if (thread == NULL) {
3335 ret = -ENOMEM;
3336 goto err;
3337 }
3338
3339 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003340 case BINDER_WRITE_READ:
3341 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
3342 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003343 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003344 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003345 case BINDER_SET_MAX_THREADS:
Riley Andrewsf48e2692015-09-15 10:49:46 -07003346 if (copy_from_user_preempt_disabled(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003347 ret = -EINVAL;
3348 goto err;
3349 }
3350 break;
3351 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03003352 ret = binder_ioctl_set_ctx_mgr(filp);
3353 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003354 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003355 break;
3356 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303357 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003358 proc->pid, thread->pid);
3359 binder_free_thread(proc, thread);
3360 thread = NULL;
3361 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02003362 case BINDER_VERSION: {
3363 struct binder_version __user *ver = ubuf;
3364
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003365 if (size != sizeof(struct binder_version)) {
3366 ret = -EINVAL;
3367 goto err;
3368 }
Riley Andrewsf48e2692015-09-15 10:49:46 -07003369 if (put_user_preempt_disabled(BINDER_CURRENT_PROTOCOL_VERSION, &ver->protocol_version)) {
3370 ret = -EINVAL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003371 goto err;
3372 }
3373 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02003374 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003375 default:
3376 ret = -EINVAL;
3377 goto err;
3378 }
3379 ret = 0;
3380err:
3381 if (thread)
3382 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003383 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003384 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
3385 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05303386 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 -07003387err_unlocked:
3388 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003389 return ret;
3390}
3391
3392static void binder_vma_open(struct vm_area_struct *vma)
3393{
3394 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003395
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303397 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003398 proc->pid, vma->vm_start, vma->vm_end,
3399 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3400 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401}
3402
3403static void binder_vma_close(struct vm_area_struct *vma)
3404{
3405 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003406
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003407 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303408 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003409 proc->pid, vma->vm_start, vma->vm_end,
3410 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3411 (unsigned long)pgprot_val(vma->vm_page_prot));
3412 proc->vma = NULL;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08003413 proc->vma_vm_mm = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003414 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
3415}
3416
Vinayak Menonddac7d52014-06-02 18:17:59 +05303417static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3418{
3419 return VM_FAULT_SIGBUS;
3420}
3421
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07003422static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003423 .open = binder_vma_open,
3424 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05303425 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003426};
3427
3428static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
3429{
3430 int ret;
Riley Andrewsf48e2692015-09-15 10:49:46 -07003431
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003432 struct vm_struct *area;
3433 struct binder_proc *proc = filp->private_data;
3434 const char *failure_string;
3435 struct binder_buffer *buffer;
3436
Martijn Coenen872c26e2017-03-07 15:51:18 +01003437 if (proc->tsk != current->group_leader)
Al Viroa79f41e2012-08-15 18:23:36 -04003438 return -EINVAL;
3439
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003440 if ((vma->vm_end - vma->vm_start) > SZ_4M)
3441 vma->vm_end = vma->vm_start + SZ_4M;
3442
3443 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3444 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
3445 proc->pid, vma->vm_start, vma->vm_end,
3446 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3447 (unsigned long)pgprot_val(vma->vm_page_prot));
3448
3449 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
3450 ret = -EPERM;
3451 failure_string = "bad vm_flags";
3452 goto err_bad_arg;
3453 }
3454 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
3455
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08003456 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003457 if (proc->buffer) {
3458 ret = -EBUSY;
3459 failure_string = "already mapped";
3460 goto err_already_mapped;
3461 }
3462
3463 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
3464 if (area == NULL) {
3465 ret = -ENOMEM;
3466 failure_string = "get_vm_area";
3467 goto err_get_vm_area_failed;
3468 }
3469 proc->buffer = area->addr;
3470 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08003471 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003472
3473#ifdef CONFIG_CPU_CACHE_VIPT
3474 if (cache_is_vipt_aliasing()) {
3475 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
Sherwin Soltani258767f2012-06-26 02:00:30 -04003476 pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003477 vma->vm_start += PAGE_SIZE;
3478 }
3479 }
3480#endif
3481 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
3482 if (proc->pages == NULL) {
3483 ret = -ENOMEM;
3484 failure_string = "alloc page array";
3485 goto err_alloc_pages_failed;
3486 }
3487 proc->buffer_size = vma->vm_end - vma->vm_start;
3488
3489 vma->vm_ops = &binder_vm_ops;
3490 vma->vm_private_data = proc;
3491
Riley Andrewsf48e2692015-09-15 10:49:46 -07003492 /* binder_update_page_range assumes preemption is disabled */
3493 preempt_disable();
3494 ret = binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma);
3495 preempt_enable_no_resched();
3496 if (ret) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003497 ret = -ENOMEM;
3498 failure_string = "alloc small buf";
3499 goto err_alloc_small_buf_failed;
3500 }
3501 buffer = proc->buffer;
3502 INIT_LIST_HEAD(&proc->buffers);
3503 list_add(&buffer->entry, &proc->buffers);
3504 buffer->free = 1;
3505 binder_insert_free_buffer(proc, buffer);
3506 proc->free_async_space = proc->buffer_size / 2;
3507 barrier();
Al Viroa79f41e2012-08-15 18:23:36 -04003508 proc->files = get_files_struct(current);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003509 proc->vma = vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08003510 proc->vma_vm_mm = vma->vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003511
Sherwin Soltani258767f2012-06-26 02:00:30 -04003512 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003513 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
3514 return 0;
3515
3516err_alloc_small_buf_failed:
3517 kfree(proc->pages);
3518 proc->pages = NULL;
3519err_alloc_pages_failed:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08003520 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003521 vfree(proc->buffer);
3522 proc->buffer = NULL;
3523err_get_vm_area_failed:
3524err_already_mapped:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08003525 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003526err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04003527 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003528 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
3529 return ret;
3530}
3531
3532static int binder_open(struct inode *nodp, struct file *filp)
3533{
3534 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003535 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003536
3537 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
3538 current->group_leader->pid, current->pid);
3539
3540 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
3541 if (proc == NULL)
3542 return -ENOMEM;
Martijn Coenen872c26e2017-03-07 15:51:18 +01003543 get_task_struct(current->group_leader);
3544 proc->tsk = current->group_leader;
3545 proc->vma_vm_mm = current->group_leader->mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003546 INIT_LIST_HEAD(&proc->todo);
3547 init_waitqueue_head(&proc->wait);
3548 proc->default_priority = task_nice(current);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003549 binder_dev = container_of(filp->private_data, struct binder_device,
3550 miscdev);
3551 proc->context = &binder_dev->context;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003552
3553 binder_lock(__func__);
3554
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003555 binder_stats_created(BINDER_STAT_PROC);
3556 hlist_add_head(&proc->proc_node, &binder_procs);
3557 proc->pid = current->group_leader->pid;
3558 INIT_LIST_HEAD(&proc->delivered_death);
3559 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003560
3561 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003562
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003563 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003564 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09003565
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003566 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003567 /*
3568 * proc debug entries are shared between contexts, so
3569 * this will fail if the process tries to open the driver
3570 * again with a different context. The priting code will
3571 * anyway print all contexts that a given PID has, so this
3572 * is not a problem.
3573 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003574 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003575 binder_debugfs_dir_entry_proc,
3576 (void *)(unsigned long)proc->pid,
3577 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003578 }
3579
3580 return 0;
3581}
3582
3583static int binder_flush(struct file *filp, fl_owner_t id)
3584{
3585 struct binder_proc *proc = filp->private_data;
3586
3587 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3588
3589 return 0;
3590}
3591
3592static void binder_deferred_flush(struct binder_proc *proc)
3593{
3594 struct rb_node *n;
3595 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09003596
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003597 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3598 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09003599
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003600 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3601 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3602 wake_up_interruptible(&thread->wait);
3603 wake_count++;
3604 }
3605 }
3606 wake_up_interruptible_all(&proc->wait);
3607
3608 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3609 "binder_flush: %d woke %d threads\n", proc->pid,
3610 wake_count);
3611}
3612
3613static int binder_release(struct inode *nodp, struct file *filp)
3614{
3615 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003616
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003617 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003618 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3619
3620 return 0;
3621}
3622
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003623static int binder_node_release(struct binder_node *node, int refs)
3624{
3625 struct binder_ref *ref;
3626 int death = 0;
3627
3628 list_del_init(&node->work.entry);
3629 binder_release_work(&node->async_todo);
3630
3631 if (hlist_empty(&node->refs)) {
3632 kfree(node);
3633 binder_stats_deleted(BINDER_STAT_NODE);
3634
3635 return refs;
3636 }
3637
3638 node->proc = NULL;
3639 node->local_strong_refs = 0;
3640 node->local_weak_refs = 0;
3641 hlist_add_head(&node->dead_node, &binder_dead_nodes);
3642
3643 hlist_for_each_entry(ref, &node->refs, node_entry) {
3644 refs++;
3645
3646 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08003647 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003648
3649 death++;
3650
3651 if (list_empty(&ref->death->work.entry)) {
3652 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3653 list_add_tail(&ref->death->work.entry,
3654 &ref->proc->todo);
3655 wake_up_interruptible(&ref->proc->wait);
3656 } else
3657 BUG();
3658 }
3659
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003660 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3661 "node %d now dead, refs %d, death %d\n",
3662 node->debug_id, refs, death);
3663
3664 return refs;
3665}
3666
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003667static void binder_deferred_release(struct binder_proc *proc)
3668{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003669 struct binder_transaction *t;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003670 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003671 struct rb_node *n;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003672 int threads, nodes, incoming_refs, outgoing_refs, buffers,
3673 active_transactions, page_count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003674
3675 BUG_ON(proc->vma);
3676 BUG_ON(proc->files);
3677
3678 hlist_del(&proc->proc_node);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003679
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003680 if (context->binder_context_mgr_node &&
3681 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003682 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003683 "%s: %d context_mgr_node gone\n",
3684 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003685 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003686 }
3687
3688 threads = 0;
3689 active_transactions = 0;
3690 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003691 struct binder_thread *thread;
3692
3693 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003694 threads++;
3695 active_transactions += binder_free_thread(proc, thread);
3696 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003697
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003698 nodes = 0;
3699 incoming_refs = 0;
3700 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003701 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003702
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003703 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003704 nodes++;
3705 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003706 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003707 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003708
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003709 outgoing_refs = 0;
3710 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003711 struct binder_ref *ref;
3712
3713 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003714 outgoing_refs++;
3715 binder_delete_ref(ref);
3716 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003717
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003718 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003719 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003720
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003721 buffers = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003722 while ((n = rb_first(&proc->allocated_buffers))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003723 struct binder_buffer *buffer;
3724
3725 buffer = rb_entry(n, struct binder_buffer, rb_node);
3726
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003727 t = buffer->transaction;
3728 if (t) {
3729 t->buffer = NULL;
3730 buffer->transaction = NULL;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303731 pr_err("release proc %d, transaction %d, not freed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003732 proc->pid, t->debug_id);
3733 /*BUG();*/
3734 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003735
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003736 binder_free_buf(proc, buffer);
3737 buffers++;
3738 }
3739
3740 binder_stats_deleted(BINDER_STAT_PROC);
3741
3742 page_count = 0;
3743 if (proc->pages) {
3744 int i;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003745
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003746 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003747 void *page_addr;
3748
3749 if (!proc->pages[i])
3750 continue;
3751
3752 page_addr = proc->buffer + i * PAGE_SIZE;
3753 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003754 "%s: %d: page %d at %p not freed\n",
3755 __func__, proc->pid, i, page_addr);
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003756 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3757 __free_page(proc->pages[i]);
3758 page_count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003759 }
3760 kfree(proc->pages);
3761 vfree(proc->buffer);
3762 }
3763
3764 put_task_struct(proc->tsk);
3765
3766 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003767 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3768 __func__, proc->pid, threads, nodes, incoming_refs,
3769 outgoing_refs, active_transactions, buffers, page_count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003770
3771 kfree(proc);
3772}
3773
3774static void binder_deferred_func(struct work_struct *work)
3775{
3776 struct binder_proc *proc;
3777 struct files_struct *files;
3778
3779 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003780
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003781 do {
Riley Andrewsf48e2692015-09-15 10:49:46 -07003782 trace_binder_lock(__func__);
3783 mutex_lock(&binder_main_lock);
3784 trace_binder_locked(__func__);
3785
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003786 mutex_lock(&binder_deferred_lock);
Riley Andrewsf48e2692015-09-15 10:49:46 -07003787 preempt_disable();
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003788 if (!hlist_empty(&binder_deferred_list)) {
3789 proc = hlist_entry(binder_deferred_list.first,
3790 struct binder_proc, deferred_work_node);
3791 hlist_del_init(&proc->deferred_work_node);
3792 defer = proc->deferred_work;
3793 proc->deferred_work = 0;
3794 } else {
3795 proc = NULL;
3796 defer = 0;
3797 }
3798 mutex_unlock(&binder_deferred_lock);
3799
3800 files = NULL;
3801 if (defer & BINDER_DEFERRED_PUT_FILES) {
3802 files = proc->files;
3803 if (files)
3804 proc->files = NULL;
3805 }
3806
3807 if (defer & BINDER_DEFERRED_FLUSH)
3808 binder_deferred_flush(proc);
3809
3810 if (defer & BINDER_DEFERRED_RELEASE)
3811 binder_deferred_release(proc); /* frees proc */
3812
Riley Andrewsf48e2692015-09-15 10:49:46 -07003813 trace_binder_unlock(__func__);
3814 mutex_unlock(&binder_main_lock);
3815 preempt_enable_no_resched();
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003816 if (files)
3817 put_files_struct(files);
3818 } while (proc);
3819}
3820static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3821
3822static void
3823binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3824{
3825 mutex_lock(&binder_deferred_lock);
3826 proc->deferred_work |= defer;
3827 if (hlist_unhashed(&proc->deferred_work_node)) {
3828 hlist_add_head(&proc->deferred_work_node,
3829 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05303830 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003831 }
3832 mutex_unlock(&binder_deferred_lock);
3833}
3834
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003835static void print_binder_transaction(struct seq_file *m, const char *prefix,
3836 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003837{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003838 seq_printf(m,
3839 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3840 prefix, t->debug_id, t,
3841 t->from ? t->from->proc->pid : 0,
3842 t->from ? t->from->pid : 0,
3843 t->to_proc ? t->to_proc->pid : 0,
3844 t->to_thread ? t->to_thread->pid : 0,
3845 t->code, t->flags, t->priority, t->need_reply);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003846 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003847 seq_puts(m, " buffer free\n");
3848 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003849 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003850 if (t->buffer->target_node)
3851 seq_printf(m, " node %d",
3852 t->buffer->target_node->debug_id);
3853 seq_printf(m, " size %zd:%zd data %p\n",
3854 t->buffer->data_size, t->buffer->offsets_size,
3855 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003856}
3857
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003858static void print_binder_buffer(struct seq_file *m, const char *prefix,
3859 struct binder_buffer *buffer)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003860{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003861 seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
3862 prefix, buffer->debug_id, buffer->data,
3863 buffer->data_size, buffer->offsets_size,
3864 buffer->transaction ? "active" : "delivered");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003865}
3866
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003867static void print_binder_work(struct seq_file *m, const char *prefix,
3868 const char *transaction_prefix,
3869 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003870{
3871 struct binder_node *node;
3872 struct binder_transaction *t;
3873
3874 switch (w->type) {
3875 case BINDER_WORK_TRANSACTION:
3876 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003877 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003878 break;
3879 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003880 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003881 break;
3882 case BINDER_WORK_NODE:
3883 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003884 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3885 prefix, node->debug_id,
3886 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003887 break;
3888 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003889 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003890 break;
3891 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003892 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003893 break;
3894 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003895 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003896 break;
3897 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003898 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003899 break;
3900 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003901}
3902
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003903static void print_binder_thread(struct seq_file *m,
3904 struct binder_thread *thread,
3905 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003906{
3907 struct binder_transaction *t;
3908 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003909 size_t start_pos = m->count;
3910 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003911
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003912 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3913 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003914 t = thread->transaction_stack;
3915 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003916 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003917 print_binder_transaction(m,
3918 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003919 t = t->from_parent;
3920 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003921 print_binder_transaction(m,
3922 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003923 t = t->to_parent;
3924 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003925 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003926 t = NULL;
3927 }
3928 }
3929 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003930 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003931 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003932 if (!print_always && m->count == header_pos)
3933 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003934}
3935
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003936static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003937{
3938 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003939 struct binder_work *w;
3940 int count;
3941
3942 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003943 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003944 count++;
3945
Arve Hjønnevågda498892014-02-21 14:40:26 -08003946 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3947 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003948 node->has_strong_ref, node->has_weak_ref,
3949 node->local_strong_refs, node->local_weak_refs,
3950 node->internal_strong_refs, count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003951 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003952 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003953 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003954 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003955 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003956 seq_puts(m, "\n");
3957 list_for_each_entry(w, &node->async_todo, entry)
3958 print_binder_work(m, " ",
3959 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003960}
3961
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003962static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003963{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003964 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3965 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3966 ref->node->debug_id, ref->strong, ref->weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003967}
3968
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003969static void print_binder_proc(struct seq_file *m,
3970 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003971{
3972 struct binder_work *w;
3973 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003974 size_t start_pos = m->count;
3975 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003976
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003977 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003978 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003979 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003980
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003981 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3982 print_binder_thread(m, rb_entry(n, struct binder_thread,
3983 rb_node), print_all);
3984 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003985 struct binder_node *node = rb_entry(n, struct binder_node,
3986 rb_node);
3987 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003988 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003989 }
3990 if (print_all) {
3991 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003992 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003993 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003994 print_binder_ref(m, rb_entry(n, struct binder_ref,
3995 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003996 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003997 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3998 print_binder_buffer(m, " buffer",
3999 rb_entry(n, struct binder_buffer, rb_node));
4000 list_for_each_entry(w, &proc->todo, entry)
4001 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004002 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004003 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004004 break;
4005 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004006 if (!print_all && m->count == header_pos)
4007 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004008}
4009
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10004010static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004011 "BR_ERROR",
4012 "BR_OK",
4013 "BR_TRANSACTION",
4014 "BR_REPLY",
4015 "BR_ACQUIRE_RESULT",
4016 "BR_DEAD_REPLY",
4017 "BR_TRANSACTION_COMPLETE",
4018 "BR_INCREFS",
4019 "BR_ACQUIRE",
4020 "BR_RELEASE",
4021 "BR_DECREFS",
4022 "BR_ATTEMPT_ACQUIRE",
4023 "BR_NOOP",
4024 "BR_SPAWN_LOOPER",
4025 "BR_FINISHED",
4026 "BR_DEAD_BINDER",
4027 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
4028 "BR_FAILED_REPLY"
4029};
4030
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10004031static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004032 "BC_TRANSACTION",
4033 "BC_REPLY",
4034 "BC_ACQUIRE_RESULT",
4035 "BC_FREE_BUFFER",
4036 "BC_INCREFS",
4037 "BC_ACQUIRE",
4038 "BC_RELEASE",
4039 "BC_DECREFS",
4040 "BC_INCREFS_DONE",
4041 "BC_ACQUIRE_DONE",
4042 "BC_ATTEMPT_ACQUIRE",
4043 "BC_REGISTER_LOOPER",
4044 "BC_ENTER_LOOPER",
4045 "BC_EXIT_LOOPER",
4046 "BC_REQUEST_DEATH_NOTIFICATION",
4047 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02004048 "BC_DEAD_BINDER_DONE",
4049 "BC_TRANSACTION_SG",
4050 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004051};
4052
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10004053static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004054 "proc",
4055 "thread",
4056 "node",
4057 "ref",
4058 "death",
4059 "transaction",
4060 "transaction_complete"
4061};
4062
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004063static void print_binder_stats(struct seq_file *m, const char *prefix,
4064 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004065{
4066 int i;
4067
4068 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004069 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004070 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
4071 if (stats->bc[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004072 seq_printf(m, "%s%s: %d\n", prefix,
4073 binder_command_strings[i], stats->bc[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004074 }
4075
4076 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004077 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004078 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
4079 if (stats->br[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004080 seq_printf(m, "%s%s: %d\n", prefix,
4081 binder_return_strings[i], stats->br[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004082 }
4083
4084 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004085 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004086 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004087 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004088 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
4089 if (stats->obj_created[i] || stats->obj_deleted[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004090 seq_printf(m, "%s%s: active %d total %d\n", prefix,
4091 binder_objstat_strings[i],
4092 stats->obj_created[i] - stats->obj_deleted[i],
4093 stats->obj_created[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004094 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004095}
4096
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004097static void print_binder_proc_stats(struct seq_file *m,
4098 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004099{
4100 struct binder_work *w;
4101 struct rb_node *n;
4102 int count, strong, weak;
4103
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004104 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004105 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004106 count = 0;
4107 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
4108 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004109 seq_printf(m, " threads: %d\n", count);
4110 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004111 " ready threads %d\n"
4112 " free async space %zd\n", proc->requested_threads,
4113 proc->requested_threads_started, proc->max_threads,
4114 proc->ready_threads, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004115 count = 0;
4116 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
4117 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004118 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004119 count = 0;
4120 strong = 0;
4121 weak = 0;
4122 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
4123 struct binder_ref *ref = rb_entry(n, struct binder_ref,
4124 rb_node_desc);
4125 count++;
4126 strong += ref->strong;
4127 weak += ref->weak;
4128 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004129 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004130
4131 count = 0;
4132 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
4133 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004134 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004135
4136 count = 0;
4137 list_for_each_entry(w, &proc->todo, entry) {
4138 switch (w->type) {
4139 case BINDER_WORK_TRANSACTION:
4140 count++;
4141 break;
4142 default:
4143 break;
4144 }
4145 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004146 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004147
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004148 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004149}
4150
4151
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004152static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004153{
4154 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004155 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004156 int do_lock = !binder_debug_no_lock;
4157
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004158 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004159 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004160
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004161 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004162
4163 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004164 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08004165 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004166 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004167
Sasha Levinb67bfe02013-02-27 17:06:00 -08004168 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004169 print_binder_proc(m, proc, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004170 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004171 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004172 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004173}
4174
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004175static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004176{
4177 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004178 int do_lock = !binder_debug_no_lock;
4179
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004180 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004181 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004182
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004183 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004184
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004185 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004186
Sasha Levinb67bfe02013-02-27 17:06:00 -08004187 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004188 print_binder_proc_stats(m, proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004189 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004190 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004191 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004192}
4193
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004194static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004195{
4196 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004197 int do_lock = !binder_debug_no_lock;
4198
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004199 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004200 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004201
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004202 seq_puts(m, "binder transactions:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08004203 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004204 print_binder_proc(m, proc, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004205 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004206 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004207 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004208}
4209
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004210static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004211{
Riley Andrews83050a42016-02-09 21:05:33 -08004212 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004213 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004214 int do_lock = !binder_debug_no_lock;
4215
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004216 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004217 binder_lock(__func__);
Riley Andrews83050a42016-02-09 21:05:33 -08004218
4219 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004220 if (itr->pid == pid) {
4221 seq_puts(m, "binder proc state:\n");
4222 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08004223 }
4224 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004225 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004226 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004227 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004228}
4229
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004230static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004231 struct binder_transaction_log_entry *e)
4232{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004233 seq_printf(m,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004234 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d\n",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004235 e->debug_id, (e->call_type == 2) ? "reply" :
4236 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004237 e->from_thread, e->to_proc, e->to_thread, e->context_name,
4238 e->to_node, e->target_handle, e->data_size, e->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004239}
4240
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004241static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004242{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004243 struct binder_transaction_log *log = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004244 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004245
4246 if (log->full) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004247 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
4248 print_binder_transaction_log_entry(m, &log->entry[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004249 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004250 for (i = 0; i < log->next; i++)
4251 print_binder_transaction_log_entry(m, &log->entry[i]);
4252 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004253}
4254
4255static const struct file_operations binder_fops = {
4256 .owner = THIS_MODULE,
4257 .poll = binder_poll,
4258 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004259 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004260 .mmap = binder_mmap,
4261 .open = binder_open,
4262 .flush = binder_flush,
4263 .release = binder_release,
4264};
4265
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004266BINDER_DEBUG_ENTRY(state);
4267BINDER_DEBUG_ENTRY(stats);
4268BINDER_DEBUG_ENTRY(transactions);
4269BINDER_DEBUG_ENTRY(transaction_log);
4270
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004271static int __init init_binder_device(const char *name)
4272{
4273 int ret;
4274 struct binder_device *binder_device;
4275
4276 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
4277 if (!binder_device)
4278 return -ENOMEM;
4279
4280 binder_device->miscdev.fops = &binder_fops;
4281 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
4282 binder_device->miscdev.name = name;
4283
4284 binder_device->context.binder_context_mgr_uid = INVALID_UID;
4285 binder_device->context.name = name;
4286
4287 ret = misc_register(&binder_device->miscdev);
4288 if (ret < 0) {
4289 kfree(binder_device);
4290 return ret;
4291 }
4292
4293 hlist_add_head(&binder_device->hlist, &binder_devices);
4294
4295 return ret;
4296}
4297
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004298static int __init binder_init(void)
4299{
4300 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004301 char *device_name, *device_names;
4302 struct binder_device *device;
4303 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004304
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004305 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
4306 if (binder_debugfs_dir_entry_root)
4307 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
4308 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004309
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004310 if (binder_debugfs_dir_entry_root) {
4311 debugfs_create_file("state",
4312 S_IRUGO,
4313 binder_debugfs_dir_entry_root,
4314 NULL,
4315 &binder_state_fops);
4316 debugfs_create_file("stats",
4317 S_IRUGO,
4318 binder_debugfs_dir_entry_root,
4319 NULL,
4320 &binder_stats_fops);
4321 debugfs_create_file("transactions",
4322 S_IRUGO,
4323 binder_debugfs_dir_entry_root,
4324 NULL,
4325 &binder_transactions_fops);
4326 debugfs_create_file("transaction_log",
4327 S_IRUGO,
4328 binder_debugfs_dir_entry_root,
4329 &binder_transaction_log,
4330 &binder_transaction_log_fops);
4331 debugfs_create_file("failed_transaction_log",
4332 S_IRUGO,
4333 binder_debugfs_dir_entry_root,
4334 &binder_transaction_log_failed,
4335 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004336 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004337
4338 /*
4339 * Copy the module_parameter string, because we don't want to
4340 * tokenize it in-place.
4341 */
4342 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
4343 if (!device_names) {
4344 ret = -ENOMEM;
4345 goto err_alloc_device_names_failed;
4346 }
4347 strcpy(device_names, binder_devices_param);
4348
4349 while ((device_name = strsep(&device_names, ","))) {
4350 ret = init_binder_device(device_name);
4351 if (ret)
4352 goto err_init_binder_device_failed;
4353 }
4354
4355 return ret;
4356
4357err_init_binder_device_failed:
4358 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
4359 misc_deregister(&device->miscdev);
4360 hlist_del(&device->hlist);
4361 kfree(device);
4362 }
4363err_alloc_device_names_failed:
4364 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
4365
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004366 return ret;
4367}
4368
4369device_initcall(binder_init);
4370
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004371#define CREATE_TRACE_POINTS
4372#include "binder_trace.h"
4373
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004374MODULE_LICENSE("GPL v2");