blob: 80499f421a296b3e743e8135e427e78570d32487 [file] [log] [blame]
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001/* binder.c
2 *
3 * Android IPC Subsystem
4 *
5 * Copyright (C) 2007-2008 Google, Inc.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Anmol Sarma56b468f2012-10-30 22:35:43 +053018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090020#include <asm/cacheflush.h>
21#include <linux/fdtable.h>
22#include <linux/file.h>
Colin Crosse2610b22013-05-06 23:50:15 +000023#include <linux/freezer.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090024#include <linux/fs.h>
25#include <linux/list.h>
26#include <linux/miscdevice.h>
27#include <linux/mm.h>
28#include <linux/module.h>
29#include <linux/mutex.h>
30#include <linux/nsproxy.h>
31#include <linux/poll.h>
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070032#include <linux/debugfs.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090033#include <linux/rbtree.h>
34#include <linux/sched.h>
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070035#include <linux/seq_file.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090036#include <linux/uaccess.h>
37#include <linux/vmalloc.h>
Colin Crossc11a1662010-04-15 15:21:51 -070038#include <linux/slab.h>
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080039#include <linux/pid_namespace.h>
Stephen Smalley79af7302015-01-21 10:54:10 -050040#include <linux/security.h>
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090041
Greg Kroah-Hartman9246a4a2014-10-16 15:26:51 +020042#ifdef CONFIG_ANDROID_BINDER_IPC_32BIT
43#define BINDER_IPC_32BIT 1
44#endif
45
46#include <uapi/linux/android/binder.h>
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070047#include "binder_trace.h"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090048
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -070049static DEFINE_MUTEX(binder_main_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090050static DEFINE_MUTEX(binder_deferred_lock);
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -080051static DEFINE_MUTEX(binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090052
53static HLIST_HEAD(binder_procs);
54static HLIST_HEAD(binder_deferred_list);
55static HLIST_HEAD(binder_dead_nodes);
56
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070057static struct dentry *binder_debugfs_dir_entry_root;
58static struct dentry *binder_debugfs_dir_entry_proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090059static struct binder_node *binder_context_mgr_node;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -060060static kuid_t binder_context_mgr_uid = INVALID_UID;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090061static int binder_last_id;
62
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070063#define BINDER_DEBUG_ENTRY(name) \
64static int binder_##name##_open(struct inode *inode, struct file *file) \
65{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070066 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070067} \
68\
69static const struct file_operations binder_##name##_fops = { \
70 .owner = THIS_MODULE, \
71 .open = binder_##name##_open, \
72 .read = seq_read, \
73 .llseek = seq_lseek, \
74 .release = single_release, \
75}
76
77static int binder_proc_show(struct seq_file *m, void *unused);
78BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090079
80/* This is only defined in include/asm-arm/sizes.h */
81#ifndef SZ_1K
82#define SZ_1K 0x400
83#endif
84
85#ifndef SZ_4M
86#define SZ_4M 0x400000
87#endif
88
89#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
90
91#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
92
93enum {
94 BINDER_DEBUG_USER_ERROR = 1U << 0,
95 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
96 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
97 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
98 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
99 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
100 BINDER_DEBUG_READ_WRITE = 1U << 6,
101 BINDER_DEBUG_USER_REFS = 1U << 7,
102 BINDER_DEBUG_THREADS = 1U << 8,
103 BINDER_DEBUG_TRANSACTION = 1U << 9,
104 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
105 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
106 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
107 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
108 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
109 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
110};
111static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
112 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
113module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
114
Zhengwang Ruan2c523252012-03-07 10:36:57 +0800115static bool binder_debug_no_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900116module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
117
118static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
119static int binder_stop_on_user_error;
120
121static int binder_set_stop_on_user_error(const char *val,
122 struct kernel_param *kp)
123{
124 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900125
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900126 ret = param_set_int(val, kp);
127 if (binder_stop_on_user_error < 2)
128 wake_up(&binder_user_error_wait);
129 return ret;
130}
131module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
132 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
133
134#define binder_debug(mask, x...) \
135 do { \
136 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400137 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900138 } while (0)
139
140#define binder_user_error(x...) \
141 do { \
142 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400143 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900144 if (binder_stop_on_user_error) \
145 binder_stop_on_user_error = 2; \
146 } while (0)
147
148enum binder_stat_types {
149 BINDER_STAT_PROC,
150 BINDER_STAT_THREAD,
151 BINDER_STAT_NODE,
152 BINDER_STAT_REF,
153 BINDER_STAT_DEATH,
154 BINDER_STAT_TRANSACTION,
155 BINDER_STAT_TRANSACTION_COMPLETE,
156 BINDER_STAT_COUNT
157};
158
159struct binder_stats {
160 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
161 int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1];
162 int obj_created[BINDER_STAT_COUNT];
163 int obj_deleted[BINDER_STAT_COUNT];
164};
165
166static struct binder_stats binder_stats;
167
168static inline void binder_stats_deleted(enum binder_stat_types type)
169{
170 binder_stats.obj_deleted[type]++;
171}
172
173static inline void binder_stats_created(enum binder_stat_types type)
174{
175 binder_stats.obj_created[type]++;
176}
177
178struct binder_transaction_log_entry {
179 int debug_id;
180 int call_type;
181 int from_proc;
182 int from_thread;
183 int target_handle;
184 int to_proc;
185 int to_thread;
186 int to_node;
187 int data_size;
188 int offsets_size;
189};
190struct binder_transaction_log {
191 int next;
192 int full;
193 struct binder_transaction_log_entry entry[32];
194};
195static struct binder_transaction_log binder_transaction_log;
196static struct binder_transaction_log binder_transaction_log_failed;
197
198static struct binder_transaction_log_entry *binder_transaction_log_add(
199 struct binder_transaction_log *log)
200{
201 struct binder_transaction_log_entry *e;
Seunghun Lee10f62862014-05-01 01:30:23 +0900202
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900203 e = &log->entry[log->next];
204 memset(e, 0, sizeof(*e));
205 log->next++;
206 if (log->next == ARRAY_SIZE(log->entry)) {
207 log->next = 0;
208 log->full = 1;
209 }
210 return e;
211}
212
213struct binder_work {
214 struct list_head entry;
215 enum {
216 BINDER_WORK_TRANSACTION = 1,
217 BINDER_WORK_TRANSACTION_COMPLETE,
218 BINDER_WORK_NODE,
219 BINDER_WORK_DEAD_BINDER,
220 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
221 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
222 } type;
223};
224
225struct binder_node {
226 int debug_id;
227 struct binder_work work;
228 union {
229 struct rb_node rb_node;
230 struct hlist_node dead_node;
231 };
232 struct binder_proc *proc;
233 struct hlist_head refs;
234 int internal_strong_refs;
235 int local_weak_refs;
236 int local_strong_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800237 binder_uintptr_t ptr;
238 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900239 unsigned has_strong_ref:1;
240 unsigned pending_strong_ref:1;
241 unsigned has_weak_ref:1;
242 unsigned pending_weak_ref:1;
243 unsigned has_async_transaction:1;
244 unsigned accept_fds:1;
245 unsigned min_priority:8;
246 struct list_head async_todo;
247};
248
249struct binder_ref_death {
250 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800251 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900252};
253
254struct binder_ref {
255 /* Lookups needed: */
256 /* node + proc => ref (transaction) */
257 /* desc + proc => ref (transaction, inc/dec ref) */
258 /* node => refs + procs (proc exit) */
259 int debug_id;
260 struct rb_node rb_node_desc;
261 struct rb_node rb_node_node;
262 struct hlist_node node_entry;
263 struct binder_proc *proc;
264 struct binder_node *node;
265 uint32_t desc;
266 int strong;
267 int weak;
268 struct binder_ref_death *death;
269};
270
271struct binder_buffer {
Justin P. Mattock217218f2012-01-12 06:51:31 -0800272 struct list_head entry; /* free and allocated entries by address */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900273 struct rb_node rb_node; /* free entry by size or allocated entry */
274 /* by address */
275 unsigned free:1;
276 unsigned allow_user_free:1;
277 unsigned async_transaction:1;
278 unsigned debug_id:29;
279
280 struct binder_transaction *transaction;
281
282 struct binder_node *target_node;
283 size_t data_size;
284 size_t offsets_size;
285 uint8_t data[0];
286};
287
288enum binder_deferred_state {
289 BINDER_DEFERRED_PUT_FILES = 0x01,
290 BINDER_DEFERRED_FLUSH = 0x02,
291 BINDER_DEFERRED_RELEASE = 0x04,
292};
293
294struct binder_proc {
295 struct hlist_node proc_node;
296 struct rb_root threads;
297 struct rb_root nodes;
298 struct rb_root refs_by_desc;
299 struct rb_root refs_by_node;
300 int pid;
301 struct vm_area_struct *vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800302 struct mm_struct *vma_vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900303 struct task_struct *tsk;
304 struct files_struct *files;
Todd Kjosc0d75da2017-11-27 09:32:33 -0800305 struct mutex files_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900306 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{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900379 unsigned long rlim_cur;
380 unsigned long irqs;
Todd Kjosc0d75da2017-11-27 09:32:33 -0800381 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900382
Todd Kjosc0d75da2017-11-27 09:32:33 -0800383 mutex_lock(&proc->files_lock);
384 if (proc->files == NULL) {
385 ret = -ESRCH;
386 goto err;
387 }
388 if (!lock_task_sighand(proc->tsk, &irqs)) {
389 ret = -EMFILE;
390 goto err;
391 }
Al Virodcfadfa2012-08-12 17:27:30 -0400392 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
393 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900394
Todd Kjosc0d75da2017-11-27 09:32:33 -0800395 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
396err:
397 mutex_unlock(&proc->files_lock);
398 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900399}
400
401/*
402 * copied from fd_install
403 */
404static void task_fd_install(
405 struct binder_proc *proc, unsigned int fd, struct file *file)
406{
Todd Kjosc0d75da2017-11-27 09:32:33 -0800407 mutex_lock(&proc->files_lock);
Al Virof869e8a2012-08-15 21:06:33 -0400408 if (proc->files)
409 __fd_install(proc->files, fd, file);
Todd Kjosc0d75da2017-11-27 09:32:33 -0800410 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900411}
412
413/*
414 * copied from sys_close
415 */
416static long task_close_fd(struct binder_proc *proc, unsigned int fd)
417{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900418 int retval;
419
Todd Kjosc0d75da2017-11-27 09:32:33 -0800420 mutex_lock(&proc->files_lock);
421 if (proc->files == NULL) {
422 retval = -ESRCH;
423 goto err;
424 }
Al Viro483ce1d2012-08-19 12:04:24 -0400425 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900426 /* can't restart close syscall because file table entry was cleared */
427 if (unlikely(retval == -ERESTARTSYS ||
428 retval == -ERESTARTNOINTR ||
429 retval == -ERESTARTNOHAND ||
430 retval == -ERESTART_RESTARTBLOCK))
431 retval = -EINTR;
Todd Kjosc0d75da2017-11-27 09:32:33 -0800432err:
433 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900434 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900435}
436
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700437static inline void binder_lock(const char *tag)
438{
439 trace_binder_lock(tag);
440 mutex_lock(&binder_main_lock);
441 trace_binder_locked(tag);
442}
443
444static inline void binder_unlock(const char *tag)
445{
446 trace_binder_unlock(tag);
447 mutex_unlock(&binder_main_lock);
448}
449
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900450static void binder_set_nice(long nice)
451{
452 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900453
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900454 if (can_nice(current, nice)) {
455 set_user_nice(current, nice);
456 return;
457 }
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900458 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900459 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530460 "%d: nice value %ld not allowed use %ld instead\n",
461 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900462 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +0800463 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900464 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530465 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900466}
467
468static size_t binder_buffer_size(struct binder_proc *proc,
469 struct binder_buffer *buffer)
470{
471 if (list_is_last(&buffer->entry, &proc->buffers))
472 return proc->buffer + proc->buffer_size - (void *)buffer->data;
Karthik Nayak78733112014-06-21 20:23:16 +0530473 return (size_t)list_entry(buffer->entry.next,
474 struct binder_buffer, entry) - (size_t)buffer->data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900475}
476
477static void binder_insert_free_buffer(struct binder_proc *proc,
478 struct binder_buffer *new_buffer)
479{
480 struct rb_node **p = &proc->free_buffers.rb_node;
481 struct rb_node *parent = NULL;
482 struct binder_buffer *buffer;
483 size_t buffer_size;
484 size_t new_buffer_size;
485
486 BUG_ON(!new_buffer->free);
487
488 new_buffer_size = binder_buffer_size(proc, new_buffer);
489
490 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530491 "%d: add free buffer, size %zd, at %p\n",
492 proc->pid, new_buffer_size, new_buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900493
494 while (*p) {
495 parent = *p;
496 buffer = rb_entry(parent, struct binder_buffer, rb_node);
497 BUG_ON(!buffer->free);
498
499 buffer_size = binder_buffer_size(proc, buffer);
500
501 if (new_buffer_size < buffer_size)
502 p = &parent->rb_left;
503 else
504 p = &parent->rb_right;
505 }
506 rb_link_node(&new_buffer->rb_node, parent, p);
507 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
508}
509
510static void binder_insert_allocated_buffer(struct binder_proc *proc,
511 struct binder_buffer *new_buffer)
512{
513 struct rb_node **p = &proc->allocated_buffers.rb_node;
514 struct rb_node *parent = NULL;
515 struct binder_buffer *buffer;
516
517 BUG_ON(new_buffer->free);
518
519 while (*p) {
520 parent = *p;
521 buffer = rb_entry(parent, struct binder_buffer, rb_node);
522 BUG_ON(buffer->free);
523
524 if (new_buffer < buffer)
525 p = &parent->rb_left;
526 else if (new_buffer > buffer)
527 p = &parent->rb_right;
528 else
529 BUG();
530 }
531 rb_link_node(&new_buffer->rb_node, parent, p);
532 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
533}
534
535static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800536 uintptr_t user_ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900537{
538 struct rb_node *n = proc->allocated_buffers.rb_node;
539 struct binder_buffer *buffer;
540 struct binder_buffer *kern_ptr;
541
Arve Hjønnevågda498892014-02-21 14:40:26 -0800542 kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
543 - offsetof(struct binder_buffer, data));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900544
545 while (n) {
546 buffer = rb_entry(n, struct binder_buffer, rb_node);
547 BUG_ON(buffer->free);
548
549 if (kern_ptr < buffer)
550 n = n->rb_left;
551 else if (kern_ptr > buffer)
552 n = n->rb_right;
553 else
554 return buffer;
555 }
556 return NULL;
557}
558
559static int binder_update_page_range(struct binder_proc *proc, int allocate,
560 void *start, void *end,
561 struct vm_area_struct *vma)
562{
563 void *page_addr;
564 unsigned long user_page_addr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900565 struct page **page;
566 struct mm_struct *mm;
567
568 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530569 "%d: %s pages %p-%p\n", proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900570 allocate ? "allocate" : "free", start, end);
571
572 if (end <= start)
573 return 0;
574
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700575 trace_binder_update_page_range(proc, allocate, start, end);
576
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900577 if (vma)
578 mm = NULL;
579 else
580 mm = get_task_mm(proc->tsk);
581
582 if (mm) {
583 down_write(&mm->mmap_sem);
584 vma = proc->vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800585 if (vma && mm != proc->vma_vm_mm) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530586 pr_err("%d: vma mm and task mm mismatch\n",
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -0800587 proc->pid);
588 vma = NULL;
589 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900590 }
591
592 if (allocate == 0)
593 goto free_range;
594
595 if (vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530596 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
597 proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900598 goto err_no_vma;
599 }
600
601 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
602 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900603
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900604 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
605
606 BUG_ON(*page);
Arve Hjønnevåg585650d2012-10-16 15:29:55 -0700607 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900608 if (*page == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530609 pr_err("%d: binder_alloc_buf failed for page at %p\n",
610 proc->pid, page_addr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900611 goto err_alloc_page_failed;
612 }
Andrey Ryabininf4c72c72015-02-27 20:44:21 +0300613 ret = map_kernel_range_noflush((unsigned long)page_addr,
614 PAGE_SIZE, PAGE_KERNEL, page);
615 flush_cache_vmap((unsigned long)page_addr,
616 (unsigned long)page_addr + PAGE_SIZE);
617 if (ret != 1) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530618 pr_err("%d: binder_alloc_buf failed to map page at %p in kernel\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900619 proc->pid, page_addr);
620 goto err_map_kernel_failed;
621 }
622 user_page_addr =
623 (uintptr_t)page_addr + proc->user_buffer_offset;
624 ret = vm_insert_page(vma, user_page_addr, page[0]);
625 if (ret) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530626 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900627 proc->pid, user_page_addr);
628 goto err_vm_insert_page_failed;
629 }
630 /* vm_insert_page does not seem to increment the refcount */
631 }
632 if (mm) {
633 up_write(&mm->mmap_sem);
634 mmput(mm);
635 }
636 return 0;
637
638free_range:
639 for (page_addr = end - PAGE_SIZE; page_addr >= start;
640 page_addr -= PAGE_SIZE) {
641 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
642 if (vma)
643 zap_page_range(vma, (uintptr_t)page_addr +
644 proc->user_buffer_offset, PAGE_SIZE, NULL);
645err_vm_insert_page_failed:
646 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
647err_map_kernel_failed:
648 __free_page(*page);
649 *page = NULL;
650err_alloc_page_failed:
651 ;
652 }
653err_no_vma:
654 if (mm) {
655 up_write(&mm->mmap_sem);
656 mmput(mm);
657 }
658 return -ENOMEM;
659}
660
661static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
662 size_t data_size,
663 size_t offsets_size, int is_async)
664{
665 struct rb_node *n = proc->free_buffers.rb_node;
666 struct binder_buffer *buffer;
667 size_t buffer_size;
668 struct rb_node *best_fit = NULL;
669 void *has_page_addr;
670 void *end_page_addr;
671 size_t size;
672
673 if (proc->vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530674 pr_err("%d: binder_alloc_buf, no vma\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900675 proc->pid);
676 return NULL;
677 }
678
679 size = ALIGN(data_size, sizeof(void *)) +
680 ALIGN(offsets_size, sizeof(void *));
681
682 if (size < data_size || size < offsets_size) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530683 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
684 proc->pid, data_size, offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900685 return NULL;
686 }
687
688 if (is_async &&
689 proc->free_async_space < size + sizeof(struct binder_buffer)) {
690 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530691 "%d: binder_alloc_buf size %zd failed, no async space left\n",
692 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900693 return NULL;
694 }
695
696 while (n) {
697 buffer = rb_entry(n, struct binder_buffer, rb_node);
698 BUG_ON(!buffer->free);
699 buffer_size = binder_buffer_size(proc, buffer);
700
701 if (size < buffer_size) {
702 best_fit = n;
703 n = n->rb_left;
704 } else if (size > buffer_size)
705 n = n->rb_right;
706 else {
707 best_fit = n;
708 break;
709 }
710 }
711 if (best_fit == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530712 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
713 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900714 return NULL;
715 }
716 if (n == NULL) {
717 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
718 buffer_size = binder_buffer_size(proc, buffer);
719 }
720
721 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530722 "%d: binder_alloc_buf size %zd got buffer %p size %zd\n",
723 proc->pid, size, buffer, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900724
725 has_page_addr =
726 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
727 if (n == NULL) {
728 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
729 buffer_size = size; /* no room for other buffers */
730 else
731 buffer_size = size + sizeof(struct binder_buffer);
732 }
733 end_page_addr =
734 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
735 if (end_page_addr > has_page_addr)
736 end_page_addr = has_page_addr;
737 if (binder_update_page_range(proc, 1,
738 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
739 return NULL;
740
741 rb_erase(best_fit, &proc->free_buffers);
742 buffer->free = 0;
743 binder_insert_allocated_buffer(proc, buffer);
744 if (buffer_size != size) {
745 struct binder_buffer *new_buffer = (void *)buffer->data + size;
Seunghun Lee10f62862014-05-01 01:30:23 +0900746
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900747 list_add(&new_buffer->entry, &buffer->entry);
748 new_buffer->free = 1;
749 binder_insert_free_buffer(proc, new_buffer);
750 }
751 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530752 "%d: binder_alloc_buf size %zd got %p\n",
753 proc->pid, size, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900754 buffer->data_size = data_size;
755 buffer->offsets_size = offsets_size;
756 buffer->async_transaction = is_async;
757 if (is_async) {
758 proc->free_async_space -= size + sizeof(struct binder_buffer);
759 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530760 "%d: binder_alloc_buf size %zd async free %zd\n",
761 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900762 }
763
764 return buffer;
765}
766
767static void *buffer_start_page(struct binder_buffer *buffer)
768{
769 return (void *)((uintptr_t)buffer & PAGE_MASK);
770}
771
772static void *buffer_end_page(struct binder_buffer *buffer)
773{
774 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
775}
776
777static void binder_delete_free_buffer(struct binder_proc *proc,
778 struct binder_buffer *buffer)
779{
780 struct binder_buffer *prev, *next = NULL;
781 int free_page_end = 1;
782 int free_page_start = 1;
783
784 BUG_ON(proc->buffers.next == &buffer->entry);
785 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
786 BUG_ON(!prev->free);
787 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
788 free_page_start = 0;
789 if (buffer_end_page(prev) == buffer_end_page(buffer))
790 free_page_end = 0;
791 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530792 "%d: merge free, buffer %p share page with %p\n",
793 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900794 }
795
796 if (!list_is_last(&buffer->entry, &proc->buffers)) {
797 next = list_entry(buffer->entry.next,
798 struct binder_buffer, entry);
799 if (buffer_start_page(next) == buffer_end_page(buffer)) {
800 free_page_end = 0;
801 if (buffer_start_page(next) ==
802 buffer_start_page(buffer))
803 free_page_start = 0;
804 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530805 "%d: merge free, buffer %p share page with %p\n",
806 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900807 }
808 }
809 list_del(&buffer->entry);
810 if (free_page_start || free_page_end) {
811 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Masanari Iida1dcdbfd2013-06-23 23:47:15 +0900812 "%d: merge free, buffer %p do not share page%s%s with %p or %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900813 proc->pid, buffer, free_page_start ? "" : " end",
814 free_page_end ? "" : " start", prev, next);
815 binder_update_page_range(proc, 0, free_page_start ?
816 buffer_start_page(buffer) : buffer_end_page(buffer),
817 (free_page_end ? buffer_end_page(buffer) :
818 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
819 }
820}
821
822static void binder_free_buf(struct binder_proc *proc,
823 struct binder_buffer *buffer)
824{
825 size_t size, buffer_size;
826
827 buffer_size = binder_buffer_size(proc, buffer);
828
829 size = ALIGN(buffer->data_size, sizeof(void *)) +
830 ALIGN(buffer->offsets_size, sizeof(void *));
831
832 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530833 "%d: binder_free_buf %p size %zd buffer_size %zd\n",
834 proc->pid, buffer, size, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900835
836 BUG_ON(buffer->free);
837 BUG_ON(size > buffer_size);
838 BUG_ON(buffer->transaction != NULL);
839 BUG_ON((void *)buffer < proc->buffer);
840 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
841
842 if (buffer->async_transaction) {
843 proc->free_async_space += size + sizeof(struct binder_buffer);
844
845 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530846 "%d: binder_free_buf size %zd async free %zd\n",
847 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900848 }
849
850 binder_update_page_range(proc, 0,
851 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
852 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
853 NULL);
854 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
855 buffer->free = 1;
856 if (!list_is_last(&buffer->entry, &proc->buffers)) {
857 struct binder_buffer *next = list_entry(buffer->entry.next,
858 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900859
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900860 if (next->free) {
861 rb_erase(&next->rb_node, &proc->free_buffers);
862 binder_delete_free_buffer(proc, next);
863 }
864 }
865 if (proc->buffers.next != &buffer->entry) {
866 struct binder_buffer *prev = list_entry(buffer->entry.prev,
867 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900868
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900869 if (prev->free) {
870 binder_delete_free_buffer(proc, buffer);
871 rb_erase(&prev->rb_node, &proc->free_buffers);
872 buffer = prev;
873 }
874 }
875 binder_insert_free_buffer(proc, buffer);
876}
877
878static struct binder_node *binder_get_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800879 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900880{
881 struct rb_node *n = proc->nodes.rb_node;
882 struct binder_node *node;
883
884 while (n) {
885 node = rb_entry(n, struct binder_node, rb_node);
886
887 if (ptr < node->ptr)
888 n = n->rb_left;
889 else if (ptr > node->ptr)
890 n = n->rb_right;
891 else
892 return node;
893 }
894 return NULL;
895}
896
897static struct binder_node *binder_new_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800898 binder_uintptr_t ptr,
899 binder_uintptr_t cookie)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900900{
901 struct rb_node **p = &proc->nodes.rb_node;
902 struct rb_node *parent = NULL;
903 struct binder_node *node;
904
905 while (*p) {
906 parent = *p;
907 node = rb_entry(parent, struct binder_node, rb_node);
908
909 if (ptr < node->ptr)
910 p = &(*p)->rb_left;
911 else if (ptr > node->ptr)
912 p = &(*p)->rb_right;
913 else
914 return NULL;
915 }
916
917 node = kzalloc(sizeof(*node), GFP_KERNEL);
918 if (node == NULL)
919 return NULL;
920 binder_stats_created(BINDER_STAT_NODE);
921 rb_link_node(&node->rb_node, parent, p);
922 rb_insert_color(&node->rb_node, &proc->nodes);
923 node->debug_id = ++binder_last_id;
924 node->proc = proc;
925 node->ptr = ptr;
926 node->cookie = cookie;
927 node->work.type = BINDER_WORK_NODE;
928 INIT_LIST_HEAD(&node->work.entry);
929 INIT_LIST_HEAD(&node->async_todo);
930 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800931 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900932 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800933 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900934 return node;
935}
936
937static int binder_inc_node(struct binder_node *node, int strong, int internal,
938 struct list_head *target_list)
939{
940 if (strong) {
941 if (internal) {
942 if (target_list == NULL &&
943 node->internal_strong_refs == 0 &&
944 !(node == binder_context_mgr_node &&
945 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530946 pr_err("invalid inc strong node for %d\n",
947 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900948 return -EINVAL;
949 }
950 node->internal_strong_refs++;
951 } else
952 node->local_strong_refs++;
953 if (!node->has_strong_ref && target_list) {
954 list_del_init(&node->work.entry);
955 list_add_tail(&node->work.entry, target_list);
956 }
957 } else {
958 if (!internal)
959 node->local_weak_refs++;
960 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
961 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530962 pr_err("invalid inc weak node for %d\n",
963 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900964 return -EINVAL;
965 }
966 list_add_tail(&node->work.entry, target_list);
967 }
968 }
969 return 0;
970}
971
972static int binder_dec_node(struct binder_node *node, int strong, int internal)
973{
974 if (strong) {
975 if (internal)
976 node->internal_strong_refs--;
977 else
978 node->local_strong_refs--;
979 if (node->local_strong_refs || node->internal_strong_refs)
980 return 0;
981 } else {
982 if (!internal)
983 node->local_weak_refs--;
984 if (node->local_weak_refs || !hlist_empty(&node->refs))
985 return 0;
986 }
987 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
988 if (list_empty(&node->work.entry)) {
989 list_add_tail(&node->work.entry, &node->proc->todo);
990 wake_up_interruptible(&node->proc->wait);
991 }
992 } else {
993 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
994 !node->local_weak_refs) {
995 list_del_init(&node->work.entry);
996 if (node->proc) {
997 rb_erase(&node->rb_node, &node->proc->nodes);
998 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530999 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001000 node->debug_id);
1001 } else {
1002 hlist_del(&node->dead_node);
1003 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301004 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001005 node->debug_id);
1006 }
1007 kfree(node);
1008 binder_stats_deleted(BINDER_STAT_NODE);
1009 }
1010 }
1011
1012 return 0;
1013}
1014
1015
1016static struct binder_ref *binder_get_ref(struct binder_proc *proc,
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001017 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001018{
1019 struct rb_node *n = proc->refs_by_desc.rb_node;
1020 struct binder_ref *ref;
1021
1022 while (n) {
1023 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1024
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001025 if (desc < ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001026 n = n->rb_left;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001027 } else if (desc > ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001028 n = n->rb_right;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001029 } else if (need_strong_ref && !ref->strong) {
1030 binder_user_error("tried to use weak ref as strong ref\n");
1031 return NULL;
1032 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001033 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001034 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001035 }
1036 return NULL;
1037}
1038
1039static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1040 struct binder_node *node)
1041{
1042 struct rb_node *n;
1043 struct rb_node **p = &proc->refs_by_node.rb_node;
1044 struct rb_node *parent = NULL;
1045 struct binder_ref *ref, *new_ref;
1046
1047 while (*p) {
1048 parent = *p;
1049 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1050
1051 if (node < ref->node)
1052 p = &(*p)->rb_left;
1053 else if (node > ref->node)
1054 p = &(*p)->rb_right;
1055 else
1056 return ref;
1057 }
1058 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1059 if (new_ref == NULL)
1060 return NULL;
1061 binder_stats_created(BINDER_STAT_REF);
1062 new_ref->debug_id = ++binder_last_id;
1063 new_ref->proc = proc;
1064 new_ref->node = node;
1065 rb_link_node(&new_ref->rb_node_node, parent, p);
1066 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1067
1068 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1069 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1070 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1071 if (ref->desc > new_ref->desc)
1072 break;
1073 new_ref->desc = ref->desc + 1;
1074 }
1075
1076 p = &proc->refs_by_desc.rb_node;
1077 while (*p) {
1078 parent = *p;
1079 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1080
1081 if (new_ref->desc < ref->desc)
1082 p = &(*p)->rb_left;
1083 else if (new_ref->desc > ref->desc)
1084 p = &(*p)->rb_right;
1085 else
1086 BUG();
1087 }
1088 rb_link_node(&new_ref->rb_node_desc, parent, p);
1089 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1090 if (node) {
1091 hlist_add_head(&new_ref->node_entry, &node->refs);
1092
1093 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301094 "%d new ref %d desc %d for node %d\n",
1095 proc->pid, new_ref->debug_id, new_ref->desc,
1096 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001097 } else {
1098 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301099 "%d new ref %d desc %d for dead node\n",
1100 proc->pid, new_ref->debug_id, new_ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001101 }
1102 return new_ref;
1103}
1104
1105static void binder_delete_ref(struct binder_ref *ref)
1106{
1107 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301108 "%d delete ref %d desc %d for node %d\n",
1109 ref->proc->pid, ref->debug_id, ref->desc,
1110 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001111
1112 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1113 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1114 if (ref->strong)
1115 binder_dec_node(ref->node, 1, 1);
1116 hlist_del(&ref->node_entry);
1117 binder_dec_node(ref->node, 0, 1);
1118 if (ref->death) {
1119 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301120 "%d delete ref %d desc %d has death notification\n",
1121 ref->proc->pid, ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001122 list_del(&ref->death->work.entry);
1123 kfree(ref->death);
1124 binder_stats_deleted(BINDER_STAT_DEATH);
1125 }
1126 kfree(ref);
1127 binder_stats_deleted(BINDER_STAT_REF);
1128}
1129
1130static int binder_inc_ref(struct binder_ref *ref, int strong,
1131 struct list_head *target_list)
1132{
1133 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001134
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001135 if (strong) {
1136 if (ref->strong == 0) {
1137 ret = binder_inc_node(ref->node, 1, 1, target_list);
1138 if (ret)
1139 return ret;
1140 }
1141 ref->strong++;
1142 } else {
1143 if (ref->weak == 0) {
1144 ret = binder_inc_node(ref->node, 0, 1, target_list);
1145 if (ret)
1146 return ret;
1147 }
1148 ref->weak++;
1149 }
1150 return 0;
1151}
1152
1153
1154static int binder_dec_ref(struct binder_ref *ref, int strong)
1155{
1156 if (strong) {
1157 if (ref->strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301158 binder_user_error("%d invalid dec strong, 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->strong--;
1164 if (ref->strong == 0) {
1165 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001166
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001167 ret = binder_dec_node(ref->node, strong, 1);
1168 if (ret)
1169 return ret;
1170 }
1171 } else {
1172 if (ref->weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301173 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001174 ref->proc->pid, ref->debug_id,
1175 ref->desc, ref->strong, ref->weak);
1176 return -EINVAL;
1177 }
1178 ref->weak--;
1179 }
1180 if (ref->strong == 0 && ref->weak == 0)
1181 binder_delete_ref(ref);
1182 return 0;
1183}
1184
1185static void binder_pop_transaction(struct binder_thread *target_thread,
1186 struct binder_transaction *t)
1187{
1188 if (target_thread) {
1189 BUG_ON(target_thread->transaction_stack != t);
1190 BUG_ON(target_thread->transaction_stack->from != target_thread);
1191 target_thread->transaction_stack =
1192 target_thread->transaction_stack->from_parent;
1193 t->from = NULL;
1194 }
1195 t->need_reply = 0;
1196 if (t->buffer)
1197 t->buffer->transaction = NULL;
1198 kfree(t);
1199 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1200}
1201
1202static void binder_send_failed_reply(struct binder_transaction *t,
1203 uint32_t error_code)
1204{
1205 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001206 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001207
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001208 BUG_ON(t->flags & TF_ONE_WAY);
1209 while (1) {
1210 target_thread = t->from;
1211 if (target_thread) {
1212 if (target_thread->return_error != BR_OK &&
1213 target_thread->return_error2 == BR_OK) {
1214 target_thread->return_error2 =
1215 target_thread->return_error;
1216 target_thread->return_error = BR_OK;
1217 }
1218 if (target_thread->return_error == BR_OK) {
1219 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301220 "send failed reply for transaction %d to %d:%d\n",
William Panlener0232a422014-09-03 22:44:03 -05001221 t->debug_id,
1222 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001223 target_thread->pid);
1224
1225 binder_pop_transaction(target_thread, t);
1226 target_thread->return_error = error_code;
1227 wake_up_interruptible(&target_thread->wait);
1228 } else {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301229 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1230 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001231 target_thread->pid,
1232 target_thread->return_error);
1233 }
1234 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001235 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001236 next = t->from_parent;
1237
1238 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1239 "send failed reply for transaction %d, target dead\n",
1240 t->debug_id);
1241
1242 binder_pop_transaction(target_thread, t);
1243 if (next == NULL) {
1244 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1245 "reply failed, no target thread at root\n");
1246 return;
1247 }
1248 t = next;
1249 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1250 "reply failed, no target thread -- retry %d\n",
1251 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001252 }
1253}
1254
1255static void binder_transaction_buffer_release(struct binder_proc *proc,
1256 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001257 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001258{
Arve Hjønnevågda498892014-02-21 14:40:26 -08001259 binder_size_t *offp, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001260 int debug_id = buffer->debug_id;
1261
1262 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301263 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001264 proc->pid, buffer->debug_id,
1265 buffer->data_size, buffer->offsets_size, failed_at);
1266
1267 if (buffer->target_node)
1268 binder_dec_node(buffer->target_node, 1, 0);
1269
Arve Hjønnevågda498892014-02-21 14:40:26 -08001270 offp = (binder_size_t *)(buffer->data +
1271 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001272 if (failed_at)
1273 off_end = failed_at;
1274 else
1275 off_end = (void *)offp + buffer->offsets_size;
1276 for (; offp < off_end; offp++) {
1277 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001278
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001279 if (*offp > buffer->data_size - sizeof(*fp) ||
1280 buffer->data_size < sizeof(*fp) ||
Serban Constantinescuec35e852013-07-04 10:54:46 +01001281 !IS_ALIGNED(*offp, sizeof(u32))) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001282 pr_err("transaction release %d bad offset %lld, size %zd\n",
1283 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001284 continue;
1285 }
1286 fp = (struct flat_binder_object *)(buffer->data + *offp);
1287 switch (fp->type) {
1288 case BINDER_TYPE_BINDER:
1289 case BINDER_TYPE_WEAK_BINDER: {
1290 struct binder_node *node = binder_get_node(proc, fp->binder);
Seunghun Lee10f62862014-05-01 01:30:23 +09001291
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001292 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001293 pr_err("transaction release %d bad node %016llx\n",
1294 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001295 break;
1296 }
1297 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001298 " node %d u%016llx\n",
1299 node->debug_id, (u64)node->ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001300 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1301 } break;
1302 case BINDER_TYPE_HANDLE:
1303 case BINDER_TYPE_WEAK_HANDLE: {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001304 struct binder_ref *ref;
1305
1306 ref = binder_get_ref(proc, fp->handle,
1307 fp->type == BINDER_TYPE_HANDLE);
Seunghun Lee10f62862014-05-01 01:30:23 +09001308
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001309 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001310 pr_err("transaction release %d bad handle %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301311 debug_id, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001312 break;
1313 }
1314 binder_debug(BINDER_DEBUG_TRANSACTION,
1315 " ref %d desc %d (node %d)\n",
1316 ref->debug_id, ref->desc, ref->node->debug_id);
1317 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1318 } break;
1319
1320 case BINDER_TYPE_FD:
1321 binder_debug(BINDER_DEBUG_TRANSACTION,
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001322 " fd %d\n", fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001323 if (failed_at)
1324 task_close_fd(proc, fp->handle);
1325 break;
1326
1327 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001328 pr_err("transaction release %d bad object type %x\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301329 debug_id, fp->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001330 break;
1331 }
1332 }
1333}
1334
1335static void binder_transaction(struct binder_proc *proc,
1336 struct binder_thread *thread,
1337 struct binder_transaction_data *tr, int reply)
1338{
1339 struct binder_transaction *t;
1340 struct binder_work *tcomplete;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001341 binder_size_t *offp, *off_end;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001342 binder_size_t off_min;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001343 struct binder_proc *target_proc;
1344 struct binder_thread *target_thread = NULL;
1345 struct binder_node *target_node = NULL;
1346 struct list_head *target_list;
1347 wait_queue_head_t *target_wait;
1348 struct binder_transaction *in_reply_to = NULL;
1349 struct binder_transaction_log_entry *e;
1350 uint32_t return_error;
1351
1352 e = binder_transaction_log_add(&binder_transaction_log);
1353 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1354 e->from_proc = proc->pid;
1355 e->from_thread = thread->pid;
1356 e->target_handle = tr->target.handle;
1357 e->data_size = tr->data_size;
1358 e->offsets_size = tr->offsets_size;
1359
1360 if (reply) {
1361 in_reply_to = thread->transaction_stack;
1362 if (in_reply_to == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301363 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001364 proc->pid, thread->pid);
1365 return_error = BR_FAILED_REPLY;
1366 goto err_empty_call_stack;
1367 }
1368 binder_set_nice(in_reply_to->saved_priority);
1369 if (in_reply_to->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301370 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 +09001371 proc->pid, thread->pid, in_reply_to->debug_id,
1372 in_reply_to->to_proc ?
1373 in_reply_to->to_proc->pid : 0,
1374 in_reply_to->to_thread ?
1375 in_reply_to->to_thread->pid : 0);
1376 return_error = BR_FAILED_REPLY;
1377 in_reply_to = NULL;
1378 goto err_bad_call_stack;
1379 }
1380 thread->transaction_stack = in_reply_to->to_parent;
1381 target_thread = in_reply_to->from;
1382 if (target_thread == NULL) {
1383 return_error = BR_DEAD_REPLY;
1384 goto err_dead_binder;
1385 }
1386 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301387 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 +09001388 proc->pid, thread->pid,
1389 target_thread->transaction_stack ?
1390 target_thread->transaction_stack->debug_id : 0,
1391 in_reply_to->debug_id);
1392 return_error = BR_FAILED_REPLY;
1393 in_reply_to = NULL;
1394 target_thread = NULL;
1395 goto err_dead_binder;
1396 }
1397 target_proc = target_thread->proc;
1398 } else {
1399 if (tr->target.handle) {
1400 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001401
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001402 ref = binder_get_ref(proc, tr->target.handle, true);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001403 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301404 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001405 proc->pid, thread->pid);
1406 return_error = BR_FAILED_REPLY;
1407 goto err_invalid_target_handle;
1408 }
1409 target_node = ref->node;
1410 } else {
1411 target_node = binder_context_mgr_node;
1412 if (target_node == NULL) {
1413 return_error = BR_DEAD_REPLY;
1414 goto err_no_context_mgr_node;
1415 }
1416 }
1417 e->to_node = target_node->debug_id;
1418 target_proc = target_node->proc;
1419 if (target_proc == NULL) {
1420 return_error = BR_DEAD_REPLY;
1421 goto err_dead_binder;
1422 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001423 if (security_binder_transaction(proc->tsk,
1424 target_proc->tsk) < 0) {
1425 return_error = BR_FAILED_REPLY;
1426 goto err_invalid_target_handle;
1427 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001428 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1429 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001430
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001431 tmp = thread->transaction_stack;
1432 if (tmp->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301433 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 +09001434 proc->pid, thread->pid, tmp->debug_id,
1435 tmp->to_proc ? tmp->to_proc->pid : 0,
1436 tmp->to_thread ?
1437 tmp->to_thread->pid : 0);
1438 return_error = BR_FAILED_REPLY;
1439 goto err_bad_call_stack;
1440 }
1441 while (tmp) {
1442 if (tmp->from && tmp->from->proc == target_proc)
1443 target_thread = tmp->from;
1444 tmp = tmp->from_parent;
1445 }
1446 }
1447 }
1448 if (target_thread) {
1449 e->to_thread = target_thread->pid;
1450 target_list = &target_thread->todo;
1451 target_wait = &target_thread->wait;
1452 } else {
1453 target_list = &target_proc->todo;
1454 target_wait = &target_proc->wait;
1455 }
1456 e->to_proc = target_proc->pid;
1457
1458 /* TODO: reuse incoming transaction for reply */
1459 t = kzalloc(sizeof(*t), GFP_KERNEL);
1460 if (t == NULL) {
1461 return_error = BR_FAILED_REPLY;
1462 goto err_alloc_t_failed;
1463 }
1464 binder_stats_created(BINDER_STAT_TRANSACTION);
1465
1466 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1467 if (tcomplete == NULL) {
1468 return_error = BR_FAILED_REPLY;
1469 goto err_alloc_tcomplete_failed;
1470 }
1471 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1472
1473 t->debug_id = ++binder_last_id;
1474 e->debug_id = t->debug_id;
1475
1476 if (reply)
1477 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001478 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001479 proc->pid, thread->pid, t->debug_id,
1480 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001481 (u64)tr->data.ptr.buffer,
1482 (u64)tr->data.ptr.offsets,
1483 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001484 else
1485 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001486 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001487 proc->pid, thread->pid, t->debug_id,
1488 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001489 (u64)tr->data.ptr.buffer,
1490 (u64)tr->data.ptr.offsets,
1491 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001492
1493 if (!reply && !(tr->flags & TF_ONE_WAY))
1494 t->from = thread;
1495 else
1496 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03001497 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001498 t->to_proc = target_proc;
1499 t->to_thread = target_thread;
1500 t->code = tr->code;
1501 t->flags = tr->flags;
1502 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001503
1504 trace_binder_transaction(reply, t, target_node);
1505
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001506 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1507 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1508 if (t->buffer == NULL) {
1509 return_error = BR_FAILED_REPLY;
1510 goto err_binder_alloc_buf_failed;
1511 }
1512 t->buffer->allow_user_free = 0;
1513 t->buffer->debug_id = t->debug_id;
1514 t->buffer->transaction = t;
1515 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001516 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001517 if (target_node)
1518 binder_inc_node(target_node, 1, 0, NULL);
1519
Arve Hjønnevågda498892014-02-21 14:40:26 -08001520 offp = (binder_size_t *)(t->buffer->data +
1521 ALIGN(tr->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001522
Arve Hjønnevågda498892014-02-21 14:40:26 -08001523 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1524 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301525 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1526 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001527 return_error = BR_FAILED_REPLY;
1528 goto err_copy_data_failed;
1529 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001530 if (copy_from_user(offp, (const void __user *)(uintptr_t)
1531 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301532 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1533 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001534 return_error = BR_FAILED_REPLY;
1535 goto err_copy_data_failed;
1536 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001537 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1538 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1539 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001540 return_error = BR_FAILED_REPLY;
1541 goto err_bad_offset;
1542 }
1543 off_end = (void *)offp + tr->offsets_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001544 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001545 for (; offp < off_end; offp++) {
1546 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001547
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001548 if (*offp > t->buffer->data_size - sizeof(*fp) ||
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001549 *offp < off_min ||
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001550 t->buffer->data_size < sizeof(*fp) ||
Serban Constantinescuec35e852013-07-04 10:54:46 +01001551 !IS_ALIGNED(*offp, sizeof(u32))) {
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001552 binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
1553 proc->pid, thread->pid, (u64)*offp,
1554 (u64)off_min,
1555 (u64)(t->buffer->data_size -
1556 sizeof(*fp)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001557 return_error = BR_FAILED_REPLY;
1558 goto err_bad_offset;
1559 }
1560 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001561 off_min = *offp + sizeof(struct flat_binder_object);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001562 switch (fp->type) {
1563 case BINDER_TYPE_BINDER:
1564 case BINDER_TYPE_WEAK_BINDER: {
1565 struct binder_ref *ref;
1566 struct binder_node *node = binder_get_node(proc, fp->binder);
Seunghun Lee10f62862014-05-01 01:30:23 +09001567
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001568 if (node == NULL) {
1569 node = binder_new_node(proc, fp->binder, fp->cookie);
1570 if (node == NULL) {
1571 return_error = BR_FAILED_REPLY;
1572 goto err_binder_new_node_failed;
1573 }
1574 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1575 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1576 }
1577 if (fp->cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001578 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001579 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001580 (u64)fp->binder, node->debug_id,
1581 (u64)fp->cookie, (u64)node->cookie);
Christian Engelmayer7d420432014-05-07 21:44:53 +02001582 return_error = BR_FAILED_REPLY;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001583 goto err_binder_get_ref_for_node_failed;
1584 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001585 if (security_binder_transfer_binder(proc->tsk,
1586 target_proc->tsk)) {
1587 return_error = BR_FAILED_REPLY;
1588 goto err_binder_get_ref_for_node_failed;
1589 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001590 ref = binder_get_ref_for_node(target_proc, node);
1591 if (ref == NULL) {
1592 return_error = BR_FAILED_REPLY;
1593 goto err_binder_get_ref_for_node_failed;
1594 }
1595 if (fp->type == BINDER_TYPE_BINDER)
1596 fp->type = BINDER_TYPE_HANDLE;
1597 else
1598 fp->type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001599 fp->binder = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001600 fp->handle = ref->desc;
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001601 fp->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001602 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1603 &thread->todo);
1604
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001605 trace_binder_transaction_node_to_ref(t, node, ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001606 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001607 " node %d u%016llx -> ref %d desc %d\n",
1608 node->debug_id, (u64)node->ptr,
1609 ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001610 } break;
1611 case BINDER_TYPE_HANDLE:
1612 case BINDER_TYPE_WEAK_HANDLE: {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001613 struct binder_ref *ref;
1614
1615 ref = binder_get_ref(proc, fp->handle,
1616 fp->type == BINDER_TYPE_HANDLE);
Seunghun Lee10f62862014-05-01 01:30:23 +09001617
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001618 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001619 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301620 proc->pid,
1621 thread->pid, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001622 return_error = BR_FAILED_REPLY;
1623 goto err_binder_get_ref_failed;
1624 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001625 if (security_binder_transfer_binder(proc->tsk,
1626 target_proc->tsk)) {
1627 return_error = BR_FAILED_REPLY;
1628 goto err_binder_get_ref_failed;
1629 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001630 if (ref->node->proc == target_proc) {
1631 if (fp->type == BINDER_TYPE_HANDLE)
1632 fp->type = BINDER_TYPE_BINDER;
1633 else
1634 fp->type = BINDER_TYPE_WEAK_BINDER;
1635 fp->binder = ref->node->ptr;
1636 fp->cookie = ref->node->cookie;
1637 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001638 trace_binder_transaction_ref_to_node(t, ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001639 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001640 " ref %d desc %d -> node %d u%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001641 ref->debug_id, ref->desc, ref->node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001642 (u64)ref->node->ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001643 } else {
1644 struct binder_ref *new_ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001645
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001646 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1647 if (new_ref == NULL) {
1648 return_error = BR_FAILED_REPLY;
1649 goto err_binder_get_ref_for_node_failed;
1650 }
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001651 fp->binder = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001652 fp->handle = new_ref->desc;
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001653 fp->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001654 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001655 trace_binder_transaction_ref_to_ref(t, ref,
1656 new_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001657 binder_debug(BINDER_DEBUG_TRANSACTION,
1658 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1659 ref->debug_id, ref->desc, new_ref->debug_id,
1660 new_ref->desc, ref->node->debug_id);
1661 }
1662 } break;
1663
1664 case BINDER_TYPE_FD: {
1665 int target_fd;
1666 struct file *file;
1667
1668 if (reply) {
1669 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001670 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 +09001671 proc->pid, thread->pid, fp->handle);
1672 return_error = BR_FAILED_REPLY;
1673 goto err_fd_not_allowed;
1674 }
1675 } else if (!target_node->accept_fds) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001676 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 +09001677 proc->pid, thread->pid, fp->handle);
1678 return_error = BR_FAILED_REPLY;
1679 goto err_fd_not_allowed;
1680 }
1681
1682 file = fget(fp->handle);
1683 if (file == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001684 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001685 proc->pid, thread->pid, fp->handle);
1686 return_error = BR_FAILED_REPLY;
1687 goto err_fget_failed;
1688 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001689 if (security_binder_transfer_file(proc->tsk,
1690 target_proc->tsk,
1691 file) < 0) {
1692 fput(file);
1693 return_error = BR_FAILED_REPLY;
1694 goto err_get_unused_fd_failed;
1695 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001696 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1697 if (target_fd < 0) {
1698 fput(file);
1699 return_error = BR_FAILED_REPLY;
1700 goto err_get_unused_fd_failed;
1701 }
1702 task_fd_install(target_proc, target_fd, file);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001703 trace_binder_transaction_fd(t, fp->handle, target_fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001704 binder_debug(BINDER_DEBUG_TRANSACTION,
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001705 " fd %d -> %d\n", fp->handle, target_fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001706 /* TODO: fput? */
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001707 fp->binder = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001708 fp->handle = target_fd;
1709 } break;
1710
1711 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001712 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001713 proc->pid, thread->pid, fp->type);
1714 return_error = BR_FAILED_REPLY;
1715 goto err_bad_object_type;
1716 }
1717 }
1718 if (reply) {
1719 BUG_ON(t->buffer->async_transaction != 0);
1720 binder_pop_transaction(target_thread, in_reply_to);
1721 } else if (!(t->flags & TF_ONE_WAY)) {
1722 BUG_ON(t->buffer->async_transaction != 0);
1723 t->need_reply = 1;
1724 t->from_parent = thread->transaction_stack;
1725 thread->transaction_stack = t;
1726 } else {
1727 BUG_ON(target_node == NULL);
1728 BUG_ON(t->buffer->async_transaction != 1);
1729 if (target_node->has_async_transaction) {
1730 target_list = &target_node->async_todo;
1731 target_wait = NULL;
1732 } else
1733 target_node->has_async_transaction = 1;
1734 }
1735 t->work.type = BINDER_WORK_TRANSACTION;
1736 list_add_tail(&t->work.entry, target_list);
1737 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1738 list_add_tail(&tcomplete->entry, &thread->todo);
Riley Andrews8fb0b0c2017-06-29 12:01:37 -07001739 if (target_wait) {
1740 if (reply || !(t->flags & TF_ONE_WAY))
1741 wake_up_interruptible_sync(target_wait);
1742 else
1743 wake_up_interruptible(target_wait);
1744 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001745 return;
1746
1747err_get_unused_fd_failed:
1748err_fget_failed:
1749err_fd_not_allowed:
1750err_binder_get_ref_for_node_failed:
1751err_binder_get_ref_failed:
1752err_binder_new_node_failed:
1753err_bad_object_type:
1754err_bad_offset:
1755err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001756 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001757 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1758 t->buffer->transaction = NULL;
1759 binder_free_buf(target_proc, t->buffer);
1760err_binder_alloc_buf_failed:
1761 kfree(tcomplete);
1762 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1763err_alloc_tcomplete_failed:
1764 kfree(t);
1765 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1766err_alloc_t_failed:
1767err_bad_call_stack:
1768err_empty_call_stack:
1769err_dead_binder:
1770err_invalid_target_handle:
1771err_no_context_mgr_node:
1772 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001773 "%d:%d transaction failed %d, size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001774 proc->pid, thread->pid, return_error,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001775 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001776
1777 {
1778 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09001779
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001780 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1781 *fe = *e;
1782 }
1783
1784 BUG_ON(thread->return_error != BR_OK);
1785 if (in_reply_to) {
1786 thread->return_error = BR_TRANSACTION_COMPLETE;
1787 binder_send_failed_reply(in_reply_to, return_error);
1788 } else
1789 thread->return_error = return_error;
1790}
1791
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02001792static int binder_thread_write(struct binder_proc *proc,
1793 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001794 binder_uintptr_t binder_buffer, size_t size,
1795 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001796{
1797 uint32_t cmd;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001798 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001799 void __user *ptr = buffer + *consumed;
1800 void __user *end = buffer + size;
1801
1802 while (ptr < end && thread->return_error == BR_OK) {
1803 if (get_user(cmd, (uint32_t __user *)ptr))
1804 return -EFAULT;
1805 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001806 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001807 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1808 binder_stats.bc[_IOC_NR(cmd)]++;
1809 proc->stats.bc[_IOC_NR(cmd)]++;
1810 thread->stats.bc[_IOC_NR(cmd)]++;
1811 }
1812 switch (cmd) {
1813 case BC_INCREFS:
1814 case BC_ACQUIRE:
1815 case BC_RELEASE:
1816 case BC_DECREFS: {
1817 uint32_t target;
1818 struct binder_ref *ref;
1819 const char *debug_string;
1820
1821 if (get_user(target, (uint32_t __user *)ptr))
1822 return -EFAULT;
1823 ptr += sizeof(uint32_t);
1824 if (target == 0 && binder_context_mgr_node &&
1825 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1826 ref = binder_get_ref_for_node(proc,
1827 binder_context_mgr_node);
1828 if (ref->desc != target) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301829 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001830 proc->pid, thread->pid,
1831 ref->desc);
1832 }
1833 } else
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001834 ref = binder_get_ref(proc, target,
1835 cmd == BC_ACQUIRE ||
1836 cmd == BC_RELEASE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001837 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301838 binder_user_error("%d:%d refcount change on invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001839 proc->pid, thread->pid, target);
1840 break;
1841 }
1842 switch (cmd) {
1843 case BC_INCREFS:
1844 debug_string = "IncRefs";
1845 binder_inc_ref(ref, 0, NULL);
1846 break;
1847 case BC_ACQUIRE:
1848 debug_string = "Acquire";
1849 binder_inc_ref(ref, 1, NULL);
1850 break;
1851 case BC_RELEASE:
1852 debug_string = "Release";
1853 binder_dec_ref(ref, 1);
1854 break;
1855 case BC_DECREFS:
1856 default:
1857 debug_string = "DecRefs";
1858 binder_dec_ref(ref, 0);
1859 break;
1860 }
1861 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301862 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001863 proc->pid, thread->pid, debug_string, ref->debug_id,
1864 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1865 break;
1866 }
1867 case BC_INCREFS_DONE:
1868 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001869 binder_uintptr_t node_ptr;
1870 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001871 struct binder_node *node;
1872
Arve Hjønnevågda498892014-02-21 14:40:26 -08001873 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001874 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001875 ptr += sizeof(binder_uintptr_t);
1876 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001877 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001878 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001879 node = binder_get_node(proc, node_ptr);
1880 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001881 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001882 proc->pid, thread->pid,
1883 cmd == BC_INCREFS_DONE ?
1884 "BC_INCREFS_DONE" :
1885 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001886 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001887 break;
1888 }
1889 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001890 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001891 proc->pid, thread->pid,
1892 cmd == BC_INCREFS_DONE ?
1893 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001894 (u64)node_ptr, node->debug_id,
1895 (u64)cookie, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001896 break;
1897 }
1898 if (cmd == BC_ACQUIRE_DONE) {
1899 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301900 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001901 proc->pid, thread->pid,
1902 node->debug_id);
1903 break;
1904 }
1905 node->pending_strong_ref = 0;
1906 } else {
1907 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301908 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001909 proc->pid, thread->pid,
1910 node->debug_id);
1911 break;
1912 }
1913 node->pending_weak_ref = 0;
1914 }
1915 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1916 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301917 "%d:%d %s node %d ls %d lw %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001918 proc->pid, thread->pid,
1919 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1920 node->debug_id, node->local_strong_refs, node->local_weak_refs);
1921 break;
1922 }
1923 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05301924 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001925 return -EINVAL;
1926 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05301927 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001928 return -EINVAL;
1929
1930 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001931 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001932 struct binder_buffer *buffer;
1933
Arve Hjønnevågda498892014-02-21 14:40:26 -08001934 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001935 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001936 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001937
1938 buffer = binder_buffer_lookup(proc, data_ptr);
1939 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001940 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
1941 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001942 break;
1943 }
1944 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001945 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
1946 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001947 break;
1948 }
1949 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001950 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
1951 proc->pid, thread->pid, (u64)data_ptr,
1952 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001953 buffer->transaction ? "active" : "finished");
1954
1955 if (buffer->transaction) {
1956 buffer->transaction->buffer = NULL;
1957 buffer->transaction = NULL;
1958 }
1959 if (buffer->async_transaction && buffer->target_node) {
1960 BUG_ON(!buffer->target_node->has_async_transaction);
1961 if (list_empty(&buffer->target_node->async_todo))
1962 buffer->target_node->has_async_transaction = 0;
1963 else
1964 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1965 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001966 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001967 binder_transaction_buffer_release(proc, buffer, NULL);
1968 binder_free_buf(proc, buffer);
1969 break;
1970 }
1971
1972 case BC_TRANSACTION:
1973 case BC_REPLY: {
1974 struct binder_transaction_data tr;
1975
1976 if (copy_from_user(&tr, ptr, sizeof(tr)))
1977 return -EFAULT;
1978 ptr += sizeof(tr);
1979 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1980 break;
1981 }
1982
1983 case BC_REGISTER_LOOPER:
1984 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301985 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001986 proc->pid, thread->pid);
1987 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1988 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301989 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001990 proc->pid, thread->pid);
1991 } else if (proc->requested_threads == 0) {
1992 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301993 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001994 proc->pid, thread->pid);
1995 } else {
1996 proc->requested_threads--;
1997 proc->requested_threads_started++;
1998 }
1999 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
2000 break;
2001 case BC_ENTER_LOOPER:
2002 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302003 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002004 proc->pid, thread->pid);
2005 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
2006 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302007 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002008 proc->pid, thread->pid);
2009 }
2010 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
2011 break;
2012 case BC_EXIT_LOOPER:
2013 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302014 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002015 proc->pid, thread->pid);
2016 thread->looper |= BINDER_LOOPER_STATE_EXITED;
2017 break;
2018
2019 case BC_REQUEST_DEATH_NOTIFICATION:
2020 case BC_CLEAR_DEATH_NOTIFICATION: {
2021 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002022 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002023 struct binder_ref *ref;
2024 struct binder_ref_death *death;
2025
2026 if (get_user(target, (uint32_t __user *)ptr))
2027 return -EFAULT;
2028 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002029 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002030 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002031 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002032 ref = binder_get_ref(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002033 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302034 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002035 proc->pid, thread->pid,
2036 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2037 "BC_REQUEST_DEATH_NOTIFICATION" :
2038 "BC_CLEAR_DEATH_NOTIFICATION",
2039 target);
2040 break;
2041 }
2042
2043 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002044 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002045 proc->pid, thread->pid,
2046 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2047 "BC_REQUEST_DEATH_NOTIFICATION" :
2048 "BC_CLEAR_DEATH_NOTIFICATION",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002049 (u64)cookie, ref->debug_id, ref->desc,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002050 ref->strong, ref->weak, ref->node->debug_id);
2051
2052 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2053 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302054 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002055 proc->pid, thread->pid);
2056 break;
2057 }
2058 death = kzalloc(sizeof(*death), GFP_KERNEL);
2059 if (death == NULL) {
2060 thread->return_error = BR_ERROR;
2061 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302062 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002063 proc->pid, thread->pid);
2064 break;
2065 }
2066 binder_stats_created(BINDER_STAT_DEATH);
2067 INIT_LIST_HEAD(&death->work.entry);
2068 death->cookie = cookie;
2069 ref->death = death;
2070 if (ref->node->proc == NULL) {
2071 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2072 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2073 list_add_tail(&ref->death->work.entry, &thread->todo);
2074 } else {
2075 list_add_tail(&ref->death->work.entry, &proc->todo);
2076 wake_up_interruptible(&proc->wait);
2077 }
2078 }
2079 } else {
2080 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302081 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002082 proc->pid, thread->pid);
2083 break;
2084 }
2085 death = ref->death;
2086 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002087 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002088 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002089 (u64)death->cookie,
2090 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002091 break;
2092 }
2093 ref->death = NULL;
2094 if (list_empty(&death->work.entry)) {
2095 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2096 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2097 list_add_tail(&death->work.entry, &thread->todo);
2098 } else {
2099 list_add_tail(&death->work.entry, &proc->todo);
2100 wake_up_interruptible(&proc->wait);
2101 }
2102 } else {
2103 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2104 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2105 }
2106 }
2107 } break;
2108 case BC_DEAD_BINDER_DONE: {
2109 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002110 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002111 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09002112
Arve Hjønnevågda498892014-02-21 14:40:26 -08002113 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002114 return -EFAULT;
2115
Lisa Du7a64cd82016-02-17 09:32:52 +08002116 ptr += sizeof(cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002117 list_for_each_entry(w, &proc->delivered_death, entry) {
2118 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
Seunghun Lee10f62862014-05-01 01:30:23 +09002119
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002120 if (tmp_death->cookie == cookie) {
2121 death = tmp_death;
2122 break;
2123 }
2124 }
2125 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002126 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2127 proc->pid, thread->pid, (u64)cookie,
2128 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002129 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002130 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2131 proc->pid, thread->pid, (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002132 break;
2133 }
2134
2135 list_del_init(&death->work.entry);
2136 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2137 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2138 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2139 list_add_tail(&death->work.entry, &thread->todo);
2140 } else {
2141 list_add_tail(&death->work.entry, &proc->todo);
2142 wake_up_interruptible(&proc->wait);
2143 }
2144 }
2145 } break;
2146
2147 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302148 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002149 proc->pid, thread->pid, cmd);
2150 return -EINVAL;
2151 }
2152 *consumed = ptr - buffer;
2153 }
2154 return 0;
2155}
2156
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002157static void binder_stat_br(struct binder_proc *proc,
2158 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002159{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002160 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002161 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2162 binder_stats.br[_IOC_NR(cmd)]++;
2163 proc->stats.br[_IOC_NR(cmd)]++;
2164 thread->stats.br[_IOC_NR(cmd)]++;
2165 }
2166}
2167
2168static int binder_has_proc_work(struct binder_proc *proc,
2169 struct binder_thread *thread)
2170{
2171 return !list_empty(&proc->todo) ||
2172 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2173}
2174
2175static int binder_has_thread_work(struct binder_thread *thread)
2176{
2177 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2178 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2179}
2180
2181static int binder_thread_read(struct binder_proc *proc,
2182 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002183 binder_uintptr_t binder_buffer, size_t size,
2184 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002185{
Arve Hjønnevågda498892014-02-21 14:40:26 -08002186 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002187 void __user *ptr = buffer + *consumed;
2188 void __user *end = buffer + size;
2189
2190 int ret = 0;
2191 int wait_for_proc_work;
2192
2193 if (*consumed == 0) {
2194 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2195 return -EFAULT;
2196 ptr += sizeof(uint32_t);
2197 }
2198
2199retry:
2200 wait_for_proc_work = thread->transaction_stack == NULL &&
2201 list_empty(&thread->todo);
2202
2203 if (thread->return_error != BR_OK && ptr < end) {
2204 if (thread->return_error2 != BR_OK) {
2205 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2206 return -EFAULT;
2207 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002208 binder_stat_br(proc, thread, thread->return_error2);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002209 if (ptr == end)
2210 goto done;
2211 thread->return_error2 = BR_OK;
2212 }
2213 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2214 return -EFAULT;
2215 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002216 binder_stat_br(proc, thread, thread->return_error);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002217 thread->return_error = BR_OK;
2218 goto done;
2219 }
2220
2221
2222 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2223 if (wait_for_proc_work)
2224 proc->ready_threads++;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002225
2226 binder_unlock(__func__);
2227
2228 trace_binder_wait_for_work(wait_for_proc_work,
2229 !!thread->transaction_stack,
2230 !list_empty(&thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002231 if (wait_for_proc_work) {
2232 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2233 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302234 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 +09002235 proc->pid, thread->pid, thread->looper);
2236 wait_event_interruptible(binder_user_error_wait,
2237 binder_stop_on_user_error < 2);
2238 }
2239 binder_set_nice(proc->default_priority);
2240 if (non_block) {
2241 if (!binder_has_proc_work(proc, thread))
2242 ret = -EAGAIN;
2243 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002244 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002245 } else {
2246 if (non_block) {
2247 if (!binder_has_thread_work(thread))
2248 ret = -EAGAIN;
2249 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002250 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002251 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002252
2253 binder_lock(__func__);
2254
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002255 if (wait_for_proc_work)
2256 proc->ready_threads--;
2257 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2258
2259 if (ret)
2260 return ret;
2261
2262 while (1) {
2263 uint32_t cmd;
2264 struct binder_transaction_data tr;
2265 struct binder_work *w;
2266 struct binder_transaction *t = NULL;
2267
Dmitry Voytik395262a2014-09-08 18:16:34 +04002268 if (!list_empty(&thread->todo)) {
2269 w = list_first_entry(&thread->todo, struct binder_work,
2270 entry);
2271 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2272 w = list_first_entry(&proc->todo, struct binder_work,
2273 entry);
2274 } else {
2275 /* no data added */
2276 if (ptr - buffer == 4 &&
2277 !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002278 goto retry;
2279 break;
2280 }
2281
2282 if (end - ptr < sizeof(tr) + 4)
2283 break;
2284
2285 switch (w->type) {
2286 case BINDER_WORK_TRANSACTION: {
2287 t = container_of(w, struct binder_transaction, work);
2288 } break;
2289 case BINDER_WORK_TRANSACTION_COMPLETE: {
2290 cmd = BR_TRANSACTION_COMPLETE;
2291 if (put_user(cmd, (uint32_t __user *)ptr))
2292 return -EFAULT;
2293 ptr += sizeof(uint32_t);
2294
2295 binder_stat_br(proc, thread, cmd);
2296 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302297 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002298 proc->pid, thread->pid);
2299
2300 list_del(&w->entry);
2301 kfree(w);
2302 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2303 } break;
2304 case BINDER_WORK_NODE: {
2305 struct binder_node *node = container_of(w, struct binder_node, work);
2306 uint32_t cmd = BR_NOOP;
2307 const char *cmd_name;
2308 int strong = node->internal_strong_refs || node->local_strong_refs;
2309 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
Seunghun Lee10f62862014-05-01 01:30:23 +09002310
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002311 if (weak && !node->has_weak_ref) {
2312 cmd = BR_INCREFS;
2313 cmd_name = "BR_INCREFS";
2314 node->has_weak_ref = 1;
2315 node->pending_weak_ref = 1;
2316 node->local_weak_refs++;
2317 } else if (strong && !node->has_strong_ref) {
2318 cmd = BR_ACQUIRE;
2319 cmd_name = "BR_ACQUIRE";
2320 node->has_strong_ref = 1;
2321 node->pending_strong_ref = 1;
2322 node->local_strong_refs++;
2323 } else if (!strong && node->has_strong_ref) {
2324 cmd = BR_RELEASE;
2325 cmd_name = "BR_RELEASE";
2326 node->has_strong_ref = 0;
2327 } else if (!weak && node->has_weak_ref) {
2328 cmd = BR_DECREFS;
2329 cmd_name = "BR_DECREFS";
2330 node->has_weak_ref = 0;
2331 }
2332 if (cmd != BR_NOOP) {
2333 if (put_user(cmd, (uint32_t __user *)ptr))
2334 return -EFAULT;
2335 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002336 if (put_user(node->ptr,
2337 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002338 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002339 ptr += sizeof(binder_uintptr_t);
2340 if (put_user(node->cookie,
2341 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002342 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002343 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002344
2345 binder_stat_br(proc, thread, cmd);
2346 binder_debug(BINDER_DEBUG_USER_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002347 "%d:%d %s %d u%016llx c%016llx\n",
2348 proc->pid, thread->pid, cmd_name,
2349 node->debug_id,
2350 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002351 } else {
2352 list_del_init(&w->entry);
2353 if (!weak && !strong) {
2354 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002355 "%d:%d node %d u%016llx c%016llx deleted\n",
2356 proc->pid, thread->pid,
2357 node->debug_id,
2358 (u64)node->ptr,
2359 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002360 rb_erase(&node->rb_node, &proc->nodes);
2361 kfree(node);
2362 binder_stats_deleted(BINDER_STAT_NODE);
2363 } else {
2364 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002365 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2366 proc->pid, thread->pid,
2367 node->debug_id,
2368 (u64)node->ptr,
2369 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002370 }
2371 }
2372 } break;
2373 case BINDER_WORK_DEAD_BINDER:
2374 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2375 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2376 struct binder_ref_death *death;
2377 uint32_t cmd;
2378
2379 death = container_of(w, struct binder_ref_death, work);
2380 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2381 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2382 else
2383 cmd = BR_DEAD_BINDER;
2384 if (put_user(cmd, (uint32_t __user *)ptr))
2385 return -EFAULT;
2386 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002387 if (put_user(death->cookie,
2388 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002389 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002390 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002391 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002392 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002393 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002394 proc->pid, thread->pid,
2395 cmd == BR_DEAD_BINDER ?
2396 "BR_DEAD_BINDER" :
2397 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002398 (u64)death->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002399
2400 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2401 list_del(&w->entry);
2402 kfree(death);
2403 binder_stats_deleted(BINDER_STAT_DEATH);
2404 } else
2405 list_move(&w->entry, &proc->delivered_death);
2406 if (cmd == BR_DEAD_BINDER)
2407 goto done; /* DEAD_BINDER notifications can cause transactions */
2408 } break;
2409 }
2410
2411 if (!t)
2412 continue;
2413
2414 BUG_ON(t->buffer == NULL);
2415 if (t->buffer->target_node) {
2416 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002417
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002418 tr.target.ptr = target_node->ptr;
2419 tr.cookie = target_node->cookie;
2420 t->saved_priority = task_nice(current);
2421 if (t->priority < target_node->min_priority &&
2422 !(t->flags & TF_ONE_WAY))
2423 binder_set_nice(t->priority);
2424 else if (!(t->flags & TF_ONE_WAY) ||
2425 t->saved_priority > target_node->min_priority)
2426 binder_set_nice(target_node->min_priority);
2427 cmd = BR_TRANSACTION;
2428 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002429 tr.target.ptr = 0;
2430 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002431 cmd = BR_REPLY;
2432 }
2433 tr.code = t->code;
2434 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002435 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002436
2437 if (t->from) {
2438 struct task_struct *sender = t->from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09002439
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002440 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002441 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002442 } else {
2443 tr.sender_pid = 0;
2444 }
2445
2446 tr.data_size = t->buffer->data_size;
2447 tr.offsets_size = t->buffer->offsets_size;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002448 tr.data.ptr.buffer = (binder_uintptr_t)(
2449 (uintptr_t)t->buffer->data +
2450 proc->user_buffer_offset);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002451 tr.data.ptr.offsets = tr.data.ptr.buffer +
2452 ALIGN(t->buffer->data_size,
2453 sizeof(void *));
2454
2455 if (put_user(cmd, (uint32_t __user *)ptr))
2456 return -EFAULT;
2457 ptr += sizeof(uint32_t);
2458 if (copy_to_user(ptr, &tr, sizeof(tr)))
2459 return -EFAULT;
2460 ptr += sizeof(tr);
2461
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002462 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002463 binder_stat_br(proc, thread, cmd);
2464 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002465 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002466 proc->pid, thread->pid,
2467 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2468 "BR_REPLY",
2469 t->debug_id, t->from ? t->from->proc->pid : 0,
2470 t->from ? t->from->pid : 0, cmd,
2471 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002472 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002473
2474 list_del(&t->work.entry);
2475 t->buffer->allow_user_free = 1;
2476 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2477 t->to_parent = thread->transaction_stack;
2478 t->to_thread = thread;
2479 thread->transaction_stack = t;
2480 } else {
2481 t->buffer->transaction = NULL;
2482 kfree(t);
2483 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2484 }
2485 break;
2486 }
2487
2488done:
2489
2490 *consumed = ptr - buffer;
2491 if (proc->requested_threads + proc->ready_threads == 0 &&
2492 proc->requested_threads_started < proc->max_threads &&
2493 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2494 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2495 /*spawn a new thread if we leave this out */) {
2496 proc->requested_threads++;
2497 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302498 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002499 proc->pid, thread->pid);
2500 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2501 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002502 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002503 }
2504 return 0;
2505}
2506
2507static void binder_release_work(struct list_head *list)
2508{
2509 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09002510
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002511 while (!list_empty(list)) {
2512 w = list_first_entry(list, struct binder_work, entry);
2513 list_del_init(&w->entry);
2514 switch (w->type) {
2515 case BINDER_WORK_TRANSACTION: {
2516 struct binder_transaction *t;
2517
2518 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002519 if (t->buffer->target_node &&
2520 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002521 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002522 } else {
2523 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302524 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002525 t->debug_id);
2526 t->buffer->transaction = NULL;
2527 kfree(t);
2528 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2529 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002530 } break;
2531 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002532 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302533 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002534 kfree(w);
2535 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2536 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002537 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2538 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2539 struct binder_ref_death *death;
2540
2541 death = container_of(w, struct binder_ref_death, work);
2542 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002543 "undelivered death notification, %016llx\n",
2544 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002545 kfree(death);
2546 binder_stats_deleted(BINDER_STAT_DEATH);
2547 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002548 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302549 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002550 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002551 break;
2552 }
2553 }
2554
2555}
2556
2557static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2558{
2559 struct binder_thread *thread = NULL;
2560 struct rb_node *parent = NULL;
2561 struct rb_node **p = &proc->threads.rb_node;
2562
2563 while (*p) {
2564 parent = *p;
2565 thread = rb_entry(parent, struct binder_thread, rb_node);
2566
2567 if (current->pid < thread->pid)
2568 p = &(*p)->rb_left;
2569 else if (current->pid > thread->pid)
2570 p = &(*p)->rb_right;
2571 else
2572 break;
2573 }
2574 if (*p == NULL) {
2575 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2576 if (thread == NULL)
2577 return NULL;
2578 binder_stats_created(BINDER_STAT_THREAD);
2579 thread->proc = proc;
2580 thread->pid = current->pid;
2581 init_waitqueue_head(&thread->wait);
2582 INIT_LIST_HEAD(&thread->todo);
2583 rb_link_node(&thread->rb_node, parent, p);
2584 rb_insert_color(&thread->rb_node, &proc->threads);
2585 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2586 thread->return_error = BR_OK;
2587 thread->return_error2 = BR_OK;
2588 }
2589 return thread;
2590}
2591
2592static int binder_free_thread(struct binder_proc *proc,
2593 struct binder_thread *thread)
2594{
2595 struct binder_transaction *t;
2596 struct binder_transaction *send_reply = NULL;
2597 int active_transactions = 0;
2598
2599 rb_erase(&thread->rb_node, &proc->threads);
2600 t = thread->transaction_stack;
2601 if (t && t->to_thread == thread)
2602 send_reply = t;
2603 while (t) {
2604 active_transactions++;
2605 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302606 "release %d:%d transaction %d %s, still active\n",
2607 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002608 t->debug_id,
2609 (t->to_thread == thread) ? "in" : "out");
2610
2611 if (t->to_thread == thread) {
2612 t->to_proc = NULL;
2613 t->to_thread = NULL;
2614 if (t->buffer) {
2615 t->buffer->transaction = NULL;
2616 t->buffer = NULL;
2617 }
2618 t = t->to_parent;
2619 } else if (t->from == thread) {
2620 t->from = NULL;
2621 t = t->from_parent;
2622 } else
2623 BUG();
2624 }
2625 if (send_reply)
2626 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2627 binder_release_work(&thread->todo);
2628 kfree(thread);
2629 binder_stats_deleted(BINDER_STAT_THREAD);
2630 return active_transactions;
2631}
2632
2633static unsigned int binder_poll(struct file *filp,
2634 struct poll_table_struct *wait)
2635{
2636 struct binder_proc *proc = filp->private_data;
2637 struct binder_thread *thread = NULL;
2638 int wait_for_proc_work;
2639
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002640 binder_lock(__func__);
2641
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002642 thread = binder_get_thread(proc);
Eric Biggersfebf1082018-02-26 10:56:45 -08002643 if (!thread) {
2644 binder_unlock(__func__);
Eric Biggers4be5a282018-01-30 23:11:24 -08002645 return POLLERR;
Eric Biggersfebf1082018-02-26 10:56:45 -08002646 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002647
2648 wait_for_proc_work = thread->transaction_stack == NULL &&
2649 list_empty(&thread->todo) && thread->return_error == BR_OK;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002650
2651 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002652
2653 if (wait_for_proc_work) {
2654 if (binder_has_proc_work(proc, thread))
2655 return POLLIN;
2656 poll_wait(filp, &proc->wait, wait);
2657 if (binder_has_proc_work(proc, thread))
2658 return POLLIN;
2659 } else {
2660 if (binder_has_thread_work(thread))
2661 return POLLIN;
2662 poll_wait(filp, &thread->wait, wait);
2663 if (binder_has_thread_work(thread))
2664 return POLLIN;
2665 }
2666 return 0;
2667}
2668
Tair Rzayev78260ac2014-06-03 22:27:21 +03002669static int binder_ioctl_write_read(struct file *filp,
2670 unsigned int cmd, unsigned long arg,
2671 struct binder_thread *thread)
2672{
2673 int ret = 0;
2674 struct binder_proc *proc = filp->private_data;
2675 unsigned int size = _IOC_SIZE(cmd);
2676 void __user *ubuf = (void __user *)arg;
2677 struct binder_write_read bwr;
2678
2679 if (size != sizeof(struct binder_write_read)) {
2680 ret = -EINVAL;
2681 goto out;
2682 }
2683 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2684 ret = -EFAULT;
2685 goto out;
2686 }
2687 binder_debug(BINDER_DEBUG_READ_WRITE,
2688 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2689 proc->pid, thread->pid,
2690 (u64)bwr.write_size, (u64)bwr.write_buffer,
2691 (u64)bwr.read_size, (u64)bwr.read_buffer);
2692
2693 if (bwr.write_size > 0) {
2694 ret = binder_thread_write(proc, thread,
2695 bwr.write_buffer,
2696 bwr.write_size,
2697 &bwr.write_consumed);
2698 trace_binder_write_done(ret);
2699 if (ret < 0) {
2700 bwr.read_consumed = 0;
2701 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2702 ret = -EFAULT;
2703 goto out;
2704 }
2705 }
2706 if (bwr.read_size > 0) {
2707 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2708 bwr.read_size,
2709 &bwr.read_consumed,
2710 filp->f_flags & O_NONBLOCK);
2711 trace_binder_read_done(ret);
2712 if (!list_empty(&proc->todo))
2713 wake_up_interruptible(&proc->wait);
2714 if (ret < 0) {
2715 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2716 ret = -EFAULT;
2717 goto out;
2718 }
2719 }
2720 binder_debug(BINDER_DEBUG_READ_WRITE,
2721 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2722 proc->pid, thread->pid,
2723 (u64)bwr.write_consumed, (u64)bwr.write_size,
2724 (u64)bwr.read_consumed, (u64)bwr.read_size);
2725 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2726 ret = -EFAULT;
2727 goto out;
2728 }
2729out:
2730 return ret;
2731}
2732
2733static int binder_ioctl_set_ctx_mgr(struct file *filp)
2734{
2735 int ret = 0;
2736 struct binder_proc *proc = filp->private_data;
2737 kuid_t curr_euid = current_euid();
2738
2739 if (binder_context_mgr_node != NULL) {
2740 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2741 ret = -EBUSY;
2742 goto out;
2743 }
Stephen Smalley79af7302015-01-21 10:54:10 -05002744 ret = security_binder_set_context_mgr(proc->tsk);
2745 if (ret < 0)
2746 goto out;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002747 if (uid_valid(binder_context_mgr_uid)) {
2748 if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
2749 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2750 from_kuid(&init_user_ns, curr_euid),
2751 from_kuid(&init_user_ns,
2752 binder_context_mgr_uid));
2753 ret = -EPERM;
2754 goto out;
2755 }
2756 } else {
2757 binder_context_mgr_uid = curr_euid;
2758 }
2759 binder_context_mgr_node = binder_new_node(proc, 0, 0);
2760 if (binder_context_mgr_node == NULL) {
2761 ret = -ENOMEM;
2762 goto out;
2763 }
2764 binder_context_mgr_node->local_weak_refs++;
2765 binder_context_mgr_node->local_strong_refs++;
2766 binder_context_mgr_node->has_strong_ref = 1;
2767 binder_context_mgr_node->has_weak_ref = 1;
2768out:
2769 return ret;
2770}
2771
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002772static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2773{
2774 int ret;
2775 struct binder_proc *proc = filp->private_data;
2776 struct binder_thread *thread;
2777 unsigned int size = _IOC_SIZE(cmd);
2778 void __user *ubuf = (void __user *)arg;
2779
Tair Rzayev78260ac2014-06-03 22:27:21 +03002780 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2781 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002782
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002783 trace_binder_ioctl(cmd, arg);
2784
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002785 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2786 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002787 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002788
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002789 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002790 thread = binder_get_thread(proc);
2791 if (thread == NULL) {
2792 ret = -ENOMEM;
2793 goto err;
2794 }
2795
2796 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002797 case BINDER_WRITE_READ:
2798 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
2799 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002800 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002801 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002802 case BINDER_SET_MAX_THREADS:
2803 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2804 ret = -EINVAL;
2805 goto err;
2806 }
2807 break;
2808 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03002809 ret = binder_ioctl_set_ctx_mgr(filp);
2810 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002811 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002812 break;
2813 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302814 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002815 proc->pid, thread->pid);
2816 binder_free_thread(proc, thread);
2817 thread = NULL;
2818 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002819 case BINDER_VERSION: {
2820 struct binder_version __user *ver = ubuf;
2821
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002822 if (size != sizeof(struct binder_version)) {
2823 ret = -EINVAL;
2824 goto err;
2825 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02002826 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2827 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002828 ret = -EINVAL;
2829 goto err;
2830 }
2831 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002832 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002833 default:
2834 ret = -EINVAL;
2835 goto err;
2836 }
2837 ret = 0;
2838err:
2839 if (thread)
2840 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002841 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002842 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2843 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05302844 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 -07002845err_unlocked:
2846 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002847 return ret;
2848}
2849
2850static void binder_vma_open(struct vm_area_struct *vma)
2851{
2852 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002853
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002854 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302855 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002856 proc->pid, vma->vm_start, vma->vm_end,
2857 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2858 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002859}
2860
2861static void binder_vma_close(struct vm_area_struct *vma)
2862{
2863 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002864
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002865 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302866 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002867 proc->pid, vma->vm_start, vma->vm_end,
2868 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2869 (unsigned long)pgprot_val(vma->vm_page_prot));
2870 proc->vma = NULL;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08002871 proc->vma_vm_mm = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002872 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2873}
2874
Vinayak Menonddac7d52014-06-02 18:17:59 +05302875static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2876{
2877 return VM_FAULT_SIGBUS;
2878}
2879
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07002880static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002881 .open = binder_vma_open,
2882 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05302883 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002884};
2885
2886static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2887{
2888 int ret;
2889 struct vm_struct *area;
2890 struct binder_proc *proc = filp->private_data;
2891 const char *failure_string;
2892 struct binder_buffer *buffer;
2893
Martijn Coenencbd854d2017-07-28 13:56:08 +02002894 if (proc->tsk != current->group_leader)
Al Viroa79f41e2012-08-15 18:23:36 -04002895 return -EINVAL;
2896
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002897 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2898 vma->vm_end = vma->vm_start + SZ_4M;
2899
2900 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2901 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2902 proc->pid, vma->vm_start, vma->vm_end,
2903 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2904 (unsigned long)pgprot_val(vma->vm_page_prot));
2905
2906 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2907 ret = -EPERM;
2908 failure_string = "bad vm_flags";
2909 goto err_bad_arg;
2910 }
2911 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2912
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002913 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002914 if (proc->buffer) {
2915 ret = -EBUSY;
2916 failure_string = "already mapped";
2917 goto err_already_mapped;
2918 }
2919
2920 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2921 if (area == NULL) {
2922 ret = -ENOMEM;
2923 failure_string = "get_vm_area";
2924 goto err_get_vm_area_failed;
2925 }
2926 proc->buffer = area->addr;
2927 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002928 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002929
2930#ifdef CONFIG_CPU_CACHE_VIPT
2931 if (cache_is_vipt_aliasing()) {
2932 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
Sherwin Soltani258767f2012-06-26 02:00:30 -04002933 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 +09002934 vma->vm_start += PAGE_SIZE;
2935 }
2936 }
2937#endif
2938 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2939 if (proc->pages == NULL) {
2940 ret = -ENOMEM;
2941 failure_string = "alloc page array";
2942 goto err_alloc_pages_failed;
2943 }
2944 proc->buffer_size = vma->vm_end - vma->vm_start;
2945
2946 vma->vm_ops = &binder_vm_ops;
2947 vma->vm_private_data = proc;
2948
2949 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2950 ret = -ENOMEM;
2951 failure_string = "alloc small buf";
2952 goto err_alloc_small_buf_failed;
2953 }
2954 buffer = proc->buffer;
2955 INIT_LIST_HEAD(&proc->buffers);
2956 list_add(&buffer->entry, &proc->buffers);
2957 buffer->free = 1;
2958 binder_insert_free_buffer(proc, buffer);
2959 proc->free_async_space = proc->buffer_size / 2;
2960 barrier();
Todd Kjosc0d75da2017-11-27 09:32:33 -08002961 mutex_lock(&proc->files_lock);
Al Viroa79f41e2012-08-15 18:23:36 -04002962 proc->files = get_files_struct(current);
Todd Kjosc0d75da2017-11-27 09:32:33 -08002963 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002964 proc->vma = vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08002965 proc->vma_vm_mm = vma->vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002966
Sherwin Soltani258767f2012-06-26 02:00:30 -04002967 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002968 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2969 return 0;
2970
2971err_alloc_small_buf_failed:
2972 kfree(proc->pages);
2973 proc->pages = NULL;
2974err_alloc_pages_failed:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002975 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002976 vfree(proc->buffer);
2977 proc->buffer = NULL;
2978err_get_vm_area_failed:
2979err_already_mapped:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002980 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002981err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04002982 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002983 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
2984 return ret;
2985}
2986
2987static int binder_open(struct inode *nodp, struct file *filp)
2988{
2989 struct binder_proc *proc;
2990
2991 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
2992 current->group_leader->pid, current->pid);
2993
2994 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
2995 if (proc == NULL)
2996 return -ENOMEM;
Todd Kjos51050752017-06-29 12:01:36 -07002997 get_task_struct(current->group_leader);
2998 proc->tsk = current->group_leader;
Todd Kjosc0d75da2017-11-27 09:32:33 -08002999 mutex_init(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003000 INIT_LIST_HEAD(&proc->todo);
3001 init_waitqueue_head(&proc->wait);
3002 proc->default_priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003003
3004 binder_lock(__func__);
3005
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003006 binder_stats_created(BINDER_STAT_PROC);
3007 hlist_add_head(&proc->proc_node, &binder_procs);
3008 proc->pid = current->group_leader->pid;
3009 INIT_LIST_HEAD(&proc->delivered_death);
3010 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003011
3012 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003013
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003014 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003015 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09003016
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003017 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003018 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
3019 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003020 }
3021
3022 return 0;
3023}
3024
3025static int binder_flush(struct file *filp, fl_owner_t id)
3026{
3027 struct binder_proc *proc = filp->private_data;
3028
3029 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3030
3031 return 0;
3032}
3033
3034static void binder_deferred_flush(struct binder_proc *proc)
3035{
3036 struct rb_node *n;
3037 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09003038
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003039 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3040 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09003041
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003042 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3043 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3044 wake_up_interruptible(&thread->wait);
3045 wake_count++;
3046 }
3047 }
3048 wake_up_interruptible_all(&proc->wait);
3049
3050 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3051 "binder_flush: %d woke %d threads\n", proc->pid,
3052 wake_count);
3053}
3054
3055static int binder_release(struct inode *nodp, struct file *filp)
3056{
3057 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003058
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003059 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003060 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3061
3062 return 0;
3063}
3064
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003065static int binder_node_release(struct binder_node *node, int refs)
3066{
3067 struct binder_ref *ref;
3068 int death = 0;
3069
3070 list_del_init(&node->work.entry);
3071 binder_release_work(&node->async_todo);
3072
3073 if (hlist_empty(&node->refs)) {
3074 kfree(node);
3075 binder_stats_deleted(BINDER_STAT_NODE);
3076
3077 return refs;
3078 }
3079
3080 node->proc = NULL;
3081 node->local_strong_refs = 0;
3082 node->local_weak_refs = 0;
3083 hlist_add_head(&node->dead_node, &binder_dead_nodes);
3084
3085 hlist_for_each_entry(ref, &node->refs, node_entry) {
3086 refs++;
3087
3088 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08003089 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003090
3091 death++;
3092
3093 if (list_empty(&ref->death->work.entry)) {
3094 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3095 list_add_tail(&ref->death->work.entry,
3096 &ref->proc->todo);
3097 wake_up_interruptible(&ref->proc->wait);
3098 } else
3099 BUG();
3100 }
3101
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003102 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3103 "node %d now dead, refs %d, death %d\n",
3104 node->debug_id, refs, death);
3105
3106 return refs;
3107}
3108
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003109static void binder_deferred_release(struct binder_proc *proc)
3110{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003111 struct binder_transaction *t;
3112 struct rb_node *n;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003113 int threads, nodes, incoming_refs, outgoing_refs, buffers,
3114 active_transactions, page_count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003115
3116 BUG_ON(proc->vma);
3117 BUG_ON(proc->files);
3118
3119 hlist_del(&proc->proc_node);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003120
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003121 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
3122 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003123 "%s: %d context_mgr_node gone\n",
3124 __func__, proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003125 binder_context_mgr_node = NULL;
3126 }
3127
3128 threads = 0;
3129 active_transactions = 0;
3130 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003131 struct binder_thread *thread;
3132
3133 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003134 threads++;
3135 active_transactions += binder_free_thread(proc, thread);
3136 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003137
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003138 nodes = 0;
3139 incoming_refs = 0;
3140 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003141 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003142
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003143 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003144 nodes++;
3145 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003146 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003147 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003148
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003149 outgoing_refs = 0;
3150 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003151 struct binder_ref *ref;
3152
3153 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003154 outgoing_refs++;
3155 binder_delete_ref(ref);
3156 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003157
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003158 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003159 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003160
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003161 buffers = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003162 while ((n = rb_first(&proc->allocated_buffers))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003163 struct binder_buffer *buffer;
3164
3165 buffer = rb_entry(n, struct binder_buffer, rb_node);
3166
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003167 t = buffer->transaction;
3168 if (t) {
3169 t->buffer = NULL;
3170 buffer->transaction = NULL;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303171 pr_err("release proc %d, transaction %d, not freed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003172 proc->pid, t->debug_id);
3173 /*BUG();*/
3174 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003175
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003176 binder_free_buf(proc, buffer);
3177 buffers++;
3178 }
3179
3180 binder_stats_deleted(BINDER_STAT_PROC);
3181
3182 page_count = 0;
3183 if (proc->pages) {
3184 int i;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003185
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003186 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003187 void *page_addr;
3188
3189 if (!proc->pages[i])
3190 continue;
3191
3192 page_addr = proc->buffer + i * PAGE_SIZE;
3193 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003194 "%s: %d: page %d at %p not freed\n",
3195 __func__, proc->pid, i, page_addr);
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003196 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3197 __free_page(proc->pages[i]);
3198 page_count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003199 }
3200 kfree(proc->pages);
3201 vfree(proc->buffer);
3202 }
3203
3204 put_task_struct(proc->tsk);
3205
3206 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003207 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3208 __func__, proc->pid, threads, nodes, incoming_refs,
3209 outgoing_refs, active_transactions, buffers, page_count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003210
3211 kfree(proc);
3212}
3213
3214static void binder_deferred_func(struct work_struct *work)
3215{
3216 struct binder_proc *proc;
3217 struct files_struct *files;
3218
3219 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003220
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003221 do {
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003222 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003223 mutex_lock(&binder_deferred_lock);
3224 if (!hlist_empty(&binder_deferred_list)) {
3225 proc = hlist_entry(binder_deferred_list.first,
3226 struct binder_proc, deferred_work_node);
3227 hlist_del_init(&proc->deferred_work_node);
3228 defer = proc->deferred_work;
3229 proc->deferred_work = 0;
3230 } else {
3231 proc = NULL;
3232 defer = 0;
3233 }
3234 mutex_unlock(&binder_deferred_lock);
3235
3236 files = NULL;
3237 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjosc0d75da2017-11-27 09:32:33 -08003238 mutex_lock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003239 files = proc->files;
3240 if (files)
3241 proc->files = NULL;
Todd Kjosc0d75da2017-11-27 09:32:33 -08003242 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003243 }
3244
3245 if (defer & BINDER_DEFERRED_FLUSH)
3246 binder_deferred_flush(proc);
3247
3248 if (defer & BINDER_DEFERRED_RELEASE)
3249 binder_deferred_release(proc); /* frees proc */
3250
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003251 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003252 if (files)
3253 put_files_struct(files);
3254 } while (proc);
3255}
3256static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3257
3258static void
3259binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3260{
3261 mutex_lock(&binder_deferred_lock);
3262 proc->deferred_work |= defer;
3263 if (hlist_unhashed(&proc->deferred_work_node)) {
3264 hlist_add_head(&proc->deferred_work_node,
3265 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05303266 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003267 }
3268 mutex_unlock(&binder_deferred_lock);
3269}
3270
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003271static void print_binder_transaction(struct seq_file *m, const char *prefix,
3272 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003273{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003274 seq_printf(m,
3275 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3276 prefix, t->debug_id, t,
3277 t->from ? t->from->proc->pid : 0,
3278 t->from ? t->from->pid : 0,
3279 t->to_proc ? t->to_proc->pid : 0,
3280 t->to_thread ? t->to_thread->pid : 0,
3281 t->code, t->flags, t->priority, t->need_reply);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003282 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003283 seq_puts(m, " buffer free\n");
3284 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003285 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003286 if (t->buffer->target_node)
3287 seq_printf(m, " node %d",
3288 t->buffer->target_node->debug_id);
3289 seq_printf(m, " size %zd:%zd data %p\n",
3290 t->buffer->data_size, t->buffer->offsets_size,
3291 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003292}
3293
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003294static void print_binder_buffer(struct seq_file *m, const char *prefix,
3295 struct binder_buffer *buffer)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003296{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003297 seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
3298 prefix, buffer->debug_id, buffer->data,
3299 buffer->data_size, buffer->offsets_size,
3300 buffer->transaction ? "active" : "delivered");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003301}
3302
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003303static void print_binder_work(struct seq_file *m, const char *prefix,
3304 const char *transaction_prefix,
3305 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003306{
3307 struct binder_node *node;
3308 struct binder_transaction *t;
3309
3310 switch (w->type) {
3311 case BINDER_WORK_TRANSACTION:
3312 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003313 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003314 break;
3315 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003316 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003317 break;
3318 case BINDER_WORK_NODE:
3319 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003320 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3321 prefix, node->debug_id,
3322 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003323 break;
3324 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003325 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003326 break;
3327 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003328 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003329 break;
3330 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003331 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003332 break;
3333 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003334 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003335 break;
3336 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003337}
3338
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003339static void print_binder_thread(struct seq_file *m,
3340 struct binder_thread *thread,
3341 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003342{
3343 struct binder_transaction *t;
3344 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003345 size_t start_pos = m->count;
3346 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003347
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003348 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3349 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003350 t = thread->transaction_stack;
3351 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003352 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003353 print_binder_transaction(m,
3354 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003355 t = t->from_parent;
3356 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003357 print_binder_transaction(m,
3358 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003359 t = t->to_parent;
3360 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003361 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003362 t = NULL;
3363 }
3364 }
3365 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003366 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003367 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003368 if (!print_always && m->count == header_pos)
3369 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003370}
3371
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003372static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003373{
3374 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003375 struct binder_work *w;
3376 int count;
3377
3378 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003379 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003380 count++;
3381
Arve Hjønnevågda498892014-02-21 14:40:26 -08003382 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3383 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003384 node->has_strong_ref, node->has_weak_ref,
3385 node->local_strong_refs, node->local_weak_refs,
3386 node->internal_strong_refs, count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003387 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003388 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003389 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003390 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003391 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003392 seq_puts(m, "\n");
3393 list_for_each_entry(w, &node->async_todo, entry)
3394 print_binder_work(m, " ",
3395 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396}
3397
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003398static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003399{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003400 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3401 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3402 ref->node->debug_id, ref->strong, ref->weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003403}
3404
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003405static void print_binder_proc(struct seq_file *m,
3406 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003407{
3408 struct binder_work *w;
3409 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003410 size_t start_pos = m->count;
3411 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003412
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003413 seq_printf(m, "proc %d\n", proc->pid);
3414 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003415
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003416 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3417 print_binder_thread(m, rb_entry(n, struct binder_thread,
3418 rb_node), print_all);
3419 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003420 struct binder_node *node = rb_entry(n, struct binder_node,
3421 rb_node);
3422 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003423 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003424 }
3425 if (print_all) {
3426 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003427 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003428 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003429 print_binder_ref(m, rb_entry(n, struct binder_ref,
3430 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003431 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003432 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3433 print_binder_buffer(m, " buffer",
3434 rb_entry(n, struct binder_buffer, rb_node));
3435 list_for_each_entry(w, &proc->todo, entry)
3436 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003437 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003438 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003439 break;
3440 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003441 if (!print_all && m->count == header_pos)
3442 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003443}
3444
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003445static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003446 "BR_ERROR",
3447 "BR_OK",
3448 "BR_TRANSACTION",
3449 "BR_REPLY",
3450 "BR_ACQUIRE_RESULT",
3451 "BR_DEAD_REPLY",
3452 "BR_TRANSACTION_COMPLETE",
3453 "BR_INCREFS",
3454 "BR_ACQUIRE",
3455 "BR_RELEASE",
3456 "BR_DECREFS",
3457 "BR_ATTEMPT_ACQUIRE",
3458 "BR_NOOP",
3459 "BR_SPAWN_LOOPER",
3460 "BR_FINISHED",
3461 "BR_DEAD_BINDER",
3462 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3463 "BR_FAILED_REPLY"
3464};
3465
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003466static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003467 "BC_TRANSACTION",
3468 "BC_REPLY",
3469 "BC_ACQUIRE_RESULT",
3470 "BC_FREE_BUFFER",
3471 "BC_INCREFS",
3472 "BC_ACQUIRE",
3473 "BC_RELEASE",
3474 "BC_DECREFS",
3475 "BC_INCREFS_DONE",
3476 "BC_ACQUIRE_DONE",
3477 "BC_ATTEMPT_ACQUIRE",
3478 "BC_REGISTER_LOOPER",
3479 "BC_ENTER_LOOPER",
3480 "BC_EXIT_LOOPER",
3481 "BC_REQUEST_DEATH_NOTIFICATION",
3482 "BC_CLEAR_DEATH_NOTIFICATION",
3483 "BC_DEAD_BINDER_DONE"
3484};
3485
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003486static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003487 "proc",
3488 "thread",
3489 "node",
3490 "ref",
3491 "death",
3492 "transaction",
3493 "transaction_complete"
3494};
3495
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003496static void print_binder_stats(struct seq_file *m, const char *prefix,
3497 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003498{
3499 int i;
3500
3501 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003502 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003503 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3504 if (stats->bc[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003505 seq_printf(m, "%s%s: %d\n", prefix,
3506 binder_command_strings[i], stats->bc[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003507 }
3508
3509 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003510 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003511 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3512 if (stats->br[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003513 seq_printf(m, "%s%s: %d\n", prefix,
3514 binder_return_strings[i], stats->br[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003515 }
3516
3517 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003518 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003519 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003520 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003521 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3522 if (stats->obj_created[i] || stats->obj_deleted[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003523 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3524 binder_objstat_strings[i],
3525 stats->obj_created[i] - stats->obj_deleted[i],
3526 stats->obj_created[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003527 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003528}
3529
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003530static void print_binder_proc_stats(struct seq_file *m,
3531 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003532{
3533 struct binder_work *w;
3534 struct rb_node *n;
3535 int count, strong, weak;
3536
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003537 seq_printf(m, "proc %d\n", proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003538 count = 0;
3539 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3540 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003541 seq_printf(m, " threads: %d\n", count);
3542 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003543 " ready threads %d\n"
3544 " free async space %zd\n", proc->requested_threads,
3545 proc->requested_threads_started, proc->max_threads,
3546 proc->ready_threads, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003547 count = 0;
3548 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3549 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003550 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003551 count = 0;
3552 strong = 0;
3553 weak = 0;
3554 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3555 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3556 rb_node_desc);
3557 count++;
3558 strong += ref->strong;
3559 weak += ref->weak;
3560 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003561 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003562
3563 count = 0;
3564 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3565 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003566 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003567
3568 count = 0;
3569 list_for_each_entry(w, &proc->todo, entry) {
3570 switch (w->type) {
3571 case BINDER_WORK_TRANSACTION:
3572 count++;
3573 break;
3574 default:
3575 break;
3576 }
3577 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003578 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003579
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003580 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003581}
3582
3583
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003584static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003585{
3586 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003587 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003588 int do_lock = !binder_debug_no_lock;
3589
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003590 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003591 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003592
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003593 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003594
3595 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003596 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003597 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003598 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003599
Sasha Levinb67bfe02013-02-27 17:06:00 -08003600 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003601 print_binder_proc(m, proc, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003602 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003603 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003604 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003605}
3606
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003607static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003608{
3609 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003610 int do_lock = !binder_debug_no_lock;
3611
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003612 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003613 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003614
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003615 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003616
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003617 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003618
Sasha Levinb67bfe02013-02-27 17:06:00 -08003619 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003620 print_binder_proc_stats(m, proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003621 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003622 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003623 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003624}
3625
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003626static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003627{
3628 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003629 int do_lock = !binder_debug_no_lock;
3630
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003631 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003632 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003633
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003634 seq_puts(m, "binder transactions:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003635 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003636 print_binder_proc(m, proc, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003637 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003638 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003639 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003640}
3641
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003642static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003643{
Riley Andrews83050a42016-02-09 21:05:33 -08003644 struct binder_proc *itr;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003645 struct binder_proc *proc = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003646 int do_lock = !binder_debug_no_lock;
Riley Andrews83050a42016-02-09 21:05:33 -08003647 bool valid_proc = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003648
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003649 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003650 binder_lock(__func__);
Riley Andrews83050a42016-02-09 21:05:33 -08003651
3652 hlist_for_each_entry(itr, &binder_procs, proc_node) {
3653 if (itr == proc) {
3654 valid_proc = true;
3655 break;
3656 }
3657 }
3658 if (valid_proc) {
3659 seq_puts(m, "binder proc state:\n");
3660 print_binder_proc(m, proc, 1);
3661 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003662 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003663 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003664 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003665}
3666
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003667static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003668 struct binder_transaction_log_entry *e)
3669{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003670 seq_printf(m,
3671 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3672 e->debug_id, (e->call_type == 2) ? "reply" :
3673 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3674 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3675 e->target_handle, e->data_size, e->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003676}
3677
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003678static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003679{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003680 struct binder_transaction_log *log = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003681 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003682
3683 if (log->full) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003684 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3685 print_binder_transaction_log_entry(m, &log->entry[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003686 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003687 for (i = 0; i < log->next; i++)
3688 print_binder_transaction_log_entry(m, &log->entry[i]);
3689 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003690}
3691
3692static const struct file_operations binder_fops = {
3693 .owner = THIS_MODULE,
3694 .poll = binder_poll,
3695 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003696 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003697 .mmap = binder_mmap,
3698 .open = binder_open,
3699 .flush = binder_flush,
3700 .release = binder_release,
3701};
3702
3703static struct miscdevice binder_miscdev = {
3704 .minor = MISC_DYNAMIC_MINOR,
3705 .name = "binder",
3706 .fops = &binder_fops
3707};
3708
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003709BINDER_DEBUG_ENTRY(state);
3710BINDER_DEBUG_ENTRY(stats);
3711BINDER_DEBUG_ENTRY(transactions);
3712BINDER_DEBUG_ENTRY(transaction_log);
3713
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003714static int __init binder_init(void)
3715{
3716 int ret;
3717
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003718 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3719 if (binder_debugfs_dir_entry_root)
3720 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3721 binder_debugfs_dir_entry_root);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003722 ret = misc_register(&binder_miscdev);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003723 if (binder_debugfs_dir_entry_root) {
3724 debugfs_create_file("state",
3725 S_IRUGO,
3726 binder_debugfs_dir_entry_root,
3727 NULL,
3728 &binder_state_fops);
3729 debugfs_create_file("stats",
3730 S_IRUGO,
3731 binder_debugfs_dir_entry_root,
3732 NULL,
3733 &binder_stats_fops);
3734 debugfs_create_file("transactions",
3735 S_IRUGO,
3736 binder_debugfs_dir_entry_root,
3737 NULL,
3738 &binder_transactions_fops);
3739 debugfs_create_file("transaction_log",
3740 S_IRUGO,
3741 binder_debugfs_dir_entry_root,
3742 &binder_transaction_log,
3743 &binder_transaction_log_fops);
3744 debugfs_create_file("failed_transaction_log",
3745 S_IRUGO,
3746 binder_debugfs_dir_entry_root,
3747 &binder_transaction_log_failed,
3748 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003749 }
3750 return ret;
3751}
3752
3753device_initcall(binder_init);
3754
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003755#define CREATE_TRACE_POINTS
3756#include "binder_trace.h"
3757
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003758MODULE_LICENSE("GPL v2");