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