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 | |
Martijn Coenen | 81402ea | 2017-05-08 09:33:22 -0700 | [diff] [blame] | 1156 | trace_binder_set_priority(task->tgid, task->pid, task->normal_prio, |
| 1157 | to_kernel_prio(policy, priority), |
| 1158 | desired.prio); |
| 1159 | |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1160 | /* Set the actual priority */ |
| 1161 | if (task->policy != policy || is_rt_policy(policy)) { |
| 1162 | struct sched_param params; |
| 1163 | |
| 1164 | params.sched_priority = is_rt_policy(policy) ? priority : 0; |
| 1165 | |
| 1166 | sched_setscheduler_nocheck(task, |
| 1167 | policy | SCHED_RESET_ON_FORK, |
| 1168 | ¶ms); |
| 1169 | } |
| 1170 | if (is_fair_policy(policy)) |
| 1171 | set_user_nice(task, priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1172 | } |
| 1173 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1174 | static void binder_set_priority(struct task_struct *task, |
| 1175 | struct binder_priority desired) |
| 1176 | { |
| 1177 | binder_do_set_priority(task, desired, /* verify = */ true); |
| 1178 | } |
| 1179 | |
| 1180 | static void binder_restore_priority(struct task_struct *task, |
| 1181 | struct binder_priority desired) |
| 1182 | { |
| 1183 | binder_do_set_priority(task, desired, /* verify = */ false); |
| 1184 | } |
| 1185 | |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1186 | static void binder_transaction_priority(struct task_struct *task, |
| 1187 | struct binder_transaction *t, |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1188 | struct binder_priority node_prio, |
| 1189 | bool inherit_rt) |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1190 | { |
| 1191 | struct binder_priority desired_prio; |
| 1192 | |
| 1193 | if (t->set_priority_called) |
| 1194 | return; |
| 1195 | |
| 1196 | t->set_priority_called = true; |
| 1197 | t->saved_priority.sched_policy = task->policy; |
| 1198 | t->saved_priority.prio = task->normal_prio; |
| 1199 | |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1200 | if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) { |
| 1201 | desired_prio.prio = NICE_TO_PRIO(0); |
| 1202 | desired_prio.sched_policy = SCHED_NORMAL; |
| 1203 | } else { |
| 1204 | desired_prio.prio = t->priority.prio; |
| 1205 | desired_prio.sched_policy = t->priority.sched_policy; |
| 1206 | } |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1207 | |
| 1208 | if (node_prio.prio < t->priority.prio || |
| 1209 | (node_prio.prio == t->priority.prio && |
| 1210 | node_prio.sched_policy == SCHED_FIFO)) { |
| 1211 | /* |
| 1212 | * In case the minimum priority on the node is |
| 1213 | * higher (lower value), use that priority. If |
| 1214 | * the priority is the same, but the node uses |
| 1215 | * SCHED_FIFO, prefer SCHED_FIFO, since it can |
| 1216 | * run unbounded, unlike SCHED_RR. |
| 1217 | */ |
| 1218 | desired_prio = node_prio; |
| 1219 | } |
| 1220 | |
| 1221 | binder_set_priority(task, desired_prio); |
| 1222 | } |
| 1223 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1224 | static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc, |
| 1225 | binder_uintptr_t ptr) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1226 | { |
| 1227 | struct rb_node *n = proc->nodes.rb_node; |
| 1228 | struct binder_node *node; |
| 1229 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1230 | assert_spin_locked(&proc->inner_lock); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1231 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1232 | while (n) { |
| 1233 | node = rb_entry(n, struct binder_node, rb_node); |
| 1234 | |
| 1235 | if (ptr < node->ptr) |
| 1236 | n = n->rb_left; |
| 1237 | else if (ptr > node->ptr) |
| 1238 | n = n->rb_right; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1239 | else { |
| 1240 | /* |
| 1241 | * take an implicit weak reference |
| 1242 | * to ensure node stays alive until |
| 1243 | * call to binder_put_node() |
| 1244 | */ |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1245 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1246 | return node; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1247 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1248 | } |
| 1249 | return NULL; |
| 1250 | } |
| 1251 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1252 | static struct binder_node *binder_get_node(struct binder_proc *proc, |
| 1253 | binder_uintptr_t ptr) |
| 1254 | { |
| 1255 | struct binder_node *node; |
| 1256 | |
| 1257 | binder_inner_proc_lock(proc); |
| 1258 | node = binder_get_node_ilocked(proc, ptr); |
| 1259 | binder_inner_proc_unlock(proc); |
| 1260 | return node; |
| 1261 | } |
| 1262 | |
| 1263 | static struct binder_node *binder_init_node_ilocked( |
| 1264 | struct binder_proc *proc, |
| 1265 | struct binder_node *new_node, |
| 1266 | struct flat_binder_object *fp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1267 | { |
| 1268 | struct rb_node **p = &proc->nodes.rb_node; |
| 1269 | struct rb_node *parent = NULL; |
| 1270 | struct binder_node *node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1271 | binder_uintptr_t ptr = fp ? fp->binder : 0; |
| 1272 | binder_uintptr_t cookie = fp ? fp->cookie : 0; |
| 1273 | __u32 flags = fp ? fp->flags : 0; |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1274 | s8 priority; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1275 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1276 | assert_spin_locked(&proc->inner_lock); |
| 1277 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1278 | while (*p) { |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1279 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1280 | parent = *p; |
| 1281 | node = rb_entry(parent, struct binder_node, rb_node); |
| 1282 | |
| 1283 | if (ptr < node->ptr) |
| 1284 | p = &(*p)->rb_left; |
| 1285 | else if (ptr > node->ptr) |
| 1286 | p = &(*p)->rb_right; |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1287 | else { |
| 1288 | /* |
| 1289 | * A matching node is already in |
| 1290 | * the rb tree. Abandon the init |
| 1291 | * and return it. |
| 1292 | */ |
| 1293 | binder_inc_node_tmpref_ilocked(node); |
| 1294 | return node; |
| 1295 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1296 | } |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1297 | node = new_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1298 | binder_stats_created(BINDER_STAT_NODE); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1299 | node->tmp_refs++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1300 | rb_link_node(&node->rb_node, parent, p); |
| 1301 | rb_insert_color(&node->rb_node, &proc->nodes); |
Todd Kjos | c4bd08b | 2017-05-25 10:56:00 -0700 | [diff] [blame] | 1302 | node->debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1303 | node->proc = proc; |
| 1304 | node->ptr = ptr; |
| 1305 | node->cookie = cookie; |
| 1306 | node->work.type = BINDER_WORK_NODE; |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1307 | priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK; |
| 1308 | node->sched_policy = (flags & FLAT_BINDER_FLAG_PRIORITY_MASK) >> |
| 1309 | FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT; |
| 1310 | node->min_priority = to_kernel_prio(node->sched_policy, priority); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1311 | node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1312 | node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT); |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 1313 | spin_lock_init(&node->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1314 | INIT_LIST_HEAD(&node->work.entry); |
| 1315 | INIT_LIST_HEAD(&node->async_todo); |
| 1316 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1317 | "%d:%d node %d u%016llx c%016llx created\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1318 | proc->pid, current->pid, node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1319 | (u64)node->ptr, (u64)node->cookie); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1320 | |
| 1321 | return node; |
| 1322 | } |
| 1323 | |
| 1324 | static struct binder_node *binder_new_node(struct binder_proc *proc, |
| 1325 | struct flat_binder_object *fp) |
| 1326 | { |
| 1327 | struct binder_node *node; |
| 1328 | struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 1329 | |
| 1330 | if (!new_node) |
| 1331 | return NULL; |
| 1332 | binder_inner_proc_lock(proc); |
| 1333 | node = binder_init_node_ilocked(proc, new_node, fp); |
| 1334 | binder_inner_proc_unlock(proc); |
| 1335 | if (node != new_node) |
| 1336 | /* |
| 1337 | * The node was already added by another thread |
| 1338 | */ |
| 1339 | kfree(new_node); |
| 1340 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1341 | return node; |
| 1342 | } |
| 1343 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1344 | static void binder_free_node(struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1345 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1346 | kfree(node); |
| 1347 | binder_stats_deleted(BINDER_STAT_NODE); |
| 1348 | } |
| 1349 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1350 | static int binder_inc_node_nilocked(struct binder_node *node, int strong, |
| 1351 | int internal, |
| 1352 | struct list_head *target_list) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1353 | { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1354 | struct binder_proc *proc = node->proc; |
| 1355 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1356 | assert_spin_locked(&node->lock); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1357 | if (proc) |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1358 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1359 | if (strong) { |
| 1360 | if (internal) { |
| 1361 | if (target_list == NULL && |
| 1362 | node->internal_strong_refs == 0 && |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 1363 | !(node->proc && |
| 1364 | node == node->proc->context-> |
| 1365 | binder_context_mgr_node && |
| 1366 | node->has_strong_ref)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1367 | pr_err("invalid inc strong node for %d\n", |
| 1368 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1369 | return -EINVAL; |
| 1370 | } |
| 1371 | node->internal_strong_refs++; |
| 1372 | } else |
| 1373 | node->local_strong_refs++; |
| 1374 | if (!node->has_strong_ref && target_list) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1375 | binder_dequeue_work_ilocked(&node->work); |
| 1376 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1377 | } |
| 1378 | } else { |
| 1379 | if (!internal) |
| 1380 | node->local_weak_refs++; |
| 1381 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { |
| 1382 | if (target_list == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1383 | pr_err("invalid inc weak node for %d\n", |
| 1384 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1385 | return -EINVAL; |
| 1386 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1387 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1388 | } |
| 1389 | } |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1393 | static int binder_inc_node(struct binder_node *node, int strong, int internal, |
| 1394 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1395 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1396 | int ret; |
| 1397 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1398 | binder_node_inner_lock(node); |
| 1399 | ret = binder_inc_node_nilocked(node, strong, internal, target_list); |
| 1400 | binder_node_inner_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1401 | |
| 1402 | return ret; |
| 1403 | } |
| 1404 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1405 | static bool binder_dec_node_nilocked(struct binder_node *node, |
| 1406 | int strong, int internal) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1407 | { |
| 1408 | struct binder_proc *proc = node->proc; |
| 1409 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1410 | assert_spin_locked(&node->lock); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1411 | if (proc) |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1412 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1413 | if (strong) { |
| 1414 | if (internal) |
| 1415 | node->internal_strong_refs--; |
| 1416 | else |
| 1417 | node->local_strong_refs--; |
| 1418 | if (node->local_strong_refs || node->internal_strong_refs) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1419 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1420 | } else { |
| 1421 | if (!internal) |
| 1422 | node->local_weak_refs--; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1423 | if (node->local_weak_refs || node->tmp_refs || |
| 1424 | !hlist_empty(&node->refs)) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1425 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1426 | } |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1427 | |
| 1428 | if (proc && (node->has_strong_ref || node->has_weak_ref)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1429 | if (list_empty(&node->work.entry)) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1430 | binder_enqueue_work_ilocked(&node->work, &proc->todo); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1431 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1432 | } |
| 1433 | } else { |
| 1434 | if (hlist_empty(&node->refs) && !node->local_strong_refs && |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1435 | !node->local_weak_refs && !node->tmp_refs) { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1436 | if (proc) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1437 | binder_dequeue_work_ilocked(&node->work); |
| 1438 | rb_erase(&node->rb_node, &proc->nodes); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1439 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1440 | "refless node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1441 | node->debug_id); |
| 1442 | } else { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1443 | BUG_ON(!list_empty(&node->work.entry)); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 1444 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1445 | /* |
| 1446 | * tmp_refs could have changed so |
| 1447 | * check it again |
| 1448 | */ |
| 1449 | if (node->tmp_refs) { |
| 1450 | spin_unlock(&binder_dead_nodes_lock); |
| 1451 | return false; |
| 1452 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1453 | hlist_del(&node->dead_node); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 1454 | spin_unlock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1455 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1456 | "dead node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1457 | node->debug_id); |
| 1458 | } |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1459 | return true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1460 | } |
| 1461 | } |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1462 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1463 | } |
| 1464 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1465 | static void binder_dec_node(struct binder_node *node, int strong, int internal) |
| 1466 | { |
| 1467 | bool free_node; |
| 1468 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1469 | binder_node_inner_lock(node); |
| 1470 | free_node = binder_dec_node_nilocked(node, strong, internal); |
| 1471 | binder_node_inner_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1472 | if (free_node) |
| 1473 | binder_free_node(node); |
| 1474 | } |
| 1475 | |
| 1476 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node) |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1477 | { |
| 1478 | /* |
| 1479 | * No call to binder_inc_node() is needed since we |
| 1480 | * don't need to inform userspace of any changes to |
| 1481 | * tmp_refs |
| 1482 | */ |
| 1483 | node->tmp_refs++; |
| 1484 | } |
| 1485 | |
| 1486 | /** |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1487 | * binder_inc_node_tmpref() - take a temporary reference on node |
| 1488 | * @node: node to reference |
| 1489 | * |
| 1490 | * Take reference on node to prevent the node from being freed |
| 1491 | * while referenced only by a local variable. The inner lock is |
| 1492 | * needed to serialize with the node work on the queue (which |
| 1493 | * isn't needed after the node is dead). If the node is dead |
| 1494 | * (node->proc is NULL), use binder_dead_nodes_lock to protect |
| 1495 | * node->tmp_refs against dead-node-only cases where the node |
| 1496 | * lock cannot be acquired (eg traversing the dead node list to |
| 1497 | * print nodes) |
| 1498 | */ |
| 1499 | static void binder_inc_node_tmpref(struct binder_node *node) |
| 1500 | { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1501 | binder_node_lock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1502 | if (node->proc) |
| 1503 | binder_inner_proc_lock(node->proc); |
| 1504 | else |
| 1505 | spin_lock(&binder_dead_nodes_lock); |
| 1506 | binder_inc_node_tmpref_ilocked(node); |
| 1507 | if (node->proc) |
| 1508 | binder_inner_proc_unlock(node->proc); |
| 1509 | else |
| 1510 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1511 | binder_node_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | /** |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1515 | * binder_dec_node_tmpref() - remove a temporary reference on node |
| 1516 | * @node: node to reference |
| 1517 | * |
| 1518 | * Release temporary reference on node taken via binder_inc_node_tmpref() |
| 1519 | */ |
| 1520 | static void binder_dec_node_tmpref(struct binder_node *node) |
| 1521 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1522 | bool free_node; |
| 1523 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1524 | binder_node_inner_lock(node); |
| 1525 | if (!node->proc) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1526 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1527 | node->tmp_refs--; |
| 1528 | BUG_ON(node->tmp_refs < 0); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1529 | if (!node->proc) |
| 1530 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1531 | /* |
| 1532 | * Call binder_dec_node() to check if all refcounts are 0 |
| 1533 | * and cleanup is needed. Calling with strong=0 and internal=1 |
| 1534 | * causes no actual reference to be released in binder_dec_node(). |
| 1535 | * If that changes, a change is needed here too. |
| 1536 | */ |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1537 | free_node = binder_dec_node_nilocked(node, 0, 1); |
| 1538 | binder_node_inner_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1539 | if (free_node) |
| 1540 | binder_free_node(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | static void binder_put_node(struct binder_node *node) |
| 1544 | { |
| 1545 | binder_dec_node_tmpref(node); |
| 1546 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1547 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1548 | static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc, |
| 1549 | u32 desc, bool need_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1550 | { |
| 1551 | struct rb_node *n = proc->refs_by_desc.rb_node; |
| 1552 | struct binder_ref *ref; |
| 1553 | |
| 1554 | while (n) { |
| 1555 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1556 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1557 | if (desc < ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1558 | n = n->rb_left; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1559 | } else if (desc > ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1560 | n = n->rb_right; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1561 | } else if (need_strong_ref && !ref->data.strong) { |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1562 | binder_user_error("tried to use weak ref as strong ref\n"); |
| 1563 | return NULL; |
| 1564 | } else { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1565 | return ref; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1566 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1567 | } |
| 1568 | return NULL; |
| 1569 | } |
| 1570 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1571 | /** |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1572 | * 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] | 1573 | * @proc: binder_proc that owns the ref |
| 1574 | * @node: binder_node of target |
| 1575 | * @new_ref: newly allocated binder_ref to be initialized or %NULL |
| 1576 | * |
| 1577 | * Look up the ref for the given node and return it if it exists |
| 1578 | * |
| 1579 | * If it doesn't exist and the caller provides a newly allocated |
| 1580 | * ref, initialize the fields of the newly allocated ref and insert |
| 1581 | * into the given proc rb_trees and node refs list. |
| 1582 | * |
| 1583 | * Return: the ref for node. It is possible that another thread |
| 1584 | * allocated/initialized the ref first in which case the |
| 1585 | * returned ref would be different than the passed-in |
| 1586 | * new_ref. new_ref must be kfree'd by the caller in |
| 1587 | * this case. |
| 1588 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1589 | static struct binder_ref *binder_get_ref_for_node_olocked( |
| 1590 | struct binder_proc *proc, |
| 1591 | struct binder_node *node, |
| 1592 | struct binder_ref *new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1593 | { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1594 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1595 | struct rb_node **p = &proc->refs_by_node.rb_node; |
| 1596 | struct rb_node *parent = NULL; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1597 | struct binder_ref *ref; |
| 1598 | struct rb_node *n; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1599 | |
| 1600 | while (*p) { |
| 1601 | parent = *p; |
| 1602 | ref = rb_entry(parent, struct binder_ref, rb_node_node); |
| 1603 | |
| 1604 | if (node < ref->node) |
| 1605 | p = &(*p)->rb_left; |
| 1606 | else if (node > ref->node) |
| 1607 | p = &(*p)->rb_right; |
| 1608 | else |
| 1609 | return ref; |
| 1610 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1611 | if (!new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1612 | return NULL; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1613 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1614 | binder_stats_created(BINDER_STAT_REF); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1615 | new_ref->data.debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1616 | new_ref->proc = proc; |
| 1617 | new_ref->node = node; |
| 1618 | rb_link_node(&new_ref->rb_node_node, parent, p); |
| 1619 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); |
| 1620 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1621 | 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] | 1622 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 1623 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1624 | if (ref->data.desc > new_ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1625 | break; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1626 | new_ref->data.desc = ref->data.desc + 1; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | p = &proc->refs_by_desc.rb_node; |
| 1630 | while (*p) { |
| 1631 | parent = *p; |
| 1632 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); |
| 1633 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1634 | if (new_ref->data.desc < ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1635 | p = &(*p)->rb_left; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1636 | else if (new_ref->data.desc > ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1637 | p = &(*p)->rb_right; |
| 1638 | else |
| 1639 | BUG(); |
| 1640 | } |
| 1641 | rb_link_node(&new_ref->rb_node_desc, parent, p); |
| 1642 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1643 | |
| 1644 | binder_node_lock(node); |
Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1645 | hlist_add_head(&new_ref->node_entry, &node->refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1646 | |
Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1647 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1648 | "%d new ref %d desc %d for node %d\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1649 | proc->pid, new_ref->data.debug_id, new_ref->data.desc, |
Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1650 | node->debug_id); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1651 | binder_node_unlock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1652 | return new_ref; |
| 1653 | } |
| 1654 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1655 | static void binder_cleanup_ref_olocked(struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1656 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1657 | bool delete_node = false; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1658 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1659 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1660 | "%d delete ref %d desc %d for node %d\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1661 | ref->proc->pid, ref->data.debug_id, ref->data.desc, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1662 | ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1663 | |
| 1664 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); |
| 1665 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1666 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1667 | binder_node_inner_lock(ref->node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1668 | if (ref->data.strong) |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1669 | binder_dec_node_nilocked(ref->node, 1, 1); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1670 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1671 | hlist_del(&ref->node_entry); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1672 | delete_node = binder_dec_node_nilocked(ref->node, 0, 1); |
| 1673 | binder_node_inner_unlock(ref->node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1674 | /* |
| 1675 | * Clear ref->node unless we want the caller to free the node |
| 1676 | */ |
| 1677 | if (!delete_node) { |
| 1678 | /* |
| 1679 | * The caller uses ref->node to determine |
| 1680 | * whether the node needs to be freed. Clear |
| 1681 | * it since the node is still alive. |
| 1682 | */ |
| 1683 | ref->node = NULL; |
| 1684 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1685 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1686 | if (ref->death) { |
| 1687 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1688 | "%d delete ref %d desc %d has death notification\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1689 | ref->proc->pid, ref->data.debug_id, |
| 1690 | ref->data.desc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1691 | binder_dequeue_work(ref->proc, &ref->death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1692 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 1693 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1694 | binder_stats_deleted(BINDER_STAT_REF); |
| 1695 | } |
| 1696 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1697 | /** |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1698 | * binder_inc_ref_olocked() - increment the ref for given handle |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1699 | * @ref: ref to be incremented |
| 1700 | * @strong: if true, strong increment, else weak |
| 1701 | * @target_list: list to queue node work on |
| 1702 | * |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1703 | * Increment the ref. @ref->proc->outer_lock must be held on entry |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1704 | * |
| 1705 | * Return: 0, if successful, else errno |
| 1706 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1707 | static int binder_inc_ref_olocked(struct binder_ref *ref, int strong, |
| 1708 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1709 | { |
| 1710 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1711 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1712 | if (strong) { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1713 | if (ref->data.strong == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1714 | ret = binder_inc_node(ref->node, 1, 1, target_list); |
| 1715 | if (ret) |
| 1716 | return ret; |
| 1717 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1718 | ref->data.strong++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1719 | } else { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1720 | if (ref->data.weak == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1721 | ret = binder_inc_node(ref->node, 0, 1, target_list); |
| 1722 | if (ret) |
| 1723 | return ret; |
| 1724 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1725 | ref->data.weak++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1726 | } |
| 1727 | return 0; |
| 1728 | } |
| 1729 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1730 | /** |
| 1731 | * binder_dec_ref() - dec the ref for given handle |
| 1732 | * @ref: ref to be decremented |
| 1733 | * @strong: if true, strong decrement, else weak |
| 1734 | * |
| 1735 | * Decrement the ref. |
| 1736 | * |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1737 | * Return: true if ref is cleaned up and ready to be freed |
| 1738 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1739 | 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] | 1740 | { |
| 1741 | if (strong) { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1742 | if (ref->data.strong == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1743 | 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] | 1744 | ref->proc->pid, ref->data.debug_id, |
| 1745 | ref->data.desc, ref->data.strong, |
| 1746 | ref->data.weak); |
| 1747 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1748 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1749 | ref->data.strong--; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1750 | if (ref->data.strong == 0) |
| 1751 | binder_dec_node(ref->node, strong, 1); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1752 | } else { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1753 | if (ref->data.weak == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1754 | 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] | 1755 | ref->proc->pid, ref->data.debug_id, |
| 1756 | ref->data.desc, ref->data.strong, |
| 1757 | ref->data.weak); |
| 1758 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1759 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1760 | ref->data.weak--; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1761 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1762 | if (ref->data.strong == 0 && ref->data.weak == 0) { |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1763 | binder_cleanup_ref_olocked(ref); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1764 | return true; |
| 1765 | } |
| 1766 | return false; |
| 1767 | } |
| 1768 | |
| 1769 | /** |
| 1770 | * binder_get_node_from_ref() - get the node from the given proc/desc |
| 1771 | * @proc: proc containing the ref |
| 1772 | * @desc: the handle associated with the ref |
| 1773 | * @need_strong_ref: if true, only return node if ref is strong |
| 1774 | * @rdata: the id/refcount data for the ref |
| 1775 | * |
| 1776 | * Given a proc and ref handle, return the associated binder_node |
| 1777 | * |
| 1778 | * Return: a binder_node or NULL if not found or not strong when strong required |
| 1779 | */ |
| 1780 | static struct binder_node *binder_get_node_from_ref( |
| 1781 | struct binder_proc *proc, |
| 1782 | u32 desc, bool need_strong_ref, |
| 1783 | struct binder_ref_data *rdata) |
| 1784 | { |
| 1785 | struct binder_node *node; |
| 1786 | struct binder_ref *ref; |
| 1787 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1788 | binder_proc_lock(proc); |
| 1789 | ref = binder_get_ref_olocked(proc, desc, need_strong_ref); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1790 | if (!ref) |
| 1791 | goto err_no_ref; |
| 1792 | node = ref->node; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1793 | /* |
| 1794 | * Take an implicit reference on the node to ensure |
| 1795 | * it stays alive until the call to binder_put_node() |
| 1796 | */ |
| 1797 | binder_inc_node_tmpref(node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1798 | if (rdata) |
| 1799 | *rdata = ref->data; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1800 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1801 | |
| 1802 | return node; |
| 1803 | |
| 1804 | err_no_ref: |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1805 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1806 | return NULL; |
| 1807 | } |
| 1808 | |
| 1809 | /** |
| 1810 | * binder_free_ref() - free the binder_ref |
| 1811 | * @ref: ref to free |
| 1812 | * |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1813 | * Free the binder_ref. Free the binder_node indicated by ref->node |
| 1814 | * (if non-NULL) and the binder_ref_death indicated by ref->death. |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1815 | */ |
| 1816 | static void binder_free_ref(struct binder_ref *ref) |
| 1817 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1818 | if (ref->node) |
| 1819 | binder_free_node(ref->node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1820 | kfree(ref->death); |
| 1821 | kfree(ref); |
| 1822 | } |
| 1823 | |
| 1824 | /** |
| 1825 | * binder_update_ref_for_handle() - inc/dec the ref for given handle |
| 1826 | * @proc: proc containing the ref |
| 1827 | * @desc: the handle associated with the ref |
| 1828 | * @increment: true=inc reference, false=dec reference |
| 1829 | * @strong: true=strong reference, false=weak reference |
| 1830 | * @rdata: the id/refcount data for the ref |
| 1831 | * |
| 1832 | * Given a proc and ref handle, increment or decrement the ref |
| 1833 | * according to "increment" arg. |
| 1834 | * |
| 1835 | * Return: 0 if successful, else errno |
| 1836 | */ |
| 1837 | static int binder_update_ref_for_handle(struct binder_proc *proc, |
| 1838 | uint32_t desc, bool increment, bool strong, |
| 1839 | struct binder_ref_data *rdata) |
| 1840 | { |
| 1841 | int ret = 0; |
| 1842 | struct binder_ref *ref; |
| 1843 | bool delete_ref = false; |
| 1844 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1845 | binder_proc_lock(proc); |
| 1846 | ref = binder_get_ref_olocked(proc, desc, strong); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1847 | if (!ref) { |
| 1848 | ret = -EINVAL; |
| 1849 | goto err_no_ref; |
| 1850 | } |
| 1851 | if (increment) |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1852 | ret = binder_inc_ref_olocked(ref, strong, NULL); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1853 | else |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1854 | delete_ref = binder_dec_ref_olocked(ref, strong); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1855 | |
| 1856 | if (rdata) |
| 1857 | *rdata = ref->data; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1858 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1859 | |
| 1860 | if (delete_ref) |
| 1861 | binder_free_ref(ref); |
| 1862 | return ret; |
| 1863 | |
| 1864 | err_no_ref: |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1865 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1866 | return ret; |
| 1867 | } |
| 1868 | |
| 1869 | /** |
| 1870 | * binder_dec_ref_for_handle() - dec the ref for given handle |
| 1871 | * @proc: proc containing the ref |
| 1872 | * @desc: the handle associated with the ref |
| 1873 | * @strong: true=strong reference, false=weak reference |
| 1874 | * @rdata: the id/refcount data for the ref |
| 1875 | * |
| 1876 | * Just calls binder_update_ref_for_handle() to decrement the ref. |
| 1877 | * |
| 1878 | * Return: 0 if successful, else errno |
| 1879 | */ |
| 1880 | static int binder_dec_ref_for_handle(struct binder_proc *proc, |
| 1881 | uint32_t desc, bool strong, struct binder_ref_data *rdata) |
| 1882 | { |
| 1883 | return binder_update_ref_for_handle(proc, desc, false, strong, rdata); |
| 1884 | } |
| 1885 | |
| 1886 | |
| 1887 | /** |
| 1888 | * binder_inc_ref_for_node() - increment the ref for given proc/node |
| 1889 | * @proc: proc containing the ref |
| 1890 | * @node: target node |
| 1891 | * @strong: true=strong reference, false=weak reference |
| 1892 | * @target_list: worklist to use if node is incremented |
| 1893 | * @rdata: the id/refcount data for the ref |
| 1894 | * |
| 1895 | * Given a proc and node, increment the ref. Create the ref if it |
| 1896 | * doesn't already exist |
| 1897 | * |
| 1898 | * Return: 0 if successful, else errno |
| 1899 | */ |
| 1900 | static int binder_inc_ref_for_node(struct binder_proc *proc, |
| 1901 | struct binder_node *node, |
| 1902 | bool strong, |
| 1903 | struct list_head *target_list, |
| 1904 | struct binder_ref_data *rdata) |
| 1905 | { |
| 1906 | struct binder_ref *ref; |
| 1907 | struct binder_ref *new_ref = NULL; |
| 1908 | int ret = 0; |
| 1909 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1910 | binder_proc_lock(proc); |
| 1911 | ref = binder_get_ref_for_node_olocked(proc, node, NULL); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1912 | if (!ref) { |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1913 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1914 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
| 1915 | if (!new_ref) |
| 1916 | return -ENOMEM; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1917 | binder_proc_lock(proc); |
| 1918 | ref = binder_get_ref_for_node_olocked(proc, node, new_ref); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1919 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1920 | ret = binder_inc_ref_olocked(ref, strong, target_list); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1921 | *rdata = ref->data; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1922 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1923 | if (new_ref && ref != new_ref) |
| 1924 | /* |
| 1925 | * Another thread created the ref first so |
| 1926 | * free the one we allocated |
| 1927 | */ |
| 1928 | kfree(new_ref); |
| 1929 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1930 | } |
| 1931 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 1932 | static void binder_pop_transaction_ilocked(struct binder_thread *target_thread, |
| 1933 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1934 | { |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 1935 | BUG_ON(!target_thread); |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1936 | assert_spin_locked(&target_thread->proc->inner_lock); |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 1937 | BUG_ON(target_thread->transaction_stack != t); |
| 1938 | BUG_ON(target_thread->transaction_stack->from != target_thread); |
| 1939 | target_thread->transaction_stack = |
| 1940 | target_thread->transaction_stack->from_parent; |
| 1941 | t->from = NULL; |
| 1942 | } |
| 1943 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1944 | /** |
| 1945 | * binder_thread_dec_tmpref() - decrement thread->tmp_ref |
| 1946 | * @thread: thread to decrement |
| 1947 | * |
| 1948 | * A thread needs to be kept alive while being used to create or |
| 1949 | * handle a transaction. binder_get_txn_from() is used to safely |
| 1950 | * extract t->from from a binder_transaction and keep the thread |
| 1951 | * indicated by t->from from being freed. When done with that |
| 1952 | * binder_thread, this function is called to decrement the |
| 1953 | * tmp_ref and free if appropriate (thread has been released |
| 1954 | * and no transaction being processed by the driver) |
| 1955 | */ |
| 1956 | static void binder_thread_dec_tmpref(struct binder_thread *thread) |
| 1957 | { |
| 1958 | /* |
| 1959 | * atomic is used to protect the counter value while |
| 1960 | * it cannot reach zero or thread->is_dead is false |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1961 | */ |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 1962 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1963 | atomic_dec(&thread->tmp_ref); |
| 1964 | if (thread->is_dead && !atomic_read(&thread->tmp_ref)) { |
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 | binder_free_thread(thread); |
| 1967 | return; |
| 1968 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 1969 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | /** |
| 1973 | * binder_proc_dec_tmpref() - decrement proc->tmp_ref |
| 1974 | * @proc: proc to decrement |
| 1975 | * |
| 1976 | * A binder_proc needs to be kept alive while being used to create or |
| 1977 | * handle a transaction. proc->tmp_ref is incremented when |
| 1978 | * creating a new transaction or the binder_proc is currently in-use |
| 1979 | * by threads that are being released. When done with the binder_proc, |
| 1980 | * this function is called to decrement the counter and free the |
| 1981 | * proc if appropriate (proc has been released, all threads have |
| 1982 | * been released and not currenly in-use to process a transaction). |
| 1983 | */ |
| 1984 | static void binder_proc_dec_tmpref(struct binder_proc *proc) |
| 1985 | { |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 1986 | binder_inner_proc_lock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1987 | proc->tmp_ref--; |
| 1988 | if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) && |
| 1989 | !proc->tmp_ref) { |
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 | binder_free_proc(proc); |
| 1992 | return; |
| 1993 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 1994 | binder_inner_proc_unlock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | /** |
| 1998 | * binder_get_txn_from() - safely extract the "from" thread in transaction |
| 1999 | * @t: binder transaction for t->from |
| 2000 | * |
| 2001 | * Atomically return the "from" thread and increment the tmp_ref |
| 2002 | * count for the thread to ensure it stays alive until |
| 2003 | * binder_thread_dec_tmpref() is called. |
| 2004 | * |
| 2005 | * Return: the value of t->from |
| 2006 | */ |
| 2007 | static struct binder_thread *binder_get_txn_from( |
| 2008 | struct binder_transaction *t) |
| 2009 | { |
| 2010 | struct binder_thread *from; |
| 2011 | |
| 2012 | spin_lock(&t->lock); |
| 2013 | from = t->from; |
| 2014 | if (from) |
| 2015 | atomic_inc(&from->tmp_ref); |
| 2016 | spin_unlock(&t->lock); |
| 2017 | return from; |
| 2018 | } |
| 2019 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2020 | /** |
| 2021 | * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock |
| 2022 | * @t: binder transaction for t->from |
| 2023 | * |
| 2024 | * Same as binder_get_txn_from() except it also acquires the proc->inner_lock |
| 2025 | * to guarantee that the thread cannot be released while operating on it. |
| 2026 | * The caller must call binder_inner_proc_unlock() to release the inner lock |
| 2027 | * as well as call binder_dec_thread_txn() to release the reference. |
| 2028 | * |
| 2029 | * Return: the value of t->from |
| 2030 | */ |
| 2031 | static struct binder_thread *binder_get_txn_from_and_acq_inner( |
| 2032 | struct binder_transaction *t) |
| 2033 | { |
| 2034 | struct binder_thread *from; |
| 2035 | |
| 2036 | from = binder_get_txn_from(t); |
| 2037 | if (!from) |
| 2038 | return NULL; |
| 2039 | binder_inner_proc_lock(from->proc); |
| 2040 | if (t->from) { |
| 2041 | BUG_ON(from != t->from); |
| 2042 | return from; |
| 2043 | } |
| 2044 | binder_inner_proc_unlock(from->proc); |
| 2045 | binder_thread_dec_tmpref(from); |
| 2046 | return NULL; |
| 2047 | } |
| 2048 | |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2049 | static void binder_free_transaction(struct binder_transaction *t) |
| 2050 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2051 | if (t->buffer) |
| 2052 | t->buffer->transaction = NULL; |
| 2053 | kfree(t); |
| 2054 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 2055 | } |
| 2056 | |
| 2057 | static void binder_send_failed_reply(struct binder_transaction *t, |
| 2058 | uint32_t error_code) |
| 2059 | { |
| 2060 | struct binder_thread *target_thread; |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2061 | struct binder_transaction *next; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2062 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2063 | BUG_ON(t->flags & TF_ONE_WAY); |
| 2064 | while (1) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2065 | target_thread = binder_get_txn_from_and_acq_inner(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2066 | if (target_thread) { |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2067 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2068 | "send failed reply for transaction %d to %d:%d\n", |
| 2069 | t->debug_id, |
| 2070 | target_thread->proc->pid, |
| 2071 | target_thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2072 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2073 | binder_pop_transaction_ilocked(target_thread, t); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2074 | if (target_thread->reply_error.cmd == BR_OK) { |
| 2075 | target_thread->reply_error.cmd = error_code; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2076 | binder_enqueue_work_ilocked( |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 2077 | &target_thread->reply_error.work, |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2078 | &target_thread->todo); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2079 | wake_up_interruptible(&target_thread->wait); |
| 2080 | } else { |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2081 | WARN(1, "Unexpected reply error: %u\n", |
| 2082 | target_thread->reply_error.cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2083 | } |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2084 | binder_inner_proc_unlock(target_thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2085 | binder_thread_dec_tmpref(target_thread); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2086 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2087 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2088 | } |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2089 | next = t->from_parent; |
| 2090 | |
| 2091 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2092 | "send failed reply for transaction %d, target dead\n", |
| 2093 | t->debug_id); |
| 2094 | |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2095 | binder_free_transaction(t); |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2096 | if (next == NULL) { |
| 2097 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2098 | "reply failed, no target thread at root\n"); |
| 2099 | return; |
| 2100 | } |
| 2101 | t = next; |
| 2102 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2103 | "reply failed, no target thread -- retry %d\n", |
| 2104 | t->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2105 | } |
| 2106 | } |
| 2107 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2108 | /** |
Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 2109 | * binder_cleanup_transaction() - cleans up undelivered transaction |
| 2110 | * @t: transaction that needs to be cleaned up |
| 2111 | * @reason: reason the transaction wasn't delivered |
| 2112 | * @error_code: error to return to caller (if synchronous call) |
| 2113 | */ |
| 2114 | static void binder_cleanup_transaction(struct binder_transaction *t, |
| 2115 | const char *reason, |
| 2116 | uint32_t error_code) |
| 2117 | { |
| 2118 | if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) { |
| 2119 | binder_send_failed_reply(t, error_code); |
| 2120 | } else { |
| 2121 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 2122 | "undelivered transaction %d, %s\n", |
| 2123 | t->debug_id, reason); |
| 2124 | binder_free_transaction(t); |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | /** |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2129 | * binder_validate_object() - checks for a valid metadata object in a buffer. |
| 2130 | * @buffer: binder_buffer that we're parsing. |
| 2131 | * @offset: offset in the buffer at which to validate an object. |
| 2132 | * |
| 2133 | * Return: If there's a valid metadata object at @offset in @buffer, the |
| 2134 | * size of that object. Otherwise, it returns zero. |
| 2135 | */ |
| 2136 | static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset) |
| 2137 | { |
| 2138 | /* Check if we can read a header first */ |
| 2139 | struct binder_object_header *hdr; |
| 2140 | size_t object_size = 0; |
| 2141 | |
| 2142 | if (offset > buffer->data_size - sizeof(*hdr) || |
| 2143 | buffer->data_size < sizeof(*hdr) || |
| 2144 | !IS_ALIGNED(offset, sizeof(u32))) |
| 2145 | return 0; |
| 2146 | |
| 2147 | /* Ok, now see if we can read a complete object. */ |
| 2148 | hdr = (struct binder_object_header *)(buffer->data + offset); |
| 2149 | switch (hdr->type) { |
| 2150 | case BINDER_TYPE_BINDER: |
| 2151 | case BINDER_TYPE_WEAK_BINDER: |
| 2152 | case BINDER_TYPE_HANDLE: |
| 2153 | case BINDER_TYPE_WEAK_HANDLE: |
| 2154 | object_size = sizeof(struct flat_binder_object); |
| 2155 | break; |
| 2156 | case BINDER_TYPE_FD: |
| 2157 | object_size = sizeof(struct binder_fd_object); |
| 2158 | break; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2159 | case BINDER_TYPE_PTR: |
| 2160 | object_size = sizeof(struct binder_buffer_object); |
| 2161 | break; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2162 | case BINDER_TYPE_FDA: |
| 2163 | object_size = sizeof(struct binder_fd_array_object); |
| 2164 | break; |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2165 | default: |
| 2166 | return 0; |
| 2167 | } |
| 2168 | if (offset <= buffer->data_size - object_size && |
| 2169 | buffer->data_size >= object_size) |
| 2170 | return object_size; |
| 2171 | else |
| 2172 | return 0; |
| 2173 | } |
| 2174 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2175 | /** |
| 2176 | * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer. |
| 2177 | * @b: binder_buffer containing the object |
| 2178 | * @index: index in offset array at which the binder_buffer_object is |
| 2179 | * located |
| 2180 | * @start: points to the start of the offset array |
| 2181 | * @num_valid: the number of valid offsets in the offset array |
| 2182 | * |
| 2183 | * Return: If @index is within the valid range of the offset array |
| 2184 | * described by @start and @num_valid, and if there's a valid |
| 2185 | * binder_buffer_object at the offset found in index @index |
| 2186 | * of the offset array, that object is returned. Otherwise, |
| 2187 | * %NULL is returned. |
| 2188 | * Note that the offset found in index @index itself is not |
| 2189 | * verified; this function assumes that @num_valid elements |
| 2190 | * from @start were previously verified to have valid offsets. |
| 2191 | */ |
| 2192 | static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b, |
| 2193 | binder_size_t index, |
| 2194 | binder_size_t *start, |
| 2195 | binder_size_t num_valid) |
| 2196 | { |
| 2197 | struct binder_buffer_object *buffer_obj; |
| 2198 | binder_size_t *offp; |
| 2199 | |
| 2200 | if (index >= num_valid) |
| 2201 | return NULL; |
| 2202 | |
| 2203 | offp = start + index; |
| 2204 | buffer_obj = (struct binder_buffer_object *)(b->data + *offp); |
| 2205 | if (buffer_obj->hdr.type != BINDER_TYPE_PTR) |
| 2206 | return NULL; |
| 2207 | |
| 2208 | return buffer_obj; |
| 2209 | } |
| 2210 | |
| 2211 | /** |
| 2212 | * binder_validate_fixup() - validates pointer/fd fixups happen in order. |
| 2213 | * @b: transaction buffer |
| 2214 | * @objects_start start of objects buffer |
| 2215 | * @buffer: binder_buffer_object in which to fix up |
| 2216 | * @offset: start offset in @buffer to fix up |
| 2217 | * @last_obj: last binder_buffer_object that we fixed up in |
| 2218 | * @last_min_offset: minimum fixup offset in @last_obj |
| 2219 | * |
| 2220 | * Return: %true if a fixup in buffer @buffer at offset @offset is |
| 2221 | * allowed. |
| 2222 | * |
| 2223 | * For safety reasons, we only allow fixups inside a buffer to happen |
| 2224 | * at increasing offsets; additionally, we only allow fixup on the last |
| 2225 | * buffer object that was verified, or one of its parents. |
| 2226 | * |
| 2227 | * Example of what is allowed: |
| 2228 | * |
| 2229 | * A |
| 2230 | * B (parent = A, offset = 0) |
| 2231 | * C (parent = A, offset = 16) |
| 2232 | * D (parent = C, offset = 0) |
| 2233 | * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset) |
| 2234 | * |
| 2235 | * Examples of what is not allowed: |
| 2236 | * |
| 2237 | * Decreasing offsets within the same parent: |
| 2238 | * A |
| 2239 | * C (parent = A, offset = 16) |
| 2240 | * B (parent = A, offset = 0) // decreasing offset within A |
| 2241 | * |
| 2242 | * Referring to a parent that wasn't the last object or any of its parents: |
| 2243 | * A |
| 2244 | * B (parent = A, offset = 0) |
| 2245 | * C (parent = A, offset = 0) |
| 2246 | * C (parent = A, offset = 16) |
| 2247 | * D (parent = B, offset = 0) // B is not A or any of A's parents |
| 2248 | */ |
| 2249 | static bool binder_validate_fixup(struct binder_buffer *b, |
| 2250 | binder_size_t *objects_start, |
| 2251 | struct binder_buffer_object *buffer, |
| 2252 | binder_size_t fixup_offset, |
| 2253 | struct binder_buffer_object *last_obj, |
| 2254 | binder_size_t last_min_offset) |
| 2255 | { |
| 2256 | if (!last_obj) { |
| 2257 | /* Nothing to fix up in */ |
| 2258 | return false; |
| 2259 | } |
| 2260 | |
| 2261 | while (last_obj != buffer) { |
| 2262 | /* |
| 2263 | * Safe to retrieve the parent of last_obj, since it |
| 2264 | * was already previously verified by the driver. |
| 2265 | */ |
| 2266 | if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0) |
| 2267 | return false; |
| 2268 | last_min_offset = last_obj->parent_offset + sizeof(uintptr_t); |
| 2269 | last_obj = (struct binder_buffer_object *) |
| 2270 | (b->data + *(objects_start + last_obj->parent)); |
| 2271 | } |
| 2272 | return (fixup_offset >= last_min_offset); |
| 2273 | } |
| 2274 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2275 | static void binder_transaction_buffer_release(struct binder_proc *proc, |
| 2276 | struct binder_buffer *buffer, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2277 | binder_size_t *failed_at) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2278 | { |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2279 | binder_size_t *offp, *off_start, *off_end; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2280 | int debug_id = buffer->debug_id; |
| 2281 | |
| 2282 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2283 | "%d buffer release %d, size %zd-%zd, failed at %p\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2284 | proc->pid, buffer->debug_id, |
| 2285 | buffer->data_size, buffer->offsets_size, failed_at); |
| 2286 | |
| 2287 | if (buffer->target_node) |
| 2288 | binder_dec_node(buffer->target_node, 1, 0); |
| 2289 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2290 | off_start = (binder_size_t *)(buffer->data + |
| 2291 | ALIGN(buffer->data_size, sizeof(void *))); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2292 | if (failed_at) |
| 2293 | off_end = failed_at; |
| 2294 | else |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2295 | off_end = (void *)off_start + buffer->offsets_size; |
| 2296 | for (offp = off_start; offp < off_end; offp++) { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2297 | struct binder_object_header *hdr; |
| 2298 | size_t object_size = binder_validate_object(buffer, *offp); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2299 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2300 | if (object_size == 0) { |
| 2301 | 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] | 2302 | debug_id, (u64)*offp, buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2303 | continue; |
| 2304 | } |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2305 | hdr = (struct binder_object_header *)(buffer->data + *offp); |
| 2306 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2307 | case BINDER_TYPE_BINDER: |
| 2308 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2309 | struct flat_binder_object *fp; |
| 2310 | struct binder_node *node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2311 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2312 | fp = to_flat_binder_object(hdr); |
| 2313 | node = binder_get_node(proc, fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2314 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2315 | pr_err("transaction release %d bad node %016llx\n", |
| 2316 | debug_id, (u64)fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2317 | break; |
| 2318 | } |
| 2319 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2320 | " node %d u%016llx\n", |
| 2321 | node->debug_id, (u64)node->ptr); |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2322 | binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER, |
| 2323 | 0); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2324 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2325 | } break; |
| 2326 | case BINDER_TYPE_HANDLE: |
| 2327 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2328 | struct flat_binder_object *fp; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2329 | struct binder_ref_data rdata; |
| 2330 | int ret; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 2331 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2332 | fp = to_flat_binder_object(hdr); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2333 | ret = binder_dec_ref_for_handle(proc, fp->handle, |
| 2334 | hdr->type == BINDER_TYPE_HANDLE, &rdata); |
| 2335 | |
| 2336 | if (ret) { |
| 2337 | pr_err("transaction release %d bad handle %d, ret = %d\n", |
| 2338 | debug_id, fp->handle, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2339 | break; |
| 2340 | } |
| 2341 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2342 | " ref %d desc %d\n", |
| 2343 | rdata.debug_id, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2344 | } break; |
| 2345 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2346 | case BINDER_TYPE_FD: { |
| 2347 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
| 2348 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2349 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2350 | " fd %d\n", fp->fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2351 | if (failed_at) |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2352 | task_close_fd(proc, fp->fd); |
| 2353 | } break; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2354 | case BINDER_TYPE_PTR: |
| 2355 | /* |
| 2356 | * Nothing to do here, this will get cleaned up when the |
| 2357 | * transaction buffer gets freed |
| 2358 | */ |
| 2359 | break; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2360 | case BINDER_TYPE_FDA: { |
| 2361 | struct binder_fd_array_object *fda; |
| 2362 | struct binder_buffer_object *parent; |
| 2363 | uintptr_t parent_buffer; |
| 2364 | u32 *fd_array; |
| 2365 | size_t fd_index; |
| 2366 | binder_size_t fd_buf_size; |
| 2367 | |
| 2368 | fda = to_binder_fd_array_object(hdr); |
| 2369 | parent = binder_validate_ptr(buffer, fda->parent, |
| 2370 | off_start, |
| 2371 | offp - off_start); |
| 2372 | if (!parent) { |
| 2373 | pr_err("transaction release %d bad parent offset", |
| 2374 | debug_id); |
| 2375 | continue; |
| 2376 | } |
| 2377 | /* |
| 2378 | * Since the parent was already fixed up, convert it |
| 2379 | * back to kernel address space to access it |
| 2380 | */ |
| 2381 | parent_buffer = parent->buffer - |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 2382 | binder_alloc_get_user_buffer_offset( |
| 2383 | &proc->alloc); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2384 | |
| 2385 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2386 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2387 | pr_err("transaction release %d invalid number of fds (%lld)\n", |
| 2388 | debug_id, (u64)fda->num_fds); |
| 2389 | continue; |
| 2390 | } |
| 2391 | if (fd_buf_size > parent->length || |
| 2392 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2393 | /* No space for all file descriptors here. */ |
| 2394 | pr_err("transaction release %d not enough space for %lld fds in buffer\n", |
| 2395 | debug_id, (u64)fda->num_fds); |
| 2396 | continue; |
| 2397 | } |
| 2398 | fd_array = (u32 *)(parent_buffer + fda->parent_offset); |
| 2399 | for (fd_index = 0; fd_index < fda->num_fds; fd_index++) |
| 2400 | task_close_fd(proc, fd_array[fd_index]); |
| 2401 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2402 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 2403 | pr_err("transaction release %d bad object type %x\n", |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2404 | debug_id, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2405 | break; |
| 2406 | } |
| 2407 | } |
| 2408 | } |
| 2409 | |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2410 | static int binder_translate_binder(struct flat_binder_object *fp, |
| 2411 | struct binder_transaction *t, |
| 2412 | struct binder_thread *thread) |
| 2413 | { |
| 2414 | struct binder_node *node; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2415 | struct binder_proc *proc = thread->proc; |
| 2416 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2417 | struct binder_ref_data rdata; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2418 | int ret = 0; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2419 | |
| 2420 | node = binder_get_node(proc, fp->binder); |
| 2421 | if (!node) { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2422 | node = binder_new_node(proc, fp); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2423 | if (!node) |
| 2424 | return -ENOMEM; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2425 | } |
| 2426 | if (fp->cookie != node->cookie) { |
| 2427 | binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n", |
| 2428 | proc->pid, thread->pid, (u64)fp->binder, |
| 2429 | node->debug_id, (u64)fp->cookie, |
| 2430 | (u64)node->cookie); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2431 | ret = -EINVAL; |
| 2432 | goto done; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2433 | } |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2434 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2435 | ret = -EPERM; |
| 2436 | goto done; |
| 2437 | } |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2438 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2439 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2440 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2441 | &thread->todo, &rdata); |
| 2442 | if (ret) |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2443 | goto done; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2444 | |
| 2445 | if (fp->hdr.type == BINDER_TYPE_BINDER) |
| 2446 | fp->hdr.type = BINDER_TYPE_HANDLE; |
| 2447 | else |
| 2448 | fp->hdr.type = BINDER_TYPE_WEAK_HANDLE; |
| 2449 | fp->binder = 0; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2450 | fp->handle = rdata.desc; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2451 | fp->cookie = 0; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2452 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2453 | trace_binder_transaction_node_to_ref(t, node, &rdata); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2454 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2455 | " node %d u%016llx -> ref %d desc %d\n", |
| 2456 | node->debug_id, (u64)node->ptr, |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2457 | rdata.debug_id, rdata.desc); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2458 | done: |
| 2459 | binder_put_node(node); |
| 2460 | return ret; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | static int binder_translate_handle(struct flat_binder_object *fp, |
| 2464 | struct binder_transaction *t, |
| 2465 | struct binder_thread *thread) |
| 2466 | { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2467 | struct binder_proc *proc = thread->proc; |
| 2468 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2469 | struct binder_node *node; |
| 2470 | struct binder_ref_data src_rdata; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2471 | int ret = 0; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2472 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2473 | node = binder_get_node_from_ref(proc, fp->handle, |
| 2474 | fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata); |
| 2475 | if (!node) { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2476 | binder_user_error("%d:%d got transaction with invalid handle, %d\n", |
| 2477 | proc->pid, thread->pid, fp->handle); |
| 2478 | return -EINVAL; |
| 2479 | } |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2480 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2481 | ret = -EPERM; |
| 2482 | goto done; |
| 2483 | } |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2484 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2485 | binder_node_lock(node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2486 | if (node->proc == target_proc) { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2487 | if (fp->hdr.type == BINDER_TYPE_HANDLE) |
| 2488 | fp->hdr.type = BINDER_TYPE_BINDER; |
| 2489 | else |
| 2490 | fp->hdr.type = BINDER_TYPE_WEAK_BINDER; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2491 | fp->binder = node->ptr; |
| 2492 | fp->cookie = node->cookie; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2493 | if (node->proc) |
| 2494 | binder_inner_proc_lock(node->proc); |
| 2495 | binder_inc_node_nilocked(node, |
| 2496 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2497 | 0, NULL); |
| 2498 | if (node->proc) |
| 2499 | binder_inner_proc_unlock(node->proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2500 | trace_binder_transaction_ref_to_node(t, node, &src_rdata); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2501 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2502 | " ref %d desc %d -> node %d u%016llx\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2503 | src_rdata.debug_id, src_rdata.desc, node->debug_id, |
| 2504 | (u64)node->ptr); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2505 | binder_node_unlock(node); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2506 | } else { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2507 | struct binder_ref_data dest_rdata; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2508 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2509 | binder_node_unlock(node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2510 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2511 | fp->hdr.type == BINDER_TYPE_HANDLE, |
| 2512 | NULL, &dest_rdata); |
| 2513 | if (ret) |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2514 | goto done; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2515 | |
| 2516 | fp->binder = 0; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2517 | fp->handle = dest_rdata.desc; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2518 | fp->cookie = 0; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2519 | trace_binder_transaction_ref_to_ref(t, node, &src_rdata, |
| 2520 | &dest_rdata); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2521 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2522 | " ref %d desc %d -> ref %d desc %d (node %d)\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2523 | src_rdata.debug_id, src_rdata.desc, |
| 2524 | dest_rdata.debug_id, dest_rdata.desc, |
| 2525 | node->debug_id); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2526 | } |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2527 | done: |
| 2528 | binder_put_node(node); |
| 2529 | return ret; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | static int binder_translate_fd(int fd, |
| 2533 | struct binder_transaction *t, |
| 2534 | struct binder_thread *thread, |
| 2535 | struct binder_transaction *in_reply_to) |
| 2536 | { |
| 2537 | struct binder_proc *proc = thread->proc; |
| 2538 | struct binder_proc *target_proc = t->to_proc; |
| 2539 | int target_fd; |
| 2540 | struct file *file; |
| 2541 | int ret; |
| 2542 | bool target_allows_fd; |
| 2543 | |
| 2544 | if (in_reply_to) |
| 2545 | target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS); |
| 2546 | else |
| 2547 | target_allows_fd = t->buffer->target_node->accept_fds; |
| 2548 | if (!target_allows_fd) { |
| 2549 | binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n", |
| 2550 | proc->pid, thread->pid, |
| 2551 | in_reply_to ? "reply" : "transaction", |
| 2552 | fd); |
| 2553 | ret = -EPERM; |
| 2554 | goto err_fd_not_accepted; |
| 2555 | } |
| 2556 | |
| 2557 | file = fget(fd); |
| 2558 | if (!file) { |
| 2559 | binder_user_error("%d:%d got transaction with invalid fd, %d\n", |
| 2560 | proc->pid, thread->pid, fd); |
| 2561 | ret = -EBADF; |
| 2562 | goto err_fget; |
| 2563 | } |
| 2564 | ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file); |
| 2565 | if (ret < 0) { |
| 2566 | ret = -EPERM; |
| 2567 | goto err_security; |
| 2568 | } |
| 2569 | |
| 2570 | target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC); |
| 2571 | if (target_fd < 0) { |
| 2572 | ret = -ENOMEM; |
| 2573 | goto err_get_unused_fd; |
| 2574 | } |
| 2575 | task_fd_install(target_proc, target_fd, file); |
| 2576 | trace_binder_transaction_fd(t, fd, target_fd); |
| 2577 | binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n", |
| 2578 | fd, target_fd); |
| 2579 | |
| 2580 | return target_fd; |
| 2581 | |
| 2582 | err_get_unused_fd: |
| 2583 | err_security: |
| 2584 | fput(file); |
| 2585 | err_fget: |
| 2586 | err_fd_not_accepted: |
| 2587 | return ret; |
| 2588 | } |
| 2589 | |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2590 | static int binder_translate_fd_array(struct binder_fd_array_object *fda, |
| 2591 | struct binder_buffer_object *parent, |
| 2592 | struct binder_transaction *t, |
| 2593 | struct binder_thread *thread, |
| 2594 | struct binder_transaction *in_reply_to) |
| 2595 | { |
| 2596 | binder_size_t fdi, fd_buf_size, num_installed_fds; |
| 2597 | int target_fd; |
| 2598 | uintptr_t parent_buffer; |
| 2599 | u32 *fd_array; |
| 2600 | struct binder_proc *proc = thread->proc; |
| 2601 | struct binder_proc *target_proc = t->to_proc; |
| 2602 | |
| 2603 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2604 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2605 | binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n", |
| 2606 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2607 | return -EINVAL; |
| 2608 | } |
| 2609 | if (fd_buf_size > parent->length || |
| 2610 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2611 | /* No space for all file descriptors here. */ |
| 2612 | binder_user_error("%d:%d not enough space to store %lld fds in buffer\n", |
| 2613 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2614 | return -EINVAL; |
| 2615 | } |
| 2616 | /* |
| 2617 | * Since the parent was already fixed up, convert it |
| 2618 | * back to the kernel address space to access it |
| 2619 | */ |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 2620 | parent_buffer = parent->buffer - |
| 2621 | binder_alloc_get_user_buffer_offset(&target_proc->alloc); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2622 | fd_array = (u32 *)(parent_buffer + fda->parent_offset); |
| 2623 | if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) { |
| 2624 | binder_user_error("%d:%d parent offset not aligned correctly.\n", |
| 2625 | proc->pid, thread->pid); |
| 2626 | return -EINVAL; |
| 2627 | } |
| 2628 | for (fdi = 0; fdi < fda->num_fds; fdi++) { |
| 2629 | target_fd = binder_translate_fd(fd_array[fdi], t, thread, |
| 2630 | in_reply_to); |
| 2631 | if (target_fd < 0) |
| 2632 | goto err_translate_fd_failed; |
| 2633 | fd_array[fdi] = target_fd; |
| 2634 | } |
| 2635 | return 0; |
| 2636 | |
| 2637 | err_translate_fd_failed: |
| 2638 | /* |
| 2639 | * Failed to allocate fd or security error, free fds |
| 2640 | * installed so far. |
| 2641 | */ |
| 2642 | num_installed_fds = fdi; |
| 2643 | for (fdi = 0; fdi < num_installed_fds; fdi++) |
| 2644 | task_close_fd(target_proc, fd_array[fdi]); |
| 2645 | return target_fd; |
| 2646 | } |
| 2647 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2648 | static int binder_fixup_parent(struct binder_transaction *t, |
| 2649 | struct binder_thread *thread, |
| 2650 | struct binder_buffer_object *bp, |
| 2651 | binder_size_t *off_start, |
| 2652 | binder_size_t num_valid, |
| 2653 | struct binder_buffer_object *last_fixup_obj, |
| 2654 | binder_size_t last_fixup_min_off) |
| 2655 | { |
| 2656 | struct binder_buffer_object *parent; |
| 2657 | u8 *parent_buffer; |
| 2658 | struct binder_buffer *b = t->buffer; |
| 2659 | struct binder_proc *proc = thread->proc; |
| 2660 | struct binder_proc *target_proc = t->to_proc; |
| 2661 | |
| 2662 | if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT)) |
| 2663 | return 0; |
| 2664 | |
| 2665 | parent = binder_validate_ptr(b, bp->parent, off_start, num_valid); |
| 2666 | if (!parent) { |
| 2667 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 2668 | proc->pid, thread->pid); |
| 2669 | return -EINVAL; |
| 2670 | } |
| 2671 | |
| 2672 | if (!binder_validate_fixup(b, off_start, |
| 2673 | parent, bp->parent_offset, |
| 2674 | last_fixup_obj, |
| 2675 | last_fixup_min_off)) { |
| 2676 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 2677 | proc->pid, thread->pid); |
| 2678 | return -EINVAL; |
| 2679 | } |
| 2680 | |
| 2681 | if (parent->length < sizeof(binder_uintptr_t) || |
| 2682 | bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) { |
| 2683 | /* No space for a pointer here! */ |
| 2684 | binder_user_error("%d:%d got transaction with invalid parent offset\n", |
| 2685 | proc->pid, thread->pid); |
| 2686 | return -EINVAL; |
| 2687 | } |
| 2688 | parent_buffer = (u8 *)(parent->buffer - |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 2689 | binder_alloc_get_user_buffer_offset( |
| 2690 | &target_proc->alloc)); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2691 | *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer; |
| 2692 | |
| 2693 | return 0; |
| 2694 | } |
| 2695 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2696 | /** |
| 2697 | * binder_proc_transaction() - sends a transaction to a process and wakes it up |
| 2698 | * @t: transaction to send |
| 2699 | * @proc: process to send the transaction to |
| 2700 | * @thread: thread in @proc to send the transaction to (may be NULL) |
| 2701 | * |
| 2702 | * This function queues a transaction to the specified process. It will try |
| 2703 | * to find a thread in the target process to handle the transaction and |
| 2704 | * wake it up. If no thread is found, the work is queued to the proc |
| 2705 | * waitqueue. |
| 2706 | * |
| 2707 | * If the @thread parameter is not NULL, the transaction is always queued |
| 2708 | * to the waitlist of that specific thread. |
| 2709 | * |
| 2710 | * Return: true if the transactions was successfully queued |
| 2711 | * false if the target process or thread is dead |
| 2712 | */ |
| 2713 | static bool binder_proc_transaction(struct binder_transaction *t, |
| 2714 | struct binder_proc *proc, |
| 2715 | struct binder_thread *thread) |
| 2716 | { |
| 2717 | struct list_head *target_list = NULL; |
| 2718 | struct binder_node *node = t->buffer->target_node; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2719 | struct binder_priority node_prio; |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2720 | bool oneway = !!(t->flags & TF_ONE_WAY); |
| 2721 | bool wakeup = true; |
| 2722 | |
| 2723 | BUG_ON(!node); |
| 2724 | binder_node_lock(node); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2725 | node_prio.prio = node->min_priority; |
| 2726 | node_prio.sched_policy = node->sched_policy; |
| 2727 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2728 | if (oneway) { |
| 2729 | BUG_ON(thread); |
| 2730 | if (node->has_async_transaction) { |
| 2731 | target_list = &node->async_todo; |
| 2732 | wakeup = false; |
| 2733 | } else { |
| 2734 | node->has_async_transaction = 1; |
| 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | binder_inner_proc_lock(proc); |
| 2739 | |
| 2740 | if (proc->is_dead || (thread && thread->is_dead)) { |
| 2741 | binder_inner_proc_unlock(proc); |
| 2742 | binder_node_unlock(node); |
| 2743 | return false; |
| 2744 | } |
| 2745 | |
| 2746 | if (!thread && !target_list) |
| 2747 | thread = binder_select_thread_ilocked(proc); |
| 2748 | |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2749 | if (thread) { |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2750 | target_list = &thread->todo; |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 2751 | binder_transaction_priority(thread->task, t, node_prio, |
| 2752 | node->inherit_rt); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2753 | } else if (!target_list) { |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2754 | target_list = &proc->todo; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2755 | } else { |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2756 | BUG_ON(target_list != &node->async_todo); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2757 | } |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2758 | |
| 2759 | binder_enqueue_work_ilocked(&t->work, target_list); |
| 2760 | |
| 2761 | if (wakeup) |
| 2762 | binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */); |
| 2763 | |
| 2764 | binder_inner_proc_unlock(proc); |
| 2765 | binder_node_unlock(node); |
| 2766 | |
| 2767 | return true; |
| 2768 | } |
| 2769 | |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 2770 | /** |
| 2771 | * binder_get_node_refs_for_txn() - Get required refs on node for txn |
| 2772 | * @node: struct binder_node for which to get refs |
| 2773 | * @proc: returns @node->proc if valid |
| 2774 | * @error: if no @proc then returns BR_DEAD_REPLY |
| 2775 | * |
| 2776 | * User-space normally keeps the node alive when creating a transaction |
| 2777 | * since it has a reference to the target. The local strong ref keeps it |
| 2778 | * alive if the sending process dies before the target process processes |
| 2779 | * the transaction. If the source process is malicious or has a reference |
| 2780 | * counting bug, relying on the local strong ref can fail. |
| 2781 | * |
| 2782 | * Since user-space can cause the local strong ref to go away, we also take |
| 2783 | * a tmpref on the node to ensure it survives while we are constructing |
| 2784 | * the transaction. We also need a tmpref on the proc while we are |
| 2785 | * constructing the transaction, so we take that here as well. |
| 2786 | * |
| 2787 | * Return: The target_node with refs taken or NULL if no @node->proc is NULL. |
| 2788 | * Also sets @proc if valid. If the @node->proc is NULL indicating that the |
| 2789 | * target proc has died, @error is set to BR_DEAD_REPLY |
| 2790 | */ |
| 2791 | static struct binder_node *binder_get_node_refs_for_txn( |
| 2792 | struct binder_node *node, |
| 2793 | struct binder_proc **procp, |
| 2794 | uint32_t *error) |
| 2795 | { |
| 2796 | struct binder_node *target_node = NULL; |
| 2797 | |
| 2798 | binder_node_inner_lock(node); |
| 2799 | if (node->proc) { |
| 2800 | target_node = node; |
| 2801 | binder_inc_node_nilocked(node, 1, 0, NULL); |
| 2802 | binder_inc_node_tmpref_ilocked(node); |
| 2803 | node->proc->tmp_ref++; |
| 2804 | *procp = node->proc; |
| 2805 | } else |
| 2806 | *error = BR_DEAD_REPLY; |
| 2807 | binder_node_inner_unlock(node); |
| 2808 | |
| 2809 | return target_node; |
| 2810 | } |
| 2811 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2812 | static void binder_transaction(struct binder_proc *proc, |
| 2813 | struct binder_thread *thread, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 2814 | struct binder_transaction_data *tr, int reply, |
| 2815 | binder_size_t extra_buffers_size) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2816 | { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2817 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2818 | struct binder_transaction *t; |
| 2819 | struct binder_work *tcomplete; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2820 | binder_size_t *offp, *off_end, *off_start; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 2821 | binder_size_t off_min; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2822 | u8 *sg_bufp, *sg_buf_end; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2823 | struct binder_proc *target_proc = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2824 | struct binder_thread *target_thread = NULL; |
| 2825 | struct binder_node *target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2826 | struct binder_transaction *in_reply_to = NULL; |
| 2827 | struct binder_transaction_log_entry *e; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2828 | uint32_t return_error = 0; |
| 2829 | uint32_t return_error_param = 0; |
| 2830 | uint32_t return_error_line = 0; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2831 | struct binder_buffer_object *last_fixup_obj = NULL; |
| 2832 | binder_size_t last_fixup_min_off = 0; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 2833 | struct binder_context *context = proc->context; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 2834 | int t_debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2835 | |
| 2836 | e = binder_transaction_log_add(&binder_transaction_log); |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 2837 | e->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2838 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); |
| 2839 | e->from_proc = proc->pid; |
| 2840 | e->from_thread = thread->pid; |
| 2841 | e->target_handle = tr->target.handle; |
| 2842 | e->data_size = tr->data_size; |
| 2843 | e->offsets_size = tr->offsets_size; |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 2844 | e->context_name = proc->context->name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2845 | |
| 2846 | if (reply) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2847 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2848 | in_reply_to = thread->transaction_stack; |
| 2849 | if (in_reply_to == NULL) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2850 | binder_inner_proc_unlock(proc); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2851 | 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] | 2852 | proc->pid, thread->pid); |
| 2853 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2854 | return_error_param = -EPROTO; |
| 2855 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2856 | goto err_empty_call_stack; |
| 2857 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2858 | if (in_reply_to->to_thread != thread) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2859 | spin_lock(&in_reply_to->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2860 | 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] | 2861 | proc->pid, thread->pid, in_reply_to->debug_id, |
| 2862 | in_reply_to->to_proc ? |
| 2863 | in_reply_to->to_proc->pid : 0, |
| 2864 | in_reply_to->to_thread ? |
| 2865 | in_reply_to->to_thread->pid : 0); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2866 | spin_unlock(&in_reply_to->lock); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2867 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2868 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2869 | return_error_param = -EPROTO; |
| 2870 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2871 | in_reply_to = NULL; |
| 2872 | goto err_bad_call_stack; |
| 2873 | } |
| 2874 | thread->transaction_stack = in_reply_to->to_parent; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2875 | binder_inner_proc_unlock(proc); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2876 | 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] | 2877 | if (target_thread == NULL) { |
| 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 | } |
| 2882 | if (target_thread->transaction_stack != in_reply_to) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2883 | 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] | 2884 | proc->pid, thread->pid, |
| 2885 | target_thread->transaction_stack ? |
| 2886 | target_thread->transaction_stack->debug_id : 0, |
| 2887 | in_reply_to->debug_id); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2888 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2889 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2890 | return_error_param = -EPROTO; |
| 2891 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2892 | in_reply_to = NULL; |
| 2893 | target_thread = NULL; |
| 2894 | goto err_dead_binder; |
| 2895 | } |
| 2896 | target_proc = target_thread->proc; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2897 | target_proc->tmp_ref++; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2898 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2899 | } else { |
| 2900 | if (tr->target.handle) { |
| 2901 | struct binder_ref *ref; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2902 | |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 2903 | /* |
| 2904 | * There must already be a strong ref |
| 2905 | * on this node. If so, do a strong |
| 2906 | * increment on the node to ensure it |
| 2907 | * stays alive until the transaction is |
| 2908 | * done. |
| 2909 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2910 | binder_proc_lock(proc); |
| 2911 | ref = binder_get_ref_olocked(proc, tr->target.handle, |
| 2912 | true); |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 2913 | if (ref) { |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 2914 | target_node = binder_get_node_refs_for_txn( |
| 2915 | ref->node, &target_proc, |
| 2916 | &return_error); |
| 2917 | } else { |
| 2918 | binder_user_error("%d:%d got transaction to invalid handle\n", |
| 2919 | proc->pid, thread->pid); |
| 2920 | return_error = BR_FAILED_REPLY; |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 2921 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2922 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2923 | } else { |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 2924 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 2925 | target_node = context->binder_context_mgr_node; |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 2926 | if (target_node) |
| 2927 | target_node = binder_get_node_refs_for_txn( |
| 2928 | target_node, &target_proc, |
| 2929 | &return_error); |
| 2930 | else |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2931 | return_error = BR_DEAD_REPLY; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 2932 | mutex_unlock(&context->context_mgr_node_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2933 | } |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 2934 | if (!target_node) { |
| 2935 | /* |
| 2936 | * return_error is set above |
| 2937 | */ |
| 2938 | return_error_param = -EINVAL; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2939 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2940 | goto err_dead_binder; |
| 2941 | } |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 2942 | e->to_node = target_node->debug_id; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2943 | if (security_binder_transaction(proc->tsk, |
| 2944 | target_proc->tsk) < 0) { |
| 2945 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2946 | return_error_param = -EPERM; |
| 2947 | return_error_line = __LINE__; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2948 | goto err_invalid_target_handle; |
| 2949 | } |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2950 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2951 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { |
| 2952 | struct binder_transaction *tmp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2953 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2954 | tmp = thread->transaction_stack; |
| 2955 | if (tmp->to_thread != thread) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2956 | spin_lock(&tmp->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2957 | 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] | 2958 | proc->pid, thread->pid, tmp->debug_id, |
| 2959 | tmp->to_proc ? tmp->to_proc->pid : 0, |
| 2960 | tmp->to_thread ? |
| 2961 | tmp->to_thread->pid : 0); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2962 | spin_unlock(&tmp->lock); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2963 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2964 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2965 | return_error_param = -EPROTO; |
| 2966 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2967 | goto err_bad_call_stack; |
| 2968 | } |
| 2969 | while (tmp) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2970 | struct binder_thread *from; |
| 2971 | |
| 2972 | spin_lock(&tmp->lock); |
| 2973 | from = tmp->from; |
| 2974 | if (from && from->proc == target_proc) { |
| 2975 | atomic_inc(&from->tmp_ref); |
| 2976 | target_thread = from; |
| 2977 | spin_unlock(&tmp->lock); |
| 2978 | break; |
| 2979 | } |
| 2980 | spin_unlock(&tmp->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2981 | tmp = tmp->from_parent; |
| 2982 | } |
| 2983 | } |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2984 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2985 | } |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2986 | if (target_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2987 | e->to_thread = target_thread->pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2988 | e->to_proc = target_proc->pid; |
| 2989 | |
| 2990 | /* TODO: reuse incoming transaction for reply */ |
| 2991 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 2992 | if (t == NULL) { |
| 2993 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2994 | return_error_param = -ENOMEM; |
| 2995 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2996 | goto err_alloc_t_failed; |
| 2997 | } |
| 2998 | binder_stats_created(BINDER_STAT_TRANSACTION); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2999 | spin_lock_init(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3000 | |
| 3001 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); |
| 3002 | if (tcomplete == NULL) { |
| 3003 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3004 | return_error_param = -ENOMEM; |
| 3005 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3006 | goto err_alloc_tcomplete_failed; |
| 3007 | } |
| 3008 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3009 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3010 | t->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3011 | |
| 3012 | if (reply) |
| 3013 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3014 | "%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] | 3015 | proc->pid, thread->pid, t->debug_id, |
| 3016 | target_proc->pid, target_thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3017 | (u64)tr->data.ptr.buffer, |
| 3018 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3019 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3020 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3021 | else |
| 3022 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3023 | "%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] | 3024 | proc->pid, thread->pid, t->debug_id, |
| 3025 | target_proc->pid, target_node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3026 | (u64)tr->data.ptr.buffer, |
| 3027 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3028 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3029 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3030 | |
| 3031 | if (!reply && !(tr->flags & TF_ONE_WAY)) |
| 3032 | t->from = thread; |
| 3033 | else |
| 3034 | t->from = NULL; |
Tair Rzayev | 57bab7c | 2014-05-31 22:43:34 +0300 | [diff] [blame] | 3035 | t->sender_euid = task_euid(proc->tsk); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3036 | t->to_proc = target_proc; |
| 3037 | t->to_thread = target_thread; |
| 3038 | t->code = tr->code; |
| 3039 | t->flags = tr->flags; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 3040 | if (!(t->flags & TF_ONE_WAY) && |
| 3041 | binder_supported_policy(current->policy)) { |
| 3042 | /* Inherit supported policies for synchronous transactions */ |
| 3043 | t->priority.sched_policy = current->policy; |
| 3044 | t->priority.prio = current->normal_prio; |
| 3045 | } else { |
| 3046 | /* Otherwise, fall back to the default priority */ |
| 3047 | t->priority = target_proc->default_priority; |
| 3048 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3049 | |
| 3050 | trace_binder_transaction(reply, t, target_node); |
| 3051 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3052 | t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3053 | tr->offsets_size, extra_buffers_size, |
| 3054 | !reply && (t->flags & TF_ONE_WAY)); |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3055 | if (IS_ERR(t->buffer)) { |
| 3056 | /* |
| 3057 | * -ESRCH indicates VMA cleared. The target is dying. |
| 3058 | */ |
| 3059 | return_error_param = PTR_ERR(t->buffer); |
| 3060 | return_error = return_error_param == -ESRCH ? |
| 3061 | BR_DEAD_REPLY : BR_FAILED_REPLY; |
| 3062 | return_error_line = __LINE__; |
| 3063 | t->buffer = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3064 | goto err_binder_alloc_buf_failed; |
| 3065 | } |
| 3066 | t->buffer->allow_user_free = 0; |
| 3067 | t->buffer->debug_id = t->debug_id; |
| 3068 | t->buffer->transaction = t; |
| 3069 | t->buffer->target_node = target_node; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3070 | trace_binder_transaction_alloc_buf(t->buffer); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3071 | off_start = (binder_size_t *)(t->buffer->data + |
| 3072 | ALIGN(tr->data_size, sizeof(void *))); |
| 3073 | offp = off_start; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3074 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3075 | if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t) |
| 3076 | tr->data.ptr.buffer, tr->data_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3077 | binder_user_error("%d:%d got transaction with invalid data ptr\n", |
| 3078 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3079 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3080 | return_error_param = -EFAULT; |
| 3081 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3082 | goto err_copy_data_failed; |
| 3083 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3084 | if (copy_from_user(offp, (const void __user *)(uintptr_t) |
| 3085 | tr->data.ptr.offsets, tr->offsets_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3086 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3087 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3088 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3089 | return_error_param = -EFAULT; |
| 3090 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3091 | goto err_copy_data_failed; |
| 3092 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3093 | if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) { |
| 3094 | binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n", |
| 3095 | proc->pid, thread->pid, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3096 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3097 | return_error_param = -EINVAL; |
| 3098 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3099 | goto err_bad_offset; |
| 3100 | } |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3101 | if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) { |
| 3102 | binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n", |
| 3103 | proc->pid, thread->pid, |
Amit Pundir | 44cbb18 | 2017-02-01 12:53:45 +0530 | [diff] [blame] | 3104 | (u64)extra_buffers_size); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3105 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3106 | return_error_param = -EINVAL; |
| 3107 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3108 | goto err_bad_offset; |
| 3109 | } |
| 3110 | off_end = (void *)off_start + tr->offsets_size; |
| 3111 | sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *))); |
| 3112 | sg_buf_end = sg_bufp + extra_buffers_size; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3113 | off_min = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3114 | for (; offp < off_end; offp++) { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3115 | struct binder_object_header *hdr; |
| 3116 | size_t object_size = binder_validate_object(t->buffer, *offp); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3117 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3118 | if (object_size == 0 || *offp < off_min) { |
| 3119 | 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] | 3120 | proc->pid, thread->pid, (u64)*offp, |
| 3121 | (u64)off_min, |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3122 | (u64)t->buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3123 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3124 | return_error_param = -EINVAL; |
| 3125 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3126 | goto err_bad_offset; |
| 3127 | } |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3128 | |
| 3129 | hdr = (struct binder_object_header *)(t->buffer->data + *offp); |
| 3130 | off_min = *offp + object_size; |
| 3131 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3132 | case BINDER_TYPE_BINDER: |
| 3133 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3134 | struct flat_binder_object *fp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3135 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3136 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3137 | ret = binder_translate_binder(fp, t, thread); |
| 3138 | if (ret < 0) { |
Christian Engelmayer | 7d42043 | 2014-05-07 21:44:53 +0200 | [diff] [blame] | 3139 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3140 | return_error_param = ret; |
| 3141 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3142 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3143 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3144 | } break; |
| 3145 | case BINDER_TYPE_HANDLE: |
| 3146 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3147 | struct flat_binder_object *fp; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 3148 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3149 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3150 | ret = binder_translate_handle(fp, t, thread); |
| 3151 | if (ret < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3152 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3153 | return_error_param = ret; |
| 3154 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3155 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3156 | } |
| 3157 | } break; |
| 3158 | |
| 3159 | case BINDER_TYPE_FD: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3160 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3161 | int target_fd = binder_translate_fd(fp->fd, t, thread, |
| 3162 | in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3163 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3164 | if (target_fd < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3165 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3166 | return_error_param = target_fd; |
| 3167 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3168 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3169 | } |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3170 | fp->pad_binder = 0; |
| 3171 | fp->fd = target_fd; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3172 | } break; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3173 | case BINDER_TYPE_FDA: { |
| 3174 | struct binder_fd_array_object *fda = |
| 3175 | to_binder_fd_array_object(hdr); |
| 3176 | struct binder_buffer_object *parent = |
| 3177 | binder_validate_ptr(t->buffer, fda->parent, |
| 3178 | off_start, |
| 3179 | offp - off_start); |
| 3180 | if (!parent) { |
| 3181 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 3182 | proc->pid, thread->pid); |
| 3183 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3184 | return_error_param = -EINVAL; |
| 3185 | return_error_line = __LINE__; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3186 | goto err_bad_parent; |
| 3187 | } |
| 3188 | if (!binder_validate_fixup(t->buffer, off_start, |
| 3189 | parent, fda->parent_offset, |
| 3190 | last_fixup_obj, |
| 3191 | last_fixup_min_off)) { |
| 3192 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 3193 | proc->pid, thread->pid); |
| 3194 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3195 | return_error_param = -EINVAL; |
| 3196 | return_error_line = __LINE__; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3197 | goto err_bad_parent; |
| 3198 | } |
| 3199 | ret = binder_translate_fd_array(fda, parent, t, thread, |
| 3200 | in_reply_to); |
| 3201 | if (ret < 0) { |
| 3202 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3203 | return_error_param = ret; |
| 3204 | return_error_line = __LINE__; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3205 | goto err_translate_failed; |
| 3206 | } |
| 3207 | last_fixup_obj = parent; |
| 3208 | last_fixup_min_off = |
| 3209 | fda->parent_offset + sizeof(u32) * fda->num_fds; |
| 3210 | } break; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3211 | case BINDER_TYPE_PTR: { |
| 3212 | struct binder_buffer_object *bp = |
| 3213 | to_binder_buffer_object(hdr); |
| 3214 | size_t buf_left = sg_buf_end - sg_bufp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3215 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3216 | if (bp->length > buf_left) { |
| 3217 | binder_user_error("%d:%d got transaction with too large buffer\n", |
| 3218 | proc->pid, thread->pid); |
| 3219 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3220 | return_error_param = -EINVAL; |
| 3221 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3222 | goto err_bad_offset; |
| 3223 | } |
| 3224 | if (copy_from_user(sg_bufp, |
| 3225 | (const void __user *)(uintptr_t) |
| 3226 | bp->buffer, bp->length)) { |
| 3227 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3228 | proc->pid, thread->pid); |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3229 | return_error_param = -EFAULT; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3230 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3231 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3232 | goto err_copy_data_failed; |
| 3233 | } |
| 3234 | /* Fixup buffer pointer to target proc address space */ |
| 3235 | bp->buffer = (uintptr_t)sg_bufp + |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3236 | binder_alloc_get_user_buffer_offset( |
| 3237 | &target_proc->alloc); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3238 | sg_bufp += ALIGN(bp->length, sizeof(u64)); |
| 3239 | |
| 3240 | ret = binder_fixup_parent(t, thread, bp, off_start, |
| 3241 | offp - off_start, |
| 3242 | last_fixup_obj, |
| 3243 | last_fixup_min_off); |
| 3244 | if (ret < 0) { |
| 3245 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3246 | return_error_param = ret; |
| 3247 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3248 | goto err_translate_failed; |
| 3249 | } |
| 3250 | last_fixup_obj = bp; |
| 3251 | last_fixup_min_off = 0; |
| 3252 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3253 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 3254 | 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] | 3255 | proc->pid, thread->pid, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3256 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3257 | return_error_param = -EINVAL; |
| 3258 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3259 | goto err_bad_object_type; |
| 3260 | } |
| 3261 | } |
Todd Kjos | 8dedb0c | 2017-05-09 08:31:32 -0700 | [diff] [blame] | 3262 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3263 | binder_enqueue_work(proc, tcomplete, &thread->todo); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3264 | t->work.type = BINDER_WORK_TRANSACTION; |
Todd Kjos | 8dedb0c | 2017-05-09 08:31:32 -0700 | [diff] [blame] | 3265 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3266 | if (reply) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3267 | binder_inner_proc_lock(target_proc); |
| 3268 | if (target_thread->is_dead) { |
| 3269 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3270 | goto err_dead_proc_or_thread; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3271 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3272 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3273 | binder_pop_transaction_ilocked(target_thread, in_reply_to); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3274 | binder_enqueue_work_ilocked(&t->work, &target_thread->todo); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3275 | binder_inner_proc_unlock(target_proc); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3276 | wake_up_interruptible_sync(&target_thread->wait); |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3277 | binder_restore_priority(current, in_reply_to->saved_priority); |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 3278 | binder_free_transaction(in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3279 | } else if (!(t->flags & TF_ONE_WAY)) { |
| 3280 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3281 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3282 | t->need_reply = 1; |
| 3283 | t->from_parent = thread->transaction_stack; |
| 3284 | thread->transaction_stack = t; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3285 | binder_inner_proc_unlock(proc); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3286 | if (!binder_proc_transaction(t, target_proc, target_thread)) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3287 | binder_inner_proc_lock(proc); |
| 3288 | binder_pop_transaction_ilocked(thread, t); |
| 3289 | binder_inner_proc_unlock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3290 | goto err_dead_proc_or_thread; |
| 3291 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3292 | } else { |
| 3293 | BUG_ON(target_node == NULL); |
| 3294 | BUG_ON(t->buffer->async_transaction != 1); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3295 | if (!binder_proc_transaction(t, target_proc, NULL)) |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3296 | goto err_dead_proc_or_thread; |
Riley Andrews | b596881 | 2015-09-01 12:42:07 -0700 | [diff] [blame] | 3297 | } |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3298 | if (target_thread) |
| 3299 | binder_thread_dec_tmpref(target_thread); |
| 3300 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3301 | if (target_node) |
| 3302 | binder_dec_node_tmpref(target_node); |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3303 | /* |
| 3304 | * write barrier to synchronize with initialization |
| 3305 | * of log entry |
| 3306 | */ |
| 3307 | smp_wmb(); |
| 3308 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3309 | return; |
| 3310 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3311 | err_dead_proc_or_thread: |
| 3312 | return_error = BR_DEAD_REPLY; |
| 3313 | return_error_line = __LINE__; |
Xu YiPing | 86578a0 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3314 | binder_dequeue_work(proc, tcomplete); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3315 | err_translate_failed: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3316 | err_bad_object_type: |
| 3317 | err_bad_offset: |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3318 | err_bad_parent: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3319 | err_copy_data_failed: |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3320 | trace_binder_transaction_failed_buffer_release(t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3321 | binder_transaction_buffer_release(target_proc, t->buffer, offp); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3322 | if (target_node) |
| 3323 | binder_dec_node_tmpref(target_node); |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3324 | target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3325 | t->buffer->transaction = NULL; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3326 | binder_alloc_free_buf(&target_proc->alloc, t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3327 | err_binder_alloc_buf_failed: |
| 3328 | kfree(tcomplete); |
| 3329 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3330 | err_alloc_tcomplete_failed: |
| 3331 | kfree(t); |
| 3332 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 3333 | err_alloc_t_failed: |
| 3334 | err_bad_call_stack: |
| 3335 | err_empty_call_stack: |
| 3336 | err_dead_binder: |
| 3337 | err_invalid_target_handle: |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3338 | if (target_thread) |
| 3339 | binder_thread_dec_tmpref(target_thread); |
| 3340 | if (target_proc) |
| 3341 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3342 | if (target_node) { |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3343 | binder_dec_node(target_node, 1, 0); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3344 | binder_dec_node_tmpref(target_node); |
| 3345 | } |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3346 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3347 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3348 | "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n", |
| 3349 | proc->pid, thread->pid, return_error, return_error_param, |
| 3350 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3351 | return_error_line); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3352 | |
| 3353 | { |
| 3354 | struct binder_transaction_log_entry *fe; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3355 | |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3356 | e->return_error = return_error; |
| 3357 | e->return_error_param = return_error_param; |
| 3358 | e->return_error_line = return_error_line; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3359 | fe = binder_transaction_log_add(&binder_transaction_log_failed); |
| 3360 | *fe = *e; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3361 | /* |
| 3362 | * write barrier to synchronize with initialization |
| 3363 | * of log entry |
| 3364 | */ |
| 3365 | smp_wmb(); |
| 3366 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
| 3367 | WRITE_ONCE(fe->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3368 | } |
| 3369 | |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3370 | BUG_ON(thread->return_error.cmd != BR_OK); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3371 | if (in_reply_to) { |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3372 | binder_restore_priority(current, in_reply_to->saved_priority); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3373 | thread->return_error.cmd = BR_TRANSACTION_COMPLETE; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3374 | binder_enqueue_work(thread->proc, |
| 3375 | &thread->return_error.work, |
| 3376 | &thread->todo); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3377 | binder_send_failed_reply(in_reply_to, return_error); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3378 | } else { |
| 3379 | thread->return_error.cmd = return_error; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3380 | binder_enqueue_work(thread->proc, |
| 3381 | &thread->return_error.work, |
| 3382 | &thread->todo); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3383 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3384 | } |
| 3385 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3386 | static int binder_thread_write(struct binder_proc *proc, |
| 3387 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3388 | binder_uintptr_t binder_buffer, size_t size, |
| 3389 | binder_size_t *consumed) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3390 | { |
| 3391 | uint32_t cmd; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 3392 | struct binder_context *context = proc->context; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3393 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3394 | void __user *ptr = buffer + *consumed; |
| 3395 | void __user *end = buffer + size; |
| 3396 | |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3397 | while (ptr < end && thread->return_error.cmd == BR_OK) { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3398 | int ret; |
| 3399 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3400 | if (get_user(cmd, (uint32_t __user *)ptr)) |
| 3401 | return -EFAULT; |
| 3402 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3403 | trace_binder_command(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3404 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 3405 | atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]); |
| 3406 | atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]); |
| 3407 | atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3408 | } |
| 3409 | switch (cmd) { |
| 3410 | case BC_INCREFS: |
| 3411 | case BC_ACQUIRE: |
| 3412 | case BC_RELEASE: |
| 3413 | case BC_DECREFS: { |
| 3414 | uint32_t target; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3415 | const char *debug_string; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3416 | bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE; |
| 3417 | bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE; |
| 3418 | struct binder_ref_data rdata; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3419 | |
| 3420 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3421 | return -EFAULT; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3422 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3423 | ptr += sizeof(uint32_t); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3424 | ret = -1; |
| 3425 | if (increment && !target) { |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3426 | struct binder_node *ctx_mgr_node; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3427 | mutex_lock(&context->context_mgr_node_lock); |
| 3428 | ctx_mgr_node = context->binder_context_mgr_node; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3429 | if (ctx_mgr_node) |
| 3430 | ret = binder_inc_ref_for_node( |
| 3431 | proc, ctx_mgr_node, |
| 3432 | strong, NULL, &rdata); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3433 | mutex_unlock(&context->context_mgr_node_lock); |
| 3434 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3435 | if (ret) |
| 3436 | ret = binder_update_ref_for_handle( |
| 3437 | proc, target, increment, strong, |
| 3438 | &rdata); |
| 3439 | if (!ret && rdata.desc != target) { |
| 3440 | binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n", |
| 3441 | proc->pid, thread->pid, |
| 3442 | target, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3443 | } |
| 3444 | switch (cmd) { |
| 3445 | case BC_INCREFS: |
| 3446 | debug_string = "IncRefs"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3447 | break; |
| 3448 | case BC_ACQUIRE: |
| 3449 | debug_string = "Acquire"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3450 | break; |
| 3451 | case BC_RELEASE: |
| 3452 | debug_string = "Release"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3453 | break; |
| 3454 | case BC_DECREFS: |
| 3455 | default: |
| 3456 | debug_string = "DecRefs"; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3457 | break; |
| 3458 | } |
| 3459 | if (ret) { |
| 3460 | binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n", |
| 3461 | proc->pid, thread->pid, debug_string, |
| 3462 | strong, target, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3463 | break; |
| 3464 | } |
| 3465 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3466 | "%d:%d %s ref %d desc %d s %d w %d\n", |
| 3467 | proc->pid, thread->pid, debug_string, |
| 3468 | rdata.debug_id, rdata.desc, rdata.strong, |
| 3469 | rdata.weak); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3470 | break; |
| 3471 | } |
| 3472 | case BC_INCREFS_DONE: |
| 3473 | case BC_ACQUIRE_DONE: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3474 | binder_uintptr_t node_ptr; |
| 3475 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3476 | struct binder_node *node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3477 | bool free_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3478 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3479 | if (get_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3480 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3481 | ptr += sizeof(binder_uintptr_t); |
| 3482 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3483 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3484 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3485 | node = binder_get_node(proc, node_ptr); |
| 3486 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3487 | binder_user_error("%d:%d %s u%016llx no match\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3488 | proc->pid, thread->pid, |
| 3489 | cmd == BC_INCREFS_DONE ? |
| 3490 | "BC_INCREFS_DONE" : |
| 3491 | "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3492 | (u64)node_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3493 | break; |
| 3494 | } |
| 3495 | if (cookie != node->cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3496 | 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] | 3497 | proc->pid, thread->pid, |
| 3498 | cmd == BC_INCREFS_DONE ? |
| 3499 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3500 | (u64)node_ptr, node->debug_id, |
| 3501 | (u64)cookie, (u64)node->cookie); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3502 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3503 | break; |
| 3504 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3505 | binder_node_inner_lock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3506 | if (cmd == BC_ACQUIRE_DONE) { |
| 3507 | if (node->pending_strong_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3508 | 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] | 3509 | proc->pid, thread->pid, |
| 3510 | node->debug_id); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3511 | binder_node_inner_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3512 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3513 | break; |
| 3514 | } |
| 3515 | node->pending_strong_ref = 0; |
| 3516 | } else { |
| 3517 | if (node->pending_weak_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3518 | 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] | 3519 | proc->pid, thread->pid, |
| 3520 | node->debug_id); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3521 | binder_node_inner_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3522 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3523 | break; |
| 3524 | } |
| 3525 | node->pending_weak_ref = 0; |
| 3526 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3527 | free_node = binder_dec_node_nilocked(node, |
| 3528 | cmd == BC_ACQUIRE_DONE, 0); |
| 3529 | WARN_ON(free_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3530 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3531 | "%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] | 3532 | proc->pid, thread->pid, |
| 3533 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3534 | node->debug_id, node->local_strong_refs, |
| 3535 | node->local_weak_refs, node->tmp_refs); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3536 | binder_node_inner_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3537 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3538 | break; |
| 3539 | } |
| 3540 | case BC_ATTEMPT_ACQUIRE: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3541 | pr_err("BC_ATTEMPT_ACQUIRE not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3542 | return -EINVAL; |
| 3543 | case BC_ACQUIRE_RESULT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3544 | pr_err("BC_ACQUIRE_RESULT not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3545 | return -EINVAL; |
| 3546 | |
| 3547 | case BC_FREE_BUFFER: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3548 | binder_uintptr_t data_ptr; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3549 | struct binder_buffer *buffer; |
| 3550 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3551 | if (get_user(data_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3552 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3553 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3554 | |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 3555 | buffer = binder_alloc_prepare_to_free(&proc->alloc, |
| 3556 | data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3557 | if (buffer == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3558 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n", |
| 3559 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3560 | break; |
| 3561 | } |
| 3562 | if (!buffer->allow_user_free) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3563 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n", |
| 3564 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3565 | break; |
| 3566 | } |
| 3567 | binder_debug(BINDER_DEBUG_FREE_BUFFER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3568 | "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n", |
| 3569 | proc->pid, thread->pid, (u64)data_ptr, |
| 3570 | buffer->debug_id, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3571 | buffer->transaction ? "active" : "finished"); |
| 3572 | |
| 3573 | if (buffer->transaction) { |
| 3574 | buffer->transaction->buffer = NULL; |
| 3575 | buffer->transaction = NULL; |
| 3576 | } |
| 3577 | if (buffer->async_transaction && buffer->target_node) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3578 | struct binder_node *buf_node; |
| 3579 | struct binder_work *w; |
| 3580 | |
| 3581 | buf_node = buffer->target_node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3582 | binder_node_inner_lock(buf_node); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3583 | BUG_ON(!buf_node->has_async_transaction); |
| 3584 | BUG_ON(buf_node->proc != proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3585 | w = binder_dequeue_work_head_ilocked( |
| 3586 | &buf_node->async_todo); |
Martijn Coenen | 4501c04 | 2017-08-10 13:56:16 +0200 | [diff] [blame] | 3587 | if (!w) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3588 | buf_node->has_async_transaction = 0; |
Martijn Coenen | 4501c04 | 2017-08-10 13:56:16 +0200 | [diff] [blame] | 3589 | } else { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3590 | binder_enqueue_work_ilocked( |
Martijn Coenen | 4501c04 | 2017-08-10 13:56:16 +0200 | [diff] [blame] | 3591 | w, &proc->todo); |
| 3592 | binder_wakeup_proc_ilocked(proc); |
| 3593 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3594 | binder_node_inner_unlock(buf_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3595 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3596 | trace_binder_transaction_buffer_release(buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3597 | binder_transaction_buffer_release(proc, buffer, NULL); |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3598 | binder_alloc_free_buf(&proc->alloc, buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3599 | break; |
| 3600 | } |
| 3601 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3602 | case BC_TRANSACTION_SG: |
| 3603 | case BC_REPLY_SG: { |
| 3604 | struct binder_transaction_data_sg tr; |
| 3605 | |
| 3606 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3607 | return -EFAULT; |
| 3608 | ptr += sizeof(tr); |
| 3609 | binder_transaction(proc, thread, &tr.transaction_data, |
| 3610 | cmd == BC_REPLY_SG, tr.buffers_size); |
| 3611 | break; |
| 3612 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3613 | case BC_TRANSACTION: |
| 3614 | case BC_REPLY: { |
| 3615 | struct binder_transaction_data tr; |
| 3616 | |
| 3617 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3618 | return -EFAULT; |
| 3619 | ptr += sizeof(tr); |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3620 | binder_transaction(proc, thread, &tr, |
| 3621 | cmd == BC_REPLY, 0); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3622 | break; |
| 3623 | } |
| 3624 | |
| 3625 | case BC_REGISTER_LOOPER: |
| 3626 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3627 | "%d:%d BC_REGISTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3628 | proc->pid, thread->pid); |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 3629 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3630 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { |
| 3631 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3632 | 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] | 3633 | proc->pid, thread->pid); |
| 3634 | } else if (proc->requested_threads == 0) { |
| 3635 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3636 | 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] | 3637 | proc->pid, thread->pid); |
| 3638 | } else { |
| 3639 | proc->requested_threads--; |
| 3640 | proc->requested_threads_started++; |
| 3641 | } |
| 3642 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 3643 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3644 | break; |
| 3645 | case BC_ENTER_LOOPER: |
| 3646 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3647 | "%d:%d BC_ENTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3648 | proc->pid, thread->pid); |
| 3649 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { |
| 3650 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3651 | 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] | 3652 | proc->pid, thread->pid); |
| 3653 | } |
| 3654 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; |
| 3655 | break; |
| 3656 | case BC_EXIT_LOOPER: |
| 3657 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3658 | "%d:%d BC_EXIT_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3659 | proc->pid, thread->pid); |
| 3660 | thread->looper |= BINDER_LOOPER_STATE_EXITED; |
| 3661 | break; |
| 3662 | |
| 3663 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 3664 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 3665 | uint32_t target; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3666 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3667 | struct binder_ref *ref; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3668 | struct binder_ref_death *death = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3669 | |
| 3670 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3671 | return -EFAULT; |
| 3672 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3673 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3674 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3675 | ptr += sizeof(binder_uintptr_t); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3676 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3677 | /* |
| 3678 | * Allocate memory for death notification |
| 3679 | * before taking lock |
| 3680 | */ |
| 3681 | death = kzalloc(sizeof(*death), GFP_KERNEL); |
| 3682 | if (death == NULL) { |
| 3683 | WARN_ON(thread->return_error.cmd != |
| 3684 | BR_OK); |
| 3685 | thread->return_error.cmd = BR_ERROR; |
| 3686 | binder_enqueue_work( |
| 3687 | thread->proc, |
| 3688 | &thread->return_error.work, |
| 3689 | &thread->todo); |
| 3690 | binder_debug( |
| 3691 | BINDER_DEBUG_FAILED_TRANSACTION, |
| 3692 | "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n", |
| 3693 | proc->pid, thread->pid); |
| 3694 | break; |
| 3695 | } |
| 3696 | } |
| 3697 | binder_proc_lock(proc); |
| 3698 | ref = binder_get_ref_olocked(proc, target, false); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3699 | if (ref == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3700 | binder_user_error("%d:%d %s invalid ref %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3701 | proc->pid, thread->pid, |
| 3702 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3703 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3704 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 3705 | target); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3706 | binder_proc_unlock(proc); |
| 3707 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3708 | break; |
| 3709 | } |
| 3710 | |
| 3711 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3712 | "%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] | 3713 | proc->pid, thread->pid, |
| 3714 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3715 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3716 | "BC_CLEAR_DEATH_NOTIFICATION", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3717 | (u64)cookie, ref->data.debug_id, |
| 3718 | ref->data.desc, ref->data.strong, |
| 3719 | ref->data.weak, ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3720 | |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3721 | binder_node_lock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3722 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3723 | if (ref->death) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3724 | 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] | 3725 | proc->pid, thread->pid); |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3726 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3727 | binder_proc_unlock(proc); |
| 3728 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3729 | break; |
| 3730 | } |
| 3731 | binder_stats_created(BINDER_STAT_DEATH); |
| 3732 | INIT_LIST_HEAD(&death->work.entry); |
| 3733 | death->cookie = cookie; |
| 3734 | ref->death = death; |
| 3735 | if (ref->node->proc == NULL) { |
| 3736 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
Martijn Coenen | 3bdbe4c | 2017-08-10 13:50:52 +0200 | [diff] [blame] | 3737 | |
| 3738 | binder_inner_proc_lock(proc); |
| 3739 | binder_enqueue_work_ilocked( |
| 3740 | &ref->death->work, &proc->todo); |
| 3741 | binder_wakeup_proc_ilocked(proc); |
| 3742 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3743 | } |
| 3744 | } else { |
| 3745 | if (ref->death == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3746 | 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] | 3747 | proc->pid, thread->pid); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3748 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3749 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3750 | break; |
| 3751 | } |
| 3752 | death = ref->death; |
| 3753 | if (death->cookie != cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3754 | 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] | 3755 | proc->pid, thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3756 | (u64)death->cookie, |
| 3757 | (u64)cookie); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3758 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3759 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3760 | break; |
| 3761 | } |
| 3762 | ref->death = NULL; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3763 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3764 | if (list_empty(&death->work.entry)) { |
| 3765 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3766 | if (thread->looper & |
| 3767 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3768 | BINDER_LOOPER_STATE_ENTERED)) |
| 3769 | binder_enqueue_work_ilocked( |
| 3770 | &death->work, |
| 3771 | &thread->todo); |
| 3772 | else { |
| 3773 | binder_enqueue_work_ilocked( |
| 3774 | &death->work, |
| 3775 | &proc->todo); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3776 | binder_wakeup_proc_ilocked( |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3777 | proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3778 | } |
| 3779 | } else { |
| 3780 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); |
| 3781 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; |
| 3782 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3783 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3784 | } |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3785 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3786 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3787 | } break; |
| 3788 | case BC_DEAD_BINDER_DONE: { |
| 3789 | struct binder_work *w; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3790 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3791 | struct binder_ref_death *death = NULL; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3792 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3793 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3794 | return -EFAULT; |
| 3795 | |
Lisa Du | 7a64cd8 | 2016-02-17 09:32:52 +0800 | [diff] [blame] | 3796 | ptr += sizeof(cookie); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3797 | binder_inner_proc_lock(proc); |
| 3798 | list_for_each_entry(w, &proc->delivered_death, |
| 3799 | entry) { |
| 3800 | struct binder_ref_death *tmp_death = |
| 3801 | container_of(w, |
| 3802 | struct binder_ref_death, |
| 3803 | work); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3804 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3805 | if (tmp_death->cookie == cookie) { |
| 3806 | death = tmp_death; |
| 3807 | break; |
| 3808 | } |
| 3809 | } |
| 3810 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3811 | "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n", |
| 3812 | proc->pid, thread->pid, (u64)cookie, |
| 3813 | death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3814 | if (death == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3815 | binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n", |
| 3816 | proc->pid, thread->pid, (u64)cookie); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3817 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3818 | break; |
| 3819 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3820 | binder_dequeue_work_ilocked(&death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3821 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { |
| 3822 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3823 | if (thread->looper & |
| 3824 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3825 | BINDER_LOOPER_STATE_ENTERED)) |
| 3826 | binder_enqueue_work_ilocked( |
| 3827 | &death->work, &thread->todo); |
| 3828 | else { |
| 3829 | binder_enqueue_work_ilocked( |
| 3830 | &death->work, |
| 3831 | &proc->todo); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3832 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3833 | } |
| 3834 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3835 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3836 | } break; |
| 3837 | |
| 3838 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3839 | pr_err("%d:%d unknown command %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3840 | proc->pid, thread->pid, cmd); |
| 3841 | return -EINVAL; |
| 3842 | } |
| 3843 | *consumed = ptr - buffer; |
| 3844 | } |
| 3845 | return 0; |
| 3846 | } |
| 3847 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3848 | static void binder_stat_br(struct binder_proc *proc, |
| 3849 | struct binder_thread *thread, uint32_t cmd) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3850 | { |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3851 | trace_binder_return(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3852 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 3853 | atomic_inc(&binder_stats.br[_IOC_NR(cmd)]); |
| 3854 | atomic_inc(&proc->stats.br[_IOC_NR(cmd)]); |
| 3855 | atomic_inc(&thread->stats.br[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3856 | } |
| 3857 | } |
| 3858 | |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 3859 | static int binder_put_node_cmd(struct binder_proc *proc, |
| 3860 | struct binder_thread *thread, |
| 3861 | void __user **ptrp, |
| 3862 | binder_uintptr_t node_ptr, |
| 3863 | binder_uintptr_t node_cookie, |
| 3864 | int node_debug_id, |
| 3865 | uint32_t cmd, const char *cmd_name) |
| 3866 | { |
| 3867 | void __user *ptr = *ptrp; |
| 3868 | |
| 3869 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 3870 | return -EFAULT; |
| 3871 | ptr += sizeof(uint32_t); |
| 3872 | |
| 3873 | if (put_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
| 3874 | return -EFAULT; |
| 3875 | ptr += sizeof(binder_uintptr_t); |
| 3876 | |
| 3877 | if (put_user(node_cookie, (binder_uintptr_t __user *)ptr)) |
| 3878 | return -EFAULT; |
| 3879 | ptr += sizeof(binder_uintptr_t); |
| 3880 | |
| 3881 | binder_stat_br(proc, thread, cmd); |
| 3882 | binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n", |
| 3883 | proc->pid, thread->pid, cmd_name, node_debug_id, |
| 3884 | (u64)node_ptr, (u64)node_cookie); |
| 3885 | |
| 3886 | *ptrp = ptr; |
| 3887 | return 0; |
| 3888 | } |
| 3889 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3890 | static int binder_wait_for_work(struct binder_thread *thread, |
| 3891 | bool do_proc_work) |
| 3892 | { |
| 3893 | DEFINE_WAIT(wait); |
| 3894 | struct binder_proc *proc = thread->proc; |
| 3895 | int ret = 0; |
| 3896 | |
| 3897 | freezer_do_not_count(); |
| 3898 | binder_inner_proc_lock(proc); |
| 3899 | for (;;) { |
| 3900 | prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE); |
| 3901 | if (binder_has_work_ilocked(thread, do_proc_work)) |
| 3902 | break; |
| 3903 | if (do_proc_work) |
| 3904 | list_add(&thread->waiting_thread_node, |
| 3905 | &proc->waiting_threads); |
| 3906 | binder_inner_proc_unlock(proc); |
| 3907 | schedule(); |
| 3908 | binder_inner_proc_lock(proc); |
| 3909 | list_del_init(&thread->waiting_thread_node); |
| 3910 | if (signal_pending(current)) { |
| 3911 | ret = -ERESTARTSYS; |
| 3912 | break; |
| 3913 | } |
| 3914 | } |
| 3915 | finish_wait(&thread->wait, &wait); |
| 3916 | binder_inner_proc_unlock(proc); |
| 3917 | freezer_count(); |
| 3918 | |
| 3919 | return ret; |
| 3920 | } |
| 3921 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3922 | static int binder_thread_read(struct binder_proc *proc, |
| 3923 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3924 | binder_uintptr_t binder_buffer, size_t size, |
| 3925 | binder_size_t *consumed, int non_block) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3926 | { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3927 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3928 | void __user *ptr = buffer + *consumed; |
| 3929 | void __user *end = buffer + size; |
| 3930 | |
| 3931 | int ret = 0; |
| 3932 | int wait_for_proc_work; |
| 3933 | |
| 3934 | if (*consumed == 0) { |
| 3935 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) |
| 3936 | return -EFAULT; |
| 3937 | ptr += sizeof(uint32_t); |
| 3938 | } |
| 3939 | |
| 3940 | retry: |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3941 | binder_inner_proc_lock(proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3942 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3943 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3944 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3945 | thread->looper |= BINDER_LOOPER_STATE_WAITING; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3946 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3947 | trace_binder_wait_for_work(wait_for_proc_work, |
| 3948 | !!thread->transaction_stack, |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3949 | !binder_worklist_empty(proc, &thread->todo)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3950 | if (wait_for_proc_work) { |
| 3951 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 3952 | BINDER_LOOPER_STATE_ENTERED))) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3953 | 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] | 3954 | proc->pid, thread->pid, thread->looper); |
| 3955 | wait_event_interruptible(binder_user_error_wait, |
| 3956 | binder_stop_on_user_error < 2); |
| 3957 | } |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3958 | binder_restore_priority(current, proc->default_priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3959 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3960 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 3961 | if (non_block) { |
| 3962 | if (!binder_has_work(thread, wait_for_proc_work)) |
| 3963 | ret = -EAGAIN; |
| 3964 | } else { |
| 3965 | ret = binder_wait_for_work(thread, wait_for_proc_work); |
| 3966 | } |
| 3967 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3968 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; |
| 3969 | |
| 3970 | if (ret) |
| 3971 | return ret; |
| 3972 | |
| 3973 | while (1) { |
| 3974 | uint32_t cmd; |
| 3975 | struct binder_transaction_data tr; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3976 | struct binder_work *w = NULL; |
| 3977 | struct list_head *list = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3978 | struct binder_transaction *t = NULL; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3979 | struct binder_thread *t_from; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3980 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 3981 | binder_inner_proc_lock(proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3982 | if (!binder_worklist_empty_ilocked(&thread->todo)) |
| 3983 | list = &thread->todo; |
| 3984 | else if (!binder_worklist_empty_ilocked(&proc->todo) && |
| 3985 | wait_for_proc_work) |
| 3986 | list = &proc->todo; |
| 3987 | else { |
| 3988 | binder_inner_proc_unlock(proc); |
| 3989 | |
Dmitry Voytik | 395262a | 2014-09-08 18:16:34 +0400 | [diff] [blame] | 3990 | /* no data added */ |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 3991 | if (ptr - buffer == 4 && !thread->looper_need_return) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3992 | goto retry; |
| 3993 | break; |
| 3994 | } |
| 3995 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 3996 | if (end - ptr < sizeof(tr) + 4) { |
| 3997 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3998 | break; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 3999 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4000 | w = binder_dequeue_work_head_ilocked(list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4001 | |
| 4002 | switch (w->type) { |
| 4003 | case BINDER_WORK_TRANSACTION: { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4004 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4005 | t = container_of(w, struct binder_transaction, work); |
| 4006 | } break; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4007 | case BINDER_WORK_RETURN_ERROR: { |
| 4008 | struct binder_error *e = container_of( |
| 4009 | w, struct binder_error, work); |
| 4010 | |
| 4011 | WARN_ON(e->cmd == BR_OK); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4012 | binder_inner_proc_unlock(proc); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4013 | if (put_user(e->cmd, (uint32_t __user *)ptr)) |
| 4014 | return -EFAULT; |
| 4015 | e->cmd = BR_OK; |
| 4016 | ptr += sizeof(uint32_t); |
| 4017 | |
| 4018 | binder_stat_br(proc, thread, cmd); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4019 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4020 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4021 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4022 | cmd = BR_TRANSACTION_COMPLETE; |
| 4023 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4024 | return -EFAULT; |
| 4025 | ptr += sizeof(uint32_t); |
| 4026 | |
| 4027 | binder_stat_br(proc, thread, cmd); |
| 4028 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4029 | "%d:%d BR_TRANSACTION_COMPLETE\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4030 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4031 | kfree(w); |
| 4032 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4033 | } break; |
| 4034 | case BINDER_WORK_NODE: { |
| 4035 | struct binder_node *node = container_of(w, struct binder_node, work); |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4036 | int strong, weak; |
| 4037 | binder_uintptr_t node_ptr = node->ptr; |
| 4038 | binder_uintptr_t node_cookie = node->cookie; |
| 4039 | int node_debug_id = node->debug_id; |
| 4040 | int has_weak_ref; |
| 4041 | int has_strong_ref; |
| 4042 | void __user *orig_ptr = ptr; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4043 | |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4044 | BUG_ON(proc != node->proc); |
| 4045 | strong = node->internal_strong_refs || |
| 4046 | node->local_strong_refs; |
| 4047 | weak = !hlist_empty(&node->refs) || |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4048 | node->local_weak_refs || |
| 4049 | node->tmp_refs || strong; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4050 | has_strong_ref = node->has_strong_ref; |
| 4051 | has_weak_ref = node->has_weak_ref; |
| 4052 | |
| 4053 | if (weak && !has_weak_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4054 | node->has_weak_ref = 1; |
| 4055 | node->pending_weak_ref = 1; |
| 4056 | node->local_weak_refs++; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4057 | } |
| 4058 | if (strong && !has_strong_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4059 | node->has_strong_ref = 1; |
| 4060 | node->pending_strong_ref = 1; |
| 4061 | node->local_strong_refs++; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4062 | } |
| 4063 | if (!strong && has_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4064 | node->has_strong_ref = 0; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4065 | if (!weak && has_weak_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4066 | node->has_weak_ref = 0; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4067 | if (!weak && !strong) { |
| 4068 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4069 | "%d:%d node %d u%016llx c%016llx deleted\n", |
| 4070 | proc->pid, thread->pid, |
| 4071 | node_debug_id, |
| 4072 | (u64)node_ptr, |
| 4073 | (u64)node_cookie); |
| 4074 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4075 | binder_inner_proc_unlock(proc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4076 | binder_node_lock(node); |
| 4077 | /* |
| 4078 | * Acquire the node lock before freeing the |
| 4079 | * node to serialize with other threads that |
| 4080 | * may have been holding the node lock while |
| 4081 | * decrementing this node (avoids race where |
| 4082 | * this thread frees while the other thread |
| 4083 | * is unlocking the node after the final |
| 4084 | * decrement) |
| 4085 | */ |
| 4086 | binder_node_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4087 | binder_free_node(node); |
| 4088 | } else |
| 4089 | binder_inner_proc_unlock(proc); |
| 4090 | |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4091 | if (weak && !has_weak_ref) |
| 4092 | ret = binder_put_node_cmd( |
| 4093 | proc, thread, &ptr, node_ptr, |
| 4094 | node_cookie, node_debug_id, |
| 4095 | BR_INCREFS, "BR_INCREFS"); |
| 4096 | if (!ret && strong && !has_strong_ref) |
| 4097 | ret = binder_put_node_cmd( |
| 4098 | proc, thread, &ptr, node_ptr, |
| 4099 | node_cookie, node_debug_id, |
| 4100 | BR_ACQUIRE, "BR_ACQUIRE"); |
| 4101 | if (!ret && !strong && has_strong_ref) |
| 4102 | ret = binder_put_node_cmd( |
| 4103 | proc, thread, &ptr, node_ptr, |
| 4104 | node_cookie, node_debug_id, |
| 4105 | BR_RELEASE, "BR_RELEASE"); |
| 4106 | if (!ret && !weak && has_weak_ref) |
| 4107 | ret = binder_put_node_cmd( |
| 4108 | proc, thread, &ptr, node_ptr, |
| 4109 | node_cookie, node_debug_id, |
| 4110 | BR_DECREFS, "BR_DECREFS"); |
| 4111 | if (orig_ptr == ptr) |
| 4112 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4113 | "%d:%d node %d u%016llx c%016llx state unchanged\n", |
| 4114 | proc->pid, thread->pid, |
| 4115 | node_debug_id, |
| 4116 | (u64)node_ptr, |
| 4117 | (u64)node_cookie); |
| 4118 | if (ret) |
| 4119 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4120 | } break; |
| 4121 | case BINDER_WORK_DEAD_BINDER: |
| 4122 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4123 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4124 | struct binder_ref_death *death; |
| 4125 | uint32_t cmd; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4126 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4127 | |
| 4128 | death = container_of(w, struct binder_ref_death, work); |
| 4129 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) |
| 4130 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; |
| 4131 | else |
| 4132 | cmd = BR_DEAD_BINDER; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4133 | cookie = death->cookie; |
| 4134 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4135 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4136 | "%d:%d %s %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4137 | proc->pid, thread->pid, |
| 4138 | cmd == BR_DEAD_BINDER ? |
| 4139 | "BR_DEAD_BINDER" : |
| 4140 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4141 | (u64)cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4142 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4143 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4144 | kfree(death); |
| 4145 | binder_stats_deleted(BINDER_STAT_DEATH); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4146 | } else { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4147 | binder_enqueue_work_ilocked( |
| 4148 | w, &proc->delivered_death); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4149 | binder_inner_proc_unlock(proc); |
| 4150 | } |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4151 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4152 | return -EFAULT; |
| 4153 | ptr += sizeof(uint32_t); |
| 4154 | if (put_user(cookie, |
| 4155 | (binder_uintptr_t __user *)ptr)) |
| 4156 | return -EFAULT; |
| 4157 | ptr += sizeof(binder_uintptr_t); |
| 4158 | binder_stat_br(proc, thread, cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4159 | if (cmd == BR_DEAD_BINDER) |
| 4160 | goto done; /* DEAD_BINDER notifications can cause transactions */ |
| 4161 | } break; |
| 4162 | } |
| 4163 | |
| 4164 | if (!t) |
| 4165 | continue; |
| 4166 | |
| 4167 | BUG_ON(t->buffer == NULL); |
| 4168 | if (t->buffer->target_node) { |
| 4169 | struct binder_node *target_node = t->buffer->target_node; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4170 | struct binder_priority node_prio; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4171 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4172 | tr.target.ptr = target_node->ptr; |
| 4173 | tr.cookie = target_node->cookie; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4174 | node_prio.sched_policy = target_node->sched_policy; |
| 4175 | node_prio.prio = target_node->min_priority; |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 4176 | binder_transaction_priority(current, t, node_prio, |
| 4177 | target_node->inherit_rt); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4178 | cmd = BR_TRANSACTION; |
| 4179 | } else { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4180 | tr.target.ptr = 0; |
| 4181 | tr.cookie = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4182 | cmd = BR_REPLY; |
| 4183 | } |
| 4184 | tr.code = t->code; |
| 4185 | tr.flags = t->flags; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 4186 | tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4187 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4188 | t_from = binder_get_txn_from(t); |
| 4189 | if (t_from) { |
| 4190 | struct task_struct *sender = t_from->proc->tsk; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4191 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4192 | tr.sender_pid = task_tgid_nr_ns(sender, |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 4193 | task_active_pid_ns(current)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4194 | } else { |
| 4195 | tr.sender_pid = 0; |
| 4196 | } |
| 4197 | |
| 4198 | tr.data_size = t->buffer->data_size; |
| 4199 | tr.offsets_size = t->buffer->offsets_size; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4200 | tr.data.ptr.buffer = (binder_uintptr_t) |
| 4201 | ((uintptr_t)t->buffer->data + |
| 4202 | binder_alloc_get_user_buffer_offset(&proc->alloc)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4203 | tr.data.ptr.offsets = tr.data.ptr.buffer + |
| 4204 | ALIGN(t->buffer->data_size, |
| 4205 | sizeof(void *)); |
| 4206 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4207 | if (put_user(cmd, (uint32_t __user *)ptr)) { |
| 4208 | if (t_from) |
| 4209 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4210 | |
| 4211 | binder_cleanup_transaction(t, "put_user failed", |
| 4212 | BR_FAILED_REPLY); |
| 4213 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4214 | return -EFAULT; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4215 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4216 | ptr += sizeof(uint32_t); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4217 | if (copy_to_user(ptr, &tr, sizeof(tr))) { |
| 4218 | if (t_from) |
| 4219 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4220 | |
| 4221 | binder_cleanup_transaction(t, "copy_to_user failed", |
| 4222 | BR_FAILED_REPLY); |
| 4223 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4224 | return -EFAULT; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4225 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4226 | ptr += sizeof(tr); |
| 4227 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4228 | trace_binder_transaction_received(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4229 | binder_stat_br(proc, thread, cmd); |
| 4230 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4231 | "%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] | 4232 | proc->pid, thread->pid, |
| 4233 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : |
| 4234 | "BR_REPLY", |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4235 | t->debug_id, t_from ? t_from->proc->pid : 0, |
| 4236 | t_from ? t_from->pid : 0, cmd, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4237 | t->buffer->data_size, t->buffer->offsets_size, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4238 | (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4239 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4240 | if (t_from) |
| 4241 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4242 | t->buffer->allow_user_free = 1; |
| 4243 | if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4244 | binder_inner_proc_lock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4245 | t->to_parent = thread->transaction_stack; |
| 4246 | t->to_thread = thread; |
| 4247 | thread->transaction_stack = t; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4248 | binder_inner_proc_unlock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4249 | } else { |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 4250 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4251 | } |
| 4252 | break; |
| 4253 | } |
| 4254 | |
| 4255 | done: |
| 4256 | |
| 4257 | *consumed = ptr - buffer; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4258 | binder_inner_proc_lock(proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4259 | if (proc->requested_threads == 0 && |
| 4260 | list_empty(&thread->proc->waiting_threads) && |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4261 | proc->requested_threads_started < proc->max_threads && |
| 4262 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4263 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ |
| 4264 | /*spawn a new thread if we leave this out */) { |
| 4265 | proc->requested_threads++; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4266 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4267 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4268 | "%d:%d BR_SPAWN_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4269 | proc->pid, thread->pid); |
| 4270 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) |
| 4271 | return -EFAULT; |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 4272 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4273 | } else |
| 4274 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4275 | return 0; |
| 4276 | } |
| 4277 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4278 | static void binder_release_work(struct binder_proc *proc, |
| 4279 | struct list_head *list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4280 | { |
| 4281 | struct binder_work *w; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4282 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4283 | while (1) { |
| 4284 | w = binder_dequeue_work_head(proc, list); |
| 4285 | if (!w) |
| 4286 | return; |
| 4287 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4288 | switch (w->type) { |
| 4289 | case BINDER_WORK_TRANSACTION: { |
| 4290 | struct binder_transaction *t; |
| 4291 | |
| 4292 | t = container_of(w, struct binder_transaction, work); |
Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4293 | |
| 4294 | binder_cleanup_transaction(t, "process died.", |
| 4295 | BR_DEAD_REPLY); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4296 | } break; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4297 | case BINDER_WORK_RETURN_ERROR: { |
| 4298 | struct binder_error *e = container_of( |
| 4299 | w, struct binder_error, work); |
| 4300 | |
| 4301 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 4302 | "undelivered TRANSACTION_ERROR: %u\n", |
| 4303 | e->cmd); |
| 4304 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4305 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4306 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4307 | "undelivered TRANSACTION_COMPLETE\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4308 | kfree(w); |
| 4309 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4310 | } break; |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4311 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4312 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4313 | struct binder_ref_death *death; |
| 4314 | |
| 4315 | death = container_of(w, struct binder_ref_death, work); |
| 4316 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4317 | "undelivered death notification, %016llx\n", |
| 4318 | (u64)death->cookie); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4319 | kfree(death); |
| 4320 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 4321 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4322 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4323 | pr_err("unexpected work type, %d, not freed\n", |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4324 | w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4325 | break; |
| 4326 | } |
| 4327 | } |
| 4328 | |
| 4329 | } |
| 4330 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4331 | static struct binder_thread *binder_get_thread_ilocked( |
| 4332 | struct binder_proc *proc, struct binder_thread *new_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4333 | { |
| 4334 | struct binder_thread *thread = NULL; |
| 4335 | struct rb_node *parent = NULL; |
| 4336 | struct rb_node **p = &proc->threads.rb_node; |
| 4337 | |
| 4338 | while (*p) { |
| 4339 | parent = *p; |
| 4340 | thread = rb_entry(parent, struct binder_thread, rb_node); |
| 4341 | |
| 4342 | if (current->pid < thread->pid) |
| 4343 | p = &(*p)->rb_left; |
| 4344 | else if (current->pid > thread->pid) |
| 4345 | p = &(*p)->rb_right; |
| 4346 | else |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4347 | return thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4348 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4349 | if (!new_thread) |
| 4350 | return NULL; |
| 4351 | thread = new_thread; |
| 4352 | binder_stats_created(BINDER_STAT_THREAD); |
| 4353 | thread->proc = proc; |
| 4354 | thread->pid = current->pid; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4355 | get_task_struct(current); |
| 4356 | thread->task = current; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4357 | atomic_set(&thread->tmp_ref, 0); |
| 4358 | init_waitqueue_head(&thread->wait); |
| 4359 | INIT_LIST_HEAD(&thread->todo); |
| 4360 | rb_link_node(&thread->rb_node, parent, p); |
| 4361 | rb_insert_color(&thread->rb_node, &proc->threads); |
| 4362 | thread->looper_need_return = true; |
| 4363 | thread->return_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4364 | thread->return_error.cmd = BR_OK; |
| 4365 | thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4366 | thread->reply_error.cmd = BR_OK; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4367 | INIT_LIST_HEAD(&new_thread->waiting_thread_node); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4368 | return thread; |
| 4369 | } |
| 4370 | |
| 4371 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) |
| 4372 | { |
| 4373 | struct binder_thread *thread; |
| 4374 | struct binder_thread *new_thread; |
| 4375 | |
| 4376 | binder_inner_proc_lock(proc); |
| 4377 | thread = binder_get_thread_ilocked(proc, NULL); |
| 4378 | binder_inner_proc_unlock(proc); |
| 4379 | if (!thread) { |
| 4380 | new_thread = kzalloc(sizeof(*thread), GFP_KERNEL); |
| 4381 | if (new_thread == NULL) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4382 | return NULL; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4383 | binder_inner_proc_lock(proc); |
| 4384 | thread = binder_get_thread_ilocked(proc, new_thread); |
| 4385 | binder_inner_proc_unlock(proc); |
| 4386 | if (thread != new_thread) |
| 4387 | kfree(new_thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4388 | } |
| 4389 | return thread; |
| 4390 | } |
| 4391 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4392 | static void binder_free_proc(struct binder_proc *proc) |
| 4393 | { |
| 4394 | BUG_ON(!list_empty(&proc->todo)); |
| 4395 | BUG_ON(!list_empty(&proc->delivered_death)); |
| 4396 | binder_alloc_deferred_release(&proc->alloc); |
| 4397 | put_task_struct(proc->tsk); |
| 4398 | binder_stats_deleted(BINDER_STAT_PROC); |
| 4399 | kfree(proc); |
| 4400 | } |
| 4401 | |
| 4402 | static void binder_free_thread(struct binder_thread *thread) |
| 4403 | { |
| 4404 | BUG_ON(!list_empty(&thread->todo)); |
| 4405 | binder_stats_deleted(BINDER_STAT_THREAD); |
| 4406 | binder_proc_dec_tmpref(thread->proc); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4407 | put_task_struct(thread->task); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4408 | kfree(thread); |
| 4409 | } |
| 4410 | |
| 4411 | static int binder_thread_release(struct binder_proc *proc, |
| 4412 | struct binder_thread *thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4413 | { |
| 4414 | struct binder_transaction *t; |
| 4415 | struct binder_transaction *send_reply = NULL; |
| 4416 | int active_transactions = 0; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4417 | struct binder_transaction *last_t = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4418 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4419 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4420 | /* |
| 4421 | * take a ref on the proc so it survives |
| 4422 | * after we remove this thread from proc->threads. |
| 4423 | * The corresponding dec is when we actually |
| 4424 | * free the thread in binder_free_thread() |
| 4425 | */ |
| 4426 | proc->tmp_ref++; |
| 4427 | /* |
| 4428 | * take a ref on this thread to ensure it |
| 4429 | * survives while we are releasing it |
| 4430 | */ |
| 4431 | atomic_inc(&thread->tmp_ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4432 | rb_erase(&thread->rb_node, &proc->threads); |
| 4433 | t = thread->transaction_stack; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4434 | if (t) { |
| 4435 | spin_lock(&t->lock); |
| 4436 | if (t->to_thread == thread) |
| 4437 | send_reply = t; |
| 4438 | } |
| 4439 | thread->is_dead = true; |
| 4440 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4441 | while (t) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4442 | last_t = t; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4443 | active_transactions++; |
| 4444 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4445 | "release %d:%d transaction %d %s, still active\n", |
| 4446 | proc->pid, thread->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4447 | t->debug_id, |
| 4448 | (t->to_thread == thread) ? "in" : "out"); |
| 4449 | |
| 4450 | if (t->to_thread == thread) { |
| 4451 | t->to_proc = NULL; |
| 4452 | t->to_thread = NULL; |
| 4453 | if (t->buffer) { |
| 4454 | t->buffer->transaction = NULL; |
| 4455 | t->buffer = NULL; |
| 4456 | } |
| 4457 | t = t->to_parent; |
| 4458 | } else if (t->from == thread) { |
| 4459 | t->from = NULL; |
| 4460 | t = t->from_parent; |
| 4461 | } else |
| 4462 | BUG(); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4463 | spin_unlock(&last_t->lock); |
| 4464 | if (t) |
| 4465 | spin_lock(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4466 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4467 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4468 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4469 | if (send_reply) |
| 4470 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4471 | binder_release_work(proc, &thread->todo); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4472 | binder_thread_dec_tmpref(thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4473 | return active_transactions; |
| 4474 | } |
| 4475 | |
| 4476 | static unsigned int binder_poll(struct file *filp, |
| 4477 | struct poll_table_struct *wait) |
| 4478 | { |
| 4479 | struct binder_proc *proc = filp->private_data; |
| 4480 | struct binder_thread *thread = NULL; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4481 | bool wait_for_proc_work; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4482 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4483 | thread = binder_get_thread(proc); |
| 4484 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4485 | binder_inner_proc_lock(thread->proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4486 | thread->looper |= BINDER_LOOPER_STATE_POLL; |
| 4487 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
| 4488 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4489 | binder_inner_proc_unlock(thread->proc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4490 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4491 | poll_wait(filp, &thread->wait, wait); |
| 4492 | |
Martijn Coenen | 4781093 | 2017-08-10 12:32:00 +0200 | [diff] [blame] | 4493 | if (binder_has_work(thread, wait_for_proc_work)) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4494 | return POLLIN; |
| 4495 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4496 | return 0; |
| 4497 | } |
| 4498 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4499 | static int binder_ioctl_write_read(struct file *filp, |
| 4500 | unsigned int cmd, unsigned long arg, |
| 4501 | struct binder_thread *thread) |
| 4502 | { |
| 4503 | int ret = 0; |
| 4504 | struct binder_proc *proc = filp->private_data; |
| 4505 | unsigned int size = _IOC_SIZE(cmd); |
| 4506 | void __user *ubuf = (void __user *)arg; |
| 4507 | struct binder_write_read bwr; |
| 4508 | |
| 4509 | if (size != sizeof(struct binder_write_read)) { |
| 4510 | ret = -EINVAL; |
| 4511 | goto out; |
| 4512 | } |
| 4513 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { |
| 4514 | ret = -EFAULT; |
| 4515 | goto out; |
| 4516 | } |
| 4517 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4518 | "%d:%d write %lld at %016llx, read %lld at %016llx\n", |
| 4519 | proc->pid, thread->pid, |
| 4520 | (u64)bwr.write_size, (u64)bwr.write_buffer, |
| 4521 | (u64)bwr.read_size, (u64)bwr.read_buffer); |
| 4522 | |
| 4523 | if (bwr.write_size > 0) { |
| 4524 | ret = binder_thread_write(proc, thread, |
| 4525 | bwr.write_buffer, |
| 4526 | bwr.write_size, |
| 4527 | &bwr.write_consumed); |
| 4528 | trace_binder_write_done(ret); |
| 4529 | if (ret < 0) { |
| 4530 | bwr.read_consumed = 0; |
| 4531 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4532 | ret = -EFAULT; |
| 4533 | goto out; |
| 4534 | } |
| 4535 | } |
| 4536 | if (bwr.read_size > 0) { |
| 4537 | ret = binder_thread_read(proc, thread, bwr.read_buffer, |
| 4538 | bwr.read_size, |
| 4539 | &bwr.read_consumed, |
| 4540 | filp->f_flags & O_NONBLOCK); |
| 4541 | trace_binder_read_done(ret); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4542 | binder_inner_proc_lock(proc); |
| 4543 | if (!binder_worklist_empty_ilocked(&proc->todo)) |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 4544 | binder_wakeup_proc_ilocked(proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4545 | binder_inner_proc_unlock(proc); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4546 | if (ret < 0) { |
| 4547 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4548 | ret = -EFAULT; |
| 4549 | goto out; |
| 4550 | } |
| 4551 | } |
| 4552 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4553 | "%d:%d wrote %lld of %lld, read return %lld of %lld\n", |
| 4554 | proc->pid, thread->pid, |
| 4555 | (u64)bwr.write_consumed, (u64)bwr.write_size, |
| 4556 | (u64)bwr.read_consumed, (u64)bwr.read_size); |
| 4557 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { |
| 4558 | ret = -EFAULT; |
| 4559 | goto out; |
| 4560 | } |
| 4561 | out: |
| 4562 | return ret; |
| 4563 | } |
| 4564 | |
| 4565 | static int binder_ioctl_set_ctx_mgr(struct file *filp) |
| 4566 | { |
| 4567 | int ret = 0; |
| 4568 | struct binder_proc *proc = filp->private_data; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4569 | struct binder_context *context = proc->context; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4570 | struct binder_node *new_node; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4571 | kuid_t curr_euid = current_euid(); |
| 4572 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4573 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4574 | if (context->binder_context_mgr_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4575 | pr_err("BINDER_SET_CONTEXT_MGR already set\n"); |
| 4576 | ret = -EBUSY; |
| 4577 | goto out; |
| 4578 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 4579 | ret = security_binder_set_context_mgr(proc->tsk); |
| 4580 | if (ret < 0) |
| 4581 | goto out; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4582 | if (uid_valid(context->binder_context_mgr_uid)) { |
| 4583 | if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4584 | pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n", |
| 4585 | from_kuid(&init_user_ns, curr_euid), |
| 4586 | from_kuid(&init_user_ns, |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4587 | context->binder_context_mgr_uid)); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4588 | ret = -EPERM; |
| 4589 | goto out; |
| 4590 | } |
| 4591 | } else { |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4592 | context->binder_context_mgr_uid = curr_euid; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4593 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4594 | new_node = binder_new_node(proc, NULL); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4595 | if (!new_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4596 | ret = -ENOMEM; |
| 4597 | goto out; |
| 4598 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4599 | binder_node_lock(new_node); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4600 | new_node->local_weak_refs++; |
| 4601 | new_node->local_strong_refs++; |
| 4602 | new_node->has_strong_ref = 1; |
| 4603 | new_node->has_weak_ref = 1; |
| 4604 | context->binder_context_mgr_node = new_node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4605 | binder_node_unlock(new_node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4606 | binder_put_node(new_node); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4607 | out: |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4608 | mutex_unlock(&context->context_mgr_node_lock); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4609 | return ret; |
| 4610 | } |
| 4611 | |
Colin Cross | 833babb3 | 2017-06-20 13:54:44 -0700 | [diff] [blame] | 4612 | static int binder_ioctl_get_node_debug_info(struct binder_proc *proc, |
| 4613 | struct binder_node_debug_info *info) { |
| 4614 | struct rb_node *n; |
| 4615 | binder_uintptr_t ptr = info->ptr; |
| 4616 | |
| 4617 | memset(info, 0, sizeof(*info)); |
| 4618 | |
| 4619 | binder_inner_proc_lock(proc); |
| 4620 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { |
| 4621 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 4622 | rb_node); |
| 4623 | if (node->ptr > ptr) { |
| 4624 | info->ptr = node->ptr; |
| 4625 | info->cookie = node->cookie; |
| 4626 | info->has_strong_ref = node->has_strong_ref; |
| 4627 | info->has_weak_ref = node->has_weak_ref; |
| 4628 | break; |
| 4629 | } |
| 4630 | } |
| 4631 | binder_inner_proc_unlock(proc); |
| 4632 | |
| 4633 | return 0; |
| 4634 | } |
| 4635 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4636 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 4637 | { |
| 4638 | int ret; |
| 4639 | struct binder_proc *proc = filp->private_data; |
| 4640 | struct binder_thread *thread; |
| 4641 | unsigned int size = _IOC_SIZE(cmd); |
| 4642 | void __user *ubuf = (void __user *)arg; |
| 4643 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4644 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", |
| 4645 | proc->pid, current->pid, cmd, arg);*/ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4646 | |
Sherry Yang | 435416b | 2017-06-22 14:37:45 -0700 | [diff] [blame] | 4647 | binder_selftest_alloc(&proc->alloc); |
| 4648 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4649 | trace_binder_ioctl(cmd, arg); |
| 4650 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4651 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4652 | if (ret) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4653 | goto err_unlocked; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4654 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4655 | thread = binder_get_thread(proc); |
| 4656 | if (thread == NULL) { |
| 4657 | ret = -ENOMEM; |
| 4658 | goto err; |
| 4659 | } |
| 4660 | |
| 4661 | switch (cmd) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4662 | case BINDER_WRITE_READ: |
| 4663 | ret = binder_ioctl_write_read(filp, cmd, arg, thread); |
| 4664 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4665 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4666 | break; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4667 | case BINDER_SET_MAX_THREADS: { |
| 4668 | int max_threads; |
| 4669 | |
| 4670 | if (copy_from_user(&max_threads, ubuf, |
| 4671 | sizeof(max_threads))) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4672 | ret = -EINVAL; |
| 4673 | goto err; |
| 4674 | } |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4675 | binder_inner_proc_lock(proc); |
| 4676 | proc->max_threads = max_threads; |
| 4677 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4678 | break; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4679 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4680 | case BINDER_SET_CONTEXT_MGR: |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4681 | ret = binder_ioctl_set_ctx_mgr(filp); |
| 4682 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4683 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4684 | break; |
| 4685 | case BINDER_THREAD_EXIT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4686 | binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4687 | proc->pid, thread->pid); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4688 | binder_thread_release(proc, thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4689 | thread = NULL; |
| 4690 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4691 | case BINDER_VERSION: { |
| 4692 | struct binder_version __user *ver = ubuf; |
| 4693 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4694 | if (size != sizeof(struct binder_version)) { |
| 4695 | ret = -EINVAL; |
| 4696 | goto err; |
| 4697 | } |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4698 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, |
| 4699 | &ver->protocol_version)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4700 | ret = -EINVAL; |
| 4701 | goto err; |
| 4702 | } |
| 4703 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4704 | } |
Colin Cross | 833babb3 | 2017-06-20 13:54:44 -0700 | [diff] [blame] | 4705 | case BINDER_GET_NODE_DEBUG_INFO: { |
| 4706 | struct binder_node_debug_info info; |
| 4707 | |
| 4708 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 4709 | ret = -EFAULT; |
| 4710 | goto err; |
| 4711 | } |
| 4712 | |
| 4713 | ret = binder_ioctl_get_node_debug_info(proc, &info); |
| 4714 | if (ret < 0) |
| 4715 | goto err; |
| 4716 | |
| 4717 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 4718 | ret = -EFAULT; |
| 4719 | goto err; |
| 4720 | } |
| 4721 | break; |
| 4722 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4723 | default: |
| 4724 | ret = -EINVAL; |
| 4725 | goto err; |
| 4726 | } |
| 4727 | ret = 0; |
| 4728 | err: |
| 4729 | if (thread) |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 4730 | thread->looper_need_return = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4731 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4732 | if (ret && ret != -ERESTARTSYS) |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4733 | 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] | 4734 | err_unlocked: |
| 4735 | trace_binder_ioctl_done(ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4736 | return ret; |
| 4737 | } |
| 4738 | |
| 4739 | static void binder_vma_open(struct vm_area_struct *vma) |
| 4740 | { |
| 4741 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4742 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4743 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4744 | "%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] | 4745 | proc->pid, vma->vm_start, vma->vm_end, |
| 4746 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4747 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | static void binder_vma_close(struct vm_area_struct *vma) |
| 4751 | { |
| 4752 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4753 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4754 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4755 | "%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] | 4756 | proc->pid, vma->vm_start, vma->vm_end, |
| 4757 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4758 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4759 | binder_alloc_vma_close(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4760 | binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES); |
| 4761 | } |
| 4762 | |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4763 | static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 4764 | { |
| 4765 | return VM_FAULT_SIGBUS; |
| 4766 | } |
| 4767 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 4768 | static const struct vm_operations_struct binder_vm_ops = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4769 | .open = binder_vma_open, |
| 4770 | .close = binder_vma_close, |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4771 | .fault = binder_vm_fault, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4772 | }; |
| 4773 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4774 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) |
| 4775 | { |
| 4776 | int ret; |
| 4777 | struct binder_proc *proc = filp->private_data; |
| 4778 | const char *failure_string; |
| 4779 | |
| 4780 | if (proc->tsk != current->group_leader) |
| 4781 | return -EINVAL; |
| 4782 | |
| 4783 | if ((vma->vm_end - vma->vm_start) > SZ_4M) |
| 4784 | vma->vm_end = vma->vm_start + SZ_4M; |
| 4785 | |
| 4786 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 4787 | "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 4788 | __func__, proc->pid, vma->vm_start, vma->vm_end, |
| 4789 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4790 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 4791 | |
| 4792 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { |
| 4793 | ret = -EPERM; |
| 4794 | failure_string = "bad vm_flags"; |
| 4795 | goto err_bad_arg; |
| 4796 | } |
| 4797 | vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE; |
| 4798 | vma->vm_ops = &binder_vm_ops; |
| 4799 | vma->vm_private_data = proc; |
| 4800 | |
| 4801 | ret = binder_alloc_mmap_handler(&proc->alloc, vma); |
| 4802 | if (ret) |
| 4803 | return ret; |
| 4804 | proc->files = get_files_struct(current); |
| 4805 | return 0; |
| 4806 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4807 | err_bad_arg: |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 4808 | pr_err("binder_mmap: %d %lx-%lx %s failed %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4809 | proc->pid, vma->vm_start, vma->vm_end, failure_string, ret); |
| 4810 | return ret; |
| 4811 | } |
| 4812 | |
| 4813 | static int binder_open(struct inode *nodp, struct file *filp) |
| 4814 | { |
| 4815 | struct binder_proc *proc; |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 4816 | struct binder_device *binder_dev; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4817 | |
| 4818 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n", |
| 4819 | current->group_leader->pid, current->pid); |
| 4820 | |
| 4821 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 4822 | if (proc == NULL) |
| 4823 | return -ENOMEM; |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 4824 | spin_lock_init(&proc->inner_lock); |
| 4825 | spin_lock_init(&proc->outer_lock); |
Martijn Coenen | 872c26e | 2017-03-07 15:51:18 +0100 | [diff] [blame] | 4826 | get_task_struct(current->group_leader); |
| 4827 | proc->tsk = current->group_leader; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4828 | INIT_LIST_HEAD(&proc->todo); |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 4829 | if (binder_supported_policy(current->policy)) { |
| 4830 | proc->default_priority.sched_policy = current->policy; |
| 4831 | proc->default_priority.prio = current->normal_prio; |
| 4832 | } else { |
| 4833 | proc->default_priority.sched_policy = SCHED_NORMAL; |
| 4834 | proc->default_priority.prio = NICE_TO_PRIO(0); |
| 4835 | } |
| 4836 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 4837 | binder_dev = container_of(filp->private_data, struct binder_device, |
| 4838 | miscdev); |
| 4839 | proc->context = &binder_dev->context; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4840 | binder_alloc_init(&proc->alloc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4841 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4842 | binder_stats_created(BINDER_STAT_PROC); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4843 | proc->pid = current->group_leader->pid; |
| 4844 | INIT_LIST_HEAD(&proc->delivered_death); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4845 | INIT_LIST_HEAD(&proc->waiting_threads); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4846 | filp->private_data = proc; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4847 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4848 | mutex_lock(&binder_procs_lock); |
| 4849 | hlist_add_head(&proc->proc_node, &binder_procs); |
| 4850 | mutex_unlock(&binder_procs_lock); |
| 4851 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4852 | if (binder_debugfs_dir_entry_proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4853 | char strbuf[11]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4854 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4855 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 4856 | /* |
| 4857 | * proc debug entries are shared between contexts, so |
| 4858 | * this will fail if the process tries to open the driver |
| 4859 | * again with a different context. The priting code will |
| 4860 | * anyway print all contexts that a given PID has, so this |
| 4861 | * is not a problem. |
| 4862 | */ |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4863 | proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO, |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 4864 | binder_debugfs_dir_entry_proc, |
| 4865 | (void *)(unsigned long)proc->pid, |
| 4866 | &binder_proc_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4867 | } |
| 4868 | |
| 4869 | return 0; |
| 4870 | } |
| 4871 | |
| 4872 | static int binder_flush(struct file *filp, fl_owner_t id) |
| 4873 | { |
| 4874 | struct binder_proc *proc = filp->private_data; |
| 4875 | |
| 4876 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); |
| 4877 | |
| 4878 | return 0; |
| 4879 | } |
| 4880 | |
| 4881 | static void binder_deferred_flush(struct binder_proc *proc) |
| 4882 | { |
| 4883 | struct rb_node *n; |
| 4884 | int wake_count = 0; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4885 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4886 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4887 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 4888 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4889 | |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 4890 | thread->looper_need_return = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4891 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { |
| 4892 | wake_up_interruptible(&thread->wait); |
| 4893 | wake_count++; |
| 4894 | } |
| 4895 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4896 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4897 | |
| 4898 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 4899 | "binder_flush: %d woke %d threads\n", proc->pid, |
| 4900 | wake_count); |
| 4901 | } |
| 4902 | |
| 4903 | static int binder_release(struct inode *nodp, struct file *filp) |
| 4904 | { |
| 4905 | struct binder_proc *proc = filp->private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4906 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4907 | debugfs_remove(proc->debugfs_entry); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4908 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); |
| 4909 | |
| 4910 | return 0; |
| 4911 | } |
| 4912 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4913 | static int binder_node_release(struct binder_node *node, int refs) |
| 4914 | { |
| 4915 | struct binder_ref *ref; |
| 4916 | int death = 0; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4917 | struct binder_proc *proc = node->proc; |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4918 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4919 | binder_release_work(proc, &node->async_todo); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4920 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4921 | binder_node_lock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4922 | binder_inner_proc_lock(proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4923 | binder_dequeue_work_ilocked(&node->work); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4924 | /* |
| 4925 | * The caller must have taken a temporary ref on the node, |
| 4926 | */ |
| 4927 | BUG_ON(!node->tmp_refs); |
| 4928 | if (hlist_empty(&node->refs) && node->tmp_refs == 1) { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4929 | binder_inner_proc_unlock(proc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4930 | binder_node_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4931 | binder_free_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4932 | |
| 4933 | return refs; |
| 4934 | } |
| 4935 | |
| 4936 | node->proc = NULL; |
| 4937 | node->local_strong_refs = 0; |
| 4938 | node->local_weak_refs = 0; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4939 | binder_inner_proc_unlock(proc); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4940 | |
| 4941 | spin_lock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4942 | hlist_add_head(&node->dead_node, &binder_dead_nodes); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4943 | spin_unlock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4944 | |
| 4945 | hlist_for_each_entry(ref, &node->refs, node_entry) { |
| 4946 | refs++; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4947 | /* |
| 4948 | * Need the node lock to synchronize |
| 4949 | * with new notification requests and the |
| 4950 | * inner lock to synchronize with queued |
| 4951 | * death notifications. |
| 4952 | */ |
| 4953 | binder_inner_proc_lock(ref->proc); |
| 4954 | if (!ref->death) { |
| 4955 | binder_inner_proc_unlock(ref->proc); |
Arve Hjønnevåg | e194fd8 | 2014-02-17 13:58:29 -0800 | [diff] [blame] | 4956 | continue; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4957 | } |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4958 | |
| 4959 | death++; |
| 4960 | |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4961 | BUG_ON(!list_empty(&ref->death->work.entry)); |
| 4962 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 4963 | binder_enqueue_work_ilocked(&ref->death->work, |
| 4964 | &ref->proc->todo); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 4965 | binder_wakeup_proc_ilocked(ref->proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4966 | binder_inner_proc_unlock(ref->proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4967 | } |
| 4968 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4969 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 4970 | "node %d now dead, refs %d, death %d\n", |
| 4971 | node->debug_id, refs, death); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4972 | binder_node_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4973 | binder_put_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4974 | |
| 4975 | return refs; |
| 4976 | } |
| 4977 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4978 | static void binder_deferred_release(struct binder_proc *proc) |
| 4979 | { |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4980 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4981 | struct rb_node *n; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 4982 | int threads, nodes, incoming_refs, outgoing_refs, active_transactions; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4983 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4984 | BUG_ON(proc->files); |
| 4985 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4986 | mutex_lock(&binder_procs_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4987 | hlist_del(&proc->proc_node); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4988 | mutex_unlock(&binder_procs_lock); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4989 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4990 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4991 | if (context->binder_context_mgr_node && |
| 4992 | context->binder_context_mgr_node->proc == proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4993 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 4994 | "%s: %d context_mgr_node gone\n", |
| 4995 | __func__, proc->pid); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4996 | context->binder_context_mgr_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4997 | } |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4998 | mutex_unlock(&context->context_mgr_node_lock); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4999 | binder_inner_proc_lock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5000 | /* |
| 5001 | * Make sure proc stays alive after we |
| 5002 | * remove all the threads |
| 5003 | */ |
| 5004 | proc->tmp_ref++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5005 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5006 | proc->is_dead = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5007 | threads = 0; |
| 5008 | active_transactions = 0; |
| 5009 | while ((n = rb_first(&proc->threads))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5010 | struct binder_thread *thread; |
| 5011 | |
| 5012 | thread = rb_entry(n, struct binder_thread, rb_node); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5013 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5014 | threads++; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5015 | active_transactions += binder_thread_release(proc, thread); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5016 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5017 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5018 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5019 | nodes = 0; |
| 5020 | incoming_refs = 0; |
| 5021 | while ((n = rb_first(&proc->nodes))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5022 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5023 | |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5024 | node = rb_entry(n, struct binder_node, rb_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5025 | nodes++; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5026 | /* |
| 5027 | * take a temporary ref on the node before |
| 5028 | * calling binder_node_release() which will either |
| 5029 | * kfree() the node or call binder_put_node() |
| 5030 | */ |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5031 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5032 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5033 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5034 | incoming_refs = binder_node_release(node, incoming_refs); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5035 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5036 | } |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5037 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5038 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5039 | outgoing_refs = 0; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5040 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5041 | while ((n = rb_first(&proc->refs_by_desc))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5042 | struct binder_ref *ref; |
| 5043 | |
| 5044 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5045 | outgoing_refs++; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5046 | binder_cleanup_ref_olocked(ref); |
| 5047 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5048 | binder_free_ref(ref); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5049 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5050 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5051 | binder_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5052 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5053 | binder_release_work(proc, &proc->todo); |
| 5054 | binder_release_work(proc, &proc->delivered_death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5055 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5056 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5057 | "%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] | 5058 | __func__, proc->pid, threads, nodes, incoming_refs, |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5059 | outgoing_refs, active_transactions); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5060 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5061 | binder_proc_dec_tmpref(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5062 | } |
| 5063 | |
| 5064 | static void binder_deferred_func(struct work_struct *work) |
| 5065 | { |
| 5066 | struct binder_proc *proc; |
| 5067 | struct files_struct *files; |
| 5068 | |
| 5069 | int defer; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5070 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5071 | do { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5072 | mutex_lock(&binder_deferred_lock); |
| 5073 | if (!hlist_empty(&binder_deferred_list)) { |
| 5074 | proc = hlist_entry(binder_deferred_list.first, |
| 5075 | struct binder_proc, deferred_work_node); |
| 5076 | hlist_del_init(&proc->deferred_work_node); |
| 5077 | defer = proc->deferred_work; |
| 5078 | proc->deferred_work = 0; |
| 5079 | } else { |
| 5080 | proc = NULL; |
| 5081 | defer = 0; |
| 5082 | } |
| 5083 | mutex_unlock(&binder_deferred_lock); |
| 5084 | |
| 5085 | files = NULL; |
| 5086 | if (defer & BINDER_DEFERRED_PUT_FILES) { |
| 5087 | files = proc->files; |
| 5088 | if (files) |
| 5089 | proc->files = NULL; |
| 5090 | } |
| 5091 | |
| 5092 | if (defer & BINDER_DEFERRED_FLUSH) |
| 5093 | binder_deferred_flush(proc); |
| 5094 | |
| 5095 | if (defer & BINDER_DEFERRED_RELEASE) |
| 5096 | binder_deferred_release(proc); /* frees proc */ |
| 5097 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5098 | if (files) |
| 5099 | put_files_struct(files); |
| 5100 | } while (proc); |
| 5101 | } |
| 5102 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); |
| 5103 | |
| 5104 | static void |
| 5105 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) |
| 5106 | { |
| 5107 | mutex_lock(&binder_deferred_lock); |
| 5108 | proc->deferred_work |= defer; |
| 5109 | if (hlist_unhashed(&proc->deferred_work_node)) { |
| 5110 | hlist_add_head(&proc->deferred_work_node, |
| 5111 | &binder_deferred_list); |
Bhaktipriya Shridhar | 1beba52 | 2016-08-13 22:16:24 +0530 | [diff] [blame] | 5112 | schedule_work(&binder_deferred_work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5113 | } |
| 5114 | mutex_unlock(&binder_deferred_lock); |
| 5115 | } |
| 5116 | |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5117 | static void print_binder_transaction_ilocked(struct seq_file *m, |
| 5118 | struct binder_proc *proc, |
| 5119 | const char *prefix, |
| 5120 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5121 | { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5122 | struct binder_proc *to_proc; |
| 5123 | struct binder_buffer *buffer = t->buffer; |
| 5124 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5125 | spin_lock(&t->lock); |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5126 | to_proc = t->to_proc; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5127 | seq_printf(m, |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 5128 | "%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] | 5129 | prefix, t->debug_id, t, |
| 5130 | t->from ? t->from->proc->pid : 0, |
| 5131 | t->from ? t->from->pid : 0, |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5132 | to_proc ? to_proc->pid : 0, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5133 | t->to_thread ? t->to_thread->pid : 0, |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 5134 | t->code, t->flags, t->priority.sched_policy, |
| 5135 | t->priority.prio, t->need_reply); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5136 | spin_unlock(&t->lock); |
| 5137 | |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5138 | if (proc != to_proc) { |
| 5139 | /* |
| 5140 | * Can only safely deref buffer if we are holding the |
| 5141 | * correct proc inner lock for this node |
| 5142 | */ |
| 5143 | seq_puts(m, "\n"); |
| 5144 | return; |
| 5145 | } |
| 5146 | |
| 5147 | if (buffer == NULL) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5148 | seq_puts(m, " buffer free\n"); |
| 5149 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5150 | } |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5151 | if (buffer->target_node) |
| 5152 | seq_printf(m, " node %d", buffer->target_node->debug_id); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5153 | seq_printf(m, " size %zd:%zd data %p\n", |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5154 | buffer->data_size, buffer->offsets_size, |
| 5155 | buffer->data); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5156 | } |
| 5157 | |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5158 | static void print_binder_work_ilocked(struct seq_file *m, |
| 5159 | struct binder_proc *proc, |
| 5160 | const char *prefix, |
| 5161 | const char *transaction_prefix, |
| 5162 | struct binder_work *w) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5163 | { |
| 5164 | struct binder_node *node; |
| 5165 | struct binder_transaction *t; |
| 5166 | |
| 5167 | switch (w->type) { |
| 5168 | case BINDER_WORK_TRANSACTION: |
| 5169 | t = container_of(w, struct binder_transaction, work); |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5170 | print_binder_transaction_ilocked( |
| 5171 | m, proc, transaction_prefix, t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5172 | break; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 5173 | case BINDER_WORK_RETURN_ERROR: { |
| 5174 | struct binder_error *e = container_of( |
| 5175 | w, struct binder_error, work); |
| 5176 | |
| 5177 | seq_printf(m, "%stransaction error: %u\n", |
| 5178 | prefix, e->cmd); |
| 5179 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5180 | case BINDER_WORK_TRANSACTION_COMPLETE: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5181 | seq_printf(m, "%stransaction complete\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5182 | break; |
| 5183 | case BINDER_WORK_NODE: |
| 5184 | node = container_of(w, struct binder_node, work); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5185 | seq_printf(m, "%snode work %d: u%016llx c%016llx\n", |
| 5186 | prefix, node->debug_id, |
| 5187 | (u64)node->ptr, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5188 | break; |
| 5189 | case BINDER_WORK_DEAD_BINDER: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5190 | seq_printf(m, "%shas dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5191 | break; |
| 5192 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5193 | seq_printf(m, "%shas cleared dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5194 | break; |
| 5195 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5196 | seq_printf(m, "%shas cleared death notification\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5197 | break; |
| 5198 | default: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5199 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5200 | break; |
| 5201 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5202 | } |
| 5203 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5204 | static void print_binder_thread_ilocked(struct seq_file *m, |
| 5205 | struct binder_thread *thread, |
| 5206 | int print_always) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5207 | { |
| 5208 | struct binder_transaction *t; |
| 5209 | struct binder_work *w; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5210 | size_t start_pos = m->count; |
| 5211 | size_t header_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5212 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5213 | 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] | 5214 | thread->pid, thread->looper, |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5215 | thread->looper_need_return, |
| 5216 | atomic_read(&thread->tmp_ref)); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5217 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5218 | t = thread->transaction_stack; |
| 5219 | while (t) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5220 | if (t->from == thread) { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5221 | print_binder_transaction_ilocked(m, thread->proc, |
| 5222 | " outgoing transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5223 | t = t->from_parent; |
| 5224 | } else if (t->to_thread == thread) { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5225 | print_binder_transaction_ilocked(m, thread->proc, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5226 | " incoming transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5227 | t = t->to_parent; |
| 5228 | } else { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5229 | print_binder_transaction_ilocked(m, thread->proc, |
| 5230 | " bad transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5231 | t = NULL; |
| 5232 | } |
| 5233 | } |
| 5234 | list_for_each_entry(w, &thread->todo, entry) { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5235 | print_binder_work_ilocked(m, thread->proc, " ", |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5236 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5237 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5238 | if (!print_always && m->count == header_pos) |
| 5239 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5240 | } |
| 5241 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5242 | static void print_binder_node_nilocked(struct seq_file *m, |
| 5243 | struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5244 | { |
| 5245 | struct binder_ref *ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5246 | struct binder_work *w; |
| 5247 | int count; |
| 5248 | |
| 5249 | count = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5250 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5251 | count++; |
| 5252 | |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 5253 | 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] | 5254 | node->debug_id, (u64)node->ptr, (u64)node->cookie, |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 5255 | node->sched_policy, node->min_priority, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5256 | node->has_strong_ref, node->has_weak_ref, |
| 5257 | node->local_strong_refs, node->local_weak_refs, |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5258 | node->internal_strong_refs, count, node->tmp_refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5259 | if (count) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5260 | seq_puts(m, " proc"); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5261 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5262 | seq_printf(m, " %d", ref->proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5263 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5264 | seq_puts(m, "\n"); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5265 | if (node->proc) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5266 | list_for_each_entry(w, &node->async_todo, entry) |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5267 | print_binder_work_ilocked(m, node->proc, " ", |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5268 | " pending async transaction", w); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5269 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5270 | } |
| 5271 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5272 | static void print_binder_ref_olocked(struct seq_file *m, |
| 5273 | struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5274 | { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5275 | binder_node_lock(ref->node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5276 | seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n", |
| 5277 | ref->data.debug_id, ref->data.desc, |
| 5278 | ref->node->proc ? "" : "dead ", |
| 5279 | ref->node->debug_id, ref->data.strong, |
| 5280 | ref->data.weak, ref->death); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5281 | binder_node_unlock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5282 | } |
| 5283 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5284 | static void print_binder_proc(struct seq_file *m, |
| 5285 | struct binder_proc *proc, int print_all) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5286 | { |
| 5287 | struct binder_work *w; |
| 5288 | struct rb_node *n; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5289 | size_t start_pos = m->count; |
| 5290 | size_t header_pos; |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5291 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5292 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5293 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5294 | seq_printf(m, "context %s\n", proc->context->name); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5295 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5296 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5297 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5298 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5299 | 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] | 5300 | rb_node), print_all); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5301 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5302 | 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] | 5303 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 5304 | rb_node); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5305 | /* |
| 5306 | * take a temporary reference on the node so it |
| 5307 | * survives and isn't removed from the tree |
| 5308 | * while we print it. |
| 5309 | */ |
| 5310 | binder_inc_node_tmpref_ilocked(node); |
| 5311 | /* Need to drop inner lock to take node lock */ |
| 5312 | binder_inner_proc_unlock(proc); |
| 5313 | if (last_node) |
| 5314 | binder_put_node(last_node); |
| 5315 | binder_node_inner_lock(node); |
| 5316 | print_binder_node_nilocked(m, node); |
| 5317 | binder_node_inner_unlock(node); |
| 5318 | last_node = node; |
| 5319 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5320 | } |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5321 | binder_inner_proc_unlock(proc); |
| 5322 | if (last_node) |
| 5323 | binder_put_node(last_node); |
| 5324 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5325 | if (print_all) { |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5326 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5327 | for (n = rb_first(&proc->refs_by_desc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5328 | n != NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5329 | n = rb_next(n)) |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5330 | print_binder_ref_olocked(m, rb_entry(n, |
| 5331 | struct binder_ref, |
| 5332 | rb_node_desc)); |
| 5333 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5334 | } |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5335 | binder_alloc_print_allocated(m, &proc->alloc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5336 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5337 | list_for_each_entry(w, &proc->todo, entry) |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5338 | print_binder_work_ilocked(m, proc, " ", |
| 5339 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5340 | list_for_each_entry(w, &proc->delivered_death, entry) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5341 | seq_puts(m, " has delivered dead binder\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5342 | break; |
| 5343 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5344 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5345 | if (!print_all && m->count == header_pos) |
| 5346 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5347 | } |
| 5348 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5349 | static const char * const binder_return_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5350 | "BR_ERROR", |
| 5351 | "BR_OK", |
| 5352 | "BR_TRANSACTION", |
| 5353 | "BR_REPLY", |
| 5354 | "BR_ACQUIRE_RESULT", |
| 5355 | "BR_DEAD_REPLY", |
| 5356 | "BR_TRANSACTION_COMPLETE", |
| 5357 | "BR_INCREFS", |
| 5358 | "BR_ACQUIRE", |
| 5359 | "BR_RELEASE", |
| 5360 | "BR_DECREFS", |
| 5361 | "BR_ATTEMPT_ACQUIRE", |
| 5362 | "BR_NOOP", |
| 5363 | "BR_SPAWN_LOOPER", |
| 5364 | "BR_FINISHED", |
| 5365 | "BR_DEAD_BINDER", |
| 5366 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 5367 | "BR_FAILED_REPLY" |
| 5368 | }; |
| 5369 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5370 | static const char * const binder_command_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5371 | "BC_TRANSACTION", |
| 5372 | "BC_REPLY", |
| 5373 | "BC_ACQUIRE_RESULT", |
| 5374 | "BC_FREE_BUFFER", |
| 5375 | "BC_INCREFS", |
| 5376 | "BC_ACQUIRE", |
| 5377 | "BC_RELEASE", |
| 5378 | "BC_DECREFS", |
| 5379 | "BC_INCREFS_DONE", |
| 5380 | "BC_ACQUIRE_DONE", |
| 5381 | "BC_ATTEMPT_ACQUIRE", |
| 5382 | "BC_REGISTER_LOOPER", |
| 5383 | "BC_ENTER_LOOPER", |
| 5384 | "BC_EXIT_LOOPER", |
| 5385 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 5386 | "BC_CLEAR_DEATH_NOTIFICATION", |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 5387 | "BC_DEAD_BINDER_DONE", |
| 5388 | "BC_TRANSACTION_SG", |
| 5389 | "BC_REPLY_SG", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5390 | }; |
| 5391 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5392 | static const char * const binder_objstat_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5393 | "proc", |
| 5394 | "thread", |
| 5395 | "node", |
| 5396 | "ref", |
| 5397 | "death", |
| 5398 | "transaction", |
| 5399 | "transaction_complete" |
| 5400 | }; |
| 5401 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5402 | static void print_binder_stats(struct seq_file *m, const char *prefix, |
| 5403 | struct binder_stats *stats) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5404 | { |
| 5405 | int i; |
| 5406 | |
| 5407 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5408 | ARRAY_SIZE(binder_command_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5409 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5410 | int temp = atomic_read(&stats->bc[i]); |
| 5411 | |
| 5412 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5413 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5414 | binder_command_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5415 | } |
| 5416 | |
| 5417 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5418 | ARRAY_SIZE(binder_return_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5419 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5420 | int temp = atomic_read(&stats->br[i]); |
| 5421 | |
| 5422 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5423 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5424 | binder_return_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5425 | } |
| 5426 | |
| 5427 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5428 | ARRAY_SIZE(binder_objstat_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5429 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5430 | ARRAY_SIZE(stats->obj_deleted)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5431 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5432 | int created = atomic_read(&stats->obj_created[i]); |
| 5433 | int deleted = atomic_read(&stats->obj_deleted[i]); |
| 5434 | |
| 5435 | if (created || deleted) |
| 5436 | seq_printf(m, "%s%s: active %d total %d\n", |
| 5437 | prefix, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5438 | binder_objstat_strings[i], |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5439 | created - deleted, |
| 5440 | created); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5441 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5442 | } |
| 5443 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5444 | static void print_binder_proc_stats(struct seq_file *m, |
| 5445 | struct binder_proc *proc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5446 | { |
| 5447 | struct binder_work *w; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5448 | struct binder_thread *thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5449 | struct rb_node *n; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5450 | int count, strong, weak, ready_threads; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5451 | size_t free_async_space = |
| 5452 | binder_alloc_get_free_async_space(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5453 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5454 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5455 | seq_printf(m, "context %s\n", proc->context->name); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5456 | count = 0; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5457 | ready_threads = 0; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5458 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5459 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 5460 | count++; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5461 | |
| 5462 | list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node) |
| 5463 | ready_threads++; |
| 5464 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5465 | seq_printf(m, " threads: %d\n", count); |
| 5466 | seq_printf(m, " requested threads: %d+%d/%d\n" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5467 | " ready threads %d\n" |
| 5468 | " free async space %zd\n", proc->requested_threads, |
| 5469 | proc->requested_threads_started, proc->max_threads, |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5470 | ready_threads, |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5471 | free_async_space); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5472 | count = 0; |
| 5473 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) |
| 5474 | count++; |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5475 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5476 | seq_printf(m, " nodes: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5477 | count = 0; |
| 5478 | strong = 0; |
| 5479 | weak = 0; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5480 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5481 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 5482 | struct binder_ref *ref = rb_entry(n, struct binder_ref, |
| 5483 | rb_node_desc); |
| 5484 | count++; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5485 | strong += ref->data.strong; |
| 5486 | weak += ref->data.weak; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5487 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5488 | binder_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5489 | 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] | 5490 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5491 | count = binder_alloc_get_allocated_count(&proc->alloc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5492 | seq_printf(m, " buffers: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5493 | |
Sherry Yang | 9100442 | 2017-08-22 17:26:57 -0700 | [diff] [blame] | 5494 | binder_alloc_print_pages(m, &proc->alloc); |
| 5495 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5496 | count = 0; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5497 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5498 | list_for_each_entry(w, &proc->todo, entry) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5499 | if (w->type == BINDER_WORK_TRANSACTION) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5500 | count++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5501 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5502 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5503 | seq_printf(m, " pending transactions: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5504 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5505 | print_binder_stats(m, " ", &proc->stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5509 | static int binder_state_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5510 | { |
| 5511 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5512 | struct binder_node *node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5513 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5514 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5515 | seq_puts(m, "binder state:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5516 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5517 | spin_lock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5518 | if (!hlist_empty(&binder_dead_nodes)) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5519 | seq_puts(m, "dead nodes:\n"); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5520 | hlist_for_each_entry(node, &binder_dead_nodes, dead_node) { |
| 5521 | /* |
| 5522 | * take a temporary reference on the node so it |
| 5523 | * survives and isn't removed from the list |
| 5524 | * while we print it. |
| 5525 | */ |
| 5526 | node->tmp_refs++; |
| 5527 | spin_unlock(&binder_dead_nodes_lock); |
| 5528 | if (last_node) |
| 5529 | binder_put_node(last_node); |
| 5530 | binder_node_lock(node); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5531 | print_binder_node_nilocked(m, node); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5532 | binder_node_unlock(node); |
| 5533 | last_node = node; |
| 5534 | spin_lock(&binder_dead_nodes_lock); |
| 5535 | } |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5536 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5537 | if (last_node) |
| 5538 | binder_put_node(last_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5539 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5540 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5541 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5542 | print_binder_proc(m, proc, 1); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5543 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5544 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5545 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5546 | } |
| 5547 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5548 | static int binder_stats_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5549 | { |
| 5550 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5551 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5552 | seq_puts(m, "binder stats:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5553 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5554 | print_binder_stats(m, "", &binder_stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5555 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5556 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5557 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5558 | print_binder_proc_stats(m, proc); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5559 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5560 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5561 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5562 | } |
| 5563 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5564 | static int binder_transactions_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5565 | { |
| 5566 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5567 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5568 | seq_puts(m, "binder transactions:\n"); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5569 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5570 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5571 | print_binder_proc(m, proc, 0); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5572 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5573 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5574 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5575 | } |
| 5576 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5577 | static int binder_proc_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5578 | { |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5579 | struct binder_proc *itr; |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5580 | int pid = (unsigned long)m->private; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5581 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5582 | mutex_lock(&binder_procs_lock); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5583 | hlist_for_each_entry(itr, &binder_procs, proc_node) { |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5584 | if (itr->pid == pid) { |
| 5585 | seq_puts(m, "binder proc state:\n"); |
| 5586 | print_binder_proc(m, itr, 1); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5587 | } |
| 5588 | } |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5589 | mutex_unlock(&binder_procs_lock); |
| 5590 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5591 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5592 | } |
| 5593 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5594 | static void print_binder_transaction_log_entry(struct seq_file *m, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5595 | struct binder_transaction_log_entry *e) |
| 5596 | { |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5597 | int debug_id = READ_ONCE(e->debug_id_done); |
| 5598 | /* |
| 5599 | * read barrier to guarantee debug_id_done read before |
| 5600 | * we print the log values |
| 5601 | */ |
| 5602 | smp_rmb(); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5603 | seq_printf(m, |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5604 | "%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] | 5605 | e->debug_id, (e->call_type == 2) ? "reply" : |
| 5606 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5607 | e->from_thread, e->to_proc, e->to_thread, e->context_name, |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 5608 | e->to_node, e->target_handle, e->data_size, e->offsets_size, |
| 5609 | e->return_error, e->return_error_param, |
| 5610 | e->return_error_line); |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5611 | /* |
| 5612 | * read-barrier to guarantee read of debug_id_done after |
| 5613 | * done printing the fields of the entry |
| 5614 | */ |
| 5615 | smp_rmb(); |
| 5616 | seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ? |
| 5617 | "\n" : " (incomplete)\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5618 | } |
| 5619 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5620 | 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] | 5621 | { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5622 | struct binder_transaction_log *log = m->private; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5623 | unsigned int log_cur = atomic_read(&log->cur); |
| 5624 | unsigned int count; |
| 5625 | unsigned int cur; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5626 | int i; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5627 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5628 | count = log_cur + 1; |
| 5629 | cur = count < ARRAY_SIZE(log->entry) && !log->full ? |
| 5630 | 0 : count % ARRAY_SIZE(log->entry); |
| 5631 | if (count > ARRAY_SIZE(log->entry) || log->full) |
| 5632 | count = ARRAY_SIZE(log->entry); |
| 5633 | for (i = 0; i < count; i++) { |
| 5634 | unsigned int index = cur++ % ARRAY_SIZE(log->entry); |
| 5635 | |
| 5636 | print_binder_transaction_log_entry(m, &log->entry[index]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5637 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5638 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5639 | } |
| 5640 | |
| 5641 | static const struct file_operations binder_fops = { |
| 5642 | .owner = THIS_MODULE, |
| 5643 | .poll = binder_poll, |
| 5644 | .unlocked_ioctl = binder_ioctl, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5645 | .compat_ioctl = binder_ioctl, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5646 | .mmap = binder_mmap, |
| 5647 | .open = binder_open, |
| 5648 | .flush = binder_flush, |
| 5649 | .release = binder_release, |
| 5650 | }; |
| 5651 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5652 | BINDER_DEBUG_ENTRY(state); |
| 5653 | BINDER_DEBUG_ENTRY(stats); |
| 5654 | BINDER_DEBUG_ENTRY(transactions); |
| 5655 | BINDER_DEBUG_ENTRY(transaction_log); |
| 5656 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5657 | static int __init init_binder_device(const char *name) |
| 5658 | { |
| 5659 | int ret; |
| 5660 | struct binder_device *binder_device; |
| 5661 | |
| 5662 | binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL); |
| 5663 | if (!binder_device) |
| 5664 | return -ENOMEM; |
| 5665 | |
| 5666 | binder_device->miscdev.fops = &binder_fops; |
| 5667 | binder_device->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 5668 | binder_device->miscdev.name = name; |
| 5669 | |
| 5670 | binder_device->context.binder_context_mgr_uid = INVALID_UID; |
| 5671 | binder_device->context.name = name; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5672 | mutex_init(&binder_device->context.context_mgr_node_lock); |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5673 | |
| 5674 | ret = misc_register(&binder_device->miscdev); |
| 5675 | if (ret < 0) { |
| 5676 | kfree(binder_device); |
| 5677 | return ret; |
| 5678 | } |
| 5679 | |
| 5680 | hlist_add_head(&binder_device->hlist, &binder_devices); |
| 5681 | |
| 5682 | return ret; |
| 5683 | } |
| 5684 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5685 | static int __init binder_init(void) |
| 5686 | { |
| 5687 | int ret; |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5688 | char *device_name, *device_names; |
| 5689 | struct binder_device *device; |
| 5690 | struct hlist_node *tmp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5691 | |
Sherry Yang | 5828d70 | 2017-07-29 13:24:11 -0700 | [diff] [blame] | 5692 | binder_alloc_shrinker_init(); |
| 5693 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5694 | atomic_set(&binder_transaction_log.cur, ~0U); |
| 5695 | atomic_set(&binder_transaction_log_failed.cur, ~0U); |
| 5696 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5697 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); |
| 5698 | if (binder_debugfs_dir_entry_root) |
| 5699 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", |
| 5700 | binder_debugfs_dir_entry_root); |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5701 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5702 | if (binder_debugfs_dir_entry_root) { |
| 5703 | debugfs_create_file("state", |
| 5704 | S_IRUGO, |
| 5705 | binder_debugfs_dir_entry_root, |
| 5706 | NULL, |
| 5707 | &binder_state_fops); |
| 5708 | debugfs_create_file("stats", |
| 5709 | S_IRUGO, |
| 5710 | binder_debugfs_dir_entry_root, |
| 5711 | NULL, |
| 5712 | &binder_stats_fops); |
| 5713 | debugfs_create_file("transactions", |
| 5714 | S_IRUGO, |
| 5715 | binder_debugfs_dir_entry_root, |
| 5716 | NULL, |
| 5717 | &binder_transactions_fops); |
| 5718 | debugfs_create_file("transaction_log", |
| 5719 | S_IRUGO, |
| 5720 | binder_debugfs_dir_entry_root, |
| 5721 | &binder_transaction_log, |
| 5722 | &binder_transaction_log_fops); |
| 5723 | debugfs_create_file("failed_transaction_log", |
| 5724 | S_IRUGO, |
| 5725 | binder_debugfs_dir_entry_root, |
| 5726 | &binder_transaction_log_failed, |
| 5727 | &binder_transaction_log_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5728 | } |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5729 | |
| 5730 | /* |
| 5731 | * Copy the module_parameter string, because we don't want to |
| 5732 | * tokenize it in-place. |
| 5733 | */ |
| 5734 | device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL); |
| 5735 | if (!device_names) { |
| 5736 | ret = -ENOMEM; |
| 5737 | goto err_alloc_device_names_failed; |
| 5738 | } |
| 5739 | strcpy(device_names, binder_devices_param); |
| 5740 | |
| 5741 | while ((device_name = strsep(&device_names, ","))) { |
| 5742 | ret = init_binder_device(device_name); |
| 5743 | if (ret) |
| 5744 | goto err_init_binder_device_failed; |
| 5745 | } |
| 5746 | |
| 5747 | return ret; |
| 5748 | |
| 5749 | err_init_binder_device_failed: |
| 5750 | hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) { |
| 5751 | misc_deregister(&device->miscdev); |
| 5752 | hlist_del(&device->hlist); |
| 5753 | kfree(device); |
| 5754 | } |
| 5755 | err_alloc_device_names_failed: |
| 5756 | debugfs_remove_recursive(binder_debugfs_dir_entry_root); |
| 5757 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5758 | return ret; |
| 5759 | } |
| 5760 | |
| 5761 | device_initcall(binder_init); |
| 5762 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5763 | #define CREATE_TRACE_POINTS |
| 5764 | #include "binder_trace.h" |
| 5765 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5766 | MODULE_LICENSE("GPL v2"); |