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