blob: 6cf87476938825839acf6c5bfe6e802316b143df [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
Martijn Coenen6b7c7122016-09-30 16:08:09 +020053static HLIST_HEAD(binder_devices);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090054static HLIST_HEAD(binder_procs);
55static HLIST_HEAD(binder_deferred_list);
56static HLIST_HEAD(binder_dead_nodes);
57
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070058static struct dentry *binder_debugfs_dir_entry_root;
59static struct dentry *binder_debugfs_dir_entry_proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090060static int binder_last_id;
61
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070062#define BINDER_DEBUG_ENTRY(name) \
63static int binder_##name##_open(struct inode *inode, struct file *file) \
64{ \
Arve Hjønnevåg16b66552009-04-28 20:57:50 -070065 return single_open(file, binder_##name##_show, inode->i_private); \
Arve Hjønnevåg5249f482009-04-28 20:57:50 -070066} \
67\
68static const struct file_operations binder_##name##_fops = { \
69 .owner = THIS_MODULE, \
70 .open = binder_##name##_open, \
71 .read = seq_read, \
72 .llseek = seq_lseek, \
73 .release = single_release, \
74}
75
76static int binder_proc_show(struct seq_file *m, void *unused);
77BINDER_DEBUG_ENTRY(proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +090078
79/* This is only defined in include/asm-arm/sizes.h */
80#ifndef SZ_1K
81#define SZ_1K 0x400
82#endif
83
84#ifndef SZ_4M
85#define SZ_4M 0x400000
86#endif
87
88#define FORBIDDEN_MMAP_FLAGS (VM_WRITE)
89
90#define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64)
91
92enum {
93 BINDER_DEBUG_USER_ERROR = 1U << 0,
94 BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1,
95 BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2,
96 BINDER_DEBUG_OPEN_CLOSE = 1U << 3,
97 BINDER_DEBUG_DEAD_BINDER = 1U << 4,
98 BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5,
99 BINDER_DEBUG_READ_WRITE = 1U << 6,
100 BINDER_DEBUG_USER_REFS = 1U << 7,
101 BINDER_DEBUG_THREADS = 1U << 8,
102 BINDER_DEBUG_TRANSACTION = 1U << 9,
103 BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10,
104 BINDER_DEBUG_FREE_BUFFER = 1U << 11,
105 BINDER_DEBUG_INTERNAL_REFS = 1U << 12,
106 BINDER_DEBUG_BUFFER_ALLOC = 1U << 13,
107 BINDER_DEBUG_PRIORITY_CAP = 1U << 14,
108 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15,
109};
110static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR |
111 BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION;
112module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO);
113
Zhengwang Ruan2c523252012-03-07 10:36:57 +0800114static bool binder_debug_no_lock;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900115module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO);
116
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200117static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
118module_param_named(devices, binder_devices_param, charp, S_IRUGO);
119
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900120static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait);
121static int binder_stop_on_user_error;
122
123static int binder_set_stop_on_user_error(const char *val,
124 struct kernel_param *kp)
125{
126 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900127
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900128 ret = param_set_int(val, kp);
129 if (binder_stop_on_user_error < 2)
130 wake_up(&binder_user_error_wait);
131 return ret;
132}
133module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
134 param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO);
135
136#define binder_debug(mask, x...) \
137 do { \
138 if (binder_debug_mask & mask) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400139 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900140 } while (0)
141
142#define binder_user_error(x...) \
143 do { \
144 if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \
Sherwin Soltani258767f2012-06-26 02:00:30 -0400145 pr_info(x); \
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900146 if (binder_stop_on_user_error) \
147 binder_stop_on_user_error = 2; \
148 } while (0)
149
Martijn Coenen00c80372016-07-13 12:06:49 +0200150#define to_flat_binder_object(hdr) \
151 container_of(hdr, struct flat_binder_object, hdr)
152
153#define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr)
154
Martijn Coenen5a6da532016-09-30 14:10:07 +0200155#define to_binder_buffer_object(hdr) \
156 container_of(hdr, struct binder_buffer_object, hdr)
157
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900158enum binder_stat_types {
159 BINDER_STAT_PROC,
160 BINDER_STAT_THREAD,
161 BINDER_STAT_NODE,
162 BINDER_STAT_REF,
163 BINDER_STAT_DEATH,
164 BINDER_STAT_TRANSACTION,
165 BINDER_STAT_TRANSACTION_COMPLETE,
166 BINDER_STAT_COUNT
167};
168
169struct binder_stats {
170 int br[_IOC_NR(BR_FAILED_REPLY) + 1];
Martijn Coenen5a6da532016-09-30 14:10:07 +0200171 int bc[_IOC_NR(BC_REPLY_SG) + 1];
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900172 int obj_created[BINDER_STAT_COUNT];
173 int obj_deleted[BINDER_STAT_COUNT];
174};
175
176static struct binder_stats binder_stats;
177
178static inline void binder_stats_deleted(enum binder_stat_types type)
179{
180 binder_stats.obj_deleted[type]++;
181}
182
183static inline void binder_stats_created(enum binder_stat_types type)
184{
185 binder_stats.obj_created[type]++;
186}
187
188struct binder_transaction_log_entry {
189 int debug_id;
190 int call_type;
191 int from_proc;
192 int from_thread;
193 int target_handle;
194 int to_proc;
195 int to_thread;
196 int to_node;
197 int data_size;
198 int offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200199 const char *context_name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900200};
201struct binder_transaction_log {
202 int next;
203 int full;
204 struct binder_transaction_log_entry entry[32];
205};
206static struct binder_transaction_log binder_transaction_log;
207static struct binder_transaction_log binder_transaction_log_failed;
208
209static struct binder_transaction_log_entry *binder_transaction_log_add(
210 struct binder_transaction_log *log)
211{
212 struct binder_transaction_log_entry *e;
Seunghun Lee10f62862014-05-01 01:30:23 +0900213
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900214 e = &log->entry[log->next];
215 memset(e, 0, sizeof(*e));
216 log->next++;
217 if (log->next == ARRAY_SIZE(log->entry)) {
218 log->next = 0;
219 log->full = 1;
220 }
221 return e;
222}
223
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200224struct binder_context {
225 struct binder_node *binder_context_mgr_node;
226 kuid_t binder_context_mgr_uid;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +0200227 const char *name;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200228};
229
Martijn Coenen6b7c7122016-09-30 16:08:09 +0200230struct binder_device {
231 struct hlist_node hlist;
232 struct miscdevice miscdev;
233 struct binder_context context;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200234};
235
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900236struct binder_work {
237 struct list_head entry;
238 enum {
239 BINDER_WORK_TRANSACTION = 1,
240 BINDER_WORK_TRANSACTION_COMPLETE,
241 BINDER_WORK_NODE,
242 BINDER_WORK_DEAD_BINDER,
243 BINDER_WORK_DEAD_BINDER_AND_CLEAR,
244 BINDER_WORK_CLEAR_DEATH_NOTIFICATION,
245 } type;
246};
247
248struct binder_node {
249 int debug_id;
250 struct binder_work work;
251 union {
252 struct rb_node rb_node;
253 struct hlist_node dead_node;
254 };
255 struct binder_proc *proc;
256 struct hlist_head refs;
257 int internal_strong_refs;
258 int local_weak_refs;
259 int local_strong_refs;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800260 binder_uintptr_t ptr;
261 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900262 unsigned has_strong_ref:1;
263 unsigned pending_strong_ref:1;
264 unsigned has_weak_ref:1;
265 unsigned pending_weak_ref:1;
266 unsigned has_async_transaction:1;
267 unsigned accept_fds:1;
268 unsigned min_priority:8;
269 struct list_head async_todo;
270};
271
272struct binder_ref_death {
273 struct binder_work work;
Arve Hjønnevågda498892014-02-21 14:40:26 -0800274 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900275};
276
277struct binder_ref {
278 /* Lookups needed: */
279 /* node + proc => ref (transaction) */
280 /* desc + proc => ref (transaction, inc/dec ref) */
281 /* node => refs + procs (proc exit) */
282 int debug_id;
283 struct rb_node rb_node_desc;
284 struct rb_node rb_node_node;
285 struct hlist_node node_entry;
286 struct binder_proc *proc;
287 struct binder_node *node;
288 uint32_t desc;
289 int strong;
290 int weak;
291 struct binder_ref_death *death;
292};
293
294struct binder_buffer {
Justin P. Mattock217218f2012-01-12 06:51:31 -0800295 struct list_head entry; /* free and allocated entries by address */
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900296 struct rb_node rb_node; /* free entry by size or allocated entry */
297 /* by address */
298 unsigned free:1;
299 unsigned allow_user_free:1;
300 unsigned async_transaction:1;
301 unsigned debug_id:29;
302
303 struct binder_transaction *transaction;
304
305 struct binder_node *target_node;
306 size_t data_size;
307 size_t offsets_size;
Martijn Coenen59878d72016-09-30 14:05:40 +0200308 size_t extra_buffers_size;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900309 uint8_t data[0];
310};
311
312enum binder_deferred_state {
313 BINDER_DEFERRED_PUT_FILES = 0x01,
314 BINDER_DEFERRED_FLUSH = 0x02,
315 BINDER_DEFERRED_RELEASE = 0x04,
316};
317
318struct binder_proc {
319 struct hlist_node proc_node;
320 struct rb_root threads;
321 struct rb_root nodes;
322 struct rb_root refs_by_desc;
323 struct rb_root refs_by_node;
324 int pid;
325 struct vm_area_struct *vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800326 struct mm_struct *vma_vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900327 struct task_struct *tsk;
328 struct files_struct *files;
329 struct hlist_node deferred_work_node;
330 int deferred_work;
331 void *buffer;
332 ptrdiff_t user_buffer_offset;
333
334 struct list_head buffers;
335 struct rb_root free_buffers;
336 struct rb_root allocated_buffers;
337 size_t free_async_space;
338
339 struct page **pages;
340 size_t buffer_size;
341 uint32_t buffer_free;
342 struct list_head todo;
343 wait_queue_head_t wait;
344 struct binder_stats stats;
345 struct list_head delivered_death;
346 int max_threads;
347 int requested_threads;
348 int requested_threads_started;
349 int ready_threads;
350 long default_priority;
Arve Hjønnevåg16b66552009-04-28 20:57:50 -0700351 struct dentry *debugfs_entry;
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200352 struct binder_context *context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900353};
354
355enum {
356 BINDER_LOOPER_STATE_REGISTERED = 0x01,
357 BINDER_LOOPER_STATE_ENTERED = 0x02,
358 BINDER_LOOPER_STATE_EXITED = 0x04,
359 BINDER_LOOPER_STATE_INVALID = 0x08,
360 BINDER_LOOPER_STATE_WAITING = 0x10,
361 BINDER_LOOPER_STATE_NEED_RETURN = 0x20
362};
363
364struct binder_thread {
365 struct binder_proc *proc;
366 struct rb_node rb_node;
367 int pid;
368 int looper;
369 struct binder_transaction *transaction_stack;
370 struct list_head todo;
371 uint32_t return_error; /* Write failed, return error code in read buf */
372 uint32_t return_error2; /* Write failed, return error code in read */
373 /* buffer. Used when sending a reply to a dead process that */
374 /* we are also waiting on */
375 wait_queue_head_t wait;
376 struct binder_stats stats;
377};
378
379struct binder_transaction {
380 int debug_id;
381 struct binder_work work;
382 struct binder_thread *from;
383 struct binder_transaction *from_parent;
384 struct binder_proc *to_proc;
385 struct binder_thread *to_thread;
386 struct binder_transaction *to_parent;
387 unsigned need_reply:1;
388 /* unsigned is_dead:1; */ /* not used at the moment */
389
390 struct binder_buffer *buffer;
391 unsigned int code;
392 unsigned int flags;
393 long priority;
394 long saved_priority;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -0600395 kuid_t sender_euid;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900396};
397
398static void
399binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer);
400
Sachin Kamatefde99c2012-08-17 16:39:36 +0530401static int task_get_unused_fd_flags(struct binder_proc *proc, int flags)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900402{
403 struct files_struct *files = proc->files;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900404 unsigned long rlim_cur;
405 unsigned long irqs;
406
407 if (files == NULL)
408 return -ESRCH;
409
Al Virodcfadfa2012-08-12 17:27:30 -0400410 if (!lock_task_sighand(proc->tsk, &irqs))
411 return -EMFILE;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900412
Al Virodcfadfa2012-08-12 17:27:30 -0400413 rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE);
414 unlock_task_sighand(proc->tsk, &irqs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900415
Al Virodcfadfa2012-08-12 17:27:30 -0400416 return __alloc_fd(files, 0, rlim_cur, flags);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900417}
418
419/*
420 * copied from fd_install
421 */
422static void task_fd_install(
423 struct binder_proc *proc, unsigned int fd, struct file *file)
424{
Al Virof869e8a2012-08-15 21:06:33 -0400425 if (proc->files)
426 __fd_install(proc->files, fd, file);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900427}
428
429/*
430 * copied from sys_close
431 */
432static long task_close_fd(struct binder_proc *proc, unsigned int fd)
433{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900434 int retval;
435
Al Viro483ce1d2012-08-19 12:04:24 -0400436 if (proc->files == NULL)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900437 return -ESRCH;
438
Al Viro483ce1d2012-08-19 12:04:24 -0400439 retval = __close_fd(proc->files, fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900440 /* can't restart close syscall because file table entry was cleared */
441 if (unlikely(retval == -ERESTARTSYS ||
442 retval == -ERESTARTNOINTR ||
443 retval == -ERESTARTNOHAND ||
444 retval == -ERESTART_RESTARTBLOCK))
445 retval = -EINTR;
446
447 return retval;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900448}
449
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700450static inline void binder_lock(const char *tag)
451{
452 trace_binder_lock(tag);
453 mutex_lock(&binder_main_lock);
454 trace_binder_locked(tag);
455}
456
457static inline void binder_unlock(const char *tag)
458{
459 trace_binder_unlock(tag);
460 mutex_unlock(&binder_main_lock);
461}
462
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900463static void binder_set_nice(long nice)
464{
465 long min_nice;
Seunghun Lee10f62862014-05-01 01:30:23 +0900466
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900467 if (can_nice(current, nice)) {
468 set_user_nice(current, nice);
469 return;
470 }
Dongsheng Yang7aa2c012014-05-08 18:33:49 +0900471 min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900472 binder_debug(BINDER_DEBUG_PRIORITY_CAP,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530473 "%d: nice value %ld not allowed use %ld instead\n",
474 current->pid, nice, min_nice);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900475 set_user_nice(current, min_nice);
Dongsheng Yang8698a742014-03-11 18:09:12 +0800476 if (min_nice <= MAX_NICE)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900477 return;
Anmol Sarma56b468f2012-10-30 22:35:43 +0530478 binder_user_error("%d RLIMIT_NICE not set\n", current->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900479}
480
481static size_t binder_buffer_size(struct binder_proc *proc,
482 struct binder_buffer *buffer)
483{
484 if (list_is_last(&buffer->entry, &proc->buffers))
485 return proc->buffer + proc->buffer_size - (void *)buffer->data;
Karthik Nayak78733112014-06-21 20:23:16 +0530486 return (size_t)list_entry(buffer->entry.next,
487 struct binder_buffer, entry) - (size_t)buffer->data;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900488}
489
490static void binder_insert_free_buffer(struct binder_proc *proc,
491 struct binder_buffer *new_buffer)
492{
493 struct rb_node **p = &proc->free_buffers.rb_node;
494 struct rb_node *parent = NULL;
495 struct binder_buffer *buffer;
496 size_t buffer_size;
497 size_t new_buffer_size;
498
499 BUG_ON(!new_buffer->free);
500
501 new_buffer_size = binder_buffer_size(proc, new_buffer);
502
503 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530504 "%d: add free buffer, size %zd, at %p\n",
505 proc->pid, new_buffer_size, new_buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900506
507 while (*p) {
508 parent = *p;
509 buffer = rb_entry(parent, struct binder_buffer, rb_node);
510 BUG_ON(!buffer->free);
511
512 buffer_size = binder_buffer_size(proc, buffer);
513
514 if (new_buffer_size < buffer_size)
515 p = &parent->rb_left;
516 else
517 p = &parent->rb_right;
518 }
519 rb_link_node(&new_buffer->rb_node, parent, p);
520 rb_insert_color(&new_buffer->rb_node, &proc->free_buffers);
521}
522
523static void binder_insert_allocated_buffer(struct binder_proc *proc,
524 struct binder_buffer *new_buffer)
525{
526 struct rb_node **p = &proc->allocated_buffers.rb_node;
527 struct rb_node *parent = NULL;
528 struct binder_buffer *buffer;
529
530 BUG_ON(new_buffer->free);
531
532 while (*p) {
533 parent = *p;
534 buffer = rb_entry(parent, struct binder_buffer, rb_node);
535 BUG_ON(buffer->free);
536
537 if (new_buffer < buffer)
538 p = &parent->rb_left;
539 else if (new_buffer > buffer)
540 p = &parent->rb_right;
541 else
542 BUG();
543 }
544 rb_link_node(&new_buffer->rb_node, parent, p);
545 rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers);
546}
547
548static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800549 uintptr_t user_ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900550{
551 struct rb_node *n = proc->allocated_buffers.rb_node;
552 struct binder_buffer *buffer;
553 struct binder_buffer *kern_ptr;
554
Arve Hjønnevågda498892014-02-21 14:40:26 -0800555 kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset
556 - offsetof(struct binder_buffer, data));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900557
558 while (n) {
559 buffer = rb_entry(n, struct binder_buffer, rb_node);
560 BUG_ON(buffer->free);
561
562 if (kern_ptr < buffer)
563 n = n->rb_left;
564 else if (kern_ptr > buffer)
565 n = n->rb_right;
566 else
567 return buffer;
568 }
569 return NULL;
570}
571
572static int binder_update_page_range(struct binder_proc *proc, int allocate,
573 void *start, void *end,
574 struct vm_area_struct *vma)
575{
576 void *page_addr;
577 unsigned long user_page_addr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900578 struct page **page;
579 struct mm_struct *mm;
580
581 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530582 "%d: %s pages %p-%p\n", proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900583 allocate ? "allocate" : "free", start, end);
584
585 if (end <= start)
586 return 0;
587
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -0700588 trace_binder_update_page_range(proc, allocate, start, end);
589
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900590 if (vma)
591 mm = NULL;
592 else
593 mm = get_task_mm(proc->tsk);
594
595 if (mm) {
596 down_write(&mm->mmap_sem);
597 vma = proc->vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -0800598 if (vma && mm != proc->vma_vm_mm) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530599 pr_err("%d: vma mm and task mm mismatch\n",
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -0800600 proc->pid);
601 vma = NULL;
602 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900603 }
604
605 if (allocate == 0)
606 goto free_range;
607
608 if (vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530609 pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
610 proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900611 goto err_no_vma;
612 }
613
614 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
615 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +0900616
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900617 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
618
619 BUG_ON(*page);
Arve Hjønnevåg585650d2012-10-16 15:29:55 -0700620 *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900621 if (*page == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530622 pr_err("%d: binder_alloc_buf failed for page at %p\n",
623 proc->pid, page_addr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900624 goto err_alloc_page_failed;
625 }
Andrey Ryabininf4c72c72015-02-27 20:44:21 +0300626 ret = map_kernel_range_noflush((unsigned long)page_addr,
627 PAGE_SIZE, PAGE_KERNEL, page);
628 flush_cache_vmap((unsigned long)page_addr,
629 (unsigned long)page_addr + PAGE_SIZE);
630 if (ret != 1) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530631 pr_err("%d: binder_alloc_buf failed to map page at %p in kernel\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900632 proc->pid, page_addr);
633 goto err_map_kernel_failed;
634 }
635 user_page_addr =
636 (uintptr_t)page_addr + proc->user_buffer_offset;
637 ret = vm_insert_page(vma, user_page_addr, page[0]);
638 if (ret) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530639 pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900640 proc->pid, user_page_addr);
641 goto err_vm_insert_page_failed;
642 }
643 /* vm_insert_page does not seem to increment the refcount */
644 }
645 if (mm) {
646 up_write(&mm->mmap_sem);
647 mmput(mm);
648 }
649 return 0;
650
651free_range:
652 for (page_addr = end - PAGE_SIZE; page_addr >= start;
653 page_addr -= PAGE_SIZE) {
654 page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE];
655 if (vma)
656 zap_page_range(vma, (uintptr_t)page_addr +
657 proc->user_buffer_offset, PAGE_SIZE, NULL);
658err_vm_insert_page_failed:
659 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
660err_map_kernel_failed:
661 __free_page(*page);
662 *page = NULL;
663err_alloc_page_failed:
664 ;
665 }
666err_no_vma:
667 if (mm) {
668 up_write(&mm->mmap_sem);
669 mmput(mm);
670 }
671 return -ENOMEM;
672}
673
674static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc,
675 size_t data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +0200676 size_t offsets_size,
677 size_t extra_buffers_size,
678 int is_async)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900679{
680 struct rb_node *n = proc->free_buffers.rb_node;
681 struct binder_buffer *buffer;
682 size_t buffer_size;
683 struct rb_node *best_fit = NULL;
684 void *has_page_addr;
685 void *end_page_addr;
Martijn Coenen59878d72016-09-30 14:05:40 +0200686 size_t size, data_offsets_size;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900687
688 if (proc->vma == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530689 pr_err("%d: binder_alloc_buf, no vma\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900690 proc->pid);
691 return NULL;
692 }
693
Martijn Coenen59878d72016-09-30 14:05:40 +0200694 data_offsets_size = ALIGN(data_size, sizeof(void *)) +
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900695 ALIGN(offsets_size, sizeof(void *));
696
Martijn Coenen59878d72016-09-30 14:05:40 +0200697 if (data_offsets_size < data_size || data_offsets_size < offsets_size) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530698 binder_user_error("%d: got transaction with invalid size %zd-%zd\n",
699 proc->pid, data_size, offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900700 return NULL;
701 }
Martijn Coenen59878d72016-09-30 14:05:40 +0200702 size = data_offsets_size + ALIGN(extra_buffers_size, sizeof(void *));
703 if (size < data_offsets_size || size < extra_buffers_size) {
704 binder_user_error("%d: got transaction with invalid extra_buffers_size %zd\n",
705 proc->pid, extra_buffers_size);
706 return NULL;
707 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900708 if (is_async &&
709 proc->free_async_space < size + sizeof(struct binder_buffer)) {
710 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530711 "%d: binder_alloc_buf size %zd failed, no async space left\n",
712 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900713 return NULL;
714 }
715
716 while (n) {
717 buffer = rb_entry(n, struct binder_buffer, rb_node);
718 BUG_ON(!buffer->free);
719 buffer_size = binder_buffer_size(proc, buffer);
720
721 if (size < buffer_size) {
722 best_fit = n;
723 n = n->rb_left;
724 } else if (size > buffer_size)
725 n = n->rb_right;
726 else {
727 best_fit = n;
728 break;
729 }
730 }
731 if (best_fit == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530732 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
733 proc->pid, size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900734 return NULL;
735 }
736 if (n == NULL) {
737 buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
738 buffer_size = binder_buffer_size(proc, buffer);
739 }
740
741 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530742 "%d: binder_alloc_buf size %zd got buffer %p size %zd\n",
743 proc->pid, size, buffer, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900744
745 has_page_addr =
746 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
747 if (n == NULL) {
748 if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
749 buffer_size = size; /* no room for other buffers */
750 else
751 buffer_size = size + sizeof(struct binder_buffer);
752 }
753 end_page_addr =
754 (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
755 if (end_page_addr > has_page_addr)
756 end_page_addr = has_page_addr;
757 if (binder_update_page_range(proc, 1,
758 (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL))
759 return NULL;
760
761 rb_erase(best_fit, &proc->free_buffers);
762 buffer->free = 0;
763 binder_insert_allocated_buffer(proc, buffer);
764 if (buffer_size != size) {
765 struct binder_buffer *new_buffer = (void *)buffer->data + size;
Seunghun Lee10f62862014-05-01 01:30:23 +0900766
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900767 list_add(&new_buffer->entry, &buffer->entry);
768 new_buffer->free = 1;
769 binder_insert_free_buffer(proc, new_buffer);
770 }
771 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530772 "%d: binder_alloc_buf size %zd got %p\n",
773 proc->pid, size, buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900774 buffer->data_size = data_size;
775 buffer->offsets_size = offsets_size;
Martijn Coenen59878d72016-09-30 14:05:40 +0200776 buffer->extra_buffers_size = extra_buffers_size;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900777 buffer->async_transaction = is_async;
778 if (is_async) {
779 proc->free_async_space -= size + sizeof(struct binder_buffer);
780 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530781 "%d: binder_alloc_buf size %zd async free %zd\n",
782 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900783 }
784
785 return buffer;
786}
787
788static void *buffer_start_page(struct binder_buffer *buffer)
789{
790 return (void *)((uintptr_t)buffer & PAGE_MASK);
791}
792
793static void *buffer_end_page(struct binder_buffer *buffer)
794{
795 return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
796}
797
798static void binder_delete_free_buffer(struct binder_proc *proc,
799 struct binder_buffer *buffer)
800{
801 struct binder_buffer *prev, *next = NULL;
802 int free_page_end = 1;
803 int free_page_start = 1;
804
805 BUG_ON(proc->buffers.next == &buffer->entry);
806 prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
807 BUG_ON(!prev->free);
808 if (buffer_end_page(prev) == buffer_start_page(buffer)) {
809 free_page_start = 0;
810 if (buffer_end_page(prev) == buffer_end_page(buffer))
811 free_page_end = 0;
812 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530813 "%d: merge free, buffer %p share page with %p\n",
814 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900815 }
816
817 if (!list_is_last(&buffer->entry, &proc->buffers)) {
818 next = list_entry(buffer->entry.next,
819 struct binder_buffer, entry);
820 if (buffer_start_page(next) == buffer_end_page(buffer)) {
821 free_page_end = 0;
822 if (buffer_start_page(next) ==
823 buffer_start_page(buffer))
824 free_page_start = 0;
825 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530826 "%d: merge free, buffer %p share page with %p\n",
827 proc->pid, buffer, prev);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900828 }
829 }
830 list_del(&buffer->entry);
831 if (free_page_start || free_page_end) {
832 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Masanari Iida1dcdbfd2013-06-23 23:47:15 +0900833 "%d: merge free, buffer %p do not share page%s%s with %p or %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900834 proc->pid, buffer, free_page_start ? "" : " end",
835 free_page_end ? "" : " start", prev, next);
836 binder_update_page_range(proc, 0, free_page_start ?
837 buffer_start_page(buffer) : buffer_end_page(buffer),
838 (free_page_end ? buffer_end_page(buffer) :
839 buffer_start_page(buffer)) + PAGE_SIZE, NULL);
840 }
841}
842
843static void binder_free_buf(struct binder_proc *proc,
844 struct binder_buffer *buffer)
845{
846 size_t size, buffer_size;
847
848 buffer_size = binder_buffer_size(proc, buffer);
849
850 size = ALIGN(buffer->data_size, sizeof(void *)) +
Martijn Coenen59878d72016-09-30 14:05:40 +0200851 ALIGN(buffer->offsets_size, sizeof(void *)) +
852 ALIGN(buffer->extra_buffers_size, sizeof(void *));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900853
854 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530855 "%d: binder_free_buf %p size %zd buffer_size %zd\n",
856 proc->pid, buffer, size, buffer_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900857
858 BUG_ON(buffer->free);
859 BUG_ON(size > buffer_size);
860 BUG_ON(buffer->transaction != NULL);
861 BUG_ON((void *)buffer < proc->buffer);
862 BUG_ON((void *)buffer > proc->buffer + proc->buffer_size);
863
864 if (buffer->async_transaction) {
865 proc->free_async_space += size + sizeof(struct binder_buffer);
866
867 binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
Anmol Sarma56b468f2012-10-30 22:35:43 +0530868 "%d: binder_free_buf size %zd async free %zd\n",
869 proc->pid, size, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900870 }
871
872 binder_update_page_range(proc, 0,
873 (void *)PAGE_ALIGN((uintptr_t)buffer->data),
874 (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
875 NULL);
876 rb_erase(&buffer->rb_node, &proc->allocated_buffers);
877 buffer->free = 1;
878 if (!list_is_last(&buffer->entry, &proc->buffers)) {
879 struct binder_buffer *next = list_entry(buffer->entry.next,
880 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900881
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900882 if (next->free) {
883 rb_erase(&next->rb_node, &proc->free_buffers);
884 binder_delete_free_buffer(proc, next);
885 }
886 }
887 if (proc->buffers.next != &buffer->entry) {
888 struct binder_buffer *prev = list_entry(buffer->entry.prev,
889 struct binder_buffer, entry);
Seunghun Lee10f62862014-05-01 01:30:23 +0900890
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900891 if (prev->free) {
892 binder_delete_free_buffer(proc, buffer);
893 rb_erase(&prev->rb_node, &proc->free_buffers);
894 buffer = prev;
895 }
896 }
897 binder_insert_free_buffer(proc, buffer);
898}
899
900static struct binder_node *binder_get_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800901 binder_uintptr_t ptr)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900902{
903 struct rb_node *n = proc->nodes.rb_node;
904 struct binder_node *node;
905
906 while (n) {
907 node = rb_entry(n, struct binder_node, rb_node);
908
909 if (ptr < node->ptr)
910 n = n->rb_left;
911 else if (ptr > node->ptr)
912 n = n->rb_right;
913 else
914 return node;
915 }
916 return NULL;
917}
918
919static struct binder_node *binder_new_node(struct binder_proc *proc,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800920 binder_uintptr_t ptr,
921 binder_uintptr_t cookie)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900922{
923 struct rb_node **p = &proc->nodes.rb_node;
924 struct rb_node *parent = NULL;
925 struct binder_node *node;
926
927 while (*p) {
928 parent = *p;
929 node = rb_entry(parent, struct binder_node, rb_node);
930
931 if (ptr < node->ptr)
932 p = &(*p)->rb_left;
933 else if (ptr > node->ptr)
934 p = &(*p)->rb_right;
935 else
936 return NULL;
937 }
938
939 node = kzalloc(sizeof(*node), GFP_KERNEL);
940 if (node == NULL)
941 return NULL;
942 binder_stats_created(BINDER_STAT_NODE);
943 rb_link_node(&node->rb_node, parent, p);
944 rb_insert_color(&node->rb_node, &proc->nodes);
945 node->debug_id = ++binder_last_id;
946 node->proc = proc;
947 node->ptr = ptr;
948 node->cookie = cookie;
949 node->work.type = BINDER_WORK_NODE;
950 INIT_LIST_HEAD(&node->work.entry);
951 INIT_LIST_HEAD(&node->async_todo);
952 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800953 "%d:%d node %d u%016llx c%016llx created\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900954 proc->pid, current->pid, node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -0800955 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900956 return node;
957}
958
959static int binder_inc_node(struct binder_node *node, int strong, int internal,
960 struct list_head *target_list)
961{
962 if (strong) {
963 if (internal) {
964 if (target_list == NULL &&
965 node->internal_strong_refs == 0 &&
Martijn Coenen0b3311e2016-09-30 15:51:48 +0200966 !(node->proc &&
967 node == node->proc->context->
968 binder_context_mgr_node &&
969 node->has_strong_ref)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530970 pr_err("invalid inc strong node for %d\n",
971 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900972 return -EINVAL;
973 }
974 node->internal_strong_refs++;
975 } else
976 node->local_strong_refs++;
977 if (!node->has_strong_ref && target_list) {
978 list_del_init(&node->work.entry);
979 list_add_tail(&node->work.entry, target_list);
980 }
981 } else {
982 if (!internal)
983 node->local_weak_refs++;
984 if (!node->has_weak_ref && list_empty(&node->work.entry)) {
985 if (target_list == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +0530986 pr_err("invalid inc weak node for %d\n",
987 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +0900988 return -EINVAL;
989 }
990 list_add_tail(&node->work.entry, target_list);
991 }
992 }
993 return 0;
994}
995
996static int binder_dec_node(struct binder_node *node, int strong, int internal)
997{
998 if (strong) {
999 if (internal)
1000 node->internal_strong_refs--;
1001 else
1002 node->local_strong_refs--;
1003 if (node->local_strong_refs || node->internal_strong_refs)
1004 return 0;
1005 } else {
1006 if (!internal)
1007 node->local_weak_refs--;
1008 if (node->local_weak_refs || !hlist_empty(&node->refs))
1009 return 0;
1010 }
1011 if (node->proc && (node->has_strong_ref || node->has_weak_ref)) {
1012 if (list_empty(&node->work.entry)) {
1013 list_add_tail(&node->work.entry, &node->proc->todo);
1014 wake_up_interruptible(&node->proc->wait);
1015 }
1016 } else {
1017 if (hlist_empty(&node->refs) && !node->local_strong_refs &&
1018 !node->local_weak_refs) {
1019 list_del_init(&node->work.entry);
1020 if (node->proc) {
1021 rb_erase(&node->rb_node, &node->proc->nodes);
1022 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301023 "refless node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001024 node->debug_id);
1025 } else {
1026 hlist_del(&node->dead_node);
1027 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301028 "dead node %d deleted\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001029 node->debug_id);
1030 }
1031 kfree(node);
1032 binder_stats_deleted(BINDER_STAT_NODE);
1033 }
1034 }
1035
1036 return 0;
1037}
1038
1039
1040static struct binder_ref *binder_get_ref(struct binder_proc *proc,
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001041 u32 desc, bool need_strong_ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001042{
1043 struct rb_node *n = proc->refs_by_desc.rb_node;
1044 struct binder_ref *ref;
1045
1046 while (n) {
1047 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1048
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001049 if (desc < ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001050 n = n->rb_left;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001051 } else if (desc > ref->desc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001052 n = n->rb_right;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001053 } else if (need_strong_ref && !ref->strong) {
1054 binder_user_error("tried to use weak ref as strong ref\n");
1055 return NULL;
1056 } else {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001057 return ref;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001058 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001059 }
1060 return NULL;
1061}
1062
1063static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc,
1064 struct binder_node *node)
1065{
1066 struct rb_node *n;
1067 struct rb_node **p = &proc->refs_by_node.rb_node;
1068 struct rb_node *parent = NULL;
1069 struct binder_ref *ref, *new_ref;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001070 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001071
1072 while (*p) {
1073 parent = *p;
1074 ref = rb_entry(parent, struct binder_ref, rb_node_node);
1075
1076 if (node < ref->node)
1077 p = &(*p)->rb_left;
1078 else if (node > ref->node)
1079 p = &(*p)->rb_right;
1080 else
1081 return ref;
1082 }
1083 new_ref = kzalloc(sizeof(*ref), GFP_KERNEL);
1084 if (new_ref == NULL)
1085 return NULL;
1086 binder_stats_created(BINDER_STAT_REF);
1087 new_ref->debug_id = ++binder_last_id;
1088 new_ref->proc = proc;
1089 new_ref->node = node;
1090 rb_link_node(&new_ref->rb_node_node, parent, p);
1091 rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node);
1092
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001093 new_ref->desc = (node == context->binder_context_mgr_node) ? 0 : 1;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001094 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
1095 ref = rb_entry(n, struct binder_ref, rb_node_desc);
1096 if (ref->desc > new_ref->desc)
1097 break;
1098 new_ref->desc = ref->desc + 1;
1099 }
1100
1101 p = &proc->refs_by_desc.rb_node;
1102 while (*p) {
1103 parent = *p;
1104 ref = rb_entry(parent, struct binder_ref, rb_node_desc);
1105
1106 if (new_ref->desc < ref->desc)
1107 p = &(*p)->rb_left;
1108 else if (new_ref->desc > ref->desc)
1109 p = &(*p)->rb_right;
1110 else
1111 BUG();
1112 }
1113 rb_link_node(&new_ref->rb_node_desc, parent, p);
1114 rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc);
1115 if (node) {
1116 hlist_add_head(&new_ref->node_entry, &node->refs);
1117
1118 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301119 "%d new ref %d desc %d for node %d\n",
1120 proc->pid, new_ref->debug_id, new_ref->desc,
1121 node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001122 } else {
1123 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301124 "%d new ref %d desc %d for dead node\n",
1125 proc->pid, new_ref->debug_id, new_ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001126 }
1127 return new_ref;
1128}
1129
1130static void binder_delete_ref(struct binder_ref *ref)
1131{
1132 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301133 "%d delete ref %d desc %d for node %d\n",
1134 ref->proc->pid, ref->debug_id, ref->desc,
1135 ref->node->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001136
1137 rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc);
1138 rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node);
1139 if (ref->strong)
1140 binder_dec_node(ref->node, 1, 1);
1141 hlist_del(&ref->node_entry);
1142 binder_dec_node(ref->node, 0, 1);
1143 if (ref->death) {
1144 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301145 "%d delete ref %d desc %d has death notification\n",
1146 ref->proc->pid, ref->debug_id, ref->desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001147 list_del(&ref->death->work.entry);
1148 kfree(ref->death);
1149 binder_stats_deleted(BINDER_STAT_DEATH);
1150 }
1151 kfree(ref);
1152 binder_stats_deleted(BINDER_STAT_REF);
1153}
1154
1155static int binder_inc_ref(struct binder_ref *ref, int strong,
1156 struct list_head *target_list)
1157{
1158 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001159
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001160 if (strong) {
1161 if (ref->strong == 0) {
1162 ret = binder_inc_node(ref->node, 1, 1, target_list);
1163 if (ret)
1164 return ret;
1165 }
1166 ref->strong++;
1167 } else {
1168 if (ref->weak == 0) {
1169 ret = binder_inc_node(ref->node, 0, 1, target_list);
1170 if (ret)
1171 return ret;
1172 }
1173 ref->weak++;
1174 }
1175 return 0;
1176}
1177
1178
1179static int binder_dec_ref(struct binder_ref *ref, int strong)
1180{
1181 if (strong) {
1182 if (ref->strong == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301183 binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001184 ref->proc->pid, ref->debug_id,
1185 ref->desc, ref->strong, ref->weak);
1186 return -EINVAL;
1187 }
1188 ref->strong--;
1189 if (ref->strong == 0) {
1190 int ret;
Seunghun Lee10f62862014-05-01 01:30:23 +09001191
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001192 ret = binder_dec_node(ref->node, strong, 1);
1193 if (ret)
1194 return ret;
1195 }
1196 } else {
1197 if (ref->weak == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301198 binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001199 ref->proc->pid, ref->debug_id,
1200 ref->desc, ref->strong, ref->weak);
1201 return -EINVAL;
1202 }
1203 ref->weak--;
1204 }
1205 if (ref->strong == 0 && ref->weak == 0)
1206 binder_delete_ref(ref);
1207 return 0;
1208}
1209
1210static void binder_pop_transaction(struct binder_thread *target_thread,
1211 struct binder_transaction *t)
1212{
1213 if (target_thread) {
1214 BUG_ON(target_thread->transaction_stack != t);
1215 BUG_ON(target_thread->transaction_stack->from != target_thread);
1216 target_thread->transaction_stack =
1217 target_thread->transaction_stack->from_parent;
1218 t->from = NULL;
1219 }
1220 t->need_reply = 0;
1221 if (t->buffer)
1222 t->buffer->transaction = NULL;
1223 kfree(t);
1224 binder_stats_deleted(BINDER_STAT_TRANSACTION);
1225}
1226
1227static void binder_send_failed_reply(struct binder_transaction *t,
1228 uint32_t error_code)
1229{
1230 struct binder_thread *target_thread;
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001231 struct binder_transaction *next;
Seunghun Lee10f62862014-05-01 01:30:23 +09001232
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001233 BUG_ON(t->flags & TF_ONE_WAY);
1234 while (1) {
1235 target_thread = t->from;
1236 if (target_thread) {
1237 if (target_thread->return_error != BR_OK &&
1238 target_thread->return_error2 == BR_OK) {
1239 target_thread->return_error2 =
1240 target_thread->return_error;
1241 target_thread->return_error = BR_OK;
1242 }
1243 if (target_thread->return_error == BR_OK) {
1244 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301245 "send failed reply for transaction %d to %d:%d\n",
William Panlener0232a422014-09-03 22:44:03 -05001246 t->debug_id,
1247 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001248 target_thread->pid);
1249
1250 binder_pop_transaction(target_thread, t);
1251 target_thread->return_error = error_code;
1252 wake_up_interruptible(&target_thread->wait);
1253 } else {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301254 pr_err("reply failed, target thread, %d:%d, has error code %d already\n",
1255 target_thread->proc->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001256 target_thread->pid,
1257 target_thread->return_error);
1258 }
1259 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001260 }
Lucas Tanured4ec15e2014-07-13 21:31:05 -03001261 next = t->from_parent;
1262
1263 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
1264 "send failed reply for transaction %d, target dead\n",
1265 t->debug_id);
1266
1267 binder_pop_transaction(target_thread, t);
1268 if (next == NULL) {
1269 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1270 "reply failed, no target thread at root\n");
1271 return;
1272 }
1273 t = next;
1274 binder_debug(BINDER_DEBUG_DEAD_BINDER,
1275 "reply failed, no target thread -- retry %d\n",
1276 t->debug_id);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001277 }
1278}
1279
Martijn Coenen00c80372016-07-13 12:06:49 +02001280/**
1281 * binder_validate_object() - checks for a valid metadata object in a buffer.
1282 * @buffer: binder_buffer that we're parsing.
1283 * @offset: offset in the buffer at which to validate an object.
1284 *
1285 * Return: If there's a valid metadata object at @offset in @buffer, the
1286 * size of that object. Otherwise, it returns zero.
1287 */
1288static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
1289{
1290 /* Check if we can read a header first */
1291 struct binder_object_header *hdr;
1292 size_t object_size = 0;
1293
1294 if (offset > buffer->data_size - sizeof(*hdr) ||
1295 buffer->data_size < sizeof(*hdr) ||
1296 !IS_ALIGNED(offset, sizeof(u32)))
1297 return 0;
1298
1299 /* Ok, now see if we can read a complete object. */
1300 hdr = (struct binder_object_header *)(buffer->data + offset);
1301 switch (hdr->type) {
1302 case BINDER_TYPE_BINDER:
1303 case BINDER_TYPE_WEAK_BINDER:
1304 case BINDER_TYPE_HANDLE:
1305 case BINDER_TYPE_WEAK_HANDLE:
1306 object_size = sizeof(struct flat_binder_object);
1307 break;
1308 case BINDER_TYPE_FD:
1309 object_size = sizeof(struct binder_fd_object);
1310 break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001311 case BINDER_TYPE_PTR:
1312 object_size = sizeof(struct binder_buffer_object);
1313 break;
Martijn Coenen00c80372016-07-13 12:06:49 +02001314 default:
1315 return 0;
1316 }
1317 if (offset <= buffer->data_size - object_size &&
1318 buffer->data_size >= object_size)
1319 return object_size;
1320 else
1321 return 0;
1322}
1323
Martijn Coenen5a6da532016-09-30 14:10:07 +02001324/**
1325 * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer.
1326 * @b: binder_buffer containing the object
1327 * @index: index in offset array at which the binder_buffer_object is
1328 * located
1329 * @start: points to the start of the offset array
1330 * @num_valid: the number of valid offsets in the offset array
1331 *
1332 * Return: If @index is within the valid range of the offset array
1333 * described by @start and @num_valid, and if there's a valid
1334 * binder_buffer_object at the offset found in index @index
1335 * of the offset array, that object is returned. Otherwise,
1336 * %NULL is returned.
1337 * Note that the offset found in index @index itself is not
1338 * verified; this function assumes that @num_valid elements
1339 * from @start were previously verified to have valid offsets.
1340 */
1341static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b,
1342 binder_size_t index,
1343 binder_size_t *start,
1344 binder_size_t num_valid)
1345{
1346 struct binder_buffer_object *buffer_obj;
1347 binder_size_t *offp;
1348
1349 if (index >= num_valid)
1350 return NULL;
1351
1352 offp = start + index;
1353 buffer_obj = (struct binder_buffer_object *)(b->data + *offp);
1354 if (buffer_obj->hdr.type != BINDER_TYPE_PTR)
1355 return NULL;
1356
1357 return buffer_obj;
1358}
1359
1360/**
1361 * binder_validate_fixup() - validates pointer/fd fixups happen in order.
1362 * @b: transaction buffer
1363 * @objects_start start of objects buffer
1364 * @buffer: binder_buffer_object in which to fix up
1365 * @offset: start offset in @buffer to fix up
1366 * @last_obj: last binder_buffer_object that we fixed up in
1367 * @last_min_offset: minimum fixup offset in @last_obj
1368 *
1369 * Return: %true if a fixup in buffer @buffer at offset @offset is
1370 * allowed.
1371 *
1372 * For safety reasons, we only allow fixups inside a buffer to happen
1373 * at increasing offsets; additionally, we only allow fixup on the last
1374 * buffer object that was verified, or one of its parents.
1375 *
1376 * Example of what is allowed:
1377 *
1378 * A
1379 * B (parent = A, offset = 0)
1380 * C (parent = A, offset = 16)
1381 * D (parent = C, offset = 0)
1382 * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset)
1383 *
1384 * Examples of what is not allowed:
1385 *
1386 * Decreasing offsets within the same parent:
1387 * A
1388 * C (parent = A, offset = 16)
1389 * B (parent = A, offset = 0) // decreasing offset within A
1390 *
1391 * Referring to a parent that wasn't the last object or any of its parents:
1392 * A
1393 * B (parent = A, offset = 0)
1394 * C (parent = A, offset = 0)
1395 * C (parent = A, offset = 16)
1396 * D (parent = B, offset = 0) // B is not A or any of A's parents
1397 */
1398static bool binder_validate_fixup(struct binder_buffer *b,
1399 binder_size_t *objects_start,
1400 struct binder_buffer_object *buffer,
1401 binder_size_t fixup_offset,
1402 struct binder_buffer_object *last_obj,
1403 binder_size_t last_min_offset)
1404{
1405 if (!last_obj) {
1406 /* Nothing to fix up in */
1407 return false;
1408 }
1409
1410 while (last_obj != buffer) {
1411 /*
1412 * Safe to retrieve the parent of last_obj, since it
1413 * was already previously verified by the driver.
1414 */
1415 if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0)
1416 return false;
1417 last_min_offset = last_obj->parent_offset + sizeof(uintptr_t);
1418 last_obj = (struct binder_buffer_object *)
1419 (b->data + *(objects_start + last_obj->parent));
1420 }
1421 return (fixup_offset >= last_min_offset);
1422}
1423
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001424static void binder_transaction_buffer_release(struct binder_proc *proc,
1425 struct binder_buffer *buffer,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001426 binder_size_t *failed_at)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001427{
Martijn Coenen5a6da532016-09-30 14:10:07 +02001428 binder_size_t *offp, *off_start, *off_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001429 int debug_id = buffer->debug_id;
1430
1431 binder_debug(BINDER_DEBUG_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05301432 "%d buffer release %d, size %zd-%zd, failed at %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001433 proc->pid, buffer->debug_id,
1434 buffer->data_size, buffer->offsets_size, failed_at);
1435
1436 if (buffer->target_node)
1437 binder_dec_node(buffer->target_node, 1, 0);
1438
Martijn Coenen5a6da532016-09-30 14:10:07 +02001439 off_start = (binder_size_t *)(buffer->data +
1440 ALIGN(buffer->data_size, sizeof(void *)));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001441 if (failed_at)
1442 off_end = failed_at;
1443 else
Martijn Coenen5a6da532016-09-30 14:10:07 +02001444 off_end = (void *)off_start + buffer->offsets_size;
1445 for (offp = off_start; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02001446 struct binder_object_header *hdr;
1447 size_t object_size = binder_validate_object(buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09001448
Martijn Coenen00c80372016-07-13 12:06:49 +02001449 if (object_size == 0) {
1450 pr_err("transaction release %d bad object at offset %lld, size %zd\n",
Arve Hjønnevågda498892014-02-21 14:40:26 -08001451 debug_id, (u64)*offp, buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001452 continue;
1453 }
Martijn Coenen00c80372016-07-13 12:06:49 +02001454 hdr = (struct binder_object_header *)(buffer->data + *offp);
1455 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001456 case BINDER_TYPE_BINDER:
1457 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02001458 struct flat_binder_object *fp;
1459 struct binder_node *node;
Seunghun Lee10f62862014-05-01 01:30:23 +09001460
Martijn Coenen00c80372016-07-13 12:06:49 +02001461 fp = to_flat_binder_object(hdr);
1462 node = binder_get_node(proc, fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001463 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08001464 pr_err("transaction release %d bad node %016llx\n",
1465 debug_id, (u64)fp->binder);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001466 break;
1467 }
1468 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001469 " node %d u%016llx\n",
1470 node->debug_id, (u64)node->ptr);
Martijn Coenen00c80372016-07-13 12:06:49 +02001471 binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER,
1472 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001473 } break;
1474 case BINDER_TYPE_HANDLE:
1475 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02001476 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001477 struct binder_ref *ref;
1478
Martijn Coenen00c80372016-07-13 12:06:49 +02001479 fp = to_flat_binder_object(hdr);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001480 ref = binder_get_ref(proc, fp->handle,
Martijn Coenen00c80372016-07-13 12:06:49 +02001481 hdr->type == BINDER_TYPE_HANDLE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001482 if (ref == NULL) {
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001483 pr_err("transaction release %d bad handle %d\n",
Anmol Sarma56b468f2012-10-30 22:35:43 +05301484 debug_id, fp->handle);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001485 break;
1486 }
1487 binder_debug(BINDER_DEBUG_TRANSACTION,
1488 " ref %d desc %d (node %d)\n",
1489 ref->debug_id, ref->desc, ref->node->debug_id);
Martijn Coenen00c80372016-07-13 12:06:49 +02001490 binder_dec_ref(ref, hdr->type == BINDER_TYPE_HANDLE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001491 } break;
1492
Martijn Coenen00c80372016-07-13 12:06:49 +02001493 case BINDER_TYPE_FD: {
1494 struct binder_fd_object *fp = to_binder_fd_object(hdr);
1495
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001496 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen00c80372016-07-13 12:06:49 +02001497 " fd %d\n", fp->fd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001498 if (failed_at)
Martijn Coenen00c80372016-07-13 12:06:49 +02001499 task_close_fd(proc, fp->fd);
1500 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001501 case BINDER_TYPE_PTR:
1502 /*
1503 * Nothing to do here, this will get cleaned up when the
1504 * transaction buffer gets freed
1505 */
1506 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001507 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01001508 pr_err("transaction release %d bad object type %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02001509 debug_id, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001510 break;
1511 }
1512 }
1513}
1514
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001515static int binder_translate_binder(struct flat_binder_object *fp,
1516 struct binder_transaction *t,
1517 struct binder_thread *thread)
1518{
1519 struct binder_node *node;
1520 struct binder_ref *ref;
1521 struct binder_proc *proc = thread->proc;
1522 struct binder_proc *target_proc = t->to_proc;
1523
1524 node = binder_get_node(proc, fp->binder);
1525 if (!node) {
1526 node = binder_new_node(proc, fp->binder, fp->cookie);
1527 if (!node)
1528 return -ENOMEM;
1529
1530 node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK;
1531 node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS);
1532 }
1533 if (fp->cookie != node->cookie) {
1534 binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n",
1535 proc->pid, thread->pid, (u64)fp->binder,
1536 node->debug_id, (u64)fp->cookie,
1537 (u64)node->cookie);
1538 return -EINVAL;
1539 }
1540 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk))
1541 return -EPERM;
1542
1543 ref = binder_get_ref_for_node(target_proc, node);
1544 if (!ref)
1545 return -EINVAL;
1546
1547 if (fp->hdr.type == BINDER_TYPE_BINDER)
1548 fp->hdr.type = BINDER_TYPE_HANDLE;
1549 else
1550 fp->hdr.type = BINDER_TYPE_WEAK_HANDLE;
1551 fp->binder = 0;
1552 fp->handle = ref->desc;
1553 fp->cookie = 0;
1554 binder_inc_ref(ref, fp->hdr.type == BINDER_TYPE_HANDLE, &thread->todo);
1555
1556 trace_binder_transaction_node_to_ref(t, node, ref);
1557 binder_debug(BINDER_DEBUG_TRANSACTION,
1558 " node %d u%016llx -> ref %d desc %d\n",
1559 node->debug_id, (u64)node->ptr,
1560 ref->debug_id, ref->desc);
1561
1562 return 0;
1563}
1564
1565static int binder_translate_handle(struct flat_binder_object *fp,
1566 struct binder_transaction *t,
1567 struct binder_thread *thread)
1568{
1569 struct binder_ref *ref;
1570 struct binder_proc *proc = thread->proc;
1571 struct binder_proc *target_proc = t->to_proc;
1572
1573 ref = binder_get_ref(proc, fp->handle,
1574 fp->hdr.type == BINDER_TYPE_HANDLE);
1575 if (!ref) {
1576 binder_user_error("%d:%d got transaction with invalid handle, %d\n",
1577 proc->pid, thread->pid, fp->handle);
1578 return -EINVAL;
1579 }
1580 if (security_binder_transfer_binder(proc->tsk, target_proc->tsk))
1581 return -EPERM;
1582
1583 if (ref->node->proc == target_proc) {
1584 if (fp->hdr.type == BINDER_TYPE_HANDLE)
1585 fp->hdr.type = BINDER_TYPE_BINDER;
1586 else
1587 fp->hdr.type = BINDER_TYPE_WEAK_BINDER;
1588 fp->binder = ref->node->ptr;
1589 fp->cookie = ref->node->cookie;
1590 binder_inc_node(ref->node, fp->hdr.type == BINDER_TYPE_BINDER,
1591 0, NULL);
1592 trace_binder_transaction_ref_to_node(t, ref);
1593 binder_debug(BINDER_DEBUG_TRANSACTION,
1594 " ref %d desc %d -> node %d u%016llx\n",
1595 ref->debug_id, ref->desc, ref->node->debug_id,
1596 (u64)ref->node->ptr);
1597 } else {
1598 struct binder_ref *new_ref;
1599
1600 new_ref = binder_get_ref_for_node(target_proc, ref->node);
1601 if (!new_ref)
1602 return -EINVAL;
1603
1604 fp->binder = 0;
1605 fp->handle = new_ref->desc;
1606 fp->cookie = 0;
1607 binder_inc_ref(new_ref, fp->hdr.type == BINDER_TYPE_HANDLE,
1608 NULL);
1609 trace_binder_transaction_ref_to_ref(t, ref, new_ref);
1610 binder_debug(BINDER_DEBUG_TRANSACTION,
1611 " ref %d desc %d -> ref %d desc %d (node %d)\n",
1612 ref->debug_id, ref->desc, new_ref->debug_id,
1613 new_ref->desc, ref->node->debug_id);
1614 }
1615 return 0;
1616}
1617
1618static int binder_translate_fd(int fd,
1619 struct binder_transaction *t,
1620 struct binder_thread *thread,
1621 struct binder_transaction *in_reply_to)
1622{
1623 struct binder_proc *proc = thread->proc;
1624 struct binder_proc *target_proc = t->to_proc;
1625 int target_fd;
1626 struct file *file;
1627 int ret;
1628 bool target_allows_fd;
1629
1630 if (in_reply_to)
1631 target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS);
1632 else
1633 target_allows_fd = t->buffer->target_node->accept_fds;
1634 if (!target_allows_fd) {
1635 binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n",
1636 proc->pid, thread->pid,
1637 in_reply_to ? "reply" : "transaction",
1638 fd);
1639 ret = -EPERM;
1640 goto err_fd_not_accepted;
1641 }
1642
1643 file = fget(fd);
1644 if (!file) {
1645 binder_user_error("%d:%d got transaction with invalid fd, %d\n",
1646 proc->pid, thread->pid, fd);
1647 ret = -EBADF;
1648 goto err_fget;
1649 }
1650 ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file);
1651 if (ret < 0) {
1652 ret = -EPERM;
1653 goto err_security;
1654 }
1655
1656 target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC);
1657 if (target_fd < 0) {
1658 ret = -ENOMEM;
1659 goto err_get_unused_fd;
1660 }
1661 task_fd_install(target_proc, target_fd, file);
1662 trace_binder_transaction_fd(t, fd, target_fd);
1663 binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n",
1664 fd, target_fd);
1665
1666 return target_fd;
1667
1668err_get_unused_fd:
1669err_security:
1670 fput(file);
1671err_fget:
1672err_fd_not_accepted:
1673 return ret;
1674}
1675
Martijn Coenen5a6da532016-09-30 14:10:07 +02001676static int binder_fixup_parent(struct binder_transaction *t,
1677 struct binder_thread *thread,
1678 struct binder_buffer_object *bp,
1679 binder_size_t *off_start,
1680 binder_size_t num_valid,
1681 struct binder_buffer_object *last_fixup_obj,
1682 binder_size_t last_fixup_min_off)
1683{
1684 struct binder_buffer_object *parent;
1685 u8 *parent_buffer;
1686 struct binder_buffer *b = t->buffer;
1687 struct binder_proc *proc = thread->proc;
1688 struct binder_proc *target_proc = t->to_proc;
1689
1690 if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT))
1691 return 0;
1692
1693 parent = binder_validate_ptr(b, bp->parent, off_start, num_valid);
1694 if (!parent) {
1695 binder_user_error("%d:%d got transaction with invalid parent offset or type\n",
1696 proc->pid, thread->pid);
1697 return -EINVAL;
1698 }
1699
1700 if (!binder_validate_fixup(b, off_start,
1701 parent, bp->parent_offset,
1702 last_fixup_obj,
1703 last_fixup_min_off)) {
1704 binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n",
1705 proc->pid, thread->pid);
1706 return -EINVAL;
1707 }
1708
1709 if (parent->length < sizeof(binder_uintptr_t) ||
1710 bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) {
1711 /* No space for a pointer here! */
1712 binder_user_error("%d:%d got transaction with invalid parent offset\n",
1713 proc->pid, thread->pid);
1714 return -EINVAL;
1715 }
1716 parent_buffer = (u8 *)(parent->buffer -
1717 target_proc->user_buffer_offset);
1718 *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer;
1719
1720 return 0;
1721}
1722
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001723static void binder_transaction(struct binder_proc *proc,
1724 struct binder_thread *thread,
Martijn Coenen59878d72016-09-30 14:05:40 +02001725 struct binder_transaction_data *tr, int reply,
1726 binder_size_t extra_buffers_size)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001727{
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001728 int ret;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001729 struct binder_transaction *t;
1730 struct binder_work *tcomplete;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001731 binder_size_t *offp, *off_end, *off_start;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001732 binder_size_t off_min;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001733 u8 *sg_bufp, *sg_buf_end;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001734 struct binder_proc *target_proc;
1735 struct binder_thread *target_thread = NULL;
1736 struct binder_node *target_node = NULL;
1737 struct list_head *target_list;
1738 wait_queue_head_t *target_wait;
1739 struct binder_transaction *in_reply_to = NULL;
1740 struct binder_transaction_log_entry *e;
1741 uint32_t return_error;
Martijn Coenen5a6da532016-09-30 14:10:07 +02001742 struct binder_buffer_object *last_fixup_obj = NULL;
1743 binder_size_t last_fixup_min_off = 0;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001744 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001745
1746 e = binder_transaction_log_add(&binder_transaction_log);
1747 e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY);
1748 e->from_proc = proc->pid;
1749 e->from_thread = thread->pid;
1750 e->target_handle = tr->target.handle;
1751 e->data_size = tr->data_size;
1752 e->offsets_size = tr->offsets_size;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02001753 e->context_name = proc->context->name;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001754
1755 if (reply) {
1756 in_reply_to = thread->transaction_stack;
1757 if (in_reply_to == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301758 binder_user_error("%d:%d got reply transaction with no transaction stack\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001759 proc->pid, thread->pid);
1760 return_error = BR_FAILED_REPLY;
1761 goto err_empty_call_stack;
1762 }
1763 binder_set_nice(in_reply_to->saved_priority);
1764 if (in_reply_to->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301765 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 +09001766 proc->pid, thread->pid, in_reply_to->debug_id,
1767 in_reply_to->to_proc ?
1768 in_reply_to->to_proc->pid : 0,
1769 in_reply_to->to_thread ?
1770 in_reply_to->to_thread->pid : 0);
1771 return_error = BR_FAILED_REPLY;
1772 in_reply_to = NULL;
1773 goto err_bad_call_stack;
1774 }
1775 thread->transaction_stack = in_reply_to->to_parent;
1776 target_thread = in_reply_to->from;
1777 if (target_thread == NULL) {
1778 return_error = BR_DEAD_REPLY;
1779 goto err_dead_binder;
1780 }
1781 if (target_thread->transaction_stack != in_reply_to) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301782 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 +09001783 proc->pid, thread->pid,
1784 target_thread->transaction_stack ?
1785 target_thread->transaction_stack->debug_id : 0,
1786 in_reply_to->debug_id);
1787 return_error = BR_FAILED_REPLY;
1788 in_reply_to = NULL;
1789 target_thread = NULL;
1790 goto err_dead_binder;
1791 }
1792 target_proc = target_thread->proc;
1793 } else {
1794 if (tr->target.handle) {
1795 struct binder_ref *ref;
Seunghun Lee10f62862014-05-01 01:30:23 +09001796
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001797 ref = binder_get_ref(proc, tr->target.handle, true);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001798 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301799 binder_user_error("%d:%d got transaction to invalid handle\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001800 proc->pid, thread->pid);
1801 return_error = BR_FAILED_REPLY;
1802 goto err_invalid_target_handle;
1803 }
1804 target_node = ref->node;
1805 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02001806 target_node = context->binder_context_mgr_node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001807 if (target_node == NULL) {
1808 return_error = BR_DEAD_REPLY;
1809 goto err_no_context_mgr_node;
1810 }
1811 }
1812 e->to_node = target_node->debug_id;
1813 target_proc = target_node->proc;
1814 if (target_proc == NULL) {
1815 return_error = BR_DEAD_REPLY;
1816 goto err_dead_binder;
1817 }
Stephen Smalley79af7302015-01-21 10:54:10 -05001818 if (security_binder_transaction(proc->tsk,
1819 target_proc->tsk) < 0) {
1820 return_error = BR_FAILED_REPLY;
1821 goto err_invalid_target_handle;
1822 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001823 if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) {
1824 struct binder_transaction *tmp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001825
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001826 tmp = thread->transaction_stack;
1827 if (tmp->to_thread != thread) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301828 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 +09001829 proc->pid, thread->pid, tmp->debug_id,
1830 tmp->to_proc ? tmp->to_proc->pid : 0,
1831 tmp->to_thread ?
1832 tmp->to_thread->pid : 0);
1833 return_error = BR_FAILED_REPLY;
1834 goto err_bad_call_stack;
1835 }
1836 while (tmp) {
1837 if (tmp->from && tmp->from->proc == target_proc)
1838 target_thread = tmp->from;
1839 tmp = tmp->from_parent;
1840 }
1841 }
1842 }
1843 if (target_thread) {
1844 e->to_thread = target_thread->pid;
1845 target_list = &target_thread->todo;
1846 target_wait = &target_thread->wait;
1847 } else {
1848 target_list = &target_proc->todo;
1849 target_wait = &target_proc->wait;
1850 }
1851 e->to_proc = target_proc->pid;
1852
1853 /* TODO: reuse incoming transaction for reply */
1854 t = kzalloc(sizeof(*t), GFP_KERNEL);
1855 if (t == NULL) {
1856 return_error = BR_FAILED_REPLY;
1857 goto err_alloc_t_failed;
1858 }
1859 binder_stats_created(BINDER_STAT_TRANSACTION);
1860
1861 tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL);
1862 if (tcomplete == NULL) {
1863 return_error = BR_FAILED_REPLY;
1864 goto err_alloc_tcomplete_failed;
1865 }
1866 binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE);
1867
1868 t->debug_id = ++binder_last_id;
1869 e->debug_id = t->debug_id;
1870
1871 if (reply)
1872 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02001873 "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001874 proc->pid, thread->pid, t->debug_id,
1875 target_proc->pid, target_thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001876 (u64)tr->data.ptr.buffer,
1877 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02001878 (u64)tr->data_size, (u64)tr->offsets_size,
1879 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001880 else
1881 binder_debug(BINDER_DEBUG_TRANSACTION,
Martijn Coenen59878d72016-09-30 14:05:40 +02001882 "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001883 proc->pid, thread->pid, t->debug_id,
1884 target_proc->pid, target_node->debug_id,
Arve Hjønnevågda498892014-02-21 14:40:26 -08001885 (u64)tr->data.ptr.buffer,
1886 (u64)tr->data.ptr.offsets,
Martijn Coenen59878d72016-09-30 14:05:40 +02001887 (u64)tr->data_size, (u64)tr->offsets_size,
1888 (u64)extra_buffers_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001889
1890 if (!reply && !(tr->flags & TF_ONE_WAY))
1891 t->from = thread;
1892 else
1893 t->from = NULL;
Tair Rzayev57bab7c2014-05-31 22:43:34 +03001894 t->sender_euid = task_euid(proc->tsk);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001895 t->to_proc = target_proc;
1896 t->to_thread = target_thread;
1897 t->code = tr->code;
1898 t->flags = tr->flags;
1899 t->priority = task_nice(current);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001900
1901 trace_binder_transaction(reply, t, target_node);
1902
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001903 t->buffer = binder_alloc_buf(target_proc, tr->data_size,
Martijn Coenen59878d72016-09-30 14:05:40 +02001904 tr->offsets_size, extra_buffers_size,
1905 !reply && (t->flags & TF_ONE_WAY));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001906 if (t->buffer == NULL) {
1907 return_error = BR_FAILED_REPLY;
1908 goto err_binder_alloc_buf_failed;
1909 }
1910 t->buffer->allow_user_free = 0;
1911 t->buffer->debug_id = t->debug_id;
1912 t->buffer->transaction = t;
1913 t->buffer->target_node = target_node;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07001914 trace_binder_transaction_alloc_buf(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001915 if (target_node)
1916 binder_inc_node(target_node, 1, 0, NULL);
1917
Martijn Coenen5a6da532016-09-30 14:10:07 +02001918 off_start = (binder_size_t *)(t->buffer->data +
1919 ALIGN(tr->data_size, sizeof(void *)));
1920 offp = off_start;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001921
Arve Hjønnevågda498892014-02-21 14:40:26 -08001922 if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t)
1923 tr->data.ptr.buffer, tr->data_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301924 binder_user_error("%d:%d got transaction with invalid data ptr\n",
1925 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001926 return_error = BR_FAILED_REPLY;
1927 goto err_copy_data_failed;
1928 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001929 if (copy_from_user(offp, (const void __user *)(uintptr_t)
1930 tr->data.ptr.offsets, tr->offsets_size)) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05301931 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
1932 proc->pid, thread->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001933 return_error = BR_FAILED_REPLY;
1934 goto err_copy_data_failed;
1935 }
Arve Hjønnevågda498892014-02-21 14:40:26 -08001936 if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) {
1937 binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n",
1938 proc->pid, thread->pid, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001939 return_error = BR_FAILED_REPLY;
1940 goto err_bad_offset;
1941 }
Martijn Coenen5a6da532016-09-30 14:10:07 +02001942 if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) {
1943 binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n",
1944 proc->pid, thread->pid,
1945 extra_buffers_size);
1946 return_error = BR_FAILED_REPLY;
1947 goto err_bad_offset;
1948 }
1949 off_end = (void *)off_start + tr->offsets_size;
1950 sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *)));
1951 sg_buf_end = sg_bufp + extra_buffers_size;
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001952 off_min = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001953 for (; offp < off_end; offp++) {
Martijn Coenen00c80372016-07-13 12:06:49 +02001954 struct binder_object_header *hdr;
1955 size_t object_size = binder_validate_object(t->buffer, *offp);
Seunghun Lee10f62862014-05-01 01:30:23 +09001956
Martijn Coenen00c80372016-07-13 12:06:49 +02001957 if (object_size == 0 || *offp < off_min) {
1958 binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n",
Arve Hjønnevåg212265e2016-02-09 21:05:32 -08001959 proc->pid, thread->pid, (u64)*offp,
1960 (u64)off_min,
Martijn Coenen00c80372016-07-13 12:06:49 +02001961 (u64)t->buffer->data_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001962 return_error = BR_FAILED_REPLY;
1963 goto err_bad_offset;
1964 }
Martijn Coenen00c80372016-07-13 12:06:49 +02001965
1966 hdr = (struct binder_object_header *)(t->buffer->data + *offp);
1967 off_min = *offp + object_size;
1968 switch (hdr->type) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001969 case BINDER_TYPE_BINDER:
1970 case BINDER_TYPE_WEAK_BINDER: {
Martijn Coenen00c80372016-07-13 12:06:49 +02001971 struct flat_binder_object *fp;
Seunghun Lee10f62862014-05-01 01:30:23 +09001972
Martijn Coenen00c80372016-07-13 12:06:49 +02001973 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001974 ret = binder_translate_binder(fp, t, thread);
1975 if (ret < 0) {
Christian Engelmayer7d420432014-05-07 21:44:53 +02001976 return_error = BR_FAILED_REPLY;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001977 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001978 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001979 } break;
1980 case BINDER_TYPE_HANDLE:
1981 case BINDER_TYPE_WEAK_HANDLE: {
Martijn Coenen00c80372016-07-13 12:06:49 +02001982 struct flat_binder_object *fp;
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02001983
Martijn Coenen00c80372016-07-13 12:06:49 +02001984 fp = to_flat_binder_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001985 ret = binder_translate_handle(fp, t, thread);
1986 if (ret < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001987 return_error = BR_FAILED_REPLY;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001988 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001989 }
1990 } break;
1991
1992 case BINDER_TYPE_FD: {
Martijn Coenen00c80372016-07-13 12:06:49 +02001993 struct binder_fd_object *fp = to_binder_fd_object(hdr);
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001994 int target_fd = binder_translate_fd(fp->fd, t, thread,
1995 in_reply_to);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001996
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001997 if (target_fd < 0) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09001998 return_error = BR_FAILED_REPLY;
Martijn Coenend82cb8b2016-09-29 15:38:14 +02001999 goto err_translate_failed;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002000 }
Martijn Coenen00c80372016-07-13 12:06:49 +02002001 fp->pad_binder = 0;
2002 fp->fd = target_fd;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002003 } break;
Martijn Coenen5a6da532016-09-30 14:10:07 +02002004 case BINDER_TYPE_PTR: {
2005 struct binder_buffer_object *bp =
2006 to_binder_buffer_object(hdr);
2007 size_t buf_left = sg_buf_end - sg_bufp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002008
Martijn Coenen5a6da532016-09-30 14:10:07 +02002009 if (bp->length > buf_left) {
2010 binder_user_error("%d:%d got transaction with too large buffer\n",
2011 proc->pid, thread->pid);
2012 return_error = BR_FAILED_REPLY;
2013 goto err_bad_offset;
2014 }
2015 if (copy_from_user(sg_bufp,
2016 (const void __user *)(uintptr_t)
2017 bp->buffer, bp->length)) {
2018 binder_user_error("%d:%d got transaction with invalid offsets ptr\n",
2019 proc->pid, thread->pid);
2020 return_error = BR_FAILED_REPLY;
2021 goto err_copy_data_failed;
2022 }
2023 /* Fixup buffer pointer to target proc address space */
2024 bp->buffer = (uintptr_t)sg_bufp +
2025 target_proc->user_buffer_offset;
2026 sg_bufp += ALIGN(bp->length, sizeof(u64));
2027
2028 ret = binder_fixup_parent(t, thread, bp, off_start,
2029 offp - off_start,
2030 last_fixup_obj,
2031 last_fixup_min_off);
2032 if (ret < 0) {
2033 return_error = BR_FAILED_REPLY;
2034 goto err_translate_failed;
2035 }
2036 last_fixup_obj = bp;
2037 last_fixup_min_off = 0;
2038 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002039 default:
Serban Constantinescu64dcfe62013-07-04 10:54:48 +01002040 binder_user_error("%d:%d got transaction with invalid object type, %x\n",
Martijn Coenen00c80372016-07-13 12:06:49 +02002041 proc->pid, thread->pid, hdr->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002042 return_error = BR_FAILED_REPLY;
2043 goto err_bad_object_type;
2044 }
2045 }
2046 if (reply) {
2047 BUG_ON(t->buffer->async_transaction != 0);
2048 binder_pop_transaction(target_thread, in_reply_to);
2049 } else if (!(t->flags & TF_ONE_WAY)) {
2050 BUG_ON(t->buffer->async_transaction != 0);
2051 t->need_reply = 1;
2052 t->from_parent = thread->transaction_stack;
2053 thread->transaction_stack = t;
2054 } else {
2055 BUG_ON(target_node == NULL);
2056 BUG_ON(t->buffer->async_transaction != 1);
2057 if (target_node->has_async_transaction) {
2058 target_list = &target_node->async_todo;
2059 target_wait = NULL;
2060 } else
2061 target_node->has_async_transaction = 1;
2062 }
2063 t->work.type = BINDER_WORK_TRANSACTION;
2064 list_add_tail(&t->work.entry, target_list);
2065 tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
2066 list_add_tail(&tcomplete->entry, &thread->todo);
2067 if (target_wait)
2068 wake_up_interruptible(target_wait);
2069 return;
2070
Martijn Coenend82cb8b2016-09-29 15:38:14 +02002071err_translate_failed:
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002072err_bad_object_type:
2073err_bad_offset:
2074err_copy_data_failed:
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002075 trace_binder_transaction_failed_buffer_release(t->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002076 binder_transaction_buffer_release(target_proc, t->buffer, offp);
2077 t->buffer->transaction = NULL;
2078 binder_free_buf(target_proc, t->buffer);
2079err_binder_alloc_buf_failed:
2080 kfree(tcomplete);
2081 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2082err_alloc_tcomplete_failed:
2083 kfree(t);
2084 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2085err_alloc_t_failed:
2086err_bad_call_stack:
2087err_empty_call_stack:
2088err_dead_binder:
2089err_invalid_target_handle:
2090err_no_context_mgr_node:
2091 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002092 "%d:%d transaction failed %d, size %lld-%lld\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002093 proc->pid, thread->pid, return_error,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002094 (u64)tr->data_size, (u64)tr->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002095
2096 {
2097 struct binder_transaction_log_entry *fe;
Seunghun Lee10f62862014-05-01 01:30:23 +09002098
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002099 fe = binder_transaction_log_add(&binder_transaction_log_failed);
2100 *fe = *e;
2101 }
2102
2103 BUG_ON(thread->return_error != BR_OK);
2104 if (in_reply_to) {
2105 thread->return_error = BR_TRANSACTION_COMPLETE;
2106 binder_send_failed_reply(in_reply_to, return_error);
2107 } else
2108 thread->return_error = return_error;
2109}
2110
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002111static int binder_thread_write(struct binder_proc *proc,
2112 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002113 binder_uintptr_t binder_buffer, size_t size,
2114 binder_size_t *consumed)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002115{
2116 uint32_t cmd;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002117 struct binder_context *context = proc->context;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002118 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002119 void __user *ptr = buffer + *consumed;
2120 void __user *end = buffer + size;
2121
2122 while (ptr < end && thread->return_error == BR_OK) {
2123 if (get_user(cmd, (uint32_t __user *)ptr))
2124 return -EFAULT;
2125 ptr += sizeof(uint32_t);
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002126 trace_binder_command(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002127 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) {
2128 binder_stats.bc[_IOC_NR(cmd)]++;
2129 proc->stats.bc[_IOC_NR(cmd)]++;
2130 thread->stats.bc[_IOC_NR(cmd)]++;
2131 }
2132 switch (cmd) {
2133 case BC_INCREFS:
2134 case BC_ACQUIRE:
2135 case BC_RELEASE:
2136 case BC_DECREFS: {
2137 uint32_t target;
2138 struct binder_ref *ref;
2139 const char *debug_string;
2140
2141 if (get_user(target, (uint32_t __user *)ptr))
2142 return -EFAULT;
2143 ptr += sizeof(uint32_t);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002144 if (target == 0 && context->binder_context_mgr_node &&
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002145 (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) {
2146 ref = binder_get_ref_for_node(proc,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02002147 context->binder_context_mgr_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002148 if (ref->desc != target) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302149 binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002150 proc->pid, thread->pid,
2151 ref->desc);
2152 }
2153 } else
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002154 ref = binder_get_ref(proc, target,
2155 cmd == BC_ACQUIRE ||
2156 cmd == BC_RELEASE);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002157 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302158 binder_user_error("%d:%d refcount change on invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002159 proc->pid, thread->pid, target);
2160 break;
2161 }
2162 switch (cmd) {
2163 case BC_INCREFS:
2164 debug_string = "IncRefs";
2165 binder_inc_ref(ref, 0, NULL);
2166 break;
2167 case BC_ACQUIRE:
2168 debug_string = "Acquire";
2169 binder_inc_ref(ref, 1, NULL);
2170 break;
2171 case BC_RELEASE:
2172 debug_string = "Release";
2173 binder_dec_ref(ref, 1);
2174 break;
2175 case BC_DECREFS:
2176 default:
2177 debug_string = "DecRefs";
2178 binder_dec_ref(ref, 0);
2179 break;
2180 }
2181 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302182 "%d:%d %s ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002183 proc->pid, thread->pid, debug_string, ref->debug_id,
2184 ref->desc, ref->strong, ref->weak, ref->node->debug_id);
2185 break;
2186 }
2187 case BC_INCREFS_DONE:
2188 case BC_ACQUIRE_DONE: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002189 binder_uintptr_t node_ptr;
2190 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002191 struct binder_node *node;
2192
Arve Hjønnevågda498892014-02-21 14:40:26 -08002193 if (get_user(node_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002194 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002195 ptr += sizeof(binder_uintptr_t);
2196 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002197 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002198 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002199 node = binder_get_node(proc, node_ptr);
2200 if (node == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002201 binder_user_error("%d:%d %s u%016llx no match\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002202 proc->pid, thread->pid,
2203 cmd == BC_INCREFS_DONE ?
2204 "BC_INCREFS_DONE" :
2205 "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002206 (u64)node_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002207 break;
2208 }
2209 if (cookie != node->cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002210 binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002211 proc->pid, thread->pid,
2212 cmd == BC_INCREFS_DONE ?
2213 "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002214 (u64)node_ptr, node->debug_id,
2215 (u64)cookie, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002216 break;
2217 }
2218 if (cmd == BC_ACQUIRE_DONE) {
2219 if (node->pending_strong_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302220 binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002221 proc->pid, thread->pid,
2222 node->debug_id);
2223 break;
2224 }
2225 node->pending_strong_ref = 0;
2226 } else {
2227 if (node->pending_weak_ref == 0) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302228 binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002229 proc->pid, thread->pid,
2230 node->debug_id);
2231 break;
2232 }
2233 node->pending_weak_ref = 0;
2234 }
2235 binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0);
2236 binder_debug(BINDER_DEBUG_USER_REFS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302237 "%d:%d %s node %d ls %d lw %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002238 proc->pid, thread->pid,
2239 cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE",
2240 node->debug_id, node->local_strong_refs, node->local_weak_refs);
2241 break;
2242 }
2243 case BC_ATTEMPT_ACQUIRE:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302244 pr_err("BC_ATTEMPT_ACQUIRE not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002245 return -EINVAL;
2246 case BC_ACQUIRE_RESULT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302247 pr_err("BC_ACQUIRE_RESULT not supported\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002248 return -EINVAL;
2249
2250 case BC_FREE_BUFFER: {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002251 binder_uintptr_t data_ptr;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002252 struct binder_buffer *buffer;
2253
Arve Hjønnevågda498892014-02-21 14:40:26 -08002254 if (get_user(data_ptr, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002255 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002256 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002257
2258 buffer = binder_buffer_lookup(proc, data_ptr);
2259 if (buffer == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002260 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n",
2261 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002262 break;
2263 }
2264 if (!buffer->allow_user_free) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002265 binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n",
2266 proc->pid, thread->pid, (u64)data_ptr);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002267 break;
2268 }
2269 binder_debug(BINDER_DEBUG_FREE_BUFFER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002270 "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n",
2271 proc->pid, thread->pid, (u64)data_ptr,
2272 buffer->debug_id,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002273 buffer->transaction ? "active" : "finished");
2274
2275 if (buffer->transaction) {
2276 buffer->transaction->buffer = NULL;
2277 buffer->transaction = NULL;
2278 }
2279 if (buffer->async_transaction && buffer->target_node) {
2280 BUG_ON(!buffer->target_node->has_async_transaction);
2281 if (list_empty(&buffer->target_node->async_todo))
2282 buffer->target_node->has_async_transaction = 0;
2283 else
2284 list_move_tail(buffer->target_node->async_todo.next, &thread->todo);
2285 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002286 trace_binder_transaction_buffer_release(buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002287 binder_transaction_buffer_release(proc, buffer, NULL);
2288 binder_free_buf(proc, buffer);
2289 break;
2290 }
2291
Martijn Coenen5a6da532016-09-30 14:10:07 +02002292 case BC_TRANSACTION_SG:
2293 case BC_REPLY_SG: {
2294 struct binder_transaction_data_sg tr;
2295
2296 if (copy_from_user(&tr, ptr, sizeof(tr)))
2297 return -EFAULT;
2298 ptr += sizeof(tr);
2299 binder_transaction(proc, thread, &tr.transaction_data,
2300 cmd == BC_REPLY_SG, tr.buffers_size);
2301 break;
2302 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002303 case BC_TRANSACTION:
2304 case BC_REPLY: {
2305 struct binder_transaction_data tr;
2306
2307 if (copy_from_user(&tr, ptr, sizeof(tr)))
2308 return -EFAULT;
2309 ptr += sizeof(tr);
Martijn Coenen59878d72016-09-30 14:05:40 +02002310 binder_transaction(proc, thread, &tr,
2311 cmd == BC_REPLY, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002312 break;
2313 }
2314
2315 case BC_REGISTER_LOOPER:
2316 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302317 "%d:%d BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002318 proc->pid, thread->pid);
2319 if (thread->looper & BINDER_LOOPER_STATE_ENTERED) {
2320 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302321 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002322 proc->pid, thread->pid);
2323 } else if (proc->requested_threads == 0) {
2324 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302325 binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002326 proc->pid, thread->pid);
2327 } else {
2328 proc->requested_threads--;
2329 proc->requested_threads_started++;
2330 }
2331 thread->looper |= BINDER_LOOPER_STATE_REGISTERED;
2332 break;
2333 case BC_ENTER_LOOPER:
2334 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302335 "%d:%d BC_ENTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002336 proc->pid, thread->pid);
2337 if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) {
2338 thread->looper |= BINDER_LOOPER_STATE_INVALID;
Anmol Sarma56b468f2012-10-30 22:35:43 +05302339 binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002340 proc->pid, thread->pid);
2341 }
2342 thread->looper |= BINDER_LOOPER_STATE_ENTERED;
2343 break;
2344 case BC_EXIT_LOOPER:
2345 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302346 "%d:%d BC_EXIT_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002347 proc->pid, thread->pid);
2348 thread->looper |= BINDER_LOOPER_STATE_EXITED;
2349 break;
2350
2351 case BC_REQUEST_DEATH_NOTIFICATION:
2352 case BC_CLEAR_DEATH_NOTIFICATION: {
2353 uint32_t target;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002354 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002355 struct binder_ref *ref;
2356 struct binder_ref_death *death;
2357
2358 if (get_user(target, (uint32_t __user *)ptr))
2359 return -EFAULT;
2360 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002361 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002362 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002363 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg0a3ffab2016-10-24 15:20:29 +02002364 ref = binder_get_ref(proc, target, false);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002365 if (ref == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302366 binder_user_error("%d:%d %s invalid ref %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002367 proc->pid, thread->pid,
2368 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2369 "BC_REQUEST_DEATH_NOTIFICATION" :
2370 "BC_CLEAR_DEATH_NOTIFICATION",
2371 target);
2372 break;
2373 }
2374
2375 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002376 "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002377 proc->pid, thread->pid,
2378 cmd == BC_REQUEST_DEATH_NOTIFICATION ?
2379 "BC_REQUEST_DEATH_NOTIFICATION" :
2380 "BC_CLEAR_DEATH_NOTIFICATION",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002381 (u64)cookie, ref->debug_id, ref->desc,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002382 ref->strong, ref->weak, ref->node->debug_id);
2383
2384 if (cmd == BC_REQUEST_DEATH_NOTIFICATION) {
2385 if (ref->death) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302386 binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002387 proc->pid, thread->pid);
2388 break;
2389 }
2390 death = kzalloc(sizeof(*death), GFP_KERNEL);
2391 if (death == NULL) {
2392 thread->return_error = BR_ERROR;
2393 binder_debug(BINDER_DEBUG_FAILED_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302394 "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002395 proc->pid, thread->pid);
2396 break;
2397 }
2398 binder_stats_created(BINDER_STAT_DEATH);
2399 INIT_LIST_HEAD(&death->work.entry);
2400 death->cookie = cookie;
2401 ref->death = death;
2402 if (ref->node->proc == NULL) {
2403 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
2404 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2405 list_add_tail(&ref->death->work.entry, &thread->todo);
2406 } else {
2407 list_add_tail(&ref->death->work.entry, &proc->todo);
2408 wake_up_interruptible(&proc->wait);
2409 }
2410 }
2411 } else {
2412 if (ref->death == NULL) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302413 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002414 proc->pid, thread->pid);
2415 break;
2416 }
2417 death = ref->death;
2418 if (death->cookie != cookie) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002419 binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002420 proc->pid, thread->pid,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002421 (u64)death->cookie,
2422 (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002423 break;
2424 }
2425 ref->death = NULL;
2426 if (list_empty(&death->work.entry)) {
2427 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2428 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2429 list_add_tail(&death->work.entry, &thread->todo);
2430 } else {
2431 list_add_tail(&death->work.entry, &proc->todo);
2432 wake_up_interruptible(&proc->wait);
2433 }
2434 } else {
2435 BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER);
2436 death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR;
2437 }
2438 }
2439 } break;
2440 case BC_DEAD_BINDER_DONE: {
2441 struct binder_work *w;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002442 binder_uintptr_t cookie;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002443 struct binder_ref_death *death = NULL;
Seunghun Lee10f62862014-05-01 01:30:23 +09002444
Arve Hjønnevågda498892014-02-21 14:40:26 -08002445 if (get_user(cookie, (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002446 return -EFAULT;
2447
Lisa Du7a64cd82016-02-17 09:32:52 +08002448 ptr += sizeof(cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002449 list_for_each_entry(w, &proc->delivered_death, entry) {
2450 struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work);
Seunghun Lee10f62862014-05-01 01:30:23 +09002451
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002452 if (tmp_death->cookie == cookie) {
2453 death = tmp_death;
2454 break;
2455 }
2456 }
2457 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002458 "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n",
2459 proc->pid, thread->pid, (u64)cookie,
2460 death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002461 if (death == NULL) {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002462 binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n",
2463 proc->pid, thread->pid, (u64)cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002464 break;
2465 }
2466
2467 list_del_init(&death->work.entry);
2468 if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) {
2469 death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION;
2470 if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) {
2471 list_add_tail(&death->work.entry, &thread->todo);
2472 } else {
2473 list_add_tail(&death->work.entry, &proc->todo);
2474 wake_up_interruptible(&proc->wait);
2475 }
2476 }
2477 } break;
2478
2479 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302480 pr_err("%d:%d unknown command %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002481 proc->pid, thread->pid, cmd);
2482 return -EINVAL;
2483 }
2484 *consumed = ptr - buffer;
2485 }
2486 return 0;
2487}
2488
Bojan Prtvarfb07ebc2013-09-02 08:18:40 +02002489static void binder_stat_br(struct binder_proc *proc,
2490 struct binder_thread *thread, uint32_t cmd)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002491{
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002492 trace_binder_return(cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002493 if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) {
2494 binder_stats.br[_IOC_NR(cmd)]++;
2495 proc->stats.br[_IOC_NR(cmd)]++;
2496 thread->stats.br[_IOC_NR(cmd)]++;
2497 }
2498}
2499
2500static int binder_has_proc_work(struct binder_proc *proc,
2501 struct binder_thread *thread)
2502{
2503 return !list_empty(&proc->todo) ||
2504 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2505}
2506
2507static int binder_has_thread_work(struct binder_thread *thread)
2508{
2509 return !list_empty(&thread->todo) || thread->return_error != BR_OK ||
2510 (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN);
2511}
2512
2513static int binder_thread_read(struct binder_proc *proc,
2514 struct binder_thread *thread,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002515 binder_uintptr_t binder_buffer, size_t size,
2516 binder_size_t *consumed, int non_block)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002517{
Arve Hjønnevågda498892014-02-21 14:40:26 -08002518 void __user *buffer = (void __user *)(uintptr_t)binder_buffer;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002519 void __user *ptr = buffer + *consumed;
2520 void __user *end = buffer + size;
2521
2522 int ret = 0;
2523 int wait_for_proc_work;
2524
2525 if (*consumed == 0) {
2526 if (put_user(BR_NOOP, (uint32_t __user *)ptr))
2527 return -EFAULT;
2528 ptr += sizeof(uint32_t);
2529 }
2530
2531retry:
2532 wait_for_proc_work = thread->transaction_stack == NULL &&
2533 list_empty(&thread->todo);
2534
2535 if (thread->return_error != BR_OK && ptr < end) {
2536 if (thread->return_error2 != BR_OK) {
2537 if (put_user(thread->return_error2, (uint32_t __user *)ptr))
2538 return -EFAULT;
2539 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002540 binder_stat_br(proc, thread, thread->return_error2);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002541 if (ptr == end)
2542 goto done;
2543 thread->return_error2 = BR_OK;
2544 }
2545 if (put_user(thread->return_error, (uint32_t __user *)ptr))
2546 return -EFAULT;
2547 ptr += sizeof(uint32_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002548 binder_stat_br(proc, thread, thread->return_error);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002549 thread->return_error = BR_OK;
2550 goto done;
2551 }
2552
2553
2554 thread->looper |= BINDER_LOOPER_STATE_WAITING;
2555 if (wait_for_proc_work)
2556 proc->ready_threads++;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002557
2558 binder_unlock(__func__);
2559
2560 trace_binder_wait_for_work(wait_for_proc_work,
2561 !!thread->transaction_stack,
2562 !list_empty(&thread->todo));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002563 if (wait_for_proc_work) {
2564 if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2565 BINDER_LOOPER_STATE_ENTERED))) {
Anmol Sarma56b468f2012-10-30 22:35:43 +05302566 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 +09002567 proc->pid, thread->pid, thread->looper);
2568 wait_event_interruptible(binder_user_error_wait,
2569 binder_stop_on_user_error < 2);
2570 }
2571 binder_set_nice(proc->default_priority);
2572 if (non_block) {
2573 if (!binder_has_proc_work(proc, thread))
2574 ret = -EAGAIN;
2575 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002576 ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002577 } else {
2578 if (non_block) {
2579 if (!binder_has_thread_work(thread))
2580 ret = -EAGAIN;
2581 } else
Colin Crosse2610b22013-05-06 23:50:15 +00002582 ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002583 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002584
2585 binder_lock(__func__);
2586
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002587 if (wait_for_proc_work)
2588 proc->ready_threads--;
2589 thread->looper &= ~BINDER_LOOPER_STATE_WAITING;
2590
2591 if (ret)
2592 return ret;
2593
2594 while (1) {
2595 uint32_t cmd;
2596 struct binder_transaction_data tr;
2597 struct binder_work *w;
2598 struct binder_transaction *t = NULL;
2599
Dmitry Voytik395262a2014-09-08 18:16:34 +04002600 if (!list_empty(&thread->todo)) {
2601 w = list_first_entry(&thread->todo, struct binder_work,
2602 entry);
2603 } else if (!list_empty(&proc->todo) && wait_for_proc_work) {
2604 w = list_first_entry(&proc->todo, struct binder_work,
2605 entry);
2606 } else {
2607 /* no data added */
2608 if (ptr - buffer == 4 &&
2609 !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002610 goto retry;
2611 break;
2612 }
2613
2614 if (end - ptr < sizeof(tr) + 4)
2615 break;
2616
2617 switch (w->type) {
2618 case BINDER_WORK_TRANSACTION: {
2619 t = container_of(w, struct binder_transaction, work);
2620 } break;
2621 case BINDER_WORK_TRANSACTION_COMPLETE: {
2622 cmd = BR_TRANSACTION_COMPLETE;
2623 if (put_user(cmd, (uint32_t __user *)ptr))
2624 return -EFAULT;
2625 ptr += sizeof(uint32_t);
2626
2627 binder_stat_br(proc, thread, cmd);
2628 binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302629 "%d:%d BR_TRANSACTION_COMPLETE\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002630 proc->pid, thread->pid);
2631
2632 list_del(&w->entry);
2633 kfree(w);
2634 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2635 } break;
2636 case BINDER_WORK_NODE: {
2637 struct binder_node *node = container_of(w, struct binder_node, work);
2638 uint32_t cmd = BR_NOOP;
2639 const char *cmd_name;
2640 int strong = node->internal_strong_refs || node->local_strong_refs;
2641 int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong;
Seunghun Lee10f62862014-05-01 01:30:23 +09002642
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002643 if (weak && !node->has_weak_ref) {
2644 cmd = BR_INCREFS;
2645 cmd_name = "BR_INCREFS";
2646 node->has_weak_ref = 1;
2647 node->pending_weak_ref = 1;
2648 node->local_weak_refs++;
2649 } else if (strong && !node->has_strong_ref) {
2650 cmd = BR_ACQUIRE;
2651 cmd_name = "BR_ACQUIRE";
2652 node->has_strong_ref = 1;
2653 node->pending_strong_ref = 1;
2654 node->local_strong_refs++;
2655 } else if (!strong && node->has_strong_ref) {
2656 cmd = BR_RELEASE;
2657 cmd_name = "BR_RELEASE";
2658 node->has_strong_ref = 0;
2659 } else if (!weak && node->has_weak_ref) {
2660 cmd = BR_DECREFS;
2661 cmd_name = "BR_DECREFS";
2662 node->has_weak_ref = 0;
2663 }
2664 if (cmd != BR_NOOP) {
2665 if (put_user(cmd, (uint32_t __user *)ptr))
2666 return -EFAULT;
2667 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002668 if (put_user(node->ptr,
2669 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002670 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002671 ptr += sizeof(binder_uintptr_t);
2672 if (put_user(node->cookie,
2673 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002674 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002675 ptr += sizeof(binder_uintptr_t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002676
2677 binder_stat_br(proc, thread, cmd);
2678 binder_debug(BINDER_DEBUG_USER_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002679 "%d:%d %s %d u%016llx c%016llx\n",
2680 proc->pid, thread->pid, cmd_name,
2681 node->debug_id,
2682 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002683 } else {
2684 list_del_init(&w->entry);
2685 if (!weak && !strong) {
2686 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002687 "%d:%d node %d u%016llx c%016llx deleted\n",
2688 proc->pid, thread->pid,
2689 node->debug_id,
2690 (u64)node->ptr,
2691 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002692 rb_erase(&node->rb_node, &proc->nodes);
2693 kfree(node);
2694 binder_stats_deleted(BINDER_STAT_NODE);
2695 } else {
2696 binder_debug(BINDER_DEBUG_INTERNAL_REFS,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002697 "%d:%d node %d u%016llx c%016llx state unchanged\n",
2698 proc->pid, thread->pid,
2699 node->debug_id,
2700 (u64)node->ptr,
2701 (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002702 }
2703 }
2704 } break;
2705 case BINDER_WORK_DEAD_BINDER:
2706 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2707 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2708 struct binder_ref_death *death;
2709 uint32_t cmd;
2710
2711 death = container_of(w, struct binder_ref_death, work);
2712 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION)
2713 cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE;
2714 else
2715 cmd = BR_DEAD_BINDER;
2716 if (put_user(cmd, (uint32_t __user *)ptr))
2717 return -EFAULT;
2718 ptr += sizeof(uint32_t);
Arve Hjønnevågda498892014-02-21 14:40:26 -08002719 if (put_user(death->cookie,
2720 (binder_uintptr_t __user *)ptr))
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002721 return -EFAULT;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002722 ptr += sizeof(binder_uintptr_t);
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002723 binder_stat_br(proc, thread, cmd);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002724 binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002725 "%d:%d %s %016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002726 proc->pid, thread->pid,
2727 cmd == BR_DEAD_BINDER ?
2728 "BR_DEAD_BINDER" :
2729 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
Arve Hjønnevågda498892014-02-21 14:40:26 -08002730 (u64)death->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002731
2732 if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) {
2733 list_del(&w->entry);
2734 kfree(death);
2735 binder_stats_deleted(BINDER_STAT_DEATH);
2736 } else
2737 list_move(&w->entry, &proc->delivered_death);
2738 if (cmd == BR_DEAD_BINDER)
2739 goto done; /* DEAD_BINDER notifications can cause transactions */
2740 } break;
2741 }
2742
2743 if (!t)
2744 continue;
2745
2746 BUG_ON(t->buffer == NULL);
2747 if (t->buffer->target_node) {
2748 struct binder_node *target_node = t->buffer->target_node;
Seunghun Lee10f62862014-05-01 01:30:23 +09002749
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002750 tr.target.ptr = target_node->ptr;
2751 tr.cookie = target_node->cookie;
2752 t->saved_priority = task_nice(current);
2753 if (t->priority < target_node->min_priority &&
2754 !(t->flags & TF_ONE_WAY))
2755 binder_set_nice(t->priority);
2756 else if (!(t->flags & TF_ONE_WAY) ||
2757 t->saved_priority > target_node->min_priority)
2758 binder_set_nice(target_node->min_priority);
2759 cmd = BR_TRANSACTION;
2760 } else {
Arve Hjønnevågda498892014-02-21 14:40:26 -08002761 tr.target.ptr = 0;
2762 tr.cookie = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002763 cmd = BR_REPLY;
2764 }
2765 tr.code = t->code;
2766 tr.flags = t->flags;
Eric W. Biederman4a2ebb92012-05-25 18:34:53 -06002767 tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002768
2769 if (t->from) {
2770 struct task_struct *sender = t->from->proc->tsk;
Seunghun Lee10f62862014-05-01 01:30:23 +09002771
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002772 tr.sender_pid = task_tgid_nr_ns(sender,
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002773 task_active_pid_ns(current));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002774 } else {
2775 tr.sender_pid = 0;
2776 }
2777
2778 tr.data_size = t->buffer->data_size;
2779 tr.offsets_size = t->buffer->offsets_size;
Arve Hjønnevågda498892014-02-21 14:40:26 -08002780 tr.data.ptr.buffer = (binder_uintptr_t)(
2781 (uintptr_t)t->buffer->data +
2782 proc->user_buffer_offset);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002783 tr.data.ptr.offsets = tr.data.ptr.buffer +
2784 ALIGN(t->buffer->data_size,
2785 sizeof(void *));
2786
2787 if (put_user(cmd, (uint32_t __user *)ptr))
2788 return -EFAULT;
2789 ptr += sizeof(uint32_t);
2790 if (copy_to_user(ptr, &tr, sizeof(tr)))
2791 return -EFAULT;
2792 ptr += sizeof(tr);
2793
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002794 trace_binder_transaction_received(t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002795 binder_stat_br(proc, thread, cmd);
2796 binder_debug(BINDER_DEBUG_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002797 "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002798 proc->pid, thread->pid,
2799 (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" :
2800 "BR_REPLY",
2801 t->debug_id, t->from ? t->from->proc->pid : 0,
2802 t->from ? t->from->pid : 0, cmd,
2803 t->buffer->data_size, t->buffer->offsets_size,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002804 (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002805
2806 list_del(&t->work.entry);
2807 t->buffer->allow_user_free = 1;
2808 if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) {
2809 t->to_parent = thread->transaction_stack;
2810 t->to_thread = thread;
2811 thread->transaction_stack = t;
2812 } else {
2813 t->buffer->transaction = NULL;
2814 kfree(t);
2815 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2816 }
2817 break;
2818 }
2819
2820done:
2821
2822 *consumed = ptr - buffer;
2823 if (proc->requested_threads + proc->ready_threads == 0 &&
2824 proc->requested_threads_started < proc->max_threads &&
2825 (thread->looper & (BINDER_LOOPER_STATE_REGISTERED |
2826 BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */
2827 /*spawn a new thread if we leave this out */) {
2828 proc->requested_threads++;
2829 binder_debug(BINDER_DEBUG_THREADS,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302830 "%d:%d BR_SPAWN_LOOPER\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002831 proc->pid, thread->pid);
2832 if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer))
2833 return -EFAULT;
Arve Hjønnevåg89334ab2012-10-16 15:29:52 -07002834 binder_stat_br(proc, thread, BR_SPAWN_LOOPER);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002835 }
2836 return 0;
2837}
2838
2839static void binder_release_work(struct list_head *list)
2840{
2841 struct binder_work *w;
Seunghun Lee10f62862014-05-01 01:30:23 +09002842
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002843 while (!list_empty(list)) {
2844 w = list_first_entry(list, struct binder_work, entry);
2845 list_del_init(&w->entry);
2846 switch (w->type) {
2847 case BINDER_WORK_TRANSACTION: {
2848 struct binder_transaction *t;
2849
2850 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002851 if (t->buffer->target_node &&
2852 !(t->flags & TF_ONE_WAY)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002853 binder_send_failed_reply(t, BR_DEAD_REPLY);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002854 } else {
2855 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302856 "undelivered transaction %d\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002857 t->debug_id);
2858 t->buffer->transaction = NULL;
2859 kfree(t);
2860 binder_stats_deleted(BINDER_STAT_TRANSACTION);
2861 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002862 } break;
2863 case BINDER_WORK_TRANSACTION_COMPLETE: {
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002864 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302865 "undelivered TRANSACTION_COMPLETE\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002866 kfree(w);
2867 binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE);
2868 } break;
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002869 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
2870 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: {
2871 struct binder_ref_death *death;
2872
2873 death = container_of(w, struct binder_ref_death, work);
2874 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Arve Hjønnevågda498892014-02-21 14:40:26 -08002875 "undelivered death notification, %016llx\n",
2876 (u64)death->cookie);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002877 kfree(death);
2878 binder_stats_deleted(BINDER_STAT_DEATH);
2879 } break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002880 default:
Anmol Sarma56b468f2012-10-30 22:35:43 +05302881 pr_err("unexpected work type, %d, not freed\n",
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07002882 w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002883 break;
2884 }
2885 }
2886
2887}
2888
2889static struct binder_thread *binder_get_thread(struct binder_proc *proc)
2890{
2891 struct binder_thread *thread = NULL;
2892 struct rb_node *parent = NULL;
2893 struct rb_node **p = &proc->threads.rb_node;
2894
2895 while (*p) {
2896 parent = *p;
2897 thread = rb_entry(parent, struct binder_thread, rb_node);
2898
2899 if (current->pid < thread->pid)
2900 p = &(*p)->rb_left;
2901 else if (current->pid > thread->pid)
2902 p = &(*p)->rb_right;
2903 else
2904 break;
2905 }
2906 if (*p == NULL) {
2907 thread = kzalloc(sizeof(*thread), GFP_KERNEL);
2908 if (thread == NULL)
2909 return NULL;
2910 binder_stats_created(BINDER_STAT_THREAD);
2911 thread->proc = proc;
2912 thread->pid = current->pid;
2913 init_waitqueue_head(&thread->wait);
2914 INIT_LIST_HEAD(&thread->todo);
2915 rb_link_node(&thread->rb_node, parent, p);
2916 rb_insert_color(&thread->rb_node, &proc->threads);
2917 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
2918 thread->return_error = BR_OK;
2919 thread->return_error2 = BR_OK;
2920 }
2921 return thread;
2922}
2923
2924static int binder_free_thread(struct binder_proc *proc,
2925 struct binder_thread *thread)
2926{
2927 struct binder_transaction *t;
2928 struct binder_transaction *send_reply = NULL;
2929 int active_transactions = 0;
2930
2931 rb_erase(&thread->rb_node, &proc->threads);
2932 t = thread->transaction_stack;
2933 if (t && t->to_thread == thread)
2934 send_reply = t;
2935 while (t) {
2936 active_transactions++;
2937 binder_debug(BINDER_DEBUG_DEAD_TRANSACTION,
Anmol Sarma56b468f2012-10-30 22:35:43 +05302938 "release %d:%d transaction %d %s, still active\n",
2939 proc->pid, thread->pid,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002940 t->debug_id,
2941 (t->to_thread == thread) ? "in" : "out");
2942
2943 if (t->to_thread == thread) {
2944 t->to_proc = NULL;
2945 t->to_thread = NULL;
2946 if (t->buffer) {
2947 t->buffer->transaction = NULL;
2948 t->buffer = NULL;
2949 }
2950 t = t->to_parent;
2951 } else if (t->from == thread) {
2952 t->from = NULL;
2953 t = t->from_parent;
2954 } else
2955 BUG();
2956 }
2957 if (send_reply)
2958 binder_send_failed_reply(send_reply, BR_DEAD_REPLY);
2959 binder_release_work(&thread->todo);
2960 kfree(thread);
2961 binder_stats_deleted(BINDER_STAT_THREAD);
2962 return active_transactions;
2963}
2964
2965static unsigned int binder_poll(struct file *filp,
2966 struct poll_table_struct *wait)
2967{
2968 struct binder_proc *proc = filp->private_data;
2969 struct binder_thread *thread = NULL;
2970 int wait_for_proc_work;
2971
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002972 binder_lock(__func__);
2973
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002974 thread = binder_get_thread(proc);
2975
2976 wait_for_proc_work = thread->transaction_stack == NULL &&
2977 list_empty(&thread->todo) && thread->return_error == BR_OK;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07002978
2979 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09002980
2981 if (wait_for_proc_work) {
2982 if (binder_has_proc_work(proc, thread))
2983 return POLLIN;
2984 poll_wait(filp, &proc->wait, wait);
2985 if (binder_has_proc_work(proc, thread))
2986 return POLLIN;
2987 } else {
2988 if (binder_has_thread_work(thread))
2989 return POLLIN;
2990 poll_wait(filp, &thread->wait, wait);
2991 if (binder_has_thread_work(thread))
2992 return POLLIN;
2993 }
2994 return 0;
2995}
2996
Tair Rzayev78260ac2014-06-03 22:27:21 +03002997static int binder_ioctl_write_read(struct file *filp,
2998 unsigned int cmd, unsigned long arg,
2999 struct binder_thread *thread)
3000{
3001 int ret = 0;
3002 struct binder_proc *proc = filp->private_data;
3003 unsigned int size = _IOC_SIZE(cmd);
3004 void __user *ubuf = (void __user *)arg;
3005 struct binder_write_read bwr;
3006
3007 if (size != sizeof(struct binder_write_read)) {
3008 ret = -EINVAL;
3009 goto out;
3010 }
3011 if (copy_from_user(&bwr, ubuf, sizeof(bwr))) {
3012 ret = -EFAULT;
3013 goto out;
3014 }
3015 binder_debug(BINDER_DEBUG_READ_WRITE,
3016 "%d:%d write %lld at %016llx, read %lld at %016llx\n",
3017 proc->pid, thread->pid,
3018 (u64)bwr.write_size, (u64)bwr.write_buffer,
3019 (u64)bwr.read_size, (u64)bwr.read_buffer);
3020
3021 if (bwr.write_size > 0) {
3022 ret = binder_thread_write(proc, thread,
3023 bwr.write_buffer,
3024 bwr.write_size,
3025 &bwr.write_consumed);
3026 trace_binder_write_done(ret);
3027 if (ret < 0) {
3028 bwr.read_consumed = 0;
3029 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
3030 ret = -EFAULT;
3031 goto out;
3032 }
3033 }
3034 if (bwr.read_size > 0) {
3035 ret = binder_thread_read(proc, thread, bwr.read_buffer,
3036 bwr.read_size,
3037 &bwr.read_consumed,
3038 filp->f_flags & O_NONBLOCK);
3039 trace_binder_read_done(ret);
3040 if (!list_empty(&proc->todo))
3041 wake_up_interruptible(&proc->wait);
3042 if (ret < 0) {
3043 if (copy_to_user(ubuf, &bwr, sizeof(bwr)))
3044 ret = -EFAULT;
3045 goto out;
3046 }
3047 }
3048 binder_debug(BINDER_DEBUG_READ_WRITE,
3049 "%d:%d wrote %lld of %lld, read return %lld of %lld\n",
3050 proc->pid, thread->pid,
3051 (u64)bwr.write_consumed, (u64)bwr.write_size,
3052 (u64)bwr.read_consumed, (u64)bwr.read_size);
3053 if (copy_to_user(ubuf, &bwr, sizeof(bwr))) {
3054 ret = -EFAULT;
3055 goto out;
3056 }
3057out:
3058 return ret;
3059}
3060
3061static int binder_ioctl_set_ctx_mgr(struct file *filp)
3062{
3063 int ret = 0;
3064 struct binder_proc *proc = filp->private_data;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003065 struct binder_context *context = proc->context;
3066
Tair Rzayev78260ac2014-06-03 22:27:21 +03003067 kuid_t curr_euid = current_euid();
3068
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003069 if (context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003070 pr_err("BINDER_SET_CONTEXT_MGR already set\n");
3071 ret = -EBUSY;
3072 goto out;
3073 }
Stephen Smalley79af7302015-01-21 10:54:10 -05003074 ret = security_binder_set_context_mgr(proc->tsk);
3075 if (ret < 0)
3076 goto out;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003077 if (uid_valid(context->binder_context_mgr_uid)) {
3078 if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003079 pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n",
3080 from_kuid(&init_user_ns, curr_euid),
3081 from_kuid(&init_user_ns,
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003082 context->binder_context_mgr_uid));
Tair Rzayev78260ac2014-06-03 22:27:21 +03003083 ret = -EPERM;
3084 goto out;
3085 }
3086 } else {
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003087 context->binder_context_mgr_uid = curr_euid;
Tair Rzayev78260ac2014-06-03 22:27:21 +03003088 }
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003089 context->binder_context_mgr_node = binder_new_node(proc, 0, 0);
3090 if (!context->binder_context_mgr_node) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003091 ret = -ENOMEM;
3092 goto out;
3093 }
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003094 context->binder_context_mgr_node->local_weak_refs++;
3095 context->binder_context_mgr_node->local_strong_refs++;
3096 context->binder_context_mgr_node->has_strong_ref = 1;
3097 context->binder_context_mgr_node->has_weak_ref = 1;
Tair Rzayev78260ac2014-06-03 22:27:21 +03003098out:
3099 return ret;
3100}
3101
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003102static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3103{
3104 int ret;
3105 struct binder_proc *proc = filp->private_data;
3106 struct binder_thread *thread;
3107 unsigned int size = _IOC_SIZE(cmd);
3108 void __user *ubuf = (void __user *)arg;
3109
Tair Rzayev78260ac2014-06-03 22:27:21 +03003110 /*pr_info("binder_ioctl: %d:%d %x %lx\n",
3111 proc->pid, current->pid, cmd, arg);*/
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003112
Chen Fenga906d692016-02-01 14:04:02 +08003113 if (unlikely(current->mm != proc->vma_vm_mm)) {
3114 pr_err("current mm mismatch proc mm\n");
3115 return -EINVAL;
3116 }
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003117 trace_binder_ioctl(cmd, arg);
3118
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003119 ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
3120 if (ret)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003121 goto err_unlocked;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003122
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003123 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003124 thread = binder_get_thread(proc);
3125 if (thread == NULL) {
3126 ret = -ENOMEM;
3127 goto err;
3128 }
3129
3130 switch (cmd) {
Tair Rzayev78260ac2014-06-03 22:27:21 +03003131 case BINDER_WRITE_READ:
3132 ret = binder_ioctl_write_read(filp, cmd, arg, thread);
3133 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003134 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003135 break;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003136 case BINDER_SET_MAX_THREADS:
3137 if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) {
3138 ret = -EINVAL;
3139 goto err;
3140 }
3141 break;
3142 case BINDER_SET_CONTEXT_MGR:
Tair Rzayev78260ac2014-06-03 22:27:21 +03003143 ret = binder_ioctl_set_ctx_mgr(filp);
3144 if (ret)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003145 goto err;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003146 break;
3147 case BINDER_THREAD_EXIT:
Anmol Sarma56b468f2012-10-30 22:35:43 +05303148 binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003149 proc->pid, thread->pid);
3150 binder_free_thread(proc, thread);
3151 thread = NULL;
3152 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02003153 case BINDER_VERSION: {
3154 struct binder_version __user *ver = ubuf;
3155
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003156 if (size != sizeof(struct binder_version)) {
3157 ret = -EINVAL;
3158 goto err;
3159 }
Mathieu Maret36c89c02014-04-15 12:03:05 +02003160 if (put_user(BINDER_CURRENT_PROTOCOL_VERSION,
3161 &ver->protocol_version)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003162 ret = -EINVAL;
3163 goto err;
3164 }
3165 break;
Mathieu Maret36c89c02014-04-15 12:03:05 +02003166 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003167 default:
3168 ret = -EINVAL;
3169 goto err;
3170 }
3171 ret = 0;
3172err:
3173 if (thread)
3174 thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003175 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003176 wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2);
3177 if (ret && ret != -ERESTARTSYS)
Anmol Sarma56b468f2012-10-30 22:35:43 +05303178 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 -07003179err_unlocked:
3180 trace_binder_ioctl_done(ret);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003181 return ret;
3182}
3183
3184static void binder_vma_open(struct vm_area_struct *vma)
3185{
3186 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003187
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003188 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303189 "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003190 proc->pid, vma->vm_start, vma->vm_end,
3191 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3192 (unsigned long)pgprot_val(vma->vm_page_prot));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003193}
3194
3195static void binder_vma_close(struct vm_area_struct *vma)
3196{
3197 struct binder_proc *proc = vma->vm_private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003198
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003199 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Anmol Sarma56b468f2012-10-30 22:35:43 +05303200 "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003201 proc->pid, vma->vm_start, vma->vm_end,
3202 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3203 (unsigned long)pgprot_val(vma->vm_page_prot));
3204 proc->vma = NULL;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08003205 proc->vma_vm_mm = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003206 binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES);
3207}
3208
Vinayak Menonddac7d52014-06-02 18:17:59 +05303209static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3210{
3211 return VM_FAULT_SIGBUS;
3212}
3213
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07003214static const struct vm_operations_struct binder_vm_ops = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003215 .open = binder_vma_open,
3216 .close = binder_vma_close,
Vinayak Menonddac7d52014-06-02 18:17:59 +05303217 .fault = binder_vm_fault,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003218};
3219
3220static int binder_mmap(struct file *filp, struct vm_area_struct *vma)
3221{
3222 int ret;
3223 struct vm_struct *area;
3224 struct binder_proc *proc = filp->private_data;
3225 const char *failure_string;
3226 struct binder_buffer *buffer;
3227
Al Viroa79f41e2012-08-15 18:23:36 -04003228 if (proc->tsk != current)
3229 return -EINVAL;
3230
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003231 if ((vma->vm_end - vma->vm_start) > SZ_4M)
3232 vma->vm_end = vma->vm_start + SZ_4M;
3233
3234 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3235 "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n",
3236 proc->pid, vma->vm_start, vma->vm_end,
3237 (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags,
3238 (unsigned long)pgprot_val(vma->vm_page_prot));
3239
3240 if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) {
3241 ret = -EPERM;
3242 failure_string = "bad vm_flags";
3243 goto err_bad_arg;
3244 }
3245 vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE;
3246
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08003247 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003248 if (proc->buffer) {
3249 ret = -EBUSY;
3250 failure_string = "already mapped";
3251 goto err_already_mapped;
3252 }
3253
3254 area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
3255 if (area == NULL) {
3256 ret = -ENOMEM;
3257 failure_string = "get_vm_area";
3258 goto err_get_vm_area_failed;
3259 }
3260 proc->buffer = area->addr;
3261 proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer;
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08003262 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003263
3264#ifdef CONFIG_CPU_CACHE_VIPT
3265 if (cache_is_vipt_aliasing()) {
3266 while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) {
Sherwin Soltani258767f2012-06-26 02:00:30 -04003267 pr_info("binder_mmap: %d %lx-%lx maps %p bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003268 vma->vm_start += PAGE_SIZE;
3269 }
3270 }
3271#endif
3272 proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL);
3273 if (proc->pages == NULL) {
3274 ret = -ENOMEM;
3275 failure_string = "alloc page array";
3276 goto err_alloc_pages_failed;
3277 }
3278 proc->buffer_size = vma->vm_end - vma->vm_start;
3279
3280 vma->vm_ops = &binder_vm_ops;
3281 vma->vm_private_data = proc;
3282
3283 if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) {
3284 ret = -ENOMEM;
3285 failure_string = "alloc small buf";
3286 goto err_alloc_small_buf_failed;
3287 }
3288 buffer = proc->buffer;
3289 INIT_LIST_HEAD(&proc->buffers);
3290 list_add(&buffer->entry, &proc->buffers);
3291 buffer->free = 1;
3292 binder_insert_free_buffer(proc, buffer);
3293 proc->free_async_space = proc->buffer_size / 2;
3294 barrier();
Al Viroa79f41e2012-08-15 18:23:36 -04003295 proc->files = get_files_struct(current);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003296 proc->vma = vma;
Arve Hjønnevåg2a909572012-03-08 15:43:36 -08003297 proc->vma_vm_mm = vma->vm_mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003298
Sherwin Soltani258767f2012-06-26 02:00:30 -04003299 /*pr_info("binder_mmap: %d %lx-%lx maps %p\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003300 proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/
3301 return 0;
3302
3303err_alloc_small_buf_failed:
3304 kfree(proc->pages);
3305 proc->pages = NULL;
3306err_alloc_pages_failed:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08003307 mutex_lock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003308 vfree(proc->buffer);
3309 proc->buffer = NULL;
3310err_get_vm_area_failed:
3311err_already_mapped:
Arve Hjønnevågbd1eff92012-02-01 15:29:13 -08003312 mutex_unlock(&binder_mmap_lock);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003313err_bad_arg:
Sherwin Soltani258767f2012-06-26 02:00:30 -04003314 pr_err("binder_mmap: %d %lx-%lx %s failed %d\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003315 proc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
3316 return ret;
3317}
3318
3319static int binder_open(struct inode *nodp, struct file *filp)
3320{
3321 struct binder_proc *proc;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003322 struct binder_device *binder_dev;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003323
3324 binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n",
3325 current->group_leader->pid, current->pid);
3326
3327 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
3328 if (proc == NULL)
3329 return -ENOMEM;
3330 get_task_struct(current);
3331 proc->tsk = current;
Chen Fenga906d692016-02-01 14:04:02 +08003332 proc->vma_vm_mm = current->mm;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003333 INIT_LIST_HEAD(&proc->todo);
3334 init_waitqueue_head(&proc->wait);
3335 proc->default_priority = task_nice(current);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02003336 binder_dev = container_of(filp->private_data, struct binder_device,
3337 miscdev);
3338 proc->context = &binder_dev->context;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003339
3340 binder_lock(__func__);
3341
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003342 binder_stats_created(BINDER_STAT_PROC);
3343 hlist_add_head(&proc->proc_node, &binder_procs);
3344 proc->pid = current->group_leader->pid;
3345 INIT_LIST_HEAD(&proc->delivered_death);
3346 filp->private_data = proc;
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003347
3348 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003349
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003350 if (binder_debugfs_dir_entry_proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003351 char strbuf[11];
Seunghun Lee10f62862014-05-01 01:30:23 +09003352
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003353 snprintf(strbuf, sizeof(strbuf), "%u", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003354 /*
3355 * proc debug entries are shared between contexts, so
3356 * this will fail if the process tries to open the driver
3357 * again with a different context. The priting code will
3358 * anyway print all contexts that a given PID has, so this
3359 * is not a problem.
3360 */
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003361 proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003362 binder_debugfs_dir_entry_proc,
3363 (void *)(unsigned long)proc->pid,
3364 &binder_proc_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003365 }
3366
3367 return 0;
3368}
3369
3370static int binder_flush(struct file *filp, fl_owner_t id)
3371{
3372 struct binder_proc *proc = filp->private_data;
3373
3374 binder_defer_work(proc, BINDER_DEFERRED_FLUSH);
3375
3376 return 0;
3377}
3378
3379static void binder_deferred_flush(struct binder_proc *proc)
3380{
3381 struct rb_node *n;
3382 int wake_count = 0;
Seunghun Lee10f62862014-05-01 01:30:23 +09003383
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003384 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) {
3385 struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node);
Seunghun Lee10f62862014-05-01 01:30:23 +09003386
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003387 thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN;
3388 if (thread->looper & BINDER_LOOPER_STATE_WAITING) {
3389 wake_up_interruptible(&thread->wait);
3390 wake_count++;
3391 }
3392 }
3393 wake_up_interruptible_all(&proc->wait);
3394
3395 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
3396 "binder_flush: %d woke %d threads\n", proc->pid,
3397 wake_count);
3398}
3399
3400static int binder_release(struct inode *nodp, struct file *filp)
3401{
3402 struct binder_proc *proc = filp->private_data;
Seunghun Lee10f62862014-05-01 01:30:23 +09003403
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07003404 debugfs_remove(proc->debugfs_entry);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003405 binder_defer_work(proc, BINDER_DEFERRED_RELEASE);
3406
3407 return 0;
3408}
3409
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003410static int binder_node_release(struct binder_node *node, int refs)
3411{
3412 struct binder_ref *ref;
3413 int death = 0;
3414
3415 list_del_init(&node->work.entry);
3416 binder_release_work(&node->async_todo);
3417
3418 if (hlist_empty(&node->refs)) {
3419 kfree(node);
3420 binder_stats_deleted(BINDER_STAT_NODE);
3421
3422 return refs;
3423 }
3424
3425 node->proc = NULL;
3426 node->local_strong_refs = 0;
3427 node->local_weak_refs = 0;
3428 hlist_add_head(&node->dead_node, &binder_dead_nodes);
3429
3430 hlist_for_each_entry(ref, &node->refs, node_entry) {
3431 refs++;
3432
3433 if (!ref->death)
Arve Hjønnevåge194fd82014-02-17 13:58:29 -08003434 continue;
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003435
3436 death++;
3437
3438 if (list_empty(&ref->death->work.entry)) {
3439 ref->death->work.type = BINDER_WORK_DEAD_BINDER;
3440 list_add_tail(&ref->death->work.entry,
3441 &ref->proc->todo);
3442 wake_up_interruptible(&ref->proc->wait);
3443 } else
3444 BUG();
3445 }
3446
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003447 binder_debug(BINDER_DEBUG_DEAD_BINDER,
3448 "node %d now dead, refs %d, death %d\n",
3449 node->debug_id, refs, death);
3450
3451 return refs;
3452}
3453
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003454static void binder_deferred_release(struct binder_proc *proc)
3455{
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003456 struct binder_transaction *t;
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003457 struct binder_context *context = proc->context;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003458 struct rb_node *n;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003459 int threads, nodes, incoming_refs, outgoing_refs, buffers,
3460 active_transactions, page_count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003461
3462 BUG_ON(proc->vma);
3463 BUG_ON(proc->files);
3464
3465 hlist_del(&proc->proc_node);
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003466
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003467 if (context->binder_context_mgr_node &&
3468 context->binder_context_mgr_node->proc == proc) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003469 binder_debug(BINDER_DEBUG_DEAD_BINDER,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003470 "%s: %d context_mgr_node gone\n",
3471 __func__, proc->pid);
Martijn Coenen0b3311e2016-09-30 15:51:48 +02003472 context->binder_context_mgr_node = NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003473 }
3474
3475 threads = 0;
3476 active_transactions = 0;
3477 while ((n = rb_first(&proc->threads))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003478 struct binder_thread *thread;
3479
3480 thread = rb_entry(n, struct binder_thread, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003481 threads++;
3482 active_transactions += binder_free_thread(proc, thread);
3483 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003484
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003485 nodes = 0;
3486 incoming_refs = 0;
3487 while ((n = rb_first(&proc->nodes))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003488 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003489
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003490 node = rb_entry(n, struct binder_node, rb_node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003491 nodes++;
3492 rb_erase(&node->rb_node, &proc->nodes);
Mirsal Ennaime008fa742013-03-12 11:41:59 +01003493 incoming_refs = binder_node_release(node, incoming_refs);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003494 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003495
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003496 outgoing_refs = 0;
3497 while ((n = rb_first(&proc->refs_by_desc))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003498 struct binder_ref *ref;
3499
3500 ref = rb_entry(n, struct binder_ref, rb_node_desc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003501 outgoing_refs++;
3502 binder_delete_ref(ref);
3503 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003504
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003505 binder_release_work(&proc->todo);
Arve Hjønnevåg675d66b2012-10-16 15:29:54 -07003506 binder_release_work(&proc->delivered_death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003507
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003508 buffers = 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003509 while ((n = rb_first(&proc->allocated_buffers))) {
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003510 struct binder_buffer *buffer;
3511
3512 buffer = rb_entry(n, struct binder_buffer, rb_node);
3513
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003514 t = buffer->transaction;
3515 if (t) {
3516 t->buffer = NULL;
3517 buffer->transaction = NULL;
Anmol Sarma56b468f2012-10-30 22:35:43 +05303518 pr_err("release proc %d, transaction %d, not freed\n",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003519 proc->pid, t->debug_id);
3520 /*BUG();*/
3521 }
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003522
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003523 binder_free_buf(proc, buffer);
3524 buffers++;
3525 }
3526
3527 binder_stats_deleted(BINDER_STAT_PROC);
3528
3529 page_count = 0;
3530 if (proc->pages) {
3531 int i;
Mirsal Ennaime53413e72013-03-12 11:42:00 +01003532
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003533 for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) {
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003534 void *page_addr;
3535
3536 if (!proc->pages[i])
3537 continue;
3538
3539 page_addr = proc->buffer + i * PAGE_SIZE;
3540 binder_debug(BINDER_DEBUG_BUFFER_ALLOC,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003541 "%s: %d: page %d at %p not freed\n",
3542 __func__, proc->pid, i, page_addr);
Mirsal Ennaimeba97bc52013-03-12 11:42:01 +01003543 unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
3544 __free_page(proc->pages[i]);
3545 page_count++;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003546 }
3547 kfree(proc->pages);
3548 vfree(proc->buffer);
3549 }
3550
3551 put_task_struct(proc->tsk);
3552
3553 binder_debug(BINDER_DEBUG_OPEN_CLOSE,
Mirsal Ennaimec07c9332013-03-12 11:42:02 +01003554 "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n",
3555 __func__, proc->pid, threads, nodes, incoming_refs,
3556 outgoing_refs, active_transactions, buffers, page_count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003557
3558 kfree(proc);
3559}
3560
3561static void binder_deferred_func(struct work_struct *work)
3562{
3563 struct binder_proc *proc;
3564 struct files_struct *files;
3565
3566 int defer;
Seunghun Lee10f62862014-05-01 01:30:23 +09003567
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003568 do {
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003569 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003570 mutex_lock(&binder_deferred_lock);
3571 if (!hlist_empty(&binder_deferred_list)) {
3572 proc = hlist_entry(binder_deferred_list.first,
3573 struct binder_proc, deferred_work_node);
3574 hlist_del_init(&proc->deferred_work_node);
3575 defer = proc->deferred_work;
3576 proc->deferred_work = 0;
3577 } else {
3578 proc = NULL;
3579 defer = 0;
3580 }
3581 mutex_unlock(&binder_deferred_lock);
3582
3583 files = NULL;
3584 if (defer & BINDER_DEFERRED_PUT_FILES) {
3585 files = proc->files;
3586 if (files)
3587 proc->files = NULL;
3588 }
3589
3590 if (defer & BINDER_DEFERRED_FLUSH)
3591 binder_deferred_flush(proc);
3592
3593 if (defer & BINDER_DEFERRED_RELEASE)
3594 binder_deferred_release(proc); /* frees proc */
3595
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003596 binder_unlock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003597 if (files)
3598 put_files_struct(files);
3599 } while (proc);
3600}
3601static DECLARE_WORK(binder_deferred_work, binder_deferred_func);
3602
3603static void
3604binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer)
3605{
3606 mutex_lock(&binder_deferred_lock);
3607 proc->deferred_work |= defer;
3608 if (hlist_unhashed(&proc->deferred_work_node)) {
3609 hlist_add_head(&proc->deferred_work_node,
3610 &binder_deferred_list);
Bhaktipriya Shridhar1beba522016-08-13 22:16:24 +05303611 schedule_work(&binder_deferred_work);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003612 }
3613 mutex_unlock(&binder_deferred_lock);
3614}
3615
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003616static void print_binder_transaction(struct seq_file *m, const char *prefix,
3617 struct binder_transaction *t)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003618{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003619 seq_printf(m,
3620 "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d",
3621 prefix, t->debug_id, t,
3622 t->from ? t->from->proc->pid : 0,
3623 t->from ? t->from->pid : 0,
3624 t->to_proc ? t->to_proc->pid : 0,
3625 t->to_thread ? t->to_thread->pid : 0,
3626 t->code, t->flags, t->priority, t->need_reply);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003627 if (t->buffer == NULL) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003628 seq_puts(m, " buffer free\n");
3629 return;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003630 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003631 if (t->buffer->target_node)
3632 seq_printf(m, " node %d",
3633 t->buffer->target_node->debug_id);
3634 seq_printf(m, " size %zd:%zd data %p\n",
3635 t->buffer->data_size, t->buffer->offsets_size,
3636 t->buffer->data);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003637}
3638
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003639static void print_binder_buffer(struct seq_file *m, const char *prefix,
3640 struct binder_buffer *buffer)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003641{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003642 seq_printf(m, "%s %d: %p size %zd:%zd %s\n",
3643 prefix, buffer->debug_id, buffer->data,
3644 buffer->data_size, buffer->offsets_size,
3645 buffer->transaction ? "active" : "delivered");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003646}
3647
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003648static void print_binder_work(struct seq_file *m, const char *prefix,
3649 const char *transaction_prefix,
3650 struct binder_work *w)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003651{
3652 struct binder_node *node;
3653 struct binder_transaction *t;
3654
3655 switch (w->type) {
3656 case BINDER_WORK_TRANSACTION:
3657 t = container_of(w, struct binder_transaction, work);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003658 print_binder_transaction(m, transaction_prefix, t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003659 break;
3660 case BINDER_WORK_TRANSACTION_COMPLETE:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003661 seq_printf(m, "%stransaction complete\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003662 break;
3663 case BINDER_WORK_NODE:
3664 node = container_of(w, struct binder_node, work);
Arve Hjønnevågda498892014-02-21 14:40:26 -08003665 seq_printf(m, "%snode work %d: u%016llx c%016llx\n",
3666 prefix, node->debug_id,
3667 (u64)node->ptr, (u64)node->cookie);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003668 break;
3669 case BINDER_WORK_DEAD_BINDER:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003670 seq_printf(m, "%shas dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003671 break;
3672 case BINDER_WORK_DEAD_BINDER_AND_CLEAR:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003673 seq_printf(m, "%shas cleared dead binder\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003674 break;
3675 case BINDER_WORK_CLEAR_DEATH_NOTIFICATION:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003676 seq_printf(m, "%shas cleared death notification\n", prefix);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003677 break;
3678 default:
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003679 seq_printf(m, "%sunknown work: type %d\n", prefix, w->type);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003680 break;
3681 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003682}
3683
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003684static void print_binder_thread(struct seq_file *m,
3685 struct binder_thread *thread,
3686 int print_always)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003687{
3688 struct binder_transaction *t;
3689 struct binder_work *w;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003690 size_t start_pos = m->count;
3691 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003692
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003693 seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper);
3694 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003695 t = thread->transaction_stack;
3696 while (t) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003697 if (t->from == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003698 print_binder_transaction(m,
3699 " outgoing transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003700 t = t->from_parent;
3701 } else if (t->to_thread == thread) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003702 print_binder_transaction(m,
3703 " incoming transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003704 t = t->to_parent;
3705 } else {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003706 print_binder_transaction(m, " bad transaction", t);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003707 t = NULL;
3708 }
3709 }
3710 list_for_each_entry(w, &thread->todo, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003711 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003712 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003713 if (!print_always && m->count == header_pos)
3714 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003715}
3716
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003717static void print_binder_node(struct seq_file *m, struct binder_node *node)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003718{
3719 struct binder_ref *ref;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003720 struct binder_work *w;
3721 int count;
3722
3723 count = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003724 hlist_for_each_entry(ref, &node->refs, node_entry)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003725 count++;
3726
Arve Hjønnevågda498892014-02-21 14:40:26 -08003727 seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d",
3728 node->debug_id, (u64)node->ptr, (u64)node->cookie,
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003729 node->has_strong_ref, node->has_weak_ref,
3730 node->local_strong_refs, node->local_weak_refs,
3731 node->internal_strong_refs, count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003732 if (count) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003733 seq_puts(m, " proc");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003734 hlist_for_each_entry(ref, &node->refs, node_entry)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003735 seq_printf(m, " %d", ref->proc->pid);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003736 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003737 seq_puts(m, "\n");
3738 list_for_each_entry(w, &node->async_todo, entry)
3739 print_binder_work(m, " ",
3740 " pending async transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003741}
3742
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003743static void print_binder_ref(struct seq_file *m, struct binder_ref *ref)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003744{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003745 seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %p\n",
3746 ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ",
3747 ref->node->debug_id, ref->strong, ref->weak, ref->death);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003748}
3749
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003750static void print_binder_proc(struct seq_file *m,
3751 struct binder_proc *proc, int print_all)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003752{
3753 struct binder_work *w;
3754 struct rb_node *n;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003755 size_t start_pos = m->count;
3756 size_t header_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003757
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003758 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003759 seq_printf(m, "context %s\n", proc->context->name);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003760 header_pos = m->count;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003761
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003762 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3763 print_binder_thread(m, rb_entry(n, struct binder_thread,
3764 rb_node), print_all);
3765 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003766 struct binder_node *node = rb_entry(n, struct binder_node,
3767 rb_node);
3768 if (print_all || node->has_async_transaction)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003769 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003770 }
3771 if (print_all) {
3772 for (n = rb_first(&proc->refs_by_desc);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003773 n != NULL;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003774 n = rb_next(n))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003775 print_binder_ref(m, rb_entry(n, struct binder_ref,
3776 rb_node_desc));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003777 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003778 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3779 print_binder_buffer(m, " buffer",
3780 rb_entry(n, struct binder_buffer, rb_node));
3781 list_for_each_entry(w, &proc->todo, entry)
3782 print_binder_work(m, " ", " pending transaction", w);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003783 list_for_each_entry(w, &proc->delivered_death, entry) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003784 seq_puts(m, " has delivered dead binder\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003785 break;
3786 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003787 if (!print_all && m->count == header_pos)
3788 m->count = start_pos;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003789}
3790
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003791static const char * const binder_return_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003792 "BR_ERROR",
3793 "BR_OK",
3794 "BR_TRANSACTION",
3795 "BR_REPLY",
3796 "BR_ACQUIRE_RESULT",
3797 "BR_DEAD_REPLY",
3798 "BR_TRANSACTION_COMPLETE",
3799 "BR_INCREFS",
3800 "BR_ACQUIRE",
3801 "BR_RELEASE",
3802 "BR_DECREFS",
3803 "BR_ATTEMPT_ACQUIRE",
3804 "BR_NOOP",
3805 "BR_SPAWN_LOOPER",
3806 "BR_FINISHED",
3807 "BR_DEAD_BINDER",
3808 "BR_CLEAR_DEATH_NOTIFICATION_DONE",
3809 "BR_FAILED_REPLY"
3810};
3811
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003812static const char * const binder_command_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003813 "BC_TRANSACTION",
3814 "BC_REPLY",
3815 "BC_ACQUIRE_RESULT",
3816 "BC_FREE_BUFFER",
3817 "BC_INCREFS",
3818 "BC_ACQUIRE",
3819 "BC_RELEASE",
3820 "BC_DECREFS",
3821 "BC_INCREFS_DONE",
3822 "BC_ACQUIRE_DONE",
3823 "BC_ATTEMPT_ACQUIRE",
3824 "BC_REGISTER_LOOPER",
3825 "BC_ENTER_LOOPER",
3826 "BC_EXIT_LOOPER",
3827 "BC_REQUEST_DEATH_NOTIFICATION",
3828 "BC_CLEAR_DEATH_NOTIFICATION",
Martijn Coenen5a6da532016-09-30 14:10:07 +02003829 "BC_DEAD_BINDER_DONE",
3830 "BC_TRANSACTION_SG",
3831 "BC_REPLY_SG",
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003832};
3833
Cruz Julian Bishop167bccb2012-12-22 09:00:45 +10003834static const char * const binder_objstat_strings[] = {
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003835 "proc",
3836 "thread",
3837 "node",
3838 "ref",
3839 "death",
3840 "transaction",
3841 "transaction_complete"
3842};
3843
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003844static void print_binder_stats(struct seq_file *m, const char *prefix,
3845 struct binder_stats *stats)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003846{
3847 int i;
3848
3849 BUILD_BUG_ON(ARRAY_SIZE(stats->bc) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003850 ARRAY_SIZE(binder_command_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003851 for (i = 0; i < ARRAY_SIZE(stats->bc); i++) {
3852 if (stats->bc[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003853 seq_printf(m, "%s%s: %d\n", prefix,
3854 binder_command_strings[i], stats->bc[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003855 }
3856
3857 BUILD_BUG_ON(ARRAY_SIZE(stats->br) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003858 ARRAY_SIZE(binder_return_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003859 for (i = 0; i < ARRAY_SIZE(stats->br); i++) {
3860 if (stats->br[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003861 seq_printf(m, "%s%s: %d\n", prefix,
3862 binder_return_strings[i], stats->br[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003863 }
3864
3865 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003866 ARRAY_SIZE(binder_objstat_strings));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003867 BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) !=
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003868 ARRAY_SIZE(stats->obj_deleted));
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003869 for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) {
3870 if (stats->obj_created[i] || stats->obj_deleted[i])
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003871 seq_printf(m, "%s%s: active %d total %d\n", prefix,
3872 binder_objstat_strings[i],
3873 stats->obj_created[i] - stats->obj_deleted[i],
3874 stats->obj_created[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003875 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003876}
3877
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003878static void print_binder_proc_stats(struct seq_file *m,
3879 struct binder_proc *proc)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003880{
3881 struct binder_work *w;
3882 struct rb_node *n;
3883 int count, strong, weak;
3884
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003885 seq_printf(m, "proc %d\n", proc->pid);
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003886 seq_printf(m, "context %s\n", proc->context->name);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003887 count = 0;
3888 for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n))
3889 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003890 seq_printf(m, " threads: %d\n", count);
3891 seq_printf(m, " requested threads: %d+%d/%d\n"
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003892 " ready threads %d\n"
3893 " free async space %zd\n", proc->requested_threads,
3894 proc->requested_threads_started, proc->max_threads,
3895 proc->ready_threads, proc->free_async_space);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003896 count = 0;
3897 for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n))
3898 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003899 seq_printf(m, " nodes: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003900 count = 0;
3901 strong = 0;
3902 weak = 0;
3903 for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) {
3904 struct binder_ref *ref = rb_entry(n, struct binder_ref,
3905 rb_node_desc);
3906 count++;
3907 strong += ref->strong;
3908 weak += ref->weak;
3909 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003910 seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003911
3912 count = 0;
3913 for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n))
3914 count++;
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003915 seq_printf(m, " buffers: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003916
3917 count = 0;
3918 list_for_each_entry(w, &proc->todo, entry) {
3919 switch (w->type) {
3920 case BINDER_WORK_TRANSACTION:
3921 count++;
3922 break;
3923 default:
3924 break;
3925 }
3926 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003927 seq_printf(m, " pending transactions: %d\n", count);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003928
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003929 print_binder_stats(m, " ", &proc->stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003930}
3931
3932
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003933static int binder_state_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003934{
3935 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003936 struct binder_node *node;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003937 int do_lock = !binder_debug_no_lock;
3938
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003939 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003940 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003941
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003942 seq_puts(m, "binder state:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003943
3944 if (!hlist_empty(&binder_dead_nodes))
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003945 seq_puts(m, "dead nodes:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003946 hlist_for_each_entry(node, &binder_dead_nodes, dead_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003947 print_binder_node(m, node);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003948
Sasha Levinb67bfe02013-02-27 17:06:00 -08003949 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003950 print_binder_proc(m, proc, 1);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003951 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003952 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003953 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003954}
3955
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003956static int binder_stats_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003957{
3958 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003959 int do_lock = !binder_debug_no_lock;
3960
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003961 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003962 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003963
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003964 seq_puts(m, "binder stats:\n");
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003965
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003966 print_binder_stats(m, "", &binder_stats);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003967
Sasha Levinb67bfe02013-02-27 17:06:00 -08003968 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003969 print_binder_proc_stats(m, proc);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003970 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003971 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003972 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003973}
3974
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003975static int binder_transactions_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003976{
3977 struct binder_proc *proc;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003978 int do_lock = !binder_debug_no_lock;
3979
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003980 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003981 binder_lock(__func__);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003982
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003983 seq_puts(m, "binder transactions:\n");
Sasha Levinb67bfe02013-02-27 17:06:00 -08003984 hlist_for_each_entry(proc, &binder_procs, proc_node)
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003985 print_binder_proc(m, proc, 0);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003986 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003987 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003988 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003989}
3990
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07003991static int binder_proc_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003992{
Riley Andrews83050a42016-02-09 21:05:33 -08003993 struct binder_proc *itr;
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02003994 int pid = (unsigned long)m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003995 int do_lock = !binder_debug_no_lock;
3996
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09003997 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07003998 binder_lock(__func__);
Riley Andrews83050a42016-02-09 21:05:33 -08003999
4000 hlist_for_each_entry(itr, &binder_procs, proc_node) {
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004001 if (itr->pid == pid) {
4002 seq_puts(m, "binder proc state:\n");
4003 print_binder_proc(m, itr, 1);
Riley Andrews83050a42016-02-09 21:05:33 -08004004 }
4005 }
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004006 if (do_lock)
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004007 binder_unlock(__func__);
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004008 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004009}
4010
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004011static void print_binder_transaction_log_entry(struct seq_file *m,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004012 struct binder_transaction_log_entry *e)
4013{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004014 seq_printf(m,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004015 "%d: %s from %d:%d to %d:%d context %s node %d handle %d size %d:%d\n",
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004016 e->debug_id, (e->call_type == 2) ? "reply" :
4017 ((e->call_type == 1) ? "async" : "call "), e->from_proc,
Martijn Coenen63b9f3b2016-10-17 15:17:31 +02004018 e->from_thread, e->to_proc, e->to_thread, e->context_name,
4019 e->to_node, e->target_handle, e->data_size, e->offsets_size);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004020}
4021
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004022static int binder_transaction_log_show(struct seq_file *m, void *unused)
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004023{
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004024 struct binder_transaction_log *log = m->private;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004025 int i;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004026
4027 if (log->full) {
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004028 for (i = log->next; i < ARRAY_SIZE(log->entry); i++)
4029 print_binder_transaction_log_entry(m, &log->entry[i]);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004030 }
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004031 for (i = 0; i < log->next; i++)
4032 print_binder_transaction_log_entry(m, &log->entry[i]);
4033 return 0;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004034}
4035
4036static const struct file_operations binder_fops = {
4037 .owner = THIS_MODULE,
4038 .poll = binder_poll,
4039 .unlocked_ioctl = binder_ioctl,
Arve Hjønnevågda498892014-02-21 14:40:26 -08004040 .compat_ioctl = binder_ioctl,
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004041 .mmap = binder_mmap,
4042 .open = binder_open,
4043 .flush = binder_flush,
4044 .release = binder_release,
4045};
4046
Arve Hjønnevåg5249f482009-04-28 20:57:50 -07004047BINDER_DEBUG_ENTRY(state);
4048BINDER_DEBUG_ENTRY(stats);
4049BINDER_DEBUG_ENTRY(transactions);
4050BINDER_DEBUG_ENTRY(transaction_log);
4051
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004052static int __init init_binder_device(const char *name)
4053{
4054 int ret;
4055 struct binder_device *binder_device;
4056
4057 binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);
4058 if (!binder_device)
4059 return -ENOMEM;
4060
4061 binder_device->miscdev.fops = &binder_fops;
4062 binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
4063 binder_device->miscdev.name = name;
4064
4065 binder_device->context.binder_context_mgr_uid = INVALID_UID;
4066 binder_device->context.name = name;
4067
4068 ret = misc_register(&binder_device->miscdev);
4069 if (ret < 0) {
4070 kfree(binder_device);
4071 return ret;
4072 }
4073
4074 hlist_add_head(&binder_device->hlist, &binder_devices);
4075
4076 return ret;
4077}
4078
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004079static int __init binder_init(void)
4080{
4081 int ret;
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004082 char *device_name, *device_names;
4083 struct binder_device *device;
4084 struct hlist_node *tmp;
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004085
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004086 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL);
4087 if (binder_debugfs_dir_entry_root)
4088 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc",
4089 binder_debugfs_dir_entry_root);
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004090
Arve Hjønnevåg16b66552009-04-28 20:57:50 -07004091 if (binder_debugfs_dir_entry_root) {
4092 debugfs_create_file("state",
4093 S_IRUGO,
4094 binder_debugfs_dir_entry_root,
4095 NULL,
4096 &binder_state_fops);
4097 debugfs_create_file("stats",
4098 S_IRUGO,
4099 binder_debugfs_dir_entry_root,
4100 NULL,
4101 &binder_stats_fops);
4102 debugfs_create_file("transactions",
4103 S_IRUGO,
4104 binder_debugfs_dir_entry_root,
4105 NULL,
4106 &binder_transactions_fops);
4107 debugfs_create_file("transaction_log",
4108 S_IRUGO,
4109 binder_debugfs_dir_entry_root,
4110 &binder_transaction_log,
4111 &binder_transaction_log_fops);
4112 debugfs_create_file("failed_transaction_log",
4113 S_IRUGO,
4114 binder_debugfs_dir_entry_root,
4115 &binder_transaction_log_failed,
4116 &binder_transaction_log_fops);
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004117 }
Martijn Coenen6b7c7122016-09-30 16:08:09 +02004118
4119 /*
4120 * Copy the module_parameter string, because we don't want to
4121 * tokenize it in-place.
4122 */
4123 device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
4124 if (!device_names) {
4125 ret = -ENOMEM;
4126 goto err_alloc_device_names_failed;
4127 }
4128 strcpy(device_names, binder_devices_param);
4129
4130 while ((device_name = strsep(&device_names, ","))) {
4131 ret = init_binder_device(device_name);
4132 if (ret)
4133 goto err_init_binder_device_failed;
4134 }
4135
4136 return ret;
4137
4138err_init_binder_device_failed:
4139 hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
4140 misc_deregister(&device->miscdev);
4141 hlist_del(&device->hlist);
4142 kfree(device);
4143 }
4144err_alloc_device_names_failed:
4145 debugfs_remove_recursive(binder_debugfs_dir_entry_root);
4146
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004147 return ret;
4148}
4149
4150device_initcall(binder_init);
4151
Arve Hjønnevåg975a1ac2012-10-16 15:29:53 -07004152#define CREATE_TRACE_POINTS
4153#include "binder_trace.h"
4154
Greg Kroah-Hartman355b0502011-11-30 20:18:14 +09004155MODULE_LICENSE("GPL v2");