blob: 5ce7ee98f3b52645a8a26bd58e6c9f0552c5fbba [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
53static HLIST_HEAD(binder_procs);
54static HLIST_HEAD(binder_deferred_list);
55static HLIST_HEAD(binder_dead_nodes);
56
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070057static struct dentry *binder_debugfs_dir_entry_root;
58static struct dentry *binder_debugfs_dir_entry_proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090059static struct binder_node *binder_context_mgr_node;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -060060static kuid_t binder_context_mgr_uid = INVALID_UID;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090061static int binder_last_id;
62
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070063#define BINDER_DEBUG_ENTRY(name) \
64static int binder_##name##_open(struct inode *inode, struct file *file) \
65{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070066 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070067} \
68\
69static const struct file_operations binder_##name##_fops = { \
70 .owner = THIS_MODULE, \
71 .open = binder_##name##_open, \
72 .read = seq_read, \
73 .llseek = seq_lseek, \
74 .release = single_release, \
75}
76
77static int binder_proc_show(struct seq_file *m, void *unused);
78BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090079
80/* This is only defined in include/asm-arm/sizes.h */
81#ifndef SZ_1K
82#define SZ_1K 0x400
83#endif
84
85#ifndef SZ_4M
86#define SZ_4M 0x400000
87#endif
88
89#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
90
91#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
92
93enum {
94 BINDER_DEBUG_USER_ERROR = 1U << 0,
95 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
96 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
97 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
98 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
99 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
100 BINDER_DEBUG_READ_WRITE = 1U << 6,
101 BINDER_DEBUG_USER_REFS = 1U << 7,
102 BINDER_DEBUG_THREADS = 1U << 8,
103 BINDER_DEBUG_TRANSACTION = 1U << 9,
104 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
105 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
106 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
107 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
108 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
109 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
110};
111static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
112 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
113module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
114
Zhengwang Ruan2c523252012-03-07 10:36:57 +0800115static bool binder_debug_no_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900116module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
117
118static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
119static int binder_stop_on_user_error;
120
121static int binder_set_stop_on_user_error(const char *val,
122 struct kernel_param *kp)
123{
124 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900125
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900126 ret = param_set_int(val, kp);
127 if (binder_stop_on_user_error < 2)
128 wake_up(&binder_user_error_wait);
129 return ret;
130}
131module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
132 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
133
134#define binder_debug(mask, x...) \
135 do { \
136 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400137 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900138 } while (0)
139
140#define binder_user_error(x...) \
141 do { \
142 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400143 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900144 if (binder_stop_on_user_error) \
145 binder_stop_on_user_error = 2; \
146 } while (0)
147
148enum binder_stat_types {
149 BINDER_STAT_PROC,
150 BINDER_STAT_THREAD,
151 BINDER_STAT_NODE,
152 BINDER_STAT_REF,
153 BINDER_STAT_DEATH,
154 BINDER_STAT_TRANSACTION,
155 BINDER_STAT_TRANSACTION_COMPLETE,
156 BINDER_STAT_COUNT
157};
158
159struct binder_stats {
160 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
161 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
162 int obj_created[BINDER_STAT_COUNT];
163 int obj_deleted[BINDER_STAT_COUNT];
164};
165
166static struct binder_stats binder_stats;
167
168static inline void binder_stats_deleted(enum binder_stat_types type)
169{
170 binder_stats.obj_deleted[type]++;
171}
172
173static inline void binder_stats_created(enum binder_stat_types type)
174{
175 binder_stats.obj_created[type]++;
176}
177
178struct binder_transaction_log_entry {
179 int debug_id;
180 int call_type;
181 int from_proc;
182 int from_thread;
183 int target_handle;
184 int to_proc;
185 int to_thread;
186 int to_node;
187 int data_size;
188 int offsets_size;
189};
190struct binder_transaction_log {
191 int next;
192 int full;
193 struct binder_transaction_log_entry entry[32];
194};
195static struct binder_transaction_log binder_transaction_log;
196static struct binder_transaction_log binder_transaction_log_failed;
197
198static struct binder_transaction_log_entry *binder_transaction_log_add(
199 struct binder_transaction_log *log)
200{
201 struct binder_transaction_log_entry *e;
Seunghun Lee10f62862014-05-01 01:30:23 +0900202
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900203 e = &log->entry[log->next];
204 memset(e, 0, sizeof(*e));
205 log->next++;
206 if (log->next == ARRAY_SIZE(log->entry)) {
207 log->next = 0;
208 log->full = 1;
209 }
210 return e;
211}
212
213struct binder_work {
214 struct list_head entry;
215 enum {
216 BINDER_WORK_TRANSACTION = 1,
217 BINDER_WORK_TRANSACTION_COMPLETE,
218 BINDER_WORK_NODE,
219 BINDER_WORK_DEAD_BINDER,
220 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
221 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
222 } type;
223};
224
225struct binder_node {
226 int debug_id;
227 struct binder_work work;
228 union {
229 struct rb_node rb_node;
230 struct hlist_node dead_node;
231 };
232 struct binder_proc *proc;
233 struct hlist_head refs;
234 int internal_strong_refs;
235 int local_weak_refs;
236 int local_strong_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800237 binder_uintptr_t ptr;
238 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900239 unsigned has_strong_ref:1;
240 unsigned pending_strong_ref:1;
241 unsigned has_weak_ref:1;
242 unsigned pending_weak_ref:1;
243 unsigned has_async_transaction:1;
244 unsigned accept_fds:1;
245 unsigned min_priority:8;
246 struct list_head async_todo;
247};
248
249struct binder_ref_death {
250 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800251 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900252};
253
254struct binder_ref {
255 /* Lookups needed: */
256 /* node + proc => ref (transaction) */
257 /* desc + proc => ref (transaction, inc/dec ref) */
258 /* node => refs + procs (proc exit) */
259 int debug_id;
260 struct rb_node rb_node_desc;
261 struct rb_node rb_node_node;
262 struct hlist_node node_entry;
263 struct binder_proc *proc;
264 struct binder_node *node;
265 uint32_t desc;
266 int strong;
267 int weak;
268 struct binder_ref_death *death;
269};
270
271struct binder_buffer {
Justin P. Mattock217218f2012-01-12 06:51:31 -0800272 struct list_head entry; /* free and allocated entries by address */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900273 struct rb_node rb_node; /* free entry by size or allocated entry */
274 /* by address */
275 unsigned free:1;
276 unsigned allow_user_free:1;
277 unsigned async_transaction:1;
278 unsigned debug_id:29;
279
280 struct binder_transaction *transaction;
281
282 struct binder_node *target_node;
283 size_t data_size;
284 size_t offsets_size;
285 uint8_t data[0];
286};
287
288enum binder_deferred_state {
289 BINDER_DEFERRED_PUT_FILES = 0x01,
290 BINDER_DEFERRED_FLUSH = 0x02,
291 BINDER_DEFERRED_RELEASE = 0x04,
292};
293
294struct binder_proc {
295 struct hlist_node proc_node;
296 struct rb_root threads;
297 struct rb_root nodes;
298 struct rb_root refs_by_desc;
299 struct rb_root refs_by_node;
300 int pid;
301 struct vm_area_struct *vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800302 struct mm_struct *vma_vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900303 struct task_struct *tsk;
304 struct files_struct *files;
305 struct hlist_node deferred_work_node;
306 int deferred_work;
307 void *buffer;
308 ptrdiff_t user_buffer_offset;
309
310 struct list_head buffers;
311 struct rb_root free_buffers;
312 struct rb_root allocated_buffers;
313 size_t free_async_space;
314
315 struct page **pages;
316 size_t buffer_size;
317 uint32_t buffer_free;
318 struct list_head todo;
319 wait_queue_head_t wait;
320 struct binder_stats stats;
321 struct list_head delivered_death;
322 int max_threads;
323 int requested_threads;
324 int requested_threads_started;
325 int ready_threads;
326 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700327 struct dentry *debugfs_entry;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900328};
329
330enum {
331 BINDER_LOOPER_STATE_REGISTERED = 0x01,
332 BINDER_LOOPER_STATE_ENTERED = 0x02,
333 BINDER_LOOPER_STATE_EXITED = 0x04,
334 BINDER_LOOPER_STATE_INVALID = 0x08,
335 BINDER_LOOPER_STATE_WAITING = 0x10,
336 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
337};
338
339struct binder_thread {
340 struct binder_proc *proc;
341 struct rb_node rb_node;
342 int pid;
343 int looper;
344 struct binder_transaction *transaction_stack;
345 struct list_head todo;
346 uint32_t return_error; /* Write failed, return error code in read buf */
347 uint32_t return_error2; /* Write failed, return error code in read */
348 /* buffer. Used when sending a reply to a dead process that */
349 /* we are also waiting on */
350 wait_queue_head_t wait;
351 struct binder_stats stats;
352};
353
354struct binder_transaction {
355 int debug_id;
356 struct binder_work work;
357 struct binder_thread *from;
358 struct binder_transaction *from_parent;
359 struct binder_proc *to_proc;
360 struct binder_thread *to_thread;
361 struct binder_transaction *to_parent;
362 unsigned need_reply:1;
363 /* unsigned is_dead:1; */ /* not used at the moment */
364
365 struct binder_buffer *buffer;
366 unsigned int code;
367 unsigned int flags;
368 long priority;
369 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600370 kuid_t sender_euid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900371};
372
373static void
374binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
375
Sachin Kamatefde99c2012-08-17 16:39:36 +0530376static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900377{
378 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900379 unsigned long rlim_cur;
380 unsigned long irqs;
381
382 if (files == NULL)
383 return -ESRCH;
384
Al Virodcfadfa2012-08-12 17:27:30 -0400385 if (!lock_task_sighand(proc->tsk, &irqs))
386 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900387
Al Virodcfadfa2012-08-12 17:27:30 -0400388 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
389 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900390
Al Virodcfadfa2012-08-12 17:27:30 -0400391 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900392}
393
394/*
395 * copied from fd_install
396 */
397static void task_fd_install(
398 struct binder_proc *proc, unsigned int fd, struct file *file)
399{
Al Virof869e8a2012-08-15 21:06:33 -0400400 if (proc->files)
401 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900402}
403
404/*
405 * copied from sys_close
406 */
407static long task_close_fd(struct binder_proc *proc, unsigned int fd)
408{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900409 int retval;
410
Al Viro483ce1d2012-08-19 12:04:24 -0400411 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900412 return -ESRCH;
413
Al Viro483ce1d2012-08-19 12:04:24 -0400414 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900415 /* can't restart close syscall because file table entry was cleared */
416 if (unlikely(retval == -ERESTARTSYS ||
417 retval == -ERESTARTNOINTR ||
418 retval == -ERESTARTNOHAND ||
419 retval == -ERESTART_RESTARTBLOCK))
420 retval = -EINTR;
421
422 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900423}
424
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700425static inline void binder_lock(const char *tag)
426{
427 trace_binder_lock(tag);
428 mutex_lock(&binder_main_lock);
429 trace_binder_locked(tag);
430}
431
432static inline void binder_unlock(const char *tag)
433{
434 trace_binder_unlock(tag);
435 mutex_unlock(&binder_main_lock);
436}
437
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900438static void binder_set_nice(long nice)
439{
440 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900441
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900442 if (can_nice(current, nice)) {
443 set_user_nice(current, nice);
444 return;
445 }
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900446 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900447 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530448 "%d: nice value %ld not allowed use %ld instead\n",
449 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900450 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +0800451 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900452 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530453 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900454}
455
456static size_t binder_buffer_size(struct binder_proc *proc,
457 struct binder_buffer *buffer)
458{
459 if (list_is_last(&buffer->entry, &proc->buffers))
460 return proc->buffer + proc->buffer_size - (void *)buffer->data;
Karthik Nayak78733112014-06-21 20:23:16 +0530461 return (size_t)list_entry(buffer->entry.next,
462 struct binder_buffer, entry) - (size_t)buffer->data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900463}
464
465static void binder_insert_free_buffer(struct binder_proc *proc,
466 struct binder_buffer *new_buffer)
467{
468 struct rb_node **p = &proc->free_buffers.rb_node;
469 struct rb_node *parent = NULL;
470 struct binder_buffer *buffer;
471 size_t buffer_size;
472 size_t new_buffer_size;
473
474 BUG_ON(!new_buffer->free);
475
476 new_buffer_size = binder_buffer_size(proc, new_buffer);
477
478 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530479 "%d: add free buffer, size %zd, at %p\n",
480 proc->pid, new_buffer_size, new_buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900481
482 while (*p) {
483 parent = *p;
484 buffer = rb_entry(parent, struct binder_buffer, rb_node);
485 BUG_ON(!buffer->free);
486
487 buffer_size = binder_buffer_size(proc, buffer);
488
489 if (new_buffer_size < buffer_size)
490 p = &parent->rb_left;
491 else
492 p = &parent->rb_right;
493 }
494 rb_link_node(&new_buffer->rb_node, parent, p);
495 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
496}
497
498static void binder_insert_allocated_buffer(struct binder_proc *proc,
499 struct binder_buffer *new_buffer)
500{
501 struct rb_node **p = &proc->allocated_buffers.rb_node;
502 struct rb_node *parent = NULL;
503 struct binder_buffer *buffer;
504
505 BUG_ON(new_buffer->free);
506
507 while (*p) {
508 parent = *p;
509 buffer = rb_entry(parent, struct binder_buffer, rb_node);
510 BUG_ON(buffer->free);
511
512 if (new_buffer < buffer)
513 p = &parent->rb_left;
514 else if (new_buffer > buffer)
515 p = &parent->rb_right;
516 else
517 BUG();
518 }
519 rb_link_node(&new_buffer->rb_node, parent, p);
520 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
521}
522
523static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800524 uintptr_t user_ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900525{
526 struct rb_node *n = proc->allocated_buffers.rb_node;
527 struct binder_buffer *buffer;
528 struct binder_buffer *kern_ptr;
529
Arve Hjønnevågda498892014-02-21 14:40:26 -0800530 kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
531 - offsetof(struct binder_buffer, data));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900532
533 while (n) {
534 buffer = rb_entry(n, struct binder_buffer, rb_node);
535 BUG_ON(buffer->free);
536
537 if (kern_ptr < buffer)
538 n = n->rb_left;
539 else if (kern_ptr > buffer)
540 n = n->rb_right;
541 else
542 return buffer;
543 }
544 return NULL;
545}
546
547static int binder_update_page_range(struct binder_proc *proc, int allocate,
548 void *start, void *end,
549 struct vm_area_struct *vma)
550{
551 void *page_addr;
552 unsigned long user_page_addr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900553 struct page **page;
554 struct mm_struct *mm;
555
556 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530557 "%d: %s pages %p-%p\n", proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900558 allocate ? "allocate" : "free", start, end);
559
560 if (end <= start)
561 return 0;
562
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700563 trace_binder_update_page_range(proc, allocate, start, end);
564
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900565 if (vma)
566 mm = NULL;
567 else
568 mm = get_task_mm(proc->tsk);
569
570 if (mm) {
571 down_write(&mm->mmap_sem);
572 vma = proc->vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800573 if (vma && mm != proc->vma_vm_mm) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530574 pr_err("%d: vma mm and task mm mismatch\n",
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -0800575 proc->pid);
576 vma = NULL;
577 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900578 }
579
580 if (allocate == 0)
581 goto free_range;
582
583 if (vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530584 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
585 proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900586 goto err_no_vma;
587 }
588
589 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
590 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900591
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900592 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
593
594 BUG_ON(*page);
Arve Hjønnevåg585650d2012-10-16 15:29:55 -0700595 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900596 if (*page == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530597 pr_err("%d: binder_alloc_buf failed for page at %p\n",
598 proc->pid, page_addr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900599 goto err_alloc_page_failed;
600 }
Andrey Ryabininf4c72c72015-02-27 20:44:21 +0300601 ret = map_kernel_range_noflush((unsigned long)page_addr,
602 PAGE_SIZE, PAGE_KERNEL, page);
603 flush_cache_vmap((unsigned long)page_addr,
604 (unsigned long)page_addr + PAGE_SIZE);
605 if (ret != 1) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530606 pr_err("%d: binder_alloc_buf failed to map page at %p in kernel\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900607 proc->pid, page_addr);
608 goto err_map_kernel_failed;
609 }
610 user_page_addr =
611 (uintptr_t)page_addr + proc->user_buffer_offset;
612 ret = vm_insert_page(vma, user_page_addr, page[0]);
613 if (ret) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530614 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900615 proc->pid, user_page_addr);
616 goto err_vm_insert_page_failed;
617 }
618 /* vm_insert_page does not seem to increment the refcount */
619 }
620 if (mm) {
621 up_write(&mm->mmap_sem);
622 mmput(mm);
623 }
624 return 0;
625
626free_range:
627 for (page_addr = end - PAGE_SIZE; page_addr >= start;
628 page_addr -= PAGE_SIZE) {
629 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
630 if (vma)
631 zap_page_range(vma, (uintptr_t)page_addr +
632 proc->user_buffer_offset, PAGE_SIZE, NULL);
633err_vm_insert_page_failed:
634 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
635err_map_kernel_failed:
636 __free_page(*page);
637 *page = NULL;
638err_alloc_page_failed:
639 ;
640 }
641err_no_vma:
642 if (mm) {
643 up_write(&mm->mmap_sem);
644 mmput(mm);
645 }
646 return -ENOMEM;
647}
648
649static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
650 size_t data_size,
651 size_t offsets_size, int is_async)
652{
653 struct rb_node *n = proc->free_buffers.rb_node;
654 struct binder_buffer *buffer;
655 size_t buffer_size;
656 struct rb_node *best_fit = NULL;
657 void *has_page_addr;
658 void *end_page_addr;
659 size_t size;
660
661 if (proc->vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530662 pr_err("%d: binder_alloc_buf, no vma\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900663 proc->pid);
664 return NULL;
665 }
666
667 size = ALIGN(data_size, sizeof(void *)) +
668 ALIGN(offsets_size, sizeof(void *));
669
670 if (size < data_size || size < offsets_size) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530671 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
672 proc->pid, data_size, offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900673 return NULL;
674 }
675
676 if (is_async &&
677 proc->free_async_space < size + sizeof(struct binder_buffer)) {
678 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530679 "%d: binder_alloc_buf size %zd failed, no async space left\n",
680 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900681 return NULL;
682 }
683
684 while (n) {
685 buffer = rb_entry(n, struct binder_buffer, rb_node);
686 BUG_ON(!buffer->free);
687 buffer_size = binder_buffer_size(proc, buffer);
688
689 if (size < buffer_size) {
690 best_fit = n;
691 n = n->rb_left;
692 } else if (size > buffer_size)
693 n = n->rb_right;
694 else {
695 best_fit = n;
696 break;
697 }
698 }
699 if (best_fit == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530700 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
701 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900702 return NULL;
703 }
704 if (n == NULL) {
705 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
706 buffer_size = binder_buffer_size(proc, buffer);
707 }
708
709 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530710 "%d: binder_alloc_buf size %zd got buffer %p size %zd\n",
711 proc->pid, size, buffer, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900712
713 has_page_addr =
714 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
715 if (n == NULL) {
716 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
717 buffer_size = size; /* no room for other buffers */
718 else
719 buffer_size = size + sizeof(struct binder_buffer);
720 }
721 end_page_addr =
722 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
723 if (end_page_addr > has_page_addr)
724 end_page_addr = has_page_addr;
725 if (binder_update_page_range(proc, 1,
726 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
727 return NULL;
728
729 rb_erase(best_fit, &proc->free_buffers);
730 buffer->free = 0;
731 binder_insert_allocated_buffer(proc, buffer);
732 if (buffer_size != size) {
733 struct binder_buffer *new_buffer = (void *)buffer->data + size;
Seunghun Lee10f62862014-05-01 01:30:23 +0900734
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900735 list_add(&new_buffer->entry, &buffer->entry);
736 new_buffer->free = 1;
737 binder_insert_free_buffer(proc, new_buffer);
738 }
739 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530740 "%d: binder_alloc_buf size %zd got %p\n",
741 proc->pid, size, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900742 buffer->data_size = data_size;
743 buffer->offsets_size = offsets_size;
744 buffer->async_transaction = is_async;
745 if (is_async) {
746 proc->free_async_space -= size + sizeof(struct binder_buffer);
747 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530748 "%d: binder_alloc_buf size %zd async free %zd\n",
749 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900750 }
751
752 return buffer;
753}
754
755static void *buffer_start_page(struct binder_buffer *buffer)
756{
757 return (void *)((uintptr_t)buffer & PAGE_MASK);
758}
759
760static void *buffer_end_page(struct binder_buffer *buffer)
761{
762 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
763}
764
765static void binder_delete_free_buffer(struct binder_proc *proc,
766 struct binder_buffer *buffer)
767{
768 struct binder_buffer *prev, *next = NULL;
769 int free_page_end = 1;
770 int free_page_start = 1;
771
772 BUG_ON(proc->buffers.next == &buffer->entry);
773 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
774 BUG_ON(!prev->free);
775 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
776 free_page_start = 0;
777 if (buffer_end_page(prev) == buffer_end_page(buffer))
778 free_page_end = 0;
779 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530780 "%d: merge free, buffer %p share page with %p\n",
781 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900782 }
783
784 if (!list_is_last(&buffer->entry, &proc->buffers)) {
785 next = list_entry(buffer->entry.next,
786 struct binder_buffer, entry);
787 if (buffer_start_page(next) == buffer_end_page(buffer)) {
788 free_page_end = 0;
789 if (buffer_start_page(next) ==
790 buffer_start_page(buffer))
791 free_page_start = 0;
792 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530793 "%d: merge free, buffer %p share page with %p\n",
794 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900795 }
796 }
797 list_del(&buffer->entry);
798 if (free_page_start || free_page_end) {
799 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Masanari Iida1dcdbfd2013-06-23 23:47:15 +0900800 "%d: merge free, buffer %p do not share page%s%s with %p or %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900801 proc->pid, buffer, free_page_start ? "" : " end",
802 free_page_end ? "" : " start", prev, next);
803 binder_update_page_range(proc, 0, free_page_start ?
804 buffer_start_page(buffer) : buffer_end_page(buffer),
805 (free_page_end ? buffer_end_page(buffer) :
806 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
807 }
808}
809
810static void binder_free_buf(struct binder_proc *proc,
811 struct binder_buffer *buffer)
812{
813 size_t size, buffer_size;
814
815 buffer_size = binder_buffer_size(proc, buffer);
816
817 size = ALIGN(buffer->data_size, sizeof(void *)) +
818 ALIGN(buffer->offsets_size, sizeof(void *));
819
820 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530821 "%d: binder_free_buf %p size %zd buffer_size %zd\n",
822 proc->pid, buffer, size, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900823
824 BUG_ON(buffer->free);
825 BUG_ON(size > buffer_size);
826 BUG_ON(buffer->transaction != NULL);
827 BUG_ON((void *)buffer < proc->buffer);
828 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
829
830 if (buffer->async_transaction) {
831 proc->free_async_space += size + sizeof(struct binder_buffer);
832
833 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530834 "%d: binder_free_buf size %zd async free %zd\n",
835 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900836 }
837
838 binder_update_page_range(proc, 0,
839 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
840 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
841 NULL);
842 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
843 buffer->free = 1;
844 if (!list_is_last(&buffer->entry, &proc->buffers)) {
845 struct binder_buffer *next = list_entry(buffer->entry.next,
846 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900847
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900848 if (next->free) {
849 rb_erase(&next->rb_node, &proc->free_buffers);
850 binder_delete_free_buffer(proc, next);
851 }
852 }
853 if (proc->buffers.next != &buffer->entry) {
854 struct binder_buffer *prev = list_entry(buffer->entry.prev,
855 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900856
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900857 if (prev->free) {
858 binder_delete_free_buffer(proc, buffer);
859 rb_erase(&prev->rb_node, &proc->free_buffers);
860 buffer = prev;
861 }
862 }
863 binder_insert_free_buffer(proc, buffer);
864}
865
866static struct binder_node *binder_get_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800867 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900868{
869 struct rb_node *n = proc->nodes.rb_node;
870 struct binder_node *node;
871
872 while (n) {
873 node = rb_entry(n, struct binder_node, rb_node);
874
875 if (ptr < node->ptr)
876 n = n->rb_left;
877 else if (ptr > node->ptr)
878 n = n->rb_right;
879 else
880 return node;
881 }
882 return NULL;
883}
884
885static struct binder_node *binder_new_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800886 binder_uintptr_t ptr,
887 binder_uintptr_t cookie)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900888{
889 struct rb_node **p = &proc->nodes.rb_node;
890 struct rb_node *parent = NULL;
891 struct binder_node *node;
892
893 while (*p) {
894 parent = *p;
895 node = rb_entry(parent, struct binder_node, rb_node);
896
897 if (ptr < node->ptr)
898 p = &(*p)->rb_left;
899 else if (ptr > node->ptr)
900 p = &(*p)->rb_right;
901 else
902 return NULL;
903 }
904
905 node = kzalloc(sizeof(*node), GFP_KERNEL);
906 if (node == NULL)
907 return NULL;
908 binder_stats_created(BINDER_STAT_NODE);
909 rb_link_node(&node->rb_node, parent, p);
910 rb_insert_color(&node->rb_node, &proc->nodes);
911 node->debug_id = ++binder_last_id;
912 node->proc = proc;
913 node->ptr = ptr;
914 node->cookie = cookie;
915 node->work.type = BINDER_WORK_NODE;
916 INIT_LIST_HEAD(&node->work.entry);
917 INIT_LIST_HEAD(&node->async_todo);
918 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800919 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900920 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800921 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900922 return node;
923}
924
925static int binder_inc_node(struct binder_node *node, int strong, int internal,
926 struct list_head *target_list)
927{
928 if (strong) {
929 if (internal) {
930 if (target_list == NULL &&
931 node->internal_strong_refs == 0 &&
932 !(node == binder_context_mgr_node &&
933 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530934 pr_err("invalid inc strong node for %d\n",
935 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900936 return -EINVAL;
937 }
938 node->internal_strong_refs++;
939 } else
940 node->local_strong_refs++;
941 if (!node->has_strong_ref && target_list) {
942 list_del_init(&node->work.entry);
943 list_add_tail(&node->work.entry, target_list);
944 }
945 } else {
946 if (!internal)
947 node->local_weak_refs++;
948 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
949 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530950 pr_err("invalid inc weak node for %d\n",
951 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900952 return -EINVAL;
953 }
954 list_add_tail(&node->work.entry, target_list);
955 }
956 }
957 return 0;
958}
959
960static int binder_dec_node(struct binder_node *node, int strong, int internal)
961{
962 if (strong) {
963 if (internal)
964 node->internal_strong_refs--;
965 else
966 node->local_strong_refs--;
967 if (node->local_strong_refs || node->internal_strong_refs)
968 return 0;
969 } else {
970 if (!internal)
971 node->local_weak_refs--;
972 if (node->local_weak_refs || !hlist_empty(&node->refs))
973 return 0;
974 }
975 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
976 if (list_empty(&node->work.entry)) {
977 list_add_tail(&node->work.entry, &node->proc->todo);
978 wake_up_interruptible(&node->proc->wait);
979 }
980 } else {
981 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
982 !node->local_weak_refs) {
983 list_del_init(&node->work.entry);
984 if (node->proc) {
985 rb_erase(&node->rb_node, &node->proc->nodes);
986 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530987 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900988 node->debug_id);
989 } else {
990 hlist_del(&node->dead_node);
991 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530992 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900993 node->debug_id);
994 }
995 kfree(node);
996 binder_stats_deleted(BINDER_STAT_NODE);
997 }
998 }
999
1000 return 0;
1001}
1002
1003
1004static struct binder_ref *binder_get_ref(struct binder_proc *proc,
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001005 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001006{
1007 struct rb_node *n = proc->refs_by_desc.rb_node;
1008 struct binder_ref *ref;
1009
1010 while (n) {
1011 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1012
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001013 if (desc < ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001014 n = n->rb_left;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001015 } else if (desc > ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001016 n = n->rb_right;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001017 } else if (need_strong_ref && !ref->strong) {
1018 binder_user_error("tried to use weak ref as strong ref\n");
1019 return NULL;
1020 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001021 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001022 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001023 }
1024 return NULL;
1025}
1026
1027static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1028 struct binder_node *node)
1029{
1030 struct rb_node *n;
1031 struct rb_node **p = &proc->refs_by_node.rb_node;
1032 struct rb_node *parent = NULL;
1033 struct binder_ref *ref, *new_ref;
1034
1035 while (*p) {
1036 parent = *p;
1037 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1038
1039 if (node < ref->node)
1040 p = &(*p)->rb_left;
1041 else if (node > ref->node)
1042 p = &(*p)->rb_right;
1043 else
1044 return ref;
1045 }
1046 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1047 if (new_ref == NULL)
1048 return NULL;
1049 binder_stats_created(BINDER_STAT_REF);
1050 new_ref->debug_id = ++binder_last_id;
1051 new_ref->proc = proc;
1052 new_ref->node = node;
1053 rb_link_node(&new_ref->rb_node_node, parent, p);
1054 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1055
1056 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1057 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1058 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1059 if (ref->desc > new_ref->desc)
1060 break;
1061 new_ref->desc = ref->desc + 1;
1062 }
1063
1064 p = &proc->refs_by_desc.rb_node;
1065 while (*p) {
1066 parent = *p;
1067 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1068
1069 if (new_ref->desc < ref->desc)
1070 p = &(*p)->rb_left;
1071 else if (new_ref->desc > ref->desc)
1072 p = &(*p)->rb_right;
1073 else
1074 BUG();
1075 }
1076 rb_link_node(&new_ref->rb_node_desc, parent, p);
1077 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1078 if (node) {
1079 hlist_add_head(&new_ref->node_entry, &node->refs);
1080
1081 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301082 "%d new ref %d desc %d for node %d\n",
1083 proc->pid, new_ref->debug_id, new_ref->desc,
1084 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001085 } else {
1086 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301087 "%d new ref %d desc %d for dead node\n",
1088 proc->pid, new_ref->debug_id, new_ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001089 }
1090 return new_ref;
1091}
1092
1093static void binder_delete_ref(struct binder_ref *ref)
1094{
1095 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301096 "%d delete ref %d desc %d for node %d\n",
1097 ref->proc->pid, ref->debug_id, ref->desc,
1098 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001099
1100 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1101 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1102 if (ref->strong)
1103 binder_dec_node(ref->node, 1, 1);
1104 hlist_del(&ref->node_entry);
1105 binder_dec_node(ref->node, 0, 1);
1106 if (ref->death) {
1107 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301108 "%d delete ref %d desc %d has death notification\n",
1109 ref->proc->pid, ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001110 list_del(&ref->death->work.entry);
1111 kfree(ref->death);
1112 binder_stats_deleted(BINDER_STAT_DEATH);
1113 }
1114 kfree(ref);
1115 binder_stats_deleted(BINDER_STAT_REF);
1116}
1117
1118static int binder_inc_ref(struct binder_ref *ref, int strong,
1119 struct list_head *target_list)
1120{
1121 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001122
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001123 if (strong) {
1124 if (ref->strong == 0) {
1125 ret = binder_inc_node(ref->node, 1, 1, target_list);
1126 if (ret)
1127 return ret;
1128 }
1129 ref->strong++;
1130 } else {
1131 if (ref->weak == 0) {
1132 ret = binder_inc_node(ref->node, 0, 1, target_list);
1133 if (ret)
1134 return ret;
1135 }
1136 ref->weak++;
1137 }
1138 return 0;
1139}
1140
1141
1142static int binder_dec_ref(struct binder_ref *ref, int strong)
1143{
1144 if (strong) {
1145 if (ref->strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301146 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001147 ref->proc->pid, ref->debug_id,
1148 ref->desc, ref->strong, ref->weak);
1149 return -EINVAL;
1150 }
1151 ref->strong--;
1152 if (ref->strong == 0) {
1153 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001154
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001155 ret = binder_dec_node(ref->node, strong, 1);
1156 if (ret)
1157 return ret;
1158 }
1159 } else {
1160 if (ref->weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301161 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001162 ref->proc->pid, ref->debug_id,
1163 ref->desc, ref->strong, ref->weak);
1164 return -EINVAL;
1165 }
1166 ref->weak--;
1167 }
1168 if (ref->strong == 0 && ref->weak == 0)
1169 binder_delete_ref(ref);
1170 return 0;
1171}
1172
1173static void binder_pop_transaction(struct binder_thread *target_thread,
1174 struct binder_transaction *t)
1175{
1176 if (target_thread) {
1177 BUG_ON(target_thread->transaction_stack != t);
1178 BUG_ON(target_thread->transaction_stack->from != target_thread);
1179 target_thread->transaction_stack =
1180 target_thread->transaction_stack->from_parent;
1181 t->from = NULL;
1182 }
1183 t->need_reply = 0;
1184 if (t->buffer)
1185 t->buffer->transaction = NULL;
1186 kfree(t);
1187 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1188}
1189
1190static void binder_send_failed_reply(struct binder_transaction *t,
1191 uint32_t error_code)
1192{
1193 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001194 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001195
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001196 BUG_ON(t->flags & TF_ONE_WAY);
1197 while (1) {
1198 target_thread = t->from;
1199 if (target_thread) {
1200 if (target_thread->return_error != BR_OK &&
1201 target_thread->return_error2 == BR_OK) {
1202 target_thread->return_error2 =
1203 target_thread->return_error;
1204 target_thread->return_error = BR_OK;
1205 }
1206 if (target_thread->return_error == BR_OK) {
1207 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301208 "send failed reply for transaction %d to %d:%d\n",
William Panlener0232a422014-09-03 22:44:03 -05001209 t->debug_id,
1210 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001211 target_thread->pid);
1212
1213 binder_pop_transaction(target_thread, t);
1214 target_thread->return_error = error_code;
1215 wake_up_interruptible(&target_thread->wait);
1216 } else {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301217 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1218 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001219 target_thread->pid,
1220 target_thread->return_error);
1221 }
1222 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001223 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001224 next = t->from_parent;
1225
1226 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1227 "send failed reply for transaction %d, target dead\n",
1228 t->debug_id);
1229
1230 binder_pop_transaction(target_thread, t);
1231 if (next == NULL) {
1232 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1233 "reply failed, no target thread at root\n");
1234 return;
1235 }
1236 t = next;
1237 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1238 "reply failed, no target thread -- retry %d\n",
1239 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001240 }
1241}
1242
1243static void binder_transaction_buffer_release(struct binder_proc *proc,
1244 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001245 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001246{
Arve Hjønnevågda498892014-02-21 14:40:26 -08001247 binder_size_t *offp, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001248 int debug_id = buffer->debug_id;
1249
1250 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301251 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001252 proc->pid, buffer->debug_id,
1253 buffer->data_size, buffer->offsets_size, failed_at);
1254
1255 if (buffer->target_node)
1256 binder_dec_node(buffer->target_node, 1, 0);
1257
Arve Hjønnevågda498892014-02-21 14:40:26 -08001258 offp = (binder_size_t *)(buffer->data +
1259 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001260 if (failed_at)
1261 off_end = failed_at;
1262 else
1263 off_end = (void *)offp + buffer->offsets_size;
1264 for (; offp < off_end; offp++) {
1265 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001266
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001267 if (*offp > buffer->data_size - sizeof(*fp) ||
1268 buffer->data_size < sizeof(*fp) ||
Serban Constantinescuec35e852013-07-04 10:54:46 +01001269 !IS_ALIGNED(*offp, sizeof(u32))) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001270 pr_err("transaction release %d bad offset %lld, size %zd\n",
1271 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001272 continue;
1273 }
1274 fp = (struct flat_binder_object *)(buffer->data + *offp);
1275 switch (fp->type) {
1276 case BINDER_TYPE_BINDER:
1277 case BINDER_TYPE_WEAK_BINDER: {
1278 struct binder_node *node = binder_get_node(proc, fp->binder);
Seunghun Lee10f62862014-05-01 01:30:23 +09001279
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001280 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001281 pr_err("transaction release %d bad node %016llx\n",
1282 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001283 break;
1284 }
1285 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001286 " node %d u%016llx\n",
1287 node->debug_id, (u64)node->ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001288 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1289 } break;
1290 case BINDER_TYPE_HANDLE:
1291 case BINDER_TYPE_WEAK_HANDLE: {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001292 struct binder_ref *ref;
1293
1294 ref = binder_get_ref(proc, fp->handle,
1295 fp->type == BINDER_TYPE_HANDLE);
Seunghun Lee10f62862014-05-01 01:30:23 +09001296
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001297 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001298 pr_err("transaction release %d bad handle %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301299 debug_id, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001300 break;
1301 }
1302 binder_debug(BINDER_DEBUG_TRANSACTION,
1303 " ref %d desc %d (node %d)\n",
1304 ref->debug_id, ref->desc, ref->node->debug_id);
1305 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1306 } break;
1307
1308 case BINDER_TYPE_FD:
1309 binder_debug(BINDER_DEBUG_TRANSACTION,
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001310 " fd %d\n", fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001311 if (failed_at)
1312 task_close_fd(proc, fp->handle);
1313 break;
1314
1315 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001316 pr_err("transaction release %d bad object type %x\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301317 debug_id, fp->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001318 break;
1319 }
1320 }
1321}
1322
1323static void binder_transaction(struct binder_proc *proc,
1324 struct binder_thread *thread,
1325 struct binder_transaction_data *tr, int reply)
1326{
1327 struct binder_transaction *t;
1328 struct binder_work *tcomplete;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001329 binder_size_t *offp, *off_end;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001330 binder_size_t off_min;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001331 struct binder_proc *target_proc;
1332 struct binder_thread *target_thread = NULL;
1333 struct binder_node *target_node = NULL;
1334 struct list_head *target_list;
1335 wait_queue_head_t *target_wait;
1336 struct binder_transaction *in_reply_to = NULL;
1337 struct binder_transaction_log_entry *e;
1338 uint32_t return_error;
1339
1340 e = binder_transaction_log_add(&binder_transaction_log);
1341 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1342 e->from_proc = proc->pid;
1343 e->from_thread = thread->pid;
1344 e->target_handle = tr->target.handle;
1345 e->data_size = tr->data_size;
1346 e->offsets_size = tr->offsets_size;
1347
1348 if (reply) {
1349 in_reply_to = thread->transaction_stack;
1350 if (in_reply_to == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301351 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001352 proc->pid, thread->pid);
1353 return_error = BR_FAILED_REPLY;
1354 goto err_empty_call_stack;
1355 }
1356 binder_set_nice(in_reply_to->saved_priority);
1357 if (in_reply_to->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301358 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 +09001359 proc->pid, thread->pid, in_reply_to->debug_id,
1360 in_reply_to->to_proc ?
1361 in_reply_to->to_proc->pid : 0,
1362 in_reply_to->to_thread ?
1363 in_reply_to->to_thread->pid : 0);
1364 return_error = BR_FAILED_REPLY;
1365 in_reply_to = NULL;
1366 goto err_bad_call_stack;
1367 }
1368 thread->transaction_stack = in_reply_to->to_parent;
1369 target_thread = in_reply_to->from;
1370 if (target_thread == NULL) {
1371 return_error = BR_DEAD_REPLY;
1372 goto err_dead_binder;
1373 }
1374 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301375 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 +09001376 proc->pid, thread->pid,
1377 target_thread->transaction_stack ?
1378 target_thread->transaction_stack->debug_id : 0,
1379 in_reply_to->debug_id);
1380 return_error = BR_FAILED_REPLY;
1381 in_reply_to = NULL;
1382 target_thread = NULL;
1383 goto err_dead_binder;
1384 }
1385 target_proc = target_thread->proc;
1386 } else {
1387 if (tr->target.handle) {
1388 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001389
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001390 ref = binder_get_ref(proc, tr->target.handle, true);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001391 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301392 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001393 proc->pid, thread->pid);
1394 return_error = BR_FAILED_REPLY;
1395 goto err_invalid_target_handle;
1396 }
1397 target_node = ref->node;
1398 } else {
1399 target_node = binder_context_mgr_node;
1400 if (target_node == NULL) {
1401 return_error = BR_DEAD_REPLY;
1402 goto err_no_context_mgr_node;
1403 }
1404 }
1405 e->to_node = target_node->debug_id;
1406 target_proc = target_node->proc;
1407 if (target_proc == NULL) {
1408 return_error = BR_DEAD_REPLY;
1409 goto err_dead_binder;
1410 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001411 if (security_binder_transaction(proc->tsk,
1412 target_proc->tsk) < 0) {
1413 return_error = BR_FAILED_REPLY;
1414 goto err_invalid_target_handle;
1415 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001416 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1417 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001418
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001419 tmp = thread->transaction_stack;
1420 if (tmp->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301421 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 +09001422 proc->pid, thread->pid, tmp->debug_id,
1423 tmp->to_proc ? tmp->to_proc->pid : 0,
1424 tmp->to_thread ?
1425 tmp->to_thread->pid : 0);
1426 return_error = BR_FAILED_REPLY;
1427 goto err_bad_call_stack;
1428 }
1429 while (tmp) {
1430 if (tmp->from && tmp->from->proc == target_proc)
1431 target_thread = tmp->from;
1432 tmp = tmp->from_parent;
1433 }
1434 }
1435 }
1436 if (target_thread) {
1437 e->to_thread = target_thread->pid;
1438 target_list = &target_thread->todo;
1439 target_wait = &target_thread->wait;
1440 } else {
1441 target_list = &target_proc->todo;
1442 target_wait = &target_proc->wait;
1443 }
1444 e->to_proc = target_proc->pid;
1445
1446 /* TODO: reuse incoming transaction for reply */
1447 t = kzalloc(sizeof(*t), GFP_KERNEL);
1448 if (t == NULL) {
1449 return_error = BR_FAILED_REPLY;
1450 goto err_alloc_t_failed;
1451 }
1452 binder_stats_created(BINDER_STAT_TRANSACTION);
1453
1454 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1455 if (tcomplete == NULL) {
1456 return_error = BR_FAILED_REPLY;
1457 goto err_alloc_tcomplete_failed;
1458 }
1459 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1460
1461 t->debug_id = ++binder_last_id;
1462 e->debug_id = t->debug_id;
1463
1464 if (reply)
1465 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001466 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001467 proc->pid, thread->pid, t->debug_id,
1468 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001469 (u64)tr->data.ptr.buffer,
1470 (u64)tr->data.ptr.offsets,
1471 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001472 else
1473 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001474 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001475 proc->pid, thread->pid, t->debug_id,
1476 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001477 (u64)tr->data.ptr.buffer,
1478 (u64)tr->data.ptr.offsets,
1479 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001480
1481 if (!reply && !(tr->flags & TF_ONE_WAY))
1482 t->from = thread;
1483 else
1484 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03001485 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001486 t->to_proc = target_proc;
1487 t->to_thread = target_thread;
1488 t->code = tr->code;
1489 t->flags = tr->flags;
1490 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001491
1492 trace_binder_transaction(reply, t, target_node);
1493
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001494 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1495 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1496 if (t->buffer == NULL) {
1497 return_error = BR_FAILED_REPLY;
1498 goto err_binder_alloc_buf_failed;
1499 }
1500 t->buffer->allow_user_free = 0;
1501 t->buffer->debug_id = t->debug_id;
1502 t->buffer->transaction = t;
1503 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001504 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001505 if (target_node)
1506 binder_inc_node(target_node, 1, 0, NULL);
1507
Arve Hjønnevågda498892014-02-21 14:40:26 -08001508 offp = (binder_size_t *)(t->buffer->data +
1509 ALIGN(tr->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001510
Arve Hjønnevågda498892014-02-21 14:40:26 -08001511 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1512 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301513 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1514 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001515 return_error = BR_FAILED_REPLY;
1516 goto err_copy_data_failed;
1517 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001518 if (copy_from_user(offp, (const void __user *)(uintptr_t)
1519 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301520 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1521 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001522 return_error = BR_FAILED_REPLY;
1523 goto err_copy_data_failed;
1524 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001525 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1526 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1527 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001528 return_error = BR_FAILED_REPLY;
1529 goto err_bad_offset;
1530 }
1531 off_end = (void *)offp + tr->offsets_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001532 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001533 for (; offp < off_end; offp++) {
1534 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001535
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001536 if (*offp > t->buffer->data_size - sizeof(*fp) ||
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001537 *offp < off_min ||
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001538 t->buffer->data_size < sizeof(*fp) ||
Serban Constantinescuec35e852013-07-04 10:54:46 +01001539 !IS_ALIGNED(*offp, sizeof(u32))) {
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001540 binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
1541 proc->pid, thread->pid, (u64)*offp,
1542 (u64)off_min,
1543 (u64)(t->buffer->data_size -
1544 sizeof(*fp)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001545 return_error = BR_FAILED_REPLY;
1546 goto err_bad_offset;
1547 }
1548 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001549 off_min = *offp + sizeof(struct flat_binder_object);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001550 switch (fp->type) {
1551 case BINDER_TYPE_BINDER:
1552 case BINDER_TYPE_WEAK_BINDER: {
1553 struct binder_ref *ref;
1554 struct binder_node *node = binder_get_node(proc, fp->binder);
Seunghun Lee10f62862014-05-01 01:30:23 +09001555
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001556 if (node == NULL) {
1557 node = binder_new_node(proc, fp->binder, fp->cookie);
1558 if (node == NULL) {
1559 return_error = BR_FAILED_REPLY;
1560 goto err_binder_new_node_failed;
1561 }
1562 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1563 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1564 }
1565 if (fp->cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001566 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001567 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001568 (u64)fp->binder, node->debug_id,
1569 (u64)fp->cookie, (u64)node->cookie);
Christian Engelmayer7d420432014-05-07 21:44:53 +02001570 return_error = BR_FAILED_REPLY;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001571 goto err_binder_get_ref_for_node_failed;
1572 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001573 if (security_binder_transfer_binder(proc->tsk,
1574 target_proc->tsk)) {
1575 return_error = BR_FAILED_REPLY;
1576 goto err_binder_get_ref_for_node_failed;
1577 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001578 ref = binder_get_ref_for_node(target_proc, node);
1579 if (ref == NULL) {
1580 return_error = BR_FAILED_REPLY;
1581 goto err_binder_get_ref_for_node_failed;
1582 }
1583 if (fp->type == BINDER_TYPE_BINDER)
1584 fp->type = BINDER_TYPE_HANDLE;
1585 else
1586 fp->type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001587 fp->binder = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001588 fp->handle = ref->desc;
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001589 fp->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001590 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1591 &thread->todo);
1592
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001593 trace_binder_transaction_node_to_ref(t, node, ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001594 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001595 " node %d u%016llx -> ref %d desc %d\n",
1596 node->debug_id, (u64)node->ptr,
1597 ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001598 } break;
1599 case BINDER_TYPE_HANDLE:
1600 case BINDER_TYPE_WEAK_HANDLE: {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001601 struct binder_ref *ref;
1602
1603 ref = binder_get_ref(proc, fp->handle,
1604 fp->type == BINDER_TYPE_HANDLE);
Seunghun Lee10f62862014-05-01 01:30:23 +09001605
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001606 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001607 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301608 proc->pid,
1609 thread->pid, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001610 return_error = BR_FAILED_REPLY;
1611 goto err_binder_get_ref_failed;
1612 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001613 if (security_binder_transfer_binder(proc->tsk,
1614 target_proc->tsk)) {
1615 return_error = BR_FAILED_REPLY;
1616 goto err_binder_get_ref_failed;
1617 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001618 if (ref->node->proc == target_proc) {
1619 if (fp->type == BINDER_TYPE_HANDLE)
1620 fp->type = BINDER_TYPE_BINDER;
1621 else
1622 fp->type = BINDER_TYPE_WEAK_BINDER;
1623 fp->binder = ref->node->ptr;
1624 fp->cookie = ref->node->cookie;
1625 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001626 trace_binder_transaction_ref_to_node(t, ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001627 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001628 " ref %d desc %d -> node %d u%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001629 ref->debug_id, ref->desc, ref->node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001630 (u64)ref->node->ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001631 } else {
1632 struct binder_ref *new_ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001633
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001634 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1635 if (new_ref == NULL) {
1636 return_error = BR_FAILED_REPLY;
1637 goto err_binder_get_ref_for_node_failed;
1638 }
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001639 fp->binder = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001640 fp->handle = new_ref->desc;
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001641 fp->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001642 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001643 trace_binder_transaction_ref_to_ref(t, ref,
1644 new_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001645 binder_debug(BINDER_DEBUG_TRANSACTION,
1646 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1647 ref->debug_id, ref->desc, new_ref->debug_id,
1648 new_ref->desc, ref->node->debug_id);
1649 }
1650 } break;
1651
1652 case BINDER_TYPE_FD: {
1653 int target_fd;
1654 struct file *file;
1655
1656 if (reply) {
1657 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001658 binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001659 proc->pid, thread->pid, fp->handle);
1660 return_error = BR_FAILED_REPLY;
1661 goto err_fd_not_allowed;
1662 }
1663 } else if (!target_node->accept_fds) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001664 binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001665 proc->pid, thread->pid, fp->handle);
1666 return_error = BR_FAILED_REPLY;
1667 goto err_fd_not_allowed;
1668 }
1669
1670 file = fget(fp->handle);
1671 if (file == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001672 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001673 proc->pid, thread->pid, fp->handle);
1674 return_error = BR_FAILED_REPLY;
1675 goto err_fget_failed;
1676 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001677 if (security_binder_transfer_file(proc->tsk,
1678 target_proc->tsk,
1679 file) < 0) {
1680 fput(file);
1681 return_error = BR_FAILED_REPLY;
1682 goto err_get_unused_fd_failed;
1683 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001684 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1685 if (target_fd < 0) {
1686 fput(file);
1687 return_error = BR_FAILED_REPLY;
1688 goto err_get_unused_fd_failed;
1689 }
1690 task_fd_install(target_proc, target_fd, file);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001691 trace_binder_transaction_fd(t, fp->handle, target_fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001692 binder_debug(BINDER_DEBUG_TRANSACTION,
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001693 " fd %d -> %d\n", fp->handle, target_fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001694 /* TODO: fput? */
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001695 fp->binder = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001696 fp->handle = target_fd;
1697 } break;
1698
1699 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001700 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001701 proc->pid, thread->pid, fp->type);
1702 return_error = BR_FAILED_REPLY;
1703 goto err_bad_object_type;
1704 }
1705 }
1706 if (reply) {
1707 BUG_ON(t->buffer->async_transaction != 0);
1708 binder_pop_transaction(target_thread, in_reply_to);
1709 } else if (!(t->flags & TF_ONE_WAY)) {
1710 BUG_ON(t->buffer->async_transaction != 0);
1711 t->need_reply = 1;
1712 t->from_parent = thread->transaction_stack;
1713 thread->transaction_stack = t;
1714 } else {
1715 BUG_ON(target_node == NULL);
1716 BUG_ON(t->buffer->async_transaction != 1);
1717 if (target_node->has_async_transaction) {
1718 target_list = &target_node->async_todo;
1719 target_wait = NULL;
1720 } else
1721 target_node->has_async_transaction = 1;
1722 }
1723 t->work.type = BINDER_WORK_TRANSACTION;
1724 list_add_tail(&t->work.entry, target_list);
1725 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1726 list_add_tail(&tcomplete->entry, &thread->todo);
Riley Andrews8fb0b0c2017-06-29 12:01:37 -07001727 if (target_wait) {
1728 if (reply || !(t->flags & TF_ONE_WAY))
1729 wake_up_interruptible_sync(target_wait);
1730 else
1731 wake_up_interruptible(target_wait);
1732 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001733 return;
1734
1735err_get_unused_fd_failed:
1736err_fget_failed:
1737err_fd_not_allowed:
1738err_binder_get_ref_for_node_failed:
1739err_binder_get_ref_failed:
1740err_binder_new_node_failed:
1741err_bad_object_type:
1742err_bad_offset:
1743err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001744 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001745 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1746 t->buffer->transaction = NULL;
1747 binder_free_buf(target_proc, t->buffer);
1748err_binder_alloc_buf_failed:
1749 kfree(tcomplete);
1750 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1751err_alloc_tcomplete_failed:
1752 kfree(t);
1753 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1754err_alloc_t_failed:
1755err_bad_call_stack:
1756err_empty_call_stack:
1757err_dead_binder:
1758err_invalid_target_handle:
1759err_no_context_mgr_node:
1760 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001761 "%d:%d transaction failed %d, size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001762 proc->pid, thread->pid, return_error,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001763 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001764
1765 {
1766 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09001767
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001768 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1769 *fe = *e;
1770 }
1771
1772 BUG_ON(thread->return_error != BR_OK);
1773 if (in_reply_to) {
1774 thread->return_error = BR_TRANSACTION_COMPLETE;
1775 binder_send_failed_reply(in_reply_to, return_error);
1776 } else
1777 thread->return_error = return_error;
1778}
1779
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02001780static int binder_thread_write(struct binder_proc *proc,
1781 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001782 binder_uintptr_t binder_buffer, size_t size,
1783 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001784{
1785 uint32_t cmd;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001786 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001787 void __user *ptr = buffer + *consumed;
1788 void __user *end = buffer + size;
1789
1790 while (ptr < end && thread->return_error == BR_OK) {
1791 if (get_user(cmd, (uint32_t __user *)ptr))
1792 return -EFAULT;
1793 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001794 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001795 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1796 binder_stats.bc[_IOC_NR(cmd)]++;
1797 proc->stats.bc[_IOC_NR(cmd)]++;
1798 thread->stats.bc[_IOC_NR(cmd)]++;
1799 }
1800 switch (cmd) {
1801 case BC_INCREFS:
1802 case BC_ACQUIRE:
1803 case BC_RELEASE:
1804 case BC_DECREFS: {
1805 uint32_t target;
1806 struct binder_ref *ref;
1807 const char *debug_string;
1808
1809 if (get_user(target, (uint32_t __user *)ptr))
1810 return -EFAULT;
1811 ptr += sizeof(uint32_t);
1812 if (target == 0 && binder_context_mgr_node &&
1813 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1814 ref = binder_get_ref_for_node(proc,
1815 binder_context_mgr_node);
1816 if (ref->desc != target) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301817 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001818 proc->pid, thread->pid,
1819 ref->desc);
1820 }
1821 } else
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001822 ref = binder_get_ref(proc, target,
1823 cmd == BC_ACQUIRE ||
1824 cmd == BC_RELEASE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001825 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301826 binder_user_error("%d:%d refcount change on invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001827 proc->pid, thread->pid, target);
1828 break;
1829 }
1830 switch (cmd) {
1831 case BC_INCREFS:
1832 debug_string = "IncRefs";
1833 binder_inc_ref(ref, 0, NULL);
1834 break;
1835 case BC_ACQUIRE:
1836 debug_string = "Acquire";
1837 binder_inc_ref(ref, 1, NULL);
1838 break;
1839 case BC_RELEASE:
1840 debug_string = "Release";
1841 binder_dec_ref(ref, 1);
1842 break;
1843 case BC_DECREFS:
1844 default:
1845 debug_string = "DecRefs";
1846 binder_dec_ref(ref, 0);
1847 break;
1848 }
1849 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301850 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001851 proc->pid, thread->pid, debug_string, ref->debug_id,
1852 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1853 break;
1854 }
1855 case BC_INCREFS_DONE:
1856 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001857 binder_uintptr_t node_ptr;
1858 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001859 struct binder_node *node;
1860
Arve Hjønnevågda498892014-02-21 14:40:26 -08001861 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001862 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001863 ptr += sizeof(binder_uintptr_t);
1864 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001865 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001866 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001867 node = binder_get_node(proc, node_ptr);
1868 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001869 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001870 proc->pid, thread->pid,
1871 cmd == BC_INCREFS_DONE ?
1872 "BC_INCREFS_DONE" :
1873 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001874 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001875 break;
1876 }
1877 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001878 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001879 proc->pid, thread->pid,
1880 cmd == BC_INCREFS_DONE ?
1881 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001882 (u64)node_ptr, node->debug_id,
1883 (u64)cookie, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001884 break;
1885 }
1886 if (cmd == BC_ACQUIRE_DONE) {
1887 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301888 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001889 proc->pid, thread->pid,
1890 node->debug_id);
1891 break;
1892 }
1893 node->pending_strong_ref = 0;
1894 } else {
1895 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301896 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001897 proc->pid, thread->pid,
1898 node->debug_id);
1899 break;
1900 }
1901 node->pending_weak_ref = 0;
1902 }
1903 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1904 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301905 "%d:%d %s node %d ls %d lw %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001906 proc->pid, thread->pid,
1907 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1908 node->debug_id, node->local_strong_refs, node->local_weak_refs);
1909 break;
1910 }
1911 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05301912 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001913 return -EINVAL;
1914 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05301915 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001916 return -EINVAL;
1917
1918 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001919 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001920 struct binder_buffer *buffer;
1921
Arve Hjønnevågda498892014-02-21 14:40:26 -08001922 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001923 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001924 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001925
1926 buffer = binder_buffer_lookup(proc, data_ptr);
1927 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001928 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
1929 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001930 break;
1931 }
1932 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001933 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
1934 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001935 break;
1936 }
1937 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001938 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
1939 proc->pid, thread->pid, (u64)data_ptr,
1940 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001941 buffer->transaction ? "active" : "finished");
1942
1943 if (buffer->transaction) {
1944 buffer->transaction->buffer = NULL;
1945 buffer->transaction = NULL;
1946 }
1947 if (buffer->async_transaction && buffer->target_node) {
1948 BUG_ON(!buffer->target_node->has_async_transaction);
1949 if (list_empty(&buffer->target_node->async_todo))
1950 buffer->target_node->has_async_transaction = 0;
1951 else
1952 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1953 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001954 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001955 binder_transaction_buffer_release(proc, buffer, NULL);
1956 binder_free_buf(proc, buffer);
1957 break;
1958 }
1959
1960 case BC_TRANSACTION:
1961 case BC_REPLY: {
1962 struct binder_transaction_data tr;
1963
1964 if (copy_from_user(&tr, ptr, sizeof(tr)))
1965 return -EFAULT;
1966 ptr += sizeof(tr);
1967 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1968 break;
1969 }
1970
1971 case BC_REGISTER_LOOPER:
1972 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301973 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001974 proc->pid, thread->pid);
1975 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1976 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301977 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001978 proc->pid, thread->pid);
1979 } else if (proc->requested_threads == 0) {
1980 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301981 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001982 proc->pid, thread->pid);
1983 } else {
1984 proc->requested_threads--;
1985 proc->requested_threads_started++;
1986 }
1987 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1988 break;
1989 case BC_ENTER_LOOPER:
1990 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301991 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001992 proc->pid, thread->pid);
1993 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
1994 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301995 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001996 proc->pid, thread->pid);
1997 }
1998 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
1999 break;
2000 case BC_EXIT_LOOPER:
2001 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302002 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002003 proc->pid, thread->pid);
2004 thread->looper |= BINDER_LOOPER_STATE_EXITED;
2005 break;
2006
2007 case BC_REQUEST_DEATH_NOTIFICATION:
2008 case BC_CLEAR_DEATH_NOTIFICATION: {
2009 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002010 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002011 struct binder_ref *ref;
2012 struct binder_ref_death *death;
2013
2014 if (get_user(target, (uint32_t __user *)ptr))
2015 return -EFAULT;
2016 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002017 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002018 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002019 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002020 ref = binder_get_ref(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002021 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302022 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002023 proc->pid, thread->pid,
2024 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2025 "BC_REQUEST_DEATH_NOTIFICATION" :
2026 "BC_CLEAR_DEATH_NOTIFICATION",
2027 target);
2028 break;
2029 }
2030
2031 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002032 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002033 proc->pid, thread->pid,
2034 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2035 "BC_REQUEST_DEATH_NOTIFICATION" :
2036 "BC_CLEAR_DEATH_NOTIFICATION",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002037 (u64)cookie, ref->debug_id, ref->desc,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002038 ref->strong, ref->weak, ref->node->debug_id);
2039
2040 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2041 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302042 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002043 proc->pid, thread->pid);
2044 break;
2045 }
2046 death = kzalloc(sizeof(*death), GFP_KERNEL);
2047 if (death == NULL) {
2048 thread->return_error = BR_ERROR;
2049 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302050 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002051 proc->pid, thread->pid);
2052 break;
2053 }
2054 binder_stats_created(BINDER_STAT_DEATH);
2055 INIT_LIST_HEAD(&death->work.entry);
2056 death->cookie = cookie;
2057 ref->death = death;
2058 if (ref->node->proc == NULL) {
2059 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2060 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2061 list_add_tail(&ref->death->work.entry, &thread->todo);
2062 } else {
2063 list_add_tail(&ref->death->work.entry, &proc->todo);
2064 wake_up_interruptible(&proc->wait);
2065 }
2066 }
2067 } else {
2068 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302069 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002070 proc->pid, thread->pid);
2071 break;
2072 }
2073 death = ref->death;
2074 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002075 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002076 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002077 (u64)death->cookie,
2078 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002079 break;
2080 }
2081 ref->death = NULL;
2082 if (list_empty(&death->work.entry)) {
2083 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2084 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2085 list_add_tail(&death->work.entry, &thread->todo);
2086 } else {
2087 list_add_tail(&death->work.entry, &proc->todo);
2088 wake_up_interruptible(&proc->wait);
2089 }
2090 } else {
2091 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2092 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2093 }
2094 }
2095 } break;
2096 case BC_DEAD_BINDER_DONE: {
2097 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002098 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002099 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09002100
Arve Hjønnevågda498892014-02-21 14:40:26 -08002101 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002102 return -EFAULT;
2103
Lisa Du7a64cd82016-02-17 09:32:52 +08002104 ptr += sizeof(cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002105 list_for_each_entry(w, &proc->delivered_death, entry) {
2106 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
Seunghun Lee10f62862014-05-01 01:30:23 +09002107
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002108 if (tmp_death->cookie == cookie) {
2109 death = tmp_death;
2110 break;
2111 }
2112 }
2113 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002114 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2115 proc->pid, thread->pid, (u64)cookie,
2116 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002117 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002118 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2119 proc->pid, thread->pid, (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002120 break;
2121 }
2122
2123 list_del_init(&death->work.entry);
2124 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2125 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2126 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2127 list_add_tail(&death->work.entry, &thread->todo);
2128 } else {
2129 list_add_tail(&death->work.entry, &proc->todo);
2130 wake_up_interruptible(&proc->wait);
2131 }
2132 }
2133 } break;
2134
2135 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302136 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002137 proc->pid, thread->pid, cmd);
2138 return -EINVAL;
2139 }
2140 *consumed = ptr - buffer;
2141 }
2142 return 0;
2143}
2144
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002145static void binder_stat_br(struct binder_proc *proc,
2146 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002147{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002148 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002149 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2150 binder_stats.br[_IOC_NR(cmd)]++;
2151 proc->stats.br[_IOC_NR(cmd)]++;
2152 thread->stats.br[_IOC_NR(cmd)]++;
2153 }
2154}
2155
2156static int binder_has_proc_work(struct binder_proc *proc,
2157 struct binder_thread *thread)
2158{
2159 return !list_empty(&proc->todo) ||
2160 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2161}
2162
2163static int binder_has_thread_work(struct binder_thread *thread)
2164{
2165 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2166 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2167}
2168
2169static int binder_thread_read(struct binder_proc *proc,
2170 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002171 binder_uintptr_t binder_buffer, size_t size,
2172 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002173{
Arve Hjønnevågda498892014-02-21 14:40:26 -08002174 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002175 void __user *ptr = buffer + *consumed;
2176 void __user *end = buffer + size;
2177
2178 int ret = 0;
2179 int wait_for_proc_work;
2180
2181 if (*consumed == 0) {
2182 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2183 return -EFAULT;
2184 ptr += sizeof(uint32_t);
2185 }
2186
2187retry:
2188 wait_for_proc_work = thread->transaction_stack == NULL &&
2189 list_empty(&thread->todo);
2190
2191 if (thread->return_error != BR_OK && ptr < end) {
2192 if (thread->return_error2 != BR_OK) {
2193 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2194 return -EFAULT;
2195 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002196 binder_stat_br(proc, thread, thread->return_error2);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002197 if (ptr == end)
2198 goto done;
2199 thread->return_error2 = BR_OK;
2200 }
2201 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2202 return -EFAULT;
2203 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002204 binder_stat_br(proc, thread, thread->return_error);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002205 thread->return_error = BR_OK;
2206 goto done;
2207 }
2208
2209
2210 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2211 if (wait_for_proc_work)
2212 proc->ready_threads++;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002213
2214 binder_unlock(__func__);
2215
2216 trace_binder_wait_for_work(wait_for_proc_work,
2217 !!thread->transaction_stack,
2218 !list_empty(&thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002219 if (wait_for_proc_work) {
2220 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2221 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302222 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 +09002223 proc->pid, thread->pid, thread->looper);
2224 wait_event_interruptible(binder_user_error_wait,
2225 binder_stop_on_user_error < 2);
2226 }
2227 binder_set_nice(proc->default_priority);
2228 if (non_block) {
2229 if (!binder_has_proc_work(proc, thread))
2230 ret = -EAGAIN;
2231 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002232 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002233 } else {
2234 if (non_block) {
2235 if (!binder_has_thread_work(thread))
2236 ret = -EAGAIN;
2237 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002238 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002239 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002240
2241 binder_lock(__func__);
2242
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002243 if (wait_for_proc_work)
2244 proc->ready_threads--;
2245 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2246
2247 if (ret)
2248 return ret;
2249
2250 while (1) {
2251 uint32_t cmd;
2252 struct binder_transaction_data tr;
2253 struct binder_work *w;
2254 struct binder_transaction *t = NULL;
2255
Dmitry Voytik395262a2014-09-08 18:16:34 +04002256 if (!list_empty(&thread->todo)) {
2257 w = list_first_entry(&thread->todo, struct binder_work,
2258 entry);
2259 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2260 w = list_first_entry(&proc->todo, struct binder_work,
2261 entry);
2262 } else {
2263 /* no data added */
2264 if (ptr - buffer == 4 &&
2265 !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002266 goto retry;
2267 break;
2268 }
2269
2270 if (end - ptr < sizeof(tr) + 4)
2271 break;
2272
2273 switch (w->type) {
2274 case BINDER_WORK_TRANSACTION: {
2275 t = container_of(w, struct binder_transaction, work);
2276 } break;
2277 case BINDER_WORK_TRANSACTION_COMPLETE: {
2278 cmd = BR_TRANSACTION_COMPLETE;
2279 if (put_user(cmd, (uint32_t __user *)ptr))
2280 return -EFAULT;
2281 ptr += sizeof(uint32_t);
2282
2283 binder_stat_br(proc, thread, cmd);
2284 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302285 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002286 proc->pid, thread->pid);
2287
2288 list_del(&w->entry);
2289 kfree(w);
2290 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2291 } break;
2292 case BINDER_WORK_NODE: {
2293 struct binder_node *node = container_of(w, struct binder_node, work);
2294 uint32_t cmd = BR_NOOP;
2295 const char *cmd_name;
2296 int strong = node->internal_strong_refs || node->local_strong_refs;
2297 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
Seunghun Lee10f62862014-05-01 01:30:23 +09002298
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002299 if (weak && !node->has_weak_ref) {
2300 cmd = BR_INCREFS;
2301 cmd_name = "BR_INCREFS";
2302 node->has_weak_ref = 1;
2303 node->pending_weak_ref = 1;
2304 node->local_weak_refs++;
2305 } else if (strong && !node->has_strong_ref) {
2306 cmd = BR_ACQUIRE;
2307 cmd_name = "BR_ACQUIRE";
2308 node->has_strong_ref = 1;
2309 node->pending_strong_ref = 1;
2310 node->local_strong_refs++;
2311 } else if (!strong && node->has_strong_ref) {
2312 cmd = BR_RELEASE;
2313 cmd_name = "BR_RELEASE";
2314 node->has_strong_ref = 0;
2315 } else if (!weak && node->has_weak_ref) {
2316 cmd = BR_DECREFS;
2317 cmd_name = "BR_DECREFS";
2318 node->has_weak_ref = 0;
2319 }
2320 if (cmd != BR_NOOP) {
2321 if (put_user(cmd, (uint32_t __user *)ptr))
2322 return -EFAULT;
2323 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002324 if (put_user(node->ptr,
2325 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002326 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002327 ptr += sizeof(binder_uintptr_t);
2328 if (put_user(node->cookie,
2329 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002330 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002331 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002332
2333 binder_stat_br(proc, thread, cmd);
2334 binder_debug(BINDER_DEBUG_USER_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002335 "%d:%d %s %d u%016llx c%016llx\n",
2336 proc->pid, thread->pid, cmd_name,
2337 node->debug_id,
2338 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002339 } else {
2340 list_del_init(&w->entry);
2341 if (!weak && !strong) {
2342 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002343 "%d:%d node %d u%016llx c%016llx deleted\n",
2344 proc->pid, thread->pid,
2345 node->debug_id,
2346 (u64)node->ptr,
2347 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002348 rb_erase(&node->rb_node, &proc->nodes);
2349 kfree(node);
2350 binder_stats_deleted(BINDER_STAT_NODE);
2351 } else {
2352 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002353 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2354 proc->pid, thread->pid,
2355 node->debug_id,
2356 (u64)node->ptr,
2357 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002358 }
2359 }
2360 } break;
2361 case BINDER_WORK_DEAD_BINDER:
2362 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2363 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2364 struct binder_ref_death *death;
2365 uint32_t cmd;
2366
2367 death = container_of(w, struct binder_ref_death, work);
2368 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2369 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2370 else
2371 cmd = BR_DEAD_BINDER;
2372 if (put_user(cmd, (uint32_t __user *)ptr))
2373 return -EFAULT;
2374 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002375 if (put_user(death->cookie,
2376 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002377 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002378 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002379 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002380 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002381 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002382 proc->pid, thread->pid,
2383 cmd == BR_DEAD_BINDER ?
2384 "BR_DEAD_BINDER" :
2385 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002386 (u64)death->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002387
2388 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2389 list_del(&w->entry);
2390 kfree(death);
2391 binder_stats_deleted(BINDER_STAT_DEATH);
2392 } else
2393 list_move(&w->entry, &proc->delivered_death);
2394 if (cmd == BR_DEAD_BINDER)
2395 goto done; /* DEAD_BINDER notifications can cause transactions */
2396 } break;
2397 }
2398
2399 if (!t)
2400 continue;
2401
2402 BUG_ON(t->buffer == NULL);
2403 if (t->buffer->target_node) {
2404 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002405
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002406 tr.target.ptr = target_node->ptr;
2407 tr.cookie = target_node->cookie;
2408 t->saved_priority = task_nice(current);
2409 if (t->priority < target_node->min_priority &&
2410 !(t->flags & TF_ONE_WAY))
2411 binder_set_nice(t->priority);
2412 else if (!(t->flags & TF_ONE_WAY) ||
2413 t->saved_priority > target_node->min_priority)
2414 binder_set_nice(target_node->min_priority);
2415 cmd = BR_TRANSACTION;
2416 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002417 tr.target.ptr = 0;
2418 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002419 cmd = BR_REPLY;
2420 }
2421 tr.code = t->code;
2422 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002423 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002424
2425 if (t->from) {
2426 struct task_struct *sender = t->from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09002427
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002428 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002429 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002430 } else {
2431 tr.sender_pid = 0;
2432 }
2433
2434 tr.data_size = t->buffer->data_size;
2435 tr.offsets_size = t->buffer->offsets_size;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002436 tr.data.ptr.buffer = (binder_uintptr_t)(
2437 (uintptr_t)t->buffer->data +
2438 proc->user_buffer_offset);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002439 tr.data.ptr.offsets = tr.data.ptr.buffer +
2440 ALIGN(t->buffer->data_size,
2441 sizeof(void *));
2442
2443 if (put_user(cmd, (uint32_t __user *)ptr))
2444 return -EFAULT;
2445 ptr += sizeof(uint32_t);
2446 if (copy_to_user(ptr, &tr, sizeof(tr)))
2447 return -EFAULT;
2448 ptr += sizeof(tr);
2449
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002450 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002451 binder_stat_br(proc, thread, cmd);
2452 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002453 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002454 proc->pid, thread->pid,
2455 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2456 "BR_REPLY",
2457 t->debug_id, t->from ? t->from->proc->pid : 0,
2458 t->from ? t->from->pid : 0, cmd,
2459 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002460 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002461
2462 list_del(&t->work.entry);
2463 t->buffer->allow_user_free = 1;
2464 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2465 t->to_parent = thread->transaction_stack;
2466 t->to_thread = thread;
2467 thread->transaction_stack = t;
2468 } else {
2469 t->buffer->transaction = NULL;
2470 kfree(t);
2471 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2472 }
2473 break;
2474 }
2475
2476done:
2477
2478 *consumed = ptr - buffer;
2479 if (proc->requested_threads + proc->ready_threads == 0 &&
2480 proc->requested_threads_started < proc->max_threads &&
2481 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2482 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2483 /*spawn a new thread if we leave this out */) {
2484 proc->requested_threads++;
2485 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302486 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002487 proc->pid, thread->pid);
2488 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2489 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002490 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002491 }
2492 return 0;
2493}
2494
2495static void binder_release_work(struct list_head *list)
2496{
2497 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09002498
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002499 while (!list_empty(list)) {
2500 w = list_first_entry(list, struct binder_work, entry);
2501 list_del_init(&w->entry);
2502 switch (w->type) {
2503 case BINDER_WORK_TRANSACTION: {
2504 struct binder_transaction *t;
2505
2506 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002507 if (t->buffer->target_node &&
2508 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002509 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002510 } else {
2511 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302512 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002513 t->debug_id);
2514 t->buffer->transaction = NULL;
2515 kfree(t);
2516 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2517 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002518 } break;
2519 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002520 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302521 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002522 kfree(w);
2523 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2524 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002525 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2526 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2527 struct binder_ref_death *death;
2528
2529 death = container_of(w, struct binder_ref_death, work);
2530 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002531 "undelivered death notification, %016llx\n",
2532 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002533 kfree(death);
2534 binder_stats_deleted(BINDER_STAT_DEATH);
2535 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002536 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302537 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002538 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002539 break;
2540 }
2541 }
2542
2543}
2544
2545static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2546{
2547 struct binder_thread *thread = NULL;
2548 struct rb_node *parent = NULL;
2549 struct rb_node **p = &proc->threads.rb_node;
2550
2551 while (*p) {
2552 parent = *p;
2553 thread = rb_entry(parent, struct binder_thread, rb_node);
2554
2555 if (current->pid < thread->pid)
2556 p = &(*p)->rb_left;
2557 else if (current->pid > thread->pid)
2558 p = &(*p)->rb_right;
2559 else
2560 break;
2561 }
2562 if (*p == NULL) {
2563 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2564 if (thread == NULL)
2565 return NULL;
2566 binder_stats_created(BINDER_STAT_THREAD);
2567 thread->proc = proc;
2568 thread->pid = current->pid;
2569 init_waitqueue_head(&thread->wait);
2570 INIT_LIST_HEAD(&thread->todo);
2571 rb_link_node(&thread->rb_node, parent, p);
2572 rb_insert_color(&thread->rb_node, &proc->threads);
2573 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2574 thread->return_error = BR_OK;
2575 thread->return_error2 = BR_OK;
2576 }
2577 return thread;
2578}
2579
2580static int binder_free_thread(struct binder_proc *proc,
2581 struct binder_thread *thread)
2582{
2583 struct binder_transaction *t;
2584 struct binder_transaction *send_reply = NULL;
2585 int active_transactions = 0;
2586
2587 rb_erase(&thread->rb_node, &proc->threads);
2588 t = thread->transaction_stack;
2589 if (t && t->to_thread == thread)
2590 send_reply = t;
2591 while (t) {
2592 active_transactions++;
2593 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302594 "release %d:%d transaction %d %s, still active\n",
2595 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002596 t->debug_id,
2597 (t->to_thread == thread) ? "in" : "out");
2598
2599 if (t->to_thread == thread) {
2600 t->to_proc = NULL;
2601 t->to_thread = NULL;
2602 if (t->buffer) {
2603 t->buffer->transaction = NULL;
2604 t->buffer = NULL;
2605 }
2606 t = t->to_parent;
2607 } else if (t->from == thread) {
2608 t->from = NULL;
2609 t = t->from_parent;
2610 } else
2611 BUG();
2612 }
2613 if (send_reply)
2614 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2615 binder_release_work(&thread->todo);
2616 kfree(thread);
2617 binder_stats_deleted(BINDER_STAT_THREAD);
2618 return active_transactions;
2619}
2620
2621static unsigned int binder_poll(struct file *filp,
2622 struct poll_table_struct *wait)
2623{
2624 struct binder_proc *proc = filp->private_data;
2625 struct binder_thread *thread = NULL;
2626 int wait_for_proc_work;
2627
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002628 binder_lock(__func__);
2629
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002630 thread = binder_get_thread(proc);
2631
2632 wait_for_proc_work = thread->transaction_stack == NULL &&
2633 list_empty(&thread->todo) && thread->return_error == BR_OK;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002634
2635 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002636
2637 if (wait_for_proc_work) {
2638 if (binder_has_proc_work(proc, thread))
2639 return POLLIN;
2640 poll_wait(filp, &proc->wait, wait);
2641 if (binder_has_proc_work(proc, thread))
2642 return POLLIN;
2643 } else {
2644 if (binder_has_thread_work(thread))
2645 return POLLIN;
2646 poll_wait(filp, &thread->wait, wait);
2647 if (binder_has_thread_work(thread))
2648 return POLLIN;
2649 }
2650 return 0;
2651}
2652
Tair Rzayev78260ac2014-06-03 22:27:21 +03002653static int binder_ioctl_write_read(struct file *filp,
2654 unsigned int cmd, unsigned long arg,
2655 struct binder_thread *thread)
2656{
2657 int ret = 0;
2658 struct binder_proc *proc = filp->private_data;
2659 unsigned int size = _IOC_SIZE(cmd);
2660 void __user *ubuf = (void __user *)arg;
2661 struct binder_write_read bwr;
2662
2663 if (size != sizeof(struct binder_write_read)) {
2664 ret = -EINVAL;
2665 goto out;
2666 }
2667 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2668 ret = -EFAULT;
2669 goto out;
2670 }
2671 binder_debug(BINDER_DEBUG_READ_WRITE,
2672 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2673 proc->pid, thread->pid,
2674 (u64)bwr.write_size, (u64)bwr.write_buffer,
2675 (u64)bwr.read_size, (u64)bwr.read_buffer);
2676
2677 if (bwr.write_size > 0) {
2678 ret = binder_thread_write(proc, thread,
2679 bwr.write_buffer,
2680 bwr.write_size,
2681 &bwr.write_consumed);
2682 trace_binder_write_done(ret);
2683 if (ret < 0) {
2684 bwr.read_consumed = 0;
2685 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2686 ret = -EFAULT;
2687 goto out;
2688 }
2689 }
2690 if (bwr.read_size > 0) {
2691 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2692 bwr.read_size,
2693 &bwr.read_consumed,
2694 filp->f_flags & O_NONBLOCK);
2695 trace_binder_read_done(ret);
2696 if (!list_empty(&proc->todo))
2697 wake_up_interruptible(&proc->wait);
2698 if (ret < 0) {
2699 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2700 ret = -EFAULT;
2701 goto out;
2702 }
2703 }
2704 binder_debug(BINDER_DEBUG_READ_WRITE,
2705 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2706 proc->pid, thread->pid,
2707 (u64)bwr.write_consumed, (u64)bwr.write_size,
2708 (u64)bwr.read_consumed, (u64)bwr.read_size);
2709 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2710 ret = -EFAULT;
2711 goto out;
2712 }
2713out:
2714 return ret;
2715}
2716
2717static int binder_ioctl_set_ctx_mgr(struct file *filp)
2718{
2719 int ret = 0;
2720 struct binder_proc *proc = filp->private_data;
2721 kuid_t curr_euid = current_euid();
2722
2723 if (binder_context_mgr_node != NULL) {
2724 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2725 ret = -EBUSY;
2726 goto out;
2727 }
Stephen Smalley79af7302015-01-21 10:54:10 -05002728 ret = security_binder_set_context_mgr(proc->tsk);
2729 if (ret < 0)
2730 goto out;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002731 if (uid_valid(binder_context_mgr_uid)) {
2732 if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
2733 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2734 from_kuid(&init_user_ns, curr_euid),
2735 from_kuid(&init_user_ns,
2736 binder_context_mgr_uid));
2737 ret = -EPERM;
2738 goto out;
2739 }
2740 } else {
2741 binder_context_mgr_uid = curr_euid;
2742 }
2743 binder_context_mgr_node = binder_new_node(proc, 0, 0);
2744 if (binder_context_mgr_node == NULL) {
2745 ret = -ENOMEM;
2746 goto out;
2747 }
2748 binder_context_mgr_node->local_weak_refs++;
2749 binder_context_mgr_node->local_strong_refs++;
2750 binder_context_mgr_node->has_strong_ref = 1;
2751 binder_context_mgr_node->has_weak_ref = 1;
2752out:
2753 return ret;
2754}
2755
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002756static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2757{
2758 int ret;
2759 struct binder_proc *proc = filp->private_data;
2760 struct binder_thread *thread;
2761 unsigned int size = _IOC_SIZE(cmd);
2762 void __user *ubuf = (void __user *)arg;
2763
Tair Rzayev78260ac2014-06-03 22:27:21 +03002764 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2765 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002766
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002767 trace_binder_ioctl(cmd, arg);
2768
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002769 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2770 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002771 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002772
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002773 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002774 thread = binder_get_thread(proc);
2775 if (thread == NULL) {
2776 ret = -ENOMEM;
2777 goto err;
2778 }
2779
2780 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002781 case BINDER_WRITE_READ:
2782 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
2783 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002784 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002785 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002786 case BINDER_SET_MAX_THREADS:
2787 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2788 ret = -EINVAL;
2789 goto err;
2790 }
2791 break;
2792 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03002793 ret = binder_ioctl_set_ctx_mgr(filp);
2794 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002795 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002796 break;
2797 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302798 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002799 proc->pid, thread->pid);
2800 binder_free_thread(proc, thread);
2801 thread = NULL;
2802 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002803 case BINDER_VERSION: {
2804 struct binder_version __user *ver = ubuf;
2805
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002806 if (size != sizeof(struct binder_version)) {
2807 ret = -EINVAL;
2808 goto err;
2809 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02002810 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2811 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002812 ret = -EINVAL;
2813 goto err;
2814 }
2815 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002816 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002817 default:
2818 ret = -EINVAL;
2819 goto err;
2820 }
2821 ret = 0;
2822err:
2823 if (thread)
2824 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002825 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002826 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2827 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05302828 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 -07002829err_unlocked:
2830 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002831 return ret;
2832}
2833
2834static void binder_vma_open(struct vm_area_struct *vma)
2835{
2836 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002837
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002838 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302839 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002840 proc->pid, vma->vm_start, vma->vm_end,
2841 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2842 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002843}
2844
2845static void binder_vma_close(struct vm_area_struct *vma)
2846{
2847 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002848
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002849 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302850 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002851 proc->pid, vma->vm_start, vma->vm_end,
2852 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2853 (unsigned long)pgprot_val(vma->vm_page_prot));
2854 proc->vma = NULL;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08002855 proc->vma_vm_mm = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002856 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2857}
2858
Vinayak Menonddac7d52014-06-02 18:17:59 +05302859static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2860{
2861 return VM_FAULT_SIGBUS;
2862}
2863
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07002864static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002865 .open = binder_vma_open,
2866 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05302867 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002868};
2869
2870static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2871{
2872 int ret;
2873 struct vm_struct *area;
2874 struct binder_proc *proc = filp->private_data;
2875 const char *failure_string;
2876 struct binder_buffer *buffer;
2877
Al Viroa79f41e2012-08-15 18:23:36 -04002878 if (proc->tsk != current)
2879 return -EINVAL;
2880
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002881 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2882 vma->vm_end = vma->vm_start + SZ_4M;
2883
2884 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2885 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2886 proc->pid, vma->vm_start, vma->vm_end,
2887 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2888 (unsigned long)pgprot_val(vma->vm_page_prot));
2889
2890 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2891 ret = -EPERM;
2892 failure_string = "bad vm_flags";
2893 goto err_bad_arg;
2894 }
2895 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2896
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002897 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002898 if (proc->buffer) {
2899 ret = -EBUSY;
2900 failure_string = "already mapped";
2901 goto err_already_mapped;
2902 }
2903
2904 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2905 if (area == NULL) {
2906 ret = -ENOMEM;
2907 failure_string = "get_vm_area";
2908 goto err_get_vm_area_failed;
2909 }
2910 proc->buffer = area->addr;
2911 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002912 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002913
2914#ifdef CONFIG_CPU_CACHE_VIPT
2915 if (cache_is_vipt_aliasing()) {
2916 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
Sherwin Soltani258767f2012-06-26 02:00:30 -04002917 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 +09002918 vma->vm_start += PAGE_SIZE;
2919 }
2920 }
2921#endif
2922 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2923 if (proc->pages == NULL) {
2924 ret = -ENOMEM;
2925 failure_string = "alloc page array";
2926 goto err_alloc_pages_failed;
2927 }
2928 proc->buffer_size = vma->vm_end - vma->vm_start;
2929
2930 vma->vm_ops = &binder_vm_ops;
2931 vma->vm_private_data = proc;
2932
2933 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2934 ret = -ENOMEM;
2935 failure_string = "alloc small buf";
2936 goto err_alloc_small_buf_failed;
2937 }
2938 buffer = proc->buffer;
2939 INIT_LIST_HEAD(&proc->buffers);
2940 list_add(&buffer->entry, &proc->buffers);
2941 buffer->free = 1;
2942 binder_insert_free_buffer(proc, buffer);
2943 proc->free_async_space = proc->buffer_size / 2;
2944 barrier();
Al Viroa79f41e2012-08-15 18:23:36 -04002945 proc->files = get_files_struct(current);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002946 proc->vma = vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08002947 proc->vma_vm_mm = vma->vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002948
Sherwin Soltani258767f2012-06-26 02:00:30 -04002949 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002950 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2951 return 0;
2952
2953err_alloc_small_buf_failed:
2954 kfree(proc->pages);
2955 proc->pages = NULL;
2956err_alloc_pages_failed:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002957 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002958 vfree(proc->buffer);
2959 proc->buffer = NULL;
2960err_get_vm_area_failed:
2961err_already_mapped:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002962 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002963err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04002964 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002965 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2966 return ret;
2967}
2968
2969static int binder_open(struct inode *nodp, struct file *filp)
2970{
2971 struct binder_proc *proc;
2972
2973 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
2974 current->group_leader->pid, current->pid);
2975
2976 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2977 if (proc == NULL)
2978 return -ENOMEM;
Todd Kjos51050752017-06-29 12:01:36 -07002979 get_task_struct(current->group_leader);
2980 proc->tsk = current->group_leader;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002981 INIT_LIST_HEAD(&proc->todo);
2982 init_waitqueue_head(&proc->wait);
2983 proc->default_priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002984
2985 binder_lock(__func__);
2986
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002987 binder_stats_created(BINDER_STAT_PROC);
2988 hlist_add_head(&proc->proc_node, &binder_procs);
2989 proc->pid = current->group_leader->pid;
2990 INIT_LIST_HEAD(&proc->delivered_death);
2991 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002992
2993 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002994
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07002995 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002996 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09002997
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002998 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07002999 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
3000 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003001 }
3002
3003 return 0;
3004}
3005
3006static int binder_flush(struct file *filp, fl_owner_t id)
3007{
3008 struct binder_proc *proc = filp->private_data;
3009
3010 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3011
3012 return 0;
3013}
3014
3015static void binder_deferred_flush(struct binder_proc *proc)
3016{
3017 struct rb_node *n;
3018 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09003019
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003020 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3021 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09003022
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003023 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3024 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3025 wake_up_interruptible(&thread->wait);
3026 wake_count++;
3027 }
3028 }
3029 wake_up_interruptible_all(&proc->wait);
3030
3031 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3032 "binder_flush: %d woke %d threads\n", proc->pid,
3033 wake_count);
3034}
3035
3036static int binder_release(struct inode *nodp, struct file *filp)
3037{
3038 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003039
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003040 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003041 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3042
3043 return 0;
3044}
3045
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003046static int binder_node_release(struct binder_node *node, int refs)
3047{
3048 struct binder_ref *ref;
3049 int death = 0;
3050
3051 list_del_init(&node->work.entry);
3052 binder_release_work(&node->async_todo);
3053
3054 if (hlist_empty(&node->refs)) {
3055 kfree(node);
3056 binder_stats_deleted(BINDER_STAT_NODE);
3057
3058 return refs;
3059 }
3060
3061 node->proc = NULL;
3062 node->local_strong_refs = 0;
3063 node->local_weak_refs = 0;
3064 hlist_add_head(&node->dead_node, &binder_dead_nodes);
3065
3066 hlist_for_each_entry(ref, &node->refs, node_entry) {
3067 refs++;
3068
3069 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08003070 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003071
3072 death++;
3073
3074 if (list_empty(&ref->death->work.entry)) {
3075 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3076 list_add_tail(&ref->death->work.entry,
3077 &ref->proc->todo);
3078 wake_up_interruptible(&ref->proc->wait);
3079 } else
3080 BUG();
3081 }
3082
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003083 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3084 "node %d now dead, refs %d, death %d\n",
3085 node->debug_id, refs, death);
3086
3087 return refs;
3088}
3089
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003090static void binder_deferred_release(struct binder_proc *proc)
3091{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003092 struct binder_transaction *t;
3093 struct rb_node *n;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003094 int threads, nodes, incoming_refs, outgoing_refs, buffers,
3095 active_transactions, page_count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003096
3097 BUG_ON(proc->vma);
3098 BUG_ON(proc->files);
3099
3100 hlist_del(&proc->proc_node);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003101
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003102 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
3103 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003104 "%s: %d context_mgr_node gone\n",
3105 __func__, proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003106 binder_context_mgr_node = NULL;
3107 }
3108
3109 threads = 0;
3110 active_transactions = 0;
3111 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003112 struct binder_thread *thread;
3113
3114 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003115 threads++;
3116 active_transactions += binder_free_thread(proc, thread);
3117 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003118
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003119 nodes = 0;
3120 incoming_refs = 0;
3121 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003122 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003123
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003124 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003125 nodes++;
3126 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003127 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003128 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003129
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003130 outgoing_refs = 0;
3131 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003132 struct binder_ref *ref;
3133
3134 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003135 outgoing_refs++;
3136 binder_delete_ref(ref);
3137 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003138
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003139 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003140 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003141
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003142 buffers = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003143 while ((n = rb_first(&proc->allocated_buffers))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003144 struct binder_buffer *buffer;
3145
3146 buffer = rb_entry(n, struct binder_buffer, rb_node);
3147
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003148 t = buffer->transaction;
3149 if (t) {
3150 t->buffer = NULL;
3151 buffer->transaction = NULL;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303152 pr_err("release proc %d, transaction %d, not freed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003153 proc->pid, t->debug_id);
3154 /*BUG();*/
3155 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003156
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003157 binder_free_buf(proc, buffer);
3158 buffers++;
3159 }
3160
3161 binder_stats_deleted(BINDER_STAT_PROC);
3162
3163 page_count = 0;
3164 if (proc->pages) {
3165 int i;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003166
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003167 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003168 void *page_addr;
3169
3170 if (!proc->pages[i])
3171 continue;
3172
3173 page_addr = proc->buffer + i * PAGE_SIZE;
3174 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003175 "%s: %d: page %d at %p not freed\n",
3176 __func__, proc->pid, i, page_addr);
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003177 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3178 __free_page(proc->pages[i]);
3179 page_count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003180 }
3181 kfree(proc->pages);
3182 vfree(proc->buffer);
3183 }
3184
3185 put_task_struct(proc->tsk);
3186
3187 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003188 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3189 __func__, proc->pid, threads, nodes, incoming_refs,
3190 outgoing_refs, active_transactions, buffers, page_count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003191
3192 kfree(proc);
3193}
3194
3195static void binder_deferred_func(struct work_struct *work)
3196{
3197 struct binder_proc *proc;
3198 struct files_struct *files;
3199
3200 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003201
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003202 do {
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003203 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003204 mutex_lock(&binder_deferred_lock);
3205 if (!hlist_empty(&binder_deferred_list)) {
3206 proc = hlist_entry(binder_deferred_list.first,
3207 struct binder_proc, deferred_work_node);
3208 hlist_del_init(&proc->deferred_work_node);
3209 defer = proc->deferred_work;
3210 proc->deferred_work = 0;
3211 } else {
3212 proc = NULL;
3213 defer = 0;
3214 }
3215 mutex_unlock(&binder_deferred_lock);
3216
3217 files = NULL;
3218 if (defer & BINDER_DEFERRED_PUT_FILES) {
3219 files = proc->files;
3220 if (files)
3221 proc->files = NULL;
3222 }
3223
3224 if (defer & BINDER_DEFERRED_FLUSH)
3225 binder_deferred_flush(proc);
3226
3227 if (defer & BINDER_DEFERRED_RELEASE)
3228 binder_deferred_release(proc); /* frees proc */
3229
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003230 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003231 if (files)
3232 put_files_struct(files);
3233 } while (proc);
3234}
3235static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3236
3237static void
3238binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3239{
3240 mutex_lock(&binder_deferred_lock);
3241 proc->deferred_work |= defer;
3242 if (hlist_unhashed(&proc->deferred_work_node)) {
3243 hlist_add_head(&proc->deferred_work_node,
3244 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05303245 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003246 }
3247 mutex_unlock(&binder_deferred_lock);
3248}
3249
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003250static void print_binder_transaction(struct seq_file *m, const char *prefix,
3251 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003252{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003253 seq_printf(m,
3254 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3255 prefix, t->debug_id, t,
3256 t->from ? t->from->proc->pid : 0,
3257 t->from ? t->from->pid : 0,
3258 t->to_proc ? t->to_proc->pid : 0,
3259 t->to_thread ? t->to_thread->pid : 0,
3260 t->code, t->flags, t->priority, t->need_reply);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003261 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003262 seq_puts(m, " buffer free\n");
3263 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003264 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003265 if (t->buffer->target_node)
3266 seq_printf(m, " node %d",
3267 t->buffer->target_node->debug_id);
3268 seq_printf(m, " size %zd:%zd data %p\n",
3269 t->buffer->data_size, t->buffer->offsets_size,
3270 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003271}
3272
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003273static void print_binder_buffer(struct seq_file *m, const char *prefix,
3274 struct binder_buffer *buffer)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003275{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003276 seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
3277 prefix, buffer->debug_id, buffer->data,
3278 buffer->data_size, buffer->offsets_size,
3279 buffer->transaction ? "active" : "delivered");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003280}
3281
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003282static void print_binder_work(struct seq_file *m, const char *prefix,
3283 const char *transaction_prefix,
3284 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003285{
3286 struct binder_node *node;
3287 struct binder_transaction *t;
3288
3289 switch (w->type) {
3290 case BINDER_WORK_TRANSACTION:
3291 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003292 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003293 break;
3294 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003295 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003296 break;
3297 case BINDER_WORK_NODE:
3298 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003299 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3300 prefix, node->debug_id,
3301 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003302 break;
3303 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003304 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003305 break;
3306 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003307 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003308 break;
3309 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003310 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003311 break;
3312 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003313 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003314 break;
3315 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003316}
3317
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003318static void print_binder_thread(struct seq_file *m,
3319 struct binder_thread *thread,
3320 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003321{
3322 struct binder_transaction *t;
3323 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003324 size_t start_pos = m->count;
3325 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003326
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003327 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3328 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003329 t = thread->transaction_stack;
3330 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003331 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003332 print_binder_transaction(m,
3333 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003334 t = t->from_parent;
3335 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003336 print_binder_transaction(m,
3337 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003338 t = t->to_parent;
3339 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003340 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003341 t = NULL;
3342 }
3343 }
3344 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003345 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003346 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003347 if (!print_always && m->count == header_pos)
3348 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003349}
3350
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003351static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003352{
3353 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003354 struct binder_work *w;
3355 int count;
3356
3357 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003358 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003359 count++;
3360
Arve Hjønnevågda498892014-02-21 14:40:26 -08003361 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3362 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003363 node->has_strong_ref, node->has_weak_ref,
3364 node->local_strong_refs, node->local_weak_refs,
3365 node->internal_strong_refs, count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003366 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003367 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003368 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003369 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003370 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003371 seq_puts(m, "\n");
3372 list_for_each_entry(w, &node->async_todo, entry)
3373 print_binder_work(m, " ",
3374 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003375}
3376
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003377static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003378{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003379 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3380 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3381 ref->node->debug_id, ref->strong, ref->weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003382}
3383
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003384static void print_binder_proc(struct seq_file *m,
3385 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003386{
3387 struct binder_work *w;
3388 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003389 size_t start_pos = m->count;
3390 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003391
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003392 seq_printf(m, "proc %d\n", proc->pid);
3393 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003394
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003395 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3396 print_binder_thread(m, rb_entry(n, struct binder_thread,
3397 rb_node), print_all);
3398 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003399 struct binder_node *node = rb_entry(n, struct binder_node,
3400 rb_node);
3401 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003402 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003403 }
3404 if (print_all) {
3405 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003406 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003407 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003408 print_binder_ref(m, rb_entry(n, struct binder_ref,
3409 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003410 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003411 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3412 print_binder_buffer(m, " buffer",
3413 rb_entry(n, struct binder_buffer, rb_node));
3414 list_for_each_entry(w, &proc->todo, entry)
3415 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003416 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003417 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003418 break;
3419 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003420 if (!print_all && m->count == header_pos)
3421 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003422}
3423
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003424static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003425 "BR_ERROR",
3426 "BR_OK",
3427 "BR_TRANSACTION",
3428 "BR_REPLY",
3429 "BR_ACQUIRE_RESULT",
3430 "BR_DEAD_REPLY",
3431 "BR_TRANSACTION_COMPLETE",
3432 "BR_INCREFS",
3433 "BR_ACQUIRE",
3434 "BR_RELEASE",
3435 "BR_DECREFS",
3436 "BR_ATTEMPT_ACQUIRE",
3437 "BR_NOOP",
3438 "BR_SPAWN_LOOPER",
3439 "BR_FINISHED",
3440 "BR_DEAD_BINDER",
3441 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3442 "BR_FAILED_REPLY"
3443};
3444
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003445static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003446 "BC_TRANSACTION",
3447 "BC_REPLY",
3448 "BC_ACQUIRE_RESULT",
3449 "BC_FREE_BUFFER",
3450 "BC_INCREFS",
3451 "BC_ACQUIRE",
3452 "BC_RELEASE",
3453 "BC_DECREFS",
3454 "BC_INCREFS_DONE",
3455 "BC_ACQUIRE_DONE",
3456 "BC_ATTEMPT_ACQUIRE",
3457 "BC_REGISTER_LOOPER",
3458 "BC_ENTER_LOOPER",
3459 "BC_EXIT_LOOPER",
3460 "BC_REQUEST_DEATH_NOTIFICATION",
3461 "BC_CLEAR_DEATH_NOTIFICATION",
3462 "BC_DEAD_BINDER_DONE"
3463};
3464
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003465static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003466 "proc",
3467 "thread",
3468 "node",
3469 "ref",
3470 "death",
3471 "transaction",
3472 "transaction_complete"
3473};
3474
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003475static void print_binder_stats(struct seq_file *m, const char *prefix,
3476 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003477{
3478 int i;
3479
3480 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003481 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003482 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3483 if (stats->bc[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003484 seq_printf(m, "%s%s: %d\n", prefix,
3485 binder_command_strings[i], stats->bc[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003486 }
3487
3488 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003489 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003490 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3491 if (stats->br[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003492 seq_printf(m, "%s%s: %d\n", prefix,
3493 binder_return_strings[i], stats->br[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003494 }
3495
3496 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003497 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003498 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003499 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003500 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3501 if (stats->obj_created[i] || stats->obj_deleted[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003502 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3503 binder_objstat_strings[i],
3504 stats->obj_created[i] - stats->obj_deleted[i],
3505 stats->obj_created[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003506 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003507}
3508
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003509static void print_binder_proc_stats(struct seq_file *m,
3510 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003511{
3512 struct binder_work *w;
3513 struct rb_node *n;
3514 int count, strong, weak;
3515
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003516 seq_printf(m, "proc %d\n", proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003517 count = 0;
3518 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3519 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003520 seq_printf(m, " threads: %d\n", count);
3521 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003522 " ready threads %d\n"
3523 " free async space %zd\n", proc->requested_threads,
3524 proc->requested_threads_started, proc->max_threads,
3525 proc->ready_threads, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003526 count = 0;
3527 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3528 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003529 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003530 count = 0;
3531 strong = 0;
3532 weak = 0;
3533 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3534 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3535 rb_node_desc);
3536 count++;
3537 strong += ref->strong;
3538 weak += ref->weak;
3539 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003540 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003541
3542 count = 0;
3543 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3544 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003545 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003546
3547 count = 0;
3548 list_for_each_entry(w, &proc->todo, entry) {
3549 switch (w->type) {
3550 case BINDER_WORK_TRANSACTION:
3551 count++;
3552 break;
3553 default:
3554 break;
3555 }
3556 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003557 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003558
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003559 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003560}
3561
3562
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003563static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003564{
3565 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003566 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003567 int do_lock = !binder_debug_no_lock;
3568
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003569 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003570 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003571
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003572 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003573
3574 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003575 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003576 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003577 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003578
Sasha Levinb67bfe02013-02-27 17:06:00 -08003579 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003580 print_binder_proc(m, proc, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003581 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003582 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003583 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003584}
3585
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003586static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003587{
3588 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003589 int do_lock = !binder_debug_no_lock;
3590
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003591 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003592 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003593
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003594 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003595
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003596 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003597
Sasha Levinb67bfe02013-02-27 17:06:00 -08003598 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003599 print_binder_proc_stats(m, proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003600 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003601 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003602 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003603}
3604
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003605static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003606{
3607 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003608 int do_lock = !binder_debug_no_lock;
3609
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003610 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003611 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003612
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003613 seq_puts(m, "binder transactions:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003614 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003615 print_binder_proc(m, proc, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003616 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003617 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003618 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003619}
3620
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003621static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003622{
Riley Andrews83050a42016-02-09 21:05:33 -08003623 struct binder_proc *itr;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003624 struct binder_proc *proc = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003625 int do_lock = !binder_debug_no_lock;
Riley Andrews83050a42016-02-09 21:05:33 -08003626 bool valid_proc = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003627
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003628 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003629 binder_lock(__func__);
Riley Andrews83050a42016-02-09 21:05:33 -08003630
3631 hlist_for_each_entry(itr, &binder_procs, proc_node) {
3632 if (itr == proc) {
3633 valid_proc = true;
3634 break;
3635 }
3636 }
3637 if (valid_proc) {
3638 seq_puts(m, "binder proc state:\n");
3639 print_binder_proc(m, proc, 1);
3640 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003641 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003642 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003643 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003644}
3645
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003646static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003647 struct binder_transaction_log_entry *e)
3648{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003649 seq_printf(m,
3650 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3651 e->debug_id, (e->call_type == 2) ? "reply" :
3652 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3653 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3654 e->target_handle, e->data_size, e->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003655}
3656
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003657static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003658{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003659 struct binder_transaction_log *log = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003660 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003661
3662 if (log->full) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003663 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3664 print_binder_transaction_log_entry(m, &log->entry[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003665 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003666 for (i = 0; i < log->next; i++)
3667 print_binder_transaction_log_entry(m, &log->entry[i]);
3668 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003669}
3670
3671static const struct file_operations binder_fops = {
3672 .owner = THIS_MODULE,
3673 .poll = binder_poll,
3674 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003675 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003676 .mmap = binder_mmap,
3677 .open = binder_open,
3678 .flush = binder_flush,
3679 .release = binder_release,
3680};
3681
3682static struct miscdevice binder_miscdev = {
3683 .minor = MISC_DYNAMIC_MINOR,
3684 .name = "binder",
3685 .fops = &binder_fops
3686};
3687
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003688BINDER_DEBUG_ENTRY(state);
3689BINDER_DEBUG_ENTRY(stats);
3690BINDER_DEBUG_ENTRY(transactions);
3691BINDER_DEBUG_ENTRY(transaction_log);
3692
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003693static int __init binder_init(void)
3694{
3695 int ret;
3696
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003697 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3698 if (binder_debugfs_dir_entry_root)
3699 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3700 binder_debugfs_dir_entry_root);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003701 ret = misc_register(&binder_miscdev);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003702 if (binder_debugfs_dir_entry_root) {
3703 debugfs_create_file("state",
3704 S_IRUGO,
3705 binder_debugfs_dir_entry_root,
3706 NULL,
3707 &binder_state_fops);
3708 debugfs_create_file("stats",
3709 S_IRUGO,
3710 binder_debugfs_dir_entry_root,
3711 NULL,
3712 &binder_stats_fops);
3713 debugfs_create_file("transactions",
3714 S_IRUGO,
3715 binder_debugfs_dir_entry_root,
3716 NULL,
3717 &binder_transactions_fops);
3718 debugfs_create_file("transaction_log",
3719 S_IRUGO,
3720 binder_debugfs_dir_entry_root,
3721 &binder_transaction_log,
3722 &binder_transaction_log_fops);
3723 debugfs_create_file("failed_transaction_log",
3724 S_IRUGO,
3725 binder_debugfs_dir_entry_root,
3726 &binder_transaction_log_failed,
3727 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003728 }
3729 return ret;
3730}
3731
3732device_initcall(binder_init);
3733
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003734#define CREATE_TRACE_POINTS
3735#include "binder_trace.h"
3736
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003737MODULE_LICENSE("GPL v2");