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