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