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