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