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