Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1 | /* binder.c |
| 2 | * |
| 3 | * Android IPC Subsystem |
| 4 | * |
| 5 | * Copyright (C) 2007-2008 Google, Inc. |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 18 | /* |
| 19 | * Locking overview |
| 20 | * |
| 21 | * There are 3 main spinlocks which must be acquired in the |
| 22 | * order shown: |
| 23 | * |
| 24 | * 1) proc->outer_lock : protects binder_ref |
| 25 | * binder_proc_lock() and binder_proc_unlock() are |
| 26 | * used to acq/rel. |
| 27 | * 2) node->lock : protects most fields of binder_node. |
| 28 | * binder_node_lock() and binder_node_unlock() are |
| 29 | * used to acq/rel |
| 30 | * 3) proc->inner_lock : protects the thread and node lists |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 31 | * (proc->threads, proc->waiting_threads, proc->nodes) |
| 32 | * and all todo lists associated with the binder_proc |
| 33 | * (proc->todo, thread->todo, proc->delivered_death and |
| 34 | * node->async_todo), as well as thread->transaction_stack |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 35 | * binder_inner_proc_lock() and binder_inner_proc_unlock() |
| 36 | * are used to acq/rel |
| 37 | * |
| 38 | * Any lock under procA must never be nested under any lock at the same |
| 39 | * level or below on procB. |
| 40 | * |
| 41 | * Functions that require a lock held on entry indicate which lock |
| 42 | * in the suffix of the function name: |
| 43 | * |
| 44 | * foo_olocked() : requires node->outer_lock |
| 45 | * foo_nlocked() : requires node->lock |
| 46 | * foo_ilocked() : requires proc->inner_lock |
| 47 | * foo_oilocked(): requires proc->outer_lock and proc->inner_lock |
| 48 | * foo_nilocked(): requires node->lock and proc->inner_lock |
| 49 | * ... |
| 50 | */ |
| 51 | |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 52 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 53 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 54 | #include <asm/cacheflush.h> |
| 55 | #include <linux/fdtable.h> |
| 56 | #include <linux/file.h> |
Colin Cross | e2610b2 | 2013-05-06 23:50:15 +0000 | [diff] [blame] | 57 | #include <linux/freezer.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 58 | #include <linux/fs.h> |
| 59 | #include <linux/list.h> |
| 60 | #include <linux/miscdevice.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 61 | #include <linux/module.h> |
| 62 | #include <linux/mutex.h> |
| 63 | #include <linux/nsproxy.h> |
| 64 | #include <linux/poll.h> |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 65 | #include <linux/debugfs.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 66 | #include <linux/rbtree.h> |
| 67 | #include <linux/sched.h> |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 68 | #include <linux/seq_file.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 69 | #include <linux/uaccess.h> |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 70 | #include <linux/pid_namespace.h> |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 71 | #include <linux/security.h> |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 72 | #include <linux/spinlock.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 73 | |
Greg Kroah-Hartman | 9246a4a | 2014-10-16 15:26:51 +0200 | [diff] [blame] | 74 | #include <uapi/linux/android/binder.h> |
Todd Kjos | b934102 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 75 | #include "binder_alloc.h" |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 76 | #include "binder_trace.h" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 77 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 78 | static HLIST_HEAD(binder_deferred_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 79 | static DEFINE_MUTEX(binder_deferred_lock); |
| 80 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 81 | static HLIST_HEAD(binder_devices); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 82 | static HLIST_HEAD(binder_procs); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 83 | static DEFINE_MUTEX(binder_procs_lock); |
| 84 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 85 | static HLIST_HEAD(binder_dead_nodes); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 86 | static DEFINE_SPINLOCK(binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 87 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 88 | static struct dentry *binder_debugfs_dir_entry_root; |
| 89 | static struct dentry *binder_debugfs_dir_entry_proc; |
Todd Kjos | c4bd08b | 2017-05-25 10:56:00 -0700 | [diff] [blame] | 90 | static atomic_t binder_last_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 91 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 92 | #define BINDER_DEBUG_ENTRY(name) \ |
| 93 | static int binder_##name##_open(struct inode *inode, struct file *file) \ |
| 94 | { \ |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 95 | return single_open(file, binder_##name##_show, inode->i_private); \ |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 96 | } \ |
| 97 | \ |
| 98 | static const struct file_operations binder_##name##_fops = { \ |
| 99 | .owner = THIS_MODULE, \ |
| 100 | .open = binder_##name##_open, \ |
| 101 | .read = seq_read, \ |
| 102 | .llseek = seq_lseek, \ |
| 103 | .release = single_release, \ |
| 104 | } |
| 105 | |
| 106 | static int binder_proc_show(struct seq_file *m, void *unused); |
| 107 | BINDER_DEBUG_ENTRY(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 108 | |
| 109 | /* This is only defined in include/asm-arm/sizes.h */ |
| 110 | #ifndef SZ_1K |
| 111 | #define SZ_1K 0x400 |
| 112 | #endif |
| 113 | |
| 114 | #ifndef SZ_4M |
| 115 | #define SZ_4M 0x400000 |
| 116 | #endif |
| 117 | |
| 118 | #define FORBIDDEN_MMAP_FLAGS (VM_WRITE) |
| 119 | |
| 120 | #define BINDER_SMALL_BUF_SIZE (PAGE_SIZE * 64) |
| 121 | |
| 122 | enum { |
| 123 | BINDER_DEBUG_USER_ERROR = 1U << 0, |
| 124 | BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1, |
| 125 | BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2, |
| 126 | BINDER_DEBUG_OPEN_CLOSE = 1U << 3, |
| 127 | BINDER_DEBUG_DEAD_BINDER = 1U << 4, |
| 128 | BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5, |
| 129 | BINDER_DEBUG_READ_WRITE = 1U << 6, |
| 130 | BINDER_DEBUG_USER_REFS = 1U << 7, |
| 131 | BINDER_DEBUG_THREADS = 1U << 8, |
| 132 | BINDER_DEBUG_TRANSACTION = 1U << 9, |
| 133 | BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10, |
| 134 | BINDER_DEBUG_FREE_BUFFER = 1U << 11, |
| 135 | BINDER_DEBUG_INTERNAL_REFS = 1U << 12, |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 136 | BINDER_DEBUG_PRIORITY_CAP = 1U << 13, |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 137 | BINDER_DEBUG_SPINLOCKS = 1U << 14, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 138 | }; |
| 139 | static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR | |
| 140 | BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION; |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 141 | module_param_named(debug_mask, binder_debug_mask, uint, 0644); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 142 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 143 | static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES; |
| 144 | module_param_named(devices, binder_devices_param, charp, S_IRUGO); |
| 145 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 146 | static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait); |
| 147 | static int binder_stop_on_user_error; |
| 148 | |
| 149 | static int binder_set_stop_on_user_error(const char *val, |
Kees Cook | 24da2c8 | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 150 | const struct kernel_param *kp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 151 | { |
| 152 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 153 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 154 | ret = param_set_int(val, kp); |
| 155 | if (binder_stop_on_user_error < 2) |
| 156 | wake_up(&binder_user_error_wait); |
| 157 | return ret; |
| 158 | } |
| 159 | module_param_call(stop_on_user_error, binder_set_stop_on_user_error, |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 160 | param_get_int, &binder_stop_on_user_error, 0644); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 161 | |
| 162 | #define binder_debug(mask, x...) \ |
| 163 | do { \ |
| 164 | if (binder_debug_mask & mask) \ |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 165 | pr_info(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 166 | } while (0) |
| 167 | |
| 168 | #define binder_user_error(x...) \ |
| 169 | do { \ |
| 170 | if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \ |
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 | if (binder_stop_on_user_error) \ |
| 173 | binder_stop_on_user_error = 2; \ |
| 174 | } while (0) |
| 175 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 176 | #define to_flat_binder_object(hdr) \ |
| 177 | container_of(hdr, struct flat_binder_object, hdr) |
| 178 | |
| 179 | #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr) |
| 180 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 181 | #define to_binder_buffer_object(hdr) \ |
| 182 | container_of(hdr, struct binder_buffer_object, hdr) |
| 183 | |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 184 | #define to_binder_fd_array_object(hdr) \ |
| 185 | container_of(hdr, struct binder_fd_array_object, hdr) |
| 186 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 187 | enum binder_stat_types { |
| 188 | BINDER_STAT_PROC, |
| 189 | BINDER_STAT_THREAD, |
| 190 | BINDER_STAT_NODE, |
| 191 | BINDER_STAT_REF, |
| 192 | BINDER_STAT_DEATH, |
| 193 | BINDER_STAT_TRANSACTION, |
| 194 | BINDER_STAT_TRANSACTION_COMPLETE, |
| 195 | BINDER_STAT_COUNT |
| 196 | }; |
| 197 | |
| 198 | struct binder_stats { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 199 | atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1]; |
| 200 | atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1]; |
| 201 | atomic_t obj_created[BINDER_STAT_COUNT]; |
| 202 | atomic_t obj_deleted[BINDER_STAT_COUNT]; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | static struct binder_stats binder_stats; |
| 206 | |
| 207 | static inline void binder_stats_deleted(enum binder_stat_types type) |
| 208 | { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 209 | atomic_inc(&binder_stats.obj_deleted[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static inline void binder_stats_created(enum binder_stat_types type) |
| 213 | { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 214 | atomic_inc(&binder_stats.obj_created[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | struct binder_transaction_log_entry { |
| 218 | int debug_id; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 219 | int debug_id_done; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 220 | int call_type; |
| 221 | int from_proc; |
| 222 | int from_thread; |
| 223 | int target_handle; |
| 224 | int to_proc; |
| 225 | int to_thread; |
| 226 | int to_node; |
| 227 | int data_size; |
| 228 | int offsets_size; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 229 | int return_error_line; |
| 230 | uint32_t return_error; |
| 231 | uint32_t return_error_param; |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 232 | const char *context_name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 233 | }; |
| 234 | struct binder_transaction_log { |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 235 | atomic_t cur; |
| 236 | bool full; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 237 | struct binder_transaction_log_entry entry[32]; |
| 238 | }; |
| 239 | static struct binder_transaction_log binder_transaction_log; |
| 240 | static struct binder_transaction_log binder_transaction_log_failed; |
| 241 | |
| 242 | static struct binder_transaction_log_entry *binder_transaction_log_add( |
| 243 | struct binder_transaction_log *log) |
| 244 | { |
| 245 | struct binder_transaction_log_entry *e; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 246 | unsigned int cur = atomic_inc_return(&log->cur); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 247 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 248 | if (cur >= ARRAY_SIZE(log->entry)) |
Gustavo A. R. Silva | e62dd6f | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 249 | log->full = true; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 250 | e = &log->entry[cur % ARRAY_SIZE(log->entry)]; |
| 251 | WRITE_ONCE(e->debug_id_done, 0); |
| 252 | /* |
| 253 | * write-barrier to synchronize access to e->debug_id_done. |
| 254 | * We make sure the initialized 0 value is seen before |
| 255 | * memset() other fields are zeroed by memset. |
| 256 | */ |
| 257 | smp_wmb(); |
| 258 | memset(e, 0, sizeof(*e)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 259 | return e; |
| 260 | } |
| 261 | |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 262 | struct binder_context { |
| 263 | struct binder_node *binder_context_mgr_node; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 264 | struct mutex context_mgr_node_lock; |
| 265 | |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 266 | kuid_t binder_context_mgr_uid; |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 267 | const char *name; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 268 | }; |
| 269 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 270 | struct binder_device { |
| 271 | struct hlist_node hlist; |
| 272 | struct miscdevice miscdev; |
| 273 | struct binder_context context; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 274 | }; |
| 275 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 276 | /** |
| 277 | * struct binder_work - work enqueued on a worklist |
| 278 | * @entry: node enqueued on list |
| 279 | * @type: type of work to be performed |
| 280 | * |
| 281 | * There are separate work lists for proc, thread, and node (async). |
| 282 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 283 | struct binder_work { |
| 284 | struct list_head entry; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 285 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 286 | enum { |
| 287 | BINDER_WORK_TRANSACTION = 1, |
| 288 | BINDER_WORK_TRANSACTION_COMPLETE, |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 289 | BINDER_WORK_RETURN_ERROR, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 290 | BINDER_WORK_NODE, |
| 291 | BINDER_WORK_DEAD_BINDER, |
| 292 | BINDER_WORK_DEAD_BINDER_AND_CLEAR, |
| 293 | BINDER_WORK_CLEAR_DEATH_NOTIFICATION, |
| 294 | } type; |
| 295 | }; |
| 296 | |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 297 | struct binder_error { |
| 298 | struct binder_work work; |
| 299 | uint32_t cmd; |
| 300 | }; |
| 301 | |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 302 | /** |
| 303 | * struct binder_node - binder node bookkeeping |
| 304 | * @debug_id: unique ID for debugging |
| 305 | * (invariant after initialized) |
| 306 | * @lock: lock for node fields |
| 307 | * @work: worklist element for node work |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 308 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 309 | * @rb_node: element for proc->nodes tree |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 310 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 311 | * @dead_node: element for binder_dead_nodes list |
| 312 | * (protected by binder_dead_nodes_lock) |
| 313 | * @proc: binder_proc that owns this node |
| 314 | * (invariant after initialized) |
| 315 | * @refs: list of references on this node |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 316 | * (protected by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 317 | * @internal_strong_refs: used to take strong references when |
| 318 | * initiating a transaction |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 319 | * (protected by @proc->inner_lock if @proc |
| 320 | * and by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 321 | * @local_weak_refs: weak user refs from local process |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 322 | * (protected by @proc->inner_lock if @proc |
| 323 | * and by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 324 | * @local_strong_refs: strong user refs from local process |
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 | * @tmp_refs: temporary kernel refs |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 328 | * (protected by @proc->inner_lock while @proc |
| 329 | * is valid, and by binder_dead_nodes_lock |
| 330 | * if @proc is NULL. During inc/dec and node release |
| 331 | * it is also protected by @lock to provide safety |
| 332 | * as the node dies and @proc becomes NULL) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 333 | * @ptr: userspace pointer for node |
| 334 | * (invariant, no lock needed) |
| 335 | * @cookie: userspace cookie for node |
| 336 | * (invariant, no lock needed) |
| 337 | * @has_strong_ref: userspace notified of strong ref |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 338 | * (protected by @proc->inner_lock if @proc |
| 339 | * and by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 340 | * @pending_strong_ref: userspace has acked notification of strong ref |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 341 | * (protected by @proc->inner_lock if @proc |
| 342 | * and by @lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 343 | * @has_weak_ref: userspace notified of weak 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_weak_ref: userspace has acked notification of weak 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_async_transaction: async transaction to node in progress |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 350 | * (protected by @lock) |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 351 | * @sched_policy: minimum scheduling policy for node |
| 352 | * (invariant after initialized) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 353 | * @accept_fds: file descriptor operations supported for node |
| 354 | * (invariant after initialized) |
| 355 | * @min_priority: minimum scheduling priority |
| 356 | * (invariant after initialized) |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 357 | * @inherit_rt: inherit RT scheduling policy from caller |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 358 | * @txn_security_ctx: require sender's security context |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 359 | * (invariant after initialized) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 360 | * @async_todo: list of async work items |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 361 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 362 | * |
| 363 | * Bookkeeping structure for binder nodes. |
| 364 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 365 | struct binder_node { |
| 366 | int debug_id; |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 367 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 368 | struct binder_work work; |
| 369 | union { |
| 370 | struct rb_node rb_node; |
| 371 | struct hlist_node dead_node; |
| 372 | }; |
| 373 | struct binder_proc *proc; |
| 374 | struct hlist_head refs; |
| 375 | int internal_strong_refs; |
| 376 | int local_weak_refs; |
| 377 | int local_strong_refs; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 378 | int tmp_refs; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 379 | binder_uintptr_t ptr; |
| 380 | binder_uintptr_t cookie; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 381 | struct { |
| 382 | /* |
| 383 | * bitfield elements protected by |
| 384 | * proc inner_lock |
| 385 | */ |
| 386 | u8 has_strong_ref:1; |
| 387 | u8 pending_strong_ref:1; |
| 388 | u8 has_weak_ref:1; |
| 389 | u8 pending_weak_ref:1; |
| 390 | }; |
| 391 | struct { |
| 392 | /* |
| 393 | * invariant after initialization |
| 394 | */ |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 395 | u8 sched_policy:2; |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 396 | u8 inherit_rt:1; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 397 | u8 accept_fds:1; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 398 | u8 txn_security_ctx:1; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 399 | u8 min_priority; |
| 400 | }; |
| 401 | bool has_async_transaction; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 402 | struct list_head async_todo; |
| 403 | }; |
| 404 | |
| 405 | struct binder_ref_death { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 406 | /** |
| 407 | * @work: worklist element for death notifications |
| 408 | * (protected by inner_lock of the proc that |
| 409 | * this ref belongs to) |
| 410 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 411 | struct binder_work work; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 412 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 413 | }; |
| 414 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 415 | /** |
| 416 | * struct binder_ref_data - binder_ref counts and id |
| 417 | * @debug_id: unique ID for the ref |
| 418 | * @desc: unique userspace handle for ref |
| 419 | * @strong: strong ref count (debugging only if not locked) |
| 420 | * @weak: weak ref count (debugging only if not locked) |
| 421 | * |
| 422 | * Structure to hold ref count and ref id information. Since |
| 423 | * the actual ref can only be accessed with a lock, this structure |
| 424 | * is used to return information about the ref to callers of |
| 425 | * ref inc/dec functions. |
| 426 | */ |
| 427 | struct binder_ref_data { |
| 428 | int debug_id; |
| 429 | uint32_t desc; |
| 430 | int strong; |
| 431 | int weak; |
| 432 | }; |
| 433 | |
| 434 | /** |
| 435 | * struct binder_ref - struct to track references on nodes |
| 436 | * @data: binder_ref_data containing id, handle, and current refcounts |
| 437 | * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree |
| 438 | * @rb_node_node: node for lookup by @node in proc's rb_tree |
| 439 | * @node_entry: list entry for node->refs list in target node |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 440 | * (protected by @node->lock) |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 441 | * @proc: binder_proc containing ref |
| 442 | * @node: binder_node of target node. When cleaning up a |
| 443 | * ref for deletion in binder_cleanup_ref, a non-NULL |
| 444 | * @node indicates the node must be freed |
| 445 | * @death: pointer to death notification (ref_death) if requested |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 446 | * (protected by @node->lock) |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 447 | * |
| 448 | * Structure to track references from procA to target node (on procB). This |
| 449 | * structure is unsafe to access without holding @proc->outer_lock. |
| 450 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 451 | struct binder_ref { |
| 452 | /* Lookups needed: */ |
| 453 | /* node + proc => ref (transaction) */ |
| 454 | /* desc + proc => ref (transaction, inc/dec ref) */ |
| 455 | /* node => refs + procs (proc exit) */ |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 456 | struct binder_ref_data data; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 457 | struct rb_node rb_node_desc; |
| 458 | struct rb_node rb_node_node; |
| 459 | struct hlist_node node_entry; |
| 460 | struct binder_proc *proc; |
| 461 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 462 | struct binder_ref_death *death; |
| 463 | }; |
| 464 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 465 | enum binder_deferred_state { |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 466 | BINDER_DEFERRED_PUT_FILES = 0x01, |
| 467 | BINDER_DEFERRED_FLUSH = 0x02, |
| 468 | BINDER_DEFERRED_RELEASE = 0x04, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 469 | }; |
| 470 | |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 471 | /** |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 472 | * struct binder_priority - scheduler policy and priority |
| 473 | * @sched_policy scheduler policy |
| 474 | * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT |
| 475 | * |
| 476 | * The binder driver supports inheriting the following scheduler policies: |
| 477 | * SCHED_NORMAL |
| 478 | * SCHED_BATCH |
| 479 | * SCHED_FIFO |
| 480 | * SCHED_RR |
| 481 | */ |
| 482 | struct binder_priority { |
| 483 | unsigned int sched_policy; |
| 484 | int prio; |
| 485 | }; |
| 486 | |
| 487 | /** |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 488 | * struct binder_proc - binder process bookkeeping |
| 489 | * @proc_node: element for binder_procs list |
| 490 | * @threads: rbtree of binder_threads in this proc |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 491 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 492 | * @nodes: rbtree of binder nodes associated with |
| 493 | * this proc ordered by node->ptr |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 494 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 495 | * @refs_by_desc: rbtree of refs ordered by ref->desc |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 496 | * (protected by @outer_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 497 | * @refs_by_node: rbtree of refs ordered by ref->node |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 498 | * (protected by @outer_lock) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 499 | * @waiting_threads: threads currently waiting for proc work |
| 500 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 501 | * @pid PID of group_leader of process |
| 502 | * (invariant after initialized) |
| 503 | * @tsk task_struct for group_leader of process |
| 504 | * (invariant after initialized) |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 505 | * @files files_struct for process |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 506 | * (protected by @files_lock) |
| 507 | * @files_lock mutex to protect @files |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 508 | * @deferred_work_node: element for binder_deferred_list |
| 509 | * (protected by binder_deferred_lock) |
| 510 | * @deferred_work: bitmap of deferred work to perform |
| 511 | * (protected by binder_deferred_lock) |
| 512 | * @is_dead: process is dead and awaiting free |
| 513 | * when outstanding transactions are cleaned up |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 514 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 515 | * @todo: list of work for this process |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 516 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 517 | * @stats: per-process binder statistics |
| 518 | * (atomics, no lock needed) |
| 519 | * @delivered_death: list of delivered death notification |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 520 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 521 | * @max_threads: cap on number of binder threads |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 522 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 523 | * @requested_threads: number of binder threads requested but not |
| 524 | * yet started. In current implementation, can |
| 525 | * only be 0 or 1. |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 526 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 527 | * @requested_threads_started: number binder threads started |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 528 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 529 | * @tmp_ref: temporary reference to indicate proc is in use |
Todd Kjos | dc734d7 | 2019-06-10 09:14:25 -0700 | [diff] [blame] | 530 | * (atomic since @proc->inner_lock cannot |
| 531 | * always be acquired) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 532 | * @default_priority: default scheduler priority |
| 533 | * (invariant after initialized) |
| 534 | * @debugfs_entry: debugfs node |
| 535 | * @alloc: binder allocator bookkeeping |
| 536 | * @context: binder_context for this proc |
| 537 | * (invariant after initialized) |
| 538 | * @inner_lock: can nest under outer_lock and/or node lock |
| 539 | * @outer_lock: no nesting under innor or node lock |
| 540 | * Lock order: 1) outer, 2) node, 3) inner |
| 541 | * |
| 542 | * Bookkeeping structure for binder processes |
| 543 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 544 | struct binder_proc { |
| 545 | struct hlist_node proc_node; |
| 546 | struct rb_root threads; |
| 547 | struct rb_root nodes; |
| 548 | struct rb_root refs_by_desc; |
| 549 | struct rb_root refs_by_node; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 550 | struct list_head waiting_threads; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 551 | int pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 552 | struct task_struct *tsk; |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 553 | struct files_struct *files; |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 554 | struct mutex files_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 555 | struct hlist_node deferred_work_node; |
| 556 | int deferred_work; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 557 | bool is_dead; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 558 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 559 | struct list_head todo; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 560 | struct binder_stats stats; |
| 561 | struct list_head delivered_death; |
| 562 | int max_threads; |
| 563 | int requested_threads; |
| 564 | int requested_threads_started; |
Todd Kjos | dc734d7 | 2019-06-10 09:14:25 -0700 | [diff] [blame] | 565 | atomic_t tmp_ref; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 566 | struct binder_priority default_priority; |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 567 | struct dentry *debugfs_entry; |
Todd Kjos | f85d229 | 2016-10-10 10:39:59 -0700 | [diff] [blame] | 568 | struct binder_alloc alloc; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 569 | struct binder_context *context; |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 570 | spinlock_t inner_lock; |
| 571 | spinlock_t outer_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 572 | }; |
| 573 | |
| 574 | enum { |
| 575 | BINDER_LOOPER_STATE_REGISTERED = 0x01, |
| 576 | BINDER_LOOPER_STATE_ENTERED = 0x02, |
| 577 | BINDER_LOOPER_STATE_EXITED = 0x04, |
| 578 | BINDER_LOOPER_STATE_INVALID = 0x08, |
| 579 | BINDER_LOOPER_STATE_WAITING = 0x10, |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 580 | BINDER_LOOPER_STATE_POLL = 0x20, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 581 | }; |
| 582 | |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 583 | /** |
| 584 | * struct binder_thread - binder thread bookkeeping |
| 585 | * @proc: binder process for this thread |
| 586 | * (invariant after initialization) |
| 587 | * @rb_node: element for proc->threads rbtree |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 588 | * (protected by @proc->inner_lock) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 589 | * @waiting_thread_node: element for @proc->waiting_threads list |
| 590 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 591 | * @pid: PID for this thread |
| 592 | * (invariant after initialization) |
| 593 | * @looper: bitmap of looping state |
| 594 | * (only accessed by this thread) |
| 595 | * @looper_needs_return: looping thread needs to exit driver |
| 596 | * (no lock needed) |
| 597 | * @transaction_stack: stack of in-progress transactions for this thread |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 598 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 599 | * @todo: list of work to do for this thread |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 600 | * (protected by @proc->inner_lock) |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 601 | * @process_todo: whether work in @todo should be processed |
| 602 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 603 | * @return_error: transaction errors reported by this thread |
| 604 | * (only accessed by this thread) |
| 605 | * @reply_error: transaction errors reported by target thread |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 606 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 607 | * @wait: wait queue for thread work |
| 608 | * @stats: per-thread statistics |
| 609 | * (atomics, no lock needed) |
| 610 | * @tmp_ref: temporary reference to indicate thread is in use |
| 611 | * (atomic since @proc->inner_lock cannot |
| 612 | * always be acquired) |
| 613 | * @is_dead: thread is dead and awaiting free |
| 614 | * when outstanding transactions are cleaned up |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 615 | * (protected by @proc->inner_lock) |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 616 | * @task: struct task_struct for this thread |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 617 | * |
| 618 | * Bookkeeping structure for binder threads. |
| 619 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 620 | struct binder_thread { |
| 621 | struct binder_proc *proc; |
| 622 | struct rb_node rb_node; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 623 | struct list_head waiting_thread_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 624 | int pid; |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 625 | int looper; /* only modified by this thread */ |
| 626 | bool looper_need_return; /* can be written by other thread */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 627 | struct binder_transaction *transaction_stack; |
| 628 | struct list_head todo; |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 629 | bool process_todo; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 630 | struct binder_error return_error; |
| 631 | struct binder_error reply_error; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 632 | wait_queue_head_t wait; |
| 633 | struct binder_stats stats; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 634 | atomic_t tmp_ref; |
| 635 | bool is_dead; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 636 | struct task_struct *task; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 637 | }; |
| 638 | |
| 639 | struct binder_transaction { |
| 640 | int debug_id; |
| 641 | struct binder_work work; |
| 642 | struct binder_thread *from; |
| 643 | struct binder_transaction *from_parent; |
| 644 | struct binder_proc *to_proc; |
| 645 | struct binder_thread *to_thread; |
| 646 | struct binder_transaction *to_parent; |
| 647 | unsigned need_reply:1; |
| 648 | /* unsigned is_dead:1; */ /* not used at the moment */ |
| 649 | |
| 650 | struct binder_buffer *buffer; |
| 651 | unsigned int code; |
| 652 | unsigned int flags; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 653 | struct binder_priority priority; |
| 654 | struct binder_priority saved_priority; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 655 | bool set_priority_called; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 656 | kuid_t sender_euid; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 657 | binder_uintptr_t security_ctx; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 658 | /** |
| 659 | * @lock: protects @from, @to_proc, and @to_thread |
| 660 | * |
| 661 | * @from, @to_proc, and @to_thread can be set to NULL |
| 662 | * during thread teardown |
| 663 | */ |
| 664 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 665 | }; |
| 666 | |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 667 | /** |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 668 | * struct binder_object - union of flat binder object types |
| 669 | * @hdr: generic object header |
| 670 | * @fbo: binder object (nodes and refs) |
| 671 | * @fdo: file descriptor object |
| 672 | * @bbo: binder buffer pointer |
| 673 | * @fdao: file descriptor array |
| 674 | * |
| 675 | * Used for type-independent object copies |
| 676 | */ |
| 677 | struct binder_object { |
| 678 | union { |
| 679 | struct binder_object_header hdr; |
| 680 | struct flat_binder_object fbo; |
| 681 | struct binder_fd_object fdo; |
| 682 | struct binder_buffer_object bbo; |
| 683 | struct binder_fd_array_object fdao; |
| 684 | }; |
| 685 | }; |
| 686 | |
| 687 | /** |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 688 | * binder_proc_lock() - Acquire outer lock for given binder_proc |
| 689 | * @proc: struct binder_proc to acquire |
| 690 | * |
| 691 | * Acquires proc->outer_lock. Used to protect binder_ref |
| 692 | * structures associated with the given proc. |
| 693 | */ |
| 694 | #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__) |
| 695 | static void |
| 696 | _binder_proc_lock(struct binder_proc *proc, int line) |
| 697 | { |
| 698 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 699 | "%s: line=%d\n", __func__, line); |
| 700 | spin_lock(&proc->outer_lock); |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * binder_proc_unlock() - Release spinlock for given binder_proc |
| 705 | * @proc: struct binder_proc to acquire |
| 706 | * |
| 707 | * Release lock acquired via binder_proc_lock() |
| 708 | */ |
| 709 | #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__) |
| 710 | static void |
| 711 | _binder_proc_unlock(struct binder_proc *proc, int line) |
| 712 | { |
| 713 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 714 | "%s: line=%d\n", __func__, line); |
| 715 | spin_unlock(&proc->outer_lock); |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * binder_inner_proc_lock() - Acquire inner lock for given binder_proc |
| 720 | * @proc: struct binder_proc to acquire |
| 721 | * |
| 722 | * Acquires proc->inner_lock. Used to protect todo lists |
| 723 | */ |
| 724 | #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__) |
| 725 | static void |
| 726 | _binder_inner_proc_lock(struct binder_proc *proc, int line) |
| 727 | { |
| 728 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 729 | "%s: line=%d\n", __func__, line); |
| 730 | spin_lock(&proc->inner_lock); |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * binder_inner_proc_unlock() - Release inner lock for given binder_proc |
| 735 | * @proc: struct binder_proc to acquire |
| 736 | * |
| 737 | * Release lock acquired via binder_inner_proc_lock() |
| 738 | */ |
| 739 | #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__) |
| 740 | static void |
| 741 | _binder_inner_proc_unlock(struct binder_proc *proc, int line) |
| 742 | { |
| 743 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 744 | "%s: line=%d\n", __func__, line); |
| 745 | spin_unlock(&proc->inner_lock); |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * binder_node_lock() - Acquire spinlock for given binder_node |
| 750 | * @node: struct binder_node to acquire |
| 751 | * |
| 752 | * Acquires node->lock. Used to protect binder_node fields |
| 753 | */ |
| 754 | #define binder_node_lock(node) _binder_node_lock(node, __LINE__) |
| 755 | static void |
| 756 | _binder_node_lock(struct binder_node *node, int line) |
| 757 | { |
| 758 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 759 | "%s: line=%d\n", __func__, line); |
| 760 | spin_lock(&node->lock); |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * binder_node_unlock() - Release spinlock for given binder_proc |
| 765 | * @node: struct binder_node to acquire |
| 766 | * |
| 767 | * Release lock acquired via binder_node_lock() |
| 768 | */ |
| 769 | #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__) |
| 770 | static void |
| 771 | _binder_node_unlock(struct binder_node *node, int line) |
| 772 | { |
| 773 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 774 | "%s: line=%d\n", __func__, line); |
| 775 | spin_unlock(&node->lock); |
| 776 | } |
| 777 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 778 | /** |
| 779 | * binder_node_inner_lock() - Acquire node and inner locks |
| 780 | * @node: struct binder_node to acquire |
| 781 | * |
| 782 | * Acquires node->lock. If node->proc also acquires |
| 783 | * proc->inner_lock. Used to protect binder_node fields |
| 784 | */ |
| 785 | #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__) |
| 786 | static void |
| 787 | _binder_node_inner_lock(struct binder_node *node, int line) |
| 788 | { |
| 789 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 790 | "%s: line=%d\n", __func__, line); |
| 791 | spin_lock(&node->lock); |
| 792 | if (node->proc) |
| 793 | binder_inner_proc_lock(node->proc); |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * binder_node_unlock() - Release node and inner locks |
| 798 | * @node: struct binder_node to acquire |
| 799 | * |
| 800 | * Release lock acquired via binder_node_lock() |
| 801 | */ |
| 802 | #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__) |
| 803 | static void |
| 804 | _binder_node_inner_unlock(struct binder_node *node, int line) |
| 805 | { |
| 806 | struct binder_proc *proc = node->proc; |
| 807 | |
| 808 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 809 | "%s: line=%d\n", __func__, line); |
| 810 | if (proc) |
| 811 | binder_inner_proc_unlock(proc); |
| 812 | spin_unlock(&node->lock); |
| 813 | } |
| 814 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 815 | static bool binder_worklist_empty_ilocked(struct list_head *list) |
| 816 | { |
| 817 | return list_empty(list); |
| 818 | } |
| 819 | |
| 820 | /** |
| 821 | * binder_worklist_empty() - Check if no items on the work list |
| 822 | * @proc: binder_proc associated with list |
| 823 | * @list: list to check |
| 824 | * |
| 825 | * Return: true if there are no items on list, else false |
| 826 | */ |
| 827 | static bool binder_worklist_empty(struct binder_proc *proc, |
| 828 | struct list_head *list) |
| 829 | { |
| 830 | bool ret; |
| 831 | |
| 832 | binder_inner_proc_lock(proc); |
| 833 | ret = binder_worklist_empty_ilocked(list); |
| 834 | binder_inner_proc_unlock(proc); |
| 835 | return ret; |
| 836 | } |
| 837 | |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 838 | /** |
| 839 | * binder_enqueue_work_ilocked() - Add an item to the work list |
| 840 | * @work: struct binder_work to add to list |
| 841 | * @target_list: list to add work to |
| 842 | * |
| 843 | * Adds the work to the specified list. Asserts that work |
| 844 | * is not already on a list. |
| 845 | * |
| 846 | * Requires the proc->inner_lock to be held. |
| 847 | */ |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 848 | static void |
| 849 | binder_enqueue_work_ilocked(struct binder_work *work, |
| 850 | struct list_head *target_list) |
| 851 | { |
| 852 | BUG_ON(target_list == NULL); |
| 853 | BUG_ON(work->entry.next && !list_empty(&work->entry)); |
| 854 | list_add_tail(&work->entry, target_list); |
| 855 | } |
| 856 | |
| 857 | /** |
Martijn Coenen | dac2e9c | 2017-11-13 09:55:21 +0100 | [diff] [blame] | 858 | * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 859 | * @thread: thread to queue work to |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 860 | * @work: struct binder_work to add to list |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 861 | * |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 862 | * Adds the work to the todo list of the thread. Doesn't set the process_todo |
| 863 | * flag, which means that (if it wasn't already set) the thread will go to |
| 864 | * sleep without handling this work when it calls read. |
| 865 | * |
| 866 | * Requires the proc->inner_lock to be held. |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 867 | */ |
| 868 | static void |
Martijn Coenen | dac2e9c | 2017-11-13 09:55:21 +0100 | [diff] [blame] | 869 | binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread, |
| 870 | struct binder_work *work) |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 871 | { |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 872 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list |
| 877 | * @thread: thread to queue work to |
| 878 | * @work: struct binder_work to add to list |
| 879 | * |
| 880 | * Adds the work to the todo list of the thread, and enables processing |
| 881 | * of the todo queue. |
| 882 | * |
| 883 | * Requires the proc->inner_lock to be held. |
| 884 | */ |
| 885 | static void |
| 886 | binder_enqueue_thread_work_ilocked(struct binder_thread *thread, |
| 887 | struct binder_work *work) |
| 888 | { |
| 889 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 890 | thread->process_todo = true; |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * binder_enqueue_thread_work() - Add an item to the thread work list |
| 895 | * @thread: thread to queue work to |
| 896 | * @work: struct binder_work to add to list |
| 897 | * |
| 898 | * Adds the work to the todo list of the thread, and enables processing |
| 899 | * of the todo queue. |
| 900 | */ |
| 901 | static void |
| 902 | binder_enqueue_thread_work(struct binder_thread *thread, |
| 903 | struct binder_work *work) |
| 904 | { |
| 905 | binder_inner_proc_lock(thread->proc); |
| 906 | binder_enqueue_thread_work_ilocked(thread, work); |
| 907 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | static void |
| 911 | binder_dequeue_work_ilocked(struct binder_work *work) |
| 912 | { |
| 913 | list_del_init(&work->entry); |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * binder_dequeue_work() - Removes an item from the work list |
| 918 | * @proc: binder_proc associated with list |
| 919 | * @work: struct binder_work to remove from list |
| 920 | * |
| 921 | * Removes the specified work item from whatever list it is on. |
| 922 | * Can safely be called if work is not on any list. |
| 923 | */ |
| 924 | static void |
| 925 | binder_dequeue_work(struct binder_proc *proc, struct binder_work *work) |
| 926 | { |
| 927 | binder_inner_proc_lock(proc); |
| 928 | binder_dequeue_work_ilocked(work); |
| 929 | binder_inner_proc_unlock(proc); |
| 930 | } |
| 931 | |
| 932 | static struct binder_work *binder_dequeue_work_head_ilocked( |
| 933 | struct list_head *list) |
| 934 | { |
| 935 | struct binder_work *w; |
| 936 | |
| 937 | w = list_first_entry_or_null(list, struct binder_work, entry); |
| 938 | if (w) |
| 939 | list_del_init(&w->entry); |
| 940 | return w; |
| 941 | } |
| 942 | |
| 943 | /** |
| 944 | * binder_dequeue_work_head() - Dequeues the item at head of list |
| 945 | * @proc: binder_proc associated with list |
| 946 | * @list: list to dequeue head |
| 947 | * |
| 948 | * Removes the head of the list if there are items on the list |
| 949 | * |
| 950 | * Return: pointer dequeued binder_work, NULL if list was empty |
| 951 | */ |
| 952 | static struct binder_work *binder_dequeue_work_head( |
| 953 | struct binder_proc *proc, |
| 954 | struct list_head *list) |
| 955 | { |
| 956 | struct binder_work *w; |
| 957 | |
| 958 | binder_inner_proc_lock(proc); |
| 959 | w = binder_dequeue_work_head_ilocked(list); |
| 960 | binder_inner_proc_unlock(proc); |
| 961 | return w; |
| 962 | } |
| 963 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 964 | static void |
| 965 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 966 | static void binder_free_thread(struct binder_thread *thread); |
| 967 | static void binder_free_proc(struct binder_proc *proc); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 968 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 969 | |
Sachin Kamat | efde99c | 2012-08-17 16:39:36 +0530 | [diff] [blame] | 970 | 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] | 971 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 972 | unsigned long rlim_cur; |
| 973 | unsigned long irqs; |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 974 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 975 | |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 976 | mutex_lock(&proc->files_lock); |
| 977 | if (proc->files == NULL) { |
| 978 | ret = -ESRCH; |
| 979 | goto err; |
| 980 | } |
| 981 | if (!lock_task_sighand(proc->tsk, &irqs)) { |
| 982 | ret = -EMFILE; |
| 983 | goto err; |
| 984 | } |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 985 | rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE); |
| 986 | unlock_task_sighand(proc->tsk, &irqs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 987 | |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 988 | ret = __alloc_fd(proc->files, 0, rlim_cur, flags); |
| 989 | err: |
| 990 | mutex_unlock(&proc->files_lock); |
| 991 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | /* |
| 995 | * copied from fd_install |
| 996 | */ |
| 997 | static void task_fd_install( |
| 998 | struct binder_proc *proc, unsigned int fd, struct file *file) |
| 999 | { |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 1000 | mutex_lock(&proc->files_lock); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 1001 | if (proc->files) |
| 1002 | __fd_install(proc->files, fd, file); |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 1003 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * copied from sys_close |
| 1008 | */ |
| 1009 | static long task_close_fd(struct binder_proc *proc, unsigned int fd) |
| 1010 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1011 | int retval; |
| 1012 | |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 1013 | mutex_lock(&proc->files_lock); |
| 1014 | if (proc->files == NULL) { |
| 1015 | retval = -ESRCH; |
| 1016 | goto err; |
| 1017 | } |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 1018 | retval = __close_fd(proc->files, fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1019 | /* can't restart close syscall because file table entry was cleared */ |
| 1020 | if (unlikely(retval == -ERESTARTSYS || |
| 1021 | retval == -ERESTARTNOINTR || |
| 1022 | retval == -ERESTARTNOHAND || |
| 1023 | retval == -ERESTART_RESTARTBLOCK)) |
| 1024 | retval = -EINTR; |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 1025 | err: |
| 1026 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1027 | return retval; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1028 | } |
| 1029 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1030 | static bool binder_has_work_ilocked(struct binder_thread *thread, |
| 1031 | bool do_proc_work) |
| 1032 | { |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 1033 | return thread->process_todo || |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1034 | thread->looper_need_return || |
| 1035 | (do_proc_work && |
| 1036 | !binder_worklist_empty_ilocked(&thread->proc->todo)); |
| 1037 | } |
| 1038 | |
| 1039 | static bool binder_has_work(struct binder_thread *thread, bool do_proc_work) |
| 1040 | { |
| 1041 | bool has_work; |
| 1042 | |
| 1043 | binder_inner_proc_lock(thread->proc); |
| 1044 | has_work = binder_has_work_ilocked(thread, do_proc_work); |
| 1045 | binder_inner_proc_unlock(thread->proc); |
| 1046 | |
| 1047 | return has_work; |
| 1048 | } |
| 1049 | |
| 1050 | static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread) |
| 1051 | { |
| 1052 | return !thread->transaction_stack && |
| 1053 | binder_worklist_empty_ilocked(&thread->todo) && |
| 1054 | (thread->looper & (BINDER_LOOPER_STATE_ENTERED | |
| 1055 | BINDER_LOOPER_STATE_REGISTERED)); |
| 1056 | } |
| 1057 | |
| 1058 | static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc, |
| 1059 | bool sync) |
| 1060 | { |
| 1061 | struct rb_node *n; |
| 1062 | struct binder_thread *thread; |
| 1063 | |
| 1064 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 1065 | thread = rb_entry(n, struct binder_thread, rb_node); |
| 1066 | if (thread->looper & BINDER_LOOPER_STATE_POLL && |
| 1067 | binder_available_for_proc_work_ilocked(thread)) { |
| 1068 | if (sync) |
| 1069 | wake_up_interruptible_sync(&thread->wait); |
| 1070 | else |
| 1071 | wake_up_interruptible(&thread->wait); |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1076 | /** |
| 1077 | * binder_select_thread_ilocked() - selects a thread for doing proc work. |
| 1078 | * @proc: process to select a thread from |
| 1079 | * |
| 1080 | * Note that calling this function moves the thread off the waiting_threads |
| 1081 | * list, so it can only be woken up by the caller of this function, or a |
| 1082 | * signal. Therefore, callers *should* always wake up the thread this function |
| 1083 | * returns. |
| 1084 | * |
| 1085 | * Return: If there's a thread currently waiting for process work, |
| 1086 | * returns that thread. Otherwise returns NULL. |
| 1087 | */ |
| 1088 | static struct binder_thread * |
| 1089 | binder_select_thread_ilocked(struct binder_proc *proc) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1090 | { |
| 1091 | struct binder_thread *thread; |
| 1092 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1093 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1094 | thread = list_first_entry_or_null(&proc->waiting_threads, |
| 1095 | struct binder_thread, |
| 1096 | waiting_thread_node); |
| 1097 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1098 | if (thread) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1099 | list_del_init(&thread->waiting_thread_node); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1100 | |
| 1101 | return thread; |
| 1102 | } |
| 1103 | |
| 1104 | /** |
| 1105 | * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work. |
| 1106 | * @proc: process to wake up a thread in |
| 1107 | * @thread: specific thread to wake-up (may be NULL) |
| 1108 | * @sync: whether to do a synchronous wake-up |
| 1109 | * |
| 1110 | * This function wakes up a thread in the @proc process. |
| 1111 | * The caller may provide a specific thread to wake-up in |
| 1112 | * the @thread parameter. If @thread is NULL, this function |
| 1113 | * will wake up threads that have called poll(). |
| 1114 | * |
| 1115 | * Note that for this function to work as expected, callers |
| 1116 | * should first call binder_select_thread() to find a thread |
| 1117 | * to handle the work (if they don't have a thread already), |
| 1118 | * and pass the result into the @thread parameter. |
| 1119 | */ |
| 1120 | static void binder_wakeup_thread_ilocked(struct binder_proc *proc, |
| 1121 | struct binder_thread *thread, |
| 1122 | bool sync) |
| 1123 | { |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1124 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1125 | |
| 1126 | if (thread) { |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1127 | if (sync) |
| 1128 | wake_up_interruptible_sync(&thread->wait); |
| 1129 | else |
| 1130 | wake_up_interruptible(&thread->wait); |
| 1131 | return; |
| 1132 | } |
| 1133 | |
| 1134 | /* Didn't find a thread waiting for proc work; this can happen |
| 1135 | * in two scenarios: |
| 1136 | * 1. All threads are busy handling transactions |
| 1137 | * In that case, one of those threads should call back into |
| 1138 | * the kernel driver soon and pick up this work. |
| 1139 | * 2. Threads are using the (e)poll interface, in which case |
| 1140 | * they may be blocked on the waitqueue without having been |
| 1141 | * added to waiting_threads. For this case, we just iterate |
| 1142 | * over all threads not handling transaction work, and |
| 1143 | * wake them all up. We wake all because we don't know whether |
| 1144 | * a thread that called into (e)poll is handling non-binder |
| 1145 | * work currently. |
| 1146 | */ |
| 1147 | binder_wakeup_poll_threads_ilocked(proc, sync); |
| 1148 | } |
| 1149 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1150 | static void binder_wakeup_proc_ilocked(struct binder_proc *proc) |
| 1151 | { |
| 1152 | struct binder_thread *thread = binder_select_thread_ilocked(proc); |
| 1153 | |
| 1154 | binder_wakeup_thread_ilocked(proc, thread, /* sync = */false); |
| 1155 | } |
| 1156 | |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1157 | static bool is_rt_policy(int policy) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1158 | { |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1159 | return policy == SCHED_FIFO || policy == SCHED_RR; |
| 1160 | } |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1161 | |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1162 | static bool is_fair_policy(int policy) |
| 1163 | { |
| 1164 | return policy == SCHED_NORMAL || policy == SCHED_BATCH; |
| 1165 | } |
| 1166 | |
| 1167 | static bool binder_supported_policy(int policy) |
| 1168 | { |
| 1169 | return is_fair_policy(policy) || is_rt_policy(policy); |
| 1170 | } |
| 1171 | |
| 1172 | static int to_userspace_prio(int policy, int kernel_priority) |
| 1173 | { |
| 1174 | if (is_fair_policy(policy)) |
| 1175 | return PRIO_TO_NICE(kernel_priority); |
| 1176 | else |
| 1177 | return MAX_USER_RT_PRIO - 1 - kernel_priority; |
| 1178 | } |
| 1179 | |
| 1180 | static int to_kernel_prio(int policy, int user_priority) |
| 1181 | { |
| 1182 | if (is_fair_policy(policy)) |
| 1183 | return NICE_TO_PRIO(user_priority); |
| 1184 | else |
| 1185 | return MAX_USER_RT_PRIO - 1 - user_priority; |
| 1186 | } |
| 1187 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1188 | static void binder_do_set_priority(struct task_struct *task, |
| 1189 | struct binder_priority desired, |
| 1190 | bool verify) |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1191 | { |
| 1192 | int priority; /* user-space prio value */ |
| 1193 | bool has_cap_nice; |
| 1194 | unsigned int policy = desired.sched_policy; |
| 1195 | |
| 1196 | if (task->policy == policy && task->normal_prio == desired.prio) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1197 | return; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1198 | |
| 1199 | has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE); |
| 1200 | |
| 1201 | priority = to_userspace_prio(policy, desired.prio); |
| 1202 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1203 | if (verify && is_rt_policy(policy) && !has_cap_nice) { |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1204 | long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO); |
| 1205 | |
| 1206 | if (max_rtprio == 0) { |
| 1207 | policy = SCHED_NORMAL; |
| 1208 | priority = MIN_NICE; |
| 1209 | } else if (priority > max_rtprio) { |
| 1210 | priority = max_rtprio; |
| 1211 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1212 | } |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1213 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1214 | if (verify && is_fair_policy(policy) && !has_cap_nice) { |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1215 | long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE)); |
| 1216 | |
| 1217 | if (min_nice > MAX_NICE) { |
| 1218 | binder_user_error("%d RLIMIT_NICE not set\n", |
| 1219 | task->pid); |
| 1220 | return; |
| 1221 | } else if (priority < min_nice) { |
| 1222 | priority = min_nice; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | if (policy != desired.sched_policy || |
| 1227 | to_kernel_prio(policy, priority) != desired.prio) |
| 1228 | binder_debug(BINDER_DEBUG_PRIORITY_CAP, |
| 1229 | "%d: priority %d not allowed, using %d instead\n", |
| 1230 | task->pid, desired.prio, |
| 1231 | to_kernel_prio(policy, priority)); |
| 1232 | |
Martijn Coenen | 81402ea | 2017-05-08 09:33:22 -0700 | [diff] [blame] | 1233 | trace_binder_set_priority(task->tgid, task->pid, task->normal_prio, |
| 1234 | to_kernel_prio(policy, priority), |
| 1235 | desired.prio); |
| 1236 | |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1237 | /* Set the actual priority */ |
| 1238 | if (task->policy != policy || is_rt_policy(policy)) { |
| 1239 | struct sched_param params; |
| 1240 | |
| 1241 | params.sched_priority = is_rt_policy(policy) ? priority : 0; |
| 1242 | |
| 1243 | sched_setscheduler_nocheck(task, |
| 1244 | policy | SCHED_RESET_ON_FORK, |
| 1245 | ¶ms); |
| 1246 | } |
| 1247 | if (is_fair_policy(policy)) |
| 1248 | set_user_nice(task, priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1249 | } |
| 1250 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1251 | static void binder_set_priority(struct task_struct *task, |
| 1252 | struct binder_priority desired) |
| 1253 | { |
| 1254 | binder_do_set_priority(task, desired, /* verify = */ true); |
| 1255 | } |
| 1256 | |
| 1257 | static void binder_restore_priority(struct task_struct *task, |
| 1258 | struct binder_priority desired) |
| 1259 | { |
| 1260 | binder_do_set_priority(task, desired, /* verify = */ false); |
| 1261 | } |
| 1262 | |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1263 | static void binder_transaction_priority(struct task_struct *task, |
| 1264 | struct binder_transaction *t, |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1265 | struct binder_priority node_prio, |
| 1266 | bool inherit_rt) |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1267 | { |
Ganesh Mahendran | 9add7c4 | 2017-09-27 15:12:25 +0800 | [diff] [blame] | 1268 | struct binder_priority desired_prio = t->priority; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1269 | |
| 1270 | if (t->set_priority_called) |
| 1271 | return; |
| 1272 | |
| 1273 | t->set_priority_called = true; |
| 1274 | t->saved_priority.sched_policy = task->policy; |
| 1275 | t->saved_priority.prio = task->normal_prio; |
| 1276 | |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1277 | if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) { |
| 1278 | desired_prio.prio = NICE_TO_PRIO(0); |
| 1279 | desired_prio.sched_policy = SCHED_NORMAL; |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1280 | } |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1281 | |
| 1282 | if (node_prio.prio < t->priority.prio || |
| 1283 | (node_prio.prio == t->priority.prio && |
| 1284 | node_prio.sched_policy == SCHED_FIFO)) { |
| 1285 | /* |
| 1286 | * In case the minimum priority on the node is |
| 1287 | * higher (lower value), use that priority. If |
| 1288 | * the priority is the same, but the node uses |
| 1289 | * SCHED_FIFO, prefer SCHED_FIFO, since it can |
| 1290 | * run unbounded, unlike SCHED_RR. |
| 1291 | */ |
| 1292 | desired_prio = node_prio; |
| 1293 | } |
| 1294 | |
| 1295 | binder_set_priority(task, desired_prio); |
| 1296 | } |
| 1297 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1298 | static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc, |
| 1299 | binder_uintptr_t ptr) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1300 | { |
| 1301 | struct rb_node *n = proc->nodes.rb_node; |
| 1302 | struct binder_node *node; |
| 1303 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1304 | assert_spin_locked(&proc->inner_lock); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1305 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1306 | while (n) { |
| 1307 | node = rb_entry(n, struct binder_node, rb_node); |
| 1308 | |
| 1309 | if (ptr < node->ptr) |
| 1310 | n = n->rb_left; |
| 1311 | else if (ptr > node->ptr) |
| 1312 | n = n->rb_right; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1313 | else { |
| 1314 | /* |
| 1315 | * take an implicit weak reference |
| 1316 | * to ensure node stays alive until |
| 1317 | * call to binder_put_node() |
| 1318 | */ |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1319 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1320 | return node; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1321 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1322 | } |
| 1323 | return NULL; |
| 1324 | } |
| 1325 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1326 | static struct binder_node *binder_get_node(struct binder_proc *proc, |
| 1327 | binder_uintptr_t ptr) |
| 1328 | { |
| 1329 | struct binder_node *node; |
| 1330 | |
| 1331 | binder_inner_proc_lock(proc); |
| 1332 | node = binder_get_node_ilocked(proc, ptr); |
| 1333 | binder_inner_proc_unlock(proc); |
| 1334 | return node; |
| 1335 | } |
| 1336 | |
| 1337 | static struct binder_node *binder_init_node_ilocked( |
| 1338 | struct binder_proc *proc, |
| 1339 | struct binder_node *new_node, |
| 1340 | struct flat_binder_object *fp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1341 | { |
| 1342 | struct rb_node **p = &proc->nodes.rb_node; |
| 1343 | struct rb_node *parent = NULL; |
| 1344 | struct binder_node *node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1345 | binder_uintptr_t ptr = fp ? fp->binder : 0; |
| 1346 | binder_uintptr_t cookie = fp ? fp->cookie : 0; |
| 1347 | __u32 flags = fp ? fp->flags : 0; |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1348 | s8 priority; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1349 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1350 | assert_spin_locked(&proc->inner_lock); |
| 1351 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1352 | while (*p) { |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1353 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1354 | parent = *p; |
| 1355 | node = rb_entry(parent, struct binder_node, rb_node); |
| 1356 | |
| 1357 | if (ptr < node->ptr) |
| 1358 | p = &(*p)->rb_left; |
| 1359 | else if (ptr > node->ptr) |
| 1360 | p = &(*p)->rb_right; |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1361 | else { |
| 1362 | /* |
| 1363 | * A matching node is already in |
| 1364 | * the rb tree. Abandon the init |
| 1365 | * and return it. |
| 1366 | */ |
| 1367 | binder_inc_node_tmpref_ilocked(node); |
| 1368 | return node; |
| 1369 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1370 | } |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1371 | node = new_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1372 | binder_stats_created(BINDER_STAT_NODE); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1373 | node->tmp_refs++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1374 | rb_link_node(&node->rb_node, parent, p); |
| 1375 | rb_insert_color(&node->rb_node, &proc->nodes); |
Todd Kjos | c4bd08b | 2017-05-25 10:56:00 -0700 | [diff] [blame] | 1376 | node->debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1377 | node->proc = proc; |
| 1378 | node->ptr = ptr; |
| 1379 | node->cookie = cookie; |
| 1380 | node->work.type = BINDER_WORK_NODE; |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1381 | priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK; |
Ganesh Mahendran | 6cd2631 | 2017-09-26 17:56:25 +0800 | [diff] [blame] | 1382 | node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >> |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1383 | FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT; |
| 1384 | node->min_priority = to_kernel_prio(node->sched_policy, priority); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1385 | node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1386 | node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT); |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 1387 | node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX); |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 1388 | spin_lock_init(&node->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1389 | INIT_LIST_HEAD(&node->work.entry); |
| 1390 | INIT_LIST_HEAD(&node->async_todo); |
| 1391 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1392 | "%d:%d node %d u%016llx c%016llx created\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1393 | proc->pid, current->pid, node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1394 | (u64)node->ptr, (u64)node->cookie); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1395 | |
| 1396 | return node; |
| 1397 | } |
| 1398 | |
| 1399 | static struct binder_node *binder_new_node(struct binder_proc *proc, |
| 1400 | struct flat_binder_object *fp) |
| 1401 | { |
| 1402 | struct binder_node *node; |
| 1403 | struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 1404 | |
| 1405 | if (!new_node) |
| 1406 | return NULL; |
| 1407 | binder_inner_proc_lock(proc); |
| 1408 | node = binder_init_node_ilocked(proc, new_node, fp); |
| 1409 | binder_inner_proc_unlock(proc); |
| 1410 | if (node != new_node) |
| 1411 | /* |
| 1412 | * The node was already added by another thread |
| 1413 | */ |
| 1414 | kfree(new_node); |
| 1415 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1416 | return node; |
| 1417 | } |
| 1418 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1419 | static void binder_free_node(struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1420 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1421 | kfree(node); |
| 1422 | binder_stats_deleted(BINDER_STAT_NODE); |
| 1423 | } |
| 1424 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1425 | static int binder_inc_node_nilocked(struct binder_node *node, int strong, |
| 1426 | int internal, |
| 1427 | struct list_head *target_list) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1428 | { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1429 | struct binder_proc *proc = node->proc; |
| 1430 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1431 | assert_spin_locked(&node->lock); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1432 | if (proc) |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1433 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1434 | if (strong) { |
| 1435 | if (internal) { |
| 1436 | if (target_list == NULL && |
| 1437 | node->internal_strong_refs == 0 && |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 1438 | !(node->proc && |
| 1439 | node == node->proc->context-> |
| 1440 | binder_context_mgr_node && |
| 1441 | node->has_strong_ref)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1442 | pr_err("invalid inc strong node for %d\n", |
| 1443 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1444 | return -EINVAL; |
| 1445 | } |
| 1446 | node->internal_strong_refs++; |
| 1447 | } else |
| 1448 | node->local_strong_refs++; |
| 1449 | if (!node->has_strong_ref && target_list) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1450 | binder_dequeue_work_ilocked(&node->work); |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 1451 | /* |
| 1452 | * Note: this function is the only place where we queue |
| 1453 | * directly to a thread->todo without using the |
| 1454 | * corresponding binder_enqueue_thread_work() helper |
| 1455 | * functions; in this case it's ok to not set the |
| 1456 | * process_todo flag, since we know this node work will |
| 1457 | * always be followed by other work that starts queue |
| 1458 | * processing: in case of synchronous transactions, a |
| 1459 | * BR_REPLY or BR_ERROR; in case of oneway |
| 1460 | * transactions, a BR_TRANSACTION_COMPLETE. |
| 1461 | */ |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1462 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1463 | } |
| 1464 | } else { |
| 1465 | if (!internal) |
| 1466 | node->local_weak_refs++; |
| 1467 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { |
| 1468 | if (target_list == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1469 | pr_err("invalid inc weak node for %d\n", |
| 1470 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1471 | return -EINVAL; |
| 1472 | } |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 1473 | /* |
| 1474 | * See comment above |
| 1475 | */ |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1476 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1482 | static int binder_inc_node(struct binder_node *node, int strong, int internal, |
| 1483 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1484 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1485 | int ret; |
| 1486 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1487 | binder_node_inner_lock(node); |
| 1488 | ret = binder_inc_node_nilocked(node, strong, internal, target_list); |
| 1489 | binder_node_inner_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1490 | |
| 1491 | return ret; |
| 1492 | } |
| 1493 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1494 | static bool binder_dec_node_nilocked(struct binder_node *node, |
| 1495 | int strong, int internal) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1496 | { |
| 1497 | struct binder_proc *proc = node->proc; |
| 1498 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1499 | assert_spin_locked(&node->lock); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1500 | if (proc) |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1501 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1502 | if (strong) { |
| 1503 | if (internal) |
| 1504 | node->internal_strong_refs--; |
| 1505 | else |
| 1506 | node->local_strong_refs--; |
| 1507 | if (node->local_strong_refs || node->internal_strong_refs) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1508 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1509 | } else { |
| 1510 | if (!internal) |
| 1511 | node->local_weak_refs--; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1512 | if (node->local_weak_refs || node->tmp_refs || |
| 1513 | !hlist_empty(&node->refs)) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1514 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1515 | } |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1516 | |
| 1517 | if (proc && (node->has_strong_ref || node->has_weak_ref)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1518 | if (list_empty(&node->work.entry)) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1519 | binder_enqueue_work_ilocked(&node->work, &proc->todo); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1520 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1521 | } |
| 1522 | } else { |
| 1523 | if (hlist_empty(&node->refs) && !node->local_strong_refs && |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1524 | !node->local_weak_refs && !node->tmp_refs) { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1525 | if (proc) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1526 | binder_dequeue_work_ilocked(&node->work); |
| 1527 | rb_erase(&node->rb_node, &proc->nodes); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1528 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1529 | "refless node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1530 | node->debug_id); |
| 1531 | } else { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1532 | BUG_ON(!list_empty(&node->work.entry)); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 1533 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1534 | /* |
| 1535 | * tmp_refs could have changed so |
| 1536 | * check it again |
| 1537 | */ |
| 1538 | if (node->tmp_refs) { |
| 1539 | spin_unlock(&binder_dead_nodes_lock); |
| 1540 | return false; |
| 1541 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1542 | hlist_del(&node->dead_node); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 1543 | spin_unlock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1544 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1545 | "dead node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1546 | node->debug_id); |
| 1547 | } |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1548 | return true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1549 | } |
| 1550 | } |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1551 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1552 | } |
| 1553 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1554 | static void binder_dec_node(struct binder_node *node, int strong, int internal) |
| 1555 | { |
| 1556 | bool free_node; |
| 1557 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1558 | binder_node_inner_lock(node); |
| 1559 | free_node = binder_dec_node_nilocked(node, strong, internal); |
| 1560 | binder_node_inner_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1561 | if (free_node) |
| 1562 | binder_free_node(node); |
| 1563 | } |
| 1564 | |
| 1565 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node) |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1566 | { |
| 1567 | /* |
| 1568 | * No call to binder_inc_node() is needed since we |
| 1569 | * don't need to inform userspace of any changes to |
| 1570 | * tmp_refs |
| 1571 | */ |
| 1572 | node->tmp_refs++; |
| 1573 | } |
| 1574 | |
| 1575 | /** |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1576 | * binder_inc_node_tmpref() - take a temporary reference on node |
| 1577 | * @node: node to reference |
| 1578 | * |
| 1579 | * Take reference on node to prevent the node from being freed |
| 1580 | * while referenced only by a local variable. The inner lock is |
| 1581 | * needed to serialize with the node work on the queue (which |
| 1582 | * isn't needed after the node is dead). If the node is dead |
| 1583 | * (node->proc is NULL), use binder_dead_nodes_lock to protect |
| 1584 | * node->tmp_refs against dead-node-only cases where the node |
| 1585 | * lock cannot be acquired (eg traversing the dead node list to |
| 1586 | * print nodes) |
| 1587 | */ |
| 1588 | static void binder_inc_node_tmpref(struct binder_node *node) |
| 1589 | { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1590 | binder_node_lock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1591 | if (node->proc) |
| 1592 | binder_inner_proc_lock(node->proc); |
| 1593 | else |
| 1594 | spin_lock(&binder_dead_nodes_lock); |
| 1595 | binder_inc_node_tmpref_ilocked(node); |
| 1596 | if (node->proc) |
| 1597 | binder_inner_proc_unlock(node->proc); |
| 1598 | else |
| 1599 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1600 | binder_node_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | /** |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1604 | * binder_dec_node_tmpref() - remove a temporary reference on node |
| 1605 | * @node: node to reference |
| 1606 | * |
| 1607 | * Release temporary reference on node taken via binder_inc_node_tmpref() |
| 1608 | */ |
| 1609 | static void binder_dec_node_tmpref(struct binder_node *node) |
| 1610 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1611 | bool free_node; |
| 1612 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1613 | binder_node_inner_lock(node); |
| 1614 | if (!node->proc) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1615 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1616 | node->tmp_refs--; |
| 1617 | BUG_ON(node->tmp_refs < 0); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1618 | if (!node->proc) |
| 1619 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1620 | /* |
| 1621 | * Call binder_dec_node() to check if all refcounts are 0 |
| 1622 | * and cleanup is needed. Calling with strong=0 and internal=1 |
| 1623 | * causes no actual reference to be released in binder_dec_node(). |
| 1624 | * If that changes, a change is needed here too. |
| 1625 | */ |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1626 | free_node = binder_dec_node_nilocked(node, 0, 1); |
| 1627 | binder_node_inner_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1628 | if (free_node) |
| 1629 | binder_free_node(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | static void binder_put_node(struct binder_node *node) |
| 1633 | { |
| 1634 | binder_dec_node_tmpref(node); |
| 1635 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1636 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1637 | static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc, |
| 1638 | u32 desc, bool need_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1639 | { |
| 1640 | struct rb_node *n = proc->refs_by_desc.rb_node; |
| 1641 | struct binder_ref *ref; |
| 1642 | |
| 1643 | while (n) { |
| 1644 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1645 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1646 | if (desc < ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1647 | n = n->rb_left; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1648 | } else if (desc > ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1649 | n = n->rb_right; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1650 | } else if (need_strong_ref && !ref->data.strong) { |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1651 | binder_user_error("tried to use weak ref as strong ref\n"); |
| 1652 | return NULL; |
| 1653 | } else { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1654 | return ref; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1655 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1656 | } |
| 1657 | return NULL; |
| 1658 | } |
| 1659 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1660 | /** |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1661 | * 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] | 1662 | * @proc: binder_proc that owns the ref |
| 1663 | * @node: binder_node of target |
| 1664 | * @new_ref: newly allocated binder_ref to be initialized or %NULL |
| 1665 | * |
| 1666 | * Look up the ref for the given node and return it if it exists |
| 1667 | * |
| 1668 | * If it doesn't exist and the caller provides a newly allocated |
| 1669 | * ref, initialize the fields of the newly allocated ref and insert |
| 1670 | * into the given proc rb_trees and node refs list. |
| 1671 | * |
| 1672 | * Return: the ref for node. It is possible that another thread |
| 1673 | * allocated/initialized the ref first in which case the |
| 1674 | * returned ref would be different than the passed-in |
| 1675 | * new_ref. new_ref must be kfree'd by the caller in |
| 1676 | * this case. |
| 1677 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1678 | static struct binder_ref *binder_get_ref_for_node_olocked( |
| 1679 | struct binder_proc *proc, |
| 1680 | struct binder_node *node, |
| 1681 | struct binder_ref *new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1682 | { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1683 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1684 | struct rb_node **p = &proc->refs_by_node.rb_node; |
| 1685 | struct rb_node *parent = NULL; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1686 | struct binder_ref *ref; |
| 1687 | struct rb_node *n; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1688 | |
| 1689 | while (*p) { |
| 1690 | parent = *p; |
| 1691 | ref = rb_entry(parent, struct binder_ref, rb_node_node); |
| 1692 | |
| 1693 | if (node < ref->node) |
| 1694 | p = &(*p)->rb_left; |
| 1695 | else if (node > ref->node) |
| 1696 | p = &(*p)->rb_right; |
| 1697 | else |
| 1698 | return ref; |
| 1699 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1700 | if (!new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1701 | return NULL; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1702 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1703 | binder_stats_created(BINDER_STAT_REF); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1704 | new_ref->data.debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1705 | new_ref->proc = proc; |
| 1706 | new_ref->node = node; |
| 1707 | rb_link_node(&new_ref->rb_node_node, parent, p); |
| 1708 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); |
| 1709 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1710 | 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] | 1711 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 1712 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1713 | if (ref->data.desc > new_ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1714 | break; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1715 | new_ref->data.desc = ref->data.desc + 1; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | p = &proc->refs_by_desc.rb_node; |
| 1719 | while (*p) { |
| 1720 | parent = *p; |
| 1721 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); |
| 1722 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1723 | if (new_ref->data.desc < ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1724 | p = &(*p)->rb_left; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1725 | else if (new_ref->data.desc > ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1726 | p = &(*p)->rb_right; |
| 1727 | else |
| 1728 | BUG(); |
| 1729 | } |
| 1730 | rb_link_node(&new_ref->rb_node_desc, parent, p); |
| 1731 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1732 | |
| 1733 | binder_node_lock(node); |
Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1734 | hlist_add_head(&new_ref->node_entry, &node->refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1735 | |
Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1736 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1737 | "%d new ref %d desc %d for node %d\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1738 | proc->pid, new_ref->data.debug_id, new_ref->data.desc, |
Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1739 | node->debug_id); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1740 | binder_node_unlock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1741 | return new_ref; |
| 1742 | } |
| 1743 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1744 | static void binder_cleanup_ref_olocked(struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1745 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1746 | bool delete_node = false; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1747 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1748 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1749 | "%d delete ref %d desc %d for node %d\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1750 | ref->proc->pid, ref->data.debug_id, ref->data.desc, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1751 | ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1752 | |
| 1753 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); |
| 1754 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1755 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1756 | binder_node_inner_lock(ref->node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1757 | if (ref->data.strong) |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1758 | binder_dec_node_nilocked(ref->node, 1, 1); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1759 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1760 | hlist_del(&ref->node_entry); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1761 | delete_node = binder_dec_node_nilocked(ref->node, 0, 1); |
| 1762 | binder_node_inner_unlock(ref->node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1763 | /* |
| 1764 | * Clear ref->node unless we want the caller to free the node |
| 1765 | */ |
| 1766 | if (!delete_node) { |
| 1767 | /* |
| 1768 | * The caller uses ref->node to determine |
| 1769 | * whether the node needs to be freed. Clear |
| 1770 | * it since the node is still alive. |
| 1771 | */ |
| 1772 | ref->node = NULL; |
| 1773 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1774 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1775 | if (ref->death) { |
| 1776 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1777 | "%d delete ref %d desc %d has death notification\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1778 | ref->proc->pid, ref->data.debug_id, |
| 1779 | ref->data.desc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1780 | binder_dequeue_work(ref->proc, &ref->death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1781 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 1782 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1783 | binder_stats_deleted(BINDER_STAT_REF); |
| 1784 | } |
| 1785 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1786 | /** |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1787 | * binder_inc_ref_olocked() - increment the ref for given handle |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1788 | * @ref: ref to be incremented |
| 1789 | * @strong: if true, strong increment, else weak |
| 1790 | * @target_list: list to queue node work on |
| 1791 | * |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1792 | * Increment the ref. @ref->proc->outer_lock must be held on entry |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1793 | * |
| 1794 | * Return: 0, if successful, else errno |
| 1795 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1796 | static int binder_inc_ref_olocked(struct binder_ref *ref, int strong, |
| 1797 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1798 | { |
| 1799 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1800 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1801 | if (strong) { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1802 | if (ref->data.strong == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1803 | ret = binder_inc_node(ref->node, 1, 1, target_list); |
| 1804 | if (ret) |
| 1805 | return ret; |
| 1806 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1807 | ref->data.strong++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1808 | } else { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1809 | if (ref->data.weak == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1810 | ret = binder_inc_node(ref->node, 0, 1, target_list); |
| 1811 | if (ret) |
| 1812 | return ret; |
| 1813 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1814 | ref->data.weak++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1815 | } |
| 1816 | return 0; |
| 1817 | } |
| 1818 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1819 | /** |
| 1820 | * binder_dec_ref() - dec the ref for given handle |
| 1821 | * @ref: ref to be decremented |
| 1822 | * @strong: if true, strong decrement, else weak |
| 1823 | * |
| 1824 | * Decrement the ref. |
| 1825 | * |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1826 | * Return: true if ref is cleaned up and ready to be freed |
| 1827 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1828 | 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] | 1829 | { |
| 1830 | if (strong) { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1831 | if (ref->data.strong == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1832 | 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] | 1833 | ref->proc->pid, ref->data.debug_id, |
| 1834 | ref->data.desc, ref->data.strong, |
| 1835 | ref->data.weak); |
| 1836 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1837 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1838 | ref->data.strong--; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1839 | if (ref->data.strong == 0) |
| 1840 | binder_dec_node(ref->node, strong, 1); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1841 | } else { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1842 | if (ref->data.weak == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1843 | 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] | 1844 | ref->proc->pid, ref->data.debug_id, |
| 1845 | ref->data.desc, ref->data.strong, |
| 1846 | ref->data.weak); |
| 1847 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1848 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1849 | ref->data.weak--; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1850 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1851 | if (ref->data.strong == 0 && ref->data.weak == 0) { |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1852 | binder_cleanup_ref_olocked(ref); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1853 | return true; |
| 1854 | } |
| 1855 | return false; |
| 1856 | } |
| 1857 | |
| 1858 | /** |
| 1859 | * binder_get_node_from_ref() - get the node from the given proc/desc |
| 1860 | * @proc: proc containing the ref |
| 1861 | * @desc: the handle associated with the ref |
| 1862 | * @need_strong_ref: if true, only return node if ref is strong |
| 1863 | * @rdata: the id/refcount data for the ref |
| 1864 | * |
| 1865 | * Given a proc and ref handle, return the associated binder_node |
| 1866 | * |
| 1867 | * Return: a binder_node or NULL if not found or not strong when strong required |
| 1868 | */ |
| 1869 | static struct binder_node *binder_get_node_from_ref( |
| 1870 | struct binder_proc *proc, |
| 1871 | u32 desc, bool need_strong_ref, |
| 1872 | struct binder_ref_data *rdata) |
| 1873 | { |
| 1874 | struct binder_node *node; |
| 1875 | struct binder_ref *ref; |
| 1876 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1877 | binder_proc_lock(proc); |
| 1878 | ref = binder_get_ref_olocked(proc, desc, need_strong_ref); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1879 | if (!ref) |
| 1880 | goto err_no_ref; |
| 1881 | node = ref->node; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1882 | /* |
| 1883 | * Take an implicit reference on the node to ensure |
| 1884 | * it stays alive until the call to binder_put_node() |
| 1885 | */ |
| 1886 | binder_inc_node_tmpref(node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1887 | if (rdata) |
| 1888 | *rdata = ref->data; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1889 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1890 | |
| 1891 | return node; |
| 1892 | |
| 1893 | err_no_ref: |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1894 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1895 | return NULL; |
| 1896 | } |
| 1897 | |
| 1898 | /** |
| 1899 | * binder_free_ref() - free the binder_ref |
| 1900 | * @ref: ref to free |
| 1901 | * |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1902 | * Free the binder_ref. Free the binder_node indicated by ref->node |
| 1903 | * (if non-NULL) and the binder_ref_death indicated by ref->death. |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1904 | */ |
| 1905 | static void binder_free_ref(struct binder_ref *ref) |
| 1906 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1907 | if (ref->node) |
| 1908 | binder_free_node(ref->node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1909 | kfree(ref->death); |
| 1910 | kfree(ref); |
| 1911 | } |
| 1912 | |
| 1913 | /** |
| 1914 | * binder_update_ref_for_handle() - inc/dec the ref for given handle |
| 1915 | * @proc: proc containing the ref |
| 1916 | * @desc: the handle associated with the ref |
| 1917 | * @increment: true=inc reference, false=dec reference |
| 1918 | * @strong: true=strong reference, false=weak reference |
| 1919 | * @rdata: the id/refcount data for the ref |
| 1920 | * |
| 1921 | * Given a proc and ref handle, increment or decrement the ref |
| 1922 | * according to "increment" arg. |
| 1923 | * |
| 1924 | * Return: 0 if successful, else errno |
| 1925 | */ |
| 1926 | static int binder_update_ref_for_handle(struct binder_proc *proc, |
| 1927 | uint32_t desc, bool increment, bool strong, |
| 1928 | struct binder_ref_data *rdata) |
| 1929 | { |
| 1930 | int ret = 0; |
| 1931 | struct binder_ref *ref; |
| 1932 | bool delete_ref = false; |
| 1933 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1934 | binder_proc_lock(proc); |
| 1935 | ref = binder_get_ref_olocked(proc, desc, strong); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1936 | if (!ref) { |
| 1937 | ret = -EINVAL; |
| 1938 | goto err_no_ref; |
| 1939 | } |
| 1940 | if (increment) |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1941 | ret = binder_inc_ref_olocked(ref, strong, NULL); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1942 | else |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1943 | delete_ref = binder_dec_ref_olocked(ref, strong); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1944 | |
| 1945 | if (rdata) |
| 1946 | *rdata = ref->data; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1947 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1948 | |
| 1949 | if (delete_ref) |
| 1950 | binder_free_ref(ref); |
| 1951 | return ret; |
| 1952 | |
| 1953 | err_no_ref: |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1954 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1955 | return ret; |
| 1956 | } |
| 1957 | |
| 1958 | /** |
| 1959 | * binder_dec_ref_for_handle() - dec the ref for given handle |
| 1960 | * @proc: proc containing the ref |
| 1961 | * @desc: the handle associated with the ref |
| 1962 | * @strong: true=strong reference, false=weak reference |
| 1963 | * @rdata: the id/refcount data for the ref |
| 1964 | * |
| 1965 | * Just calls binder_update_ref_for_handle() to decrement the ref. |
| 1966 | * |
| 1967 | * Return: 0 if successful, else errno |
| 1968 | */ |
| 1969 | static int binder_dec_ref_for_handle(struct binder_proc *proc, |
| 1970 | uint32_t desc, bool strong, struct binder_ref_data *rdata) |
| 1971 | { |
| 1972 | return binder_update_ref_for_handle(proc, desc, false, strong, rdata); |
| 1973 | } |
| 1974 | |
| 1975 | |
| 1976 | /** |
| 1977 | * binder_inc_ref_for_node() - increment the ref for given proc/node |
| 1978 | * @proc: proc containing the ref |
| 1979 | * @node: target node |
| 1980 | * @strong: true=strong reference, false=weak reference |
| 1981 | * @target_list: worklist to use if node is incremented |
| 1982 | * @rdata: the id/refcount data for the ref |
| 1983 | * |
| 1984 | * Given a proc and node, increment the ref. Create the ref if it |
| 1985 | * doesn't already exist |
| 1986 | * |
| 1987 | * Return: 0 if successful, else errno |
| 1988 | */ |
| 1989 | static int binder_inc_ref_for_node(struct binder_proc *proc, |
| 1990 | struct binder_node *node, |
| 1991 | bool strong, |
| 1992 | struct list_head *target_list, |
| 1993 | struct binder_ref_data *rdata) |
| 1994 | { |
| 1995 | struct binder_ref *ref; |
| 1996 | struct binder_ref *new_ref = NULL; |
| 1997 | int ret = 0; |
| 1998 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1999 | binder_proc_lock(proc); |
| 2000 | ref = binder_get_ref_for_node_olocked(proc, node, NULL); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2001 | if (!ref) { |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2002 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2003 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
| 2004 | if (!new_ref) |
| 2005 | return -ENOMEM; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2006 | binder_proc_lock(proc); |
| 2007 | ref = binder_get_ref_for_node_olocked(proc, node, new_ref); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2008 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2009 | ret = binder_inc_ref_olocked(ref, strong, target_list); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2010 | *rdata = ref->data; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2011 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2012 | if (new_ref && ref != new_ref) |
| 2013 | /* |
| 2014 | * Another thread created the ref first so |
| 2015 | * free the one we allocated |
| 2016 | */ |
| 2017 | kfree(new_ref); |
| 2018 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2019 | } |
| 2020 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2021 | static void binder_pop_transaction_ilocked(struct binder_thread *target_thread, |
| 2022 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2023 | { |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2024 | BUG_ON(!target_thread); |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 2025 | assert_spin_locked(&target_thread->proc->inner_lock); |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2026 | BUG_ON(target_thread->transaction_stack != t); |
| 2027 | BUG_ON(target_thread->transaction_stack->from != target_thread); |
| 2028 | target_thread->transaction_stack = |
| 2029 | target_thread->transaction_stack->from_parent; |
| 2030 | t->from = NULL; |
| 2031 | } |
| 2032 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2033 | /** |
| 2034 | * binder_thread_dec_tmpref() - decrement thread->tmp_ref |
| 2035 | * @thread: thread to decrement |
| 2036 | * |
| 2037 | * A thread needs to be kept alive while being used to create or |
| 2038 | * handle a transaction. binder_get_txn_from() is used to safely |
| 2039 | * extract t->from from a binder_transaction and keep the thread |
| 2040 | * indicated by t->from from being freed. When done with that |
| 2041 | * binder_thread, this function is called to decrement the |
| 2042 | * tmp_ref and free if appropriate (thread has been released |
| 2043 | * and no transaction being processed by the driver) |
| 2044 | */ |
| 2045 | static void binder_thread_dec_tmpref(struct binder_thread *thread) |
| 2046 | { |
| 2047 | /* |
| 2048 | * atomic is used to protect the counter value while |
| 2049 | * it cannot reach zero or thread->is_dead is false |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2050 | */ |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2051 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2052 | atomic_dec(&thread->tmp_ref); |
| 2053 | if (thread->is_dead && !atomic_read(&thread->tmp_ref)) { |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2054 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2055 | binder_free_thread(thread); |
| 2056 | return; |
| 2057 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2058 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | /** |
| 2062 | * binder_proc_dec_tmpref() - decrement proc->tmp_ref |
| 2063 | * @proc: proc to decrement |
| 2064 | * |
| 2065 | * A binder_proc needs to be kept alive while being used to create or |
| 2066 | * handle a transaction. proc->tmp_ref is incremented when |
| 2067 | * creating a new transaction or the binder_proc is currently in-use |
| 2068 | * by threads that are being released. When done with the binder_proc, |
| 2069 | * this function is called to decrement the counter and free the |
| 2070 | * proc if appropriate (proc has been released, all threads have |
| 2071 | * been released and not currenly in-use to process a transaction). |
| 2072 | */ |
| 2073 | static void binder_proc_dec_tmpref(struct binder_proc *proc) |
| 2074 | { |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2075 | binder_inner_proc_lock(proc); |
Todd Kjos | dc734d7 | 2019-06-10 09:14:25 -0700 | [diff] [blame] | 2076 | atomic_dec(&proc->tmp_ref); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2077 | if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) && |
Todd Kjos | dc734d7 | 2019-06-10 09:14:25 -0700 | [diff] [blame] | 2078 | !atomic_read(&proc->tmp_ref)) { |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2079 | binder_inner_proc_unlock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2080 | binder_free_proc(proc); |
| 2081 | return; |
| 2082 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2083 | binder_inner_proc_unlock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | /** |
| 2087 | * binder_get_txn_from() - safely extract the "from" thread in transaction |
| 2088 | * @t: binder transaction for t->from |
| 2089 | * |
| 2090 | * Atomically return the "from" thread and increment the tmp_ref |
| 2091 | * count for the thread to ensure it stays alive until |
| 2092 | * binder_thread_dec_tmpref() is called. |
| 2093 | * |
| 2094 | * Return: the value of t->from |
| 2095 | */ |
| 2096 | static struct binder_thread *binder_get_txn_from( |
| 2097 | struct binder_transaction *t) |
| 2098 | { |
| 2099 | struct binder_thread *from; |
| 2100 | |
| 2101 | spin_lock(&t->lock); |
| 2102 | from = t->from; |
| 2103 | if (from) |
| 2104 | atomic_inc(&from->tmp_ref); |
| 2105 | spin_unlock(&t->lock); |
| 2106 | return from; |
| 2107 | } |
| 2108 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2109 | /** |
| 2110 | * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock |
| 2111 | * @t: binder transaction for t->from |
| 2112 | * |
| 2113 | * Same as binder_get_txn_from() except it also acquires the proc->inner_lock |
| 2114 | * to guarantee that the thread cannot be released while operating on it. |
| 2115 | * The caller must call binder_inner_proc_unlock() to release the inner lock |
| 2116 | * as well as call binder_dec_thread_txn() to release the reference. |
| 2117 | * |
| 2118 | * Return: the value of t->from |
| 2119 | */ |
| 2120 | static struct binder_thread *binder_get_txn_from_and_acq_inner( |
| 2121 | struct binder_transaction *t) |
| 2122 | { |
| 2123 | struct binder_thread *from; |
| 2124 | |
| 2125 | from = binder_get_txn_from(t); |
| 2126 | if (!from) |
| 2127 | return NULL; |
| 2128 | binder_inner_proc_lock(from->proc); |
| 2129 | if (t->from) { |
| 2130 | BUG_ON(from != t->from); |
| 2131 | return from; |
| 2132 | } |
| 2133 | binder_inner_proc_unlock(from->proc); |
| 2134 | binder_thread_dec_tmpref(from); |
| 2135 | return NULL; |
| 2136 | } |
| 2137 | |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2138 | static void binder_free_transaction(struct binder_transaction *t) |
| 2139 | { |
Todd Kjos | dc734d7 | 2019-06-10 09:14:25 -0700 | [diff] [blame] | 2140 | struct binder_proc *target_proc; |
| 2141 | |
| 2142 | spin_lock(&t->lock); |
| 2143 | target_proc = t->to_proc; |
| 2144 | if (target_proc) { |
| 2145 | atomic_inc(&target_proc->tmp_ref); |
| 2146 | spin_unlock(&t->lock); |
| 2147 | |
| 2148 | binder_inner_proc_lock(target_proc); |
| 2149 | if (t->buffer) |
| 2150 | t->buffer->transaction = NULL; |
| 2151 | binder_inner_proc_unlock(target_proc); |
| 2152 | binder_proc_dec_tmpref(target_proc); |
| 2153 | } else { |
| 2154 | /* |
| 2155 | * If the transaction has no target_proc, then |
| 2156 | * t->buffer->transaction * has already been cleared. |
| 2157 | */ |
| 2158 | spin_unlock(&t->lock); |
| 2159 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2160 | kfree(t); |
| 2161 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 2162 | } |
| 2163 | |
| 2164 | static void binder_send_failed_reply(struct binder_transaction *t, |
| 2165 | uint32_t error_code) |
| 2166 | { |
| 2167 | struct binder_thread *target_thread; |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2168 | struct binder_transaction *next; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2169 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2170 | BUG_ON(t->flags & TF_ONE_WAY); |
| 2171 | while (1) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2172 | target_thread = binder_get_txn_from_and_acq_inner(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2173 | if (target_thread) { |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2174 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2175 | "send failed reply for transaction %d to %d:%d\n", |
| 2176 | t->debug_id, |
| 2177 | target_thread->proc->pid, |
| 2178 | target_thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2179 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2180 | binder_pop_transaction_ilocked(target_thread, t); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2181 | if (target_thread->reply_error.cmd == BR_OK) { |
| 2182 | target_thread->reply_error.cmd = error_code; |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2183 | binder_enqueue_thread_work_ilocked( |
| 2184 | target_thread, |
| 2185 | &target_thread->reply_error.work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2186 | wake_up_interruptible(&target_thread->wait); |
| 2187 | } else { |
Todd Kjos | d3a2afb | 2018-02-07 12:38:47 -0800 | [diff] [blame] | 2188 | /* |
| 2189 | * Cannot get here for normal operation, but |
| 2190 | * we can if multiple synchronous transactions |
| 2191 | * are sent without blocking for responses. |
| 2192 | * Just ignore the 2nd error in this case. |
| 2193 | */ |
| 2194 | pr_warn("Unexpected reply error: %u\n", |
| 2195 | target_thread->reply_error.cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2196 | } |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2197 | binder_inner_proc_unlock(target_thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2198 | binder_thread_dec_tmpref(target_thread); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2199 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2200 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2201 | } |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2202 | next = t->from_parent; |
| 2203 | |
| 2204 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2205 | "send failed reply for transaction %d, target dead\n", |
| 2206 | t->debug_id); |
| 2207 | |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2208 | binder_free_transaction(t); |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2209 | if (next == NULL) { |
| 2210 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2211 | "reply failed, no target thread at root\n"); |
| 2212 | return; |
| 2213 | } |
| 2214 | t = next; |
| 2215 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2216 | "reply failed, no target thread -- retry %d\n", |
| 2217 | t->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2221 | /** |
Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 2222 | * binder_cleanup_transaction() - cleans up undelivered transaction |
| 2223 | * @t: transaction that needs to be cleaned up |
| 2224 | * @reason: reason the transaction wasn't delivered |
| 2225 | * @error_code: error to return to caller (if synchronous call) |
| 2226 | */ |
| 2227 | static void binder_cleanup_transaction(struct binder_transaction *t, |
| 2228 | const char *reason, |
| 2229 | uint32_t error_code) |
| 2230 | { |
| 2231 | if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) { |
| 2232 | binder_send_failed_reply(t, error_code); |
| 2233 | } else { |
| 2234 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 2235 | "undelivered transaction %d, %s\n", |
| 2236 | t->debug_id, reason); |
| 2237 | binder_free_transaction(t); |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | /** |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2242 | * binder_get_object() - gets object and checks for valid metadata |
| 2243 | * @proc: binder_proc owning the buffer |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2244 | * @buffer: binder_buffer that we're parsing. |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2245 | * @offset: offset in the @buffer at which to validate an object. |
| 2246 | * @object: struct binder_object to read into |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2247 | * |
| 2248 | * Return: If there's a valid metadata object at @offset in @buffer, the |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2249 | * size of that object. Otherwise, it returns zero. The object |
| 2250 | * is read into the struct binder_object pointed to by @object. |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2251 | */ |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2252 | static size_t binder_get_object(struct binder_proc *proc, |
| 2253 | struct binder_buffer *buffer, |
| 2254 | unsigned long offset, |
| 2255 | struct binder_object *object) |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2256 | { |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2257 | size_t read_size; |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2258 | struct binder_object_header *hdr; |
| 2259 | size_t object_size = 0; |
| 2260 | |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2261 | read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset); |
Todd Kjos | c8ddc8c | 2019-03-19 09:53:01 -0700 | [diff] [blame] | 2262 | if (offset > buffer->data_size || read_size < sizeof(*hdr) || |
| 2263 | !IS_ALIGNED(offset, sizeof(u32))) |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2264 | return 0; |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2265 | binder_alloc_copy_from_buffer(&proc->alloc, object, buffer, |
| 2266 | offset, read_size); |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2267 | |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2268 | /* Ok, now see if we read a complete object. */ |
| 2269 | hdr = &object->hdr; |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2270 | switch (hdr->type) { |
| 2271 | case BINDER_TYPE_BINDER: |
| 2272 | case BINDER_TYPE_WEAK_BINDER: |
| 2273 | case BINDER_TYPE_HANDLE: |
| 2274 | case BINDER_TYPE_WEAK_HANDLE: |
| 2275 | object_size = sizeof(struct flat_binder_object); |
| 2276 | break; |
| 2277 | case BINDER_TYPE_FD: |
| 2278 | object_size = sizeof(struct binder_fd_object); |
| 2279 | break; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2280 | case BINDER_TYPE_PTR: |
| 2281 | object_size = sizeof(struct binder_buffer_object); |
| 2282 | break; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2283 | case BINDER_TYPE_FDA: |
| 2284 | object_size = sizeof(struct binder_fd_array_object); |
| 2285 | break; |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2286 | default: |
| 2287 | return 0; |
| 2288 | } |
| 2289 | if (offset <= buffer->data_size - object_size && |
| 2290 | buffer->data_size >= object_size) |
| 2291 | return object_size; |
| 2292 | else |
| 2293 | return 0; |
| 2294 | } |
| 2295 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2296 | /** |
| 2297 | * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer. |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2298 | * @proc: binder_proc owning the buffer |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2299 | * @b: binder_buffer containing the object |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2300 | * @object: struct binder_object to read into |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2301 | * @index: index in offset array at which the binder_buffer_object is |
| 2302 | * located |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2303 | * @start_offset: points to the start of the offset array |
| 2304 | * @object_offsetp: offset of @object read from @b |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2305 | * @num_valid: the number of valid offsets in the offset array |
| 2306 | * |
| 2307 | * Return: If @index is within the valid range of the offset array |
| 2308 | * described by @start and @num_valid, and if there's a valid |
| 2309 | * binder_buffer_object at the offset found in index @index |
| 2310 | * of the offset array, that object is returned. Otherwise, |
| 2311 | * %NULL is returned. |
| 2312 | * Note that the offset found in index @index itself is not |
| 2313 | * verified; this function assumes that @num_valid elements |
| 2314 | * from @start were previously verified to have valid offsets. |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2315 | * If @object_offsetp is non-NULL, then the offset within |
| 2316 | * @b is written to it. |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2317 | */ |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2318 | static struct binder_buffer_object *binder_validate_ptr( |
| 2319 | struct binder_proc *proc, |
| 2320 | struct binder_buffer *b, |
| 2321 | struct binder_object *object, |
| 2322 | binder_size_t index, |
| 2323 | binder_size_t start_offset, |
| 2324 | binder_size_t *object_offsetp, |
| 2325 | binder_size_t num_valid) |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2326 | { |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2327 | size_t object_size; |
| 2328 | binder_size_t object_offset; |
| 2329 | unsigned long buffer_offset; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2330 | |
| 2331 | if (index >= num_valid) |
| 2332 | return NULL; |
| 2333 | |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2334 | buffer_offset = start_offset + sizeof(binder_size_t) * index; |
| 2335 | binder_alloc_copy_from_buffer(&proc->alloc, &object_offset, |
| 2336 | b, buffer_offset, sizeof(object_offset)); |
| 2337 | object_size = binder_get_object(proc, b, object_offset, object); |
| 2338 | if (!object_size || object->hdr.type != BINDER_TYPE_PTR) |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2339 | return NULL; |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2340 | if (object_offsetp) |
| 2341 | *object_offsetp = object_offset; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2342 | |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2343 | return &object->bbo; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | /** |
| 2347 | * binder_validate_fixup() - validates pointer/fd fixups happen in order. |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2348 | * @proc: binder_proc owning the buffer |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2349 | * @b: transaction buffer |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2350 | * @objects_start_offset: offset to start of objects buffer |
| 2351 | * @buffer_obj_offset: offset to binder_buffer_object in which to fix up |
| 2352 | * @fixup_offset: start offset in @buffer to fix up |
| 2353 | * @last_obj_offset: offset to last binder_buffer_object that we fixed |
| 2354 | * @last_min_offset: minimum fixup offset in object at @last_obj_offset |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2355 | * |
| 2356 | * Return: %true if a fixup in buffer @buffer at offset @offset is |
| 2357 | * allowed. |
| 2358 | * |
| 2359 | * For safety reasons, we only allow fixups inside a buffer to happen |
| 2360 | * at increasing offsets; additionally, we only allow fixup on the last |
| 2361 | * buffer object that was verified, or one of its parents. |
| 2362 | * |
| 2363 | * Example of what is allowed: |
| 2364 | * |
| 2365 | * A |
| 2366 | * B (parent = A, offset = 0) |
| 2367 | * C (parent = A, offset = 16) |
| 2368 | * D (parent = C, offset = 0) |
| 2369 | * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset) |
| 2370 | * |
| 2371 | * Examples of what is not allowed: |
| 2372 | * |
| 2373 | * Decreasing offsets within the same parent: |
| 2374 | * A |
| 2375 | * C (parent = A, offset = 16) |
| 2376 | * B (parent = A, offset = 0) // decreasing offset within A |
| 2377 | * |
| 2378 | * Referring to a parent that wasn't the last object or any of its parents: |
| 2379 | * A |
| 2380 | * B (parent = A, offset = 0) |
| 2381 | * C (parent = A, offset = 0) |
| 2382 | * C (parent = A, offset = 16) |
| 2383 | * D (parent = B, offset = 0) // B is not A or any of A's parents |
| 2384 | */ |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2385 | static bool binder_validate_fixup(struct binder_proc *proc, |
| 2386 | struct binder_buffer *b, |
| 2387 | binder_size_t objects_start_offset, |
| 2388 | binder_size_t buffer_obj_offset, |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2389 | binder_size_t fixup_offset, |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2390 | binder_size_t last_obj_offset, |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2391 | binder_size_t last_min_offset) |
| 2392 | { |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2393 | if (!last_obj_offset) { |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2394 | /* Nothing to fix up in */ |
| 2395 | return false; |
| 2396 | } |
| 2397 | |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2398 | while (last_obj_offset != buffer_obj_offset) { |
| 2399 | unsigned long buffer_offset; |
| 2400 | struct binder_object last_object; |
| 2401 | struct binder_buffer_object *last_bbo; |
| 2402 | size_t object_size = binder_get_object(proc, b, last_obj_offset, |
| 2403 | &last_object); |
| 2404 | if (object_size != sizeof(*last_bbo)) |
| 2405 | return false; |
| 2406 | |
| 2407 | last_bbo = &last_object.bbo; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2408 | /* |
| 2409 | * Safe to retrieve the parent of last_obj, since it |
| 2410 | * was already previously verified by the driver. |
| 2411 | */ |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2412 | if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0) |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2413 | return false; |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2414 | last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t); |
| 2415 | buffer_offset = objects_start_offset + |
| 2416 | sizeof(binder_size_t) * last_bbo->parent, |
| 2417 | binder_alloc_copy_from_buffer(&proc->alloc, &last_obj_offset, |
| 2418 | b, buffer_offset, |
| 2419 | sizeof(last_obj_offset)); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2420 | } |
| 2421 | return (fixup_offset >= last_min_offset); |
| 2422 | } |
| 2423 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2424 | static void binder_transaction_buffer_release(struct binder_proc *proc, |
| 2425 | struct binder_buffer *buffer, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2426 | binder_size_t failed_at, |
| 2427 | bool is_failure) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2428 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2429 | int debug_id = buffer->debug_id; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2430 | binder_size_t off_start_offset, buffer_offset, off_end_offset; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2431 | |
| 2432 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2433 | "%d buffer release %d, size %zd-%zd, failed at %llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2434 | proc->pid, buffer->debug_id, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2435 | buffer->data_size, buffer->offsets_size, |
| 2436 | (unsigned long long)failed_at); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2437 | |
| 2438 | if (buffer->target_node) |
| 2439 | binder_dec_node(buffer->target_node, 1, 0); |
| 2440 | |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2441 | off_start_offset = ALIGN(buffer->data_size, sizeof(void *)); |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2442 | off_end_offset = is_failure ? failed_at : |
| 2443 | off_start_offset + buffer->offsets_size; |
| 2444 | for (buffer_offset = off_start_offset; buffer_offset < off_end_offset; |
| 2445 | buffer_offset += sizeof(binder_size_t)) { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2446 | struct binder_object_header *hdr; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2447 | size_t object_size; |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2448 | struct binder_object object; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2449 | binder_size_t object_offset; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2450 | |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2451 | binder_alloc_copy_from_buffer(&proc->alloc, &object_offset, |
| 2452 | buffer, buffer_offset, |
| 2453 | sizeof(object_offset)); |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2454 | object_size = binder_get_object(proc, buffer, |
| 2455 | object_offset, &object); |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2456 | if (object_size == 0) { |
| 2457 | pr_err("transaction release %d bad object at offset %lld, size %zd\n", |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2458 | debug_id, (u64)object_offset, buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2459 | continue; |
| 2460 | } |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2461 | hdr = &object.hdr; |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2462 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2463 | case BINDER_TYPE_BINDER: |
| 2464 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2465 | struct flat_binder_object *fp; |
| 2466 | struct binder_node *node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2467 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2468 | fp = to_flat_binder_object(hdr); |
| 2469 | node = binder_get_node(proc, fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2470 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2471 | pr_err("transaction release %d bad node %016llx\n", |
| 2472 | debug_id, (u64)fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2473 | break; |
| 2474 | } |
| 2475 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2476 | " node %d u%016llx\n", |
| 2477 | node->debug_id, (u64)node->ptr); |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2478 | binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER, |
| 2479 | 0); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2480 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2481 | } break; |
| 2482 | case BINDER_TYPE_HANDLE: |
| 2483 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2484 | struct flat_binder_object *fp; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2485 | struct binder_ref_data rdata; |
| 2486 | int ret; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 2487 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2488 | fp = to_flat_binder_object(hdr); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2489 | ret = binder_dec_ref_for_handle(proc, fp->handle, |
| 2490 | hdr->type == BINDER_TYPE_HANDLE, &rdata); |
| 2491 | |
| 2492 | if (ret) { |
| 2493 | pr_err("transaction release %d bad handle %d, ret = %d\n", |
| 2494 | debug_id, fp->handle, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2495 | break; |
| 2496 | } |
| 2497 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2498 | " ref %d desc %d\n", |
| 2499 | rdata.debug_id, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2500 | } break; |
| 2501 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2502 | case BINDER_TYPE_FD: { |
| 2503 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
| 2504 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2505 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2506 | " fd %d\n", fp->fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2507 | if (failed_at) |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2508 | task_close_fd(proc, fp->fd); |
| 2509 | } break; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2510 | case BINDER_TYPE_PTR: |
| 2511 | /* |
| 2512 | * Nothing to do here, this will get cleaned up when the |
| 2513 | * transaction buffer gets freed |
| 2514 | */ |
| 2515 | break; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2516 | case BINDER_TYPE_FDA: { |
| 2517 | struct binder_fd_array_object *fda; |
| 2518 | struct binder_buffer_object *parent; |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2519 | struct binder_object ptr_object; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2520 | binder_size_t fda_offset; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2521 | size_t fd_index; |
| 2522 | binder_size_t fd_buf_size; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2523 | binder_size_t num_valid; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2524 | |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2525 | num_valid = (buffer_offset - off_start_offset) / |
| 2526 | sizeof(binder_size_t); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2527 | fda = to_binder_fd_array_object(hdr); |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2528 | parent = binder_validate_ptr(proc, buffer, &ptr_object, |
| 2529 | fda->parent, |
| 2530 | off_start_offset, |
| 2531 | NULL, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2532 | num_valid); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2533 | if (!parent) { |
| 2534 | pr_err("transaction release %d bad parent offset", |
| 2535 | debug_id); |
| 2536 | continue; |
| 2537 | } |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2538 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2539 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2540 | pr_err("transaction release %d invalid number of fds (%lld)\n", |
| 2541 | debug_id, (u64)fda->num_fds); |
| 2542 | continue; |
| 2543 | } |
| 2544 | if (fd_buf_size > parent->length || |
| 2545 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2546 | /* No space for all file descriptors here. */ |
| 2547 | pr_err("transaction release %d not enough space for %lld fds in buffer\n", |
| 2548 | debug_id, (u64)fda->num_fds); |
| 2549 | continue; |
| 2550 | } |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2551 | /* |
| 2552 | * the source data for binder_buffer_object is visible |
| 2553 | * to user-space and the @buffer element is the user |
| 2554 | * pointer to the buffer_object containing the fd_array. |
| 2555 | * Convert the address to an offset relative to |
| 2556 | * the base of the transaction buffer. |
| 2557 | */ |
| 2558 | fda_offset = |
| 2559 | (parent->buffer - (uintptr_t)buffer->user_data) + |
| 2560 | fda->parent_offset; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2561 | for (fd_index = 0; fd_index < fda->num_fds; |
| 2562 | fd_index++) { |
| 2563 | u32 fd; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2564 | binder_size_t offset = fda_offset + |
| 2565 | fd_index * sizeof(fd); |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2566 | |
| 2567 | binder_alloc_copy_from_buffer(&proc->alloc, |
| 2568 | &fd, |
| 2569 | buffer, |
| 2570 | offset, |
| 2571 | sizeof(fd)); |
| 2572 | task_close_fd(proc, fd); |
| 2573 | } |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2574 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2575 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 2576 | pr_err("transaction release %d bad object type %x\n", |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2577 | debug_id, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2578 | break; |
| 2579 | } |
| 2580 | } |
| 2581 | } |
| 2582 | |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2583 | static int binder_translate_binder(struct flat_binder_object *fp, |
| 2584 | struct binder_transaction *t, |
| 2585 | struct binder_thread *thread) |
| 2586 | { |
| 2587 | struct binder_node *node; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2588 | struct binder_proc *proc = thread->proc; |
| 2589 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2590 | struct binder_ref_data rdata; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2591 | int ret = 0; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2592 | |
| 2593 | node = binder_get_node(proc, fp->binder); |
| 2594 | if (!node) { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2595 | node = binder_new_node(proc, fp); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2596 | if (!node) |
| 2597 | return -ENOMEM; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2598 | } |
| 2599 | if (fp->cookie != node->cookie) { |
| 2600 | binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n", |
| 2601 | proc->pid, thread->pid, (u64)fp->binder, |
| 2602 | node->debug_id, (u64)fp->cookie, |
| 2603 | (u64)node->cookie); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2604 | ret = -EINVAL; |
| 2605 | goto done; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2606 | } |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2607 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2608 | ret = -EPERM; |
| 2609 | goto done; |
| 2610 | } |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2611 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2612 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2613 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2614 | &thread->todo, &rdata); |
| 2615 | if (ret) |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2616 | goto done; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2617 | |
| 2618 | if (fp->hdr.type == BINDER_TYPE_BINDER) |
| 2619 | fp->hdr.type = BINDER_TYPE_HANDLE; |
| 2620 | else |
| 2621 | fp->hdr.type = BINDER_TYPE_WEAK_HANDLE; |
| 2622 | fp->binder = 0; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2623 | fp->handle = rdata.desc; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2624 | fp->cookie = 0; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2625 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2626 | trace_binder_transaction_node_to_ref(t, node, &rdata); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2627 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2628 | " node %d u%016llx -> ref %d desc %d\n", |
| 2629 | node->debug_id, (u64)node->ptr, |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2630 | rdata.debug_id, rdata.desc); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2631 | done: |
| 2632 | binder_put_node(node); |
| 2633 | return ret; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | static int binder_translate_handle(struct flat_binder_object *fp, |
| 2637 | struct binder_transaction *t, |
| 2638 | struct binder_thread *thread) |
| 2639 | { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2640 | struct binder_proc *proc = thread->proc; |
| 2641 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2642 | struct binder_node *node; |
| 2643 | struct binder_ref_data src_rdata; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2644 | int ret = 0; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2645 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2646 | node = binder_get_node_from_ref(proc, fp->handle, |
| 2647 | fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata); |
| 2648 | if (!node) { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2649 | binder_user_error("%d:%d got transaction with invalid handle, %d\n", |
| 2650 | proc->pid, thread->pid, fp->handle); |
| 2651 | return -EINVAL; |
| 2652 | } |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2653 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2654 | ret = -EPERM; |
| 2655 | goto done; |
| 2656 | } |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2657 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2658 | binder_node_lock(node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2659 | if (node->proc == target_proc) { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2660 | if (fp->hdr.type == BINDER_TYPE_HANDLE) |
| 2661 | fp->hdr.type = BINDER_TYPE_BINDER; |
| 2662 | else |
| 2663 | fp->hdr.type = BINDER_TYPE_WEAK_BINDER; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2664 | fp->binder = node->ptr; |
| 2665 | fp->cookie = node->cookie; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2666 | if (node->proc) |
| 2667 | binder_inner_proc_lock(node->proc); |
| 2668 | binder_inc_node_nilocked(node, |
| 2669 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2670 | 0, NULL); |
| 2671 | if (node->proc) |
| 2672 | binder_inner_proc_unlock(node->proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2673 | trace_binder_transaction_ref_to_node(t, node, &src_rdata); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2674 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2675 | " ref %d desc %d -> node %d u%016llx\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2676 | src_rdata.debug_id, src_rdata.desc, node->debug_id, |
| 2677 | (u64)node->ptr); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2678 | binder_node_unlock(node); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2679 | } else { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2680 | struct binder_ref_data dest_rdata; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2681 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2682 | binder_node_unlock(node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2683 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2684 | fp->hdr.type == BINDER_TYPE_HANDLE, |
| 2685 | NULL, &dest_rdata); |
| 2686 | if (ret) |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2687 | goto done; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2688 | |
| 2689 | fp->binder = 0; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2690 | fp->handle = dest_rdata.desc; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2691 | fp->cookie = 0; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2692 | trace_binder_transaction_ref_to_ref(t, node, &src_rdata, |
| 2693 | &dest_rdata); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2694 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2695 | " ref %d desc %d -> ref %d desc %d (node %d)\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2696 | src_rdata.debug_id, src_rdata.desc, |
| 2697 | dest_rdata.debug_id, dest_rdata.desc, |
| 2698 | node->debug_id); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2699 | } |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2700 | done: |
| 2701 | binder_put_node(node); |
| 2702 | return ret; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | static int binder_translate_fd(int fd, |
| 2706 | struct binder_transaction *t, |
| 2707 | struct binder_thread *thread, |
| 2708 | struct binder_transaction *in_reply_to) |
| 2709 | { |
| 2710 | struct binder_proc *proc = thread->proc; |
| 2711 | struct binder_proc *target_proc = t->to_proc; |
| 2712 | int target_fd; |
| 2713 | struct file *file; |
| 2714 | int ret; |
| 2715 | bool target_allows_fd; |
| 2716 | |
| 2717 | if (in_reply_to) |
| 2718 | target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS); |
| 2719 | else |
| 2720 | target_allows_fd = t->buffer->target_node->accept_fds; |
| 2721 | if (!target_allows_fd) { |
| 2722 | binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n", |
| 2723 | proc->pid, thread->pid, |
| 2724 | in_reply_to ? "reply" : "transaction", |
| 2725 | fd); |
| 2726 | ret = -EPERM; |
| 2727 | goto err_fd_not_accepted; |
| 2728 | } |
| 2729 | |
| 2730 | file = fget(fd); |
| 2731 | if (!file) { |
| 2732 | binder_user_error("%d:%d got transaction with invalid fd, %d\n", |
| 2733 | proc->pid, thread->pid, fd); |
| 2734 | ret = -EBADF; |
| 2735 | goto err_fget; |
| 2736 | } |
| 2737 | ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file); |
| 2738 | if (ret < 0) { |
| 2739 | ret = -EPERM; |
| 2740 | goto err_security; |
| 2741 | } |
| 2742 | |
| 2743 | target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC); |
| 2744 | if (target_fd < 0) { |
| 2745 | ret = -ENOMEM; |
| 2746 | goto err_get_unused_fd; |
| 2747 | } |
| 2748 | task_fd_install(target_proc, target_fd, file); |
| 2749 | trace_binder_transaction_fd(t, fd, target_fd); |
| 2750 | binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n", |
| 2751 | fd, target_fd); |
| 2752 | |
| 2753 | return target_fd; |
| 2754 | |
| 2755 | err_get_unused_fd: |
| 2756 | err_security: |
| 2757 | fput(file); |
| 2758 | err_fget: |
| 2759 | err_fd_not_accepted: |
| 2760 | return ret; |
| 2761 | } |
| 2762 | |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2763 | static int binder_translate_fd_array(struct binder_fd_array_object *fda, |
| 2764 | struct binder_buffer_object *parent, |
| 2765 | struct binder_transaction *t, |
| 2766 | struct binder_thread *thread, |
| 2767 | struct binder_transaction *in_reply_to) |
| 2768 | { |
| 2769 | binder_size_t fdi, fd_buf_size, num_installed_fds; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2770 | binder_size_t fda_offset; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2771 | int target_fd; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2772 | struct binder_proc *proc = thread->proc; |
| 2773 | struct binder_proc *target_proc = t->to_proc; |
| 2774 | |
| 2775 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2776 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2777 | binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n", |
| 2778 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2779 | return -EINVAL; |
| 2780 | } |
| 2781 | if (fd_buf_size > parent->length || |
| 2782 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2783 | /* No space for all file descriptors here. */ |
| 2784 | binder_user_error("%d:%d not enough space to store %lld fds in buffer\n", |
| 2785 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2786 | return -EINVAL; |
| 2787 | } |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2788 | /* |
| 2789 | * the source data for binder_buffer_object is visible |
| 2790 | * to user-space and the @buffer element is the user |
| 2791 | * pointer to the buffer_object containing the fd_array. |
| 2792 | * Convert the address to an offset relative to |
| 2793 | * the base of the transaction buffer. |
| 2794 | */ |
| 2795 | fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) + |
| 2796 | fda->parent_offset; |
| 2797 | if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) { |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2798 | binder_user_error("%d:%d parent offset not aligned correctly.\n", |
| 2799 | proc->pid, thread->pid); |
| 2800 | return -EINVAL; |
| 2801 | } |
| 2802 | for (fdi = 0; fdi < fda->num_fds; fdi++) { |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2803 | u32 fd; |
Todd Kjos | 0b90a82 | 2019-03-27 16:12:31 -0700 | [diff] [blame] | 2804 | |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2805 | binder_size_t offset = fda_offset + fdi * sizeof(fd); |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2806 | |
| 2807 | binder_alloc_copy_from_buffer(&target_proc->alloc, |
| 2808 | &fd, t->buffer, |
| 2809 | offset, sizeof(fd)); |
| 2810 | target_fd = binder_translate_fd(fd, t, thread, in_reply_to); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2811 | if (target_fd < 0) |
| 2812 | goto err_translate_fd_failed; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2813 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 2814 | t->buffer, offset, |
| 2815 | &target_fd, sizeof(fd)); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2816 | } |
| 2817 | return 0; |
| 2818 | |
| 2819 | err_translate_fd_failed: |
| 2820 | /* |
| 2821 | * Failed to allocate fd or security error, free fds |
| 2822 | * installed so far. |
| 2823 | */ |
| 2824 | num_installed_fds = fdi; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2825 | for (fdi = 0; fdi < num_installed_fds; fdi++) { |
| 2826 | u32 fd; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2827 | binder_size_t offset = fda_offset + fdi * sizeof(fd); |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2828 | binder_alloc_copy_from_buffer(&target_proc->alloc, |
| 2829 | &fd, t->buffer, |
| 2830 | offset, sizeof(fd)); |
| 2831 | task_close_fd(target_proc, fd); |
| 2832 | } |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2833 | return target_fd; |
| 2834 | } |
| 2835 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2836 | static int binder_fixup_parent(struct binder_transaction *t, |
| 2837 | struct binder_thread *thread, |
| 2838 | struct binder_buffer_object *bp, |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2839 | binder_size_t off_start_offset, |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2840 | binder_size_t num_valid, |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2841 | binder_size_t last_fixup_obj_off, |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2842 | binder_size_t last_fixup_min_off) |
| 2843 | { |
| 2844 | struct binder_buffer_object *parent; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2845 | struct binder_buffer *b = t->buffer; |
| 2846 | struct binder_proc *proc = thread->proc; |
| 2847 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2848 | struct binder_object object; |
| 2849 | binder_size_t buffer_offset; |
| 2850 | binder_size_t parent_offset; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2851 | |
| 2852 | if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT)) |
| 2853 | return 0; |
| 2854 | |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2855 | parent = binder_validate_ptr(target_proc, b, &object, bp->parent, |
| 2856 | off_start_offset, &parent_offset, |
| 2857 | num_valid); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2858 | if (!parent) { |
| 2859 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 2860 | proc->pid, thread->pid); |
| 2861 | return -EINVAL; |
| 2862 | } |
| 2863 | |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2864 | if (!binder_validate_fixup(target_proc, b, off_start_offset, |
| 2865 | parent_offset, bp->parent_offset, |
| 2866 | last_fixup_obj_off, |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2867 | last_fixup_min_off)) { |
| 2868 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 2869 | proc->pid, thread->pid); |
| 2870 | return -EINVAL; |
| 2871 | } |
| 2872 | |
| 2873 | if (parent->length < sizeof(binder_uintptr_t) || |
| 2874 | bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) { |
| 2875 | /* No space for a pointer here! */ |
| 2876 | binder_user_error("%d:%d got transaction with invalid parent offset\n", |
| 2877 | proc->pid, thread->pid); |
| 2878 | return -EINVAL; |
| 2879 | } |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2880 | buffer_offset = bp->parent_offset + |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2881 | (uintptr_t)parent->buffer - (uintptr_t)b->user_data; |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2882 | binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset, |
| 2883 | &bp->buffer, sizeof(bp->buffer)); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2884 | |
| 2885 | return 0; |
| 2886 | } |
| 2887 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2888 | /** |
| 2889 | * binder_proc_transaction() - sends a transaction to a process and wakes it up |
| 2890 | * @t: transaction to send |
| 2891 | * @proc: process to send the transaction to |
| 2892 | * @thread: thread in @proc to send the transaction to (may be NULL) |
| 2893 | * |
| 2894 | * This function queues a transaction to the specified process. It will try |
| 2895 | * to find a thread in the target process to handle the transaction and |
| 2896 | * wake it up. If no thread is found, the work is queued to the proc |
| 2897 | * waitqueue. |
| 2898 | * |
| 2899 | * If the @thread parameter is not NULL, the transaction is always queued |
| 2900 | * to the waitlist of that specific thread. |
| 2901 | * |
| 2902 | * Return: true if the transactions was successfully queued |
| 2903 | * false if the target process or thread is dead |
| 2904 | */ |
| 2905 | static bool binder_proc_transaction(struct binder_transaction *t, |
| 2906 | struct binder_proc *proc, |
| 2907 | struct binder_thread *thread) |
| 2908 | { |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2909 | struct binder_node *node = t->buffer->target_node; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2910 | struct binder_priority node_prio; |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2911 | bool oneway = !!(t->flags & TF_ONE_WAY); |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2912 | bool pending_async = false; |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2913 | |
| 2914 | BUG_ON(!node); |
| 2915 | binder_node_lock(node); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2916 | node_prio.prio = node->min_priority; |
| 2917 | node_prio.sched_policy = node->sched_policy; |
| 2918 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2919 | if (oneway) { |
| 2920 | BUG_ON(thread); |
| 2921 | if (node->has_async_transaction) { |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2922 | pending_async = true; |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2923 | } else { |
Gustavo A. R. Silva | e62dd6f | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 2924 | node->has_async_transaction = true; |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2925 | } |
| 2926 | } |
| 2927 | |
| 2928 | binder_inner_proc_lock(proc); |
| 2929 | |
| 2930 | if (proc->is_dead || (thread && thread->is_dead)) { |
| 2931 | binder_inner_proc_unlock(proc); |
| 2932 | binder_node_unlock(node); |
| 2933 | return false; |
| 2934 | } |
| 2935 | |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2936 | if (!thread && !pending_async) |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2937 | thread = binder_select_thread_ilocked(proc); |
| 2938 | |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2939 | if (thread) { |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 2940 | binder_transaction_priority(thread->task, t, node_prio, |
| 2941 | node->inherit_rt); |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2942 | binder_enqueue_thread_work_ilocked(thread, &t->work); |
| 2943 | } else if (!pending_async) { |
| 2944 | binder_enqueue_work_ilocked(&t->work, &proc->todo); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2945 | } else { |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2946 | binder_enqueue_work_ilocked(&t->work, &node->async_todo); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2947 | } |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2948 | |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2949 | if (!pending_async) |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2950 | binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */); |
| 2951 | |
| 2952 | binder_inner_proc_unlock(proc); |
| 2953 | binder_node_unlock(node); |
| 2954 | |
| 2955 | return true; |
| 2956 | } |
| 2957 | |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 2958 | /** |
| 2959 | * binder_get_node_refs_for_txn() - Get required refs on node for txn |
| 2960 | * @node: struct binder_node for which to get refs |
| 2961 | * @proc: returns @node->proc if valid |
| 2962 | * @error: if no @proc then returns BR_DEAD_REPLY |
| 2963 | * |
| 2964 | * User-space normally keeps the node alive when creating a transaction |
| 2965 | * since it has a reference to the target. The local strong ref keeps it |
| 2966 | * alive if the sending process dies before the target process processes |
| 2967 | * the transaction. If the source process is malicious or has a reference |
| 2968 | * counting bug, relying on the local strong ref can fail. |
| 2969 | * |
| 2970 | * Since user-space can cause the local strong ref to go away, we also take |
| 2971 | * a tmpref on the node to ensure it survives while we are constructing |
| 2972 | * the transaction. We also need a tmpref on the proc while we are |
| 2973 | * constructing the transaction, so we take that here as well. |
| 2974 | * |
| 2975 | * Return: The target_node with refs taken or NULL if no @node->proc is NULL. |
| 2976 | * Also sets @proc if valid. If the @node->proc is NULL indicating that the |
| 2977 | * target proc has died, @error is set to BR_DEAD_REPLY |
| 2978 | */ |
| 2979 | static struct binder_node *binder_get_node_refs_for_txn( |
| 2980 | struct binder_node *node, |
| 2981 | struct binder_proc **procp, |
| 2982 | uint32_t *error) |
| 2983 | { |
| 2984 | struct binder_node *target_node = NULL; |
| 2985 | |
| 2986 | binder_node_inner_lock(node); |
| 2987 | if (node->proc) { |
| 2988 | target_node = node; |
| 2989 | binder_inc_node_nilocked(node, 1, 0, NULL); |
| 2990 | binder_inc_node_tmpref_ilocked(node); |
Todd Kjos | dc734d7 | 2019-06-10 09:14:25 -0700 | [diff] [blame] | 2991 | atomic_inc(&node->proc->tmp_ref); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 2992 | *procp = node->proc; |
| 2993 | } else |
| 2994 | *error = BR_DEAD_REPLY; |
| 2995 | binder_node_inner_unlock(node); |
| 2996 | |
| 2997 | return target_node; |
| 2998 | } |
| 2999 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3000 | static void binder_transaction(struct binder_proc *proc, |
| 3001 | struct binder_thread *thread, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3002 | struct binder_transaction_data *tr, int reply, |
| 3003 | binder_size_t extra_buffers_size) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3004 | { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3005 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3006 | struct binder_transaction *t; |
| 3007 | struct binder_work *tcomplete; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3008 | binder_size_t buffer_offset = 0; |
| 3009 | binder_size_t off_start_offset, off_end_offset; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3010 | binder_size_t off_min; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3011 | binder_size_t sg_buf_offset, sg_buf_end_offset; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3012 | struct binder_proc *target_proc = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3013 | struct binder_thread *target_thread = NULL; |
| 3014 | struct binder_node *target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3015 | struct binder_transaction *in_reply_to = NULL; |
| 3016 | struct binder_transaction_log_entry *e; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3017 | uint32_t return_error = 0; |
| 3018 | uint32_t return_error_param = 0; |
| 3019 | uint32_t return_error_line = 0; |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3020 | binder_size_t last_fixup_obj_off = 0; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3021 | binder_size_t last_fixup_min_off = 0; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 3022 | struct binder_context *context = proc->context; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3023 | int t_debug_id = atomic_inc_return(&binder_last_id); |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3024 | char *secctx = NULL; |
| 3025 | u32 secctx_sz = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3026 | |
| 3027 | e = binder_transaction_log_add(&binder_transaction_log); |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3028 | e->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3029 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); |
| 3030 | e->from_proc = proc->pid; |
| 3031 | e->from_thread = thread->pid; |
| 3032 | e->target_handle = tr->target.handle; |
| 3033 | e->data_size = tr->data_size; |
| 3034 | e->offsets_size = tr->offsets_size; |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 3035 | e->context_name = proc->context->name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3036 | |
| 3037 | if (reply) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3038 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3039 | in_reply_to = thread->transaction_stack; |
| 3040 | if (in_reply_to == NULL) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3041 | binder_inner_proc_unlock(proc); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3042 | 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] | 3043 | proc->pid, thread->pid); |
| 3044 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3045 | return_error_param = -EPROTO; |
| 3046 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3047 | goto err_empty_call_stack; |
| 3048 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3049 | if (in_reply_to->to_thread != thread) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3050 | spin_lock(&in_reply_to->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3051 | 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] | 3052 | proc->pid, thread->pid, in_reply_to->debug_id, |
| 3053 | in_reply_to->to_proc ? |
| 3054 | in_reply_to->to_proc->pid : 0, |
| 3055 | in_reply_to->to_thread ? |
| 3056 | in_reply_to->to_thread->pid : 0); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3057 | spin_unlock(&in_reply_to->lock); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3058 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3059 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3060 | return_error_param = -EPROTO; |
| 3061 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3062 | in_reply_to = NULL; |
| 3063 | goto err_bad_call_stack; |
| 3064 | } |
| 3065 | thread->transaction_stack = in_reply_to->to_parent; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3066 | binder_inner_proc_unlock(proc); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3067 | 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] | 3068 | if (target_thread == NULL) { |
| 3069 | return_error = BR_DEAD_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3070 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3071 | goto err_dead_binder; |
| 3072 | } |
| 3073 | if (target_thread->transaction_stack != in_reply_to) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3074 | 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] | 3075 | proc->pid, thread->pid, |
| 3076 | target_thread->transaction_stack ? |
| 3077 | target_thread->transaction_stack->debug_id : 0, |
| 3078 | in_reply_to->debug_id); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3079 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3080 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3081 | return_error_param = -EPROTO; |
| 3082 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3083 | in_reply_to = NULL; |
| 3084 | target_thread = NULL; |
| 3085 | goto err_dead_binder; |
| 3086 | } |
| 3087 | target_proc = target_thread->proc; |
Todd Kjos | dc734d7 | 2019-06-10 09:14:25 -0700 | [diff] [blame] | 3088 | atomic_inc(&target_proc->tmp_ref); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3089 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3090 | } else { |
| 3091 | if (tr->target.handle) { |
| 3092 | struct binder_ref *ref; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3093 | |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3094 | /* |
| 3095 | * There must already be a strong ref |
| 3096 | * on this node. If so, do a strong |
| 3097 | * increment on the node to ensure it |
| 3098 | * stays alive until the transaction is |
| 3099 | * done. |
| 3100 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3101 | binder_proc_lock(proc); |
| 3102 | ref = binder_get_ref_olocked(proc, tr->target.handle, |
| 3103 | true); |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3104 | if (ref) { |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3105 | target_node = binder_get_node_refs_for_txn( |
| 3106 | ref->node, &target_proc, |
| 3107 | &return_error); |
| 3108 | } else { |
| 3109 | binder_user_error("%d:%d got transaction to invalid handle\n", |
| 3110 | proc->pid, thread->pid); |
| 3111 | return_error = BR_FAILED_REPLY; |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3112 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3113 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3114 | } else { |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3115 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 3116 | target_node = context->binder_context_mgr_node; |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3117 | if (target_node) |
| 3118 | target_node = binder_get_node_refs_for_txn( |
| 3119 | target_node, &target_proc, |
| 3120 | &return_error); |
| 3121 | else |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3122 | return_error = BR_DEAD_REPLY; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3123 | mutex_unlock(&context->context_mgr_node_lock); |
Martijn Coenen | c4048b2 | 2018-03-28 11:14:50 +0200 | [diff] [blame] | 3124 | if (target_node && target_proc == proc) { |
| 3125 | binder_user_error("%d:%d got transaction to context manager from process owning it\n", |
| 3126 | proc->pid, thread->pid); |
| 3127 | return_error = BR_FAILED_REPLY; |
| 3128 | return_error_param = -EINVAL; |
| 3129 | return_error_line = __LINE__; |
| 3130 | goto err_invalid_target_handle; |
| 3131 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3132 | } |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3133 | if (!target_node) { |
| 3134 | /* |
| 3135 | * return_error is set above |
| 3136 | */ |
| 3137 | return_error_param = -EINVAL; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3138 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3139 | goto err_dead_binder; |
| 3140 | } |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3141 | e->to_node = target_node->debug_id; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 3142 | if (security_binder_transaction(proc->tsk, |
| 3143 | target_proc->tsk) < 0) { |
| 3144 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3145 | return_error_param = -EPERM; |
| 3146 | return_error_line = __LINE__; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 3147 | goto err_invalid_target_handle; |
| 3148 | } |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3149 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3150 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { |
| 3151 | struct binder_transaction *tmp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3152 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3153 | tmp = thread->transaction_stack; |
| 3154 | if (tmp->to_thread != thread) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3155 | spin_lock(&tmp->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3156 | 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] | 3157 | proc->pid, thread->pid, tmp->debug_id, |
| 3158 | tmp->to_proc ? tmp->to_proc->pid : 0, |
| 3159 | tmp->to_thread ? |
| 3160 | tmp->to_thread->pid : 0); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3161 | spin_unlock(&tmp->lock); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3162 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3163 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3164 | return_error_param = -EPROTO; |
| 3165 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3166 | goto err_bad_call_stack; |
| 3167 | } |
| 3168 | while (tmp) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3169 | struct binder_thread *from; |
| 3170 | |
| 3171 | spin_lock(&tmp->lock); |
| 3172 | from = tmp->from; |
| 3173 | if (from && from->proc == target_proc) { |
| 3174 | atomic_inc(&from->tmp_ref); |
| 3175 | target_thread = from; |
| 3176 | spin_unlock(&tmp->lock); |
| 3177 | break; |
| 3178 | } |
| 3179 | spin_unlock(&tmp->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3180 | tmp = tmp->from_parent; |
| 3181 | } |
| 3182 | } |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3183 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3184 | } |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3185 | if (target_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3186 | e->to_thread = target_thread->pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3187 | e->to_proc = target_proc->pid; |
| 3188 | |
| 3189 | /* TODO: reuse incoming transaction for reply */ |
| 3190 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 3191 | if (t == NULL) { |
| 3192 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3193 | return_error_param = -ENOMEM; |
| 3194 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3195 | goto err_alloc_t_failed; |
| 3196 | } |
| 3197 | binder_stats_created(BINDER_STAT_TRANSACTION); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3198 | spin_lock_init(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3199 | |
| 3200 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); |
| 3201 | if (tcomplete == NULL) { |
| 3202 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3203 | return_error_param = -ENOMEM; |
| 3204 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3205 | goto err_alloc_tcomplete_failed; |
| 3206 | } |
| 3207 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3208 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3209 | t->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3210 | |
| 3211 | if (reply) |
| 3212 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3213 | "%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] | 3214 | proc->pid, thread->pid, t->debug_id, |
| 3215 | target_proc->pid, target_thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3216 | (u64)tr->data.ptr.buffer, |
| 3217 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3218 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3219 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3220 | else |
| 3221 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3222 | "%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] | 3223 | proc->pid, thread->pid, t->debug_id, |
| 3224 | target_proc->pid, target_node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3225 | (u64)tr->data.ptr.buffer, |
| 3226 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3227 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3228 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3229 | |
| 3230 | if (!reply && !(tr->flags & TF_ONE_WAY)) |
| 3231 | t->from = thread; |
| 3232 | else |
| 3233 | t->from = NULL; |
Tair Rzayev | 57bab7c | 2014-05-31 22:43:34 +0300 | [diff] [blame] | 3234 | t->sender_euid = task_euid(proc->tsk); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3235 | t->to_proc = target_proc; |
| 3236 | t->to_thread = target_thread; |
| 3237 | t->code = tr->code; |
| 3238 | t->flags = tr->flags; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 3239 | if (!(t->flags & TF_ONE_WAY) && |
| 3240 | binder_supported_policy(current->policy)) { |
| 3241 | /* Inherit supported policies for synchronous transactions */ |
| 3242 | t->priority.sched_policy = current->policy; |
| 3243 | t->priority.prio = current->normal_prio; |
| 3244 | } else { |
| 3245 | /* Otherwise, fall back to the default priority */ |
| 3246 | t->priority = target_proc->default_priority; |
| 3247 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3248 | |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3249 | if (target_node && target_node->txn_security_ctx) { |
| 3250 | u32 secid; |
Todd Kjos | f65c15f | 2019-04-24 12:31:18 -0700 | [diff] [blame] | 3251 | size_t added_size; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3252 | |
| 3253 | security_task_getsecid(proc->tsk, &secid); |
| 3254 | ret = security_secid_to_secctx(secid, &secctx, &secctx_sz); |
| 3255 | if (ret) { |
| 3256 | return_error = BR_FAILED_REPLY; |
| 3257 | return_error_param = ret; |
| 3258 | return_error_line = __LINE__; |
| 3259 | goto err_get_secctx_failed; |
| 3260 | } |
Todd Kjos | f65c15f | 2019-04-24 12:31:18 -0700 | [diff] [blame] | 3261 | added_size = ALIGN(secctx_sz, sizeof(u64)); |
| 3262 | extra_buffers_size += added_size; |
| 3263 | if (extra_buffers_size < added_size) { |
| 3264 | /* integer overflow of extra_buffers_size */ |
| 3265 | return_error = BR_FAILED_REPLY; |
| 3266 | return_error_param = EINVAL; |
| 3267 | return_error_line = __LINE__; |
| 3268 | goto err_bad_extra_size; |
| 3269 | } |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3270 | } |
| 3271 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3272 | trace_binder_transaction(reply, t, target_node); |
| 3273 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3274 | t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3275 | tr->offsets_size, extra_buffers_size, |
| 3276 | !reply && (t->flags & TF_ONE_WAY)); |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3277 | if (IS_ERR(t->buffer)) { |
| 3278 | /* |
| 3279 | * -ESRCH indicates VMA cleared. The target is dying. |
| 3280 | */ |
| 3281 | return_error_param = PTR_ERR(t->buffer); |
| 3282 | return_error = return_error_param == -ESRCH ? |
| 3283 | BR_DEAD_REPLY : BR_FAILED_REPLY; |
| 3284 | return_error_line = __LINE__; |
| 3285 | t->buffer = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3286 | goto err_binder_alloc_buf_failed; |
| 3287 | } |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3288 | if (secctx) { |
| 3289 | size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) + |
| 3290 | ALIGN(tr->offsets_size, sizeof(void *)) + |
| 3291 | ALIGN(extra_buffers_size, sizeof(void *)) - |
| 3292 | ALIGN(secctx_sz, sizeof(u64)); |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3293 | |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3294 | t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3295 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3296 | t->buffer, buf_offset, |
| 3297 | secctx, secctx_sz); |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3298 | security_release_secctx(secctx, secctx_sz); |
| 3299 | secctx = NULL; |
| 3300 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3301 | t->buffer->debug_id = t->debug_id; |
| 3302 | t->buffer->transaction = t; |
| 3303 | t->buffer->target_node = target_node; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3304 | trace_binder_transaction_alloc_buf(t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3305 | |
Todd Kjos | d504949 | 2019-02-08 10:35:14 -0800 | [diff] [blame] | 3306 | if (binder_alloc_copy_user_to_buffer( |
| 3307 | &target_proc->alloc, |
| 3308 | t->buffer, 0, |
| 3309 | (const void __user *) |
| 3310 | (uintptr_t)tr->data.ptr.buffer, |
| 3311 | tr->data_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3312 | binder_user_error("%d:%d got transaction with invalid data ptr\n", |
| 3313 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3314 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3315 | return_error_param = -EFAULT; |
| 3316 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3317 | goto err_copy_data_failed; |
| 3318 | } |
Todd Kjos | d504949 | 2019-02-08 10:35:14 -0800 | [diff] [blame] | 3319 | if (binder_alloc_copy_user_to_buffer( |
| 3320 | &target_proc->alloc, |
| 3321 | t->buffer, |
| 3322 | ALIGN(tr->data_size, sizeof(void *)), |
| 3323 | (const void __user *) |
| 3324 | (uintptr_t)tr->data.ptr.offsets, |
| 3325 | tr->offsets_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3326 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3327 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3328 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3329 | return_error_param = -EFAULT; |
| 3330 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3331 | goto err_copy_data_failed; |
| 3332 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3333 | if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) { |
| 3334 | binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n", |
| 3335 | proc->pid, thread->pid, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3336 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3337 | return_error_param = -EINVAL; |
| 3338 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3339 | goto err_bad_offset; |
| 3340 | } |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3341 | if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) { |
| 3342 | binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n", |
| 3343 | proc->pid, thread->pid, |
Amit Pundir | 44cbb18 | 2017-02-01 12:53:45 +0530 | [diff] [blame] | 3344 | (u64)extra_buffers_size); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3345 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3346 | return_error_param = -EINVAL; |
| 3347 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3348 | goto err_bad_offset; |
| 3349 | } |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3350 | off_start_offset = ALIGN(tr->data_size, sizeof(void *)); |
| 3351 | buffer_offset = off_start_offset; |
| 3352 | off_end_offset = off_start_offset + tr->offsets_size; |
| 3353 | sg_buf_offset = ALIGN(off_end_offset, sizeof(void *)); |
Martijn Coenen | 76d4c94 | 2019-07-09 13:09:23 +0200 | [diff] [blame] | 3354 | sg_buf_end_offset = sg_buf_offset + extra_buffers_size - |
| 3355 | ALIGN(secctx_sz, sizeof(u64)); |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3356 | off_min = 0; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3357 | for (buffer_offset = off_start_offset; buffer_offset < off_end_offset; |
| 3358 | buffer_offset += sizeof(binder_size_t)) { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3359 | struct binder_object_header *hdr; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3360 | size_t object_size; |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3361 | struct binder_object object; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3362 | binder_size_t object_offset; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3363 | |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3364 | binder_alloc_copy_from_buffer(&target_proc->alloc, |
| 3365 | &object_offset, |
| 3366 | t->buffer, |
| 3367 | buffer_offset, |
| 3368 | sizeof(object_offset)); |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3369 | object_size = binder_get_object(target_proc, t->buffer, |
| 3370 | object_offset, &object); |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3371 | if (object_size == 0 || object_offset < off_min) { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3372 | binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n", |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3373 | proc->pid, thread->pid, |
| 3374 | (u64)object_offset, |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3375 | (u64)off_min, |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3376 | (u64)t->buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3377 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3378 | return_error_param = -EINVAL; |
| 3379 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3380 | goto err_bad_offset; |
| 3381 | } |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3382 | |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3383 | hdr = &object.hdr; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3384 | off_min = object_offset + object_size; |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3385 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3386 | case BINDER_TYPE_BINDER: |
| 3387 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3388 | struct flat_binder_object *fp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3389 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3390 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3391 | ret = binder_translate_binder(fp, t, thread); |
| 3392 | if (ret < 0) { |
Christian Engelmayer | 7d42043 | 2014-05-07 21:44:53 +0200 | [diff] [blame] | 3393 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3394 | return_error_param = ret; |
| 3395 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3396 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3397 | } |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3398 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3399 | t->buffer, object_offset, |
| 3400 | fp, sizeof(*fp)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3401 | } break; |
| 3402 | case BINDER_TYPE_HANDLE: |
| 3403 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3404 | struct flat_binder_object *fp; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 3405 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3406 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3407 | ret = binder_translate_handle(fp, t, thread); |
| 3408 | if (ret < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3409 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3410 | return_error_param = ret; |
| 3411 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3412 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3413 | } |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3414 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3415 | t->buffer, object_offset, |
| 3416 | fp, sizeof(*fp)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3417 | } break; |
| 3418 | |
| 3419 | case BINDER_TYPE_FD: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3420 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3421 | int target_fd = binder_translate_fd(fp->fd, t, thread, |
| 3422 | in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3423 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3424 | if (target_fd < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3425 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3426 | return_error_param = target_fd; |
| 3427 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3428 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3429 | } |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3430 | fp->pad_binder = 0; |
| 3431 | fp->fd = target_fd; |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3432 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3433 | t->buffer, object_offset, |
| 3434 | fp, sizeof(*fp)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3435 | } break; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3436 | case BINDER_TYPE_FDA: { |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3437 | struct binder_object ptr_object; |
| 3438 | binder_size_t parent_offset; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3439 | struct binder_fd_array_object *fda = |
| 3440 | to_binder_fd_array_object(hdr); |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3441 | size_t num_valid = (buffer_offset - off_start_offset) * |
| 3442 | sizeof(binder_size_t); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3443 | struct binder_buffer_object *parent = |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3444 | binder_validate_ptr(target_proc, t->buffer, |
| 3445 | &ptr_object, fda->parent, |
| 3446 | off_start_offset, |
| 3447 | &parent_offset, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3448 | num_valid); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3449 | if (!parent) { |
| 3450 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 3451 | proc->pid, thread->pid); |
| 3452 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3453 | return_error_param = -EINVAL; |
| 3454 | return_error_line = __LINE__; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3455 | goto err_bad_parent; |
| 3456 | } |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3457 | if (!binder_validate_fixup(target_proc, t->buffer, |
| 3458 | off_start_offset, |
| 3459 | parent_offset, |
| 3460 | fda->parent_offset, |
| 3461 | last_fixup_obj_off, |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3462 | last_fixup_min_off)) { |
| 3463 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 3464 | proc->pid, thread->pid); |
| 3465 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3466 | return_error_param = -EINVAL; |
| 3467 | return_error_line = __LINE__; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3468 | goto err_bad_parent; |
| 3469 | } |
| 3470 | ret = binder_translate_fd_array(fda, parent, t, thread, |
| 3471 | in_reply_to); |
| 3472 | if (ret < 0) { |
| 3473 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3474 | return_error_param = ret; |
| 3475 | return_error_line = __LINE__; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3476 | goto err_translate_failed; |
| 3477 | } |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3478 | last_fixup_obj_off = parent_offset; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3479 | last_fixup_min_off = |
| 3480 | fda->parent_offset + sizeof(u32) * fda->num_fds; |
| 3481 | } break; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3482 | case BINDER_TYPE_PTR: { |
| 3483 | struct binder_buffer_object *bp = |
| 3484 | to_binder_buffer_object(hdr); |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3485 | size_t buf_left = sg_buf_end_offset - sg_buf_offset; |
| 3486 | size_t num_valid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3487 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3488 | if (bp->length > buf_left) { |
| 3489 | binder_user_error("%d:%d got transaction with too large buffer\n", |
| 3490 | proc->pid, thread->pid); |
| 3491 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3492 | return_error_param = -EINVAL; |
| 3493 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3494 | goto err_bad_offset; |
| 3495 | } |
Todd Kjos | d504949 | 2019-02-08 10:35:14 -0800 | [diff] [blame] | 3496 | if (binder_alloc_copy_user_to_buffer( |
| 3497 | &target_proc->alloc, |
| 3498 | t->buffer, |
| 3499 | sg_buf_offset, |
| 3500 | (const void __user *) |
| 3501 | (uintptr_t)bp->buffer, |
| 3502 | bp->length)) { |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3503 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3504 | proc->pid, thread->pid); |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3505 | return_error_param = -EFAULT; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3506 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3507 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3508 | goto err_copy_data_failed; |
| 3509 | } |
| 3510 | /* Fixup buffer pointer to target proc address space */ |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3511 | bp->buffer = (uintptr_t) |
| 3512 | t->buffer->user_data + sg_buf_offset; |
| 3513 | sg_buf_offset += ALIGN(bp->length, sizeof(u64)); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3514 | |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3515 | num_valid = (buffer_offset - off_start_offset) * |
| 3516 | sizeof(binder_size_t); |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3517 | ret = binder_fixup_parent(t, thread, bp, |
| 3518 | off_start_offset, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3519 | num_valid, |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3520 | last_fixup_obj_off, |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3521 | last_fixup_min_off); |
| 3522 | if (ret < 0) { |
| 3523 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3524 | return_error_param = ret; |
| 3525 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3526 | goto err_translate_failed; |
| 3527 | } |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3528 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3529 | t->buffer, object_offset, |
| 3530 | bp, sizeof(*bp)); |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3531 | last_fixup_obj_off = object_offset; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3532 | last_fixup_min_off = 0; |
| 3533 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3534 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 3535 | 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] | 3536 | proc->pid, thread->pid, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3537 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3538 | return_error_param = -EINVAL; |
| 3539 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3540 | goto err_bad_object_type; |
| 3541 | } |
| 3542 | } |
Todd Kjos | 8dedb0c | 2017-05-09 08:31:32 -0700 | [diff] [blame] | 3543 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3544 | t->work.type = BINDER_WORK_TRANSACTION; |
Todd Kjos | 8dedb0c | 2017-05-09 08:31:32 -0700 | [diff] [blame] | 3545 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3546 | if (reply) { |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3547 | binder_enqueue_thread_work(thread, tcomplete); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3548 | binder_inner_proc_lock(target_proc); |
| 3549 | if (target_thread->is_dead) { |
| 3550 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3551 | goto err_dead_proc_or_thread; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3552 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3553 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3554 | binder_pop_transaction_ilocked(target_thread, in_reply_to); |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3555 | binder_enqueue_thread_work_ilocked(target_thread, &t->work); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3556 | binder_inner_proc_unlock(target_proc); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3557 | wake_up_interruptible_sync(&target_thread->wait); |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3558 | binder_restore_priority(current, in_reply_to->saved_priority); |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 3559 | binder_free_transaction(in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3560 | } else if (!(t->flags & TF_ONE_WAY)) { |
| 3561 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3562 | binder_inner_proc_lock(proc); |
Martijn Coenen | dac2e9c | 2017-11-13 09:55:21 +0100 | [diff] [blame] | 3563 | /* |
| 3564 | * Defer the TRANSACTION_COMPLETE, so we don't return to |
| 3565 | * userspace immediately; this allows the target process to |
| 3566 | * immediately start processing this transaction, reducing |
| 3567 | * latency. We will then return the TRANSACTION_COMPLETE when |
| 3568 | * the target replies (or there is an error). |
| 3569 | */ |
| 3570 | binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3571 | t->need_reply = 1; |
| 3572 | t->from_parent = thread->transaction_stack; |
| 3573 | thread->transaction_stack = t; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3574 | binder_inner_proc_unlock(proc); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3575 | if (!binder_proc_transaction(t, target_proc, target_thread)) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3576 | binder_inner_proc_lock(proc); |
| 3577 | binder_pop_transaction_ilocked(thread, t); |
| 3578 | binder_inner_proc_unlock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3579 | goto err_dead_proc_or_thread; |
| 3580 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3581 | } else { |
| 3582 | BUG_ON(target_node == NULL); |
| 3583 | BUG_ON(t->buffer->async_transaction != 1); |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3584 | binder_enqueue_thread_work(thread, tcomplete); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3585 | if (!binder_proc_transaction(t, target_proc, NULL)) |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3586 | goto err_dead_proc_or_thread; |
Riley Andrews | b596881 | 2015-09-01 12:42:07 -0700 | [diff] [blame] | 3587 | } |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3588 | if (target_thread) |
| 3589 | binder_thread_dec_tmpref(target_thread); |
| 3590 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3591 | if (target_node) |
| 3592 | binder_dec_node_tmpref(target_node); |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3593 | /* |
| 3594 | * write barrier to synchronize with initialization |
| 3595 | * of log entry |
| 3596 | */ |
| 3597 | smp_wmb(); |
| 3598 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3599 | return; |
| 3600 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3601 | err_dead_proc_or_thread: |
| 3602 | return_error = BR_DEAD_REPLY; |
| 3603 | return_error_line = __LINE__; |
Xu YiPing | 86578a0 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3604 | binder_dequeue_work(proc, tcomplete); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3605 | err_translate_failed: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3606 | err_bad_object_type: |
| 3607 | err_bad_offset: |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3608 | err_bad_parent: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3609 | err_copy_data_failed: |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3610 | trace_binder_transaction_failed_buffer_release(t->buffer); |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3611 | binder_transaction_buffer_release(target_proc, t->buffer, |
| 3612 | buffer_offset, true); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3613 | if (target_node) |
| 3614 | binder_dec_node_tmpref(target_node); |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3615 | target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3616 | t->buffer->transaction = NULL; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3617 | binder_alloc_free_buf(&target_proc->alloc, t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3618 | err_binder_alloc_buf_failed: |
Todd Kjos | f65c15f | 2019-04-24 12:31:18 -0700 | [diff] [blame] | 3619 | err_bad_extra_size: |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3620 | if (secctx) |
| 3621 | security_release_secctx(secctx, secctx_sz); |
| 3622 | err_get_secctx_failed: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3623 | kfree(tcomplete); |
| 3624 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3625 | err_alloc_tcomplete_failed: |
| 3626 | kfree(t); |
| 3627 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 3628 | err_alloc_t_failed: |
| 3629 | err_bad_call_stack: |
| 3630 | err_empty_call_stack: |
| 3631 | err_dead_binder: |
| 3632 | err_invalid_target_handle: |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3633 | if (target_thread) |
| 3634 | binder_thread_dec_tmpref(target_thread); |
| 3635 | if (target_proc) |
| 3636 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3637 | if (target_node) { |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3638 | binder_dec_node(target_node, 1, 0); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3639 | binder_dec_node_tmpref(target_node); |
| 3640 | } |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3641 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3642 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3643 | "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n", |
| 3644 | proc->pid, thread->pid, return_error, return_error_param, |
| 3645 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3646 | return_error_line); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3647 | |
| 3648 | { |
| 3649 | struct binder_transaction_log_entry *fe; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3650 | |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3651 | e->return_error = return_error; |
| 3652 | e->return_error_param = return_error_param; |
| 3653 | e->return_error_line = return_error_line; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3654 | fe = binder_transaction_log_add(&binder_transaction_log_failed); |
| 3655 | *fe = *e; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3656 | /* |
| 3657 | * write barrier to synchronize with initialization |
| 3658 | * of log entry |
| 3659 | */ |
| 3660 | smp_wmb(); |
| 3661 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
| 3662 | WRITE_ONCE(fe->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3663 | } |
| 3664 | |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3665 | BUG_ON(thread->return_error.cmd != BR_OK); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3666 | if (in_reply_to) { |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3667 | binder_restore_priority(current, in_reply_to->saved_priority); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3668 | thread->return_error.cmd = BR_TRANSACTION_COMPLETE; |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3669 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3670 | binder_send_failed_reply(in_reply_to, return_error); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3671 | } else { |
| 3672 | thread->return_error.cmd = return_error; |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3673 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3674 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3675 | } |
| 3676 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3677 | static int binder_thread_write(struct binder_proc *proc, |
| 3678 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3679 | binder_uintptr_t binder_buffer, size_t size, |
| 3680 | binder_size_t *consumed) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3681 | { |
| 3682 | uint32_t cmd; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 3683 | struct binder_context *context = proc->context; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3684 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3685 | void __user *ptr = buffer + *consumed; |
| 3686 | void __user *end = buffer + size; |
| 3687 | |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3688 | while (ptr < end && thread->return_error.cmd == BR_OK) { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3689 | int ret; |
| 3690 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3691 | if (get_user(cmd, (uint32_t __user *)ptr)) |
| 3692 | return -EFAULT; |
| 3693 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3694 | trace_binder_command(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3695 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 3696 | atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]); |
| 3697 | atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]); |
| 3698 | atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3699 | } |
| 3700 | switch (cmd) { |
| 3701 | case BC_INCREFS: |
| 3702 | case BC_ACQUIRE: |
| 3703 | case BC_RELEASE: |
| 3704 | case BC_DECREFS: { |
| 3705 | uint32_t target; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3706 | const char *debug_string; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3707 | bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE; |
| 3708 | bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE; |
| 3709 | struct binder_ref_data rdata; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3710 | |
| 3711 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3712 | return -EFAULT; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3713 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3714 | ptr += sizeof(uint32_t); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3715 | ret = -1; |
| 3716 | if (increment && !target) { |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3717 | struct binder_node *ctx_mgr_node; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3718 | mutex_lock(&context->context_mgr_node_lock); |
| 3719 | ctx_mgr_node = context->binder_context_mgr_node; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3720 | if (ctx_mgr_node) |
| 3721 | ret = binder_inc_ref_for_node( |
| 3722 | proc, ctx_mgr_node, |
| 3723 | strong, NULL, &rdata); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3724 | mutex_unlock(&context->context_mgr_node_lock); |
| 3725 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3726 | if (ret) |
| 3727 | ret = binder_update_ref_for_handle( |
| 3728 | proc, target, increment, strong, |
| 3729 | &rdata); |
| 3730 | if (!ret && rdata.desc != target) { |
| 3731 | binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n", |
| 3732 | proc->pid, thread->pid, |
| 3733 | target, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3734 | } |
| 3735 | switch (cmd) { |
| 3736 | case BC_INCREFS: |
| 3737 | debug_string = "IncRefs"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3738 | break; |
| 3739 | case BC_ACQUIRE: |
| 3740 | debug_string = "Acquire"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3741 | break; |
| 3742 | case BC_RELEASE: |
| 3743 | debug_string = "Release"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3744 | break; |
| 3745 | case BC_DECREFS: |
| 3746 | default: |
| 3747 | debug_string = "DecRefs"; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3748 | break; |
| 3749 | } |
| 3750 | if (ret) { |
| 3751 | binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n", |
| 3752 | proc->pid, thread->pid, debug_string, |
| 3753 | strong, target, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3754 | break; |
| 3755 | } |
| 3756 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3757 | "%d:%d %s ref %d desc %d s %d w %d\n", |
| 3758 | proc->pid, thread->pid, debug_string, |
| 3759 | rdata.debug_id, rdata.desc, rdata.strong, |
| 3760 | rdata.weak); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3761 | break; |
| 3762 | } |
| 3763 | case BC_INCREFS_DONE: |
| 3764 | case BC_ACQUIRE_DONE: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3765 | binder_uintptr_t node_ptr; |
| 3766 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3767 | struct binder_node *node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3768 | bool free_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3769 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3770 | if (get_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3771 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3772 | ptr += sizeof(binder_uintptr_t); |
| 3773 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3774 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3775 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3776 | node = binder_get_node(proc, node_ptr); |
| 3777 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3778 | binder_user_error("%d:%d %s u%016llx no match\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3779 | proc->pid, thread->pid, |
| 3780 | cmd == BC_INCREFS_DONE ? |
| 3781 | "BC_INCREFS_DONE" : |
| 3782 | "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3783 | (u64)node_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3784 | break; |
| 3785 | } |
| 3786 | if (cookie != node->cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3787 | 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] | 3788 | proc->pid, thread->pid, |
| 3789 | cmd == BC_INCREFS_DONE ? |
| 3790 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3791 | (u64)node_ptr, node->debug_id, |
| 3792 | (u64)cookie, (u64)node->cookie); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3793 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3794 | break; |
| 3795 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3796 | binder_node_inner_lock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3797 | if (cmd == BC_ACQUIRE_DONE) { |
| 3798 | if (node->pending_strong_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3799 | 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] | 3800 | proc->pid, thread->pid, |
| 3801 | node->debug_id); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3802 | binder_node_inner_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3803 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3804 | break; |
| 3805 | } |
| 3806 | node->pending_strong_ref = 0; |
| 3807 | } else { |
| 3808 | if (node->pending_weak_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3809 | 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] | 3810 | proc->pid, thread->pid, |
| 3811 | node->debug_id); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3812 | binder_node_inner_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3813 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3814 | break; |
| 3815 | } |
| 3816 | node->pending_weak_ref = 0; |
| 3817 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3818 | free_node = binder_dec_node_nilocked(node, |
| 3819 | cmd == BC_ACQUIRE_DONE, 0); |
| 3820 | WARN_ON(free_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3821 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3822 | "%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] | 3823 | proc->pid, thread->pid, |
| 3824 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3825 | node->debug_id, node->local_strong_refs, |
| 3826 | node->local_weak_refs, node->tmp_refs); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3827 | binder_node_inner_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3828 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3829 | break; |
| 3830 | } |
| 3831 | case BC_ATTEMPT_ACQUIRE: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3832 | pr_err("BC_ATTEMPT_ACQUIRE not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3833 | return -EINVAL; |
| 3834 | case BC_ACQUIRE_RESULT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3835 | pr_err("BC_ACQUIRE_RESULT not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3836 | return -EINVAL; |
| 3837 | |
| 3838 | case BC_FREE_BUFFER: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3839 | binder_uintptr_t data_ptr; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3840 | struct binder_buffer *buffer; |
| 3841 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3842 | if (get_user(data_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3843 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3844 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3845 | |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 3846 | buffer = binder_alloc_prepare_to_free(&proc->alloc, |
| 3847 | data_ptr); |
Todd Kjos | d29b73e | 2018-11-06 15:55:32 -0800 | [diff] [blame] | 3848 | if (IS_ERR_OR_NULL(buffer)) { |
| 3849 | if (PTR_ERR(buffer) == -EPERM) { |
| 3850 | binder_user_error( |
| 3851 | "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n", |
| 3852 | proc->pid, thread->pid, |
| 3853 | (u64)data_ptr); |
| 3854 | } else { |
| 3855 | binder_user_error( |
| 3856 | "%d:%d BC_FREE_BUFFER u%016llx no match\n", |
| 3857 | proc->pid, thread->pid, |
| 3858 | (u64)data_ptr); |
| 3859 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3860 | break; |
| 3861 | } |
| 3862 | binder_debug(BINDER_DEBUG_FREE_BUFFER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3863 | "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n", |
| 3864 | proc->pid, thread->pid, (u64)data_ptr, |
| 3865 | buffer->debug_id, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3866 | buffer->transaction ? "active" : "finished"); |
| 3867 | |
Todd Kjos | dc734d7 | 2019-06-10 09:14:25 -0700 | [diff] [blame] | 3868 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3869 | if (buffer->transaction) { |
| 3870 | buffer->transaction->buffer = NULL; |
| 3871 | buffer->transaction = NULL; |
| 3872 | } |
Todd Kjos | dc734d7 | 2019-06-10 09:14:25 -0700 | [diff] [blame] | 3873 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3874 | if (buffer->async_transaction && buffer->target_node) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3875 | struct binder_node *buf_node; |
| 3876 | struct binder_work *w; |
| 3877 | |
| 3878 | buf_node = buffer->target_node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3879 | binder_node_inner_lock(buf_node); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3880 | BUG_ON(!buf_node->has_async_transaction); |
| 3881 | BUG_ON(buf_node->proc != proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3882 | w = binder_dequeue_work_head_ilocked( |
| 3883 | &buf_node->async_todo); |
Martijn Coenen | 4501c04 | 2017-08-10 13:56:16 +0200 | [diff] [blame] | 3884 | if (!w) { |
Gustavo A. R. Silva | e62dd6f | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 3885 | buf_node->has_async_transaction = false; |
Martijn Coenen | 4501c04 | 2017-08-10 13:56:16 +0200 | [diff] [blame] | 3886 | } else { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3887 | binder_enqueue_work_ilocked( |
Martijn Coenen | 4501c04 | 2017-08-10 13:56:16 +0200 | [diff] [blame] | 3888 | w, &proc->todo); |
| 3889 | binder_wakeup_proc_ilocked(proc); |
| 3890 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3891 | binder_node_inner_unlock(buf_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3892 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3893 | trace_binder_transaction_buffer_release(buffer); |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3894 | binder_transaction_buffer_release(proc, buffer, 0, false); |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3895 | binder_alloc_free_buf(&proc->alloc, buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3896 | break; |
| 3897 | } |
| 3898 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3899 | case BC_TRANSACTION_SG: |
| 3900 | case BC_REPLY_SG: { |
| 3901 | struct binder_transaction_data_sg tr; |
| 3902 | |
| 3903 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3904 | return -EFAULT; |
| 3905 | ptr += sizeof(tr); |
| 3906 | binder_transaction(proc, thread, &tr.transaction_data, |
| 3907 | cmd == BC_REPLY_SG, tr.buffers_size); |
| 3908 | break; |
| 3909 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3910 | case BC_TRANSACTION: |
| 3911 | case BC_REPLY: { |
| 3912 | struct binder_transaction_data tr; |
| 3913 | |
| 3914 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3915 | return -EFAULT; |
| 3916 | ptr += sizeof(tr); |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3917 | binder_transaction(proc, thread, &tr, |
| 3918 | cmd == BC_REPLY, 0); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3919 | break; |
| 3920 | } |
| 3921 | |
| 3922 | case BC_REGISTER_LOOPER: |
| 3923 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3924 | "%d:%d BC_REGISTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3925 | proc->pid, thread->pid); |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 3926 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3927 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { |
| 3928 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3929 | 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] | 3930 | proc->pid, thread->pid); |
| 3931 | } else if (proc->requested_threads == 0) { |
| 3932 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3933 | 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] | 3934 | proc->pid, thread->pid); |
| 3935 | } else { |
| 3936 | proc->requested_threads--; |
| 3937 | proc->requested_threads_started++; |
| 3938 | } |
| 3939 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 3940 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3941 | break; |
| 3942 | case BC_ENTER_LOOPER: |
| 3943 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3944 | "%d:%d BC_ENTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3945 | proc->pid, thread->pid); |
| 3946 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { |
| 3947 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3948 | 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] | 3949 | proc->pid, thread->pid); |
| 3950 | } |
| 3951 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; |
| 3952 | break; |
| 3953 | case BC_EXIT_LOOPER: |
| 3954 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3955 | "%d:%d BC_EXIT_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3956 | proc->pid, thread->pid); |
| 3957 | thread->looper |= BINDER_LOOPER_STATE_EXITED; |
| 3958 | break; |
| 3959 | |
| 3960 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 3961 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 3962 | uint32_t target; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3963 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3964 | struct binder_ref *ref; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3965 | struct binder_ref_death *death = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3966 | |
| 3967 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3968 | return -EFAULT; |
| 3969 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3970 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3971 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3972 | ptr += sizeof(binder_uintptr_t); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3973 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3974 | /* |
| 3975 | * Allocate memory for death notification |
| 3976 | * before taking lock |
| 3977 | */ |
| 3978 | death = kzalloc(sizeof(*death), GFP_KERNEL); |
| 3979 | if (death == NULL) { |
| 3980 | WARN_ON(thread->return_error.cmd != |
| 3981 | BR_OK); |
| 3982 | thread->return_error.cmd = BR_ERROR; |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3983 | binder_enqueue_thread_work( |
| 3984 | thread, |
| 3985 | &thread->return_error.work); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3986 | binder_debug( |
| 3987 | BINDER_DEBUG_FAILED_TRANSACTION, |
| 3988 | "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n", |
| 3989 | proc->pid, thread->pid); |
| 3990 | break; |
| 3991 | } |
| 3992 | } |
| 3993 | binder_proc_lock(proc); |
| 3994 | ref = binder_get_ref_olocked(proc, target, false); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3995 | if (ref == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3996 | binder_user_error("%d:%d %s invalid ref %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3997 | proc->pid, thread->pid, |
| 3998 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3999 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 4000 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 4001 | target); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 4002 | binder_proc_unlock(proc); |
| 4003 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4004 | break; |
| 4005 | } |
| 4006 | |
| 4007 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4008 | "%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] | 4009 | proc->pid, thread->pid, |
| 4010 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 4011 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 4012 | "BC_CLEAR_DEATH_NOTIFICATION", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 4013 | (u64)cookie, ref->data.debug_id, |
| 4014 | ref->data.desc, ref->data.strong, |
| 4015 | ref->data.weak, ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4016 | |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4017 | binder_node_lock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4018 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 4019 | if (ref->death) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4020 | 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] | 4021 | proc->pid, thread->pid); |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4022 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 4023 | binder_proc_unlock(proc); |
| 4024 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4025 | break; |
| 4026 | } |
| 4027 | binder_stats_created(BINDER_STAT_DEATH); |
| 4028 | INIT_LIST_HEAD(&death->work.entry); |
| 4029 | death->cookie = cookie; |
| 4030 | ref->death = death; |
| 4031 | if (ref->node->proc == NULL) { |
| 4032 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
Martijn Coenen | 3bdbe4c | 2017-08-10 13:50:52 +0200 | [diff] [blame] | 4033 | |
| 4034 | binder_inner_proc_lock(proc); |
| 4035 | binder_enqueue_work_ilocked( |
| 4036 | &ref->death->work, &proc->todo); |
| 4037 | binder_wakeup_proc_ilocked(proc); |
| 4038 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4039 | } |
| 4040 | } else { |
| 4041 | if (ref->death == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4042 | 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] | 4043 | proc->pid, thread->pid); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4044 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 4045 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4046 | break; |
| 4047 | } |
| 4048 | death = ref->death; |
| 4049 | if (death->cookie != cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4050 | 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] | 4051 | proc->pid, thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4052 | (u64)death->cookie, |
| 4053 | (u64)cookie); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4054 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 4055 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4056 | break; |
| 4057 | } |
| 4058 | ref->death = NULL; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4059 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4060 | if (list_empty(&death->work.entry)) { |
| 4061 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4062 | if (thread->looper & |
| 4063 | (BINDER_LOOPER_STATE_REGISTERED | |
| 4064 | BINDER_LOOPER_STATE_ENTERED)) |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 4065 | binder_enqueue_thread_work_ilocked( |
| 4066 | thread, |
| 4067 | &death->work); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4068 | else { |
| 4069 | binder_enqueue_work_ilocked( |
| 4070 | &death->work, |
| 4071 | &proc->todo); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4072 | binder_wakeup_proc_ilocked( |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 4073 | proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4074 | } |
| 4075 | } else { |
| 4076 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); |
| 4077 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; |
| 4078 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4079 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4080 | } |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4081 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 4082 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4083 | } break; |
| 4084 | case BC_DEAD_BINDER_DONE: { |
| 4085 | struct binder_work *w; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4086 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4087 | struct binder_ref_death *death = NULL; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4088 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4089 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4090 | return -EFAULT; |
| 4091 | |
Lisa Du | 7a64cd8 | 2016-02-17 09:32:52 +0800 | [diff] [blame] | 4092 | ptr += sizeof(cookie); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4093 | binder_inner_proc_lock(proc); |
| 4094 | list_for_each_entry(w, &proc->delivered_death, |
| 4095 | entry) { |
| 4096 | struct binder_ref_death *tmp_death = |
| 4097 | container_of(w, |
| 4098 | struct binder_ref_death, |
| 4099 | work); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4100 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4101 | if (tmp_death->cookie == cookie) { |
| 4102 | death = tmp_death; |
| 4103 | break; |
| 4104 | } |
| 4105 | } |
| 4106 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Todd Kjos | f540ce0 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 4107 | "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4108 | proc->pid, thread->pid, (u64)cookie, |
| 4109 | death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4110 | if (death == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4111 | binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n", |
| 4112 | proc->pid, thread->pid, (u64)cookie); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4113 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4114 | break; |
| 4115 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4116 | binder_dequeue_work_ilocked(&death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4117 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { |
| 4118 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4119 | if (thread->looper & |
| 4120 | (BINDER_LOOPER_STATE_REGISTERED | |
| 4121 | BINDER_LOOPER_STATE_ENTERED)) |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 4122 | binder_enqueue_thread_work_ilocked( |
| 4123 | thread, &death->work); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4124 | else { |
| 4125 | binder_enqueue_work_ilocked( |
| 4126 | &death->work, |
| 4127 | &proc->todo); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 4128 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4129 | } |
| 4130 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4131 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4132 | } break; |
| 4133 | |
| 4134 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4135 | pr_err("%d:%d unknown command %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4136 | proc->pid, thread->pid, cmd); |
| 4137 | return -EINVAL; |
| 4138 | } |
| 4139 | *consumed = ptr - buffer; |
| 4140 | } |
| 4141 | return 0; |
| 4142 | } |
| 4143 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 4144 | static void binder_stat_br(struct binder_proc *proc, |
| 4145 | struct binder_thread *thread, uint32_t cmd) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4146 | { |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4147 | trace_binder_return(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4148 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 4149 | atomic_inc(&binder_stats.br[_IOC_NR(cmd)]); |
| 4150 | atomic_inc(&proc->stats.br[_IOC_NR(cmd)]); |
| 4151 | atomic_inc(&thread->stats.br[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4152 | } |
| 4153 | } |
| 4154 | |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4155 | static int binder_put_node_cmd(struct binder_proc *proc, |
| 4156 | struct binder_thread *thread, |
| 4157 | void __user **ptrp, |
| 4158 | binder_uintptr_t node_ptr, |
| 4159 | binder_uintptr_t node_cookie, |
| 4160 | int node_debug_id, |
| 4161 | uint32_t cmd, const char *cmd_name) |
| 4162 | { |
| 4163 | void __user *ptr = *ptrp; |
| 4164 | |
| 4165 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4166 | return -EFAULT; |
| 4167 | ptr += sizeof(uint32_t); |
| 4168 | |
| 4169 | if (put_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
| 4170 | return -EFAULT; |
| 4171 | ptr += sizeof(binder_uintptr_t); |
| 4172 | |
| 4173 | if (put_user(node_cookie, (binder_uintptr_t __user *)ptr)) |
| 4174 | return -EFAULT; |
| 4175 | ptr += sizeof(binder_uintptr_t); |
| 4176 | |
| 4177 | binder_stat_br(proc, thread, cmd); |
| 4178 | binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n", |
| 4179 | proc->pid, thread->pid, cmd_name, node_debug_id, |
| 4180 | (u64)node_ptr, (u64)node_cookie); |
| 4181 | |
| 4182 | *ptrp = ptr; |
| 4183 | return 0; |
| 4184 | } |
| 4185 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4186 | static int binder_wait_for_work(struct binder_thread *thread, |
| 4187 | bool do_proc_work) |
| 4188 | { |
| 4189 | DEFINE_WAIT(wait); |
| 4190 | struct binder_proc *proc = thread->proc; |
| 4191 | int ret = 0; |
| 4192 | |
| 4193 | freezer_do_not_count(); |
| 4194 | binder_inner_proc_lock(proc); |
| 4195 | for (;;) { |
| 4196 | prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE); |
| 4197 | if (binder_has_work_ilocked(thread, do_proc_work)) |
| 4198 | break; |
| 4199 | if (do_proc_work) |
| 4200 | list_add(&thread->waiting_thread_node, |
| 4201 | &proc->waiting_threads); |
| 4202 | binder_inner_proc_unlock(proc); |
| 4203 | schedule(); |
| 4204 | binder_inner_proc_lock(proc); |
| 4205 | list_del_init(&thread->waiting_thread_node); |
| 4206 | if (signal_pending(current)) { |
| 4207 | ret = -ERESTARTSYS; |
| 4208 | break; |
| 4209 | } |
| 4210 | } |
| 4211 | finish_wait(&thread->wait, &wait); |
| 4212 | binder_inner_proc_unlock(proc); |
| 4213 | freezer_count(); |
| 4214 | |
| 4215 | return ret; |
| 4216 | } |
| 4217 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4218 | static int binder_thread_read(struct binder_proc *proc, |
| 4219 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4220 | binder_uintptr_t binder_buffer, size_t size, |
| 4221 | binder_size_t *consumed, int non_block) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4222 | { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4223 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4224 | void __user *ptr = buffer + *consumed; |
| 4225 | void __user *end = buffer + size; |
| 4226 | |
| 4227 | int ret = 0; |
| 4228 | int wait_for_proc_work; |
| 4229 | |
| 4230 | if (*consumed == 0) { |
| 4231 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) |
| 4232 | return -EFAULT; |
| 4233 | ptr += sizeof(uint32_t); |
| 4234 | } |
| 4235 | |
| 4236 | retry: |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4237 | binder_inner_proc_lock(proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4238 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4239 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4240 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4241 | thread->looper |= BINDER_LOOPER_STATE_WAITING; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4242 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4243 | trace_binder_wait_for_work(wait_for_proc_work, |
| 4244 | !!thread->transaction_stack, |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4245 | !binder_worklist_empty(proc, &thread->todo)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4246 | if (wait_for_proc_work) { |
| 4247 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4248 | BINDER_LOOPER_STATE_ENTERED))) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4249 | 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] | 4250 | proc->pid, thread->pid, thread->looper); |
| 4251 | wait_event_interruptible(binder_user_error_wait, |
| 4252 | binder_stop_on_user_error < 2); |
| 4253 | } |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 4254 | binder_restore_priority(current, proc->default_priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4255 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4256 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4257 | if (non_block) { |
| 4258 | if (!binder_has_work(thread, wait_for_proc_work)) |
| 4259 | ret = -EAGAIN; |
| 4260 | } else { |
| 4261 | ret = binder_wait_for_work(thread, wait_for_proc_work); |
| 4262 | } |
| 4263 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4264 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; |
| 4265 | |
| 4266 | if (ret) |
| 4267 | return ret; |
| 4268 | |
| 4269 | while (1) { |
| 4270 | uint32_t cmd; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4271 | struct binder_transaction_data_secctx tr; |
| 4272 | struct binder_transaction_data *trd = &tr.transaction_data; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4273 | struct binder_work *w = NULL; |
| 4274 | struct list_head *list = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4275 | struct binder_transaction *t = NULL; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4276 | struct binder_thread *t_from; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4277 | size_t trsize = sizeof(*trd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4278 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4279 | binder_inner_proc_lock(proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4280 | if (!binder_worklist_empty_ilocked(&thread->todo)) |
| 4281 | list = &thread->todo; |
| 4282 | else if (!binder_worklist_empty_ilocked(&proc->todo) && |
| 4283 | wait_for_proc_work) |
| 4284 | list = &proc->todo; |
| 4285 | else { |
| 4286 | binder_inner_proc_unlock(proc); |
| 4287 | |
Dmitry Voytik | 395262a | 2014-09-08 18:16:34 +0400 | [diff] [blame] | 4288 | /* no data added */ |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 4289 | if (ptr - buffer == 4 && !thread->looper_need_return) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4290 | goto retry; |
| 4291 | break; |
| 4292 | } |
| 4293 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4294 | if (end - ptr < sizeof(tr) + 4) { |
| 4295 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4296 | break; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4297 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4298 | w = binder_dequeue_work_head_ilocked(list); |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 4299 | if (binder_worklist_empty_ilocked(&thread->todo)) |
| 4300 | thread->process_todo = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4301 | |
| 4302 | switch (w->type) { |
| 4303 | case BINDER_WORK_TRANSACTION: { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4304 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4305 | t = container_of(w, struct binder_transaction, work); |
| 4306 | } break; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4307 | case BINDER_WORK_RETURN_ERROR: { |
| 4308 | struct binder_error *e = container_of( |
| 4309 | w, struct binder_error, work); |
| 4310 | |
| 4311 | WARN_ON(e->cmd == BR_OK); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4312 | binder_inner_proc_unlock(proc); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4313 | if (put_user(e->cmd, (uint32_t __user *)ptr)) |
| 4314 | return -EFAULT; |
宋金时 | e1b1a8b | 2018-05-10 02:05:03 +0000 | [diff] [blame] | 4315 | cmd = e->cmd; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4316 | e->cmd = BR_OK; |
| 4317 | ptr += sizeof(uint32_t); |
| 4318 | |
| 4319 | binder_stat_br(proc, thread, cmd); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4320 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4321 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4322 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4323 | cmd = BR_TRANSACTION_COMPLETE; |
| 4324 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4325 | return -EFAULT; |
| 4326 | ptr += sizeof(uint32_t); |
| 4327 | |
| 4328 | binder_stat_br(proc, thread, cmd); |
| 4329 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4330 | "%d:%d BR_TRANSACTION_COMPLETE\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4331 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4332 | kfree(w); |
| 4333 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4334 | } break; |
| 4335 | case BINDER_WORK_NODE: { |
| 4336 | struct binder_node *node = container_of(w, struct binder_node, work); |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4337 | int strong, weak; |
| 4338 | binder_uintptr_t node_ptr = node->ptr; |
| 4339 | binder_uintptr_t node_cookie = node->cookie; |
| 4340 | int node_debug_id = node->debug_id; |
| 4341 | int has_weak_ref; |
| 4342 | int has_strong_ref; |
| 4343 | void __user *orig_ptr = ptr; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4344 | |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4345 | BUG_ON(proc != node->proc); |
| 4346 | strong = node->internal_strong_refs || |
| 4347 | node->local_strong_refs; |
| 4348 | weak = !hlist_empty(&node->refs) || |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4349 | node->local_weak_refs || |
| 4350 | node->tmp_refs || strong; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4351 | has_strong_ref = node->has_strong_ref; |
| 4352 | has_weak_ref = node->has_weak_ref; |
| 4353 | |
| 4354 | if (weak && !has_weak_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4355 | node->has_weak_ref = 1; |
| 4356 | node->pending_weak_ref = 1; |
| 4357 | node->local_weak_refs++; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4358 | } |
| 4359 | if (strong && !has_strong_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4360 | node->has_strong_ref = 1; |
| 4361 | node->pending_strong_ref = 1; |
| 4362 | node->local_strong_refs++; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4363 | } |
| 4364 | if (!strong && has_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4365 | node->has_strong_ref = 0; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4366 | if (!weak && has_weak_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4367 | node->has_weak_ref = 0; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4368 | if (!weak && !strong) { |
| 4369 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4370 | "%d:%d node %d u%016llx c%016llx deleted\n", |
| 4371 | proc->pid, thread->pid, |
| 4372 | node_debug_id, |
| 4373 | (u64)node_ptr, |
| 4374 | (u64)node_cookie); |
| 4375 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4376 | binder_inner_proc_unlock(proc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4377 | binder_node_lock(node); |
| 4378 | /* |
| 4379 | * Acquire the node lock before freeing the |
| 4380 | * node to serialize with other threads that |
| 4381 | * may have been holding the node lock while |
| 4382 | * decrementing this node (avoids race where |
| 4383 | * this thread frees while the other thread |
| 4384 | * is unlocking the node after the final |
| 4385 | * decrement) |
| 4386 | */ |
| 4387 | binder_node_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4388 | binder_free_node(node); |
| 4389 | } else |
| 4390 | binder_inner_proc_unlock(proc); |
| 4391 | |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4392 | if (weak && !has_weak_ref) |
| 4393 | ret = binder_put_node_cmd( |
| 4394 | proc, thread, &ptr, node_ptr, |
| 4395 | node_cookie, node_debug_id, |
| 4396 | BR_INCREFS, "BR_INCREFS"); |
| 4397 | if (!ret && strong && !has_strong_ref) |
| 4398 | ret = binder_put_node_cmd( |
| 4399 | proc, thread, &ptr, node_ptr, |
| 4400 | node_cookie, node_debug_id, |
| 4401 | BR_ACQUIRE, "BR_ACQUIRE"); |
| 4402 | if (!ret && !strong && has_strong_ref) |
| 4403 | ret = binder_put_node_cmd( |
| 4404 | proc, thread, &ptr, node_ptr, |
| 4405 | node_cookie, node_debug_id, |
| 4406 | BR_RELEASE, "BR_RELEASE"); |
| 4407 | if (!ret && !weak && has_weak_ref) |
| 4408 | ret = binder_put_node_cmd( |
| 4409 | proc, thread, &ptr, node_ptr, |
| 4410 | node_cookie, node_debug_id, |
| 4411 | BR_DECREFS, "BR_DECREFS"); |
| 4412 | if (orig_ptr == ptr) |
| 4413 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4414 | "%d:%d node %d u%016llx c%016llx state unchanged\n", |
| 4415 | proc->pid, thread->pid, |
| 4416 | node_debug_id, |
| 4417 | (u64)node_ptr, |
| 4418 | (u64)node_cookie); |
| 4419 | if (ret) |
| 4420 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4421 | } break; |
| 4422 | case BINDER_WORK_DEAD_BINDER: |
| 4423 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4424 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4425 | struct binder_ref_death *death; |
| 4426 | uint32_t cmd; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4427 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4428 | |
| 4429 | death = container_of(w, struct binder_ref_death, work); |
| 4430 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) |
| 4431 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; |
| 4432 | else |
| 4433 | cmd = BR_DEAD_BINDER; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4434 | cookie = death->cookie; |
| 4435 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4436 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4437 | "%d:%d %s %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4438 | proc->pid, thread->pid, |
| 4439 | cmd == BR_DEAD_BINDER ? |
| 4440 | "BR_DEAD_BINDER" : |
| 4441 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4442 | (u64)cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4443 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4444 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4445 | kfree(death); |
| 4446 | binder_stats_deleted(BINDER_STAT_DEATH); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4447 | } else { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4448 | binder_enqueue_work_ilocked( |
| 4449 | w, &proc->delivered_death); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4450 | binder_inner_proc_unlock(proc); |
| 4451 | } |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4452 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4453 | return -EFAULT; |
| 4454 | ptr += sizeof(uint32_t); |
| 4455 | if (put_user(cookie, |
| 4456 | (binder_uintptr_t __user *)ptr)) |
| 4457 | return -EFAULT; |
| 4458 | ptr += sizeof(binder_uintptr_t); |
| 4459 | binder_stat_br(proc, thread, cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4460 | if (cmd == BR_DEAD_BINDER) |
| 4461 | goto done; /* DEAD_BINDER notifications can cause transactions */ |
| 4462 | } break; |
| 4463 | } |
| 4464 | |
| 4465 | if (!t) |
| 4466 | continue; |
| 4467 | |
| 4468 | BUG_ON(t->buffer == NULL); |
| 4469 | if (t->buffer->target_node) { |
| 4470 | struct binder_node *target_node = t->buffer->target_node; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4471 | struct binder_priority node_prio; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4472 | |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4473 | trd->target.ptr = target_node->ptr; |
| 4474 | trd->cookie = target_node->cookie; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4475 | node_prio.sched_policy = target_node->sched_policy; |
| 4476 | node_prio.prio = target_node->min_priority; |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 4477 | binder_transaction_priority(current, t, node_prio, |
| 4478 | target_node->inherit_rt); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4479 | cmd = BR_TRANSACTION; |
| 4480 | } else { |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4481 | trd->target.ptr = 0; |
| 4482 | trd->cookie = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4483 | cmd = BR_REPLY; |
| 4484 | } |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4485 | trd->code = t->code; |
| 4486 | trd->flags = t->flags; |
| 4487 | trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4488 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4489 | t_from = binder_get_txn_from(t); |
| 4490 | if (t_from) { |
| 4491 | struct task_struct *sender = t_from->proc->tsk; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4492 | |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4493 | trd->sender_pid = |
| 4494 | task_tgid_nr_ns(sender, |
| 4495 | task_active_pid_ns(current)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4496 | } else { |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4497 | trd->sender_pid = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4498 | } |
| 4499 | |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4500 | trd->data_size = t->buffer->data_size; |
| 4501 | trd->offsets_size = t->buffer->offsets_size; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 4502 | trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4503 | trd->data.ptr.offsets = trd->data.ptr.buffer + |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4504 | ALIGN(t->buffer->data_size, |
| 4505 | sizeof(void *)); |
| 4506 | |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4507 | tr.secctx = t->security_ctx; |
| 4508 | if (t->security_ctx) { |
| 4509 | cmd = BR_TRANSACTION_SEC_CTX; |
| 4510 | trsize = sizeof(tr); |
| 4511 | } |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4512 | if (put_user(cmd, (uint32_t __user *)ptr)) { |
| 4513 | if (t_from) |
| 4514 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4515 | |
| 4516 | binder_cleanup_transaction(t, "put_user failed", |
| 4517 | BR_FAILED_REPLY); |
| 4518 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4519 | return -EFAULT; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4520 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4521 | ptr += sizeof(uint32_t); |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4522 | if (copy_to_user(ptr, &tr, trsize)) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4523 | if (t_from) |
| 4524 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4525 | |
| 4526 | binder_cleanup_transaction(t, "copy_to_user failed", |
| 4527 | BR_FAILED_REPLY); |
| 4528 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4529 | return -EFAULT; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4530 | } |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4531 | ptr += trsize; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4532 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4533 | trace_binder_transaction_received(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4534 | binder_stat_br(proc, thread, cmd); |
| 4535 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4536 | "%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] | 4537 | proc->pid, thread->pid, |
| 4538 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4539 | (cmd == BR_TRANSACTION_SEC_CTX) ? |
| 4540 | "BR_TRANSACTION_SEC_CTX" : "BR_REPLY", |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4541 | t->debug_id, t_from ? t_from->proc->pid : 0, |
| 4542 | t_from ? t_from->pid : 0, cmd, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4543 | t->buffer->data_size, t->buffer->offsets_size, |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4544 | (u64)trd->data.ptr.buffer, |
| 4545 | (u64)trd->data.ptr.offsets); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4546 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4547 | if (t_from) |
| 4548 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4549 | t->buffer->allow_user_free = 1; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4550 | if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4551 | binder_inner_proc_lock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4552 | t->to_parent = thread->transaction_stack; |
| 4553 | t->to_thread = thread; |
| 4554 | thread->transaction_stack = t; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4555 | binder_inner_proc_unlock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4556 | } else { |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 4557 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4558 | } |
| 4559 | break; |
| 4560 | } |
| 4561 | |
| 4562 | done: |
| 4563 | |
| 4564 | *consumed = ptr - buffer; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4565 | binder_inner_proc_lock(proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4566 | if (proc->requested_threads == 0 && |
| 4567 | list_empty(&thread->proc->waiting_threads) && |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4568 | proc->requested_threads_started < proc->max_threads && |
| 4569 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4570 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ |
| 4571 | /*spawn a new thread if we leave this out */) { |
| 4572 | proc->requested_threads++; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4573 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4574 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4575 | "%d:%d BR_SPAWN_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4576 | proc->pid, thread->pid); |
| 4577 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) |
| 4578 | return -EFAULT; |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 4579 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4580 | } else |
| 4581 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4582 | return 0; |
| 4583 | } |
| 4584 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4585 | static void binder_release_work(struct binder_proc *proc, |
| 4586 | struct list_head *list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4587 | { |
| 4588 | struct binder_work *w; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4589 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4590 | while (1) { |
| 4591 | w = binder_dequeue_work_head(proc, list); |
| 4592 | if (!w) |
| 4593 | return; |
| 4594 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4595 | switch (w->type) { |
| 4596 | case BINDER_WORK_TRANSACTION: { |
| 4597 | struct binder_transaction *t; |
| 4598 | |
| 4599 | t = container_of(w, struct binder_transaction, work); |
Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4600 | |
| 4601 | binder_cleanup_transaction(t, "process died.", |
| 4602 | BR_DEAD_REPLY); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4603 | } break; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4604 | case BINDER_WORK_RETURN_ERROR: { |
| 4605 | struct binder_error *e = container_of( |
| 4606 | w, struct binder_error, work); |
| 4607 | |
| 4608 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 4609 | "undelivered TRANSACTION_ERROR: %u\n", |
| 4610 | e->cmd); |
| 4611 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4612 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4613 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4614 | "undelivered TRANSACTION_COMPLETE\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4615 | kfree(w); |
| 4616 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4617 | } break; |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4618 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4619 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4620 | struct binder_ref_death *death; |
| 4621 | |
| 4622 | death = container_of(w, struct binder_ref_death, work); |
| 4623 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4624 | "undelivered death notification, %016llx\n", |
| 4625 | (u64)death->cookie); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4626 | kfree(death); |
| 4627 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 4628 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4629 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4630 | pr_err("unexpected work type, %d, not freed\n", |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4631 | w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4632 | break; |
| 4633 | } |
| 4634 | } |
| 4635 | |
| 4636 | } |
| 4637 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4638 | static struct binder_thread *binder_get_thread_ilocked( |
| 4639 | struct binder_proc *proc, struct binder_thread *new_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4640 | { |
| 4641 | struct binder_thread *thread = NULL; |
| 4642 | struct rb_node *parent = NULL; |
| 4643 | struct rb_node **p = &proc->threads.rb_node; |
| 4644 | |
| 4645 | while (*p) { |
| 4646 | parent = *p; |
| 4647 | thread = rb_entry(parent, struct binder_thread, rb_node); |
| 4648 | |
| 4649 | if (current->pid < thread->pid) |
| 4650 | p = &(*p)->rb_left; |
| 4651 | else if (current->pid > thread->pid) |
| 4652 | p = &(*p)->rb_right; |
| 4653 | else |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4654 | return thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4655 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4656 | if (!new_thread) |
| 4657 | return NULL; |
| 4658 | thread = new_thread; |
| 4659 | binder_stats_created(BINDER_STAT_THREAD); |
| 4660 | thread->proc = proc; |
| 4661 | thread->pid = current->pid; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4662 | get_task_struct(current); |
| 4663 | thread->task = current; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4664 | atomic_set(&thread->tmp_ref, 0); |
| 4665 | init_waitqueue_head(&thread->wait); |
| 4666 | INIT_LIST_HEAD(&thread->todo); |
| 4667 | rb_link_node(&thread->rb_node, parent, p); |
| 4668 | rb_insert_color(&thread->rb_node, &proc->threads); |
| 4669 | thread->looper_need_return = true; |
| 4670 | thread->return_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4671 | thread->return_error.cmd = BR_OK; |
| 4672 | thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4673 | thread->reply_error.cmd = BR_OK; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4674 | INIT_LIST_HEAD(&new_thread->waiting_thread_node); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4675 | return thread; |
| 4676 | } |
| 4677 | |
| 4678 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) |
| 4679 | { |
| 4680 | struct binder_thread *thread; |
| 4681 | struct binder_thread *new_thread; |
| 4682 | |
| 4683 | binder_inner_proc_lock(proc); |
| 4684 | thread = binder_get_thread_ilocked(proc, NULL); |
| 4685 | binder_inner_proc_unlock(proc); |
| 4686 | if (!thread) { |
| 4687 | new_thread = kzalloc(sizeof(*thread), GFP_KERNEL); |
| 4688 | if (new_thread == NULL) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4689 | return NULL; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4690 | binder_inner_proc_lock(proc); |
| 4691 | thread = binder_get_thread_ilocked(proc, new_thread); |
| 4692 | binder_inner_proc_unlock(proc); |
| 4693 | if (thread != new_thread) |
| 4694 | kfree(new_thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4695 | } |
| 4696 | return thread; |
| 4697 | } |
| 4698 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4699 | static void binder_free_proc(struct binder_proc *proc) |
| 4700 | { |
| 4701 | BUG_ON(!list_empty(&proc->todo)); |
| 4702 | BUG_ON(!list_empty(&proc->delivered_death)); |
| 4703 | binder_alloc_deferred_release(&proc->alloc); |
| 4704 | put_task_struct(proc->tsk); |
| 4705 | binder_stats_deleted(BINDER_STAT_PROC); |
| 4706 | kfree(proc); |
| 4707 | } |
| 4708 | |
| 4709 | static void binder_free_thread(struct binder_thread *thread) |
| 4710 | { |
| 4711 | BUG_ON(!list_empty(&thread->todo)); |
| 4712 | binder_stats_deleted(BINDER_STAT_THREAD); |
| 4713 | binder_proc_dec_tmpref(thread->proc); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4714 | put_task_struct(thread->task); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4715 | kfree(thread); |
| 4716 | } |
| 4717 | |
| 4718 | static int binder_thread_release(struct binder_proc *proc, |
| 4719 | struct binder_thread *thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4720 | { |
| 4721 | struct binder_transaction *t; |
| 4722 | struct binder_transaction *send_reply = NULL; |
| 4723 | int active_transactions = 0; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4724 | struct binder_transaction *last_t = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4725 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4726 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4727 | /* |
| 4728 | * take a ref on the proc so it survives |
| 4729 | * after we remove this thread from proc->threads. |
| 4730 | * The corresponding dec is when we actually |
| 4731 | * free the thread in binder_free_thread() |
| 4732 | */ |
Todd Kjos | dc734d7 | 2019-06-10 09:14:25 -0700 | [diff] [blame] | 4733 | atomic_inc(&proc->tmp_ref); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4734 | /* |
| 4735 | * take a ref on this thread to ensure it |
| 4736 | * survives while we are releasing it |
| 4737 | */ |
| 4738 | atomic_inc(&thread->tmp_ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4739 | rb_erase(&thread->rb_node, &proc->threads); |
| 4740 | t = thread->transaction_stack; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4741 | if (t) { |
| 4742 | spin_lock(&t->lock); |
| 4743 | if (t->to_thread == thread) |
| 4744 | send_reply = t; |
| 4745 | } |
| 4746 | thread->is_dead = true; |
| 4747 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4748 | while (t) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4749 | last_t = t; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4750 | active_transactions++; |
| 4751 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4752 | "release %d:%d transaction %d %s, still active\n", |
| 4753 | proc->pid, thread->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4754 | t->debug_id, |
| 4755 | (t->to_thread == thread) ? "in" : "out"); |
| 4756 | |
| 4757 | if (t->to_thread == thread) { |
| 4758 | t->to_proc = NULL; |
| 4759 | t->to_thread = NULL; |
| 4760 | if (t->buffer) { |
| 4761 | t->buffer->transaction = NULL; |
| 4762 | t->buffer = NULL; |
| 4763 | } |
| 4764 | t = t->to_parent; |
| 4765 | } else if (t->from == thread) { |
| 4766 | t->from = NULL; |
| 4767 | t = t->from_parent; |
| 4768 | } else |
| 4769 | BUG(); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4770 | spin_unlock(&last_t->lock); |
| 4771 | if (t) |
| 4772 | spin_lock(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4773 | } |
Martijn Coenen | 550c01d | 2018-01-05 11:27:07 +0100 | [diff] [blame] | 4774 | |
| 4775 | /* |
| 4776 | * If this thread used poll, make sure we remove the waitqueue |
| 4777 | * from any epoll data structures holding it with POLLFREE. |
| 4778 | * waitqueue_active() is safe to use here because we're holding |
| 4779 | * the inner lock. |
| 4780 | */ |
| 4781 | if ((thread->looper & BINDER_LOOPER_STATE_POLL) && |
| 4782 | waitqueue_active(&thread->wait)) { |
| 4783 | wake_up_poll(&thread->wait, POLLHUP | POLLFREE); |
| 4784 | } |
| 4785 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4786 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4787 | |
Martijn Coenen | 72766d7 | 2018-02-16 09:47:15 +0100 | [diff] [blame] | 4788 | /* |
| 4789 | * This is needed to avoid races between wake_up_poll() above and |
| 4790 | * and ep_remove_waitqueue() called for other reasons (eg the epoll file |
| 4791 | * descriptor being closed); ep_remove_waitqueue() holds an RCU read |
| 4792 | * lock, so we can be sure it's done after calling synchronize_rcu(). |
| 4793 | */ |
| 4794 | if (thread->looper & BINDER_LOOPER_STATE_POLL) |
| 4795 | synchronize_rcu(); |
| 4796 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4797 | if (send_reply) |
| 4798 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4799 | binder_release_work(proc, &thread->todo); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4800 | binder_thread_dec_tmpref(thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4801 | return active_transactions; |
| 4802 | } |
| 4803 | |
| 4804 | static unsigned int binder_poll(struct file *filp, |
| 4805 | struct poll_table_struct *wait) |
| 4806 | { |
| 4807 | struct binder_proc *proc = filp->private_data; |
| 4808 | struct binder_thread *thread = NULL; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4809 | bool wait_for_proc_work; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4810 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4811 | thread = binder_get_thread(proc); |
Greg Kroah-Hartman | 6e463bb | 2018-02-28 17:17:14 +0100 | [diff] [blame] | 4812 | if (!thread) |
Eric Biggers | 4be5a28 | 2018-01-30 23:11:24 -0800 | [diff] [blame] | 4813 | return POLLERR; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4814 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4815 | binder_inner_proc_lock(thread->proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4816 | thread->looper |= BINDER_LOOPER_STATE_POLL; |
| 4817 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
| 4818 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4819 | binder_inner_proc_unlock(thread->proc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4820 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4821 | poll_wait(filp, &thread->wait, wait); |
| 4822 | |
Martijn Coenen | 4781093 | 2017-08-10 12:32:00 +0200 | [diff] [blame] | 4823 | if (binder_has_work(thread, wait_for_proc_work)) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4824 | return POLLIN; |
| 4825 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4826 | return 0; |
| 4827 | } |
| 4828 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4829 | static int binder_ioctl_write_read(struct file *filp, |
| 4830 | unsigned int cmd, unsigned long arg, |
| 4831 | struct binder_thread *thread) |
| 4832 | { |
| 4833 | int ret = 0; |
| 4834 | struct binder_proc *proc = filp->private_data; |
| 4835 | unsigned int size = _IOC_SIZE(cmd); |
| 4836 | void __user *ubuf = (void __user *)arg; |
| 4837 | struct binder_write_read bwr; |
| 4838 | |
| 4839 | if (size != sizeof(struct binder_write_read)) { |
| 4840 | ret = -EINVAL; |
| 4841 | goto out; |
| 4842 | } |
| 4843 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { |
| 4844 | ret = -EFAULT; |
| 4845 | goto out; |
| 4846 | } |
| 4847 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4848 | "%d:%d write %lld at %016llx, read %lld at %016llx\n", |
| 4849 | proc->pid, thread->pid, |
| 4850 | (u64)bwr.write_size, (u64)bwr.write_buffer, |
| 4851 | (u64)bwr.read_size, (u64)bwr.read_buffer); |
| 4852 | |
| 4853 | if (bwr.write_size > 0) { |
| 4854 | ret = binder_thread_write(proc, thread, |
| 4855 | bwr.write_buffer, |
| 4856 | bwr.write_size, |
| 4857 | &bwr.write_consumed); |
| 4858 | trace_binder_write_done(ret); |
| 4859 | if (ret < 0) { |
| 4860 | bwr.read_consumed = 0; |
| 4861 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4862 | ret = -EFAULT; |
| 4863 | goto out; |
| 4864 | } |
| 4865 | } |
| 4866 | if (bwr.read_size > 0) { |
| 4867 | ret = binder_thread_read(proc, thread, bwr.read_buffer, |
| 4868 | bwr.read_size, |
| 4869 | &bwr.read_consumed, |
| 4870 | filp->f_flags & O_NONBLOCK); |
| 4871 | trace_binder_read_done(ret); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4872 | binder_inner_proc_lock(proc); |
| 4873 | if (!binder_worklist_empty_ilocked(&proc->todo)) |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 4874 | binder_wakeup_proc_ilocked(proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4875 | binder_inner_proc_unlock(proc); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4876 | if (ret < 0) { |
| 4877 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4878 | ret = -EFAULT; |
| 4879 | goto out; |
| 4880 | } |
| 4881 | } |
| 4882 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4883 | "%d:%d wrote %lld of %lld, read return %lld of %lld\n", |
| 4884 | proc->pid, thread->pid, |
| 4885 | (u64)bwr.write_consumed, (u64)bwr.write_size, |
| 4886 | (u64)bwr.read_consumed, (u64)bwr.read_size); |
| 4887 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { |
| 4888 | ret = -EFAULT; |
| 4889 | goto out; |
| 4890 | } |
| 4891 | out: |
| 4892 | return ret; |
| 4893 | } |
| 4894 | |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4895 | static int binder_ioctl_set_ctx_mgr(struct file *filp, |
| 4896 | struct flat_binder_object *fbo) |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4897 | { |
| 4898 | int ret = 0; |
| 4899 | struct binder_proc *proc = filp->private_data; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4900 | struct binder_context *context = proc->context; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4901 | struct binder_node *new_node; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4902 | kuid_t curr_euid = current_euid(); |
| 4903 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4904 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4905 | if (context->binder_context_mgr_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4906 | pr_err("BINDER_SET_CONTEXT_MGR already set\n"); |
| 4907 | ret = -EBUSY; |
| 4908 | goto out; |
| 4909 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 4910 | ret = security_binder_set_context_mgr(proc->tsk); |
| 4911 | if (ret < 0) |
| 4912 | goto out; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4913 | if (uid_valid(context->binder_context_mgr_uid)) { |
| 4914 | if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4915 | pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n", |
| 4916 | from_kuid(&init_user_ns, curr_euid), |
| 4917 | from_kuid(&init_user_ns, |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4918 | context->binder_context_mgr_uid)); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4919 | ret = -EPERM; |
| 4920 | goto out; |
| 4921 | } |
| 4922 | } else { |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4923 | context->binder_context_mgr_uid = curr_euid; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4924 | } |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4925 | new_node = binder_new_node(proc, fbo); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4926 | if (!new_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4927 | ret = -ENOMEM; |
| 4928 | goto out; |
| 4929 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4930 | binder_node_lock(new_node); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4931 | new_node->local_weak_refs++; |
| 4932 | new_node->local_strong_refs++; |
| 4933 | new_node->has_strong_ref = 1; |
| 4934 | new_node->has_weak_ref = 1; |
| 4935 | context->binder_context_mgr_node = new_node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4936 | binder_node_unlock(new_node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4937 | binder_put_node(new_node); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4938 | out: |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4939 | mutex_unlock(&context->context_mgr_node_lock); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4940 | return ret; |
| 4941 | } |
| 4942 | |
Martijn Coenen | 1c57ba4 | 2018-08-25 13:50:56 -0700 | [diff] [blame] | 4943 | static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc, |
| 4944 | struct binder_node_info_for_ref *info) |
| 4945 | { |
| 4946 | struct binder_node *node; |
| 4947 | struct binder_context *context = proc->context; |
| 4948 | __u32 handle = info->handle; |
| 4949 | |
| 4950 | if (info->strong_count || info->weak_count || info->reserved1 || |
| 4951 | info->reserved2 || info->reserved3) { |
| 4952 | binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.", |
| 4953 | proc->pid); |
| 4954 | return -EINVAL; |
| 4955 | } |
| 4956 | |
| 4957 | /* This ioctl may only be used by the context manager */ |
| 4958 | mutex_lock(&context->context_mgr_node_lock); |
| 4959 | if (!context->binder_context_mgr_node || |
| 4960 | context->binder_context_mgr_node->proc != proc) { |
| 4961 | mutex_unlock(&context->context_mgr_node_lock); |
| 4962 | return -EPERM; |
| 4963 | } |
| 4964 | mutex_unlock(&context->context_mgr_node_lock); |
| 4965 | |
| 4966 | node = binder_get_node_from_ref(proc, handle, true, NULL); |
| 4967 | if (!node) |
| 4968 | return -EINVAL; |
| 4969 | |
| 4970 | info->strong_count = node->local_strong_refs + |
| 4971 | node->internal_strong_refs; |
| 4972 | info->weak_count = node->local_weak_refs; |
| 4973 | |
| 4974 | binder_put_node(node); |
| 4975 | |
| 4976 | return 0; |
| 4977 | } |
| 4978 | |
Colin Cross | 833babb3 | 2017-06-20 13:54:44 -0700 | [diff] [blame] | 4979 | static int binder_ioctl_get_node_debug_info(struct binder_proc *proc, |
| 4980 | struct binder_node_debug_info *info) { |
| 4981 | struct rb_node *n; |
| 4982 | binder_uintptr_t ptr = info->ptr; |
| 4983 | |
| 4984 | memset(info, 0, sizeof(*info)); |
| 4985 | |
| 4986 | binder_inner_proc_lock(proc); |
| 4987 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { |
| 4988 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 4989 | rb_node); |
| 4990 | if (node->ptr > ptr) { |
| 4991 | info->ptr = node->ptr; |
| 4992 | info->cookie = node->cookie; |
| 4993 | info->has_strong_ref = node->has_strong_ref; |
| 4994 | info->has_weak_ref = node->has_weak_ref; |
| 4995 | break; |
| 4996 | } |
| 4997 | } |
| 4998 | binder_inner_proc_unlock(proc); |
| 4999 | |
| 5000 | return 0; |
| 5001 | } |
| 5002 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5003 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 5004 | { |
| 5005 | int ret; |
| 5006 | struct binder_proc *proc = filp->private_data; |
| 5007 | struct binder_thread *thread; |
| 5008 | unsigned int size = _IOC_SIZE(cmd); |
| 5009 | void __user *ubuf = (void __user *)arg; |
| 5010 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5011 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", |
| 5012 | proc->pid, current->pid, cmd, arg);*/ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5013 | |
Sherry Yang | 435416b | 2017-06-22 14:37:45 -0700 | [diff] [blame] | 5014 | binder_selftest_alloc(&proc->alloc); |
| 5015 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5016 | trace_binder_ioctl(cmd, arg); |
| 5017 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5018 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 5019 | if (ret) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5020 | goto err_unlocked; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5021 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5022 | thread = binder_get_thread(proc); |
| 5023 | if (thread == NULL) { |
| 5024 | ret = -ENOMEM; |
| 5025 | goto err; |
| 5026 | } |
| 5027 | |
| 5028 | switch (cmd) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5029 | case BINDER_WRITE_READ: |
| 5030 | ret = binder_ioctl_write_read(filp, cmd, arg, thread); |
| 5031 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5032 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5033 | break; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 5034 | case BINDER_SET_MAX_THREADS: { |
| 5035 | int max_threads; |
| 5036 | |
| 5037 | if (copy_from_user(&max_threads, ubuf, |
| 5038 | sizeof(max_threads))) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5039 | ret = -EINVAL; |
| 5040 | goto err; |
| 5041 | } |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 5042 | binder_inner_proc_lock(proc); |
| 5043 | proc->max_threads = max_threads; |
| 5044 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5045 | break; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 5046 | } |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 5047 | case BINDER_SET_CONTEXT_MGR_EXT: { |
| 5048 | struct flat_binder_object fbo; |
| 5049 | |
| 5050 | if (copy_from_user(&fbo, ubuf, sizeof(fbo))) { |
| 5051 | ret = -EINVAL; |
| 5052 | goto err; |
| 5053 | } |
| 5054 | ret = binder_ioctl_set_ctx_mgr(filp, &fbo); |
| 5055 | if (ret) |
| 5056 | goto err; |
| 5057 | break; |
| 5058 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5059 | case BINDER_SET_CONTEXT_MGR: |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 5060 | ret = binder_ioctl_set_ctx_mgr(filp, NULL); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5061 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5062 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5063 | break; |
| 5064 | case BINDER_THREAD_EXIT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 5065 | binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5066 | proc->pid, thread->pid); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5067 | binder_thread_release(proc, thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5068 | thread = NULL; |
| 5069 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 5070 | case BINDER_VERSION: { |
| 5071 | struct binder_version __user *ver = ubuf; |
| 5072 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5073 | if (size != sizeof(struct binder_version)) { |
| 5074 | ret = -EINVAL; |
| 5075 | goto err; |
| 5076 | } |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 5077 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, |
| 5078 | &ver->protocol_version)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5079 | ret = -EINVAL; |
| 5080 | goto err; |
| 5081 | } |
| 5082 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 5083 | } |
Martijn Coenen | 1c57ba4 | 2018-08-25 13:50:56 -0700 | [diff] [blame] | 5084 | case BINDER_GET_NODE_INFO_FOR_REF: { |
| 5085 | struct binder_node_info_for_ref info; |
| 5086 | |
| 5087 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 5088 | ret = -EFAULT; |
| 5089 | goto err; |
| 5090 | } |
| 5091 | |
| 5092 | ret = binder_ioctl_get_node_info_for_ref(proc, &info); |
| 5093 | if (ret < 0) |
| 5094 | goto err; |
| 5095 | |
| 5096 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 5097 | ret = -EFAULT; |
| 5098 | goto err; |
| 5099 | } |
| 5100 | |
| 5101 | break; |
| 5102 | } |
Colin Cross | 833babb3 | 2017-06-20 13:54:44 -0700 | [diff] [blame] | 5103 | case BINDER_GET_NODE_DEBUG_INFO: { |
| 5104 | struct binder_node_debug_info info; |
| 5105 | |
| 5106 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 5107 | ret = -EFAULT; |
| 5108 | goto err; |
| 5109 | } |
| 5110 | |
| 5111 | ret = binder_ioctl_get_node_debug_info(proc, &info); |
| 5112 | if (ret < 0) |
| 5113 | goto err; |
| 5114 | |
| 5115 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 5116 | ret = -EFAULT; |
| 5117 | goto err; |
| 5118 | } |
| 5119 | break; |
| 5120 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5121 | default: |
| 5122 | ret = -EINVAL; |
| 5123 | goto err; |
| 5124 | } |
| 5125 | ret = 0; |
| 5126 | err: |
| 5127 | if (thread) |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 5128 | thread->looper_need_return = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5129 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 5130 | if (ret && ret != -ERESTARTSYS) |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 5131 | 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] | 5132 | err_unlocked: |
| 5133 | trace_binder_ioctl_done(ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5134 | return ret; |
| 5135 | } |
| 5136 | |
| 5137 | static void binder_vma_open(struct vm_area_struct *vma) |
| 5138 | { |
| 5139 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5140 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5141 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 5142 | "%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] | 5143 | proc->pid, vma->vm_start, vma->vm_end, |
| 5144 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 5145 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | static void binder_vma_close(struct vm_area_struct *vma) |
| 5149 | { |
| 5150 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5151 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5152 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 5153 | "%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] | 5154 | proc->pid, vma->vm_start, vma->vm_end, |
| 5155 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 5156 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5157 | binder_alloc_vma_close(&proc->alloc); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5158 | binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5159 | } |
| 5160 | |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 5161 | static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 5162 | { |
| 5163 | return VM_FAULT_SIGBUS; |
| 5164 | } |
| 5165 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 5166 | static const struct vm_operations_struct binder_vm_ops = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5167 | .open = binder_vma_open, |
| 5168 | .close = binder_vma_close, |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 5169 | .fault = binder_vm_fault, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5170 | }; |
| 5171 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5172 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) |
| 5173 | { |
| 5174 | int ret; |
| 5175 | struct binder_proc *proc = filp->private_data; |
| 5176 | const char *failure_string; |
| 5177 | |
| 5178 | if (proc->tsk != current->group_leader) |
| 5179 | return -EINVAL; |
| 5180 | |
| 5181 | if ((vma->vm_end - vma->vm_start) > SZ_4M) |
| 5182 | vma->vm_end = vma->vm_start + SZ_4M; |
| 5183 | |
| 5184 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 5185 | "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 5186 | __func__, proc->pid, vma->vm_start, vma->vm_end, |
| 5187 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 5188 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 5189 | |
| 5190 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { |
| 5191 | ret = -EPERM; |
| 5192 | failure_string = "bad vm_flags"; |
| 5193 | goto err_bad_arg; |
| 5194 | } |
Minchan Kim | 2cafd5b | 2018-05-07 23:15:37 +0900 | [diff] [blame] | 5195 | vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP; |
| 5196 | vma->vm_flags &= ~VM_MAYWRITE; |
| 5197 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5198 | vma->vm_ops = &binder_vm_ops; |
| 5199 | vma->vm_private_data = proc; |
| 5200 | |
| 5201 | ret = binder_alloc_mmap_handler(&proc->alloc, vma); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5202 | if (ret) |
| 5203 | return ret; |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5204 | mutex_lock(&proc->files_lock); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5205 | proc->files = get_files_struct(current); |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5206 | mutex_unlock(&proc->files_lock); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5207 | return 0; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5208 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5209 | err_bad_arg: |
Elad Wexler | 6b64640 | 2017-12-29 11:03:37 +0200 | [diff] [blame] | 5210 | pr_err("%s: %d %lx-%lx %s failed %d\n", __func__, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5211 | proc->pid, vma->vm_start, vma->vm_end, failure_string, ret); |
| 5212 | return ret; |
| 5213 | } |
| 5214 | |
| 5215 | static int binder_open(struct inode *nodp, struct file *filp) |
| 5216 | { |
| 5217 | struct binder_proc *proc; |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5218 | struct binder_device *binder_dev; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5219 | |
Elad Wexler | 6b64640 | 2017-12-29 11:03:37 +0200 | [diff] [blame] | 5220 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5221 | current->group_leader->pid, current->pid); |
| 5222 | |
| 5223 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 5224 | if (proc == NULL) |
| 5225 | return -ENOMEM; |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 5226 | spin_lock_init(&proc->inner_lock); |
| 5227 | spin_lock_init(&proc->outer_lock); |
Todd Kjos | dc734d7 | 2019-06-10 09:14:25 -0700 | [diff] [blame] | 5228 | atomic_set(&proc->tmp_ref, 0); |
Martijn Coenen | 872c26e | 2017-03-07 15:51:18 +0100 | [diff] [blame] | 5229 | get_task_struct(current->group_leader); |
| 5230 | proc->tsk = current->group_leader; |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5231 | mutex_init(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5232 | INIT_LIST_HEAD(&proc->todo); |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 5233 | if (binder_supported_policy(current->policy)) { |
| 5234 | proc->default_priority.sched_policy = current->policy; |
| 5235 | proc->default_priority.prio = current->normal_prio; |
| 5236 | } else { |
| 5237 | proc->default_priority.sched_policy = SCHED_NORMAL; |
| 5238 | proc->default_priority.prio = NICE_TO_PRIO(0); |
| 5239 | } |
| 5240 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5241 | binder_dev = container_of(filp->private_data, struct binder_device, |
| 5242 | miscdev); |
| 5243 | proc->context = &binder_dev->context; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5244 | binder_alloc_init(&proc->alloc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5245 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5246 | binder_stats_created(BINDER_STAT_PROC); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5247 | proc->pid = current->group_leader->pid; |
| 5248 | INIT_LIST_HEAD(&proc->delivered_death); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5249 | INIT_LIST_HEAD(&proc->waiting_threads); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5250 | filp->private_data = proc; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5251 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5252 | mutex_lock(&binder_procs_lock); |
| 5253 | hlist_add_head(&proc->proc_node, &binder_procs); |
| 5254 | mutex_unlock(&binder_procs_lock); |
| 5255 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5256 | if (binder_debugfs_dir_entry_proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5257 | char strbuf[11]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5258 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5259 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5260 | /* |
| 5261 | * proc debug entries are shared between contexts, so |
| 5262 | * this will fail if the process tries to open the driver |
| 5263 | * again with a different context. The priting code will |
| 5264 | * anyway print all contexts that a given PID has, so this |
| 5265 | * is not a problem. |
| 5266 | */ |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5267 | proc->debugfs_entry = debugfs_create_file(strbuf, 0444, |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5268 | binder_debugfs_dir_entry_proc, |
| 5269 | (void *)(unsigned long)proc->pid, |
| 5270 | &binder_proc_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5271 | } |
| 5272 | |
| 5273 | return 0; |
| 5274 | } |
| 5275 | |
| 5276 | static int binder_flush(struct file *filp, fl_owner_t id) |
| 5277 | { |
| 5278 | struct binder_proc *proc = filp->private_data; |
| 5279 | |
| 5280 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); |
| 5281 | |
| 5282 | return 0; |
| 5283 | } |
| 5284 | |
| 5285 | static void binder_deferred_flush(struct binder_proc *proc) |
| 5286 | { |
| 5287 | struct rb_node *n; |
| 5288 | int wake_count = 0; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5289 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5290 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5291 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 5292 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5293 | |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 5294 | thread->looper_need_return = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5295 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { |
| 5296 | wake_up_interruptible(&thread->wait); |
| 5297 | wake_count++; |
| 5298 | } |
| 5299 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5300 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5301 | |
| 5302 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 5303 | "binder_flush: %d woke %d threads\n", proc->pid, |
| 5304 | wake_count); |
| 5305 | } |
| 5306 | |
| 5307 | static int binder_release(struct inode *nodp, struct file *filp) |
| 5308 | { |
| 5309 | struct binder_proc *proc = filp->private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5310 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5311 | debugfs_remove(proc->debugfs_entry); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5312 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); |
| 5313 | |
| 5314 | return 0; |
| 5315 | } |
| 5316 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5317 | static int binder_node_release(struct binder_node *node, int refs) |
| 5318 | { |
| 5319 | struct binder_ref *ref; |
| 5320 | int death = 0; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5321 | struct binder_proc *proc = node->proc; |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5322 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5323 | binder_release_work(proc, &node->async_todo); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5324 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5325 | binder_node_lock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5326 | binder_inner_proc_lock(proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5327 | binder_dequeue_work_ilocked(&node->work); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5328 | /* |
| 5329 | * The caller must have taken a temporary ref on the node, |
| 5330 | */ |
| 5331 | BUG_ON(!node->tmp_refs); |
| 5332 | if (hlist_empty(&node->refs) && node->tmp_refs == 1) { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5333 | binder_inner_proc_unlock(proc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5334 | binder_node_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5335 | binder_free_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5336 | |
| 5337 | return refs; |
| 5338 | } |
| 5339 | |
| 5340 | node->proc = NULL; |
| 5341 | node->local_strong_refs = 0; |
| 5342 | node->local_weak_refs = 0; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5343 | binder_inner_proc_unlock(proc); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5344 | |
| 5345 | spin_lock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5346 | hlist_add_head(&node->dead_node, &binder_dead_nodes); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5347 | spin_unlock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5348 | |
| 5349 | hlist_for_each_entry(ref, &node->refs, node_entry) { |
| 5350 | refs++; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 5351 | /* |
| 5352 | * Need the node lock to synchronize |
| 5353 | * with new notification requests and the |
| 5354 | * inner lock to synchronize with queued |
| 5355 | * death notifications. |
| 5356 | */ |
| 5357 | binder_inner_proc_lock(ref->proc); |
| 5358 | if (!ref->death) { |
| 5359 | binder_inner_proc_unlock(ref->proc); |
Arve Hjønnevåg | e194fd8 | 2014-02-17 13:58:29 -0800 | [diff] [blame] | 5360 | continue; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 5361 | } |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5362 | |
| 5363 | death++; |
| 5364 | |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 5365 | BUG_ON(!list_empty(&ref->death->work.entry)); |
| 5366 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 5367 | binder_enqueue_work_ilocked(&ref->death->work, |
| 5368 | &ref->proc->todo); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 5369 | binder_wakeup_proc_ilocked(ref->proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5370 | binder_inner_proc_unlock(ref->proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5371 | } |
| 5372 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5373 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 5374 | "node %d now dead, refs %d, death %d\n", |
| 5375 | node->debug_id, refs, death); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5376 | binder_node_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5377 | binder_put_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5378 | |
| 5379 | return refs; |
| 5380 | } |
| 5381 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5382 | static void binder_deferred_release(struct binder_proc *proc) |
| 5383 | { |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 5384 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5385 | struct rb_node *n; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5386 | int threads, nodes, incoming_refs, outgoing_refs, active_transactions; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5387 | |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5388 | BUG_ON(proc->files); |
| 5389 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5390 | mutex_lock(&binder_procs_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5391 | hlist_del(&proc->proc_node); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5392 | mutex_unlock(&binder_procs_lock); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5393 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5394 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 5395 | if (context->binder_context_mgr_node && |
| 5396 | context->binder_context_mgr_node->proc == proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5397 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 5398 | "%s: %d context_mgr_node gone\n", |
| 5399 | __func__, proc->pid); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 5400 | context->binder_context_mgr_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5401 | } |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5402 | mutex_unlock(&context->context_mgr_node_lock); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5403 | binder_inner_proc_lock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5404 | /* |
| 5405 | * Make sure proc stays alive after we |
| 5406 | * remove all the threads |
| 5407 | */ |
Todd Kjos | dc734d7 | 2019-06-10 09:14:25 -0700 | [diff] [blame] | 5408 | atomic_inc(&proc->tmp_ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5409 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5410 | proc->is_dead = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5411 | threads = 0; |
| 5412 | active_transactions = 0; |
| 5413 | while ((n = rb_first(&proc->threads))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5414 | struct binder_thread *thread; |
| 5415 | |
| 5416 | thread = rb_entry(n, struct binder_thread, rb_node); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5417 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5418 | threads++; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5419 | active_transactions += binder_thread_release(proc, thread); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5420 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5421 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5422 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5423 | nodes = 0; |
| 5424 | incoming_refs = 0; |
| 5425 | while ((n = rb_first(&proc->nodes))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5426 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5427 | |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5428 | node = rb_entry(n, struct binder_node, rb_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5429 | nodes++; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5430 | /* |
| 5431 | * take a temporary ref on the node before |
| 5432 | * calling binder_node_release() which will either |
| 5433 | * kfree() the node or call binder_put_node() |
| 5434 | */ |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5435 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5436 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5437 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5438 | incoming_refs = binder_node_release(node, incoming_refs); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5439 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5440 | } |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5441 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5442 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5443 | outgoing_refs = 0; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5444 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5445 | while ((n = rb_first(&proc->refs_by_desc))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5446 | struct binder_ref *ref; |
| 5447 | |
| 5448 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5449 | outgoing_refs++; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5450 | binder_cleanup_ref_olocked(ref); |
| 5451 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5452 | binder_free_ref(ref); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5453 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5454 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5455 | binder_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5456 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5457 | binder_release_work(proc, &proc->todo); |
| 5458 | binder_release_work(proc, &proc->delivered_death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5459 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5460 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5461 | "%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] | 5462 | __func__, proc->pid, threads, nodes, incoming_refs, |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5463 | outgoing_refs, active_transactions); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5464 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5465 | binder_proc_dec_tmpref(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | static void binder_deferred_func(struct work_struct *work) |
| 5469 | { |
| 5470 | struct binder_proc *proc; |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5471 | struct files_struct *files; |
| 5472 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5473 | int defer; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5474 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5475 | do { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5476 | mutex_lock(&binder_deferred_lock); |
| 5477 | if (!hlist_empty(&binder_deferred_list)) { |
| 5478 | proc = hlist_entry(binder_deferred_list.first, |
| 5479 | struct binder_proc, deferred_work_node); |
| 5480 | hlist_del_init(&proc->deferred_work_node); |
| 5481 | defer = proc->deferred_work; |
| 5482 | proc->deferred_work = 0; |
| 5483 | } else { |
| 5484 | proc = NULL; |
| 5485 | defer = 0; |
| 5486 | } |
| 5487 | mutex_unlock(&binder_deferred_lock); |
| 5488 | |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5489 | files = NULL; |
| 5490 | if (defer & BINDER_DEFERRED_PUT_FILES) { |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5491 | mutex_lock(&proc->files_lock); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5492 | files = proc->files; |
| 5493 | if (files) |
| 5494 | proc->files = NULL; |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5495 | mutex_unlock(&proc->files_lock); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5496 | } |
| 5497 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5498 | if (defer & BINDER_DEFERRED_FLUSH) |
| 5499 | binder_deferred_flush(proc); |
| 5500 | |
| 5501 | if (defer & BINDER_DEFERRED_RELEASE) |
| 5502 | binder_deferred_release(proc); /* frees proc */ |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5503 | |
| 5504 | if (files) |
| 5505 | put_files_struct(files); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5506 | } while (proc); |
| 5507 | } |
| 5508 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); |
| 5509 | |
| 5510 | static void |
| 5511 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) |
| 5512 | { |
| 5513 | mutex_lock(&binder_deferred_lock); |
| 5514 | proc->deferred_work |= defer; |
| 5515 | if (hlist_unhashed(&proc->deferred_work_node)) { |
| 5516 | hlist_add_head(&proc->deferred_work_node, |
| 5517 | &binder_deferred_list); |
Bhaktipriya Shridhar | 1beba52 | 2016-08-13 22:16:24 +0530 | [diff] [blame] | 5518 | schedule_work(&binder_deferred_work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5519 | } |
| 5520 | mutex_unlock(&binder_deferred_lock); |
| 5521 | } |
| 5522 | |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5523 | static void print_binder_transaction_ilocked(struct seq_file *m, |
| 5524 | struct binder_proc *proc, |
| 5525 | const char *prefix, |
| 5526 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5527 | { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5528 | struct binder_proc *to_proc; |
| 5529 | struct binder_buffer *buffer = t->buffer; |
| 5530 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5531 | spin_lock(&t->lock); |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5532 | to_proc = t->to_proc; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5533 | seq_printf(m, |
Todd Kjos | f540ce0 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 5534 | "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %d:%d r%d", |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5535 | prefix, t->debug_id, t, |
| 5536 | t->from ? t->from->proc->pid : 0, |
| 5537 | t->from ? t->from->pid : 0, |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5538 | to_proc ? to_proc->pid : 0, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5539 | t->to_thread ? t->to_thread->pid : 0, |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 5540 | t->code, t->flags, t->priority.sched_policy, |
| 5541 | t->priority.prio, t->need_reply); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5542 | spin_unlock(&t->lock); |
| 5543 | |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5544 | if (proc != to_proc) { |
| 5545 | /* |
| 5546 | * Can only safely deref buffer if we are holding the |
| 5547 | * correct proc inner lock for this node |
| 5548 | */ |
| 5549 | seq_puts(m, "\n"); |
| 5550 | return; |
| 5551 | } |
| 5552 | |
| 5553 | if (buffer == NULL) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5554 | seq_puts(m, " buffer free\n"); |
| 5555 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5556 | } |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5557 | if (buffer->target_node) |
| 5558 | seq_printf(m, " node %d", buffer->target_node->debug_id); |
Todd Kjos | f540ce0 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 5559 | seq_printf(m, " size %zd:%zd data %pK\n", |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5560 | buffer->data_size, buffer->offsets_size, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 5561 | buffer->user_data); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5562 | } |
| 5563 | |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5564 | static void print_binder_work_ilocked(struct seq_file *m, |
| 5565 | struct binder_proc *proc, |
| 5566 | const char *prefix, |
| 5567 | const char *transaction_prefix, |
| 5568 | struct binder_work *w) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5569 | { |
| 5570 | struct binder_node *node; |
| 5571 | struct binder_transaction *t; |
| 5572 | |
| 5573 | switch (w->type) { |
| 5574 | case BINDER_WORK_TRANSACTION: |
| 5575 | t = container_of(w, struct binder_transaction, work); |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5576 | print_binder_transaction_ilocked( |
| 5577 | m, proc, transaction_prefix, t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5578 | break; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 5579 | case BINDER_WORK_RETURN_ERROR: { |
| 5580 | struct binder_error *e = container_of( |
| 5581 | w, struct binder_error, work); |
| 5582 | |
| 5583 | seq_printf(m, "%stransaction error: %u\n", |
| 5584 | prefix, e->cmd); |
| 5585 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5586 | case BINDER_WORK_TRANSACTION_COMPLETE: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5587 | seq_printf(m, "%stransaction complete\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5588 | break; |
| 5589 | case BINDER_WORK_NODE: |
| 5590 | node = container_of(w, struct binder_node, work); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5591 | seq_printf(m, "%snode work %d: u%016llx c%016llx\n", |
| 5592 | prefix, node->debug_id, |
| 5593 | (u64)node->ptr, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5594 | break; |
| 5595 | case BINDER_WORK_DEAD_BINDER: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5596 | seq_printf(m, "%shas dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5597 | break; |
| 5598 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5599 | seq_printf(m, "%shas cleared dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5600 | break; |
| 5601 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5602 | seq_printf(m, "%shas cleared death notification\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5603 | break; |
| 5604 | default: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5605 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5606 | break; |
| 5607 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5608 | } |
| 5609 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5610 | static void print_binder_thread_ilocked(struct seq_file *m, |
| 5611 | struct binder_thread *thread, |
| 5612 | int print_always) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5613 | { |
| 5614 | struct binder_transaction *t; |
| 5615 | struct binder_work *w; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5616 | size_t start_pos = m->count; |
| 5617 | size_t header_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5618 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5619 | 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] | 5620 | thread->pid, thread->looper, |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5621 | thread->looper_need_return, |
| 5622 | atomic_read(&thread->tmp_ref)); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5623 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5624 | t = thread->transaction_stack; |
| 5625 | while (t) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5626 | if (t->from == thread) { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5627 | print_binder_transaction_ilocked(m, thread->proc, |
| 5628 | " outgoing transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5629 | t = t->from_parent; |
| 5630 | } else if (t->to_thread == thread) { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5631 | print_binder_transaction_ilocked(m, thread->proc, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5632 | " incoming transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5633 | t = t->to_parent; |
| 5634 | } else { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5635 | print_binder_transaction_ilocked(m, thread->proc, |
| 5636 | " bad transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5637 | t = NULL; |
| 5638 | } |
| 5639 | } |
| 5640 | list_for_each_entry(w, &thread->todo, entry) { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5641 | print_binder_work_ilocked(m, thread->proc, " ", |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5642 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5643 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5644 | if (!print_always && m->count == header_pos) |
| 5645 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5646 | } |
| 5647 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5648 | static void print_binder_node_nilocked(struct seq_file *m, |
| 5649 | struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5650 | { |
| 5651 | struct binder_ref *ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5652 | struct binder_work *w; |
| 5653 | int count; |
| 5654 | |
| 5655 | count = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5656 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5657 | count++; |
| 5658 | |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 5659 | seq_printf(m, " node %d: u%016llx c%016llx pri %d:%d hs %d hw %d ls %d lw %d is %d iw %d tr %d", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5660 | node->debug_id, (u64)node->ptr, (u64)node->cookie, |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 5661 | node->sched_policy, node->min_priority, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5662 | node->has_strong_ref, node->has_weak_ref, |
| 5663 | node->local_strong_refs, node->local_weak_refs, |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5664 | node->internal_strong_refs, count, node->tmp_refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5665 | if (count) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5666 | seq_puts(m, " proc"); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5667 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5668 | seq_printf(m, " %d", ref->proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5669 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5670 | seq_puts(m, "\n"); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5671 | if (node->proc) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5672 | list_for_each_entry(w, &node->async_todo, entry) |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5673 | print_binder_work_ilocked(m, node->proc, " ", |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5674 | " pending async transaction", w); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5675 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5676 | } |
| 5677 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5678 | static void print_binder_ref_olocked(struct seq_file *m, |
| 5679 | struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5680 | { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5681 | binder_node_lock(ref->node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5682 | seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n", |
| 5683 | ref->data.debug_id, ref->data.desc, |
| 5684 | ref->node->proc ? "" : "dead ", |
| 5685 | ref->node->debug_id, ref->data.strong, |
| 5686 | ref->data.weak, ref->death); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5687 | binder_node_unlock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5688 | } |
| 5689 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5690 | static void print_binder_proc(struct seq_file *m, |
| 5691 | struct binder_proc *proc, int print_all) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5692 | { |
| 5693 | struct binder_work *w; |
| 5694 | struct rb_node *n; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5695 | size_t start_pos = m->count; |
| 5696 | size_t header_pos; |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5697 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5698 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5699 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5700 | seq_printf(m, "context %s\n", proc->context->name); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5701 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5702 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5703 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5704 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5705 | 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] | 5706 | rb_node), print_all); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5707 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5708 | 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] | 5709 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 5710 | rb_node); |
Todd Kjos | d79aa41 | 2018-12-05 15:19:26 -0800 | [diff] [blame] | 5711 | if (!print_all && !node->has_async_transaction) |
| 5712 | continue; |
| 5713 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5714 | /* |
| 5715 | * take a temporary reference on the node so it |
| 5716 | * survives and isn't removed from the tree |
| 5717 | * while we print it. |
| 5718 | */ |
| 5719 | binder_inc_node_tmpref_ilocked(node); |
| 5720 | /* Need to drop inner lock to take node lock */ |
| 5721 | binder_inner_proc_unlock(proc); |
| 5722 | if (last_node) |
| 5723 | binder_put_node(last_node); |
| 5724 | binder_node_inner_lock(node); |
| 5725 | print_binder_node_nilocked(m, node); |
| 5726 | binder_node_inner_unlock(node); |
| 5727 | last_node = node; |
| 5728 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5729 | } |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5730 | binder_inner_proc_unlock(proc); |
| 5731 | if (last_node) |
| 5732 | binder_put_node(last_node); |
| 5733 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5734 | if (print_all) { |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5735 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5736 | for (n = rb_first(&proc->refs_by_desc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5737 | n != NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5738 | n = rb_next(n)) |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5739 | print_binder_ref_olocked(m, rb_entry(n, |
| 5740 | struct binder_ref, |
| 5741 | rb_node_desc)); |
| 5742 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5743 | } |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5744 | binder_alloc_print_allocated(m, &proc->alloc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5745 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5746 | list_for_each_entry(w, &proc->todo, entry) |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5747 | print_binder_work_ilocked(m, proc, " ", |
| 5748 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5749 | list_for_each_entry(w, &proc->delivered_death, entry) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5750 | seq_puts(m, " has delivered dead binder\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5751 | break; |
| 5752 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5753 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5754 | if (!print_all && m->count == header_pos) |
| 5755 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5756 | } |
| 5757 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5758 | static const char * const binder_return_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5759 | "BR_ERROR", |
| 5760 | "BR_OK", |
| 5761 | "BR_TRANSACTION", |
| 5762 | "BR_REPLY", |
| 5763 | "BR_ACQUIRE_RESULT", |
| 5764 | "BR_DEAD_REPLY", |
| 5765 | "BR_TRANSACTION_COMPLETE", |
| 5766 | "BR_INCREFS", |
| 5767 | "BR_ACQUIRE", |
| 5768 | "BR_RELEASE", |
| 5769 | "BR_DECREFS", |
| 5770 | "BR_ATTEMPT_ACQUIRE", |
| 5771 | "BR_NOOP", |
| 5772 | "BR_SPAWN_LOOPER", |
| 5773 | "BR_FINISHED", |
| 5774 | "BR_DEAD_BINDER", |
| 5775 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 5776 | "BR_FAILED_REPLY" |
| 5777 | }; |
| 5778 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5779 | static const char * const binder_command_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5780 | "BC_TRANSACTION", |
| 5781 | "BC_REPLY", |
| 5782 | "BC_ACQUIRE_RESULT", |
| 5783 | "BC_FREE_BUFFER", |
| 5784 | "BC_INCREFS", |
| 5785 | "BC_ACQUIRE", |
| 5786 | "BC_RELEASE", |
| 5787 | "BC_DECREFS", |
| 5788 | "BC_INCREFS_DONE", |
| 5789 | "BC_ACQUIRE_DONE", |
| 5790 | "BC_ATTEMPT_ACQUIRE", |
| 5791 | "BC_REGISTER_LOOPER", |
| 5792 | "BC_ENTER_LOOPER", |
| 5793 | "BC_EXIT_LOOPER", |
| 5794 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 5795 | "BC_CLEAR_DEATH_NOTIFICATION", |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 5796 | "BC_DEAD_BINDER_DONE", |
| 5797 | "BC_TRANSACTION_SG", |
| 5798 | "BC_REPLY_SG", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5799 | }; |
| 5800 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5801 | static const char * const binder_objstat_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5802 | "proc", |
| 5803 | "thread", |
| 5804 | "node", |
| 5805 | "ref", |
| 5806 | "death", |
| 5807 | "transaction", |
| 5808 | "transaction_complete" |
| 5809 | }; |
| 5810 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5811 | static void print_binder_stats(struct seq_file *m, const char *prefix, |
| 5812 | struct binder_stats *stats) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5813 | { |
| 5814 | int i; |
| 5815 | |
| 5816 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5817 | ARRAY_SIZE(binder_command_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5818 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5819 | int temp = atomic_read(&stats->bc[i]); |
| 5820 | |
| 5821 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5822 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5823 | binder_command_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5824 | } |
| 5825 | |
| 5826 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5827 | ARRAY_SIZE(binder_return_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5828 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5829 | int temp = atomic_read(&stats->br[i]); |
| 5830 | |
| 5831 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5832 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5833 | binder_return_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5834 | } |
| 5835 | |
| 5836 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5837 | ARRAY_SIZE(binder_objstat_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5838 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5839 | ARRAY_SIZE(stats->obj_deleted)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5840 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5841 | int created = atomic_read(&stats->obj_created[i]); |
| 5842 | int deleted = atomic_read(&stats->obj_deleted[i]); |
| 5843 | |
| 5844 | if (created || deleted) |
| 5845 | seq_printf(m, "%s%s: active %d total %d\n", |
| 5846 | prefix, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5847 | binder_objstat_strings[i], |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5848 | created - deleted, |
| 5849 | created); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5850 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5851 | } |
| 5852 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5853 | static void print_binder_proc_stats(struct seq_file *m, |
| 5854 | struct binder_proc *proc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5855 | { |
| 5856 | struct binder_work *w; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5857 | struct binder_thread *thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5858 | struct rb_node *n; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5859 | int count, strong, weak, ready_threads; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5860 | size_t free_async_space = |
| 5861 | binder_alloc_get_free_async_space(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5862 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5863 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5864 | seq_printf(m, "context %s\n", proc->context->name); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5865 | count = 0; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5866 | ready_threads = 0; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5867 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5868 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 5869 | count++; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5870 | |
| 5871 | list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node) |
| 5872 | ready_threads++; |
| 5873 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5874 | seq_printf(m, " threads: %d\n", count); |
| 5875 | seq_printf(m, " requested threads: %d+%d/%d\n" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5876 | " ready threads %d\n" |
| 5877 | " free async space %zd\n", proc->requested_threads, |
| 5878 | proc->requested_threads_started, proc->max_threads, |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5879 | ready_threads, |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5880 | free_async_space); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5881 | count = 0; |
| 5882 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) |
| 5883 | count++; |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5884 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5885 | seq_printf(m, " nodes: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5886 | count = 0; |
| 5887 | strong = 0; |
| 5888 | weak = 0; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5889 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5890 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 5891 | struct binder_ref *ref = rb_entry(n, struct binder_ref, |
| 5892 | rb_node_desc); |
| 5893 | count++; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5894 | strong += ref->data.strong; |
| 5895 | weak += ref->data.weak; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5896 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5897 | binder_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5898 | 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] | 5899 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5900 | count = binder_alloc_get_allocated_count(&proc->alloc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5901 | seq_printf(m, " buffers: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5902 | |
Sherry Yang | 9100442 | 2017-08-22 17:26:57 -0700 | [diff] [blame] | 5903 | binder_alloc_print_pages(m, &proc->alloc); |
| 5904 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5905 | count = 0; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5906 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5907 | list_for_each_entry(w, &proc->todo, entry) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5908 | if (w->type == BINDER_WORK_TRANSACTION) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5909 | count++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5910 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5911 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5912 | seq_printf(m, " pending transactions: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5913 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5914 | print_binder_stats(m, " ", &proc->stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5915 | } |
| 5916 | |
| 5917 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5918 | static int binder_state_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5919 | { |
| 5920 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5921 | struct binder_node *node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5922 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5923 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5924 | seq_puts(m, "binder state:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5925 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5926 | spin_lock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5927 | if (!hlist_empty(&binder_dead_nodes)) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5928 | seq_puts(m, "dead nodes:\n"); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5929 | hlist_for_each_entry(node, &binder_dead_nodes, dead_node) { |
| 5930 | /* |
| 5931 | * take a temporary reference on the node so it |
| 5932 | * survives and isn't removed from the list |
| 5933 | * while we print it. |
| 5934 | */ |
| 5935 | node->tmp_refs++; |
| 5936 | spin_unlock(&binder_dead_nodes_lock); |
| 5937 | if (last_node) |
| 5938 | binder_put_node(last_node); |
| 5939 | binder_node_lock(node); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5940 | print_binder_node_nilocked(m, node); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5941 | binder_node_unlock(node); |
| 5942 | last_node = node; |
| 5943 | spin_lock(&binder_dead_nodes_lock); |
| 5944 | } |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5945 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5946 | if (last_node) |
| 5947 | binder_put_node(last_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5948 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5949 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5950 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5951 | print_binder_proc(m, proc, 1); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5952 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5953 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5954 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5955 | } |
| 5956 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5957 | static int binder_stats_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5958 | { |
| 5959 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5960 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5961 | seq_puts(m, "binder stats:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5962 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5963 | print_binder_stats(m, "", &binder_stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5964 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5965 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5966 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5967 | print_binder_proc_stats(m, proc); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5968 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5969 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5970 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5971 | } |
| 5972 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5973 | static int binder_transactions_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5974 | { |
| 5975 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5976 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5977 | seq_puts(m, "binder transactions:\n"); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5978 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5979 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5980 | print_binder_proc(m, proc, 0); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5981 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5982 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5983 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5984 | } |
| 5985 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5986 | static int binder_proc_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5987 | { |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5988 | struct binder_proc *itr; |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5989 | int pid = (unsigned long)m->private; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5990 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5991 | mutex_lock(&binder_procs_lock); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5992 | hlist_for_each_entry(itr, &binder_procs, proc_node) { |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5993 | if (itr->pid == pid) { |
| 5994 | seq_puts(m, "binder proc state:\n"); |
| 5995 | print_binder_proc(m, itr, 1); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5996 | } |
| 5997 | } |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5998 | mutex_unlock(&binder_procs_lock); |
| 5999 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6000 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6001 | } |
| 6002 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6003 | static void print_binder_transaction_log_entry(struct seq_file *m, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6004 | struct binder_transaction_log_entry *e) |
| 6005 | { |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 6006 | int debug_id = READ_ONCE(e->debug_id_done); |
| 6007 | /* |
| 6008 | * read barrier to guarantee debug_id_done read before |
| 6009 | * we print the log values |
| 6010 | */ |
| 6011 | smp_rmb(); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6012 | seq_printf(m, |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 6013 | "%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] | 6014 | e->debug_id, (e->call_type == 2) ? "reply" : |
| 6015 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 6016 | e->from_thread, e->to_proc, e->to_thread, e->context_name, |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 6017 | e->to_node, e->target_handle, e->data_size, e->offsets_size, |
| 6018 | e->return_error, e->return_error_param, |
| 6019 | e->return_error_line); |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 6020 | /* |
| 6021 | * read-barrier to guarantee read of debug_id_done after |
| 6022 | * done printing the fields of the entry |
| 6023 | */ |
| 6024 | smp_rmb(); |
| 6025 | seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ? |
| 6026 | "\n" : " (incomplete)\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6027 | } |
| 6028 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6029 | 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] | 6030 | { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6031 | struct binder_transaction_log *log = m->private; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 6032 | unsigned int log_cur = atomic_read(&log->cur); |
| 6033 | unsigned int count; |
| 6034 | unsigned int cur; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6035 | int i; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6036 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 6037 | count = log_cur + 1; |
| 6038 | cur = count < ARRAY_SIZE(log->entry) && !log->full ? |
| 6039 | 0 : count % ARRAY_SIZE(log->entry); |
| 6040 | if (count > ARRAY_SIZE(log->entry) || log->full) |
| 6041 | count = ARRAY_SIZE(log->entry); |
| 6042 | for (i = 0; i < count; i++) { |
| 6043 | unsigned int index = cur++ % ARRAY_SIZE(log->entry); |
| 6044 | |
| 6045 | print_binder_transaction_log_entry(m, &log->entry[index]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6046 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6047 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6048 | } |
| 6049 | |
| 6050 | static const struct file_operations binder_fops = { |
| 6051 | .owner = THIS_MODULE, |
| 6052 | .poll = binder_poll, |
| 6053 | .unlocked_ioctl = binder_ioctl, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 6054 | .compat_ioctl = binder_ioctl, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6055 | .mmap = binder_mmap, |
| 6056 | .open = binder_open, |
| 6057 | .flush = binder_flush, |
| 6058 | .release = binder_release, |
| 6059 | }; |
| 6060 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6061 | BINDER_DEBUG_ENTRY(state); |
| 6062 | BINDER_DEBUG_ENTRY(stats); |
| 6063 | BINDER_DEBUG_ENTRY(transactions); |
| 6064 | BINDER_DEBUG_ENTRY(transaction_log); |
| 6065 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6066 | static int __init init_binder_device(const char *name) |
| 6067 | { |
| 6068 | int ret; |
| 6069 | struct binder_device *binder_device; |
| 6070 | |
| 6071 | binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL); |
| 6072 | if (!binder_device) |
| 6073 | return -ENOMEM; |
| 6074 | |
| 6075 | binder_device->miscdev.fops = &binder_fops; |
| 6076 | binder_device->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 6077 | binder_device->miscdev.name = name; |
| 6078 | |
| 6079 | binder_device->context.binder_context_mgr_uid = INVALID_UID; |
| 6080 | binder_device->context.name = name; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 6081 | mutex_init(&binder_device->context.context_mgr_node_lock); |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6082 | |
| 6083 | ret = misc_register(&binder_device->miscdev); |
| 6084 | if (ret < 0) { |
| 6085 | kfree(binder_device); |
| 6086 | return ret; |
| 6087 | } |
| 6088 | |
| 6089 | hlist_add_head(&binder_device->hlist, &binder_devices); |
| 6090 | |
| 6091 | return ret; |
| 6092 | } |
| 6093 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6094 | static int __init binder_init(void) |
| 6095 | { |
| 6096 | int ret; |
Christian Brauner | 558ee93 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 6097 | char *device_name, *device_names, *device_tmp; |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6098 | struct binder_device *device; |
| 6099 | struct hlist_node *tmp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6100 | |
Tetsuo Handa | f8cb822 | 2017-11-29 22:29:47 +0900 | [diff] [blame] | 6101 | ret = binder_alloc_shrinker_init(); |
| 6102 | if (ret) |
| 6103 | return ret; |
Sherry Yang | 5828d70 | 2017-07-29 13:24:11 -0700 | [diff] [blame] | 6104 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 6105 | atomic_set(&binder_transaction_log.cur, ~0U); |
| 6106 | atomic_set(&binder_transaction_log_failed.cur, ~0U); |
| 6107 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6108 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); |
| 6109 | if (binder_debugfs_dir_entry_root) |
| 6110 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", |
| 6111 | binder_debugfs_dir_entry_root); |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6112 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6113 | if (binder_debugfs_dir_entry_root) { |
| 6114 | debugfs_create_file("state", |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6115 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6116 | binder_debugfs_dir_entry_root, |
| 6117 | NULL, |
| 6118 | &binder_state_fops); |
| 6119 | debugfs_create_file("stats", |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6120 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6121 | binder_debugfs_dir_entry_root, |
| 6122 | NULL, |
| 6123 | &binder_stats_fops); |
| 6124 | debugfs_create_file("transactions", |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6125 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6126 | binder_debugfs_dir_entry_root, |
| 6127 | NULL, |
| 6128 | &binder_transactions_fops); |
| 6129 | debugfs_create_file("transaction_log", |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6130 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6131 | binder_debugfs_dir_entry_root, |
| 6132 | &binder_transaction_log, |
| 6133 | &binder_transaction_log_fops); |
| 6134 | debugfs_create_file("failed_transaction_log", |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6135 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6136 | binder_debugfs_dir_entry_root, |
| 6137 | &binder_transaction_log_failed, |
| 6138 | &binder_transaction_log_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6139 | } |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6140 | |
| 6141 | /* |
| 6142 | * Copy the module_parameter string, because we don't want to |
| 6143 | * tokenize it in-place. |
| 6144 | */ |
| 6145 | device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL); |
| 6146 | if (!device_names) { |
| 6147 | ret = -ENOMEM; |
| 6148 | goto err_alloc_device_names_failed; |
| 6149 | } |
| 6150 | strcpy(device_names, binder_devices_param); |
| 6151 | |
Christian Brauner | 558ee93 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 6152 | device_tmp = device_names; |
| 6153 | while ((device_name = strsep(&device_tmp, ","))) { |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6154 | ret = init_binder_device(device_name); |
| 6155 | if (ret) |
| 6156 | goto err_init_binder_device_failed; |
| 6157 | } |
| 6158 | |
| 6159 | return ret; |
| 6160 | |
| 6161 | err_init_binder_device_failed: |
| 6162 | hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) { |
| 6163 | misc_deregister(&device->miscdev); |
| 6164 | hlist_del(&device->hlist); |
| 6165 | kfree(device); |
| 6166 | } |
Christian Brauner | 558ee93 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 6167 | |
| 6168 | kfree(device_names); |
| 6169 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6170 | err_alloc_device_names_failed: |
| 6171 | debugfs_remove_recursive(binder_debugfs_dir_entry_root); |
| 6172 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6173 | return ret; |
| 6174 | } |
| 6175 | |
| 6176 | device_initcall(binder_init); |
| 6177 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 6178 | #define CREATE_TRACE_POINTS |
| 6179 | #include "binder_trace.h" |
| 6180 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6181 | MODULE_LICENSE("GPL v2"); |