blob: e12288c245b5424c0c2a7137775f85b0949d1ec0 [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,
Martijn Coenena494a712018-01-05 11:27:07 +0100337 BINDER_LOOPER_STATE_NEED_RETURN = 0x20,
338 BINDER_LOOPER_STATE_POLL = 0x40,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900339};
340
341struct binder_thread {
342 struct binder_proc *proc;
343 struct rb_node rb_node;
344 int pid;
345 int looper;
346 struct binder_transaction *transaction_stack;
347 struct list_head todo;
348 uint32_t return_error; /* Write failed, return error code in read buf */
349 uint32_t return_error2; /* Write failed, return error code in read */
350 /* buffer. Used when sending a reply to a dead process that */
351 /* we are also waiting on */
352 wait_queue_head_t wait;
353 struct binder_stats stats;
354};
355
356struct binder_transaction {
357 int debug_id;
358 struct binder_work work;
359 struct binder_thread *from;
360 struct binder_transaction *from_parent;
361 struct binder_proc *to_proc;
362 struct binder_thread *to_thread;
363 struct binder_transaction *to_parent;
364 unsigned need_reply:1;
365 /* unsigned is_dead:1; */ /* not used at the moment */
366
367 struct binder_buffer *buffer;
368 unsigned int code;
369 unsigned int flags;
370 long priority;
371 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600372 kuid_t sender_euid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900373};
374
375static void
376binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
377
Sachin Kamatefde99c2012-08-17 16:39:36 +0530378static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900379{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900380 unsigned long rlim_cur;
381 unsigned long irqs;
Todd Kjosc0d75da2017-11-27 09:32:33 -0800382 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900383
Todd Kjosc0d75da2017-11-27 09:32:33 -0800384 mutex_lock(&proc->files_lock);
385 if (proc->files == NULL) {
386 ret = -ESRCH;
387 goto err;
388 }
389 if (!lock_task_sighand(proc->tsk, &irqs)) {
390 ret = -EMFILE;
391 goto err;
392 }
Al Virodcfadfa2012-08-12 17:27:30 -0400393 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
394 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900395
Todd Kjosc0d75da2017-11-27 09:32:33 -0800396 ret = __alloc_fd(proc->files, 0, rlim_cur, flags);
397err:
398 mutex_unlock(&proc->files_lock);
399 return ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900400}
401
402/*
403 * copied from fd_install
404 */
405static void task_fd_install(
406 struct binder_proc *proc, unsigned int fd, struct file *file)
407{
Todd Kjosc0d75da2017-11-27 09:32:33 -0800408 mutex_lock(&proc->files_lock);
Al Virof869e8a2012-08-15 21:06:33 -0400409 if (proc->files)
410 __fd_install(proc->files, fd, file);
Todd Kjosc0d75da2017-11-27 09:32:33 -0800411 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900412}
413
414/*
415 * copied from sys_close
416 */
417static long task_close_fd(struct binder_proc *proc, unsigned int fd)
418{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900419 int retval;
420
Todd Kjosc0d75da2017-11-27 09:32:33 -0800421 mutex_lock(&proc->files_lock);
422 if (proc->files == NULL) {
423 retval = -ESRCH;
424 goto err;
425 }
Al Viro483ce1d2012-08-19 12:04:24 -0400426 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900427 /* can't restart close syscall because file table entry was cleared */
428 if (unlikely(retval == -ERESTARTSYS ||
429 retval == -ERESTARTNOINTR ||
430 retval == -ERESTARTNOHAND ||
431 retval == -ERESTART_RESTARTBLOCK))
432 retval = -EINTR;
Todd Kjosc0d75da2017-11-27 09:32:33 -0800433err:
434 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900435 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900436}
437
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700438static inline void binder_lock(const char *tag)
439{
440 trace_binder_lock(tag);
441 mutex_lock(&binder_main_lock);
442 trace_binder_locked(tag);
443}
444
445static inline void binder_unlock(const char *tag)
446{
447 trace_binder_unlock(tag);
448 mutex_unlock(&binder_main_lock);
449}
450
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900451static void binder_set_nice(long nice)
452{
453 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900454
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900455 if (can_nice(current, nice)) {
456 set_user_nice(current, nice);
457 return;
458 }
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900459 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900460 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530461 "%d: nice value %ld not allowed use %ld instead\n",
462 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900463 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +0800464 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900465 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530466 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900467}
468
469static size_t binder_buffer_size(struct binder_proc *proc,
470 struct binder_buffer *buffer)
471{
472 if (list_is_last(&buffer->entry, &proc->buffers))
473 return proc->buffer + proc->buffer_size - (void *)buffer->data;
Karthik Nayak78733112014-06-21 20:23:16 +0530474 return (size_t)list_entry(buffer->entry.next,
475 struct binder_buffer, entry) - (size_t)buffer->data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900476}
477
478static void binder_insert_free_buffer(struct binder_proc *proc,
479 struct binder_buffer *new_buffer)
480{
481 struct rb_node **p = &proc->free_buffers.rb_node;
482 struct rb_node *parent = NULL;
483 struct binder_buffer *buffer;
484 size_t buffer_size;
485 size_t new_buffer_size;
486
487 BUG_ON(!new_buffer->free);
488
489 new_buffer_size = binder_buffer_size(proc, new_buffer);
490
491 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Ben Hutchings9cd14472019-05-29 18:02:44 +0100492 "%d: add free buffer, size %zd, at %pK\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +0530493 proc->pid, new_buffer_size, new_buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900494
495 while (*p) {
496 parent = *p;
497 buffer = rb_entry(parent, struct binder_buffer, rb_node);
498 BUG_ON(!buffer->free);
499
500 buffer_size = binder_buffer_size(proc, buffer);
501
502 if (new_buffer_size < buffer_size)
503 p = &parent->rb_left;
504 else
505 p = &parent->rb_right;
506 }
507 rb_link_node(&new_buffer->rb_node, parent, p);
508 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
509}
510
511static void binder_insert_allocated_buffer(struct binder_proc *proc,
512 struct binder_buffer *new_buffer)
513{
514 struct rb_node **p = &proc->allocated_buffers.rb_node;
515 struct rb_node *parent = NULL;
516 struct binder_buffer *buffer;
517
518 BUG_ON(new_buffer->free);
519
520 while (*p) {
521 parent = *p;
522 buffer = rb_entry(parent, struct binder_buffer, rb_node);
523 BUG_ON(buffer->free);
524
525 if (new_buffer < buffer)
526 p = &parent->rb_left;
527 else if (new_buffer > buffer)
528 p = &parent->rb_right;
529 else
530 BUG();
531 }
532 rb_link_node(&new_buffer->rb_node, parent, p);
533 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
534}
535
536static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800537 uintptr_t user_ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900538{
539 struct rb_node *n = proc->allocated_buffers.rb_node;
540 struct binder_buffer *buffer;
541 struct binder_buffer *kern_ptr;
542
Arve Hjønnevågda498892014-02-21 14:40:26 -0800543 kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
544 - offsetof(struct binder_buffer, data));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900545
546 while (n) {
547 buffer = rb_entry(n, struct binder_buffer, rb_node);
548 BUG_ON(buffer->free);
549
550 if (kern_ptr < buffer)
551 n = n->rb_left;
552 else if (kern_ptr > buffer)
553 n = n->rb_right;
554 else
555 return buffer;
556 }
557 return NULL;
558}
559
560static int binder_update_page_range(struct binder_proc *proc, int allocate,
561 void *start, void *end,
562 struct vm_area_struct *vma)
563{
564 void *page_addr;
565 unsigned long user_page_addr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900566 struct page **page;
567 struct mm_struct *mm;
568
569 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Ben Hutchings9cd14472019-05-29 18:02:44 +0100570 "%d: %s pages %pK-%pK\n", proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900571 allocate ? "allocate" : "free", start, end);
572
573 if (end <= start)
574 return 0;
575
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700576 trace_binder_update_page_range(proc, allocate, start, end);
577
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900578 if (vma)
579 mm = NULL;
580 else
581 mm = get_task_mm(proc->tsk);
582
583 if (mm) {
584 down_write(&mm->mmap_sem);
Andrea Arcangeli16903f12019-04-18 17:50:52 -0700585 if (!mmget_still_valid(mm)) {
586 if (allocate == 0)
587 goto free_range;
588 goto err_no_vma;
589 }
590
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900591 vma = proc->vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800592 if (vma && mm != proc->vma_vm_mm) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530593 pr_err("%d: vma mm and task mm mismatch\n",
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -0800594 proc->pid);
595 vma = NULL;
596 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900597 }
598
599 if (allocate == 0)
600 goto free_range;
601
602 if (vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530603 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
604 proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900605 goto err_no_vma;
606 }
607
608 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
609 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900610
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900611 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
612
613 BUG_ON(*page);
Arve Hjønnevåg585650d2012-10-16 15:29:55 -0700614 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900615 if (*page == NULL) {
Ben Hutchings9cd14472019-05-29 18:02:44 +0100616 pr_err("%d: binder_alloc_buf failed for page at %pK\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +0530617 proc->pid, page_addr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900618 goto err_alloc_page_failed;
619 }
Andrey Ryabininf4c72c72015-02-27 20:44:21 +0300620 ret = map_kernel_range_noflush((unsigned long)page_addr,
621 PAGE_SIZE, PAGE_KERNEL, page);
622 flush_cache_vmap((unsigned long)page_addr,
623 (unsigned long)page_addr + PAGE_SIZE);
624 if (ret != 1) {
Ben Hutchings9cd14472019-05-29 18:02:44 +0100625 pr_err("%d: binder_alloc_buf failed to map page at %pK in kernel\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900626 proc->pid, page_addr);
627 goto err_map_kernel_failed;
628 }
629 user_page_addr =
630 (uintptr_t)page_addr + proc->user_buffer_offset;
631 ret = vm_insert_page(vma, user_page_addr, page[0]);
632 if (ret) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530633 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900634 proc->pid, user_page_addr);
635 goto err_vm_insert_page_failed;
636 }
637 /* vm_insert_page does not seem to increment the refcount */
638 }
639 if (mm) {
640 up_write(&mm->mmap_sem);
641 mmput(mm);
642 }
643 return 0;
644
645free_range:
646 for (page_addr = end - PAGE_SIZE; page_addr >= start;
647 page_addr -= PAGE_SIZE) {
648 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
649 if (vma)
650 zap_page_range(vma, (uintptr_t)page_addr +
651 proc->user_buffer_offset, PAGE_SIZE, NULL);
652err_vm_insert_page_failed:
653 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
654err_map_kernel_failed:
655 __free_page(*page);
656 *page = NULL;
657err_alloc_page_failed:
658 ;
659 }
660err_no_vma:
661 if (mm) {
662 up_write(&mm->mmap_sem);
663 mmput(mm);
664 }
665 return -ENOMEM;
666}
667
668static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
669 size_t data_size,
670 size_t offsets_size, int is_async)
671{
672 struct rb_node *n = proc->free_buffers.rb_node;
673 struct binder_buffer *buffer;
674 size_t buffer_size;
675 struct rb_node *best_fit = NULL;
676 void *has_page_addr;
677 void *end_page_addr;
678 size_t size;
679
680 if (proc->vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530681 pr_err("%d: binder_alloc_buf, no vma\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900682 proc->pid);
683 return NULL;
684 }
685
686 size = ALIGN(data_size, sizeof(void *)) +
687 ALIGN(offsets_size, sizeof(void *));
688
689 if (size < data_size || size < offsets_size) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530690 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
691 proc->pid, data_size, offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900692 return NULL;
693 }
694
695 if (is_async &&
696 proc->free_async_space < size + sizeof(struct binder_buffer)) {
697 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530698 "%d: binder_alloc_buf size %zd failed, no async space left\n",
699 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900700 return NULL;
701 }
702
703 while (n) {
704 buffer = rb_entry(n, struct binder_buffer, rb_node);
705 BUG_ON(!buffer->free);
706 buffer_size = binder_buffer_size(proc, buffer);
707
708 if (size < buffer_size) {
709 best_fit = n;
710 n = n->rb_left;
711 } else if (size > buffer_size)
712 n = n->rb_right;
713 else {
714 best_fit = n;
715 break;
716 }
717 }
718 if (best_fit == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530719 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
720 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900721 return NULL;
722 }
723 if (n == NULL) {
724 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
725 buffer_size = binder_buffer_size(proc, buffer);
726 }
727
728 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Ben Hutchings9cd14472019-05-29 18:02:44 +0100729 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +0530730 proc->pid, size, buffer, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900731
732 has_page_addr =
733 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
734 if (n == NULL) {
735 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
736 buffer_size = size; /* no room for other buffers */
737 else
738 buffer_size = size + sizeof(struct binder_buffer);
739 }
740 end_page_addr =
741 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
742 if (end_page_addr > has_page_addr)
743 end_page_addr = has_page_addr;
744 if (binder_update_page_range(proc, 1,
745 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
746 return NULL;
747
748 rb_erase(best_fit, &proc->free_buffers);
749 buffer->free = 0;
750 binder_insert_allocated_buffer(proc, buffer);
751 if (buffer_size != size) {
752 struct binder_buffer *new_buffer = (void *)buffer->data + size;
Seunghun Lee10f62862014-05-01 01:30:23 +0900753
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900754 list_add(&new_buffer->entry, &buffer->entry);
755 new_buffer->free = 1;
756 binder_insert_free_buffer(proc, new_buffer);
757 }
758 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Ben Hutchings9cd14472019-05-29 18:02:44 +0100759 "%d: binder_alloc_buf size %zd got %pK\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +0530760 proc->pid, size, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900761 buffer->data_size = data_size;
762 buffer->offsets_size = offsets_size;
763 buffer->async_transaction = is_async;
764 if (is_async) {
765 proc->free_async_space -= size + sizeof(struct binder_buffer);
766 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530767 "%d: binder_alloc_buf size %zd async free %zd\n",
768 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900769 }
770
771 return buffer;
772}
773
774static void *buffer_start_page(struct binder_buffer *buffer)
775{
776 return (void *)((uintptr_t)buffer & PAGE_MASK);
777}
778
779static void *buffer_end_page(struct binder_buffer *buffer)
780{
781 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
782}
783
784static void binder_delete_free_buffer(struct binder_proc *proc,
785 struct binder_buffer *buffer)
786{
787 struct binder_buffer *prev, *next = NULL;
788 int free_page_end = 1;
789 int free_page_start = 1;
790
791 BUG_ON(proc->buffers.next == &buffer->entry);
792 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
793 BUG_ON(!prev->free);
794 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
795 free_page_start = 0;
796 if (buffer_end_page(prev) == buffer_end_page(buffer))
797 free_page_end = 0;
798 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Ben Hutchings9cd14472019-05-29 18:02:44 +0100799 "%d: merge free, buffer %pK share page with %pK\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +0530800 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900801 }
802
803 if (!list_is_last(&buffer->entry, &proc->buffers)) {
804 next = list_entry(buffer->entry.next,
805 struct binder_buffer, entry);
806 if (buffer_start_page(next) == buffer_end_page(buffer)) {
807 free_page_end = 0;
808 if (buffer_start_page(next) ==
809 buffer_start_page(buffer))
810 free_page_start = 0;
811 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Ben Hutchings9cd14472019-05-29 18:02:44 +0100812 "%d: merge free, buffer %pK share page with %pK\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +0530813 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900814 }
815 }
816 list_del(&buffer->entry);
817 if (free_page_start || free_page_end) {
818 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Ben Hutchings9cd14472019-05-29 18:02:44 +0100819 "%d: merge free, buffer %pK do not share page%s%s with %pK or %pK\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900820 proc->pid, buffer, free_page_start ? "" : " end",
821 free_page_end ? "" : " start", prev, next);
822 binder_update_page_range(proc, 0, free_page_start ?
823 buffer_start_page(buffer) : buffer_end_page(buffer),
824 (free_page_end ? buffer_end_page(buffer) :
825 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
826 }
827}
828
829static void binder_free_buf(struct binder_proc *proc,
830 struct binder_buffer *buffer)
831{
832 size_t size, buffer_size;
833
834 buffer_size = binder_buffer_size(proc, buffer);
835
836 size = ALIGN(buffer->data_size, sizeof(void *)) +
837 ALIGN(buffer->offsets_size, sizeof(void *));
838
839 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Ben Hutchings9cd14472019-05-29 18:02:44 +0100840 "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +0530841 proc->pid, buffer, size, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900842
843 BUG_ON(buffer->free);
844 BUG_ON(size > buffer_size);
845 BUG_ON(buffer->transaction != NULL);
846 BUG_ON((void *)buffer < proc->buffer);
847 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
848
849 if (buffer->async_transaction) {
850 proc->free_async_space += size + sizeof(struct binder_buffer);
851
852 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530853 "%d: binder_free_buf size %zd async free %zd\n",
854 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900855 }
856
857 binder_update_page_range(proc, 0,
858 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
859 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
860 NULL);
861 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
862 buffer->free = 1;
863 if (!list_is_last(&buffer->entry, &proc->buffers)) {
864 struct binder_buffer *next = list_entry(buffer->entry.next,
865 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900866
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900867 if (next->free) {
868 rb_erase(&next->rb_node, &proc->free_buffers);
869 binder_delete_free_buffer(proc, next);
870 }
871 }
872 if (proc->buffers.next != &buffer->entry) {
873 struct binder_buffer *prev = list_entry(buffer->entry.prev,
874 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900875
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900876 if (prev->free) {
877 binder_delete_free_buffer(proc, buffer);
878 rb_erase(&prev->rb_node, &proc->free_buffers);
879 buffer = prev;
880 }
881 }
882 binder_insert_free_buffer(proc, buffer);
883}
884
885static struct binder_node *binder_get_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800886 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900887{
888 struct rb_node *n = proc->nodes.rb_node;
889 struct binder_node *node;
890
891 while (n) {
892 node = rb_entry(n, struct binder_node, rb_node);
893
894 if (ptr < node->ptr)
895 n = n->rb_left;
896 else if (ptr > node->ptr)
897 n = n->rb_right;
898 else
899 return node;
900 }
901 return NULL;
902}
903
904static struct binder_node *binder_new_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800905 binder_uintptr_t ptr,
906 binder_uintptr_t cookie)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900907{
908 struct rb_node **p = &proc->nodes.rb_node;
909 struct rb_node *parent = NULL;
910 struct binder_node *node;
911
912 while (*p) {
913 parent = *p;
914 node = rb_entry(parent, struct binder_node, rb_node);
915
916 if (ptr < node->ptr)
917 p = &(*p)->rb_left;
918 else if (ptr > node->ptr)
919 p = &(*p)->rb_right;
920 else
921 return NULL;
922 }
923
924 node = kzalloc(sizeof(*node), GFP_KERNEL);
925 if (node == NULL)
926 return NULL;
927 binder_stats_created(BINDER_STAT_NODE);
928 rb_link_node(&node->rb_node, parent, p);
929 rb_insert_color(&node->rb_node, &proc->nodes);
930 node->debug_id = ++binder_last_id;
931 node->proc = proc;
932 node->ptr = ptr;
933 node->cookie = cookie;
934 node->work.type = BINDER_WORK_NODE;
935 INIT_LIST_HEAD(&node->work.entry);
936 INIT_LIST_HEAD(&node->async_todo);
937 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800938 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900939 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800940 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900941 return node;
942}
943
944static int binder_inc_node(struct binder_node *node, int strong, int internal,
945 struct list_head *target_list)
946{
947 if (strong) {
948 if (internal) {
949 if (target_list == NULL &&
950 node->internal_strong_refs == 0 &&
951 !(node == binder_context_mgr_node &&
952 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530953 pr_err("invalid inc strong node for %d\n",
954 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900955 return -EINVAL;
956 }
957 node->internal_strong_refs++;
958 } else
959 node->local_strong_refs++;
960 if (!node->has_strong_ref && target_list) {
961 list_del_init(&node->work.entry);
962 list_add_tail(&node->work.entry, target_list);
963 }
964 } else {
965 if (!internal)
966 node->local_weak_refs++;
967 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
968 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530969 pr_err("invalid inc weak node for %d\n",
970 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900971 return -EINVAL;
972 }
973 list_add_tail(&node->work.entry, target_list);
974 }
975 }
976 return 0;
977}
978
979static int binder_dec_node(struct binder_node *node, int strong, int internal)
980{
981 if (strong) {
982 if (internal)
983 node->internal_strong_refs--;
984 else
985 node->local_strong_refs--;
986 if (node->local_strong_refs || node->internal_strong_refs)
987 return 0;
988 } else {
989 if (!internal)
990 node->local_weak_refs--;
991 if (node->local_weak_refs || !hlist_empty(&node->refs))
992 return 0;
993 }
994 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
995 if (list_empty(&node->work.entry)) {
996 list_add_tail(&node->work.entry, &node->proc->todo);
997 wake_up_interruptible(&node->proc->wait);
998 }
999 } else {
1000 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1001 !node->local_weak_refs) {
1002 list_del_init(&node->work.entry);
1003 if (node->proc) {
1004 rb_erase(&node->rb_node, &node->proc->nodes);
1005 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301006 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001007 node->debug_id);
1008 } else {
1009 hlist_del(&node->dead_node);
1010 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301011 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001012 node->debug_id);
1013 }
1014 kfree(node);
1015 binder_stats_deleted(BINDER_STAT_NODE);
1016 }
1017 }
1018
1019 return 0;
1020}
1021
1022
1023static struct binder_ref *binder_get_ref(struct binder_proc *proc,
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001024 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001025{
1026 struct rb_node *n = proc->refs_by_desc.rb_node;
1027 struct binder_ref *ref;
1028
1029 while (n) {
1030 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1031
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001032 if (desc < ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001033 n = n->rb_left;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001034 } else if (desc > ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001035 n = n->rb_right;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001036 } else if (need_strong_ref && !ref->strong) {
1037 binder_user_error("tried to use weak ref as strong ref\n");
1038 return NULL;
1039 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001040 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001041 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001042 }
1043 return NULL;
1044}
1045
1046static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1047 struct binder_node *node)
1048{
1049 struct rb_node *n;
1050 struct rb_node **p = &proc->refs_by_node.rb_node;
1051 struct rb_node *parent = NULL;
1052 struct binder_ref *ref, *new_ref;
1053
1054 while (*p) {
1055 parent = *p;
1056 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1057
1058 if (node < ref->node)
1059 p = &(*p)->rb_left;
1060 else if (node > ref->node)
1061 p = &(*p)->rb_right;
1062 else
1063 return ref;
1064 }
1065 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1066 if (new_ref == NULL)
1067 return NULL;
1068 binder_stats_created(BINDER_STAT_REF);
1069 new_ref->debug_id = ++binder_last_id;
1070 new_ref->proc = proc;
1071 new_ref->node = node;
1072 rb_link_node(&new_ref->rb_node_node, parent, p);
1073 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1074
1075 new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1;
1076 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1077 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1078 if (ref->desc > new_ref->desc)
1079 break;
1080 new_ref->desc = ref->desc + 1;
1081 }
1082
1083 p = &proc->refs_by_desc.rb_node;
1084 while (*p) {
1085 parent = *p;
1086 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1087
1088 if (new_ref->desc < ref->desc)
1089 p = &(*p)->rb_left;
1090 else if (new_ref->desc > ref->desc)
1091 p = &(*p)->rb_right;
1092 else
1093 BUG();
1094 }
1095 rb_link_node(&new_ref->rb_node_desc, parent, p);
1096 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1097 if (node) {
1098 hlist_add_head(&new_ref->node_entry, &node->refs);
1099
1100 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301101 "%d new ref %d desc %d for node %d\n",
1102 proc->pid, new_ref->debug_id, new_ref->desc,
1103 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001104 } else {
1105 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301106 "%d new ref %d desc %d for dead node\n",
1107 proc->pid, new_ref->debug_id, new_ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001108 }
1109 return new_ref;
1110}
1111
1112static void binder_delete_ref(struct binder_ref *ref)
1113{
1114 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301115 "%d delete ref %d desc %d for node %d\n",
1116 ref->proc->pid, ref->debug_id, ref->desc,
1117 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001118
1119 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1120 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1121 if (ref->strong)
1122 binder_dec_node(ref->node, 1, 1);
1123 hlist_del(&ref->node_entry);
1124 binder_dec_node(ref->node, 0, 1);
1125 if (ref->death) {
1126 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301127 "%d delete ref %d desc %d has death notification\n",
1128 ref->proc->pid, ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001129 list_del(&ref->death->work.entry);
1130 kfree(ref->death);
1131 binder_stats_deleted(BINDER_STAT_DEATH);
1132 }
1133 kfree(ref);
1134 binder_stats_deleted(BINDER_STAT_REF);
1135}
1136
1137static int binder_inc_ref(struct binder_ref *ref, int strong,
1138 struct list_head *target_list)
1139{
1140 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001141
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001142 if (strong) {
1143 if (ref->strong == 0) {
1144 ret = binder_inc_node(ref->node, 1, 1, target_list);
1145 if (ret)
1146 return ret;
1147 }
1148 ref->strong++;
1149 } else {
1150 if (ref->weak == 0) {
1151 ret = binder_inc_node(ref->node, 0, 1, target_list);
1152 if (ret)
1153 return ret;
1154 }
1155 ref->weak++;
1156 }
1157 return 0;
1158}
1159
1160
1161static int binder_dec_ref(struct binder_ref *ref, int strong)
1162{
1163 if (strong) {
1164 if (ref->strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301165 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001166 ref->proc->pid, ref->debug_id,
1167 ref->desc, ref->strong, ref->weak);
1168 return -EINVAL;
1169 }
1170 ref->strong--;
1171 if (ref->strong == 0) {
1172 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001173
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001174 ret = binder_dec_node(ref->node, strong, 1);
1175 if (ret)
1176 return ret;
1177 }
1178 } else {
1179 if (ref->weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301180 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001181 ref->proc->pid, ref->debug_id,
1182 ref->desc, ref->strong, ref->weak);
1183 return -EINVAL;
1184 }
1185 ref->weak--;
1186 }
1187 if (ref->strong == 0 && ref->weak == 0)
1188 binder_delete_ref(ref);
1189 return 0;
1190}
1191
1192static void binder_pop_transaction(struct binder_thread *target_thread,
1193 struct binder_transaction *t)
1194{
1195 if (target_thread) {
1196 BUG_ON(target_thread->transaction_stack != t);
1197 BUG_ON(target_thread->transaction_stack->from != target_thread);
1198 target_thread->transaction_stack =
1199 target_thread->transaction_stack->from_parent;
1200 t->from = NULL;
1201 }
1202 t->need_reply = 0;
1203 if (t->buffer)
1204 t->buffer->transaction = NULL;
1205 kfree(t);
1206 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1207}
1208
1209static void binder_send_failed_reply(struct binder_transaction *t,
1210 uint32_t error_code)
1211{
1212 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001213 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001214
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001215 BUG_ON(t->flags & TF_ONE_WAY);
1216 while (1) {
1217 target_thread = t->from;
1218 if (target_thread) {
1219 if (target_thread->return_error != BR_OK &&
1220 target_thread->return_error2 == BR_OK) {
1221 target_thread->return_error2 =
1222 target_thread->return_error;
1223 target_thread->return_error = BR_OK;
1224 }
1225 if (target_thread->return_error == BR_OK) {
1226 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301227 "send failed reply for transaction %d to %d:%d\n",
William Panlener0232a422014-09-03 22:44:03 -05001228 t->debug_id,
1229 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001230 target_thread->pid);
1231
1232 binder_pop_transaction(target_thread, t);
1233 target_thread->return_error = error_code;
1234 wake_up_interruptible(&target_thread->wait);
1235 } else {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301236 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1237 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001238 target_thread->pid,
1239 target_thread->return_error);
1240 }
1241 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001242 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001243 next = t->from_parent;
1244
1245 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1246 "send failed reply for transaction %d, target dead\n",
1247 t->debug_id);
1248
1249 binder_pop_transaction(target_thread, t);
1250 if (next == NULL) {
1251 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1252 "reply failed, no target thread at root\n");
1253 return;
1254 }
1255 t = next;
1256 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1257 "reply failed, no target thread -- retry %d\n",
1258 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001259 }
1260}
1261
1262static void binder_transaction_buffer_release(struct binder_proc *proc,
1263 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001264 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001265{
Arve Hjønnevågda498892014-02-21 14:40:26 -08001266 binder_size_t *offp, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001267 int debug_id = buffer->debug_id;
1268
1269 binder_debug(BINDER_DEBUG_TRANSACTION,
Todd Kjos6f3433c2018-02-07 13:57:37 -08001270 "%d buffer release %d, size %zd-%zd, failed at %pK\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001271 proc->pid, buffer->debug_id,
1272 buffer->data_size, buffer->offsets_size, failed_at);
1273
1274 if (buffer->target_node)
1275 binder_dec_node(buffer->target_node, 1, 0);
1276
Arve Hjønnevågda498892014-02-21 14:40:26 -08001277 offp = (binder_size_t *)(buffer->data +
1278 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001279 if (failed_at)
1280 off_end = failed_at;
1281 else
1282 off_end = (void *)offp + buffer->offsets_size;
1283 for (; offp < off_end; offp++) {
1284 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001285
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001286 if (*offp > buffer->data_size - sizeof(*fp) ||
1287 buffer->data_size < sizeof(*fp) ||
Serban Constantinescuec35e852013-07-04 10:54:46 +01001288 !IS_ALIGNED(*offp, sizeof(u32))) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001289 pr_err("transaction release %d bad offset %lld, size %zd\n",
1290 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001291 continue;
1292 }
1293 fp = (struct flat_binder_object *)(buffer->data + *offp);
1294 switch (fp->type) {
1295 case BINDER_TYPE_BINDER:
1296 case BINDER_TYPE_WEAK_BINDER: {
1297 struct binder_node *node = binder_get_node(proc, fp->binder);
Seunghun Lee10f62862014-05-01 01:30:23 +09001298
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001299 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001300 pr_err("transaction release %d bad node %016llx\n",
1301 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001302 break;
1303 }
1304 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001305 " node %d u%016llx\n",
1306 node->debug_id, (u64)node->ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001307 binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0);
1308 } break;
1309 case BINDER_TYPE_HANDLE:
1310 case BINDER_TYPE_WEAK_HANDLE: {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001311 struct binder_ref *ref;
1312
1313 ref = binder_get_ref(proc, fp->handle,
1314 fp->type == BINDER_TYPE_HANDLE);
Seunghun Lee10f62862014-05-01 01:30:23 +09001315
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001316 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001317 pr_err("transaction release %d bad handle %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301318 debug_id, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001319 break;
1320 }
1321 binder_debug(BINDER_DEBUG_TRANSACTION,
1322 " ref %d desc %d (node %d)\n",
1323 ref->debug_id, ref->desc, ref->node->debug_id);
1324 binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE);
1325 } break;
1326
1327 case BINDER_TYPE_FD:
1328 binder_debug(BINDER_DEBUG_TRANSACTION,
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001329 " fd %d\n", fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001330 if (failed_at)
1331 task_close_fd(proc, fp->handle);
1332 break;
1333
1334 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001335 pr_err("transaction release %d bad object type %x\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301336 debug_id, fp->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001337 break;
1338 }
1339 }
1340}
1341
1342static void binder_transaction(struct binder_proc *proc,
1343 struct binder_thread *thread,
1344 struct binder_transaction_data *tr, int reply)
1345{
1346 struct binder_transaction *t;
1347 struct binder_work *tcomplete;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001348 binder_size_t *offp, *off_end;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001349 binder_size_t off_min;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001350 struct binder_proc *target_proc;
1351 struct binder_thread *target_thread = NULL;
1352 struct binder_node *target_node = NULL;
1353 struct list_head *target_list;
1354 wait_queue_head_t *target_wait;
1355 struct binder_transaction *in_reply_to = NULL;
1356 struct binder_transaction_log_entry *e;
1357 uint32_t return_error;
1358
1359 e = binder_transaction_log_add(&binder_transaction_log);
1360 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1361 e->from_proc = proc->pid;
1362 e->from_thread = thread->pid;
1363 e->target_handle = tr->target.handle;
1364 e->data_size = tr->data_size;
1365 e->offsets_size = tr->offsets_size;
1366
1367 if (reply) {
1368 in_reply_to = thread->transaction_stack;
1369 if (in_reply_to == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301370 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001371 proc->pid, thread->pid);
1372 return_error = BR_FAILED_REPLY;
1373 goto err_empty_call_stack;
1374 }
1375 binder_set_nice(in_reply_to->saved_priority);
1376 if (in_reply_to->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301377 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 +09001378 proc->pid, thread->pid, in_reply_to->debug_id,
1379 in_reply_to->to_proc ?
1380 in_reply_to->to_proc->pid : 0,
1381 in_reply_to->to_thread ?
1382 in_reply_to->to_thread->pid : 0);
1383 return_error = BR_FAILED_REPLY;
1384 in_reply_to = NULL;
1385 goto err_bad_call_stack;
1386 }
1387 thread->transaction_stack = in_reply_to->to_parent;
1388 target_thread = in_reply_to->from;
1389 if (target_thread == NULL) {
1390 return_error = BR_DEAD_REPLY;
1391 goto err_dead_binder;
1392 }
1393 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301394 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 +09001395 proc->pid, thread->pid,
1396 target_thread->transaction_stack ?
1397 target_thread->transaction_stack->debug_id : 0,
1398 in_reply_to->debug_id);
1399 return_error = BR_FAILED_REPLY;
1400 in_reply_to = NULL;
1401 target_thread = NULL;
1402 goto err_dead_binder;
1403 }
1404 target_proc = target_thread->proc;
1405 } else {
1406 if (tr->target.handle) {
1407 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001408
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001409 ref = binder_get_ref(proc, tr->target.handle, true);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001410 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301411 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001412 proc->pid, thread->pid);
1413 return_error = BR_FAILED_REPLY;
1414 goto err_invalid_target_handle;
1415 }
1416 target_node = ref->node;
1417 } else {
1418 target_node = binder_context_mgr_node;
1419 if (target_node == NULL) {
1420 return_error = BR_DEAD_REPLY;
1421 goto err_no_context_mgr_node;
1422 }
1423 }
1424 e->to_node = target_node->debug_id;
1425 target_proc = target_node->proc;
1426 if (target_proc == NULL) {
1427 return_error = BR_DEAD_REPLY;
1428 goto err_dead_binder;
1429 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001430 if (security_binder_transaction(proc->tsk,
1431 target_proc->tsk) < 0) {
1432 return_error = BR_FAILED_REPLY;
1433 goto err_invalid_target_handle;
1434 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001435 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1436 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001437
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001438 tmp = thread->transaction_stack;
1439 if (tmp->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301440 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 +09001441 proc->pid, thread->pid, tmp->debug_id,
1442 tmp->to_proc ? tmp->to_proc->pid : 0,
1443 tmp->to_thread ?
1444 tmp->to_thread->pid : 0);
1445 return_error = BR_FAILED_REPLY;
1446 goto err_bad_call_stack;
1447 }
1448 while (tmp) {
1449 if (tmp->from && tmp->from->proc == target_proc)
1450 target_thread = tmp->from;
1451 tmp = tmp->from_parent;
1452 }
1453 }
1454 }
1455 if (target_thread) {
1456 e->to_thread = target_thread->pid;
1457 target_list = &target_thread->todo;
1458 target_wait = &target_thread->wait;
1459 } else {
1460 target_list = &target_proc->todo;
1461 target_wait = &target_proc->wait;
1462 }
1463 e->to_proc = target_proc->pid;
1464
1465 /* TODO: reuse incoming transaction for reply */
1466 t = kzalloc(sizeof(*t), GFP_KERNEL);
1467 if (t == NULL) {
1468 return_error = BR_FAILED_REPLY;
1469 goto err_alloc_t_failed;
1470 }
1471 binder_stats_created(BINDER_STAT_TRANSACTION);
1472
1473 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1474 if (tcomplete == NULL) {
1475 return_error = BR_FAILED_REPLY;
1476 goto err_alloc_tcomplete_failed;
1477 }
1478 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1479
1480 t->debug_id = ++binder_last_id;
1481 e->debug_id = t->debug_id;
1482
1483 if (reply)
1484 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001485 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001486 proc->pid, thread->pid, t->debug_id,
1487 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001488 (u64)tr->data.ptr.buffer,
1489 (u64)tr->data.ptr.offsets,
1490 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001491 else
1492 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001493 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001494 proc->pid, thread->pid, t->debug_id,
1495 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001496 (u64)tr->data.ptr.buffer,
1497 (u64)tr->data.ptr.offsets,
1498 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001499
1500 if (!reply && !(tr->flags & TF_ONE_WAY))
1501 t->from = thread;
1502 else
1503 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03001504 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001505 t->to_proc = target_proc;
1506 t->to_thread = target_thread;
1507 t->code = tr->code;
1508 t->flags = tr->flags;
1509 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001510
1511 trace_binder_transaction(reply, t, target_node);
1512
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001513 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
1514 tr->offsets_size, !reply && (t->flags & TF_ONE_WAY));
1515 if (t->buffer == NULL) {
1516 return_error = BR_FAILED_REPLY;
1517 goto err_binder_alloc_buf_failed;
1518 }
1519 t->buffer->allow_user_free = 0;
1520 t->buffer->debug_id = t->debug_id;
1521 t->buffer->transaction = t;
1522 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001523 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001524 if (target_node)
1525 binder_inc_node(target_node, 1, 0, NULL);
1526
Arve Hjønnevågda498892014-02-21 14:40:26 -08001527 offp = (binder_size_t *)(t->buffer->data +
1528 ALIGN(tr->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001529
Arve Hjønnevågda498892014-02-21 14:40:26 -08001530 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1531 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301532 binder_user_error("%d:%d got transaction with invalid data 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 (copy_from_user(offp, (const void __user *)(uintptr_t)
1538 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301539 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1540 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001541 return_error = BR_FAILED_REPLY;
1542 goto err_copy_data_failed;
1543 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001544 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1545 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1546 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001547 return_error = BR_FAILED_REPLY;
1548 goto err_bad_offset;
1549 }
1550 off_end = (void *)offp + tr->offsets_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001551 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001552 for (; offp < off_end; offp++) {
1553 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001554
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001555 if (*offp > t->buffer->data_size - sizeof(*fp) ||
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001556 *offp < off_min ||
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001557 t->buffer->data_size < sizeof(*fp) ||
Serban Constantinescuec35e852013-07-04 10:54:46 +01001558 !IS_ALIGNED(*offp, sizeof(u32))) {
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001559 binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n",
1560 proc->pid, thread->pid, (u64)*offp,
1561 (u64)off_min,
1562 (u64)(t->buffer->data_size -
1563 sizeof(*fp)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001564 return_error = BR_FAILED_REPLY;
1565 goto err_bad_offset;
1566 }
1567 fp = (struct flat_binder_object *)(t->buffer->data + *offp);
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001568 off_min = *offp + sizeof(struct flat_binder_object);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001569 switch (fp->type) {
1570 case BINDER_TYPE_BINDER:
1571 case BINDER_TYPE_WEAK_BINDER: {
1572 struct binder_ref *ref;
1573 struct binder_node *node = binder_get_node(proc, fp->binder);
Seunghun Lee10f62862014-05-01 01:30:23 +09001574
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001575 if (node == NULL) {
1576 node = binder_new_node(proc, fp->binder, fp->cookie);
1577 if (node == NULL) {
1578 return_error = BR_FAILED_REPLY;
1579 goto err_binder_new_node_failed;
1580 }
1581 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1582 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1583 }
1584 if (fp->cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001585 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001586 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001587 (u64)fp->binder, node->debug_id,
1588 (u64)fp->cookie, (u64)node->cookie);
Christian Engelmayer7d420432014-05-07 21:44:53 +02001589 return_error = BR_FAILED_REPLY;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001590 goto err_binder_get_ref_for_node_failed;
1591 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001592 if (security_binder_transfer_binder(proc->tsk,
1593 target_proc->tsk)) {
1594 return_error = BR_FAILED_REPLY;
1595 goto err_binder_get_ref_for_node_failed;
1596 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001597 ref = binder_get_ref_for_node(target_proc, node);
1598 if (ref == NULL) {
1599 return_error = BR_FAILED_REPLY;
1600 goto err_binder_get_ref_for_node_failed;
1601 }
1602 if (fp->type == BINDER_TYPE_BINDER)
1603 fp->type = BINDER_TYPE_HANDLE;
1604 else
1605 fp->type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001606 fp->binder = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001607 fp->handle = ref->desc;
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001608 fp->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001609 binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE,
1610 &thread->todo);
1611
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001612 trace_binder_transaction_node_to_ref(t, node, ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001613 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001614 " node %d u%016llx -> ref %d desc %d\n",
1615 node->debug_id, (u64)node->ptr,
1616 ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001617 } break;
1618 case BINDER_TYPE_HANDLE:
1619 case BINDER_TYPE_WEAK_HANDLE: {
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001620 struct binder_ref *ref;
1621
1622 ref = binder_get_ref(proc, fp->handle,
1623 fp->type == BINDER_TYPE_HANDLE);
Seunghun Lee10f62862014-05-01 01:30:23 +09001624
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001625 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001626 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301627 proc->pid,
1628 thread->pid, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001629 return_error = BR_FAILED_REPLY;
1630 goto err_binder_get_ref_failed;
1631 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001632 if (security_binder_transfer_binder(proc->tsk,
1633 target_proc->tsk)) {
1634 return_error = BR_FAILED_REPLY;
1635 goto err_binder_get_ref_failed;
1636 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001637 if (ref->node->proc == target_proc) {
1638 if (fp->type == BINDER_TYPE_HANDLE)
1639 fp->type = BINDER_TYPE_BINDER;
1640 else
1641 fp->type = BINDER_TYPE_WEAK_BINDER;
1642 fp->binder = ref->node->ptr;
1643 fp->cookie = ref->node->cookie;
1644 binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001645 trace_binder_transaction_ref_to_node(t, ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001646 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001647 " ref %d desc %d -> node %d u%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001648 ref->debug_id, ref->desc, ref->node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001649 (u64)ref->node->ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001650 } else {
1651 struct binder_ref *new_ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001652
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001653 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1654 if (new_ref == NULL) {
1655 return_error = BR_FAILED_REPLY;
1656 goto err_binder_get_ref_for_node_failed;
1657 }
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001658 fp->binder = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001659 fp->handle = new_ref->desc;
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001660 fp->cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001661 binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001662 trace_binder_transaction_ref_to_ref(t, ref,
1663 new_ref);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001664 binder_debug(BINDER_DEBUG_TRANSACTION,
1665 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1666 ref->debug_id, ref->desc, new_ref->debug_id,
1667 new_ref->desc, ref->node->debug_id);
1668 }
1669 } break;
1670
1671 case BINDER_TYPE_FD: {
1672 int target_fd;
1673 struct file *file;
1674
1675 if (reply) {
1676 if (!(in_reply_to->flags & TF_ACCEPT_FDS)) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001677 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 +09001678 proc->pid, thread->pid, fp->handle);
1679 return_error = BR_FAILED_REPLY;
1680 goto err_fd_not_allowed;
1681 }
1682 } else if (!target_node->accept_fds) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001683 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 +09001684 proc->pid, thread->pid, fp->handle);
1685 return_error = BR_FAILED_REPLY;
1686 goto err_fd_not_allowed;
1687 }
1688
1689 file = fget(fp->handle);
1690 if (file == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001691 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001692 proc->pid, thread->pid, fp->handle);
1693 return_error = BR_FAILED_REPLY;
1694 goto err_fget_failed;
1695 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001696 if (security_binder_transfer_file(proc->tsk,
1697 target_proc->tsk,
1698 file) < 0) {
1699 fput(file);
1700 return_error = BR_FAILED_REPLY;
1701 goto err_get_unused_fd_failed;
1702 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001703 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1704 if (target_fd < 0) {
1705 fput(file);
1706 return_error = BR_FAILED_REPLY;
1707 goto err_get_unused_fd_failed;
1708 }
1709 task_fd_install(target_proc, target_fd, file);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001710 trace_binder_transaction_fd(t, fp->handle, target_fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001711 binder_debug(BINDER_DEBUG_TRANSACTION,
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001712 " fd %d -> %d\n", fp->handle, target_fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001713 /* TODO: fput? */
Arve Hjønnevåg4afb6042016-10-24 15:20:30 +02001714 fp->binder = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001715 fp->handle = target_fd;
1716 } break;
1717
1718 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001719 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001720 proc->pid, thread->pid, fp->type);
1721 return_error = BR_FAILED_REPLY;
1722 goto err_bad_object_type;
1723 }
1724 }
1725 if (reply) {
1726 BUG_ON(t->buffer->async_transaction != 0);
1727 binder_pop_transaction(target_thread, in_reply_to);
1728 } else if (!(t->flags & TF_ONE_WAY)) {
1729 BUG_ON(t->buffer->async_transaction != 0);
1730 t->need_reply = 1;
1731 t->from_parent = thread->transaction_stack;
1732 thread->transaction_stack = t;
1733 } else {
1734 BUG_ON(target_node == NULL);
1735 BUG_ON(t->buffer->async_transaction != 1);
1736 if (target_node->has_async_transaction) {
1737 target_list = &target_node->async_todo;
1738 target_wait = NULL;
1739 } else
1740 target_node->has_async_transaction = 1;
1741 }
1742 t->work.type = BINDER_WORK_TRANSACTION;
1743 list_add_tail(&t->work.entry, target_list);
1744 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
1745 list_add_tail(&tcomplete->entry, &thread->todo);
Riley Andrews8fb0b0c2017-06-29 12:01:37 -07001746 if (target_wait) {
1747 if (reply || !(t->flags & TF_ONE_WAY))
1748 wake_up_interruptible_sync(target_wait);
1749 else
1750 wake_up_interruptible(target_wait);
1751 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001752 return;
1753
1754err_get_unused_fd_failed:
1755err_fget_failed:
1756err_fd_not_allowed:
1757err_binder_get_ref_for_node_failed:
1758err_binder_get_ref_failed:
1759err_binder_new_node_failed:
1760err_bad_object_type:
1761err_bad_offset:
1762err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001763 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001764 binder_transaction_buffer_release(target_proc, t->buffer, offp);
1765 t->buffer->transaction = NULL;
1766 binder_free_buf(target_proc, t->buffer);
1767err_binder_alloc_buf_failed:
1768 kfree(tcomplete);
1769 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
1770err_alloc_tcomplete_failed:
1771 kfree(t);
1772 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1773err_alloc_t_failed:
1774err_bad_call_stack:
1775err_empty_call_stack:
1776err_dead_binder:
1777err_invalid_target_handle:
1778err_no_context_mgr_node:
1779 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001780 "%d:%d transaction failed %d, size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001781 proc->pid, thread->pid, return_error,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001782 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001783
1784 {
1785 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09001786
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001787 fe = binder_transaction_log_add(&binder_transaction_log_failed);
1788 *fe = *e;
1789 }
1790
1791 BUG_ON(thread->return_error != BR_OK);
1792 if (in_reply_to) {
1793 thread->return_error = BR_TRANSACTION_COMPLETE;
1794 binder_send_failed_reply(in_reply_to, return_error);
1795 } else
1796 thread->return_error = return_error;
1797}
1798
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02001799static int binder_thread_write(struct binder_proc *proc,
1800 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001801 binder_uintptr_t binder_buffer, size_t size,
1802 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001803{
1804 uint32_t cmd;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001805 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001806 void __user *ptr = buffer + *consumed;
1807 void __user *end = buffer + size;
1808
1809 while (ptr < end && thread->return_error == BR_OK) {
1810 if (get_user(cmd, (uint32_t __user *)ptr))
1811 return -EFAULT;
1812 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001813 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001814 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
1815 binder_stats.bc[_IOC_NR(cmd)]++;
1816 proc->stats.bc[_IOC_NR(cmd)]++;
1817 thread->stats.bc[_IOC_NR(cmd)]++;
1818 }
1819 switch (cmd) {
1820 case BC_INCREFS:
1821 case BC_ACQUIRE:
1822 case BC_RELEASE:
1823 case BC_DECREFS: {
1824 uint32_t target;
1825 struct binder_ref *ref;
1826 const char *debug_string;
1827
1828 if (get_user(target, (uint32_t __user *)ptr))
1829 return -EFAULT;
1830 ptr += sizeof(uint32_t);
1831 if (target == 0 && binder_context_mgr_node &&
1832 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
1833 ref = binder_get_ref_for_node(proc,
1834 binder_context_mgr_node);
1835 if (ref->desc != target) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301836 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001837 proc->pid, thread->pid,
1838 ref->desc);
1839 }
1840 } else
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001841 ref = binder_get_ref(proc, target,
1842 cmd == BC_ACQUIRE ||
1843 cmd == BC_RELEASE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001844 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301845 binder_user_error("%d:%d refcount change on invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001846 proc->pid, thread->pid, target);
1847 break;
1848 }
1849 switch (cmd) {
1850 case BC_INCREFS:
1851 debug_string = "IncRefs";
1852 binder_inc_ref(ref, 0, NULL);
1853 break;
1854 case BC_ACQUIRE:
1855 debug_string = "Acquire";
1856 binder_inc_ref(ref, 1, NULL);
1857 break;
1858 case BC_RELEASE:
1859 debug_string = "Release";
1860 binder_dec_ref(ref, 1);
1861 break;
1862 case BC_DECREFS:
1863 default:
1864 debug_string = "DecRefs";
1865 binder_dec_ref(ref, 0);
1866 break;
1867 }
1868 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301869 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001870 proc->pid, thread->pid, debug_string, ref->debug_id,
1871 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
1872 break;
1873 }
1874 case BC_INCREFS_DONE:
1875 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001876 binder_uintptr_t node_ptr;
1877 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001878 struct binder_node *node;
1879
Arve Hjønnevågda498892014-02-21 14:40:26 -08001880 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001881 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001882 ptr += sizeof(binder_uintptr_t);
1883 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001884 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001885 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001886 node = binder_get_node(proc, node_ptr);
1887 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001888 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001889 proc->pid, thread->pid,
1890 cmd == BC_INCREFS_DONE ?
1891 "BC_INCREFS_DONE" :
1892 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001893 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001894 break;
1895 }
1896 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001897 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001898 proc->pid, thread->pid,
1899 cmd == BC_INCREFS_DONE ?
1900 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001901 (u64)node_ptr, node->debug_id,
1902 (u64)cookie, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001903 break;
1904 }
1905 if (cmd == BC_ACQUIRE_DONE) {
1906 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301907 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001908 proc->pid, thread->pid,
1909 node->debug_id);
1910 break;
1911 }
1912 node->pending_strong_ref = 0;
1913 } else {
1914 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301915 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001916 proc->pid, thread->pid,
1917 node->debug_id);
1918 break;
1919 }
1920 node->pending_weak_ref = 0;
1921 }
1922 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
1923 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301924 "%d:%d %s node %d ls %d lw %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001925 proc->pid, thread->pid,
1926 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
1927 node->debug_id, node->local_strong_refs, node->local_weak_refs);
1928 break;
1929 }
1930 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05301931 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001932 return -EINVAL;
1933 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05301934 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001935 return -EINVAL;
1936
1937 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001938 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001939 struct binder_buffer *buffer;
1940
Arve Hjønnevågda498892014-02-21 14:40:26 -08001941 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001942 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08001943 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001944
1945 buffer = binder_buffer_lookup(proc, data_ptr);
1946 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001947 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
1948 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001949 break;
1950 }
1951 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001952 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
1953 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001954 break;
1955 }
1956 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001957 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
1958 proc->pid, thread->pid, (u64)data_ptr,
1959 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001960 buffer->transaction ? "active" : "finished");
1961
1962 if (buffer->transaction) {
1963 buffer->transaction->buffer = NULL;
1964 buffer->transaction = NULL;
1965 }
1966 if (buffer->async_transaction && buffer->target_node) {
1967 BUG_ON(!buffer->target_node->has_async_transaction);
1968 if (list_empty(&buffer->target_node->async_todo))
1969 buffer->target_node->has_async_transaction = 0;
1970 else
1971 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
1972 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001973 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001974 binder_transaction_buffer_release(proc, buffer, NULL);
1975 binder_free_buf(proc, buffer);
1976 break;
1977 }
1978
1979 case BC_TRANSACTION:
1980 case BC_REPLY: {
1981 struct binder_transaction_data tr;
1982
1983 if (copy_from_user(&tr, ptr, sizeof(tr)))
1984 return -EFAULT;
1985 ptr += sizeof(tr);
1986 binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
1987 break;
1988 }
1989
1990 case BC_REGISTER_LOOPER:
1991 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301992 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001993 proc->pid, thread->pid);
1994 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
1995 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05301996 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001997 proc->pid, thread->pid);
1998 } else if (proc->requested_threads == 0) {
1999 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302000 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002001 proc->pid, thread->pid);
2002 } else {
2003 proc->requested_threads--;
2004 proc->requested_threads_started++;
2005 }
2006 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
2007 break;
2008 case BC_ENTER_LOOPER:
2009 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302010 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002011 proc->pid, thread->pid);
2012 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
2013 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302014 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002015 proc->pid, thread->pid);
2016 }
2017 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
2018 break;
2019 case BC_EXIT_LOOPER:
2020 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302021 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002022 proc->pid, thread->pid);
2023 thread->looper |= BINDER_LOOPER_STATE_EXITED;
2024 break;
2025
2026 case BC_REQUEST_DEATH_NOTIFICATION:
2027 case BC_CLEAR_DEATH_NOTIFICATION: {
2028 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002029 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002030 struct binder_ref *ref;
2031 struct binder_ref_death *death;
2032
2033 if (get_user(target, (uint32_t __user *)ptr))
2034 return -EFAULT;
2035 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002036 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002037 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002038 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002039 ref = binder_get_ref(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002040 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302041 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002042 proc->pid, thread->pid,
2043 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2044 "BC_REQUEST_DEATH_NOTIFICATION" :
2045 "BC_CLEAR_DEATH_NOTIFICATION",
2046 target);
2047 break;
2048 }
2049
2050 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002051 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002052 proc->pid, thread->pid,
2053 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2054 "BC_REQUEST_DEATH_NOTIFICATION" :
2055 "BC_CLEAR_DEATH_NOTIFICATION",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002056 (u64)cookie, ref->debug_id, ref->desc,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002057 ref->strong, ref->weak, ref->node->debug_id);
2058
2059 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2060 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302061 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002062 proc->pid, thread->pid);
2063 break;
2064 }
2065 death = kzalloc(sizeof(*death), GFP_KERNEL);
2066 if (death == NULL) {
2067 thread->return_error = BR_ERROR;
2068 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302069 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002070 proc->pid, thread->pid);
2071 break;
2072 }
2073 binder_stats_created(BINDER_STAT_DEATH);
2074 INIT_LIST_HEAD(&death->work.entry);
2075 death->cookie = cookie;
2076 ref->death = death;
2077 if (ref->node->proc == NULL) {
2078 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2079 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2080 list_add_tail(&ref->death->work.entry, &thread->todo);
2081 } else {
2082 list_add_tail(&ref->death->work.entry, &proc->todo);
2083 wake_up_interruptible(&proc->wait);
2084 }
2085 }
2086 } else {
2087 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302088 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002089 proc->pid, thread->pid);
2090 break;
2091 }
2092 death = ref->death;
2093 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002094 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002095 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002096 (u64)death->cookie,
2097 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002098 break;
2099 }
2100 ref->death = NULL;
2101 if (list_empty(&death->work.entry)) {
2102 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2103 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2104 list_add_tail(&death->work.entry, &thread->todo);
2105 } else {
2106 list_add_tail(&death->work.entry, &proc->todo);
2107 wake_up_interruptible(&proc->wait);
2108 }
2109 } else {
2110 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2111 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2112 }
2113 }
2114 } break;
2115 case BC_DEAD_BINDER_DONE: {
2116 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002117 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002118 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09002119
Arve Hjønnevågda498892014-02-21 14:40:26 -08002120 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002121 return -EFAULT;
2122
Lisa Du7a64cd82016-02-17 09:32:52 +08002123 ptr += sizeof(cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002124 list_for_each_entry(w, &proc->delivered_death, entry) {
2125 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
Seunghun Lee10f62862014-05-01 01:30:23 +09002126
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002127 if (tmp_death->cookie == cookie) {
2128 death = tmp_death;
2129 break;
2130 }
2131 }
2132 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Todd Kjos6f3433c2018-02-07 13:57:37 -08002133 "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002134 proc->pid, thread->pid, (u64)cookie,
2135 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002136 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002137 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2138 proc->pid, thread->pid, (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002139 break;
2140 }
2141
2142 list_del_init(&death->work.entry);
2143 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2144 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2145 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2146 list_add_tail(&death->work.entry, &thread->todo);
2147 } else {
2148 list_add_tail(&death->work.entry, &proc->todo);
2149 wake_up_interruptible(&proc->wait);
2150 }
2151 }
2152 } break;
2153
2154 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302155 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002156 proc->pid, thread->pid, cmd);
2157 return -EINVAL;
2158 }
2159 *consumed = ptr - buffer;
2160 }
2161 return 0;
2162}
2163
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002164static void binder_stat_br(struct binder_proc *proc,
2165 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002166{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002167 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002168 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2169 binder_stats.br[_IOC_NR(cmd)]++;
2170 proc->stats.br[_IOC_NR(cmd)]++;
2171 thread->stats.br[_IOC_NR(cmd)]++;
2172 }
2173}
2174
2175static int binder_has_proc_work(struct binder_proc *proc,
2176 struct binder_thread *thread)
2177{
2178 return !list_empty(&proc->todo) ||
2179 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2180}
2181
2182static int binder_has_thread_work(struct binder_thread *thread)
2183{
2184 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2185 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2186}
2187
2188static int binder_thread_read(struct binder_proc *proc,
2189 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002190 binder_uintptr_t binder_buffer, size_t size,
2191 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002192{
Arve Hjønnevågda498892014-02-21 14:40:26 -08002193 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002194 void __user *ptr = buffer + *consumed;
2195 void __user *end = buffer + size;
2196
2197 int ret = 0;
2198 int wait_for_proc_work;
2199
2200 if (*consumed == 0) {
2201 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2202 return -EFAULT;
2203 ptr += sizeof(uint32_t);
2204 }
2205
2206retry:
2207 wait_for_proc_work = thread->transaction_stack == NULL &&
2208 list_empty(&thread->todo);
2209
2210 if (thread->return_error != BR_OK && ptr < end) {
2211 if (thread->return_error2 != BR_OK) {
2212 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2213 return -EFAULT;
2214 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002215 binder_stat_br(proc, thread, thread->return_error2);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002216 if (ptr == end)
2217 goto done;
2218 thread->return_error2 = BR_OK;
2219 }
2220 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2221 return -EFAULT;
2222 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002223 binder_stat_br(proc, thread, thread->return_error);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002224 thread->return_error = BR_OK;
2225 goto done;
2226 }
2227
2228
2229 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2230 if (wait_for_proc_work)
2231 proc->ready_threads++;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002232
2233 binder_unlock(__func__);
2234
2235 trace_binder_wait_for_work(wait_for_proc_work,
2236 !!thread->transaction_stack,
2237 !list_empty(&thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002238 if (wait_for_proc_work) {
2239 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2240 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302241 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 +09002242 proc->pid, thread->pid, thread->looper);
2243 wait_event_interruptible(binder_user_error_wait,
2244 binder_stop_on_user_error < 2);
2245 }
2246 binder_set_nice(proc->default_priority);
2247 if (non_block) {
2248 if (!binder_has_proc_work(proc, thread))
2249 ret = -EAGAIN;
2250 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002251 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002252 } else {
2253 if (non_block) {
2254 if (!binder_has_thread_work(thread))
2255 ret = -EAGAIN;
2256 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002257 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002258 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002259
2260 binder_lock(__func__);
2261
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002262 if (wait_for_proc_work)
2263 proc->ready_threads--;
2264 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2265
2266 if (ret)
2267 return ret;
2268
2269 while (1) {
2270 uint32_t cmd;
2271 struct binder_transaction_data tr;
2272 struct binder_work *w;
2273 struct binder_transaction *t = NULL;
2274
Dmitry Voytik395262a2014-09-08 18:16:34 +04002275 if (!list_empty(&thread->todo)) {
2276 w = list_first_entry(&thread->todo, struct binder_work,
2277 entry);
2278 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2279 w = list_first_entry(&proc->todo, struct binder_work,
2280 entry);
2281 } else {
2282 /* no data added */
2283 if (ptr - buffer == 4 &&
2284 !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002285 goto retry;
2286 break;
2287 }
2288
2289 if (end - ptr < sizeof(tr) + 4)
2290 break;
2291
2292 switch (w->type) {
2293 case BINDER_WORK_TRANSACTION: {
2294 t = container_of(w, struct binder_transaction, work);
2295 } break;
2296 case BINDER_WORK_TRANSACTION_COMPLETE: {
2297 cmd = BR_TRANSACTION_COMPLETE;
2298 if (put_user(cmd, (uint32_t __user *)ptr))
2299 return -EFAULT;
2300 ptr += sizeof(uint32_t);
2301
2302 binder_stat_br(proc, thread, cmd);
2303 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302304 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002305 proc->pid, thread->pid);
2306
2307 list_del(&w->entry);
2308 kfree(w);
2309 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2310 } break;
2311 case BINDER_WORK_NODE: {
2312 struct binder_node *node = container_of(w, struct binder_node, work);
2313 uint32_t cmd = BR_NOOP;
2314 const char *cmd_name;
2315 int strong = node->internal_strong_refs || node->local_strong_refs;
2316 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
Seunghun Lee10f62862014-05-01 01:30:23 +09002317
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002318 if (weak && !node->has_weak_ref) {
2319 cmd = BR_INCREFS;
2320 cmd_name = "BR_INCREFS";
2321 node->has_weak_ref = 1;
2322 node->pending_weak_ref = 1;
2323 node->local_weak_refs++;
2324 } else if (strong && !node->has_strong_ref) {
2325 cmd = BR_ACQUIRE;
2326 cmd_name = "BR_ACQUIRE";
2327 node->has_strong_ref = 1;
2328 node->pending_strong_ref = 1;
2329 node->local_strong_refs++;
2330 } else if (!strong && node->has_strong_ref) {
2331 cmd = BR_RELEASE;
2332 cmd_name = "BR_RELEASE";
2333 node->has_strong_ref = 0;
2334 } else if (!weak && node->has_weak_ref) {
2335 cmd = BR_DECREFS;
2336 cmd_name = "BR_DECREFS";
2337 node->has_weak_ref = 0;
2338 }
2339 if (cmd != BR_NOOP) {
2340 if (put_user(cmd, (uint32_t __user *)ptr))
2341 return -EFAULT;
2342 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002343 if (put_user(node->ptr,
2344 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002345 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002346 ptr += sizeof(binder_uintptr_t);
2347 if (put_user(node->cookie,
2348 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002349 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002350 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002351
2352 binder_stat_br(proc, thread, cmd);
2353 binder_debug(BINDER_DEBUG_USER_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002354 "%d:%d %s %d u%016llx c%016llx\n",
2355 proc->pid, thread->pid, cmd_name,
2356 node->debug_id,
2357 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002358 } else {
2359 list_del_init(&w->entry);
2360 if (!weak && !strong) {
2361 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002362 "%d:%d node %d u%016llx c%016llx deleted\n",
2363 proc->pid, thread->pid,
2364 node->debug_id,
2365 (u64)node->ptr,
2366 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002367 rb_erase(&node->rb_node, &proc->nodes);
2368 kfree(node);
2369 binder_stats_deleted(BINDER_STAT_NODE);
2370 } else {
2371 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002372 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2373 proc->pid, thread->pid,
2374 node->debug_id,
2375 (u64)node->ptr,
2376 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002377 }
2378 }
2379 } break;
2380 case BINDER_WORK_DEAD_BINDER:
2381 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2382 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2383 struct binder_ref_death *death;
2384 uint32_t cmd;
2385
2386 death = container_of(w, struct binder_ref_death, work);
2387 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2388 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2389 else
2390 cmd = BR_DEAD_BINDER;
2391 if (put_user(cmd, (uint32_t __user *)ptr))
2392 return -EFAULT;
2393 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002394 if (put_user(death->cookie,
2395 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002396 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002397 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002398 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002399 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002400 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002401 proc->pid, thread->pid,
2402 cmd == BR_DEAD_BINDER ?
2403 "BR_DEAD_BINDER" :
2404 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002405 (u64)death->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002406
2407 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2408 list_del(&w->entry);
2409 kfree(death);
2410 binder_stats_deleted(BINDER_STAT_DEATH);
2411 } else
2412 list_move(&w->entry, &proc->delivered_death);
2413 if (cmd == BR_DEAD_BINDER)
2414 goto done; /* DEAD_BINDER notifications can cause transactions */
2415 } break;
2416 }
2417
2418 if (!t)
2419 continue;
2420
2421 BUG_ON(t->buffer == NULL);
2422 if (t->buffer->target_node) {
2423 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002424
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002425 tr.target.ptr = target_node->ptr;
2426 tr.cookie = target_node->cookie;
2427 t->saved_priority = task_nice(current);
2428 if (t->priority < target_node->min_priority &&
2429 !(t->flags & TF_ONE_WAY))
2430 binder_set_nice(t->priority);
2431 else if (!(t->flags & TF_ONE_WAY) ||
2432 t->saved_priority > target_node->min_priority)
2433 binder_set_nice(target_node->min_priority);
2434 cmd = BR_TRANSACTION;
2435 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002436 tr.target.ptr = 0;
2437 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002438 cmd = BR_REPLY;
2439 }
2440 tr.code = t->code;
2441 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002442 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002443
2444 if (t->from) {
2445 struct task_struct *sender = t->from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09002446
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002447 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002448 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002449 } else {
2450 tr.sender_pid = 0;
2451 }
2452
2453 tr.data_size = t->buffer->data_size;
2454 tr.offsets_size = t->buffer->offsets_size;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002455 tr.data.ptr.buffer = (binder_uintptr_t)(
2456 (uintptr_t)t->buffer->data +
2457 proc->user_buffer_offset);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002458 tr.data.ptr.offsets = tr.data.ptr.buffer +
2459 ALIGN(t->buffer->data_size,
2460 sizeof(void *));
2461
2462 if (put_user(cmd, (uint32_t __user *)ptr))
2463 return -EFAULT;
2464 ptr += sizeof(uint32_t);
2465 if (copy_to_user(ptr, &tr, sizeof(tr)))
2466 return -EFAULT;
2467 ptr += sizeof(tr);
2468
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002469 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002470 binder_stat_br(proc, thread, cmd);
2471 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002472 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002473 proc->pid, thread->pid,
2474 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2475 "BR_REPLY",
2476 t->debug_id, t->from ? t->from->proc->pid : 0,
2477 t->from ? t->from->pid : 0, cmd,
2478 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002479 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002480
2481 list_del(&t->work.entry);
2482 t->buffer->allow_user_free = 1;
2483 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2484 t->to_parent = thread->transaction_stack;
2485 t->to_thread = thread;
2486 thread->transaction_stack = t;
2487 } else {
2488 t->buffer->transaction = NULL;
2489 kfree(t);
2490 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2491 }
2492 break;
2493 }
2494
2495done:
2496
2497 *consumed = ptr - buffer;
2498 if (proc->requested_threads + proc->ready_threads == 0 &&
2499 proc->requested_threads_started < proc->max_threads &&
2500 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2501 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2502 /*spawn a new thread if we leave this out */) {
2503 proc->requested_threads++;
2504 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302505 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002506 proc->pid, thread->pid);
2507 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2508 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002509 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002510 }
2511 return 0;
2512}
2513
2514static void binder_release_work(struct list_head *list)
2515{
2516 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09002517
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002518 while (!list_empty(list)) {
2519 w = list_first_entry(list, struct binder_work, entry);
2520 list_del_init(&w->entry);
2521 switch (w->type) {
2522 case BINDER_WORK_TRANSACTION: {
2523 struct binder_transaction *t;
2524
2525 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002526 if (t->buffer->target_node &&
2527 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002528 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002529 } else {
2530 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302531 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002532 t->debug_id);
2533 t->buffer->transaction = NULL;
2534 kfree(t);
2535 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2536 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002537 } break;
2538 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002539 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302540 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002541 kfree(w);
2542 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2543 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002544 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2545 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2546 struct binder_ref_death *death;
2547
2548 death = container_of(w, struct binder_ref_death, work);
2549 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002550 "undelivered death notification, %016llx\n",
2551 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002552 kfree(death);
2553 binder_stats_deleted(BINDER_STAT_DEATH);
2554 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002555 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302556 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002557 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002558 break;
2559 }
2560 }
2561
2562}
2563
2564static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2565{
2566 struct binder_thread *thread = NULL;
2567 struct rb_node *parent = NULL;
2568 struct rb_node **p = &proc->threads.rb_node;
2569
2570 while (*p) {
2571 parent = *p;
2572 thread = rb_entry(parent, struct binder_thread, rb_node);
2573
2574 if (current->pid < thread->pid)
2575 p = &(*p)->rb_left;
2576 else if (current->pid > thread->pid)
2577 p = &(*p)->rb_right;
2578 else
2579 break;
2580 }
2581 if (*p == NULL) {
2582 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2583 if (thread == NULL)
2584 return NULL;
2585 binder_stats_created(BINDER_STAT_THREAD);
2586 thread->proc = proc;
2587 thread->pid = current->pid;
2588 init_waitqueue_head(&thread->wait);
2589 INIT_LIST_HEAD(&thread->todo);
2590 rb_link_node(&thread->rb_node, parent, p);
2591 rb_insert_color(&thread->rb_node, &proc->threads);
2592 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2593 thread->return_error = BR_OK;
2594 thread->return_error2 = BR_OK;
2595 }
2596 return thread;
2597}
2598
2599static int binder_free_thread(struct binder_proc *proc,
2600 struct binder_thread *thread)
2601{
2602 struct binder_transaction *t;
2603 struct binder_transaction *send_reply = NULL;
2604 int active_transactions = 0;
2605
2606 rb_erase(&thread->rb_node, &proc->threads);
2607 t = thread->transaction_stack;
2608 if (t && t->to_thread == thread)
2609 send_reply = t;
2610 while (t) {
2611 active_transactions++;
2612 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302613 "release %d:%d transaction %d %s, still active\n",
2614 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002615 t->debug_id,
2616 (t->to_thread == thread) ? "in" : "out");
2617
2618 if (t->to_thread == thread) {
2619 t->to_proc = NULL;
2620 t->to_thread = NULL;
2621 if (t->buffer) {
2622 t->buffer->transaction = NULL;
2623 t->buffer = NULL;
2624 }
2625 t = t->to_parent;
2626 } else if (t->from == thread) {
2627 t->from = NULL;
2628 t = t->from_parent;
2629 } else
2630 BUG();
2631 }
Martijn Coenena494a712018-01-05 11:27:07 +01002632
2633 /*
2634 * If this thread used poll, make sure we remove the waitqueue
2635 * from any epoll data structures holding it with POLLFREE.
2636 * waitqueue_active() is safe to use here because we're holding
2637 * the global lock.
2638 */
2639 if ((thread->looper & BINDER_LOOPER_STATE_POLL) &&
2640 waitqueue_active(&thread->wait)) {
2641 wake_up_poll(&thread->wait, POLLHUP | POLLFREE);
2642 }
2643
Martijn Coenenb6c62122018-02-16 09:47:15 +01002644 /*
2645 * This is needed to avoid races between wake_up_poll() above and
2646 * and ep_remove_waitqueue() called for other reasons (eg the epoll file
2647 * descriptor being closed); ep_remove_waitqueue() holds an RCU read
2648 * lock, so we can be sure it's done after calling synchronize_rcu().
2649 */
2650 if (thread->looper & BINDER_LOOPER_STATE_POLL)
2651 synchronize_rcu();
2652
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002653 if (send_reply)
2654 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2655 binder_release_work(&thread->todo);
2656 kfree(thread);
2657 binder_stats_deleted(BINDER_STAT_THREAD);
2658 return active_transactions;
2659}
2660
2661static unsigned int binder_poll(struct file *filp,
2662 struct poll_table_struct *wait)
2663{
2664 struct binder_proc *proc = filp->private_data;
2665 struct binder_thread *thread = NULL;
2666 int wait_for_proc_work;
2667
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002668 binder_lock(__func__);
2669
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002670 thread = binder_get_thread(proc);
Eric Biggersfebf1082018-02-26 10:56:45 -08002671 if (!thread) {
2672 binder_unlock(__func__);
Eric Biggers4be5a282018-01-30 23:11:24 -08002673 return POLLERR;
Eric Biggersfebf1082018-02-26 10:56:45 -08002674 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002675
Martijn Coenena494a712018-01-05 11:27:07 +01002676 thread->looper |= BINDER_LOOPER_STATE_POLL;
2677
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002678 wait_for_proc_work = thread->transaction_stack == NULL &&
2679 list_empty(&thread->todo) && thread->return_error == BR_OK;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002680
2681 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002682
2683 if (wait_for_proc_work) {
2684 if (binder_has_proc_work(proc, thread))
2685 return POLLIN;
2686 poll_wait(filp, &proc->wait, wait);
2687 if (binder_has_proc_work(proc, thread))
2688 return POLLIN;
2689 } else {
2690 if (binder_has_thread_work(thread))
2691 return POLLIN;
2692 poll_wait(filp, &thread->wait, wait);
2693 if (binder_has_thread_work(thread))
2694 return POLLIN;
2695 }
2696 return 0;
2697}
2698
Tair Rzayev78260ac2014-06-03 22:27:21 +03002699static int binder_ioctl_write_read(struct file *filp,
2700 unsigned int cmd, unsigned long arg,
2701 struct binder_thread *thread)
2702{
2703 int ret = 0;
2704 struct binder_proc *proc = filp->private_data;
2705 unsigned int size = _IOC_SIZE(cmd);
2706 void __user *ubuf = (void __user *)arg;
2707 struct binder_write_read bwr;
2708
2709 if (size != sizeof(struct binder_write_read)) {
2710 ret = -EINVAL;
2711 goto out;
2712 }
2713 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2714 ret = -EFAULT;
2715 goto out;
2716 }
2717 binder_debug(BINDER_DEBUG_READ_WRITE,
2718 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2719 proc->pid, thread->pid,
2720 (u64)bwr.write_size, (u64)bwr.write_buffer,
2721 (u64)bwr.read_size, (u64)bwr.read_buffer);
2722
2723 if (bwr.write_size > 0) {
2724 ret = binder_thread_write(proc, thread,
2725 bwr.write_buffer,
2726 bwr.write_size,
2727 &bwr.write_consumed);
2728 trace_binder_write_done(ret);
2729 if (ret < 0) {
2730 bwr.read_consumed = 0;
2731 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2732 ret = -EFAULT;
2733 goto out;
2734 }
2735 }
2736 if (bwr.read_size > 0) {
2737 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2738 bwr.read_size,
2739 &bwr.read_consumed,
2740 filp->f_flags & O_NONBLOCK);
2741 trace_binder_read_done(ret);
2742 if (!list_empty(&proc->todo))
2743 wake_up_interruptible(&proc->wait);
2744 if (ret < 0) {
2745 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2746 ret = -EFAULT;
2747 goto out;
2748 }
2749 }
2750 binder_debug(BINDER_DEBUG_READ_WRITE,
2751 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2752 proc->pid, thread->pid,
2753 (u64)bwr.write_consumed, (u64)bwr.write_size,
2754 (u64)bwr.read_consumed, (u64)bwr.read_size);
2755 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2756 ret = -EFAULT;
2757 goto out;
2758 }
2759out:
2760 return ret;
2761}
2762
2763static int binder_ioctl_set_ctx_mgr(struct file *filp)
2764{
2765 int ret = 0;
2766 struct binder_proc *proc = filp->private_data;
2767 kuid_t curr_euid = current_euid();
2768
2769 if (binder_context_mgr_node != NULL) {
2770 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2771 ret = -EBUSY;
2772 goto out;
2773 }
Stephen Smalley79af7302015-01-21 10:54:10 -05002774 ret = security_binder_set_context_mgr(proc->tsk);
2775 if (ret < 0)
2776 goto out;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002777 if (uid_valid(binder_context_mgr_uid)) {
2778 if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
2779 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2780 from_kuid(&init_user_ns, curr_euid),
2781 from_kuid(&init_user_ns,
2782 binder_context_mgr_uid));
2783 ret = -EPERM;
2784 goto out;
2785 }
2786 } else {
2787 binder_context_mgr_uid = curr_euid;
2788 }
2789 binder_context_mgr_node = binder_new_node(proc, 0, 0);
2790 if (binder_context_mgr_node == NULL) {
2791 ret = -ENOMEM;
2792 goto out;
2793 }
2794 binder_context_mgr_node->local_weak_refs++;
2795 binder_context_mgr_node->local_strong_refs++;
2796 binder_context_mgr_node->has_strong_ref = 1;
2797 binder_context_mgr_node->has_weak_ref = 1;
2798out:
2799 return ret;
2800}
2801
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002802static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2803{
2804 int ret;
2805 struct binder_proc *proc = filp->private_data;
2806 struct binder_thread *thread;
2807 unsigned int size = _IOC_SIZE(cmd);
2808 void __user *ubuf = (void __user *)arg;
2809
Tair Rzayev78260ac2014-06-03 22:27:21 +03002810 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2811 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002812
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002813 trace_binder_ioctl(cmd, arg);
2814
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002815 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2816 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002817 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002818
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002819 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002820 thread = binder_get_thread(proc);
2821 if (thread == NULL) {
2822 ret = -ENOMEM;
2823 goto err;
2824 }
2825
2826 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002827 case BINDER_WRITE_READ:
2828 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
2829 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002830 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002831 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002832 case BINDER_SET_MAX_THREADS:
2833 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2834 ret = -EINVAL;
2835 goto err;
2836 }
2837 break;
2838 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03002839 ret = binder_ioctl_set_ctx_mgr(filp);
2840 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002841 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002842 break;
2843 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302844 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002845 proc->pid, thread->pid);
2846 binder_free_thread(proc, thread);
2847 thread = NULL;
2848 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002849 case BINDER_VERSION: {
2850 struct binder_version __user *ver = ubuf;
2851
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002852 if (size != sizeof(struct binder_version)) {
2853 ret = -EINVAL;
2854 goto err;
2855 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02002856 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2857 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002858 ret = -EINVAL;
2859 goto err;
2860 }
2861 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002862 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002863 default:
2864 ret = -EINVAL;
2865 goto err;
2866 }
2867 ret = 0;
2868err:
2869 if (thread)
2870 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002871 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002872 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2873 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05302874 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 -07002875err_unlocked:
2876 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002877 return ret;
2878}
2879
2880static void binder_vma_open(struct vm_area_struct *vma)
2881{
2882 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002883
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002884 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302885 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002886 proc->pid, vma->vm_start, vma->vm_end,
2887 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2888 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002889}
2890
2891static void binder_vma_close(struct vm_area_struct *vma)
2892{
2893 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002894
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002895 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302896 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002897 proc->pid, vma->vm_start, vma->vm_end,
2898 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2899 (unsigned long)pgprot_val(vma->vm_page_prot));
2900 proc->vma = NULL;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08002901 proc->vma_vm_mm = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002902 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2903}
2904
Vinayak Menonddac7d52014-06-02 18:17:59 +05302905static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2906{
2907 return VM_FAULT_SIGBUS;
2908}
2909
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07002910static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002911 .open = binder_vma_open,
2912 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05302913 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002914};
2915
2916static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2917{
2918 int ret;
2919 struct vm_struct *area;
2920 struct binder_proc *proc = filp->private_data;
2921 const char *failure_string;
2922 struct binder_buffer *buffer;
2923
Martijn Coenencbd854d2017-07-28 13:56:08 +02002924 if (proc->tsk != current->group_leader)
Al Viroa79f41e2012-08-15 18:23:36 -04002925 return -EINVAL;
2926
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002927 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2928 vma->vm_end = vma->vm_start + SZ_4M;
2929
2930 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2931 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2932 proc->pid, vma->vm_start, vma->vm_end,
2933 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2934 (unsigned long)pgprot_val(vma->vm_page_prot));
2935
2936 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2937 ret = -EPERM;
2938 failure_string = "bad vm_flags";
2939 goto err_bad_arg;
2940 }
2941 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2942
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002943 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002944 if (proc->buffer) {
2945 ret = -EBUSY;
2946 failure_string = "already mapped";
2947 goto err_already_mapped;
2948 }
2949
2950 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2951 if (area == NULL) {
2952 ret = -ENOMEM;
2953 failure_string = "get_vm_area";
2954 goto err_get_vm_area_failed;
2955 }
2956 proc->buffer = area->addr;
2957 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002958 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002959
2960#ifdef CONFIG_CPU_CACHE_VIPT
2961 if (cache_is_vipt_aliasing()) {
2962 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
Ben Hutchings9cd14472019-05-29 18:02:44 +01002963 pr_info("binder_mmap: %d %lx-%lx maps %pK bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002964 vma->vm_start += PAGE_SIZE;
2965 }
2966 }
2967#endif
2968 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2969 if (proc->pages == NULL) {
2970 ret = -ENOMEM;
2971 failure_string = "alloc page array";
2972 goto err_alloc_pages_failed;
2973 }
2974 proc->buffer_size = vma->vm_end - vma->vm_start;
2975
2976 vma->vm_ops = &binder_vm_ops;
2977 vma->vm_private_data = proc;
2978
2979 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2980 ret = -ENOMEM;
2981 failure_string = "alloc small buf";
2982 goto err_alloc_small_buf_failed;
2983 }
2984 buffer = proc->buffer;
2985 INIT_LIST_HEAD(&proc->buffers);
2986 list_add(&buffer->entry, &proc->buffers);
2987 buffer->free = 1;
2988 binder_insert_free_buffer(proc, buffer);
2989 proc->free_async_space = proc->buffer_size / 2;
2990 barrier();
Todd Kjosc0d75da2017-11-27 09:32:33 -08002991 mutex_lock(&proc->files_lock);
Al Viroa79f41e2012-08-15 18:23:36 -04002992 proc->files = get_files_struct(current);
Todd Kjosc0d75da2017-11-27 09:32:33 -08002993 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002994 proc->vma = vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08002995 proc->vma_vm_mm = vma->vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002996
Sherwin Soltani258767f2012-06-26 02:00:30 -04002997 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002998 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2999 return 0;
3000
3001err_alloc_small_buf_failed:
3002 kfree(proc->pages);
3003 proc->pages = NULL;
3004err_alloc_pages_failed:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08003005 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003006 vfree(proc->buffer);
3007 proc->buffer = NULL;
3008err_get_vm_area_failed:
3009err_already_mapped:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08003010 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003011err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04003012 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003013 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
3014 return ret;
3015}
3016
3017static int binder_open(struct inode *nodp, struct file *filp)
3018{
3019 struct binder_proc *proc;
3020
3021 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
3022 current->group_leader->pid, current->pid);
3023
3024 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
3025 if (proc == NULL)
3026 return -ENOMEM;
Todd Kjos51050752017-06-29 12:01:36 -07003027 get_task_struct(current->group_leader);
3028 proc->tsk = current->group_leader;
Todd Kjosc0d75da2017-11-27 09:32:33 -08003029 mutex_init(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003030 INIT_LIST_HEAD(&proc->todo);
3031 init_waitqueue_head(&proc->wait);
3032 proc->default_priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003033
3034 binder_lock(__func__);
3035
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003036 binder_stats_created(BINDER_STAT_PROC);
3037 hlist_add_head(&proc->proc_node, &binder_procs);
3038 proc->pid = current->group_leader->pid;
3039 INIT_LIST_HEAD(&proc->delivered_death);
3040 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003041
3042 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003043
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003044 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003045 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09003046
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003047 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003048 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
3049 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003050 }
3051
3052 return 0;
3053}
3054
3055static int binder_flush(struct file *filp, fl_owner_t id)
3056{
3057 struct binder_proc *proc = filp->private_data;
3058
3059 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3060
3061 return 0;
3062}
3063
3064static void binder_deferred_flush(struct binder_proc *proc)
3065{
3066 struct rb_node *n;
3067 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09003068
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003069 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3070 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09003071
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003072 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3073 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3074 wake_up_interruptible(&thread->wait);
3075 wake_count++;
3076 }
3077 }
3078 wake_up_interruptible_all(&proc->wait);
3079
3080 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3081 "binder_flush: %d woke %d threads\n", proc->pid,
3082 wake_count);
3083}
3084
3085static int binder_release(struct inode *nodp, struct file *filp)
3086{
3087 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003088
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003089 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003090 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3091
3092 return 0;
3093}
3094
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003095static int binder_node_release(struct binder_node *node, int refs)
3096{
3097 struct binder_ref *ref;
3098 int death = 0;
3099
3100 list_del_init(&node->work.entry);
3101 binder_release_work(&node->async_todo);
3102
3103 if (hlist_empty(&node->refs)) {
3104 kfree(node);
3105 binder_stats_deleted(BINDER_STAT_NODE);
3106
3107 return refs;
3108 }
3109
3110 node->proc = NULL;
3111 node->local_strong_refs = 0;
3112 node->local_weak_refs = 0;
3113 hlist_add_head(&node->dead_node, &binder_dead_nodes);
3114
3115 hlist_for_each_entry(ref, &node->refs, node_entry) {
3116 refs++;
3117
3118 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08003119 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003120
3121 death++;
3122
3123 if (list_empty(&ref->death->work.entry)) {
3124 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3125 list_add_tail(&ref->death->work.entry,
3126 &ref->proc->todo);
3127 wake_up_interruptible(&ref->proc->wait);
3128 } else
3129 BUG();
3130 }
3131
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003132 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3133 "node %d now dead, refs %d, death %d\n",
3134 node->debug_id, refs, death);
3135
3136 return refs;
3137}
3138
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003139static void binder_deferred_release(struct binder_proc *proc)
3140{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003141 struct binder_transaction *t;
3142 struct rb_node *n;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003143 int threads, nodes, incoming_refs, outgoing_refs, buffers,
3144 active_transactions, page_count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003145
3146 BUG_ON(proc->vma);
3147 BUG_ON(proc->files);
3148
3149 hlist_del(&proc->proc_node);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003150
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003151 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
3152 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003153 "%s: %d context_mgr_node gone\n",
3154 __func__, proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003155 binder_context_mgr_node = NULL;
3156 }
3157
3158 threads = 0;
3159 active_transactions = 0;
3160 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003161 struct binder_thread *thread;
3162
3163 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003164 threads++;
3165 active_transactions += binder_free_thread(proc, thread);
3166 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003167
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003168 nodes = 0;
3169 incoming_refs = 0;
3170 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003171 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003172
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003173 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003174 nodes++;
3175 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003176 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003177 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003178
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003179 outgoing_refs = 0;
3180 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003181 struct binder_ref *ref;
3182
3183 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003184 outgoing_refs++;
3185 binder_delete_ref(ref);
3186 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003187
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003188 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003189 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003190
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003191 buffers = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003192 while ((n = rb_first(&proc->allocated_buffers))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003193 struct binder_buffer *buffer;
3194
3195 buffer = rb_entry(n, struct binder_buffer, rb_node);
3196
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003197 t = buffer->transaction;
3198 if (t) {
3199 t->buffer = NULL;
3200 buffer->transaction = NULL;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303201 pr_err("release proc %d, transaction %d, not freed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003202 proc->pid, t->debug_id);
3203 /*BUG();*/
3204 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003205
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003206 binder_free_buf(proc, buffer);
3207 buffers++;
3208 }
3209
3210 binder_stats_deleted(BINDER_STAT_PROC);
3211
3212 page_count = 0;
3213 if (proc->pages) {
3214 int i;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003215
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003216 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003217 void *page_addr;
3218
3219 if (!proc->pages[i])
3220 continue;
3221
3222 page_addr = proc->buffer + i * PAGE_SIZE;
3223 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Ben Hutchings9cd14472019-05-29 18:02:44 +01003224 "%s: %d: page %d at %pK not freed\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003225 __func__, proc->pid, i, page_addr);
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003226 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3227 __free_page(proc->pages[i]);
3228 page_count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003229 }
3230 kfree(proc->pages);
3231 vfree(proc->buffer);
3232 }
3233
3234 put_task_struct(proc->tsk);
3235
3236 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003237 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3238 __func__, proc->pid, threads, nodes, incoming_refs,
3239 outgoing_refs, active_transactions, buffers, page_count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003240
3241 kfree(proc);
3242}
3243
3244static void binder_deferred_func(struct work_struct *work)
3245{
3246 struct binder_proc *proc;
3247 struct files_struct *files;
3248
3249 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003250
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003251 do {
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003252 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003253 mutex_lock(&binder_deferred_lock);
3254 if (!hlist_empty(&binder_deferred_list)) {
3255 proc = hlist_entry(binder_deferred_list.first,
3256 struct binder_proc, deferred_work_node);
3257 hlist_del_init(&proc->deferred_work_node);
3258 defer = proc->deferred_work;
3259 proc->deferred_work = 0;
3260 } else {
3261 proc = NULL;
3262 defer = 0;
3263 }
3264 mutex_unlock(&binder_deferred_lock);
3265
3266 files = NULL;
3267 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjosc0d75da2017-11-27 09:32:33 -08003268 mutex_lock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003269 files = proc->files;
3270 if (files)
3271 proc->files = NULL;
Todd Kjosc0d75da2017-11-27 09:32:33 -08003272 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003273 }
3274
3275 if (defer & BINDER_DEFERRED_FLUSH)
3276 binder_deferred_flush(proc);
3277
3278 if (defer & BINDER_DEFERRED_RELEASE)
3279 binder_deferred_release(proc); /* frees proc */
3280
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003281 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003282 if (files)
3283 put_files_struct(files);
3284 } while (proc);
3285}
3286static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3287
3288static void
3289binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3290{
3291 mutex_lock(&binder_deferred_lock);
3292 proc->deferred_work |= defer;
3293 if (hlist_unhashed(&proc->deferred_work_node)) {
3294 hlist_add_head(&proc->deferred_work_node,
3295 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05303296 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003297 }
3298 mutex_unlock(&binder_deferred_lock);
3299}
3300
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003301static void print_binder_transaction(struct seq_file *m, const char *prefix,
3302 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003303{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003304 seq_printf(m,
Todd Kjos6f3433c2018-02-07 13:57:37 -08003305 "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003306 prefix, t->debug_id, t,
3307 t->from ? t->from->proc->pid : 0,
3308 t->from ? t->from->pid : 0,
3309 t->to_proc ? t->to_proc->pid : 0,
3310 t->to_thread ? t->to_thread->pid : 0,
3311 t->code, t->flags, t->priority, t->need_reply);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003312 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003313 seq_puts(m, " buffer free\n");
3314 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003315 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003316 if (t->buffer->target_node)
3317 seq_printf(m, " node %d",
3318 t->buffer->target_node->debug_id);
Todd Kjos6f3433c2018-02-07 13:57:37 -08003319 seq_printf(m, " size %zd:%zd data %pK\n",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003320 t->buffer->data_size, t->buffer->offsets_size,
3321 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003322}
3323
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003324static void print_binder_buffer(struct seq_file *m, const char *prefix,
3325 struct binder_buffer *buffer)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003326{
Ben Hutchings9cd14472019-05-29 18:02:44 +01003327 seq_printf(m, "%s %d: %pK size %zd:%zd %s\n",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003328 prefix, buffer->debug_id, buffer->data,
3329 buffer->data_size, buffer->offsets_size,
3330 buffer->transaction ? "active" : "delivered");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003331}
3332
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003333static void print_binder_work(struct seq_file *m, const char *prefix,
3334 const char *transaction_prefix,
3335 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003336{
3337 struct binder_node *node;
3338 struct binder_transaction *t;
3339
3340 switch (w->type) {
3341 case BINDER_WORK_TRANSACTION:
3342 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003343 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003344 break;
3345 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003346 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003347 break;
3348 case BINDER_WORK_NODE:
3349 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003350 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3351 prefix, node->debug_id,
3352 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003353 break;
3354 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003355 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003356 break;
3357 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003358 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003359 break;
3360 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003361 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003362 break;
3363 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003364 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003365 break;
3366 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003367}
3368
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003369static void print_binder_thread(struct seq_file *m,
3370 struct binder_thread *thread,
3371 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003372{
3373 struct binder_transaction *t;
3374 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003375 size_t start_pos = m->count;
3376 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003377
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003378 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3379 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003380 t = thread->transaction_stack;
3381 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003382 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003383 print_binder_transaction(m,
3384 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003385 t = t->from_parent;
3386 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003387 print_binder_transaction(m,
3388 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003389 t = t->to_parent;
3390 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003391 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003392 t = NULL;
3393 }
3394 }
3395 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003396 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003397 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003398 if (!print_always && m->count == header_pos)
3399 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003400}
3401
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003402static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003403{
3404 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003405 struct binder_work *w;
3406 int count;
3407
3408 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003409 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003410 count++;
3411
Arve Hjønnevågda498892014-02-21 14:40:26 -08003412 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3413 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003414 node->has_strong_ref, node->has_weak_ref,
3415 node->local_strong_refs, node->local_weak_refs,
3416 node->internal_strong_refs, count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003417 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003418 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003419 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003420 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003421 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003422 seq_puts(m, "\n");
3423 list_for_each_entry(w, &node->async_todo, entry)
3424 print_binder_work(m, " ",
3425 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003426}
3427
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003428static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003429{
Ben Hutchings9cd14472019-05-29 18:02:44 +01003430 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003431 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3432 ref->node->debug_id, ref->strong, ref->weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003433}
3434
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003435static void print_binder_proc(struct seq_file *m,
3436 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003437{
3438 struct binder_work *w;
3439 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003440 size_t start_pos = m->count;
3441 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003442
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003443 seq_printf(m, "proc %d\n", proc->pid);
3444 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003445
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003446 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3447 print_binder_thread(m, rb_entry(n, struct binder_thread,
3448 rb_node), print_all);
3449 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003450 struct binder_node *node = rb_entry(n, struct binder_node,
3451 rb_node);
3452 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003453 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003454 }
3455 if (print_all) {
3456 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003457 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003458 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003459 print_binder_ref(m, rb_entry(n, struct binder_ref,
3460 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003461 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003462 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3463 print_binder_buffer(m, " buffer",
3464 rb_entry(n, struct binder_buffer, rb_node));
3465 list_for_each_entry(w, &proc->todo, entry)
3466 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003467 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003468 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003469 break;
3470 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003471 if (!print_all && m->count == header_pos)
3472 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003473}
3474
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003475static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003476 "BR_ERROR",
3477 "BR_OK",
3478 "BR_TRANSACTION",
3479 "BR_REPLY",
3480 "BR_ACQUIRE_RESULT",
3481 "BR_DEAD_REPLY",
3482 "BR_TRANSACTION_COMPLETE",
3483 "BR_INCREFS",
3484 "BR_ACQUIRE",
3485 "BR_RELEASE",
3486 "BR_DECREFS",
3487 "BR_ATTEMPT_ACQUIRE",
3488 "BR_NOOP",
3489 "BR_SPAWN_LOOPER",
3490 "BR_FINISHED",
3491 "BR_DEAD_BINDER",
3492 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3493 "BR_FAILED_REPLY"
3494};
3495
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003496static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003497 "BC_TRANSACTION",
3498 "BC_REPLY",
3499 "BC_ACQUIRE_RESULT",
3500 "BC_FREE_BUFFER",
3501 "BC_INCREFS",
3502 "BC_ACQUIRE",
3503 "BC_RELEASE",
3504 "BC_DECREFS",
3505 "BC_INCREFS_DONE",
3506 "BC_ACQUIRE_DONE",
3507 "BC_ATTEMPT_ACQUIRE",
3508 "BC_REGISTER_LOOPER",
3509 "BC_ENTER_LOOPER",
3510 "BC_EXIT_LOOPER",
3511 "BC_REQUEST_DEATH_NOTIFICATION",
3512 "BC_CLEAR_DEATH_NOTIFICATION",
3513 "BC_DEAD_BINDER_DONE"
3514};
3515
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003516static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003517 "proc",
3518 "thread",
3519 "node",
3520 "ref",
3521 "death",
3522 "transaction",
3523 "transaction_complete"
3524};
3525
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003526static void print_binder_stats(struct seq_file *m, const char *prefix,
3527 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003528{
3529 int i;
3530
3531 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003532 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003533 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3534 if (stats->bc[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003535 seq_printf(m, "%s%s: %d\n", prefix,
3536 binder_command_strings[i], stats->bc[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003537 }
3538
3539 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003540 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003541 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3542 if (stats->br[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003543 seq_printf(m, "%s%s: %d\n", prefix,
3544 binder_return_strings[i], stats->br[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003545 }
3546
3547 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003548 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003549 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003550 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003551 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3552 if (stats->obj_created[i] || stats->obj_deleted[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003553 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3554 binder_objstat_strings[i],
3555 stats->obj_created[i] - stats->obj_deleted[i],
3556 stats->obj_created[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003557 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003558}
3559
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003560static void print_binder_proc_stats(struct seq_file *m,
3561 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003562{
3563 struct binder_work *w;
3564 struct rb_node *n;
3565 int count, strong, weak;
3566
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003567 seq_printf(m, "proc %d\n", proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003568 count = 0;
3569 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3570 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003571 seq_printf(m, " threads: %d\n", count);
3572 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003573 " ready threads %d\n"
3574 " free async space %zd\n", proc->requested_threads,
3575 proc->requested_threads_started, proc->max_threads,
3576 proc->ready_threads, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003577 count = 0;
3578 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3579 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003580 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003581 count = 0;
3582 strong = 0;
3583 weak = 0;
3584 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3585 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3586 rb_node_desc);
3587 count++;
3588 strong += ref->strong;
3589 weak += ref->weak;
3590 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003591 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003592
3593 count = 0;
3594 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3595 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003596 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003597
3598 count = 0;
3599 list_for_each_entry(w, &proc->todo, entry) {
3600 switch (w->type) {
3601 case BINDER_WORK_TRANSACTION:
3602 count++;
3603 break;
3604 default:
3605 break;
3606 }
3607 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003608 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003609
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003610 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003611}
3612
3613
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003614static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003615{
3616 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003617 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003618 int do_lock = !binder_debug_no_lock;
3619
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003620 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003621 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003622
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003623 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003624
3625 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003626 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003627 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003628 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003629
Sasha Levinb67bfe02013-02-27 17:06:00 -08003630 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003631 print_binder_proc(m, proc, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003632 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003633 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003634 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003635}
3636
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003637static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003638{
3639 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003640 int do_lock = !binder_debug_no_lock;
3641
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003642 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003643 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003644
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003645 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003646
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003647 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003648
Sasha Levinb67bfe02013-02-27 17:06:00 -08003649 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003650 print_binder_proc_stats(m, proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003651 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003652 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003653 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003654}
3655
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003656static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003657{
3658 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003659 int do_lock = !binder_debug_no_lock;
3660
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003661 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003662 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003663
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003664 seq_puts(m, "binder transactions:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003665 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003666 print_binder_proc(m, proc, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003667 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003668 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003669 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003670}
3671
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003672static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003673{
Riley Andrews83050a42016-02-09 21:05:33 -08003674 struct binder_proc *itr;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003675 struct binder_proc *proc = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003676 int do_lock = !binder_debug_no_lock;
Riley Andrews83050a42016-02-09 21:05:33 -08003677 bool valid_proc = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003678
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003679 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003680 binder_lock(__func__);
Riley Andrews83050a42016-02-09 21:05:33 -08003681
3682 hlist_for_each_entry(itr, &binder_procs, proc_node) {
3683 if (itr == proc) {
3684 valid_proc = true;
3685 break;
3686 }
3687 }
3688 if (valid_proc) {
3689 seq_puts(m, "binder proc state:\n");
3690 print_binder_proc(m, proc, 1);
3691 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003692 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003693 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003694 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003695}
3696
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003697static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003698 struct binder_transaction_log_entry *e)
3699{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003700 seq_printf(m,
3701 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3702 e->debug_id, (e->call_type == 2) ? "reply" :
3703 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3704 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3705 e->target_handle, e->data_size, e->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003706}
3707
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003708static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003709{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003710 struct binder_transaction_log *log = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003711 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003712
3713 if (log->full) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003714 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3715 print_binder_transaction_log_entry(m, &log->entry[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003716 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003717 for (i = 0; i < log->next; i++)
3718 print_binder_transaction_log_entry(m, &log->entry[i]);
3719 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003720}
3721
3722static const struct file_operations binder_fops = {
3723 .owner = THIS_MODULE,
3724 .poll = binder_poll,
3725 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003726 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003727 .mmap = binder_mmap,
3728 .open = binder_open,
3729 .flush = binder_flush,
3730 .release = binder_release,
3731};
3732
3733static struct miscdevice binder_miscdev = {
3734 .minor = MISC_DYNAMIC_MINOR,
3735 .name = "binder",
3736 .fops = &binder_fops
3737};
3738
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003739BINDER_DEBUG_ENTRY(state);
3740BINDER_DEBUG_ENTRY(stats);
3741BINDER_DEBUG_ENTRY(transactions);
3742BINDER_DEBUG_ENTRY(transaction_log);
3743
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003744static int __init binder_init(void)
3745{
3746 int ret;
3747
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003748 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3749 if (binder_debugfs_dir_entry_root)
3750 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3751 binder_debugfs_dir_entry_root);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003752 ret = misc_register(&binder_miscdev);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003753 if (binder_debugfs_dir_entry_root) {
3754 debugfs_create_file("state",
3755 S_IRUGO,
3756 binder_debugfs_dir_entry_root,
3757 NULL,
3758 &binder_state_fops);
3759 debugfs_create_file("stats",
3760 S_IRUGO,
3761 binder_debugfs_dir_entry_root,
3762 NULL,
3763 &binder_stats_fops);
3764 debugfs_create_file("transactions",
3765 S_IRUGO,
3766 binder_debugfs_dir_entry_root,
3767 NULL,
3768 &binder_transactions_fops);
3769 debugfs_create_file("transaction_log",
3770 S_IRUGO,
3771 binder_debugfs_dir_entry_root,
3772 &binder_transaction_log,
3773 &binder_transaction_log_fops);
3774 debugfs_create_file("failed_transaction_log",
3775 S_IRUGO,
3776 binder_debugfs_dir_entry_root,
3777 &binder_transaction_log_failed,
3778 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003779 }
3780 return ret;
3781}
3782
3783device_initcall(binder_init);
3784
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003785#define CREATE_TRACE_POINTS
3786#include "binder_trace.h"
3787
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003788MODULE_LICENSE("GPL v2");