blob: 16288e777ec3dfdb50db2b133dd44f7e0045d8ad [file] [log] [blame]
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001/* binder.c
2 *
3 * Android IPC Subsystem
4 *
5 * Copyright (C) 2007-2008 Google, Inc.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Anmol Sarma56b468f2012-10-30 22:35:43 +053018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090020#include <asm/cacheflush.h>
21#include <linux/fdtable.h>
22#include <linux/file.h>
Colin Crosse2610b22013-05-06 23:50:15 +000023#include <linux/freezer.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090024#include <linux/fs.h>
25#include <linux/list.h>
26#include <linux/miscdevice.h>
27#include <linux/mm.h>
28#include <linux/module.h>
29#include <linux/mutex.h>
30#include <linux/nsproxy.h>
31#include <linux/poll.h>
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070032#include <linux/debugfs.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090033#include <linux/rbtree.h>
34#include <linux/sched.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070035#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090036#include <linux/uaccess.h>
37#include <linux/vmalloc.h>
Colin Crossc11a1662010-04-15 15:21:51 -070038#include <linux/slab.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080039#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050040#include <linux/security.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090041
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020042#ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
43#define BINDER_IPC_32BIT 1
44#endif
45
46#include <uapi/linux/android/binder.h>
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070047#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090048
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070049static DEFINE_MUTEX(binder_main_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090050static DEFINE_MUTEX(binder_deferred_lock);
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -080051static DEFINE_MUTEX(binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090052
53static HLIST_HEAD(binder_procs);
54static HLIST_HEAD(binder_deferred_list);
55static HLIST_HEAD(binder_dead_nodes);
56
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070057static struct dentry *binder_debugfs_dir_entry_root;
58static struct dentry *binder_debugfs_dir_entry_proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090059static struct binder_node *binder_context_mgr_node;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -060060static kuid_t binder_context_mgr_uid = INVALID_UID;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090061static int binder_last_id;
Arve Hjønnevåg3c762a42010-04-22 15:53:23 -070062static struct workqueue_struct *binder_deferred_workqueue;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090063
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070064#define BINDER_DEBUG_ENTRY(name) \
65static int binder_##name##_open(struct inode *inode, struct file *file) \
66{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070067 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070068} \
69\
70static const struct file_operations binder_##name##_fops = { \
71 .owner = THIS_MODULE, \
72 .open = binder_##name##_open, \
73 .read = seq_read, \
74 .llseek = seq_lseek, \
75 .release = single_release, \
76}
77
78static int binder_proc_show(struct seq_file *m, void *unused);
79BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090080
81/* This is only defined in include/asm-arm/sizes.h */
82#ifndef SZ_1K
83#define SZ_1K 0x400
84#endif
85
86#ifndef SZ_4M
87#define SZ_4M 0x400000
88#endif
89
90#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
91
92#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
93
94enum {
95 BINDER_DEBUG_USER_ERROR = 1U << 0,
96 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
97 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
98 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
99 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
100 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
101 BINDER_DEBUG_READ_WRITE = 1U << 6,
102 BINDER_DEBUG_USER_REFS = 1U << 7,
103 BINDER_DEBUG_THREADS = 1U << 8,
104 BINDER_DEBUG_TRANSACTION = 1U << 9,
105 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
106 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
107 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
108 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
109 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
110 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
111};
112static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
113 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
114module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
115
Zhengwang Ruan2c523252012-03-07 10:36:57 +0800116static bool binder_debug_no_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900117module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
118
119static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
120static int binder_stop_on_user_error;
121
122static int binder_set_stop_on_user_error(const char *val,
123 struct kernel_param *kp)
124{
125 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900126
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900127 ret = param_set_int(val, kp);
128 if (binder_stop_on_user_error < 2)
129 wake_up(&binder_user_error_wait);
130 return ret;
131}
132module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
133 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
134
135#define binder_debug(mask, x...) \
136 do { \
137 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400138 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900139 } while (0)
140
141#define binder_user_error(x...) \
142 do { \
143 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400144 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900145 if (binder_stop_on_user_error) \
146 binder_stop_on_user_error = 2; \
147 } while (0)
148
149enum binder_stat_types {
150 BINDER_STAT_PROC,
151 BINDER_STAT_THREAD,
152 BINDER_STAT_NODE,
153 BINDER_STAT_REF,
154 BINDER_STAT_DEATH,
155 BINDER_STAT_TRANSACTION,
156 BINDER_STAT_TRANSACTION_COMPLETE,
157 BINDER_STAT_COUNT
158};
159
160struct binder_stats {
161 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
162 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
163 int obj_created[BINDER_STAT_COUNT];
164 int obj_deleted[BINDER_STAT_COUNT];
165};
166
167static struct binder_stats binder_stats;
168
169static inline void binder_stats_deleted(enum binder_stat_types type)
170{
171 binder_stats.obj_deleted[type]++;
172}
173
174static inline void binder_stats_created(enum binder_stat_types type)
175{
176 binder_stats.obj_created[type]++;
177}
178
179struct binder_transaction_log_entry {
180 int debug_id;
181 int call_type;
182 int from_proc;
183 int from_thread;
184 int target_handle;
185 int to_proc;
186 int to_thread;
187 int to_node;
188 int data_size;
189 int offsets_size;
190};
191struct binder_transaction_log {
192 int next;
193 int full;
194 struct binder_transaction_log_entry entry[32];
195};
196static struct binder_transaction_log binder_transaction_log;
197static struct binder_transaction_log binder_transaction_log_failed;
198
199static struct binder_transaction_log_entry *binder_transaction_log_add(
200 struct binder_transaction_log *log)
201{
202 struct binder_transaction_log_entry *e;
Seunghun Lee10f62862014-05-01 01:30:23 +0900203
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900204 e = &log->entry[log->next];
205 memset(e, 0, sizeof(*e));
206 log->next++;
207 if (log->next == ARRAY_SIZE(log->entry)) {
208 log->next = 0;
209 log->full = 1;
210 }
211 return e;
212}
213
214struct binder_work {
215 struct list_head entry;
216 enum {
217 BINDER_WORK_TRANSACTION = 1,
218 BINDER_WORK_TRANSACTION_COMPLETE,
219 BINDER_WORK_NODE,
220 BINDER_WORK_DEAD_BINDER,
221 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
222 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
223 } type;
224};
225
226struct binder_node {
227 int debug_id;
228 struct binder_work work;
229 union {
230 struct rb_node rb_node;
231 struct hlist_node dead_node;
232 };
233 struct binder_proc *proc;
234 struct hlist_head refs;
235 int internal_strong_refs;
236 int local_weak_refs;
237 int local_strong_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800238 binder_uintptr_t ptr;
239 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900240 unsigned has_strong_ref:1;
241 unsigned pending_strong_ref:1;
242 unsigned has_weak_ref:1;
243 unsigned pending_weak_ref:1;
244 unsigned has_async_transaction:1;
245 unsigned accept_fds:1;
246 unsigned min_priority:8;
247 struct list_head async_todo;
248};
249
250struct binder_ref_death {
251 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800252 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900253};
254
255struct binder_ref {
256 /* Lookups needed: */
257 /* node + proc => ref (transaction) */
258 /* desc + proc => ref (transaction, inc/dec ref) */
259 /* node => refs + procs (proc exit) */
260 int debug_id;
261 struct rb_node rb_node_desc;
262 struct rb_node rb_node_node;
263 struct hlist_node node_entry;
264 struct binder_proc *proc;
265 struct binder_node *node;
266 uint32_t desc;
267 int strong;
268 int weak;
269 struct binder_ref_death *death;
270};
271
272struct binder_buffer {
Justin P. Mattock217218f2012-01-12 06:51:31 -0800273 struct list_head entry; /* free and allocated entries by address */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900274 struct rb_node rb_node; /* free entry by size or allocated entry */
275 /* by address */
276 unsigned free:1;
277 unsigned allow_user_free:1;
278 unsigned async_transaction:1;
279 unsigned debug_id:29;
280
281 struct binder_transaction *transaction;
282
283 struct binder_node *target_node;
284 size_t data_size;
285 size_t offsets_size;
286 uint8_t data[0];
287};
288
289enum binder_deferred_state {
290 BINDER_DEFERRED_PUT_FILES = 0x01,
291 BINDER_DEFERRED_FLUSH = 0x02,
292 BINDER_DEFERRED_RELEASE = 0x04,
293};
294
295struct binder_proc {
296 struct hlist_node proc_node;
297 struct rb_root threads;
298 struct rb_root nodes;
299 struct rb_root refs_by_desc;
300 struct rb_root refs_by_node;
301 int pid;
302 struct vm_area_struct *vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800303 struct mm_struct *vma_vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900304 struct task_struct *tsk;
305 struct files_struct *files;
306 struct hlist_node deferred_work_node;
307 int deferred_work;
308 void *buffer;
309 ptrdiff_t user_buffer_offset;
310
311 struct list_head buffers;
312 struct rb_root free_buffers;
313 struct rb_root allocated_buffers;
314 size_t free_async_space;
315
316 struct page **pages;
317 size_t buffer_size;
318 uint32_t buffer_free;
319 struct list_head todo;
320 wait_queue_head_t wait;
321 struct binder_stats stats;
322 struct list_head delivered_death;
323 int max_threads;
324 int requested_threads;
325 int requested_threads_started;
326 int ready_threads;
327 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700328 struct dentry *debugfs_entry;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900329};
330
331enum {
332 BINDER_LOOPER_STATE_REGISTERED = 0x01,
333 BINDER_LOOPER_STATE_ENTERED = 0x02,
334 BINDER_LOOPER_STATE_EXITED = 0x04,
335 BINDER_LOOPER_STATE_INVALID = 0x08,
336 BINDER_LOOPER_STATE_WAITING = 0x10,
337 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
338};
339
340struct binder_thread {
341 struct binder_proc *proc;
342 struct rb_node rb_node;
343 int pid;
344 int looper;
345 struct binder_transaction *transaction_stack;
346 struct list_head todo;
347 uint32_t return_error; /* Write failed, return error code in read buf */
348 uint32_t return_error2; /* Write failed, return error code in read */
349 /* buffer. Used when sending a reply to a dead process that */
350 /* we are also waiting on */
351 wait_queue_head_t wait;
352 struct binder_stats stats;
353};
354
355struct binder_transaction {
356 int debug_id;
357 struct binder_work work;
358 struct binder_thread *from;
359 struct binder_transaction *from_parent;
360 struct binder_proc *to_proc;
361 struct binder_thread *to_thread;
362 struct binder_transaction *to_parent;
363 unsigned need_reply:1;
364 /* unsigned is_dead:1; */ /* not used at the moment */
365
366 struct binder_buffer *buffer;
367 unsigned int code;
368 unsigned int flags;
369 long priority;
370 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600371 kuid_t sender_euid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900372};
373
374static void
375binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
376
Sachin Kamatefde99c2012-08-17 16:39:36 +0530377static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900378{
379 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900380 unsigned long rlim_cur;
381 unsigned long irqs;
382
383 if (files == NULL)
384 return -ESRCH;
385
Al Virodcfadfa2012-08-12 17:27:30 -0400386 if (!lock_task_sighand(proc->tsk, &irqs))
387 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900388
Al Virodcfadfa2012-08-12 17:27:30 -0400389 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
390 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900391
Al Virodcfadfa2012-08-12 17:27:30 -0400392 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900393}
394
395/*
396 * copied from fd_install
397 */
398static void task_fd_install(
399 struct binder_proc *proc, unsigned int fd, struct file *file)
400{
Al Virof869e8a2012-08-15 21:06:33 -0400401 if (proc->files)
402 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900403}
404
405/*
406 * copied from sys_close
407 */
408static long task_close_fd(struct binder_proc *proc, unsigned int fd)
409{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900410 int retval;
411
Al Viro483ce1d2012-08-19 12:04:24 -0400412 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900413 return -ESRCH;
414
Al Viro483ce1d2012-08-19 12:04:24 -0400415 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900416 /* can't restart close syscall because file table entry was cleared */
417 if (unlikely(retval == -ERESTARTSYS ||
418 retval == -ERESTARTNOINTR ||
419 retval == -ERESTARTNOHAND ||
420 retval == -ERESTART_RESTARTBLOCK))
421 retval = -EINTR;
422
423 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900424}
425
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700426static inline void binder_lock(const char *tag)
427{
428 trace_binder_lock(tag);
429 mutex_lock(&binder_main_lock);
430 trace_binder_locked(tag);
431}
432
433static inline void binder_unlock(const char *tag)
434{
435 trace_binder_unlock(tag);
436 mutex_unlock(&binder_main_lock);
437}
438
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900439static void binder_set_nice(long nice)
440{
441 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900442
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900443 if (can_nice(current, nice)) {
444 set_user_nice(current, nice);
445 return;
446 }
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900447 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900448 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530449 "%d: nice value %ld not allowed use %ld instead\n",
450 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900451 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +0800452 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900453 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530454 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900455}
456
457static size_t binder_buffer_size(struct binder_proc *proc,
458 struct binder_buffer *buffer)
459{
460 if (list_is_last(&buffer->entry, &proc->buffers))
461 return proc->buffer + proc->buffer_size - (void *)buffer->data;
Karthik Nayak78733112014-06-21 20:23:16 +0530462 return (size_t)list_entry(buffer->entry.next,
463 struct binder_buffer, entry) - (size_t)buffer->data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900464}
465
466static void binder_insert_free_buffer(struct binder_proc *proc,
467 struct binder_buffer *new_buffer)
468{
469 struct rb_node **p = &proc->free_buffers.rb_node;
470 struct rb_node *parent = NULL;
471 struct binder_buffer *buffer;
472 size_t buffer_size;
473 size_t new_buffer_size;
474
475 BUG_ON(!new_buffer->free);
476
477 new_buffer_size = binder_buffer_size(proc, new_buffer);
478
479 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530480 "%d: add free buffer, size %zd, at %p\n",
481 proc->pid, new_buffer_size, new_buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900482
483 while (*p) {
484 parent = *p;
485 buffer = rb_entry(parent, struct binder_buffer, rb_node);
486 BUG_ON(!buffer->free);
487
488 buffer_size = binder_buffer_size(proc, buffer);
489
490 if (new_buffer_size < buffer_size)
491 p = &parent->rb_left;
492 else
493 p = &parent->rb_right;
494 }
495 rb_link_node(&new_buffer->rb_node, parent, p);
496 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
497}
498
499static void binder_insert_allocated_buffer(struct binder_proc *proc,
500 struct binder_buffer *new_buffer)
501{
502 struct rb_node **p = &proc->allocated_buffers.rb_node;
503 struct rb_node *parent = NULL;
504 struct binder_buffer *buffer;
505
506 BUG_ON(new_buffer->free);
507
508 while (*p) {
509 parent = *p;
510 buffer = rb_entry(parent, struct binder_buffer, rb_node);
511 BUG_ON(buffer->free);
512
513 if (new_buffer < buffer)
514 p = &parent->rb_left;
515 else if (new_buffer > buffer)
516 p = &parent->rb_right;
517 else
518 BUG();
519 }
520 rb_link_node(&new_buffer->rb_node, parent, p);
521 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
522}
523
524static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800525 uintptr_t user_ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900526{
527 struct rb_node *n = proc->allocated_buffers.rb_node;
528 struct binder_buffer *buffer;
529 struct binder_buffer *kern_ptr;
530
Arve Hjønnevågda498892014-02-21 14:40:26 -0800531 kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
532 - offsetof(struct binder_buffer, data));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900533
534 while (n) {
535 buffer = rb_entry(n, struct binder_buffer, rb_node);
536 BUG_ON(buffer->free);
537
538 if (kern_ptr < buffer)
539 n = n->rb_left;
540 else if (kern_ptr > buffer)
541 n = n->rb_right;
542 else
543 return buffer;
544 }
545 return NULL;
546}
547
548static int binder_update_page_range(struct binder_proc *proc, int allocate,
549 void *start, void *end,
550 struct vm_area_struct *vma)
551{
552 void *page_addr;
553 unsigned long user_page_addr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900554 struct page **page;
555 struct mm_struct *mm;
556
557 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530558 "%d: %s pages %p-%p\n", proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900559 allocate ? "allocate" : "free", start, end);
560
561 if (end <= start)
562 return 0;
563
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700564 trace_binder_update_page_range(proc, allocate, start, end);
565
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900566 if (vma)
567 mm = NULL;
568 else
569 mm = get_task_mm(proc->tsk);
570
571 if (mm) {
572 down_write(&mm->mmap_sem);
573 vma = proc->vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800574 if (vma && mm != proc->vma_vm_mm) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530575 pr_err("%d: vma mm and task mm mismatch\n",
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -0800576 proc->pid);
577 vma = NULL;
578 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900579 }
580
581 if (allocate == 0)
582 goto free_range;
583
584 if (vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530585 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
586 proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900587 goto err_no_vma;
588 }
589
590 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
591 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900592
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900593 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
594
595 BUG_ON(*page);
Arve Hjønnevåg585650d2012-10-16 15:29:55 -0700596 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900597 if (*page == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530598 pr_err("%d: binder_alloc_buf failed for page at %p\n",
599 proc->pid, page_addr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900600 goto err_alloc_page_failed;
601 }
Andrey Ryabininf4c72c72015-02-27 20:44:21 +0300602 ret = map_kernel_range_noflush((unsigned long)page_addr,
603 PAGE_SIZE, PAGE_KERNEL, page);
604 flush_cache_vmap((unsigned long)page_addr,
605 (unsigned long)page_addr + PAGE_SIZE);
606 if (ret != 1) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530607 pr_err("%d: binder_alloc_buf failed to map page at %p in kernel\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900608 proc->pid, page_addr);
609 goto err_map_kernel_failed;
610 }
611 user_page_addr =
612 (uintptr_t)page_addr + proc->user_buffer_offset;
613 ret = vm_insert_page(vma, user_page_addr, page[0]);
614 if (ret) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530615 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900616 proc->pid, user_page_addr);
617 goto err_vm_insert_page_failed;
618 }
619 /* vm_insert_page does not seem to increment the refcount */
620 }
621 if (mm) {
622 up_write(&mm->mmap_sem);
623 mmput(mm);
624 }
625 return 0;
626
627free_range:
628 for (page_addr = end - PAGE_SIZE; page_addr >= start;
629 page_addr -= PAGE_SIZE) {
630 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
631 if (vma)
632 zap_page_range(vma, (uintptr_t)page_addr +
633 proc->user_buffer_offset, PAGE_SIZE, NULL);
634err_vm_insert_page_failed:
635 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
636err_map_kernel_failed:
637 __free_page(*page);
638 *page = NULL;
639err_alloc_page_failed:
640 ;
641 }
642err_no_vma:
643 if (mm) {
644 up_write(&mm->mmap_sem);
645 mmput(mm);
646 }
647 return -ENOMEM;
648}
649
650static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
651 size_t data_size,
652 size_t offsets_size, int is_async)
653{
654 struct rb_node *n = proc->free_buffers.rb_node;
655 struct binder_buffer *buffer;
656 size_t buffer_size;
657 struct rb_node *best_fit = NULL;
658 void *has_page_addr;
659 void *end_page_addr;
660 size_t size;
661
662 if (proc->vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530663 pr_err("%d: binder_alloc_buf, no vma\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900664 proc->pid);
665 return NULL;
666 }
667
668 size = ALIGN(data_size, sizeof(void *)) +
669 ALIGN(offsets_size, sizeof(void *));
670
671 if (size < data_size || size < offsets_size) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530672 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
673 proc->pid, data_size, offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900674 return NULL;
675 }
676
677 if (is_async &&
678 proc->free_async_space < size + sizeof(struct binder_buffer)) {
679 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530680 "%d: binder_alloc_buf size %zd failed, no async space left\n",
681 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900682 return NULL;
683 }
684
685 while (n) {
686 buffer = rb_entry(n, struct binder_buffer, rb_node);
687 BUG_ON(!buffer->free);
688 buffer_size = binder_buffer_size(proc, buffer);
689
690 if (size < buffer_size) {
691 best_fit = n;
692 n = n->rb_left;
693 } else if (size > buffer_size)
694 n = n->rb_right;
695 else {
696 best_fit = n;
697 break;
698 }
699 }
700 if (best_fit == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530701 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
702 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900703 return NULL;
704 }
705 if (n == NULL) {
706 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
707 buffer_size = binder_buffer_size(proc, buffer);
708 }
709
710 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530711 "%d: binder_alloc_buf size %zd got buffer %p size %zd\n",
712 proc->pid, size, buffer, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900713
714 has_page_addr =
715 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
716 if (n == NULL) {
717 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
718 buffer_size = size; /* no room for other buffers */
719 else
720 buffer_size = size + sizeof(struct binder_buffer);
721 }
722 end_page_addr =
723 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
724 if (end_page_addr > has_page_addr)
725 end_page_addr = has_page_addr;
726 if (binder_update_page_range(proc, 1,
727 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
728 return NULL;
729
730 rb_erase(best_fit, &proc->free_buffers);
731 buffer->free = 0;
732 binder_insert_allocated_buffer(proc, buffer);
733 if (buffer_size != size) {
734 struct binder_buffer *new_buffer = (void *)buffer->data + size;
Seunghun Lee10f62862014-05-01 01:30:23 +0900735
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900736 list_add(&new_buffer->entry, &buffer->entry);
737 new_buffer->free = 1;
738 binder_insert_free_buffer(proc, new_buffer);
739 }
740 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530741 "%d: binder_alloc_buf size %zd got %p\n",
742 proc->pid, size, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900743 buffer->data_size = data_size;
744 buffer->offsets_size = offsets_size;
745 buffer->async_transaction = is_async;
746 if (is_async) {
747 proc->free_async_space -= size + sizeof(struct binder_buffer);
748 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530749 "%d: binder_alloc_buf size %zd async free %zd\n",
750 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900751 }
752
753 return buffer;
754}
755
756static void *buffer_start_page(struct binder_buffer *buffer)
757{
758 return (void *)((uintptr_t)buffer & PAGE_MASK);
759}
760
761static void *buffer_end_page(struct binder_buffer *buffer)
762{
763 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
764}
765
766static void binder_delete_free_buffer(struct binder_proc *proc,
767 struct binder_buffer *buffer)
768{
769 struct binder_buffer *prev, *next = NULL;
770 int free_page_end = 1;
771 int free_page_start = 1;
772
773 BUG_ON(proc->buffers.next == &buffer->entry);
774 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
775 BUG_ON(!prev->free);
776 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
777 free_page_start = 0;
778 if (buffer_end_page(prev) == buffer_end_page(buffer))
779 free_page_end = 0;
780 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530781 "%d: merge free, buffer %p share page with %p\n",
782 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900783 }
784
785 if (!list_is_last(&buffer->entry, &proc->buffers)) {
786 next = list_entry(buffer->entry.next,
787 struct binder_buffer, entry);
788 if (buffer_start_page(next) == buffer_end_page(buffer)) {
789 free_page_end = 0;
790 if (buffer_start_page(next) ==
791 buffer_start_page(buffer))
792 free_page_start = 0;
793 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530794 "%d: merge free, buffer %p share page with %p\n",
795 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900796 }
797 }
798 list_del(&buffer->entry);
799 if (free_page_start || free_page_end) {
800 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Masanari Iida1dcdbfd2013-06-23 23:47:15 +0900801 "%d: merge free, buffer %p do not share page%s%s with %p or %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900802 proc->pid, buffer, free_page_start ? "" : " end",
803 free_page_end ? "" : " start", prev, next);
804 binder_update_page_range(proc, 0, free_page_start ?
805 buffer_start_page(buffer) : buffer_end_page(buffer),
806 (free_page_end ? buffer_end_page(buffer) :
807 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
808 }
809}
810
811static void binder_free_buf(struct binder_proc *proc,
812 struct binder_buffer *buffer)
813{
814 size_t size, buffer_size;
815
816 buffer_size = binder_buffer_size(proc, buffer);
817
818 size = ALIGN(buffer->data_size, sizeof(void *)) +
819 ALIGN(buffer->offsets_size, sizeof(void *));
820
821 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530822 "%d: binder_free_buf %p size %zd buffer_size %zd\n",
823 proc->pid, buffer, size, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900824
825 BUG_ON(buffer->free);
826 BUG_ON(size > buffer_size);
827 BUG_ON(buffer->transaction != NULL);
828 BUG_ON((void *)buffer < proc->buffer);
829 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
830
831 if (buffer->async_transaction) {
832 proc->free_async_space += size + sizeof(struct binder_buffer);
833
834 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530835 "%d: binder_free_buf size %zd async free %zd\n",
836 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900837 }
838
839 binder_update_page_range(proc, 0,
840 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
841 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
842 NULL);
843 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
844 buffer->free = 1;
845 if (!list_is_last(&buffer->entry, &proc->buffers)) {
846 struct binder_buffer *next = list_entry(buffer->entry.next,
847 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900848
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900849 if (next->free) {
850 rb_erase(&next->rb_node, &proc->free_buffers);
851 binder_delete_free_buffer(proc, next);
852 }
853 }
854 if (proc->buffers.next != &buffer->entry) {
855 struct binder_buffer *prev = list_entry(buffer->entry.prev,
856 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900857
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900858 if (prev->free) {
859 binder_delete_free_buffer(proc, buffer);
860 rb_erase(&prev->rb_node, &proc->free_buffers);
861 buffer = prev;
862 }
863 }
864 binder_insert_free_buffer(proc, buffer);
865}
866
867static struct binder_node *binder_get_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800868 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900869{
870 struct rb_node *n = proc->nodes.rb_node;
871 struct binder_node *node;
872
873 while (n) {
874 node = rb_entry(n, struct binder_node, rb_node);
875
876 if (ptr < node->ptr)
877 n = n->rb_left;
878 else if (ptr > node->ptr)
879 n = n->rb_right;
880 else
881 return node;
882 }
883 return NULL;
884}
885
886static struct binder_node *binder_new_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800887 binder_uintptr_t ptr,
888 binder_uintptr_t cookie)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900889{
890 struct rb_node **p = &proc->nodes.rb_node;
891 struct rb_node *parent = NULL;
892 struct binder_node *node;
893
894 while (*p) {
895 parent = *p;
896 node = rb_entry(parent, struct binder_node, rb_node);
897
898 if (ptr < node->ptr)
899 p = &(*p)->rb_left;
900 else if (ptr > node->ptr)
901 p = &(*p)->rb_right;
902 else
903 return NULL;
904 }
905
906 node = kzalloc(sizeof(*node), GFP_KERNEL);
907 if (node == NULL)
908 return NULL;
909 binder_stats_created(BINDER_STAT_NODE);
910 rb_link_node(&node->rb_node, parent, p);
911 rb_insert_color(&node->rb_node, &proc->nodes);
912 node->debug_id = ++binder_last_id;
913 node->proc = proc;
914 node->ptr = ptr;
915 node->cookie = cookie;
916 node->work.type = BINDER_WORK_NODE;
917 INIT_LIST_HEAD(&node->work.entry);
918 INIT_LIST_HEAD(&node->async_todo);
919 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800920 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900921 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800922 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900923 return node;
924}
925
926static int binder_inc_node(struct binder_node *node, int strong, int internal,
927 struct list_head *target_list)
928{
929 if (strong) {
930 if (internal) {
931 if (target_list == NULL &&
932 node->internal_strong_refs == 0 &&
933 !(node == binder_context_mgr_node &&
934 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530935 pr_err("invalid inc strong node for %d\n",
936 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900937 return -EINVAL;
938 }
939 node->internal_strong_refs++;
940 } else
941 node->local_strong_refs++;
942 if (!node->has_strong_ref && target_list) {
943 list_del_init(&node->work.entry);
944 list_add_tail(&node->work.entry, target_list);
945 }
946 } else {
947 if (!internal)
948 node->local_weak_refs++;
949 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
950 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530951 pr_err("invalid inc weak node for %d\n",
952 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900953 return -EINVAL;
954 }
955 list_add_tail(&node->work.entry, target_list);
956 }
957 }
958 return 0;
959}
960
961static int binder_dec_node(struct binder_node *node, int strong, int internal)
962{
963 if (strong) {
964 if (internal)
965 node->internal_strong_refs--;
966 else
967 node->local_strong_refs--;
968 if (node->local_strong_refs || node->internal_strong_refs)
969 return 0;
970 } else {
971 if (!internal)
972 node->local_weak_refs--;
973 if (node->local_weak_refs || !hlist_empty(&node->refs))
974 return 0;
975 }
976 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
977 if (list_empty(&node->work.entry)) {
978 list_add_tail(&node->work.entry, &node->proc->todo);
979 wake_up_interruptible(&node->proc->wait);
980 }
981 } else {
982 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
983 !node->local_weak_refs) {
984 list_del_init(&node->work.entry);
985 if (node->proc) {
986 rb_erase(&node->rb_node, &node->proc->nodes);
987 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530988 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900989 node->debug_id);
990 } else {
991 hlist_del(&node->dead_node);
992 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530993 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900994 node->debug_id);
995 }
996 kfree(node);
997 binder_stats_deleted(BINDER_STAT_NODE);
998 }
999 }
1000
1001 return 0;
1002}
1003
1004
1005static struct binder_ref *binder_get_ref(struct binder_proc *proc,
1006 uint32_t desc)
1007{
1008 struct rb_node *n = proc->refs_by_desc.rb_node;
1009 struct binder_ref *ref;
1010
1011 while (n) {
1012 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1013
1014 if (desc < ref->desc)
1015 n = n->rb_left;
1016 else if (desc > ref->desc)
1017 n = n->rb_right;
1018 else
1019 return ref;
1020 }
1021 return NULL;
1022}
1023
1024static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1025 struct binder_node *node)
1026{
1027 struct rb_node *n;
1028 struct rb_node **p = &proc->refs_by_node.rb_node;
1029 struct rb_node *parent = NULL;
1030 struct binder_ref *ref, *new_ref;
1031
1032 while (*p) {
1033 parent = *p;
1034 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1035
1036 if (node < ref->node)
1037 p = &(*p)->rb_left;
1038 else if (node > ref->node)
1039 p = &(*p)->rb_right;
1040 else
1041 return ref;
1042 }
1043 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1044 if (new_ref == NULL)
1045 return NULL;
1046 binder_stats_created(BINDER_STAT_REF);
1047 new_ref->debug_id = ++binder_last_id;
1048 new_ref->proc = proc;
1049 new_ref->node = node;
1050 rb_link_node(&new_ref->rb_node_node, parent, p);
1051 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1052
1053 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1054 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1055 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1056 if (ref->desc > new_ref->desc)
1057 break;
1058 new_ref->desc = ref->desc + 1;
1059 }
1060
1061 p = &proc->refs_by_desc.rb_node;
1062 while (*p) {
1063 parent = *p;
1064 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1065
1066 if (new_ref->desc < ref->desc)
1067 p = &(*p)->rb_left;
1068 else if (new_ref->desc > ref->desc)
1069 p = &(*p)->rb_right;
1070 else
1071 BUG();
1072 }
1073 rb_link_node(&new_ref->rb_node_desc, parent, p);
1074 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1075 if (node) {
1076 hlist_add_head(&new_ref->node_entry, &node->refs);
1077
1078 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301079 "%d new ref %d desc %d for node %d\n",
1080 proc->pid, new_ref->debug_id, new_ref->desc,
1081 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001082 } else {
1083 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301084 "%d new ref %d desc %d for dead node\n",
1085 proc->pid, new_ref->debug_id, new_ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001086 }
1087 return new_ref;
1088}
1089
1090static void binder_delete_ref(struct binder_ref *ref)
1091{
1092 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301093 "%d delete ref %d desc %d for node %d\n",
1094 ref->proc->pid, ref->debug_id, ref->desc,
1095 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001096
1097 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1098 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1099 if (ref->strong)
1100 binder_dec_node(ref->node, 1, 1);
1101 hlist_del(&ref->node_entry);
1102 binder_dec_node(ref->node, 0, 1);
1103 if (ref->death) {
1104 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301105 "%d delete ref %d desc %d has death notification\n",
1106 ref->proc->pid, ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001107 list_del(&ref->death->work.entry);
1108 kfree(ref->death);
1109 binder_stats_deleted(BINDER_STAT_DEATH);
1110 }
1111 kfree(ref);
1112 binder_stats_deleted(BINDER_STAT_REF);
1113}
1114
1115static int binder_inc_ref(struct binder_ref *ref, int strong,
1116 struct list_head *target_list)
1117{
1118 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001119
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001120 if (strong) {
1121 if (ref->strong == 0) {
1122 ret = binder_inc_node(ref->node, 1, 1, target_list);
1123 if (ret)
1124 return ret;
1125 }
1126 ref->strong++;
1127 } else {
1128 if (ref->weak == 0) {
1129 ret = binder_inc_node(ref->node, 0, 1, target_list);
1130 if (ret)
1131 return ret;
1132 }
1133 ref->weak++;
1134 }
1135 return 0;
1136}
1137
1138
1139static int binder_dec_ref(struct binder_ref *ref, int strong)
1140{
1141 if (strong) {
1142 if (ref->strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301143 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001144 ref->proc->pid, ref->debug_id,
1145 ref->desc, ref->strong, ref->weak);
1146 return -EINVAL;
1147 }
1148 ref->strong--;
1149 if (ref->strong == 0) {
1150 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001151
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001152 ret = binder_dec_node(ref->node, strong, 1);
1153 if (ret)
1154 return ret;
1155 }
1156 } else {
1157 if (ref->weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301158 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001159 ref->proc->pid, ref->debug_id,
1160 ref->desc, ref->strong, ref->weak);
1161 return -EINVAL;
1162 }
1163 ref->weak--;
1164 }
1165 if (ref->strong == 0 && ref->weak == 0)
1166 binder_delete_ref(ref);
1167 return 0;
1168}
1169
1170static void binder_pop_transaction(struct binder_thread *target_thread,
1171 struct binder_transaction *t)
1172{
1173 if (target_thread) {
1174 BUG_ON(target_thread->transaction_stack != t);
1175 BUG_ON(target_thread->transaction_stack->from != target_thread);
1176 target_thread->transaction_stack =
1177 target_thread->transaction_stack->from_parent;
1178 t->from = NULL;
1179 }
1180 t->need_reply = 0;
1181 if (t->buffer)
1182 t->buffer->transaction = NULL;
1183 kfree(t);
1184 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1185}
1186
1187static void binder_send_failed_reply(struct binder_transaction *t,
1188 uint32_t error_code)
1189{
1190 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001191 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001192
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001193 BUG_ON(t->flags & TF_ONE_WAY);
1194 while (1) {
1195 target_thread = t->from;
1196 if (target_thread) {
1197 if (target_thread->return_error != BR_OK &&
1198 target_thread->return_error2 == BR_OK) {
1199 target_thread->return_error2 =
1200 target_thread->return_error;
1201 target_thread->return_error = BR_OK;
1202 }
1203 if (target_thread->return_error == BR_OK) {
1204 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301205 "send failed reply for transaction %d to %d:%d\n",
William Panlener0232a422014-09-03 22:44:03 -05001206 t->debug_id,
1207 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001208 target_thread->pid);
1209
1210 binder_pop_transaction(target_thread, t);
1211 target_thread->return_error = error_code;
1212 wake_up_interruptible(&target_thread->wait);
1213 } else {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301214 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1215 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001216 target_thread->pid,
1217 target_thread->return_error);
1218 }
1219 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001220 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001221 next = t->from_parent;
1222
1223 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1224 "send failed reply for transaction %d, target dead\n",
1225 t->debug_id);
1226
1227 binder_pop_transaction(target_thread, t);
1228 if (next == NULL) {
1229 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1230 "reply failed, no target thread at root\n");
1231 return;
1232 }
1233 t = next;
1234 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1235 "reply failed, no target thread -- retry %d\n",
1236 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001237 }
1238}
1239
1240static void binder_transaction_buffer_release(struct binder_proc *proc,
1241 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001242 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001243{
Arve Hjønnevågda498892014-02-21 14:40:26 -08001244 binder_size_t *offp, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001245 int debug_id = buffer->debug_id;
1246
1247 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301248 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001249 proc->pid, buffer->debug_id,
1250 buffer->data_size, buffer->offsets_size, failed_at);
1251
1252 if (buffer->target_node)
1253 binder_dec_node(buffer->target_node, 1, 0);
1254
Arve Hjønnevågda498892014-02-21 14:40:26 -08001255 offp = (binder_size_t *)(buffer->data +
1256 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001257 if (failed_at)
1258 off_end = failed_at;
1259 else
1260 off_end = (void *)offp + buffer->offsets_size;
1261 for (; offp < off_end; offp++) {
1262 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001263
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001264 if (*offp > buffer->data_size - sizeof(*fp) ||
1265 buffer->data_size < sizeof(*fp) ||
Serban Constantinescuec35e852013-07-04 10:54:46 +01001266 !IS_ALIGNED(*offp, sizeof(u32))) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001267 pr_err("transaction release %d bad offset %lld, size %zd\n",
1268 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001269 continue;
1270 }
1271 fp = (struct flat_binder_object *)(buffer->data + *offp);
1272 switch (fp->type) {
1273 case BINDER_TYPE_BINDER:
1274 case BINDER_TYPE_WEAK_BINDER: {
1275 struct binder_node *node = binder_get_node(proc, fp->binder);
Seunghun Lee10f62862014-05-01 01:30:23 +09001276
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001277 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001278 pr_err("transaction release %d bad node %016llx\n",
1279 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001280 break;
1281 }
1282 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001283 " node %d u%016llx\n",
1284 node->debug_id, (u64)node->ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001285 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1286 } break;
1287 case BINDER_TYPE_HANDLE:
1288 case BINDER_TYPE_WEAK_HANDLE: {
1289 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
Seunghun Lee10f62862014-05-01 01:30:23 +09001290
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001291 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001292 pr_err("transaction release %d bad handle %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301293 debug_id, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001294 break;
1295 }
1296 binder_debug(BINDER_DEBUG_TRANSACTION,
1297 " ref %d desc %d (node %d)\n",
1298 ref->debug_id, ref->desc, ref->node->debug_id);
1299 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1300 } break;
1301
1302 case BINDER_TYPE_FD:
1303 binder_debug(BINDER_DEBUG_TRANSACTION,
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001304 " fd %d\n", fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001305 if (failed_at)
1306 task_close_fd(proc, fp->handle);
1307 break;
1308
1309 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001310 pr_err("transaction release %d bad object type %x\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301311 debug_id, fp->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001312 break;
1313 }
1314 }
1315}
1316
1317static void binder_transaction(struct binder_proc *proc,
1318 struct binder_thread *thread,
1319 struct binder_transaction_data *tr, int reply)
1320{
1321 struct binder_transaction *t;
1322 struct binder_work *tcomplete;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001323 binder_size_t *offp, *off_end;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001324 binder_size_t off_min;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001325 struct binder_proc *target_proc;
1326 struct binder_thread *target_thread = NULL;
1327 struct binder_node *target_node = NULL;
1328 struct list_head *target_list;
1329 wait_queue_head_t *target_wait;
1330 struct binder_transaction *in_reply_to = NULL;
1331 struct binder_transaction_log_entry *e;
1332 uint32_t return_error;
1333
1334 e = binder_transaction_log_add(&binder_transaction_log);
1335 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1336 e->from_proc = proc->pid;
1337 e->from_thread = thread->pid;
1338 e->target_handle = tr->target.handle;
1339 e->data_size = tr->data_size;
1340 e->offsets_size = tr->offsets_size;
1341
1342 if (reply) {
1343 in_reply_to = thread->transaction_stack;
1344 if (in_reply_to == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301345 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001346 proc->pid, thread->pid);
1347 return_error = BR_FAILED_REPLY;
1348 goto err_empty_call_stack;
1349 }
1350 binder_set_nice(in_reply_to->saved_priority);
1351 if (in_reply_to->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301352 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 +09001353 proc->pid, thread->pid, in_reply_to->debug_id,
1354 in_reply_to->to_proc ?
1355 in_reply_to->to_proc->pid : 0,
1356 in_reply_to->to_thread ?
1357 in_reply_to->to_thread->pid : 0);
1358 return_error = BR_FAILED_REPLY;
1359 in_reply_to = NULL;
1360 goto err_bad_call_stack;
1361 }
1362 thread->transaction_stack = in_reply_to->to_parent;
1363 target_thread = in_reply_to->from;
1364 if (target_thread == NULL) {
1365 return_error = BR_DEAD_REPLY;
1366 goto err_dead_binder;
1367 }
1368 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301369 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 +09001370 proc->pid, thread->pid,
1371 target_thread->transaction_stack ?
1372 target_thread->transaction_stack->debug_id : 0,
1373 in_reply_to->debug_id);
1374 return_error = BR_FAILED_REPLY;
1375 in_reply_to = NULL;
1376 target_thread = NULL;
1377 goto err_dead_binder;
1378 }
1379 target_proc = target_thread->proc;
1380 } else {
1381 if (tr->target.handle) {
1382 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001383
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001384 ref = binder_get_ref(proc, tr->target.handle);
1385 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301386 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001387 proc->pid, thread->pid);
1388 return_error = BR_FAILED_REPLY;
1389 goto err_invalid_target_handle;
1390 }
1391 target_node = ref->node;
1392 } else {
1393 target_node = binder_context_mgr_node;
1394 if (target_node == NULL) {
1395 return_error = BR_DEAD_REPLY;
1396 goto err_no_context_mgr_node;
1397 }
1398 }
1399 e->to_node = target_node->debug_id;
1400 target_proc = target_node->proc;
1401 if (target_proc == NULL) {
1402 return_error = BR_DEAD_REPLY;
1403 goto err_dead_binder;
1404 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001405 if (security_binder_transaction(proc->tsk,
1406 target_proc->tsk) < 0) {
1407 return_error = BR_FAILED_REPLY;
1408 goto err_invalid_target_handle;
1409 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001410 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1411 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001412
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001413 tmp = thread->transaction_stack;
1414 if (tmp->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301415 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 +09001416 proc->pid, thread->pid, tmp->debug_id,
1417 tmp->to_proc ? tmp->to_proc->pid : 0,
1418 tmp->to_thread ?
1419 tmp->to_thread->pid : 0);
1420 return_error = BR_FAILED_REPLY;
1421 goto err_bad_call_stack;
1422 }
1423 while (tmp) {
1424 if (tmp->from && tmp->from->proc == target_proc)
1425 target_thread = tmp->from;
1426 tmp = tmp->from_parent;
1427 }
1428 }
1429 }
1430 if (target_thread) {
1431 e->to_thread = target_thread->pid;
1432 target_list = &target_thread->todo;
1433 target_wait = &target_thread->wait;
1434 } else {
1435 target_list = &target_proc->todo;
1436 target_wait = &target_proc->wait;
1437 }
1438 e->to_proc = target_proc->pid;
1439
1440 /* TODO: reuse incoming transaction for reply */
1441 t = kzalloc(sizeof(*t), GFP_KERNEL);
1442 if (t == NULL) {
1443 return_error = BR_FAILED_REPLY;
1444 goto err_alloc_t_failed;
1445 }
1446 binder_stats_created(BINDER_STAT_TRANSACTION);
1447
1448 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1449 if (tcomplete == NULL) {
1450 return_error = BR_FAILED_REPLY;
1451 goto err_alloc_tcomplete_failed;
1452 }
1453 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1454
1455 t->debug_id = ++binder_last_id;
1456 e->debug_id = t->debug_id;
1457
1458 if (reply)
1459 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001460 "%d:%d BC_REPLY %d -> %d:%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_thread->pid,
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 else
1467 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001468 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001469 proc->pid, thread->pid, t->debug_id,
1470 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001471 (u64)tr->data.ptr.buffer,
1472 (u64)tr->data.ptr.offsets,
1473 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001474
1475 if (!reply && !(tr->flags & TF_ONE_WAY))
1476 t->from = thread;
1477 else
1478 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03001479 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001480 t->to_proc = target_proc;
1481 t->to_thread = target_thread;
1482 t->code = tr->code;
1483 t->flags = tr->flags;
1484 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001485
1486 trace_binder_transaction(reply, t, target_node);
1487
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001488 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1489 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1490 if (t->buffer == NULL) {
1491 return_error = BR_FAILED_REPLY;
1492 goto err_binder_alloc_buf_failed;
1493 }
1494 t->buffer->allow_user_free = 0;
1495 t->buffer->debug_id = t->debug_id;
1496 t->buffer->transaction = t;
1497 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001498 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001499 if (target_node)
1500 binder_inc_node(target_node, 1, 0, NULL);
1501
Arve Hjønnevågda498892014-02-21 14:40:26 -08001502 offp = (binder_size_t *)(t->buffer->data +
1503 ALIGN(tr->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001504
Arve Hjønnevågda498892014-02-21 14:40:26 -08001505 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1506 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301507 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1508 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001509 return_error = BR_FAILED_REPLY;
1510 goto err_copy_data_failed;
1511 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001512 if (copy_from_user(offp, (const void __user *)(uintptr_t)
1513 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301514 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1515 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001516 return_error = BR_FAILED_REPLY;
1517 goto err_copy_data_failed;
1518 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001519 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1520 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1521 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001522 return_error = BR_FAILED_REPLY;
1523 goto err_bad_offset;
1524 }
1525 off_end = (void *)offp + tr->offsets_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001526 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001527 for (; offp < off_end; offp++) {
1528 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001529
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001530 if (*offp > t->buffer->data_size - sizeof(*fp) ||
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001531 *offp < off_min ||
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001532 t->buffer->data_size < sizeof(*fp) ||
Serban Constantinescuec35e852013-07-04 10:54:46 +01001533 !IS_ALIGNED(*offp, sizeof(u32))) {
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001534 binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
1535 proc->pid, thread->pid, (u64)*offp,
1536 (u64)off_min,
1537 (u64)(t->buffer->data_size -
1538 sizeof(*fp)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001539 return_error = BR_FAILED_REPLY;
1540 goto err_bad_offset;
1541 }
1542 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001543 off_min = *offp + sizeof(struct flat_binder_object);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001544 switch (fp->type) {
1545 case BINDER_TYPE_BINDER:
1546 case BINDER_TYPE_WEAK_BINDER: {
1547 struct binder_ref *ref;
1548 struct binder_node *node = binder_get_node(proc, fp->binder);
Seunghun Lee10f62862014-05-01 01:30:23 +09001549
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001550 if (node == NULL) {
1551 node = binder_new_node(proc, fp->binder, fp->cookie);
1552 if (node == NULL) {
1553 return_error = BR_FAILED_REPLY;
1554 goto err_binder_new_node_failed;
1555 }
1556 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1557 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1558 }
1559 if (fp->cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001560 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001561 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001562 (u64)fp->binder, node->debug_id,
1563 (u64)fp->cookie, (u64)node->cookie);
Christian Engelmayer7d420432014-05-07 21:44:53 +02001564 return_error = BR_FAILED_REPLY;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001565 goto err_binder_get_ref_for_node_failed;
1566 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001567 if (security_binder_transfer_binder(proc->tsk,
1568 target_proc->tsk)) {
1569 return_error = BR_FAILED_REPLY;
1570 goto err_binder_get_ref_for_node_failed;
1571 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001572 ref = binder_get_ref_for_node(target_proc, node);
1573 if (ref == NULL) {
1574 return_error = BR_FAILED_REPLY;
1575 goto err_binder_get_ref_for_node_failed;
1576 }
1577 if (fp->type == BINDER_TYPE_BINDER)
1578 fp->type = BINDER_TYPE_HANDLE;
1579 else
1580 fp->type = BINDER_TYPE_WEAK_HANDLE;
1581 fp->handle = ref->desc;
1582 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1583 &thread->todo);
1584
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001585 trace_binder_transaction_node_to_ref(t, node, ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001586 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001587 " node %d u%016llx -> ref %d desc %d\n",
1588 node->debug_id, (u64)node->ptr,
1589 ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001590 } break;
1591 case BINDER_TYPE_HANDLE:
1592 case BINDER_TYPE_WEAK_HANDLE: {
1593 struct binder_ref *ref = binder_get_ref(proc, fp->handle);
Seunghun Lee10f62862014-05-01 01:30:23 +09001594
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001595 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001596 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301597 proc->pid,
1598 thread->pid, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001599 return_error = BR_FAILED_REPLY;
1600 goto err_binder_get_ref_failed;
1601 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001602 if (security_binder_transfer_binder(proc->tsk,
1603 target_proc->tsk)) {
1604 return_error = BR_FAILED_REPLY;
1605 goto err_binder_get_ref_failed;
1606 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001607 if (ref->node->proc == target_proc) {
1608 if (fp->type == BINDER_TYPE_HANDLE)
1609 fp->type = BINDER_TYPE_BINDER;
1610 else
1611 fp->type = BINDER_TYPE_WEAK_BINDER;
1612 fp->binder = ref->node->ptr;
1613 fp->cookie = ref->node->cookie;
1614 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001615 trace_binder_transaction_ref_to_node(t, ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001616 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001617 " ref %d desc %d -> node %d u%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001618 ref->debug_id, ref->desc, ref->node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001619 (u64)ref->node->ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001620 } else {
1621 struct binder_ref *new_ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001622
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001623 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1624 if (new_ref == NULL) {
1625 return_error = BR_FAILED_REPLY;
1626 goto err_binder_get_ref_for_node_failed;
1627 }
1628 fp->handle = new_ref->desc;
1629 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001630 trace_binder_transaction_ref_to_ref(t, ref,
1631 new_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001632 binder_debug(BINDER_DEBUG_TRANSACTION,
1633 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1634 ref->debug_id, ref->desc, new_ref->debug_id,
1635 new_ref->desc, ref->node->debug_id);
1636 }
1637 } break;
1638
1639 case BINDER_TYPE_FD: {
1640 int target_fd;
1641 struct file *file;
1642
1643 if (reply) {
1644 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001645 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 +09001646 proc->pid, thread->pid, fp->handle);
1647 return_error = BR_FAILED_REPLY;
1648 goto err_fd_not_allowed;
1649 }
1650 } else if (!target_node->accept_fds) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001651 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 +09001652 proc->pid, thread->pid, fp->handle);
1653 return_error = BR_FAILED_REPLY;
1654 goto err_fd_not_allowed;
1655 }
1656
1657 file = fget(fp->handle);
1658 if (file == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001659 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001660 proc->pid, thread->pid, fp->handle);
1661 return_error = BR_FAILED_REPLY;
1662 goto err_fget_failed;
1663 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001664 if (security_binder_transfer_file(proc->tsk,
1665 target_proc->tsk,
1666 file) < 0) {
1667 fput(file);
1668 return_error = BR_FAILED_REPLY;
1669 goto err_get_unused_fd_failed;
1670 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001671 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1672 if (target_fd < 0) {
1673 fput(file);
1674 return_error = BR_FAILED_REPLY;
1675 goto err_get_unused_fd_failed;
1676 }
1677 task_fd_install(target_proc, target_fd, file);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001678 trace_binder_transaction_fd(t, fp->handle, target_fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001679 binder_debug(BINDER_DEBUG_TRANSACTION,
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001680 " fd %d -> %d\n", fp->handle, target_fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001681 /* TODO: fput? */
1682 fp->handle = target_fd;
1683 } break;
1684
1685 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001686 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001687 proc->pid, thread->pid, fp->type);
1688 return_error = BR_FAILED_REPLY;
1689 goto err_bad_object_type;
1690 }
1691 }
1692 if (reply) {
1693 BUG_ON(t->buffer->async_transaction != 0);
1694 binder_pop_transaction(target_thread, in_reply_to);
1695 } else if (!(t->flags & TF_ONE_WAY)) {
1696 BUG_ON(t->buffer->async_transaction != 0);
1697 t->need_reply = 1;
1698 t->from_parent = thread->transaction_stack;
1699 thread->transaction_stack = t;
1700 } else {
1701 BUG_ON(target_node == NULL);
1702 BUG_ON(t->buffer->async_transaction != 1);
1703 if (target_node->has_async_transaction) {
1704 target_list = &target_node->async_todo;
1705 target_wait = NULL;
1706 } else
1707 target_node->has_async_transaction = 1;
1708 }
1709 t->work.type = BINDER_WORK_TRANSACTION;
1710 list_add_tail(&t->work.entry, target_list);
1711 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1712 list_add_tail(&tcomplete->entry, &thread->todo);
1713 if (target_wait)
1714 wake_up_interruptible(target_wait);
1715 return;
1716
1717err_get_unused_fd_failed:
1718err_fget_failed:
1719err_fd_not_allowed:
1720err_binder_get_ref_for_node_failed:
1721err_binder_get_ref_failed:
1722err_binder_new_node_failed:
1723err_bad_object_type:
1724err_bad_offset:
1725err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001726 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001727 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1728 t->buffer->transaction = NULL;
1729 binder_free_buf(target_proc, t->buffer);
1730err_binder_alloc_buf_failed:
1731 kfree(tcomplete);
1732 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1733err_alloc_tcomplete_failed:
1734 kfree(t);
1735 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1736err_alloc_t_failed:
1737err_bad_call_stack:
1738err_empty_call_stack:
1739err_dead_binder:
1740err_invalid_target_handle:
1741err_no_context_mgr_node:
1742 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001743 "%d:%d transaction failed %d, size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001744 proc->pid, thread->pid, return_error,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001745 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001746
1747 {
1748 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09001749
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001750 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1751 *fe = *e;
1752 }
1753
1754 BUG_ON(thread->return_error != BR_OK);
1755 if (in_reply_to) {
1756 thread->return_error = BR_TRANSACTION_COMPLETE;
1757 binder_send_failed_reply(in_reply_to, return_error);
1758 } else
1759 thread->return_error = return_error;
1760}
1761
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02001762static int binder_thread_write(struct binder_proc *proc,
1763 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001764 binder_uintptr_t binder_buffer, size_t size,
1765 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001766{
1767 uint32_t cmd;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001768 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001769 void __user *ptr = buffer + *consumed;
1770 void __user *end = buffer + size;
1771
1772 while (ptr < end && thread->return_error == BR_OK) {
1773 if (get_user(cmd, (uint32_t __user *)ptr))
1774 return -EFAULT;
1775 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001776 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001777 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1778 binder_stats.bc[_IOC_NR(cmd)]++;
1779 proc->stats.bc[_IOC_NR(cmd)]++;
1780 thread->stats.bc[_IOC_NR(cmd)]++;
1781 }
1782 switch (cmd) {
1783 case BC_INCREFS:
1784 case BC_ACQUIRE:
1785 case BC_RELEASE:
1786 case BC_DECREFS: {
1787 uint32_t target;
1788 struct binder_ref *ref;
1789 const char *debug_string;
1790
1791 if (get_user(target, (uint32_t __user *)ptr))
1792 return -EFAULT;
1793 ptr += sizeof(uint32_t);
1794 if (target == 0 && binder_context_mgr_node &&
1795 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1796 ref = binder_get_ref_for_node(proc,
1797 binder_context_mgr_node);
1798 if (ref->desc != target) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301799 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001800 proc->pid, thread->pid,
1801 ref->desc);
1802 }
1803 } else
1804 ref = binder_get_ref(proc, target);
1805 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301806 binder_user_error("%d:%d refcount change on invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001807 proc->pid, thread->pid, target);
1808 break;
1809 }
1810 switch (cmd) {
1811 case BC_INCREFS:
1812 debug_string = "IncRefs";
1813 binder_inc_ref(ref, 0, NULL);
1814 break;
1815 case BC_ACQUIRE:
1816 debug_string = "Acquire";
1817 binder_inc_ref(ref, 1, NULL);
1818 break;
1819 case BC_RELEASE:
1820 debug_string = "Release";
1821 binder_dec_ref(ref, 1);
1822 break;
1823 case BC_DECREFS:
1824 default:
1825 debug_string = "DecRefs";
1826 binder_dec_ref(ref, 0);
1827 break;
1828 }
1829 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301830 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001831 proc->pid, thread->pid, debug_string, ref->debug_id,
1832 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1833 break;
1834 }
1835 case BC_INCREFS_DONE:
1836 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001837 binder_uintptr_t node_ptr;
1838 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001839 struct binder_node *node;
1840
Arve Hjønnevågda498892014-02-21 14:40:26 -08001841 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001842 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001843 ptr += sizeof(binder_uintptr_t);
1844 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001845 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001846 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001847 node = binder_get_node(proc, node_ptr);
1848 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001849 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001850 proc->pid, thread->pid,
1851 cmd == BC_INCREFS_DONE ?
1852 "BC_INCREFS_DONE" :
1853 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001854 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001855 break;
1856 }
1857 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001858 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001859 proc->pid, thread->pid,
1860 cmd == BC_INCREFS_DONE ?
1861 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001862 (u64)node_ptr, node->debug_id,
1863 (u64)cookie, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001864 break;
1865 }
1866 if (cmd == BC_ACQUIRE_DONE) {
1867 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301868 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001869 proc->pid, thread->pid,
1870 node->debug_id);
1871 break;
1872 }
1873 node->pending_strong_ref = 0;
1874 } else {
1875 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301876 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001877 proc->pid, thread->pid,
1878 node->debug_id);
1879 break;
1880 }
1881 node->pending_weak_ref = 0;
1882 }
1883 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1884 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301885 "%d:%d %s node %d ls %d lw %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001886 proc->pid, thread->pid,
1887 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1888 node->debug_id, node->local_strong_refs, node->local_weak_refs);
1889 break;
1890 }
1891 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05301892 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001893 return -EINVAL;
1894 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05301895 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001896 return -EINVAL;
1897
1898 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001899 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001900 struct binder_buffer *buffer;
1901
Arve Hjønnevågda498892014-02-21 14:40:26 -08001902 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001903 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001904 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001905
1906 buffer = binder_buffer_lookup(proc, data_ptr);
1907 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001908 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
1909 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001910 break;
1911 }
1912 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001913 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
1914 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001915 break;
1916 }
1917 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001918 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
1919 proc->pid, thread->pid, (u64)data_ptr,
1920 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001921 buffer->transaction ? "active" : "finished");
1922
1923 if (buffer->transaction) {
1924 buffer->transaction->buffer = NULL;
1925 buffer->transaction = NULL;
1926 }
1927 if (buffer->async_transaction && buffer->target_node) {
1928 BUG_ON(!buffer->target_node->has_async_transaction);
1929 if (list_empty(&buffer->target_node->async_todo))
1930 buffer->target_node->has_async_transaction = 0;
1931 else
1932 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1933 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001934 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001935 binder_transaction_buffer_release(proc, buffer, NULL);
1936 binder_free_buf(proc, buffer);
1937 break;
1938 }
1939
1940 case BC_TRANSACTION:
1941 case BC_REPLY: {
1942 struct binder_transaction_data tr;
1943
1944 if (copy_from_user(&tr, ptr, sizeof(tr)))
1945 return -EFAULT;
1946 ptr += sizeof(tr);
1947 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1948 break;
1949 }
1950
1951 case BC_REGISTER_LOOPER:
1952 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301953 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001954 proc->pid, thread->pid);
1955 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1956 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301957 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001958 proc->pid, thread->pid);
1959 } else if (proc->requested_threads == 0) {
1960 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301961 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001962 proc->pid, thread->pid);
1963 } else {
1964 proc->requested_threads--;
1965 proc->requested_threads_started++;
1966 }
1967 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
1968 break;
1969 case BC_ENTER_LOOPER:
1970 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301971 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001972 proc->pid, thread->pid);
1973 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
1974 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301975 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001976 proc->pid, thread->pid);
1977 }
1978 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
1979 break;
1980 case BC_EXIT_LOOPER:
1981 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301982 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001983 proc->pid, thread->pid);
1984 thread->looper |= BINDER_LOOPER_STATE_EXITED;
1985 break;
1986
1987 case BC_REQUEST_DEATH_NOTIFICATION:
1988 case BC_CLEAR_DEATH_NOTIFICATION: {
1989 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001990 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001991 struct binder_ref *ref;
1992 struct binder_ref_death *death;
1993
1994 if (get_user(target, (uint32_t __user *)ptr))
1995 return -EFAULT;
1996 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08001997 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001998 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001999 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002000 ref = binder_get_ref(proc, target);
2001 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302002 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002003 proc->pid, thread->pid,
2004 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2005 "BC_REQUEST_DEATH_NOTIFICATION" :
2006 "BC_CLEAR_DEATH_NOTIFICATION",
2007 target);
2008 break;
2009 }
2010
2011 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002012 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002013 proc->pid, thread->pid,
2014 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2015 "BC_REQUEST_DEATH_NOTIFICATION" :
2016 "BC_CLEAR_DEATH_NOTIFICATION",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002017 (u64)cookie, ref->debug_id, ref->desc,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002018 ref->strong, ref->weak, ref->node->debug_id);
2019
2020 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2021 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302022 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002023 proc->pid, thread->pid);
2024 break;
2025 }
2026 death = kzalloc(sizeof(*death), GFP_KERNEL);
2027 if (death == NULL) {
2028 thread->return_error = BR_ERROR;
2029 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302030 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002031 proc->pid, thread->pid);
2032 break;
2033 }
2034 binder_stats_created(BINDER_STAT_DEATH);
2035 INIT_LIST_HEAD(&death->work.entry);
2036 death->cookie = cookie;
2037 ref->death = death;
2038 if (ref->node->proc == NULL) {
2039 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2040 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2041 list_add_tail(&ref->death->work.entry, &thread->todo);
2042 } else {
2043 list_add_tail(&ref->death->work.entry, &proc->todo);
2044 wake_up_interruptible(&proc->wait);
2045 }
2046 }
2047 } else {
2048 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302049 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002050 proc->pid, thread->pid);
2051 break;
2052 }
2053 death = ref->death;
2054 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002055 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002056 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002057 (u64)death->cookie,
2058 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002059 break;
2060 }
2061 ref->death = NULL;
2062 if (list_empty(&death->work.entry)) {
2063 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2064 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2065 list_add_tail(&death->work.entry, &thread->todo);
2066 } else {
2067 list_add_tail(&death->work.entry, &proc->todo);
2068 wake_up_interruptible(&proc->wait);
2069 }
2070 } else {
2071 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2072 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2073 }
2074 }
2075 } break;
2076 case BC_DEAD_BINDER_DONE: {
2077 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002078 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002079 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09002080
Arve Hjønnevågda498892014-02-21 14:40:26 -08002081 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002082 return -EFAULT;
2083
Lisa Du7a64cd82016-02-17 09:32:52 +08002084 ptr += sizeof(cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002085 list_for_each_entry(w, &proc->delivered_death, entry) {
2086 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
Seunghun Lee10f62862014-05-01 01:30:23 +09002087
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002088 if (tmp_death->cookie == cookie) {
2089 death = tmp_death;
2090 break;
2091 }
2092 }
2093 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002094 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2095 proc->pid, thread->pid, (u64)cookie,
2096 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002097 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002098 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2099 proc->pid, thread->pid, (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002100 break;
2101 }
2102
2103 list_del_init(&death->work.entry);
2104 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2105 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2106 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2107 list_add_tail(&death->work.entry, &thread->todo);
2108 } else {
2109 list_add_tail(&death->work.entry, &proc->todo);
2110 wake_up_interruptible(&proc->wait);
2111 }
2112 }
2113 } break;
2114
2115 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302116 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002117 proc->pid, thread->pid, cmd);
2118 return -EINVAL;
2119 }
2120 *consumed = ptr - buffer;
2121 }
2122 return 0;
2123}
2124
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002125static void binder_stat_br(struct binder_proc *proc,
2126 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002127{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002128 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002129 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2130 binder_stats.br[_IOC_NR(cmd)]++;
2131 proc->stats.br[_IOC_NR(cmd)]++;
2132 thread->stats.br[_IOC_NR(cmd)]++;
2133 }
2134}
2135
2136static int binder_has_proc_work(struct binder_proc *proc,
2137 struct binder_thread *thread)
2138{
2139 return !list_empty(&proc->todo) ||
2140 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2141}
2142
2143static int binder_has_thread_work(struct binder_thread *thread)
2144{
2145 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2146 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2147}
2148
2149static int binder_thread_read(struct binder_proc *proc,
2150 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002151 binder_uintptr_t binder_buffer, size_t size,
2152 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002153{
Arve Hjønnevågda498892014-02-21 14:40:26 -08002154 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002155 void __user *ptr = buffer + *consumed;
2156 void __user *end = buffer + size;
2157
2158 int ret = 0;
2159 int wait_for_proc_work;
2160
2161 if (*consumed == 0) {
2162 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2163 return -EFAULT;
2164 ptr += sizeof(uint32_t);
2165 }
2166
2167retry:
2168 wait_for_proc_work = thread->transaction_stack == NULL &&
2169 list_empty(&thread->todo);
2170
2171 if (thread->return_error != BR_OK && ptr < end) {
2172 if (thread->return_error2 != BR_OK) {
2173 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2174 return -EFAULT;
2175 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002176 binder_stat_br(proc, thread, thread->return_error2);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002177 if (ptr == end)
2178 goto done;
2179 thread->return_error2 = BR_OK;
2180 }
2181 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2182 return -EFAULT;
2183 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002184 binder_stat_br(proc, thread, thread->return_error);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002185 thread->return_error = BR_OK;
2186 goto done;
2187 }
2188
2189
2190 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2191 if (wait_for_proc_work)
2192 proc->ready_threads++;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002193
2194 binder_unlock(__func__);
2195
2196 trace_binder_wait_for_work(wait_for_proc_work,
2197 !!thread->transaction_stack,
2198 !list_empty(&thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002199 if (wait_for_proc_work) {
2200 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2201 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302202 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 +09002203 proc->pid, thread->pid, thread->looper);
2204 wait_event_interruptible(binder_user_error_wait,
2205 binder_stop_on_user_error < 2);
2206 }
2207 binder_set_nice(proc->default_priority);
2208 if (non_block) {
2209 if (!binder_has_proc_work(proc, thread))
2210 ret = -EAGAIN;
2211 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002212 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002213 } else {
2214 if (non_block) {
2215 if (!binder_has_thread_work(thread))
2216 ret = -EAGAIN;
2217 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002218 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002219 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002220
2221 binder_lock(__func__);
2222
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002223 if (wait_for_proc_work)
2224 proc->ready_threads--;
2225 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2226
2227 if (ret)
2228 return ret;
2229
2230 while (1) {
2231 uint32_t cmd;
2232 struct binder_transaction_data tr;
2233 struct binder_work *w;
2234 struct binder_transaction *t = NULL;
2235
Dmitry Voytik395262a2014-09-08 18:16:34 +04002236 if (!list_empty(&thread->todo)) {
2237 w = list_first_entry(&thread->todo, struct binder_work,
2238 entry);
2239 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2240 w = list_first_entry(&proc->todo, struct binder_work,
2241 entry);
2242 } else {
2243 /* no data added */
2244 if (ptr - buffer == 4 &&
2245 !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002246 goto retry;
2247 break;
2248 }
2249
2250 if (end - ptr < sizeof(tr) + 4)
2251 break;
2252
2253 switch (w->type) {
2254 case BINDER_WORK_TRANSACTION: {
2255 t = container_of(w, struct binder_transaction, work);
2256 } break;
2257 case BINDER_WORK_TRANSACTION_COMPLETE: {
2258 cmd = BR_TRANSACTION_COMPLETE;
2259 if (put_user(cmd, (uint32_t __user *)ptr))
2260 return -EFAULT;
2261 ptr += sizeof(uint32_t);
2262
2263 binder_stat_br(proc, thread, cmd);
2264 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302265 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002266 proc->pid, thread->pid);
2267
2268 list_del(&w->entry);
2269 kfree(w);
2270 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2271 } break;
2272 case BINDER_WORK_NODE: {
2273 struct binder_node *node = container_of(w, struct binder_node, work);
2274 uint32_t cmd = BR_NOOP;
2275 const char *cmd_name;
2276 int strong = node->internal_strong_refs || node->local_strong_refs;
2277 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
Seunghun Lee10f62862014-05-01 01:30:23 +09002278
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002279 if (weak && !node->has_weak_ref) {
2280 cmd = BR_INCREFS;
2281 cmd_name = "BR_INCREFS";
2282 node->has_weak_ref = 1;
2283 node->pending_weak_ref = 1;
2284 node->local_weak_refs++;
2285 } else if (strong && !node->has_strong_ref) {
2286 cmd = BR_ACQUIRE;
2287 cmd_name = "BR_ACQUIRE";
2288 node->has_strong_ref = 1;
2289 node->pending_strong_ref = 1;
2290 node->local_strong_refs++;
2291 } else if (!strong && node->has_strong_ref) {
2292 cmd = BR_RELEASE;
2293 cmd_name = "BR_RELEASE";
2294 node->has_strong_ref = 0;
2295 } else if (!weak && node->has_weak_ref) {
2296 cmd = BR_DECREFS;
2297 cmd_name = "BR_DECREFS";
2298 node->has_weak_ref = 0;
2299 }
2300 if (cmd != BR_NOOP) {
2301 if (put_user(cmd, (uint32_t __user *)ptr))
2302 return -EFAULT;
2303 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002304 if (put_user(node->ptr,
2305 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002306 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002307 ptr += sizeof(binder_uintptr_t);
2308 if (put_user(node->cookie,
2309 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002310 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002311 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002312
2313 binder_stat_br(proc, thread, cmd);
2314 binder_debug(BINDER_DEBUG_USER_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002315 "%d:%d %s %d u%016llx c%016llx\n",
2316 proc->pid, thread->pid, cmd_name,
2317 node->debug_id,
2318 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002319 } else {
2320 list_del_init(&w->entry);
2321 if (!weak && !strong) {
2322 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002323 "%d:%d node %d u%016llx c%016llx deleted\n",
2324 proc->pid, thread->pid,
2325 node->debug_id,
2326 (u64)node->ptr,
2327 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002328 rb_erase(&node->rb_node, &proc->nodes);
2329 kfree(node);
2330 binder_stats_deleted(BINDER_STAT_NODE);
2331 } else {
2332 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002333 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2334 proc->pid, thread->pid,
2335 node->debug_id,
2336 (u64)node->ptr,
2337 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002338 }
2339 }
2340 } break;
2341 case BINDER_WORK_DEAD_BINDER:
2342 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2343 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2344 struct binder_ref_death *death;
2345 uint32_t cmd;
2346
2347 death = container_of(w, struct binder_ref_death, work);
2348 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2349 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2350 else
2351 cmd = BR_DEAD_BINDER;
2352 if (put_user(cmd, (uint32_t __user *)ptr))
2353 return -EFAULT;
2354 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002355 if (put_user(death->cookie,
2356 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002357 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002358 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002359 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002360 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002361 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002362 proc->pid, thread->pid,
2363 cmd == BR_DEAD_BINDER ?
2364 "BR_DEAD_BINDER" :
2365 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002366 (u64)death->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002367
2368 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2369 list_del(&w->entry);
2370 kfree(death);
2371 binder_stats_deleted(BINDER_STAT_DEATH);
2372 } else
2373 list_move(&w->entry, &proc->delivered_death);
2374 if (cmd == BR_DEAD_BINDER)
2375 goto done; /* DEAD_BINDER notifications can cause transactions */
2376 } break;
2377 }
2378
2379 if (!t)
2380 continue;
2381
2382 BUG_ON(t->buffer == NULL);
2383 if (t->buffer->target_node) {
2384 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002385
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002386 tr.target.ptr = target_node->ptr;
2387 tr.cookie = target_node->cookie;
2388 t->saved_priority = task_nice(current);
2389 if (t->priority < target_node->min_priority &&
2390 !(t->flags & TF_ONE_WAY))
2391 binder_set_nice(t->priority);
2392 else if (!(t->flags & TF_ONE_WAY) ||
2393 t->saved_priority > target_node->min_priority)
2394 binder_set_nice(target_node->min_priority);
2395 cmd = BR_TRANSACTION;
2396 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002397 tr.target.ptr = 0;
2398 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002399 cmd = BR_REPLY;
2400 }
2401 tr.code = t->code;
2402 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002403 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002404
2405 if (t->from) {
2406 struct task_struct *sender = t->from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09002407
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002408 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002409 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002410 } else {
2411 tr.sender_pid = 0;
2412 }
2413
2414 tr.data_size = t->buffer->data_size;
2415 tr.offsets_size = t->buffer->offsets_size;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002416 tr.data.ptr.buffer = (binder_uintptr_t)(
2417 (uintptr_t)t->buffer->data +
2418 proc->user_buffer_offset);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002419 tr.data.ptr.offsets = tr.data.ptr.buffer +
2420 ALIGN(t->buffer->data_size,
2421 sizeof(void *));
2422
2423 if (put_user(cmd, (uint32_t __user *)ptr))
2424 return -EFAULT;
2425 ptr += sizeof(uint32_t);
2426 if (copy_to_user(ptr, &tr, sizeof(tr)))
2427 return -EFAULT;
2428 ptr += sizeof(tr);
2429
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002430 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002431 binder_stat_br(proc, thread, cmd);
2432 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002433 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002434 proc->pid, thread->pid,
2435 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2436 "BR_REPLY",
2437 t->debug_id, t->from ? t->from->proc->pid : 0,
2438 t->from ? t->from->pid : 0, cmd,
2439 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002440 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002441
2442 list_del(&t->work.entry);
2443 t->buffer->allow_user_free = 1;
2444 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2445 t->to_parent = thread->transaction_stack;
2446 t->to_thread = thread;
2447 thread->transaction_stack = t;
2448 } else {
2449 t->buffer->transaction = NULL;
2450 kfree(t);
2451 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2452 }
2453 break;
2454 }
2455
2456done:
2457
2458 *consumed = ptr - buffer;
2459 if (proc->requested_threads + proc->ready_threads == 0 &&
2460 proc->requested_threads_started < proc->max_threads &&
2461 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2462 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2463 /*spawn a new thread if we leave this out */) {
2464 proc->requested_threads++;
2465 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302466 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002467 proc->pid, thread->pid);
2468 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2469 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002470 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002471 }
2472 return 0;
2473}
2474
2475static void binder_release_work(struct list_head *list)
2476{
2477 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09002478
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002479 while (!list_empty(list)) {
2480 w = list_first_entry(list, struct binder_work, entry);
2481 list_del_init(&w->entry);
2482 switch (w->type) {
2483 case BINDER_WORK_TRANSACTION: {
2484 struct binder_transaction *t;
2485
2486 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002487 if (t->buffer->target_node &&
2488 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002489 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002490 } else {
2491 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302492 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002493 t->debug_id);
2494 t->buffer->transaction = NULL;
2495 kfree(t);
2496 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2497 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002498 } break;
2499 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002500 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302501 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002502 kfree(w);
2503 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2504 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002505 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2506 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2507 struct binder_ref_death *death;
2508
2509 death = container_of(w, struct binder_ref_death, work);
2510 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002511 "undelivered death notification, %016llx\n",
2512 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002513 kfree(death);
2514 binder_stats_deleted(BINDER_STAT_DEATH);
2515 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002516 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302517 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002518 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002519 break;
2520 }
2521 }
2522
2523}
2524
2525static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2526{
2527 struct binder_thread *thread = NULL;
2528 struct rb_node *parent = NULL;
2529 struct rb_node **p = &proc->threads.rb_node;
2530
2531 while (*p) {
2532 parent = *p;
2533 thread = rb_entry(parent, struct binder_thread, rb_node);
2534
2535 if (current->pid < thread->pid)
2536 p = &(*p)->rb_left;
2537 else if (current->pid > thread->pid)
2538 p = &(*p)->rb_right;
2539 else
2540 break;
2541 }
2542 if (*p == NULL) {
2543 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2544 if (thread == NULL)
2545 return NULL;
2546 binder_stats_created(BINDER_STAT_THREAD);
2547 thread->proc = proc;
2548 thread->pid = current->pid;
2549 init_waitqueue_head(&thread->wait);
2550 INIT_LIST_HEAD(&thread->todo);
2551 rb_link_node(&thread->rb_node, parent, p);
2552 rb_insert_color(&thread->rb_node, &proc->threads);
2553 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2554 thread->return_error = BR_OK;
2555 thread->return_error2 = BR_OK;
2556 }
2557 return thread;
2558}
2559
2560static int binder_free_thread(struct binder_proc *proc,
2561 struct binder_thread *thread)
2562{
2563 struct binder_transaction *t;
2564 struct binder_transaction *send_reply = NULL;
2565 int active_transactions = 0;
2566
2567 rb_erase(&thread->rb_node, &proc->threads);
2568 t = thread->transaction_stack;
2569 if (t && t->to_thread == thread)
2570 send_reply = t;
2571 while (t) {
2572 active_transactions++;
2573 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302574 "release %d:%d transaction %d %s, still active\n",
2575 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002576 t->debug_id,
2577 (t->to_thread == thread) ? "in" : "out");
2578
2579 if (t->to_thread == thread) {
2580 t->to_proc = NULL;
2581 t->to_thread = NULL;
2582 if (t->buffer) {
2583 t->buffer->transaction = NULL;
2584 t->buffer = NULL;
2585 }
2586 t = t->to_parent;
2587 } else if (t->from == thread) {
2588 t->from = NULL;
2589 t = t->from_parent;
2590 } else
2591 BUG();
2592 }
2593 if (send_reply)
2594 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2595 binder_release_work(&thread->todo);
2596 kfree(thread);
2597 binder_stats_deleted(BINDER_STAT_THREAD);
2598 return active_transactions;
2599}
2600
2601static unsigned int binder_poll(struct file *filp,
2602 struct poll_table_struct *wait)
2603{
2604 struct binder_proc *proc = filp->private_data;
2605 struct binder_thread *thread = NULL;
2606 int wait_for_proc_work;
2607
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002608 binder_lock(__func__);
2609
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002610 thread = binder_get_thread(proc);
2611
2612 wait_for_proc_work = thread->transaction_stack == NULL &&
2613 list_empty(&thread->todo) && thread->return_error == BR_OK;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002614
2615 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002616
2617 if (wait_for_proc_work) {
2618 if (binder_has_proc_work(proc, thread))
2619 return POLLIN;
2620 poll_wait(filp, &proc->wait, wait);
2621 if (binder_has_proc_work(proc, thread))
2622 return POLLIN;
2623 } else {
2624 if (binder_has_thread_work(thread))
2625 return POLLIN;
2626 poll_wait(filp, &thread->wait, wait);
2627 if (binder_has_thread_work(thread))
2628 return POLLIN;
2629 }
2630 return 0;
2631}
2632
Tair Rzayev78260ac2014-06-03 22:27:21 +03002633static int binder_ioctl_write_read(struct file *filp,
2634 unsigned int cmd, unsigned long arg,
2635 struct binder_thread *thread)
2636{
2637 int ret = 0;
2638 struct binder_proc *proc = filp->private_data;
2639 unsigned int size = _IOC_SIZE(cmd);
2640 void __user *ubuf = (void __user *)arg;
2641 struct binder_write_read bwr;
2642
2643 if (size != sizeof(struct binder_write_read)) {
2644 ret = -EINVAL;
2645 goto out;
2646 }
2647 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2648 ret = -EFAULT;
2649 goto out;
2650 }
2651 binder_debug(BINDER_DEBUG_READ_WRITE,
2652 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2653 proc->pid, thread->pid,
2654 (u64)bwr.write_size, (u64)bwr.write_buffer,
2655 (u64)bwr.read_size, (u64)bwr.read_buffer);
2656
2657 if (bwr.write_size > 0) {
2658 ret = binder_thread_write(proc, thread,
2659 bwr.write_buffer,
2660 bwr.write_size,
2661 &bwr.write_consumed);
2662 trace_binder_write_done(ret);
2663 if (ret < 0) {
2664 bwr.read_consumed = 0;
2665 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2666 ret = -EFAULT;
2667 goto out;
2668 }
2669 }
2670 if (bwr.read_size > 0) {
2671 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2672 bwr.read_size,
2673 &bwr.read_consumed,
2674 filp->f_flags & O_NONBLOCK);
2675 trace_binder_read_done(ret);
2676 if (!list_empty(&proc->todo))
2677 wake_up_interruptible(&proc->wait);
2678 if (ret < 0) {
2679 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2680 ret = -EFAULT;
2681 goto out;
2682 }
2683 }
2684 binder_debug(BINDER_DEBUG_READ_WRITE,
2685 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2686 proc->pid, thread->pid,
2687 (u64)bwr.write_consumed, (u64)bwr.write_size,
2688 (u64)bwr.read_consumed, (u64)bwr.read_size);
2689 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2690 ret = -EFAULT;
2691 goto out;
2692 }
2693out:
2694 return ret;
2695}
2696
2697static int binder_ioctl_set_ctx_mgr(struct file *filp)
2698{
2699 int ret = 0;
2700 struct binder_proc *proc = filp->private_data;
2701 kuid_t curr_euid = current_euid();
2702
2703 if (binder_context_mgr_node != NULL) {
2704 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2705 ret = -EBUSY;
2706 goto out;
2707 }
Stephen Smalley79af7302015-01-21 10:54:10 -05002708 ret = security_binder_set_context_mgr(proc->tsk);
2709 if (ret < 0)
2710 goto out;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002711 if (uid_valid(binder_context_mgr_uid)) {
2712 if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
2713 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2714 from_kuid(&init_user_ns, curr_euid),
2715 from_kuid(&init_user_ns,
2716 binder_context_mgr_uid));
2717 ret = -EPERM;
2718 goto out;
2719 }
2720 } else {
2721 binder_context_mgr_uid = curr_euid;
2722 }
2723 binder_context_mgr_node = binder_new_node(proc, 0, 0);
2724 if (binder_context_mgr_node == NULL) {
2725 ret = -ENOMEM;
2726 goto out;
2727 }
2728 binder_context_mgr_node->local_weak_refs++;
2729 binder_context_mgr_node->local_strong_refs++;
2730 binder_context_mgr_node->has_strong_ref = 1;
2731 binder_context_mgr_node->has_weak_ref = 1;
2732out:
2733 return ret;
2734}
2735
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002736static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2737{
2738 int ret;
2739 struct binder_proc *proc = filp->private_data;
2740 struct binder_thread *thread;
2741 unsigned int size = _IOC_SIZE(cmd);
2742 void __user *ubuf = (void __user *)arg;
2743
Tair Rzayev78260ac2014-06-03 22:27:21 +03002744 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2745 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002746
Chen Fenga906d692016-02-01 14:04:02 +08002747 if (unlikely(current->mm != proc->vma_vm_mm)) {
2748 pr_err("current mm mismatch proc mm\n");
2749 return -EINVAL;
2750 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002751 trace_binder_ioctl(cmd, arg);
2752
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002753 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2754 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002755 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002756
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002757 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002758 thread = binder_get_thread(proc);
2759 if (thread == NULL) {
2760 ret = -ENOMEM;
2761 goto err;
2762 }
2763
2764 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002765 case BINDER_WRITE_READ:
2766 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
2767 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002768 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002769 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002770 case BINDER_SET_MAX_THREADS:
2771 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2772 ret = -EINVAL;
2773 goto err;
2774 }
2775 break;
2776 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03002777 ret = binder_ioctl_set_ctx_mgr(filp);
2778 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002779 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002780 break;
2781 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302782 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002783 proc->pid, thread->pid);
2784 binder_free_thread(proc, thread);
2785 thread = NULL;
2786 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002787 case BINDER_VERSION: {
2788 struct binder_version __user *ver = ubuf;
2789
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002790 if (size != sizeof(struct binder_version)) {
2791 ret = -EINVAL;
2792 goto err;
2793 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02002794 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2795 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002796 ret = -EINVAL;
2797 goto err;
2798 }
2799 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002800 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002801 default:
2802 ret = -EINVAL;
2803 goto err;
2804 }
2805 ret = 0;
2806err:
2807 if (thread)
2808 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002809 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002810 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2811 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05302812 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 -07002813err_unlocked:
2814 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002815 return ret;
2816}
2817
2818static void binder_vma_open(struct vm_area_struct *vma)
2819{
2820 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002821
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002822 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302823 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002824 proc->pid, vma->vm_start, vma->vm_end,
2825 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2826 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002827}
2828
2829static void binder_vma_close(struct vm_area_struct *vma)
2830{
2831 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002832
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002833 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302834 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002835 proc->pid, vma->vm_start, vma->vm_end,
2836 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2837 (unsigned long)pgprot_val(vma->vm_page_prot));
2838 proc->vma = NULL;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08002839 proc->vma_vm_mm = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002840 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2841}
2842
Vinayak Menonddac7d52014-06-02 18:17:59 +05302843static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2844{
2845 return VM_FAULT_SIGBUS;
2846}
2847
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07002848static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002849 .open = binder_vma_open,
2850 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05302851 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002852};
2853
2854static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2855{
2856 int ret;
2857 struct vm_struct *area;
2858 struct binder_proc *proc = filp->private_data;
2859 const char *failure_string;
2860 struct binder_buffer *buffer;
2861
Al Viroa79f41e2012-08-15 18:23:36 -04002862 if (proc->tsk != current)
2863 return -EINVAL;
2864
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002865 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2866 vma->vm_end = vma->vm_start + SZ_4M;
2867
2868 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2869 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2870 proc->pid, vma->vm_start, vma->vm_end,
2871 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2872 (unsigned long)pgprot_val(vma->vm_page_prot));
2873
2874 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2875 ret = -EPERM;
2876 failure_string = "bad vm_flags";
2877 goto err_bad_arg;
2878 }
2879 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2880
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002881 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002882 if (proc->buffer) {
2883 ret = -EBUSY;
2884 failure_string = "already mapped";
2885 goto err_already_mapped;
2886 }
2887
2888 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2889 if (area == NULL) {
2890 ret = -ENOMEM;
2891 failure_string = "get_vm_area";
2892 goto err_get_vm_area_failed;
2893 }
2894 proc->buffer = area->addr;
2895 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002896 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002897
2898#ifdef CONFIG_CPU_CACHE_VIPT
2899 if (cache_is_vipt_aliasing()) {
2900 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
Sherwin Soltani258767f2012-06-26 02:00:30 -04002901 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 +09002902 vma->vm_start += PAGE_SIZE;
2903 }
2904 }
2905#endif
2906 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2907 if (proc->pages == NULL) {
2908 ret = -ENOMEM;
2909 failure_string = "alloc page array";
2910 goto err_alloc_pages_failed;
2911 }
2912 proc->buffer_size = vma->vm_end - vma->vm_start;
2913
2914 vma->vm_ops = &binder_vm_ops;
2915 vma->vm_private_data = proc;
2916
2917 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2918 ret = -ENOMEM;
2919 failure_string = "alloc small buf";
2920 goto err_alloc_small_buf_failed;
2921 }
2922 buffer = proc->buffer;
2923 INIT_LIST_HEAD(&proc->buffers);
2924 list_add(&buffer->entry, &proc->buffers);
2925 buffer->free = 1;
2926 binder_insert_free_buffer(proc, buffer);
2927 proc->free_async_space = proc->buffer_size / 2;
2928 barrier();
Al Viroa79f41e2012-08-15 18:23:36 -04002929 proc->files = get_files_struct(current);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002930 proc->vma = vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08002931 proc->vma_vm_mm = vma->vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002932
Sherwin Soltani258767f2012-06-26 02:00:30 -04002933 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002934 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2935 return 0;
2936
2937err_alloc_small_buf_failed:
2938 kfree(proc->pages);
2939 proc->pages = NULL;
2940err_alloc_pages_failed:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002941 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002942 vfree(proc->buffer);
2943 proc->buffer = NULL;
2944err_get_vm_area_failed:
2945err_already_mapped:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002946 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002947err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04002948 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002949 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2950 return ret;
2951}
2952
2953static int binder_open(struct inode *nodp, struct file *filp)
2954{
2955 struct binder_proc *proc;
2956
2957 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
2958 current->group_leader->pid, current->pid);
2959
2960 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2961 if (proc == NULL)
2962 return -ENOMEM;
2963 get_task_struct(current);
2964 proc->tsk = current;
Chen Fenga906d692016-02-01 14:04:02 +08002965 proc->vma_vm_mm = current->mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002966 INIT_LIST_HEAD(&proc->todo);
2967 init_waitqueue_head(&proc->wait);
2968 proc->default_priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002969
2970 binder_lock(__func__);
2971
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002972 binder_stats_created(BINDER_STAT_PROC);
2973 hlist_add_head(&proc->proc_node, &binder_procs);
2974 proc->pid = current->group_leader->pid;
2975 INIT_LIST_HEAD(&proc->delivered_death);
2976 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002977
2978 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002979
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07002980 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002981 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09002982
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002983 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07002984 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
2985 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002986 }
2987
2988 return 0;
2989}
2990
2991static int binder_flush(struct file *filp, fl_owner_t id)
2992{
2993 struct binder_proc *proc = filp->private_data;
2994
2995 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
2996
2997 return 0;
2998}
2999
3000static void binder_deferred_flush(struct binder_proc *proc)
3001{
3002 struct rb_node *n;
3003 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09003004
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003005 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3006 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09003007
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003008 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3009 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3010 wake_up_interruptible(&thread->wait);
3011 wake_count++;
3012 }
3013 }
3014 wake_up_interruptible_all(&proc->wait);
3015
3016 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3017 "binder_flush: %d woke %d threads\n", proc->pid,
3018 wake_count);
3019}
3020
3021static int binder_release(struct inode *nodp, struct file *filp)
3022{
3023 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003024
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003025 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003026 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3027
3028 return 0;
3029}
3030
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003031static int binder_node_release(struct binder_node *node, int refs)
3032{
3033 struct binder_ref *ref;
3034 int death = 0;
3035
3036 list_del_init(&node->work.entry);
3037 binder_release_work(&node->async_todo);
3038
3039 if (hlist_empty(&node->refs)) {
3040 kfree(node);
3041 binder_stats_deleted(BINDER_STAT_NODE);
3042
3043 return refs;
3044 }
3045
3046 node->proc = NULL;
3047 node->local_strong_refs = 0;
3048 node->local_weak_refs = 0;
3049 hlist_add_head(&node->dead_node, &binder_dead_nodes);
3050
3051 hlist_for_each_entry(ref, &node->refs, node_entry) {
3052 refs++;
3053
3054 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08003055 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003056
3057 death++;
3058
3059 if (list_empty(&ref->death->work.entry)) {
3060 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3061 list_add_tail(&ref->death->work.entry,
3062 &ref->proc->todo);
3063 wake_up_interruptible(&ref->proc->wait);
3064 } else
3065 BUG();
3066 }
3067
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003068 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3069 "node %d now dead, refs %d, death %d\n",
3070 node->debug_id, refs, death);
3071
3072 return refs;
3073}
3074
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003075static void binder_deferred_release(struct binder_proc *proc)
3076{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003077 struct binder_transaction *t;
3078 struct rb_node *n;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003079 int threads, nodes, incoming_refs, outgoing_refs, buffers,
3080 active_transactions, page_count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003081
3082 BUG_ON(proc->vma);
3083 BUG_ON(proc->files);
3084
3085 hlist_del(&proc->proc_node);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003086
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003087 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
3088 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003089 "%s: %d context_mgr_node gone\n",
3090 __func__, proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003091 binder_context_mgr_node = NULL;
3092 }
3093
3094 threads = 0;
3095 active_transactions = 0;
3096 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003097 struct binder_thread *thread;
3098
3099 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003100 threads++;
3101 active_transactions += binder_free_thread(proc, thread);
3102 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003103
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003104 nodes = 0;
3105 incoming_refs = 0;
3106 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003107 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003108
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003109 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003110 nodes++;
3111 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003112 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003113 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003114
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003115 outgoing_refs = 0;
3116 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003117 struct binder_ref *ref;
3118
3119 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003120 outgoing_refs++;
3121 binder_delete_ref(ref);
3122 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003123
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003124 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003125 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003126
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003127 buffers = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003128 while ((n = rb_first(&proc->allocated_buffers))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003129 struct binder_buffer *buffer;
3130
3131 buffer = rb_entry(n, struct binder_buffer, rb_node);
3132
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003133 t = buffer->transaction;
3134 if (t) {
3135 t->buffer = NULL;
3136 buffer->transaction = NULL;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303137 pr_err("release proc %d, transaction %d, not freed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003138 proc->pid, t->debug_id);
3139 /*BUG();*/
3140 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003141
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003142 binder_free_buf(proc, buffer);
3143 buffers++;
3144 }
3145
3146 binder_stats_deleted(BINDER_STAT_PROC);
3147
3148 page_count = 0;
3149 if (proc->pages) {
3150 int i;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003151
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003152 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003153 void *page_addr;
3154
3155 if (!proc->pages[i])
3156 continue;
3157
3158 page_addr = proc->buffer + i * PAGE_SIZE;
3159 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003160 "%s: %d: page %d at %p not freed\n",
3161 __func__, proc->pid, i, page_addr);
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003162 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3163 __free_page(proc->pages[i]);
3164 page_count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003165 }
3166 kfree(proc->pages);
3167 vfree(proc->buffer);
3168 }
3169
3170 put_task_struct(proc->tsk);
3171
3172 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003173 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3174 __func__, proc->pid, threads, nodes, incoming_refs,
3175 outgoing_refs, active_transactions, buffers, page_count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003176
3177 kfree(proc);
3178}
3179
3180static void binder_deferred_func(struct work_struct *work)
3181{
3182 struct binder_proc *proc;
3183 struct files_struct *files;
3184
3185 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003186
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003187 do {
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003188 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003189 mutex_lock(&binder_deferred_lock);
3190 if (!hlist_empty(&binder_deferred_list)) {
3191 proc = hlist_entry(binder_deferred_list.first,
3192 struct binder_proc, deferred_work_node);
3193 hlist_del_init(&proc->deferred_work_node);
3194 defer = proc->deferred_work;
3195 proc->deferred_work = 0;
3196 } else {
3197 proc = NULL;
3198 defer = 0;
3199 }
3200 mutex_unlock(&binder_deferred_lock);
3201
3202 files = NULL;
3203 if (defer & BINDER_DEFERRED_PUT_FILES) {
3204 files = proc->files;
3205 if (files)
3206 proc->files = NULL;
3207 }
3208
3209 if (defer & BINDER_DEFERRED_FLUSH)
3210 binder_deferred_flush(proc);
3211
3212 if (defer & BINDER_DEFERRED_RELEASE)
3213 binder_deferred_release(proc); /* frees proc */
3214
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003215 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003216 if (files)
3217 put_files_struct(files);
3218 } while (proc);
3219}
3220static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3221
3222static void
3223binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3224{
3225 mutex_lock(&binder_deferred_lock);
3226 proc->deferred_work |= defer;
3227 if (hlist_unhashed(&proc->deferred_work_node)) {
3228 hlist_add_head(&proc->deferred_work_node,
3229 &binder_deferred_list);
Arve Hjønnevåg3c762a42010-04-22 15:53:23 -07003230 queue_work(binder_deferred_workqueue, &binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003231 }
3232 mutex_unlock(&binder_deferred_lock);
3233}
3234
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003235static void print_binder_transaction(struct seq_file *m, const char *prefix,
3236 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003237{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003238 seq_printf(m,
3239 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3240 prefix, t->debug_id, t,
3241 t->from ? t->from->proc->pid : 0,
3242 t->from ? t->from->pid : 0,
3243 t->to_proc ? t->to_proc->pid : 0,
3244 t->to_thread ? t->to_thread->pid : 0,
3245 t->code, t->flags, t->priority, t->need_reply);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003246 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003247 seq_puts(m, " buffer free\n");
3248 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003249 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003250 if (t->buffer->target_node)
3251 seq_printf(m, " node %d",
3252 t->buffer->target_node->debug_id);
3253 seq_printf(m, " size %zd:%zd data %p\n",
3254 t->buffer->data_size, t->buffer->offsets_size,
3255 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003256}
3257
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003258static void print_binder_buffer(struct seq_file *m, const char *prefix,
3259 struct binder_buffer *buffer)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003260{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003261 seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
3262 prefix, buffer->debug_id, buffer->data,
3263 buffer->data_size, buffer->offsets_size,
3264 buffer->transaction ? "active" : "delivered");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003265}
3266
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003267static void print_binder_work(struct seq_file *m, const char *prefix,
3268 const char *transaction_prefix,
3269 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003270{
3271 struct binder_node *node;
3272 struct binder_transaction *t;
3273
3274 switch (w->type) {
3275 case BINDER_WORK_TRANSACTION:
3276 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003277 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003278 break;
3279 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003280 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003281 break;
3282 case BINDER_WORK_NODE:
3283 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003284 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3285 prefix, node->debug_id,
3286 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003287 break;
3288 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003289 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003290 break;
3291 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003292 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003293 break;
3294 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003295 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003296 break;
3297 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003298 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003299 break;
3300 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003301}
3302
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003303static void print_binder_thread(struct seq_file *m,
3304 struct binder_thread *thread,
3305 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003306{
3307 struct binder_transaction *t;
3308 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003309 size_t start_pos = m->count;
3310 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003311
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003312 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3313 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003314 t = thread->transaction_stack;
3315 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003316 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003317 print_binder_transaction(m,
3318 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003319 t = t->from_parent;
3320 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003321 print_binder_transaction(m,
3322 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003323 t = t->to_parent;
3324 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003325 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003326 t = NULL;
3327 }
3328 }
3329 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003330 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003331 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003332 if (!print_always && m->count == header_pos)
3333 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003334}
3335
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003336static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003337{
3338 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003339 struct binder_work *w;
3340 int count;
3341
3342 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003343 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003344 count++;
3345
Arve Hjønnevågda498892014-02-21 14:40:26 -08003346 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3347 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003348 node->has_strong_ref, node->has_weak_ref,
3349 node->local_strong_refs, node->local_weak_refs,
3350 node->internal_strong_refs, count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003351 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003352 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003353 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003354 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003355 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003356 seq_puts(m, "\n");
3357 list_for_each_entry(w, &node->async_todo, entry)
3358 print_binder_work(m, " ",
3359 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003360}
3361
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003362static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003363{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003364 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3365 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3366 ref->node->debug_id, ref->strong, ref->weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003367}
3368
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003369static void print_binder_proc(struct seq_file *m,
3370 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003371{
3372 struct binder_work *w;
3373 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003374 size_t start_pos = m->count;
3375 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003376
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003377 seq_printf(m, "proc %d\n", proc->pid);
3378 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003379
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003380 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3381 print_binder_thread(m, rb_entry(n, struct binder_thread,
3382 rb_node), print_all);
3383 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003384 struct binder_node *node = rb_entry(n, struct binder_node,
3385 rb_node);
3386 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003387 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003388 }
3389 if (print_all) {
3390 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003391 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003392 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003393 print_binder_ref(m, rb_entry(n, struct binder_ref,
3394 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003395 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003396 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3397 print_binder_buffer(m, " buffer",
3398 rb_entry(n, struct binder_buffer, rb_node));
3399 list_for_each_entry(w, &proc->todo, entry)
3400 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003402 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003403 break;
3404 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003405 if (!print_all && m->count == header_pos)
3406 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003407}
3408
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003409static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003410 "BR_ERROR",
3411 "BR_OK",
3412 "BR_TRANSACTION",
3413 "BR_REPLY",
3414 "BR_ACQUIRE_RESULT",
3415 "BR_DEAD_REPLY",
3416 "BR_TRANSACTION_COMPLETE",
3417 "BR_INCREFS",
3418 "BR_ACQUIRE",
3419 "BR_RELEASE",
3420 "BR_DECREFS",
3421 "BR_ATTEMPT_ACQUIRE",
3422 "BR_NOOP",
3423 "BR_SPAWN_LOOPER",
3424 "BR_FINISHED",
3425 "BR_DEAD_BINDER",
3426 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3427 "BR_FAILED_REPLY"
3428};
3429
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003430static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003431 "BC_TRANSACTION",
3432 "BC_REPLY",
3433 "BC_ACQUIRE_RESULT",
3434 "BC_FREE_BUFFER",
3435 "BC_INCREFS",
3436 "BC_ACQUIRE",
3437 "BC_RELEASE",
3438 "BC_DECREFS",
3439 "BC_INCREFS_DONE",
3440 "BC_ACQUIRE_DONE",
3441 "BC_ATTEMPT_ACQUIRE",
3442 "BC_REGISTER_LOOPER",
3443 "BC_ENTER_LOOPER",
3444 "BC_EXIT_LOOPER",
3445 "BC_REQUEST_DEATH_NOTIFICATION",
3446 "BC_CLEAR_DEATH_NOTIFICATION",
3447 "BC_DEAD_BINDER_DONE"
3448};
3449
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003450static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003451 "proc",
3452 "thread",
3453 "node",
3454 "ref",
3455 "death",
3456 "transaction",
3457 "transaction_complete"
3458};
3459
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003460static void print_binder_stats(struct seq_file *m, const char *prefix,
3461 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003462{
3463 int i;
3464
3465 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003466 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003467 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3468 if (stats->bc[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003469 seq_printf(m, "%s%s: %d\n", prefix,
3470 binder_command_strings[i], stats->bc[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003471 }
3472
3473 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003474 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003475 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3476 if (stats->br[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003477 seq_printf(m, "%s%s: %d\n", prefix,
3478 binder_return_strings[i], stats->br[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003479 }
3480
3481 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003482 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003483 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003484 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003485 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3486 if (stats->obj_created[i] || stats->obj_deleted[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003487 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3488 binder_objstat_strings[i],
3489 stats->obj_created[i] - stats->obj_deleted[i],
3490 stats->obj_created[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003491 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003492}
3493
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003494static void print_binder_proc_stats(struct seq_file *m,
3495 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003496{
3497 struct binder_work *w;
3498 struct rb_node *n;
3499 int count, strong, weak;
3500
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003501 seq_printf(m, "proc %d\n", proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003502 count = 0;
3503 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3504 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003505 seq_printf(m, " threads: %d\n", count);
3506 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003507 " ready threads %d\n"
3508 " free async space %zd\n", proc->requested_threads,
3509 proc->requested_threads_started, proc->max_threads,
3510 proc->ready_threads, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003511 count = 0;
3512 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3513 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003514 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003515 count = 0;
3516 strong = 0;
3517 weak = 0;
3518 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3519 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3520 rb_node_desc);
3521 count++;
3522 strong += ref->strong;
3523 weak += ref->weak;
3524 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003525 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003526
3527 count = 0;
3528 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3529 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003530 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003531
3532 count = 0;
3533 list_for_each_entry(w, &proc->todo, entry) {
3534 switch (w->type) {
3535 case BINDER_WORK_TRANSACTION:
3536 count++;
3537 break;
3538 default:
3539 break;
3540 }
3541 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003542 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003543
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003544 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003545}
3546
3547
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003548static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003549{
3550 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003551 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003552 int do_lock = !binder_debug_no_lock;
3553
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003554 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003555 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003556
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003557 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003558
3559 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003560 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003561 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003562 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003563
Sasha Levinb67bfe02013-02-27 17:06:00 -08003564 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003565 print_binder_proc(m, proc, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003566 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003567 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003568 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003569}
3570
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003571static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003572{
3573 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003574 int do_lock = !binder_debug_no_lock;
3575
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003576 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003577 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003578
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003579 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003580
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003581 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003582
Sasha Levinb67bfe02013-02-27 17:06:00 -08003583 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003584 print_binder_proc_stats(m, proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003585 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003586 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003587 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003588}
3589
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003590static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003591{
3592 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003593 int do_lock = !binder_debug_no_lock;
3594
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003595 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003596 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003597
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003598 seq_puts(m, "binder transactions:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003599 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003600 print_binder_proc(m, proc, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003601 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003602 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003603 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003604}
3605
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003606static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003607{
Riley Andrews83050a42016-02-09 21:05:33 -08003608 struct binder_proc *itr;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003609 struct binder_proc *proc = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003610 int do_lock = !binder_debug_no_lock;
Riley Andrews83050a42016-02-09 21:05:33 -08003611 bool valid_proc = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003612
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003613 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003614 binder_lock(__func__);
Riley Andrews83050a42016-02-09 21:05:33 -08003615
3616 hlist_for_each_entry(itr, &binder_procs, proc_node) {
3617 if (itr == proc) {
3618 valid_proc = true;
3619 break;
3620 }
3621 }
3622 if (valid_proc) {
3623 seq_puts(m, "binder proc state:\n");
3624 print_binder_proc(m, proc, 1);
3625 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003626 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003627 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003628 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003629}
3630
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003631static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003632 struct binder_transaction_log_entry *e)
3633{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003634 seq_printf(m,
3635 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3636 e->debug_id, (e->call_type == 2) ? "reply" :
3637 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3638 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3639 e->target_handle, e->data_size, e->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003640}
3641
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003642static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003643{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003644 struct binder_transaction_log *log = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003645 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003646
3647 if (log->full) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003648 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3649 print_binder_transaction_log_entry(m, &log->entry[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003650 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003651 for (i = 0; i < log->next; i++)
3652 print_binder_transaction_log_entry(m, &log->entry[i]);
3653 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003654}
3655
3656static const struct file_operations binder_fops = {
3657 .owner = THIS_MODULE,
3658 .poll = binder_poll,
3659 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003660 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003661 .mmap = binder_mmap,
3662 .open = binder_open,
3663 .flush = binder_flush,
3664 .release = binder_release,
3665};
3666
3667static struct miscdevice binder_miscdev = {
3668 .minor = MISC_DYNAMIC_MINOR,
3669 .name = "binder",
3670 .fops = &binder_fops
3671};
3672
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003673BINDER_DEBUG_ENTRY(state);
3674BINDER_DEBUG_ENTRY(stats);
3675BINDER_DEBUG_ENTRY(transactions);
3676BINDER_DEBUG_ENTRY(transaction_log);
3677
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003678static int __init binder_init(void)
3679{
3680 int ret;
3681
Arve Hjønnevåg3c762a42010-04-22 15:53:23 -07003682 binder_deferred_workqueue = create_singlethread_workqueue("binder");
3683 if (!binder_deferred_workqueue)
3684 return -ENOMEM;
3685
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003686 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3687 if (binder_debugfs_dir_entry_root)
3688 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3689 binder_debugfs_dir_entry_root);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003690 ret = misc_register(&binder_miscdev);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003691 if (binder_debugfs_dir_entry_root) {
3692 debugfs_create_file("state",
3693 S_IRUGO,
3694 binder_debugfs_dir_entry_root,
3695 NULL,
3696 &binder_state_fops);
3697 debugfs_create_file("stats",
3698 S_IRUGO,
3699 binder_debugfs_dir_entry_root,
3700 NULL,
3701 &binder_stats_fops);
3702 debugfs_create_file("transactions",
3703 S_IRUGO,
3704 binder_debugfs_dir_entry_root,
3705 NULL,
3706 &binder_transactions_fops);
3707 debugfs_create_file("transaction_log",
3708 S_IRUGO,
3709 binder_debugfs_dir_entry_root,
3710 &binder_transaction_log,
3711 &binder_transaction_log_fops);
3712 debugfs_create_file("failed_transaction_log",
3713 S_IRUGO,
3714 binder_debugfs_dir_entry_root,
3715 &binder_transaction_log_failed,
3716 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003717 }
3718 return ret;
3719}
3720
3721device_initcall(binder_init);
3722
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003723#define CREATE_TRACE_POINTS
3724#include "binder_trace.h"
3725
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003726MODULE_LICENSE("GPL v2");