blob: c78411a22562dc0696307bc7d7d663c141e03277 [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>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090040
41#include "binder.h"
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070042#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090043
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070044static DEFINE_MUTEX(binder_main_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090045static DEFINE_MUTEX(binder_deferred_lock);
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -080046static DEFINE_MUTEX(binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090047
48static HLIST_HEAD(binder_procs);
49static HLIST_HEAD(binder_deferred_list);
50static HLIST_HEAD(binder_dead_nodes);
51
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070052static struct dentry *binder_debugfs_dir_entry_root;
53static struct dentry *binder_debugfs_dir_entry_proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090054static struct binder_node *binder_context_mgr_node;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -060055static kuid_t binder_context_mgr_uid = INVALID_UID;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090056static int binder_last_id;
Arve Hjønnevåg3c762a42010-04-22 15:53:23 -070057static struct workqueue_struct *binder_deferred_workqueue;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090058
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070059#define BINDER_DEBUG_ENTRY(name) \
60static int binder_##name##_open(struct inode *inode, struct file *file) \
61{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070062 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070063} \
64\
65static const struct file_operations binder_##name##_fops = { \
66 .owner = THIS_MODULE, \
67 .open = binder_##name##_open, \
68 .read = seq_read, \
69 .llseek = seq_lseek, \
70 .release = single_release, \
71}
72
73static int binder_proc_show(struct seq_file *m, void *unused);
74BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090075
76/* This is only defined in include/asm-arm/sizes.h */
77#ifndef SZ_1K
78#define SZ_1K 0x400
79#endif
80
81#ifndef SZ_4M
82#define SZ_4M 0x400000
83#endif
84
85#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
86
87#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
88
89enum {
90 BINDER_DEBUG_USER_ERROR = 1U << 0,
91 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
92 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
93 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
94 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
95 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
96 BINDER_DEBUG_READ_WRITE = 1U << 6,
97 BINDER_DEBUG_USER_REFS = 1U << 7,
98 BINDER_DEBUG_THREADS = 1U << 8,
99 BINDER_DEBUG_TRANSACTION = 1U << 9,
100 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
101 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
102 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
103 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
104 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
105 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
106};
107static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
108 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
109module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
110
Zhengwang Ruan2c523252012-03-07 10:36:57 +0800111static bool binder_debug_no_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900112module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
113
114static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
115static int binder_stop_on_user_error;
116
117static int binder_set_stop_on_user_error(const char *val,
118 struct kernel_param *kp)
119{
120 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900121
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900122 ret = param_set_int(val, kp);
123 if (binder_stop_on_user_error < 2)
124 wake_up(&binder_user_error_wait);
125 return ret;
126}
127module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
128 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
129
130#define binder_debug(mask, x...) \
131 do { \
132 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400133 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900134 } while (0)
135
136#define binder_user_error(x...) \
137 do { \
138 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400139 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900140 if (binder_stop_on_user_error) \
141 binder_stop_on_user_error = 2; \
142 } while (0)
143
144enum binder_stat_types {
145 BINDER_STAT_PROC,
146 BINDER_STAT_THREAD,
147 BINDER_STAT_NODE,
148 BINDER_STAT_REF,
149 BINDER_STAT_DEATH,
150 BINDER_STAT_TRANSACTION,
151 BINDER_STAT_TRANSACTION_COMPLETE,
152 BINDER_STAT_COUNT
153};
154
155struct binder_stats {
156 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
157 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
158 int obj_created[BINDER_STAT_COUNT];
159 int obj_deleted[BINDER_STAT_COUNT];
160};
161
162static struct binder_stats binder_stats;
163
164static inline void binder_stats_deleted(enum binder_stat_types type)
165{
166 binder_stats.obj_deleted[type]++;
167}
168
169static inline void binder_stats_created(enum binder_stat_types type)
170{
171 binder_stats.obj_created[type]++;
172}
173
174struct binder_transaction_log_entry {
175 int debug_id;
176 int call_type;
177 int from_proc;
178 int from_thread;
179 int target_handle;
180 int to_proc;
181 int to_thread;
182 int to_node;
183 int data_size;
184 int offsets_size;
185};
186struct binder_transaction_log {
187 int next;
188 int full;
189 struct binder_transaction_log_entry entry[32];
190};
191static struct binder_transaction_log binder_transaction_log;
192static struct binder_transaction_log binder_transaction_log_failed;
193
194static struct binder_transaction_log_entry *binder_transaction_log_add(
195 struct binder_transaction_log *log)
196{
197 struct binder_transaction_log_entry *e;
Seunghun Lee10f62862014-05-01 01:30:23 +0900198
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900199 e = &log->entry[log->next];
200 memset(e, 0, sizeof(*e));
201 log->next++;
202 if (log->next == ARRAY_SIZE(log->entry)) {
203 log->next = 0;
204 log->full = 1;
205 }
206 return e;
207}
208
209struct binder_work {
210 struct list_head entry;
211 enum {
212 BINDER_WORK_TRANSACTION = 1,
213 BINDER_WORK_TRANSACTION_COMPLETE,
214 BINDER_WORK_NODE,
215 BINDER_WORK_DEAD_BINDER,
216 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
217 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
218 } type;
219};
220
221struct binder_node {
222 int debug_id;
223 struct binder_work work;
224 union {
225 struct rb_node rb_node;
226 struct hlist_node dead_node;
227 };
228 struct binder_proc *proc;
229 struct hlist_head refs;
230 int internal_strong_refs;
231 int local_weak_refs;
232 int local_strong_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800233 binder_uintptr_t ptr;
234 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900235 unsigned has_strong_ref:1;
236 unsigned pending_strong_ref:1;
237 unsigned has_weak_ref:1;
238 unsigned pending_weak_ref:1;
239 unsigned has_async_transaction:1;
240 unsigned accept_fds:1;
241 unsigned min_priority:8;
242 struct list_head async_todo;
243};
244
245struct binder_ref_death {
246 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800247 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900248};
249
250struct binder_ref {
251 /* Lookups needed: */
252 /* node + proc => ref (transaction) */
253 /* desc + proc => ref (transaction, inc/dec ref) */
254 /* node => refs + procs (proc exit) */
255 int debug_id;
256 struct rb_node rb_node_desc;
257 struct rb_node rb_node_node;
258 struct hlist_node node_entry;
259 struct binder_proc *proc;
260 struct binder_node *node;
261 uint32_t desc;
262 int strong;
263 int weak;
264 struct binder_ref_death *death;
265};
266
267struct binder_buffer {
Justin P. Mattock217218f2012-01-12 06:51:31 -0800268 struct list_head entry; /* free and allocated entries by address */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900269 struct rb_node rb_node; /* free entry by size or allocated entry */
270 /* by address */
271 unsigned free:1;
272 unsigned allow_user_free:1;
273 unsigned async_transaction:1;
274 unsigned debug_id:29;
275
276 struct binder_transaction *transaction;
277
278 struct binder_node *target_node;
279 size_t data_size;
280 size_t offsets_size;
281 uint8_t data[0];
282};
283
284enum binder_deferred_state {
285 BINDER_DEFERRED_PUT_FILES = 0x01,
286 BINDER_DEFERRED_FLUSH = 0x02,
287 BINDER_DEFERRED_RELEASE = 0x04,
288};
289
290struct binder_proc {
291 struct hlist_node proc_node;
292 struct rb_root threads;
293 struct rb_root nodes;
294 struct rb_root refs_by_desc;
295 struct rb_root refs_by_node;
296 int pid;
297 struct vm_area_struct *vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800298 struct mm_struct *vma_vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900299 struct task_struct *tsk;
300 struct files_struct *files;
301 struct hlist_node deferred_work_node;
302 int deferred_work;
303 void *buffer;
304 ptrdiff_t user_buffer_offset;
305
306 struct list_head buffers;
307 struct rb_root free_buffers;
308 struct rb_root allocated_buffers;
309 size_t free_async_space;
310
311 struct page **pages;
312 size_t buffer_size;
313 uint32_t buffer_free;
314 struct list_head todo;
315 wait_queue_head_t wait;
316 struct binder_stats stats;
317 struct list_head delivered_death;
318 int max_threads;
319 int requested_threads;
320 int requested_threads_started;
321 int ready_threads;
322 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700323 struct dentry *debugfs_entry;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900324};
325
326enum {
327 BINDER_LOOPER_STATE_REGISTERED = 0x01,
328 BINDER_LOOPER_STATE_ENTERED = 0x02,
329 BINDER_LOOPER_STATE_EXITED = 0x04,
330 BINDER_LOOPER_STATE_INVALID = 0x08,
331 BINDER_LOOPER_STATE_WAITING = 0x10,
332 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
333};
334
335struct binder_thread {
336 struct binder_proc *proc;
337 struct rb_node rb_node;
338 int pid;
339 int looper;
340 struct binder_transaction *transaction_stack;
341 struct list_head todo;
342 uint32_t return_error; /* Write failed, return error code in read buf */
343 uint32_t return_error2; /* Write failed, return error code in read */
344 /* buffer. Used when sending a reply to a dead process that */
345 /* we are also waiting on */
346 wait_queue_head_t wait;
347 struct binder_stats stats;
348};
349
350struct binder_transaction {
351 int debug_id;
352 struct binder_work work;
353 struct binder_thread *from;
354 struct binder_transaction *from_parent;
355 struct binder_proc *to_proc;
356 struct binder_thread *to_thread;
357 struct binder_transaction *to_parent;
358 unsigned need_reply:1;
359 /* unsigned is_dead:1; */ /* not used at the moment */
360
361 struct binder_buffer *buffer;
362 unsigned int code;
363 unsigned int flags;
364 long priority;
365 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600366 kuid_t sender_euid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900367};
368
369static void
370binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
371
Sachin Kamatefde99c2012-08-17 16:39:36 +0530372static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900373{
374 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900375 unsigned long rlim_cur;
376 unsigned long irqs;
377
378 if (files == NULL)
379 return -ESRCH;
380
Al Virodcfadfa2012-08-12 17:27:30 -0400381 if (!lock_task_sighand(proc->tsk, &irqs))
382 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900383
Al Virodcfadfa2012-08-12 17:27:30 -0400384 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
385 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900386
Al Virodcfadfa2012-08-12 17:27:30 -0400387 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900388}
389
390/*
391 * copied from fd_install
392 */
393static void task_fd_install(
394 struct binder_proc *proc, unsigned int fd, struct file *file)
395{
Al Virof869e8a2012-08-15 21:06:33 -0400396 if (proc->files)
397 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900398}
399
400/*
401 * copied from sys_close
402 */
403static long task_close_fd(struct binder_proc *proc, unsigned int fd)
404{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900405 int retval;
406
Al Viro483ce1d2012-08-19 12:04:24 -0400407 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900408 return -ESRCH;
409
Al Viro483ce1d2012-08-19 12:04:24 -0400410 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900411 /* can't restart close syscall because file table entry was cleared */
412 if (unlikely(retval == -ERESTARTSYS ||
413 retval == -ERESTARTNOINTR ||
414 retval == -ERESTARTNOHAND ||
415 retval == -ERESTART_RESTARTBLOCK))
416 retval = -EINTR;
417
418 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900419}
420
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700421static inline void binder_lock(const char *tag)
422{
423 trace_binder_lock(tag);
424 mutex_lock(&binder_main_lock);
425 trace_binder_locked(tag);
426}
427
428static inline void binder_unlock(const char *tag)
429{
430 trace_binder_unlock(tag);
431 mutex_unlock(&binder_main_lock);
432}
433
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900434static void binder_set_nice(long nice)
435{
436 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900437
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900438 if (can_nice(current, nice)) {
439 set_user_nice(current, nice);
440 return;
441 }
442 min_nice = 20 - current->signal->rlim[RLIMIT_NICE].rlim_cur;
443 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530444 "%d: nice value %ld not allowed use %ld instead\n",
445 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900446 set_user_nice(current, min_nice);
447 if (min_nice < 20)
448 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530449 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900450}
451
452static size_t binder_buffer_size(struct binder_proc *proc,
453 struct binder_buffer *buffer)
454{
455 if (list_is_last(&buffer->entry, &proc->buffers))
456 return proc->buffer + proc->buffer_size - (void *)buffer->data;
457 else
458 return (size_t)list_entry(buffer->entry.next,
459 struct binder_buffer, entry) - (size_t)buffer->data;
460}
461
462static void binder_insert_free_buffer(struct binder_proc *proc,
463 struct binder_buffer *new_buffer)
464{
465 struct rb_node **p = &proc->free_buffers.rb_node;
466 struct rb_node *parent = NULL;
467 struct binder_buffer *buffer;
468 size_t buffer_size;
469 size_t new_buffer_size;
470
471 BUG_ON(!new_buffer->free);
472
473 new_buffer_size = binder_buffer_size(proc, new_buffer);
474
475 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530476 "%d: add free buffer, size %zd, at %p\n",
477 proc->pid, new_buffer_size, new_buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900478
479 while (*p) {
480 parent = *p;
481 buffer = rb_entry(parent, struct binder_buffer, rb_node);
482 BUG_ON(!buffer->free);
483
484 buffer_size = binder_buffer_size(proc, buffer);
485
486 if (new_buffer_size < buffer_size)
487 p = &parent->rb_left;
488 else
489 p = &parent->rb_right;
490 }
491 rb_link_node(&new_buffer->rb_node, parent, p);
492 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
493}
494
495static void binder_insert_allocated_buffer(struct binder_proc *proc,
496 struct binder_buffer *new_buffer)
497{
498 struct rb_node **p = &proc->allocated_buffers.rb_node;
499 struct rb_node *parent = NULL;
500 struct binder_buffer *buffer;
501
502 BUG_ON(new_buffer->free);
503
504 while (*p) {
505 parent = *p;
506 buffer = rb_entry(parent, struct binder_buffer, rb_node);
507 BUG_ON(buffer->free);
508
509 if (new_buffer < buffer)
510 p = &parent->rb_left;
511 else if (new_buffer > buffer)
512 p = &parent->rb_right;
513 else
514 BUG();
515 }
516 rb_link_node(&new_buffer->rb_node, parent, p);
517 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
518}
519
520static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800521 uintptr_t user_ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900522{
523 struct rb_node *n = proc->allocated_buffers.rb_node;
524 struct binder_buffer *buffer;
525 struct binder_buffer *kern_ptr;
526
Arve Hjønnevågda498892014-02-21 14:40:26 -0800527 kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
528 - offsetof(struct binder_buffer, data));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900529
530 while (n) {
531 buffer = rb_entry(n, struct binder_buffer, rb_node);
532 BUG_ON(buffer->free);
533
534 if (kern_ptr < buffer)
535 n = n->rb_left;
536 else if (kern_ptr > buffer)
537 n = n->rb_right;
538 else
539 return buffer;
540 }
541 return NULL;
542}
543
544static int binder_update_page_range(struct binder_proc *proc, int allocate,
545 void *start, void *end,
546 struct vm_area_struct *vma)
547{
548 void *page_addr;
549 unsigned long user_page_addr;
550 struct vm_struct tmp_area;
551 struct page **page;
552 struct mm_struct *mm;
553
554 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530555 "%d: %s pages %p-%p\n", proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900556 allocate ? "allocate" : "free", start, end);
557
558 if (end <= start)
559 return 0;
560
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700561 trace_binder_update_page_range(proc, allocate, start, end);
562
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900563 if (vma)
564 mm = NULL;
565 else
566 mm = get_task_mm(proc->tsk);
567
568 if (mm) {
569 down_write(&mm->mmap_sem);
570 vma = proc->vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800571 if (vma && mm != proc->vma_vm_mm) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530572 pr_err("%d: vma mm and task mm mismatch\n",
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -0800573 proc->pid);
574 vma = NULL;
575 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900576 }
577
578 if (allocate == 0)
579 goto free_range;
580
581 if (vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530582 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
583 proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900584 goto err_no_vma;
585 }
586
587 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
588 int ret;
589 struct page **page_array_ptr;
Seunghun Lee10f62862014-05-01 01:30:23 +0900590
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900591 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
592
593 BUG_ON(*page);
Arve Hjønnevåg585650d2012-10-16 15:29:55 -0700594 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900595 if (*page == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530596 pr_err("%d: binder_alloc_buf failed for page at %p\n",
597 proc->pid, page_addr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900598 goto err_alloc_page_failed;
599 }
600 tmp_area.addr = page_addr;
601 tmp_area.size = PAGE_SIZE + PAGE_SIZE /* guard page? */;
602 page_array_ptr = page;
603 ret = map_vm_area(&tmp_area, PAGE_KERNEL, &page_array_ptr);
604 if (ret) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530605 pr_err("%d: binder_alloc_buf failed to map page at %p in kernel\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900606 proc->pid, page_addr);
607 goto err_map_kernel_failed;
608 }
609 user_page_addr =
610 (uintptr_t)page_addr + proc->user_buffer_offset;
611 ret = vm_insert_page(vma, user_page_addr, page[0]);
612 if (ret) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530613 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900614 proc->pid, user_page_addr);
615 goto err_vm_insert_page_failed;
616 }
617 /* vm_insert_page does not seem to increment the refcount */
618 }
619 if (mm) {
620 up_write(&mm->mmap_sem);
621 mmput(mm);
622 }
623 return 0;
624
625free_range:
626 for (page_addr = end - PAGE_SIZE; page_addr >= start;
627 page_addr -= PAGE_SIZE) {
628 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
629 if (vma)
630 zap_page_range(vma, (uintptr_t)page_addr +
631 proc->user_buffer_offset, PAGE_SIZE, NULL);
632err_vm_insert_page_failed:
633 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
634err_map_kernel_failed:
635 __free_page(*page);
636 *page = NULL;
637err_alloc_page_failed:
638 ;
639 }
640err_no_vma:
641 if (mm) {
642 up_write(&mm->mmap_sem);
643 mmput(mm);
644 }
645 return -ENOMEM;
646}
647
648static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
649 size_t data_size,
650 size_t offsets_size, int is_async)
651{
652 struct rb_node *n = proc->free_buffers.rb_node;
653 struct binder_buffer *buffer;
654 size_t buffer_size;
655 struct rb_node *best_fit = NULL;
656 void *has_page_addr;
657 void *end_page_addr;
658 size_t size;
659
660 if (proc->vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530661 pr_err("%d: binder_alloc_buf, no vma\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900662 proc->pid);
663 return NULL;
664 }
665
666 size = ALIGN(data_size, sizeof(void *)) +
667 ALIGN(offsets_size, sizeof(void *));
668
669 if (size < data_size || size < offsets_size) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530670 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
671 proc->pid, data_size, offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900672 return NULL;
673 }
674
675 if (is_async &&
676 proc->free_async_space < size + sizeof(struct binder_buffer)) {
677 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530678 "%d: binder_alloc_buf size %zd failed, no async space left\n",
679 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900680 return NULL;
681 }
682
683 while (n) {
684 buffer = rb_entry(n, struct binder_buffer, rb_node);
685 BUG_ON(!buffer->free);
686 buffer_size = binder_buffer_size(proc, buffer);
687
688 if (size < buffer_size) {
689 best_fit = n;
690 n = n->rb_left;
691 } else if (size > buffer_size)
692 n = n->rb_right;
693 else {
694 best_fit = n;
695 break;
696 }
697 }
698 if (best_fit == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530699 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
700 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900701 return NULL;
702 }
703 if (n == NULL) {
704 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
705 buffer_size = binder_buffer_size(proc, buffer);
706 }
707
708 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530709 "%d: binder_alloc_buf size %zd got buffer %p size %zd\n",
710 proc->pid, size, buffer, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900711
712 has_page_addr =
713 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
714 if (n == NULL) {
715 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
716 buffer_size = size; /* no room for other buffers */
717 else
718 buffer_size = size + sizeof(struct binder_buffer);
719 }
720 end_page_addr =
721 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
722 if (end_page_addr > has_page_addr)
723 end_page_addr = has_page_addr;
724 if (binder_update_page_range(proc, 1,
725 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
726 return NULL;
727
728 rb_erase(best_fit, &proc->free_buffers);
729 buffer->free = 0;
730 binder_insert_allocated_buffer(proc, buffer);
731 if (buffer_size != size) {
732 struct binder_buffer *new_buffer = (void *)buffer->data + size;
Seunghun Lee10f62862014-05-01 01:30:23 +0900733
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900734 list_add(&new_buffer->entry, &buffer->entry);
735 new_buffer->free = 1;
736 binder_insert_free_buffer(proc, new_buffer);
737 }
738 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530739 "%d: binder_alloc_buf size %zd got %p\n",
740 proc->pid, size, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900741 buffer->data_size = data_size;
742 buffer->offsets_size = offsets_size;
743 buffer->async_transaction = is_async;
744 if (is_async) {
745 proc->free_async_space -= size + sizeof(struct binder_buffer);
746 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530747 "%d: binder_alloc_buf size %zd async free %zd\n",
748 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900749 }
750
751 return buffer;
752}
753
754static void *buffer_start_page(struct binder_buffer *buffer)
755{
756 return (void *)((uintptr_t)buffer & PAGE_MASK);
757}
758
759static void *buffer_end_page(struct binder_buffer *buffer)
760{
761 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
762}
763
764static void binder_delete_free_buffer(struct binder_proc *proc,
765 struct binder_buffer *buffer)
766{
767 struct binder_buffer *prev, *next = NULL;
768 int free_page_end = 1;
769 int free_page_start = 1;
770
771 BUG_ON(proc->buffers.next == &buffer->entry);
772 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
773 BUG_ON(!prev->free);
774 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
775 free_page_start = 0;
776 if (buffer_end_page(prev) == buffer_end_page(buffer))
777 free_page_end = 0;
778 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530779 "%d: merge free, buffer %p share page with %p\n",
780 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900781 }
782
783 if (!list_is_last(&buffer->entry, &proc->buffers)) {
784 next = list_entry(buffer->entry.next,
785 struct binder_buffer, entry);
786 if (buffer_start_page(next) == buffer_end_page(buffer)) {
787 free_page_end = 0;
788 if (buffer_start_page(next) ==
789 buffer_start_page(buffer))
790 free_page_start = 0;
791 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530792 "%d: merge free, buffer %p share page with %p\n",
793 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900794 }
795 }
796 list_del(&buffer->entry);
797 if (free_page_start || free_page_end) {
798 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Masanari Iida1dcdbfd2013-06-23 23:47:15 +0900799 "%d: merge free, buffer %p do not share page%s%s with %p or %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900800 proc->pid, buffer, free_page_start ? "" : " end",
801 free_page_end ? "" : " start", prev, next);
802 binder_update_page_range(proc, 0, free_page_start ?
803 buffer_start_page(buffer) : buffer_end_page(buffer),
804 (free_page_end ? buffer_end_page(buffer) :
805 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
806 }
807}
808
809static void binder_free_buf(struct binder_proc *proc,
810 struct binder_buffer *buffer)
811{
812 size_t size, buffer_size;
813
814 buffer_size = binder_buffer_size(proc, buffer);
815
816 size = ALIGN(buffer->data_size, sizeof(void *)) +
817 ALIGN(buffer->offsets_size, sizeof(void *));
818
819 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530820 "%d: binder_free_buf %p size %zd buffer_size %zd\n",
821 proc->pid, buffer, size, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900822
823 BUG_ON(buffer->free);
824 BUG_ON(size > buffer_size);
825 BUG_ON(buffer->transaction != NULL);
826 BUG_ON((void *)buffer < proc->buffer);
827 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
828
829 if (buffer->async_transaction) {
830 proc->free_async_space += size + sizeof(struct binder_buffer);
831
832 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530833 "%d: binder_free_buf size %zd async free %zd\n",
834 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900835 }
836
837 binder_update_page_range(proc, 0,
838 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
839 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
840 NULL);
841 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
842 buffer->free = 1;
843 if (!list_is_last(&buffer->entry, &proc->buffers)) {
844 struct binder_buffer *next = list_entry(buffer->entry.next,
845 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900846
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900847 if (next->free) {
848 rb_erase(&next->rb_node, &proc->free_buffers);
849 binder_delete_free_buffer(proc, next);
850 }
851 }
852 if (proc->buffers.next != &buffer->entry) {
853 struct binder_buffer *prev = list_entry(buffer->entry.prev,
854 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900855
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900856 if (prev->free) {
857 binder_delete_free_buffer(proc, buffer);
858 rb_erase(&prev->rb_node, &proc->free_buffers);
859 buffer = prev;
860 }
861 }
862 binder_insert_free_buffer(proc, buffer);
863}
864
865static struct binder_node *binder_get_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800866 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900867{
868 struct rb_node *n = proc->nodes.rb_node;
869 struct binder_node *node;
870
871 while (n) {
872 node = rb_entry(n, struct binder_node, rb_node);
873
874 if (ptr < node->ptr)
875 n = n->rb_left;
876 else if (ptr > node->ptr)
877 n = n->rb_right;
878 else
879 return node;
880 }
881 return NULL;
882}
883
884static struct binder_node *binder_new_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800885 binder_uintptr_t ptr,
886 binder_uintptr_t cookie)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900887{
888 struct rb_node **p = &proc->nodes.rb_node;
889 struct rb_node *parent = NULL;
890 struct binder_node *node;
891
892 while (*p) {
893 parent = *p;
894 node = rb_entry(parent, struct binder_node, rb_node);
895
896 if (ptr < node->ptr)
897 p = &(*p)->rb_left;
898 else if (ptr > node->ptr)
899 p = &(*p)->rb_right;
900 else
901 return NULL;
902 }
903
904 node = kzalloc(sizeof(*node), GFP_KERNEL);
905 if (node == NULL)
906 return NULL;
907 binder_stats_created(BINDER_STAT_NODE);
908 rb_link_node(&node->rb_node, parent, p);
909 rb_insert_color(&node->rb_node, &proc->nodes);
910 node->debug_id = ++binder_last_id;
911 node->proc = proc;
912 node->ptr = ptr;
913 node->cookie = cookie;
914 node->work.type = BINDER_WORK_NODE;
915 INIT_LIST_HEAD(&node->work.entry);
916 INIT_LIST_HEAD(&node->async_todo);
917 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800918 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900919 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800920 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900921 return node;
922}
923
924static int binder_inc_node(struct binder_node *node, int strong, int internal,
925 struct list_head *target_list)
926{
927 if (strong) {
928 if (internal) {
929 if (target_list == NULL &&
930 node->internal_strong_refs == 0 &&
931 !(node == binder_context_mgr_node &&
932 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530933 pr_err("invalid inc strong node for %d\n",
934 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900935 return -EINVAL;
936 }
937 node->internal_strong_refs++;
938 } else
939 node->local_strong_refs++;
940 if (!node->has_strong_ref && target_list) {
941 list_del_init(&node->work.entry);
942 list_add_tail(&node->work.entry, target_list);
943 }
944 } else {
945 if (!internal)
946 node->local_weak_refs++;
947 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
948 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530949 pr_err("invalid inc weak node for %d\n",
950 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900951 return -EINVAL;
952 }
953 list_add_tail(&node->work.entry, target_list);
954 }
955 }
956 return 0;
957}
958
959static int binder_dec_node(struct binder_node *node, int strong, int internal)
960{
961 if (strong) {
962 if (internal)
963 node->internal_strong_refs--;
964 else
965 node->local_strong_refs--;
966 if (node->local_strong_refs || node->internal_strong_refs)
967 return 0;
968 } else {
969 if (!internal)
970 node->local_weak_refs--;
971 if (node->local_weak_refs || !hlist_empty(&node->refs))
972 return 0;
973 }
974 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
975 if (list_empty(&node->work.entry)) {
976 list_add_tail(&node->work.entry, &node->proc->todo);
977 wake_up_interruptible(&node->proc->wait);
978 }
979 } else {
980 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
981 !node->local_weak_refs) {
982 list_del_init(&node->work.entry);
983 if (node->proc) {
984 rb_erase(&node->rb_node, &node->proc->nodes);
985 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530986 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900987 node->debug_id);
988 } else {
989 hlist_del(&node->dead_node);
990 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530991 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900992 node->debug_id);
993 }
994 kfree(node);
995 binder_stats_deleted(BINDER_STAT_NODE);
996 }
997 }
998
999 return 0;
1000}
1001
1002
1003static struct binder_ref *binder_get_ref(struct binder_proc *proc,
1004 uint32_t desc)
1005{
1006 struct rb_node *n = proc->refs_by_desc.rb_node;
1007 struct binder_ref *ref;
1008
1009 while (n) {
1010 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1011
1012 if (desc < ref->desc)
1013 n = n->rb_left;
1014 else if (desc > ref->desc)
1015 n = n->rb_right;
1016 else
1017 return ref;
1018 }
1019 return NULL;
1020}
1021
1022static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1023 struct binder_node *node)
1024{
1025 struct rb_node *n;
1026 struct rb_node **p = &proc->refs_by_node.rb_node;
1027 struct rb_node *parent = NULL;
1028 struct binder_ref *ref, *new_ref;
1029
1030 while (*p) {
1031 parent = *p;
1032 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1033
1034 if (node < ref->node)
1035 p = &(*p)->rb_left;
1036 else if (node > ref->node)
1037 p = &(*p)->rb_right;
1038 else
1039 return ref;
1040 }
1041 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1042 if (new_ref == NULL)
1043 return NULL;
1044 binder_stats_created(BINDER_STAT_REF);
1045 new_ref->debug_id = ++binder_last_id;
1046 new_ref->proc = proc;
1047 new_ref->node = node;
1048 rb_link_node(&new_ref->rb_node_node, parent, p);
1049 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1050
1051 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1052 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1053 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1054 if (ref->desc > new_ref->desc)
1055 break;
1056 new_ref->desc = ref->desc + 1;
1057 }
1058
1059 p = &proc->refs_by_desc.rb_node;
1060 while (*p) {
1061 parent = *p;
1062 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1063
1064 if (new_ref->desc < ref->desc)
1065 p = &(*p)->rb_left;
1066 else if (new_ref->desc > ref->desc)
1067 p = &(*p)->rb_right;
1068 else
1069 BUG();
1070 }
1071 rb_link_node(&new_ref->rb_node_desc, parent, p);
1072 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1073 if (node) {
1074 hlist_add_head(&new_ref->node_entry, &node->refs);
1075
1076 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301077 "%d new ref %d desc %d for node %d\n",
1078 proc->pid, new_ref->debug_id, new_ref->desc,
1079 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001080 } else {
1081 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301082 "%d new ref %d desc %d for dead node\n",
1083 proc->pid, new_ref->debug_id, new_ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001084 }
1085 return new_ref;
1086}
1087
1088static void binder_delete_ref(struct binder_ref *ref)
1089{
1090 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301091 "%d delete ref %d desc %d for node %d\n",
1092 ref->proc->pid, ref->debug_id, ref->desc,
1093 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001094
1095 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1096 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1097 if (ref->strong)
1098 binder_dec_node(ref->node, 1, 1);
1099 hlist_del(&ref->node_entry);
1100 binder_dec_node(ref->node, 0, 1);
1101 if (ref->death) {
1102 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301103 "%d delete ref %d desc %d has death notification\n",
1104 ref->proc->pid, ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001105 list_del(&ref->death->work.entry);
1106 kfree(ref->death);
1107 binder_stats_deleted(BINDER_STAT_DEATH);
1108 }
1109 kfree(ref);
1110 binder_stats_deleted(BINDER_STAT_REF);
1111}
1112
1113static int binder_inc_ref(struct binder_ref *ref, int strong,
1114 struct list_head *target_list)
1115{
1116 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001117
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001118 if (strong) {
1119 if (ref->strong == 0) {
1120 ret = binder_inc_node(ref->node, 1, 1, target_list);
1121 if (ret)
1122 return ret;
1123 }
1124 ref->strong++;
1125 } else {
1126 if (ref->weak == 0) {
1127 ret = binder_inc_node(ref->node, 0, 1, target_list);
1128 if (ret)
1129 return ret;
1130 }
1131 ref->weak++;
1132 }
1133 return 0;
1134}
1135
1136
1137static int binder_dec_ref(struct binder_ref *ref, int strong)
1138{
1139 if (strong) {
1140 if (ref->strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301141 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001142 ref->proc->pid, ref->debug_id,
1143 ref->desc, ref->strong, ref->weak);
1144 return -EINVAL;
1145 }
1146 ref->strong--;
1147 if (ref->strong == 0) {
1148 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001149
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001150 ret = binder_dec_node(ref->node, strong, 1);
1151 if (ret)
1152 return ret;
1153 }
1154 } else {
1155 if (ref->weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301156 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001157 ref->proc->pid, ref->debug_id,
1158 ref->desc, ref->strong, ref->weak);
1159 return -EINVAL;
1160 }
1161 ref->weak--;
1162 }
1163 if (ref->strong == 0 && ref->weak == 0)
1164 binder_delete_ref(ref);
1165 return 0;
1166}
1167
1168static void binder_pop_transaction(struct binder_thread *target_thread,
1169 struct binder_transaction *t)
1170{
1171 if (target_thread) {
1172 BUG_ON(target_thread->transaction_stack != t);
1173 BUG_ON(target_thread->transaction_stack->from != target_thread);
1174 target_thread->transaction_stack =
1175 target_thread->transaction_stack->from_parent;
1176 t->from = NULL;
1177 }
1178 t->need_reply = 0;
1179 if (t->buffer)
1180 t->buffer->transaction = NULL;
1181 kfree(t);
1182 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1183}
1184
1185static void binder_send_failed_reply(struct binder_transaction *t,
1186 uint32_t error_code)
1187{
1188 struct binder_thread *target_thread;
Seunghun Lee10f62862014-05-01 01:30:23 +09001189
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001190 BUG_ON(t->flags & TF_ONE_WAY);
1191 while (1) {
1192 target_thread = t->from;
1193 if (target_thread) {
1194 if (target_thread->return_error != BR_OK &&
1195 target_thread->return_error2 == BR_OK) {
1196 target_thread->return_error2 =
1197 target_thread->return_error;
1198 target_thread->return_error = BR_OK;
1199 }
1200 if (target_thread->return_error == BR_OK) {
1201 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301202 "send failed reply for transaction %d to %d:%d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001203 t->debug_id, target_thread->proc->pid,
1204 target_thread->pid);
1205
1206 binder_pop_transaction(target_thread, t);
1207 target_thread->return_error = error_code;
1208 wake_up_interruptible(&target_thread->wait);
1209 } else {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301210 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1211 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001212 target_thread->pid,
1213 target_thread->return_error);
1214 }
1215 return;
1216 } else {
1217 struct binder_transaction *next = t->from_parent;
1218
1219 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301220 "send failed reply for transaction %d, target dead\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001221 t->debug_id);
1222
1223 binder_pop_transaction(target_thread, t);
1224 if (next == NULL) {
1225 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301226 "reply failed, no target thread at root\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001227 return;
1228 }
1229 t = next;
1230 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301231 "reply failed, no target thread -- retry %d\n",
1232 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001233 }
1234 }
1235}
1236
1237static void binder_transaction_buffer_release(struct binder_proc *proc,
1238 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001239 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001240{
Arve Hjønnevågda498892014-02-21 14:40:26 -08001241 binder_size_t *offp, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001242 int debug_id = buffer->debug_id;
1243
1244 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301245 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001246 proc->pid, buffer->debug_id,
1247 buffer->data_size, buffer->offsets_size, failed_at);
1248
1249 if (buffer->target_node)
1250 binder_dec_node(buffer->target_node, 1, 0);
1251
Arve Hjønnevågda498892014-02-21 14:40:26 -08001252 offp = (binder_size_t *)(buffer->data +
1253 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001254 if (failed_at)
1255 off_end = failed_at;
1256 else
1257 off_end = (void *)offp + buffer->offsets_size;
1258 for (; offp < off_end; offp++) {
1259 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001260
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001261 if (*offp > buffer->data_size - sizeof(*fp) ||
1262 buffer->data_size < sizeof(*fp) ||
Serban Constantinescuec35e852013-07-04 10:54:46 +01001263 !IS_ALIGNED(*offp, sizeof(u32))) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001264 pr_err("transaction release %d bad offset %lld, size %zd\n",
1265 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001266 continue;
1267 }
1268 fp = (struct flat_binder_object *)(buffer->data + *offp);
1269 switch (fp->type) {
1270 case BINDER_TYPE_BINDER:
1271 case BINDER_TYPE_WEAK_BINDER: {
1272 struct binder_node *node = binder_get_node(proc, fp->binder);
Seunghun Lee10f62862014-05-01 01:30:23 +09001273
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001274 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001275 pr_err("transaction release %d bad node %016llx\n",
1276 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001277 break;
1278 }
1279 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001280 " node %d u%016llx\n",
1281 node->debug_id, (u64)node->ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001282 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1283 } break;
1284 case BINDER_TYPE_HANDLE:
1285 case BINDER_TYPE_WEAK_HANDLE: {
1286 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
Seunghun Lee10f62862014-05-01 01:30:23 +09001287
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001288 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001289 pr_err("transaction release %d bad handle %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301290 debug_id, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001291 break;
1292 }
1293 binder_debug(BINDER_DEBUG_TRANSACTION,
1294 " ref %d desc %d (node %d)\n",
1295 ref->debug_id, ref->desc, ref->node->debug_id);
1296 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1297 } break;
1298
1299 case BINDER_TYPE_FD:
1300 binder_debug(BINDER_DEBUG_TRANSACTION,
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001301 " fd %d\n", fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001302 if (failed_at)
1303 task_close_fd(proc, fp->handle);
1304 break;
1305
1306 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001307 pr_err("transaction release %d bad object type %x\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301308 debug_id, fp->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001309 break;
1310 }
1311 }
1312}
1313
1314static void binder_transaction(struct binder_proc *proc,
1315 struct binder_thread *thread,
1316 struct binder_transaction_data *tr, int reply)
1317{
1318 struct binder_transaction *t;
1319 struct binder_work *tcomplete;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001320 binder_size_t *offp, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001321 struct binder_proc *target_proc;
1322 struct binder_thread *target_thread = NULL;
1323 struct binder_node *target_node = NULL;
1324 struct list_head *target_list;
1325 wait_queue_head_t *target_wait;
1326 struct binder_transaction *in_reply_to = NULL;
1327 struct binder_transaction_log_entry *e;
1328 uint32_t return_error;
Jerry Snitselaarf994d832014-04-30 23:58:55 -07001329 const struct cred *cred = __task_cred(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001330
1331 e = binder_transaction_log_add(&binder_transaction_log);
1332 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1333 e->from_proc = proc->pid;
1334 e->from_thread = thread->pid;
1335 e->target_handle = tr->target.handle;
1336 e->data_size = tr->data_size;
1337 e->offsets_size = tr->offsets_size;
1338
1339 if (reply) {
1340 in_reply_to = thread->transaction_stack;
1341 if (in_reply_to == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301342 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001343 proc->pid, thread->pid);
1344 return_error = BR_FAILED_REPLY;
1345 goto err_empty_call_stack;
1346 }
1347 binder_set_nice(in_reply_to->saved_priority);
1348 if (in_reply_to->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301349 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 +09001350 proc->pid, thread->pid, in_reply_to->debug_id,
1351 in_reply_to->to_proc ?
1352 in_reply_to->to_proc->pid : 0,
1353 in_reply_to->to_thread ?
1354 in_reply_to->to_thread->pid : 0);
1355 return_error = BR_FAILED_REPLY;
1356 in_reply_to = NULL;
1357 goto err_bad_call_stack;
1358 }
1359 thread->transaction_stack = in_reply_to->to_parent;
1360 target_thread = in_reply_to->from;
1361 if (target_thread == NULL) {
1362 return_error = BR_DEAD_REPLY;
1363 goto err_dead_binder;
1364 }
1365 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301366 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 +09001367 proc->pid, thread->pid,
1368 target_thread->transaction_stack ?
1369 target_thread->transaction_stack->debug_id : 0,
1370 in_reply_to->debug_id);
1371 return_error = BR_FAILED_REPLY;
1372 in_reply_to = NULL;
1373 target_thread = NULL;
1374 goto err_dead_binder;
1375 }
1376 target_proc = target_thread->proc;
1377 } else {
1378 if (tr->target.handle) {
1379 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001380
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001381 ref = binder_get_ref(proc, tr->target.handle);
1382 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301383 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001384 proc->pid, thread->pid);
1385 return_error = BR_FAILED_REPLY;
1386 goto err_invalid_target_handle;
1387 }
1388 target_node = ref->node;
1389 } else {
1390 target_node = binder_context_mgr_node;
1391 if (target_node == NULL) {
1392 return_error = BR_DEAD_REPLY;
1393 goto err_no_context_mgr_node;
1394 }
1395 }
1396 e->to_node = target_node->debug_id;
1397 target_proc = target_node->proc;
1398 if (target_proc == NULL) {
1399 return_error = BR_DEAD_REPLY;
1400 goto err_dead_binder;
1401 }
1402 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1403 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001404
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001405 tmp = thread->transaction_stack;
1406 if (tmp->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301407 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 +09001408 proc->pid, thread->pid, tmp->debug_id,
1409 tmp->to_proc ? tmp->to_proc->pid : 0,
1410 tmp->to_thread ?
1411 tmp->to_thread->pid : 0);
1412 return_error = BR_FAILED_REPLY;
1413 goto err_bad_call_stack;
1414 }
1415 while (tmp) {
1416 if (tmp->from && tmp->from->proc == target_proc)
1417 target_thread = tmp->from;
1418 tmp = tmp->from_parent;
1419 }
1420 }
1421 }
1422 if (target_thread) {
1423 e->to_thread = target_thread->pid;
1424 target_list = &target_thread->todo;
1425 target_wait = &target_thread->wait;
1426 } else {
1427 target_list = &target_proc->todo;
1428 target_wait = &target_proc->wait;
1429 }
1430 e->to_proc = target_proc->pid;
1431
1432 /* TODO: reuse incoming transaction for reply */
1433 t = kzalloc(sizeof(*t), GFP_KERNEL);
1434 if (t == NULL) {
1435 return_error = BR_FAILED_REPLY;
1436 goto err_alloc_t_failed;
1437 }
1438 binder_stats_created(BINDER_STAT_TRANSACTION);
1439
1440 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1441 if (tcomplete == NULL) {
1442 return_error = BR_FAILED_REPLY;
1443 goto err_alloc_tcomplete_failed;
1444 }
1445 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1446
1447 t->debug_id = ++binder_last_id;
1448 e->debug_id = t->debug_id;
1449
1450 if (reply)
1451 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001452 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001453 proc->pid, thread->pid, t->debug_id,
1454 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001455 (u64)tr->data.ptr.buffer,
1456 (u64)tr->data.ptr.offsets,
1457 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001458 else
1459 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001460 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001461 proc->pid, thread->pid, t->debug_id,
1462 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001463 (u64)tr->data.ptr.buffer,
1464 (u64)tr->data.ptr.offsets,
1465 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001466
1467 if (!reply && !(tr->flags & TF_ONE_WAY))
1468 t->from = thread;
1469 else
1470 t->from = NULL;
Jerry Snitselaarf994d832014-04-30 23:58:55 -07001471 t->sender_euid = cred->euid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001472 t->to_proc = target_proc;
1473 t->to_thread = target_thread;
1474 t->code = tr->code;
1475 t->flags = tr->flags;
1476 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001477
1478 trace_binder_transaction(reply, t, target_node);
1479
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001480 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1481 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1482 if (t->buffer == NULL) {
1483 return_error = BR_FAILED_REPLY;
1484 goto err_binder_alloc_buf_failed;
1485 }
1486 t->buffer->allow_user_free = 0;
1487 t->buffer->debug_id = t->debug_id;
1488 t->buffer->transaction = t;
1489 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001490 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001491 if (target_node)
1492 binder_inc_node(target_node, 1, 0, NULL);
1493
Arve Hjønnevågda498892014-02-21 14:40:26 -08001494 offp = (binder_size_t *)(t->buffer->data +
1495 ALIGN(tr->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001496
Arve Hjønnevågda498892014-02-21 14:40:26 -08001497 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1498 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301499 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1500 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001501 return_error = BR_FAILED_REPLY;
1502 goto err_copy_data_failed;
1503 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001504 if (copy_from_user(offp, (const void __user *)(uintptr_t)
1505 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301506 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1507 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001508 return_error = BR_FAILED_REPLY;
1509 goto err_copy_data_failed;
1510 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001511 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1512 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1513 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001514 return_error = BR_FAILED_REPLY;
1515 goto err_bad_offset;
1516 }
1517 off_end = (void *)offp + tr->offsets_size;
1518 for (; offp < off_end; offp++) {
1519 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001520
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001521 if (*offp > t->buffer->data_size - sizeof(*fp) ||
1522 t->buffer->data_size < sizeof(*fp) ||
Serban Constantinescuec35e852013-07-04 10:54:46 +01001523 !IS_ALIGNED(*offp, sizeof(u32))) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001524 binder_user_error("%d:%d got transaction with invalid offset, %lld\n",
1525 proc->pid, thread->pid, (u64)*offp);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001526 return_error = BR_FAILED_REPLY;
1527 goto err_bad_offset;
1528 }
1529 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
1530 switch (fp->type) {
1531 case BINDER_TYPE_BINDER:
1532 case BINDER_TYPE_WEAK_BINDER: {
1533 struct binder_ref *ref;
1534 struct binder_node *node = binder_get_node(proc, fp->binder);
Seunghun Lee10f62862014-05-01 01:30:23 +09001535
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001536 if (node == NULL) {
1537 node = binder_new_node(proc, fp->binder, fp->cookie);
1538 if (node == NULL) {
1539 return_error = BR_FAILED_REPLY;
1540 goto err_binder_new_node_failed;
1541 }
1542 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1543 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1544 }
1545 if (fp->cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001546 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001547 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001548 (u64)fp->binder, node->debug_id,
1549 (u64)fp->cookie, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001550 goto err_binder_get_ref_for_node_failed;
1551 }
1552 ref = binder_get_ref_for_node(target_proc, node);
1553 if (ref == NULL) {
1554 return_error = BR_FAILED_REPLY;
1555 goto err_binder_get_ref_for_node_failed;
1556 }
1557 if (fp->type == BINDER_TYPE_BINDER)
1558 fp->type = BINDER_TYPE_HANDLE;
1559 else
1560 fp->type = BINDER_TYPE_WEAK_HANDLE;
1561 fp->handle = ref->desc;
1562 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1563 &thread->todo);
1564
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001565 trace_binder_transaction_node_to_ref(t, node, ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001566 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001567 " node %d u%016llx -> ref %d desc %d\n",
1568 node->debug_id, (u64)node->ptr,
1569 ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001570 } break;
1571 case BINDER_TYPE_HANDLE:
1572 case BINDER_TYPE_WEAK_HANDLE: {
1573 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
Seunghun Lee10f62862014-05-01 01:30:23 +09001574
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001575 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001576 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301577 proc->pid,
1578 thread->pid, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001579 return_error = BR_FAILED_REPLY;
1580 goto err_binder_get_ref_failed;
1581 }
1582 if (ref->node->proc == target_proc) {
1583 if (fp->type == BINDER_TYPE_HANDLE)
1584 fp->type = BINDER_TYPE_BINDER;
1585 else
1586 fp->type = BINDER_TYPE_WEAK_BINDER;
1587 fp->binder = ref->node->ptr;
1588 fp->cookie = ref->node->cookie;
1589 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001590 trace_binder_transaction_ref_to_node(t, ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001591 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001592 " ref %d desc %d -> node %d u%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001593 ref->debug_id, ref->desc, ref->node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001594 (u64)ref->node->ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001595 } else {
1596 struct binder_ref *new_ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001597
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001598 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1599 if (new_ref == NULL) {
1600 return_error = BR_FAILED_REPLY;
1601 goto err_binder_get_ref_for_node_failed;
1602 }
1603 fp->handle = new_ref->desc;
1604 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001605 trace_binder_transaction_ref_to_ref(t, ref,
1606 new_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001607 binder_debug(BINDER_DEBUG_TRANSACTION,
1608 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1609 ref->debug_id, ref->desc, new_ref->debug_id,
1610 new_ref->desc, ref->node->debug_id);
1611 }
1612 } break;
1613
1614 case BINDER_TYPE_FD: {
1615 int target_fd;
1616 struct file *file;
1617
1618 if (reply) {
1619 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001620 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 +09001621 proc->pid, thread->pid, fp->handle);
1622 return_error = BR_FAILED_REPLY;
1623 goto err_fd_not_allowed;
1624 }
1625 } else if (!target_node->accept_fds) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001626 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 +09001627 proc->pid, thread->pid, fp->handle);
1628 return_error = BR_FAILED_REPLY;
1629 goto err_fd_not_allowed;
1630 }
1631
1632 file = fget(fp->handle);
1633 if (file == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001634 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001635 proc->pid, thread->pid, fp->handle);
1636 return_error = BR_FAILED_REPLY;
1637 goto err_fget_failed;
1638 }
1639 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1640 if (target_fd < 0) {
1641 fput(file);
1642 return_error = BR_FAILED_REPLY;
1643 goto err_get_unused_fd_failed;
1644 }
1645 task_fd_install(target_proc, target_fd, file);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001646 trace_binder_transaction_fd(t, fp->handle, target_fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001647 binder_debug(BINDER_DEBUG_TRANSACTION,
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001648 " fd %d -> %d\n", fp->handle, target_fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001649 /* TODO: fput? */
1650 fp->handle = target_fd;
1651 } break;
1652
1653 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001654 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001655 proc->pid, thread->pid, fp->type);
1656 return_error = BR_FAILED_REPLY;
1657 goto err_bad_object_type;
1658 }
1659 }
1660 if (reply) {
1661 BUG_ON(t->buffer->async_transaction != 0);
1662 binder_pop_transaction(target_thread, in_reply_to);
1663 } else if (!(t->flags & TF_ONE_WAY)) {
1664 BUG_ON(t->buffer->async_transaction != 0);
1665 t->need_reply = 1;
1666 t->from_parent = thread->transaction_stack;
1667 thread->transaction_stack = t;
1668 } else {
1669 BUG_ON(target_node == NULL);
1670 BUG_ON(t->buffer->async_transaction != 1);
1671 if (target_node->has_async_transaction) {
1672 target_list = &target_node->async_todo;
1673 target_wait = NULL;
1674 } else
1675 target_node->has_async_transaction = 1;
1676 }
1677 t->work.type = BINDER_WORK_TRANSACTION;
1678 list_add_tail(&t->work.entry, target_list);
1679 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1680 list_add_tail(&tcomplete->entry, &thread->todo);
1681 if (target_wait)
1682 wake_up_interruptible(target_wait);
1683 return;
1684
1685err_get_unused_fd_failed:
1686err_fget_failed:
1687err_fd_not_allowed:
1688err_binder_get_ref_for_node_failed:
1689err_binder_get_ref_failed:
1690err_binder_new_node_failed:
1691err_bad_object_type:
1692err_bad_offset:
1693err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001694 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001695 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1696 t->buffer->transaction = NULL;
1697 binder_free_buf(target_proc, t->buffer);
1698err_binder_alloc_buf_failed:
1699 kfree(tcomplete);
1700 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1701err_alloc_tcomplete_failed:
1702 kfree(t);
1703 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1704err_alloc_t_failed:
1705err_bad_call_stack:
1706err_empty_call_stack:
1707err_dead_binder:
1708err_invalid_target_handle:
1709err_no_context_mgr_node:
1710 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001711 "%d:%d transaction failed %d, size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001712 proc->pid, thread->pid, return_error,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001713 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001714
1715 {
1716 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09001717
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001718 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1719 *fe = *e;
1720 }
1721
1722 BUG_ON(thread->return_error != BR_OK);
1723 if (in_reply_to) {
1724 thread->return_error = BR_TRANSACTION_COMPLETE;
1725 binder_send_failed_reply(in_reply_to, return_error);
1726 } else
1727 thread->return_error = return_error;
1728}
1729
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02001730static int binder_thread_write(struct binder_proc *proc,
1731 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001732 binder_uintptr_t binder_buffer, size_t size,
1733 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001734{
1735 uint32_t cmd;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001736 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001737 void __user *ptr = buffer + *consumed;
1738 void __user *end = buffer + size;
1739
1740 while (ptr < end && thread->return_error == BR_OK) {
1741 if (get_user(cmd, (uint32_t __user *)ptr))
1742 return -EFAULT;
1743 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001744 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001745 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1746 binder_stats.bc[_IOC_NR(cmd)]++;
1747 proc->stats.bc[_IOC_NR(cmd)]++;
1748 thread->stats.bc[_IOC_NR(cmd)]++;
1749 }
1750 switch (cmd) {
1751 case BC_INCREFS:
1752 case BC_ACQUIRE:
1753 case BC_RELEASE:
1754 case BC_DECREFS: {
1755 uint32_t target;
1756 struct binder_ref *ref;
1757 const char *debug_string;
1758
1759 if (get_user(target, (uint32_t __user *)ptr))
1760 return -EFAULT;
1761 ptr += sizeof(uint32_t);
1762 if (target == 0 && binder_context_mgr_node &&
1763 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1764 ref = binder_get_ref_for_node(proc,
1765 binder_context_mgr_node);
1766 if (ref->desc != target) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301767 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001768 proc->pid, thread->pid,
1769 ref->desc);
1770 }
1771 } else
1772 ref = binder_get_ref(proc, target);
1773 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301774 binder_user_error("%d:%d refcount change on invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001775 proc->pid, thread->pid, target);
1776 break;
1777 }
1778 switch (cmd) {
1779 case BC_INCREFS:
1780 debug_string = "IncRefs";
1781 binder_inc_ref(ref, 0, NULL);
1782 break;
1783 case BC_ACQUIRE:
1784 debug_string = "Acquire";
1785 binder_inc_ref(ref, 1, NULL);
1786 break;
1787 case BC_RELEASE:
1788 debug_string = "Release";
1789 binder_dec_ref(ref, 1);
1790 break;
1791 case BC_DECREFS:
1792 default:
1793 debug_string = "DecRefs";
1794 binder_dec_ref(ref, 0);
1795 break;
1796 }
1797 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301798 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001799 proc->pid, thread->pid, debug_string, ref->debug_id,
1800 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1801 break;
1802 }
1803 case BC_INCREFS_DONE:
1804 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001805 binder_uintptr_t node_ptr;
1806 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001807 struct binder_node *node;
1808
Arve Hjønnevågda498892014-02-21 14:40:26 -08001809 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001810 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001811 ptr += sizeof(binder_uintptr_t);
1812 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001813 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001814 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001815 node = binder_get_node(proc, node_ptr);
1816 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001817 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001818 proc->pid, thread->pid,
1819 cmd == BC_INCREFS_DONE ?
1820 "BC_INCREFS_DONE" :
1821 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001822 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001823 break;
1824 }
1825 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001826 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001827 proc->pid, thread->pid,
1828 cmd == BC_INCREFS_DONE ?
1829 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001830 (u64)node_ptr, node->debug_id,
1831 (u64)cookie, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001832 break;
1833 }
1834 if (cmd == BC_ACQUIRE_DONE) {
1835 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301836 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001837 proc->pid, thread->pid,
1838 node->debug_id);
1839 break;
1840 }
1841 node->pending_strong_ref = 0;
1842 } else {
1843 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301844 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001845 proc->pid, thread->pid,
1846 node->debug_id);
1847 break;
1848 }
1849 node->pending_weak_ref = 0;
1850 }
1851 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1852 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301853 "%d:%d %s node %d ls %d lw %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001854 proc->pid, thread->pid,
1855 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1856 node->debug_id, node->local_strong_refs, node->local_weak_refs);
1857 break;
1858 }
1859 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05301860 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001861 return -EINVAL;
1862 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05301863 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001864 return -EINVAL;
1865
1866 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001867 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001868 struct binder_buffer *buffer;
1869
Arve Hjønnevågda498892014-02-21 14:40:26 -08001870 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001871 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001872 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001873
1874 buffer = binder_buffer_lookup(proc, data_ptr);
1875 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001876 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
1877 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001878 break;
1879 }
1880 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001881 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
1882 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001883 break;
1884 }
1885 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001886 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
1887 proc->pid, thread->pid, (u64)data_ptr,
1888 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001889 buffer->transaction ? "active" : "finished");
1890
1891 if (buffer->transaction) {
1892 buffer->transaction->buffer = NULL;
1893 buffer->transaction = NULL;
1894 }
1895 if (buffer->async_transaction && buffer->target_node) {
1896 BUG_ON(!buffer->target_node->has_async_transaction);
1897 if (list_empty(&buffer->target_node->async_todo))
1898 buffer->target_node->has_async_transaction = 0;
1899 else
1900 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1901 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001902 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001903 binder_transaction_buffer_release(proc, buffer, NULL);
1904 binder_free_buf(proc, buffer);
1905 break;
1906 }
1907
1908 case BC_TRANSACTION:
1909 case BC_REPLY: {
1910 struct binder_transaction_data tr;
1911
1912 if (copy_from_user(&tr, ptr, sizeof(tr)))
1913 return -EFAULT;
1914 ptr += sizeof(tr);
1915 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1916 break;
1917 }
1918
1919 case BC_REGISTER_LOOPER:
1920 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301921 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001922 proc->pid, thread->pid);
1923 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1924 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301925 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001926 proc->pid, thread->pid);
1927 } else if (proc->requested_threads == 0) {
1928 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301929 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001930 proc->pid, thread->pid);
1931 } else {
1932 proc->requested_threads--;
1933 proc->requested_threads_started++;
1934 }
1935 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1936 break;
1937 case BC_ENTER_LOOPER:
1938 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301939 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001940 proc->pid, thread->pid);
1941 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
1942 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301943 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001944 proc->pid, thread->pid);
1945 }
1946 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
1947 break;
1948 case BC_EXIT_LOOPER:
1949 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301950 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001951 proc->pid, thread->pid);
1952 thread->looper |= BINDER_LOOPER_STATE_EXITED;
1953 break;
1954
1955 case BC_REQUEST_DEATH_NOTIFICATION:
1956 case BC_CLEAR_DEATH_NOTIFICATION: {
1957 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001958 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001959 struct binder_ref *ref;
1960 struct binder_ref_death *death;
1961
1962 if (get_user(target, (uint32_t __user *)ptr))
1963 return -EFAULT;
1964 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08001965 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001966 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001967 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001968 ref = binder_get_ref(proc, target);
1969 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301970 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001971 proc->pid, thread->pid,
1972 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1973 "BC_REQUEST_DEATH_NOTIFICATION" :
1974 "BC_CLEAR_DEATH_NOTIFICATION",
1975 target);
1976 break;
1977 }
1978
1979 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001980 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001981 proc->pid, thread->pid,
1982 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
1983 "BC_REQUEST_DEATH_NOTIFICATION" :
1984 "BC_CLEAR_DEATH_NOTIFICATION",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001985 (u64)cookie, ref->debug_id, ref->desc,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001986 ref->strong, ref->weak, ref->node->debug_id);
1987
1988 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
1989 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301990 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001991 proc->pid, thread->pid);
1992 break;
1993 }
1994 death = kzalloc(sizeof(*death), GFP_KERNEL);
1995 if (death == NULL) {
1996 thread->return_error = BR_ERROR;
1997 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301998 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001999 proc->pid, thread->pid);
2000 break;
2001 }
2002 binder_stats_created(BINDER_STAT_DEATH);
2003 INIT_LIST_HEAD(&death->work.entry);
2004 death->cookie = cookie;
2005 ref->death = death;
2006 if (ref->node->proc == NULL) {
2007 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2008 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2009 list_add_tail(&ref->death->work.entry, &thread->todo);
2010 } else {
2011 list_add_tail(&ref->death->work.entry, &proc->todo);
2012 wake_up_interruptible(&proc->wait);
2013 }
2014 }
2015 } else {
2016 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302017 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002018 proc->pid, thread->pid);
2019 break;
2020 }
2021 death = ref->death;
2022 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002023 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002024 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002025 (u64)death->cookie,
2026 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002027 break;
2028 }
2029 ref->death = NULL;
2030 if (list_empty(&death->work.entry)) {
2031 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2032 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2033 list_add_tail(&death->work.entry, &thread->todo);
2034 } else {
2035 list_add_tail(&death->work.entry, &proc->todo);
2036 wake_up_interruptible(&proc->wait);
2037 }
2038 } else {
2039 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2040 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2041 }
2042 }
2043 } break;
2044 case BC_DEAD_BINDER_DONE: {
2045 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002046 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002047 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09002048
Arve Hjønnevågda498892014-02-21 14:40:26 -08002049 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002050 return -EFAULT;
2051
2052 ptr += sizeof(void *);
2053 list_for_each_entry(w, &proc->delivered_death, entry) {
2054 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
Seunghun Lee10f62862014-05-01 01:30:23 +09002055
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002056 if (tmp_death->cookie == cookie) {
2057 death = tmp_death;
2058 break;
2059 }
2060 }
2061 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002062 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2063 proc->pid, thread->pid, (u64)cookie,
2064 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002065 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002066 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2067 proc->pid, thread->pid, (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002068 break;
2069 }
2070
2071 list_del_init(&death->work.entry);
2072 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2073 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2074 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2075 list_add_tail(&death->work.entry, &thread->todo);
2076 } else {
2077 list_add_tail(&death->work.entry, &proc->todo);
2078 wake_up_interruptible(&proc->wait);
2079 }
2080 }
2081 } break;
2082
2083 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302084 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002085 proc->pid, thread->pid, cmd);
2086 return -EINVAL;
2087 }
2088 *consumed = ptr - buffer;
2089 }
2090 return 0;
2091}
2092
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002093static void binder_stat_br(struct binder_proc *proc,
2094 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002095{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002096 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002097 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2098 binder_stats.br[_IOC_NR(cmd)]++;
2099 proc->stats.br[_IOC_NR(cmd)]++;
2100 thread->stats.br[_IOC_NR(cmd)]++;
2101 }
2102}
2103
2104static int binder_has_proc_work(struct binder_proc *proc,
2105 struct binder_thread *thread)
2106{
2107 return !list_empty(&proc->todo) ||
2108 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2109}
2110
2111static int binder_has_thread_work(struct binder_thread *thread)
2112{
2113 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2114 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2115}
2116
2117static int binder_thread_read(struct binder_proc *proc,
2118 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002119 binder_uintptr_t binder_buffer, size_t size,
2120 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002121{
Arve Hjønnevågda498892014-02-21 14:40:26 -08002122 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002123 void __user *ptr = buffer + *consumed;
2124 void __user *end = buffer + size;
2125
2126 int ret = 0;
2127 int wait_for_proc_work;
2128
2129 if (*consumed == 0) {
2130 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2131 return -EFAULT;
2132 ptr += sizeof(uint32_t);
2133 }
2134
2135retry:
2136 wait_for_proc_work = thread->transaction_stack == NULL &&
2137 list_empty(&thread->todo);
2138
2139 if (thread->return_error != BR_OK && ptr < end) {
2140 if (thread->return_error2 != BR_OK) {
2141 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2142 return -EFAULT;
2143 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002144 binder_stat_br(proc, thread, thread->return_error2);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002145 if (ptr == end)
2146 goto done;
2147 thread->return_error2 = BR_OK;
2148 }
2149 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2150 return -EFAULT;
2151 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002152 binder_stat_br(proc, thread, thread->return_error);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002153 thread->return_error = BR_OK;
2154 goto done;
2155 }
2156
2157
2158 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2159 if (wait_for_proc_work)
2160 proc->ready_threads++;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002161
2162 binder_unlock(__func__);
2163
2164 trace_binder_wait_for_work(wait_for_proc_work,
2165 !!thread->transaction_stack,
2166 !list_empty(&thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002167 if (wait_for_proc_work) {
2168 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2169 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302170 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 +09002171 proc->pid, thread->pid, thread->looper);
2172 wait_event_interruptible(binder_user_error_wait,
2173 binder_stop_on_user_error < 2);
2174 }
2175 binder_set_nice(proc->default_priority);
2176 if (non_block) {
2177 if (!binder_has_proc_work(proc, thread))
2178 ret = -EAGAIN;
2179 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002180 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002181 } else {
2182 if (non_block) {
2183 if (!binder_has_thread_work(thread))
2184 ret = -EAGAIN;
2185 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002186 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002187 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002188
2189 binder_lock(__func__);
2190
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002191 if (wait_for_proc_work)
2192 proc->ready_threads--;
2193 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2194
2195 if (ret)
2196 return ret;
2197
2198 while (1) {
2199 uint32_t cmd;
2200 struct binder_transaction_data tr;
2201 struct binder_work *w;
2202 struct binder_transaction *t = NULL;
2203
2204 if (!list_empty(&thread->todo))
2205 w = list_first_entry(&thread->todo, struct binder_work, entry);
2206 else if (!list_empty(&proc->todo) && wait_for_proc_work)
2207 w = list_first_entry(&proc->todo, struct binder_work, entry);
2208 else {
2209 if (ptr - buffer == 4 && !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) /* no data added */
2210 goto retry;
2211 break;
2212 }
2213
2214 if (end - ptr < sizeof(tr) + 4)
2215 break;
2216
2217 switch (w->type) {
2218 case BINDER_WORK_TRANSACTION: {
2219 t = container_of(w, struct binder_transaction, work);
2220 } break;
2221 case BINDER_WORK_TRANSACTION_COMPLETE: {
2222 cmd = BR_TRANSACTION_COMPLETE;
2223 if (put_user(cmd, (uint32_t __user *)ptr))
2224 return -EFAULT;
2225 ptr += sizeof(uint32_t);
2226
2227 binder_stat_br(proc, thread, cmd);
2228 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302229 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002230 proc->pid, thread->pid);
2231
2232 list_del(&w->entry);
2233 kfree(w);
2234 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2235 } break;
2236 case BINDER_WORK_NODE: {
2237 struct binder_node *node = container_of(w, struct binder_node, work);
2238 uint32_t cmd = BR_NOOP;
2239 const char *cmd_name;
2240 int strong = node->internal_strong_refs || node->local_strong_refs;
2241 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
Seunghun Lee10f62862014-05-01 01:30:23 +09002242
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002243 if (weak && !node->has_weak_ref) {
2244 cmd = BR_INCREFS;
2245 cmd_name = "BR_INCREFS";
2246 node->has_weak_ref = 1;
2247 node->pending_weak_ref = 1;
2248 node->local_weak_refs++;
2249 } else if (strong && !node->has_strong_ref) {
2250 cmd = BR_ACQUIRE;
2251 cmd_name = "BR_ACQUIRE";
2252 node->has_strong_ref = 1;
2253 node->pending_strong_ref = 1;
2254 node->local_strong_refs++;
2255 } else if (!strong && node->has_strong_ref) {
2256 cmd = BR_RELEASE;
2257 cmd_name = "BR_RELEASE";
2258 node->has_strong_ref = 0;
2259 } else if (!weak && node->has_weak_ref) {
2260 cmd = BR_DECREFS;
2261 cmd_name = "BR_DECREFS";
2262 node->has_weak_ref = 0;
2263 }
2264 if (cmd != BR_NOOP) {
2265 if (put_user(cmd, (uint32_t __user *)ptr))
2266 return -EFAULT;
2267 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002268 if (put_user(node->ptr,
2269 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002270 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002271 ptr += sizeof(binder_uintptr_t);
2272 if (put_user(node->cookie,
2273 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002274 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002275 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002276
2277 binder_stat_br(proc, thread, cmd);
2278 binder_debug(BINDER_DEBUG_USER_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002279 "%d:%d %s %d u%016llx c%016llx\n",
2280 proc->pid, thread->pid, cmd_name,
2281 node->debug_id,
2282 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002283 } else {
2284 list_del_init(&w->entry);
2285 if (!weak && !strong) {
2286 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002287 "%d:%d node %d u%016llx c%016llx deleted\n",
2288 proc->pid, thread->pid,
2289 node->debug_id,
2290 (u64)node->ptr,
2291 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002292 rb_erase(&node->rb_node, &proc->nodes);
2293 kfree(node);
2294 binder_stats_deleted(BINDER_STAT_NODE);
2295 } else {
2296 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002297 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2298 proc->pid, thread->pid,
2299 node->debug_id,
2300 (u64)node->ptr,
2301 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002302 }
2303 }
2304 } break;
2305 case BINDER_WORK_DEAD_BINDER:
2306 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2307 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2308 struct binder_ref_death *death;
2309 uint32_t cmd;
2310
2311 death = container_of(w, struct binder_ref_death, work);
2312 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2313 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2314 else
2315 cmd = BR_DEAD_BINDER;
2316 if (put_user(cmd, (uint32_t __user *)ptr))
2317 return -EFAULT;
2318 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002319 if (put_user(death->cookie,
2320 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002321 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002322 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002323 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002324 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002325 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002326 proc->pid, thread->pid,
2327 cmd == BR_DEAD_BINDER ?
2328 "BR_DEAD_BINDER" :
2329 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002330 (u64)death->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002331
2332 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2333 list_del(&w->entry);
2334 kfree(death);
2335 binder_stats_deleted(BINDER_STAT_DEATH);
2336 } else
2337 list_move(&w->entry, &proc->delivered_death);
2338 if (cmd == BR_DEAD_BINDER)
2339 goto done; /* DEAD_BINDER notifications can cause transactions */
2340 } break;
2341 }
2342
2343 if (!t)
2344 continue;
2345
2346 BUG_ON(t->buffer == NULL);
2347 if (t->buffer->target_node) {
2348 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002349
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002350 tr.target.ptr = target_node->ptr;
2351 tr.cookie = target_node->cookie;
2352 t->saved_priority = task_nice(current);
2353 if (t->priority < target_node->min_priority &&
2354 !(t->flags & TF_ONE_WAY))
2355 binder_set_nice(t->priority);
2356 else if (!(t->flags & TF_ONE_WAY) ||
2357 t->saved_priority > target_node->min_priority)
2358 binder_set_nice(target_node->min_priority);
2359 cmd = BR_TRANSACTION;
2360 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002361 tr.target.ptr = 0;
2362 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002363 cmd = BR_REPLY;
2364 }
2365 tr.code = t->code;
2366 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002367 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002368
2369 if (t->from) {
2370 struct task_struct *sender = t->from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09002371
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002372 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002373 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002374 } else {
2375 tr.sender_pid = 0;
2376 }
2377
2378 tr.data_size = t->buffer->data_size;
2379 tr.offsets_size = t->buffer->offsets_size;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002380 tr.data.ptr.buffer = (binder_uintptr_t)(
2381 (uintptr_t)t->buffer->data +
2382 proc->user_buffer_offset);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002383 tr.data.ptr.offsets = tr.data.ptr.buffer +
2384 ALIGN(t->buffer->data_size,
2385 sizeof(void *));
2386
2387 if (put_user(cmd, (uint32_t __user *)ptr))
2388 return -EFAULT;
2389 ptr += sizeof(uint32_t);
2390 if (copy_to_user(ptr, &tr, sizeof(tr)))
2391 return -EFAULT;
2392 ptr += sizeof(tr);
2393
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002394 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002395 binder_stat_br(proc, thread, cmd);
2396 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002397 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002398 proc->pid, thread->pid,
2399 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2400 "BR_REPLY",
2401 t->debug_id, t->from ? t->from->proc->pid : 0,
2402 t->from ? t->from->pid : 0, cmd,
2403 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002404 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002405
2406 list_del(&t->work.entry);
2407 t->buffer->allow_user_free = 1;
2408 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2409 t->to_parent = thread->transaction_stack;
2410 t->to_thread = thread;
2411 thread->transaction_stack = t;
2412 } else {
2413 t->buffer->transaction = NULL;
2414 kfree(t);
2415 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2416 }
2417 break;
2418 }
2419
2420done:
2421
2422 *consumed = ptr - buffer;
2423 if (proc->requested_threads + proc->ready_threads == 0 &&
2424 proc->requested_threads_started < proc->max_threads &&
2425 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2426 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2427 /*spawn a new thread if we leave this out */) {
2428 proc->requested_threads++;
2429 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302430 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002431 proc->pid, thread->pid);
2432 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2433 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002434 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002435 }
2436 return 0;
2437}
2438
2439static void binder_release_work(struct list_head *list)
2440{
2441 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09002442
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002443 while (!list_empty(list)) {
2444 w = list_first_entry(list, struct binder_work, entry);
2445 list_del_init(&w->entry);
2446 switch (w->type) {
2447 case BINDER_WORK_TRANSACTION: {
2448 struct binder_transaction *t;
2449
2450 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002451 if (t->buffer->target_node &&
2452 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002453 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002454 } else {
2455 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302456 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002457 t->debug_id);
2458 t->buffer->transaction = NULL;
2459 kfree(t);
2460 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2461 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002462 } break;
2463 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002464 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302465 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002466 kfree(w);
2467 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2468 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002469 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2470 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2471 struct binder_ref_death *death;
2472
2473 death = container_of(w, struct binder_ref_death, work);
2474 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002475 "undelivered death notification, %016llx\n",
2476 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002477 kfree(death);
2478 binder_stats_deleted(BINDER_STAT_DEATH);
2479 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002480 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302481 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002482 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002483 break;
2484 }
2485 }
2486
2487}
2488
2489static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2490{
2491 struct binder_thread *thread = NULL;
2492 struct rb_node *parent = NULL;
2493 struct rb_node **p = &proc->threads.rb_node;
2494
2495 while (*p) {
2496 parent = *p;
2497 thread = rb_entry(parent, struct binder_thread, rb_node);
2498
2499 if (current->pid < thread->pid)
2500 p = &(*p)->rb_left;
2501 else if (current->pid > thread->pid)
2502 p = &(*p)->rb_right;
2503 else
2504 break;
2505 }
2506 if (*p == NULL) {
2507 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2508 if (thread == NULL)
2509 return NULL;
2510 binder_stats_created(BINDER_STAT_THREAD);
2511 thread->proc = proc;
2512 thread->pid = current->pid;
2513 init_waitqueue_head(&thread->wait);
2514 INIT_LIST_HEAD(&thread->todo);
2515 rb_link_node(&thread->rb_node, parent, p);
2516 rb_insert_color(&thread->rb_node, &proc->threads);
2517 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2518 thread->return_error = BR_OK;
2519 thread->return_error2 = BR_OK;
2520 }
2521 return thread;
2522}
2523
2524static int binder_free_thread(struct binder_proc *proc,
2525 struct binder_thread *thread)
2526{
2527 struct binder_transaction *t;
2528 struct binder_transaction *send_reply = NULL;
2529 int active_transactions = 0;
2530
2531 rb_erase(&thread->rb_node, &proc->threads);
2532 t = thread->transaction_stack;
2533 if (t && t->to_thread == thread)
2534 send_reply = t;
2535 while (t) {
2536 active_transactions++;
2537 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302538 "release %d:%d transaction %d %s, still active\n",
2539 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002540 t->debug_id,
2541 (t->to_thread == thread) ? "in" : "out");
2542
2543 if (t->to_thread == thread) {
2544 t->to_proc = NULL;
2545 t->to_thread = NULL;
2546 if (t->buffer) {
2547 t->buffer->transaction = NULL;
2548 t->buffer = NULL;
2549 }
2550 t = t->to_parent;
2551 } else if (t->from == thread) {
2552 t->from = NULL;
2553 t = t->from_parent;
2554 } else
2555 BUG();
2556 }
2557 if (send_reply)
2558 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2559 binder_release_work(&thread->todo);
2560 kfree(thread);
2561 binder_stats_deleted(BINDER_STAT_THREAD);
2562 return active_transactions;
2563}
2564
2565static unsigned int binder_poll(struct file *filp,
2566 struct poll_table_struct *wait)
2567{
2568 struct binder_proc *proc = filp->private_data;
2569 struct binder_thread *thread = NULL;
2570 int wait_for_proc_work;
2571
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002572 binder_lock(__func__);
2573
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002574 thread = binder_get_thread(proc);
2575
2576 wait_for_proc_work = thread->transaction_stack == NULL &&
2577 list_empty(&thread->todo) && thread->return_error == BR_OK;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002578
2579 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002580
2581 if (wait_for_proc_work) {
2582 if (binder_has_proc_work(proc, thread))
2583 return POLLIN;
2584 poll_wait(filp, &proc->wait, wait);
2585 if (binder_has_proc_work(proc, thread))
2586 return POLLIN;
2587 } else {
2588 if (binder_has_thread_work(thread))
2589 return POLLIN;
2590 poll_wait(filp, &thread->wait, wait);
2591 if (binder_has_thread_work(thread))
2592 return POLLIN;
2593 }
2594 return 0;
2595}
2596
2597static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2598{
2599 int ret;
2600 struct binder_proc *proc = filp->private_data;
2601 struct binder_thread *thread;
2602 unsigned int size = _IOC_SIZE(cmd);
2603 void __user *ubuf = (void __user *)arg;
Jerry Snitselaarf994d832014-04-30 23:58:55 -07002604 const struct cred *cred = current_cred();
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002605
Sherwin Soltani258767f2012-06-26 02:00:30 -04002606 /*pr_info("binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002607
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002608 trace_binder_ioctl(cmd, arg);
2609
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002610 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2611 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002612 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002613
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002614 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002615 thread = binder_get_thread(proc);
2616 if (thread == NULL) {
2617 ret = -ENOMEM;
2618 goto err;
2619 }
2620
2621 switch (cmd) {
2622 case BINDER_WRITE_READ: {
2623 struct binder_write_read bwr;
Seunghun Lee10f62862014-05-01 01:30:23 +09002624
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002625 if (size != sizeof(struct binder_write_read)) {
2626 ret = -EINVAL;
2627 goto err;
2628 }
2629 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2630 ret = -EFAULT;
2631 goto err;
2632 }
2633 binder_debug(BINDER_DEBUG_READ_WRITE,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002634 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2635 proc->pid, thread->pid,
2636 (u64)bwr.write_size, (u64)bwr.write_buffer,
2637 (u64)bwr.read_size, (u64)bwr.read_buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002638
2639 if (bwr.write_size > 0) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002640 ret = binder_thread_write(proc, thread,
2641 bwr.write_buffer,
2642 bwr.write_size,
2643 &bwr.write_consumed);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002644 trace_binder_write_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002645 if (ret < 0) {
2646 bwr.read_consumed = 0;
2647 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2648 ret = -EFAULT;
2649 goto err;
2650 }
2651 }
2652 if (bwr.read_size > 0) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002653 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2654 bwr.read_size,
2655 &bwr.read_consumed,
2656 filp->f_flags & O_NONBLOCK);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002657 trace_binder_read_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002658 if (!list_empty(&proc->todo))
2659 wake_up_interruptible(&proc->wait);
2660 if (ret < 0) {
2661 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2662 ret = -EFAULT;
2663 goto err;
2664 }
2665 }
2666 binder_debug(BINDER_DEBUG_READ_WRITE,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002667 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2668 proc->pid, thread->pid,
2669 (u64)bwr.write_consumed, (u64)bwr.write_size,
2670 (u64)bwr.read_consumed, (u64)bwr.read_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002671 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2672 ret = -EFAULT;
2673 goto err;
2674 }
2675 break;
2676 }
2677 case BINDER_SET_MAX_THREADS:
2678 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2679 ret = -EINVAL;
2680 goto err;
2681 }
2682 break;
2683 case BINDER_SET_CONTEXT_MGR:
2684 if (binder_context_mgr_node != NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302685 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002686 ret = -EBUSY;
2687 goto err;
2688 }
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002689 if (uid_valid(binder_context_mgr_uid)) {
Jerry Snitselaarf994d832014-04-30 23:58:55 -07002690 if (!uid_eq(binder_context_mgr_uid, cred->euid)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302691 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
Jerry Snitselaarf994d832014-04-30 23:58:55 -07002692 from_kuid(&init_user_ns, cred->euid),
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002693 from_kuid(&init_user_ns, binder_context_mgr_uid));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002694 ret = -EPERM;
2695 goto err;
2696 }
2697 } else
Jerry Snitselaarf994d832014-04-30 23:58:55 -07002698 binder_context_mgr_uid = cred->euid;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002699 binder_context_mgr_node = binder_new_node(proc, 0, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002700 if (binder_context_mgr_node == NULL) {
2701 ret = -ENOMEM;
2702 goto err;
2703 }
2704 binder_context_mgr_node->local_weak_refs++;
2705 binder_context_mgr_node->local_strong_refs++;
2706 binder_context_mgr_node->has_strong_ref = 1;
2707 binder_context_mgr_node->has_weak_ref = 1;
2708 break;
2709 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302710 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002711 proc->pid, thread->pid);
2712 binder_free_thread(proc, thread);
2713 thread = NULL;
2714 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002715 case BINDER_VERSION: {
2716 struct binder_version __user *ver = ubuf;
2717
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002718 if (size != sizeof(struct binder_version)) {
2719 ret = -EINVAL;
2720 goto err;
2721 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02002722 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2723 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002724 ret = -EINVAL;
2725 goto err;
2726 }
2727 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002728 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002729 default:
2730 ret = -EINVAL;
2731 goto err;
2732 }
2733 ret = 0;
2734err:
2735 if (thread)
2736 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002737 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002738 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2739 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05302740 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 -07002741err_unlocked:
2742 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002743 return ret;
2744}
2745
2746static void binder_vma_open(struct vm_area_struct *vma)
2747{
2748 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002749
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002750 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302751 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002752 proc->pid, vma->vm_start, vma->vm_end,
2753 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2754 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002755}
2756
2757static void binder_vma_close(struct vm_area_struct *vma)
2758{
2759 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002760
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002761 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302762 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002763 proc->pid, vma->vm_start, vma->vm_end,
2764 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2765 (unsigned long)pgprot_val(vma->vm_page_prot));
2766 proc->vma = NULL;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08002767 proc->vma_vm_mm = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002768 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2769}
2770
2771static struct vm_operations_struct binder_vm_ops = {
2772 .open = binder_vma_open,
2773 .close = binder_vma_close,
2774};
2775
2776static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2777{
2778 int ret;
2779 struct vm_struct *area;
2780 struct binder_proc *proc = filp->private_data;
2781 const char *failure_string;
2782 struct binder_buffer *buffer;
2783
Al Viroa79f41e2012-08-15 18:23:36 -04002784 if (proc->tsk != current)
2785 return -EINVAL;
2786
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002787 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2788 vma->vm_end = vma->vm_start + SZ_4M;
2789
2790 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2791 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2792 proc->pid, vma->vm_start, vma->vm_end,
2793 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2794 (unsigned long)pgprot_val(vma->vm_page_prot));
2795
2796 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2797 ret = -EPERM;
2798 failure_string = "bad vm_flags";
2799 goto err_bad_arg;
2800 }
2801 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2802
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002803 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002804 if (proc->buffer) {
2805 ret = -EBUSY;
2806 failure_string = "already mapped";
2807 goto err_already_mapped;
2808 }
2809
2810 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2811 if (area == NULL) {
2812 ret = -ENOMEM;
2813 failure_string = "get_vm_area";
2814 goto err_get_vm_area_failed;
2815 }
2816 proc->buffer = area->addr;
2817 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002818 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002819
2820#ifdef CONFIG_CPU_CACHE_VIPT
2821 if (cache_is_vipt_aliasing()) {
2822 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
Sherwin Soltani258767f2012-06-26 02:00:30 -04002823 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 +09002824 vma->vm_start += PAGE_SIZE;
2825 }
2826 }
2827#endif
2828 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2829 if (proc->pages == NULL) {
2830 ret = -ENOMEM;
2831 failure_string = "alloc page array";
2832 goto err_alloc_pages_failed;
2833 }
2834 proc->buffer_size = vma->vm_end - vma->vm_start;
2835
2836 vma->vm_ops = &binder_vm_ops;
2837 vma->vm_private_data = proc;
2838
2839 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2840 ret = -ENOMEM;
2841 failure_string = "alloc small buf";
2842 goto err_alloc_small_buf_failed;
2843 }
2844 buffer = proc->buffer;
2845 INIT_LIST_HEAD(&proc->buffers);
2846 list_add(&buffer->entry, &proc->buffers);
2847 buffer->free = 1;
2848 binder_insert_free_buffer(proc, buffer);
2849 proc->free_async_space = proc->buffer_size / 2;
2850 barrier();
Al Viroa79f41e2012-08-15 18:23:36 -04002851 proc->files = get_files_struct(current);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002852 proc->vma = vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08002853 proc->vma_vm_mm = vma->vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002854
Sherwin Soltani258767f2012-06-26 02:00:30 -04002855 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002856 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2857 return 0;
2858
2859err_alloc_small_buf_failed:
2860 kfree(proc->pages);
2861 proc->pages = NULL;
2862err_alloc_pages_failed:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002863 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002864 vfree(proc->buffer);
2865 proc->buffer = NULL;
2866err_get_vm_area_failed:
2867err_already_mapped:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002868 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002869err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04002870 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002871 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2872 return ret;
2873}
2874
2875static int binder_open(struct inode *nodp, struct file *filp)
2876{
2877 struct binder_proc *proc;
2878
2879 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
2880 current->group_leader->pid, current->pid);
2881
2882 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2883 if (proc == NULL)
2884 return -ENOMEM;
2885 get_task_struct(current);
2886 proc->tsk = current;
2887 INIT_LIST_HEAD(&proc->todo);
2888 init_waitqueue_head(&proc->wait);
2889 proc->default_priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002890
2891 binder_lock(__func__);
2892
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002893 binder_stats_created(BINDER_STAT_PROC);
2894 hlist_add_head(&proc->proc_node, &binder_procs);
2895 proc->pid = current->group_leader->pid;
2896 INIT_LIST_HEAD(&proc->delivered_death);
2897 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002898
2899 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002900
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07002901 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002902 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09002903
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002904 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07002905 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
2906 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002907 }
2908
2909 return 0;
2910}
2911
2912static int binder_flush(struct file *filp, fl_owner_t id)
2913{
2914 struct binder_proc *proc = filp->private_data;
2915
2916 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
2917
2918 return 0;
2919}
2920
2921static void binder_deferred_flush(struct binder_proc *proc)
2922{
2923 struct rb_node *n;
2924 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09002925
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002926 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
2927 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09002928
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002929 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2930 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
2931 wake_up_interruptible(&thread->wait);
2932 wake_count++;
2933 }
2934 }
2935 wake_up_interruptible_all(&proc->wait);
2936
2937 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2938 "binder_flush: %d woke %d threads\n", proc->pid,
2939 wake_count);
2940}
2941
2942static int binder_release(struct inode *nodp, struct file *filp)
2943{
2944 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002945
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07002946 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002947 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
2948
2949 return 0;
2950}
2951
Mirsal Ennaime008fa742013-03-12 11:41:59 +01002952static int binder_node_release(struct binder_node *node, int refs)
2953{
2954 struct binder_ref *ref;
2955 int death = 0;
2956
2957 list_del_init(&node->work.entry);
2958 binder_release_work(&node->async_todo);
2959
2960 if (hlist_empty(&node->refs)) {
2961 kfree(node);
2962 binder_stats_deleted(BINDER_STAT_NODE);
2963
2964 return refs;
2965 }
2966
2967 node->proc = NULL;
2968 node->local_strong_refs = 0;
2969 node->local_weak_refs = 0;
2970 hlist_add_head(&node->dead_node, &binder_dead_nodes);
2971
2972 hlist_for_each_entry(ref, &node->refs, node_entry) {
2973 refs++;
2974
2975 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08002976 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01002977
2978 death++;
2979
2980 if (list_empty(&ref->death->work.entry)) {
2981 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2982 list_add_tail(&ref->death->work.entry,
2983 &ref->proc->todo);
2984 wake_up_interruptible(&ref->proc->wait);
2985 } else
2986 BUG();
2987 }
2988
Mirsal Ennaime008fa742013-03-12 11:41:59 +01002989 binder_debug(BINDER_DEBUG_DEAD_BINDER,
2990 "node %d now dead, refs %d, death %d\n",
2991 node->debug_id, refs, death);
2992
2993 return refs;
2994}
2995
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002996static void binder_deferred_release(struct binder_proc *proc)
2997{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002998 struct binder_transaction *t;
2999 struct rb_node *n;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003000 int threads, nodes, incoming_refs, outgoing_refs, buffers,
3001 active_transactions, page_count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003002
3003 BUG_ON(proc->vma);
3004 BUG_ON(proc->files);
3005
3006 hlist_del(&proc->proc_node);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003007
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003008 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
3009 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003010 "%s: %d context_mgr_node gone\n",
3011 __func__, proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003012 binder_context_mgr_node = NULL;
3013 }
3014
3015 threads = 0;
3016 active_transactions = 0;
3017 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003018 struct binder_thread *thread;
3019
3020 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003021 threads++;
3022 active_transactions += binder_free_thread(proc, thread);
3023 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003024
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003025 nodes = 0;
3026 incoming_refs = 0;
3027 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003028 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003029
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003030 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003031 nodes++;
3032 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003033 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003034 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003035
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003036 outgoing_refs = 0;
3037 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003038 struct binder_ref *ref;
3039
3040 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003041 outgoing_refs++;
3042 binder_delete_ref(ref);
3043 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003044
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003045 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003046 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003047
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003048 buffers = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003049 while ((n = rb_first(&proc->allocated_buffers))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003050 struct binder_buffer *buffer;
3051
3052 buffer = rb_entry(n, struct binder_buffer, rb_node);
3053
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003054 t = buffer->transaction;
3055 if (t) {
3056 t->buffer = NULL;
3057 buffer->transaction = NULL;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303058 pr_err("release proc %d, transaction %d, not freed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003059 proc->pid, t->debug_id);
3060 /*BUG();*/
3061 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003062
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003063 binder_free_buf(proc, buffer);
3064 buffers++;
3065 }
3066
3067 binder_stats_deleted(BINDER_STAT_PROC);
3068
3069 page_count = 0;
3070 if (proc->pages) {
3071 int i;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003072
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003073 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003074 void *page_addr;
3075
3076 if (!proc->pages[i])
3077 continue;
3078
3079 page_addr = proc->buffer + i * PAGE_SIZE;
3080 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003081 "%s: %d: page %d at %p not freed\n",
3082 __func__, proc->pid, i, page_addr);
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003083 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3084 __free_page(proc->pages[i]);
3085 page_count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003086 }
3087 kfree(proc->pages);
3088 vfree(proc->buffer);
3089 }
3090
3091 put_task_struct(proc->tsk);
3092
3093 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003094 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3095 __func__, proc->pid, threads, nodes, incoming_refs,
3096 outgoing_refs, active_transactions, buffers, page_count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003097
3098 kfree(proc);
3099}
3100
3101static void binder_deferred_func(struct work_struct *work)
3102{
3103 struct binder_proc *proc;
3104 struct files_struct *files;
3105
3106 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003107
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003108 do {
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003109 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003110 mutex_lock(&binder_deferred_lock);
3111 if (!hlist_empty(&binder_deferred_list)) {
3112 proc = hlist_entry(binder_deferred_list.first,
3113 struct binder_proc, deferred_work_node);
3114 hlist_del_init(&proc->deferred_work_node);
3115 defer = proc->deferred_work;
3116 proc->deferred_work = 0;
3117 } else {
3118 proc = NULL;
3119 defer = 0;
3120 }
3121 mutex_unlock(&binder_deferred_lock);
3122
3123 files = NULL;
3124 if (defer & BINDER_DEFERRED_PUT_FILES) {
3125 files = proc->files;
3126 if (files)
3127 proc->files = NULL;
3128 }
3129
3130 if (defer & BINDER_DEFERRED_FLUSH)
3131 binder_deferred_flush(proc);
3132
3133 if (defer & BINDER_DEFERRED_RELEASE)
3134 binder_deferred_release(proc); /* frees proc */
3135
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003136 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003137 if (files)
3138 put_files_struct(files);
3139 } while (proc);
3140}
3141static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3142
3143static void
3144binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3145{
3146 mutex_lock(&binder_deferred_lock);
3147 proc->deferred_work |= defer;
3148 if (hlist_unhashed(&proc->deferred_work_node)) {
3149 hlist_add_head(&proc->deferred_work_node,
3150 &binder_deferred_list);
Arve Hjønnevåg3c762a42010-04-22 15:53:23 -07003151 queue_work(binder_deferred_workqueue, &binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003152 }
3153 mutex_unlock(&binder_deferred_lock);
3154}
3155
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003156static void print_binder_transaction(struct seq_file *m, const char *prefix,
3157 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003158{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003159 seq_printf(m,
3160 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3161 prefix, t->debug_id, t,
3162 t->from ? t->from->proc->pid : 0,
3163 t->from ? t->from->pid : 0,
3164 t->to_proc ? t->to_proc->pid : 0,
3165 t->to_thread ? t->to_thread->pid : 0,
3166 t->code, t->flags, t->priority, t->need_reply);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003167 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003168 seq_puts(m, " buffer free\n");
3169 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003170 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003171 if (t->buffer->target_node)
3172 seq_printf(m, " node %d",
3173 t->buffer->target_node->debug_id);
3174 seq_printf(m, " size %zd:%zd data %p\n",
3175 t->buffer->data_size, t->buffer->offsets_size,
3176 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003177}
3178
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003179static void print_binder_buffer(struct seq_file *m, const char *prefix,
3180 struct binder_buffer *buffer)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003181{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003182 seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
3183 prefix, buffer->debug_id, buffer->data,
3184 buffer->data_size, buffer->offsets_size,
3185 buffer->transaction ? "active" : "delivered");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003186}
3187
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003188static void print_binder_work(struct seq_file *m, const char *prefix,
3189 const char *transaction_prefix,
3190 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003191{
3192 struct binder_node *node;
3193 struct binder_transaction *t;
3194
3195 switch (w->type) {
3196 case BINDER_WORK_TRANSACTION:
3197 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003198 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003199 break;
3200 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003201 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003202 break;
3203 case BINDER_WORK_NODE:
3204 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003205 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3206 prefix, node->debug_id,
3207 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003208 break;
3209 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003210 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003211 break;
3212 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003213 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003214 break;
3215 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003216 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003217 break;
3218 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003219 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003220 break;
3221 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003222}
3223
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003224static void print_binder_thread(struct seq_file *m,
3225 struct binder_thread *thread,
3226 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003227{
3228 struct binder_transaction *t;
3229 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003230 size_t start_pos = m->count;
3231 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003232
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003233 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3234 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003235 t = thread->transaction_stack;
3236 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003237 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003238 print_binder_transaction(m,
3239 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003240 t = t->from_parent;
3241 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003242 print_binder_transaction(m,
3243 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003244 t = t->to_parent;
3245 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003246 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003247 t = NULL;
3248 }
3249 }
3250 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003251 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003252 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003253 if (!print_always && m->count == header_pos)
3254 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003255}
3256
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003257static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003258{
3259 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003260 struct binder_work *w;
3261 int count;
3262
3263 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003264 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003265 count++;
3266
Arve Hjønnevågda498892014-02-21 14:40:26 -08003267 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3268 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003269 node->has_strong_ref, node->has_weak_ref,
3270 node->local_strong_refs, node->local_weak_refs,
3271 node->internal_strong_refs, count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003272 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003273 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003274 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003275 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003276 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003277 seq_puts(m, "\n");
3278 list_for_each_entry(w, &node->async_todo, entry)
3279 print_binder_work(m, " ",
3280 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003281}
3282
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003283static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003284{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003285 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3286 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3287 ref->node->debug_id, ref->strong, ref->weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003288}
3289
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003290static void print_binder_proc(struct seq_file *m,
3291 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003292{
3293 struct binder_work *w;
3294 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003295 size_t start_pos = m->count;
3296 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003297
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003298 seq_printf(m, "proc %d\n", proc->pid);
3299 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003300
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003301 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3302 print_binder_thread(m, rb_entry(n, struct binder_thread,
3303 rb_node), print_all);
3304 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003305 struct binder_node *node = rb_entry(n, struct binder_node,
3306 rb_node);
3307 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003308 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003309 }
3310 if (print_all) {
3311 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003312 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003313 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003314 print_binder_ref(m, rb_entry(n, struct binder_ref,
3315 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003316 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003317 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3318 print_binder_buffer(m, " buffer",
3319 rb_entry(n, struct binder_buffer, rb_node));
3320 list_for_each_entry(w, &proc->todo, entry)
3321 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003322 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003323 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003324 break;
3325 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003326 if (!print_all && m->count == header_pos)
3327 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003328}
3329
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003330static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003331 "BR_ERROR",
3332 "BR_OK",
3333 "BR_TRANSACTION",
3334 "BR_REPLY",
3335 "BR_ACQUIRE_RESULT",
3336 "BR_DEAD_REPLY",
3337 "BR_TRANSACTION_COMPLETE",
3338 "BR_INCREFS",
3339 "BR_ACQUIRE",
3340 "BR_RELEASE",
3341 "BR_DECREFS",
3342 "BR_ATTEMPT_ACQUIRE",
3343 "BR_NOOP",
3344 "BR_SPAWN_LOOPER",
3345 "BR_FINISHED",
3346 "BR_DEAD_BINDER",
3347 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3348 "BR_FAILED_REPLY"
3349};
3350
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003351static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003352 "BC_TRANSACTION",
3353 "BC_REPLY",
3354 "BC_ACQUIRE_RESULT",
3355 "BC_FREE_BUFFER",
3356 "BC_INCREFS",
3357 "BC_ACQUIRE",
3358 "BC_RELEASE",
3359 "BC_DECREFS",
3360 "BC_INCREFS_DONE",
3361 "BC_ACQUIRE_DONE",
3362 "BC_ATTEMPT_ACQUIRE",
3363 "BC_REGISTER_LOOPER",
3364 "BC_ENTER_LOOPER",
3365 "BC_EXIT_LOOPER",
3366 "BC_REQUEST_DEATH_NOTIFICATION",
3367 "BC_CLEAR_DEATH_NOTIFICATION",
3368 "BC_DEAD_BINDER_DONE"
3369};
3370
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003371static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003372 "proc",
3373 "thread",
3374 "node",
3375 "ref",
3376 "death",
3377 "transaction",
3378 "transaction_complete"
3379};
3380
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003381static void print_binder_stats(struct seq_file *m, const char *prefix,
3382 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003383{
3384 int i;
3385
3386 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003387 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003388 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3389 if (stats->bc[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003390 seq_printf(m, "%s%s: %d\n", prefix,
3391 binder_command_strings[i], stats->bc[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003392 }
3393
3394 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003395 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3397 if (stats->br[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003398 seq_printf(m, "%s%s: %d\n", prefix,
3399 binder_return_strings[i], stats->br[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003400 }
3401
3402 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003403 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003404 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003405 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003406 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3407 if (stats->obj_created[i] || stats->obj_deleted[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003408 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3409 binder_objstat_strings[i],
3410 stats->obj_created[i] - stats->obj_deleted[i],
3411 stats->obj_created[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003412 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003413}
3414
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003415static void print_binder_proc_stats(struct seq_file *m,
3416 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003417{
3418 struct binder_work *w;
3419 struct rb_node *n;
3420 int count, strong, weak;
3421
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003422 seq_printf(m, "proc %d\n", proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003423 count = 0;
3424 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3425 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003426 seq_printf(m, " threads: %d\n", count);
3427 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003428 " ready threads %d\n"
3429 " free async space %zd\n", proc->requested_threads,
3430 proc->requested_threads_started, proc->max_threads,
3431 proc->ready_threads, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003432 count = 0;
3433 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3434 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003435 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003436 count = 0;
3437 strong = 0;
3438 weak = 0;
3439 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3440 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3441 rb_node_desc);
3442 count++;
3443 strong += ref->strong;
3444 weak += ref->weak;
3445 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003446 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003447
3448 count = 0;
3449 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3450 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003451 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003452
3453 count = 0;
3454 list_for_each_entry(w, &proc->todo, entry) {
3455 switch (w->type) {
3456 case BINDER_WORK_TRANSACTION:
3457 count++;
3458 break;
3459 default:
3460 break;
3461 }
3462 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003463 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003464
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003465 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003466}
3467
3468
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003469static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003470{
3471 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003472 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003473 int do_lock = !binder_debug_no_lock;
3474
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003475 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003476 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003477
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003478 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003479
3480 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003481 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003482 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003483 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003484
Sasha Levinb67bfe02013-02-27 17:06:00 -08003485 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003486 print_binder_proc(m, proc, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003487 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003488 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003489 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003490}
3491
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003492static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003493{
3494 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003495 int do_lock = !binder_debug_no_lock;
3496
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003497 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003498 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003499
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003500 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003501
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003502 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003503
Sasha Levinb67bfe02013-02-27 17:06:00 -08003504 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003505 print_binder_proc_stats(m, proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003506 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003507 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003508 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003509}
3510
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003511static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003512{
3513 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003514 int do_lock = !binder_debug_no_lock;
3515
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003516 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003517 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003518
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003519 seq_puts(m, "binder transactions:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003520 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003521 print_binder_proc(m, proc, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003522 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003523 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003524 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003525}
3526
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003527static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003528{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003529 struct binder_proc *proc = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003530 int do_lock = !binder_debug_no_lock;
3531
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003532 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003533 binder_lock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003534 seq_puts(m, "binder proc state:\n");
3535 print_binder_proc(m, proc, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003536 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003537 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003538 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003539}
3540
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003541static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003542 struct binder_transaction_log_entry *e)
3543{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003544 seq_printf(m,
3545 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3546 e->debug_id, (e->call_type == 2) ? "reply" :
3547 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3548 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3549 e->target_handle, e->data_size, e->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003550}
3551
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003552static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003553{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003554 struct binder_transaction_log *log = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003555 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003556
3557 if (log->full) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003558 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3559 print_binder_transaction_log_entry(m, &log->entry[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003560 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003561 for (i = 0; i < log->next; i++)
3562 print_binder_transaction_log_entry(m, &log->entry[i]);
3563 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003564}
3565
3566static const struct file_operations binder_fops = {
3567 .owner = THIS_MODULE,
3568 .poll = binder_poll,
3569 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003570 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003571 .mmap = binder_mmap,
3572 .open = binder_open,
3573 .flush = binder_flush,
3574 .release = binder_release,
3575};
3576
3577static struct miscdevice binder_miscdev = {
3578 .minor = MISC_DYNAMIC_MINOR,
3579 .name = "binder",
3580 .fops = &binder_fops
3581};
3582
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003583BINDER_DEBUG_ENTRY(state);
3584BINDER_DEBUG_ENTRY(stats);
3585BINDER_DEBUG_ENTRY(transactions);
3586BINDER_DEBUG_ENTRY(transaction_log);
3587
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003588static int __init binder_init(void)
3589{
3590 int ret;
3591
Arve Hjønnevåg3c762a42010-04-22 15:53:23 -07003592 binder_deferred_workqueue = create_singlethread_workqueue("binder");
3593 if (!binder_deferred_workqueue)
3594 return -ENOMEM;
3595
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003596 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3597 if (binder_debugfs_dir_entry_root)
3598 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3599 binder_debugfs_dir_entry_root);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003600 ret = misc_register(&binder_miscdev);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003601 if (binder_debugfs_dir_entry_root) {
3602 debugfs_create_file("state",
3603 S_IRUGO,
3604 binder_debugfs_dir_entry_root,
3605 NULL,
3606 &binder_state_fops);
3607 debugfs_create_file("stats",
3608 S_IRUGO,
3609 binder_debugfs_dir_entry_root,
3610 NULL,
3611 &binder_stats_fops);
3612 debugfs_create_file("transactions",
3613 S_IRUGO,
3614 binder_debugfs_dir_entry_root,
3615 NULL,
3616 &binder_transactions_fops);
3617 debugfs_create_file("transaction_log",
3618 S_IRUGO,
3619 binder_debugfs_dir_entry_root,
3620 &binder_transaction_log,
3621 &binder_transaction_log_fops);
3622 debugfs_create_file("failed_transaction_log",
3623 S_IRUGO,
3624 binder_debugfs_dir_entry_root,
3625 &binder_transaction_log_failed,
3626 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003627 }
3628 return ret;
3629}
3630
3631device_initcall(binder_init);
3632
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003633#define CREATE_TRACE_POINTS
3634#include "binder_trace.h"
3635
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003636MODULE_LICENSE("GPL v2");