Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1 | /* 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 Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 20 | #include <asm/cacheflush.h> |
| 21 | #include <linux/fdtable.h> |
| 22 | #include <linux/file.h> |
Colin Cross | e2610b2 | 2013-05-06 23:50:15 +0000 | [diff] [blame] | 23 | #include <linux/freezer.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 24 | #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åg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 32 | #include <linux/debugfs.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 33 | #include <linux/rbtree.h> |
| 34 | #include <linux/sched.h> |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 35 | #include <linux/seq_file.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 36 | #include <linux/uaccess.h> |
| 37 | #include <linux/vmalloc.h> |
Colin Cross | c11a166 | 2010-04-15 15:21:51 -0700 | [diff] [blame] | 38 | #include <linux/slab.h> |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 39 | #include <linux/pid_namespace.h> |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 40 | #include <linux/security.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 41 | |
Greg Kroah-Hartman | 9246a4a | 2014-10-16 15:26:51 +0200 | [diff] [blame] | 42 | #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åg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 47 | #include "binder_trace.h" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 48 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 49 | static DEFINE_MUTEX(binder_main_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 50 | static DEFINE_MUTEX(binder_deferred_lock); |
Arve Hjønnevåg | bd1eff9 | 2012-02-01 15:29:13 -0800 | [diff] [blame] | 51 | static DEFINE_MUTEX(binder_mmap_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 52 | |
| 53 | static HLIST_HEAD(binder_procs); |
| 54 | static HLIST_HEAD(binder_deferred_list); |
| 55 | static HLIST_HEAD(binder_dead_nodes); |
| 56 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 57 | static struct dentry *binder_debugfs_dir_entry_root; |
| 58 | static struct dentry *binder_debugfs_dir_entry_proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 59 | static struct binder_node *binder_context_mgr_node; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 60 | static kuid_t binder_context_mgr_uid = INVALID_UID; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 61 | static int binder_last_id; |
| 62 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 63 | #define BINDER_DEBUG_ENTRY(name) \ |
| 64 | static int binder_##name##_open(struct inode *inode, struct file *file) \ |
| 65 | { \ |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 66 | return single_open(file, binder_##name##_show, inode->i_private); \ |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 67 | } \ |
| 68 | \ |
| 69 | static const struct file_operations binder_##name##_fops = { \ |
| 70 | .owner = THIS_MODULE, \ |
| 71 | .open = binder_##name##_open, \ |
| 72 | .read = seq_read, \ |
| 73 | .llseek = seq_lseek, \ |
| 74 | .release = single_release, \ |
| 75 | } |
| 76 | |
| 77 | static int binder_proc_show(struct seq_file *m, void *unused); |
| 78 | BINDER_DEBUG_ENTRY(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 79 | |
| 80 | /* This is only defined in include/asm-arm/sizes.h */ |
| 81 | #ifndef SZ_1K |
| 82 | #define SZ_1K 0x400 |
| 83 | #endif |
| 84 | |
| 85 | #ifndef SZ_4M |
| 86 | #define SZ_4M 0x400000 |
| 87 | #endif |
| 88 | |
| 89 | #define FORBIDDEN_MMAP_FLAGS (VM_WRITE) |
| 90 | |
| 91 | #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64) |
| 92 | |
| 93 | enum { |
| 94 | BINDER_DEBUG_USER_ERROR = 1U << 0, |
| 95 | BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1, |
| 96 | BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2, |
| 97 | BINDER_DEBUG_OPEN_CLOSE = 1U << 3, |
| 98 | BINDER_DEBUG_DEAD_BINDER = 1U << 4, |
| 99 | BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5, |
| 100 | BINDER_DEBUG_READ_WRITE = 1U << 6, |
| 101 | BINDER_DEBUG_USER_REFS = 1U << 7, |
| 102 | BINDER_DEBUG_THREADS = 1U << 8, |
| 103 | BINDER_DEBUG_TRANSACTION = 1U << 9, |
| 104 | BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10, |
| 105 | BINDER_DEBUG_FREE_BUFFER = 1U << 11, |
| 106 | BINDER_DEBUG_INTERNAL_REFS = 1U << 12, |
| 107 | BINDER_DEBUG_BUFFER_ALLOC = 1U << 13, |
| 108 | BINDER_DEBUG_PRIORITY_CAP = 1U << 14, |
| 109 | BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 15, |
| 110 | }; |
| 111 | static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR | |
| 112 | BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION; |
| 113 | module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO); |
| 114 | |
Zhengwang Ruan | 2c52325 | 2012-03-07 10:36:57 +0800 | [diff] [blame] | 115 | static bool binder_debug_no_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 116 | module_param_named(proc_no_lock, binder_debug_no_lock, bool, S_IWUSR | S_IRUGO); |
| 117 | |
| 118 | static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait); |
| 119 | static int binder_stop_on_user_error; |
| 120 | |
| 121 | static int binder_set_stop_on_user_error(const char *val, |
| 122 | struct kernel_param *kp) |
| 123 | { |
| 124 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 125 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 126 | ret = param_set_int(val, kp); |
| 127 | if (binder_stop_on_user_error < 2) |
| 128 | wake_up(&binder_user_error_wait); |
| 129 | return ret; |
| 130 | } |
| 131 | module_param_call(stop_on_user_error, binder_set_stop_on_user_error, |
| 132 | param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO); |
| 133 | |
| 134 | #define binder_debug(mask, x...) \ |
| 135 | do { \ |
| 136 | if (binder_debug_mask & mask) \ |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 137 | pr_info(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 138 | } while (0) |
| 139 | |
| 140 | #define binder_user_error(x...) \ |
| 141 | do { \ |
| 142 | if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \ |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 143 | pr_info(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 144 | if (binder_stop_on_user_error) \ |
| 145 | binder_stop_on_user_error = 2; \ |
| 146 | } while (0) |
| 147 | |
| 148 | enum binder_stat_types { |
| 149 | BINDER_STAT_PROC, |
| 150 | BINDER_STAT_THREAD, |
| 151 | BINDER_STAT_NODE, |
| 152 | BINDER_STAT_REF, |
| 153 | BINDER_STAT_DEATH, |
| 154 | BINDER_STAT_TRANSACTION, |
| 155 | BINDER_STAT_TRANSACTION_COMPLETE, |
| 156 | BINDER_STAT_COUNT |
| 157 | }; |
| 158 | |
| 159 | struct binder_stats { |
| 160 | int br[_IOC_NR(BR_FAILED_REPLY) + 1]; |
| 161 | int bc[_IOC_NR(BC_DEAD_BINDER_DONE) + 1]; |
| 162 | int obj_created[BINDER_STAT_COUNT]; |
| 163 | int obj_deleted[BINDER_STAT_COUNT]; |
| 164 | }; |
| 165 | |
| 166 | static struct binder_stats binder_stats; |
| 167 | |
| 168 | static inline void binder_stats_deleted(enum binder_stat_types type) |
| 169 | { |
| 170 | binder_stats.obj_deleted[type]++; |
| 171 | } |
| 172 | |
| 173 | static inline void binder_stats_created(enum binder_stat_types type) |
| 174 | { |
| 175 | binder_stats.obj_created[type]++; |
| 176 | } |
| 177 | |
| 178 | struct binder_transaction_log_entry { |
| 179 | int debug_id; |
| 180 | int call_type; |
| 181 | int from_proc; |
| 182 | int from_thread; |
| 183 | int target_handle; |
| 184 | int to_proc; |
| 185 | int to_thread; |
| 186 | int to_node; |
| 187 | int data_size; |
| 188 | int offsets_size; |
| 189 | }; |
| 190 | struct binder_transaction_log { |
| 191 | int next; |
| 192 | int full; |
| 193 | struct binder_transaction_log_entry entry[32]; |
| 194 | }; |
| 195 | static struct binder_transaction_log binder_transaction_log; |
| 196 | static struct binder_transaction_log binder_transaction_log_failed; |
| 197 | |
| 198 | static struct binder_transaction_log_entry *binder_transaction_log_add( |
| 199 | struct binder_transaction_log *log) |
| 200 | { |
| 201 | struct binder_transaction_log_entry *e; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 202 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 203 | e = &log->entry[log->next]; |
| 204 | memset(e, 0, sizeof(*e)); |
| 205 | log->next++; |
| 206 | if (log->next == ARRAY_SIZE(log->entry)) { |
| 207 | log->next = 0; |
| 208 | log->full = 1; |
| 209 | } |
| 210 | return e; |
| 211 | } |
| 212 | |
| 213 | struct binder_work { |
| 214 | struct list_head entry; |
| 215 | enum { |
| 216 | BINDER_WORK_TRANSACTION = 1, |
| 217 | BINDER_WORK_TRANSACTION_COMPLETE, |
| 218 | BINDER_WORK_NODE, |
| 219 | BINDER_WORK_DEAD_BINDER, |
| 220 | BINDER_WORK_DEAD_BINDER_AND_CLEAR, |
| 221 | BINDER_WORK_CLEAR_DEATH_NOTIFICATION, |
| 222 | } type; |
| 223 | }; |
| 224 | |
| 225 | struct binder_node { |
| 226 | int debug_id; |
| 227 | struct binder_work work; |
| 228 | union { |
| 229 | struct rb_node rb_node; |
| 230 | struct hlist_node dead_node; |
| 231 | }; |
| 232 | struct binder_proc *proc; |
| 233 | struct hlist_head refs; |
| 234 | int internal_strong_refs; |
| 235 | int local_weak_refs; |
| 236 | int local_strong_refs; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 237 | binder_uintptr_t ptr; |
| 238 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 239 | unsigned has_strong_ref:1; |
| 240 | unsigned pending_strong_ref:1; |
| 241 | unsigned has_weak_ref:1; |
| 242 | unsigned pending_weak_ref:1; |
| 243 | unsigned has_async_transaction:1; |
| 244 | unsigned accept_fds:1; |
| 245 | unsigned min_priority:8; |
| 246 | struct list_head async_todo; |
| 247 | }; |
| 248 | |
| 249 | struct binder_ref_death { |
| 250 | struct binder_work work; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 251 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | struct binder_ref { |
| 255 | /* Lookups needed: */ |
| 256 | /* node + proc => ref (transaction) */ |
| 257 | /* desc + proc => ref (transaction, inc/dec ref) */ |
| 258 | /* node => refs + procs (proc exit) */ |
| 259 | int debug_id; |
| 260 | struct rb_node rb_node_desc; |
| 261 | struct rb_node rb_node_node; |
| 262 | struct hlist_node node_entry; |
| 263 | struct binder_proc *proc; |
| 264 | struct binder_node *node; |
| 265 | uint32_t desc; |
| 266 | int strong; |
| 267 | int weak; |
| 268 | struct binder_ref_death *death; |
| 269 | }; |
| 270 | |
| 271 | struct binder_buffer { |
Justin P. Mattock | 217218f | 2012-01-12 06:51:31 -0800 | [diff] [blame] | 272 | struct list_head entry; /* free and allocated entries by address */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 273 | struct rb_node rb_node; /* free entry by size or allocated entry */ |
| 274 | /* by address */ |
| 275 | unsigned free:1; |
| 276 | unsigned allow_user_free:1; |
| 277 | unsigned async_transaction:1; |
| 278 | unsigned debug_id:29; |
| 279 | |
| 280 | struct binder_transaction *transaction; |
| 281 | |
| 282 | struct binder_node *target_node; |
| 283 | size_t data_size; |
| 284 | size_t offsets_size; |
| 285 | uint8_t data[0]; |
| 286 | }; |
| 287 | |
| 288 | enum binder_deferred_state { |
| 289 | BINDER_DEFERRED_PUT_FILES = 0x01, |
| 290 | BINDER_DEFERRED_FLUSH = 0x02, |
| 291 | BINDER_DEFERRED_RELEASE = 0x04, |
| 292 | }; |
| 293 | |
| 294 | struct binder_proc { |
| 295 | struct hlist_node proc_node; |
| 296 | struct rb_root threads; |
| 297 | struct rb_root nodes; |
| 298 | struct rb_root refs_by_desc; |
| 299 | struct rb_root refs_by_node; |
| 300 | int pid; |
| 301 | struct vm_area_struct *vma; |
Arve Hjønnevåg | 2a90957 | 2012-03-08 15:43:36 -0800 | [diff] [blame] | 302 | struct mm_struct *vma_vm_mm; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 303 | struct task_struct *tsk; |
| 304 | struct files_struct *files; |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 305 | struct mutex files_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 306 | struct hlist_node deferred_work_node; |
| 307 | int deferred_work; |
| 308 | void *buffer; |
| 309 | ptrdiff_t user_buffer_offset; |
| 310 | |
| 311 | struct list_head buffers; |
| 312 | struct rb_root free_buffers; |
| 313 | struct rb_root allocated_buffers; |
| 314 | size_t free_async_space; |
| 315 | |
| 316 | struct page **pages; |
| 317 | size_t buffer_size; |
| 318 | uint32_t buffer_free; |
| 319 | struct list_head todo; |
| 320 | wait_queue_head_t wait; |
| 321 | struct binder_stats stats; |
| 322 | struct list_head delivered_death; |
| 323 | int max_threads; |
| 324 | int requested_threads; |
| 325 | int requested_threads_started; |
| 326 | int ready_threads; |
| 327 | long default_priority; |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 328 | struct dentry *debugfs_entry; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | enum { |
| 332 | BINDER_LOOPER_STATE_REGISTERED = 0x01, |
| 333 | BINDER_LOOPER_STATE_ENTERED = 0x02, |
| 334 | BINDER_LOOPER_STATE_EXITED = 0x04, |
| 335 | BINDER_LOOPER_STATE_INVALID = 0x08, |
| 336 | BINDER_LOOPER_STATE_WAITING = 0x10, |
| 337 | BINDER_LOOPER_STATE_NEED_RETURN = 0x20 |
| 338 | }; |
| 339 | |
| 340 | struct binder_thread { |
| 341 | struct binder_proc *proc; |
| 342 | struct rb_node rb_node; |
| 343 | int pid; |
| 344 | int looper; |
| 345 | struct binder_transaction *transaction_stack; |
| 346 | struct list_head todo; |
| 347 | uint32_t return_error; /* Write failed, return error code in read buf */ |
| 348 | uint32_t return_error2; /* Write failed, return error code in read */ |
| 349 | /* buffer. Used when sending a reply to a dead process that */ |
| 350 | /* we are also waiting on */ |
| 351 | wait_queue_head_t wait; |
| 352 | struct binder_stats stats; |
| 353 | }; |
| 354 | |
| 355 | struct binder_transaction { |
| 356 | int debug_id; |
| 357 | struct binder_work work; |
| 358 | struct binder_thread *from; |
| 359 | struct binder_transaction *from_parent; |
| 360 | struct binder_proc *to_proc; |
| 361 | struct binder_thread *to_thread; |
| 362 | struct binder_transaction *to_parent; |
| 363 | unsigned need_reply:1; |
| 364 | /* unsigned is_dead:1; */ /* not used at the moment */ |
| 365 | |
| 366 | struct binder_buffer *buffer; |
| 367 | unsigned int code; |
| 368 | unsigned int flags; |
| 369 | long priority; |
| 370 | long saved_priority; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 371 | kuid_t sender_euid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 372 | }; |
| 373 | |
| 374 | static void |
| 375 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer); |
| 376 | |
Sachin Kamat | efde99c | 2012-08-17 16:39:36 +0530 | [diff] [blame] | 377 | static int task_get_unused_fd_flags(struct binder_proc *proc, int flags) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 378 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 379 | unsigned long rlim_cur; |
| 380 | unsigned long irqs; |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 381 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 382 | |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 383 | mutex_lock(&proc->files_lock); |
| 384 | if (proc->files == NULL) { |
| 385 | ret = -ESRCH; |
| 386 | goto err; |
| 387 | } |
| 388 | if (!lock_task_sighand(proc->tsk, &irqs)) { |
| 389 | ret = -EMFILE; |
| 390 | goto err; |
| 391 | } |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 392 | rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE); |
| 393 | unlock_task_sighand(proc->tsk, &irqs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 394 | |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 395 | ret = __alloc_fd(proc->files, 0, rlim_cur, flags); |
| 396 | err: |
| 397 | mutex_unlock(&proc->files_lock); |
| 398 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* |
| 402 | * copied from fd_install |
| 403 | */ |
| 404 | static void task_fd_install( |
| 405 | struct binder_proc *proc, unsigned int fd, struct file *file) |
| 406 | { |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 407 | mutex_lock(&proc->files_lock); |
Al Viro | f869e8a | 2012-08-15 21:06:33 -0400 | [diff] [blame] | 408 | if (proc->files) |
| 409 | __fd_install(proc->files, fd, file); |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 410 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /* |
| 414 | * copied from sys_close |
| 415 | */ |
| 416 | static long task_close_fd(struct binder_proc *proc, unsigned int fd) |
| 417 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 418 | int retval; |
| 419 | |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 420 | mutex_lock(&proc->files_lock); |
| 421 | if (proc->files == NULL) { |
| 422 | retval = -ESRCH; |
| 423 | goto err; |
| 424 | } |
Al Viro | 483ce1d | 2012-08-19 12:04:24 -0400 | [diff] [blame] | 425 | retval = __close_fd(proc->files, fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 426 | /* can't restart close syscall because file table entry was cleared */ |
| 427 | if (unlikely(retval == -ERESTARTSYS || |
| 428 | retval == -ERESTARTNOINTR || |
| 429 | retval == -ERESTARTNOHAND || |
| 430 | retval == -ERESTART_RESTARTBLOCK)) |
| 431 | retval = -EINTR; |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 432 | err: |
| 433 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 434 | return retval; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 435 | } |
| 436 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 437 | static inline void binder_lock(const char *tag) |
| 438 | { |
| 439 | trace_binder_lock(tag); |
| 440 | mutex_lock(&binder_main_lock); |
| 441 | trace_binder_locked(tag); |
| 442 | } |
| 443 | |
| 444 | static inline void binder_unlock(const char *tag) |
| 445 | { |
| 446 | trace_binder_unlock(tag); |
| 447 | mutex_unlock(&binder_main_lock); |
| 448 | } |
| 449 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 450 | static void binder_set_nice(long nice) |
| 451 | { |
| 452 | long min_nice; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 453 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 454 | if (can_nice(current, nice)) { |
| 455 | set_user_nice(current, nice); |
| 456 | return; |
| 457 | } |
Dongsheng Yang | 7aa2c01 | 2014-05-08 18:33:49 +0900 | [diff] [blame] | 458 | min_nice = rlimit_to_nice(current->signal->rlim[RLIMIT_NICE].rlim_cur); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 459 | binder_debug(BINDER_DEBUG_PRIORITY_CAP, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 460 | "%d: nice value %ld not allowed use %ld instead\n", |
| 461 | current->pid, nice, min_nice); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 462 | set_user_nice(current, min_nice); |
Dongsheng Yang | 8698a74 | 2014-03-11 18:09:12 +0800 | [diff] [blame] | 463 | if (min_nice <= MAX_NICE) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 464 | return; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 465 | binder_user_error("%d RLIMIT_NICE not set\n", current->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static size_t binder_buffer_size(struct binder_proc *proc, |
| 469 | struct binder_buffer *buffer) |
| 470 | { |
| 471 | if (list_is_last(&buffer->entry, &proc->buffers)) |
| 472 | return proc->buffer + proc->buffer_size - (void *)buffer->data; |
Karthik Nayak | 7873311 | 2014-06-21 20:23:16 +0530 | [diff] [blame] | 473 | return (size_t)list_entry(buffer->entry.next, |
| 474 | struct binder_buffer, entry) - (size_t)buffer->data; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static void binder_insert_free_buffer(struct binder_proc *proc, |
| 478 | struct binder_buffer *new_buffer) |
| 479 | { |
| 480 | struct rb_node **p = &proc->free_buffers.rb_node; |
| 481 | struct rb_node *parent = NULL; |
| 482 | struct binder_buffer *buffer; |
| 483 | size_t buffer_size; |
| 484 | size_t new_buffer_size; |
| 485 | |
| 486 | BUG_ON(!new_buffer->free); |
| 487 | |
| 488 | new_buffer_size = binder_buffer_size(proc, new_buffer); |
| 489 | |
| 490 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 491 | "%d: add free buffer, size %zd, at %pK\n", |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 492 | proc->pid, new_buffer_size, new_buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 493 | |
| 494 | while (*p) { |
| 495 | parent = *p; |
| 496 | buffer = rb_entry(parent, struct binder_buffer, rb_node); |
| 497 | BUG_ON(!buffer->free); |
| 498 | |
| 499 | buffer_size = binder_buffer_size(proc, buffer); |
| 500 | |
| 501 | if (new_buffer_size < buffer_size) |
| 502 | p = &parent->rb_left; |
| 503 | else |
| 504 | p = &parent->rb_right; |
| 505 | } |
| 506 | rb_link_node(&new_buffer->rb_node, parent, p); |
| 507 | rb_insert_color(&new_buffer->rb_node, &proc->free_buffers); |
| 508 | } |
| 509 | |
| 510 | static void binder_insert_allocated_buffer(struct binder_proc *proc, |
| 511 | struct binder_buffer *new_buffer) |
| 512 | { |
| 513 | struct rb_node **p = &proc->allocated_buffers.rb_node; |
| 514 | struct rb_node *parent = NULL; |
| 515 | struct binder_buffer *buffer; |
| 516 | |
| 517 | BUG_ON(new_buffer->free); |
| 518 | |
| 519 | while (*p) { |
| 520 | parent = *p; |
| 521 | buffer = rb_entry(parent, struct binder_buffer, rb_node); |
| 522 | BUG_ON(buffer->free); |
| 523 | |
| 524 | if (new_buffer < buffer) |
| 525 | p = &parent->rb_left; |
| 526 | else if (new_buffer > buffer) |
| 527 | p = &parent->rb_right; |
| 528 | else |
| 529 | BUG(); |
| 530 | } |
| 531 | rb_link_node(&new_buffer->rb_node, parent, p); |
| 532 | rb_insert_color(&new_buffer->rb_node, &proc->allocated_buffers); |
| 533 | } |
| 534 | |
| 535 | static struct binder_buffer *binder_buffer_lookup(struct binder_proc *proc, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 536 | uintptr_t user_ptr) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 537 | { |
| 538 | struct rb_node *n = proc->allocated_buffers.rb_node; |
| 539 | struct binder_buffer *buffer; |
| 540 | struct binder_buffer *kern_ptr; |
| 541 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 542 | kern_ptr = (struct binder_buffer *)(user_ptr - proc->user_buffer_offset |
| 543 | - offsetof(struct binder_buffer, data)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 544 | |
| 545 | while (n) { |
| 546 | buffer = rb_entry(n, struct binder_buffer, rb_node); |
| 547 | BUG_ON(buffer->free); |
| 548 | |
| 549 | if (kern_ptr < buffer) |
| 550 | n = n->rb_left; |
| 551 | else if (kern_ptr > buffer) |
| 552 | n = n->rb_right; |
| 553 | else |
| 554 | return buffer; |
| 555 | } |
| 556 | return NULL; |
| 557 | } |
| 558 | |
| 559 | static int binder_update_page_range(struct binder_proc *proc, int allocate, |
| 560 | void *start, void *end, |
| 561 | struct vm_area_struct *vma) |
| 562 | { |
| 563 | void *page_addr; |
| 564 | unsigned long user_page_addr; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 565 | struct page **page; |
| 566 | struct mm_struct *mm; |
| 567 | |
| 568 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 569 | "%d: %s pages %pK-%pK\n", proc->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 570 | allocate ? "allocate" : "free", start, end); |
| 571 | |
| 572 | if (end <= start) |
| 573 | return 0; |
| 574 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 575 | trace_binder_update_page_range(proc, allocate, start, end); |
| 576 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 577 | if (vma) |
| 578 | mm = NULL; |
| 579 | else |
| 580 | mm = get_task_mm(proc->tsk); |
| 581 | |
| 582 | if (mm) { |
| 583 | down_write(&mm->mmap_sem); |
| 584 | vma = proc->vma; |
Arve Hjønnevåg | 2a90957 | 2012-03-08 15:43:36 -0800 | [diff] [blame] | 585 | if (vma && mm != proc->vma_vm_mm) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 586 | pr_err("%d: vma mm and task mm mismatch\n", |
Arve Hjønnevåg | bd1eff9 | 2012-02-01 15:29:13 -0800 | [diff] [blame] | 587 | proc->pid); |
| 588 | vma = NULL; |
| 589 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | if (allocate == 0) |
| 593 | goto free_range; |
| 594 | |
| 595 | if (vma == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 596 | pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n", |
| 597 | proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 598 | goto err_no_vma; |
| 599 | } |
| 600 | |
| 601 | for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) { |
| 602 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 603 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 604 | page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE]; |
| 605 | |
| 606 | BUG_ON(*page); |
Arve Hjønnevåg | 585650d | 2012-10-16 15:29:55 -0700 | [diff] [blame] | 607 | *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 608 | if (*page == NULL) { |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 609 | pr_err("%d: binder_alloc_buf failed for page at %pK\n", |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 610 | proc->pid, page_addr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 611 | goto err_alloc_page_failed; |
| 612 | } |
Andrey Ryabinin | f4c72c7 | 2015-02-27 20:44:21 +0300 | [diff] [blame] | 613 | ret = map_kernel_range_noflush((unsigned long)page_addr, |
| 614 | PAGE_SIZE, PAGE_KERNEL, page); |
| 615 | flush_cache_vmap((unsigned long)page_addr, |
| 616 | (unsigned long)page_addr + PAGE_SIZE); |
| 617 | if (ret != 1) { |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 618 | pr_err("%d: binder_alloc_buf failed to map page at %pK in kernel\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 619 | proc->pid, page_addr); |
| 620 | goto err_map_kernel_failed; |
| 621 | } |
| 622 | user_page_addr = |
| 623 | (uintptr_t)page_addr + proc->user_buffer_offset; |
| 624 | ret = vm_insert_page(vma, user_page_addr, page[0]); |
| 625 | if (ret) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 626 | pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 627 | proc->pid, user_page_addr); |
| 628 | goto err_vm_insert_page_failed; |
| 629 | } |
| 630 | /* vm_insert_page does not seem to increment the refcount */ |
| 631 | } |
| 632 | if (mm) { |
| 633 | up_write(&mm->mmap_sem); |
| 634 | mmput(mm); |
| 635 | } |
| 636 | return 0; |
| 637 | |
| 638 | free_range: |
| 639 | for (page_addr = end - PAGE_SIZE; page_addr >= start; |
| 640 | page_addr -= PAGE_SIZE) { |
| 641 | page = &proc->pages[(page_addr - proc->buffer) / PAGE_SIZE]; |
| 642 | if (vma) |
| 643 | zap_page_range(vma, (uintptr_t)page_addr + |
| 644 | proc->user_buffer_offset, PAGE_SIZE, NULL); |
| 645 | err_vm_insert_page_failed: |
| 646 | unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE); |
| 647 | err_map_kernel_failed: |
| 648 | __free_page(*page); |
| 649 | *page = NULL; |
| 650 | err_alloc_page_failed: |
| 651 | ; |
| 652 | } |
| 653 | err_no_vma: |
| 654 | if (mm) { |
| 655 | up_write(&mm->mmap_sem); |
| 656 | mmput(mm); |
| 657 | } |
| 658 | return -ENOMEM; |
| 659 | } |
| 660 | |
| 661 | static struct binder_buffer *binder_alloc_buf(struct binder_proc *proc, |
| 662 | size_t data_size, |
| 663 | size_t offsets_size, int is_async) |
| 664 | { |
| 665 | struct rb_node *n = proc->free_buffers.rb_node; |
| 666 | struct binder_buffer *buffer; |
| 667 | size_t buffer_size; |
| 668 | struct rb_node *best_fit = NULL; |
| 669 | void *has_page_addr; |
| 670 | void *end_page_addr; |
| 671 | size_t size; |
| 672 | |
| 673 | if (proc->vma == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 674 | pr_err("%d: binder_alloc_buf, no vma\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 675 | proc->pid); |
| 676 | return NULL; |
| 677 | } |
| 678 | |
| 679 | size = ALIGN(data_size, sizeof(void *)) + |
| 680 | ALIGN(offsets_size, sizeof(void *)); |
| 681 | |
| 682 | if (size < data_size || size < offsets_size) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 683 | binder_user_error("%d: got transaction with invalid size %zd-%zd\n", |
| 684 | proc->pid, data_size, offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 685 | return NULL; |
| 686 | } |
| 687 | |
| 688 | if (is_async && |
| 689 | proc->free_async_space < size + sizeof(struct binder_buffer)) { |
| 690 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 691 | "%d: binder_alloc_buf size %zd failed, no async space left\n", |
| 692 | proc->pid, size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 693 | return NULL; |
| 694 | } |
| 695 | |
| 696 | while (n) { |
| 697 | buffer = rb_entry(n, struct binder_buffer, rb_node); |
| 698 | BUG_ON(!buffer->free); |
| 699 | buffer_size = binder_buffer_size(proc, buffer); |
| 700 | |
| 701 | if (size < buffer_size) { |
| 702 | best_fit = n; |
| 703 | n = n->rb_left; |
| 704 | } else if (size > buffer_size) |
| 705 | n = n->rb_right; |
| 706 | else { |
| 707 | best_fit = n; |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | if (best_fit == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 712 | pr_err("%d: binder_alloc_buf size %zd failed, no address space\n", |
| 713 | proc->pid, size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 714 | return NULL; |
| 715 | } |
| 716 | if (n == NULL) { |
| 717 | buffer = rb_entry(best_fit, struct binder_buffer, rb_node); |
| 718 | buffer_size = binder_buffer_size(proc, buffer); |
| 719 | } |
| 720 | |
| 721 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 722 | "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n", |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 723 | proc->pid, size, buffer, buffer_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 724 | |
| 725 | has_page_addr = |
| 726 | (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK); |
| 727 | if (n == NULL) { |
| 728 | if (size + sizeof(struct binder_buffer) + 4 >= buffer_size) |
| 729 | buffer_size = size; /* no room for other buffers */ |
| 730 | else |
| 731 | buffer_size = size + sizeof(struct binder_buffer); |
| 732 | } |
| 733 | end_page_addr = |
| 734 | (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size); |
| 735 | if (end_page_addr > has_page_addr) |
| 736 | end_page_addr = has_page_addr; |
| 737 | if (binder_update_page_range(proc, 1, |
| 738 | (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL)) |
| 739 | return NULL; |
| 740 | |
| 741 | rb_erase(best_fit, &proc->free_buffers); |
| 742 | buffer->free = 0; |
| 743 | binder_insert_allocated_buffer(proc, buffer); |
| 744 | if (buffer_size != size) { |
| 745 | struct binder_buffer *new_buffer = (void *)buffer->data + size; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 746 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 747 | list_add(&new_buffer->entry, &buffer->entry); |
| 748 | new_buffer->free = 1; |
| 749 | binder_insert_free_buffer(proc, new_buffer); |
| 750 | } |
| 751 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 752 | "%d: binder_alloc_buf size %zd got %pK\n", |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 753 | proc->pid, size, buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 754 | buffer->data_size = data_size; |
| 755 | buffer->offsets_size = offsets_size; |
| 756 | buffer->async_transaction = is_async; |
| 757 | if (is_async) { |
| 758 | proc->free_async_space -= size + sizeof(struct binder_buffer); |
| 759 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 760 | "%d: binder_alloc_buf size %zd async free %zd\n", |
| 761 | proc->pid, size, proc->free_async_space); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | return buffer; |
| 765 | } |
| 766 | |
| 767 | static void *buffer_start_page(struct binder_buffer *buffer) |
| 768 | { |
| 769 | return (void *)((uintptr_t)buffer & PAGE_MASK); |
| 770 | } |
| 771 | |
| 772 | static void *buffer_end_page(struct binder_buffer *buffer) |
| 773 | { |
| 774 | return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK); |
| 775 | } |
| 776 | |
| 777 | static void binder_delete_free_buffer(struct binder_proc *proc, |
| 778 | struct binder_buffer *buffer) |
| 779 | { |
| 780 | struct binder_buffer *prev, *next = NULL; |
| 781 | int free_page_end = 1; |
| 782 | int free_page_start = 1; |
| 783 | |
| 784 | BUG_ON(proc->buffers.next == &buffer->entry); |
| 785 | prev = list_entry(buffer->entry.prev, struct binder_buffer, entry); |
| 786 | BUG_ON(!prev->free); |
| 787 | if (buffer_end_page(prev) == buffer_start_page(buffer)) { |
| 788 | free_page_start = 0; |
| 789 | if (buffer_end_page(prev) == buffer_end_page(buffer)) |
| 790 | free_page_end = 0; |
| 791 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 792 | "%d: merge free, buffer %pK share page with %pK\n", |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 793 | proc->pid, buffer, prev); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | if (!list_is_last(&buffer->entry, &proc->buffers)) { |
| 797 | next = list_entry(buffer->entry.next, |
| 798 | struct binder_buffer, entry); |
| 799 | if (buffer_start_page(next) == buffer_end_page(buffer)) { |
| 800 | free_page_end = 0; |
| 801 | if (buffer_start_page(next) == |
| 802 | buffer_start_page(buffer)) |
| 803 | free_page_start = 0; |
| 804 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 805 | "%d: merge free, buffer %pK share page with %pK\n", |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 806 | proc->pid, buffer, prev); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | list_del(&buffer->entry); |
| 810 | if (free_page_start || free_page_end) { |
| 811 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 812 | "%d: merge free, buffer %pK do not share page%s%s with %pK or %pK\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 813 | proc->pid, buffer, free_page_start ? "" : " end", |
| 814 | free_page_end ? "" : " start", prev, next); |
| 815 | binder_update_page_range(proc, 0, free_page_start ? |
| 816 | buffer_start_page(buffer) : buffer_end_page(buffer), |
| 817 | (free_page_end ? buffer_end_page(buffer) : |
| 818 | buffer_start_page(buffer)) + PAGE_SIZE, NULL); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | static void binder_free_buf(struct binder_proc *proc, |
| 823 | struct binder_buffer *buffer) |
| 824 | { |
| 825 | size_t size, buffer_size; |
| 826 | |
| 827 | buffer_size = binder_buffer_size(proc, buffer); |
| 828 | |
| 829 | size = ALIGN(buffer->data_size, sizeof(void *)) + |
| 830 | ALIGN(buffer->offsets_size, sizeof(void *)); |
| 831 | |
| 832 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 833 | "%d: binder_free_buf %pK size %zd buffer_size %zd\n", |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 834 | proc->pid, buffer, size, buffer_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 835 | |
| 836 | BUG_ON(buffer->free); |
| 837 | BUG_ON(size > buffer_size); |
| 838 | BUG_ON(buffer->transaction != NULL); |
| 839 | BUG_ON((void *)buffer < proc->buffer); |
| 840 | BUG_ON((void *)buffer > proc->buffer + proc->buffer_size); |
| 841 | |
| 842 | if (buffer->async_transaction) { |
| 843 | proc->free_async_space += size + sizeof(struct binder_buffer); |
| 844 | |
| 845 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 846 | "%d: binder_free_buf size %zd async free %zd\n", |
| 847 | proc->pid, size, proc->free_async_space); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | binder_update_page_range(proc, 0, |
| 851 | (void *)PAGE_ALIGN((uintptr_t)buffer->data), |
| 852 | (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK), |
| 853 | NULL); |
| 854 | rb_erase(&buffer->rb_node, &proc->allocated_buffers); |
| 855 | buffer->free = 1; |
| 856 | if (!list_is_last(&buffer->entry, &proc->buffers)) { |
| 857 | struct binder_buffer *next = list_entry(buffer->entry.next, |
| 858 | struct binder_buffer, entry); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 859 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 860 | if (next->free) { |
| 861 | rb_erase(&next->rb_node, &proc->free_buffers); |
| 862 | binder_delete_free_buffer(proc, next); |
| 863 | } |
| 864 | } |
| 865 | if (proc->buffers.next != &buffer->entry) { |
| 866 | struct binder_buffer *prev = list_entry(buffer->entry.prev, |
| 867 | struct binder_buffer, entry); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 868 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 869 | if (prev->free) { |
| 870 | binder_delete_free_buffer(proc, buffer); |
| 871 | rb_erase(&prev->rb_node, &proc->free_buffers); |
| 872 | buffer = prev; |
| 873 | } |
| 874 | } |
| 875 | binder_insert_free_buffer(proc, buffer); |
| 876 | } |
| 877 | |
| 878 | static struct binder_node *binder_get_node(struct binder_proc *proc, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 879 | binder_uintptr_t ptr) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 880 | { |
| 881 | struct rb_node *n = proc->nodes.rb_node; |
| 882 | struct binder_node *node; |
| 883 | |
| 884 | while (n) { |
| 885 | node = rb_entry(n, struct binder_node, rb_node); |
| 886 | |
| 887 | if (ptr < node->ptr) |
| 888 | n = n->rb_left; |
| 889 | else if (ptr > node->ptr) |
| 890 | n = n->rb_right; |
| 891 | else |
| 892 | return node; |
| 893 | } |
| 894 | return NULL; |
| 895 | } |
| 896 | |
| 897 | static struct binder_node *binder_new_node(struct binder_proc *proc, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 898 | binder_uintptr_t ptr, |
| 899 | binder_uintptr_t cookie) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 900 | { |
| 901 | struct rb_node **p = &proc->nodes.rb_node; |
| 902 | struct rb_node *parent = NULL; |
| 903 | struct binder_node *node; |
| 904 | |
| 905 | while (*p) { |
| 906 | parent = *p; |
| 907 | node = rb_entry(parent, struct binder_node, rb_node); |
| 908 | |
| 909 | if (ptr < node->ptr) |
| 910 | p = &(*p)->rb_left; |
| 911 | else if (ptr > node->ptr) |
| 912 | p = &(*p)->rb_right; |
| 913 | else |
| 914 | return NULL; |
| 915 | } |
| 916 | |
| 917 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 918 | if (node == NULL) |
| 919 | return NULL; |
| 920 | binder_stats_created(BINDER_STAT_NODE); |
| 921 | rb_link_node(&node->rb_node, parent, p); |
| 922 | rb_insert_color(&node->rb_node, &proc->nodes); |
| 923 | node->debug_id = ++binder_last_id; |
| 924 | node->proc = proc; |
| 925 | node->ptr = ptr; |
| 926 | node->cookie = cookie; |
| 927 | node->work.type = BINDER_WORK_NODE; |
| 928 | INIT_LIST_HEAD(&node->work.entry); |
| 929 | INIT_LIST_HEAD(&node->async_todo); |
| 930 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 931 | "%d:%d node %d u%016llx c%016llx created\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 932 | proc->pid, current->pid, node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 933 | (u64)node->ptr, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 934 | return node; |
| 935 | } |
| 936 | |
| 937 | static int binder_inc_node(struct binder_node *node, int strong, int internal, |
| 938 | struct list_head *target_list) |
| 939 | { |
| 940 | if (strong) { |
| 941 | if (internal) { |
| 942 | if (target_list == NULL && |
| 943 | node->internal_strong_refs == 0 && |
| 944 | !(node == binder_context_mgr_node && |
| 945 | node->has_strong_ref)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 946 | pr_err("invalid inc strong node for %d\n", |
| 947 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 948 | return -EINVAL; |
| 949 | } |
| 950 | node->internal_strong_refs++; |
| 951 | } else |
| 952 | node->local_strong_refs++; |
| 953 | if (!node->has_strong_ref && target_list) { |
| 954 | list_del_init(&node->work.entry); |
| 955 | list_add_tail(&node->work.entry, target_list); |
| 956 | } |
| 957 | } else { |
| 958 | if (!internal) |
| 959 | node->local_weak_refs++; |
| 960 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { |
| 961 | if (target_list == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 962 | pr_err("invalid inc weak node for %d\n", |
| 963 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 964 | return -EINVAL; |
| 965 | } |
| 966 | list_add_tail(&node->work.entry, target_list); |
| 967 | } |
| 968 | } |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | static int binder_dec_node(struct binder_node *node, int strong, int internal) |
| 973 | { |
| 974 | if (strong) { |
| 975 | if (internal) |
| 976 | node->internal_strong_refs--; |
| 977 | else |
| 978 | node->local_strong_refs--; |
| 979 | if (node->local_strong_refs || node->internal_strong_refs) |
| 980 | return 0; |
| 981 | } else { |
| 982 | if (!internal) |
| 983 | node->local_weak_refs--; |
| 984 | if (node->local_weak_refs || !hlist_empty(&node->refs)) |
| 985 | return 0; |
| 986 | } |
| 987 | if (node->proc && (node->has_strong_ref || node->has_weak_ref)) { |
| 988 | if (list_empty(&node->work.entry)) { |
| 989 | list_add_tail(&node->work.entry, &node->proc->todo); |
| 990 | wake_up_interruptible(&node->proc->wait); |
| 991 | } |
| 992 | } else { |
| 993 | if (hlist_empty(&node->refs) && !node->local_strong_refs && |
| 994 | !node->local_weak_refs) { |
| 995 | list_del_init(&node->work.entry); |
| 996 | if (node->proc) { |
| 997 | rb_erase(&node->rb_node, &node->proc->nodes); |
| 998 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 999 | "refless node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1000 | node->debug_id); |
| 1001 | } else { |
| 1002 | hlist_del(&node->dead_node); |
| 1003 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1004 | "dead node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1005 | node->debug_id); |
| 1006 | } |
| 1007 | kfree(node); |
| 1008 | binder_stats_deleted(BINDER_STAT_NODE); |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | static struct binder_ref *binder_get_ref(struct binder_proc *proc, |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1017 | u32 desc, bool need_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1018 | { |
| 1019 | struct rb_node *n = proc->refs_by_desc.rb_node; |
| 1020 | struct binder_ref *ref; |
| 1021 | |
| 1022 | while (n) { |
| 1023 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1024 | |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1025 | if (desc < ref->desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1026 | n = n->rb_left; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1027 | } else if (desc > ref->desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1028 | n = n->rb_right; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1029 | } else if (need_strong_ref && !ref->strong) { |
| 1030 | binder_user_error("tried to use weak ref as strong ref\n"); |
| 1031 | return NULL; |
| 1032 | } else { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1033 | return ref; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1034 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1035 | } |
| 1036 | return NULL; |
| 1037 | } |
| 1038 | |
| 1039 | static struct binder_ref *binder_get_ref_for_node(struct binder_proc *proc, |
| 1040 | struct binder_node *node) |
| 1041 | { |
| 1042 | struct rb_node *n; |
| 1043 | struct rb_node **p = &proc->refs_by_node.rb_node; |
| 1044 | struct rb_node *parent = NULL; |
| 1045 | struct binder_ref *ref, *new_ref; |
| 1046 | |
| 1047 | while (*p) { |
| 1048 | parent = *p; |
| 1049 | ref = rb_entry(parent, struct binder_ref, rb_node_node); |
| 1050 | |
| 1051 | if (node < ref->node) |
| 1052 | p = &(*p)->rb_left; |
| 1053 | else if (node > ref->node) |
| 1054 | p = &(*p)->rb_right; |
| 1055 | else |
| 1056 | return ref; |
| 1057 | } |
| 1058 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
| 1059 | if (new_ref == NULL) |
| 1060 | return NULL; |
| 1061 | binder_stats_created(BINDER_STAT_REF); |
| 1062 | new_ref->debug_id = ++binder_last_id; |
| 1063 | new_ref->proc = proc; |
| 1064 | new_ref->node = node; |
| 1065 | rb_link_node(&new_ref->rb_node_node, parent, p); |
| 1066 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); |
| 1067 | |
| 1068 | new_ref->desc = (node == binder_context_mgr_node) ? 0 : 1; |
| 1069 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 1070 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1071 | if (ref->desc > new_ref->desc) |
| 1072 | break; |
| 1073 | new_ref->desc = ref->desc + 1; |
| 1074 | } |
| 1075 | |
| 1076 | p = &proc->refs_by_desc.rb_node; |
| 1077 | while (*p) { |
| 1078 | parent = *p; |
| 1079 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); |
| 1080 | |
| 1081 | if (new_ref->desc < ref->desc) |
| 1082 | p = &(*p)->rb_left; |
| 1083 | else if (new_ref->desc > ref->desc) |
| 1084 | p = &(*p)->rb_right; |
| 1085 | else |
| 1086 | BUG(); |
| 1087 | } |
| 1088 | rb_link_node(&new_ref->rb_node_desc, parent, p); |
| 1089 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); |
| 1090 | if (node) { |
| 1091 | hlist_add_head(&new_ref->node_entry, &node->refs); |
| 1092 | |
| 1093 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1094 | "%d new ref %d desc %d for node %d\n", |
| 1095 | proc->pid, new_ref->debug_id, new_ref->desc, |
| 1096 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1097 | } else { |
| 1098 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1099 | "%d new ref %d desc %d for dead node\n", |
| 1100 | proc->pid, new_ref->debug_id, new_ref->desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1101 | } |
| 1102 | return new_ref; |
| 1103 | } |
| 1104 | |
| 1105 | static void binder_delete_ref(struct binder_ref *ref) |
| 1106 | { |
| 1107 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1108 | "%d delete ref %d desc %d for node %d\n", |
| 1109 | ref->proc->pid, ref->debug_id, ref->desc, |
| 1110 | ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1111 | |
| 1112 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); |
| 1113 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); |
| 1114 | if (ref->strong) |
| 1115 | binder_dec_node(ref->node, 1, 1); |
| 1116 | hlist_del(&ref->node_entry); |
| 1117 | binder_dec_node(ref->node, 0, 1); |
| 1118 | if (ref->death) { |
| 1119 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1120 | "%d delete ref %d desc %d has death notification\n", |
| 1121 | ref->proc->pid, ref->debug_id, ref->desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1122 | list_del(&ref->death->work.entry); |
| 1123 | kfree(ref->death); |
| 1124 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 1125 | } |
| 1126 | kfree(ref); |
| 1127 | binder_stats_deleted(BINDER_STAT_REF); |
| 1128 | } |
| 1129 | |
| 1130 | static int binder_inc_ref(struct binder_ref *ref, int strong, |
| 1131 | struct list_head *target_list) |
| 1132 | { |
| 1133 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1134 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1135 | if (strong) { |
| 1136 | if (ref->strong == 0) { |
| 1137 | ret = binder_inc_node(ref->node, 1, 1, target_list); |
| 1138 | if (ret) |
| 1139 | return ret; |
| 1140 | } |
| 1141 | ref->strong++; |
| 1142 | } else { |
| 1143 | if (ref->weak == 0) { |
| 1144 | ret = binder_inc_node(ref->node, 0, 1, target_list); |
| 1145 | if (ret) |
| 1146 | return ret; |
| 1147 | } |
| 1148 | ref->weak++; |
| 1149 | } |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | |
| 1154 | static int binder_dec_ref(struct binder_ref *ref, int strong) |
| 1155 | { |
| 1156 | if (strong) { |
| 1157 | if (ref->strong == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1158 | binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1159 | ref->proc->pid, ref->debug_id, |
| 1160 | ref->desc, ref->strong, ref->weak); |
| 1161 | return -EINVAL; |
| 1162 | } |
| 1163 | ref->strong--; |
| 1164 | if (ref->strong == 0) { |
| 1165 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1166 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1167 | ret = binder_dec_node(ref->node, strong, 1); |
| 1168 | if (ret) |
| 1169 | return ret; |
| 1170 | } |
| 1171 | } else { |
| 1172 | if (ref->weak == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1173 | binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1174 | ref->proc->pid, ref->debug_id, |
| 1175 | ref->desc, ref->strong, ref->weak); |
| 1176 | return -EINVAL; |
| 1177 | } |
| 1178 | ref->weak--; |
| 1179 | } |
| 1180 | if (ref->strong == 0 && ref->weak == 0) |
| 1181 | binder_delete_ref(ref); |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | static void binder_pop_transaction(struct binder_thread *target_thread, |
| 1186 | struct binder_transaction *t) |
| 1187 | { |
| 1188 | if (target_thread) { |
| 1189 | BUG_ON(target_thread->transaction_stack != t); |
| 1190 | BUG_ON(target_thread->transaction_stack->from != target_thread); |
| 1191 | target_thread->transaction_stack = |
| 1192 | target_thread->transaction_stack->from_parent; |
| 1193 | t->from = NULL; |
| 1194 | } |
| 1195 | t->need_reply = 0; |
| 1196 | if (t->buffer) |
| 1197 | t->buffer->transaction = NULL; |
| 1198 | kfree(t); |
| 1199 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 1200 | } |
| 1201 | |
| 1202 | static void binder_send_failed_reply(struct binder_transaction *t, |
| 1203 | uint32_t error_code) |
| 1204 | { |
| 1205 | struct binder_thread *target_thread; |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 1206 | struct binder_transaction *next; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1207 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1208 | BUG_ON(t->flags & TF_ONE_WAY); |
| 1209 | while (1) { |
| 1210 | target_thread = t->from; |
| 1211 | if (target_thread) { |
| 1212 | if (target_thread->return_error != BR_OK && |
| 1213 | target_thread->return_error2 == BR_OK) { |
| 1214 | target_thread->return_error2 = |
| 1215 | target_thread->return_error; |
| 1216 | target_thread->return_error = BR_OK; |
| 1217 | } |
| 1218 | if (target_thread->return_error == BR_OK) { |
| 1219 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1220 | "send failed reply for transaction %d to %d:%d\n", |
William Panlener | 0232a42 | 2014-09-03 22:44:03 -0500 | [diff] [blame] | 1221 | t->debug_id, |
| 1222 | target_thread->proc->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1223 | target_thread->pid); |
| 1224 | |
| 1225 | binder_pop_transaction(target_thread, t); |
| 1226 | target_thread->return_error = error_code; |
| 1227 | wake_up_interruptible(&target_thread->wait); |
| 1228 | } else { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1229 | pr_err("reply failed, target thread, %d:%d, has error code %d already\n", |
| 1230 | target_thread->proc->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1231 | target_thread->pid, |
| 1232 | target_thread->return_error); |
| 1233 | } |
| 1234 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1235 | } |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 1236 | next = t->from_parent; |
| 1237 | |
| 1238 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 1239 | "send failed reply for transaction %d, target dead\n", |
| 1240 | t->debug_id); |
| 1241 | |
| 1242 | binder_pop_transaction(target_thread, t); |
| 1243 | if (next == NULL) { |
| 1244 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 1245 | "reply failed, no target thread at root\n"); |
| 1246 | return; |
| 1247 | } |
| 1248 | t = next; |
| 1249 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 1250 | "reply failed, no target thread -- retry %d\n", |
| 1251 | t->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | static void binder_transaction_buffer_release(struct binder_proc *proc, |
| 1256 | struct binder_buffer *buffer, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1257 | binder_size_t *failed_at) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1258 | { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1259 | binder_size_t *offp, *off_end; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1260 | int debug_id = buffer->debug_id; |
| 1261 | |
| 1262 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | 6f3433c | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 1263 | "%d buffer release %d, size %zd-%zd, failed at %pK\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1264 | proc->pid, buffer->debug_id, |
| 1265 | buffer->data_size, buffer->offsets_size, failed_at); |
| 1266 | |
| 1267 | if (buffer->target_node) |
| 1268 | binder_dec_node(buffer->target_node, 1, 0); |
| 1269 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1270 | offp = (binder_size_t *)(buffer->data + |
| 1271 | ALIGN(buffer->data_size, sizeof(void *))); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1272 | if (failed_at) |
| 1273 | off_end = failed_at; |
| 1274 | else |
| 1275 | off_end = (void *)offp + buffer->offsets_size; |
| 1276 | for (; offp < off_end; offp++) { |
| 1277 | struct flat_binder_object *fp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1278 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1279 | if (*offp > buffer->data_size - sizeof(*fp) || |
| 1280 | buffer->data_size < sizeof(*fp) || |
Serban Constantinescu | ec35e85 | 2013-07-04 10:54:46 +0100 | [diff] [blame] | 1281 | !IS_ALIGNED(*offp, sizeof(u32))) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1282 | pr_err("transaction release %d bad offset %lld, size %zd\n", |
| 1283 | debug_id, (u64)*offp, buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1284 | continue; |
| 1285 | } |
| 1286 | fp = (struct flat_binder_object *)(buffer->data + *offp); |
| 1287 | switch (fp->type) { |
| 1288 | case BINDER_TYPE_BINDER: |
| 1289 | case BINDER_TYPE_WEAK_BINDER: { |
| 1290 | struct binder_node *node = binder_get_node(proc, fp->binder); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1291 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1292 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1293 | pr_err("transaction release %d bad node %016llx\n", |
| 1294 | debug_id, (u64)fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1295 | break; |
| 1296 | } |
| 1297 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1298 | " node %d u%016llx\n", |
| 1299 | node->debug_id, (u64)node->ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1300 | binder_dec_node(node, fp->type == BINDER_TYPE_BINDER, 0); |
| 1301 | } break; |
| 1302 | case BINDER_TYPE_HANDLE: |
| 1303 | case BINDER_TYPE_WEAK_HANDLE: { |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1304 | struct binder_ref *ref; |
| 1305 | |
| 1306 | ref = binder_get_ref(proc, fp->handle, |
| 1307 | fp->type == BINDER_TYPE_HANDLE); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1308 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1309 | if (ref == NULL) { |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 1310 | pr_err("transaction release %d bad handle %d\n", |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1311 | debug_id, fp->handle); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1312 | break; |
| 1313 | } |
| 1314 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 1315 | " ref %d desc %d (node %d)\n", |
| 1316 | ref->debug_id, ref->desc, ref->node->debug_id); |
| 1317 | binder_dec_ref(ref, fp->type == BINDER_TYPE_HANDLE); |
| 1318 | } break; |
| 1319 | |
| 1320 | case BINDER_TYPE_FD: |
| 1321 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 1322 | " fd %d\n", fp->handle); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1323 | if (failed_at) |
| 1324 | task_close_fd(proc, fp->handle); |
| 1325 | break; |
| 1326 | |
| 1327 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 1328 | pr_err("transaction release %d bad object type %x\n", |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1329 | debug_id, fp->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1330 | break; |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | static void binder_transaction(struct binder_proc *proc, |
| 1336 | struct binder_thread *thread, |
| 1337 | struct binder_transaction_data *tr, int reply) |
| 1338 | { |
| 1339 | struct binder_transaction *t; |
| 1340 | struct binder_work *tcomplete; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1341 | binder_size_t *offp, *off_end; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 1342 | binder_size_t off_min; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1343 | struct binder_proc *target_proc; |
| 1344 | struct binder_thread *target_thread = NULL; |
| 1345 | struct binder_node *target_node = NULL; |
| 1346 | struct list_head *target_list; |
| 1347 | wait_queue_head_t *target_wait; |
| 1348 | struct binder_transaction *in_reply_to = NULL; |
| 1349 | struct binder_transaction_log_entry *e; |
| 1350 | uint32_t return_error; |
| 1351 | |
| 1352 | e = binder_transaction_log_add(&binder_transaction_log); |
| 1353 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); |
| 1354 | e->from_proc = proc->pid; |
| 1355 | e->from_thread = thread->pid; |
| 1356 | e->target_handle = tr->target.handle; |
| 1357 | e->data_size = tr->data_size; |
| 1358 | e->offsets_size = tr->offsets_size; |
| 1359 | |
| 1360 | if (reply) { |
| 1361 | in_reply_to = thread->transaction_stack; |
| 1362 | if (in_reply_to == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1363 | binder_user_error("%d:%d got reply transaction with no transaction stack\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1364 | proc->pid, thread->pid); |
| 1365 | return_error = BR_FAILED_REPLY; |
| 1366 | goto err_empty_call_stack; |
| 1367 | } |
| 1368 | binder_set_nice(in_reply_to->saved_priority); |
| 1369 | if (in_reply_to->to_thread != thread) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1370 | binder_user_error("%d:%d got reply transaction with bad transaction stack, transaction %d has target %d:%d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1371 | proc->pid, thread->pid, in_reply_to->debug_id, |
| 1372 | in_reply_to->to_proc ? |
| 1373 | in_reply_to->to_proc->pid : 0, |
| 1374 | in_reply_to->to_thread ? |
| 1375 | in_reply_to->to_thread->pid : 0); |
| 1376 | return_error = BR_FAILED_REPLY; |
| 1377 | in_reply_to = NULL; |
| 1378 | goto err_bad_call_stack; |
| 1379 | } |
| 1380 | thread->transaction_stack = in_reply_to->to_parent; |
| 1381 | target_thread = in_reply_to->from; |
| 1382 | if (target_thread == NULL) { |
| 1383 | return_error = BR_DEAD_REPLY; |
| 1384 | goto err_dead_binder; |
| 1385 | } |
| 1386 | if (target_thread->transaction_stack != in_reply_to) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1387 | binder_user_error("%d:%d got reply transaction with bad target transaction stack %d, expected %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1388 | proc->pid, thread->pid, |
| 1389 | target_thread->transaction_stack ? |
| 1390 | target_thread->transaction_stack->debug_id : 0, |
| 1391 | in_reply_to->debug_id); |
| 1392 | return_error = BR_FAILED_REPLY; |
| 1393 | in_reply_to = NULL; |
| 1394 | target_thread = NULL; |
| 1395 | goto err_dead_binder; |
| 1396 | } |
| 1397 | target_proc = target_thread->proc; |
| 1398 | } else { |
| 1399 | if (tr->target.handle) { |
| 1400 | struct binder_ref *ref; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1401 | |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1402 | ref = binder_get_ref(proc, tr->target.handle, true); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1403 | if (ref == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1404 | binder_user_error("%d:%d got transaction to invalid handle\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1405 | proc->pid, thread->pid); |
| 1406 | return_error = BR_FAILED_REPLY; |
| 1407 | goto err_invalid_target_handle; |
| 1408 | } |
| 1409 | target_node = ref->node; |
| 1410 | } else { |
| 1411 | target_node = binder_context_mgr_node; |
| 1412 | if (target_node == NULL) { |
| 1413 | return_error = BR_DEAD_REPLY; |
| 1414 | goto err_no_context_mgr_node; |
| 1415 | } |
| 1416 | } |
| 1417 | e->to_node = target_node->debug_id; |
| 1418 | target_proc = target_node->proc; |
| 1419 | if (target_proc == NULL) { |
| 1420 | return_error = BR_DEAD_REPLY; |
| 1421 | goto err_dead_binder; |
| 1422 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 1423 | if (security_binder_transaction(proc->tsk, |
| 1424 | target_proc->tsk) < 0) { |
| 1425 | return_error = BR_FAILED_REPLY; |
| 1426 | goto err_invalid_target_handle; |
| 1427 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1428 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { |
| 1429 | struct binder_transaction *tmp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1430 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1431 | tmp = thread->transaction_stack; |
| 1432 | if (tmp->to_thread != thread) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1433 | binder_user_error("%d:%d got new transaction with bad transaction stack, transaction %d has target %d:%d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1434 | proc->pid, thread->pid, tmp->debug_id, |
| 1435 | tmp->to_proc ? tmp->to_proc->pid : 0, |
| 1436 | tmp->to_thread ? |
| 1437 | tmp->to_thread->pid : 0); |
| 1438 | return_error = BR_FAILED_REPLY; |
| 1439 | goto err_bad_call_stack; |
| 1440 | } |
| 1441 | while (tmp) { |
| 1442 | if (tmp->from && tmp->from->proc == target_proc) |
| 1443 | target_thread = tmp->from; |
| 1444 | tmp = tmp->from_parent; |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | if (target_thread) { |
| 1449 | e->to_thread = target_thread->pid; |
| 1450 | target_list = &target_thread->todo; |
| 1451 | target_wait = &target_thread->wait; |
| 1452 | } else { |
| 1453 | target_list = &target_proc->todo; |
| 1454 | target_wait = &target_proc->wait; |
| 1455 | } |
| 1456 | e->to_proc = target_proc->pid; |
| 1457 | |
| 1458 | /* TODO: reuse incoming transaction for reply */ |
| 1459 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 1460 | if (t == NULL) { |
| 1461 | return_error = BR_FAILED_REPLY; |
| 1462 | goto err_alloc_t_failed; |
| 1463 | } |
| 1464 | binder_stats_created(BINDER_STAT_TRANSACTION); |
| 1465 | |
| 1466 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); |
| 1467 | if (tcomplete == NULL) { |
| 1468 | return_error = BR_FAILED_REPLY; |
| 1469 | goto err_alloc_tcomplete_failed; |
| 1470 | } |
| 1471 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); |
| 1472 | |
| 1473 | t->debug_id = ++binder_last_id; |
| 1474 | e->debug_id = t->debug_id; |
| 1475 | |
| 1476 | if (reply) |
| 1477 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1478 | "%d:%d BC_REPLY %d -> %d:%d, data %016llx-%016llx size %lld-%lld\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1479 | proc->pid, thread->pid, t->debug_id, |
| 1480 | target_proc->pid, target_thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1481 | (u64)tr->data.ptr.buffer, |
| 1482 | (u64)tr->data.ptr.offsets, |
| 1483 | (u64)tr->data_size, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1484 | else |
| 1485 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1486 | "%d:%d BC_TRANSACTION %d -> %d - node %d, data %016llx-%016llx size %lld-%lld\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1487 | proc->pid, thread->pid, t->debug_id, |
| 1488 | target_proc->pid, target_node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1489 | (u64)tr->data.ptr.buffer, |
| 1490 | (u64)tr->data.ptr.offsets, |
| 1491 | (u64)tr->data_size, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1492 | |
| 1493 | if (!reply && !(tr->flags & TF_ONE_WAY)) |
| 1494 | t->from = thread; |
| 1495 | else |
| 1496 | t->from = NULL; |
Tair Rzayev | 57bab7c | 2014-05-31 22:43:34 +0300 | [diff] [blame] | 1497 | t->sender_euid = task_euid(proc->tsk); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1498 | t->to_proc = target_proc; |
| 1499 | t->to_thread = target_thread; |
| 1500 | t->code = tr->code; |
| 1501 | t->flags = tr->flags; |
| 1502 | t->priority = task_nice(current); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 1503 | |
| 1504 | trace_binder_transaction(reply, t, target_node); |
| 1505 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1506 | t->buffer = binder_alloc_buf(target_proc, tr->data_size, |
| 1507 | tr->offsets_size, !reply && (t->flags & TF_ONE_WAY)); |
| 1508 | if (t->buffer == NULL) { |
| 1509 | return_error = BR_FAILED_REPLY; |
| 1510 | goto err_binder_alloc_buf_failed; |
| 1511 | } |
| 1512 | t->buffer->allow_user_free = 0; |
| 1513 | t->buffer->debug_id = t->debug_id; |
| 1514 | t->buffer->transaction = t; |
| 1515 | t->buffer->target_node = target_node; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 1516 | trace_binder_transaction_alloc_buf(t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1517 | if (target_node) |
| 1518 | binder_inc_node(target_node, 1, 0, NULL); |
| 1519 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1520 | offp = (binder_size_t *)(t->buffer->data + |
| 1521 | ALIGN(tr->data_size, sizeof(void *))); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1522 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1523 | if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t) |
| 1524 | tr->data.ptr.buffer, tr->data_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1525 | binder_user_error("%d:%d got transaction with invalid data ptr\n", |
| 1526 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1527 | return_error = BR_FAILED_REPLY; |
| 1528 | goto err_copy_data_failed; |
| 1529 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1530 | if (copy_from_user(offp, (const void __user *)(uintptr_t) |
| 1531 | tr->data.ptr.offsets, tr->offsets_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1532 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 1533 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1534 | return_error = BR_FAILED_REPLY; |
| 1535 | goto err_copy_data_failed; |
| 1536 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1537 | if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) { |
| 1538 | binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n", |
| 1539 | proc->pid, thread->pid, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1540 | return_error = BR_FAILED_REPLY; |
| 1541 | goto err_bad_offset; |
| 1542 | } |
| 1543 | off_end = (void *)offp + tr->offsets_size; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 1544 | off_min = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1545 | for (; offp < off_end; offp++) { |
| 1546 | struct flat_binder_object *fp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1547 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1548 | if (*offp > t->buffer->data_size - sizeof(*fp) || |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 1549 | *offp < off_min || |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1550 | t->buffer->data_size < sizeof(*fp) || |
Serban Constantinescu | ec35e85 | 2013-07-04 10:54:46 +0100 | [diff] [blame] | 1551 | !IS_ALIGNED(*offp, sizeof(u32))) { |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 1552 | binder_user_error("%d:%d got transaction with invalid offset, %lld (min %lld, max %lld)\n", |
| 1553 | proc->pid, thread->pid, (u64)*offp, |
| 1554 | (u64)off_min, |
| 1555 | (u64)(t->buffer->data_size - |
| 1556 | sizeof(*fp))); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1557 | return_error = BR_FAILED_REPLY; |
| 1558 | goto err_bad_offset; |
| 1559 | } |
| 1560 | fp = (struct flat_binder_object *)(t->buffer->data + *offp); |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 1561 | off_min = *offp + sizeof(struct flat_binder_object); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1562 | switch (fp->type) { |
| 1563 | case BINDER_TYPE_BINDER: |
| 1564 | case BINDER_TYPE_WEAK_BINDER: { |
| 1565 | struct binder_ref *ref; |
| 1566 | struct binder_node *node = binder_get_node(proc, fp->binder); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1567 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1568 | if (node == NULL) { |
| 1569 | node = binder_new_node(proc, fp->binder, fp->cookie); |
| 1570 | if (node == NULL) { |
| 1571 | return_error = BR_FAILED_REPLY; |
| 1572 | goto err_binder_new_node_failed; |
| 1573 | } |
| 1574 | node->min_priority = fp->flags & FLAT_BINDER_FLAG_PRIORITY_MASK; |
| 1575 | node->accept_fds = !!(fp->flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); |
| 1576 | } |
| 1577 | if (fp->cookie != node->cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1578 | binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1579 | proc->pid, thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1580 | (u64)fp->binder, node->debug_id, |
| 1581 | (u64)fp->cookie, (u64)node->cookie); |
Christian Engelmayer | 7d42043 | 2014-05-07 21:44:53 +0200 | [diff] [blame] | 1582 | return_error = BR_FAILED_REPLY; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1583 | goto err_binder_get_ref_for_node_failed; |
| 1584 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 1585 | if (security_binder_transfer_binder(proc->tsk, |
| 1586 | target_proc->tsk)) { |
| 1587 | return_error = BR_FAILED_REPLY; |
| 1588 | goto err_binder_get_ref_for_node_failed; |
| 1589 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1590 | ref = binder_get_ref_for_node(target_proc, node); |
| 1591 | if (ref == NULL) { |
| 1592 | return_error = BR_FAILED_REPLY; |
| 1593 | goto err_binder_get_ref_for_node_failed; |
| 1594 | } |
| 1595 | if (fp->type == BINDER_TYPE_BINDER) |
| 1596 | fp->type = BINDER_TYPE_HANDLE; |
| 1597 | else |
| 1598 | fp->type = BINDER_TYPE_WEAK_HANDLE; |
Arve Hjønnevåg | 4afb604 | 2016-10-24 15:20:30 +0200 | [diff] [blame] | 1599 | fp->binder = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1600 | fp->handle = ref->desc; |
Arve Hjønnevåg | 4afb604 | 2016-10-24 15:20:30 +0200 | [diff] [blame] | 1601 | fp->cookie = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1602 | binder_inc_ref(ref, fp->type == BINDER_TYPE_HANDLE, |
| 1603 | &thread->todo); |
| 1604 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 1605 | trace_binder_transaction_node_to_ref(t, node, ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1606 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1607 | " node %d u%016llx -> ref %d desc %d\n", |
| 1608 | node->debug_id, (u64)node->ptr, |
| 1609 | ref->debug_id, ref->desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1610 | } break; |
| 1611 | case BINDER_TYPE_HANDLE: |
| 1612 | case BINDER_TYPE_WEAK_HANDLE: { |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1613 | struct binder_ref *ref; |
| 1614 | |
| 1615 | ref = binder_get_ref(proc, fp->handle, |
| 1616 | fp->type == BINDER_TYPE_HANDLE); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1617 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1618 | if (ref == NULL) { |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 1619 | binder_user_error("%d:%d got transaction with invalid handle, %d\n", |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1620 | proc->pid, |
| 1621 | thread->pid, fp->handle); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1622 | return_error = BR_FAILED_REPLY; |
| 1623 | goto err_binder_get_ref_failed; |
| 1624 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 1625 | if (security_binder_transfer_binder(proc->tsk, |
| 1626 | target_proc->tsk)) { |
| 1627 | return_error = BR_FAILED_REPLY; |
| 1628 | goto err_binder_get_ref_failed; |
| 1629 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1630 | if (ref->node->proc == target_proc) { |
| 1631 | if (fp->type == BINDER_TYPE_HANDLE) |
| 1632 | fp->type = BINDER_TYPE_BINDER; |
| 1633 | else |
| 1634 | fp->type = BINDER_TYPE_WEAK_BINDER; |
| 1635 | fp->binder = ref->node->ptr; |
| 1636 | fp->cookie = ref->node->cookie; |
| 1637 | binder_inc_node(ref->node, fp->type == BINDER_TYPE_BINDER, 0, NULL); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 1638 | trace_binder_transaction_ref_to_node(t, ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1639 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1640 | " ref %d desc %d -> node %d u%016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1641 | ref->debug_id, ref->desc, ref->node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1642 | (u64)ref->node->ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1643 | } else { |
| 1644 | struct binder_ref *new_ref; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1645 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1646 | new_ref = binder_get_ref_for_node(target_proc, ref->node); |
| 1647 | if (new_ref == NULL) { |
| 1648 | return_error = BR_FAILED_REPLY; |
| 1649 | goto err_binder_get_ref_for_node_failed; |
| 1650 | } |
Arve Hjønnevåg | 4afb604 | 2016-10-24 15:20:30 +0200 | [diff] [blame] | 1651 | fp->binder = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1652 | fp->handle = new_ref->desc; |
Arve Hjønnevåg | 4afb604 | 2016-10-24 15:20:30 +0200 | [diff] [blame] | 1653 | fp->cookie = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1654 | binder_inc_ref(new_ref, fp->type == BINDER_TYPE_HANDLE, NULL); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 1655 | trace_binder_transaction_ref_to_ref(t, ref, |
| 1656 | new_ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1657 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 1658 | " ref %d desc %d -> ref %d desc %d (node %d)\n", |
| 1659 | ref->debug_id, ref->desc, new_ref->debug_id, |
| 1660 | new_ref->desc, ref->node->debug_id); |
| 1661 | } |
| 1662 | } break; |
| 1663 | |
| 1664 | case BINDER_TYPE_FD: { |
| 1665 | int target_fd; |
| 1666 | struct file *file; |
| 1667 | |
| 1668 | if (reply) { |
| 1669 | if (!(in_reply_to->flags & TF_ACCEPT_FDS)) { |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 1670 | binder_user_error("%d:%d got reply with fd, %d, but target does not allow fds\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1671 | proc->pid, thread->pid, fp->handle); |
| 1672 | return_error = BR_FAILED_REPLY; |
| 1673 | goto err_fd_not_allowed; |
| 1674 | } |
| 1675 | } else if (!target_node->accept_fds) { |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 1676 | binder_user_error("%d:%d got transaction with fd, %d, but target does not allow fds\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1677 | proc->pid, thread->pid, fp->handle); |
| 1678 | return_error = BR_FAILED_REPLY; |
| 1679 | goto err_fd_not_allowed; |
| 1680 | } |
| 1681 | |
| 1682 | file = fget(fp->handle); |
| 1683 | if (file == NULL) { |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 1684 | binder_user_error("%d:%d got transaction with invalid fd, %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1685 | proc->pid, thread->pid, fp->handle); |
| 1686 | return_error = BR_FAILED_REPLY; |
| 1687 | goto err_fget_failed; |
| 1688 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 1689 | if (security_binder_transfer_file(proc->tsk, |
| 1690 | target_proc->tsk, |
| 1691 | file) < 0) { |
| 1692 | fput(file); |
| 1693 | return_error = BR_FAILED_REPLY; |
| 1694 | goto err_get_unused_fd_failed; |
| 1695 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1696 | target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC); |
| 1697 | if (target_fd < 0) { |
| 1698 | fput(file); |
| 1699 | return_error = BR_FAILED_REPLY; |
| 1700 | goto err_get_unused_fd_failed; |
| 1701 | } |
| 1702 | task_fd_install(target_proc, target_fd, file); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 1703 | trace_binder_transaction_fd(t, fp->handle, target_fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1704 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 1705 | " fd %d -> %d\n", fp->handle, target_fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1706 | /* TODO: fput? */ |
Arve Hjønnevåg | 4afb604 | 2016-10-24 15:20:30 +0200 | [diff] [blame] | 1707 | fp->binder = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1708 | fp->handle = target_fd; |
| 1709 | } break; |
| 1710 | |
| 1711 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 1712 | binder_user_error("%d:%d got transaction with invalid object type, %x\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1713 | proc->pid, thread->pid, fp->type); |
| 1714 | return_error = BR_FAILED_REPLY; |
| 1715 | goto err_bad_object_type; |
| 1716 | } |
| 1717 | } |
| 1718 | if (reply) { |
| 1719 | BUG_ON(t->buffer->async_transaction != 0); |
| 1720 | binder_pop_transaction(target_thread, in_reply_to); |
| 1721 | } else if (!(t->flags & TF_ONE_WAY)) { |
| 1722 | BUG_ON(t->buffer->async_transaction != 0); |
| 1723 | t->need_reply = 1; |
| 1724 | t->from_parent = thread->transaction_stack; |
| 1725 | thread->transaction_stack = t; |
| 1726 | } else { |
| 1727 | BUG_ON(target_node == NULL); |
| 1728 | BUG_ON(t->buffer->async_transaction != 1); |
| 1729 | if (target_node->has_async_transaction) { |
| 1730 | target_list = &target_node->async_todo; |
| 1731 | target_wait = NULL; |
| 1732 | } else |
| 1733 | target_node->has_async_transaction = 1; |
| 1734 | } |
| 1735 | t->work.type = BINDER_WORK_TRANSACTION; |
| 1736 | list_add_tail(&t->work.entry, target_list); |
| 1737 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; |
| 1738 | list_add_tail(&tcomplete->entry, &thread->todo); |
Riley Andrews | 8fb0b0c | 2017-06-29 12:01:37 -0700 | [diff] [blame] | 1739 | if (target_wait) { |
| 1740 | if (reply || !(t->flags & TF_ONE_WAY)) |
| 1741 | wake_up_interruptible_sync(target_wait); |
| 1742 | else |
| 1743 | wake_up_interruptible(target_wait); |
| 1744 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1745 | return; |
| 1746 | |
| 1747 | err_get_unused_fd_failed: |
| 1748 | err_fget_failed: |
| 1749 | err_fd_not_allowed: |
| 1750 | err_binder_get_ref_for_node_failed: |
| 1751 | err_binder_get_ref_failed: |
| 1752 | err_binder_new_node_failed: |
| 1753 | err_bad_object_type: |
| 1754 | err_bad_offset: |
| 1755 | err_copy_data_failed: |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 1756 | trace_binder_transaction_failed_buffer_release(t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1757 | binder_transaction_buffer_release(target_proc, t->buffer, offp); |
| 1758 | t->buffer->transaction = NULL; |
| 1759 | binder_free_buf(target_proc, t->buffer); |
| 1760 | err_binder_alloc_buf_failed: |
| 1761 | kfree(tcomplete); |
| 1762 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 1763 | err_alloc_tcomplete_failed: |
| 1764 | kfree(t); |
| 1765 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 1766 | err_alloc_t_failed: |
| 1767 | err_bad_call_stack: |
| 1768 | err_empty_call_stack: |
| 1769 | err_dead_binder: |
| 1770 | err_invalid_target_handle: |
| 1771 | err_no_context_mgr_node: |
| 1772 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1773 | "%d:%d transaction failed %d, size %lld-%lld\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1774 | proc->pid, thread->pid, return_error, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1775 | (u64)tr->data_size, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1776 | |
| 1777 | { |
| 1778 | struct binder_transaction_log_entry *fe; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1779 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1780 | fe = binder_transaction_log_add(&binder_transaction_log_failed); |
| 1781 | *fe = *e; |
| 1782 | } |
| 1783 | |
| 1784 | BUG_ON(thread->return_error != BR_OK); |
| 1785 | if (in_reply_to) { |
| 1786 | thread->return_error = BR_TRANSACTION_COMPLETE; |
| 1787 | binder_send_failed_reply(in_reply_to, return_error); |
| 1788 | } else |
| 1789 | thread->return_error = return_error; |
| 1790 | } |
| 1791 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 1792 | static int binder_thread_write(struct binder_proc *proc, |
| 1793 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1794 | binder_uintptr_t binder_buffer, size_t size, |
| 1795 | binder_size_t *consumed) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1796 | { |
| 1797 | uint32_t cmd; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1798 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1799 | void __user *ptr = buffer + *consumed; |
| 1800 | void __user *end = buffer + size; |
| 1801 | |
| 1802 | while (ptr < end && thread->return_error == BR_OK) { |
| 1803 | if (get_user(cmd, (uint32_t __user *)ptr)) |
| 1804 | return -EFAULT; |
| 1805 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 1806 | trace_binder_command(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1807 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { |
| 1808 | binder_stats.bc[_IOC_NR(cmd)]++; |
| 1809 | proc->stats.bc[_IOC_NR(cmd)]++; |
| 1810 | thread->stats.bc[_IOC_NR(cmd)]++; |
| 1811 | } |
| 1812 | switch (cmd) { |
| 1813 | case BC_INCREFS: |
| 1814 | case BC_ACQUIRE: |
| 1815 | case BC_RELEASE: |
| 1816 | case BC_DECREFS: { |
| 1817 | uint32_t target; |
| 1818 | struct binder_ref *ref; |
| 1819 | const char *debug_string; |
| 1820 | |
| 1821 | if (get_user(target, (uint32_t __user *)ptr)) |
| 1822 | return -EFAULT; |
| 1823 | ptr += sizeof(uint32_t); |
| 1824 | if (target == 0 && binder_context_mgr_node && |
| 1825 | (cmd == BC_INCREFS || cmd == BC_ACQUIRE)) { |
| 1826 | ref = binder_get_ref_for_node(proc, |
| 1827 | binder_context_mgr_node); |
| 1828 | if (ref->desc != target) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1829 | binder_user_error("%d:%d tried to acquire reference to desc 0, got %d instead\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1830 | proc->pid, thread->pid, |
| 1831 | ref->desc); |
| 1832 | } |
| 1833 | } else |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1834 | ref = binder_get_ref(proc, target, |
| 1835 | cmd == BC_ACQUIRE || |
| 1836 | cmd == BC_RELEASE); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1837 | if (ref == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1838 | binder_user_error("%d:%d refcount change on invalid ref %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1839 | proc->pid, thread->pid, target); |
| 1840 | break; |
| 1841 | } |
| 1842 | switch (cmd) { |
| 1843 | case BC_INCREFS: |
| 1844 | debug_string = "IncRefs"; |
| 1845 | binder_inc_ref(ref, 0, NULL); |
| 1846 | break; |
| 1847 | case BC_ACQUIRE: |
| 1848 | debug_string = "Acquire"; |
| 1849 | binder_inc_ref(ref, 1, NULL); |
| 1850 | break; |
| 1851 | case BC_RELEASE: |
| 1852 | debug_string = "Release"; |
| 1853 | binder_dec_ref(ref, 1); |
| 1854 | break; |
| 1855 | case BC_DECREFS: |
| 1856 | default: |
| 1857 | debug_string = "DecRefs"; |
| 1858 | binder_dec_ref(ref, 0); |
| 1859 | break; |
| 1860 | } |
| 1861 | binder_debug(BINDER_DEBUG_USER_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1862 | "%d:%d %s ref %d desc %d s %d w %d for node %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1863 | proc->pid, thread->pid, debug_string, ref->debug_id, |
| 1864 | ref->desc, ref->strong, ref->weak, ref->node->debug_id); |
| 1865 | break; |
| 1866 | } |
| 1867 | case BC_INCREFS_DONE: |
| 1868 | case BC_ACQUIRE_DONE: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1869 | binder_uintptr_t node_ptr; |
| 1870 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1871 | struct binder_node *node; |
| 1872 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1873 | if (get_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1874 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1875 | ptr += sizeof(binder_uintptr_t); |
| 1876 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1877 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1878 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1879 | node = binder_get_node(proc, node_ptr); |
| 1880 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1881 | binder_user_error("%d:%d %s u%016llx no match\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1882 | proc->pid, thread->pid, |
| 1883 | cmd == BC_INCREFS_DONE ? |
| 1884 | "BC_INCREFS_DONE" : |
| 1885 | "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1886 | (u64)node_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1887 | break; |
| 1888 | } |
| 1889 | if (cookie != node->cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1890 | binder_user_error("%d:%d %s u%016llx node %d cookie mismatch %016llx != %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1891 | proc->pid, thread->pid, |
| 1892 | cmd == BC_INCREFS_DONE ? |
| 1893 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1894 | (u64)node_ptr, node->debug_id, |
| 1895 | (u64)cookie, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1896 | break; |
| 1897 | } |
| 1898 | if (cmd == BC_ACQUIRE_DONE) { |
| 1899 | if (node->pending_strong_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1900 | binder_user_error("%d:%d BC_ACQUIRE_DONE node %d has no pending acquire request\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1901 | proc->pid, thread->pid, |
| 1902 | node->debug_id); |
| 1903 | break; |
| 1904 | } |
| 1905 | node->pending_strong_ref = 0; |
| 1906 | } else { |
| 1907 | if (node->pending_weak_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1908 | binder_user_error("%d:%d BC_INCREFS_DONE node %d has no pending increfs request\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1909 | proc->pid, thread->pid, |
| 1910 | node->debug_id); |
| 1911 | break; |
| 1912 | } |
| 1913 | node->pending_weak_ref = 0; |
| 1914 | } |
| 1915 | binder_dec_node(node, cmd == BC_ACQUIRE_DONE, 0); |
| 1916 | binder_debug(BINDER_DEBUG_USER_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1917 | "%d:%d %s node %d ls %d lw %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1918 | proc->pid, thread->pid, |
| 1919 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
| 1920 | node->debug_id, node->local_strong_refs, node->local_weak_refs); |
| 1921 | break; |
| 1922 | } |
| 1923 | case BC_ATTEMPT_ACQUIRE: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1924 | pr_err("BC_ATTEMPT_ACQUIRE not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1925 | return -EINVAL; |
| 1926 | case BC_ACQUIRE_RESULT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1927 | pr_err("BC_ACQUIRE_RESULT not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1928 | return -EINVAL; |
| 1929 | |
| 1930 | case BC_FREE_BUFFER: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1931 | binder_uintptr_t data_ptr; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1932 | struct binder_buffer *buffer; |
| 1933 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1934 | if (get_user(data_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1935 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1936 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1937 | |
| 1938 | buffer = binder_buffer_lookup(proc, data_ptr); |
| 1939 | if (buffer == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1940 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n", |
| 1941 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1942 | break; |
| 1943 | } |
| 1944 | if (!buffer->allow_user_free) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1945 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n", |
| 1946 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1947 | break; |
| 1948 | } |
| 1949 | binder_debug(BINDER_DEBUG_FREE_BUFFER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1950 | "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n", |
| 1951 | proc->pid, thread->pid, (u64)data_ptr, |
| 1952 | buffer->debug_id, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1953 | buffer->transaction ? "active" : "finished"); |
| 1954 | |
| 1955 | if (buffer->transaction) { |
| 1956 | buffer->transaction->buffer = NULL; |
| 1957 | buffer->transaction = NULL; |
| 1958 | } |
| 1959 | if (buffer->async_transaction && buffer->target_node) { |
| 1960 | BUG_ON(!buffer->target_node->has_async_transaction); |
| 1961 | if (list_empty(&buffer->target_node->async_todo)) |
| 1962 | buffer->target_node->has_async_transaction = 0; |
| 1963 | else |
| 1964 | list_move_tail(buffer->target_node->async_todo.next, &thread->todo); |
| 1965 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 1966 | trace_binder_transaction_buffer_release(buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1967 | binder_transaction_buffer_release(proc, buffer, NULL); |
| 1968 | binder_free_buf(proc, buffer); |
| 1969 | break; |
| 1970 | } |
| 1971 | |
| 1972 | case BC_TRANSACTION: |
| 1973 | case BC_REPLY: { |
| 1974 | struct binder_transaction_data tr; |
| 1975 | |
| 1976 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 1977 | return -EFAULT; |
| 1978 | ptr += sizeof(tr); |
| 1979 | binder_transaction(proc, thread, &tr, cmd == BC_REPLY); |
| 1980 | break; |
| 1981 | } |
| 1982 | |
| 1983 | case BC_REGISTER_LOOPER: |
| 1984 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1985 | "%d:%d BC_REGISTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1986 | proc->pid, thread->pid); |
| 1987 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { |
| 1988 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1989 | binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called after BC_ENTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1990 | proc->pid, thread->pid); |
| 1991 | } else if (proc->requested_threads == 0) { |
| 1992 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1993 | binder_user_error("%d:%d ERROR: BC_REGISTER_LOOPER called without request\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1994 | proc->pid, thread->pid); |
| 1995 | } else { |
| 1996 | proc->requested_threads--; |
| 1997 | proc->requested_threads_started++; |
| 1998 | } |
| 1999 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; |
| 2000 | break; |
| 2001 | case BC_ENTER_LOOPER: |
| 2002 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2003 | "%d:%d BC_ENTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2004 | proc->pid, thread->pid); |
| 2005 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { |
| 2006 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2007 | binder_user_error("%d:%d ERROR: BC_ENTER_LOOPER called after BC_REGISTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2008 | proc->pid, thread->pid); |
| 2009 | } |
| 2010 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; |
| 2011 | break; |
| 2012 | case BC_EXIT_LOOPER: |
| 2013 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2014 | "%d:%d BC_EXIT_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2015 | proc->pid, thread->pid); |
| 2016 | thread->looper |= BINDER_LOOPER_STATE_EXITED; |
| 2017 | break; |
| 2018 | |
| 2019 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 2020 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 2021 | uint32_t target; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2022 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2023 | struct binder_ref *ref; |
| 2024 | struct binder_ref_death *death; |
| 2025 | |
| 2026 | if (get_user(target, (uint32_t __user *)ptr)) |
| 2027 | return -EFAULT; |
| 2028 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2029 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2030 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2031 | ptr += sizeof(binder_uintptr_t); |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 2032 | ref = binder_get_ref(proc, target, false); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2033 | if (ref == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2034 | binder_user_error("%d:%d %s invalid ref %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2035 | proc->pid, thread->pid, |
| 2036 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 2037 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 2038 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 2039 | target); |
| 2040 | break; |
| 2041 | } |
| 2042 | |
| 2043 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2044 | "%d:%d %s %016llx ref %d desc %d s %d w %d for node %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2045 | proc->pid, thread->pid, |
| 2046 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 2047 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 2048 | "BC_CLEAR_DEATH_NOTIFICATION", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2049 | (u64)cookie, ref->debug_id, ref->desc, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2050 | ref->strong, ref->weak, ref->node->debug_id); |
| 2051 | |
| 2052 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 2053 | if (ref->death) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2054 | binder_user_error("%d:%d BC_REQUEST_DEATH_NOTIFICATION death notification already set\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2055 | proc->pid, thread->pid); |
| 2056 | break; |
| 2057 | } |
| 2058 | death = kzalloc(sizeof(*death), GFP_KERNEL); |
| 2059 | if (death == NULL) { |
| 2060 | thread->return_error = BR_ERROR; |
| 2061 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2062 | "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2063 | proc->pid, thread->pid); |
| 2064 | break; |
| 2065 | } |
| 2066 | binder_stats_created(BINDER_STAT_DEATH); |
| 2067 | INIT_LIST_HEAD(&death->work.entry); |
| 2068 | death->cookie = cookie; |
| 2069 | ref->death = death; |
| 2070 | if (ref->node->proc == NULL) { |
| 2071 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 2072 | if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) { |
| 2073 | list_add_tail(&ref->death->work.entry, &thread->todo); |
| 2074 | } else { |
| 2075 | list_add_tail(&ref->death->work.entry, &proc->todo); |
| 2076 | wake_up_interruptible(&proc->wait); |
| 2077 | } |
| 2078 | } |
| 2079 | } else { |
| 2080 | if (ref->death == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2081 | binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification not active\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2082 | proc->pid, thread->pid); |
| 2083 | break; |
| 2084 | } |
| 2085 | death = ref->death; |
| 2086 | if (death->cookie != cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2087 | binder_user_error("%d:%d BC_CLEAR_DEATH_NOTIFICATION death notification cookie mismatch %016llx != %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2088 | proc->pid, thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2089 | (u64)death->cookie, |
| 2090 | (u64)cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2091 | break; |
| 2092 | } |
| 2093 | ref->death = NULL; |
| 2094 | if (list_empty(&death->work.entry)) { |
| 2095 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
| 2096 | if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) { |
| 2097 | list_add_tail(&death->work.entry, &thread->todo); |
| 2098 | } else { |
| 2099 | list_add_tail(&death->work.entry, &proc->todo); |
| 2100 | wake_up_interruptible(&proc->wait); |
| 2101 | } |
| 2102 | } else { |
| 2103 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); |
| 2104 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; |
| 2105 | } |
| 2106 | } |
| 2107 | } break; |
| 2108 | case BC_DEAD_BINDER_DONE: { |
| 2109 | struct binder_work *w; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2110 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2111 | struct binder_ref_death *death = NULL; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2112 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2113 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2114 | return -EFAULT; |
| 2115 | |
Lisa Du | 7a64cd8 | 2016-02-17 09:32:52 +0800 | [diff] [blame] | 2116 | ptr += sizeof(cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2117 | list_for_each_entry(w, &proc->delivered_death, entry) { |
| 2118 | struct binder_ref_death *tmp_death = container_of(w, struct binder_ref_death, work); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2119 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2120 | if (tmp_death->cookie == cookie) { |
| 2121 | death = tmp_death; |
| 2122 | break; |
| 2123 | } |
| 2124 | } |
| 2125 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Todd Kjos | 6f3433c | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 2126 | "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2127 | proc->pid, thread->pid, (u64)cookie, |
| 2128 | death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2129 | if (death == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2130 | binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n", |
| 2131 | proc->pid, thread->pid, (u64)cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2132 | break; |
| 2133 | } |
| 2134 | |
| 2135 | list_del_init(&death->work.entry); |
| 2136 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { |
| 2137 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
| 2138 | if (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | BINDER_LOOPER_STATE_ENTERED)) { |
| 2139 | list_add_tail(&death->work.entry, &thread->todo); |
| 2140 | } else { |
| 2141 | list_add_tail(&death->work.entry, &proc->todo); |
| 2142 | wake_up_interruptible(&proc->wait); |
| 2143 | } |
| 2144 | } |
| 2145 | } break; |
| 2146 | |
| 2147 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2148 | pr_err("%d:%d unknown command %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2149 | proc->pid, thread->pid, cmd); |
| 2150 | return -EINVAL; |
| 2151 | } |
| 2152 | *consumed = ptr - buffer; |
| 2153 | } |
| 2154 | return 0; |
| 2155 | } |
| 2156 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 2157 | static void binder_stat_br(struct binder_proc *proc, |
| 2158 | struct binder_thread *thread, uint32_t cmd) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2159 | { |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2160 | trace_binder_return(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2161 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { |
| 2162 | binder_stats.br[_IOC_NR(cmd)]++; |
| 2163 | proc->stats.br[_IOC_NR(cmd)]++; |
| 2164 | thread->stats.br[_IOC_NR(cmd)]++; |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | static int binder_has_proc_work(struct binder_proc *proc, |
| 2169 | struct binder_thread *thread) |
| 2170 | { |
| 2171 | return !list_empty(&proc->todo) || |
| 2172 | (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN); |
| 2173 | } |
| 2174 | |
| 2175 | static int binder_has_thread_work(struct binder_thread *thread) |
| 2176 | { |
| 2177 | return !list_empty(&thread->todo) || thread->return_error != BR_OK || |
| 2178 | (thread->looper & BINDER_LOOPER_STATE_NEED_RETURN); |
| 2179 | } |
| 2180 | |
| 2181 | static int binder_thread_read(struct binder_proc *proc, |
| 2182 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2183 | binder_uintptr_t binder_buffer, size_t size, |
| 2184 | binder_size_t *consumed, int non_block) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2185 | { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2186 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2187 | void __user *ptr = buffer + *consumed; |
| 2188 | void __user *end = buffer + size; |
| 2189 | |
| 2190 | int ret = 0; |
| 2191 | int wait_for_proc_work; |
| 2192 | |
| 2193 | if (*consumed == 0) { |
| 2194 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) |
| 2195 | return -EFAULT; |
| 2196 | ptr += sizeof(uint32_t); |
| 2197 | } |
| 2198 | |
| 2199 | retry: |
| 2200 | wait_for_proc_work = thread->transaction_stack == NULL && |
| 2201 | list_empty(&thread->todo); |
| 2202 | |
| 2203 | if (thread->return_error != BR_OK && ptr < end) { |
| 2204 | if (thread->return_error2 != BR_OK) { |
| 2205 | if (put_user(thread->return_error2, (uint32_t __user *)ptr)) |
| 2206 | return -EFAULT; |
| 2207 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 2208 | binder_stat_br(proc, thread, thread->return_error2); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2209 | if (ptr == end) |
| 2210 | goto done; |
| 2211 | thread->return_error2 = BR_OK; |
| 2212 | } |
| 2213 | if (put_user(thread->return_error, (uint32_t __user *)ptr)) |
| 2214 | return -EFAULT; |
| 2215 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 2216 | binder_stat_br(proc, thread, thread->return_error); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2217 | thread->return_error = BR_OK; |
| 2218 | goto done; |
| 2219 | } |
| 2220 | |
| 2221 | |
| 2222 | thread->looper |= BINDER_LOOPER_STATE_WAITING; |
| 2223 | if (wait_for_proc_work) |
| 2224 | proc->ready_threads++; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2225 | |
| 2226 | binder_unlock(__func__); |
| 2227 | |
| 2228 | trace_binder_wait_for_work(wait_for_proc_work, |
| 2229 | !!thread->transaction_stack, |
| 2230 | !list_empty(&thread->todo)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2231 | if (wait_for_proc_work) { |
| 2232 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 2233 | BINDER_LOOPER_STATE_ENTERED))) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2234 | 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-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2235 | proc->pid, thread->pid, thread->looper); |
| 2236 | wait_event_interruptible(binder_user_error_wait, |
| 2237 | binder_stop_on_user_error < 2); |
| 2238 | } |
| 2239 | binder_set_nice(proc->default_priority); |
| 2240 | if (non_block) { |
| 2241 | if (!binder_has_proc_work(proc, thread)) |
| 2242 | ret = -EAGAIN; |
| 2243 | } else |
Colin Cross | e2610b2 | 2013-05-06 23:50:15 +0000 | [diff] [blame] | 2244 | ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2245 | } else { |
| 2246 | if (non_block) { |
| 2247 | if (!binder_has_thread_work(thread)) |
| 2248 | ret = -EAGAIN; |
| 2249 | } else |
Colin Cross | e2610b2 | 2013-05-06 23:50:15 +0000 | [diff] [blame] | 2250 | ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2251 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2252 | |
| 2253 | binder_lock(__func__); |
| 2254 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2255 | if (wait_for_proc_work) |
| 2256 | proc->ready_threads--; |
| 2257 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; |
| 2258 | |
| 2259 | if (ret) |
| 2260 | return ret; |
| 2261 | |
| 2262 | while (1) { |
| 2263 | uint32_t cmd; |
| 2264 | struct binder_transaction_data tr; |
| 2265 | struct binder_work *w; |
| 2266 | struct binder_transaction *t = NULL; |
| 2267 | |
Dmitry Voytik | 395262a | 2014-09-08 18:16:34 +0400 | [diff] [blame] | 2268 | if (!list_empty(&thread->todo)) { |
| 2269 | w = list_first_entry(&thread->todo, struct binder_work, |
| 2270 | entry); |
| 2271 | } else if (!list_empty(&proc->todo) && wait_for_proc_work) { |
| 2272 | w = list_first_entry(&proc->todo, struct binder_work, |
| 2273 | entry); |
| 2274 | } else { |
| 2275 | /* no data added */ |
| 2276 | if (ptr - buffer == 4 && |
| 2277 | !(thread->looper & BINDER_LOOPER_STATE_NEED_RETURN)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2278 | goto retry; |
| 2279 | break; |
| 2280 | } |
| 2281 | |
| 2282 | if (end - ptr < sizeof(tr) + 4) |
| 2283 | break; |
| 2284 | |
| 2285 | switch (w->type) { |
| 2286 | case BINDER_WORK_TRANSACTION: { |
| 2287 | t = container_of(w, struct binder_transaction, work); |
| 2288 | } break; |
| 2289 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
| 2290 | cmd = BR_TRANSACTION_COMPLETE; |
| 2291 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 2292 | return -EFAULT; |
| 2293 | ptr += sizeof(uint32_t); |
| 2294 | |
| 2295 | binder_stat_br(proc, thread, cmd); |
| 2296 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2297 | "%d:%d BR_TRANSACTION_COMPLETE\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2298 | proc->pid, thread->pid); |
| 2299 | |
| 2300 | list_del(&w->entry); |
| 2301 | kfree(w); |
| 2302 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 2303 | } break; |
| 2304 | case BINDER_WORK_NODE: { |
| 2305 | struct binder_node *node = container_of(w, struct binder_node, work); |
| 2306 | uint32_t cmd = BR_NOOP; |
| 2307 | const char *cmd_name; |
| 2308 | int strong = node->internal_strong_refs || node->local_strong_refs; |
| 2309 | int weak = !hlist_empty(&node->refs) || node->local_weak_refs || strong; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2310 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2311 | if (weak && !node->has_weak_ref) { |
| 2312 | cmd = BR_INCREFS; |
| 2313 | cmd_name = "BR_INCREFS"; |
| 2314 | node->has_weak_ref = 1; |
| 2315 | node->pending_weak_ref = 1; |
| 2316 | node->local_weak_refs++; |
| 2317 | } else if (strong && !node->has_strong_ref) { |
| 2318 | cmd = BR_ACQUIRE; |
| 2319 | cmd_name = "BR_ACQUIRE"; |
| 2320 | node->has_strong_ref = 1; |
| 2321 | node->pending_strong_ref = 1; |
| 2322 | node->local_strong_refs++; |
| 2323 | } else if (!strong && node->has_strong_ref) { |
| 2324 | cmd = BR_RELEASE; |
| 2325 | cmd_name = "BR_RELEASE"; |
| 2326 | node->has_strong_ref = 0; |
| 2327 | } else if (!weak && node->has_weak_ref) { |
| 2328 | cmd = BR_DECREFS; |
| 2329 | cmd_name = "BR_DECREFS"; |
| 2330 | node->has_weak_ref = 0; |
| 2331 | } |
| 2332 | if (cmd != BR_NOOP) { |
| 2333 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 2334 | return -EFAULT; |
| 2335 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2336 | if (put_user(node->ptr, |
| 2337 | (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2338 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2339 | ptr += sizeof(binder_uintptr_t); |
| 2340 | if (put_user(node->cookie, |
| 2341 | (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2342 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2343 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2344 | |
| 2345 | binder_stat_br(proc, thread, cmd); |
| 2346 | binder_debug(BINDER_DEBUG_USER_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2347 | "%d:%d %s %d u%016llx c%016llx\n", |
| 2348 | proc->pid, thread->pid, cmd_name, |
| 2349 | node->debug_id, |
| 2350 | (u64)node->ptr, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2351 | } else { |
| 2352 | list_del_init(&w->entry); |
| 2353 | if (!weak && !strong) { |
| 2354 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2355 | "%d:%d node %d u%016llx c%016llx deleted\n", |
| 2356 | proc->pid, thread->pid, |
| 2357 | node->debug_id, |
| 2358 | (u64)node->ptr, |
| 2359 | (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2360 | rb_erase(&node->rb_node, &proc->nodes); |
| 2361 | kfree(node); |
| 2362 | binder_stats_deleted(BINDER_STAT_NODE); |
| 2363 | } else { |
| 2364 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2365 | "%d:%d node %d u%016llx c%016llx state unchanged\n", |
| 2366 | proc->pid, thread->pid, |
| 2367 | node->debug_id, |
| 2368 | (u64)node->ptr, |
| 2369 | (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | } break; |
| 2373 | case BINDER_WORK_DEAD_BINDER: |
| 2374 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 2375 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 2376 | struct binder_ref_death *death; |
| 2377 | uint32_t cmd; |
| 2378 | |
| 2379 | death = container_of(w, struct binder_ref_death, work); |
| 2380 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) |
| 2381 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; |
| 2382 | else |
| 2383 | cmd = BR_DEAD_BINDER; |
| 2384 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 2385 | return -EFAULT; |
| 2386 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2387 | if (put_user(death->cookie, |
| 2388 | (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2389 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2390 | ptr += sizeof(binder_uintptr_t); |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 2391 | binder_stat_br(proc, thread, cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2392 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2393 | "%d:%d %s %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2394 | proc->pid, thread->pid, |
| 2395 | cmd == BR_DEAD_BINDER ? |
| 2396 | "BR_DEAD_BINDER" : |
| 2397 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2398 | (u64)death->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2399 | |
| 2400 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { |
| 2401 | list_del(&w->entry); |
| 2402 | kfree(death); |
| 2403 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 2404 | } else |
| 2405 | list_move(&w->entry, &proc->delivered_death); |
| 2406 | if (cmd == BR_DEAD_BINDER) |
| 2407 | goto done; /* DEAD_BINDER notifications can cause transactions */ |
| 2408 | } break; |
| 2409 | } |
| 2410 | |
| 2411 | if (!t) |
| 2412 | continue; |
| 2413 | |
| 2414 | BUG_ON(t->buffer == NULL); |
| 2415 | if (t->buffer->target_node) { |
| 2416 | struct binder_node *target_node = t->buffer->target_node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2417 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2418 | tr.target.ptr = target_node->ptr; |
| 2419 | tr.cookie = target_node->cookie; |
| 2420 | t->saved_priority = task_nice(current); |
| 2421 | if (t->priority < target_node->min_priority && |
| 2422 | !(t->flags & TF_ONE_WAY)) |
| 2423 | binder_set_nice(t->priority); |
| 2424 | else if (!(t->flags & TF_ONE_WAY) || |
| 2425 | t->saved_priority > target_node->min_priority) |
| 2426 | binder_set_nice(target_node->min_priority); |
| 2427 | cmd = BR_TRANSACTION; |
| 2428 | } else { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2429 | tr.target.ptr = 0; |
| 2430 | tr.cookie = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2431 | cmd = BR_REPLY; |
| 2432 | } |
| 2433 | tr.code = t->code; |
| 2434 | tr.flags = t->flags; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 2435 | tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2436 | |
| 2437 | if (t->from) { |
| 2438 | struct task_struct *sender = t->from->proc->tsk; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2439 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2440 | tr.sender_pid = task_tgid_nr_ns(sender, |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 2441 | task_active_pid_ns(current)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2442 | } else { |
| 2443 | tr.sender_pid = 0; |
| 2444 | } |
| 2445 | |
| 2446 | tr.data_size = t->buffer->data_size; |
| 2447 | tr.offsets_size = t->buffer->offsets_size; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2448 | tr.data.ptr.buffer = (binder_uintptr_t)( |
| 2449 | (uintptr_t)t->buffer->data + |
| 2450 | proc->user_buffer_offset); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2451 | tr.data.ptr.offsets = tr.data.ptr.buffer + |
| 2452 | ALIGN(t->buffer->data_size, |
| 2453 | sizeof(void *)); |
| 2454 | |
| 2455 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 2456 | return -EFAULT; |
| 2457 | ptr += sizeof(uint32_t); |
| 2458 | if (copy_to_user(ptr, &tr, sizeof(tr))) |
| 2459 | return -EFAULT; |
| 2460 | ptr += sizeof(tr); |
| 2461 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2462 | trace_binder_transaction_received(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2463 | binder_stat_br(proc, thread, cmd); |
| 2464 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2465 | "%d:%d %s %d %d:%d, cmd %d size %zd-%zd ptr %016llx-%016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2466 | proc->pid, thread->pid, |
| 2467 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : |
| 2468 | "BR_REPLY", |
| 2469 | t->debug_id, t->from ? t->from->proc->pid : 0, |
| 2470 | t->from ? t->from->pid : 0, cmd, |
| 2471 | t->buffer->data_size, t->buffer->offsets_size, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2472 | (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2473 | |
| 2474 | list_del(&t->work.entry); |
| 2475 | t->buffer->allow_user_free = 1; |
| 2476 | if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) { |
| 2477 | t->to_parent = thread->transaction_stack; |
| 2478 | t->to_thread = thread; |
| 2479 | thread->transaction_stack = t; |
| 2480 | } else { |
| 2481 | t->buffer->transaction = NULL; |
| 2482 | kfree(t); |
| 2483 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 2484 | } |
| 2485 | break; |
| 2486 | } |
| 2487 | |
| 2488 | done: |
| 2489 | |
| 2490 | *consumed = ptr - buffer; |
| 2491 | if (proc->requested_threads + proc->ready_threads == 0 && |
| 2492 | proc->requested_threads_started < proc->max_threads && |
| 2493 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 2494 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ |
| 2495 | /*spawn a new thread if we leave this out */) { |
| 2496 | proc->requested_threads++; |
| 2497 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2498 | "%d:%d BR_SPAWN_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2499 | proc->pid, thread->pid); |
| 2500 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) |
| 2501 | return -EFAULT; |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 2502 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2503 | } |
| 2504 | return 0; |
| 2505 | } |
| 2506 | |
| 2507 | static void binder_release_work(struct list_head *list) |
| 2508 | { |
| 2509 | struct binder_work *w; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2510 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2511 | while (!list_empty(list)) { |
| 2512 | w = list_first_entry(list, struct binder_work, entry); |
| 2513 | list_del_init(&w->entry); |
| 2514 | switch (w->type) { |
| 2515 | case BINDER_WORK_TRANSACTION: { |
| 2516 | struct binder_transaction *t; |
| 2517 | |
| 2518 | t = container_of(w, struct binder_transaction, work); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 2519 | if (t->buffer->target_node && |
| 2520 | !(t->flags & TF_ONE_WAY)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2521 | binder_send_failed_reply(t, BR_DEAD_REPLY); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 2522 | } else { |
| 2523 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2524 | "undelivered transaction %d\n", |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 2525 | t->debug_id); |
| 2526 | t->buffer->transaction = NULL; |
| 2527 | kfree(t); |
| 2528 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 2529 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2530 | } break; |
| 2531 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 2532 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2533 | "undelivered TRANSACTION_COMPLETE\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2534 | kfree(w); |
| 2535 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 2536 | } break; |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 2537 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 2538 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 2539 | struct binder_ref_death *death; |
| 2540 | |
| 2541 | death = container_of(w, struct binder_ref_death, work); |
| 2542 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2543 | "undelivered death notification, %016llx\n", |
| 2544 | (u64)death->cookie); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 2545 | kfree(death); |
| 2546 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 2547 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2548 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2549 | pr_err("unexpected work type, %d, not freed\n", |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 2550 | w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2551 | break; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | } |
| 2556 | |
| 2557 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) |
| 2558 | { |
| 2559 | struct binder_thread *thread = NULL; |
| 2560 | struct rb_node *parent = NULL; |
| 2561 | struct rb_node **p = &proc->threads.rb_node; |
| 2562 | |
| 2563 | while (*p) { |
| 2564 | parent = *p; |
| 2565 | thread = rb_entry(parent, struct binder_thread, rb_node); |
| 2566 | |
| 2567 | if (current->pid < thread->pid) |
| 2568 | p = &(*p)->rb_left; |
| 2569 | else if (current->pid > thread->pid) |
| 2570 | p = &(*p)->rb_right; |
| 2571 | else |
| 2572 | break; |
| 2573 | } |
| 2574 | if (*p == NULL) { |
| 2575 | thread = kzalloc(sizeof(*thread), GFP_KERNEL); |
| 2576 | if (thread == NULL) |
| 2577 | return NULL; |
| 2578 | binder_stats_created(BINDER_STAT_THREAD); |
| 2579 | thread->proc = proc; |
| 2580 | thread->pid = current->pid; |
| 2581 | init_waitqueue_head(&thread->wait); |
| 2582 | INIT_LIST_HEAD(&thread->todo); |
| 2583 | rb_link_node(&thread->rb_node, parent, p); |
| 2584 | rb_insert_color(&thread->rb_node, &proc->threads); |
| 2585 | thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN; |
| 2586 | thread->return_error = BR_OK; |
| 2587 | thread->return_error2 = BR_OK; |
| 2588 | } |
| 2589 | return thread; |
| 2590 | } |
| 2591 | |
| 2592 | static int binder_free_thread(struct binder_proc *proc, |
| 2593 | struct binder_thread *thread) |
| 2594 | { |
| 2595 | struct binder_transaction *t; |
| 2596 | struct binder_transaction *send_reply = NULL; |
| 2597 | int active_transactions = 0; |
| 2598 | |
| 2599 | rb_erase(&thread->rb_node, &proc->threads); |
| 2600 | t = thread->transaction_stack; |
| 2601 | if (t && t->to_thread == thread) |
| 2602 | send_reply = t; |
| 2603 | while (t) { |
| 2604 | active_transactions++; |
| 2605 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2606 | "release %d:%d transaction %d %s, still active\n", |
| 2607 | proc->pid, thread->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2608 | t->debug_id, |
| 2609 | (t->to_thread == thread) ? "in" : "out"); |
| 2610 | |
| 2611 | if (t->to_thread == thread) { |
| 2612 | t->to_proc = NULL; |
| 2613 | t->to_thread = NULL; |
| 2614 | if (t->buffer) { |
| 2615 | t->buffer->transaction = NULL; |
| 2616 | t->buffer = NULL; |
| 2617 | } |
| 2618 | t = t->to_parent; |
| 2619 | } else if (t->from == thread) { |
| 2620 | t->from = NULL; |
| 2621 | t = t->from_parent; |
| 2622 | } else |
| 2623 | BUG(); |
| 2624 | } |
| 2625 | if (send_reply) |
| 2626 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); |
| 2627 | binder_release_work(&thread->todo); |
| 2628 | kfree(thread); |
| 2629 | binder_stats_deleted(BINDER_STAT_THREAD); |
| 2630 | return active_transactions; |
| 2631 | } |
| 2632 | |
| 2633 | static unsigned int binder_poll(struct file *filp, |
| 2634 | struct poll_table_struct *wait) |
| 2635 | { |
| 2636 | struct binder_proc *proc = filp->private_data; |
| 2637 | struct binder_thread *thread = NULL; |
| 2638 | int wait_for_proc_work; |
| 2639 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2640 | binder_lock(__func__); |
| 2641 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2642 | thread = binder_get_thread(proc); |
Eric Biggers | febf108 | 2018-02-26 10:56:45 -0800 | [diff] [blame] | 2643 | if (!thread) { |
| 2644 | binder_unlock(__func__); |
Eric Biggers | 4be5a28 | 2018-01-30 23:11:24 -0800 | [diff] [blame] | 2645 | return POLLERR; |
Eric Biggers | febf108 | 2018-02-26 10:56:45 -0800 | [diff] [blame] | 2646 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2647 | |
| 2648 | wait_for_proc_work = thread->transaction_stack == NULL && |
| 2649 | list_empty(&thread->todo) && thread->return_error == BR_OK; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2650 | |
| 2651 | binder_unlock(__func__); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2652 | |
| 2653 | if (wait_for_proc_work) { |
| 2654 | if (binder_has_proc_work(proc, thread)) |
| 2655 | return POLLIN; |
| 2656 | poll_wait(filp, &proc->wait, wait); |
| 2657 | if (binder_has_proc_work(proc, thread)) |
| 2658 | return POLLIN; |
| 2659 | } else { |
| 2660 | if (binder_has_thread_work(thread)) |
| 2661 | return POLLIN; |
| 2662 | poll_wait(filp, &thread->wait, wait); |
| 2663 | if (binder_has_thread_work(thread)) |
| 2664 | return POLLIN; |
| 2665 | } |
| 2666 | return 0; |
| 2667 | } |
| 2668 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 2669 | static int binder_ioctl_write_read(struct file *filp, |
| 2670 | unsigned int cmd, unsigned long arg, |
| 2671 | struct binder_thread *thread) |
| 2672 | { |
| 2673 | int ret = 0; |
| 2674 | struct binder_proc *proc = filp->private_data; |
| 2675 | unsigned int size = _IOC_SIZE(cmd); |
| 2676 | void __user *ubuf = (void __user *)arg; |
| 2677 | struct binder_write_read bwr; |
| 2678 | |
| 2679 | if (size != sizeof(struct binder_write_read)) { |
| 2680 | ret = -EINVAL; |
| 2681 | goto out; |
| 2682 | } |
| 2683 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { |
| 2684 | ret = -EFAULT; |
| 2685 | goto out; |
| 2686 | } |
| 2687 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 2688 | "%d:%d write %lld at %016llx, read %lld at %016llx\n", |
| 2689 | proc->pid, thread->pid, |
| 2690 | (u64)bwr.write_size, (u64)bwr.write_buffer, |
| 2691 | (u64)bwr.read_size, (u64)bwr.read_buffer); |
| 2692 | |
| 2693 | if (bwr.write_size > 0) { |
| 2694 | ret = binder_thread_write(proc, thread, |
| 2695 | bwr.write_buffer, |
| 2696 | bwr.write_size, |
| 2697 | &bwr.write_consumed); |
| 2698 | trace_binder_write_done(ret); |
| 2699 | if (ret < 0) { |
| 2700 | bwr.read_consumed = 0; |
| 2701 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 2702 | ret = -EFAULT; |
| 2703 | goto out; |
| 2704 | } |
| 2705 | } |
| 2706 | if (bwr.read_size > 0) { |
| 2707 | ret = binder_thread_read(proc, thread, bwr.read_buffer, |
| 2708 | bwr.read_size, |
| 2709 | &bwr.read_consumed, |
| 2710 | filp->f_flags & O_NONBLOCK); |
| 2711 | trace_binder_read_done(ret); |
| 2712 | if (!list_empty(&proc->todo)) |
| 2713 | wake_up_interruptible(&proc->wait); |
| 2714 | if (ret < 0) { |
| 2715 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 2716 | ret = -EFAULT; |
| 2717 | goto out; |
| 2718 | } |
| 2719 | } |
| 2720 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 2721 | "%d:%d wrote %lld of %lld, read return %lld of %lld\n", |
| 2722 | proc->pid, thread->pid, |
| 2723 | (u64)bwr.write_consumed, (u64)bwr.write_size, |
| 2724 | (u64)bwr.read_consumed, (u64)bwr.read_size); |
| 2725 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { |
| 2726 | ret = -EFAULT; |
| 2727 | goto out; |
| 2728 | } |
| 2729 | out: |
| 2730 | return ret; |
| 2731 | } |
| 2732 | |
| 2733 | static int binder_ioctl_set_ctx_mgr(struct file *filp) |
| 2734 | { |
| 2735 | int ret = 0; |
| 2736 | struct binder_proc *proc = filp->private_data; |
| 2737 | kuid_t curr_euid = current_euid(); |
| 2738 | |
| 2739 | if (binder_context_mgr_node != NULL) { |
| 2740 | pr_err("BINDER_SET_CONTEXT_MGR already set\n"); |
| 2741 | ret = -EBUSY; |
| 2742 | goto out; |
| 2743 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2744 | ret = security_binder_set_context_mgr(proc->tsk); |
| 2745 | if (ret < 0) |
| 2746 | goto out; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 2747 | if (uid_valid(binder_context_mgr_uid)) { |
| 2748 | if (!uid_eq(binder_context_mgr_uid, curr_euid)) { |
| 2749 | pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n", |
| 2750 | from_kuid(&init_user_ns, curr_euid), |
| 2751 | from_kuid(&init_user_ns, |
| 2752 | binder_context_mgr_uid)); |
| 2753 | ret = -EPERM; |
| 2754 | goto out; |
| 2755 | } |
| 2756 | } else { |
| 2757 | binder_context_mgr_uid = curr_euid; |
| 2758 | } |
| 2759 | binder_context_mgr_node = binder_new_node(proc, 0, 0); |
| 2760 | if (binder_context_mgr_node == NULL) { |
| 2761 | ret = -ENOMEM; |
| 2762 | goto out; |
| 2763 | } |
| 2764 | binder_context_mgr_node->local_weak_refs++; |
| 2765 | binder_context_mgr_node->local_strong_refs++; |
| 2766 | binder_context_mgr_node->has_strong_ref = 1; |
| 2767 | binder_context_mgr_node->has_weak_ref = 1; |
| 2768 | out: |
| 2769 | return ret; |
| 2770 | } |
| 2771 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2772 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 2773 | { |
| 2774 | int ret; |
| 2775 | struct binder_proc *proc = filp->private_data; |
| 2776 | struct binder_thread *thread; |
| 2777 | unsigned int size = _IOC_SIZE(cmd); |
| 2778 | void __user *ubuf = (void __user *)arg; |
| 2779 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 2780 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", |
| 2781 | proc->pid, current->pid, cmd, arg);*/ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2782 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2783 | trace_binder_ioctl(cmd, arg); |
| 2784 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2785 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 2786 | if (ret) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2787 | goto err_unlocked; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2788 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2789 | binder_lock(__func__); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2790 | thread = binder_get_thread(proc); |
| 2791 | if (thread == NULL) { |
| 2792 | ret = -ENOMEM; |
| 2793 | goto err; |
| 2794 | } |
| 2795 | |
| 2796 | switch (cmd) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 2797 | case BINDER_WRITE_READ: |
| 2798 | ret = binder_ioctl_write_read(filp, cmd, arg, thread); |
| 2799 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2800 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2801 | break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2802 | case BINDER_SET_MAX_THREADS: |
| 2803 | if (copy_from_user(&proc->max_threads, ubuf, sizeof(proc->max_threads))) { |
| 2804 | ret = -EINVAL; |
| 2805 | goto err; |
| 2806 | } |
| 2807 | break; |
| 2808 | case BINDER_SET_CONTEXT_MGR: |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 2809 | ret = binder_ioctl_set_ctx_mgr(filp); |
| 2810 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2811 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2812 | break; |
| 2813 | case BINDER_THREAD_EXIT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2814 | binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2815 | proc->pid, thread->pid); |
| 2816 | binder_free_thread(proc, thread); |
| 2817 | thread = NULL; |
| 2818 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 2819 | case BINDER_VERSION: { |
| 2820 | struct binder_version __user *ver = ubuf; |
| 2821 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2822 | if (size != sizeof(struct binder_version)) { |
| 2823 | ret = -EINVAL; |
| 2824 | goto err; |
| 2825 | } |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 2826 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, |
| 2827 | &ver->protocol_version)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2828 | ret = -EINVAL; |
| 2829 | goto err; |
| 2830 | } |
| 2831 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 2832 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2833 | default: |
| 2834 | ret = -EINVAL; |
| 2835 | goto err; |
| 2836 | } |
| 2837 | ret = 0; |
| 2838 | err: |
| 2839 | if (thread) |
| 2840 | thread->looper &= ~BINDER_LOOPER_STATE_NEED_RETURN; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2841 | binder_unlock(__func__); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2842 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 2843 | if (ret && ret != -ERESTARTSYS) |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2844 | pr_info("%d:%d ioctl %x %lx returned %d\n", proc->pid, current->pid, cmd, arg, ret); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2845 | err_unlocked: |
| 2846 | trace_binder_ioctl_done(ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2847 | return ret; |
| 2848 | } |
| 2849 | |
| 2850 | static void binder_vma_open(struct vm_area_struct *vma) |
| 2851 | { |
| 2852 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2853 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2854 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2855 | "%d open vm area %lx-%lx (%ld K) vma %lx pagep %lx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2856 | proc->pid, vma->vm_start, vma->vm_end, |
| 2857 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 2858 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | static void binder_vma_close(struct vm_area_struct *vma) |
| 2862 | { |
| 2863 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2864 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2865 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2866 | "%d close vm area %lx-%lx (%ld K) vma %lx pagep %lx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2867 | proc->pid, vma->vm_start, vma->vm_end, |
| 2868 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 2869 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 2870 | proc->vma = NULL; |
Arve Hjønnevåg | 2a90957 | 2012-03-08 15:43:36 -0800 | [diff] [blame] | 2871 | proc->vma_vm_mm = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2872 | binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES); |
| 2873 | } |
| 2874 | |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 2875 | static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 2876 | { |
| 2877 | return VM_FAULT_SIGBUS; |
| 2878 | } |
| 2879 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 2880 | static const struct vm_operations_struct binder_vm_ops = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2881 | .open = binder_vma_open, |
| 2882 | .close = binder_vma_close, |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 2883 | .fault = binder_vm_fault, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2884 | }; |
| 2885 | |
| 2886 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) |
| 2887 | { |
| 2888 | int ret; |
| 2889 | struct vm_struct *area; |
| 2890 | struct binder_proc *proc = filp->private_data; |
| 2891 | const char *failure_string; |
| 2892 | struct binder_buffer *buffer; |
| 2893 | |
Martijn Coenen | cbd854d | 2017-07-28 13:56:08 +0200 | [diff] [blame] | 2894 | if (proc->tsk != current->group_leader) |
Al Viro | a79f41e | 2012-08-15 18:23:36 -0400 | [diff] [blame] | 2895 | return -EINVAL; |
| 2896 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2897 | if ((vma->vm_end - vma->vm_start) > SZ_4M) |
| 2898 | vma->vm_end = vma->vm_start + SZ_4M; |
| 2899 | |
| 2900 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 2901 | "binder_mmap: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 2902 | proc->pid, vma->vm_start, vma->vm_end, |
| 2903 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 2904 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 2905 | |
| 2906 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { |
| 2907 | ret = -EPERM; |
| 2908 | failure_string = "bad vm_flags"; |
| 2909 | goto err_bad_arg; |
| 2910 | } |
| 2911 | vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE; |
| 2912 | |
Arve Hjønnevåg | bd1eff9 | 2012-02-01 15:29:13 -0800 | [diff] [blame] | 2913 | mutex_lock(&binder_mmap_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2914 | if (proc->buffer) { |
| 2915 | ret = -EBUSY; |
| 2916 | failure_string = "already mapped"; |
| 2917 | goto err_already_mapped; |
| 2918 | } |
| 2919 | |
| 2920 | area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP); |
| 2921 | if (area == NULL) { |
| 2922 | ret = -ENOMEM; |
| 2923 | failure_string = "get_vm_area"; |
| 2924 | goto err_get_vm_area_failed; |
| 2925 | } |
| 2926 | proc->buffer = area->addr; |
| 2927 | proc->user_buffer_offset = vma->vm_start - (uintptr_t)proc->buffer; |
Arve Hjønnevåg | bd1eff9 | 2012-02-01 15:29:13 -0800 | [diff] [blame] | 2928 | mutex_unlock(&binder_mmap_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2929 | |
| 2930 | #ifdef CONFIG_CPU_CACHE_VIPT |
| 2931 | if (cache_is_vipt_aliasing()) { |
| 2932 | while (CACHE_COLOUR((vma->vm_start ^ (uint32_t)proc->buffer))) { |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 2933 | pr_info("binder_mmap: %d %lx-%lx maps %pK bad alignment\n", proc->pid, vma->vm_start, vma->vm_end, proc->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2934 | vma->vm_start += PAGE_SIZE; |
| 2935 | } |
| 2936 | } |
| 2937 | #endif |
| 2938 | proc->pages = kzalloc(sizeof(proc->pages[0]) * ((vma->vm_end - vma->vm_start) / PAGE_SIZE), GFP_KERNEL); |
| 2939 | if (proc->pages == NULL) { |
| 2940 | ret = -ENOMEM; |
| 2941 | failure_string = "alloc page array"; |
| 2942 | goto err_alloc_pages_failed; |
| 2943 | } |
| 2944 | proc->buffer_size = vma->vm_end - vma->vm_start; |
| 2945 | |
| 2946 | vma->vm_ops = &binder_vm_ops; |
| 2947 | vma->vm_private_data = proc; |
| 2948 | |
| 2949 | if (binder_update_page_range(proc, 1, proc->buffer, proc->buffer + PAGE_SIZE, vma)) { |
| 2950 | ret = -ENOMEM; |
| 2951 | failure_string = "alloc small buf"; |
| 2952 | goto err_alloc_small_buf_failed; |
| 2953 | } |
| 2954 | buffer = proc->buffer; |
| 2955 | INIT_LIST_HEAD(&proc->buffers); |
| 2956 | list_add(&buffer->entry, &proc->buffers); |
| 2957 | buffer->free = 1; |
| 2958 | binder_insert_free_buffer(proc, buffer); |
| 2959 | proc->free_async_space = proc->buffer_size / 2; |
| 2960 | barrier(); |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 2961 | mutex_lock(&proc->files_lock); |
Al Viro | a79f41e | 2012-08-15 18:23:36 -0400 | [diff] [blame] | 2962 | proc->files = get_files_struct(current); |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 2963 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2964 | proc->vma = vma; |
Arve Hjønnevåg | 2a90957 | 2012-03-08 15:43:36 -0800 | [diff] [blame] | 2965 | proc->vma_vm_mm = vma->vm_mm; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2966 | |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 2967 | /*pr_info("binder_mmap: %d %lx-%lx maps %p\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2968 | proc->pid, vma->vm_start, vma->vm_end, proc->buffer);*/ |
| 2969 | return 0; |
| 2970 | |
| 2971 | err_alloc_small_buf_failed: |
| 2972 | kfree(proc->pages); |
| 2973 | proc->pages = NULL; |
| 2974 | err_alloc_pages_failed: |
Arve Hjønnevåg | bd1eff9 | 2012-02-01 15:29:13 -0800 | [diff] [blame] | 2975 | mutex_lock(&binder_mmap_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2976 | vfree(proc->buffer); |
| 2977 | proc->buffer = NULL; |
| 2978 | err_get_vm_area_failed: |
| 2979 | err_already_mapped: |
Arve Hjønnevåg | bd1eff9 | 2012-02-01 15:29:13 -0800 | [diff] [blame] | 2980 | mutex_unlock(&binder_mmap_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2981 | err_bad_arg: |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 2982 | pr_err("binder_mmap: %d %lx-%lx %s failed %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2983 | proc->pid, vma->vm_start, vma->vm_end, failure_string, ret); |
| 2984 | return ret; |
| 2985 | } |
| 2986 | |
| 2987 | static int binder_open(struct inode *nodp, struct file *filp) |
| 2988 | { |
| 2989 | struct binder_proc *proc; |
| 2990 | |
| 2991 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n", |
| 2992 | current->group_leader->pid, current->pid); |
| 2993 | |
| 2994 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 2995 | if (proc == NULL) |
| 2996 | return -ENOMEM; |
Todd Kjos | 5105075 | 2017-06-29 12:01:36 -0700 | [diff] [blame] | 2997 | get_task_struct(current->group_leader); |
| 2998 | proc->tsk = current->group_leader; |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 2999 | mutex_init(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3000 | INIT_LIST_HEAD(&proc->todo); |
| 3001 | init_waitqueue_head(&proc->wait); |
| 3002 | proc->default_priority = task_nice(current); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3003 | |
| 3004 | binder_lock(__func__); |
| 3005 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3006 | binder_stats_created(BINDER_STAT_PROC); |
| 3007 | hlist_add_head(&proc->proc_node, &binder_procs); |
| 3008 | proc->pid = current->group_leader->pid; |
| 3009 | INIT_LIST_HEAD(&proc->delivered_death); |
| 3010 | filp->private_data = proc; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3011 | |
| 3012 | binder_unlock(__func__); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3013 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3014 | if (binder_debugfs_dir_entry_proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3015 | char strbuf[11]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3016 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3017 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3018 | proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO, |
| 3019 | binder_debugfs_dir_entry_proc, proc, &binder_proc_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | return 0; |
| 3023 | } |
| 3024 | |
| 3025 | static int binder_flush(struct file *filp, fl_owner_t id) |
| 3026 | { |
| 3027 | struct binder_proc *proc = filp->private_data; |
| 3028 | |
| 3029 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); |
| 3030 | |
| 3031 | return 0; |
| 3032 | } |
| 3033 | |
| 3034 | static void binder_deferred_flush(struct binder_proc *proc) |
| 3035 | { |
| 3036 | struct rb_node *n; |
| 3037 | int wake_count = 0; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3038 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3039 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 3040 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3041 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3042 | thread->looper |= BINDER_LOOPER_STATE_NEED_RETURN; |
| 3043 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { |
| 3044 | wake_up_interruptible(&thread->wait); |
| 3045 | wake_count++; |
| 3046 | } |
| 3047 | } |
| 3048 | wake_up_interruptible_all(&proc->wait); |
| 3049 | |
| 3050 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 3051 | "binder_flush: %d woke %d threads\n", proc->pid, |
| 3052 | wake_count); |
| 3053 | } |
| 3054 | |
| 3055 | static int binder_release(struct inode *nodp, struct file *filp) |
| 3056 | { |
| 3057 | struct binder_proc *proc = filp->private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3058 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3059 | debugfs_remove(proc->debugfs_entry); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3060 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); |
| 3061 | |
| 3062 | return 0; |
| 3063 | } |
| 3064 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 3065 | static int binder_node_release(struct binder_node *node, int refs) |
| 3066 | { |
| 3067 | struct binder_ref *ref; |
| 3068 | int death = 0; |
| 3069 | |
| 3070 | list_del_init(&node->work.entry); |
| 3071 | binder_release_work(&node->async_todo); |
| 3072 | |
| 3073 | if (hlist_empty(&node->refs)) { |
| 3074 | kfree(node); |
| 3075 | binder_stats_deleted(BINDER_STAT_NODE); |
| 3076 | |
| 3077 | return refs; |
| 3078 | } |
| 3079 | |
| 3080 | node->proc = NULL; |
| 3081 | node->local_strong_refs = 0; |
| 3082 | node->local_weak_refs = 0; |
| 3083 | hlist_add_head(&node->dead_node, &binder_dead_nodes); |
| 3084 | |
| 3085 | hlist_for_each_entry(ref, &node->refs, node_entry) { |
| 3086 | refs++; |
| 3087 | |
| 3088 | if (!ref->death) |
Arve Hjønnevåg | e194fd8 | 2014-02-17 13:58:29 -0800 | [diff] [blame] | 3089 | continue; |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 3090 | |
| 3091 | death++; |
| 3092 | |
| 3093 | if (list_empty(&ref->death->work.entry)) { |
| 3094 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 3095 | list_add_tail(&ref->death->work.entry, |
| 3096 | &ref->proc->todo); |
| 3097 | wake_up_interruptible(&ref->proc->wait); |
| 3098 | } else |
| 3099 | BUG(); |
| 3100 | } |
| 3101 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 3102 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 3103 | "node %d now dead, refs %d, death %d\n", |
| 3104 | node->debug_id, refs, death); |
| 3105 | |
| 3106 | return refs; |
| 3107 | } |
| 3108 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3109 | static void binder_deferred_release(struct binder_proc *proc) |
| 3110 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3111 | struct binder_transaction *t; |
| 3112 | struct rb_node *n; |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3113 | int threads, nodes, incoming_refs, outgoing_refs, buffers, |
| 3114 | active_transactions, page_count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3115 | |
| 3116 | BUG_ON(proc->vma); |
| 3117 | BUG_ON(proc->files); |
| 3118 | |
| 3119 | hlist_del(&proc->proc_node); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3120 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3121 | if (binder_context_mgr_node && binder_context_mgr_node->proc == proc) { |
| 3122 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 3123 | "%s: %d context_mgr_node gone\n", |
| 3124 | __func__, proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3125 | binder_context_mgr_node = NULL; |
| 3126 | } |
| 3127 | |
| 3128 | threads = 0; |
| 3129 | active_transactions = 0; |
| 3130 | while ((n = rb_first(&proc->threads))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3131 | struct binder_thread *thread; |
| 3132 | |
| 3133 | thread = rb_entry(n, struct binder_thread, rb_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3134 | threads++; |
| 3135 | active_transactions += binder_free_thread(proc, thread); |
| 3136 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3137 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3138 | nodes = 0; |
| 3139 | incoming_refs = 0; |
| 3140 | while ((n = rb_first(&proc->nodes))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3141 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3142 | |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3143 | node = rb_entry(n, struct binder_node, rb_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3144 | nodes++; |
| 3145 | rb_erase(&node->rb_node, &proc->nodes); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 3146 | incoming_refs = binder_node_release(node, incoming_refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3147 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3148 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3149 | outgoing_refs = 0; |
| 3150 | while ((n = rb_first(&proc->refs_by_desc))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3151 | struct binder_ref *ref; |
| 3152 | |
| 3153 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3154 | outgoing_refs++; |
| 3155 | binder_delete_ref(ref); |
| 3156 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3157 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3158 | binder_release_work(&proc->todo); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 3159 | binder_release_work(&proc->delivered_death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3160 | |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3161 | buffers = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3162 | while ((n = rb_first(&proc->allocated_buffers))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3163 | struct binder_buffer *buffer; |
| 3164 | |
| 3165 | buffer = rb_entry(n, struct binder_buffer, rb_node); |
| 3166 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3167 | t = buffer->transaction; |
| 3168 | if (t) { |
| 3169 | t->buffer = NULL; |
| 3170 | buffer->transaction = NULL; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3171 | pr_err("release proc %d, transaction %d, not freed\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3172 | proc->pid, t->debug_id); |
| 3173 | /*BUG();*/ |
| 3174 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3175 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3176 | binder_free_buf(proc, buffer); |
| 3177 | buffers++; |
| 3178 | } |
| 3179 | |
| 3180 | binder_stats_deleted(BINDER_STAT_PROC); |
| 3181 | |
| 3182 | page_count = 0; |
| 3183 | if (proc->pages) { |
| 3184 | int i; |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 3185 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3186 | for (i = 0; i < proc->buffer_size / PAGE_SIZE; i++) { |
Mirsal Ennaime | ba97bc5 | 2013-03-12 11:42:01 +0100 | [diff] [blame] | 3187 | void *page_addr; |
| 3188 | |
| 3189 | if (!proc->pages[i]) |
| 3190 | continue; |
| 3191 | |
| 3192 | page_addr = proc->buffer + i * PAGE_SIZE; |
| 3193 | binder_debug(BINDER_DEBUG_BUFFER_ALLOC, |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 3194 | "%s: %d: page %d at %pK not freed\n", |
Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 3195 | __func__, proc->pid, i, page_addr); |
Mirsal Ennaime | ba97bc5 | 2013-03-12 11:42:01 +0100 | [diff] [blame] | 3196 | unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE); |
| 3197 | __free_page(proc->pages[i]); |
| 3198 | page_count++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3199 | } |
| 3200 | kfree(proc->pages); |
| 3201 | vfree(proc->buffer); |
| 3202 | } |
| 3203 | |
| 3204 | put_task_struct(proc->tsk); |
| 3205 | |
| 3206 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 3207 | "%s: %d threads %d, nodes %d (ref %d), refs %d, active transactions %d, buffers %d, pages %d\n", |
| 3208 | __func__, proc->pid, threads, nodes, incoming_refs, |
| 3209 | outgoing_refs, active_transactions, buffers, page_count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3210 | |
| 3211 | kfree(proc); |
| 3212 | } |
| 3213 | |
| 3214 | static void binder_deferred_func(struct work_struct *work) |
| 3215 | { |
| 3216 | struct binder_proc *proc; |
| 3217 | struct files_struct *files; |
| 3218 | |
| 3219 | int defer; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3220 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3221 | do { |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3222 | binder_lock(__func__); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3223 | mutex_lock(&binder_deferred_lock); |
| 3224 | if (!hlist_empty(&binder_deferred_list)) { |
| 3225 | proc = hlist_entry(binder_deferred_list.first, |
| 3226 | struct binder_proc, deferred_work_node); |
| 3227 | hlist_del_init(&proc->deferred_work_node); |
| 3228 | defer = proc->deferred_work; |
| 3229 | proc->deferred_work = 0; |
| 3230 | } else { |
| 3231 | proc = NULL; |
| 3232 | defer = 0; |
| 3233 | } |
| 3234 | mutex_unlock(&binder_deferred_lock); |
| 3235 | |
| 3236 | files = NULL; |
| 3237 | if (defer & BINDER_DEFERRED_PUT_FILES) { |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 3238 | mutex_lock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3239 | files = proc->files; |
| 3240 | if (files) |
| 3241 | proc->files = NULL; |
Todd Kjos | c0d75da | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 3242 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | if (defer & BINDER_DEFERRED_FLUSH) |
| 3246 | binder_deferred_flush(proc); |
| 3247 | |
| 3248 | if (defer & BINDER_DEFERRED_RELEASE) |
| 3249 | binder_deferred_release(proc); /* frees proc */ |
| 3250 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3251 | binder_unlock(__func__); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3252 | if (files) |
| 3253 | put_files_struct(files); |
| 3254 | } while (proc); |
| 3255 | } |
| 3256 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); |
| 3257 | |
| 3258 | static void |
| 3259 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) |
| 3260 | { |
| 3261 | mutex_lock(&binder_deferred_lock); |
| 3262 | proc->deferred_work |= defer; |
| 3263 | if (hlist_unhashed(&proc->deferred_work_node)) { |
| 3264 | hlist_add_head(&proc->deferred_work_node, |
| 3265 | &binder_deferred_list); |
Bhaktipriya Shridhar | 1beba52 | 2016-08-13 22:16:24 +0530 | [diff] [blame] | 3266 | schedule_work(&binder_deferred_work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3267 | } |
| 3268 | mutex_unlock(&binder_deferred_lock); |
| 3269 | } |
| 3270 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3271 | static void print_binder_transaction(struct seq_file *m, const char *prefix, |
| 3272 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3273 | { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3274 | seq_printf(m, |
Todd Kjos | 6f3433c | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 3275 | "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d", |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3276 | prefix, t->debug_id, t, |
| 3277 | t->from ? t->from->proc->pid : 0, |
| 3278 | t->from ? t->from->pid : 0, |
| 3279 | t->to_proc ? t->to_proc->pid : 0, |
| 3280 | t->to_thread ? t->to_thread->pid : 0, |
| 3281 | t->code, t->flags, t->priority, t->need_reply); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3282 | if (t->buffer == NULL) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3283 | seq_puts(m, " buffer free\n"); |
| 3284 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3285 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3286 | if (t->buffer->target_node) |
| 3287 | seq_printf(m, " node %d", |
| 3288 | t->buffer->target_node->debug_id); |
Todd Kjos | 6f3433c | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 3289 | seq_printf(m, " size %zd:%zd data %pK\n", |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3290 | t->buffer->data_size, t->buffer->offsets_size, |
| 3291 | t->buffer->data); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3292 | } |
| 3293 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3294 | static void print_binder_buffer(struct seq_file *m, const char *prefix, |
| 3295 | struct binder_buffer *buffer) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3296 | { |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 3297 | seq_printf(m, "%s %d: %pK size %zd:%zd %s\n", |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3298 | prefix, buffer->debug_id, buffer->data, |
| 3299 | buffer->data_size, buffer->offsets_size, |
| 3300 | buffer->transaction ? "active" : "delivered"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3301 | } |
| 3302 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3303 | static void print_binder_work(struct seq_file *m, const char *prefix, |
| 3304 | const char *transaction_prefix, |
| 3305 | struct binder_work *w) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3306 | { |
| 3307 | struct binder_node *node; |
| 3308 | struct binder_transaction *t; |
| 3309 | |
| 3310 | switch (w->type) { |
| 3311 | case BINDER_WORK_TRANSACTION: |
| 3312 | t = container_of(w, struct binder_transaction, work); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3313 | print_binder_transaction(m, transaction_prefix, t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3314 | break; |
| 3315 | case BINDER_WORK_TRANSACTION_COMPLETE: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3316 | seq_printf(m, "%stransaction complete\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3317 | break; |
| 3318 | case BINDER_WORK_NODE: |
| 3319 | node = container_of(w, struct binder_node, work); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3320 | seq_printf(m, "%snode work %d: u%016llx c%016llx\n", |
| 3321 | prefix, node->debug_id, |
| 3322 | (u64)node->ptr, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3323 | break; |
| 3324 | case BINDER_WORK_DEAD_BINDER: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3325 | seq_printf(m, "%shas dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3326 | break; |
| 3327 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3328 | seq_printf(m, "%shas cleared dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3329 | break; |
| 3330 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3331 | seq_printf(m, "%shas cleared death notification\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3332 | break; |
| 3333 | default: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3334 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3335 | break; |
| 3336 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3337 | } |
| 3338 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3339 | static void print_binder_thread(struct seq_file *m, |
| 3340 | struct binder_thread *thread, |
| 3341 | int print_always) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3342 | { |
| 3343 | struct binder_transaction *t; |
| 3344 | struct binder_work *w; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3345 | size_t start_pos = m->count; |
| 3346 | size_t header_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3347 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3348 | seq_printf(m, " thread %d: l %02x\n", thread->pid, thread->looper); |
| 3349 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3350 | t = thread->transaction_stack; |
| 3351 | while (t) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3352 | if (t->from == thread) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3353 | print_binder_transaction(m, |
| 3354 | " outgoing transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3355 | t = t->from_parent; |
| 3356 | } else if (t->to_thread == thread) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3357 | print_binder_transaction(m, |
| 3358 | " incoming transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3359 | t = t->to_parent; |
| 3360 | } else { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3361 | print_binder_transaction(m, " bad transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3362 | t = NULL; |
| 3363 | } |
| 3364 | } |
| 3365 | list_for_each_entry(w, &thread->todo, entry) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3366 | print_binder_work(m, " ", " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3367 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3368 | if (!print_always && m->count == header_pos) |
| 3369 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3370 | } |
| 3371 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3372 | static void print_binder_node(struct seq_file *m, struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3373 | { |
| 3374 | struct binder_ref *ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3375 | struct binder_work *w; |
| 3376 | int count; |
| 3377 | |
| 3378 | count = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3379 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3380 | count++; |
| 3381 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3382 | seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d", |
| 3383 | node->debug_id, (u64)node->ptr, (u64)node->cookie, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3384 | node->has_strong_ref, node->has_weak_ref, |
| 3385 | node->local_strong_refs, node->local_weak_refs, |
| 3386 | node->internal_strong_refs, count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3387 | if (count) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3388 | seq_puts(m, " proc"); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3389 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3390 | seq_printf(m, " %d", ref->proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3391 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3392 | seq_puts(m, "\n"); |
| 3393 | list_for_each_entry(w, &node->async_todo, entry) |
| 3394 | print_binder_work(m, " ", |
| 3395 | " pending async transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3396 | } |
| 3397 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3398 | static void print_binder_ref(struct seq_file *m, struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3399 | { |
Ben Hutchings | 9cd1447 | 2019-05-29 18:02:44 +0100 | [diff] [blame] | 3400 | seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n", |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3401 | ref->debug_id, ref->desc, ref->node->proc ? "" : "dead ", |
| 3402 | ref->node->debug_id, ref->strong, ref->weak, ref->death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3403 | } |
| 3404 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3405 | static void print_binder_proc(struct seq_file *m, |
| 3406 | struct binder_proc *proc, int print_all) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3407 | { |
| 3408 | struct binder_work *w; |
| 3409 | struct rb_node *n; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3410 | size_t start_pos = m->count; |
| 3411 | size_t header_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3412 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3413 | seq_printf(m, "proc %d\n", proc->pid); |
| 3414 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3415 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3416 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 3417 | print_binder_thread(m, rb_entry(n, struct binder_thread, |
| 3418 | rb_node), print_all); |
| 3419 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3420 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 3421 | rb_node); |
| 3422 | if (print_all || node->has_async_transaction) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3423 | print_binder_node(m, node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3424 | } |
| 3425 | if (print_all) { |
| 3426 | for (n = rb_first(&proc->refs_by_desc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3427 | n != NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3428 | n = rb_next(n)) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3429 | print_binder_ref(m, rb_entry(n, struct binder_ref, |
| 3430 | rb_node_desc)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3431 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3432 | for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n)) |
| 3433 | print_binder_buffer(m, " buffer", |
| 3434 | rb_entry(n, struct binder_buffer, rb_node)); |
| 3435 | list_for_each_entry(w, &proc->todo, entry) |
| 3436 | print_binder_work(m, " ", " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3437 | list_for_each_entry(w, &proc->delivered_death, entry) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3438 | seq_puts(m, " has delivered dead binder\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3439 | break; |
| 3440 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3441 | if (!print_all && m->count == header_pos) |
| 3442 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3443 | } |
| 3444 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 3445 | static const char * const binder_return_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3446 | "BR_ERROR", |
| 3447 | "BR_OK", |
| 3448 | "BR_TRANSACTION", |
| 3449 | "BR_REPLY", |
| 3450 | "BR_ACQUIRE_RESULT", |
| 3451 | "BR_DEAD_REPLY", |
| 3452 | "BR_TRANSACTION_COMPLETE", |
| 3453 | "BR_INCREFS", |
| 3454 | "BR_ACQUIRE", |
| 3455 | "BR_RELEASE", |
| 3456 | "BR_DECREFS", |
| 3457 | "BR_ATTEMPT_ACQUIRE", |
| 3458 | "BR_NOOP", |
| 3459 | "BR_SPAWN_LOOPER", |
| 3460 | "BR_FINISHED", |
| 3461 | "BR_DEAD_BINDER", |
| 3462 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 3463 | "BR_FAILED_REPLY" |
| 3464 | }; |
| 3465 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 3466 | static const char * const binder_command_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3467 | "BC_TRANSACTION", |
| 3468 | "BC_REPLY", |
| 3469 | "BC_ACQUIRE_RESULT", |
| 3470 | "BC_FREE_BUFFER", |
| 3471 | "BC_INCREFS", |
| 3472 | "BC_ACQUIRE", |
| 3473 | "BC_RELEASE", |
| 3474 | "BC_DECREFS", |
| 3475 | "BC_INCREFS_DONE", |
| 3476 | "BC_ACQUIRE_DONE", |
| 3477 | "BC_ATTEMPT_ACQUIRE", |
| 3478 | "BC_REGISTER_LOOPER", |
| 3479 | "BC_ENTER_LOOPER", |
| 3480 | "BC_EXIT_LOOPER", |
| 3481 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 3482 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 3483 | "BC_DEAD_BINDER_DONE" |
| 3484 | }; |
| 3485 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 3486 | static const char * const binder_objstat_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3487 | "proc", |
| 3488 | "thread", |
| 3489 | "node", |
| 3490 | "ref", |
| 3491 | "death", |
| 3492 | "transaction", |
| 3493 | "transaction_complete" |
| 3494 | }; |
| 3495 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3496 | static void print_binder_stats(struct seq_file *m, const char *prefix, |
| 3497 | struct binder_stats *stats) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3498 | { |
| 3499 | int i; |
| 3500 | |
| 3501 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3502 | ARRAY_SIZE(binder_command_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3503 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { |
| 3504 | if (stats->bc[i]) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3505 | seq_printf(m, "%s%s: %d\n", prefix, |
| 3506 | binder_command_strings[i], stats->bc[i]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3510 | ARRAY_SIZE(binder_return_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3511 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { |
| 3512 | if (stats->br[i]) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3513 | seq_printf(m, "%s%s: %d\n", prefix, |
| 3514 | binder_return_strings[i], stats->br[i]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3518 | ARRAY_SIZE(binder_objstat_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3519 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3520 | ARRAY_SIZE(stats->obj_deleted)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3521 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { |
| 3522 | if (stats->obj_created[i] || stats->obj_deleted[i]) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3523 | seq_printf(m, "%s%s: active %d total %d\n", prefix, |
| 3524 | binder_objstat_strings[i], |
| 3525 | stats->obj_created[i] - stats->obj_deleted[i], |
| 3526 | stats->obj_created[i]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3527 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3528 | } |
| 3529 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3530 | static void print_binder_proc_stats(struct seq_file *m, |
| 3531 | struct binder_proc *proc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3532 | { |
| 3533 | struct binder_work *w; |
| 3534 | struct rb_node *n; |
| 3535 | int count, strong, weak; |
| 3536 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3537 | seq_printf(m, "proc %d\n", proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3538 | count = 0; |
| 3539 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 3540 | count++; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3541 | seq_printf(m, " threads: %d\n", count); |
| 3542 | seq_printf(m, " requested threads: %d+%d/%d\n" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3543 | " ready threads %d\n" |
| 3544 | " free async space %zd\n", proc->requested_threads, |
| 3545 | proc->requested_threads_started, proc->max_threads, |
| 3546 | proc->ready_threads, proc->free_async_space); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3547 | count = 0; |
| 3548 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) |
| 3549 | count++; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3550 | seq_printf(m, " nodes: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3551 | count = 0; |
| 3552 | strong = 0; |
| 3553 | weak = 0; |
| 3554 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 3555 | struct binder_ref *ref = rb_entry(n, struct binder_ref, |
| 3556 | rb_node_desc); |
| 3557 | count++; |
| 3558 | strong += ref->strong; |
| 3559 | weak += ref->weak; |
| 3560 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3561 | seq_printf(m, " refs: %d s %d w %d\n", count, strong, weak); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3562 | |
| 3563 | count = 0; |
| 3564 | for (n = rb_first(&proc->allocated_buffers); n != NULL; n = rb_next(n)) |
| 3565 | count++; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3566 | seq_printf(m, " buffers: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3567 | |
| 3568 | count = 0; |
| 3569 | list_for_each_entry(w, &proc->todo, entry) { |
| 3570 | switch (w->type) { |
| 3571 | case BINDER_WORK_TRANSACTION: |
| 3572 | count++; |
| 3573 | break; |
| 3574 | default: |
| 3575 | break; |
| 3576 | } |
| 3577 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3578 | seq_printf(m, " pending transactions: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3579 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3580 | print_binder_stats(m, " ", &proc->stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3584 | static int binder_state_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3585 | { |
| 3586 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3587 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3588 | int do_lock = !binder_debug_no_lock; |
| 3589 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3590 | if (do_lock) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3591 | binder_lock(__func__); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3592 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3593 | seq_puts(m, "binder state:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3594 | |
| 3595 | if (!hlist_empty(&binder_dead_nodes)) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3596 | seq_puts(m, "dead nodes:\n"); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3597 | hlist_for_each_entry(node, &binder_dead_nodes, dead_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3598 | print_binder_node(m, node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3599 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3600 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3601 | print_binder_proc(m, proc, 1); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3602 | if (do_lock) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3603 | binder_unlock(__func__); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3604 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3605 | } |
| 3606 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3607 | static int binder_stats_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3608 | { |
| 3609 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3610 | int do_lock = !binder_debug_no_lock; |
| 3611 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3612 | if (do_lock) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3613 | binder_lock(__func__); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3614 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3615 | seq_puts(m, "binder stats:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3616 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3617 | print_binder_stats(m, "", &binder_stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3618 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3619 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3620 | print_binder_proc_stats(m, proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3621 | if (do_lock) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3622 | binder_unlock(__func__); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3623 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3624 | } |
| 3625 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3626 | static int binder_transactions_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3627 | { |
| 3628 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3629 | int do_lock = !binder_debug_no_lock; |
| 3630 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3631 | if (do_lock) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3632 | binder_lock(__func__); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3633 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3634 | seq_puts(m, "binder transactions:\n"); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3635 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3636 | print_binder_proc(m, proc, 0); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3637 | if (do_lock) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3638 | binder_unlock(__func__); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3639 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3640 | } |
| 3641 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3642 | static int binder_proc_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3643 | { |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 3644 | struct binder_proc *itr; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3645 | struct binder_proc *proc = m->private; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3646 | int do_lock = !binder_debug_no_lock; |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 3647 | bool valid_proc = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3648 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3649 | if (do_lock) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3650 | binder_lock(__func__); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 3651 | |
| 3652 | hlist_for_each_entry(itr, &binder_procs, proc_node) { |
| 3653 | if (itr == proc) { |
| 3654 | valid_proc = true; |
| 3655 | break; |
| 3656 | } |
| 3657 | } |
| 3658 | if (valid_proc) { |
| 3659 | seq_puts(m, "binder proc state:\n"); |
| 3660 | print_binder_proc(m, proc, 1); |
| 3661 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3662 | if (do_lock) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3663 | binder_unlock(__func__); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3664 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3665 | } |
| 3666 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3667 | static void print_binder_transaction_log_entry(struct seq_file *m, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3668 | struct binder_transaction_log_entry *e) |
| 3669 | { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3670 | seq_printf(m, |
| 3671 | "%d: %s from %d:%d to %d:%d node %d handle %d size %d:%d\n", |
| 3672 | e->debug_id, (e->call_type == 2) ? "reply" : |
| 3673 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, |
| 3674 | e->from_thread, e->to_proc, e->to_thread, e->to_node, |
| 3675 | e->target_handle, e->data_size, e->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3676 | } |
| 3677 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3678 | static int binder_transaction_log_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3679 | { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3680 | struct binder_transaction_log *log = m->private; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3681 | int i; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3682 | |
| 3683 | if (log->full) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3684 | for (i = log->next; i < ARRAY_SIZE(log->entry); i++) |
| 3685 | print_binder_transaction_log_entry(m, &log->entry[i]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3686 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3687 | for (i = 0; i < log->next; i++) |
| 3688 | print_binder_transaction_log_entry(m, &log->entry[i]); |
| 3689 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | static const struct file_operations binder_fops = { |
| 3693 | .owner = THIS_MODULE, |
| 3694 | .poll = binder_poll, |
| 3695 | .unlocked_ioctl = binder_ioctl, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3696 | .compat_ioctl = binder_ioctl, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3697 | .mmap = binder_mmap, |
| 3698 | .open = binder_open, |
| 3699 | .flush = binder_flush, |
| 3700 | .release = binder_release, |
| 3701 | }; |
| 3702 | |
| 3703 | static struct miscdevice binder_miscdev = { |
| 3704 | .minor = MISC_DYNAMIC_MINOR, |
| 3705 | .name = "binder", |
| 3706 | .fops = &binder_fops |
| 3707 | }; |
| 3708 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3709 | BINDER_DEBUG_ENTRY(state); |
| 3710 | BINDER_DEBUG_ENTRY(stats); |
| 3711 | BINDER_DEBUG_ENTRY(transactions); |
| 3712 | BINDER_DEBUG_ENTRY(transaction_log); |
| 3713 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3714 | static int __init binder_init(void) |
| 3715 | { |
| 3716 | int ret; |
| 3717 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3718 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); |
| 3719 | if (binder_debugfs_dir_entry_root) |
| 3720 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", |
| 3721 | binder_debugfs_dir_entry_root); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3722 | ret = misc_register(&binder_miscdev); |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 3723 | if (binder_debugfs_dir_entry_root) { |
| 3724 | debugfs_create_file("state", |
| 3725 | S_IRUGO, |
| 3726 | binder_debugfs_dir_entry_root, |
| 3727 | NULL, |
| 3728 | &binder_state_fops); |
| 3729 | debugfs_create_file("stats", |
| 3730 | S_IRUGO, |
| 3731 | binder_debugfs_dir_entry_root, |
| 3732 | NULL, |
| 3733 | &binder_stats_fops); |
| 3734 | debugfs_create_file("transactions", |
| 3735 | S_IRUGO, |
| 3736 | binder_debugfs_dir_entry_root, |
| 3737 | NULL, |
| 3738 | &binder_transactions_fops); |
| 3739 | debugfs_create_file("transaction_log", |
| 3740 | S_IRUGO, |
| 3741 | binder_debugfs_dir_entry_root, |
| 3742 | &binder_transaction_log, |
| 3743 | &binder_transaction_log_fops); |
| 3744 | debugfs_create_file("failed_transaction_log", |
| 3745 | S_IRUGO, |
| 3746 | binder_debugfs_dir_entry_root, |
| 3747 | &binder_transaction_log_failed, |
| 3748 | &binder_transaction_log_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3749 | } |
| 3750 | return ret; |
| 3751 | } |
| 3752 | |
| 3753 | device_initcall(binder_init); |
| 3754 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3755 | #define CREATE_TRACE_POINTS |
| 3756 | #include "binder_trace.h" |
| 3757 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3758 | MODULE_LICENSE("GPL v2"); |