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 | |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 18 | /* |
| 19 | * Locking overview |
| 20 | * |
| 21 | * There are 3 main spinlocks which must be acquired in the |
| 22 | * order shown: |
| 23 | * |
| 24 | * 1) proc->outer_lock : protects binder_ref |
| 25 | * binder_proc_lock() and binder_proc_unlock() are |
| 26 | * used to acq/rel. |
| 27 | * 2) node->lock : protects most fields of binder_node. |
| 28 | * binder_node_lock() and binder_node_unlock() are |
| 29 | * used to acq/rel |
| 30 | * 3) proc->inner_lock : protects the thread and node lists |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 31 | * (proc->threads, proc->waiting_threads, proc->nodes) |
| 32 | * and all todo lists associated with the binder_proc |
| 33 | * (proc->todo, thread->todo, proc->delivered_death and |
| 34 | * node->async_todo), as well as thread->transaction_stack |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 35 | * binder_inner_proc_lock() and binder_inner_proc_unlock() |
| 36 | * are used to acq/rel |
| 37 | * |
| 38 | * Any lock under procA must never be nested under any lock at the same |
| 39 | * level or below on procB. |
| 40 | * |
| 41 | * Functions that require a lock held on entry indicate which lock |
| 42 | * in the suffix of the function name: |
| 43 | * |
| 44 | * foo_olocked() : requires node->outer_lock |
| 45 | * foo_nlocked() : requires node->lock |
| 46 | * foo_ilocked() : requires proc->inner_lock |
| 47 | * foo_oilocked(): requires proc->outer_lock and proc->inner_lock |
| 48 | * foo_nilocked(): requires node->lock and proc->inner_lock |
| 49 | * ... |
| 50 | */ |
| 51 | |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 52 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 53 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 54 | #include <asm/cacheflush.h> |
| 55 | #include <linux/fdtable.h> |
| 56 | #include <linux/file.h> |
Colin Cross | e2610b2 | 2013-05-06 23:50:15 +0000 | [diff] [blame] | 57 | #include <linux/freezer.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 58 | #include <linux/fs.h> |
| 59 | #include <linux/list.h> |
| 60 | #include <linux/miscdevice.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 61 | #include <linux/module.h> |
| 62 | #include <linux/mutex.h> |
| 63 | #include <linux/nsproxy.h> |
| 64 | #include <linux/poll.h> |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 65 | #include <linux/debugfs.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 66 | #include <linux/rbtree.h> |
| 67 | #include <linux/sched.h> |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 68 | #include <linux/seq_file.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 69 | #include <linux/uaccess.h> |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 70 | #include <linux/pid_namespace.h> |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 71 | #include <linux/security.h> |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 72 | #include <linux/spinlock.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 73 | |
Greg Kroah-Hartman | 9246a4a | 2014-10-16 15:26:51 +0200 | [diff] [blame] | 74 | #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT |
| 75 | #define BINDER_IPC_32BIT 1 |
| 76 | #endif |
| 77 | |
| 78 | #include <uapi/linux/android/binder.h> |
Todd Kjos | b934102 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 79 | #include "binder_alloc.h" |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 80 | #include "binder_trace.h" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 81 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 82 | static HLIST_HEAD(binder_deferred_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 83 | static DEFINE_MUTEX(binder_deferred_lock); |
| 84 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 85 | static HLIST_HEAD(binder_devices); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 86 | static HLIST_HEAD(binder_procs); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 87 | static DEFINE_MUTEX(binder_procs_lock); |
| 88 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 89 | static HLIST_HEAD(binder_dead_nodes); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 90 | static DEFINE_SPINLOCK(binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 91 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 92 | static struct dentry *binder_debugfs_dir_entry_root; |
| 93 | static struct dentry *binder_debugfs_dir_entry_proc; |
Todd Kjos | c4bd08b | 2017-05-25 10:56:00 -0700 | [diff] [blame] | 94 | static atomic_t binder_last_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 95 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 96 | #define BINDER_DEBUG_ENTRY(name) \ |
| 97 | static int binder_##name##_open(struct inode *inode, struct file *file) \ |
| 98 | { \ |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 99 | return single_open(file, binder_##name##_show, inode->i_private); \ |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 100 | } \ |
| 101 | \ |
| 102 | static const struct file_operations binder_##name##_fops = { \ |
| 103 | .owner = THIS_MODULE, \ |
| 104 | .open = binder_##name##_open, \ |
| 105 | .read = seq_read, \ |
| 106 | .llseek = seq_lseek, \ |
| 107 | .release = single_release, \ |
| 108 | } |
| 109 | |
| 110 | static int binder_proc_show(struct seq_file *m, void *unused); |
| 111 | BINDER_DEBUG_ENTRY(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 112 | |
| 113 | /* This is only defined in include/asm-arm/sizes.h */ |
| 114 | #ifndef SZ_1K |
| 115 | #define SZ_1K 0x400 |
| 116 | #endif |
| 117 | |
| 118 | #ifndef SZ_4M |
| 119 | #define SZ_4M 0x400000 |
| 120 | #endif |
| 121 | |
| 122 | #define FORBIDDEN_MMAP_FLAGS (VM_WRITE) |
| 123 | |
| 124 | #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64) |
| 125 | |
| 126 | enum { |
| 127 | BINDER_DEBUG_USER_ERROR = 1U << 0, |
| 128 | BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1, |
| 129 | BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2, |
| 130 | BINDER_DEBUG_OPEN_CLOSE = 1U << 3, |
| 131 | BINDER_DEBUG_DEAD_BINDER = 1U << 4, |
| 132 | BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5, |
| 133 | BINDER_DEBUG_READ_WRITE = 1U << 6, |
| 134 | BINDER_DEBUG_USER_REFS = 1U << 7, |
| 135 | BINDER_DEBUG_THREADS = 1U << 8, |
| 136 | BINDER_DEBUG_TRANSACTION = 1U << 9, |
| 137 | BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10, |
| 138 | BINDER_DEBUG_FREE_BUFFER = 1U << 11, |
| 139 | BINDER_DEBUG_INTERNAL_REFS = 1U << 12, |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 140 | BINDER_DEBUG_PRIORITY_CAP = 1U << 13, |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 141 | BINDER_DEBUG_SPINLOCKS = 1U << 14, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 142 | }; |
| 143 | static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR | |
| 144 | BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION; |
| 145 | module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO); |
| 146 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 147 | static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES; |
| 148 | module_param_named(devices, binder_devices_param, charp, S_IRUGO); |
| 149 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 150 | static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait); |
| 151 | static int binder_stop_on_user_error; |
| 152 | |
| 153 | static int binder_set_stop_on_user_error(const char *val, |
| 154 | struct kernel_param *kp) |
| 155 | { |
| 156 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 157 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 158 | ret = param_set_int(val, kp); |
| 159 | if (binder_stop_on_user_error < 2) |
| 160 | wake_up(&binder_user_error_wait); |
| 161 | return ret; |
| 162 | } |
| 163 | module_param_call(stop_on_user_error, binder_set_stop_on_user_error, |
| 164 | param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO); |
| 165 | |
| 166 | #define binder_debug(mask, x...) \ |
| 167 | do { \ |
| 168 | if (binder_debug_mask & mask) \ |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 169 | pr_info(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 170 | } while (0) |
| 171 | |
| 172 | #define binder_user_error(x...) \ |
| 173 | do { \ |
| 174 | if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \ |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 175 | pr_info(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 176 | if (binder_stop_on_user_error) \ |
| 177 | binder_stop_on_user_error = 2; \ |
| 178 | } while (0) |
| 179 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 180 | #define to_flat_binder_object(hdr) \ |
| 181 | container_of(hdr, struct flat_binder_object, hdr) |
| 182 | |
| 183 | #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr) |
| 184 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 185 | #define to_binder_buffer_object(hdr) \ |
| 186 | container_of(hdr, struct binder_buffer_object, hdr) |
| 187 | |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 188 | #define to_binder_fd_array_object(hdr) \ |
| 189 | container_of(hdr, struct binder_fd_array_object, hdr) |
| 190 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 191 | enum binder_stat_types { |
| 192 | BINDER_STAT_PROC, |
| 193 | BINDER_STAT_THREAD, |
| 194 | BINDER_STAT_NODE, |
| 195 | BINDER_STAT_REF, |
| 196 | BINDER_STAT_DEATH, |
| 197 | BINDER_STAT_TRANSACTION, |
| 198 | BINDER_STAT_TRANSACTION_COMPLETE, |
| 199 | BINDER_STAT_COUNT |
| 200 | }; |
| 201 | |
| 202 | struct binder_stats { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 203 | atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1]; |
| 204 | atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1]; |
| 205 | atomic_t obj_created[BINDER_STAT_COUNT]; |
| 206 | atomic_t obj_deleted[BINDER_STAT_COUNT]; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | static struct binder_stats binder_stats; |
| 210 | |
| 211 | static inline void binder_stats_deleted(enum binder_stat_types type) |
| 212 | { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 213 | atomic_inc(&binder_stats.obj_deleted[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static inline void binder_stats_created(enum binder_stat_types type) |
| 217 | { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 218 | atomic_inc(&binder_stats.obj_created[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | struct binder_transaction_log_entry { |
| 222 | int debug_id; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 223 | int debug_id_done; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 224 | int call_type; |
| 225 | int from_proc; |
| 226 | int from_thread; |
| 227 | int target_handle; |
| 228 | int to_proc; |
| 229 | int to_thread; |
| 230 | int to_node; |
| 231 | int data_size; |
| 232 | int offsets_size; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 233 | int return_error_line; |
| 234 | uint32_t return_error; |
| 235 | uint32_t return_error_param; |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 236 | const char *context_name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 237 | }; |
| 238 | struct binder_transaction_log { |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 239 | atomic_t cur; |
| 240 | bool full; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 241 | struct binder_transaction_log_entry entry[32]; |
| 242 | }; |
| 243 | static struct binder_transaction_log binder_transaction_log; |
| 244 | static struct binder_transaction_log binder_transaction_log_failed; |
| 245 | |
| 246 | static struct binder_transaction_log_entry *binder_transaction_log_add( |
| 247 | struct binder_transaction_log *log) |
| 248 | { |
| 249 | struct binder_transaction_log_entry *e; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 250 | unsigned int cur = atomic_inc_return(&log->cur); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 251 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 252 | if (cur >= ARRAY_SIZE(log->entry)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 253 | log->full = 1; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 254 | e = &log->entry[cur % ARRAY_SIZE(log->entry)]; |
| 255 | WRITE_ONCE(e->debug_id_done, 0); |
| 256 | /* |
| 257 | * write-barrier to synchronize access to e->debug_id_done. |
| 258 | * We make sure the initialized 0 value is seen before |
| 259 | * memset() other fields are zeroed by memset. |
| 260 | */ |
| 261 | smp_wmb(); |
| 262 | memset(e, 0, sizeof(*e)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 263 | return e; |
| 264 | } |
| 265 | |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 266 | struct binder_context { |
| 267 | struct binder_node *binder_context_mgr_node; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 268 | struct mutex context_mgr_node_lock; |
| 269 | |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 270 | kuid_t binder_context_mgr_uid; |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 271 | const char *name; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 272 | }; |
| 273 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 274 | struct binder_device { |
| 275 | struct hlist_node hlist; |
| 276 | struct miscdevice miscdev; |
| 277 | struct binder_context context; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 278 | }; |
| 279 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 280 | /** |
| 281 | * struct binder_work - work enqueued on a worklist |
| 282 | * @entry: node enqueued on list |
| 283 | * @type: type of work to be performed |
| 284 | * |
| 285 | * There are separate work lists for proc, thread, and node (async). |
| 286 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 287 | struct binder_work { |
| 288 | struct list_head entry; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 289 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 290 | enum { |
| 291 | BINDER_WORK_TRANSACTION = 1, |
| 292 | BINDER_WORK_TRANSACTION_COMPLETE, |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 293 | BINDER_WORK_RETURN_ERROR, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 294 | BINDER_WORK_NODE, |
| 295 | BINDER_WORK_DEAD_BINDER, |
| 296 | BINDER_WORK_DEAD_BINDER_AND_CLEAR, |
| 297 | BINDER_WORK_CLEAR_DEATH_NOTIFICATION, |
| 298 | } type; |
| 299 | }; |
| 300 | |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 301 | struct binder_error { |
| 302 | struct binder_work work; |
| 303 | uint32_t cmd; |
| 304 | }; |
| 305 | |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 306 | /** |
| 307 | * struct binder_node - binder node bookkeeping |
| 308 | * @debug_id: unique ID for debugging |
| 309 | * (invariant after initialized) |
| 310 | * @lock: lock for node fields |
| 311 | * @work: worklist element for node work |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 312 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 313 | * @rb_node: element for proc->nodes tree |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 314 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 315 | * @dead_node: element for binder_dead_nodes list |
| 316 | * (protected by binder_dead_nodes_lock) |
| 317 | * @proc: binder_proc that owns this node |
| 318 | * (invariant after initialized) |
| 319 | * @refs: list of references on this node |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 320 | * (protected by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 321 | * @internal_strong_refs: used to take strong references when |
| 322 | * initiating a transaction |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 323 | * (protected by @proc->inner_lock if @proc |
| 324 | * and by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 325 | * @local_weak_refs: weak user refs from local process |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 326 | * (protected by @proc->inner_lock if @proc |
| 327 | * and by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 328 | * @local_strong_refs: strong user refs from local process |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 329 | * (protected by @proc->inner_lock if @proc |
| 330 | * and by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 331 | * @tmp_refs: temporary kernel refs |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 332 | * (protected by @proc->inner_lock while @proc |
| 333 | * is valid, and by binder_dead_nodes_lock |
| 334 | * if @proc is NULL. During inc/dec and node release |
| 335 | * it is also protected by @lock to provide safety |
| 336 | * as the node dies and @proc becomes NULL) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 337 | * @ptr: userspace pointer for node |
| 338 | * (invariant, no lock needed) |
| 339 | * @cookie: userspace cookie for node |
| 340 | * (invariant, no lock needed) |
| 341 | * @has_strong_ref: userspace notified of strong ref |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 342 | * (protected by @proc->inner_lock if @proc |
| 343 | * and by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 344 | * @pending_strong_ref: userspace has acked notification of strong ref |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 345 | * (protected by @proc->inner_lock if @proc |
| 346 | * and by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 347 | * @has_weak_ref: userspace notified of weak ref |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 348 | * (protected by @proc->inner_lock if @proc |
| 349 | * and by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 350 | * @pending_weak_ref: userspace has acked notification of weak ref |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 351 | * (protected by @proc->inner_lock if @proc |
| 352 | * and by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 353 | * @has_async_transaction: async transaction to node in progress |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 354 | * (protected by @lock) |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 355 | * @sched_policy: minimum scheduling policy for node |
| 356 | * (invariant after initialized) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 357 | * @accept_fds: file descriptor operations supported for node |
| 358 | * (invariant after initialized) |
| 359 | * @min_priority: minimum scheduling priority |
| 360 | * (invariant after initialized) |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 361 | * @inherit_rt: inherit RT scheduling policy from caller |
| 362 | * (invariant after initialized) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 363 | * @async_todo: list of async work items |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 364 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 365 | * |
| 366 | * Bookkeeping structure for binder nodes. |
| 367 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 368 | struct binder_node { |
| 369 | int debug_id; |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 370 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 371 | struct binder_work work; |
| 372 | union { |
| 373 | struct rb_node rb_node; |
| 374 | struct hlist_node dead_node; |
| 375 | }; |
| 376 | struct binder_proc *proc; |
| 377 | struct hlist_head refs; |
| 378 | int internal_strong_refs; |
| 379 | int local_weak_refs; |
| 380 | int local_strong_refs; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 381 | int tmp_refs; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 382 | binder_uintptr_t ptr; |
| 383 | binder_uintptr_t cookie; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 384 | struct { |
| 385 | /* |
| 386 | * bitfield elements protected by |
| 387 | * proc inner_lock |
| 388 | */ |
| 389 | u8 has_strong_ref:1; |
| 390 | u8 pending_strong_ref:1; |
| 391 | u8 has_weak_ref:1; |
| 392 | u8 pending_weak_ref:1; |
| 393 | }; |
| 394 | struct { |
| 395 | /* |
| 396 | * invariant after initialization |
| 397 | */ |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 398 | u8 sched_policy:2; |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 399 | u8 inherit_rt:1; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 400 | u8 accept_fds:1; |
| 401 | u8 min_priority; |
| 402 | }; |
| 403 | bool has_async_transaction; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 404 | struct list_head async_todo; |
| 405 | }; |
| 406 | |
| 407 | struct binder_ref_death { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 408 | /** |
| 409 | * @work: worklist element for death notifications |
| 410 | * (protected by inner_lock of the proc that |
| 411 | * this ref belongs to) |
| 412 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 413 | struct binder_work work; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 414 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 415 | }; |
| 416 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 417 | /** |
| 418 | * struct binder_ref_data - binder_ref counts and id |
| 419 | * @debug_id: unique ID for the ref |
| 420 | * @desc: unique userspace handle for ref |
| 421 | * @strong: strong ref count (debugging only if not locked) |
| 422 | * @weak: weak ref count (debugging only if not locked) |
| 423 | * |
| 424 | * Structure to hold ref count and ref id information. Since |
| 425 | * the actual ref can only be accessed with a lock, this structure |
| 426 | * is used to return information about the ref to callers of |
| 427 | * ref inc/dec functions. |
| 428 | */ |
| 429 | struct binder_ref_data { |
| 430 | int debug_id; |
| 431 | uint32_t desc; |
| 432 | int strong; |
| 433 | int weak; |
| 434 | }; |
| 435 | |
| 436 | /** |
| 437 | * struct binder_ref - struct to track references on nodes |
| 438 | * @data: binder_ref_data containing id, handle, and current refcounts |
| 439 | * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree |
| 440 | * @rb_node_node: node for lookup by @node in proc's rb_tree |
| 441 | * @node_entry: list entry for node->refs list in target node |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 442 | * (protected by @node->lock) |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 443 | * @proc: binder_proc containing ref |
| 444 | * @node: binder_node of target node. When cleaning up a |
| 445 | * ref for deletion in binder_cleanup_ref, a non-NULL |
| 446 | * @node indicates the node must be freed |
| 447 | * @death: pointer to death notification (ref_death) if requested |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 448 | * (protected by @node->lock) |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 449 | * |
| 450 | * Structure to track references from procA to target node (on procB). This |
| 451 | * structure is unsafe to access without holding @proc->outer_lock. |
| 452 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 453 | struct binder_ref { |
| 454 | /* Lookups needed: */ |
| 455 | /* node + proc => ref (transaction) */ |
| 456 | /* desc + proc => ref (transaction, inc/dec ref) */ |
| 457 | /* node => refs + procs (proc exit) */ |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 458 | struct binder_ref_data data; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 459 | struct rb_node rb_node_desc; |
| 460 | struct rb_node rb_node_node; |
| 461 | struct hlist_node node_entry; |
| 462 | struct binder_proc *proc; |
| 463 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 464 | struct binder_ref_death *death; |
| 465 | }; |
| 466 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 467 | enum binder_deferred_state { |
| 468 | BINDER_DEFERRED_PUT_FILES = 0x01, |
| 469 | BINDER_DEFERRED_FLUSH = 0x02, |
| 470 | BINDER_DEFERRED_RELEASE = 0x04, |
| 471 | }; |
| 472 | |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 473 | /** |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 474 | * struct binder_priority - scheduler policy and priority |
| 475 | * @sched_policy scheduler policy |
| 476 | * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT |
| 477 | * |
| 478 | * The binder driver supports inheriting the following scheduler policies: |
| 479 | * SCHED_NORMAL |
| 480 | * SCHED_BATCH |
| 481 | * SCHED_FIFO |
| 482 | * SCHED_RR |
| 483 | */ |
| 484 | struct binder_priority { |
| 485 | unsigned int sched_policy; |
| 486 | int prio; |
| 487 | }; |
| 488 | |
| 489 | /** |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 490 | * struct binder_proc - binder process bookkeeping |
| 491 | * @proc_node: element for binder_procs list |
| 492 | * @threads: rbtree of binder_threads in this proc |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 493 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 494 | * @nodes: rbtree of binder nodes associated with |
| 495 | * this proc ordered by node->ptr |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 496 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 497 | * @refs_by_desc: rbtree of refs ordered by ref->desc |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 498 | * (protected by @outer_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 499 | * @refs_by_node: rbtree of refs ordered by ref->node |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 500 | * (protected by @outer_lock) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 501 | * @waiting_threads: threads currently waiting for proc work |
| 502 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 503 | * @pid PID of group_leader of process |
| 504 | * (invariant after initialized) |
| 505 | * @tsk task_struct for group_leader of process |
| 506 | * (invariant after initialized) |
| 507 | * @files files_struct for process |
| 508 | * (invariant after initialized) |
| 509 | * @deferred_work_node: element for binder_deferred_list |
| 510 | * (protected by binder_deferred_lock) |
| 511 | * @deferred_work: bitmap of deferred work to perform |
| 512 | * (protected by binder_deferred_lock) |
| 513 | * @is_dead: process is dead and awaiting free |
| 514 | * when outstanding transactions are cleaned up |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 515 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 516 | * @todo: list of work for this process |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 517 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 518 | * @wait: wait queue head to wait for proc work |
| 519 | * (invariant after initialized) |
| 520 | * @stats: per-process binder statistics |
| 521 | * (atomics, no lock needed) |
| 522 | * @delivered_death: list of delivered death notification |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 523 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 524 | * @max_threads: cap on number of binder threads |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 525 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 526 | * @requested_threads: number of binder threads requested but not |
| 527 | * yet started. In current implementation, can |
| 528 | * only be 0 or 1. |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 529 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 530 | * @requested_threads_started: number binder threads started |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 531 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 532 | * @tmp_ref: temporary reference to indicate proc is in use |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 533 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 534 | * @default_priority: default scheduler priority |
| 535 | * (invariant after initialized) |
| 536 | * @debugfs_entry: debugfs node |
| 537 | * @alloc: binder allocator bookkeeping |
| 538 | * @context: binder_context for this proc |
| 539 | * (invariant after initialized) |
| 540 | * @inner_lock: can nest under outer_lock and/or node lock |
| 541 | * @outer_lock: no nesting under innor or node lock |
| 542 | * Lock order: 1) outer, 2) node, 3) inner |
| 543 | * |
| 544 | * Bookkeeping structure for binder processes |
| 545 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 546 | struct binder_proc { |
| 547 | struct hlist_node proc_node; |
| 548 | struct rb_root threads; |
| 549 | struct rb_root nodes; |
| 550 | struct rb_root refs_by_desc; |
| 551 | struct rb_root refs_by_node; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 552 | struct list_head waiting_threads; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 553 | int pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 554 | struct task_struct *tsk; |
| 555 | struct files_struct *files; |
| 556 | struct hlist_node deferred_work_node; |
| 557 | int deferred_work; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 558 | bool is_dead; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 559 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 560 | struct list_head todo; |
| 561 | wait_queue_head_t wait; |
| 562 | struct binder_stats stats; |
| 563 | struct list_head delivered_death; |
| 564 | int max_threads; |
| 565 | int requested_threads; |
| 566 | int requested_threads_started; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 567 | int tmp_ref; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 568 | struct binder_priority default_priority; |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 569 | struct dentry *debugfs_entry; |
Todd Kjos | f85d229 | 2016-10-10 10:39:59 -0700 | [diff] [blame] | 570 | struct binder_alloc alloc; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 571 | struct binder_context *context; |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 572 | spinlock_t inner_lock; |
| 573 | spinlock_t outer_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 574 | }; |
| 575 | |
| 576 | enum { |
| 577 | BINDER_LOOPER_STATE_REGISTERED = 0x01, |
| 578 | BINDER_LOOPER_STATE_ENTERED = 0x02, |
| 579 | BINDER_LOOPER_STATE_EXITED = 0x04, |
| 580 | BINDER_LOOPER_STATE_INVALID = 0x08, |
| 581 | BINDER_LOOPER_STATE_WAITING = 0x10, |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 582 | BINDER_LOOPER_STATE_POLL = 0x20, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 583 | }; |
| 584 | |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 585 | /** |
| 586 | * struct binder_thread - binder thread bookkeeping |
| 587 | * @proc: binder process for this thread |
| 588 | * (invariant after initialization) |
| 589 | * @rb_node: element for proc->threads rbtree |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 590 | * (protected by @proc->inner_lock) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 591 | * @waiting_thread_node: element for @proc->waiting_threads list |
| 592 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 593 | * @pid: PID for this thread |
| 594 | * (invariant after initialization) |
| 595 | * @looper: bitmap of looping state |
| 596 | * (only accessed by this thread) |
| 597 | * @looper_needs_return: looping thread needs to exit driver |
| 598 | * (no lock needed) |
| 599 | * @transaction_stack: stack of in-progress transactions for this thread |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 600 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 601 | * @todo: list of work to do for this thread |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 602 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 603 | * @return_error: transaction errors reported by this thread |
| 604 | * (only accessed by this thread) |
| 605 | * @reply_error: transaction errors reported by target thread |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 606 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 607 | * @wait: wait queue for thread work |
| 608 | * @stats: per-thread statistics |
| 609 | * (atomics, no lock needed) |
| 610 | * @tmp_ref: temporary reference to indicate thread is in use |
| 611 | * (atomic since @proc->inner_lock cannot |
| 612 | * always be acquired) |
| 613 | * @is_dead: thread is dead and awaiting free |
| 614 | * when outstanding transactions are cleaned up |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 615 | * (protected by @proc->inner_lock) |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 616 | * @task: struct task_struct for this thread |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 617 | * |
| 618 | * Bookkeeping structure for binder threads. |
| 619 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 620 | struct binder_thread { |
| 621 | struct binder_proc *proc; |
| 622 | struct rb_node rb_node; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 623 | struct list_head waiting_thread_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 624 | int pid; |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 625 | int looper; /* only modified by this thread */ |
| 626 | bool looper_need_return; /* can be written by other thread */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 627 | struct binder_transaction *transaction_stack; |
| 628 | struct list_head todo; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 629 | struct binder_error return_error; |
| 630 | struct binder_error reply_error; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 631 | wait_queue_head_t wait; |
| 632 | struct binder_stats stats; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 633 | atomic_t tmp_ref; |
| 634 | bool is_dead; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 635 | struct task_struct *task; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 636 | }; |
| 637 | |
| 638 | struct binder_transaction { |
| 639 | int debug_id; |
| 640 | struct binder_work work; |
| 641 | struct binder_thread *from; |
| 642 | struct binder_transaction *from_parent; |
| 643 | struct binder_proc *to_proc; |
| 644 | struct binder_thread *to_thread; |
| 645 | struct binder_transaction *to_parent; |
| 646 | unsigned need_reply:1; |
| 647 | /* unsigned is_dead:1; */ /* not used at the moment */ |
| 648 | |
| 649 | struct binder_buffer *buffer; |
| 650 | unsigned int code; |
| 651 | unsigned int flags; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 652 | struct binder_priority priority; |
| 653 | struct binder_priority saved_priority; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 654 | bool set_priority_called; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 655 | kuid_t sender_euid; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 656 | /** |
| 657 | * @lock: protects @from, @to_proc, and @to_thread |
| 658 | * |
| 659 | * @from, @to_proc, and @to_thread can be set to NULL |
| 660 | * during thread teardown |
| 661 | */ |
| 662 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 663 | }; |
| 664 | |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 665 | /** |
| 666 | * binder_proc_lock() - Acquire outer lock for given binder_proc |
| 667 | * @proc: struct binder_proc to acquire |
| 668 | * |
| 669 | * Acquires proc->outer_lock. Used to protect binder_ref |
| 670 | * structures associated with the given proc. |
| 671 | */ |
| 672 | #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__) |
| 673 | static void |
| 674 | _binder_proc_lock(struct binder_proc *proc, int line) |
| 675 | { |
| 676 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 677 | "%s: line=%d\n", __func__, line); |
| 678 | spin_lock(&proc->outer_lock); |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * binder_proc_unlock() - Release spinlock for given binder_proc |
| 683 | * @proc: struct binder_proc to acquire |
| 684 | * |
| 685 | * Release lock acquired via binder_proc_lock() |
| 686 | */ |
| 687 | #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__) |
| 688 | static void |
| 689 | _binder_proc_unlock(struct binder_proc *proc, int line) |
| 690 | { |
| 691 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 692 | "%s: line=%d\n", __func__, line); |
| 693 | spin_unlock(&proc->outer_lock); |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * binder_inner_proc_lock() - Acquire inner lock for given binder_proc |
| 698 | * @proc: struct binder_proc to acquire |
| 699 | * |
| 700 | * Acquires proc->inner_lock. Used to protect todo lists |
| 701 | */ |
| 702 | #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__) |
| 703 | static void |
| 704 | _binder_inner_proc_lock(struct binder_proc *proc, int line) |
| 705 | { |
| 706 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 707 | "%s: line=%d\n", __func__, line); |
| 708 | spin_lock(&proc->inner_lock); |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * binder_inner_proc_unlock() - Release inner lock for given binder_proc |
| 713 | * @proc: struct binder_proc to acquire |
| 714 | * |
| 715 | * Release lock acquired via binder_inner_proc_lock() |
| 716 | */ |
| 717 | #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__) |
| 718 | static void |
| 719 | _binder_inner_proc_unlock(struct binder_proc *proc, int line) |
| 720 | { |
| 721 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 722 | "%s: line=%d\n", __func__, line); |
| 723 | spin_unlock(&proc->inner_lock); |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * binder_node_lock() - Acquire spinlock for given binder_node |
| 728 | * @node: struct binder_node to acquire |
| 729 | * |
| 730 | * Acquires node->lock. Used to protect binder_node fields |
| 731 | */ |
| 732 | #define binder_node_lock(node) _binder_node_lock(node, __LINE__) |
| 733 | static void |
| 734 | _binder_node_lock(struct binder_node *node, int line) |
| 735 | { |
| 736 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 737 | "%s: line=%d\n", __func__, line); |
| 738 | spin_lock(&node->lock); |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * binder_node_unlock() - Release spinlock for given binder_proc |
| 743 | * @node: struct binder_node to acquire |
| 744 | * |
| 745 | * Release lock acquired via binder_node_lock() |
| 746 | */ |
| 747 | #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__) |
| 748 | static void |
| 749 | _binder_node_unlock(struct binder_node *node, int line) |
| 750 | { |
| 751 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 752 | "%s: line=%d\n", __func__, line); |
| 753 | spin_unlock(&node->lock); |
| 754 | } |
| 755 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 756 | /** |
| 757 | * binder_node_inner_lock() - Acquire node and inner locks |
| 758 | * @node: struct binder_node to acquire |
| 759 | * |
| 760 | * Acquires node->lock. If node->proc also acquires |
| 761 | * proc->inner_lock. Used to protect binder_node fields |
| 762 | */ |
| 763 | #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__) |
| 764 | static void |
| 765 | _binder_node_inner_lock(struct binder_node *node, int line) |
| 766 | { |
| 767 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 768 | "%s: line=%d\n", __func__, line); |
| 769 | spin_lock(&node->lock); |
| 770 | if (node->proc) |
| 771 | binder_inner_proc_lock(node->proc); |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * binder_node_unlock() - Release node and inner locks |
| 776 | * @node: struct binder_node to acquire |
| 777 | * |
| 778 | * Release lock acquired via binder_node_lock() |
| 779 | */ |
| 780 | #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__) |
| 781 | static void |
| 782 | _binder_node_inner_unlock(struct binder_node *node, int line) |
| 783 | { |
| 784 | struct binder_proc *proc = node->proc; |
| 785 | |
| 786 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 787 | "%s: line=%d\n", __func__, line); |
| 788 | if (proc) |
| 789 | binder_inner_proc_unlock(proc); |
| 790 | spin_unlock(&node->lock); |
| 791 | } |
| 792 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 793 | static bool binder_worklist_empty_ilocked(struct list_head *list) |
| 794 | { |
| 795 | return list_empty(list); |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * binder_worklist_empty() - Check if no items on the work list |
| 800 | * @proc: binder_proc associated with list |
| 801 | * @list: list to check |
| 802 | * |
| 803 | * Return: true if there are no items on list, else false |
| 804 | */ |
| 805 | static bool binder_worklist_empty(struct binder_proc *proc, |
| 806 | struct list_head *list) |
| 807 | { |
| 808 | bool ret; |
| 809 | |
| 810 | binder_inner_proc_lock(proc); |
| 811 | ret = binder_worklist_empty_ilocked(list); |
| 812 | binder_inner_proc_unlock(proc); |
| 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | static void |
| 817 | binder_enqueue_work_ilocked(struct binder_work *work, |
| 818 | struct list_head *target_list) |
| 819 | { |
| 820 | BUG_ON(target_list == NULL); |
| 821 | BUG_ON(work->entry.next && !list_empty(&work->entry)); |
| 822 | list_add_tail(&work->entry, target_list); |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * binder_enqueue_work() - Add an item to the work list |
| 827 | * @proc: binder_proc associated with list |
| 828 | * @work: struct binder_work to add to list |
| 829 | * @target_list: list to add work to |
| 830 | * |
| 831 | * Adds the work to the specified list. Asserts that work |
| 832 | * is not already on a list. |
| 833 | */ |
| 834 | static void |
| 835 | binder_enqueue_work(struct binder_proc *proc, |
| 836 | struct binder_work *work, |
| 837 | struct list_head *target_list) |
| 838 | { |
| 839 | binder_inner_proc_lock(proc); |
| 840 | binder_enqueue_work_ilocked(work, target_list); |
| 841 | binder_inner_proc_unlock(proc); |
| 842 | } |
| 843 | |
| 844 | static void |
| 845 | binder_dequeue_work_ilocked(struct binder_work *work) |
| 846 | { |
| 847 | list_del_init(&work->entry); |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * binder_dequeue_work() - Removes an item from the work list |
| 852 | * @proc: binder_proc associated with list |
| 853 | * @work: struct binder_work to remove from list |
| 854 | * |
| 855 | * Removes the specified work item from whatever list it is on. |
| 856 | * Can safely be called if work is not on any list. |
| 857 | */ |
| 858 | static void |
| 859 | binder_dequeue_work(struct binder_proc *proc, struct binder_work *work) |
| 860 | { |
| 861 | binder_inner_proc_lock(proc); |
| 862 | binder_dequeue_work_ilocked(work); |
| 863 | binder_inner_proc_unlock(proc); |
| 864 | } |
| 865 | |
| 866 | static struct binder_work *binder_dequeue_work_head_ilocked( |
| 867 | struct list_head *list) |
| 868 | { |
| 869 | struct binder_work *w; |
| 870 | |
| 871 | w = list_first_entry_or_null(list, struct binder_work, entry); |
| 872 | if (w) |
| 873 | list_del_init(&w->entry); |
| 874 | return w; |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * binder_dequeue_work_head() - Dequeues the item at head of list |
| 879 | * @proc: binder_proc associated with list |
| 880 | * @list: list to dequeue head |
| 881 | * |
| 882 | * Removes the head of the list if there are items on the list |
| 883 | * |
| 884 | * Return: pointer dequeued binder_work, NULL if list was empty |
| 885 | */ |
| 886 | static struct binder_work *binder_dequeue_work_head( |
| 887 | struct binder_proc *proc, |
| 888 | struct list_head *list) |
| 889 | { |
| 890 | struct binder_work *w; |
| 891 | |
| 892 | binder_inner_proc_lock(proc); |
| 893 | w = binder_dequeue_work_head_ilocked(list); |
| 894 | binder_inner_proc_unlock(proc); |
| 895 | return w; |
| 896 | } |
| 897 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 898 | static void |
| 899 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 900 | static void binder_free_thread(struct binder_thread *thread); |
| 901 | static void binder_free_proc(struct binder_proc *proc); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 902 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 903 | |
Sachin Kamat | efde99c | 2012-08-17 16:39:36 +0530 | [diff] [blame] | 904 | 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] | 905 | { |
| 906 | struct files_struct *files = proc->files; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 907 | unsigned long rlim_cur; |
| 908 | unsigned long irqs; |
| 909 | |
| 910 | if (files == NULL) |
| 911 | return -ESRCH; |
| 912 | |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 913 | if (!lock_task_sighand(proc->tsk, &irqs)) |
| 914 | return -EMFILE; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 915 | |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 916 | rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE); |
| 917 | unlock_task_sighand(proc->tsk, &irqs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 918 | |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 919 | return __alloc_fd(files, 0, rlim_cur, flags); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | /* |
| 923 | * copied from fd_install |
| 924 | */ |
| 925 | static void task_fd_install( |
| 926 | struct binder_proc *proc, unsigned int fd, struct file *file) |
| 927 | { |
Al Viro | f869e8a | 2012-08-15 21:06:33 -0400 | [diff] [blame] | 928 | if (proc->files) |
| 929 | __fd_install(proc->files, fd, file); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | /* |
| 933 | * copied from sys_close |
| 934 | */ |
| 935 | static long task_close_fd(struct binder_proc *proc, unsigned int fd) |
| 936 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 937 | int retval; |
| 938 | |
Al Viro | 483ce1d | 2012-08-19 12:04:24 -0400 | [diff] [blame] | 939 | if (proc->files == NULL) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 940 | return -ESRCH; |
| 941 | |
Al Viro | 483ce1d | 2012-08-19 12:04:24 -0400 | [diff] [blame] | 942 | retval = __close_fd(proc->files, fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 943 | /* can't restart close syscall because file table entry was cleared */ |
| 944 | if (unlikely(retval == -ERESTARTSYS || |
| 945 | retval == -ERESTARTNOINTR || |
| 946 | retval == -ERESTARTNOHAND || |
| 947 | retval == -ERESTART_RESTARTBLOCK)) |
| 948 | retval = -EINTR; |
| 949 | |
| 950 | return retval; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 951 | } |
| 952 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 953 | static bool binder_has_work_ilocked(struct binder_thread *thread, |
| 954 | bool do_proc_work) |
| 955 | { |
| 956 | return !binder_worklist_empty_ilocked(&thread->todo) || |
| 957 | thread->looper_need_return || |
| 958 | (do_proc_work && |
| 959 | !binder_worklist_empty_ilocked(&thread->proc->todo)); |
| 960 | } |
| 961 | |
| 962 | static bool binder_has_work(struct binder_thread *thread, bool do_proc_work) |
| 963 | { |
| 964 | bool has_work; |
| 965 | |
| 966 | binder_inner_proc_lock(thread->proc); |
| 967 | has_work = binder_has_work_ilocked(thread, do_proc_work); |
| 968 | binder_inner_proc_unlock(thread->proc); |
| 969 | |
| 970 | return has_work; |
| 971 | } |
| 972 | |
| 973 | static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread) |
| 974 | { |
| 975 | return !thread->transaction_stack && |
| 976 | binder_worklist_empty_ilocked(&thread->todo) && |
| 977 | (thread->looper & (BINDER_LOOPER_STATE_ENTERED | |
| 978 | BINDER_LOOPER_STATE_REGISTERED)); |
| 979 | } |
| 980 | |
| 981 | static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc, |
| 982 | bool sync) |
| 983 | { |
| 984 | struct rb_node *n; |
| 985 | struct binder_thread *thread; |
| 986 | |
| 987 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 988 | thread = rb_entry(n, struct binder_thread, rb_node); |
| 989 | if (thread->looper & BINDER_LOOPER_STATE_POLL && |
| 990 | binder_available_for_proc_work_ilocked(thread)) { |
| 991 | if (sync) |
| 992 | wake_up_interruptible_sync(&thread->wait); |
| 993 | else |
| 994 | wake_up_interruptible(&thread->wait); |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 999 | /** |
| 1000 | * binder_select_thread_ilocked() - selects a thread for doing proc work. |
| 1001 | * @proc: process to select a thread from |
| 1002 | * |
| 1003 | * Note that calling this function moves the thread off the waiting_threads |
| 1004 | * list, so it can only be woken up by the caller of this function, or a |
| 1005 | * signal. Therefore, callers *should* always wake up the thread this function |
| 1006 | * returns. |
| 1007 | * |
| 1008 | * Return: If there's a thread currently waiting for process work, |
| 1009 | * returns that thread. Otherwise returns NULL. |
| 1010 | */ |
| 1011 | static struct binder_thread * |
| 1012 | binder_select_thread_ilocked(struct binder_proc *proc) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1013 | { |
| 1014 | struct binder_thread *thread; |
| 1015 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1016 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1017 | thread = list_first_entry_or_null(&proc->waiting_threads, |
| 1018 | struct binder_thread, |
| 1019 | waiting_thread_node); |
| 1020 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1021 | if (thread) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1022 | list_del_init(&thread->waiting_thread_node); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1023 | |
| 1024 | return thread; |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work. |
| 1029 | * @proc: process to wake up a thread in |
| 1030 | * @thread: specific thread to wake-up (may be NULL) |
| 1031 | * @sync: whether to do a synchronous wake-up |
| 1032 | * |
| 1033 | * This function wakes up a thread in the @proc process. |
| 1034 | * The caller may provide a specific thread to wake-up in |
| 1035 | * the @thread parameter. If @thread is NULL, this function |
| 1036 | * will wake up threads that have called poll(). |
| 1037 | * |
| 1038 | * Note that for this function to work as expected, callers |
| 1039 | * should first call binder_select_thread() to find a thread |
| 1040 | * to handle the work (if they don't have a thread already), |
| 1041 | * and pass the result into the @thread parameter. |
| 1042 | */ |
| 1043 | static void binder_wakeup_thread_ilocked(struct binder_proc *proc, |
| 1044 | struct binder_thread *thread, |
| 1045 | bool sync) |
| 1046 | { |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1047 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1048 | |
| 1049 | if (thread) { |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1050 | if (sync) |
| 1051 | wake_up_interruptible_sync(&thread->wait); |
| 1052 | else |
| 1053 | wake_up_interruptible(&thread->wait); |
| 1054 | return; |
| 1055 | } |
| 1056 | |
| 1057 | /* Didn't find a thread waiting for proc work; this can happen |
| 1058 | * in two scenarios: |
| 1059 | * 1. All threads are busy handling transactions |
| 1060 | * In that case, one of those threads should call back into |
| 1061 | * the kernel driver soon and pick up this work. |
| 1062 | * 2. Threads are using the (e)poll interface, in which case |
| 1063 | * they may be blocked on the waitqueue without having been |
| 1064 | * added to waiting_threads. For this case, we just iterate |
| 1065 | * over all threads not handling transaction work, and |
| 1066 | * wake them all up. We wake all because we don't know whether |
| 1067 | * a thread that called into (e)poll is handling non-binder |
| 1068 | * work currently. |
| 1069 | */ |
| 1070 | binder_wakeup_poll_threads_ilocked(proc, sync); |
| 1071 | } |
| 1072 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1073 | static void binder_wakeup_proc_ilocked(struct binder_proc *proc) |
| 1074 | { |
| 1075 | struct binder_thread *thread = binder_select_thread_ilocked(proc); |
| 1076 | |
| 1077 | binder_wakeup_thread_ilocked(proc, thread, /* sync = */false); |
| 1078 | } |
| 1079 | |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1080 | static bool is_rt_policy(int policy) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1081 | { |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1082 | return policy == SCHED_FIFO || policy == SCHED_RR; |
| 1083 | } |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1084 | |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1085 | static bool is_fair_policy(int policy) |
| 1086 | { |
| 1087 | return policy == SCHED_NORMAL || policy == SCHED_BATCH; |
| 1088 | } |
| 1089 | |
| 1090 | static bool binder_supported_policy(int policy) |
| 1091 | { |
| 1092 | return is_fair_policy(policy) || is_rt_policy(policy); |
| 1093 | } |
| 1094 | |
| 1095 | static int to_userspace_prio(int policy, int kernel_priority) |
| 1096 | { |
| 1097 | if (is_fair_policy(policy)) |
| 1098 | return PRIO_TO_NICE(kernel_priority); |
| 1099 | else |
| 1100 | return MAX_USER_RT_PRIO - 1 - kernel_priority; |
| 1101 | } |
| 1102 | |
| 1103 | static int to_kernel_prio(int policy, int user_priority) |
| 1104 | { |
| 1105 | if (is_fair_policy(policy)) |
| 1106 | return NICE_TO_PRIO(user_priority); |
| 1107 | else |
| 1108 | return MAX_USER_RT_PRIO - 1 - user_priority; |
| 1109 | } |
| 1110 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1111 | static void binder_do_set_priority(struct task_struct *task, |
| 1112 | struct binder_priority desired, |
| 1113 | bool verify) |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1114 | { |
| 1115 | int priority; /* user-space prio value */ |
| 1116 | bool has_cap_nice; |
| 1117 | unsigned int policy = desired.sched_policy; |
| 1118 | |
| 1119 | if (task->policy == policy && task->normal_prio == desired.prio) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1120 | return; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1121 | |
| 1122 | has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE); |
| 1123 | |
| 1124 | priority = to_userspace_prio(policy, desired.prio); |
| 1125 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1126 | if (verify && is_rt_policy(policy) && !has_cap_nice) { |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1127 | long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO); |
| 1128 | |
| 1129 | if (max_rtprio == 0) { |
| 1130 | policy = SCHED_NORMAL; |
| 1131 | priority = MIN_NICE; |
| 1132 | } else if (priority > max_rtprio) { |
| 1133 | priority = max_rtprio; |
| 1134 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1135 | } |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1136 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1137 | if (verify && is_fair_policy(policy) && !has_cap_nice) { |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1138 | long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE)); |
| 1139 | |
| 1140 | if (min_nice > MAX_NICE) { |
| 1141 | binder_user_error("%d RLIMIT_NICE not set\n", |
| 1142 | task->pid); |
| 1143 | return; |
| 1144 | } else if (priority < min_nice) { |
| 1145 | priority = min_nice; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | if (policy != desired.sched_policy || |
| 1150 | to_kernel_prio(policy, priority) != desired.prio) |
| 1151 | binder_debug(BINDER_DEBUG_PRIORITY_CAP, |
| 1152 | "%d: priority %d not allowed, using %d instead\n", |
| 1153 | task->pid, desired.prio, |
| 1154 | to_kernel_prio(policy, priority)); |
| 1155 | |
| 1156 | /* Set the actual priority */ |
| 1157 | if (task->policy != policy || is_rt_policy(policy)) { |
| 1158 | struct sched_param params; |
| 1159 | |
| 1160 | params.sched_priority = is_rt_policy(policy) ? priority : 0; |
| 1161 | |
| 1162 | sched_setscheduler_nocheck(task, |
| 1163 | policy | SCHED_RESET_ON_FORK, |
| 1164 | ¶ms); |
| 1165 | } |
| 1166 | if (is_fair_policy(policy)) |
| 1167 | set_user_nice(task, priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1168 | } |
| 1169 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1170 | static void binder_set_priority(struct task_struct *task, |
| 1171 | struct binder_priority desired) |
| 1172 | { |
| 1173 | binder_do_set_priority(task, desired, /* verify = */ true); |
| 1174 | } |
| 1175 | |
| 1176 | static void binder_restore_priority(struct task_struct *task, |
| 1177 | struct binder_priority desired) |
| 1178 | { |
| 1179 | binder_do_set_priority(task, desired, /* verify = */ false); |
| 1180 | } |
| 1181 | |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1182 | static void binder_transaction_priority(struct task_struct *task, |
| 1183 | struct binder_transaction *t, |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1184 | struct binder_priority node_prio, |
| 1185 | bool inherit_rt) |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1186 | { |
| 1187 | struct binder_priority desired_prio; |
| 1188 | |
| 1189 | if (t->set_priority_called) |
| 1190 | return; |
| 1191 | |
| 1192 | t->set_priority_called = true; |
| 1193 | t->saved_priority.sched_policy = task->policy; |
| 1194 | t->saved_priority.prio = task->normal_prio; |
| 1195 | |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1196 | if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) { |
| 1197 | desired_prio.prio = NICE_TO_PRIO(0); |
| 1198 | desired_prio.sched_policy = SCHED_NORMAL; |
| 1199 | } else { |
| 1200 | desired_prio.prio = t->priority.prio; |
| 1201 | desired_prio.sched_policy = t->priority.sched_policy; |
| 1202 | } |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1203 | |
| 1204 | if (node_prio.prio < t->priority.prio || |
| 1205 | (node_prio.prio == t->priority.prio && |
| 1206 | node_prio.sched_policy == SCHED_FIFO)) { |
| 1207 | /* |
| 1208 | * In case the minimum priority on the node is |
| 1209 | * higher (lower value), use that priority. If |
| 1210 | * the priority is the same, but the node uses |
| 1211 | * SCHED_FIFO, prefer SCHED_FIFO, since it can |
| 1212 | * run unbounded, unlike SCHED_RR. |
| 1213 | */ |
| 1214 | desired_prio = node_prio; |
| 1215 | } |
| 1216 | |
| 1217 | binder_set_priority(task, desired_prio); |
| 1218 | } |
| 1219 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1220 | static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc, |
| 1221 | binder_uintptr_t ptr) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1222 | { |
| 1223 | struct rb_node *n = proc->nodes.rb_node; |
| 1224 | struct binder_node *node; |
| 1225 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1226 | assert_spin_locked(&proc->inner_lock); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1227 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1228 | while (n) { |
| 1229 | node = rb_entry(n, struct binder_node, rb_node); |
| 1230 | |
| 1231 | if (ptr < node->ptr) |
| 1232 | n = n->rb_left; |
| 1233 | else if (ptr > node->ptr) |
| 1234 | n = n->rb_right; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1235 | else { |
| 1236 | /* |
| 1237 | * take an implicit weak reference |
| 1238 | * to ensure node stays alive until |
| 1239 | * call to binder_put_node() |
| 1240 | */ |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1241 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1242 | return node; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1243 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1244 | } |
| 1245 | return NULL; |
| 1246 | } |
| 1247 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1248 | static struct binder_node *binder_get_node(struct binder_proc *proc, |
| 1249 | binder_uintptr_t ptr) |
| 1250 | { |
| 1251 | struct binder_node *node; |
| 1252 | |
| 1253 | binder_inner_proc_lock(proc); |
| 1254 | node = binder_get_node_ilocked(proc, ptr); |
| 1255 | binder_inner_proc_unlock(proc); |
| 1256 | return node; |
| 1257 | } |
| 1258 | |
| 1259 | static struct binder_node *binder_init_node_ilocked( |
| 1260 | struct binder_proc *proc, |
| 1261 | struct binder_node *new_node, |
| 1262 | struct flat_binder_object *fp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1263 | { |
| 1264 | struct rb_node **p = &proc->nodes.rb_node; |
| 1265 | struct rb_node *parent = NULL; |
| 1266 | struct binder_node *node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1267 | binder_uintptr_t ptr = fp ? fp->binder : 0; |
| 1268 | binder_uintptr_t cookie = fp ? fp->cookie : 0; |
| 1269 | __u32 flags = fp ? fp->flags : 0; |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1270 | s8 priority; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1271 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1272 | assert_spin_locked(&proc->inner_lock); |
| 1273 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1274 | while (*p) { |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1275 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1276 | parent = *p; |
| 1277 | node = rb_entry(parent, struct binder_node, rb_node); |
| 1278 | |
| 1279 | if (ptr < node->ptr) |
| 1280 | p = &(*p)->rb_left; |
| 1281 | else if (ptr > node->ptr) |
| 1282 | p = &(*p)->rb_right; |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1283 | else { |
| 1284 | /* |
| 1285 | * A matching node is already in |
| 1286 | * the rb tree. Abandon the init |
| 1287 | * and return it. |
| 1288 | */ |
| 1289 | binder_inc_node_tmpref_ilocked(node); |
| 1290 | return node; |
| 1291 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1292 | } |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1293 | node = new_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1294 | binder_stats_created(BINDER_STAT_NODE); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1295 | node->tmp_refs++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1296 | rb_link_node(&node->rb_node, parent, p); |
| 1297 | rb_insert_color(&node->rb_node, &proc->nodes); |
Todd Kjos | c4bd08b | 2017-05-25 10:56:00 -0700 | [diff] [blame] | 1298 | node->debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1299 | node->proc = proc; |
| 1300 | node->ptr = ptr; |
| 1301 | node->cookie = cookie; |
| 1302 | node->work.type = BINDER_WORK_NODE; |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1303 | priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK; |
| 1304 | node->sched_policy = (flags & FLAT_BINDER_FLAG_PRIORITY_MASK) >> |
| 1305 | FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT; |
| 1306 | node->min_priority = to_kernel_prio(node->sched_policy, priority); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1307 | node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1308 | node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT); |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 1309 | spin_lock_init(&node->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1310 | INIT_LIST_HEAD(&node->work.entry); |
| 1311 | INIT_LIST_HEAD(&node->async_todo); |
| 1312 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1313 | "%d:%d node %d u%016llx c%016llx created\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1314 | proc->pid, current->pid, node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1315 | (u64)node->ptr, (u64)node->cookie); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1316 | |
| 1317 | return node; |
| 1318 | } |
| 1319 | |
| 1320 | static struct binder_node *binder_new_node(struct binder_proc *proc, |
| 1321 | struct flat_binder_object *fp) |
| 1322 | { |
| 1323 | struct binder_node *node; |
| 1324 | struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 1325 | |
| 1326 | if (!new_node) |
| 1327 | return NULL; |
| 1328 | binder_inner_proc_lock(proc); |
| 1329 | node = binder_init_node_ilocked(proc, new_node, fp); |
| 1330 | binder_inner_proc_unlock(proc); |
| 1331 | if (node != new_node) |
| 1332 | /* |
| 1333 | * The node was already added by another thread |
| 1334 | */ |
| 1335 | kfree(new_node); |
| 1336 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1337 | return node; |
| 1338 | } |
| 1339 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1340 | static void binder_free_node(struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1341 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1342 | kfree(node); |
| 1343 | binder_stats_deleted(BINDER_STAT_NODE); |
| 1344 | } |
| 1345 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1346 | static int binder_inc_node_nilocked(struct binder_node *node, int strong, |
| 1347 | int internal, |
| 1348 | struct list_head *target_list) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1349 | { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1350 | struct binder_proc *proc = node->proc; |
| 1351 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1352 | assert_spin_locked(&node->lock); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1353 | if (proc) |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1354 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1355 | if (strong) { |
| 1356 | if (internal) { |
| 1357 | if (target_list == NULL && |
| 1358 | node->internal_strong_refs == 0 && |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 1359 | !(node->proc && |
| 1360 | node == node->proc->context-> |
| 1361 | binder_context_mgr_node && |
| 1362 | node->has_strong_ref)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1363 | pr_err("invalid inc strong node for %d\n", |
| 1364 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1365 | return -EINVAL; |
| 1366 | } |
| 1367 | node->internal_strong_refs++; |
| 1368 | } else |
| 1369 | node->local_strong_refs++; |
| 1370 | if (!node->has_strong_ref && target_list) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1371 | binder_dequeue_work_ilocked(&node->work); |
| 1372 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1373 | } |
| 1374 | } else { |
| 1375 | if (!internal) |
| 1376 | node->local_weak_refs++; |
| 1377 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { |
| 1378 | if (target_list == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1379 | pr_err("invalid inc weak node for %d\n", |
| 1380 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1381 | return -EINVAL; |
| 1382 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1383 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1389 | static int binder_inc_node(struct binder_node *node, int strong, int internal, |
| 1390 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1391 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1392 | int ret; |
| 1393 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1394 | binder_node_inner_lock(node); |
| 1395 | ret = binder_inc_node_nilocked(node, strong, internal, target_list); |
| 1396 | binder_node_inner_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1397 | |
| 1398 | return ret; |
| 1399 | } |
| 1400 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1401 | static bool binder_dec_node_nilocked(struct binder_node *node, |
| 1402 | int strong, int internal) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1403 | { |
| 1404 | struct binder_proc *proc = node->proc; |
| 1405 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1406 | assert_spin_locked(&node->lock); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1407 | if (proc) |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1408 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1409 | if (strong) { |
| 1410 | if (internal) |
| 1411 | node->internal_strong_refs--; |
| 1412 | else |
| 1413 | node->local_strong_refs--; |
| 1414 | if (node->local_strong_refs || node->internal_strong_refs) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1415 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1416 | } else { |
| 1417 | if (!internal) |
| 1418 | node->local_weak_refs--; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1419 | if (node->local_weak_refs || node->tmp_refs || |
| 1420 | !hlist_empty(&node->refs)) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1421 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1422 | } |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1423 | |
| 1424 | if (proc && (node->has_strong_ref || node->has_weak_ref)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1425 | if (list_empty(&node->work.entry)) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1426 | binder_enqueue_work_ilocked(&node->work, &proc->todo); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1427 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1428 | } |
| 1429 | } else { |
| 1430 | if (hlist_empty(&node->refs) && !node->local_strong_refs && |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1431 | !node->local_weak_refs && !node->tmp_refs) { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1432 | if (proc) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1433 | binder_dequeue_work_ilocked(&node->work); |
| 1434 | rb_erase(&node->rb_node, &proc->nodes); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1435 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1436 | "refless node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1437 | node->debug_id); |
| 1438 | } else { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1439 | BUG_ON(!list_empty(&node->work.entry)); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 1440 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1441 | /* |
| 1442 | * tmp_refs could have changed so |
| 1443 | * check it again |
| 1444 | */ |
| 1445 | if (node->tmp_refs) { |
| 1446 | spin_unlock(&binder_dead_nodes_lock); |
| 1447 | return false; |
| 1448 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1449 | hlist_del(&node->dead_node); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 1450 | spin_unlock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1451 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1452 | "dead node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1453 | node->debug_id); |
| 1454 | } |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1455 | return true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1456 | } |
| 1457 | } |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1458 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1459 | } |
| 1460 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1461 | static void binder_dec_node(struct binder_node *node, int strong, int internal) |
| 1462 | { |
| 1463 | bool free_node; |
| 1464 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1465 | binder_node_inner_lock(node); |
| 1466 | free_node = binder_dec_node_nilocked(node, strong, internal); |
| 1467 | binder_node_inner_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1468 | if (free_node) |
| 1469 | binder_free_node(node); |
| 1470 | } |
| 1471 | |
| 1472 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node) |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1473 | { |
| 1474 | /* |
| 1475 | * No call to binder_inc_node() is needed since we |
| 1476 | * don't need to inform userspace of any changes to |
| 1477 | * tmp_refs |
| 1478 | */ |
| 1479 | node->tmp_refs++; |
| 1480 | } |
| 1481 | |
| 1482 | /** |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1483 | * binder_inc_node_tmpref() - take a temporary reference on node |
| 1484 | * @node: node to reference |
| 1485 | * |
| 1486 | * Take reference on node to prevent the node from being freed |
| 1487 | * while referenced only by a local variable. The inner lock is |
| 1488 | * needed to serialize with the node work on the queue (which |
| 1489 | * isn't needed after the node is dead). If the node is dead |
| 1490 | * (node->proc is NULL), use binder_dead_nodes_lock to protect |
| 1491 | * node->tmp_refs against dead-node-only cases where the node |
| 1492 | * lock cannot be acquired (eg traversing the dead node list to |
| 1493 | * print nodes) |
| 1494 | */ |
| 1495 | static void binder_inc_node_tmpref(struct binder_node *node) |
| 1496 | { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1497 | binder_node_lock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1498 | if (node->proc) |
| 1499 | binder_inner_proc_lock(node->proc); |
| 1500 | else |
| 1501 | spin_lock(&binder_dead_nodes_lock); |
| 1502 | binder_inc_node_tmpref_ilocked(node); |
| 1503 | if (node->proc) |
| 1504 | binder_inner_proc_unlock(node->proc); |
| 1505 | else |
| 1506 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1507 | binder_node_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | /** |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1511 | * binder_dec_node_tmpref() - remove a temporary reference on node |
| 1512 | * @node: node to reference |
| 1513 | * |
| 1514 | * Release temporary reference on node taken via binder_inc_node_tmpref() |
| 1515 | */ |
| 1516 | static void binder_dec_node_tmpref(struct binder_node *node) |
| 1517 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1518 | bool free_node; |
| 1519 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1520 | binder_node_inner_lock(node); |
| 1521 | if (!node->proc) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1522 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1523 | node->tmp_refs--; |
| 1524 | BUG_ON(node->tmp_refs < 0); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1525 | if (!node->proc) |
| 1526 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1527 | /* |
| 1528 | * Call binder_dec_node() to check if all refcounts are 0 |
| 1529 | * and cleanup is needed. Calling with strong=0 and internal=1 |
| 1530 | * causes no actual reference to be released in binder_dec_node(). |
| 1531 | * If that changes, a change is needed here too. |
| 1532 | */ |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1533 | free_node = binder_dec_node_nilocked(node, 0, 1); |
| 1534 | binder_node_inner_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1535 | if (free_node) |
| 1536 | binder_free_node(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | static void binder_put_node(struct binder_node *node) |
| 1540 | { |
| 1541 | binder_dec_node_tmpref(node); |
| 1542 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1543 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1544 | static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc, |
| 1545 | u32 desc, bool need_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1546 | { |
| 1547 | struct rb_node *n = proc->refs_by_desc.rb_node; |
| 1548 | struct binder_ref *ref; |
| 1549 | |
| 1550 | while (n) { |
| 1551 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1552 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1553 | if (desc < ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1554 | n = n->rb_left; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1555 | } else if (desc > ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1556 | n = n->rb_right; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1557 | } else if (need_strong_ref && !ref->data.strong) { |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1558 | binder_user_error("tried to use weak ref as strong ref\n"); |
| 1559 | return NULL; |
| 1560 | } else { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1561 | return ref; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1562 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1563 | } |
| 1564 | return NULL; |
| 1565 | } |
| 1566 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1567 | /** |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1568 | * binder_get_ref_for_node_olocked() - get the ref associated with given node |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1569 | * @proc: binder_proc that owns the ref |
| 1570 | * @node: binder_node of target |
| 1571 | * @new_ref: newly allocated binder_ref to be initialized or %NULL |
| 1572 | * |
| 1573 | * Look up the ref for the given node and return it if it exists |
| 1574 | * |
| 1575 | * If it doesn't exist and the caller provides a newly allocated |
| 1576 | * ref, initialize the fields of the newly allocated ref and insert |
| 1577 | * into the given proc rb_trees and node refs list. |
| 1578 | * |
| 1579 | * Return: the ref for node. It is possible that another thread |
| 1580 | * allocated/initialized the ref first in which case the |
| 1581 | * returned ref would be different than the passed-in |
| 1582 | * new_ref. new_ref must be kfree'd by the caller in |
| 1583 | * this case. |
| 1584 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1585 | static struct binder_ref *binder_get_ref_for_node_olocked( |
| 1586 | struct binder_proc *proc, |
| 1587 | struct binder_node *node, |
| 1588 | struct binder_ref *new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1589 | { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1590 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1591 | struct rb_node **p = &proc->refs_by_node.rb_node; |
| 1592 | struct rb_node *parent = NULL; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1593 | struct binder_ref *ref; |
| 1594 | struct rb_node *n; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1595 | |
| 1596 | while (*p) { |
| 1597 | parent = *p; |
| 1598 | ref = rb_entry(parent, struct binder_ref, rb_node_node); |
| 1599 | |
| 1600 | if (node < ref->node) |
| 1601 | p = &(*p)->rb_left; |
| 1602 | else if (node > ref->node) |
| 1603 | p = &(*p)->rb_right; |
| 1604 | else |
| 1605 | return ref; |
| 1606 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1607 | if (!new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1608 | return NULL; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1609 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1610 | binder_stats_created(BINDER_STAT_REF); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1611 | new_ref->data.debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1612 | new_ref->proc = proc; |
| 1613 | new_ref->node = node; |
| 1614 | rb_link_node(&new_ref->rb_node_node, parent, p); |
| 1615 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); |
| 1616 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1617 | 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] | 1618 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 1619 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1620 | if (ref->data.desc > new_ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1621 | break; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1622 | new_ref->data.desc = ref->data.desc + 1; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | p = &proc->refs_by_desc.rb_node; |
| 1626 | while (*p) { |
| 1627 | parent = *p; |
| 1628 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); |
| 1629 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1630 | if (new_ref->data.desc < ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1631 | p = &(*p)->rb_left; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1632 | else if (new_ref->data.desc > ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1633 | p = &(*p)->rb_right; |
| 1634 | else |
| 1635 | BUG(); |
| 1636 | } |
| 1637 | rb_link_node(&new_ref->rb_node_desc, parent, p); |
| 1638 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1639 | |
| 1640 | binder_node_lock(node); |
Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1641 | hlist_add_head(&new_ref->node_entry, &node->refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1642 | |
Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1643 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1644 | "%d new ref %d desc %d for node %d\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1645 | proc->pid, new_ref->data.debug_id, new_ref->data.desc, |
Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1646 | node->debug_id); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1647 | binder_node_unlock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1648 | return new_ref; |
| 1649 | } |
| 1650 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1651 | static void binder_cleanup_ref_olocked(struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1652 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1653 | bool delete_node = false; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1654 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1655 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1656 | "%d delete ref %d desc %d for node %d\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1657 | ref->proc->pid, ref->data.debug_id, ref->data.desc, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1658 | ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1659 | |
| 1660 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); |
| 1661 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1662 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1663 | binder_node_inner_lock(ref->node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1664 | if (ref->data.strong) |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1665 | binder_dec_node_nilocked(ref->node, 1, 1); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1666 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1667 | hlist_del(&ref->node_entry); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1668 | delete_node = binder_dec_node_nilocked(ref->node, 0, 1); |
| 1669 | binder_node_inner_unlock(ref->node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1670 | /* |
| 1671 | * Clear ref->node unless we want the caller to free the node |
| 1672 | */ |
| 1673 | if (!delete_node) { |
| 1674 | /* |
| 1675 | * The caller uses ref->node to determine |
| 1676 | * whether the node needs to be freed. Clear |
| 1677 | * it since the node is still alive. |
| 1678 | */ |
| 1679 | ref->node = NULL; |
| 1680 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1681 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1682 | if (ref->death) { |
| 1683 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1684 | "%d delete ref %d desc %d has death notification\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1685 | ref->proc->pid, ref->data.debug_id, |
| 1686 | ref->data.desc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1687 | binder_dequeue_work(ref->proc, &ref->death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1688 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 1689 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1690 | binder_stats_deleted(BINDER_STAT_REF); |
| 1691 | } |
| 1692 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1693 | /** |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1694 | * binder_inc_ref_olocked() - increment the ref for given handle |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1695 | * @ref: ref to be incremented |
| 1696 | * @strong: if true, strong increment, else weak |
| 1697 | * @target_list: list to queue node work on |
| 1698 | * |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1699 | * Increment the ref. @ref->proc->outer_lock must be held on entry |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1700 | * |
| 1701 | * Return: 0, if successful, else errno |
| 1702 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1703 | static int binder_inc_ref_olocked(struct binder_ref *ref, int strong, |
| 1704 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1705 | { |
| 1706 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1707 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1708 | if (strong) { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1709 | if (ref->data.strong == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1710 | ret = binder_inc_node(ref->node, 1, 1, target_list); |
| 1711 | if (ret) |
| 1712 | return ret; |
| 1713 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1714 | ref->data.strong++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1715 | } else { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1716 | if (ref->data.weak == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1717 | ret = binder_inc_node(ref->node, 0, 1, target_list); |
| 1718 | if (ret) |
| 1719 | return ret; |
| 1720 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1721 | ref->data.weak++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1722 | } |
| 1723 | return 0; |
| 1724 | } |
| 1725 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1726 | /** |
| 1727 | * binder_dec_ref() - dec the ref for given handle |
| 1728 | * @ref: ref to be decremented |
| 1729 | * @strong: if true, strong decrement, else weak |
| 1730 | * |
| 1731 | * Decrement the ref. |
| 1732 | * |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1733 | * Return: true if ref is cleaned up and ready to be freed |
| 1734 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1735 | static bool binder_dec_ref_olocked(struct binder_ref *ref, int strong) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1736 | { |
| 1737 | if (strong) { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1738 | if (ref->data.strong == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1739 | 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] | 1740 | ref->proc->pid, ref->data.debug_id, |
| 1741 | ref->data.desc, ref->data.strong, |
| 1742 | ref->data.weak); |
| 1743 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1744 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1745 | ref->data.strong--; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1746 | if (ref->data.strong == 0) |
| 1747 | binder_dec_node(ref->node, strong, 1); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1748 | } else { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1749 | if (ref->data.weak == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1750 | 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] | 1751 | ref->proc->pid, ref->data.debug_id, |
| 1752 | ref->data.desc, ref->data.strong, |
| 1753 | ref->data.weak); |
| 1754 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1755 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1756 | ref->data.weak--; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1757 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1758 | if (ref->data.strong == 0 && ref->data.weak == 0) { |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1759 | binder_cleanup_ref_olocked(ref); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1760 | return true; |
| 1761 | } |
| 1762 | return false; |
| 1763 | } |
| 1764 | |
| 1765 | /** |
| 1766 | * binder_get_node_from_ref() - get the node from the given proc/desc |
| 1767 | * @proc: proc containing the ref |
| 1768 | * @desc: the handle associated with the ref |
| 1769 | * @need_strong_ref: if true, only return node if ref is strong |
| 1770 | * @rdata: the id/refcount data for the ref |
| 1771 | * |
| 1772 | * Given a proc and ref handle, return the associated binder_node |
| 1773 | * |
| 1774 | * Return: a binder_node or NULL if not found or not strong when strong required |
| 1775 | */ |
| 1776 | static struct binder_node *binder_get_node_from_ref( |
| 1777 | struct binder_proc *proc, |
| 1778 | u32 desc, bool need_strong_ref, |
| 1779 | struct binder_ref_data *rdata) |
| 1780 | { |
| 1781 | struct binder_node *node; |
| 1782 | struct binder_ref *ref; |
| 1783 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1784 | binder_proc_lock(proc); |
| 1785 | ref = binder_get_ref_olocked(proc, desc, need_strong_ref); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1786 | if (!ref) |
| 1787 | goto err_no_ref; |
| 1788 | node = ref->node; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1789 | /* |
| 1790 | * Take an implicit reference on the node to ensure |
| 1791 | * it stays alive until the call to binder_put_node() |
| 1792 | */ |
| 1793 | binder_inc_node_tmpref(node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1794 | if (rdata) |
| 1795 | *rdata = ref->data; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1796 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1797 | |
| 1798 | return node; |
| 1799 | |
| 1800 | err_no_ref: |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1801 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1802 | return NULL; |
| 1803 | } |
| 1804 | |
| 1805 | /** |
| 1806 | * binder_free_ref() - free the binder_ref |
| 1807 | * @ref: ref to free |
| 1808 | * |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1809 | * Free the binder_ref. Free the binder_node indicated by ref->node |
| 1810 | * (if non-NULL) and the binder_ref_death indicated by ref->death. |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1811 | */ |
| 1812 | static void binder_free_ref(struct binder_ref *ref) |
| 1813 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1814 | if (ref->node) |
| 1815 | binder_free_node(ref->node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1816 | kfree(ref->death); |
| 1817 | kfree(ref); |
| 1818 | } |
| 1819 | |
| 1820 | /** |
| 1821 | * binder_update_ref_for_handle() - inc/dec the ref for given handle |
| 1822 | * @proc: proc containing the ref |
| 1823 | * @desc: the handle associated with the ref |
| 1824 | * @increment: true=inc reference, false=dec reference |
| 1825 | * @strong: true=strong reference, false=weak reference |
| 1826 | * @rdata: the id/refcount data for the ref |
| 1827 | * |
| 1828 | * Given a proc and ref handle, increment or decrement the ref |
| 1829 | * according to "increment" arg. |
| 1830 | * |
| 1831 | * Return: 0 if successful, else errno |
| 1832 | */ |
| 1833 | static int binder_update_ref_for_handle(struct binder_proc *proc, |
| 1834 | uint32_t desc, bool increment, bool strong, |
| 1835 | struct binder_ref_data *rdata) |
| 1836 | { |
| 1837 | int ret = 0; |
| 1838 | struct binder_ref *ref; |
| 1839 | bool delete_ref = false; |
| 1840 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1841 | binder_proc_lock(proc); |
| 1842 | ref = binder_get_ref_olocked(proc, desc, strong); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1843 | if (!ref) { |
| 1844 | ret = -EINVAL; |
| 1845 | goto err_no_ref; |
| 1846 | } |
| 1847 | if (increment) |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1848 | ret = binder_inc_ref_olocked(ref, strong, NULL); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1849 | else |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1850 | delete_ref = binder_dec_ref_olocked(ref, strong); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1851 | |
| 1852 | if (rdata) |
| 1853 | *rdata = ref->data; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1854 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1855 | |
| 1856 | if (delete_ref) |
| 1857 | binder_free_ref(ref); |
| 1858 | return ret; |
| 1859 | |
| 1860 | err_no_ref: |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1861 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1862 | return ret; |
| 1863 | } |
| 1864 | |
| 1865 | /** |
| 1866 | * binder_dec_ref_for_handle() - dec the ref for given handle |
| 1867 | * @proc: proc containing the ref |
| 1868 | * @desc: the handle associated with the ref |
| 1869 | * @strong: true=strong reference, false=weak reference |
| 1870 | * @rdata: the id/refcount data for the ref |
| 1871 | * |
| 1872 | * Just calls binder_update_ref_for_handle() to decrement the ref. |
| 1873 | * |
| 1874 | * Return: 0 if successful, else errno |
| 1875 | */ |
| 1876 | static int binder_dec_ref_for_handle(struct binder_proc *proc, |
| 1877 | uint32_t desc, bool strong, struct binder_ref_data *rdata) |
| 1878 | { |
| 1879 | return binder_update_ref_for_handle(proc, desc, false, strong, rdata); |
| 1880 | } |
| 1881 | |
| 1882 | |
| 1883 | /** |
| 1884 | * binder_inc_ref_for_node() - increment the ref for given proc/node |
| 1885 | * @proc: proc containing the ref |
| 1886 | * @node: target node |
| 1887 | * @strong: true=strong reference, false=weak reference |
| 1888 | * @target_list: worklist to use if node is incremented |
| 1889 | * @rdata: the id/refcount data for the ref |
| 1890 | * |
| 1891 | * Given a proc and node, increment the ref. Create the ref if it |
| 1892 | * doesn't already exist |
| 1893 | * |
| 1894 | * Return: 0 if successful, else errno |
| 1895 | */ |
| 1896 | static int binder_inc_ref_for_node(struct binder_proc *proc, |
| 1897 | struct binder_node *node, |
| 1898 | bool strong, |
| 1899 | struct list_head *target_list, |
| 1900 | struct binder_ref_data *rdata) |
| 1901 | { |
| 1902 | struct binder_ref *ref; |
| 1903 | struct binder_ref *new_ref = NULL; |
| 1904 | int ret = 0; |
| 1905 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1906 | binder_proc_lock(proc); |
| 1907 | ref = binder_get_ref_for_node_olocked(proc, node, NULL); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1908 | if (!ref) { |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1909 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1910 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
| 1911 | if (!new_ref) |
| 1912 | return -ENOMEM; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1913 | binder_proc_lock(proc); |
| 1914 | ref = binder_get_ref_for_node_olocked(proc, node, new_ref); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1915 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1916 | ret = binder_inc_ref_olocked(ref, strong, target_list); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1917 | *rdata = ref->data; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1918 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1919 | if (new_ref && ref != new_ref) |
| 1920 | /* |
| 1921 | * Another thread created the ref first so |
| 1922 | * free the one we allocated |
| 1923 | */ |
| 1924 | kfree(new_ref); |
| 1925 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1926 | } |
| 1927 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 1928 | static void binder_pop_transaction_ilocked(struct binder_thread *target_thread, |
| 1929 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1930 | { |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 1931 | BUG_ON(!target_thread); |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1932 | assert_spin_locked(&target_thread->proc->inner_lock); |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 1933 | BUG_ON(target_thread->transaction_stack != t); |
| 1934 | BUG_ON(target_thread->transaction_stack->from != target_thread); |
| 1935 | target_thread->transaction_stack = |
| 1936 | target_thread->transaction_stack->from_parent; |
| 1937 | t->from = NULL; |
| 1938 | } |
| 1939 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1940 | /** |
| 1941 | * binder_thread_dec_tmpref() - decrement thread->tmp_ref |
| 1942 | * @thread: thread to decrement |
| 1943 | * |
| 1944 | * A thread needs to be kept alive while being used to create or |
| 1945 | * handle a transaction. binder_get_txn_from() is used to safely |
| 1946 | * extract t->from from a binder_transaction and keep the thread |
| 1947 | * indicated by t->from from being freed. When done with that |
| 1948 | * binder_thread, this function is called to decrement the |
| 1949 | * tmp_ref and free if appropriate (thread has been released |
| 1950 | * and no transaction being processed by the driver) |
| 1951 | */ |
| 1952 | static void binder_thread_dec_tmpref(struct binder_thread *thread) |
| 1953 | { |
| 1954 | /* |
| 1955 | * atomic is used to protect the counter value while |
| 1956 | * it cannot reach zero or thread->is_dead is false |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1957 | */ |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 1958 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1959 | atomic_dec(&thread->tmp_ref); |
| 1960 | if (thread->is_dead && !atomic_read(&thread->tmp_ref)) { |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 1961 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1962 | binder_free_thread(thread); |
| 1963 | return; |
| 1964 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 1965 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | /** |
| 1969 | * binder_proc_dec_tmpref() - decrement proc->tmp_ref |
| 1970 | * @proc: proc to decrement |
| 1971 | * |
| 1972 | * A binder_proc needs to be kept alive while being used to create or |
| 1973 | * handle a transaction. proc->tmp_ref is incremented when |
| 1974 | * creating a new transaction or the binder_proc is currently in-use |
| 1975 | * by threads that are being released. When done with the binder_proc, |
| 1976 | * this function is called to decrement the counter and free the |
| 1977 | * proc if appropriate (proc has been released, all threads have |
| 1978 | * been released and not currenly in-use to process a transaction). |
| 1979 | */ |
| 1980 | static void binder_proc_dec_tmpref(struct binder_proc *proc) |
| 1981 | { |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 1982 | binder_inner_proc_lock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1983 | proc->tmp_ref--; |
| 1984 | if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) && |
| 1985 | !proc->tmp_ref) { |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 1986 | binder_inner_proc_unlock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1987 | binder_free_proc(proc); |
| 1988 | return; |
| 1989 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 1990 | binder_inner_proc_unlock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | /** |
| 1994 | * binder_get_txn_from() - safely extract the "from" thread in transaction |
| 1995 | * @t: binder transaction for t->from |
| 1996 | * |
| 1997 | * Atomically return the "from" thread and increment the tmp_ref |
| 1998 | * count for the thread to ensure it stays alive until |
| 1999 | * binder_thread_dec_tmpref() is called. |
| 2000 | * |
| 2001 | * Return: the value of t->from |
| 2002 | */ |
| 2003 | static struct binder_thread *binder_get_txn_from( |
| 2004 | struct binder_transaction *t) |
| 2005 | { |
| 2006 | struct binder_thread *from; |
| 2007 | |
| 2008 | spin_lock(&t->lock); |
| 2009 | from = t->from; |
| 2010 | if (from) |
| 2011 | atomic_inc(&from->tmp_ref); |
| 2012 | spin_unlock(&t->lock); |
| 2013 | return from; |
| 2014 | } |
| 2015 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2016 | /** |
| 2017 | * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock |
| 2018 | * @t: binder transaction for t->from |
| 2019 | * |
| 2020 | * Same as binder_get_txn_from() except it also acquires the proc->inner_lock |
| 2021 | * to guarantee that the thread cannot be released while operating on it. |
| 2022 | * The caller must call binder_inner_proc_unlock() to release the inner lock |
| 2023 | * as well as call binder_dec_thread_txn() to release the reference. |
| 2024 | * |
| 2025 | * Return: the value of t->from |
| 2026 | */ |
| 2027 | static struct binder_thread *binder_get_txn_from_and_acq_inner( |
| 2028 | struct binder_transaction *t) |
| 2029 | { |
| 2030 | struct binder_thread *from; |
| 2031 | |
| 2032 | from = binder_get_txn_from(t); |
| 2033 | if (!from) |
| 2034 | return NULL; |
| 2035 | binder_inner_proc_lock(from->proc); |
| 2036 | if (t->from) { |
| 2037 | BUG_ON(from != t->from); |
| 2038 | return from; |
| 2039 | } |
| 2040 | binder_inner_proc_unlock(from->proc); |
| 2041 | binder_thread_dec_tmpref(from); |
| 2042 | return NULL; |
| 2043 | } |
| 2044 | |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2045 | static void binder_free_transaction(struct binder_transaction *t) |
| 2046 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2047 | if (t->buffer) |
| 2048 | t->buffer->transaction = NULL; |
| 2049 | kfree(t); |
| 2050 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 2051 | } |
| 2052 | |
| 2053 | static void binder_send_failed_reply(struct binder_transaction *t, |
| 2054 | uint32_t error_code) |
| 2055 | { |
| 2056 | struct binder_thread *target_thread; |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2057 | struct binder_transaction *next; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2058 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2059 | BUG_ON(t->flags & TF_ONE_WAY); |
| 2060 | while (1) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2061 | target_thread = binder_get_txn_from_and_acq_inner(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2062 | if (target_thread) { |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2063 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2064 | "send failed reply for transaction %d to %d:%d\n", |
| 2065 | t->debug_id, |
| 2066 | target_thread->proc->pid, |
| 2067 | target_thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2068 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2069 | binder_pop_transaction_ilocked(target_thread, t); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2070 | if (target_thread->reply_error.cmd == BR_OK) { |
| 2071 | target_thread->reply_error.cmd = error_code; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2072 | binder_enqueue_work_ilocked( |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 2073 | &target_thread->reply_error.work, |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2074 | &target_thread->todo); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2075 | wake_up_interruptible(&target_thread->wait); |
| 2076 | } else { |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2077 | WARN(1, "Unexpected reply error: %u\n", |
| 2078 | target_thread->reply_error.cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2079 | } |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2080 | binder_inner_proc_unlock(target_thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2081 | binder_thread_dec_tmpref(target_thread); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2082 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2083 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2084 | } |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2085 | next = t->from_parent; |
| 2086 | |
| 2087 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2088 | "send failed reply for transaction %d, target dead\n", |
| 2089 | t->debug_id); |
| 2090 | |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2091 | binder_free_transaction(t); |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2092 | if (next == NULL) { |
| 2093 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2094 | "reply failed, no target thread at root\n"); |
| 2095 | return; |
| 2096 | } |
| 2097 | t = next; |
| 2098 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2099 | "reply failed, no target thread -- retry %d\n", |
| 2100 | t->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2104 | /** |
| 2105 | * binder_validate_object() - checks for a valid metadata object in a buffer. |
| 2106 | * @buffer: binder_buffer that we're parsing. |
| 2107 | * @offset: offset in the buffer at which to validate an object. |
| 2108 | * |
| 2109 | * Return: If there's a valid metadata object at @offset in @buffer, the |
| 2110 | * size of that object. Otherwise, it returns zero. |
| 2111 | */ |
| 2112 | static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset) |
| 2113 | { |
| 2114 | /* Check if we can read a header first */ |
| 2115 | struct binder_object_header *hdr; |
| 2116 | size_t object_size = 0; |
| 2117 | |
| 2118 | if (offset > buffer->data_size - sizeof(*hdr) || |
| 2119 | buffer->data_size < sizeof(*hdr) || |
| 2120 | !IS_ALIGNED(offset, sizeof(u32))) |
| 2121 | return 0; |
| 2122 | |
| 2123 | /* Ok, now see if we can read a complete object. */ |
| 2124 | hdr = (struct binder_object_header *)(buffer->data + offset); |
| 2125 | switch (hdr->type) { |
| 2126 | case BINDER_TYPE_BINDER: |
| 2127 | case BINDER_TYPE_WEAK_BINDER: |
| 2128 | case BINDER_TYPE_HANDLE: |
| 2129 | case BINDER_TYPE_WEAK_HANDLE: |
| 2130 | object_size = sizeof(struct flat_binder_object); |
| 2131 | break; |
| 2132 | case BINDER_TYPE_FD: |
| 2133 | object_size = sizeof(struct binder_fd_object); |
| 2134 | break; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2135 | case BINDER_TYPE_PTR: |
| 2136 | object_size = sizeof(struct binder_buffer_object); |
| 2137 | break; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2138 | case BINDER_TYPE_FDA: |
| 2139 | object_size = sizeof(struct binder_fd_array_object); |
| 2140 | break; |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2141 | default: |
| 2142 | return 0; |
| 2143 | } |
| 2144 | if (offset <= buffer->data_size - object_size && |
| 2145 | buffer->data_size >= object_size) |
| 2146 | return object_size; |
| 2147 | else |
| 2148 | return 0; |
| 2149 | } |
| 2150 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2151 | /** |
| 2152 | * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer. |
| 2153 | * @b: binder_buffer containing the object |
| 2154 | * @index: index in offset array at which the binder_buffer_object is |
| 2155 | * located |
| 2156 | * @start: points to the start of the offset array |
| 2157 | * @num_valid: the number of valid offsets in the offset array |
| 2158 | * |
| 2159 | * Return: If @index is within the valid range of the offset array |
| 2160 | * described by @start and @num_valid, and if there's a valid |
| 2161 | * binder_buffer_object at the offset found in index @index |
| 2162 | * of the offset array, that object is returned. Otherwise, |
| 2163 | * %NULL is returned. |
| 2164 | * Note that the offset found in index @index itself is not |
| 2165 | * verified; this function assumes that @num_valid elements |
| 2166 | * from @start were previously verified to have valid offsets. |
| 2167 | */ |
| 2168 | static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b, |
| 2169 | binder_size_t index, |
| 2170 | binder_size_t *start, |
| 2171 | binder_size_t num_valid) |
| 2172 | { |
| 2173 | struct binder_buffer_object *buffer_obj; |
| 2174 | binder_size_t *offp; |
| 2175 | |
| 2176 | if (index >= num_valid) |
| 2177 | return NULL; |
| 2178 | |
| 2179 | offp = start + index; |
| 2180 | buffer_obj = (struct binder_buffer_object *)(b->data + *offp); |
| 2181 | if (buffer_obj->hdr.type != BINDER_TYPE_PTR) |
| 2182 | return NULL; |
| 2183 | |
| 2184 | return buffer_obj; |
| 2185 | } |
| 2186 | |
| 2187 | /** |
| 2188 | * binder_validate_fixup() - validates pointer/fd fixups happen in order. |
| 2189 | * @b: transaction buffer |
| 2190 | * @objects_start start of objects buffer |
| 2191 | * @buffer: binder_buffer_object in which to fix up |
| 2192 | * @offset: start offset in @buffer to fix up |
| 2193 | * @last_obj: last binder_buffer_object that we fixed up in |
| 2194 | * @last_min_offset: minimum fixup offset in @last_obj |
| 2195 | * |
| 2196 | * Return: %true if a fixup in buffer @buffer at offset @offset is |
| 2197 | * allowed. |
| 2198 | * |
| 2199 | * For safety reasons, we only allow fixups inside a buffer to happen |
| 2200 | * at increasing offsets; additionally, we only allow fixup on the last |
| 2201 | * buffer object that was verified, or one of its parents. |
| 2202 | * |
| 2203 | * Example of what is allowed: |
| 2204 | * |
| 2205 | * A |
| 2206 | * B (parent = A, offset = 0) |
| 2207 | * C (parent = A, offset = 16) |
| 2208 | * D (parent = C, offset = 0) |
| 2209 | * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset) |
| 2210 | * |
| 2211 | * Examples of what is not allowed: |
| 2212 | * |
| 2213 | * Decreasing offsets within the same parent: |
| 2214 | * A |
| 2215 | * C (parent = A, offset = 16) |
| 2216 | * B (parent = A, offset = 0) // decreasing offset within A |
| 2217 | * |
| 2218 | * Referring to a parent that wasn't the last object or any of its parents: |
| 2219 | * A |
| 2220 | * B (parent = A, offset = 0) |
| 2221 | * C (parent = A, offset = 0) |
| 2222 | * C (parent = A, offset = 16) |
| 2223 | * D (parent = B, offset = 0) // B is not A or any of A's parents |
| 2224 | */ |
| 2225 | static bool binder_validate_fixup(struct binder_buffer *b, |
| 2226 | binder_size_t *objects_start, |
| 2227 | struct binder_buffer_object *buffer, |
| 2228 | binder_size_t fixup_offset, |
| 2229 | struct binder_buffer_object *last_obj, |
| 2230 | binder_size_t last_min_offset) |
| 2231 | { |
| 2232 | if (!last_obj) { |
| 2233 | /* Nothing to fix up in */ |
| 2234 | return false; |
| 2235 | } |
| 2236 | |
| 2237 | while (last_obj != buffer) { |
| 2238 | /* |
| 2239 | * Safe to retrieve the parent of last_obj, since it |
| 2240 | * was already previously verified by the driver. |
| 2241 | */ |
| 2242 | if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0) |
| 2243 | return false; |
| 2244 | last_min_offset = last_obj->parent_offset + sizeof(uintptr_t); |
| 2245 | last_obj = (struct binder_buffer_object *) |
| 2246 | (b->data + *(objects_start + last_obj->parent)); |
| 2247 | } |
| 2248 | return (fixup_offset >= last_min_offset); |
| 2249 | } |
| 2250 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2251 | static void binder_transaction_buffer_release(struct binder_proc *proc, |
| 2252 | struct binder_buffer *buffer, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2253 | binder_size_t *failed_at) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2254 | { |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2255 | binder_size_t *offp, *off_start, *off_end; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2256 | int debug_id = buffer->debug_id; |
| 2257 | |
| 2258 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2259 | "%d buffer release %d, size %zd-%zd, failed at %p\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2260 | proc->pid, buffer->debug_id, |
| 2261 | buffer->data_size, buffer->offsets_size, failed_at); |
| 2262 | |
| 2263 | if (buffer->target_node) |
| 2264 | binder_dec_node(buffer->target_node, 1, 0); |
| 2265 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2266 | off_start = (binder_size_t *)(buffer->data + |
| 2267 | ALIGN(buffer->data_size, sizeof(void *))); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2268 | if (failed_at) |
| 2269 | off_end = failed_at; |
| 2270 | else |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2271 | off_end = (void *)off_start + buffer->offsets_size; |
| 2272 | for (offp = off_start; offp < off_end; offp++) { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2273 | struct binder_object_header *hdr; |
| 2274 | size_t object_size = binder_validate_object(buffer, *offp); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2275 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2276 | if (object_size == 0) { |
| 2277 | 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] | 2278 | debug_id, (u64)*offp, buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2279 | continue; |
| 2280 | } |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2281 | hdr = (struct binder_object_header *)(buffer->data + *offp); |
| 2282 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2283 | case BINDER_TYPE_BINDER: |
| 2284 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2285 | struct flat_binder_object *fp; |
| 2286 | struct binder_node *node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2287 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2288 | fp = to_flat_binder_object(hdr); |
| 2289 | node = binder_get_node(proc, fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2290 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2291 | pr_err("transaction release %d bad node %016llx\n", |
| 2292 | debug_id, (u64)fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2293 | break; |
| 2294 | } |
| 2295 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2296 | " node %d u%016llx\n", |
| 2297 | node->debug_id, (u64)node->ptr); |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2298 | binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER, |
| 2299 | 0); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2300 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2301 | } break; |
| 2302 | case BINDER_TYPE_HANDLE: |
| 2303 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2304 | struct flat_binder_object *fp; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2305 | struct binder_ref_data rdata; |
| 2306 | int ret; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 2307 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2308 | fp = to_flat_binder_object(hdr); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2309 | ret = binder_dec_ref_for_handle(proc, fp->handle, |
| 2310 | hdr->type == BINDER_TYPE_HANDLE, &rdata); |
| 2311 | |
| 2312 | if (ret) { |
| 2313 | pr_err("transaction release %d bad handle %d, ret = %d\n", |
| 2314 | debug_id, fp->handle, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2315 | break; |
| 2316 | } |
| 2317 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2318 | " ref %d desc %d\n", |
| 2319 | rdata.debug_id, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2320 | } break; |
| 2321 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2322 | case BINDER_TYPE_FD: { |
| 2323 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
| 2324 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2325 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2326 | " fd %d\n", fp->fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2327 | if (failed_at) |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2328 | task_close_fd(proc, fp->fd); |
| 2329 | } break; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2330 | case BINDER_TYPE_PTR: |
| 2331 | /* |
| 2332 | * Nothing to do here, this will get cleaned up when the |
| 2333 | * transaction buffer gets freed |
| 2334 | */ |
| 2335 | break; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2336 | case BINDER_TYPE_FDA: { |
| 2337 | struct binder_fd_array_object *fda; |
| 2338 | struct binder_buffer_object *parent; |
| 2339 | uintptr_t parent_buffer; |
| 2340 | u32 *fd_array; |
| 2341 | size_t fd_index; |
| 2342 | binder_size_t fd_buf_size; |
| 2343 | |
| 2344 | fda = to_binder_fd_array_object(hdr); |
| 2345 | parent = binder_validate_ptr(buffer, fda->parent, |
| 2346 | off_start, |
| 2347 | offp - off_start); |
| 2348 | if (!parent) { |
| 2349 | pr_err("transaction release %d bad parent offset", |
| 2350 | debug_id); |
| 2351 | continue; |
| 2352 | } |
| 2353 | /* |
| 2354 | * Since the parent was already fixed up, convert it |
| 2355 | * back to kernel address space to access it |
| 2356 | */ |
| 2357 | parent_buffer = parent->buffer - |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 2358 | binder_alloc_get_user_buffer_offset( |
| 2359 | &proc->alloc); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2360 | |
| 2361 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2362 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2363 | pr_err("transaction release %d invalid number of fds (%lld)\n", |
| 2364 | debug_id, (u64)fda->num_fds); |
| 2365 | continue; |
| 2366 | } |
| 2367 | if (fd_buf_size > parent->length || |
| 2368 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2369 | /* No space for all file descriptors here. */ |
| 2370 | pr_err("transaction release %d not enough space for %lld fds in buffer\n", |
| 2371 | debug_id, (u64)fda->num_fds); |
| 2372 | continue; |
| 2373 | } |
| 2374 | fd_array = (u32 *)(parent_buffer + fda->parent_offset); |
| 2375 | for (fd_index = 0; fd_index < fda->num_fds; fd_index++) |
| 2376 | task_close_fd(proc, fd_array[fd_index]); |
| 2377 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2378 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 2379 | pr_err("transaction release %d bad object type %x\n", |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2380 | debug_id, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2381 | break; |
| 2382 | } |
| 2383 | } |
| 2384 | } |
| 2385 | |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2386 | static int binder_translate_binder(struct flat_binder_object *fp, |
| 2387 | struct binder_transaction *t, |
| 2388 | struct binder_thread *thread) |
| 2389 | { |
| 2390 | struct binder_node *node; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2391 | struct binder_proc *proc = thread->proc; |
| 2392 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2393 | struct binder_ref_data rdata; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2394 | int ret = 0; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2395 | |
| 2396 | node = binder_get_node(proc, fp->binder); |
| 2397 | if (!node) { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2398 | node = binder_new_node(proc, fp); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2399 | if (!node) |
| 2400 | return -ENOMEM; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2401 | } |
| 2402 | if (fp->cookie != node->cookie) { |
| 2403 | binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n", |
| 2404 | proc->pid, thread->pid, (u64)fp->binder, |
| 2405 | node->debug_id, (u64)fp->cookie, |
| 2406 | (u64)node->cookie); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2407 | ret = -EINVAL; |
| 2408 | goto done; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2409 | } |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2410 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2411 | ret = -EPERM; |
| 2412 | goto done; |
| 2413 | } |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2414 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2415 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2416 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2417 | &thread->todo, &rdata); |
| 2418 | if (ret) |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2419 | goto done; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2420 | |
| 2421 | if (fp->hdr.type == BINDER_TYPE_BINDER) |
| 2422 | fp->hdr.type = BINDER_TYPE_HANDLE; |
| 2423 | else |
| 2424 | fp->hdr.type = BINDER_TYPE_WEAK_HANDLE; |
| 2425 | fp->binder = 0; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2426 | fp->handle = rdata.desc; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2427 | fp->cookie = 0; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2428 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2429 | trace_binder_transaction_node_to_ref(t, node, &rdata); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2430 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2431 | " node %d u%016llx -> ref %d desc %d\n", |
| 2432 | node->debug_id, (u64)node->ptr, |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2433 | rdata.debug_id, rdata.desc); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2434 | done: |
| 2435 | binder_put_node(node); |
| 2436 | return ret; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | static int binder_translate_handle(struct flat_binder_object *fp, |
| 2440 | struct binder_transaction *t, |
| 2441 | struct binder_thread *thread) |
| 2442 | { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2443 | struct binder_proc *proc = thread->proc; |
| 2444 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2445 | struct binder_node *node; |
| 2446 | struct binder_ref_data src_rdata; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2447 | int ret = 0; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2448 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2449 | node = binder_get_node_from_ref(proc, fp->handle, |
| 2450 | fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata); |
| 2451 | if (!node) { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2452 | binder_user_error("%d:%d got transaction with invalid handle, %d\n", |
| 2453 | proc->pid, thread->pid, fp->handle); |
| 2454 | return -EINVAL; |
| 2455 | } |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2456 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2457 | ret = -EPERM; |
| 2458 | goto done; |
| 2459 | } |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2460 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2461 | binder_node_lock(node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2462 | if (node->proc == target_proc) { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2463 | if (fp->hdr.type == BINDER_TYPE_HANDLE) |
| 2464 | fp->hdr.type = BINDER_TYPE_BINDER; |
| 2465 | else |
| 2466 | fp->hdr.type = BINDER_TYPE_WEAK_BINDER; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2467 | fp->binder = node->ptr; |
| 2468 | fp->cookie = node->cookie; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2469 | if (node->proc) |
| 2470 | binder_inner_proc_lock(node->proc); |
| 2471 | binder_inc_node_nilocked(node, |
| 2472 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2473 | 0, NULL); |
| 2474 | if (node->proc) |
| 2475 | binder_inner_proc_unlock(node->proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2476 | trace_binder_transaction_ref_to_node(t, node, &src_rdata); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2477 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2478 | " ref %d desc %d -> node %d u%016llx\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2479 | src_rdata.debug_id, src_rdata.desc, node->debug_id, |
| 2480 | (u64)node->ptr); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2481 | binder_node_unlock(node); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2482 | } else { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2483 | int ret; |
| 2484 | struct binder_ref_data dest_rdata; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2485 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2486 | binder_node_unlock(node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2487 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2488 | fp->hdr.type == BINDER_TYPE_HANDLE, |
| 2489 | NULL, &dest_rdata); |
| 2490 | if (ret) |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2491 | goto done; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2492 | |
| 2493 | fp->binder = 0; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2494 | fp->handle = dest_rdata.desc; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2495 | fp->cookie = 0; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2496 | trace_binder_transaction_ref_to_ref(t, node, &src_rdata, |
| 2497 | &dest_rdata); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2498 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2499 | " ref %d desc %d -> ref %d desc %d (node %d)\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2500 | src_rdata.debug_id, src_rdata.desc, |
| 2501 | dest_rdata.debug_id, dest_rdata.desc, |
| 2502 | node->debug_id); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2503 | } |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2504 | done: |
| 2505 | binder_put_node(node); |
| 2506 | return ret; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | static int binder_translate_fd(int fd, |
| 2510 | struct binder_transaction *t, |
| 2511 | struct binder_thread *thread, |
| 2512 | struct binder_transaction *in_reply_to) |
| 2513 | { |
| 2514 | struct binder_proc *proc = thread->proc; |
| 2515 | struct binder_proc *target_proc = t->to_proc; |
| 2516 | int target_fd; |
| 2517 | struct file *file; |
| 2518 | int ret; |
| 2519 | bool target_allows_fd; |
| 2520 | |
| 2521 | if (in_reply_to) |
| 2522 | target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS); |
| 2523 | else |
| 2524 | target_allows_fd = t->buffer->target_node->accept_fds; |
| 2525 | if (!target_allows_fd) { |
| 2526 | binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n", |
| 2527 | proc->pid, thread->pid, |
| 2528 | in_reply_to ? "reply" : "transaction", |
| 2529 | fd); |
| 2530 | ret = -EPERM; |
| 2531 | goto err_fd_not_accepted; |
| 2532 | } |
| 2533 | |
| 2534 | file = fget(fd); |
| 2535 | if (!file) { |
| 2536 | binder_user_error("%d:%d got transaction with invalid fd, %d\n", |
| 2537 | proc->pid, thread->pid, fd); |
| 2538 | ret = -EBADF; |
| 2539 | goto err_fget; |
| 2540 | } |
| 2541 | ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file); |
| 2542 | if (ret < 0) { |
| 2543 | ret = -EPERM; |
| 2544 | goto err_security; |
| 2545 | } |
| 2546 | |
| 2547 | target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC); |
| 2548 | if (target_fd < 0) { |
| 2549 | ret = -ENOMEM; |
| 2550 | goto err_get_unused_fd; |
| 2551 | } |
| 2552 | task_fd_install(target_proc, target_fd, file); |
| 2553 | trace_binder_transaction_fd(t, fd, target_fd); |
| 2554 | binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n", |
| 2555 | fd, target_fd); |
| 2556 | |
| 2557 | return target_fd; |
| 2558 | |
| 2559 | err_get_unused_fd: |
| 2560 | err_security: |
| 2561 | fput(file); |
| 2562 | err_fget: |
| 2563 | err_fd_not_accepted: |
| 2564 | return ret; |
| 2565 | } |
| 2566 | |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2567 | static int binder_translate_fd_array(struct binder_fd_array_object *fda, |
| 2568 | struct binder_buffer_object *parent, |
| 2569 | struct binder_transaction *t, |
| 2570 | struct binder_thread *thread, |
| 2571 | struct binder_transaction *in_reply_to) |
| 2572 | { |
| 2573 | binder_size_t fdi, fd_buf_size, num_installed_fds; |
| 2574 | int target_fd; |
| 2575 | uintptr_t parent_buffer; |
| 2576 | u32 *fd_array; |
| 2577 | struct binder_proc *proc = thread->proc; |
| 2578 | struct binder_proc *target_proc = t->to_proc; |
| 2579 | |
| 2580 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2581 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2582 | binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n", |
| 2583 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2584 | return -EINVAL; |
| 2585 | } |
| 2586 | if (fd_buf_size > parent->length || |
| 2587 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2588 | /* No space for all file descriptors here. */ |
| 2589 | binder_user_error("%d:%d not enough space to store %lld fds in buffer\n", |
| 2590 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2591 | return -EINVAL; |
| 2592 | } |
| 2593 | /* |
| 2594 | * Since the parent was already fixed up, convert it |
| 2595 | * back to the kernel address space to access it |
| 2596 | */ |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 2597 | parent_buffer = parent->buffer - |
| 2598 | binder_alloc_get_user_buffer_offset(&target_proc->alloc); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2599 | fd_array = (u32 *)(parent_buffer + fda->parent_offset); |
| 2600 | if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) { |
| 2601 | binder_user_error("%d:%d parent offset not aligned correctly.\n", |
| 2602 | proc->pid, thread->pid); |
| 2603 | return -EINVAL; |
| 2604 | } |
| 2605 | for (fdi = 0; fdi < fda->num_fds; fdi++) { |
| 2606 | target_fd = binder_translate_fd(fd_array[fdi], t, thread, |
| 2607 | in_reply_to); |
| 2608 | if (target_fd < 0) |
| 2609 | goto err_translate_fd_failed; |
| 2610 | fd_array[fdi] = target_fd; |
| 2611 | } |
| 2612 | return 0; |
| 2613 | |
| 2614 | err_translate_fd_failed: |
| 2615 | /* |
| 2616 | * Failed to allocate fd or security error, free fds |
| 2617 | * installed so far. |
| 2618 | */ |
| 2619 | num_installed_fds = fdi; |
| 2620 | for (fdi = 0; fdi < num_installed_fds; fdi++) |
| 2621 | task_close_fd(target_proc, fd_array[fdi]); |
| 2622 | return target_fd; |
| 2623 | } |
| 2624 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2625 | static int binder_fixup_parent(struct binder_transaction *t, |
| 2626 | struct binder_thread *thread, |
| 2627 | struct binder_buffer_object *bp, |
| 2628 | binder_size_t *off_start, |
| 2629 | binder_size_t num_valid, |
| 2630 | struct binder_buffer_object *last_fixup_obj, |
| 2631 | binder_size_t last_fixup_min_off) |
| 2632 | { |
| 2633 | struct binder_buffer_object *parent; |
| 2634 | u8 *parent_buffer; |
| 2635 | struct binder_buffer *b = t->buffer; |
| 2636 | struct binder_proc *proc = thread->proc; |
| 2637 | struct binder_proc *target_proc = t->to_proc; |
| 2638 | |
| 2639 | if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT)) |
| 2640 | return 0; |
| 2641 | |
| 2642 | parent = binder_validate_ptr(b, bp->parent, off_start, num_valid); |
| 2643 | if (!parent) { |
| 2644 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 2645 | proc->pid, thread->pid); |
| 2646 | return -EINVAL; |
| 2647 | } |
| 2648 | |
| 2649 | if (!binder_validate_fixup(b, off_start, |
| 2650 | parent, bp->parent_offset, |
| 2651 | last_fixup_obj, |
| 2652 | last_fixup_min_off)) { |
| 2653 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 2654 | proc->pid, thread->pid); |
| 2655 | return -EINVAL; |
| 2656 | } |
| 2657 | |
| 2658 | if (parent->length < sizeof(binder_uintptr_t) || |
| 2659 | bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) { |
| 2660 | /* No space for a pointer here! */ |
| 2661 | binder_user_error("%d:%d got transaction with invalid parent offset\n", |
| 2662 | proc->pid, thread->pid); |
| 2663 | return -EINVAL; |
| 2664 | } |
| 2665 | parent_buffer = (u8 *)(parent->buffer - |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 2666 | binder_alloc_get_user_buffer_offset( |
| 2667 | &target_proc->alloc)); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2668 | *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer; |
| 2669 | |
| 2670 | return 0; |
| 2671 | } |
| 2672 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2673 | /** |
| 2674 | * binder_proc_transaction() - sends a transaction to a process and wakes it up |
| 2675 | * @t: transaction to send |
| 2676 | * @proc: process to send the transaction to |
| 2677 | * @thread: thread in @proc to send the transaction to (may be NULL) |
| 2678 | * |
| 2679 | * This function queues a transaction to the specified process. It will try |
| 2680 | * to find a thread in the target process to handle the transaction and |
| 2681 | * wake it up. If no thread is found, the work is queued to the proc |
| 2682 | * waitqueue. |
| 2683 | * |
| 2684 | * If the @thread parameter is not NULL, the transaction is always queued |
| 2685 | * to the waitlist of that specific thread. |
| 2686 | * |
| 2687 | * Return: true if the transactions was successfully queued |
| 2688 | * false if the target process or thread is dead |
| 2689 | */ |
| 2690 | static bool binder_proc_transaction(struct binder_transaction *t, |
| 2691 | struct binder_proc *proc, |
| 2692 | struct binder_thread *thread) |
| 2693 | { |
| 2694 | struct list_head *target_list = NULL; |
| 2695 | struct binder_node *node = t->buffer->target_node; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2696 | struct binder_priority node_prio; |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2697 | bool oneway = !!(t->flags & TF_ONE_WAY); |
| 2698 | bool wakeup = true; |
| 2699 | |
| 2700 | BUG_ON(!node); |
| 2701 | binder_node_lock(node); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2702 | node_prio.prio = node->min_priority; |
| 2703 | node_prio.sched_policy = node->sched_policy; |
| 2704 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2705 | if (oneway) { |
| 2706 | BUG_ON(thread); |
| 2707 | if (node->has_async_transaction) { |
| 2708 | target_list = &node->async_todo; |
| 2709 | wakeup = false; |
| 2710 | } else { |
| 2711 | node->has_async_transaction = 1; |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | binder_inner_proc_lock(proc); |
| 2716 | |
| 2717 | if (proc->is_dead || (thread && thread->is_dead)) { |
| 2718 | binder_inner_proc_unlock(proc); |
| 2719 | binder_node_unlock(node); |
| 2720 | return false; |
| 2721 | } |
| 2722 | |
| 2723 | if (!thread && !target_list) |
| 2724 | thread = binder_select_thread_ilocked(proc); |
| 2725 | |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2726 | if (thread) { |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2727 | target_list = &thread->todo; |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 2728 | binder_transaction_priority(thread->task, t, node_prio, |
| 2729 | node->inherit_rt); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2730 | } else if (!target_list) { |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2731 | target_list = &proc->todo; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2732 | } else { |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2733 | BUG_ON(target_list != &node->async_todo); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2734 | } |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2735 | |
| 2736 | binder_enqueue_work_ilocked(&t->work, target_list); |
| 2737 | |
| 2738 | if (wakeup) |
| 2739 | binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */); |
| 2740 | |
| 2741 | binder_inner_proc_unlock(proc); |
| 2742 | binder_node_unlock(node); |
| 2743 | |
| 2744 | return true; |
| 2745 | } |
| 2746 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2747 | static void binder_transaction(struct binder_proc *proc, |
| 2748 | struct binder_thread *thread, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 2749 | struct binder_transaction_data *tr, int reply, |
| 2750 | binder_size_t extra_buffers_size) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2751 | { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2752 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2753 | struct binder_transaction *t; |
| 2754 | struct binder_work *tcomplete; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2755 | binder_size_t *offp, *off_end, *off_start; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 2756 | binder_size_t off_min; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2757 | u8 *sg_bufp, *sg_buf_end; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2758 | struct binder_proc *target_proc = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2759 | struct binder_thread *target_thread = NULL; |
| 2760 | struct binder_node *target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2761 | struct binder_transaction *in_reply_to = NULL; |
| 2762 | struct binder_transaction_log_entry *e; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2763 | uint32_t return_error = 0; |
| 2764 | uint32_t return_error_param = 0; |
| 2765 | uint32_t return_error_line = 0; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2766 | struct binder_buffer_object *last_fixup_obj = NULL; |
| 2767 | binder_size_t last_fixup_min_off = 0; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 2768 | struct binder_context *context = proc->context; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 2769 | int t_debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2770 | |
| 2771 | e = binder_transaction_log_add(&binder_transaction_log); |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 2772 | e->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2773 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); |
| 2774 | e->from_proc = proc->pid; |
| 2775 | e->from_thread = thread->pid; |
| 2776 | e->target_handle = tr->target.handle; |
| 2777 | e->data_size = tr->data_size; |
| 2778 | e->offsets_size = tr->offsets_size; |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 2779 | e->context_name = proc->context->name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2780 | |
| 2781 | if (reply) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2782 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2783 | in_reply_to = thread->transaction_stack; |
| 2784 | if (in_reply_to == NULL) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2785 | binder_inner_proc_unlock(proc); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2786 | 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] | 2787 | proc->pid, thread->pid); |
| 2788 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2789 | return_error_param = -EPROTO; |
| 2790 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2791 | goto err_empty_call_stack; |
| 2792 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2793 | if (in_reply_to->to_thread != thread) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2794 | spin_lock(&in_reply_to->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2795 | 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] | 2796 | proc->pid, thread->pid, in_reply_to->debug_id, |
| 2797 | in_reply_to->to_proc ? |
| 2798 | in_reply_to->to_proc->pid : 0, |
| 2799 | in_reply_to->to_thread ? |
| 2800 | in_reply_to->to_thread->pid : 0); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2801 | spin_unlock(&in_reply_to->lock); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2802 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2803 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2804 | return_error_param = -EPROTO; |
| 2805 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2806 | in_reply_to = NULL; |
| 2807 | goto err_bad_call_stack; |
| 2808 | } |
| 2809 | thread->transaction_stack = in_reply_to->to_parent; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2810 | binder_inner_proc_unlock(proc); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2811 | target_thread = binder_get_txn_from_and_acq_inner(in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2812 | if (target_thread == NULL) { |
| 2813 | return_error = BR_DEAD_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2814 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2815 | goto err_dead_binder; |
| 2816 | } |
| 2817 | if (target_thread->transaction_stack != in_reply_to) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2818 | 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] | 2819 | proc->pid, thread->pid, |
| 2820 | target_thread->transaction_stack ? |
| 2821 | target_thread->transaction_stack->debug_id : 0, |
| 2822 | in_reply_to->debug_id); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2823 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2824 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2825 | return_error_param = -EPROTO; |
| 2826 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2827 | in_reply_to = NULL; |
| 2828 | target_thread = NULL; |
| 2829 | goto err_dead_binder; |
| 2830 | } |
| 2831 | target_proc = target_thread->proc; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2832 | target_proc->tmp_ref++; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2833 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2834 | } else { |
| 2835 | if (tr->target.handle) { |
| 2836 | struct binder_ref *ref; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2837 | |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 2838 | /* |
| 2839 | * There must already be a strong ref |
| 2840 | * on this node. If so, do a strong |
| 2841 | * increment on the node to ensure it |
| 2842 | * stays alive until the transaction is |
| 2843 | * done. |
| 2844 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2845 | binder_proc_lock(proc); |
| 2846 | ref = binder_get_ref_olocked(proc, tr->target.handle, |
| 2847 | true); |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 2848 | if (ref) { |
| 2849 | binder_inc_node(ref->node, 1, 0, NULL); |
| 2850 | target_node = ref->node; |
| 2851 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2852 | binder_proc_unlock(proc); |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 2853 | if (target_node == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2854 | binder_user_error("%d:%d got transaction to invalid handle\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2855 | proc->pid, thread->pid); |
| 2856 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2857 | return_error_param = -EINVAL; |
| 2858 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2859 | goto err_invalid_target_handle; |
| 2860 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2861 | } else { |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 2862 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 2863 | target_node = context->binder_context_mgr_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2864 | if (target_node == NULL) { |
| 2865 | return_error = BR_DEAD_REPLY; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 2866 | mutex_unlock(&context->context_mgr_node_lock); |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2867 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2868 | goto err_no_context_mgr_node; |
| 2869 | } |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 2870 | binder_inc_node(target_node, 1, 0, NULL); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 2871 | mutex_unlock(&context->context_mgr_node_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2872 | } |
| 2873 | e->to_node = target_node->debug_id; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2874 | binder_node_lock(target_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2875 | target_proc = target_node->proc; |
| 2876 | if (target_proc == NULL) { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2877 | binder_node_unlock(target_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2878 | return_error = BR_DEAD_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2879 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2880 | goto err_dead_binder; |
| 2881 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2882 | binder_inner_proc_lock(target_proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2883 | target_proc->tmp_ref++; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2884 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2885 | binder_node_unlock(target_node); |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2886 | if (security_binder_transaction(proc->tsk, |
| 2887 | target_proc->tsk) < 0) { |
| 2888 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2889 | return_error_param = -EPERM; |
| 2890 | return_error_line = __LINE__; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2891 | goto err_invalid_target_handle; |
| 2892 | } |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2893 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2894 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { |
| 2895 | struct binder_transaction *tmp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2896 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2897 | tmp = thread->transaction_stack; |
| 2898 | if (tmp->to_thread != thread) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2899 | spin_lock(&tmp->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2900 | 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] | 2901 | proc->pid, thread->pid, tmp->debug_id, |
| 2902 | tmp->to_proc ? tmp->to_proc->pid : 0, |
| 2903 | tmp->to_thread ? |
| 2904 | tmp->to_thread->pid : 0); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2905 | spin_unlock(&tmp->lock); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2906 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2907 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2908 | return_error_param = -EPROTO; |
| 2909 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2910 | goto err_bad_call_stack; |
| 2911 | } |
| 2912 | while (tmp) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2913 | struct binder_thread *from; |
| 2914 | |
| 2915 | spin_lock(&tmp->lock); |
| 2916 | from = tmp->from; |
| 2917 | if (from && from->proc == target_proc) { |
| 2918 | atomic_inc(&from->tmp_ref); |
| 2919 | target_thread = from; |
| 2920 | spin_unlock(&tmp->lock); |
| 2921 | break; |
| 2922 | } |
| 2923 | spin_unlock(&tmp->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2924 | tmp = tmp->from_parent; |
| 2925 | } |
| 2926 | } |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2927 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2928 | } |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2929 | if (target_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2930 | e->to_thread = target_thread->pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2931 | e->to_proc = target_proc->pid; |
| 2932 | |
| 2933 | /* TODO: reuse incoming transaction for reply */ |
| 2934 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 2935 | if (t == NULL) { |
| 2936 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2937 | return_error_param = -ENOMEM; |
| 2938 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2939 | goto err_alloc_t_failed; |
| 2940 | } |
| 2941 | binder_stats_created(BINDER_STAT_TRANSACTION); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2942 | spin_lock_init(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2943 | |
| 2944 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); |
| 2945 | if (tcomplete == NULL) { |
| 2946 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2947 | return_error_param = -ENOMEM; |
| 2948 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2949 | goto err_alloc_tcomplete_failed; |
| 2950 | } |
| 2951 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); |
| 2952 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 2953 | t->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2954 | |
| 2955 | if (reply) |
| 2956 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 2957 | "%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] | 2958 | proc->pid, thread->pid, t->debug_id, |
| 2959 | target_proc->pid, target_thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2960 | (u64)tr->data.ptr.buffer, |
| 2961 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 2962 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 2963 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2964 | else |
| 2965 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 2966 | "%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] | 2967 | proc->pid, thread->pid, t->debug_id, |
| 2968 | target_proc->pid, target_node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2969 | (u64)tr->data.ptr.buffer, |
| 2970 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 2971 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 2972 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2973 | |
| 2974 | if (!reply && !(tr->flags & TF_ONE_WAY)) |
| 2975 | t->from = thread; |
| 2976 | else |
| 2977 | t->from = NULL; |
Tair Rzayev | 57bab7c | 2014-05-31 22:43:34 +0300 | [diff] [blame] | 2978 | t->sender_euid = task_euid(proc->tsk); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2979 | t->to_proc = target_proc; |
| 2980 | t->to_thread = target_thread; |
| 2981 | t->code = tr->code; |
| 2982 | t->flags = tr->flags; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 2983 | if (!(t->flags & TF_ONE_WAY) && |
| 2984 | binder_supported_policy(current->policy)) { |
| 2985 | /* Inherit supported policies for synchronous transactions */ |
| 2986 | t->priority.sched_policy = current->policy; |
| 2987 | t->priority.prio = current->normal_prio; |
| 2988 | } else { |
| 2989 | /* Otherwise, fall back to the default priority */ |
| 2990 | t->priority = target_proc->default_priority; |
| 2991 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2992 | |
| 2993 | trace_binder_transaction(reply, t, target_node); |
| 2994 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 2995 | t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 2996 | tr->offsets_size, extra_buffers_size, |
| 2997 | !reply && (t->flags & TF_ONE_WAY)); |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2998 | if (IS_ERR(t->buffer)) { |
| 2999 | /* |
| 3000 | * -ESRCH indicates VMA cleared. The target is dying. |
| 3001 | */ |
| 3002 | return_error_param = PTR_ERR(t->buffer); |
| 3003 | return_error = return_error_param == -ESRCH ? |
| 3004 | BR_DEAD_REPLY : BR_FAILED_REPLY; |
| 3005 | return_error_line = __LINE__; |
| 3006 | t->buffer = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3007 | goto err_binder_alloc_buf_failed; |
| 3008 | } |
| 3009 | t->buffer->allow_user_free = 0; |
| 3010 | t->buffer->debug_id = t->debug_id; |
| 3011 | t->buffer->transaction = t; |
| 3012 | t->buffer->target_node = target_node; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3013 | trace_binder_transaction_alloc_buf(t->buffer); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3014 | off_start = (binder_size_t *)(t->buffer->data + |
| 3015 | ALIGN(tr->data_size, sizeof(void *))); |
| 3016 | offp = off_start; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3017 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3018 | if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t) |
| 3019 | tr->data.ptr.buffer, tr->data_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3020 | binder_user_error("%d:%d got transaction with invalid data ptr\n", |
| 3021 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3022 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3023 | return_error_param = -EFAULT; |
| 3024 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3025 | goto err_copy_data_failed; |
| 3026 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3027 | if (copy_from_user(offp, (const void __user *)(uintptr_t) |
| 3028 | tr->data.ptr.offsets, tr->offsets_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3029 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3030 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3031 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3032 | return_error_param = -EFAULT; |
| 3033 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3034 | goto err_copy_data_failed; |
| 3035 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3036 | if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) { |
| 3037 | binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n", |
| 3038 | proc->pid, thread->pid, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3039 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3040 | return_error_param = -EINVAL; |
| 3041 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3042 | goto err_bad_offset; |
| 3043 | } |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3044 | if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) { |
| 3045 | binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n", |
| 3046 | proc->pid, thread->pid, |
Amit Pundir | 44cbb18 | 2017-02-01 12:53:45 +0530 | [diff] [blame] | 3047 | (u64)extra_buffers_size); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3048 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3049 | return_error_param = -EINVAL; |
| 3050 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3051 | goto err_bad_offset; |
| 3052 | } |
| 3053 | off_end = (void *)off_start + tr->offsets_size; |
| 3054 | sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *))); |
| 3055 | sg_buf_end = sg_bufp + extra_buffers_size; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3056 | off_min = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3057 | for (; offp < off_end; offp++) { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3058 | struct binder_object_header *hdr; |
| 3059 | size_t object_size = binder_validate_object(t->buffer, *offp); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3060 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3061 | if (object_size == 0 || *offp < off_min) { |
| 3062 | 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] | 3063 | proc->pid, thread->pid, (u64)*offp, |
| 3064 | (u64)off_min, |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3065 | (u64)t->buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3066 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3067 | return_error_param = -EINVAL; |
| 3068 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3069 | goto err_bad_offset; |
| 3070 | } |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3071 | |
| 3072 | hdr = (struct binder_object_header *)(t->buffer->data + *offp); |
| 3073 | off_min = *offp + object_size; |
| 3074 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3075 | case BINDER_TYPE_BINDER: |
| 3076 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3077 | struct flat_binder_object *fp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3078 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3079 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3080 | ret = binder_translate_binder(fp, t, thread); |
| 3081 | if (ret < 0) { |
Christian Engelmayer | 7d42043 | 2014-05-07 21:44:53 +0200 | [diff] [blame] | 3082 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3083 | return_error_param = ret; |
| 3084 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3085 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3086 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3087 | } break; |
| 3088 | case BINDER_TYPE_HANDLE: |
| 3089 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3090 | struct flat_binder_object *fp; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 3091 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3092 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3093 | ret = binder_translate_handle(fp, t, thread); |
| 3094 | if (ret < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3095 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3096 | return_error_param = ret; |
| 3097 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3098 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3099 | } |
| 3100 | } break; |
| 3101 | |
| 3102 | case BINDER_TYPE_FD: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3103 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3104 | int target_fd = binder_translate_fd(fp->fd, t, thread, |
| 3105 | in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3106 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3107 | if (target_fd < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3108 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3109 | return_error_param = target_fd; |
| 3110 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3111 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3112 | } |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3113 | fp->pad_binder = 0; |
| 3114 | fp->fd = target_fd; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3115 | } break; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3116 | case BINDER_TYPE_FDA: { |
| 3117 | struct binder_fd_array_object *fda = |
| 3118 | to_binder_fd_array_object(hdr); |
| 3119 | struct binder_buffer_object *parent = |
| 3120 | binder_validate_ptr(t->buffer, fda->parent, |
| 3121 | off_start, |
| 3122 | offp - off_start); |
| 3123 | if (!parent) { |
| 3124 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 3125 | proc->pid, thread->pid); |
| 3126 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3127 | return_error_param = -EINVAL; |
| 3128 | return_error_line = __LINE__; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3129 | goto err_bad_parent; |
| 3130 | } |
| 3131 | if (!binder_validate_fixup(t->buffer, off_start, |
| 3132 | parent, fda->parent_offset, |
| 3133 | last_fixup_obj, |
| 3134 | last_fixup_min_off)) { |
| 3135 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 3136 | proc->pid, thread->pid); |
| 3137 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3138 | return_error_param = -EINVAL; |
| 3139 | return_error_line = __LINE__; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3140 | goto err_bad_parent; |
| 3141 | } |
| 3142 | ret = binder_translate_fd_array(fda, parent, t, thread, |
| 3143 | in_reply_to); |
| 3144 | if (ret < 0) { |
| 3145 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3146 | return_error_param = ret; |
| 3147 | return_error_line = __LINE__; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3148 | goto err_translate_failed; |
| 3149 | } |
| 3150 | last_fixup_obj = parent; |
| 3151 | last_fixup_min_off = |
| 3152 | fda->parent_offset + sizeof(u32) * fda->num_fds; |
| 3153 | } break; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3154 | case BINDER_TYPE_PTR: { |
| 3155 | struct binder_buffer_object *bp = |
| 3156 | to_binder_buffer_object(hdr); |
| 3157 | size_t buf_left = sg_buf_end - sg_bufp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3158 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3159 | if (bp->length > buf_left) { |
| 3160 | binder_user_error("%d:%d got transaction with too large buffer\n", |
| 3161 | proc->pid, thread->pid); |
| 3162 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3163 | return_error_param = -EINVAL; |
| 3164 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3165 | goto err_bad_offset; |
| 3166 | } |
| 3167 | if (copy_from_user(sg_bufp, |
| 3168 | (const void __user *)(uintptr_t) |
| 3169 | bp->buffer, bp->length)) { |
| 3170 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3171 | proc->pid, thread->pid); |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3172 | return_error_param = -EFAULT; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3173 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3174 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3175 | goto err_copy_data_failed; |
| 3176 | } |
| 3177 | /* Fixup buffer pointer to target proc address space */ |
| 3178 | bp->buffer = (uintptr_t)sg_bufp + |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3179 | binder_alloc_get_user_buffer_offset( |
| 3180 | &target_proc->alloc); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3181 | sg_bufp += ALIGN(bp->length, sizeof(u64)); |
| 3182 | |
| 3183 | ret = binder_fixup_parent(t, thread, bp, off_start, |
| 3184 | offp - off_start, |
| 3185 | last_fixup_obj, |
| 3186 | last_fixup_min_off); |
| 3187 | if (ret < 0) { |
| 3188 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3189 | return_error_param = ret; |
| 3190 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3191 | goto err_translate_failed; |
| 3192 | } |
| 3193 | last_fixup_obj = bp; |
| 3194 | last_fixup_min_off = 0; |
| 3195 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3196 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 3197 | 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] | 3198 | proc->pid, thread->pid, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3199 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3200 | return_error_param = -EINVAL; |
| 3201 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3202 | goto err_bad_object_type; |
| 3203 | } |
| 3204 | } |
Todd Kjos | 8dedb0c | 2017-05-09 08:31:32 -0700 | [diff] [blame] | 3205 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3206 | binder_enqueue_work(proc, tcomplete, &thread->todo); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3207 | t->work.type = BINDER_WORK_TRANSACTION; |
Todd Kjos | 8dedb0c | 2017-05-09 08:31:32 -0700 | [diff] [blame] | 3208 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3209 | if (reply) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3210 | binder_inner_proc_lock(target_proc); |
| 3211 | if (target_thread->is_dead) { |
| 3212 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3213 | goto err_dead_proc_or_thread; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3214 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3215 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3216 | binder_pop_transaction_ilocked(target_thread, in_reply_to); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3217 | binder_enqueue_work_ilocked(&t->work, &target_thread->todo); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3218 | binder_inner_proc_unlock(target_proc); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3219 | wake_up_interruptible_sync(&target_thread->wait); |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3220 | binder_restore_priority(current, in_reply_to->saved_priority); |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 3221 | binder_free_transaction(in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3222 | } else if (!(t->flags & TF_ONE_WAY)) { |
| 3223 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3224 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3225 | t->need_reply = 1; |
| 3226 | t->from_parent = thread->transaction_stack; |
| 3227 | thread->transaction_stack = t; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3228 | binder_inner_proc_unlock(proc); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3229 | if (!binder_proc_transaction(t, target_proc, target_thread)) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3230 | binder_inner_proc_lock(proc); |
| 3231 | binder_pop_transaction_ilocked(thread, t); |
| 3232 | binder_inner_proc_unlock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3233 | goto err_dead_proc_or_thread; |
| 3234 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3235 | } else { |
| 3236 | BUG_ON(target_node == NULL); |
| 3237 | BUG_ON(t->buffer->async_transaction != 1); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3238 | if (!binder_proc_transaction(t, target_proc, NULL)) |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3239 | goto err_dead_proc_or_thread; |
Riley Andrews | b596881 | 2015-09-01 12:42:07 -0700 | [diff] [blame] | 3240 | } |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3241 | if (target_thread) |
| 3242 | binder_thread_dec_tmpref(target_thread); |
| 3243 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3244 | /* |
| 3245 | * write barrier to synchronize with initialization |
| 3246 | * of log entry |
| 3247 | */ |
| 3248 | smp_wmb(); |
| 3249 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3250 | return; |
| 3251 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3252 | err_dead_proc_or_thread: |
| 3253 | return_error = BR_DEAD_REPLY; |
| 3254 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3255 | err_translate_failed: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3256 | err_bad_object_type: |
| 3257 | err_bad_offset: |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3258 | err_bad_parent: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3259 | err_copy_data_failed: |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3260 | trace_binder_transaction_failed_buffer_release(t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3261 | binder_transaction_buffer_release(target_proc, t->buffer, offp); |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3262 | target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3263 | t->buffer->transaction = NULL; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3264 | binder_alloc_free_buf(&target_proc->alloc, t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3265 | err_binder_alloc_buf_failed: |
| 3266 | kfree(tcomplete); |
| 3267 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3268 | err_alloc_tcomplete_failed: |
| 3269 | kfree(t); |
| 3270 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 3271 | err_alloc_t_failed: |
| 3272 | err_bad_call_stack: |
| 3273 | err_empty_call_stack: |
| 3274 | err_dead_binder: |
| 3275 | err_invalid_target_handle: |
| 3276 | err_no_context_mgr_node: |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3277 | if (target_thread) |
| 3278 | binder_thread_dec_tmpref(target_thread); |
| 3279 | if (target_proc) |
| 3280 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3281 | if (target_node) |
| 3282 | binder_dec_node(target_node, 1, 0); |
| 3283 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3284 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3285 | "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n", |
| 3286 | proc->pid, thread->pid, return_error, return_error_param, |
| 3287 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3288 | return_error_line); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3289 | |
| 3290 | { |
| 3291 | struct binder_transaction_log_entry *fe; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3292 | |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3293 | e->return_error = return_error; |
| 3294 | e->return_error_param = return_error_param; |
| 3295 | e->return_error_line = return_error_line; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3296 | fe = binder_transaction_log_add(&binder_transaction_log_failed); |
| 3297 | *fe = *e; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3298 | /* |
| 3299 | * write barrier to synchronize with initialization |
| 3300 | * of log entry |
| 3301 | */ |
| 3302 | smp_wmb(); |
| 3303 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
| 3304 | WRITE_ONCE(fe->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3305 | } |
| 3306 | |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3307 | BUG_ON(thread->return_error.cmd != BR_OK); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3308 | if (in_reply_to) { |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3309 | binder_restore_priority(current, in_reply_to->saved_priority); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3310 | thread->return_error.cmd = BR_TRANSACTION_COMPLETE; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3311 | binder_enqueue_work(thread->proc, |
| 3312 | &thread->return_error.work, |
| 3313 | &thread->todo); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3314 | binder_send_failed_reply(in_reply_to, return_error); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3315 | } else { |
| 3316 | thread->return_error.cmd = return_error; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3317 | binder_enqueue_work(thread->proc, |
| 3318 | &thread->return_error.work, |
| 3319 | &thread->todo); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3320 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3321 | } |
| 3322 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3323 | static int binder_thread_write(struct binder_proc *proc, |
| 3324 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3325 | binder_uintptr_t binder_buffer, size_t size, |
| 3326 | binder_size_t *consumed) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3327 | { |
| 3328 | uint32_t cmd; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 3329 | struct binder_context *context = proc->context; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3330 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3331 | void __user *ptr = buffer + *consumed; |
| 3332 | void __user *end = buffer + size; |
| 3333 | |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3334 | while (ptr < end && thread->return_error.cmd == BR_OK) { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3335 | int ret; |
| 3336 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3337 | if (get_user(cmd, (uint32_t __user *)ptr)) |
| 3338 | return -EFAULT; |
| 3339 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3340 | trace_binder_command(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3341 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 3342 | atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]); |
| 3343 | atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]); |
| 3344 | atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3345 | } |
| 3346 | switch (cmd) { |
| 3347 | case BC_INCREFS: |
| 3348 | case BC_ACQUIRE: |
| 3349 | case BC_RELEASE: |
| 3350 | case BC_DECREFS: { |
| 3351 | uint32_t target; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3352 | const char *debug_string; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3353 | bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE; |
| 3354 | bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE; |
| 3355 | struct binder_ref_data rdata; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3356 | |
| 3357 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3358 | return -EFAULT; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3359 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3360 | ptr += sizeof(uint32_t); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3361 | ret = -1; |
| 3362 | if (increment && !target) { |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3363 | struct binder_node *ctx_mgr_node; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3364 | mutex_lock(&context->context_mgr_node_lock); |
| 3365 | ctx_mgr_node = context->binder_context_mgr_node; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3366 | if (ctx_mgr_node) |
| 3367 | ret = binder_inc_ref_for_node( |
| 3368 | proc, ctx_mgr_node, |
| 3369 | strong, NULL, &rdata); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3370 | mutex_unlock(&context->context_mgr_node_lock); |
| 3371 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3372 | if (ret) |
| 3373 | ret = binder_update_ref_for_handle( |
| 3374 | proc, target, increment, strong, |
| 3375 | &rdata); |
| 3376 | if (!ret && rdata.desc != target) { |
| 3377 | binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n", |
| 3378 | proc->pid, thread->pid, |
| 3379 | target, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3380 | } |
| 3381 | switch (cmd) { |
| 3382 | case BC_INCREFS: |
| 3383 | debug_string = "IncRefs"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3384 | break; |
| 3385 | case BC_ACQUIRE: |
| 3386 | debug_string = "Acquire"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3387 | break; |
| 3388 | case BC_RELEASE: |
| 3389 | debug_string = "Release"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3390 | break; |
| 3391 | case BC_DECREFS: |
| 3392 | default: |
| 3393 | debug_string = "DecRefs"; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3394 | break; |
| 3395 | } |
| 3396 | if (ret) { |
| 3397 | binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n", |
| 3398 | proc->pid, thread->pid, debug_string, |
| 3399 | strong, target, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3400 | break; |
| 3401 | } |
| 3402 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3403 | "%d:%d %s ref %d desc %d s %d w %d\n", |
| 3404 | proc->pid, thread->pid, debug_string, |
| 3405 | rdata.debug_id, rdata.desc, rdata.strong, |
| 3406 | rdata.weak); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3407 | break; |
| 3408 | } |
| 3409 | case BC_INCREFS_DONE: |
| 3410 | case BC_ACQUIRE_DONE: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3411 | binder_uintptr_t node_ptr; |
| 3412 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3413 | struct binder_node *node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3414 | bool free_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3415 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3416 | if (get_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3417 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3418 | ptr += sizeof(binder_uintptr_t); |
| 3419 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3420 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3421 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3422 | node = binder_get_node(proc, node_ptr); |
| 3423 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3424 | binder_user_error("%d:%d %s u%016llx no match\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3425 | proc->pid, thread->pid, |
| 3426 | cmd == BC_INCREFS_DONE ? |
| 3427 | "BC_INCREFS_DONE" : |
| 3428 | "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3429 | (u64)node_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3430 | break; |
| 3431 | } |
| 3432 | if (cookie != node->cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3433 | 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] | 3434 | proc->pid, thread->pid, |
| 3435 | cmd == BC_INCREFS_DONE ? |
| 3436 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3437 | (u64)node_ptr, node->debug_id, |
| 3438 | (u64)cookie, (u64)node->cookie); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3439 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3440 | break; |
| 3441 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3442 | binder_node_inner_lock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3443 | if (cmd == BC_ACQUIRE_DONE) { |
| 3444 | if (node->pending_strong_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3445 | 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] | 3446 | proc->pid, thread->pid, |
| 3447 | node->debug_id); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3448 | binder_node_inner_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3449 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3450 | break; |
| 3451 | } |
| 3452 | node->pending_strong_ref = 0; |
| 3453 | } else { |
| 3454 | if (node->pending_weak_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3455 | 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] | 3456 | proc->pid, thread->pid, |
| 3457 | node->debug_id); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3458 | binder_node_inner_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3459 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3460 | break; |
| 3461 | } |
| 3462 | node->pending_weak_ref = 0; |
| 3463 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3464 | free_node = binder_dec_node_nilocked(node, |
| 3465 | cmd == BC_ACQUIRE_DONE, 0); |
| 3466 | WARN_ON(free_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3467 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3468 | "%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] | 3469 | proc->pid, thread->pid, |
| 3470 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3471 | node->debug_id, node->local_strong_refs, |
| 3472 | node->local_weak_refs, node->tmp_refs); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3473 | binder_node_inner_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3474 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3475 | break; |
| 3476 | } |
| 3477 | case BC_ATTEMPT_ACQUIRE: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3478 | pr_err("BC_ATTEMPT_ACQUIRE not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3479 | return -EINVAL; |
| 3480 | case BC_ACQUIRE_RESULT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3481 | pr_err("BC_ACQUIRE_RESULT not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3482 | return -EINVAL; |
| 3483 | |
| 3484 | case BC_FREE_BUFFER: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3485 | binder_uintptr_t data_ptr; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3486 | struct binder_buffer *buffer; |
| 3487 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3488 | if (get_user(data_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3489 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3490 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3491 | |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 3492 | buffer = binder_alloc_prepare_to_free(&proc->alloc, |
| 3493 | data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3494 | if (buffer == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3495 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n", |
| 3496 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3497 | break; |
| 3498 | } |
| 3499 | if (!buffer->allow_user_free) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3500 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n", |
| 3501 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3502 | break; |
| 3503 | } |
| 3504 | binder_debug(BINDER_DEBUG_FREE_BUFFER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3505 | "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n", |
| 3506 | proc->pid, thread->pid, (u64)data_ptr, |
| 3507 | buffer->debug_id, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3508 | buffer->transaction ? "active" : "finished"); |
| 3509 | |
| 3510 | if (buffer->transaction) { |
| 3511 | buffer->transaction->buffer = NULL; |
| 3512 | buffer->transaction = NULL; |
| 3513 | } |
| 3514 | if (buffer->async_transaction && buffer->target_node) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3515 | struct binder_node *buf_node; |
| 3516 | struct binder_work *w; |
| 3517 | |
| 3518 | buf_node = buffer->target_node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3519 | binder_node_inner_lock(buf_node); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3520 | BUG_ON(!buf_node->has_async_transaction); |
| 3521 | BUG_ON(buf_node->proc != proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3522 | w = binder_dequeue_work_head_ilocked( |
| 3523 | &buf_node->async_todo); |
| 3524 | if (!w) |
| 3525 | buf_node->has_async_transaction = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3526 | else |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3527 | binder_enqueue_work_ilocked( |
| 3528 | w, &thread->todo); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3529 | binder_node_inner_unlock(buf_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3530 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3531 | trace_binder_transaction_buffer_release(buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3532 | binder_transaction_buffer_release(proc, buffer, NULL); |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3533 | binder_alloc_free_buf(&proc->alloc, buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3534 | break; |
| 3535 | } |
| 3536 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3537 | case BC_TRANSACTION_SG: |
| 3538 | case BC_REPLY_SG: { |
| 3539 | struct binder_transaction_data_sg tr; |
| 3540 | |
| 3541 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3542 | return -EFAULT; |
| 3543 | ptr += sizeof(tr); |
| 3544 | binder_transaction(proc, thread, &tr.transaction_data, |
| 3545 | cmd == BC_REPLY_SG, tr.buffers_size); |
| 3546 | break; |
| 3547 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3548 | case BC_TRANSACTION: |
| 3549 | case BC_REPLY: { |
| 3550 | struct binder_transaction_data tr; |
| 3551 | |
| 3552 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3553 | return -EFAULT; |
| 3554 | ptr += sizeof(tr); |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3555 | binder_transaction(proc, thread, &tr, |
| 3556 | cmd == BC_REPLY, 0); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3557 | break; |
| 3558 | } |
| 3559 | |
| 3560 | case BC_REGISTER_LOOPER: |
| 3561 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3562 | "%d:%d BC_REGISTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3563 | proc->pid, thread->pid); |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 3564 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3565 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { |
| 3566 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3567 | 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] | 3568 | proc->pid, thread->pid); |
| 3569 | } else if (proc->requested_threads == 0) { |
| 3570 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3571 | 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] | 3572 | proc->pid, thread->pid); |
| 3573 | } else { |
| 3574 | proc->requested_threads--; |
| 3575 | proc->requested_threads_started++; |
| 3576 | } |
| 3577 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 3578 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3579 | break; |
| 3580 | case BC_ENTER_LOOPER: |
| 3581 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3582 | "%d:%d BC_ENTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3583 | proc->pid, thread->pid); |
| 3584 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { |
| 3585 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3586 | 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] | 3587 | proc->pid, thread->pid); |
| 3588 | } |
| 3589 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; |
| 3590 | break; |
| 3591 | case BC_EXIT_LOOPER: |
| 3592 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3593 | "%d:%d BC_EXIT_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3594 | proc->pid, thread->pid); |
| 3595 | thread->looper |= BINDER_LOOPER_STATE_EXITED; |
| 3596 | break; |
| 3597 | |
| 3598 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 3599 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 3600 | uint32_t target; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3601 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3602 | struct binder_ref *ref; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3603 | struct binder_ref_death *death = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3604 | |
| 3605 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3606 | return -EFAULT; |
| 3607 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3608 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3609 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3610 | ptr += sizeof(binder_uintptr_t); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3611 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3612 | /* |
| 3613 | * Allocate memory for death notification |
| 3614 | * before taking lock |
| 3615 | */ |
| 3616 | death = kzalloc(sizeof(*death), GFP_KERNEL); |
| 3617 | if (death == NULL) { |
| 3618 | WARN_ON(thread->return_error.cmd != |
| 3619 | BR_OK); |
| 3620 | thread->return_error.cmd = BR_ERROR; |
| 3621 | binder_enqueue_work( |
| 3622 | thread->proc, |
| 3623 | &thread->return_error.work, |
| 3624 | &thread->todo); |
| 3625 | binder_debug( |
| 3626 | BINDER_DEBUG_FAILED_TRANSACTION, |
| 3627 | "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n", |
| 3628 | proc->pid, thread->pid); |
| 3629 | break; |
| 3630 | } |
| 3631 | } |
| 3632 | binder_proc_lock(proc); |
| 3633 | ref = binder_get_ref_olocked(proc, target, false); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3634 | if (ref == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3635 | binder_user_error("%d:%d %s invalid ref %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3636 | proc->pid, thread->pid, |
| 3637 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3638 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3639 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 3640 | target); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3641 | binder_proc_unlock(proc); |
| 3642 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3643 | break; |
| 3644 | } |
| 3645 | |
| 3646 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3647 | "%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] | 3648 | proc->pid, thread->pid, |
| 3649 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3650 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3651 | "BC_CLEAR_DEATH_NOTIFICATION", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3652 | (u64)cookie, ref->data.debug_id, |
| 3653 | ref->data.desc, ref->data.strong, |
| 3654 | ref->data.weak, ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3655 | |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3656 | binder_node_lock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3657 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3658 | if (ref->death) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3659 | 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] | 3660 | proc->pid, thread->pid); |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3661 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3662 | binder_proc_unlock(proc); |
| 3663 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3664 | break; |
| 3665 | } |
| 3666 | binder_stats_created(BINDER_STAT_DEATH); |
| 3667 | INIT_LIST_HEAD(&death->work.entry); |
| 3668 | death->cookie = cookie; |
| 3669 | ref->death = death; |
| 3670 | if (ref->node->proc == NULL) { |
| 3671 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3672 | if (thread->looper & |
| 3673 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3674 | BINDER_LOOPER_STATE_ENTERED)) |
| 3675 | binder_enqueue_work( |
| 3676 | proc, |
| 3677 | &ref->death->work, |
| 3678 | &thread->todo); |
| 3679 | else { |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3680 | binder_inner_proc_lock(proc); |
| 3681 | binder_enqueue_work_ilocked( |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3682 | &ref->death->work, |
| 3683 | &proc->todo); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3684 | binder_wakeup_proc_ilocked( |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3685 | proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3686 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3687 | } |
| 3688 | } |
| 3689 | } else { |
| 3690 | if (ref->death == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3691 | 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] | 3692 | proc->pid, thread->pid); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3693 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3694 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3695 | break; |
| 3696 | } |
| 3697 | death = ref->death; |
| 3698 | if (death->cookie != cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3699 | 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] | 3700 | proc->pid, thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3701 | (u64)death->cookie, |
| 3702 | (u64)cookie); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3703 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3704 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3705 | break; |
| 3706 | } |
| 3707 | ref->death = NULL; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3708 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3709 | if (list_empty(&death->work.entry)) { |
| 3710 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3711 | if (thread->looper & |
| 3712 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3713 | BINDER_LOOPER_STATE_ENTERED)) |
| 3714 | binder_enqueue_work_ilocked( |
| 3715 | &death->work, |
| 3716 | &thread->todo); |
| 3717 | else { |
| 3718 | binder_enqueue_work_ilocked( |
| 3719 | &death->work, |
| 3720 | &proc->todo); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3721 | binder_wakeup_proc_ilocked( |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3722 | proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3723 | } |
| 3724 | } else { |
| 3725 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); |
| 3726 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; |
| 3727 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3728 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3729 | } |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3730 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3731 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3732 | } break; |
| 3733 | case BC_DEAD_BINDER_DONE: { |
| 3734 | struct binder_work *w; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3735 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3736 | struct binder_ref_death *death = NULL; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3737 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3738 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3739 | return -EFAULT; |
| 3740 | |
Lisa Du | 7a64cd8 | 2016-02-17 09:32:52 +0800 | [diff] [blame] | 3741 | ptr += sizeof(cookie); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3742 | binder_inner_proc_lock(proc); |
| 3743 | list_for_each_entry(w, &proc->delivered_death, |
| 3744 | entry) { |
| 3745 | struct binder_ref_death *tmp_death = |
| 3746 | container_of(w, |
| 3747 | struct binder_ref_death, |
| 3748 | work); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3749 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3750 | if (tmp_death->cookie == cookie) { |
| 3751 | death = tmp_death; |
| 3752 | break; |
| 3753 | } |
| 3754 | } |
| 3755 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3756 | "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n", |
| 3757 | proc->pid, thread->pid, (u64)cookie, |
| 3758 | death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3759 | if (death == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3760 | binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n", |
| 3761 | proc->pid, thread->pid, (u64)cookie); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3762 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3763 | break; |
| 3764 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3765 | binder_dequeue_work_ilocked(&death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3766 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { |
| 3767 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3768 | if (thread->looper & |
| 3769 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3770 | BINDER_LOOPER_STATE_ENTERED)) |
| 3771 | binder_enqueue_work_ilocked( |
| 3772 | &death->work, &thread->todo); |
| 3773 | else { |
| 3774 | binder_enqueue_work_ilocked( |
| 3775 | &death->work, |
| 3776 | &proc->todo); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3777 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3778 | } |
| 3779 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3780 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3781 | } break; |
| 3782 | |
| 3783 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3784 | pr_err("%d:%d unknown command %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3785 | proc->pid, thread->pid, cmd); |
| 3786 | return -EINVAL; |
| 3787 | } |
| 3788 | *consumed = ptr - buffer; |
| 3789 | } |
| 3790 | return 0; |
| 3791 | } |
| 3792 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3793 | static void binder_stat_br(struct binder_proc *proc, |
| 3794 | struct binder_thread *thread, uint32_t cmd) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3795 | { |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3796 | trace_binder_return(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3797 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 3798 | atomic_inc(&binder_stats.br[_IOC_NR(cmd)]); |
| 3799 | atomic_inc(&proc->stats.br[_IOC_NR(cmd)]); |
| 3800 | atomic_inc(&thread->stats.br[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3801 | } |
| 3802 | } |
| 3803 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3804 | static int binder_has_thread_work(struct binder_thread *thread) |
| 3805 | { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3806 | return !binder_worklist_empty(thread->proc, &thread->todo) || |
| 3807 | thread->looper_need_return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3808 | } |
| 3809 | |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 3810 | static int binder_put_node_cmd(struct binder_proc *proc, |
| 3811 | struct binder_thread *thread, |
| 3812 | void __user **ptrp, |
| 3813 | binder_uintptr_t node_ptr, |
| 3814 | binder_uintptr_t node_cookie, |
| 3815 | int node_debug_id, |
| 3816 | uint32_t cmd, const char *cmd_name) |
| 3817 | { |
| 3818 | void __user *ptr = *ptrp; |
| 3819 | |
| 3820 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 3821 | return -EFAULT; |
| 3822 | ptr += sizeof(uint32_t); |
| 3823 | |
| 3824 | if (put_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
| 3825 | return -EFAULT; |
| 3826 | ptr += sizeof(binder_uintptr_t); |
| 3827 | |
| 3828 | if (put_user(node_cookie, (binder_uintptr_t __user *)ptr)) |
| 3829 | return -EFAULT; |
| 3830 | ptr += sizeof(binder_uintptr_t); |
| 3831 | |
| 3832 | binder_stat_br(proc, thread, cmd); |
| 3833 | binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n", |
| 3834 | proc->pid, thread->pid, cmd_name, node_debug_id, |
| 3835 | (u64)node_ptr, (u64)node_cookie); |
| 3836 | |
| 3837 | *ptrp = ptr; |
| 3838 | return 0; |
| 3839 | } |
| 3840 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3841 | static int binder_wait_for_work(struct binder_thread *thread, |
| 3842 | bool do_proc_work) |
| 3843 | { |
| 3844 | DEFINE_WAIT(wait); |
| 3845 | struct binder_proc *proc = thread->proc; |
| 3846 | int ret = 0; |
| 3847 | |
| 3848 | freezer_do_not_count(); |
| 3849 | binder_inner_proc_lock(proc); |
| 3850 | for (;;) { |
| 3851 | prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE); |
| 3852 | if (binder_has_work_ilocked(thread, do_proc_work)) |
| 3853 | break; |
| 3854 | if (do_proc_work) |
| 3855 | list_add(&thread->waiting_thread_node, |
| 3856 | &proc->waiting_threads); |
| 3857 | binder_inner_proc_unlock(proc); |
| 3858 | schedule(); |
| 3859 | binder_inner_proc_lock(proc); |
| 3860 | list_del_init(&thread->waiting_thread_node); |
| 3861 | if (signal_pending(current)) { |
| 3862 | ret = -ERESTARTSYS; |
| 3863 | break; |
| 3864 | } |
| 3865 | } |
| 3866 | finish_wait(&thread->wait, &wait); |
| 3867 | binder_inner_proc_unlock(proc); |
| 3868 | freezer_count(); |
| 3869 | |
| 3870 | return ret; |
| 3871 | } |
| 3872 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3873 | static int binder_thread_read(struct binder_proc *proc, |
| 3874 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3875 | binder_uintptr_t binder_buffer, size_t size, |
| 3876 | binder_size_t *consumed, int non_block) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3877 | { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3878 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3879 | void __user *ptr = buffer + *consumed; |
| 3880 | void __user *end = buffer + size; |
| 3881 | |
| 3882 | int ret = 0; |
| 3883 | int wait_for_proc_work; |
| 3884 | |
| 3885 | if (*consumed == 0) { |
| 3886 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) |
| 3887 | return -EFAULT; |
| 3888 | ptr += sizeof(uint32_t); |
| 3889 | } |
| 3890 | |
| 3891 | retry: |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3892 | binder_inner_proc_lock(proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3893 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3894 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3895 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3896 | thread->looper |= BINDER_LOOPER_STATE_WAITING; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3897 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3898 | trace_binder_wait_for_work(wait_for_proc_work, |
| 3899 | !!thread->transaction_stack, |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3900 | !binder_worklist_empty(proc, &thread->todo)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3901 | if (wait_for_proc_work) { |
| 3902 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 3903 | BINDER_LOOPER_STATE_ENTERED))) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3904 | 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] | 3905 | proc->pid, thread->pid, thread->looper); |
| 3906 | wait_event_interruptible(binder_user_error_wait, |
| 3907 | binder_stop_on_user_error < 2); |
| 3908 | } |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3909 | binder_restore_priority(current, proc->default_priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3910 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3911 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3912 | if (non_block) { |
| 3913 | if (!binder_has_work(thread, wait_for_proc_work)) |
| 3914 | ret = -EAGAIN; |
| 3915 | } else { |
| 3916 | ret = binder_wait_for_work(thread, wait_for_proc_work); |
| 3917 | } |
| 3918 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3919 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; |
| 3920 | |
| 3921 | if (ret) |
| 3922 | return ret; |
| 3923 | |
| 3924 | while (1) { |
| 3925 | uint32_t cmd; |
| 3926 | struct binder_transaction_data tr; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3927 | struct binder_work *w = NULL; |
| 3928 | struct list_head *list = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3929 | struct binder_transaction *t = NULL; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3930 | struct binder_thread *t_from; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3931 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 3932 | binder_inner_proc_lock(proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3933 | if (!binder_worklist_empty_ilocked(&thread->todo)) |
| 3934 | list = &thread->todo; |
| 3935 | else if (!binder_worklist_empty_ilocked(&proc->todo) && |
| 3936 | wait_for_proc_work) |
| 3937 | list = &proc->todo; |
| 3938 | else { |
| 3939 | binder_inner_proc_unlock(proc); |
| 3940 | |
Dmitry Voytik | 395262a | 2014-09-08 18:16:34 +0400 | [diff] [blame] | 3941 | /* no data added */ |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 3942 | if (ptr - buffer == 4 && !thread->looper_need_return) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3943 | goto retry; |
| 3944 | break; |
| 3945 | } |
| 3946 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 3947 | if (end - ptr < sizeof(tr) + 4) { |
| 3948 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3949 | break; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 3950 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3951 | w = binder_dequeue_work_head_ilocked(list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3952 | |
| 3953 | switch (w->type) { |
| 3954 | case BINDER_WORK_TRANSACTION: { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 3955 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3956 | t = container_of(w, struct binder_transaction, work); |
| 3957 | } break; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3958 | case BINDER_WORK_RETURN_ERROR: { |
| 3959 | struct binder_error *e = container_of( |
| 3960 | w, struct binder_error, work); |
| 3961 | |
| 3962 | WARN_ON(e->cmd == BR_OK); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 3963 | binder_inner_proc_unlock(proc); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3964 | if (put_user(e->cmd, (uint32_t __user *)ptr)) |
| 3965 | return -EFAULT; |
| 3966 | e->cmd = BR_OK; |
| 3967 | ptr += sizeof(uint32_t); |
| 3968 | |
| 3969 | binder_stat_br(proc, thread, cmd); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3970 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3971 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 3972 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3973 | cmd = BR_TRANSACTION_COMPLETE; |
| 3974 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 3975 | return -EFAULT; |
| 3976 | ptr += sizeof(uint32_t); |
| 3977 | |
| 3978 | binder_stat_br(proc, thread, cmd); |
| 3979 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3980 | "%d:%d BR_TRANSACTION_COMPLETE\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3981 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3982 | kfree(w); |
| 3983 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3984 | } break; |
| 3985 | case BINDER_WORK_NODE: { |
| 3986 | struct binder_node *node = container_of(w, struct binder_node, work); |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 3987 | int strong, weak; |
| 3988 | binder_uintptr_t node_ptr = node->ptr; |
| 3989 | binder_uintptr_t node_cookie = node->cookie; |
| 3990 | int node_debug_id = node->debug_id; |
| 3991 | int has_weak_ref; |
| 3992 | int has_strong_ref; |
| 3993 | void __user *orig_ptr = ptr; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3994 | |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 3995 | BUG_ON(proc != node->proc); |
| 3996 | strong = node->internal_strong_refs || |
| 3997 | node->local_strong_refs; |
| 3998 | weak = !hlist_empty(&node->refs) || |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3999 | node->local_weak_refs || |
| 4000 | node->tmp_refs || strong; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4001 | has_strong_ref = node->has_strong_ref; |
| 4002 | has_weak_ref = node->has_weak_ref; |
| 4003 | |
| 4004 | if (weak && !has_weak_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4005 | node->has_weak_ref = 1; |
| 4006 | node->pending_weak_ref = 1; |
| 4007 | node->local_weak_refs++; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4008 | } |
| 4009 | if (strong && !has_strong_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4010 | node->has_strong_ref = 1; |
| 4011 | node->pending_strong_ref = 1; |
| 4012 | node->local_strong_refs++; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4013 | } |
| 4014 | if (!strong && has_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4015 | node->has_strong_ref = 0; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4016 | if (!weak && has_weak_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4017 | node->has_weak_ref = 0; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4018 | if (!weak && !strong) { |
| 4019 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4020 | "%d:%d node %d u%016llx c%016llx deleted\n", |
| 4021 | proc->pid, thread->pid, |
| 4022 | node_debug_id, |
| 4023 | (u64)node_ptr, |
| 4024 | (u64)node_cookie); |
| 4025 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4026 | binder_inner_proc_unlock(proc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4027 | binder_node_lock(node); |
| 4028 | /* |
| 4029 | * Acquire the node lock before freeing the |
| 4030 | * node to serialize with other threads that |
| 4031 | * may have been holding the node lock while |
| 4032 | * decrementing this node (avoids race where |
| 4033 | * this thread frees while the other thread |
| 4034 | * is unlocking the node after the final |
| 4035 | * decrement) |
| 4036 | */ |
| 4037 | binder_node_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4038 | binder_free_node(node); |
| 4039 | } else |
| 4040 | binder_inner_proc_unlock(proc); |
| 4041 | |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4042 | if (weak && !has_weak_ref) |
| 4043 | ret = binder_put_node_cmd( |
| 4044 | proc, thread, &ptr, node_ptr, |
| 4045 | node_cookie, node_debug_id, |
| 4046 | BR_INCREFS, "BR_INCREFS"); |
| 4047 | if (!ret && strong && !has_strong_ref) |
| 4048 | ret = binder_put_node_cmd( |
| 4049 | proc, thread, &ptr, node_ptr, |
| 4050 | node_cookie, node_debug_id, |
| 4051 | BR_ACQUIRE, "BR_ACQUIRE"); |
| 4052 | if (!ret && !strong && has_strong_ref) |
| 4053 | ret = binder_put_node_cmd( |
| 4054 | proc, thread, &ptr, node_ptr, |
| 4055 | node_cookie, node_debug_id, |
| 4056 | BR_RELEASE, "BR_RELEASE"); |
| 4057 | if (!ret && !weak && has_weak_ref) |
| 4058 | ret = binder_put_node_cmd( |
| 4059 | proc, thread, &ptr, node_ptr, |
| 4060 | node_cookie, node_debug_id, |
| 4061 | BR_DECREFS, "BR_DECREFS"); |
| 4062 | if (orig_ptr == ptr) |
| 4063 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4064 | "%d:%d node %d u%016llx c%016llx state unchanged\n", |
| 4065 | proc->pid, thread->pid, |
| 4066 | node_debug_id, |
| 4067 | (u64)node_ptr, |
| 4068 | (u64)node_cookie); |
| 4069 | if (ret) |
| 4070 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4071 | } break; |
| 4072 | case BINDER_WORK_DEAD_BINDER: |
| 4073 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4074 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4075 | struct binder_ref_death *death; |
| 4076 | uint32_t cmd; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4077 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4078 | |
| 4079 | death = container_of(w, struct binder_ref_death, work); |
| 4080 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) |
| 4081 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; |
| 4082 | else |
| 4083 | cmd = BR_DEAD_BINDER; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4084 | cookie = death->cookie; |
| 4085 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4086 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4087 | "%d:%d %s %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4088 | proc->pid, thread->pid, |
| 4089 | cmd == BR_DEAD_BINDER ? |
| 4090 | "BR_DEAD_BINDER" : |
| 4091 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4092 | (u64)cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4093 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4094 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4095 | kfree(death); |
| 4096 | binder_stats_deleted(BINDER_STAT_DEATH); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4097 | } else { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4098 | binder_enqueue_work_ilocked( |
| 4099 | w, &proc->delivered_death); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4100 | binder_inner_proc_unlock(proc); |
| 4101 | } |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4102 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4103 | return -EFAULT; |
| 4104 | ptr += sizeof(uint32_t); |
| 4105 | if (put_user(cookie, |
| 4106 | (binder_uintptr_t __user *)ptr)) |
| 4107 | return -EFAULT; |
| 4108 | ptr += sizeof(binder_uintptr_t); |
| 4109 | binder_stat_br(proc, thread, cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4110 | if (cmd == BR_DEAD_BINDER) |
| 4111 | goto done; /* DEAD_BINDER notifications can cause transactions */ |
| 4112 | } break; |
| 4113 | } |
| 4114 | |
| 4115 | if (!t) |
| 4116 | continue; |
| 4117 | |
| 4118 | BUG_ON(t->buffer == NULL); |
| 4119 | if (t->buffer->target_node) { |
| 4120 | struct binder_node *target_node = t->buffer->target_node; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4121 | struct binder_priority node_prio; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4122 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4123 | tr.target.ptr = target_node->ptr; |
| 4124 | tr.cookie = target_node->cookie; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4125 | node_prio.sched_policy = target_node->sched_policy; |
| 4126 | node_prio.prio = target_node->min_priority; |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 4127 | binder_transaction_priority(current, t, node_prio, |
| 4128 | target_node->inherit_rt); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4129 | cmd = BR_TRANSACTION; |
| 4130 | } else { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4131 | tr.target.ptr = 0; |
| 4132 | tr.cookie = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4133 | cmd = BR_REPLY; |
| 4134 | } |
| 4135 | tr.code = t->code; |
| 4136 | tr.flags = t->flags; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 4137 | tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4138 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4139 | t_from = binder_get_txn_from(t); |
| 4140 | if (t_from) { |
| 4141 | struct task_struct *sender = t_from->proc->tsk; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4142 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4143 | tr.sender_pid = task_tgid_nr_ns(sender, |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 4144 | task_active_pid_ns(current)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4145 | } else { |
| 4146 | tr.sender_pid = 0; |
| 4147 | } |
| 4148 | |
| 4149 | tr.data_size = t->buffer->data_size; |
| 4150 | tr.offsets_size = t->buffer->offsets_size; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4151 | tr.data.ptr.buffer = (binder_uintptr_t) |
| 4152 | ((uintptr_t)t->buffer->data + |
| 4153 | binder_alloc_get_user_buffer_offset(&proc->alloc)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4154 | tr.data.ptr.offsets = tr.data.ptr.buffer + |
| 4155 | ALIGN(t->buffer->data_size, |
| 4156 | sizeof(void *)); |
| 4157 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4158 | if (put_user(cmd, (uint32_t __user *)ptr)) { |
| 4159 | if (t_from) |
| 4160 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4161 | return -EFAULT; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4162 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4163 | ptr += sizeof(uint32_t); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4164 | if (copy_to_user(ptr, &tr, sizeof(tr))) { |
| 4165 | if (t_from) |
| 4166 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4167 | return -EFAULT; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4168 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4169 | ptr += sizeof(tr); |
| 4170 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4171 | trace_binder_transaction_received(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4172 | binder_stat_br(proc, thread, cmd); |
| 4173 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4174 | "%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] | 4175 | proc->pid, thread->pid, |
| 4176 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : |
| 4177 | "BR_REPLY", |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4178 | t->debug_id, t_from ? t_from->proc->pid : 0, |
| 4179 | t_from ? t_from->pid : 0, cmd, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4180 | t->buffer->data_size, t->buffer->offsets_size, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4181 | (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4182 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4183 | if (t_from) |
| 4184 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4185 | t->buffer->allow_user_free = 1; |
| 4186 | if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4187 | binder_inner_proc_lock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4188 | t->to_parent = thread->transaction_stack; |
| 4189 | t->to_thread = thread; |
| 4190 | thread->transaction_stack = t; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4191 | binder_inner_proc_unlock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4192 | } else { |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 4193 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4194 | } |
| 4195 | break; |
| 4196 | } |
| 4197 | |
| 4198 | done: |
| 4199 | |
| 4200 | *consumed = ptr - buffer; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4201 | binder_inner_proc_lock(proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4202 | if (proc->requested_threads == 0 && |
| 4203 | list_empty(&thread->proc->waiting_threads) && |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4204 | proc->requested_threads_started < proc->max_threads && |
| 4205 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4206 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ |
| 4207 | /*spawn a new thread if we leave this out */) { |
| 4208 | proc->requested_threads++; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4209 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4210 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4211 | "%d:%d BR_SPAWN_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4212 | proc->pid, thread->pid); |
| 4213 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) |
| 4214 | return -EFAULT; |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 4215 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4216 | } else |
| 4217 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4218 | return 0; |
| 4219 | } |
| 4220 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4221 | static void binder_release_work(struct binder_proc *proc, |
| 4222 | struct list_head *list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4223 | { |
| 4224 | struct binder_work *w; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4225 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4226 | while (1) { |
| 4227 | w = binder_dequeue_work_head(proc, list); |
| 4228 | if (!w) |
| 4229 | return; |
| 4230 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4231 | switch (w->type) { |
| 4232 | case BINDER_WORK_TRANSACTION: { |
| 4233 | struct binder_transaction *t; |
| 4234 | |
| 4235 | t = container_of(w, struct binder_transaction, work); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4236 | if (t->buffer->target_node && |
| 4237 | !(t->flags & TF_ONE_WAY)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4238 | binder_send_failed_reply(t, BR_DEAD_REPLY); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4239 | } else { |
| 4240 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4241 | "undelivered transaction %d\n", |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4242 | t->debug_id); |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 4243 | binder_free_transaction(t); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4244 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4245 | } break; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4246 | case BINDER_WORK_RETURN_ERROR: { |
| 4247 | struct binder_error *e = container_of( |
| 4248 | w, struct binder_error, work); |
| 4249 | |
| 4250 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 4251 | "undelivered TRANSACTION_ERROR: %u\n", |
| 4252 | e->cmd); |
| 4253 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4254 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4255 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4256 | "undelivered TRANSACTION_COMPLETE\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4257 | kfree(w); |
| 4258 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4259 | } break; |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4260 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4261 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4262 | struct binder_ref_death *death; |
| 4263 | |
| 4264 | death = container_of(w, struct binder_ref_death, work); |
| 4265 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4266 | "undelivered death notification, %016llx\n", |
| 4267 | (u64)death->cookie); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4268 | kfree(death); |
| 4269 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 4270 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4271 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4272 | pr_err("unexpected work type, %d, not freed\n", |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4273 | w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4274 | break; |
| 4275 | } |
| 4276 | } |
| 4277 | |
| 4278 | } |
| 4279 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4280 | static struct binder_thread *binder_get_thread_ilocked( |
| 4281 | struct binder_proc *proc, struct binder_thread *new_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4282 | { |
| 4283 | struct binder_thread *thread = NULL; |
| 4284 | struct rb_node *parent = NULL; |
| 4285 | struct rb_node **p = &proc->threads.rb_node; |
| 4286 | |
| 4287 | while (*p) { |
| 4288 | parent = *p; |
| 4289 | thread = rb_entry(parent, struct binder_thread, rb_node); |
| 4290 | |
| 4291 | if (current->pid < thread->pid) |
| 4292 | p = &(*p)->rb_left; |
| 4293 | else if (current->pid > thread->pid) |
| 4294 | p = &(*p)->rb_right; |
| 4295 | else |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4296 | return thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4297 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4298 | if (!new_thread) |
| 4299 | return NULL; |
| 4300 | thread = new_thread; |
| 4301 | binder_stats_created(BINDER_STAT_THREAD); |
| 4302 | thread->proc = proc; |
| 4303 | thread->pid = current->pid; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4304 | get_task_struct(current); |
| 4305 | thread->task = current; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4306 | atomic_set(&thread->tmp_ref, 0); |
| 4307 | init_waitqueue_head(&thread->wait); |
| 4308 | INIT_LIST_HEAD(&thread->todo); |
| 4309 | rb_link_node(&thread->rb_node, parent, p); |
| 4310 | rb_insert_color(&thread->rb_node, &proc->threads); |
| 4311 | thread->looper_need_return = true; |
| 4312 | thread->return_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4313 | thread->return_error.cmd = BR_OK; |
| 4314 | thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4315 | thread->reply_error.cmd = BR_OK; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4316 | INIT_LIST_HEAD(&new_thread->waiting_thread_node); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4317 | return thread; |
| 4318 | } |
| 4319 | |
| 4320 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) |
| 4321 | { |
| 4322 | struct binder_thread *thread; |
| 4323 | struct binder_thread *new_thread; |
| 4324 | |
| 4325 | binder_inner_proc_lock(proc); |
| 4326 | thread = binder_get_thread_ilocked(proc, NULL); |
| 4327 | binder_inner_proc_unlock(proc); |
| 4328 | if (!thread) { |
| 4329 | new_thread = kzalloc(sizeof(*thread), GFP_KERNEL); |
| 4330 | if (new_thread == NULL) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4331 | return NULL; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4332 | binder_inner_proc_lock(proc); |
| 4333 | thread = binder_get_thread_ilocked(proc, new_thread); |
| 4334 | binder_inner_proc_unlock(proc); |
| 4335 | if (thread != new_thread) |
| 4336 | kfree(new_thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4337 | } |
| 4338 | return thread; |
| 4339 | } |
| 4340 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4341 | static void binder_free_proc(struct binder_proc *proc) |
| 4342 | { |
| 4343 | BUG_ON(!list_empty(&proc->todo)); |
| 4344 | BUG_ON(!list_empty(&proc->delivered_death)); |
| 4345 | binder_alloc_deferred_release(&proc->alloc); |
| 4346 | put_task_struct(proc->tsk); |
| 4347 | binder_stats_deleted(BINDER_STAT_PROC); |
| 4348 | kfree(proc); |
| 4349 | } |
| 4350 | |
| 4351 | static void binder_free_thread(struct binder_thread *thread) |
| 4352 | { |
| 4353 | BUG_ON(!list_empty(&thread->todo)); |
| 4354 | binder_stats_deleted(BINDER_STAT_THREAD); |
| 4355 | binder_proc_dec_tmpref(thread->proc); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4356 | put_task_struct(thread->task); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4357 | kfree(thread); |
| 4358 | } |
| 4359 | |
| 4360 | static int binder_thread_release(struct binder_proc *proc, |
| 4361 | struct binder_thread *thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4362 | { |
| 4363 | struct binder_transaction *t; |
| 4364 | struct binder_transaction *send_reply = NULL; |
| 4365 | int active_transactions = 0; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4366 | struct binder_transaction *last_t = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4367 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4368 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4369 | /* |
| 4370 | * take a ref on the proc so it survives |
| 4371 | * after we remove this thread from proc->threads. |
| 4372 | * The corresponding dec is when we actually |
| 4373 | * free the thread in binder_free_thread() |
| 4374 | */ |
| 4375 | proc->tmp_ref++; |
| 4376 | /* |
| 4377 | * take a ref on this thread to ensure it |
| 4378 | * survives while we are releasing it |
| 4379 | */ |
| 4380 | atomic_inc(&thread->tmp_ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4381 | rb_erase(&thread->rb_node, &proc->threads); |
| 4382 | t = thread->transaction_stack; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4383 | if (t) { |
| 4384 | spin_lock(&t->lock); |
| 4385 | if (t->to_thread == thread) |
| 4386 | send_reply = t; |
| 4387 | } |
| 4388 | thread->is_dead = true; |
| 4389 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4390 | while (t) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4391 | last_t = t; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4392 | active_transactions++; |
| 4393 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4394 | "release %d:%d transaction %d %s, still active\n", |
| 4395 | proc->pid, thread->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4396 | t->debug_id, |
| 4397 | (t->to_thread == thread) ? "in" : "out"); |
| 4398 | |
| 4399 | if (t->to_thread == thread) { |
| 4400 | t->to_proc = NULL; |
| 4401 | t->to_thread = NULL; |
| 4402 | if (t->buffer) { |
| 4403 | t->buffer->transaction = NULL; |
| 4404 | t->buffer = NULL; |
| 4405 | } |
| 4406 | t = t->to_parent; |
| 4407 | } else if (t->from == thread) { |
| 4408 | t->from = NULL; |
| 4409 | t = t->from_parent; |
| 4410 | } else |
| 4411 | BUG(); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4412 | spin_unlock(&last_t->lock); |
| 4413 | if (t) |
| 4414 | spin_lock(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4415 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4416 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4417 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4418 | if (send_reply) |
| 4419 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4420 | binder_release_work(proc, &thread->todo); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4421 | binder_thread_dec_tmpref(thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4422 | return active_transactions; |
| 4423 | } |
| 4424 | |
| 4425 | static unsigned int binder_poll(struct file *filp, |
| 4426 | struct poll_table_struct *wait) |
| 4427 | { |
| 4428 | struct binder_proc *proc = filp->private_data; |
| 4429 | struct binder_thread *thread = NULL; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4430 | bool wait_for_proc_work; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4431 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4432 | thread = binder_get_thread(proc); |
| 4433 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4434 | binder_inner_proc_lock(thread->proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4435 | thread->looper |= BINDER_LOOPER_STATE_POLL; |
| 4436 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
| 4437 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4438 | binder_inner_proc_unlock(thread->proc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4439 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4440 | if (binder_has_work(thread, wait_for_proc_work)) |
| 4441 | return POLLIN; |
| 4442 | |
| 4443 | poll_wait(filp, &thread->wait, wait); |
| 4444 | |
| 4445 | if (binder_has_thread_work(thread)) |
| 4446 | return POLLIN; |
| 4447 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4448 | return 0; |
| 4449 | } |
| 4450 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4451 | static int binder_ioctl_write_read(struct file *filp, |
| 4452 | unsigned int cmd, unsigned long arg, |
| 4453 | struct binder_thread *thread) |
| 4454 | { |
| 4455 | int ret = 0; |
| 4456 | struct binder_proc *proc = filp->private_data; |
| 4457 | unsigned int size = _IOC_SIZE(cmd); |
| 4458 | void __user *ubuf = (void __user *)arg; |
| 4459 | struct binder_write_read bwr; |
| 4460 | |
| 4461 | if (size != sizeof(struct binder_write_read)) { |
| 4462 | ret = -EINVAL; |
| 4463 | goto out; |
| 4464 | } |
| 4465 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { |
| 4466 | ret = -EFAULT; |
| 4467 | goto out; |
| 4468 | } |
| 4469 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4470 | "%d:%d write %lld at %016llx, read %lld at %016llx\n", |
| 4471 | proc->pid, thread->pid, |
| 4472 | (u64)bwr.write_size, (u64)bwr.write_buffer, |
| 4473 | (u64)bwr.read_size, (u64)bwr.read_buffer); |
| 4474 | |
| 4475 | if (bwr.write_size > 0) { |
| 4476 | ret = binder_thread_write(proc, thread, |
| 4477 | bwr.write_buffer, |
| 4478 | bwr.write_size, |
| 4479 | &bwr.write_consumed); |
| 4480 | trace_binder_write_done(ret); |
| 4481 | if (ret < 0) { |
| 4482 | bwr.read_consumed = 0; |
| 4483 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4484 | ret = -EFAULT; |
| 4485 | goto out; |
| 4486 | } |
| 4487 | } |
| 4488 | if (bwr.read_size > 0) { |
| 4489 | ret = binder_thread_read(proc, thread, bwr.read_buffer, |
| 4490 | bwr.read_size, |
| 4491 | &bwr.read_consumed, |
| 4492 | filp->f_flags & O_NONBLOCK); |
| 4493 | trace_binder_read_done(ret); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4494 | binder_inner_proc_lock(proc); |
| 4495 | if (!binder_worklist_empty_ilocked(&proc->todo)) |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 4496 | binder_wakeup_proc_ilocked(proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4497 | binder_inner_proc_unlock(proc); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4498 | if (ret < 0) { |
| 4499 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4500 | ret = -EFAULT; |
| 4501 | goto out; |
| 4502 | } |
| 4503 | } |
| 4504 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4505 | "%d:%d wrote %lld of %lld, read return %lld of %lld\n", |
| 4506 | proc->pid, thread->pid, |
| 4507 | (u64)bwr.write_consumed, (u64)bwr.write_size, |
| 4508 | (u64)bwr.read_consumed, (u64)bwr.read_size); |
| 4509 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { |
| 4510 | ret = -EFAULT; |
| 4511 | goto out; |
| 4512 | } |
| 4513 | out: |
| 4514 | return ret; |
| 4515 | } |
| 4516 | |
| 4517 | static int binder_ioctl_set_ctx_mgr(struct file *filp) |
| 4518 | { |
| 4519 | int ret = 0; |
| 4520 | struct binder_proc *proc = filp->private_data; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4521 | struct binder_context *context = proc->context; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4522 | struct binder_node *new_node; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4523 | kuid_t curr_euid = current_euid(); |
| 4524 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4525 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4526 | if (context->binder_context_mgr_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4527 | pr_err("BINDER_SET_CONTEXT_MGR already set\n"); |
| 4528 | ret = -EBUSY; |
| 4529 | goto out; |
| 4530 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 4531 | ret = security_binder_set_context_mgr(proc->tsk); |
| 4532 | if (ret < 0) |
| 4533 | goto out; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4534 | if (uid_valid(context->binder_context_mgr_uid)) { |
| 4535 | if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4536 | pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n", |
| 4537 | from_kuid(&init_user_ns, curr_euid), |
| 4538 | from_kuid(&init_user_ns, |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4539 | context->binder_context_mgr_uid)); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4540 | ret = -EPERM; |
| 4541 | goto out; |
| 4542 | } |
| 4543 | } else { |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4544 | context->binder_context_mgr_uid = curr_euid; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4545 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4546 | new_node = binder_new_node(proc, NULL); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4547 | if (!new_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4548 | ret = -ENOMEM; |
| 4549 | goto out; |
| 4550 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4551 | binder_node_lock(new_node); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4552 | new_node->local_weak_refs++; |
| 4553 | new_node->local_strong_refs++; |
| 4554 | new_node->has_strong_ref = 1; |
| 4555 | new_node->has_weak_ref = 1; |
| 4556 | context->binder_context_mgr_node = new_node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4557 | binder_node_unlock(new_node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4558 | binder_put_node(new_node); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4559 | out: |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4560 | mutex_unlock(&context->context_mgr_node_lock); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4561 | return ret; |
| 4562 | } |
| 4563 | |
Colin Cross | 833babb3 | 2017-06-20 13:54:44 -0700 | [diff] [blame] | 4564 | static int binder_ioctl_get_node_debug_info(struct binder_proc *proc, |
| 4565 | struct binder_node_debug_info *info) { |
| 4566 | struct rb_node *n; |
| 4567 | binder_uintptr_t ptr = info->ptr; |
| 4568 | |
| 4569 | memset(info, 0, sizeof(*info)); |
| 4570 | |
| 4571 | binder_inner_proc_lock(proc); |
| 4572 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { |
| 4573 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 4574 | rb_node); |
| 4575 | if (node->ptr > ptr) { |
| 4576 | info->ptr = node->ptr; |
| 4577 | info->cookie = node->cookie; |
| 4578 | info->has_strong_ref = node->has_strong_ref; |
| 4579 | info->has_weak_ref = node->has_weak_ref; |
| 4580 | break; |
| 4581 | } |
| 4582 | } |
| 4583 | binder_inner_proc_unlock(proc); |
| 4584 | |
| 4585 | return 0; |
| 4586 | } |
| 4587 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4588 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 4589 | { |
| 4590 | int ret; |
| 4591 | struct binder_proc *proc = filp->private_data; |
| 4592 | struct binder_thread *thread; |
| 4593 | unsigned int size = _IOC_SIZE(cmd); |
| 4594 | void __user *ubuf = (void __user *)arg; |
| 4595 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4596 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", |
| 4597 | proc->pid, current->pid, cmd, arg);*/ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4598 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4599 | trace_binder_ioctl(cmd, arg); |
| 4600 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4601 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4602 | if (ret) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4603 | goto err_unlocked; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4604 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4605 | thread = binder_get_thread(proc); |
| 4606 | if (thread == NULL) { |
| 4607 | ret = -ENOMEM; |
| 4608 | goto err; |
| 4609 | } |
| 4610 | |
| 4611 | switch (cmd) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4612 | case BINDER_WRITE_READ: |
| 4613 | ret = binder_ioctl_write_read(filp, cmd, arg, thread); |
| 4614 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4615 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4616 | break; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4617 | case BINDER_SET_MAX_THREADS: { |
| 4618 | int max_threads; |
| 4619 | |
| 4620 | if (copy_from_user(&max_threads, ubuf, |
| 4621 | sizeof(max_threads))) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4622 | ret = -EINVAL; |
| 4623 | goto err; |
| 4624 | } |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4625 | binder_inner_proc_lock(proc); |
| 4626 | proc->max_threads = max_threads; |
| 4627 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4628 | break; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4629 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4630 | case BINDER_SET_CONTEXT_MGR: |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4631 | ret = binder_ioctl_set_ctx_mgr(filp); |
| 4632 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4633 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4634 | break; |
| 4635 | case BINDER_THREAD_EXIT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4636 | binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4637 | proc->pid, thread->pid); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4638 | binder_thread_release(proc, thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4639 | thread = NULL; |
| 4640 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4641 | case BINDER_VERSION: { |
| 4642 | struct binder_version __user *ver = ubuf; |
| 4643 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4644 | if (size != sizeof(struct binder_version)) { |
| 4645 | ret = -EINVAL; |
| 4646 | goto err; |
| 4647 | } |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4648 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, |
| 4649 | &ver->protocol_version)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4650 | ret = -EINVAL; |
| 4651 | goto err; |
| 4652 | } |
| 4653 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4654 | } |
Colin Cross | 833babb3 | 2017-06-20 13:54:44 -0700 | [diff] [blame] | 4655 | case BINDER_GET_NODE_DEBUG_INFO: { |
| 4656 | struct binder_node_debug_info info; |
| 4657 | |
| 4658 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 4659 | ret = -EFAULT; |
| 4660 | goto err; |
| 4661 | } |
| 4662 | |
| 4663 | ret = binder_ioctl_get_node_debug_info(proc, &info); |
| 4664 | if (ret < 0) |
| 4665 | goto err; |
| 4666 | |
| 4667 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 4668 | ret = -EFAULT; |
| 4669 | goto err; |
| 4670 | } |
| 4671 | break; |
| 4672 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4673 | default: |
| 4674 | ret = -EINVAL; |
| 4675 | goto err; |
| 4676 | } |
| 4677 | ret = 0; |
| 4678 | err: |
| 4679 | if (thread) |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 4680 | thread->looper_need_return = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4681 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4682 | if (ret && ret != -ERESTARTSYS) |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4683 | 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] | 4684 | err_unlocked: |
| 4685 | trace_binder_ioctl_done(ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4686 | return ret; |
| 4687 | } |
| 4688 | |
| 4689 | static void binder_vma_open(struct vm_area_struct *vma) |
| 4690 | { |
| 4691 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4692 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4693 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4694 | "%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] | 4695 | proc->pid, vma->vm_start, vma->vm_end, |
| 4696 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4697 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | static void binder_vma_close(struct vm_area_struct *vma) |
| 4701 | { |
| 4702 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4703 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4704 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4705 | "%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] | 4706 | proc->pid, vma->vm_start, vma->vm_end, |
| 4707 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4708 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4709 | binder_alloc_vma_close(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4710 | binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES); |
| 4711 | } |
| 4712 | |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4713 | static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 4714 | { |
| 4715 | return VM_FAULT_SIGBUS; |
| 4716 | } |
| 4717 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 4718 | static const struct vm_operations_struct binder_vm_ops = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4719 | .open = binder_vma_open, |
| 4720 | .close = binder_vma_close, |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4721 | .fault = binder_vm_fault, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4722 | }; |
| 4723 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4724 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) |
| 4725 | { |
| 4726 | int ret; |
| 4727 | struct binder_proc *proc = filp->private_data; |
| 4728 | const char *failure_string; |
| 4729 | |
| 4730 | if (proc->tsk != current->group_leader) |
| 4731 | return -EINVAL; |
| 4732 | |
| 4733 | if ((vma->vm_end - vma->vm_start) > SZ_4M) |
| 4734 | vma->vm_end = vma->vm_start + SZ_4M; |
| 4735 | |
| 4736 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 4737 | "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 4738 | __func__, proc->pid, vma->vm_start, vma->vm_end, |
| 4739 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4740 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 4741 | |
| 4742 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { |
| 4743 | ret = -EPERM; |
| 4744 | failure_string = "bad vm_flags"; |
| 4745 | goto err_bad_arg; |
| 4746 | } |
| 4747 | vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE; |
| 4748 | vma->vm_ops = &binder_vm_ops; |
| 4749 | vma->vm_private_data = proc; |
| 4750 | |
| 4751 | ret = binder_alloc_mmap_handler(&proc->alloc, vma); |
| 4752 | if (ret) |
| 4753 | return ret; |
| 4754 | proc->files = get_files_struct(current); |
| 4755 | return 0; |
| 4756 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4757 | err_bad_arg: |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 4758 | pr_err("binder_mmap: %d %lx-%lx %s failed %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4759 | proc->pid, vma->vm_start, vma->vm_end, failure_string, ret); |
| 4760 | return ret; |
| 4761 | } |
| 4762 | |
| 4763 | static int binder_open(struct inode *nodp, struct file *filp) |
| 4764 | { |
| 4765 | struct binder_proc *proc; |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 4766 | struct binder_device *binder_dev; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4767 | |
| 4768 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n", |
| 4769 | current->group_leader->pid, current->pid); |
| 4770 | |
| 4771 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 4772 | if (proc == NULL) |
| 4773 | return -ENOMEM; |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 4774 | spin_lock_init(&proc->inner_lock); |
| 4775 | spin_lock_init(&proc->outer_lock); |
Martijn Coenen | 872c26e | 2017-03-07 15:51:18 +0100 | [diff] [blame] | 4776 | get_task_struct(current->group_leader); |
| 4777 | proc->tsk = current->group_leader; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4778 | INIT_LIST_HEAD(&proc->todo); |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 4779 | if (binder_supported_policy(current->policy)) { |
| 4780 | proc->default_priority.sched_policy = current->policy; |
| 4781 | proc->default_priority.prio = current->normal_prio; |
| 4782 | } else { |
| 4783 | proc->default_priority.sched_policy = SCHED_NORMAL; |
| 4784 | proc->default_priority.prio = NICE_TO_PRIO(0); |
| 4785 | } |
| 4786 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 4787 | binder_dev = container_of(filp->private_data, struct binder_device, |
| 4788 | miscdev); |
| 4789 | proc->context = &binder_dev->context; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4790 | binder_alloc_init(&proc->alloc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4791 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4792 | binder_stats_created(BINDER_STAT_PROC); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4793 | proc->pid = current->group_leader->pid; |
| 4794 | INIT_LIST_HEAD(&proc->delivered_death); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4795 | INIT_LIST_HEAD(&proc->waiting_threads); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4796 | filp->private_data = proc; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4797 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4798 | mutex_lock(&binder_procs_lock); |
| 4799 | hlist_add_head(&proc->proc_node, &binder_procs); |
| 4800 | mutex_unlock(&binder_procs_lock); |
| 4801 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4802 | if (binder_debugfs_dir_entry_proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4803 | char strbuf[11]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4804 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4805 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 4806 | /* |
| 4807 | * proc debug entries are shared between contexts, so |
| 4808 | * this will fail if the process tries to open the driver |
| 4809 | * again with a different context. The priting code will |
| 4810 | * anyway print all contexts that a given PID has, so this |
| 4811 | * is not a problem. |
| 4812 | */ |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4813 | proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO, |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 4814 | binder_debugfs_dir_entry_proc, |
| 4815 | (void *)(unsigned long)proc->pid, |
| 4816 | &binder_proc_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4817 | } |
| 4818 | |
| 4819 | return 0; |
| 4820 | } |
| 4821 | |
| 4822 | static int binder_flush(struct file *filp, fl_owner_t id) |
| 4823 | { |
| 4824 | struct binder_proc *proc = filp->private_data; |
| 4825 | |
| 4826 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); |
| 4827 | |
| 4828 | return 0; |
| 4829 | } |
| 4830 | |
| 4831 | static void binder_deferred_flush(struct binder_proc *proc) |
| 4832 | { |
| 4833 | struct rb_node *n; |
| 4834 | int wake_count = 0; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4835 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4836 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4837 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 4838 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4839 | |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 4840 | thread->looper_need_return = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4841 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { |
| 4842 | wake_up_interruptible(&thread->wait); |
| 4843 | wake_count++; |
| 4844 | } |
| 4845 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4846 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4847 | |
| 4848 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 4849 | "binder_flush: %d woke %d threads\n", proc->pid, |
| 4850 | wake_count); |
| 4851 | } |
| 4852 | |
| 4853 | static int binder_release(struct inode *nodp, struct file *filp) |
| 4854 | { |
| 4855 | struct binder_proc *proc = filp->private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4856 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4857 | debugfs_remove(proc->debugfs_entry); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4858 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); |
| 4859 | |
| 4860 | return 0; |
| 4861 | } |
| 4862 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4863 | static int binder_node_release(struct binder_node *node, int refs) |
| 4864 | { |
| 4865 | struct binder_ref *ref; |
| 4866 | int death = 0; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4867 | struct binder_proc *proc = node->proc; |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4868 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4869 | binder_release_work(proc, &node->async_todo); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4870 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4871 | binder_node_lock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4872 | binder_inner_proc_lock(proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4873 | binder_dequeue_work_ilocked(&node->work); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4874 | /* |
| 4875 | * The caller must have taken a temporary ref on the node, |
| 4876 | */ |
| 4877 | BUG_ON(!node->tmp_refs); |
| 4878 | if (hlist_empty(&node->refs) && node->tmp_refs == 1) { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4879 | binder_inner_proc_unlock(proc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4880 | binder_node_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4881 | binder_free_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4882 | |
| 4883 | return refs; |
| 4884 | } |
| 4885 | |
| 4886 | node->proc = NULL; |
| 4887 | node->local_strong_refs = 0; |
| 4888 | node->local_weak_refs = 0; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4889 | binder_inner_proc_unlock(proc); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4890 | |
| 4891 | spin_lock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4892 | hlist_add_head(&node->dead_node, &binder_dead_nodes); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4893 | spin_unlock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4894 | |
| 4895 | hlist_for_each_entry(ref, &node->refs, node_entry) { |
| 4896 | refs++; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4897 | /* |
| 4898 | * Need the node lock to synchronize |
| 4899 | * with new notification requests and the |
| 4900 | * inner lock to synchronize with queued |
| 4901 | * death notifications. |
| 4902 | */ |
| 4903 | binder_inner_proc_lock(ref->proc); |
| 4904 | if (!ref->death) { |
| 4905 | binder_inner_proc_unlock(ref->proc); |
Arve Hjønnevåg | e194fd8 | 2014-02-17 13:58:29 -0800 | [diff] [blame] | 4906 | continue; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4907 | } |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4908 | |
| 4909 | death++; |
| 4910 | |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4911 | BUG_ON(!list_empty(&ref->death->work.entry)); |
| 4912 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 4913 | binder_enqueue_work_ilocked(&ref->death->work, |
| 4914 | &ref->proc->todo); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 4915 | binder_wakeup_proc_ilocked(ref->proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4916 | binder_inner_proc_unlock(ref->proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4917 | } |
| 4918 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4919 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 4920 | "node %d now dead, refs %d, death %d\n", |
| 4921 | node->debug_id, refs, death); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4922 | binder_node_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4923 | binder_put_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4924 | |
| 4925 | return refs; |
| 4926 | } |
| 4927 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4928 | static void binder_deferred_release(struct binder_proc *proc) |
| 4929 | { |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4930 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4931 | struct rb_node *n; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4932 | int threads, nodes, incoming_refs, outgoing_refs, active_transactions; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4933 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4934 | BUG_ON(proc->files); |
| 4935 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4936 | mutex_lock(&binder_procs_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4937 | hlist_del(&proc->proc_node); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4938 | mutex_unlock(&binder_procs_lock); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4939 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4940 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4941 | if (context->binder_context_mgr_node && |
| 4942 | context->binder_context_mgr_node->proc == proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4943 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 4944 | "%s: %d context_mgr_node gone\n", |
| 4945 | __func__, proc->pid); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4946 | context->binder_context_mgr_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4947 | } |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4948 | mutex_unlock(&context->context_mgr_node_lock); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4949 | binder_inner_proc_lock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4950 | /* |
| 4951 | * Make sure proc stays alive after we |
| 4952 | * remove all the threads |
| 4953 | */ |
| 4954 | proc->tmp_ref++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4955 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4956 | proc->is_dead = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4957 | threads = 0; |
| 4958 | active_transactions = 0; |
| 4959 | while ((n = rb_first(&proc->threads))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4960 | struct binder_thread *thread; |
| 4961 | |
| 4962 | thread = rb_entry(n, struct binder_thread, rb_node); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4963 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4964 | threads++; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4965 | active_transactions += binder_thread_release(proc, thread); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4966 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4967 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4968 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4969 | nodes = 0; |
| 4970 | incoming_refs = 0; |
| 4971 | while ((n = rb_first(&proc->nodes))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4972 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4973 | |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4974 | node = rb_entry(n, struct binder_node, rb_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4975 | nodes++; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4976 | /* |
| 4977 | * take a temporary ref on the node before |
| 4978 | * calling binder_node_release() which will either |
| 4979 | * kfree() the node or call binder_put_node() |
| 4980 | */ |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 4981 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4982 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 4983 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4984 | incoming_refs = binder_node_release(node, incoming_refs); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 4985 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4986 | } |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 4987 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4988 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4989 | outgoing_refs = 0; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 4990 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4991 | while ((n = rb_first(&proc->refs_by_desc))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4992 | struct binder_ref *ref; |
| 4993 | |
| 4994 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4995 | outgoing_refs++; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 4996 | binder_cleanup_ref_olocked(ref); |
| 4997 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 4998 | binder_free_ref(ref); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 4999 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5000 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5001 | binder_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5002 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5003 | binder_release_work(proc, &proc->todo); |
| 5004 | binder_release_work(proc, &proc->delivered_death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5005 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5006 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5007 | "%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] | 5008 | __func__, proc->pid, threads, nodes, incoming_refs, |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5009 | outgoing_refs, active_transactions); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5010 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5011 | binder_proc_dec_tmpref(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5012 | } |
| 5013 | |
| 5014 | static void binder_deferred_func(struct work_struct *work) |
| 5015 | { |
| 5016 | struct binder_proc *proc; |
| 5017 | struct files_struct *files; |
| 5018 | |
| 5019 | int defer; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5020 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5021 | do { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5022 | mutex_lock(&binder_deferred_lock); |
| 5023 | if (!hlist_empty(&binder_deferred_list)) { |
| 5024 | proc = hlist_entry(binder_deferred_list.first, |
| 5025 | struct binder_proc, deferred_work_node); |
| 5026 | hlist_del_init(&proc->deferred_work_node); |
| 5027 | defer = proc->deferred_work; |
| 5028 | proc->deferred_work = 0; |
| 5029 | } else { |
| 5030 | proc = NULL; |
| 5031 | defer = 0; |
| 5032 | } |
| 5033 | mutex_unlock(&binder_deferred_lock); |
| 5034 | |
| 5035 | files = NULL; |
| 5036 | if (defer & BINDER_DEFERRED_PUT_FILES) { |
| 5037 | files = proc->files; |
| 5038 | if (files) |
| 5039 | proc->files = NULL; |
| 5040 | } |
| 5041 | |
| 5042 | if (defer & BINDER_DEFERRED_FLUSH) |
| 5043 | binder_deferred_flush(proc); |
| 5044 | |
| 5045 | if (defer & BINDER_DEFERRED_RELEASE) |
| 5046 | binder_deferred_release(proc); /* frees proc */ |
| 5047 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5048 | if (files) |
| 5049 | put_files_struct(files); |
| 5050 | } while (proc); |
| 5051 | } |
| 5052 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); |
| 5053 | |
| 5054 | static void |
| 5055 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) |
| 5056 | { |
| 5057 | mutex_lock(&binder_deferred_lock); |
| 5058 | proc->deferred_work |= defer; |
| 5059 | if (hlist_unhashed(&proc->deferred_work_node)) { |
| 5060 | hlist_add_head(&proc->deferred_work_node, |
| 5061 | &binder_deferred_list); |
Bhaktipriya Shridhar | 1beba52 | 2016-08-13 22:16:24 +0530 | [diff] [blame] | 5062 | schedule_work(&binder_deferred_work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5063 | } |
| 5064 | mutex_unlock(&binder_deferred_lock); |
| 5065 | } |
| 5066 | |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5067 | static void print_binder_transaction_ilocked(struct seq_file *m, |
| 5068 | struct binder_proc *proc, |
| 5069 | const char *prefix, |
| 5070 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5071 | { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5072 | struct binder_proc *to_proc; |
| 5073 | struct binder_buffer *buffer = t->buffer; |
| 5074 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5075 | spin_lock(&t->lock); |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5076 | to_proc = t->to_proc; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5077 | seq_printf(m, |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 5078 | "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %d:%d r%d", |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5079 | prefix, t->debug_id, t, |
| 5080 | t->from ? t->from->proc->pid : 0, |
| 5081 | t->from ? t->from->pid : 0, |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5082 | to_proc ? to_proc->pid : 0, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5083 | t->to_thread ? t->to_thread->pid : 0, |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 5084 | t->code, t->flags, t->priority.sched_policy, |
| 5085 | t->priority.prio, t->need_reply); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5086 | spin_unlock(&t->lock); |
| 5087 | |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5088 | if (proc != to_proc) { |
| 5089 | /* |
| 5090 | * Can only safely deref buffer if we are holding the |
| 5091 | * correct proc inner lock for this node |
| 5092 | */ |
| 5093 | seq_puts(m, "\n"); |
| 5094 | return; |
| 5095 | } |
| 5096 | |
| 5097 | if (buffer == NULL) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5098 | seq_puts(m, " buffer free\n"); |
| 5099 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5100 | } |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5101 | if (buffer->target_node) |
| 5102 | seq_printf(m, " node %d", buffer->target_node->debug_id); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5103 | seq_printf(m, " size %zd:%zd data %p\n", |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5104 | buffer->data_size, buffer->offsets_size, |
| 5105 | buffer->data); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5106 | } |
| 5107 | |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5108 | static void print_binder_work_ilocked(struct seq_file *m, |
| 5109 | struct binder_proc *proc, |
| 5110 | const char *prefix, |
| 5111 | const char *transaction_prefix, |
| 5112 | struct binder_work *w) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5113 | { |
| 5114 | struct binder_node *node; |
| 5115 | struct binder_transaction *t; |
| 5116 | |
| 5117 | switch (w->type) { |
| 5118 | case BINDER_WORK_TRANSACTION: |
| 5119 | t = container_of(w, struct binder_transaction, work); |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5120 | print_binder_transaction_ilocked( |
| 5121 | m, proc, transaction_prefix, t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5122 | break; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 5123 | case BINDER_WORK_RETURN_ERROR: { |
| 5124 | struct binder_error *e = container_of( |
| 5125 | w, struct binder_error, work); |
| 5126 | |
| 5127 | seq_printf(m, "%stransaction error: %u\n", |
| 5128 | prefix, e->cmd); |
| 5129 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5130 | case BINDER_WORK_TRANSACTION_COMPLETE: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5131 | seq_printf(m, "%stransaction complete\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5132 | break; |
| 5133 | case BINDER_WORK_NODE: |
| 5134 | node = container_of(w, struct binder_node, work); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5135 | seq_printf(m, "%snode work %d: u%016llx c%016llx\n", |
| 5136 | prefix, node->debug_id, |
| 5137 | (u64)node->ptr, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5138 | break; |
| 5139 | case BINDER_WORK_DEAD_BINDER: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5140 | seq_printf(m, "%shas dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5141 | break; |
| 5142 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5143 | seq_printf(m, "%shas cleared dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5144 | break; |
| 5145 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5146 | seq_printf(m, "%shas cleared death notification\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5147 | break; |
| 5148 | default: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5149 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5150 | break; |
| 5151 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5152 | } |
| 5153 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5154 | static void print_binder_thread_ilocked(struct seq_file *m, |
| 5155 | struct binder_thread *thread, |
| 5156 | int print_always) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5157 | { |
| 5158 | struct binder_transaction *t; |
| 5159 | struct binder_work *w; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5160 | size_t start_pos = m->count; |
| 5161 | size_t header_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5162 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5163 | 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] | 5164 | thread->pid, thread->looper, |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5165 | thread->looper_need_return, |
| 5166 | atomic_read(&thread->tmp_ref)); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5167 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5168 | t = thread->transaction_stack; |
| 5169 | while (t) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5170 | if (t->from == thread) { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5171 | print_binder_transaction_ilocked(m, thread->proc, |
| 5172 | " outgoing transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5173 | t = t->from_parent; |
| 5174 | } else if (t->to_thread == thread) { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5175 | print_binder_transaction_ilocked(m, thread->proc, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5176 | " incoming transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5177 | t = t->to_parent; |
| 5178 | } else { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5179 | print_binder_transaction_ilocked(m, thread->proc, |
| 5180 | " bad transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5181 | t = NULL; |
| 5182 | } |
| 5183 | } |
| 5184 | list_for_each_entry(w, &thread->todo, entry) { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5185 | print_binder_work_ilocked(m, thread->proc, " ", |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5186 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5187 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5188 | if (!print_always && m->count == header_pos) |
| 5189 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5190 | } |
| 5191 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5192 | static void print_binder_node_nilocked(struct seq_file *m, |
| 5193 | struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5194 | { |
| 5195 | struct binder_ref *ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5196 | struct binder_work *w; |
| 5197 | int count; |
| 5198 | |
| 5199 | count = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5200 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5201 | count++; |
| 5202 | |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 5203 | seq_printf(m, " node %d: u%016llx c%016llx pri %d:%d 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] | 5204 | node->debug_id, (u64)node->ptr, (u64)node->cookie, |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 5205 | node->sched_policy, node->min_priority, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5206 | node->has_strong_ref, node->has_weak_ref, |
| 5207 | node->local_strong_refs, node->local_weak_refs, |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5208 | node->internal_strong_refs, count, node->tmp_refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5209 | if (count) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5210 | seq_puts(m, " proc"); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5211 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5212 | seq_printf(m, " %d", ref->proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5213 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5214 | seq_puts(m, "\n"); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5215 | if (node->proc) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5216 | list_for_each_entry(w, &node->async_todo, entry) |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5217 | print_binder_work_ilocked(m, node->proc, " ", |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5218 | " pending async transaction", w); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5219 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5220 | } |
| 5221 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5222 | static void print_binder_ref_olocked(struct seq_file *m, |
| 5223 | struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5224 | { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5225 | binder_node_lock(ref->node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5226 | seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n", |
| 5227 | ref->data.debug_id, ref->data.desc, |
| 5228 | ref->node->proc ? "" : "dead ", |
| 5229 | ref->node->debug_id, ref->data.strong, |
| 5230 | ref->data.weak, ref->death); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5231 | binder_node_unlock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5232 | } |
| 5233 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5234 | static void print_binder_proc(struct seq_file *m, |
| 5235 | struct binder_proc *proc, int print_all) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5236 | { |
| 5237 | struct binder_work *w; |
| 5238 | struct rb_node *n; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5239 | size_t start_pos = m->count; |
| 5240 | size_t header_pos; |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5241 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5242 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5243 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5244 | seq_printf(m, "context %s\n", proc->context->name); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5245 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5246 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5247 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5248 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5249 | print_binder_thread_ilocked(m, rb_entry(n, struct binder_thread, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5250 | rb_node), print_all); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5251 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5252 | 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] | 5253 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 5254 | rb_node); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5255 | /* |
| 5256 | * take a temporary reference on the node so it |
| 5257 | * survives and isn't removed from the tree |
| 5258 | * while we print it. |
| 5259 | */ |
| 5260 | binder_inc_node_tmpref_ilocked(node); |
| 5261 | /* Need to drop inner lock to take node lock */ |
| 5262 | binder_inner_proc_unlock(proc); |
| 5263 | if (last_node) |
| 5264 | binder_put_node(last_node); |
| 5265 | binder_node_inner_lock(node); |
| 5266 | print_binder_node_nilocked(m, node); |
| 5267 | binder_node_inner_unlock(node); |
| 5268 | last_node = node; |
| 5269 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5270 | } |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5271 | binder_inner_proc_unlock(proc); |
| 5272 | if (last_node) |
| 5273 | binder_put_node(last_node); |
| 5274 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5275 | if (print_all) { |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5276 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5277 | for (n = rb_first(&proc->refs_by_desc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5278 | n != NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5279 | n = rb_next(n)) |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5280 | print_binder_ref_olocked(m, rb_entry(n, |
| 5281 | struct binder_ref, |
| 5282 | rb_node_desc)); |
| 5283 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5284 | } |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5285 | binder_alloc_print_allocated(m, &proc->alloc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5286 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5287 | list_for_each_entry(w, &proc->todo, entry) |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5288 | print_binder_work_ilocked(m, proc, " ", |
| 5289 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5290 | list_for_each_entry(w, &proc->delivered_death, entry) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5291 | seq_puts(m, " has delivered dead binder\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5292 | break; |
| 5293 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5294 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5295 | if (!print_all && m->count == header_pos) |
| 5296 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5297 | } |
| 5298 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5299 | static const char * const binder_return_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5300 | "BR_ERROR", |
| 5301 | "BR_OK", |
| 5302 | "BR_TRANSACTION", |
| 5303 | "BR_REPLY", |
| 5304 | "BR_ACQUIRE_RESULT", |
| 5305 | "BR_DEAD_REPLY", |
| 5306 | "BR_TRANSACTION_COMPLETE", |
| 5307 | "BR_INCREFS", |
| 5308 | "BR_ACQUIRE", |
| 5309 | "BR_RELEASE", |
| 5310 | "BR_DECREFS", |
| 5311 | "BR_ATTEMPT_ACQUIRE", |
| 5312 | "BR_NOOP", |
| 5313 | "BR_SPAWN_LOOPER", |
| 5314 | "BR_FINISHED", |
| 5315 | "BR_DEAD_BINDER", |
| 5316 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 5317 | "BR_FAILED_REPLY" |
| 5318 | }; |
| 5319 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5320 | static const char * const binder_command_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5321 | "BC_TRANSACTION", |
| 5322 | "BC_REPLY", |
| 5323 | "BC_ACQUIRE_RESULT", |
| 5324 | "BC_FREE_BUFFER", |
| 5325 | "BC_INCREFS", |
| 5326 | "BC_ACQUIRE", |
| 5327 | "BC_RELEASE", |
| 5328 | "BC_DECREFS", |
| 5329 | "BC_INCREFS_DONE", |
| 5330 | "BC_ACQUIRE_DONE", |
| 5331 | "BC_ATTEMPT_ACQUIRE", |
| 5332 | "BC_REGISTER_LOOPER", |
| 5333 | "BC_ENTER_LOOPER", |
| 5334 | "BC_EXIT_LOOPER", |
| 5335 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 5336 | "BC_CLEAR_DEATH_NOTIFICATION", |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 5337 | "BC_DEAD_BINDER_DONE", |
| 5338 | "BC_TRANSACTION_SG", |
| 5339 | "BC_REPLY_SG", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5340 | }; |
| 5341 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5342 | static const char * const binder_objstat_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5343 | "proc", |
| 5344 | "thread", |
| 5345 | "node", |
| 5346 | "ref", |
| 5347 | "death", |
| 5348 | "transaction", |
| 5349 | "transaction_complete" |
| 5350 | }; |
| 5351 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5352 | static void print_binder_stats(struct seq_file *m, const char *prefix, |
| 5353 | struct binder_stats *stats) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5354 | { |
| 5355 | int i; |
| 5356 | |
| 5357 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5358 | ARRAY_SIZE(binder_command_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5359 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5360 | int temp = atomic_read(&stats->bc[i]); |
| 5361 | |
| 5362 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5363 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5364 | binder_command_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5365 | } |
| 5366 | |
| 5367 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5368 | ARRAY_SIZE(binder_return_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5369 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5370 | int temp = atomic_read(&stats->br[i]); |
| 5371 | |
| 5372 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5373 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5374 | binder_return_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5375 | } |
| 5376 | |
| 5377 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5378 | ARRAY_SIZE(binder_objstat_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5379 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5380 | ARRAY_SIZE(stats->obj_deleted)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5381 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5382 | int created = atomic_read(&stats->obj_created[i]); |
| 5383 | int deleted = atomic_read(&stats->obj_deleted[i]); |
| 5384 | |
| 5385 | if (created || deleted) |
| 5386 | seq_printf(m, "%s%s: active %d total %d\n", |
| 5387 | prefix, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5388 | binder_objstat_strings[i], |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5389 | created - deleted, |
| 5390 | created); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5391 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5392 | } |
| 5393 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5394 | static void print_binder_proc_stats(struct seq_file *m, |
| 5395 | struct binder_proc *proc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5396 | { |
| 5397 | struct binder_work *w; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5398 | struct binder_thread *thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5399 | struct rb_node *n; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5400 | int count, strong, weak, ready_threads; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5401 | size_t free_async_space = |
| 5402 | binder_alloc_get_free_async_space(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5403 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5404 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5405 | seq_printf(m, "context %s\n", proc->context->name); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5406 | count = 0; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5407 | ready_threads = 0; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5408 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5409 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 5410 | count++; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5411 | |
| 5412 | list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node) |
| 5413 | ready_threads++; |
| 5414 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5415 | seq_printf(m, " threads: %d\n", count); |
| 5416 | seq_printf(m, " requested threads: %d+%d/%d\n" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5417 | " ready threads %d\n" |
| 5418 | " free async space %zd\n", proc->requested_threads, |
| 5419 | proc->requested_threads_started, proc->max_threads, |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5420 | ready_threads, |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5421 | free_async_space); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5422 | count = 0; |
| 5423 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) |
| 5424 | count++; |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5425 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5426 | seq_printf(m, " nodes: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5427 | count = 0; |
| 5428 | strong = 0; |
| 5429 | weak = 0; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5430 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5431 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 5432 | struct binder_ref *ref = rb_entry(n, struct binder_ref, |
| 5433 | rb_node_desc); |
| 5434 | count++; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5435 | strong += ref->data.strong; |
| 5436 | weak += ref->data.weak; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5437 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5438 | binder_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5439 | 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] | 5440 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5441 | count = binder_alloc_get_allocated_count(&proc->alloc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5442 | seq_printf(m, " buffers: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5443 | |
| 5444 | count = 0; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5445 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5446 | list_for_each_entry(w, &proc->todo, entry) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5447 | if (w->type == BINDER_WORK_TRANSACTION) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5448 | count++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5449 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5450 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5451 | seq_printf(m, " pending transactions: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5452 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5453 | print_binder_stats(m, " ", &proc->stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5454 | } |
| 5455 | |
| 5456 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5457 | static int binder_state_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5458 | { |
| 5459 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5460 | struct binder_node *node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5461 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5462 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5463 | seq_puts(m, "binder state:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5464 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5465 | spin_lock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5466 | if (!hlist_empty(&binder_dead_nodes)) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5467 | seq_puts(m, "dead nodes:\n"); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5468 | hlist_for_each_entry(node, &binder_dead_nodes, dead_node) { |
| 5469 | /* |
| 5470 | * take a temporary reference on the node so it |
| 5471 | * survives and isn't removed from the list |
| 5472 | * while we print it. |
| 5473 | */ |
| 5474 | node->tmp_refs++; |
| 5475 | spin_unlock(&binder_dead_nodes_lock); |
| 5476 | if (last_node) |
| 5477 | binder_put_node(last_node); |
| 5478 | binder_node_lock(node); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5479 | print_binder_node_nilocked(m, node); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5480 | binder_node_unlock(node); |
| 5481 | last_node = node; |
| 5482 | spin_lock(&binder_dead_nodes_lock); |
| 5483 | } |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5484 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5485 | if (last_node) |
| 5486 | binder_put_node(last_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5487 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5488 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5489 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5490 | print_binder_proc(m, proc, 1); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5491 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5492 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5493 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5494 | } |
| 5495 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5496 | static int binder_stats_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5497 | { |
| 5498 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5499 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5500 | seq_puts(m, "binder stats:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5501 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5502 | print_binder_stats(m, "", &binder_stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5503 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5504 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5505 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5506 | print_binder_proc_stats(m, proc); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5507 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5508 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5509 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5510 | } |
| 5511 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5512 | static int binder_transactions_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5513 | { |
| 5514 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5515 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5516 | seq_puts(m, "binder transactions:\n"); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5517 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5518 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5519 | print_binder_proc(m, proc, 0); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5520 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5521 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5522 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5523 | } |
| 5524 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5525 | static int binder_proc_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5526 | { |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5527 | struct binder_proc *itr; |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5528 | int pid = (unsigned long)m->private; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5529 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5530 | mutex_lock(&binder_procs_lock); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5531 | hlist_for_each_entry(itr, &binder_procs, proc_node) { |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5532 | if (itr->pid == pid) { |
| 5533 | seq_puts(m, "binder proc state:\n"); |
| 5534 | print_binder_proc(m, itr, 1); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5535 | } |
| 5536 | } |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5537 | mutex_unlock(&binder_procs_lock); |
| 5538 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5539 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5540 | } |
| 5541 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5542 | static void print_binder_transaction_log_entry(struct seq_file *m, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5543 | struct binder_transaction_log_entry *e) |
| 5544 | { |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5545 | int debug_id = READ_ONCE(e->debug_id_done); |
| 5546 | /* |
| 5547 | * read barrier to guarantee debug_id_done read before |
| 5548 | * we print the log values |
| 5549 | */ |
| 5550 | smp_rmb(); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5551 | seq_printf(m, |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5552 | "%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] | 5553 | e->debug_id, (e->call_type == 2) ? "reply" : |
| 5554 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5555 | e->from_thread, e->to_proc, e->to_thread, e->context_name, |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 5556 | e->to_node, e->target_handle, e->data_size, e->offsets_size, |
| 5557 | e->return_error, e->return_error_param, |
| 5558 | e->return_error_line); |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5559 | /* |
| 5560 | * read-barrier to guarantee read of debug_id_done after |
| 5561 | * done printing the fields of the entry |
| 5562 | */ |
| 5563 | smp_rmb(); |
| 5564 | seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ? |
| 5565 | "\n" : " (incomplete)\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5566 | } |
| 5567 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5568 | 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] | 5569 | { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5570 | struct binder_transaction_log *log = m->private; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5571 | unsigned int log_cur = atomic_read(&log->cur); |
| 5572 | unsigned int count; |
| 5573 | unsigned int cur; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5574 | int i; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5575 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5576 | count = log_cur + 1; |
| 5577 | cur = count < ARRAY_SIZE(log->entry) && !log->full ? |
| 5578 | 0 : count % ARRAY_SIZE(log->entry); |
| 5579 | if (count > ARRAY_SIZE(log->entry) || log->full) |
| 5580 | count = ARRAY_SIZE(log->entry); |
| 5581 | for (i = 0; i < count; i++) { |
| 5582 | unsigned int index = cur++ % ARRAY_SIZE(log->entry); |
| 5583 | |
| 5584 | print_binder_transaction_log_entry(m, &log->entry[index]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5585 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5586 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5587 | } |
| 5588 | |
| 5589 | static const struct file_operations binder_fops = { |
| 5590 | .owner = THIS_MODULE, |
| 5591 | .poll = binder_poll, |
| 5592 | .unlocked_ioctl = binder_ioctl, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5593 | .compat_ioctl = binder_ioctl, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5594 | .mmap = binder_mmap, |
| 5595 | .open = binder_open, |
| 5596 | .flush = binder_flush, |
| 5597 | .release = binder_release, |
| 5598 | }; |
| 5599 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5600 | BINDER_DEBUG_ENTRY(state); |
| 5601 | BINDER_DEBUG_ENTRY(stats); |
| 5602 | BINDER_DEBUG_ENTRY(transactions); |
| 5603 | BINDER_DEBUG_ENTRY(transaction_log); |
| 5604 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5605 | static int __init init_binder_device(const char *name) |
| 5606 | { |
| 5607 | int ret; |
| 5608 | struct binder_device *binder_device; |
| 5609 | |
| 5610 | binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL); |
| 5611 | if (!binder_device) |
| 5612 | return -ENOMEM; |
| 5613 | |
| 5614 | binder_device->miscdev.fops = &binder_fops; |
| 5615 | binder_device->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 5616 | binder_device->miscdev.name = name; |
| 5617 | |
| 5618 | binder_device->context.binder_context_mgr_uid = INVALID_UID; |
| 5619 | binder_device->context.name = name; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5620 | mutex_init(&binder_device->context.context_mgr_node_lock); |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5621 | |
| 5622 | ret = misc_register(&binder_device->miscdev); |
| 5623 | if (ret < 0) { |
| 5624 | kfree(binder_device); |
| 5625 | return ret; |
| 5626 | } |
| 5627 | |
| 5628 | hlist_add_head(&binder_device->hlist, &binder_devices); |
| 5629 | |
| 5630 | return ret; |
| 5631 | } |
| 5632 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5633 | static int __init binder_init(void) |
| 5634 | { |
| 5635 | int ret; |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5636 | char *device_name, *device_names; |
| 5637 | struct binder_device *device; |
| 5638 | struct hlist_node *tmp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5639 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5640 | atomic_set(&binder_transaction_log.cur, ~0U); |
| 5641 | atomic_set(&binder_transaction_log_failed.cur, ~0U); |
| 5642 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5643 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); |
| 5644 | if (binder_debugfs_dir_entry_root) |
| 5645 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", |
| 5646 | binder_debugfs_dir_entry_root); |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5647 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5648 | if (binder_debugfs_dir_entry_root) { |
| 5649 | debugfs_create_file("state", |
| 5650 | S_IRUGO, |
| 5651 | binder_debugfs_dir_entry_root, |
| 5652 | NULL, |
| 5653 | &binder_state_fops); |
| 5654 | debugfs_create_file("stats", |
| 5655 | S_IRUGO, |
| 5656 | binder_debugfs_dir_entry_root, |
| 5657 | NULL, |
| 5658 | &binder_stats_fops); |
| 5659 | debugfs_create_file("transactions", |
| 5660 | S_IRUGO, |
| 5661 | binder_debugfs_dir_entry_root, |
| 5662 | NULL, |
| 5663 | &binder_transactions_fops); |
| 5664 | debugfs_create_file("transaction_log", |
| 5665 | S_IRUGO, |
| 5666 | binder_debugfs_dir_entry_root, |
| 5667 | &binder_transaction_log, |
| 5668 | &binder_transaction_log_fops); |
| 5669 | debugfs_create_file("failed_transaction_log", |
| 5670 | S_IRUGO, |
| 5671 | binder_debugfs_dir_entry_root, |
| 5672 | &binder_transaction_log_failed, |
| 5673 | &binder_transaction_log_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5674 | } |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5675 | |
| 5676 | /* |
| 5677 | * Copy the module_parameter string, because we don't want to |
| 5678 | * tokenize it in-place. |
| 5679 | */ |
| 5680 | device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL); |
| 5681 | if (!device_names) { |
| 5682 | ret = -ENOMEM; |
| 5683 | goto err_alloc_device_names_failed; |
| 5684 | } |
| 5685 | strcpy(device_names, binder_devices_param); |
| 5686 | |
| 5687 | while ((device_name = strsep(&device_names, ","))) { |
| 5688 | ret = init_binder_device(device_name); |
| 5689 | if (ret) |
| 5690 | goto err_init_binder_device_failed; |
| 5691 | } |
| 5692 | |
| 5693 | return ret; |
| 5694 | |
| 5695 | err_init_binder_device_failed: |
| 5696 | hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) { |
| 5697 | misc_deregister(&device->miscdev); |
| 5698 | hlist_del(&device->hlist); |
| 5699 | kfree(device); |
| 5700 | } |
| 5701 | err_alloc_device_names_failed: |
| 5702 | debugfs_remove_recursive(binder_debugfs_dir_entry_root); |
| 5703 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5704 | return ret; |
| 5705 | } |
| 5706 | |
| 5707 | device_initcall(binder_init); |
| 5708 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5709 | #define CREATE_TRACE_POINTS |
| 5710 | #include "binder_trace.h" |
| 5711 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5712 | MODULE_LICENSE("GPL v2"); |