blob: b6f4baab6848a9321067dfa6f1bccefe2d57268e [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
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002644 if (send_reply)
2645 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2646 binder_release_work(&thread->todo);
2647 kfree(thread);
2648 binder_stats_deleted(BINDER_STAT_THREAD);
2649 return active_transactions;
2650}
2651
2652static unsigned int binder_poll(struct file *filp,
2653 struct poll_table_struct *wait)
2654{
2655 struct binder_proc *proc = filp->private_data;
2656 struct binder_thread *thread = NULL;
2657 int wait_for_proc_work;
2658
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002659 binder_lock(__func__);
2660
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002661 thread = binder_get_thread(proc);
Eric Biggersfebf1082018-02-26 10:56:45 -08002662 if (!thread) {
2663 binder_unlock(__func__);
Eric Biggers4be5a282018-01-30 23:11:24 -08002664 return POLLERR;
Eric Biggersfebf1082018-02-26 10:56:45 -08002665 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002666
Martijn Coenena494a712018-01-05 11:27:07 +01002667 thread->looper |= BINDER_LOOPER_STATE_POLL;
2668
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002669 wait_for_proc_work = thread->transaction_stack == NULL &&
2670 list_empty(&thread->todo) && thread->return_error == BR_OK;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002671
2672 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002673
2674 if (wait_for_proc_work) {
2675 if (binder_has_proc_work(proc, thread))
2676 return POLLIN;
2677 poll_wait(filp, &proc->wait, wait);
2678 if (binder_has_proc_work(proc, thread))
2679 return POLLIN;
2680 } else {
2681 if (binder_has_thread_work(thread))
2682 return POLLIN;
2683 poll_wait(filp, &thread->wait, wait);
2684 if (binder_has_thread_work(thread))
2685 return POLLIN;
2686 }
2687 return 0;
2688}
2689
Tair Rzayev78260ac2014-06-03 22:27:21 +03002690static int binder_ioctl_write_read(struct file *filp,
2691 unsigned int cmd, unsigned long arg,
2692 struct binder_thread *thread)
2693{
2694 int ret = 0;
2695 struct binder_proc *proc = filp->private_data;
2696 unsigned int size = _IOC_SIZE(cmd);
2697 void __user *ubuf = (void __user *)arg;
2698 struct binder_write_read bwr;
2699
2700 if (size != sizeof(struct binder_write_read)) {
2701 ret = -EINVAL;
2702 goto out;
2703 }
2704 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
2705 ret = -EFAULT;
2706 goto out;
2707 }
2708 binder_debug(BINDER_DEBUG_READ_WRITE,
2709 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
2710 proc->pid, thread->pid,
2711 (u64)bwr.write_size, (u64)bwr.write_buffer,
2712 (u64)bwr.read_size, (u64)bwr.read_buffer);
2713
2714 if (bwr.write_size > 0) {
2715 ret = binder_thread_write(proc, thread,
2716 bwr.write_buffer,
2717 bwr.write_size,
2718 &bwr.write_consumed);
2719 trace_binder_write_done(ret);
2720 if (ret < 0) {
2721 bwr.read_consumed = 0;
2722 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2723 ret = -EFAULT;
2724 goto out;
2725 }
2726 }
2727 if (bwr.read_size > 0) {
2728 ret = binder_thread_read(proc, thread, bwr.read_buffer,
2729 bwr.read_size,
2730 &bwr.read_consumed,
2731 filp->f_flags & O_NONBLOCK);
2732 trace_binder_read_done(ret);
2733 if (!list_empty(&proc->todo))
2734 wake_up_interruptible(&proc->wait);
2735 if (ret < 0) {
2736 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
2737 ret = -EFAULT;
2738 goto out;
2739 }
2740 }
2741 binder_debug(BINDER_DEBUG_READ_WRITE,
2742 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
2743 proc->pid, thread->pid,
2744 (u64)bwr.write_consumed, (u64)bwr.write_size,
2745 (u64)bwr.read_consumed, (u64)bwr.read_size);
2746 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
2747 ret = -EFAULT;
2748 goto out;
2749 }
2750out:
2751 return ret;
2752}
2753
2754static int binder_ioctl_set_ctx_mgr(struct file *filp)
2755{
2756 int ret = 0;
2757 struct binder_proc *proc = filp->private_data;
2758 kuid_t curr_euid = current_euid();
2759
2760 if (binder_context_mgr_node != NULL) {
2761 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
2762 ret = -EBUSY;
2763 goto out;
2764 }
Stephen Smalley79af7302015-01-21 10:54:10 -05002765 ret = security_binder_set_context_mgr(proc->tsk);
2766 if (ret < 0)
2767 goto out;
Tair Rzayev78260ac2014-06-03 22:27:21 +03002768 if (uid_valid(binder_context_mgr_uid)) {
2769 if (!uid_eq(binder_context_mgr_uid, curr_euid)) {
2770 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
2771 from_kuid(&init_user_ns, curr_euid),
2772 from_kuid(&init_user_ns,
2773 binder_context_mgr_uid));
2774 ret = -EPERM;
2775 goto out;
2776 }
2777 } else {
2778 binder_context_mgr_uid = curr_euid;
2779 }
2780 binder_context_mgr_node = binder_new_node(proc, 0, 0);
2781 if (binder_context_mgr_node == NULL) {
2782 ret = -ENOMEM;
2783 goto out;
2784 }
2785 binder_context_mgr_node->local_weak_refs++;
2786 binder_context_mgr_node->local_strong_refs++;
2787 binder_context_mgr_node->has_strong_ref = 1;
2788 binder_context_mgr_node->has_weak_ref = 1;
2789out:
2790 return ret;
2791}
2792
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002793static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2794{
2795 int ret;
2796 struct binder_proc *proc = filp->private_data;
2797 struct binder_thread *thread;
2798 unsigned int size = _IOC_SIZE(cmd);
2799 void __user *ubuf = (void __user *)arg;
2800
Tair Rzayev78260ac2014-06-03 22:27:21 +03002801 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
2802 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002803
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002804 trace_binder_ioctl(cmd, arg);
2805
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002806 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2807 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002808 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002809
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002810 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002811 thread = binder_get_thread(proc);
2812 if (thread == NULL) {
2813 ret = -ENOMEM;
2814 goto err;
2815 }
2816
2817 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03002818 case BINDER_WRITE_READ:
2819 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
2820 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002821 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002822 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002823 case BINDER_SET_MAX_THREADS:
2824 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
2825 ret = -EINVAL;
2826 goto err;
2827 }
2828 break;
2829 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03002830 ret = binder_ioctl_set_ctx_mgr(filp);
2831 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002832 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002833 break;
2834 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302835 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002836 proc->pid, thread->pid);
2837 binder_free_thread(proc, thread);
2838 thread = NULL;
2839 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002840 case BINDER_VERSION: {
2841 struct binder_version __user *ver = ubuf;
2842
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002843 if (size != sizeof(struct binder_version)) {
2844 ret = -EINVAL;
2845 goto err;
2846 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02002847 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
2848 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002849 ret = -EINVAL;
2850 goto err;
2851 }
2852 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02002853 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002854 default:
2855 ret = -EINVAL;
2856 goto err;
2857 }
2858 ret = 0;
2859err:
2860 if (thread)
2861 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002862 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002863 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
2864 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05302865 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 -07002866err_unlocked:
2867 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002868 return ret;
2869}
2870
2871static void binder_vma_open(struct vm_area_struct *vma)
2872{
2873 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002874
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002875 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302876 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002877 proc->pid, vma->vm_start, vma->vm_end,
2878 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2879 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002880}
2881
2882static void binder_vma_close(struct vm_area_struct *vma)
2883{
2884 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09002885
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002886 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302887 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002888 proc->pid, vma->vm_start, vma->vm_end,
2889 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2890 (unsigned long)pgprot_val(vma->vm_page_prot));
2891 proc->vma = NULL;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08002892 proc->vma_vm_mm = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002893 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
2894}
2895
Vinayak Menonddac7d52014-06-02 18:17:59 +05302896static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2897{
2898 return VM_FAULT_SIGBUS;
2899}
2900
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07002901static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002902 .open = binder_vma_open,
2903 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05302904 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002905};
2906
2907static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
2908{
2909 int ret;
2910 struct vm_struct *area;
2911 struct binder_proc *proc = filp->private_data;
2912 const char *failure_string;
2913 struct binder_buffer *buffer;
2914
Martijn Coenencbd854d2017-07-28 13:56:08 +02002915 if (proc->tsk != current->group_leader)
Al Viroa79f41e2012-08-15 18:23:36 -04002916 return -EINVAL;
2917
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002918 if ((vma->vm_end - vma->vm_start) > SZ_4M)
2919 vma->vm_end = vma->vm_start + SZ_4M;
2920
2921 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
2922 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
2923 proc->pid, vma->vm_start, vma->vm_end,
2924 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
2925 (unsigned long)pgprot_val(vma->vm_page_prot));
2926
2927 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
2928 ret = -EPERM;
2929 failure_string = "bad vm_flags";
2930 goto err_bad_arg;
2931 }
2932 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
2933
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002934 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002935 if (proc->buffer) {
2936 ret = -EBUSY;
2937 failure_string = "already mapped";
2938 goto err_already_mapped;
2939 }
2940
2941 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
2942 if (area == NULL) {
2943 ret = -ENOMEM;
2944 failure_string = "get_vm_area";
2945 goto err_get_vm_area_failed;
2946 }
2947 proc->buffer = area->addr;
2948 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002949 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002950
2951#ifdef CONFIG_CPU_CACHE_VIPT
2952 if (cache_is_vipt_aliasing()) {
2953 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
Ben Hutchings9cd14472019-05-29 18:02:44 +01002954 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 +09002955 vma->vm_start += PAGE_SIZE;
2956 }
2957 }
2958#endif
2959 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
2960 if (proc->pages == NULL) {
2961 ret = -ENOMEM;
2962 failure_string = "alloc page array";
2963 goto err_alloc_pages_failed;
2964 }
2965 proc->buffer_size = vma->vm_end - vma->vm_start;
2966
2967 vma->vm_ops = &binder_vm_ops;
2968 vma->vm_private_data = proc;
2969
2970 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
2971 ret = -ENOMEM;
2972 failure_string = "alloc small buf";
2973 goto err_alloc_small_buf_failed;
2974 }
2975 buffer = proc->buffer;
2976 INIT_LIST_HEAD(&proc->buffers);
2977 list_add(&buffer->entry, &proc->buffers);
2978 buffer->free = 1;
2979 binder_insert_free_buffer(proc, buffer);
2980 proc->free_async_space = proc->buffer_size / 2;
2981 barrier();
Todd Kjosc0d75da2017-11-27 09:32:33 -08002982 mutex_lock(&proc->files_lock);
Al Viroa79f41e2012-08-15 18:23:36 -04002983 proc->files = get_files_struct(current);
Todd Kjosc0d75da2017-11-27 09:32:33 -08002984 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002985 proc->vma = vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08002986 proc->vma_vm_mm = vma->vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002987
Sherwin Soltani258767f2012-06-26 02:00:30 -04002988 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002989 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
2990 return 0;
2991
2992err_alloc_small_buf_failed:
2993 kfree(proc->pages);
2994 proc->pages = NULL;
2995err_alloc_pages_failed:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08002996 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002997 vfree(proc->buffer);
2998 proc->buffer = NULL;
2999err_get_vm_area_failed:
3000err_already_mapped:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08003001 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003002err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04003003 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003004 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
3005 return ret;
3006}
3007
3008static int binder_open(struct inode *nodp, struct file *filp)
3009{
3010 struct binder_proc *proc;
3011
3012 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
3013 current->group_leader->pid, current->pid);
3014
3015 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
3016 if (proc == NULL)
3017 return -ENOMEM;
Todd Kjos51050752017-06-29 12:01:36 -07003018 get_task_struct(current->group_leader);
3019 proc->tsk = current->group_leader;
Todd Kjosc0d75da2017-11-27 09:32:33 -08003020 mutex_init(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003021 INIT_LIST_HEAD(&proc->todo);
3022 init_waitqueue_head(&proc->wait);
3023 proc->default_priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003024
3025 binder_lock(__func__);
3026
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003027 binder_stats_created(BINDER_STAT_PROC);
3028 hlist_add_head(&proc->proc_node, &binder_procs);
3029 proc->pid = current->group_leader->pid;
3030 INIT_LIST_HEAD(&proc->delivered_death);
3031 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003032
3033 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003034
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003035 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003036 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09003037
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003038 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003039 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
3040 binder_debugfs_dir_entry_proc, proc, &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003041 }
3042
3043 return 0;
3044}
3045
3046static int binder_flush(struct file *filp, fl_owner_t id)
3047{
3048 struct binder_proc *proc = filp->private_data;
3049
3050 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3051
3052 return 0;
3053}
3054
3055static void binder_deferred_flush(struct binder_proc *proc)
3056{
3057 struct rb_node *n;
3058 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09003059
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003060 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3061 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09003062
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003063 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3064 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3065 wake_up_interruptible(&thread->wait);
3066 wake_count++;
3067 }
3068 }
3069 wake_up_interruptible_all(&proc->wait);
3070
3071 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3072 "binder_flush: %d woke %d threads\n", proc->pid,
3073 wake_count);
3074}
3075
3076static int binder_release(struct inode *nodp, struct file *filp)
3077{
3078 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003079
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003080 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003081 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3082
3083 return 0;
3084}
3085
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003086static int binder_node_release(struct binder_node *node, int refs)
3087{
3088 struct binder_ref *ref;
3089 int death = 0;
3090
3091 list_del_init(&node->work.entry);
3092 binder_release_work(&node->async_todo);
3093
3094 if (hlist_empty(&node->refs)) {
3095 kfree(node);
3096 binder_stats_deleted(BINDER_STAT_NODE);
3097
3098 return refs;
3099 }
3100
3101 node->proc = NULL;
3102 node->local_strong_refs = 0;
3103 node->local_weak_refs = 0;
3104 hlist_add_head(&node->dead_node, &binder_dead_nodes);
3105
3106 hlist_for_each_entry(ref, &node->refs, node_entry) {
3107 refs++;
3108
3109 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08003110 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003111
3112 death++;
3113
3114 if (list_empty(&ref->death->work.entry)) {
3115 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3116 list_add_tail(&ref->death->work.entry,
3117 &ref->proc->todo);
3118 wake_up_interruptible(&ref->proc->wait);
3119 } else
3120 BUG();
3121 }
3122
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003123 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3124 "node %d now dead, refs %d, death %d\n",
3125 node->debug_id, refs, death);
3126
3127 return refs;
3128}
3129
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003130static void binder_deferred_release(struct binder_proc *proc)
3131{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003132 struct binder_transaction *t;
3133 struct rb_node *n;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003134 int threads, nodes, incoming_refs, outgoing_refs, buffers,
3135 active_transactions, page_count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003136
3137 BUG_ON(proc->vma);
3138 BUG_ON(proc->files);
3139
3140 hlist_del(&proc->proc_node);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003141
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003142 if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) {
3143 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003144 "%s: %d context_mgr_node gone\n",
3145 __func__, proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003146 binder_context_mgr_node = NULL;
3147 }
3148
3149 threads = 0;
3150 active_transactions = 0;
3151 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003152 struct binder_thread *thread;
3153
3154 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003155 threads++;
3156 active_transactions += binder_free_thread(proc, thread);
3157 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003158
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003159 nodes = 0;
3160 incoming_refs = 0;
3161 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003162 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003163
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003164 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003165 nodes++;
3166 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003167 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003168 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003169
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003170 outgoing_refs = 0;
3171 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003172 struct binder_ref *ref;
3173
3174 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003175 outgoing_refs++;
3176 binder_delete_ref(ref);
3177 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003178
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003179 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003180 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003181
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003182 buffers = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003183 while ((n = rb_first(&proc->allocated_buffers))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003184 struct binder_buffer *buffer;
3185
3186 buffer = rb_entry(n, struct binder_buffer, rb_node);
3187
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003188 t = buffer->transaction;
3189 if (t) {
3190 t->buffer = NULL;
3191 buffer->transaction = NULL;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303192 pr_err("release proc %d, transaction %d, not freed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003193 proc->pid, t->debug_id);
3194 /*BUG();*/
3195 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003196
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003197 binder_free_buf(proc, buffer);
3198 buffers++;
3199 }
3200
3201 binder_stats_deleted(BINDER_STAT_PROC);
3202
3203 page_count = 0;
3204 if (proc->pages) {
3205 int i;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003206
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003207 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003208 void *page_addr;
3209
3210 if (!proc->pages[i])
3211 continue;
3212
3213 page_addr = proc->buffer + i * PAGE_SIZE;
3214 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Ben Hutchings9cd14472019-05-29 18:02:44 +01003215 "%s: %d: page %d at %pK not freed\n",
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003216 __func__, proc->pid, i, page_addr);
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003217 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3218 __free_page(proc->pages[i]);
3219 page_count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003220 }
3221 kfree(proc->pages);
3222 vfree(proc->buffer);
3223 }
3224
3225 put_task_struct(proc->tsk);
3226
3227 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003228 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3229 __func__, proc->pid, threads, nodes, incoming_refs,
3230 outgoing_refs, active_transactions, buffers, page_count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003231
3232 kfree(proc);
3233}
3234
3235static void binder_deferred_func(struct work_struct *work)
3236{
3237 struct binder_proc *proc;
3238 struct files_struct *files;
3239
3240 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003241
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003242 do {
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003243 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003244 mutex_lock(&binder_deferred_lock);
3245 if (!hlist_empty(&binder_deferred_list)) {
3246 proc = hlist_entry(binder_deferred_list.first,
3247 struct binder_proc, deferred_work_node);
3248 hlist_del_init(&proc->deferred_work_node);
3249 defer = proc->deferred_work;
3250 proc->deferred_work = 0;
3251 } else {
3252 proc = NULL;
3253 defer = 0;
3254 }
3255 mutex_unlock(&binder_deferred_lock);
3256
3257 files = NULL;
3258 if (defer & BINDER_DEFERRED_PUT_FILES) {
Todd Kjosc0d75da2017-11-27 09:32:33 -08003259 mutex_lock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003260 files = proc->files;
3261 if (files)
3262 proc->files = NULL;
Todd Kjosc0d75da2017-11-27 09:32:33 -08003263 mutex_unlock(&proc->files_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003264 }
3265
3266 if (defer & BINDER_DEFERRED_FLUSH)
3267 binder_deferred_flush(proc);
3268
3269 if (defer & BINDER_DEFERRED_RELEASE)
3270 binder_deferred_release(proc); /* frees proc */
3271
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003272 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003273 if (files)
3274 put_files_struct(files);
3275 } while (proc);
3276}
3277static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3278
3279static void
3280binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3281{
3282 mutex_lock(&binder_deferred_lock);
3283 proc->deferred_work |= defer;
3284 if (hlist_unhashed(&proc->deferred_work_node)) {
3285 hlist_add_head(&proc->deferred_work_node,
3286 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05303287 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003288 }
3289 mutex_unlock(&binder_deferred_lock);
3290}
3291
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003292static void print_binder_transaction(struct seq_file *m, const char *prefix,
3293 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003294{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003295 seq_printf(m,
Todd Kjos6f3433c2018-02-07 13:57:37 -08003296 "%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 -07003297 prefix, t->debug_id, t,
3298 t->from ? t->from->proc->pid : 0,
3299 t->from ? t->from->pid : 0,
3300 t->to_proc ? t->to_proc->pid : 0,
3301 t->to_thread ? t->to_thread->pid : 0,
3302 t->code, t->flags, t->priority, t->need_reply);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003303 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003304 seq_puts(m, " buffer free\n");
3305 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003306 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003307 if (t->buffer->target_node)
3308 seq_printf(m, " node %d",
3309 t->buffer->target_node->debug_id);
Todd Kjos6f3433c2018-02-07 13:57:37 -08003310 seq_printf(m, " size %zd:%zd data %pK\n",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003311 t->buffer->data_size, t->buffer->offsets_size,
3312 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003313}
3314
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003315static void print_binder_buffer(struct seq_file *m, const char *prefix,
3316 struct binder_buffer *buffer)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003317{
Ben Hutchings9cd14472019-05-29 18:02:44 +01003318 seq_printf(m, "%s %d: %pK size %zd:%zd %s\n",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003319 prefix, buffer->debug_id, buffer->data,
3320 buffer->data_size, buffer->offsets_size,
3321 buffer->transaction ? "active" : "delivered");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003322}
3323
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003324static void print_binder_work(struct seq_file *m, const char *prefix,
3325 const char *transaction_prefix,
3326 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003327{
3328 struct binder_node *node;
3329 struct binder_transaction *t;
3330
3331 switch (w->type) {
3332 case BINDER_WORK_TRANSACTION:
3333 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003334 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003335 break;
3336 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003337 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003338 break;
3339 case BINDER_WORK_NODE:
3340 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003341 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3342 prefix, node->debug_id,
3343 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003344 break;
3345 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003346 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003347 break;
3348 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003349 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003350 break;
3351 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003352 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003353 break;
3354 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003355 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003356 break;
3357 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003358}
3359
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003360static void print_binder_thread(struct seq_file *m,
3361 struct binder_thread *thread,
3362 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003363{
3364 struct binder_transaction *t;
3365 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003366 size_t start_pos = m->count;
3367 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003368
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003369 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3370 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003371 t = thread->transaction_stack;
3372 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003373 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003374 print_binder_transaction(m,
3375 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003376 t = t->from_parent;
3377 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003378 print_binder_transaction(m,
3379 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003380 t = t->to_parent;
3381 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003382 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003383 t = NULL;
3384 }
3385 }
3386 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003387 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003388 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003389 if (!print_always && m->count == header_pos)
3390 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003391}
3392
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003393static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003394{
3395 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003396 struct binder_work *w;
3397 int count;
3398
3399 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003400 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003401 count++;
3402
Arve Hjønnevågda498892014-02-21 14:40:26 -08003403 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3404 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003405 node->has_strong_ref, node->has_weak_ref,
3406 node->local_strong_refs, node->local_weak_refs,
3407 node->internal_strong_refs, count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003408 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003409 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003410 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003411 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003412 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003413 seq_puts(m, "\n");
3414 list_for_each_entry(w, &node->async_todo, entry)
3415 print_binder_work(m, " ",
3416 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003417}
3418
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003419static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003420{
Ben Hutchings9cd14472019-05-29 18:02:44 +01003421 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 -07003422 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3423 ref->node->debug_id, ref->strong, ref->weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003424}
3425
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003426static void print_binder_proc(struct seq_file *m,
3427 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003428{
3429 struct binder_work *w;
3430 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003431 size_t start_pos = m->count;
3432 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003433
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003434 seq_printf(m, "proc %d\n", proc->pid);
3435 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003436
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003437 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3438 print_binder_thread(m, rb_entry(n, struct binder_thread,
3439 rb_node), print_all);
3440 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003441 struct binder_node *node = rb_entry(n, struct binder_node,
3442 rb_node);
3443 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003444 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003445 }
3446 if (print_all) {
3447 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003448 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003449 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003450 print_binder_ref(m, rb_entry(n, struct binder_ref,
3451 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003452 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003453 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3454 print_binder_buffer(m, " buffer",
3455 rb_entry(n, struct binder_buffer, rb_node));
3456 list_for_each_entry(w, &proc->todo, entry)
3457 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003458 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003459 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003460 break;
3461 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003462 if (!print_all && m->count == header_pos)
3463 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003464}
3465
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003466static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003467 "BR_ERROR",
3468 "BR_OK",
3469 "BR_TRANSACTION",
3470 "BR_REPLY",
3471 "BR_ACQUIRE_RESULT",
3472 "BR_DEAD_REPLY",
3473 "BR_TRANSACTION_COMPLETE",
3474 "BR_INCREFS",
3475 "BR_ACQUIRE",
3476 "BR_RELEASE",
3477 "BR_DECREFS",
3478 "BR_ATTEMPT_ACQUIRE",
3479 "BR_NOOP",
3480 "BR_SPAWN_LOOPER",
3481 "BR_FINISHED",
3482 "BR_DEAD_BINDER",
3483 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3484 "BR_FAILED_REPLY"
3485};
3486
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003487static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003488 "BC_TRANSACTION",
3489 "BC_REPLY",
3490 "BC_ACQUIRE_RESULT",
3491 "BC_FREE_BUFFER",
3492 "BC_INCREFS",
3493 "BC_ACQUIRE",
3494 "BC_RELEASE",
3495 "BC_DECREFS",
3496 "BC_INCREFS_DONE",
3497 "BC_ACQUIRE_DONE",
3498 "BC_ATTEMPT_ACQUIRE",
3499 "BC_REGISTER_LOOPER",
3500 "BC_ENTER_LOOPER",
3501 "BC_EXIT_LOOPER",
3502 "BC_REQUEST_DEATH_NOTIFICATION",
3503 "BC_CLEAR_DEATH_NOTIFICATION",
3504 "BC_DEAD_BINDER_DONE"
3505};
3506
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003507static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003508 "proc",
3509 "thread",
3510 "node",
3511 "ref",
3512 "death",
3513 "transaction",
3514 "transaction_complete"
3515};
3516
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003517static void print_binder_stats(struct seq_file *m, const char *prefix,
3518 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003519{
3520 int i;
3521
3522 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003523 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003524 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3525 if (stats->bc[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003526 seq_printf(m, "%s%s: %d\n", prefix,
3527 binder_command_strings[i], stats->bc[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003528 }
3529
3530 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003531 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003532 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3533 if (stats->br[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003534 seq_printf(m, "%s%s: %d\n", prefix,
3535 binder_return_strings[i], stats->br[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003536 }
3537
3538 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003539 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003540 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003541 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003542 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3543 if (stats->obj_created[i] || stats->obj_deleted[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003544 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3545 binder_objstat_strings[i],
3546 stats->obj_created[i] - stats->obj_deleted[i],
3547 stats->obj_created[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003548 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003549}
3550
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003551static void print_binder_proc_stats(struct seq_file *m,
3552 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003553{
3554 struct binder_work *w;
3555 struct rb_node *n;
3556 int count, strong, weak;
3557
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003558 seq_printf(m, "proc %d\n", proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003559 count = 0;
3560 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3561 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003562 seq_printf(m, " threads: %d\n", count);
3563 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003564 " ready threads %d\n"
3565 " free async space %zd\n", proc->requested_threads,
3566 proc->requested_threads_started, proc->max_threads,
3567 proc->ready_threads, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003568 count = 0;
3569 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3570 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003571 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003572 count = 0;
3573 strong = 0;
3574 weak = 0;
3575 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3576 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3577 rb_node_desc);
3578 count++;
3579 strong += ref->strong;
3580 weak += ref->weak;
3581 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003582 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003583
3584 count = 0;
3585 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3586 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003587 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003588
3589 count = 0;
3590 list_for_each_entry(w, &proc->todo, entry) {
3591 switch (w->type) {
3592 case BINDER_WORK_TRANSACTION:
3593 count++;
3594 break;
3595 default:
3596 break;
3597 }
3598 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003599 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003600
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003601 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003602}
3603
3604
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003605static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003606{
3607 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003608 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003609 int do_lock = !binder_debug_no_lock;
3610
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003611 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003612 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003613
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003614 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003615
3616 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003617 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003618 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003619 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003620
Sasha Levinb67bfe02013-02-27 17:06:00 -08003621 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003622 print_binder_proc(m, proc, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003623 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003624 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003625 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003626}
3627
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003628static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003629{
3630 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003631 int do_lock = !binder_debug_no_lock;
3632
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003633 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003634 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003635
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003636 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003637
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003638 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003639
Sasha Levinb67bfe02013-02-27 17:06:00 -08003640 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003641 print_binder_proc_stats(m, proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003642 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003643 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003644 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003645}
3646
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003647static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003648{
3649 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003650 int do_lock = !binder_debug_no_lock;
3651
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003652 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003653 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003654
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003655 seq_puts(m, "binder transactions:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003656 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003657 print_binder_proc(m, proc, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003658 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003659 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003660 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003661}
3662
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003663static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003664{
Riley Andrews83050a42016-02-09 21:05:33 -08003665 struct binder_proc *itr;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003666 struct binder_proc *proc = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003667 int do_lock = !binder_debug_no_lock;
Riley Andrews83050a42016-02-09 21:05:33 -08003668 bool valid_proc = false;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003669
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003670 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003671 binder_lock(__func__);
Riley Andrews83050a42016-02-09 21:05:33 -08003672
3673 hlist_for_each_entry(itr, &binder_procs, proc_node) {
3674 if (itr == proc) {
3675 valid_proc = true;
3676 break;
3677 }
3678 }
3679 if (valid_proc) {
3680 seq_puts(m, "binder proc state:\n");
3681 print_binder_proc(m, proc, 1);
3682 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003683 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003684 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003685 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003686}
3687
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003688static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003689 struct binder_transaction_log_entry *e)
3690{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003691 seq_printf(m,
3692 "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n",
3693 e->debug_id, (e->call_type == 2) ? "reply" :
3694 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
3695 e->from_thread, e->to_proc, e->to_thread, e->to_node,
3696 e->target_handle, e->data_size, e->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003697}
3698
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003699static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003700{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003701 struct binder_transaction_log *log = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003702 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003703
3704 if (log->full) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003705 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
3706 print_binder_transaction_log_entry(m, &log->entry[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003707 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003708 for (i = 0; i < log->next; i++)
3709 print_binder_transaction_log_entry(m, &log->entry[i]);
3710 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003711}
3712
3713static const struct file_operations binder_fops = {
3714 .owner = THIS_MODULE,
3715 .poll = binder_poll,
3716 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08003717 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003718 .mmap = binder_mmap,
3719 .open = binder_open,
3720 .flush = binder_flush,
3721 .release = binder_release,
3722};
3723
3724static struct miscdevice binder_miscdev = {
3725 .minor = MISC_DYNAMIC_MINOR,
3726 .name = "binder",
3727 .fops = &binder_fops
3728};
3729
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003730BINDER_DEBUG_ENTRY(state);
3731BINDER_DEBUG_ENTRY(stats);
3732BINDER_DEBUG_ENTRY(transactions);
3733BINDER_DEBUG_ENTRY(transaction_log);
3734
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003735static int __init binder_init(void)
3736{
3737 int ret;
3738
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003739 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
3740 if (binder_debugfs_dir_entry_root)
3741 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
3742 binder_debugfs_dir_entry_root);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003743 ret = misc_register(&binder_miscdev);
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003744 if (binder_debugfs_dir_entry_root) {
3745 debugfs_create_file("state",
3746 S_IRUGO,
3747 binder_debugfs_dir_entry_root,
3748 NULL,
3749 &binder_state_fops);
3750 debugfs_create_file("stats",
3751 S_IRUGO,
3752 binder_debugfs_dir_entry_root,
3753 NULL,
3754 &binder_stats_fops);
3755 debugfs_create_file("transactions",
3756 S_IRUGO,
3757 binder_debugfs_dir_entry_root,
3758 NULL,
3759 &binder_transactions_fops);
3760 debugfs_create_file("transaction_log",
3761 S_IRUGO,
3762 binder_debugfs_dir_entry_root,
3763 &binder_transaction_log,
3764 &binder_transaction_log_fops);
3765 debugfs_create_file("failed_transaction_log",
3766 S_IRUGO,
3767 binder_debugfs_dir_entry_root,
3768 &binder_transaction_log_failed,
3769 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003770 }
3771 return ret;
3772}
3773
3774device_initcall(binder_init);
3775
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003776#define CREATE_TRACE_POINTS
3777#include "binder_trace.h"
3778
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003779MODULE_LICENSE("GPL v2");