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 | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 530 | * (protected by @inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 531 | * @default_priority: default scheduler priority |
| 532 | * (invariant after initialized) |
| 533 | * @debugfs_entry: debugfs node |
| 534 | * @alloc: binder allocator bookkeeping |
| 535 | * @context: binder_context for this proc |
| 536 | * (invariant after initialized) |
| 537 | * @inner_lock: can nest under outer_lock and/or node lock |
| 538 | * @outer_lock: no nesting under innor or node lock |
| 539 | * Lock order: 1) outer, 2) node, 3) inner |
| 540 | * |
| 541 | * Bookkeeping structure for binder processes |
| 542 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 543 | struct binder_proc { |
| 544 | struct hlist_node proc_node; |
| 545 | struct rb_root threads; |
| 546 | struct rb_root nodes; |
| 547 | struct rb_root refs_by_desc; |
| 548 | struct rb_root refs_by_node; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 549 | struct list_head waiting_threads; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 550 | int pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 551 | struct task_struct *tsk; |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 552 | struct files_struct *files; |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 553 | struct mutex files_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 554 | struct hlist_node deferred_work_node; |
| 555 | int deferred_work; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 556 | bool is_dead; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 557 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 558 | struct list_head todo; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 559 | struct binder_stats stats; |
| 560 | struct list_head delivered_death; |
| 561 | int max_threads; |
| 562 | int requested_threads; |
| 563 | int requested_threads_started; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 564 | int tmp_ref; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 565 | struct binder_priority default_priority; |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 566 | struct dentry *debugfs_entry; |
Todd Kjos | f85d229 | 2016-10-10 10:39:59 -0700 | [diff] [blame] | 567 | struct binder_alloc alloc; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 568 | struct binder_context *context; |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 569 | spinlock_t inner_lock; |
| 570 | spinlock_t outer_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 571 | }; |
| 572 | |
| 573 | enum { |
| 574 | BINDER_LOOPER_STATE_REGISTERED = 0x01, |
| 575 | BINDER_LOOPER_STATE_ENTERED = 0x02, |
| 576 | BINDER_LOOPER_STATE_EXITED = 0x04, |
| 577 | BINDER_LOOPER_STATE_INVALID = 0x08, |
| 578 | BINDER_LOOPER_STATE_WAITING = 0x10, |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 579 | BINDER_LOOPER_STATE_POLL = 0x20, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 580 | }; |
| 581 | |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 582 | /** |
| 583 | * struct binder_thread - binder thread bookkeeping |
| 584 | * @proc: binder process for this thread |
| 585 | * (invariant after initialization) |
| 586 | * @rb_node: element for proc->threads rbtree |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 587 | * (protected by @proc->inner_lock) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 588 | * @waiting_thread_node: element for @proc->waiting_threads list |
| 589 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 590 | * @pid: PID for this thread |
| 591 | * (invariant after initialization) |
| 592 | * @looper: bitmap of looping state |
| 593 | * (only accessed by this thread) |
| 594 | * @looper_needs_return: looping thread needs to exit driver |
| 595 | * (no lock needed) |
| 596 | * @transaction_stack: stack of in-progress transactions for this thread |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 597 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 598 | * @todo: list of work to do for this thread |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 599 | * (protected by @proc->inner_lock) |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 600 | * @process_todo: whether work in @todo should be processed |
| 601 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 602 | * @return_error: transaction errors reported by this thread |
| 603 | * (only accessed by this thread) |
| 604 | * @reply_error: transaction errors reported by target thread |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 605 | * (protected by @proc->inner_lock) |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 606 | * @wait: wait queue for thread work |
| 607 | * @stats: per-thread statistics |
| 608 | * (atomics, no lock needed) |
| 609 | * @tmp_ref: temporary reference to indicate thread is in use |
| 610 | * (atomic since @proc->inner_lock cannot |
| 611 | * always be acquired) |
| 612 | * @is_dead: thread is dead and awaiting free |
| 613 | * when outstanding transactions are cleaned up |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 614 | * (protected by @proc->inner_lock) |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 615 | * @task: struct task_struct for this thread |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 616 | * |
| 617 | * Bookkeeping structure for binder threads. |
| 618 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 619 | struct binder_thread { |
| 620 | struct binder_proc *proc; |
| 621 | struct rb_node rb_node; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 622 | struct list_head waiting_thread_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 623 | int pid; |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 624 | int looper; /* only modified by this thread */ |
| 625 | bool looper_need_return; /* can be written by other thread */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 626 | struct binder_transaction *transaction_stack; |
| 627 | struct list_head todo; |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 628 | bool process_todo; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 629 | struct binder_error return_error; |
| 630 | struct binder_error reply_error; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 631 | wait_queue_head_t wait; |
| 632 | struct binder_stats stats; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 633 | atomic_t tmp_ref; |
| 634 | bool is_dead; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 635 | struct task_struct *task; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 636 | }; |
| 637 | |
| 638 | struct binder_transaction { |
| 639 | int debug_id; |
| 640 | struct binder_work work; |
| 641 | struct binder_thread *from; |
| 642 | struct binder_transaction *from_parent; |
| 643 | struct binder_proc *to_proc; |
| 644 | struct binder_thread *to_thread; |
| 645 | struct binder_transaction *to_parent; |
| 646 | unsigned need_reply:1; |
| 647 | /* unsigned is_dead:1; */ /* not used at the moment */ |
| 648 | |
| 649 | struct binder_buffer *buffer; |
| 650 | unsigned int code; |
| 651 | unsigned int flags; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 652 | struct binder_priority priority; |
| 653 | struct binder_priority saved_priority; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 654 | bool set_priority_called; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 655 | kuid_t sender_euid; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 656 | binder_uintptr_t security_ctx; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 657 | /** |
| 658 | * @lock: protects @from, @to_proc, and @to_thread |
| 659 | * |
| 660 | * @from, @to_proc, and @to_thread can be set to NULL |
| 661 | * during thread teardown |
| 662 | */ |
| 663 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 664 | }; |
| 665 | |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 666 | /** |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 667 | * struct binder_object - union of flat binder object types |
| 668 | * @hdr: generic object header |
| 669 | * @fbo: binder object (nodes and refs) |
| 670 | * @fdo: file descriptor object |
| 671 | * @bbo: binder buffer pointer |
| 672 | * @fdao: file descriptor array |
| 673 | * |
| 674 | * Used for type-independent object copies |
| 675 | */ |
| 676 | struct binder_object { |
| 677 | union { |
| 678 | struct binder_object_header hdr; |
| 679 | struct flat_binder_object fbo; |
| 680 | struct binder_fd_object fdo; |
| 681 | struct binder_buffer_object bbo; |
| 682 | struct binder_fd_array_object fdao; |
| 683 | }; |
| 684 | }; |
| 685 | |
| 686 | /** |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 687 | * binder_proc_lock() - Acquire outer lock for given binder_proc |
| 688 | * @proc: struct binder_proc to acquire |
| 689 | * |
| 690 | * Acquires proc->outer_lock. Used to protect binder_ref |
| 691 | * structures associated with the given proc. |
| 692 | */ |
| 693 | #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__) |
| 694 | static void |
| 695 | _binder_proc_lock(struct binder_proc *proc, int line) |
| 696 | { |
| 697 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 698 | "%s: line=%d\n", __func__, line); |
| 699 | spin_lock(&proc->outer_lock); |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * binder_proc_unlock() - Release spinlock for given binder_proc |
| 704 | * @proc: struct binder_proc to acquire |
| 705 | * |
| 706 | * Release lock acquired via binder_proc_lock() |
| 707 | */ |
| 708 | #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__) |
| 709 | static void |
| 710 | _binder_proc_unlock(struct binder_proc *proc, int line) |
| 711 | { |
| 712 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 713 | "%s: line=%d\n", __func__, line); |
| 714 | spin_unlock(&proc->outer_lock); |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * binder_inner_proc_lock() - Acquire inner lock for given binder_proc |
| 719 | * @proc: struct binder_proc to acquire |
| 720 | * |
| 721 | * Acquires proc->inner_lock. Used to protect todo lists |
| 722 | */ |
| 723 | #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__) |
| 724 | static void |
| 725 | _binder_inner_proc_lock(struct binder_proc *proc, int line) |
| 726 | { |
| 727 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 728 | "%s: line=%d\n", __func__, line); |
| 729 | spin_lock(&proc->inner_lock); |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * binder_inner_proc_unlock() - Release inner lock for given binder_proc |
| 734 | * @proc: struct binder_proc to acquire |
| 735 | * |
| 736 | * Release lock acquired via binder_inner_proc_lock() |
| 737 | */ |
| 738 | #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__) |
| 739 | static void |
| 740 | _binder_inner_proc_unlock(struct binder_proc *proc, int line) |
| 741 | { |
| 742 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 743 | "%s: line=%d\n", __func__, line); |
| 744 | spin_unlock(&proc->inner_lock); |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * binder_node_lock() - Acquire spinlock for given binder_node |
| 749 | * @node: struct binder_node to acquire |
| 750 | * |
| 751 | * Acquires node->lock. Used to protect binder_node fields |
| 752 | */ |
| 753 | #define binder_node_lock(node) _binder_node_lock(node, __LINE__) |
| 754 | static void |
| 755 | _binder_node_lock(struct binder_node *node, int line) |
| 756 | { |
| 757 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 758 | "%s: line=%d\n", __func__, line); |
| 759 | spin_lock(&node->lock); |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * binder_node_unlock() - Release spinlock for given binder_proc |
| 764 | * @node: struct binder_node to acquire |
| 765 | * |
| 766 | * Release lock acquired via binder_node_lock() |
| 767 | */ |
| 768 | #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__) |
| 769 | static void |
| 770 | _binder_node_unlock(struct binder_node *node, int line) |
| 771 | { |
| 772 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 773 | "%s: line=%d\n", __func__, line); |
| 774 | spin_unlock(&node->lock); |
| 775 | } |
| 776 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 777 | /** |
| 778 | * binder_node_inner_lock() - Acquire node and inner locks |
| 779 | * @node: struct binder_node to acquire |
| 780 | * |
| 781 | * Acquires node->lock. If node->proc also acquires |
| 782 | * proc->inner_lock. Used to protect binder_node fields |
| 783 | */ |
| 784 | #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__) |
| 785 | static void |
| 786 | _binder_node_inner_lock(struct binder_node *node, int line) |
| 787 | { |
| 788 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 789 | "%s: line=%d\n", __func__, line); |
| 790 | spin_lock(&node->lock); |
| 791 | if (node->proc) |
| 792 | binder_inner_proc_lock(node->proc); |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * binder_node_unlock() - Release node and inner locks |
| 797 | * @node: struct binder_node to acquire |
| 798 | * |
| 799 | * Release lock acquired via binder_node_lock() |
| 800 | */ |
| 801 | #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__) |
| 802 | static void |
| 803 | _binder_node_inner_unlock(struct binder_node *node, int line) |
| 804 | { |
| 805 | struct binder_proc *proc = node->proc; |
| 806 | |
| 807 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 808 | "%s: line=%d\n", __func__, line); |
| 809 | if (proc) |
| 810 | binder_inner_proc_unlock(proc); |
| 811 | spin_unlock(&node->lock); |
| 812 | } |
| 813 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 814 | static bool binder_worklist_empty_ilocked(struct list_head *list) |
| 815 | { |
| 816 | return list_empty(list); |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * binder_worklist_empty() - Check if no items on the work list |
| 821 | * @proc: binder_proc associated with list |
| 822 | * @list: list to check |
| 823 | * |
| 824 | * Return: true if there are no items on list, else false |
| 825 | */ |
| 826 | static bool binder_worklist_empty(struct binder_proc *proc, |
| 827 | struct list_head *list) |
| 828 | { |
| 829 | bool ret; |
| 830 | |
| 831 | binder_inner_proc_lock(proc); |
| 832 | ret = binder_worklist_empty_ilocked(list); |
| 833 | binder_inner_proc_unlock(proc); |
| 834 | return ret; |
| 835 | } |
| 836 | |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 837 | /** |
| 838 | * binder_enqueue_work_ilocked() - Add an item to the work list |
| 839 | * @work: struct binder_work to add to list |
| 840 | * @target_list: list to add work to |
| 841 | * |
| 842 | * Adds the work to the specified list. Asserts that work |
| 843 | * is not already on a list. |
| 844 | * |
| 845 | * Requires the proc->inner_lock to be held. |
| 846 | */ |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 847 | static void |
| 848 | binder_enqueue_work_ilocked(struct binder_work *work, |
| 849 | struct list_head *target_list) |
| 850 | { |
| 851 | BUG_ON(target_list == NULL); |
| 852 | BUG_ON(work->entry.next && !list_empty(&work->entry)); |
| 853 | list_add_tail(&work->entry, target_list); |
| 854 | } |
| 855 | |
| 856 | /** |
Martijn Coenen | dac2e9c | 2017-11-13 09:55:21 +0100 | [diff] [blame] | 857 | * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 858 | * @thread: thread to queue work to |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 859 | * @work: struct binder_work to add to list |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 860 | * |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 861 | * Adds the work to the todo list of the thread. Doesn't set the process_todo |
| 862 | * flag, which means that (if it wasn't already set) the thread will go to |
| 863 | * sleep without handling this work when it calls read. |
| 864 | * |
| 865 | * Requires the proc->inner_lock to be held. |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 866 | */ |
| 867 | static void |
Martijn Coenen | dac2e9c | 2017-11-13 09:55:21 +0100 | [diff] [blame] | 868 | binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread, |
| 869 | struct binder_work *work) |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 870 | { |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 871 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list |
| 876 | * @thread: thread to queue work to |
| 877 | * @work: struct binder_work to add to list |
| 878 | * |
| 879 | * Adds the work to the todo list of the thread, and enables processing |
| 880 | * of the todo queue. |
| 881 | * |
| 882 | * Requires the proc->inner_lock to be held. |
| 883 | */ |
| 884 | static void |
| 885 | binder_enqueue_thread_work_ilocked(struct binder_thread *thread, |
| 886 | struct binder_work *work) |
| 887 | { |
| 888 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 889 | thread->process_todo = true; |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * binder_enqueue_thread_work() - Add an item to the thread work list |
| 894 | * @thread: thread to queue work to |
| 895 | * @work: struct binder_work to add to list |
| 896 | * |
| 897 | * Adds the work to the todo list of the thread, and enables processing |
| 898 | * of the todo queue. |
| 899 | */ |
| 900 | static void |
| 901 | binder_enqueue_thread_work(struct binder_thread *thread, |
| 902 | struct binder_work *work) |
| 903 | { |
| 904 | binder_inner_proc_lock(thread->proc); |
| 905 | binder_enqueue_thread_work_ilocked(thread, work); |
| 906 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static void |
| 910 | binder_dequeue_work_ilocked(struct binder_work *work) |
| 911 | { |
| 912 | list_del_init(&work->entry); |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * binder_dequeue_work() - Removes an item from the work list |
| 917 | * @proc: binder_proc associated with list |
| 918 | * @work: struct binder_work to remove from list |
| 919 | * |
| 920 | * Removes the specified work item from whatever list it is on. |
| 921 | * Can safely be called if work is not on any list. |
| 922 | */ |
| 923 | static void |
| 924 | binder_dequeue_work(struct binder_proc *proc, struct binder_work *work) |
| 925 | { |
| 926 | binder_inner_proc_lock(proc); |
| 927 | binder_dequeue_work_ilocked(work); |
| 928 | binder_inner_proc_unlock(proc); |
| 929 | } |
| 930 | |
| 931 | static struct binder_work *binder_dequeue_work_head_ilocked( |
| 932 | struct list_head *list) |
| 933 | { |
| 934 | struct binder_work *w; |
| 935 | |
| 936 | w = list_first_entry_or_null(list, struct binder_work, entry); |
| 937 | if (w) |
| 938 | list_del_init(&w->entry); |
| 939 | return w; |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * binder_dequeue_work_head() - Dequeues the item at head of list |
| 944 | * @proc: binder_proc associated with list |
| 945 | * @list: list to dequeue head |
| 946 | * |
| 947 | * Removes the head of the list if there are items on the list |
| 948 | * |
| 949 | * Return: pointer dequeued binder_work, NULL if list was empty |
| 950 | */ |
| 951 | static struct binder_work *binder_dequeue_work_head( |
| 952 | struct binder_proc *proc, |
| 953 | struct list_head *list) |
| 954 | { |
| 955 | struct binder_work *w; |
| 956 | |
| 957 | binder_inner_proc_lock(proc); |
| 958 | w = binder_dequeue_work_head_ilocked(list); |
| 959 | binder_inner_proc_unlock(proc); |
| 960 | return w; |
| 961 | } |
| 962 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 963 | static void |
| 964 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 965 | static void binder_free_thread(struct binder_thread *thread); |
| 966 | static void binder_free_proc(struct binder_proc *proc); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 967 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 968 | |
Sachin Kamat | efde99c | 2012-08-17 16:39:36 +0530 | [diff] [blame] | 969 | 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] | 970 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 971 | unsigned long rlim_cur; |
| 972 | unsigned long irqs; |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 973 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 974 | |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 975 | mutex_lock(&proc->files_lock); |
| 976 | if (proc->files == NULL) { |
| 977 | ret = -ESRCH; |
| 978 | goto err; |
| 979 | } |
| 980 | if (!lock_task_sighand(proc->tsk, &irqs)) { |
| 981 | ret = -EMFILE; |
| 982 | goto err; |
| 983 | } |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 984 | rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE); |
| 985 | unlock_task_sighand(proc->tsk, &irqs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 986 | |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 987 | ret = __alloc_fd(proc->files, 0, rlim_cur, flags); |
| 988 | err: |
| 989 | mutex_unlock(&proc->files_lock); |
| 990 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /* |
| 994 | * copied from fd_install |
| 995 | */ |
| 996 | static void task_fd_install( |
| 997 | struct binder_proc *proc, unsigned int fd, struct file *file) |
| 998 | { |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 999 | mutex_lock(&proc->files_lock); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 1000 | if (proc->files) |
| 1001 | __fd_install(proc->files, fd, file); |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 1002 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /* |
| 1006 | * copied from sys_close |
| 1007 | */ |
| 1008 | static long task_close_fd(struct binder_proc *proc, unsigned int fd) |
| 1009 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1010 | int retval; |
| 1011 | |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 1012 | mutex_lock(&proc->files_lock); |
| 1013 | if (proc->files == NULL) { |
| 1014 | retval = -ESRCH; |
| 1015 | goto err; |
| 1016 | } |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 1017 | retval = __close_fd(proc->files, fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1018 | /* can't restart close syscall because file table entry was cleared */ |
| 1019 | if (unlikely(retval == -ERESTARTSYS || |
| 1020 | retval == -ERESTARTNOINTR || |
| 1021 | retval == -ERESTARTNOHAND || |
| 1022 | retval == -ERESTART_RESTARTBLOCK)) |
| 1023 | retval = -EINTR; |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 1024 | err: |
| 1025 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1026 | return retval; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1027 | } |
| 1028 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1029 | static bool binder_has_work_ilocked(struct binder_thread *thread, |
| 1030 | bool do_proc_work) |
| 1031 | { |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 1032 | return thread->process_todo || |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1033 | thread->looper_need_return || |
| 1034 | (do_proc_work && |
| 1035 | !binder_worklist_empty_ilocked(&thread->proc->todo)); |
| 1036 | } |
| 1037 | |
| 1038 | static bool binder_has_work(struct binder_thread *thread, bool do_proc_work) |
| 1039 | { |
| 1040 | bool has_work; |
| 1041 | |
| 1042 | binder_inner_proc_lock(thread->proc); |
| 1043 | has_work = binder_has_work_ilocked(thread, do_proc_work); |
| 1044 | binder_inner_proc_unlock(thread->proc); |
| 1045 | |
| 1046 | return has_work; |
| 1047 | } |
| 1048 | |
| 1049 | static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread) |
| 1050 | { |
| 1051 | return !thread->transaction_stack && |
| 1052 | binder_worklist_empty_ilocked(&thread->todo) && |
| 1053 | (thread->looper & (BINDER_LOOPER_STATE_ENTERED | |
| 1054 | BINDER_LOOPER_STATE_REGISTERED)); |
| 1055 | } |
| 1056 | |
| 1057 | static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc, |
| 1058 | bool sync) |
| 1059 | { |
| 1060 | struct rb_node *n; |
| 1061 | struct binder_thread *thread; |
| 1062 | |
| 1063 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 1064 | thread = rb_entry(n, struct binder_thread, rb_node); |
| 1065 | if (thread->looper & BINDER_LOOPER_STATE_POLL && |
| 1066 | binder_available_for_proc_work_ilocked(thread)) { |
| 1067 | if (sync) |
| 1068 | wake_up_interruptible_sync(&thread->wait); |
| 1069 | else |
| 1070 | wake_up_interruptible(&thread->wait); |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1075 | /** |
| 1076 | * binder_select_thread_ilocked() - selects a thread for doing proc work. |
| 1077 | * @proc: process to select a thread from |
| 1078 | * |
| 1079 | * Note that calling this function moves the thread off the waiting_threads |
| 1080 | * list, so it can only be woken up by the caller of this function, or a |
| 1081 | * signal. Therefore, callers *should* always wake up the thread this function |
| 1082 | * returns. |
| 1083 | * |
| 1084 | * Return: If there's a thread currently waiting for process work, |
| 1085 | * returns that thread. Otherwise returns NULL. |
| 1086 | */ |
| 1087 | static struct binder_thread * |
| 1088 | binder_select_thread_ilocked(struct binder_proc *proc) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1089 | { |
| 1090 | struct binder_thread *thread; |
| 1091 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1092 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1093 | thread = list_first_entry_or_null(&proc->waiting_threads, |
| 1094 | struct binder_thread, |
| 1095 | waiting_thread_node); |
| 1096 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1097 | if (thread) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1098 | list_del_init(&thread->waiting_thread_node); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1099 | |
| 1100 | return thread; |
| 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work. |
| 1105 | * @proc: process to wake up a thread in |
| 1106 | * @thread: specific thread to wake-up (may be NULL) |
| 1107 | * @sync: whether to do a synchronous wake-up |
| 1108 | * |
| 1109 | * This function wakes up a thread in the @proc process. |
| 1110 | * The caller may provide a specific thread to wake-up in |
| 1111 | * the @thread parameter. If @thread is NULL, this function |
| 1112 | * will wake up threads that have called poll(). |
| 1113 | * |
| 1114 | * Note that for this function to work as expected, callers |
| 1115 | * should first call binder_select_thread() to find a thread |
| 1116 | * to handle the work (if they don't have a thread already), |
| 1117 | * and pass the result into the @thread parameter. |
| 1118 | */ |
| 1119 | static void binder_wakeup_thread_ilocked(struct binder_proc *proc, |
| 1120 | struct binder_thread *thread, |
| 1121 | bool sync) |
| 1122 | { |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1123 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1124 | |
| 1125 | if (thread) { |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 1126 | if (sync) |
| 1127 | wake_up_interruptible_sync(&thread->wait); |
| 1128 | else |
| 1129 | wake_up_interruptible(&thread->wait); |
| 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | /* Didn't find a thread waiting for proc work; this can happen |
| 1134 | * in two scenarios: |
| 1135 | * 1. All threads are busy handling transactions |
| 1136 | * In that case, one of those threads should call back into |
| 1137 | * the kernel driver soon and pick up this work. |
| 1138 | * 2. Threads are using the (e)poll interface, in which case |
| 1139 | * they may be blocked on the waitqueue without having been |
| 1140 | * added to waiting_threads. For this case, we just iterate |
| 1141 | * over all threads not handling transaction work, and |
| 1142 | * wake them all up. We wake all because we don't know whether |
| 1143 | * a thread that called into (e)poll is handling non-binder |
| 1144 | * work currently. |
| 1145 | */ |
| 1146 | binder_wakeup_poll_threads_ilocked(proc, sync); |
| 1147 | } |
| 1148 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1149 | static void binder_wakeup_proc_ilocked(struct binder_proc *proc) |
| 1150 | { |
| 1151 | struct binder_thread *thread = binder_select_thread_ilocked(proc); |
| 1152 | |
| 1153 | binder_wakeup_thread_ilocked(proc, thread, /* sync = */false); |
| 1154 | } |
| 1155 | |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1156 | static bool is_rt_policy(int policy) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1157 | { |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1158 | return policy == SCHED_FIFO || policy == SCHED_RR; |
| 1159 | } |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1160 | |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1161 | static bool is_fair_policy(int policy) |
| 1162 | { |
| 1163 | return policy == SCHED_NORMAL || policy == SCHED_BATCH; |
| 1164 | } |
| 1165 | |
| 1166 | static bool binder_supported_policy(int policy) |
| 1167 | { |
| 1168 | return is_fair_policy(policy) || is_rt_policy(policy); |
| 1169 | } |
| 1170 | |
| 1171 | static int to_userspace_prio(int policy, int kernel_priority) |
| 1172 | { |
| 1173 | if (is_fair_policy(policy)) |
| 1174 | return PRIO_TO_NICE(kernel_priority); |
| 1175 | else |
| 1176 | return MAX_USER_RT_PRIO - 1 - kernel_priority; |
| 1177 | } |
| 1178 | |
| 1179 | static int to_kernel_prio(int policy, int user_priority) |
| 1180 | { |
| 1181 | if (is_fair_policy(policy)) |
| 1182 | return NICE_TO_PRIO(user_priority); |
| 1183 | else |
| 1184 | return MAX_USER_RT_PRIO - 1 - user_priority; |
| 1185 | } |
| 1186 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1187 | static void binder_do_set_priority(struct task_struct *task, |
| 1188 | struct binder_priority desired, |
| 1189 | bool verify) |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1190 | { |
| 1191 | int priority; /* user-space prio value */ |
| 1192 | bool has_cap_nice; |
| 1193 | unsigned int policy = desired.sched_policy; |
| 1194 | |
| 1195 | if (task->policy == policy && task->normal_prio == desired.prio) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1196 | return; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1197 | |
| 1198 | has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE); |
| 1199 | |
| 1200 | priority = to_userspace_prio(policy, desired.prio); |
| 1201 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1202 | if (verify && is_rt_policy(policy) && !has_cap_nice) { |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1203 | long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO); |
| 1204 | |
| 1205 | if (max_rtprio == 0) { |
| 1206 | policy = SCHED_NORMAL; |
| 1207 | priority = MIN_NICE; |
| 1208 | } else if (priority > max_rtprio) { |
| 1209 | priority = max_rtprio; |
| 1210 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1211 | } |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1212 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1213 | if (verify && is_fair_policy(policy) && !has_cap_nice) { |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1214 | long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE)); |
| 1215 | |
| 1216 | if (min_nice > MAX_NICE) { |
| 1217 | binder_user_error("%d RLIMIT_NICE not set\n", |
| 1218 | task->pid); |
| 1219 | return; |
| 1220 | } else if (priority < min_nice) { |
| 1221 | priority = min_nice; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | if (policy != desired.sched_policy || |
| 1226 | to_kernel_prio(policy, priority) != desired.prio) |
| 1227 | binder_debug(BINDER_DEBUG_PRIORITY_CAP, |
| 1228 | "%d: priority %d not allowed, using %d instead\n", |
| 1229 | task->pid, desired.prio, |
| 1230 | to_kernel_prio(policy, priority)); |
| 1231 | |
Martijn Coenen | 81402ea | 2017-05-08 09:33:22 -0700 | [diff] [blame] | 1232 | trace_binder_set_priority(task->tgid, task->pid, task->normal_prio, |
| 1233 | to_kernel_prio(policy, priority), |
| 1234 | desired.prio); |
| 1235 | |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1236 | /* Set the actual priority */ |
| 1237 | if (task->policy != policy || is_rt_policy(policy)) { |
| 1238 | struct sched_param params; |
| 1239 | |
| 1240 | params.sched_priority = is_rt_policy(policy) ? priority : 0; |
| 1241 | |
| 1242 | sched_setscheduler_nocheck(task, |
| 1243 | policy | SCHED_RESET_ON_FORK, |
| 1244 | ¶ms); |
| 1245 | } |
| 1246 | if (is_fair_policy(policy)) |
| 1247 | set_user_nice(task, priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1248 | } |
| 1249 | |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1250 | static void binder_set_priority(struct task_struct *task, |
| 1251 | struct binder_priority desired) |
| 1252 | { |
| 1253 | binder_do_set_priority(task, desired, /* verify = */ true); |
| 1254 | } |
| 1255 | |
| 1256 | static void binder_restore_priority(struct task_struct *task, |
| 1257 | struct binder_priority desired) |
| 1258 | { |
| 1259 | binder_do_set_priority(task, desired, /* verify = */ false); |
| 1260 | } |
| 1261 | |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1262 | static void binder_transaction_priority(struct task_struct *task, |
| 1263 | struct binder_transaction *t, |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1264 | struct binder_priority node_prio, |
| 1265 | bool inherit_rt) |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1266 | { |
Ganesh Mahendran | 9add7c4 | 2017-09-27 15:12:25 +0800 | [diff] [blame] | 1267 | struct binder_priority desired_prio = t->priority; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1268 | |
| 1269 | if (t->set_priority_called) |
| 1270 | return; |
| 1271 | |
| 1272 | t->set_priority_called = true; |
| 1273 | t->saved_priority.sched_policy = task->policy; |
| 1274 | t->saved_priority.prio = task->normal_prio; |
| 1275 | |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1276 | if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) { |
| 1277 | desired_prio.prio = NICE_TO_PRIO(0); |
| 1278 | desired_prio.sched_policy = SCHED_NORMAL; |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1279 | } |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1280 | |
| 1281 | if (node_prio.prio < t->priority.prio || |
| 1282 | (node_prio.prio == t->priority.prio && |
| 1283 | node_prio.sched_policy == SCHED_FIFO)) { |
| 1284 | /* |
| 1285 | * In case the minimum priority on the node is |
| 1286 | * higher (lower value), use that priority. If |
| 1287 | * the priority is the same, but the node uses |
| 1288 | * SCHED_FIFO, prefer SCHED_FIFO, since it can |
| 1289 | * run unbounded, unlike SCHED_RR. |
| 1290 | */ |
| 1291 | desired_prio = node_prio; |
| 1292 | } |
| 1293 | |
| 1294 | binder_set_priority(task, desired_prio); |
| 1295 | } |
| 1296 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1297 | static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc, |
| 1298 | binder_uintptr_t ptr) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1299 | { |
| 1300 | struct rb_node *n = proc->nodes.rb_node; |
| 1301 | struct binder_node *node; |
| 1302 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1303 | assert_spin_locked(&proc->inner_lock); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1304 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1305 | while (n) { |
| 1306 | node = rb_entry(n, struct binder_node, rb_node); |
| 1307 | |
| 1308 | if (ptr < node->ptr) |
| 1309 | n = n->rb_left; |
| 1310 | else if (ptr > node->ptr) |
| 1311 | n = n->rb_right; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1312 | else { |
| 1313 | /* |
| 1314 | * take an implicit weak reference |
| 1315 | * to ensure node stays alive until |
| 1316 | * call to binder_put_node() |
| 1317 | */ |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1318 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1319 | return node; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1320 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1321 | } |
| 1322 | return NULL; |
| 1323 | } |
| 1324 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1325 | static struct binder_node *binder_get_node(struct binder_proc *proc, |
| 1326 | binder_uintptr_t ptr) |
| 1327 | { |
| 1328 | struct binder_node *node; |
| 1329 | |
| 1330 | binder_inner_proc_lock(proc); |
| 1331 | node = binder_get_node_ilocked(proc, ptr); |
| 1332 | binder_inner_proc_unlock(proc); |
| 1333 | return node; |
| 1334 | } |
| 1335 | |
| 1336 | static struct binder_node *binder_init_node_ilocked( |
| 1337 | struct binder_proc *proc, |
| 1338 | struct binder_node *new_node, |
| 1339 | struct flat_binder_object *fp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1340 | { |
| 1341 | struct rb_node **p = &proc->nodes.rb_node; |
| 1342 | struct rb_node *parent = NULL; |
| 1343 | struct binder_node *node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1344 | binder_uintptr_t ptr = fp ? fp->binder : 0; |
| 1345 | binder_uintptr_t cookie = fp ? fp->cookie : 0; |
| 1346 | __u32 flags = fp ? fp->flags : 0; |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1347 | s8 priority; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1348 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1349 | assert_spin_locked(&proc->inner_lock); |
| 1350 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1351 | while (*p) { |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1352 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1353 | parent = *p; |
| 1354 | node = rb_entry(parent, struct binder_node, rb_node); |
| 1355 | |
| 1356 | if (ptr < node->ptr) |
| 1357 | p = &(*p)->rb_left; |
| 1358 | else if (ptr > node->ptr) |
| 1359 | p = &(*p)->rb_right; |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1360 | else { |
| 1361 | /* |
| 1362 | * A matching node is already in |
| 1363 | * the rb tree. Abandon the init |
| 1364 | * and return it. |
| 1365 | */ |
| 1366 | binder_inc_node_tmpref_ilocked(node); |
| 1367 | return node; |
| 1368 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1369 | } |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1370 | node = new_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1371 | binder_stats_created(BINDER_STAT_NODE); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1372 | node->tmp_refs++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1373 | rb_link_node(&node->rb_node, parent, p); |
| 1374 | rb_insert_color(&node->rb_node, &proc->nodes); |
Todd Kjos | c4bd08b | 2017-05-25 10:56:00 -0700 | [diff] [blame] | 1375 | node->debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1376 | node->proc = proc; |
| 1377 | node->ptr = ptr; |
| 1378 | node->cookie = cookie; |
| 1379 | node->work.type = BINDER_WORK_NODE; |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1380 | priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK; |
Ganesh Mahendran | 6cd2631 | 2017-09-26 17:56:25 +0800 | [diff] [blame] | 1381 | node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >> |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1382 | FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT; |
| 1383 | node->min_priority = to_kernel_prio(node->sched_policy, priority); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1384 | node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1385 | node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT); |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 1386 | node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX); |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 1387 | spin_lock_init(&node->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1388 | INIT_LIST_HEAD(&node->work.entry); |
| 1389 | INIT_LIST_HEAD(&node->async_todo); |
| 1390 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1391 | "%d:%d node %d u%016llx c%016llx created\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1392 | proc->pid, current->pid, node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1393 | (u64)node->ptr, (u64)node->cookie); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 1394 | |
| 1395 | return node; |
| 1396 | } |
| 1397 | |
| 1398 | static struct binder_node *binder_new_node(struct binder_proc *proc, |
| 1399 | struct flat_binder_object *fp) |
| 1400 | { |
| 1401 | struct binder_node *node; |
| 1402 | struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 1403 | |
| 1404 | if (!new_node) |
| 1405 | return NULL; |
| 1406 | binder_inner_proc_lock(proc); |
| 1407 | node = binder_init_node_ilocked(proc, new_node, fp); |
| 1408 | binder_inner_proc_unlock(proc); |
| 1409 | if (node != new_node) |
| 1410 | /* |
| 1411 | * The node was already added by another thread |
| 1412 | */ |
| 1413 | kfree(new_node); |
| 1414 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1415 | return node; |
| 1416 | } |
| 1417 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1418 | static void binder_free_node(struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1419 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1420 | kfree(node); |
| 1421 | binder_stats_deleted(BINDER_STAT_NODE); |
| 1422 | } |
| 1423 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1424 | static int binder_inc_node_nilocked(struct binder_node *node, int strong, |
| 1425 | int internal, |
| 1426 | struct list_head *target_list) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1427 | { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1428 | struct binder_proc *proc = node->proc; |
| 1429 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1430 | assert_spin_locked(&node->lock); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1431 | if (proc) |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1432 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1433 | if (strong) { |
| 1434 | if (internal) { |
| 1435 | if (target_list == NULL && |
| 1436 | node->internal_strong_refs == 0 && |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 1437 | !(node->proc && |
| 1438 | node == node->proc->context-> |
| 1439 | binder_context_mgr_node && |
| 1440 | node->has_strong_ref)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1441 | pr_err("invalid inc strong node for %d\n", |
| 1442 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1443 | return -EINVAL; |
| 1444 | } |
| 1445 | node->internal_strong_refs++; |
| 1446 | } else |
| 1447 | node->local_strong_refs++; |
| 1448 | if (!node->has_strong_ref && target_list) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1449 | binder_dequeue_work_ilocked(&node->work); |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 1450 | /* |
| 1451 | * Note: this function is the only place where we queue |
| 1452 | * directly to a thread->todo without using the |
| 1453 | * corresponding binder_enqueue_thread_work() helper |
| 1454 | * functions; in this case it's ok to not set the |
| 1455 | * process_todo flag, since we know this node work will |
| 1456 | * always be followed by other work that starts queue |
| 1457 | * processing: in case of synchronous transactions, a |
| 1458 | * BR_REPLY or BR_ERROR; in case of oneway |
| 1459 | * transactions, a BR_TRANSACTION_COMPLETE. |
| 1460 | */ |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1461 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1462 | } |
| 1463 | } else { |
| 1464 | if (!internal) |
| 1465 | node->local_weak_refs++; |
| 1466 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { |
| 1467 | if (target_list == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1468 | pr_err("invalid inc weak node for %d\n", |
| 1469 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1470 | return -EINVAL; |
| 1471 | } |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 1472 | /* |
| 1473 | * See comment above |
| 1474 | */ |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1475 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1481 | static int binder_inc_node(struct binder_node *node, int strong, int internal, |
| 1482 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1483 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1484 | int ret; |
| 1485 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1486 | binder_node_inner_lock(node); |
| 1487 | ret = binder_inc_node_nilocked(node, strong, internal, target_list); |
| 1488 | binder_node_inner_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1489 | |
| 1490 | return ret; |
| 1491 | } |
| 1492 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1493 | static bool binder_dec_node_nilocked(struct binder_node *node, |
| 1494 | int strong, int internal) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1495 | { |
| 1496 | struct binder_proc *proc = node->proc; |
| 1497 | |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1498 | assert_spin_locked(&node->lock); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1499 | if (proc) |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 1500 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1501 | if (strong) { |
| 1502 | if (internal) |
| 1503 | node->internal_strong_refs--; |
| 1504 | else |
| 1505 | node->local_strong_refs--; |
| 1506 | if (node->local_strong_refs || node->internal_strong_refs) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1507 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1508 | } else { |
| 1509 | if (!internal) |
| 1510 | node->local_weak_refs--; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1511 | if (node->local_weak_refs || node->tmp_refs || |
| 1512 | !hlist_empty(&node->refs)) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1513 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1514 | } |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1515 | |
| 1516 | if (proc && (node->has_strong_ref || node->has_weak_ref)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1517 | if (list_empty(&node->work.entry)) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1518 | binder_enqueue_work_ilocked(&node->work, &proc->todo); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 1519 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1520 | } |
| 1521 | } else { |
| 1522 | if (hlist_empty(&node->refs) && !node->local_strong_refs && |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1523 | !node->local_weak_refs && !node->tmp_refs) { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1524 | if (proc) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1525 | binder_dequeue_work_ilocked(&node->work); |
| 1526 | rb_erase(&node->rb_node, &proc->nodes); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1527 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1528 | "refless node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1529 | node->debug_id); |
| 1530 | } else { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1531 | BUG_ON(!list_empty(&node->work.entry)); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 1532 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1533 | /* |
| 1534 | * tmp_refs could have changed so |
| 1535 | * check it again |
| 1536 | */ |
| 1537 | if (node->tmp_refs) { |
| 1538 | spin_unlock(&binder_dead_nodes_lock); |
| 1539 | return false; |
| 1540 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1541 | hlist_del(&node->dead_node); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 1542 | spin_unlock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1543 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1544 | "dead node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1545 | node->debug_id); |
| 1546 | } |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1547 | return true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1548 | } |
| 1549 | } |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1550 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1551 | } |
| 1552 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1553 | static void binder_dec_node(struct binder_node *node, int strong, int internal) |
| 1554 | { |
| 1555 | bool free_node; |
| 1556 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1557 | binder_node_inner_lock(node); |
| 1558 | free_node = binder_dec_node_nilocked(node, strong, internal); |
| 1559 | binder_node_inner_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1560 | if (free_node) |
| 1561 | binder_free_node(node); |
| 1562 | } |
| 1563 | |
| 1564 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node) |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1565 | { |
| 1566 | /* |
| 1567 | * No call to binder_inc_node() is needed since we |
| 1568 | * don't need to inform userspace of any changes to |
| 1569 | * tmp_refs |
| 1570 | */ |
| 1571 | node->tmp_refs++; |
| 1572 | } |
| 1573 | |
| 1574 | /** |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1575 | * binder_inc_node_tmpref() - take a temporary reference on node |
| 1576 | * @node: node to reference |
| 1577 | * |
| 1578 | * Take reference on node to prevent the node from being freed |
| 1579 | * while referenced only by a local variable. The inner lock is |
| 1580 | * needed to serialize with the node work on the queue (which |
| 1581 | * isn't needed after the node is dead). If the node is dead |
| 1582 | * (node->proc is NULL), use binder_dead_nodes_lock to protect |
| 1583 | * node->tmp_refs against dead-node-only cases where the node |
| 1584 | * lock cannot be acquired (eg traversing the dead node list to |
| 1585 | * print nodes) |
| 1586 | */ |
| 1587 | static void binder_inc_node_tmpref(struct binder_node *node) |
| 1588 | { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1589 | binder_node_lock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1590 | if (node->proc) |
| 1591 | binder_inner_proc_lock(node->proc); |
| 1592 | else |
| 1593 | spin_lock(&binder_dead_nodes_lock); |
| 1594 | binder_inc_node_tmpref_ilocked(node); |
| 1595 | if (node->proc) |
| 1596 | binder_inner_proc_unlock(node->proc); |
| 1597 | else |
| 1598 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1599 | binder_node_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | /** |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1603 | * binder_dec_node_tmpref() - remove a temporary reference on node |
| 1604 | * @node: node to reference |
| 1605 | * |
| 1606 | * Release temporary reference on node taken via binder_inc_node_tmpref() |
| 1607 | */ |
| 1608 | static void binder_dec_node_tmpref(struct binder_node *node) |
| 1609 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1610 | bool free_node; |
| 1611 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1612 | binder_node_inner_lock(node); |
| 1613 | if (!node->proc) |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1614 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1615 | node->tmp_refs--; |
| 1616 | BUG_ON(node->tmp_refs < 0); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1617 | if (!node->proc) |
| 1618 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1619 | /* |
| 1620 | * Call binder_dec_node() to check if all refcounts are 0 |
| 1621 | * and cleanup is needed. Calling with strong=0 and internal=1 |
| 1622 | * causes no actual reference to be released in binder_dec_node(). |
| 1623 | * If that changes, a change is needed here too. |
| 1624 | */ |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1625 | free_node = binder_dec_node_nilocked(node, 0, 1); |
| 1626 | binder_node_inner_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1627 | if (free_node) |
| 1628 | binder_free_node(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | static void binder_put_node(struct binder_node *node) |
| 1632 | { |
| 1633 | binder_dec_node_tmpref(node); |
| 1634 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1635 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1636 | static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc, |
| 1637 | u32 desc, bool need_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1638 | { |
| 1639 | struct rb_node *n = proc->refs_by_desc.rb_node; |
| 1640 | struct binder_ref *ref; |
| 1641 | |
| 1642 | while (n) { |
| 1643 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1644 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1645 | if (desc < ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1646 | n = n->rb_left; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1647 | } else if (desc > ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1648 | n = n->rb_right; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1649 | } else if (need_strong_ref && !ref->data.strong) { |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1650 | binder_user_error("tried to use weak ref as strong ref\n"); |
| 1651 | return NULL; |
| 1652 | } else { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1653 | return ref; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1654 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1655 | } |
| 1656 | return NULL; |
| 1657 | } |
| 1658 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1659 | /** |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1660 | * 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] | 1661 | * @proc: binder_proc that owns the ref |
| 1662 | * @node: binder_node of target |
| 1663 | * @new_ref: newly allocated binder_ref to be initialized or %NULL |
| 1664 | * |
| 1665 | * Look up the ref for the given node and return it if it exists |
| 1666 | * |
| 1667 | * If it doesn't exist and the caller provides a newly allocated |
| 1668 | * ref, initialize the fields of the newly allocated ref and insert |
| 1669 | * into the given proc rb_trees and node refs list. |
| 1670 | * |
| 1671 | * Return: the ref for node. It is possible that another thread |
| 1672 | * allocated/initialized the ref first in which case the |
| 1673 | * returned ref would be different than the passed-in |
| 1674 | * new_ref. new_ref must be kfree'd by the caller in |
| 1675 | * this case. |
| 1676 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1677 | static struct binder_ref *binder_get_ref_for_node_olocked( |
| 1678 | struct binder_proc *proc, |
| 1679 | struct binder_node *node, |
| 1680 | struct binder_ref *new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1681 | { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1682 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1683 | struct rb_node **p = &proc->refs_by_node.rb_node; |
| 1684 | struct rb_node *parent = NULL; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1685 | struct binder_ref *ref; |
| 1686 | struct rb_node *n; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1687 | |
| 1688 | while (*p) { |
| 1689 | parent = *p; |
| 1690 | ref = rb_entry(parent, struct binder_ref, rb_node_node); |
| 1691 | |
| 1692 | if (node < ref->node) |
| 1693 | p = &(*p)->rb_left; |
| 1694 | else if (node > ref->node) |
| 1695 | p = &(*p)->rb_right; |
| 1696 | else |
| 1697 | return ref; |
| 1698 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1699 | if (!new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1700 | return NULL; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1701 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1702 | binder_stats_created(BINDER_STAT_REF); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1703 | new_ref->data.debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1704 | new_ref->proc = proc; |
| 1705 | new_ref->node = node; |
| 1706 | rb_link_node(&new_ref->rb_node_node, parent, p); |
| 1707 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); |
| 1708 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1709 | 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] | 1710 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 1711 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1712 | if (ref->data.desc > new_ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1713 | break; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1714 | new_ref->data.desc = ref->data.desc + 1; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | p = &proc->refs_by_desc.rb_node; |
| 1718 | while (*p) { |
| 1719 | parent = *p; |
| 1720 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); |
| 1721 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1722 | if (new_ref->data.desc < ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1723 | p = &(*p)->rb_left; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1724 | else if (new_ref->data.desc > ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1725 | p = &(*p)->rb_right; |
| 1726 | else |
| 1727 | BUG(); |
| 1728 | } |
| 1729 | rb_link_node(&new_ref->rb_node_desc, parent, p); |
| 1730 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1731 | |
| 1732 | binder_node_lock(node); |
Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1733 | hlist_add_head(&new_ref->node_entry, &node->refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1734 | |
Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1735 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1736 | "%d new ref %d desc %d for node %d\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1737 | proc->pid, new_ref->data.debug_id, new_ref->data.desc, |
Todd Kjos | 4cbe575 | 2017-05-01 17:21:51 -0700 | [diff] [blame] | 1738 | node->debug_id); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1739 | binder_node_unlock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1740 | return new_ref; |
| 1741 | } |
| 1742 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1743 | static void binder_cleanup_ref_olocked(struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1744 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1745 | bool delete_node = false; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1746 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1747 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1748 | "%d delete ref %d desc %d for node %d\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1749 | ref->proc->pid, ref->data.debug_id, ref->data.desc, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1750 | ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1751 | |
| 1752 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); |
| 1753 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1754 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1755 | binder_node_inner_lock(ref->node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1756 | if (ref->data.strong) |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1757 | binder_dec_node_nilocked(ref->node, 1, 1); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1758 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1759 | hlist_del(&ref->node_entry); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 1760 | delete_node = binder_dec_node_nilocked(ref->node, 0, 1); |
| 1761 | binder_node_inner_unlock(ref->node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1762 | /* |
| 1763 | * Clear ref->node unless we want the caller to free the node |
| 1764 | */ |
| 1765 | if (!delete_node) { |
| 1766 | /* |
| 1767 | * The caller uses ref->node to determine |
| 1768 | * whether the node needs to be freed. Clear |
| 1769 | * it since the node is still alive. |
| 1770 | */ |
| 1771 | ref->node = NULL; |
| 1772 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1773 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1774 | if (ref->death) { |
| 1775 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1776 | "%d delete ref %d desc %d has death notification\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1777 | ref->proc->pid, ref->data.debug_id, |
| 1778 | ref->data.desc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 1779 | binder_dequeue_work(ref->proc, &ref->death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1780 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 1781 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1782 | binder_stats_deleted(BINDER_STAT_REF); |
| 1783 | } |
| 1784 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1785 | /** |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1786 | * binder_inc_ref_olocked() - increment the ref for given handle |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1787 | * @ref: ref to be incremented |
| 1788 | * @strong: if true, strong increment, else weak |
| 1789 | * @target_list: list to queue node work on |
| 1790 | * |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1791 | * Increment the ref. @ref->proc->outer_lock must be held on entry |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1792 | * |
| 1793 | * Return: 0, if successful, else errno |
| 1794 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1795 | static int binder_inc_ref_olocked(struct binder_ref *ref, int strong, |
| 1796 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1797 | { |
| 1798 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1799 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1800 | if (strong) { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1801 | if (ref->data.strong == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1802 | ret = binder_inc_node(ref->node, 1, 1, target_list); |
| 1803 | if (ret) |
| 1804 | return ret; |
| 1805 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1806 | ref->data.strong++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1807 | } else { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1808 | if (ref->data.weak == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1809 | ret = binder_inc_node(ref->node, 0, 1, target_list); |
| 1810 | if (ret) |
| 1811 | return ret; |
| 1812 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1813 | ref->data.weak++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1814 | } |
| 1815 | return 0; |
| 1816 | } |
| 1817 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1818 | /** |
| 1819 | * binder_dec_ref() - dec the ref for given handle |
| 1820 | * @ref: ref to be decremented |
| 1821 | * @strong: if true, strong decrement, else weak |
| 1822 | * |
| 1823 | * Decrement the ref. |
| 1824 | * |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1825 | * Return: true if ref is cleaned up and ready to be freed |
| 1826 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1827 | 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] | 1828 | { |
| 1829 | if (strong) { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1830 | if (ref->data.strong == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1831 | 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] | 1832 | ref->proc->pid, ref->data.debug_id, |
| 1833 | ref->data.desc, ref->data.strong, |
| 1834 | ref->data.weak); |
| 1835 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1836 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1837 | ref->data.strong--; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1838 | if (ref->data.strong == 0) |
| 1839 | binder_dec_node(ref->node, strong, 1); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1840 | } else { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1841 | if (ref->data.weak == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1842 | 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] | 1843 | ref->proc->pid, ref->data.debug_id, |
| 1844 | ref->data.desc, ref->data.strong, |
| 1845 | ref->data.weak); |
| 1846 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1847 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1848 | ref->data.weak--; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1849 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1850 | if (ref->data.strong == 0 && ref->data.weak == 0) { |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1851 | binder_cleanup_ref_olocked(ref); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1852 | return true; |
| 1853 | } |
| 1854 | return false; |
| 1855 | } |
| 1856 | |
| 1857 | /** |
| 1858 | * binder_get_node_from_ref() - get the node from the given proc/desc |
| 1859 | * @proc: proc containing the ref |
| 1860 | * @desc: the handle associated with the ref |
| 1861 | * @need_strong_ref: if true, only return node if ref is strong |
| 1862 | * @rdata: the id/refcount data for the ref |
| 1863 | * |
| 1864 | * Given a proc and ref handle, return the associated binder_node |
| 1865 | * |
| 1866 | * Return: a binder_node or NULL if not found or not strong when strong required |
| 1867 | */ |
| 1868 | static struct binder_node *binder_get_node_from_ref( |
| 1869 | struct binder_proc *proc, |
| 1870 | u32 desc, bool need_strong_ref, |
| 1871 | struct binder_ref_data *rdata) |
| 1872 | { |
| 1873 | struct binder_node *node; |
| 1874 | struct binder_ref *ref; |
| 1875 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1876 | binder_proc_lock(proc); |
| 1877 | ref = binder_get_ref_olocked(proc, desc, need_strong_ref); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1878 | if (!ref) |
| 1879 | goto err_no_ref; |
| 1880 | node = ref->node; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 1881 | /* |
| 1882 | * Take an implicit reference on the node to ensure |
| 1883 | * it stays alive until the call to binder_put_node() |
| 1884 | */ |
| 1885 | binder_inc_node_tmpref(node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1886 | if (rdata) |
| 1887 | *rdata = ref->data; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1888 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1889 | |
| 1890 | return node; |
| 1891 | |
| 1892 | err_no_ref: |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1893 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1894 | return NULL; |
| 1895 | } |
| 1896 | |
| 1897 | /** |
| 1898 | * binder_free_ref() - free the binder_ref |
| 1899 | * @ref: ref to free |
| 1900 | * |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1901 | * Free the binder_ref. Free the binder_node indicated by ref->node |
| 1902 | * (if non-NULL) and the binder_ref_death indicated by ref->death. |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1903 | */ |
| 1904 | static void binder_free_ref(struct binder_ref *ref) |
| 1905 | { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 1906 | if (ref->node) |
| 1907 | binder_free_node(ref->node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1908 | kfree(ref->death); |
| 1909 | kfree(ref); |
| 1910 | } |
| 1911 | |
| 1912 | /** |
| 1913 | * binder_update_ref_for_handle() - inc/dec the ref for given handle |
| 1914 | * @proc: proc containing the ref |
| 1915 | * @desc: the handle associated with the ref |
| 1916 | * @increment: true=inc reference, false=dec reference |
| 1917 | * @strong: true=strong reference, false=weak reference |
| 1918 | * @rdata: the id/refcount data for the ref |
| 1919 | * |
| 1920 | * Given a proc and ref handle, increment or decrement the ref |
| 1921 | * according to "increment" arg. |
| 1922 | * |
| 1923 | * Return: 0 if successful, else errno |
| 1924 | */ |
| 1925 | static int binder_update_ref_for_handle(struct binder_proc *proc, |
| 1926 | uint32_t desc, bool increment, bool strong, |
| 1927 | struct binder_ref_data *rdata) |
| 1928 | { |
| 1929 | int ret = 0; |
| 1930 | struct binder_ref *ref; |
| 1931 | bool delete_ref = false; |
| 1932 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1933 | binder_proc_lock(proc); |
| 1934 | ref = binder_get_ref_olocked(proc, desc, strong); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1935 | if (!ref) { |
| 1936 | ret = -EINVAL; |
| 1937 | goto err_no_ref; |
| 1938 | } |
| 1939 | if (increment) |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1940 | ret = binder_inc_ref_olocked(ref, strong, NULL); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1941 | else |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1942 | delete_ref = binder_dec_ref_olocked(ref, strong); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1943 | |
| 1944 | if (rdata) |
| 1945 | *rdata = ref->data; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1946 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1947 | |
| 1948 | if (delete_ref) |
| 1949 | binder_free_ref(ref); |
| 1950 | return ret; |
| 1951 | |
| 1952 | err_no_ref: |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1953 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 1954 | return ret; |
| 1955 | } |
| 1956 | |
| 1957 | /** |
| 1958 | * binder_dec_ref_for_handle() - dec the ref for given handle |
| 1959 | * @proc: proc containing the ref |
| 1960 | * @desc: the handle associated with the ref |
| 1961 | * @strong: true=strong reference, false=weak reference |
| 1962 | * @rdata: the id/refcount data for the ref |
| 1963 | * |
| 1964 | * Just calls binder_update_ref_for_handle() to decrement the ref. |
| 1965 | * |
| 1966 | * Return: 0 if successful, else errno |
| 1967 | */ |
| 1968 | static int binder_dec_ref_for_handle(struct binder_proc *proc, |
| 1969 | uint32_t desc, bool strong, struct binder_ref_data *rdata) |
| 1970 | { |
| 1971 | return binder_update_ref_for_handle(proc, desc, false, strong, rdata); |
| 1972 | } |
| 1973 | |
| 1974 | |
| 1975 | /** |
| 1976 | * binder_inc_ref_for_node() - increment the ref for given proc/node |
| 1977 | * @proc: proc containing the ref |
| 1978 | * @node: target node |
| 1979 | * @strong: true=strong reference, false=weak reference |
| 1980 | * @target_list: worklist to use if node is incremented |
| 1981 | * @rdata: the id/refcount data for the ref |
| 1982 | * |
| 1983 | * Given a proc and node, increment the ref. Create the ref if it |
| 1984 | * doesn't already exist |
| 1985 | * |
| 1986 | * Return: 0 if successful, else errno |
| 1987 | */ |
| 1988 | static int binder_inc_ref_for_node(struct binder_proc *proc, |
| 1989 | struct binder_node *node, |
| 1990 | bool strong, |
| 1991 | struct list_head *target_list, |
| 1992 | struct binder_ref_data *rdata) |
| 1993 | { |
| 1994 | struct binder_ref *ref; |
| 1995 | struct binder_ref *new_ref = NULL; |
| 1996 | int ret = 0; |
| 1997 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 1998 | binder_proc_lock(proc); |
| 1999 | ref = binder_get_ref_for_node_olocked(proc, node, NULL); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2000 | if (!ref) { |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2001 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2002 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
| 2003 | if (!new_ref) |
| 2004 | return -ENOMEM; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2005 | binder_proc_lock(proc); |
| 2006 | ref = binder_get_ref_for_node_olocked(proc, node, new_ref); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2007 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2008 | ret = binder_inc_ref_olocked(ref, strong, target_list); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2009 | *rdata = ref->data; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 2010 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2011 | if (new_ref && ref != new_ref) |
| 2012 | /* |
| 2013 | * Another thread created the ref first so |
| 2014 | * free the one we allocated |
| 2015 | */ |
| 2016 | kfree(new_ref); |
| 2017 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2018 | } |
| 2019 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2020 | static void binder_pop_transaction_ilocked(struct binder_thread *target_thread, |
| 2021 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2022 | { |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2023 | BUG_ON(!target_thread); |
Martijn Coenen | ed32335 | 2017-07-27 23:52:24 +0200 | [diff] [blame] | 2024 | assert_spin_locked(&target_thread->proc->inner_lock); |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2025 | BUG_ON(target_thread->transaction_stack != t); |
| 2026 | BUG_ON(target_thread->transaction_stack->from != target_thread); |
| 2027 | target_thread->transaction_stack = |
| 2028 | target_thread->transaction_stack->from_parent; |
| 2029 | t->from = NULL; |
| 2030 | } |
| 2031 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2032 | /** |
| 2033 | * binder_thread_dec_tmpref() - decrement thread->tmp_ref |
| 2034 | * @thread: thread to decrement |
| 2035 | * |
| 2036 | * A thread needs to be kept alive while being used to create or |
| 2037 | * handle a transaction. binder_get_txn_from() is used to safely |
| 2038 | * extract t->from from a binder_transaction and keep the thread |
| 2039 | * indicated by t->from from being freed. When done with that |
| 2040 | * binder_thread, this function is called to decrement the |
| 2041 | * tmp_ref and free if appropriate (thread has been released |
| 2042 | * and no transaction being processed by the driver) |
| 2043 | */ |
| 2044 | static void binder_thread_dec_tmpref(struct binder_thread *thread) |
| 2045 | { |
| 2046 | /* |
| 2047 | * atomic is used to protect the counter value while |
| 2048 | * it cannot reach zero or thread->is_dead is false |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2049 | */ |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2050 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2051 | atomic_dec(&thread->tmp_ref); |
| 2052 | if (thread->is_dead && !atomic_read(&thread->tmp_ref)) { |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2053 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2054 | binder_free_thread(thread); |
| 2055 | return; |
| 2056 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2057 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | /** |
| 2061 | * binder_proc_dec_tmpref() - decrement proc->tmp_ref |
| 2062 | * @proc: proc to decrement |
| 2063 | * |
| 2064 | * A binder_proc needs to be kept alive while being used to create or |
| 2065 | * handle a transaction. proc->tmp_ref is incremented when |
| 2066 | * creating a new transaction or the binder_proc is currently in-use |
| 2067 | * by threads that are being released. When done with the binder_proc, |
| 2068 | * this function is called to decrement the counter and free the |
| 2069 | * proc if appropriate (proc has been released, all threads have |
| 2070 | * been released and not currenly in-use to process a transaction). |
| 2071 | */ |
| 2072 | static void binder_proc_dec_tmpref(struct binder_proc *proc) |
| 2073 | { |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2074 | binder_inner_proc_lock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2075 | proc->tmp_ref--; |
| 2076 | if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) && |
| 2077 | !proc->tmp_ref) { |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2078 | binder_inner_proc_unlock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2079 | binder_free_proc(proc); |
| 2080 | return; |
| 2081 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 2082 | binder_inner_proc_unlock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | /** |
| 2086 | * binder_get_txn_from() - safely extract the "from" thread in transaction |
| 2087 | * @t: binder transaction for t->from |
| 2088 | * |
| 2089 | * Atomically return the "from" thread and increment the tmp_ref |
| 2090 | * count for the thread to ensure it stays alive until |
| 2091 | * binder_thread_dec_tmpref() is called. |
| 2092 | * |
| 2093 | * Return: the value of t->from |
| 2094 | */ |
| 2095 | static struct binder_thread *binder_get_txn_from( |
| 2096 | struct binder_transaction *t) |
| 2097 | { |
| 2098 | struct binder_thread *from; |
| 2099 | |
| 2100 | spin_lock(&t->lock); |
| 2101 | from = t->from; |
| 2102 | if (from) |
| 2103 | atomic_inc(&from->tmp_ref); |
| 2104 | spin_unlock(&t->lock); |
| 2105 | return from; |
| 2106 | } |
| 2107 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2108 | /** |
| 2109 | * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock |
| 2110 | * @t: binder transaction for t->from |
| 2111 | * |
| 2112 | * Same as binder_get_txn_from() except it also acquires the proc->inner_lock |
| 2113 | * to guarantee that the thread cannot be released while operating on it. |
| 2114 | * The caller must call binder_inner_proc_unlock() to release the inner lock |
| 2115 | * as well as call binder_dec_thread_txn() to release the reference. |
| 2116 | * |
| 2117 | * Return: the value of t->from |
| 2118 | */ |
| 2119 | static struct binder_thread *binder_get_txn_from_and_acq_inner( |
| 2120 | struct binder_transaction *t) |
| 2121 | { |
| 2122 | struct binder_thread *from; |
| 2123 | |
| 2124 | from = binder_get_txn_from(t); |
| 2125 | if (!from) |
| 2126 | return NULL; |
| 2127 | binder_inner_proc_lock(from->proc); |
| 2128 | if (t->from) { |
| 2129 | BUG_ON(from != t->from); |
| 2130 | return from; |
| 2131 | } |
| 2132 | binder_inner_proc_unlock(from->proc); |
| 2133 | binder_thread_dec_tmpref(from); |
| 2134 | return NULL; |
| 2135 | } |
| 2136 | |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2137 | static void binder_free_transaction(struct binder_transaction *t) |
| 2138 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2139 | if (t->buffer) |
| 2140 | t->buffer->transaction = NULL; |
| 2141 | kfree(t); |
| 2142 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 2143 | } |
| 2144 | |
| 2145 | static void binder_send_failed_reply(struct binder_transaction *t, |
| 2146 | uint32_t error_code) |
| 2147 | { |
| 2148 | struct binder_thread *target_thread; |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2149 | struct binder_transaction *next; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2150 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2151 | BUG_ON(t->flags & TF_ONE_WAY); |
| 2152 | while (1) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2153 | target_thread = binder_get_txn_from_and_acq_inner(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2154 | if (target_thread) { |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2155 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2156 | "send failed reply for transaction %d to %d:%d\n", |
| 2157 | t->debug_id, |
| 2158 | target_thread->proc->pid, |
| 2159 | target_thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2160 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2161 | binder_pop_transaction_ilocked(target_thread, t); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2162 | if (target_thread->reply_error.cmd == BR_OK) { |
| 2163 | target_thread->reply_error.cmd = error_code; |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2164 | binder_enqueue_thread_work_ilocked( |
| 2165 | target_thread, |
| 2166 | &target_thread->reply_error.work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2167 | wake_up_interruptible(&target_thread->wait); |
| 2168 | } else { |
Todd Kjos | d3a2afb | 2018-02-07 12:38:47 -0800 | [diff] [blame] | 2169 | /* |
| 2170 | * Cannot get here for normal operation, but |
| 2171 | * we can if multiple synchronous transactions |
| 2172 | * are sent without blocking for responses. |
| 2173 | * Just ignore the 2nd error in this case. |
| 2174 | */ |
| 2175 | pr_warn("Unexpected reply error: %u\n", |
| 2176 | target_thread->reply_error.cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2177 | } |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 2178 | binder_inner_proc_unlock(target_thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2179 | binder_thread_dec_tmpref(target_thread); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 2180 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2181 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2182 | } |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2183 | next = t->from_parent; |
| 2184 | |
| 2185 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2186 | "send failed reply for transaction %d, target dead\n", |
| 2187 | t->debug_id); |
| 2188 | |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 2189 | binder_free_transaction(t); |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2190 | if (next == NULL) { |
| 2191 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2192 | "reply failed, no target thread at root\n"); |
| 2193 | return; |
| 2194 | } |
| 2195 | t = next; |
| 2196 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2197 | "reply failed, no target thread -- retry %d\n", |
| 2198 | t->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2199 | } |
| 2200 | } |
| 2201 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2202 | /** |
Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 2203 | * binder_cleanup_transaction() - cleans up undelivered transaction |
| 2204 | * @t: transaction that needs to be cleaned up |
| 2205 | * @reason: reason the transaction wasn't delivered |
| 2206 | * @error_code: error to return to caller (if synchronous call) |
| 2207 | */ |
| 2208 | static void binder_cleanup_transaction(struct binder_transaction *t, |
| 2209 | const char *reason, |
| 2210 | uint32_t error_code) |
| 2211 | { |
| 2212 | if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) { |
| 2213 | binder_send_failed_reply(t, error_code); |
| 2214 | } else { |
| 2215 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 2216 | "undelivered transaction %d, %s\n", |
| 2217 | t->debug_id, reason); |
| 2218 | binder_free_transaction(t); |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | /** |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2223 | * binder_get_object() - gets object and checks for valid metadata |
| 2224 | * @proc: binder_proc owning the buffer |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2225 | * @buffer: binder_buffer that we're parsing. |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2226 | * @offset: offset in the @buffer at which to validate an object. |
| 2227 | * @object: struct binder_object to read into |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2228 | * |
| 2229 | * 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] | 2230 | * size of that object. Otherwise, it returns zero. The object |
| 2231 | * is read into the struct binder_object pointed to by @object. |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2232 | */ |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2233 | static size_t binder_get_object(struct binder_proc *proc, |
| 2234 | struct binder_buffer *buffer, |
| 2235 | unsigned long offset, |
| 2236 | struct binder_object *object) |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2237 | { |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2238 | size_t read_size; |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2239 | struct binder_object_header *hdr; |
| 2240 | size_t object_size = 0; |
| 2241 | |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2242 | read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset); |
Todd Kjos | c8ddc8c | 2019-03-19 09:53:01 -0700 | [diff] [blame] | 2243 | if (offset > buffer->data_size || read_size < sizeof(*hdr) || |
| 2244 | !IS_ALIGNED(offset, sizeof(u32))) |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2245 | return 0; |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2246 | binder_alloc_copy_from_buffer(&proc->alloc, object, buffer, |
| 2247 | offset, read_size); |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2248 | |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2249 | /* Ok, now see if we read a complete object. */ |
| 2250 | hdr = &object->hdr; |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2251 | switch (hdr->type) { |
| 2252 | case BINDER_TYPE_BINDER: |
| 2253 | case BINDER_TYPE_WEAK_BINDER: |
| 2254 | case BINDER_TYPE_HANDLE: |
| 2255 | case BINDER_TYPE_WEAK_HANDLE: |
| 2256 | object_size = sizeof(struct flat_binder_object); |
| 2257 | break; |
| 2258 | case BINDER_TYPE_FD: |
| 2259 | object_size = sizeof(struct binder_fd_object); |
| 2260 | break; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2261 | case BINDER_TYPE_PTR: |
| 2262 | object_size = sizeof(struct binder_buffer_object); |
| 2263 | break; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2264 | case BINDER_TYPE_FDA: |
| 2265 | object_size = sizeof(struct binder_fd_array_object); |
| 2266 | break; |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2267 | default: |
| 2268 | return 0; |
| 2269 | } |
| 2270 | if (offset <= buffer->data_size - object_size && |
| 2271 | buffer->data_size >= object_size) |
| 2272 | return object_size; |
| 2273 | else |
| 2274 | return 0; |
| 2275 | } |
| 2276 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2277 | /** |
| 2278 | * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer. |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2279 | * @proc: binder_proc owning the buffer |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2280 | * @b: binder_buffer containing the object |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2281 | * @object: struct binder_object to read into |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2282 | * @index: index in offset array at which the binder_buffer_object is |
| 2283 | * located |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2284 | * @start_offset: points to the start of the offset array |
| 2285 | * @object_offsetp: offset of @object read from @b |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2286 | * @num_valid: the number of valid offsets in the offset array |
| 2287 | * |
| 2288 | * Return: If @index is within the valid range of the offset array |
| 2289 | * described by @start and @num_valid, and if there's a valid |
| 2290 | * binder_buffer_object at the offset found in index @index |
| 2291 | * of the offset array, that object is returned. Otherwise, |
| 2292 | * %NULL is returned. |
| 2293 | * Note that the offset found in index @index itself is not |
| 2294 | * verified; this function assumes that @num_valid elements |
| 2295 | * from @start were previously verified to have valid offsets. |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2296 | * If @object_offsetp is non-NULL, then the offset within |
| 2297 | * @b is written to it. |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2298 | */ |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2299 | static struct binder_buffer_object *binder_validate_ptr( |
| 2300 | struct binder_proc *proc, |
| 2301 | struct binder_buffer *b, |
| 2302 | struct binder_object *object, |
| 2303 | binder_size_t index, |
| 2304 | binder_size_t start_offset, |
| 2305 | binder_size_t *object_offsetp, |
| 2306 | binder_size_t num_valid) |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2307 | { |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2308 | size_t object_size; |
| 2309 | binder_size_t object_offset; |
| 2310 | unsigned long buffer_offset; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2311 | |
| 2312 | if (index >= num_valid) |
| 2313 | return NULL; |
| 2314 | |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2315 | buffer_offset = start_offset + sizeof(binder_size_t) * index; |
| 2316 | binder_alloc_copy_from_buffer(&proc->alloc, &object_offset, |
| 2317 | b, buffer_offset, sizeof(object_offset)); |
| 2318 | object_size = binder_get_object(proc, b, object_offset, object); |
| 2319 | if (!object_size || object->hdr.type != BINDER_TYPE_PTR) |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2320 | return NULL; |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2321 | if (object_offsetp) |
| 2322 | *object_offsetp = object_offset; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2323 | |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2324 | return &object->bbo; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | /** |
| 2328 | * binder_validate_fixup() - validates pointer/fd fixups happen in order. |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2329 | * @proc: binder_proc owning the buffer |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2330 | * @b: transaction buffer |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2331 | * @objects_start_offset: offset to start of objects buffer |
| 2332 | * @buffer_obj_offset: offset to binder_buffer_object in which to fix up |
| 2333 | * @fixup_offset: start offset in @buffer to fix up |
| 2334 | * @last_obj_offset: offset to last binder_buffer_object that we fixed |
| 2335 | * @last_min_offset: minimum fixup offset in object at @last_obj_offset |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2336 | * |
| 2337 | * Return: %true if a fixup in buffer @buffer at offset @offset is |
| 2338 | * allowed. |
| 2339 | * |
| 2340 | * For safety reasons, we only allow fixups inside a buffer to happen |
| 2341 | * at increasing offsets; additionally, we only allow fixup on the last |
| 2342 | * buffer object that was verified, or one of its parents. |
| 2343 | * |
| 2344 | * Example of what is allowed: |
| 2345 | * |
| 2346 | * A |
| 2347 | * B (parent = A, offset = 0) |
| 2348 | * C (parent = A, offset = 16) |
| 2349 | * D (parent = C, offset = 0) |
| 2350 | * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset) |
| 2351 | * |
| 2352 | * Examples of what is not allowed: |
| 2353 | * |
| 2354 | * Decreasing offsets within the same parent: |
| 2355 | * A |
| 2356 | * C (parent = A, offset = 16) |
| 2357 | * B (parent = A, offset = 0) // decreasing offset within A |
| 2358 | * |
| 2359 | * Referring to a parent that wasn't the last object or any of its parents: |
| 2360 | * A |
| 2361 | * B (parent = A, offset = 0) |
| 2362 | * C (parent = A, offset = 0) |
| 2363 | * C (parent = A, offset = 16) |
| 2364 | * D (parent = B, offset = 0) // B is not A or any of A's parents |
| 2365 | */ |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2366 | static bool binder_validate_fixup(struct binder_proc *proc, |
| 2367 | struct binder_buffer *b, |
| 2368 | binder_size_t objects_start_offset, |
| 2369 | binder_size_t buffer_obj_offset, |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2370 | binder_size_t fixup_offset, |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2371 | binder_size_t last_obj_offset, |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2372 | binder_size_t last_min_offset) |
| 2373 | { |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2374 | if (!last_obj_offset) { |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2375 | /* Nothing to fix up in */ |
| 2376 | return false; |
| 2377 | } |
| 2378 | |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2379 | while (last_obj_offset != buffer_obj_offset) { |
| 2380 | unsigned long buffer_offset; |
| 2381 | struct binder_object last_object; |
| 2382 | struct binder_buffer_object *last_bbo; |
| 2383 | size_t object_size = binder_get_object(proc, b, last_obj_offset, |
| 2384 | &last_object); |
| 2385 | if (object_size != sizeof(*last_bbo)) |
| 2386 | return false; |
| 2387 | |
| 2388 | last_bbo = &last_object.bbo; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2389 | /* |
| 2390 | * Safe to retrieve the parent of last_obj, since it |
| 2391 | * was already previously verified by the driver. |
| 2392 | */ |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2393 | if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0) |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2394 | return false; |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2395 | last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t); |
| 2396 | buffer_offset = objects_start_offset + |
| 2397 | sizeof(binder_size_t) * last_bbo->parent, |
| 2398 | binder_alloc_copy_from_buffer(&proc->alloc, &last_obj_offset, |
| 2399 | b, buffer_offset, |
| 2400 | sizeof(last_obj_offset)); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2401 | } |
| 2402 | return (fixup_offset >= last_min_offset); |
| 2403 | } |
| 2404 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2405 | static void binder_transaction_buffer_release(struct binder_proc *proc, |
| 2406 | struct binder_buffer *buffer, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2407 | binder_size_t failed_at, |
| 2408 | bool is_failure) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2409 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2410 | int debug_id = buffer->debug_id; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2411 | binder_size_t off_start_offset, buffer_offset, off_end_offset; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2412 | |
| 2413 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2414 | "%d buffer release %d, size %zd-%zd, failed at %llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2415 | proc->pid, buffer->debug_id, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2416 | buffer->data_size, buffer->offsets_size, |
| 2417 | (unsigned long long)failed_at); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2418 | |
| 2419 | if (buffer->target_node) |
| 2420 | binder_dec_node(buffer->target_node, 1, 0); |
| 2421 | |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2422 | off_start_offset = ALIGN(buffer->data_size, sizeof(void *)); |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2423 | off_end_offset = is_failure ? failed_at : |
| 2424 | off_start_offset + buffer->offsets_size; |
| 2425 | for (buffer_offset = off_start_offset; buffer_offset < off_end_offset; |
| 2426 | buffer_offset += sizeof(binder_size_t)) { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2427 | struct binder_object_header *hdr; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2428 | size_t object_size; |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2429 | struct binder_object object; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2430 | binder_size_t object_offset; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2431 | |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2432 | binder_alloc_copy_from_buffer(&proc->alloc, &object_offset, |
| 2433 | buffer, buffer_offset, |
| 2434 | sizeof(object_offset)); |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2435 | object_size = binder_get_object(proc, buffer, |
| 2436 | object_offset, &object); |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2437 | if (object_size == 0) { |
| 2438 | 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] | 2439 | debug_id, (u64)object_offset, buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2440 | continue; |
| 2441 | } |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2442 | hdr = &object.hdr; |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2443 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2444 | case BINDER_TYPE_BINDER: |
| 2445 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2446 | struct flat_binder_object *fp; |
| 2447 | struct binder_node *node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2448 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2449 | fp = to_flat_binder_object(hdr); |
| 2450 | node = binder_get_node(proc, fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2451 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2452 | pr_err("transaction release %d bad node %016llx\n", |
| 2453 | debug_id, (u64)fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2454 | break; |
| 2455 | } |
| 2456 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2457 | " node %d u%016llx\n", |
| 2458 | node->debug_id, (u64)node->ptr); |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2459 | binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER, |
| 2460 | 0); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2461 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2462 | } break; |
| 2463 | case BINDER_TYPE_HANDLE: |
| 2464 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2465 | struct flat_binder_object *fp; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2466 | struct binder_ref_data rdata; |
| 2467 | int ret; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 2468 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2469 | fp = to_flat_binder_object(hdr); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2470 | ret = binder_dec_ref_for_handle(proc, fp->handle, |
| 2471 | hdr->type == BINDER_TYPE_HANDLE, &rdata); |
| 2472 | |
| 2473 | if (ret) { |
| 2474 | pr_err("transaction release %d bad handle %d, ret = %d\n", |
| 2475 | debug_id, fp->handle, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2476 | break; |
| 2477 | } |
| 2478 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2479 | " ref %d desc %d\n", |
| 2480 | rdata.debug_id, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2481 | } break; |
| 2482 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2483 | case BINDER_TYPE_FD: { |
| 2484 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
| 2485 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2486 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2487 | " fd %d\n", fp->fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2488 | if (failed_at) |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2489 | task_close_fd(proc, fp->fd); |
| 2490 | } break; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2491 | case BINDER_TYPE_PTR: |
| 2492 | /* |
| 2493 | * Nothing to do here, this will get cleaned up when the |
| 2494 | * transaction buffer gets freed |
| 2495 | */ |
| 2496 | break; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2497 | case BINDER_TYPE_FDA: { |
| 2498 | struct binder_fd_array_object *fda; |
| 2499 | struct binder_buffer_object *parent; |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2500 | struct binder_object ptr_object; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2501 | binder_size_t fda_offset; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2502 | size_t fd_index; |
| 2503 | binder_size_t fd_buf_size; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2504 | binder_size_t num_valid; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2505 | |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2506 | num_valid = (buffer_offset - off_start_offset) / |
| 2507 | sizeof(binder_size_t); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2508 | fda = to_binder_fd_array_object(hdr); |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2509 | parent = binder_validate_ptr(proc, buffer, &ptr_object, |
| 2510 | fda->parent, |
| 2511 | off_start_offset, |
| 2512 | NULL, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2513 | num_valid); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2514 | if (!parent) { |
| 2515 | pr_err("transaction release %d bad parent offset", |
| 2516 | debug_id); |
| 2517 | continue; |
| 2518 | } |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2519 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2520 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2521 | pr_err("transaction release %d invalid number of fds (%lld)\n", |
| 2522 | debug_id, (u64)fda->num_fds); |
| 2523 | continue; |
| 2524 | } |
| 2525 | if (fd_buf_size > parent->length || |
| 2526 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2527 | /* No space for all file descriptors here. */ |
| 2528 | pr_err("transaction release %d not enough space for %lld fds in buffer\n", |
| 2529 | debug_id, (u64)fda->num_fds); |
| 2530 | continue; |
| 2531 | } |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2532 | /* |
| 2533 | * the source data for binder_buffer_object is visible |
| 2534 | * to user-space and the @buffer element is the user |
| 2535 | * pointer to the buffer_object containing the fd_array. |
| 2536 | * Convert the address to an offset relative to |
| 2537 | * the base of the transaction buffer. |
| 2538 | */ |
| 2539 | fda_offset = |
| 2540 | (parent->buffer - (uintptr_t)buffer->user_data) + |
| 2541 | fda->parent_offset; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2542 | for (fd_index = 0; fd_index < fda->num_fds; |
| 2543 | fd_index++) { |
| 2544 | u32 fd; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2545 | binder_size_t offset = fda_offset + |
| 2546 | fd_index * sizeof(fd); |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2547 | |
| 2548 | binder_alloc_copy_from_buffer(&proc->alloc, |
| 2549 | &fd, |
| 2550 | buffer, |
| 2551 | offset, |
| 2552 | sizeof(fd)); |
| 2553 | task_close_fd(proc, fd); |
| 2554 | } |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2555 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2556 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 2557 | pr_err("transaction release %d bad object type %x\n", |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 2558 | debug_id, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2559 | break; |
| 2560 | } |
| 2561 | } |
| 2562 | } |
| 2563 | |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2564 | static int binder_translate_binder(struct flat_binder_object *fp, |
| 2565 | struct binder_transaction *t, |
| 2566 | struct binder_thread *thread) |
| 2567 | { |
| 2568 | struct binder_node *node; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2569 | struct binder_proc *proc = thread->proc; |
| 2570 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2571 | struct binder_ref_data rdata; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2572 | int ret = 0; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2573 | |
| 2574 | node = binder_get_node(proc, fp->binder); |
| 2575 | if (!node) { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2576 | node = binder_new_node(proc, fp); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2577 | if (!node) |
| 2578 | return -ENOMEM; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2579 | } |
| 2580 | if (fp->cookie != node->cookie) { |
| 2581 | binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n", |
| 2582 | proc->pid, thread->pid, (u64)fp->binder, |
| 2583 | node->debug_id, (u64)fp->cookie, |
| 2584 | (u64)node->cookie); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2585 | ret = -EINVAL; |
| 2586 | goto done; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2587 | } |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2588 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2589 | ret = -EPERM; |
| 2590 | goto done; |
| 2591 | } |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2592 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2593 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2594 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2595 | &thread->todo, &rdata); |
| 2596 | if (ret) |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2597 | goto done; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2598 | |
| 2599 | if (fp->hdr.type == BINDER_TYPE_BINDER) |
| 2600 | fp->hdr.type = BINDER_TYPE_HANDLE; |
| 2601 | else |
| 2602 | fp->hdr.type = BINDER_TYPE_WEAK_HANDLE; |
| 2603 | fp->binder = 0; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2604 | fp->handle = rdata.desc; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2605 | fp->cookie = 0; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2606 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2607 | trace_binder_transaction_node_to_ref(t, node, &rdata); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2608 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2609 | " node %d u%016llx -> ref %d desc %d\n", |
| 2610 | node->debug_id, (u64)node->ptr, |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2611 | rdata.debug_id, rdata.desc); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2612 | done: |
| 2613 | binder_put_node(node); |
| 2614 | return ret; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | static int binder_translate_handle(struct flat_binder_object *fp, |
| 2618 | struct binder_transaction *t, |
| 2619 | struct binder_thread *thread) |
| 2620 | { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2621 | struct binder_proc *proc = thread->proc; |
| 2622 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2623 | struct binder_node *node; |
| 2624 | struct binder_ref_data src_rdata; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2625 | int ret = 0; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2626 | |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2627 | node = binder_get_node_from_ref(proc, fp->handle, |
| 2628 | fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata); |
| 2629 | if (!node) { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2630 | binder_user_error("%d:%d got transaction with invalid handle, %d\n", |
| 2631 | proc->pid, thread->pid, fp->handle); |
| 2632 | return -EINVAL; |
| 2633 | } |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2634 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2635 | ret = -EPERM; |
| 2636 | goto done; |
| 2637 | } |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2638 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2639 | binder_node_lock(node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2640 | if (node->proc == target_proc) { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2641 | if (fp->hdr.type == BINDER_TYPE_HANDLE) |
| 2642 | fp->hdr.type = BINDER_TYPE_BINDER; |
| 2643 | else |
| 2644 | fp->hdr.type = BINDER_TYPE_WEAK_BINDER; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2645 | fp->binder = node->ptr; |
| 2646 | fp->cookie = node->cookie; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2647 | if (node->proc) |
| 2648 | binder_inner_proc_lock(node->proc); |
| 2649 | binder_inc_node_nilocked(node, |
| 2650 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2651 | 0, NULL); |
| 2652 | if (node->proc) |
| 2653 | binder_inner_proc_unlock(node->proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2654 | trace_binder_transaction_ref_to_node(t, node, &src_rdata); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2655 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2656 | " ref %d desc %d -> node %d u%016llx\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2657 | src_rdata.debug_id, src_rdata.desc, node->debug_id, |
| 2658 | (u64)node->ptr); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2659 | binder_node_unlock(node); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2660 | } else { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2661 | struct binder_ref_data dest_rdata; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2662 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 2663 | binder_node_unlock(node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2664 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2665 | fp->hdr.type == BINDER_TYPE_HANDLE, |
| 2666 | NULL, &dest_rdata); |
| 2667 | if (ret) |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2668 | goto done; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2669 | |
| 2670 | fp->binder = 0; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2671 | fp->handle = dest_rdata.desc; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2672 | fp->cookie = 0; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2673 | trace_binder_transaction_ref_to_ref(t, node, &src_rdata, |
| 2674 | &dest_rdata); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2675 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2676 | " ref %d desc %d -> ref %d desc %d (node %d)\n", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 2677 | src_rdata.debug_id, src_rdata.desc, |
| 2678 | dest_rdata.debug_id, dest_rdata.desc, |
| 2679 | node->debug_id); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2680 | } |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 2681 | done: |
| 2682 | binder_put_node(node); |
| 2683 | return ret; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | static int binder_translate_fd(int fd, |
| 2687 | struct binder_transaction *t, |
| 2688 | struct binder_thread *thread, |
| 2689 | struct binder_transaction *in_reply_to) |
| 2690 | { |
| 2691 | struct binder_proc *proc = thread->proc; |
| 2692 | struct binder_proc *target_proc = t->to_proc; |
| 2693 | int target_fd; |
| 2694 | struct file *file; |
| 2695 | int ret; |
| 2696 | bool target_allows_fd; |
| 2697 | |
| 2698 | if (in_reply_to) |
| 2699 | target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS); |
| 2700 | else |
| 2701 | target_allows_fd = t->buffer->target_node->accept_fds; |
| 2702 | if (!target_allows_fd) { |
| 2703 | binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n", |
| 2704 | proc->pid, thread->pid, |
| 2705 | in_reply_to ? "reply" : "transaction", |
| 2706 | fd); |
| 2707 | ret = -EPERM; |
| 2708 | goto err_fd_not_accepted; |
| 2709 | } |
| 2710 | |
| 2711 | file = fget(fd); |
| 2712 | if (!file) { |
| 2713 | binder_user_error("%d:%d got transaction with invalid fd, %d\n", |
| 2714 | proc->pid, thread->pid, fd); |
| 2715 | ret = -EBADF; |
| 2716 | goto err_fget; |
| 2717 | } |
| 2718 | ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file); |
| 2719 | if (ret < 0) { |
| 2720 | ret = -EPERM; |
| 2721 | goto err_security; |
| 2722 | } |
| 2723 | |
| 2724 | target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC); |
| 2725 | if (target_fd < 0) { |
| 2726 | ret = -ENOMEM; |
| 2727 | goto err_get_unused_fd; |
| 2728 | } |
| 2729 | task_fd_install(target_proc, target_fd, file); |
| 2730 | trace_binder_transaction_fd(t, fd, target_fd); |
| 2731 | binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n", |
| 2732 | fd, target_fd); |
| 2733 | |
| 2734 | return target_fd; |
| 2735 | |
| 2736 | err_get_unused_fd: |
| 2737 | err_security: |
| 2738 | fput(file); |
| 2739 | err_fget: |
| 2740 | err_fd_not_accepted: |
| 2741 | return ret; |
| 2742 | } |
| 2743 | |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2744 | static int binder_translate_fd_array(struct binder_fd_array_object *fda, |
| 2745 | struct binder_buffer_object *parent, |
| 2746 | struct binder_transaction *t, |
| 2747 | struct binder_thread *thread, |
| 2748 | struct binder_transaction *in_reply_to) |
| 2749 | { |
| 2750 | binder_size_t fdi, fd_buf_size, num_installed_fds; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2751 | binder_size_t fda_offset; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2752 | int target_fd; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2753 | struct binder_proc *proc = thread->proc; |
| 2754 | struct binder_proc *target_proc = t->to_proc; |
| 2755 | |
| 2756 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2757 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2758 | binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n", |
| 2759 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2760 | return -EINVAL; |
| 2761 | } |
| 2762 | if (fd_buf_size > parent->length || |
| 2763 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2764 | /* No space for all file descriptors here. */ |
| 2765 | binder_user_error("%d:%d not enough space to store %lld fds in buffer\n", |
| 2766 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2767 | return -EINVAL; |
| 2768 | } |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2769 | /* |
| 2770 | * the source data for binder_buffer_object is visible |
| 2771 | * to user-space and the @buffer element is the user |
| 2772 | * pointer to the buffer_object containing the fd_array. |
| 2773 | * Convert the address to an offset relative to |
| 2774 | * the base of the transaction buffer. |
| 2775 | */ |
| 2776 | fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) + |
| 2777 | fda->parent_offset; |
| 2778 | if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) { |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2779 | binder_user_error("%d:%d parent offset not aligned correctly.\n", |
| 2780 | proc->pid, thread->pid); |
| 2781 | return -EINVAL; |
| 2782 | } |
| 2783 | for (fdi = 0; fdi < fda->num_fds; fdi++) { |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2784 | u32 fd; |
Todd Kjos | 0b90a82 | 2019-03-27 16:12:31 -0700 | [diff] [blame] | 2785 | |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2786 | binder_size_t offset = fda_offset + fdi * sizeof(fd); |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2787 | |
| 2788 | binder_alloc_copy_from_buffer(&target_proc->alloc, |
| 2789 | &fd, t->buffer, |
| 2790 | offset, sizeof(fd)); |
| 2791 | target_fd = binder_translate_fd(fd, t, thread, in_reply_to); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2792 | if (target_fd < 0) |
| 2793 | goto err_translate_fd_failed; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2794 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 2795 | t->buffer, offset, |
| 2796 | &target_fd, sizeof(fd)); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2797 | } |
| 2798 | return 0; |
| 2799 | |
| 2800 | err_translate_fd_failed: |
| 2801 | /* |
| 2802 | * Failed to allocate fd or security error, free fds |
| 2803 | * installed so far. |
| 2804 | */ |
| 2805 | num_installed_fds = fdi; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2806 | for (fdi = 0; fdi < num_installed_fds; fdi++) { |
| 2807 | u32 fd; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2808 | binder_size_t offset = fda_offset + fdi * sizeof(fd); |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2809 | binder_alloc_copy_from_buffer(&target_proc->alloc, |
| 2810 | &fd, t->buffer, |
| 2811 | offset, sizeof(fd)); |
| 2812 | task_close_fd(target_proc, fd); |
| 2813 | } |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 2814 | return target_fd; |
| 2815 | } |
| 2816 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2817 | static int binder_fixup_parent(struct binder_transaction *t, |
| 2818 | struct binder_thread *thread, |
| 2819 | struct binder_buffer_object *bp, |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2820 | binder_size_t off_start_offset, |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2821 | binder_size_t num_valid, |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2822 | binder_size_t last_fixup_obj_off, |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2823 | binder_size_t last_fixup_min_off) |
| 2824 | { |
| 2825 | struct binder_buffer_object *parent; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2826 | struct binder_buffer *b = t->buffer; |
| 2827 | struct binder_proc *proc = thread->proc; |
| 2828 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2829 | struct binder_object object; |
| 2830 | binder_size_t buffer_offset; |
| 2831 | binder_size_t parent_offset; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2832 | |
| 2833 | if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT)) |
| 2834 | return 0; |
| 2835 | |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2836 | parent = binder_validate_ptr(target_proc, b, &object, bp->parent, |
| 2837 | off_start_offset, &parent_offset, |
| 2838 | num_valid); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2839 | if (!parent) { |
| 2840 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 2841 | proc->pid, thread->pid); |
| 2842 | return -EINVAL; |
| 2843 | } |
| 2844 | |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2845 | if (!binder_validate_fixup(target_proc, b, off_start_offset, |
| 2846 | parent_offset, bp->parent_offset, |
| 2847 | last_fixup_obj_off, |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2848 | last_fixup_min_off)) { |
| 2849 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 2850 | proc->pid, thread->pid); |
| 2851 | return -EINVAL; |
| 2852 | } |
| 2853 | |
| 2854 | if (parent->length < sizeof(binder_uintptr_t) || |
| 2855 | bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) { |
| 2856 | /* No space for a pointer here! */ |
| 2857 | binder_user_error("%d:%d got transaction with invalid parent offset\n", |
| 2858 | proc->pid, thread->pid); |
| 2859 | return -EINVAL; |
| 2860 | } |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2861 | buffer_offset = bp->parent_offset + |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2862 | (uintptr_t)parent->buffer - (uintptr_t)b->user_data; |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2863 | binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset, |
| 2864 | &bp->buffer, sizeof(bp->buffer)); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 2865 | |
| 2866 | return 0; |
| 2867 | } |
| 2868 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2869 | /** |
| 2870 | * binder_proc_transaction() - sends a transaction to a process and wakes it up |
| 2871 | * @t: transaction to send |
| 2872 | * @proc: process to send the transaction to |
| 2873 | * @thread: thread in @proc to send the transaction to (may be NULL) |
| 2874 | * |
| 2875 | * This function queues a transaction to the specified process. It will try |
| 2876 | * to find a thread in the target process to handle the transaction and |
| 2877 | * wake it up. If no thread is found, the work is queued to the proc |
| 2878 | * waitqueue. |
| 2879 | * |
| 2880 | * If the @thread parameter is not NULL, the transaction is always queued |
| 2881 | * to the waitlist of that specific thread. |
| 2882 | * |
| 2883 | * Return: true if the transactions was successfully queued |
| 2884 | * false if the target process or thread is dead |
| 2885 | */ |
| 2886 | static bool binder_proc_transaction(struct binder_transaction *t, |
| 2887 | struct binder_proc *proc, |
| 2888 | struct binder_thread *thread) |
| 2889 | { |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2890 | struct binder_node *node = t->buffer->target_node; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2891 | struct binder_priority node_prio; |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2892 | bool oneway = !!(t->flags & TF_ONE_WAY); |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2893 | bool pending_async = false; |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2894 | |
| 2895 | BUG_ON(!node); |
| 2896 | binder_node_lock(node); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2897 | node_prio.prio = node->min_priority; |
| 2898 | node_prio.sched_policy = node->sched_policy; |
| 2899 | |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2900 | if (oneway) { |
| 2901 | BUG_ON(thread); |
| 2902 | if (node->has_async_transaction) { |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2903 | pending_async = true; |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2904 | } else { |
Gustavo A. R. Silva | e62dd6f | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 2905 | node->has_async_transaction = true; |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2906 | } |
| 2907 | } |
| 2908 | |
| 2909 | binder_inner_proc_lock(proc); |
| 2910 | |
| 2911 | if (proc->is_dead || (thread && thread->is_dead)) { |
| 2912 | binder_inner_proc_unlock(proc); |
| 2913 | binder_node_unlock(node); |
| 2914 | return false; |
| 2915 | } |
| 2916 | |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2917 | if (!thread && !pending_async) |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2918 | thread = binder_select_thread_ilocked(proc); |
| 2919 | |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2920 | if (thread) { |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 2921 | binder_transaction_priority(thread->task, t, node_prio, |
| 2922 | node->inherit_rt); |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2923 | binder_enqueue_thread_work_ilocked(thread, &t->work); |
| 2924 | } else if (!pending_async) { |
| 2925 | binder_enqueue_work_ilocked(&t->work, &proc->todo); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2926 | } else { |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2927 | binder_enqueue_work_ilocked(&t->work, &node->async_todo); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2928 | } |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2929 | |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2930 | if (!pending_async) |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 2931 | binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */); |
| 2932 | |
| 2933 | binder_inner_proc_unlock(proc); |
| 2934 | binder_node_unlock(node); |
| 2935 | |
| 2936 | return true; |
| 2937 | } |
| 2938 | |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 2939 | /** |
| 2940 | * binder_get_node_refs_for_txn() - Get required refs on node for txn |
| 2941 | * @node: struct binder_node for which to get refs |
| 2942 | * @proc: returns @node->proc if valid |
| 2943 | * @error: if no @proc then returns BR_DEAD_REPLY |
| 2944 | * |
| 2945 | * User-space normally keeps the node alive when creating a transaction |
| 2946 | * since it has a reference to the target. The local strong ref keeps it |
| 2947 | * alive if the sending process dies before the target process processes |
| 2948 | * the transaction. If the source process is malicious or has a reference |
| 2949 | * counting bug, relying on the local strong ref can fail. |
| 2950 | * |
| 2951 | * Since user-space can cause the local strong ref to go away, we also take |
| 2952 | * a tmpref on the node to ensure it survives while we are constructing |
| 2953 | * the transaction. We also need a tmpref on the proc while we are |
| 2954 | * constructing the transaction, so we take that here as well. |
| 2955 | * |
| 2956 | * Return: The target_node with refs taken or NULL if no @node->proc is NULL. |
| 2957 | * Also sets @proc if valid. If the @node->proc is NULL indicating that the |
| 2958 | * target proc has died, @error is set to BR_DEAD_REPLY |
| 2959 | */ |
| 2960 | static struct binder_node *binder_get_node_refs_for_txn( |
| 2961 | struct binder_node *node, |
| 2962 | struct binder_proc **procp, |
| 2963 | uint32_t *error) |
| 2964 | { |
| 2965 | struct binder_node *target_node = NULL; |
| 2966 | |
| 2967 | binder_node_inner_lock(node); |
| 2968 | if (node->proc) { |
| 2969 | target_node = node; |
| 2970 | binder_inc_node_nilocked(node, 1, 0, NULL); |
| 2971 | binder_inc_node_tmpref_ilocked(node); |
| 2972 | node->proc->tmp_ref++; |
| 2973 | *procp = node->proc; |
| 2974 | } else |
| 2975 | *error = BR_DEAD_REPLY; |
| 2976 | binder_node_inner_unlock(node); |
| 2977 | |
| 2978 | return target_node; |
| 2979 | } |
| 2980 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2981 | static void binder_transaction(struct binder_proc *proc, |
| 2982 | struct binder_thread *thread, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 2983 | struct binder_transaction_data *tr, int reply, |
| 2984 | binder_size_t extra_buffers_size) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2985 | { |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 2986 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2987 | struct binder_transaction *t; |
| 2988 | struct binder_work *tcomplete; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2989 | binder_size_t buffer_offset = 0; |
| 2990 | binder_size_t off_start_offset, off_end_offset; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 2991 | binder_size_t off_min; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2992 | binder_size_t sg_buf_offset, sg_buf_end_offset; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 2993 | struct binder_proc *target_proc = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2994 | struct binder_thread *target_thread = NULL; |
| 2995 | struct binder_node *target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2996 | struct binder_transaction *in_reply_to = NULL; |
| 2997 | struct binder_transaction_log_entry *e; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 2998 | uint32_t return_error = 0; |
| 2999 | uint32_t return_error_param = 0; |
| 3000 | uint32_t return_error_line = 0; |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3001 | binder_size_t last_fixup_obj_off = 0; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3002 | binder_size_t last_fixup_min_off = 0; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 3003 | struct binder_context *context = proc->context; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3004 | int t_debug_id = atomic_inc_return(&binder_last_id); |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3005 | char *secctx = NULL; |
| 3006 | u32 secctx_sz = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3007 | |
| 3008 | e = binder_transaction_log_add(&binder_transaction_log); |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3009 | e->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3010 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); |
| 3011 | e->from_proc = proc->pid; |
| 3012 | e->from_thread = thread->pid; |
| 3013 | e->target_handle = tr->target.handle; |
| 3014 | e->data_size = tr->data_size; |
| 3015 | e->offsets_size = tr->offsets_size; |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 3016 | e->context_name = proc->context->name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3017 | |
| 3018 | if (reply) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3019 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3020 | in_reply_to = thread->transaction_stack; |
| 3021 | if (in_reply_to == NULL) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3022 | binder_inner_proc_unlock(proc); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3023 | 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] | 3024 | proc->pid, thread->pid); |
| 3025 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3026 | return_error_param = -EPROTO; |
| 3027 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3028 | goto err_empty_call_stack; |
| 3029 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3030 | if (in_reply_to->to_thread != thread) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3031 | spin_lock(&in_reply_to->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3032 | 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] | 3033 | proc->pid, thread->pid, in_reply_to->debug_id, |
| 3034 | in_reply_to->to_proc ? |
| 3035 | in_reply_to->to_proc->pid : 0, |
| 3036 | in_reply_to->to_thread ? |
| 3037 | in_reply_to->to_thread->pid : 0); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3038 | spin_unlock(&in_reply_to->lock); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3039 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3040 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3041 | return_error_param = -EPROTO; |
| 3042 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3043 | in_reply_to = NULL; |
| 3044 | goto err_bad_call_stack; |
| 3045 | } |
| 3046 | thread->transaction_stack = in_reply_to->to_parent; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3047 | binder_inner_proc_unlock(proc); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3048 | 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] | 3049 | if (target_thread == NULL) { |
| 3050 | return_error = BR_DEAD_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3051 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3052 | goto err_dead_binder; |
| 3053 | } |
| 3054 | if (target_thread->transaction_stack != in_reply_to) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3055 | 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] | 3056 | proc->pid, thread->pid, |
| 3057 | target_thread->transaction_stack ? |
| 3058 | target_thread->transaction_stack->debug_id : 0, |
| 3059 | in_reply_to->debug_id); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3060 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3061 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3062 | return_error_param = -EPROTO; |
| 3063 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3064 | in_reply_to = NULL; |
| 3065 | target_thread = NULL; |
| 3066 | goto err_dead_binder; |
| 3067 | } |
| 3068 | target_proc = target_thread->proc; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3069 | target_proc->tmp_ref++; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3070 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3071 | } else { |
| 3072 | if (tr->target.handle) { |
| 3073 | struct binder_ref *ref; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3074 | |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3075 | /* |
| 3076 | * There must already be a strong ref |
| 3077 | * on this node. If so, do a strong |
| 3078 | * increment on the node to ensure it |
| 3079 | * stays alive until the transaction is |
| 3080 | * done. |
| 3081 | */ |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3082 | binder_proc_lock(proc); |
| 3083 | ref = binder_get_ref_olocked(proc, tr->target.handle, |
| 3084 | true); |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3085 | if (ref) { |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3086 | target_node = binder_get_node_refs_for_txn( |
| 3087 | ref->node, &target_proc, |
| 3088 | &return_error); |
| 3089 | } else { |
| 3090 | binder_user_error("%d:%d got transaction to invalid handle\n", |
| 3091 | proc->pid, thread->pid); |
| 3092 | return_error = BR_FAILED_REPLY; |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3093 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3094 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3095 | } else { |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3096 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 3097 | target_node = context->binder_context_mgr_node; |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3098 | if (target_node) |
| 3099 | target_node = binder_get_node_refs_for_txn( |
| 3100 | target_node, &target_proc, |
| 3101 | &return_error); |
| 3102 | else |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3103 | return_error = BR_DEAD_REPLY; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3104 | mutex_unlock(&context->context_mgr_node_lock); |
Martijn Coenen | c4048b2 | 2018-03-28 11:14:50 +0200 | [diff] [blame] | 3105 | if (target_node && target_proc == proc) { |
| 3106 | binder_user_error("%d:%d got transaction to context manager from process owning it\n", |
| 3107 | proc->pid, thread->pid); |
| 3108 | return_error = BR_FAILED_REPLY; |
| 3109 | return_error_param = -EINVAL; |
| 3110 | return_error_line = __LINE__; |
| 3111 | goto err_invalid_target_handle; |
| 3112 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3113 | } |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3114 | if (!target_node) { |
| 3115 | /* |
| 3116 | * return_error is set above |
| 3117 | */ |
| 3118 | return_error_param = -EINVAL; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3119 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3120 | goto err_dead_binder; |
| 3121 | } |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3122 | e->to_node = target_node->debug_id; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 3123 | if (security_binder_transaction(proc->tsk, |
| 3124 | target_proc->tsk) < 0) { |
| 3125 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3126 | return_error_param = -EPERM; |
| 3127 | return_error_line = __LINE__; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 3128 | goto err_invalid_target_handle; |
| 3129 | } |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3130 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3131 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { |
| 3132 | struct binder_transaction *tmp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3133 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3134 | tmp = thread->transaction_stack; |
| 3135 | if (tmp->to_thread != thread) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3136 | spin_lock(&tmp->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3137 | 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] | 3138 | proc->pid, thread->pid, tmp->debug_id, |
| 3139 | tmp->to_proc ? tmp->to_proc->pid : 0, |
| 3140 | tmp->to_thread ? |
| 3141 | tmp->to_thread->pid : 0); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3142 | spin_unlock(&tmp->lock); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3143 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3144 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3145 | return_error_param = -EPROTO; |
| 3146 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3147 | goto err_bad_call_stack; |
| 3148 | } |
| 3149 | while (tmp) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3150 | struct binder_thread *from; |
| 3151 | |
| 3152 | spin_lock(&tmp->lock); |
| 3153 | from = tmp->from; |
| 3154 | if (from && from->proc == target_proc) { |
| 3155 | atomic_inc(&from->tmp_ref); |
| 3156 | target_thread = from; |
| 3157 | spin_unlock(&tmp->lock); |
| 3158 | break; |
| 3159 | } |
| 3160 | spin_unlock(&tmp->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3161 | tmp = tmp->from_parent; |
| 3162 | } |
| 3163 | } |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3164 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3165 | } |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3166 | if (target_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3167 | e->to_thread = target_thread->pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3168 | e->to_proc = target_proc->pid; |
| 3169 | |
| 3170 | /* TODO: reuse incoming transaction for reply */ |
| 3171 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 3172 | if (t == NULL) { |
| 3173 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3174 | return_error_param = -ENOMEM; |
| 3175 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3176 | goto err_alloc_t_failed; |
| 3177 | } |
| 3178 | binder_stats_created(BINDER_STAT_TRANSACTION); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3179 | spin_lock_init(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3180 | |
| 3181 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); |
| 3182 | if (tcomplete == NULL) { |
| 3183 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3184 | return_error_param = -ENOMEM; |
| 3185 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3186 | goto err_alloc_tcomplete_failed; |
| 3187 | } |
| 3188 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3189 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3190 | t->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3191 | |
| 3192 | if (reply) |
| 3193 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3194 | "%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] | 3195 | proc->pid, thread->pid, t->debug_id, |
| 3196 | target_proc->pid, target_thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3197 | (u64)tr->data.ptr.buffer, |
| 3198 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3199 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3200 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3201 | else |
| 3202 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3203 | "%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] | 3204 | proc->pid, thread->pid, t->debug_id, |
| 3205 | target_proc->pid, target_node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3206 | (u64)tr->data.ptr.buffer, |
| 3207 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3208 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3209 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3210 | |
| 3211 | if (!reply && !(tr->flags & TF_ONE_WAY)) |
| 3212 | t->from = thread; |
| 3213 | else |
| 3214 | t->from = NULL; |
Tair Rzayev | 57bab7c | 2014-05-31 22:43:34 +0300 | [diff] [blame] | 3215 | t->sender_euid = task_euid(proc->tsk); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3216 | t->to_proc = target_proc; |
| 3217 | t->to_thread = target_thread; |
| 3218 | t->code = tr->code; |
| 3219 | t->flags = tr->flags; |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 3220 | if (!(t->flags & TF_ONE_WAY) && |
| 3221 | binder_supported_policy(current->policy)) { |
| 3222 | /* Inherit supported policies for synchronous transactions */ |
| 3223 | t->priority.sched_policy = current->policy; |
| 3224 | t->priority.prio = current->normal_prio; |
| 3225 | } else { |
| 3226 | /* Otherwise, fall back to the default priority */ |
| 3227 | t->priority = target_proc->default_priority; |
| 3228 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3229 | |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3230 | if (target_node && target_node->txn_security_ctx) { |
| 3231 | u32 secid; |
Todd Kjos | f65c15f | 2019-04-24 12:31:18 -0700 | [diff] [blame] | 3232 | size_t added_size; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3233 | |
| 3234 | security_task_getsecid(proc->tsk, &secid); |
| 3235 | ret = security_secid_to_secctx(secid, &secctx, &secctx_sz); |
| 3236 | if (ret) { |
| 3237 | return_error = BR_FAILED_REPLY; |
| 3238 | return_error_param = ret; |
| 3239 | return_error_line = __LINE__; |
| 3240 | goto err_get_secctx_failed; |
| 3241 | } |
Todd Kjos | f65c15f | 2019-04-24 12:31:18 -0700 | [diff] [blame] | 3242 | added_size = ALIGN(secctx_sz, sizeof(u64)); |
| 3243 | extra_buffers_size += added_size; |
| 3244 | if (extra_buffers_size < added_size) { |
| 3245 | /* integer overflow of extra_buffers_size */ |
| 3246 | return_error = BR_FAILED_REPLY; |
| 3247 | return_error_param = EINVAL; |
| 3248 | return_error_line = __LINE__; |
| 3249 | goto err_bad_extra_size; |
| 3250 | } |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3251 | } |
| 3252 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3253 | trace_binder_transaction(reply, t, target_node); |
| 3254 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3255 | t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size, |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3256 | tr->offsets_size, extra_buffers_size, |
| 3257 | !reply && (t->flags & TF_ONE_WAY)); |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3258 | if (IS_ERR(t->buffer)) { |
| 3259 | /* |
| 3260 | * -ESRCH indicates VMA cleared. The target is dying. |
| 3261 | */ |
| 3262 | return_error_param = PTR_ERR(t->buffer); |
| 3263 | return_error = return_error_param == -ESRCH ? |
| 3264 | BR_DEAD_REPLY : BR_FAILED_REPLY; |
| 3265 | return_error_line = __LINE__; |
| 3266 | t->buffer = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3267 | goto err_binder_alloc_buf_failed; |
| 3268 | } |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3269 | if (secctx) { |
| 3270 | size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) + |
| 3271 | ALIGN(tr->offsets_size, sizeof(void *)) + |
| 3272 | ALIGN(extra_buffers_size, sizeof(void *)) - |
| 3273 | ALIGN(secctx_sz, sizeof(u64)); |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3274 | |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3275 | t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3276 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3277 | t->buffer, buf_offset, |
| 3278 | secctx, secctx_sz); |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3279 | security_release_secctx(secctx, secctx_sz); |
| 3280 | secctx = NULL; |
| 3281 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3282 | t->buffer->debug_id = t->debug_id; |
| 3283 | t->buffer->transaction = t; |
| 3284 | t->buffer->target_node = target_node; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3285 | trace_binder_transaction_alloc_buf(t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3286 | |
Todd Kjos | d504949 | 2019-02-08 10:35:14 -0800 | [diff] [blame] | 3287 | if (binder_alloc_copy_user_to_buffer( |
| 3288 | &target_proc->alloc, |
| 3289 | t->buffer, 0, |
| 3290 | (const void __user *) |
| 3291 | (uintptr_t)tr->data.ptr.buffer, |
| 3292 | tr->data_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3293 | binder_user_error("%d:%d got transaction with invalid data ptr\n", |
| 3294 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3295 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3296 | return_error_param = -EFAULT; |
| 3297 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3298 | goto err_copy_data_failed; |
| 3299 | } |
Todd Kjos | d504949 | 2019-02-08 10:35:14 -0800 | [diff] [blame] | 3300 | if (binder_alloc_copy_user_to_buffer( |
| 3301 | &target_proc->alloc, |
| 3302 | t->buffer, |
| 3303 | ALIGN(tr->data_size, sizeof(void *)), |
| 3304 | (const void __user *) |
| 3305 | (uintptr_t)tr->data.ptr.offsets, |
| 3306 | tr->offsets_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3307 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3308 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3309 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3310 | return_error_param = -EFAULT; |
| 3311 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3312 | goto err_copy_data_failed; |
| 3313 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3314 | if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) { |
| 3315 | binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n", |
| 3316 | proc->pid, thread->pid, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3317 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3318 | return_error_param = -EINVAL; |
| 3319 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3320 | goto err_bad_offset; |
| 3321 | } |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3322 | if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) { |
| 3323 | binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n", |
| 3324 | proc->pid, thread->pid, |
Amit Pundir | 44cbb18 | 2017-02-01 12:53:45 +0530 | [diff] [blame] | 3325 | (u64)extra_buffers_size); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3326 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3327 | return_error_param = -EINVAL; |
| 3328 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3329 | goto err_bad_offset; |
| 3330 | } |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3331 | off_start_offset = ALIGN(tr->data_size, sizeof(void *)); |
| 3332 | buffer_offset = off_start_offset; |
| 3333 | off_end_offset = off_start_offset + tr->offsets_size; |
| 3334 | sg_buf_offset = ALIGN(off_end_offset, sizeof(void *)); |
| 3335 | sg_buf_end_offset = sg_buf_offset + extra_buffers_size; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3336 | off_min = 0; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3337 | for (buffer_offset = off_start_offset; buffer_offset < off_end_offset; |
| 3338 | buffer_offset += sizeof(binder_size_t)) { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3339 | struct binder_object_header *hdr; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3340 | size_t object_size; |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3341 | struct binder_object object; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3342 | binder_size_t object_offset; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3343 | |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3344 | binder_alloc_copy_from_buffer(&target_proc->alloc, |
| 3345 | &object_offset, |
| 3346 | t->buffer, |
| 3347 | buffer_offset, |
| 3348 | sizeof(object_offset)); |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3349 | object_size = binder_get_object(target_proc, t->buffer, |
| 3350 | object_offset, &object); |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3351 | if (object_size == 0 || object_offset < off_min) { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3352 | 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] | 3353 | proc->pid, thread->pid, |
| 3354 | (u64)object_offset, |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3355 | (u64)off_min, |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3356 | (u64)t->buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3357 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3358 | return_error_param = -EINVAL; |
| 3359 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3360 | goto err_bad_offset; |
| 3361 | } |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3362 | |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3363 | hdr = &object.hdr; |
Todd Kjos | 90a570c | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3364 | off_min = object_offset + object_size; |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3365 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3366 | case BINDER_TYPE_BINDER: |
| 3367 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3368 | struct flat_binder_object *fp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3369 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3370 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3371 | ret = binder_translate_binder(fp, t, thread); |
| 3372 | if (ret < 0) { |
Christian Engelmayer | 7d42043 | 2014-05-07 21:44:53 +0200 | [diff] [blame] | 3373 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3374 | return_error_param = ret; |
| 3375 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3376 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3377 | } |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3378 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3379 | t->buffer, object_offset, |
| 3380 | fp, sizeof(*fp)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3381 | } break; |
| 3382 | case BINDER_TYPE_HANDLE: |
| 3383 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3384 | struct flat_binder_object *fp; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 3385 | |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3386 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3387 | ret = binder_translate_handle(fp, t, thread); |
| 3388 | if (ret < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3389 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3390 | return_error_param = ret; |
| 3391 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3392 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3393 | } |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3394 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3395 | t->buffer, object_offset, |
| 3396 | fp, sizeof(*fp)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3397 | } break; |
| 3398 | |
| 3399 | case BINDER_TYPE_FD: { |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3400 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3401 | int target_fd = binder_translate_fd(fp->fd, t, thread, |
| 3402 | in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3403 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3404 | if (target_fd < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3405 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3406 | return_error_param = target_fd; |
| 3407 | return_error_line = __LINE__; |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3408 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3409 | } |
Martijn Coenen | 00c8037 | 2016-07-13 12:06:49 +0200 | [diff] [blame] | 3410 | fp->pad_binder = 0; |
| 3411 | fp->fd = target_fd; |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3412 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3413 | t->buffer, object_offset, |
| 3414 | fp, sizeof(*fp)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3415 | } break; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3416 | case BINDER_TYPE_FDA: { |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3417 | struct binder_object ptr_object; |
| 3418 | binder_size_t parent_offset; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3419 | struct binder_fd_array_object *fda = |
| 3420 | to_binder_fd_array_object(hdr); |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3421 | size_t num_valid = (buffer_offset - off_start_offset) * |
| 3422 | sizeof(binder_size_t); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3423 | struct binder_buffer_object *parent = |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3424 | binder_validate_ptr(target_proc, t->buffer, |
| 3425 | &ptr_object, fda->parent, |
| 3426 | off_start_offset, |
| 3427 | &parent_offset, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3428 | num_valid); |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3429 | if (!parent) { |
| 3430 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 3431 | proc->pid, thread->pid); |
| 3432 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3433 | return_error_param = -EINVAL; |
| 3434 | return_error_line = __LINE__; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3435 | goto err_bad_parent; |
| 3436 | } |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3437 | if (!binder_validate_fixup(target_proc, t->buffer, |
| 3438 | off_start_offset, |
| 3439 | parent_offset, |
| 3440 | fda->parent_offset, |
| 3441 | last_fixup_obj_off, |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3442 | last_fixup_min_off)) { |
| 3443 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 3444 | proc->pid, thread->pid); |
| 3445 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3446 | return_error_param = -EINVAL; |
| 3447 | return_error_line = __LINE__; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3448 | goto err_bad_parent; |
| 3449 | } |
| 3450 | ret = binder_translate_fd_array(fda, parent, t, thread, |
| 3451 | in_reply_to); |
| 3452 | if (ret < 0) { |
| 3453 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3454 | return_error_param = ret; |
| 3455 | return_error_line = __LINE__; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3456 | goto err_translate_failed; |
| 3457 | } |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3458 | last_fixup_obj_off = parent_offset; |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3459 | last_fixup_min_off = |
| 3460 | fda->parent_offset + sizeof(u32) * fda->num_fds; |
| 3461 | } break; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3462 | case BINDER_TYPE_PTR: { |
| 3463 | struct binder_buffer_object *bp = |
| 3464 | to_binder_buffer_object(hdr); |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3465 | size_t buf_left = sg_buf_end_offset - sg_buf_offset; |
| 3466 | size_t num_valid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3467 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3468 | if (bp->length > buf_left) { |
| 3469 | binder_user_error("%d:%d got transaction with too large buffer\n", |
| 3470 | proc->pid, thread->pid); |
| 3471 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3472 | return_error_param = -EINVAL; |
| 3473 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3474 | goto err_bad_offset; |
| 3475 | } |
Todd Kjos | d504949 | 2019-02-08 10:35:14 -0800 | [diff] [blame] | 3476 | if (binder_alloc_copy_user_to_buffer( |
| 3477 | &target_proc->alloc, |
| 3478 | t->buffer, |
| 3479 | sg_buf_offset, |
| 3480 | (const void __user *) |
| 3481 | (uintptr_t)bp->buffer, |
| 3482 | bp->length)) { |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3483 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3484 | proc->pid, thread->pid); |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3485 | return_error_param = -EFAULT; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3486 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3487 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3488 | goto err_copy_data_failed; |
| 3489 | } |
| 3490 | /* Fixup buffer pointer to target proc address space */ |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3491 | bp->buffer = (uintptr_t) |
| 3492 | t->buffer->user_data + sg_buf_offset; |
| 3493 | sg_buf_offset += ALIGN(bp->length, sizeof(u64)); |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3494 | |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3495 | num_valid = (buffer_offset - off_start_offset) * |
| 3496 | sizeof(binder_size_t); |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3497 | ret = binder_fixup_parent(t, thread, bp, |
| 3498 | off_start_offset, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3499 | num_valid, |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3500 | last_fixup_obj_off, |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3501 | last_fixup_min_off); |
| 3502 | if (ret < 0) { |
| 3503 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3504 | return_error_param = ret; |
| 3505 | return_error_line = __LINE__; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3506 | goto err_translate_failed; |
| 3507 | } |
Todd Kjos | d73356a | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3508 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3509 | t->buffer, object_offset, |
| 3510 | bp, sizeof(*bp)); |
Todd Kjos | a2dbf9a | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3511 | last_fixup_obj_off = object_offset; |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3512 | last_fixup_min_off = 0; |
| 3513 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3514 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 3515 | 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] | 3516 | proc->pid, thread->pid, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3517 | return_error = BR_FAILED_REPLY; |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3518 | return_error_param = -EINVAL; |
| 3519 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3520 | goto err_bad_object_type; |
| 3521 | } |
| 3522 | } |
Todd Kjos | 8dedb0c | 2017-05-09 08:31:32 -0700 | [diff] [blame] | 3523 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3524 | t->work.type = BINDER_WORK_TRANSACTION; |
Todd Kjos | 8dedb0c | 2017-05-09 08:31:32 -0700 | [diff] [blame] | 3525 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3526 | if (reply) { |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3527 | binder_enqueue_thread_work(thread, tcomplete); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3528 | binder_inner_proc_lock(target_proc); |
| 3529 | if (target_thread->is_dead) { |
| 3530 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3531 | goto err_dead_proc_or_thread; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3532 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3533 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3534 | binder_pop_transaction_ilocked(target_thread, in_reply_to); |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3535 | binder_enqueue_thread_work_ilocked(target_thread, &t->work); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3536 | binder_inner_proc_unlock(target_proc); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3537 | wake_up_interruptible_sync(&target_thread->wait); |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3538 | binder_restore_priority(current, in_reply_to->saved_priority); |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 3539 | binder_free_transaction(in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3540 | } else if (!(t->flags & TF_ONE_WAY)) { |
| 3541 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3542 | binder_inner_proc_lock(proc); |
Martijn Coenen | dac2e9c | 2017-11-13 09:55:21 +0100 | [diff] [blame] | 3543 | /* |
| 3544 | * Defer the TRANSACTION_COMPLETE, so we don't return to |
| 3545 | * userspace immediately; this allows the target process to |
| 3546 | * immediately start processing this transaction, reducing |
| 3547 | * latency. We will then return the TRANSACTION_COMPLETE when |
| 3548 | * the target replies (or there is an error). |
| 3549 | */ |
| 3550 | binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3551 | t->need_reply = 1; |
| 3552 | t->from_parent = thread->transaction_stack; |
| 3553 | thread->transaction_stack = t; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3554 | binder_inner_proc_unlock(proc); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3555 | if (!binder_proc_transaction(t, target_proc, target_thread)) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 3556 | binder_inner_proc_lock(proc); |
| 3557 | binder_pop_transaction_ilocked(thread, t); |
| 3558 | binder_inner_proc_unlock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3559 | goto err_dead_proc_or_thread; |
| 3560 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3561 | } else { |
| 3562 | BUG_ON(target_node == NULL); |
| 3563 | BUG_ON(t->buffer->async_transaction != 1); |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3564 | binder_enqueue_thread_work(thread, tcomplete); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 3565 | if (!binder_proc_transaction(t, target_proc, NULL)) |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3566 | goto err_dead_proc_or_thread; |
Riley Andrews | b596881 | 2015-09-01 12:42:07 -0700 | [diff] [blame] | 3567 | } |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3568 | if (target_thread) |
| 3569 | binder_thread_dec_tmpref(target_thread); |
| 3570 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3571 | if (target_node) |
| 3572 | binder_dec_node_tmpref(target_node); |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3573 | /* |
| 3574 | * write barrier to synchronize with initialization |
| 3575 | * of log entry |
| 3576 | */ |
| 3577 | smp_wmb(); |
| 3578 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3579 | return; |
| 3580 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3581 | err_dead_proc_or_thread: |
| 3582 | return_error = BR_DEAD_REPLY; |
| 3583 | return_error_line = __LINE__; |
Xu YiPing | 86578a0 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3584 | binder_dequeue_work(proc, tcomplete); |
Martijn Coenen | d82cb8b | 2016-09-29 15:38:14 +0200 | [diff] [blame] | 3585 | err_translate_failed: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3586 | err_bad_object_type: |
| 3587 | err_bad_offset: |
Martijn Coenen | e3e0f480 | 2016-10-18 13:58:55 +0200 | [diff] [blame] | 3588 | err_bad_parent: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3589 | err_copy_data_failed: |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3590 | trace_binder_transaction_failed_buffer_release(t->buffer); |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3591 | binder_transaction_buffer_release(target_proc, t->buffer, |
| 3592 | buffer_offset, true); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3593 | if (target_node) |
| 3594 | binder_dec_node_tmpref(target_node); |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3595 | target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3596 | t->buffer->transaction = NULL; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3597 | binder_alloc_free_buf(&target_proc->alloc, t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3598 | err_binder_alloc_buf_failed: |
Todd Kjos | f65c15f | 2019-04-24 12:31:18 -0700 | [diff] [blame] | 3599 | err_bad_extra_size: |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3600 | if (secctx) |
| 3601 | security_release_secctx(secctx, secctx_sz); |
| 3602 | err_get_secctx_failed: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3603 | kfree(tcomplete); |
| 3604 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3605 | err_alloc_tcomplete_failed: |
| 3606 | kfree(t); |
| 3607 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 3608 | err_alloc_t_failed: |
| 3609 | err_bad_call_stack: |
| 3610 | err_empty_call_stack: |
| 3611 | err_dead_binder: |
| 3612 | err_invalid_target_handle: |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 3613 | if (target_thread) |
| 3614 | binder_thread_dec_tmpref(target_thread); |
| 3615 | if (target_proc) |
| 3616 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3617 | if (target_node) { |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3618 | binder_dec_node(target_node, 1, 0); |
Todd Kjos | 291d968 | 2017-09-25 08:55:09 -0700 | [diff] [blame] | 3619 | binder_dec_node_tmpref(target_node); |
| 3620 | } |
Todd Kjos | c37162d | 2017-05-26 11:56:29 -0700 | [diff] [blame] | 3621 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3622 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3623 | "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n", |
| 3624 | proc->pid, thread->pid, return_error, return_error_param, |
| 3625 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3626 | return_error_line); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3627 | |
| 3628 | { |
| 3629 | struct binder_transaction_log_entry *fe; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3630 | |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 3631 | e->return_error = return_error; |
| 3632 | e->return_error_param = return_error_param; |
| 3633 | e->return_error_line = return_error_line; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3634 | fe = binder_transaction_log_add(&binder_transaction_log_failed); |
| 3635 | *fe = *e; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 3636 | /* |
| 3637 | * write barrier to synchronize with initialization |
| 3638 | * of log entry |
| 3639 | */ |
| 3640 | smp_wmb(); |
| 3641 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
| 3642 | WRITE_ONCE(fe->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3643 | } |
| 3644 | |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3645 | BUG_ON(thread->return_error.cmd != BR_OK); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3646 | if (in_reply_to) { |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3647 | binder_restore_priority(current, in_reply_to->saved_priority); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3648 | thread->return_error.cmd = BR_TRANSACTION_COMPLETE; |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3649 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3650 | binder_send_failed_reply(in_reply_to, return_error); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3651 | } else { |
| 3652 | thread->return_error.cmd = return_error; |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3653 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3654 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3655 | } |
| 3656 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3657 | static int binder_thread_write(struct binder_proc *proc, |
| 3658 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3659 | binder_uintptr_t binder_buffer, size_t size, |
| 3660 | binder_size_t *consumed) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3661 | { |
| 3662 | uint32_t cmd; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 3663 | struct binder_context *context = proc->context; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3664 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3665 | void __user *ptr = buffer + *consumed; |
| 3666 | void __user *end = buffer + size; |
| 3667 | |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 3668 | while (ptr < end && thread->return_error.cmd == BR_OK) { |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3669 | int ret; |
| 3670 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3671 | if (get_user(cmd, (uint32_t __user *)ptr)) |
| 3672 | return -EFAULT; |
| 3673 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3674 | trace_binder_command(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3675 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 3676 | atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]); |
| 3677 | atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]); |
| 3678 | atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3679 | } |
| 3680 | switch (cmd) { |
| 3681 | case BC_INCREFS: |
| 3682 | case BC_ACQUIRE: |
| 3683 | case BC_RELEASE: |
| 3684 | case BC_DECREFS: { |
| 3685 | uint32_t target; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3686 | const char *debug_string; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3687 | bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE; |
| 3688 | bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE; |
| 3689 | struct binder_ref_data rdata; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3690 | |
| 3691 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3692 | return -EFAULT; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3693 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3694 | ptr += sizeof(uint32_t); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3695 | ret = -1; |
| 3696 | if (increment && !target) { |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3697 | struct binder_node *ctx_mgr_node; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3698 | mutex_lock(&context->context_mgr_node_lock); |
| 3699 | ctx_mgr_node = context->binder_context_mgr_node; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3700 | if (ctx_mgr_node) |
| 3701 | ret = binder_inc_ref_for_node( |
| 3702 | proc, ctx_mgr_node, |
| 3703 | strong, NULL, &rdata); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 3704 | mutex_unlock(&context->context_mgr_node_lock); |
| 3705 | } |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3706 | if (ret) |
| 3707 | ret = binder_update_ref_for_handle( |
| 3708 | proc, target, increment, strong, |
| 3709 | &rdata); |
| 3710 | if (!ret && rdata.desc != target) { |
| 3711 | binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n", |
| 3712 | proc->pid, thread->pid, |
| 3713 | target, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3714 | } |
| 3715 | switch (cmd) { |
| 3716 | case BC_INCREFS: |
| 3717 | debug_string = "IncRefs"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3718 | break; |
| 3719 | case BC_ACQUIRE: |
| 3720 | debug_string = "Acquire"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3721 | break; |
| 3722 | case BC_RELEASE: |
| 3723 | debug_string = "Release"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3724 | break; |
| 3725 | case BC_DECREFS: |
| 3726 | default: |
| 3727 | debug_string = "DecRefs"; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3728 | break; |
| 3729 | } |
| 3730 | if (ret) { |
| 3731 | binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n", |
| 3732 | proc->pid, thread->pid, debug_string, |
| 3733 | strong, target, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3734 | break; |
| 3735 | } |
| 3736 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3737 | "%d:%d %s ref %d desc %d s %d w %d\n", |
| 3738 | proc->pid, thread->pid, debug_string, |
| 3739 | rdata.debug_id, rdata.desc, rdata.strong, |
| 3740 | rdata.weak); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3741 | break; |
| 3742 | } |
| 3743 | case BC_INCREFS_DONE: |
| 3744 | case BC_ACQUIRE_DONE: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3745 | binder_uintptr_t node_ptr; |
| 3746 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3747 | struct binder_node *node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3748 | bool free_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3749 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3750 | if (get_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3751 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3752 | ptr += sizeof(binder_uintptr_t); |
| 3753 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3754 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3755 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3756 | node = binder_get_node(proc, node_ptr); |
| 3757 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3758 | binder_user_error("%d:%d %s u%016llx no match\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3759 | proc->pid, thread->pid, |
| 3760 | cmd == BC_INCREFS_DONE ? |
| 3761 | "BC_INCREFS_DONE" : |
| 3762 | "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3763 | (u64)node_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3764 | break; |
| 3765 | } |
| 3766 | if (cookie != node->cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3767 | 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] | 3768 | proc->pid, thread->pid, |
| 3769 | cmd == BC_INCREFS_DONE ? |
| 3770 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3771 | (u64)node_ptr, node->debug_id, |
| 3772 | (u64)cookie, (u64)node->cookie); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3773 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3774 | break; |
| 3775 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3776 | binder_node_inner_lock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3777 | if (cmd == BC_ACQUIRE_DONE) { |
| 3778 | if (node->pending_strong_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3779 | 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] | 3780 | proc->pid, thread->pid, |
| 3781 | node->debug_id); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3782 | binder_node_inner_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3783 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3784 | break; |
| 3785 | } |
| 3786 | node->pending_strong_ref = 0; |
| 3787 | } else { |
| 3788 | if (node->pending_weak_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3789 | 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] | 3790 | proc->pid, thread->pid, |
| 3791 | node->debug_id); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3792 | binder_node_inner_unlock(node); |
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 | } |
| 3796 | node->pending_weak_ref = 0; |
| 3797 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3798 | free_node = binder_dec_node_nilocked(node, |
| 3799 | cmd == BC_ACQUIRE_DONE, 0); |
| 3800 | WARN_ON(free_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3801 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3802 | "%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] | 3803 | proc->pid, thread->pid, |
| 3804 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3805 | node->debug_id, node->local_strong_refs, |
| 3806 | node->local_weak_refs, node->tmp_refs); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3807 | binder_node_inner_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 3808 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3809 | break; |
| 3810 | } |
| 3811 | case BC_ATTEMPT_ACQUIRE: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3812 | pr_err("BC_ATTEMPT_ACQUIRE not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3813 | return -EINVAL; |
| 3814 | case BC_ACQUIRE_RESULT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3815 | pr_err("BC_ACQUIRE_RESULT not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3816 | return -EINVAL; |
| 3817 | |
| 3818 | case BC_FREE_BUFFER: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3819 | binder_uintptr_t data_ptr; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3820 | struct binder_buffer *buffer; |
| 3821 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3822 | if (get_user(data_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3823 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3824 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3825 | |
Todd Kjos | 076072a | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 3826 | buffer = binder_alloc_prepare_to_free(&proc->alloc, |
| 3827 | data_ptr); |
Todd Kjos | d29b73e | 2018-11-06 15:55:32 -0800 | [diff] [blame] | 3828 | if (IS_ERR_OR_NULL(buffer)) { |
| 3829 | if (PTR_ERR(buffer) == -EPERM) { |
| 3830 | binder_user_error( |
| 3831 | "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n", |
| 3832 | proc->pid, thread->pid, |
| 3833 | (u64)data_ptr); |
| 3834 | } else { |
| 3835 | binder_user_error( |
| 3836 | "%d:%d BC_FREE_BUFFER u%016llx no match\n", |
| 3837 | proc->pid, thread->pid, |
| 3838 | (u64)data_ptr); |
| 3839 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3840 | break; |
| 3841 | } |
| 3842 | binder_debug(BINDER_DEBUG_FREE_BUFFER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3843 | "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n", |
| 3844 | proc->pid, thread->pid, (u64)data_ptr, |
| 3845 | buffer->debug_id, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3846 | buffer->transaction ? "active" : "finished"); |
| 3847 | |
| 3848 | if (buffer->transaction) { |
| 3849 | buffer->transaction->buffer = NULL; |
| 3850 | buffer->transaction = NULL; |
| 3851 | } |
| 3852 | if (buffer->async_transaction && buffer->target_node) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3853 | struct binder_node *buf_node; |
| 3854 | struct binder_work *w; |
| 3855 | |
| 3856 | buf_node = buffer->target_node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3857 | binder_node_inner_lock(buf_node); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3858 | BUG_ON(!buf_node->has_async_transaction); |
| 3859 | BUG_ON(buf_node->proc != proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3860 | w = binder_dequeue_work_head_ilocked( |
| 3861 | &buf_node->async_todo); |
Martijn Coenen | 4501c04 | 2017-08-10 13:56:16 +0200 | [diff] [blame] | 3862 | if (!w) { |
Gustavo A. R. Silva | e62dd6f | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 3863 | buf_node->has_async_transaction = false; |
Martijn Coenen | 4501c04 | 2017-08-10 13:56:16 +0200 | [diff] [blame] | 3864 | } else { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 3865 | binder_enqueue_work_ilocked( |
Martijn Coenen | 4501c04 | 2017-08-10 13:56:16 +0200 | [diff] [blame] | 3866 | w, &proc->todo); |
| 3867 | binder_wakeup_proc_ilocked(proc); |
| 3868 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 3869 | binder_node_inner_unlock(buf_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3870 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3871 | trace_binder_transaction_buffer_release(buffer); |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3872 | binder_transaction_buffer_release(proc, buffer, 0, false); |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 3873 | binder_alloc_free_buf(&proc->alloc, buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3874 | break; |
| 3875 | } |
| 3876 | |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 3877 | case BC_TRANSACTION_SG: |
| 3878 | case BC_REPLY_SG: { |
| 3879 | struct binder_transaction_data_sg tr; |
| 3880 | |
| 3881 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3882 | return -EFAULT; |
| 3883 | ptr += sizeof(tr); |
| 3884 | binder_transaction(proc, thread, &tr.transaction_data, |
| 3885 | cmd == BC_REPLY_SG, tr.buffers_size); |
| 3886 | break; |
| 3887 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3888 | case BC_TRANSACTION: |
| 3889 | case BC_REPLY: { |
| 3890 | struct binder_transaction_data tr; |
| 3891 | |
| 3892 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3893 | return -EFAULT; |
| 3894 | ptr += sizeof(tr); |
Martijn Coenen | 59878d7 | 2016-09-30 14:05:40 +0200 | [diff] [blame] | 3895 | binder_transaction(proc, thread, &tr, |
| 3896 | cmd == BC_REPLY, 0); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3897 | break; |
| 3898 | } |
| 3899 | |
| 3900 | case BC_REGISTER_LOOPER: |
| 3901 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3902 | "%d:%d BC_REGISTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3903 | proc->pid, thread->pid); |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 3904 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3905 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { |
| 3906 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3907 | 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] | 3908 | proc->pid, thread->pid); |
| 3909 | } else if (proc->requested_threads == 0) { |
| 3910 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3911 | 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] | 3912 | proc->pid, thread->pid); |
| 3913 | } else { |
| 3914 | proc->requested_threads--; |
| 3915 | proc->requested_threads_started++; |
| 3916 | } |
| 3917 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 3918 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3919 | break; |
| 3920 | case BC_ENTER_LOOPER: |
| 3921 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3922 | "%d:%d BC_ENTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3923 | proc->pid, thread->pid); |
| 3924 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { |
| 3925 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3926 | 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] | 3927 | proc->pid, thread->pid); |
| 3928 | } |
| 3929 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; |
| 3930 | break; |
| 3931 | case BC_EXIT_LOOPER: |
| 3932 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3933 | "%d:%d BC_EXIT_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3934 | proc->pid, thread->pid); |
| 3935 | thread->looper |= BINDER_LOOPER_STATE_EXITED; |
| 3936 | break; |
| 3937 | |
| 3938 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 3939 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 3940 | uint32_t target; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3941 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3942 | struct binder_ref *ref; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3943 | struct binder_ref_death *death = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3944 | |
| 3945 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3946 | return -EFAULT; |
| 3947 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3948 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3949 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3950 | ptr += sizeof(binder_uintptr_t); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3951 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3952 | /* |
| 3953 | * Allocate memory for death notification |
| 3954 | * before taking lock |
| 3955 | */ |
| 3956 | death = kzalloc(sizeof(*death), GFP_KERNEL); |
| 3957 | if (death == NULL) { |
| 3958 | WARN_ON(thread->return_error.cmd != |
| 3959 | BR_OK); |
| 3960 | thread->return_error.cmd = BR_ERROR; |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3961 | binder_enqueue_thread_work( |
| 3962 | thread, |
| 3963 | &thread->return_error.work); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3964 | binder_debug( |
| 3965 | BINDER_DEBUG_FAILED_TRANSACTION, |
| 3966 | "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n", |
| 3967 | proc->pid, thread->pid); |
| 3968 | break; |
| 3969 | } |
| 3970 | } |
| 3971 | binder_proc_lock(proc); |
| 3972 | ref = binder_get_ref_olocked(proc, target, false); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3973 | if (ref == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3974 | binder_user_error("%d:%d %s invalid ref %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3975 | proc->pid, thread->pid, |
| 3976 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3977 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3978 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 3979 | target); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 3980 | binder_proc_unlock(proc); |
| 3981 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3982 | break; |
| 3983 | } |
| 3984 | |
| 3985 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3986 | "%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] | 3987 | proc->pid, thread->pid, |
| 3988 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3989 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3990 | "BC_CLEAR_DEATH_NOTIFICATION", |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 3991 | (u64)cookie, ref->data.debug_id, |
| 3992 | ref->data.desc, ref->data.strong, |
| 3993 | ref->data.weak, ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3994 | |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 3995 | binder_node_lock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3996 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3997 | if (ref->death) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3998 | 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] | 3999 | proc->pid, thread->pid); |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4000 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 4001 | binder_proc_unlock(proc); |
| 4002 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4003 | break; |
| 4004 | } |
| 4005 | binder_stats_created(BINDER_STAT_DEATH); |
| 4006 | INIT_LIST_HEAD(&death->work.entry); |
| 4007 | death->cookie = cookie; |
| 4008 | ref->death = death; |
| 4009 | if (ref->node->proc == NULL) { |
| 4010 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
Martijn Coenen | 3bdbe4c | 2017-08-10 13:50:52 +0200 | [diff] [blame] | 4011 | |
| 4012 | binder_inner_proc_lock(proc); |
| 4013 | binder_enqueue_work_ilocked( |
| 4014 | &ref->death->work, &proc->todo); |
| 4015 | binder_wakeup_proc_ilocked(proc); |
| 4016 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4017 | } |
| 4018 | } else { |
| 4019 | if (ref->death == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4020 | 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] | 4021 | proc->pid, thread->pid); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -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); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4024 | break; |
| 4025 | } |
| 4026 | death = ref->death; |
| 4027 | if (death->cookie != cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4028 | 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] | 4029 | proc->pid, thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4030 | (u64)death->cookie, |
| 4031 | (u64)cookie); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4032 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 4033 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4034 | break; |
| 4035 | } |
| 4036 | ref->death = NULL; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4037 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4038 | if (list_empty(&death->work.entry)) { |
| 4039 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4040 | if (thread->looper & |
| 4041 | (BINDER_LOOPER_STATE_REGISTERED | |
| 4042 | BINDER_LOOPER_STATE_ENTERED)) |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 4043 | binder_enqueue_thread_work_ilocked( |
| 4044 | thread, |
| 4045 | &death->work); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4046 | else { |
| 4047 | binder_enqueue_work_ilocked( |
| 4048 | &death->work, |
| 4049 | &proc->todo); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4050 | binder_wakeup_proc_ilocked( |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 4051 | proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4052 | } |
| 4053 | } else { |
| 4054 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); |
| 4055 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; |
| 4056 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4057 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4058 | } |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4059 | binder_node_unlock(ref->node); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 4060 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4061 | } break; |
| 4062 | case BC_DEAD_BINDER_DONE: { |
| 4063 | struct binder_work *w; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4064 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4065 | struct binder_ref_death *death = NULL; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4066 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4067 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4068 | return -EFAULT; |
| 4069 | |
Lisa Du | 7a64cd8 | 2016-02-17 09:32:52 +0800 | [diff] [blame] | 4070 | ptr += sizeof(cookie); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4071 | binder_inner_proc_lock(proc); |
| 4072 | list_for_each_entry(w, &proc->delivered_death, |
| 4073 | entry) { |
| 4074 | struct binder_ref_death *tmp_death = |
| 4075 | container_of(w, |
| 4076 | struct binder_ref_death, |
| 4077 | work); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4078 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4079 | if (tmp_death->cookie == cookie) { |
| 4080 | death = tmp_death; |
| 4081 | break; |
| 4082 | } |
| 4083 | } |
| 4084 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Todd Kjos | f540ce0 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 4085 | "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4086 | proc->pid, thread->pid, (u64)cookie, |
| 4087 | death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4088 | if (death == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4089 | binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n", |
| 4090 | proc->pid, thread->pid, (u64)cookie); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4091 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4092 | break; |
| 4093 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4094 | binder_dequeue_work_ilocked(&death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4095 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { |
| 4096 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4097 | if (thread->looper & |
| 4098 | (BINDER_LOOPER_STATE_REGISTERED | |
| 4099 | BINDER_LOOPER_STATE_ENTERED)) |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 4100 | binder_enqueue_thread_work_ilocked( |
| 4101 | thread, &death->work); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4102 | else { |
| 4103 | binder_enqueue_work_ilocked( |
| 4104 | &death->work, |
| 4105 | &proc->todo); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 4106 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4107 | } |
| 4108 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4109 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4110 | } break; |
| 4111 | |
| 4112 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4113 | pr_err("%d:%d unknown command %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4114 | proc->pid, thread->pid, cmd); |
| 4115 | return -EINVAL; |
| 4116 | } |
| 4117 | *consumed = ptr - buffer; |
| 4118 | } |
| 4119 | return 0; |
| 4120 | } |
| 4121 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 4122 | static void binder_stat_br(struct binder_proc *proc, |
| 4123 | struct binder_thread *thread, uint32_t cmd) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4124 | { |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4125 | trace_binder_return(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4126 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 4127 | atomic_inc(&binder_stats.br[_IOC_NR(cmd)]); |
| 4128 | atomic_inc(&proc->stats.br[_IOC_NR(cmd)]); |
| 4129 | atomic_inc(&thread->stats.br[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4130 | } |
| 4131 | } |
| 4132 | |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4133 | static int binder_put_node_cmd(struct binder_proc *proc, |
| 4134 | struct binder_thread *thread, |
| 4135 | void __user **ptrp, |
| 4136 | binder_uintptr_t node_ptr, |
| 4137 | binder_uintptr_t node_cookie, |
| 4138 | int node_debug_id, |
| 4139 | uint32_t cmd, const char *cmd_name) |
| 4140 | { |
| 4141 | void __user *ptr = *ptrp; |
| 4142 | |
| 4143 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4144 | return -EFAULT; |
| 4145 | ptr += sizeof(uint32_t); |
| 4146 | |
| 4147 | if (put_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
| 4148 | return -EFAULT; |
| 4149 | ptr += sizeof(binder_uintptr_t); |
| 4150 | |
| 4151 | if (put_user(node_cookie, (binder_uintptr_t __user *)ptr)) |
| 4152 | return -EFAULT; |
| 4153 | ptr += sizeof(binder_uintptr_t); |
| 4154 | |
| 4155 | binder_stat_br(proc, thread, cmd); |
| 4156 | binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n", |
| 4157 | proc->pid, thread->pid, cmd_name, node_debug_id, |
| 4158 | (u64)node_ptr, (u64)node_cookie); |
| 4159 | |
| 4160 | *ptrp = ptr; |
| 4161 | return 0; |
| 4162 | } |
| 4163 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4164 | static int binder_wait_for_work(struct binder_thread *thread, |
| 4165 | bool do_proc_work) |
| 4166 | { |
| 4167 | DEFINE_WAIT(wait); |
| 4168 | struct binder_proc *proc = thread->proc; |
| 4169 | int ret = 0; |
| 4170 | |
| 4171 | freezer_do_not_count(); |
| 4172 | binder_inner_proc_lock(proc); |
| 4173 | for (;;) { |
| 4174 | prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE); |
| 4175 | if (binder_has_work_ilocked(thread, do_proc_work)) |
| 4176 | break; |
| 4177 | if (do_proc_work) |
| 4178 | list_add(&thread->waiting_thread_node, |
| 4179 | &proc->waiting_threads); |
| 4180 | binder_inner_proc_unlock(proc); |
| 4181 | schedule(); |
| 4182 | binder_inner_proc_lock(proc); |
| 4183 | list_del_init(&thread->waiting_thread_node); |
| 4184 | if (signal_pending(current)) { |
| 4185 | ret = -ERESTARTSYS; |
| 4186 | break; |
| 4187 | } |
| 4188 | } |
| 4189 | finish_wait(&thread->wait, &wait); |
| 4190 | binder_inner_proc_unlock(proc); |
| 4191 | freezer_count(); |
| 4192 | |
| 4193 | return ret; |
| 4194 | } |
| 4195 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4196 | static int binder_thread_read(struct binder_proc *proc, |
| 4197 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4198 | binder_uintptr_t binder_buffer, size_t size, |
| 4199 | binder_size_t *consumed, int non_block) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4200 | { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4201 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4202 | void __user *ptr = buffer + *consumed; |
| 4203 | void __user *end = buffer + size; |
| 4204 | |
| 4205 | int ret = 0; |
| 4206 | int wait_for_proc_work; |
| 4207 | |
| 4208 | if (*consumed == 0) { |
| 4209 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) |
| 4210 | return -EFAULT; |
| 4211 | ptr += sizeof(uint32_t); |
| 4212 | } |
| 4213 | |
| 4214 | retry: |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4215 | binder_inner_proc_lock(proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4216 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4217 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4218 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4219 | thread->looper |= BINDER_LOOPER_STATE_WAITING; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4220 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4221 | trace_binder_wait_for_work(wait_for_proc_work, |
| 4222 | !!thread->transaction_stack, |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4223 | !binder_worklist_empty(proc, &thread->todo)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4224 | if (wait_for_proc_work) { |
| 4225 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4226 | BINDER_LOOPER_STATE_ENTERED))) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4227 | 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] | 4228 | proc->pid, thread->pid, thread->looper); |
| 4229 | wait_event_interruptible(binder_user_error_wait, |
| 4230 | binder_stop_on_user_error < 2); |
| 4231 | } |
Martijn Coenen | ecd972d | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 4232 | binder_restore_priority(current, proc->default_priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4233 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4234 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4235 | if (non_block) { |
| 4236 | if (!binder_has_work(thread, wait_for_proc_work)) |
| 4237 | ret = -EAGAIN; |
| 4238 | } else { |
| 4239 | ret = binder_wait_for_work(thread, wait_for_proc_work); |
| 4240 | } |
| 4241 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4242 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; |
| 4243 | |
| 4244 | if (ret) |
| 4245 | return ret; |
| 4246 | |
| 4247 | while (1) { |
| 4248 | uint32_t cmd; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4249 | struct binder_transaction_data_secctx tr; |
| 4250 | struct binder_transaction_data *trd = &tr.transaction_data; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4251 | struct binder_work *w = NULL; |
| 4252 | struct list_head *list = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4253 | struct binder_transaction *t = NULL; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4254 | struct binder_thread *t_from; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4255 | size_t trsize = sizeof(*trd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4256 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4257 | binder_inner_proc_lock(proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4258 | if (!binder_worklist_empty_ilocked(&thread->todo)) |
| 4259 | list = &thread->todo; |
| 4260 | else if (!binder_worklist_empty_ilocked(&proc->todo) && |
| 4261 | wait_for_proc_work) |
| 4262 | list = &proc->todo; |
| 4263 | else { |
| 4264 | binder_inner_proc_unlock(proc); |
| 4265 | |
Dmitry Voytik | 395262a | 2014-09-08 18:16:34 +0400 | [diff] [blame] | 4266 | /* no data added */ |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 4267 | if (ptr - buffer == 4 && !thread->looper_need_return) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4268 | goto retry; |
| 4269 | break; |
| 4270 | } |
| 4271 | |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4272 | if (end - ptr < sizeof(tr) + 4) { |
| 4273 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4274 | break; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4275 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4276 | w = binder_dequeue_work_head_ilocked(list); |
Martijn Coenen | 1af6180 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 4277 | if (binder_worklist_empty_ilocked(&thread->todo)) |
| 4278 | thread->process_todo = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4279 | |
| 4280 | switch (w->type) { |
| 4281 | case BINDER_WORK_TRANSACTION: { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4282 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4283 | t = container_of(w, struct binder_transaction, work); |
| 4284 | } break; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4285 | case BINDER_WORK_RETURN_ERROR: { |
| 4286 | struct binder_error *e = container_of( |
| 4287 | w, struct binder_error, work); |
| 4288 | |
| 4289 | WARN_ON(e->cmd == BR_OK); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4290 | binder_inner_proc_unlock(proc); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4291 | if (put_user(e->cmd, (uint32_t __user *)ptr)) |
| 4292 | return -EFAULT; |
宋金时 | e1b1a8b | 2018-05-10 02:05:03 +0000 | [diff] [blame] | 4293 | cmd = e->cmd; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4294 | e->cmd = BR_OK; |
| 4295 | ptr += sizeof(uint32_t); |
| 4296 | |
| 4297 | binder_stat_br(proc, thread, cmd); |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4298 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4299 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4300 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4301 | cmd = BR_TRANSACTION_COMPLETE; |
| 4302 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4303 | return -EFAULT; |
| 4304 | ptr += sizeof(uint32_t); |
| 4305 | |
| 4306 | binder_stat_br(proc, thread, cmd); |
| 4307 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4308 | "%d:%d BR_TRANSACTION_COMPLETE\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4309 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4310 | kfree(w); |
| 4311 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4312 | } break; |
| 4313 | case BINDER_WORK_NODE: { |
| 4314 | struct binder_node *node = container_of(w, struct binder_node, work); |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4315 | int strong, weak; |
| 4316 | binder_uintptr_t node_ptr = node->ptr; |
| 4317 | binder_uintptr_t node_cookie = node->cookie; |
| 4318 | int node_debug_id = node->debug_id; |
| 4319 | int has_weak_ref; |
| 4320 | int has_strong_ref; |
| 4321 | void __user *orig_ptr = ptr; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4322 | |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4323 | BUG_ON(proc != node->proc); |
| 4324 | strong = node->internal_strong_refs || |
| 4325 | node->local_strong_refs; |
| 4326 | weak = !hlist_empty(&node->refs) || |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4327 | node->local_weak_refs || |
| 4328 | node->tmp_refs || strong; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4329 | has_strong_ref = node->has_strong_ref; |
| 4330 | has_weak_ref = node->has_weak_ref; |
| 4331 | |
| 4332 | if (weak && !has_weak_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4333 | node->has_weak_ref = 1; |
| 4334 | node->pending_weak_ref = 1; |
| 4335 | node->local_weak_refs++; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4336 | } |
| 4337 | if (strong && !has_strong_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4338 | node->has_strong_ref = 1; |
| 4339 | node->pending_strong_ref = 1; |
| 4340 | node->local_strong_refs++; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4341 | } |
| 4342 | if (!strong && has_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4343 | node->has_strong_ref = 0; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4344 | if (!weak && has_weak_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4345 | node->has_weak_ref = 0; |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4346 | if (!weak && !strong) { |
| 4347 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4348 | "%d:%d node %d u%016llx c%016llx deleted\n", |
| 4349 | proc->pid, thread->pid, |
| 4350 | node_debug_id, |
| 4351 | (u64)node_ptr, |
| 4352 | (u64)node_cookie); |
| 4353 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4354 | binder_inner_proc_unlock(proc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4355 | binder_node_lock(node); |
| 4356 | /* |
| 4357 | * Acquire the node lock before freeing the |
| 4358 | * node to serialize with other threads that |
| 4359 | * may have been holding the node lock while |
| 4360 | * decrementing this node (avoids race where |
| 4361 | * this thread frees while the other thread |
| 4362 | * is unlocking the node after the final |
| 4363 | * decrement) |
| 4364 | */ |
| 4365 | binder_node_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4366 | binder_free_node(node); |
| 4367 | } else |
| 4368 | binder_inner_proc_unlock(proc); |
| 4369 | |
Todd Kjos | 6079261 | 2017-05-24 10:51:01 -0700 | [diff] [blame] | 4370 | if (weak && !has_weak_ref) |
| 4371 | ret = binder_put_node_cmd( |
| 4372 | proc, thread, &ptr, node_ptr, |
| 4373 | node_cookie, node_debug_id, |
| 4374 | BR_INCREFS, "BR_INCREFS"); |
| 4375 | if (!ret && strong && !has_strong_ref) |
| 4376 | ret = binder_put_node_cmd( |
| 4377 | proc, thread, &ptr, node_ptr, |
| 4378 | node_cookie, node_debug_id, |
| 4379 | BR_ACQUIRE, "BR_ACQUIRE"); |
| 4380 | if (!ret && !strong && has_strong_ref) |
| 4381 | ret = binder_put_node_cmd( |
| 4382 | proc, thread, &ptr, node_ptr, |
| 4383 | node_cookie, node_debug_id, |
| 4384 | BR_RELEASE, "BR_RELEASE"); |
| 4385 | if (!ret && !weak && has_weak_ref) |
| 4386 | ret = binder_put_node_cmd( |
| 4387 | proc, thread, &ptr, node_ptr, |
| 4388 | node_cookie, node_debug_id, |
| 4389 | BR_DECREFS, "BR_DECREFS"); |
| 4390 | if (orig_ptr == ptr) |
| 4391 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4392 | "%d:%d node %d u%016llx c%016llx state unchanged\n", |
| 4393 | proc->pid, thread->pid, |
| 4394 | node_debug_id, |
| 4395 | (u64)node_ptr, |
| 4396 | (u64)node_cookie); |
| 4397 | if (ret) |
| 4398 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4399 | } break; |
| 4400 | case BINDER_WORK_DEAD_BINDER: |
| 4401 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4402 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4403 | struct binder_ref_death *death; |
| 4404 | uint32_t cmd; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4405 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4406 | |
| 4407 | death = container_of(w, struct binder_ref_death, work); |
| 4408 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) |
| 4409 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; |
| 4410 | else |
| 4411 | cmd = BR_DEAD_BINDER; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4412 | cookie = death->cookie; |
| 4413 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4414 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4415 | "%d:%d %s %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4416 | proc->pid, thread->pid, |
| 4417 | cmd == BR_DEAD_BINDER ? |
| 4418 | "BR_DEAD_BINDER" : |
| 4419 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4420 | (u64)cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4421 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4422 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4423 | kfree(death); |
| 4424 | binder_stats_deleted(BINDER_STAT_DEATH); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4425 | } else { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4426 | binder_enqueue_work_ilocked( |
| 4427 | w, &proc->delivered_death); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 4428 | binder_inner_proc_unlock(proc); |
| 4429 | } |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 4430 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4431 | return -EFAULT; |
| 4432 | ptr += sizeof(uint32_t); |
| 4433 | if (put_user(cookie, |
| 4434 | (binder_uintptr_t __user *)ptr)) |
| 4435 | return -EFAULT; |
| 4436 | ptr += sizeof(binder_uintptr_t); |
| 4437 | binder_stat_br(proc, thread, cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4438 | if (cmd == BR_DEAD_BINDER) |
| 4439 | goto done; /* DEAD_BINDER notifications can cause transactions */ |
| 4440 | } break; |
| 4441 | } |
| 4442 | |
| 4443 | if (!t) |
| 4444 | continue; |
| 4445 | |
| 4446 | BUG_ON(t->buffer == NULL); |
| 4447 | if (t->buffer->target_node) { |
| 4448 | struct binder_node *target_node = t->buffer->target_node; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4449 | struct binder_priority node_prio; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4450 | |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4451 | trd->target.ptr = target_node->ptr; |
| 4452 | trd->cookie = target_node->cookie; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4453 | node_prio.sched_policy = target_node->sched_policy; |
| 4454 | node_prio.prio = target_node->min_priority; |
Martijn Coenen | c46810c | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 4455 | binder_transaction_priority(current, t, node_prio, |
| 4456 | target_node->inherit_rt); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4457 | cmd = BR_TRANSACTION; |
| 4458 | } else { |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4459 | trd->target.ptr = 0; |
| 4460 | trd->cookie = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4461 | cmd = BR_REPLY; |
| 4462 | } |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4463 | trd->code = t->code; |
| 4464 | trd->flags = t->flags; |
| 4465 | trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4466 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4467 | t_from = binder_get_txn_from(t); |
| 4468 | if (t_from) { |
| 4469 | struct task_struct *sender = t_from->proc->tsk; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4470 | |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4471 | trd->sender_pid = |
| 4472 | task_tgid_nr_ns(sender, |
| 4473 | task_active_pid_ns(current)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4474 | } else { |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4475 | trd->sender_pid = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4476 | } |
| 4477 | |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4478 | trd->data_size = t->buffer->data_size; |
| 4479 | trd->offsets_size = t->buffer->offsets_size; |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 4480 | trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4481 | trd->data.ptr.offsets = trd->data.ptr.buffer + |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4482 | ALIGN(t->buffer->data_size, |
| 4483 | sizeof(void *)); |
| 4484 | |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4485 | tr.secctx = t->security_ctx; |
| 4486 | if (t->security_ctx) { |
| 4487 | cmd = BR_TRANSACTION_SEC_CTX; |
| 4488 | trsize = sizeof(tr); |
| 4489 | } |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4490 | if (put_user(cmd, (uint32_t __user *)ptr)) { |
| 4491 | if (t_from) |
| 4492 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4493 | |
| 4494 | binder_cleanup_transaction(t, "put_user failed", |
| 4495 | BR_FAILED_REPLY); |
| 4496 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4497 | return -EFAULT; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4498 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4499 | ptr += sizeof(uint32_t); |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4500 | if (copy_to_user(ptr, &tr, trsize)) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4501 | if (t_from) |
| 4502 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4503 | |
| 4504 | binder_cleanup_transaction(t, "copy_to_user failed", |
| 4505 | BR_FAILED_REPLY); |
| 4506 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4507 | return -EFAULT; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4508 | } |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4509 | ptr += trsize; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4510 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4511 | trace_binder_transaction_received(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4512 | binder_stat_br(proc, thread, cmd); |
| 4513 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4514 | "%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] | 4515 | proc->pid, thread->pid, |
| 4516 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4517 | (cmd == BR_TRANSACTION_SEC_CTX) ? |
| 4518 | "BR_TRANSACTION_SEC_CTX" : "BR_REPLY", |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4519 | t->debug_id, t_from ? t_from->proc->pid : 0, |
| 4520 | t_from ? t_from->pid : 0, cmd, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4521 | t->buffer->data_size, t->buffer->offsets_size, |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4522 | (u64)trd->data.ptr.buffer, |
| 4523 | (u64)trd->data.ptr.offsets); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4524 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4525 | if (t_from) |
| 4526 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4527 | t->buffer->allow_user_free = 1; |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4528 | if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) { |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4529 | binder_inner_proc_lock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4530 | t->to_parent = thread->transaction_stack; |
| 4531 | t->to_thread = thread; |
| 4532 | thread->transaction_stack = t; |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4533 | binder_inner_proc_unlock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4534 | } else { |
Todd Kjos | 21ef40a | 2017-03-30 18:02:13 -0700 | [diff] [blame] | 4535 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4536 | } |
| 4537 | break; |
| 4538 | } |
| 4539 | |
| 4540 | done: |
| 4541 | |
| 4542 | *consumed = ptr - buffer; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4543 | binder_inner_proc_lock(proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4544 | if (proc->requested_threads == 0 && |
| 4545 | list_empty(&thread->proc->waiting_threads) && |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4546 | proc->requested_threads_started < proc->max_threads && |
| 4547 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4548 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ |
| 4549 | /*spawn a new thread if we leave this out */) { |
| 4550 | proc->requested_threads++; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4551 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4552 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4553 | "%d:%d BR_SPAWN_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4554 | proc->pid, thread->pid); |
| 4555 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) |
| 4556 | return -EFAULT; |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 4557 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 4558 | } else |
| 4559 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4560 | return 0; |
| 4561 | } |
| 4562 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4563 | static void binder_release_work(struct binder_proc *proc, |
| 4564 | struct list_head *list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4565 | { |
| 4566 | struct binder_work *w; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4567 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4568 | while (1) { |
| 4569 | w = binder_dequeue_work_head(proc, list); |
| 4570 | if (!w) |
| 4571 | return; |
| 4572 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4573 | switch (w->type) { |
| 4574 | case BINDER_WORK_TRANSACTION: { |
| 4575 | struct binder_transaction *t; |
| 4576 | |
| 4577 | t = container_of(w, struct binder_transaction, work); |
Martijn Coenen | 3217ccc | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4578 | |
| 4579 | binder_cleanup_transaction(t, "process died.", |
| 4580 | BR_DEAD_REPLY); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4581 | } break; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 4582 | case BINDER_WORK_RETURN_ERROR: { |
| 4583 | struct binder_error *e = container_of( |
| 4584 | w, struct binder_error, work); |
| 4585 | |
| 4586 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 4587 | "undelivered TRANSACTION_ERROR: %u\n", |
| 4588 | e->cmd); |
| 4589 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4590 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4591 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4592 | "undelivered TRANSACTION_COMPLETE\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4593 | kfree(w); |
| 4594 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4595 | } break; |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4596 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4597 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4598 | struct binder_ref_death *death; |
| 4599 | |
| 4600 | death = container_of(w, struct binder_ref_death, work); |
| 4601 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4602 | "undelivered death notification, %016llx\n", |
| 4603 | (u64)death->cookie); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4604 | kfree(death); |
| 4605 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 4606 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4607 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4608 | pr_err("unexpected work type, %d, not freed\n", |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4609 | w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4610 | break; |
| 4611 | } |
| 4612 | } |
| 4613 | |
| 4614 | } |
| 4615 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4616 | static struct binder_thread *binder_get_thread_ilocked( |
| 4617 | struct binder_proc *proc, struct binder_thread *new_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4618 | { |
| 4619 | struct binder_thread *thread = NULL; |
| 4620 | struct rb_node *parent = NULL; |
| 4621 | struct rb_node **p = &proc->threads.rb_node; |
| 4622 | |
| 4623 | while (*p) { |
| 4624 | parent = *p; |
| 4625 | thread = rb_entry(parent, struct binder_thread, rb_node); |
| 4626 | |
| 4627 | if (current->pid < thread->pid) |
| 4628 | p = &(*p)->rb_left; |
| 4629 | else if (current->pid > thread->pid) |
| 4630 | p = &(*p)->rb_right; |
| 4631 | else |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4632 | return thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4633 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4634 | if (!new_thread) |
| 4635 | return NULL; |
| 4636 | thread = new_thread; |
| 4637 | binder_stats_created(BINDER_STAT_THREAD); |
| 4638 | thread->proc = proc; |
| 4639 | thread->pid = current->pid; |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4640 | get_task_struct(current); |
| 4641 | thread->task = current; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4642 | atomic_set(&thread->tmp_ref, 0); |
| 4643 | init_waitqueue_head(&thread->wait); |
| 4644 | INIT_LIST_HEAD(&thread->todo); |
| 4645 | rb_link_node(&thread->rb_node, parent, p); |
| 4646 | rb_insert_color(&thread->rb_node, &proc->threads); |
| 4647 | thread->looper_need_return = true; |
| 4648 | thread->return_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4649 | thread->return_error.cmd = BR_OK; |
| 4650 | thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4651 | thread->reply_error.cmd = BR_OK; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4652 | INIT_LIST_HEAD(&new_thread->waiting_thread_node); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4653 | return thread; |
| 4654 | } |
| 4655 | |
| 4656 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) |
| 4657 | { |
| 4658 | struct binder_thread *thread; |
| 4659 | struct binder_thread *new_thread; |
| 4660 | |
| 4661 | binder_inner_proc_lock(proc); |
| 4662 | thread = binder_get_thread_ilocked(proc, NULL); |
| 4663 | binder_inner_proc_unlock(proc); |
| 4664 | if (!thread) { |
| 4665 | new_thread = kzalloc(sizeof(*thread), GFP_KERNEL); |
| 4666 | if (new_thread == NULL) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4667 | return NULL; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4668 | binder_inner_proc_lock(proc); |
| 4669 | thread = binder_get_thread_ilocked(proc, new_thread); |
| 4670 | binder_inner_proc_unlock(proc); |
| 4671 | if (thread != new_thread) |
| 4672 | kfree(new_thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4673 | } |
| 4674 | return thread; |
| 4675 | } |
| 4676 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4677 | static void binder_free_proc(struct binder_proc *proc) |
| 4678 | { |
| 4679 | BUG_ON(!list_empty(&proc->todo)); |
| 4680 | BUG_ON(!list_empty(&proc->delivered_death)); |
| 4681 | binder_alloc_deferred_release(&proc->alloc); |
| 4682 | put_task_struct(proc->tsk); |
| 4683 | binder_stats_deleted(BINDER_STAT_PROC); |
| 4684 | kfree(proc); |
| 4685 | } |
| 4686 | |
| 4687 | static void binder_free_thread(struct binder_thread *thread) |
| 4688 | { |
| 4689 | BUG_ON(!list_empty(&thread->todo)); |
| 4690 | binder_stats_deleted(BINDER_STAT_THREAD); |
| 4691 | binder_proc_dec_tmpref(thread->proc); |
Martijn Coenen | 07a30fe | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4692 | put_task_struct(thread->task); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4693 | kfree(thread); |
| 4694 | } |
| 4695 | |
| 4696 | static int binder_thread_release(struct binder_proc *proc, |
| 4697 | struct binder_thread *thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4698 | { |
| 4699 | struct binder_transaction *t; |
| 4700 | struct binder_transaction *send_reply = NULL; |
| 4701 | int active_transactions = 0; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4702 | struct binder_transaction *last_t = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4703 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4704 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4705 | /* |
| 4706 | * take a ref on the proc so it survives |
| 4707 | * after we remove this thread from proc->threads. |
| 4708 | * The corresponding dec is when we actually |
| 4709 | * free the thread in binder_free_thread() |
| 4710 | */ |
| 4711 | proc->tmp_ref++; |
| 4712 | /* |
| 4713 | * take a ref on this thread to ensure it |
| 4714 | * survives while we are releasing it |
| 4715 | */ |
| 4716 | atomic_inc(&thread->tmp_ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4717 | rb_erase(&thread->rb_node, &proc->threads); |
| 4718 | t = thread->transaction_stack; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4719 | if (t) { |
| 4720 | spin_lock(&t->lock); |
| 4721 | if (t->to_thread == thread) |
| 4722 | send_reply = t; |
| 4723 | } |
| 4724 | thread->is_dead = true; |
| 4725 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4726 | while (t) { |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4727 | last_t = t; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4728 | active_transactions++; |
| 4729 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4730 | "release %d:%d transaction %d %s, still active\n", |
| 4731 | proc->pid, thread->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4732 | t->debug_id, |
| 4733 | (t->to_thread == thread) ? "in" : "out"); |
| 4734 | |
| 4735 | if (t->to_thread == thread) { |
| 4736 | t->to_proc = NULL; |
| 4737 | t->to_thread = NULL; |
| 4738 | if (t->buffer) { |
| 4739 | t->buffer->transaction = NULL; |
| 4740 | t->buffer = NULL; |
| 4741 | } |
| 4742 | t = t->to_parent; |
| 4743 | } else if (t->from == thread) { |
| 4744 | t->from = NULL; |
| 4745 | t = t->from_parent; |
| 4746 | } else |
| 4747 | BUG(); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4748 | spin_unlock(&last_t->lock); |
| 4749 | if (t) |
| 4750 | spin_lock(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4751 | } |
Martijn Coenen | 550c01d | 2018-01-05 11:27:07 +0100 | [diff] [blame] | 4752 | |
| 4753 | /* |
| 4754 | * If this thread used poll, make sure we remove the waitqueue |
| 4755 | * from any epoll data structures holding it with POLLFREE. |
| 4756 | * waitqueue_active() is safe to use here because we're holding |
| 4757 | * the inner lock. |
| 4758 | */ |
| 4759 | if ((thread->looper & BINDER_LOOPER_STATE_POLL) && |
| 4760 | waitqueue_active(&thread->wait)) { |
| 4761 | wake_up_poll(&thread->wait, POLLHUP | POLLFREE); |
| 4762 | } |
| 4763 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 4764 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4765 | |
Martijn Coenen | 72766d7 | 2018-02-16 09:47:15 +0100 | [diff] [blame] | 4766 | /* |
| 4767 | * This is needed to avoid races between wake_up_poll() above and |
| 4768 | * and ep_remove_waitqueue() called for other reasons (eg the epoll file |
| 4769 | * descriptor being closed); ep_remove_waitqueue() holds an RCU read |
| 4770 | * lock, so we can be sure it's done after calling synchronize_rcu(). |
| 4771 | */ |
| 4772 | if (thread->looper & BINDER_LOOPER_STATE_POLL) |
| 4773 | synchronize_rcu(); |
| 4774 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4775 | if (send_reply) |
| 4776 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 4777 | binder_release_work(proc, &thread->todo); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 4778 | binder_thread_dec_tmpref(thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4779 | return active_transactions; |
| 4780 | } |
| 4781 | |
| 4782 | static unsigned int binder_poll(struct file *filp, |
| 4783 | struct poll_table_struct *wait) |
| 4784 | { |
| 4785 | struct binder_proc *proc = filp->private_data; |
| 4786 | struct binder_thread *thread = NULL; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4787 | bool wait_for_proc_work; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4788 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4789 | thread = binder_get_thread(proc); |
Greg Kroah-Hartman | 6e463bb | 2018-02-28 17:17:14 +0100 | [diff] [blame] | 4790 | if (!thread) |
Eric Biggers | 4be5a28 | 2018-01-30 23:11:24 -0800 | [diff] [blame] | 4791 | return POLLERR; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4792 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4793 | binder_inner_proc_lock(thread->proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4794 | thread->looper |= BINDER_LOOPER_STATE_POLL; |
| 4795 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
| 4796 | |
Martijn Coenen | 995a36e | 2017-06-02 13:36:52 -0700 | [diff] [blame] | 4797 | binder_inner_proc_unlock(thread->proc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4798 | |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4799 | poll_wait(filp, &thread->wait, wait); |
| 4800 | |
Martijn Coenen | 4781093 | 2017-08-10 12:32:00 +0200 | [diff] [blame] | 4801 | if (binder_has_work(thread, wait_for_proc_work)) |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4802 | return POLLIN; |
| 4803 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4804 | return 0; |
| 4805 | } |
| 4806 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4807 | static int binder_ioctl_write_read(struct file *filp, |
| 4808 | unsigned int cmd, unsigned long arg, |
| 4809 | struct binder_thread *thread) |
| 4810 | { |
| 4811 | int ret = 0; |
| 4812 | struct binder_proc *proc = filp->private_data; |
| 4813 | unsigned int size = _IOC_SIZE(cmd); |
| 4814 | void __user *ubuf = (void __user *)arg; |
| 4815 | struct binder_write_read bwr; |
| 4816 | |
| 4817 | if (size != sizeof(struct binder_write_read)) { |
| 4818 | ret = -EINVAL; |
| 4819 | goto out; |
| 4820 | } |
| 4821 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { |
| 4822 | ret = -EFAULT; |
| 4823 | goto out; |
| 4824 | } |
| 4825 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4826 | "%d:%d write %lld at %016llx, read %lld at %016llx\n", |
| 4827 | proc->pid, thread->pid, |
| 4828 | (u64)bwr.write_size, (u64)bwr.write_buffer, |
| 4829 | (u64)bwr.read_size, (u64)bwr.read_buffer); |
| 4830 | |
| 4831 | if (bwr.write_size > 0) { |
| 4832 | ret = binder_thread_write(proc, thread, |
| 4833 | bwr.write_buffer, |
| 4834 | bwr.write_size, |
| 4835 | &bwr.write_consumed); |
| 4836 | trace_binder_write_done(ret); |
| 4837 | if (ret < 0) { |
| 4838 | bwr.read_consumed = 0; |
| 4839 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4840 | ret = -EFAULT; |
| 4841 | goto out; |
| 4842 | } |
| 4843 | } |
| 4844 | if (bwr.read_size > 0) { |
| 4845 | ret = binder_thread_read(proc, thread, bwr.read_buffer, |
| 4846 | bwr.read_size, |
| 4847 | &bwr.read_consumed, |
| 4848 | filp->f_flags & O_NONBLOCK); |
| 4849 | trace_binder_read_done(ret); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4850 | binder_inner_proc_lock(proc); |
| 4851 | if (!binder_worklist_empty_ilocked(&proc->todo)) |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 4852 | binder_wakeup_proc_ilocked(proc); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 4853 | binder_inner_proc_unlock(proc); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4854 | if (ret < 0) { |
| 4855 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4856 | ret = -EFAULT; |
| 4857 | goto out; |
| 4858 | } |
| 4859 | } |
| 4860 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4861 | "%d:%d wrote %lld of %lld, read return %lld of %lld\n", |
| 4862 | proc->pid, thread->pid, |
| 4863 | (u64)bwr.write_consumed, (u64)bwr.write_size, |
| 4864 | (u64)bwr.read_consumed, (u64)bwr.read_size); |
| 4865 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { |
| 4866 | ret = -EFAULT; |
| 4867 | goto out; |
| 4868 | } |
| 4869 | out: |
| 4870 | return ret; |
| 4871 | } |
| 4872 | |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4873 | static int binder_ioctl_set_ctx_mgr(struct file *filp, |
| 4874 | struct flat_binder_object *fbo) |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4875 | { |
| 4876 | int ret = 0; |
| 4877 | struct binder_proc *proc = filp->private_data; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4878 | struct binder_context *context = proc->context; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4879 | struct binder_node *new_node; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4880 | kuid_t curr_euid = current_euid(); |
| 4881 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4882 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4883 | if (context->binder_context_mgr_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4884 | pr_err("BINDER_SET_CONTEXT_MGR already set\n"); |
| 4885 | ret = -EBUSY; |
| 4886 | goto out; |
| 4887 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 4888 | ret = security_binder_set_context_mgr(proc->tsk); |
| 4889 | if (ret < 0) |
| 4890 | goto out; |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4891 | if (uid_valid(context->binder_context_mgr_uid)) { |
| 4892 | if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4893 | pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n", |
| 4894 | from_kuid(&init_user_ns, curr_euid), |
| 4895 | from_kuid(&init_user_ns, |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4896 | context->binder_context_mgr_uid)); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4897 | ret = -EPERM; |
| 4898 | goto out; |
| 4899 | } |
| 4900 | } else { |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 4901 | context->binder_context_mgr_uid = curr_euid; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4902 | } |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4903 | new_node = binder_new_node(proc, fbo); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4904 | if (!new_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4905 | ret = -ENOMEM; |
| 4906 | goto out; |
| 4907 | } |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4908 | binder_node_lock(new_node); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4909 | new_node->local_weak_refs++; |
| 4910 | new_node->local_strong_refs++; |
| 4911 | new_node->has_strong_ref = 1; |
| 4912 | new_node->has_weak_ref = 1; |
| 4913 | context->binder_context_mgr_node = new_node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 4914 | binder_node_unlock(new_node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 4915 | binder_put_node(new_node); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4916 | out: |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 4917 | mutex_unlock(&context->context_mgr_node_lock); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4918 | return ret; |
| 4919 | } |
| 4920 | |
Martijn Coenen | 1c57ba4 | 2018-08-25 13:50:56 -0700 | [diff] [blame] | 4921 | static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc, |
| 4922 | struct binder_node_info_for_ref *info) |
| 4923 | { |
| 4924 | struct binder_node *node; |
| 4925 | struct binder_context *context = proc->context; |
| 4926 | __u32 handle = info->handle; |
| 4927 | |
| 4928 | if (info->strong_count || info->weak_count || info->reserved1 || |
| 4929 | info->reserved2 || info->reserved3) { |
| 4930 | binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.", |
| 4931 | proc->pid); |
| 4932 | return -EINVAL; |
| 4933 | } |
| 4934 | |
| 4935 | /* This ioctl may only be used by the context manager */ |
| 4936 | mutex_lock(&context->context_mgr_node_lock); |
| 4937 | if (!context->binder_context_mgr_node || |
| 4938 | context->binder_context_mgr_node->proc != proc) { |
| 4939 | mutex_unlock(&context->context_mgr_node_lock); |
| 4940 | return -EPERM; |
| 4941 | } |
| 4942 | mutex_unlock(&context->context_mgr_node_lock); |
| 4943 | |
| 4944 | node = binder_get_node_from_ref(proc, handle, true, NULL); |
| 4945 | if (!node) |
| 4946 | return -EINVAL; |
| 4947 | |
| 4948 | info->strong_count = node->local_strong_refs + |
| 4949 | node->internal_strong_refs; |
| 4950 | info->weak_count = node->local_weak_refs; |
| 4951 | |
| 4952 | binder_put_node(node); |
| 4953 | |
| 4954 | return 0; |
| 4955 | } |
| 4956 | |
Colin Cross | 833babb3 | 2017-06-20 13:54:44 -0700 | [diff] [blame] | 4957 | static int binder_ioctl_get_node_debug_info(struct binder_proc *proc, |
| 4958 | struct binder_node_debug_info *info) { |
| 4959 | struct rb_node *n; |
| 4960 | binder_uintptr_t ptr = info->ptr; |
| 4961 | |
| 4962 | memset(info, 0, sizeof(*info)); |
| 4963 | |
| 4964 | binder_inner_proc_lock(proc); |
| 4965 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { |
| 4966 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 4967 | rb_node); |
| 4968 | if (node->ptr > ptr) { |
| 4969 | info->ptr = node->ptr; |
| 4970 | info->cookie = node->cookie; |
| 4971 | info->has_strong_ref = node->has_strong_ref; |
| 4972 | info->has_weak_ref = node->has_weak_ref; |
| 4973 | break; |
| 4974 | } |
| 4975 | } |
| 4976 | binder_inner_proc_unlock(proc); |
| 4977 | |
| 4978 | return 0; |
| 4979 | } |
| 4980 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4981 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 4982 | { |
| 4983 | int ret; |
| 4984 | struct binder_proc *proc = filp->private_data; |
| 4985 | struct binder_thread *thread; |
| 4986 | unsigned int size = _IOC_SIZE(cmd); |
| 4987 | void __user *ubuf = (void __user *)arg; |
| 4988 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4989 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", |
| 4990 | proc->pid, current->pid, cmd, arg);*/ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4991 | |
Sherry Yang | 435416b | 2017-06-22 14:37:45 -0700 | [diff] [blame] | 4992 | binder_selftest_alloc(&proc->alloc); |
| 4993 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4994 | trace_binder_ioctl(cmd, arg); |
| 4995 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4996 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4997 | if (ret) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4998 | goto err_unlocked; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4999 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5000 | thread = binder_get_thread(proc); |
| 5001 | if (thread == NULL) { |
| 5002 | ret = -ENOMEM; |
| 5003 | goto err; |
| 5004 | } |
| 5005 | |
| 5006 | switch (cmd) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5007 | case BINDER_WRITE_READ: |
| 5008 | ret = binder_ioctl_write_read(filp, cmd, arg, thread); |
| 5009 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5010 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5011 | break; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 5012 | case BINDER_SET_MAX_THREADS: { |
| 5013 | int max_threads; |
| 5014 | |
| 5015 | if (copy_from_user(&max_threads, ubuf, |
| 5016 | sizeof(max_threads))) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5017 | ret = -EINVAL; |
| 5018 | goto err; |
| 5019 | } |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 5020 | binder_inner_proc_lock(proc); |
| 5021 | proc->max_threads = max_threads; |
| 5022 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5023 | break; |
Todd Kjos | d600e90 | 2017-05-25 17:35:02 -0700 | [diff] [blame] | 5024 | } |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 5025 | case BINDER_SET_CONTEXT_MGR_EXT: { |
| 5026 | struct flat_binder_object fbo; |
| 5027 | |
| 5028 | if (copy_from_user(&fbo, ubuf, sizeof(fbo))) { |
| 5029 | ret = -EINVAL; |
| 5030 | goto err; |
| 5031 | } |
| 5032 | ret = binder_ioctl_set_ctx_mgr(filp, &fbo); |
| 5033 | if (ret) |
| 5034 | goto err; |
| 5035 | break; |
| 5036 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5037 | case BINDER_SET_CONTEXT_MGR: |
Todd Kjos | 63e0afa | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 5038 | ret = binder_ioctl_set_ctx_mgr(filp, NULL); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5039 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5040 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5041 | break; |
| 5042 | case BINDER_THREAD_EXIT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 5043 | binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5044 | proc->pid, thread->pid); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5045 | binder_thread_release(proc, thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5046 | thread = NULL; |
| 5047 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 5048 | case BINDER_VERSION: { |
| 5049 | struct binder_version __user *ver = ubuf; |
| 5050 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5051 | if (size != sizeof(struct binder_version)) { |
| 5052 | ret = -EINVAL; |
| 5053 | goto err; |
| 5054 | } |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 5055 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, |
| 5056 | &ver->protocol_version)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5057 | ret = -EINVAL; |
| 5058 | goto err; |
| 5059 | } |
| 5060 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 5061 | } |
Martijn Coenen | 1c57ba4 | 2018-08-25 13:50:56 -0700 | [diff] [blame] | 5062 | case BINDER_GET_NODE_INFO_FOR_REF: { |
| 5063 | struct binder_node_info_for_ref info; |
| 5064 | |
| 5065 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 5066 | ret = -EFAULT; |
| 5067 | goto err; |
| 5068 | } |
| 5069 | |
| 5070 | ret = binder_ioctl_get_node_info_for_ref(proc, &info); |
| 5071 | if (ret < 0) |
| 5072 | goto err; |
| 5073 | |
| 5074 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 5075 | ret = -EFAULT; |
| 5076 | goto err; |
| 5077 | } |
| 5078 | |
| 5079 | break; |
| 5080 | } |
Colin Cross | 833babb3 | 2017-06-20 13:54:44 -0700 | [diff] [blame] | 5081 | case BINDER_GET_NODE_DEBUG_INFO: { |
| 5082 | struct binder_node_debug_info info; |
| 5083 | |
| 5084 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 5085 | ret = -EFAULT; |
| 5086 | goto err; |
| 5087 | } |
| 5088 | |
| 5089 | ret = binder_ioctl_get_node_debug_info(proc, &info); |
| 5090 | if (ret < 0) |
| 5091 | goto err; |
| 5092 | |
| 5093 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 5094 | ret = -EFAULT; |
| 5095 | goto err; |
| 5096 | } |
| 5097 | break; |
| 5098 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5099 | default: |
| 5100 | ret = -EINVAL; |
| 5101 | goto err; |
| 5102 | } |
| 5103 | ret = 0; |
| 5104 | err: |
| 5105 | if (thread) |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 5106 | thread->looper_need_return = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5107 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 5108 | if (ret && ret != -ERESTARTSYS) |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 5109 | 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] | 5110 | err_unlocked: |
| 5111 | trace_binder_ioctl_done(ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5112 | return ret; |
| 5113 | } |
| 5114 | |
| 5115 | static void binder_vma_open(struct vm_area_struct *vma) |
| 5116 | { |
| 5117 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5118 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5119 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 5120 | "%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] | 5121 | proc->pid, vma->vm_start, vma->vm_end, |
| 5122 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 5123 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | static void binder_vma_close(struct vm_area_struct *vma) |
| 5127 | { |
| 5128 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5129 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5130 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 5131 | "%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] | 5132 | proc->pid, vma->vm_start, vma->vm_end, |
| 5133 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 5134 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5135 | binder_alloc_vma_close(&proc->alloc); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5136 | binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5137 | } |
| 5138 | |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 5139 | static int binder_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 5140 | { |
| 5141 | return VM_FAULT_SIGBUS; |
| 5142 | } |
| 5143 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 5144 | static const struct vm_operations_struct binder_vm_ops = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5145 | .open = binder_vma_open, |
| 5146 | .close = binder_vma_close, |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 5147 | .fault = binder_vm_fault, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5148 | }; |
| 5149 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5150 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) |
| 5151 | { |
| 5152 | int ret; |
| 5153 | struct binder_proc *proc = filp->private_data; |
| 5154 | const char *failure_string; |
| 5155 | |
| 5156 | if (proc->tsk != current->group_leader) |
| 5157 | return -EINVAL; |
| 5158 | |
| 5159 | if ((vma->vm_end - vma->vm_start) > SZ_4M) |
| 5160 | vma->vm_end = vma->vm_start + SZ_4M; |
| 5161 | |
| 5162 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 5163 | "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 5164 | __func__, proc->pid, vma->vm_start, vma->vm_end, |
| 5165 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 5166 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 5167 | |
| 5168 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { |
| 5169 | ret = -EPERM; |
| 5170 | failure_string = "bad vm_flags"; |
| 5171 | goto err_bad_arg; |
| 5172 | } |
Minchan Kim | 2cafd5b | 2018-05-07 23:15:37 +0900 | [diff] [blame] | 5173 | vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP; |
| 5174 | vma->vm_flags &= ~VM_MAYWRITE; |
| 5175 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5176 | vma->vm_ops = &binder_vm_ops; |
| 5177 | vma->vm_private_data = proc; |
| 5178 | |
| 5179 | ret = binder_alloc_mmap_handler(&proc->alloc, vma); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5180 | if (ret) |
| 5181 | return ret; |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5182 | mutex_lock(&proc->files_lock); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5183 | proc->files = get_files_struct(current); |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5184 | mutex_unlock(&proc->files_lock); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5185 | return 0; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5186 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5187 | err_bad_arg: |
Elad Wexler | 6b64640 | 2017-12-29 11:03:37 +0200 | [diff] [blame] | 5188 | pr_err("%s: %d %lx-%lx %s failed %d\n", __func__, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5189 | proc->pid, vma->vm_start, vma->vm_end, failure_string, ret); |
| 5190 | return ret; |
| 5191 | } |
| 5192 | |
| 5193 | static int binder_open(struct inode *nodp, struct file *filp) |
| 5194 | { |
| 5195 | struct binder_proc *proc; |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5196 | struct binder_device *binder_dev; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5197 | |
Elad Wexler | 6b64640 | 2017-12-29 11:03:37 +0200 | [diff] [blame] | 5198 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5199 | current->group_leader->pid, current->pid); |
| 5200 | |
| 5201 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 5202 | if (proc == NULL) |
| 5203 | return -ENOMEM; |
Todd Kjos | fc7a7e2 | 2017-05-29 16:44:24 -0700 | [diff] [blame] | 5204 | spin_lock_init(&proc->inner_lock); |
| 5205 | spin_lock_init(&proc->outer_lock); |
Martijn Coenen | 872c26e | 2017-03-07 15:51:18 +0100 | [diff] [blame] | 5206 | get_task_struct(current->group_leader); |
| 5207 | proc->tsk = current->group_leader; |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5208 | mutex_init(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5209 | INIT_LIST_HEAD(&proc->todo); |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 5210 | if (binder_supported_policy(current->policy)) { |
| 5211 | proc->default_priority.sched_policy = current->policy; |
| 5212 | proc->default_priority.prio = current->normal_prio; |
| 5213 | } else { |
| 5214 | proc->default_priority.sched_policy = SCHED_NORMAL; |
| 5215 | proc->default_priority.prio = NICE_TO_PRIO(0); |
| 5216 | } |
| 5217 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 5218 | binder_dev = container_of(filp->private_data, struct binder_device, |
| 5219 | miscdev); |
| 5220 | proc->context = &binder_dev->context; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5221 | binder_alloc_init(&proc->alloc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5222 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5223 | binder_stats_created(BINDER_STAT_PROC); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5224 | proc->pid = current->group_leader->pid; |
| 5225 | INIT_LIST_HEAD(&proc->delivered_death); |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5226 | INIT_LIST_HEAD(&proc->waiting_threads); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5227 | filp->private_data = proc; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5228 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5229 | mutex_lock(&binder_procs_lock); |
| 5230 | hlist_add_head(&proc->proc_node, &binder_procs); |
| 5231 | mutex_unlock(&binder_procs_lock); |
| 5232 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5233 | if (binder_debugfs_dir_entry_proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5234 | char strbuf[11]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5235 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5236 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5237 | /* |
| 5238 | * proc debug entries are shared between contexts, so |
| 5239 | * this will fail if the process tries to open the driver |
| 5240 | * again with a different context. The priting code will |
| 5241 | * anyway print all contexts that a given PID has, so this |
| 5242 | * is not a problem. |
| 5243 | */ |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5244 | proc->debugfs_entry = debugfs_create_file(strbuf, 0444, |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5245 | binder_debugfs_dir_entry_proc, |
| 5246 | (void *)(unsigned long)proc->pid, |
| 5247 | &binder_proc_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | return 0; |
| 5251 | } |
| 5252 | |
| 5253 | static int binder_flush(struct file *filp, fl_owner_t id) |
| 5254 | { |
| 5255 | struct binder_proc *proc = filp->private_data; |
| 5256 | |
| 5257 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); |
| 5258 | |
| 5259 | return 0; |
| 5260 | } |
| 5261 | |
| 5262 | static void binder_deferred_flush(struct binder_proc *proc) |
| 5263 | { |
| 5264 | struct rb_node *n; |
| 5265 | int wake_count = 0; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5266 | |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5267 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5268 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 5269 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5270 | |
Todd Kjos | 6798e6d | 2017-01-06 14:19:25 -0800 | [diff] [blame] | 5271 | thread->looper_need_return = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5272 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { |
| 5273 | wake_up_interruptible(&thread->wait); |
| 5274 | wake_count++; |
| 5275 | } |
| 5276 | } |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5277 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5278 | |
| 5279 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 5280 | "binder_flush: %d woke %d threads\n", proc->pid, |
| 5281 | wake_count); |
| 5282 | } |
| 5283 | |
| 5284 | static int binder_release(struct inode *nodp, struct file *filp) |
| 5285 | { |
| 5286 | struct binder_proc *proc = filp->private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5287 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5288 | debugfs_remove(proc->debugfs_entry); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5289 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); |
| 5290 | |
| 5291 | return 0; |
| 5292 | } |
| 5293 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5294 | static int binder_node_release(struct binder_node *node, int refs) |
| 5295 | { |
| 5296 | struct binder_ref *ref; |
| 5297 | int death = 0; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5298 | struct binder_proc *proc = node->proc; |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5299 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5300 | binder_release_work(proc, &node->async_todo); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5301 | |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5302 | binder_node_lock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5303 | binder_inner_proc_lock(proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5304 | binder_dequeue_work_ilocked(&node->work); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5305 | /* |
| 5306 | * The caller must have taken a temporary ref on the node, |
| 5307 | */ |
| 5308 | BUG_ON(!node->tmp_refs); |
| 5309 | if (hlist_empty(&node->refs) && node->tmp_refs == 1) { |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5310 | binder_inner_proc_unlock(proc); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5311 | binder_node_unlock(node); |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5312 | binder_free_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5313 | |
| 5314 | return refs; |
| 5315 | } |
| 5316 | |
| 5317 | node->proc = NULL; |
| 5318 | node->local_strong_refs = 0; |
| 5319 | node->local_weak_refs = 0; |
Todd Kjos | e7f23ed | 2017-03-21 13:06:01 -0700 | [diff] [blame] | 5320 | binder_inner_proc_unlock(proc); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5321 | |
| 5322 | spin_lock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5323 | hlist_add_head(&node->dead_node, &binder_dead_nodes); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5324 | spin_unlock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5325 | |
| 5326 | hlist_for_each_entry(ref, &node->refs, node_entry) { |
| 5327 | refs++; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 5328 | /* |
| 5329 | * Need the node lock to synchronize |
| 5330 | * with new notification requests and the |
| 5331 | * inner lock to synchronize with queued |
| 5332 | * death notifications. |
| 5333 | */ |
| 5334 | binder_inner_proc_lock(ref->proc); |
| 5335 | if (!ref->death) { |
| 5336 | binder_inner_proc_unlock(ref->proc); |
Arve Hjønnevåg | e194fd8 | 2014-02-17 13:58:29 -0800 | [diff] [blame] | 5337 | continue; |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 5338 | } |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5339 | |
| 5340 | death++; |
| 5341 | |
Martijn Coenen | f9eac64 | 2017-05-22 11:26:23 -0700 | [diff] [blame] | 5342 | BUG_ON(!list_empty(&ref->death->work.entry)); |
| 5343 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 5344 | binder_enqueue_work_ilocked(&ref->death->work, |
| 5345 | &ref->proc->todo); |
Martijn Coenen | 053be42 | 2017-06-06 15:17:46 -0700 | [diff] [blame] | 5346 | binder_wakeup_proc_ilocked(ref->proc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5347 | binder_inner_proc_unlock(ref->proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5348 | } |
| 5349 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5350 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 5351 | "node %d now dead, refs %d, death %d\n", |
| 5352 | node->debug_id, refs, death); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5353 | binder_node_unlock(node); |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5354 | binder_put_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5355 | |
| 5356 | return refs; |
| 5357 | } |
| 5358 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5359 | static void binder_deferred_release(struct binder_proc *proc) |
| 5360 | { |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 5361 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5362 | struct rb_node *n; |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5363 | int threads, nodes, incoming_refs, outgoing_refs, active_transactions; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5364 | |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5365 | BUG_ON(proc->files); |
| 5366 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5367 | mutex_lock(&binder_procs_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5368 | hlist_del(&proc->proc_node); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5369 | mutex_unlock(&binder_procs_lock); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5370 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5371 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 5372 | if (context->binder_context_mgr_node && |
| 5373 | context->binder_context_mgr_node->proc == proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5374 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 5375 | "%s: %d context_mgr_node gone\n", |
| 5376 | __func__, proc->pid); |
Martijn Coenen | 0b3311e | 2016-09-30 15:51:48 +0200 | [diff] [blame] | 5377 | context->binder_context_mgr_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5378 | } |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5379 | mutex_unlock(&context->context_mgr_node_lock); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5380 | binder_inner_proc_lock(proc); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5381 | /* |
| 5382 | * Make sure proc stays alive after we |
| 5383 | * remove all the threads |
| 5384 | */ |
| 5385 | proc->tmp_ref++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5386 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5387 | proc->is_dead = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5388 | threads = 0; |
| 5389 | active_transactions = 0; |
| 5390 | while ((n = rb_first(&proc->threads))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5391 | struct binder_thread *thread; |
| 5392 | |
| 5393 | thread = rb_entry(n, struct binder_thread, rb_node); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5394 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5395 | threads++; |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5396 | active_transactions += binder_thread_release(proc, thread); |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5397 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5398 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5399 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5400 | nodes = 0; |
| 5401 | incoming_refs = 0; |
| 5402 | while ((n = rb_first(&proc->nodes))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5403 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5404 | |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5405 | node = rb_entry(n, struct binder_node, rb_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5406 | nodes++; |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5407 | /* |
| 5408 | * take a temporary ref on the node before |
| 5409 | * calling binder_node_release() which will either |
| 5410 | * kfree() the node or call binder_put_node() |
| 5411 | */ |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5412 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5413 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5414 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5415 | incoming_refs = binder_node_release(node, incoming_refs); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5416 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5417 | } |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5418 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5419 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5420 | outgoing_refs = 0; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5421 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5422 | while ((n = rb_first(&proc->refs_by_desc))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5423 | struct binder_ref *ref; |
| 5424 | |
| 5425 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5426 | outgoing_refs++; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5427 | binder_cleanup_ref_olocked(ref); |
| 5428 | binder_proc_unlock(proc); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5429 | binder_free_ref(ref); |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5430 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5431 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5432 | binder_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5433 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5434 | binder_release_work(proc, &proc->todo); |
| 5435 | binder_release_work(proc, &proc->delivered_death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5436 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5437 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5438 | "%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] | 5439 | __func__, proc->pid, threads, nodes, incoming_refs, |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5440 | outgoing_refs, active_transactions); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5441 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5442 | binder_proc_dec_tmpref(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5443 | } |
| 5444 | |
| 5445 | static void binder_deferred_func(struct work_struct *work) |
| 5446 | { |
| 5447 | struct binder_proc *proc; |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5448 | struct files_struct *files; |
| 5449 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5450 | int defer; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5451 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5452 | do { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5453 | mutex_lock(&binder_deferred_lock); |
| 5454 | if (!hlist_empty(&binder_deferred_list)) { |
| 5455 | proc = hlist_entry(binder_deferred_list.first, |
| 5456 | struct binder_proc, deferred_work_node); |
| 5457 | hlist_del_init(&proc->deferred_work_node); |
| 5458 | defer = proc->deferred_work; |
| 5459 | proc->deferred_work = 0; |
| 5460 | } else { |
| 5461 | proc = NULL; |
| 5462 | defer = 0; |
| 5463 | } |
| 5464 | mutex_unlock(&binder_deferred_lock); |
| 5465 | |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5466 | files = NULL; |
| 5467 | if (defer & BINDER_DEFERRED_PUT_FILES) { |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5468 | mutex_lock(&proc->files_lock); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5469 | files = proc->files; |
| 5470 | if (files) |
| 5471 | proc->files = NULL; |
Todd Kjos | fbb4339 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5472 | mutex_unlock(&proc->files_lock); |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5473 | } |
| 5474 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5475 | if (defer & BINDER_DEFERRED_FLUSH) |
| 5476 | binder_deferred_flush(proc); |
| 5477 | |
| 5478 | if (defer & BINDER_DEFERRED_RELEASE) |
| 5479 | binder_deferred_release(proc); /* frees proc */ |
Martijn Coenen | 6f7e5f9 | 2018-06-15 11:53:36 +0200 | [diff] [blame] | 5480 | |
| 5481 | if (files) |
| 5482 | put_files_struct(files); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5483 | } while (proc); |
| 5484 | } |
| 5485 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); |
| 5486 | |
| 5487 | static void |
| 5488 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) |
| 5489 | { |
| 5490 | mutex_lock(&binder_deferred_lock); |
| 5491 | proc->deferred_work |= defer; |
| 5492 | if (hlist_unhashed(&proc->deferred_work_node)) { |
| 5493 | hlist_add_head(&proc->deferred_work_node, |
| 5494 | &binder_deferred_list); |
Bhaktipriya Shridhar | 1beba52 | 2016-08-13 22:16:24 +0530 | [diff] [blame] | 5495 | schedule_work(&binder_deferred_work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5496 | } |
| 5497 | mutex_unlock(&binder_deferred_lock); |
| 5498 | } |
| 5499 | |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5500 | static void print_binder_transaction_ilocked(struct seq_file *m, |
| 5501 | struct binder_proc *proc, |
| 5502 | const char *prefix, |
| 5503 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5504 | { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5505 | struct binder_proc *to_proc; |
| 5506 | struct binder_buffer *buffer = t->buffer; |
| 5507 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5508 | spin_lock(&t->lock); |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5509 | to_proc = t->to_proc; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5510 | seq_printf(m, |
Todd Kjos | f540ce0 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 5511 | "%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] | 5512 | prefix, t->debug_id, t, |
| 5513 | t->from ? t->from->proc->pid : 0, |
| 5514 | t->from ? t->from->pid : 0, |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5515 | to_proc ? to_proc->pid : 0, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5516 | t->to_thread ? t->to_thread->pid : 0, |
Martijn Coenen | 57b2ac6 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 5517 | t->code, t->flags, t->priority.sched_policy, |
| 5518 | t->priority.prio, t->need_reply); |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5519 | spin_unlock(&t->lock); |
| 5520 | |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5521 | if (proc != to_proc) { |
| 5522 | /* |
| 5523 | * Can only safely deref buffer if we are holding the |
| 5524 | * correct proc inner lock for this node |
| 5525 | */ |
| 5526 | seq_puts(m, "\n"); |
| 5527 | return; |
| 5528 | } |
| 5529 | |
| 5530 | if (buffer == NULL) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5531 | seq_puts(m, " buffer free\n"); |
| 5532 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5533 | } |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5534 | if (buffer->target_node) |
| 5535 | seq_printf(m, " node %d", buffer->target_node->debug_id); |
Todd Kjos | f540ce0 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 5536 | seq_printf(m, " size %zd:%zd data %pK\n", |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5537 | buffer->data_size, buffer->offsets_size, |
Todd Kjos | 8539b1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 5538 | buffer->user_data); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5539 | } |
| 5540 | |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5541 | static void print_binder_work_ilocked(struct seq_file *m, |
| 5542 | struct binder_proc *proc, |
| 5543 | const char *prefix, |
| 5544 | const char *transaction_prefix, |
| 5545 | struct binder_work *w) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5546 | { |
| 5547 | struct binder_node *node; |
| 5548 | struct binder_transaction *t; |
| 5549 | |
| 5550 | switch (w->type) { |
| 5551 | case BINDER_WORK_TRANSACTION: |
| 5552 | t = container_of(w, struct binder_transaction, work); |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5553 | print_binder_transaction_ilocked( |
| 5554 | m, proc, transaction_prefix, t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5555 | break; |
Todd Kjos | 858b8da | 2017-04-21 17:35:12 -0700 | [diff] [blame] | 5556 | case BINDER_WORK_RETURN_ERROR: { |
| 5557 | struct binder_error *e = container_of( |
| 5558 | w, struct binder_error, work); |
| 5559 | |
| 5560 | seq_printf(m, "%stransaction error: %u\n", |
| 5561 | prefix, e->cmd); |
| 5562 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5563 | case BINDER_WORK_TRANSACTION_COMPLETE: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5564 | seq_printf(m, "%stransaction complete\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5565 | break; |
| 5566 | case BINDER_WORK_NODE: |
| 5567 | node = container_of(w, struct binder_node, work); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5568 | seq_printf(m, "%snode work %d: u%016llx c%016llx\n", |
| 5569 | prefix, node->debug_id, |
| 5570 | (u64)node->ptr, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5571 | break; |
| 5572 | case BINDER_WORK_DEAD_BINDER: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5573 | seq_printf(m, "%shas dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5574 | break; |
| 5575 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5576 | seq_printf(m, "%shas cleared dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5577 | break; |
| 5578 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5579 | seq_printf(m, "%shas cleared death notification\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5580 | break; |
| 5581 | default: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5582 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5583 | break; |
| 5584 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5585 | } |
| 5586 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5587 | static void print_binder_thread_ilocked(struct seq_file *m, |
| 5588 | struct binder_thread *thread, |
| 5589 | int print_always) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5590 | { |
| 5591 | struct binder_transaction *t; |
| 5592 | struct binder_work *w; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5593 | size_t start_pos = m->count; |
| 5594 | size_t header_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5595 | |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5596 | 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] | 5597 | thread->pid, thread->looper, |
Todd Kjos | 2f993e2 | 2017-05-12 14:42:55 -0700 | [diff] [blame] | 5598 | thread->looper_need_return, |
| 5599 | atomic_read(&thread->tmp_ref)); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5600 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5601 | t = thread->transaction_stack; |
| 5602 | while (t) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5603 | if (t->from == thread) { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5604 | print_binder_transaction_ilocked(m, thread->proc, |
| 5605 | " outgoing transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5606 | t = t->from_parent; |
| 5607 | } else if (t->to_thread == thread) { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5608 | print_binder_transaction_ilocked(m, thread->proc, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5609 | " incoming transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5610 | t = t->to_parent; |
| 5611 | } else { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5612 | print_binder_transaction_ilocked(m, thread->proc, |
| 5613 | " bad transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5614 | t = NULL; |
| 5615 | } |
| 5616 | } |
| 5617 | list_for_each_entry(w, &thread->todo, entry) { |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5618 | print_binder_work_ilocked(m, thread->proc, " ", |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5619 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5620 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5621 | if (!print_always && m->count == header_pos) |
| 5622 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5623 | } |
| 5624 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5625 | static void print_binder_node_nilocked(struct seq_file *m, |
| 5626 | struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5627 | { |
| 5628 | struct binder_ref *ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5629 | struct binder_work *w; |
| 5630 | int count; |
| 5631 | |
| 5632 | count = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5633 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5634 | count++; |
| 5635 | |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 5636 | 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] | 5637 | node->debug_id, (u64)node->ptr, (u64)node->cookie, |
Martijn Coenen | 6aac979 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 5638 | node->sched_policy, node->min_priority, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5639 | node->has_strong_ref, node->has_weak_ref, |
| 5640 | node->local_strong_refs, node->local_weak_refs, |
Todd Kjos | f22abc7 | 2017-05-09 11:08:05 -0700 | [diff] [blame] | 5641 | node->internal_strong_refs, count, node->tmp_refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5642 | if (count) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5643 | seq_puts(m, " proc"); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5644 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5645 | seq_printf(m, " %d", ref->proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5646 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5647 | seq_puts(m, "\n"); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5648 | if (node->proc) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5649 | list_for_each_entry(w, &node->async_todo, entry) |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5650 | print_binder_work_ilocked(m, node->proc, " ", |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5651 | " pending async transaction", w); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5652 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5653 | } |
| 5654 | |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5655 | static void print_binder_ref_olocked(struct seq_file *m, |
| 5656 | struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5657 | { |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5658 | binder_node_lock(ref->node); |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5659 | seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n", |
| 5660 | ref->data.debug_id, ref->data.desc, |
| 5661 | ref->node->proc ? "" : "dead ", |
| 5662 | ref->node->debug_id, ref->data.strong, |
| 5663 | ref->data.weak, ref->death); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5664 | binder_node_unlock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5665 | } |
| 5666 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5667 | static void print_binder_proc(struct seq_file *m, |
| 5668 | struct binder_proc *proc, int print_all) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5669 | { |
| 5670 | struct binder_work *w; |
| 5671 | struct rb_node *n; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5672 | size_t start_pos = m->count; |
| 5673 | size_t header_pos; |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5674 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5675 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5676 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5677 | seq_printf(m, "context %s\n", proc->context->name); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5678 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5679 | |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5680 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5681 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5682 | 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] | 5683 | rb_node), print_all); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5684 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5685 | 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] | 5686 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 5687 | rb_node); |
Todd Kjos | d79aa41 | 2018-12-05 15:19:26 -0800 | [diff] [blame] | 5688 | if (!print_all && !node->has_async_transaction) |
| 5689 | continue; |
| 5690 | |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5691 | /* |
| 5692 | * take a temporary reference on the node so it |
| 5693 | * survives and isn't removed from the tree |
| 5694 | * while we print it. |
| 5695 | */ |
| 5696 | binder_inc_node_tmpref_ilocked(node); |
| 5697 | /* Need to drop inner lock to take node lock */ |
| 5698 | binder_inner_proc_unlock(proc); |
| 5699 | if (last_node) |
| 5700 | binder_put_node(last_node); |
| 5701 | binder_node_inner_lock(node); |
| 5702 | print_binder_node_nilocked(m, node); |
| 5703 | binder_node_inner_unlock(node); |
| 5704 | last_node = node; |
| 5705 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5706 | } |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5707 | binder_inner_proc_unlock(proc); |
| 5708 | if (last_node) |
| 5709 | binder_put_node(last_node); |
| 5710 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5711 | if (print_all) { |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5712 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5713 | for (n = rb_first(&proc->refs_by_desc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5714 | n != NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5715 | n = rb_next(n)) |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5716 | print_binder_ref_olocked(m, rb_entry(n, |
| 5717 | struct binder_ref, |
| 5718 | rb_node_desc)); |
| 5719 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5720 | } |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5721 | binder_alloc_print_allocated(m, &proc->alloc); |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5722 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5723 | list_for_each_entry(w, &proc->todo, entry) |
Todd Kjos | 6d241a4 | 2017-04-21 14:32:11 -0700 | [diff] [blame] | 5724 | print_binder_work_ilocked(m, proc, " ", |
| 5725 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5726 | list_for_each_entry(w, &proc->delivered_death, entry) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5727 | seq_puts(m, " has delivered dead binder\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5728 | break; |
| 5729 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5730 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5731 | if (!print_all && m->count == header_pos) |
| 5732 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5733 | } |
| 5734 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5735 | static const char * const binder_return_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5736 | "BR_ERROR", |
| 5737 | "BR_OK", |
| 5738 | "BR_TRANSACTION", |
| 5739 | "BR_REPLY", |
| 5740 | "BR_ACQUIRE_RESULT", |
| 5741 | "BR_DEAD_REPLY", |
| 5742 | "BR_TRANSACTION_COMPLETE", |
| 5743 | "BR_INCREFS", |
| 5744 | "BR_ACQUIRE", |
| 5745 | "BR_RELEASE", |
| 5746 | "BR_DECREFS", |
| 5747 | "BR_ATTEMPT_ACQUIRE", |
| 5748 | "BR_NOOP", |
| 5749 | "BR_SPAWN_LOOPER", |
| 5750 | "BR_FINISHED", |
| 5751 | "BR_DEAD_BINDER", |
| 5752 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 5753 | "BR_FAILED_REPLY" |
| 5754 | }; |
| 5755 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5756 | static const char * const binder_command_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5757 | "BC_TRANSACTION", |
| 5758 | "BC_REPLY", |
| 5759 | "BC_ACQUIRE_RESULT", |
| 5760 | "BC_FREE_BUFFER", |
| 5761 | "BC_INCREFS", |
| 5762 | "BC_ACQUIRE", |
| 5763 | "BC_RELEASE", |
| 5764 | "BC_DECREFS", |
| 5765 | "BC_INCREFS_DONE", |
| 5766 | "BC_ACQUIRE_DONE", |
| 5767 | "BC_ATTEMPT_ACQUIRE", |
| 5768 | "BC_REGISTER_LOOPER", |
| 5769 | "BC_ENTER_LOOPER", |
| 5770 | "BC_EXIT_LOOPER", |
| 5771 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 5772 | "BC_CLEAR_DEATH_NOTIFICATION", |
Martijn Coenen | 5a6da53 | 2016-09-30 14:10:07 +0200 | [diff] [blame] | 5773 | "BC_DEAD_BINDER_DONE", |
| 5774 | "BC_TRANSACTION_SG", |
| 5775 | "BC_REPLY_SG", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5776 | }; |
| 5777 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5778 | static const char * const binder_objstat_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5779 | "proc", |
| 5780 | "thread", |
| 5781 | "node", |
| 5782 | "ref", |
| 5783 | "death", |
| 5784 | "transaction", |
| 5785 | "transaction_complete" |
| 5786 | }; |
| 5787 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5788 | static void print_binder_stats(struct seq_file *m, const char *prefix, |
| 5789 | struct binder_stats *stats) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5790 | { |
| 5791 | int i; |
| 5792 | |
| 5793 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5794 | ARRAY_SIZE(binder_command_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5795 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5796 | int temp = atomic_read(&stats->bc[i]); |
| 5797 | |
| 5798 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5799 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5800 | binder_command_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5801 | } |
| 5802 | |
| 5803 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5804 | ARRAY_SIZE(binder_return_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5805 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5806 | int temp = atomic_read(&stats->br[i]); |
| 5807 | |
| 5808 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5809 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5810 | binder_return_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5811 | } |
| 5812 | |
| 5813 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5814 | ARRAY_SIZE(binder_objstat_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5815 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5816 | ARRAY_SIZE(stats->obj_deleted)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5817 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5818 | int created = atomic_read(&stats->obj_created[i]); |
| 5819 | int deleted = atomic_read(&stats->obj_deleted[i]); |
| 5820 | |
| 5821 | if (created || deleted) |
| 5822 | seq_printf(m, "%s%s: active %d total %d\n", |
| 5823 | prefix, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5824 | binder_objstat_strings[i], |
Badhri Jagan Sridharan | 5551ff2 | 2016-10-13 16:36:15 -0700 | [diff] [blame] | 5825 | created - deleted, |
| 5826 | created); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5827 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5828 | } |
| 5829 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5830 | static void print_binder_proc_stats(struct seq_file *m, |
| 5831 | struct binder_proc *proc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5832 | { |
| 5833 | struct binder_work *w; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5834 | struct binder_thread *thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5835 | struct rb_node *n; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5836 | int count, strong, weak, ready_threads; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5837 | size_t free_async_space = |
| 5838 | binder_alloc_get_free_async_space(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5839 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5840 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5841 | seq_printf(m, "context %s\n", proc->context->name); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5842 | count = 0; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5843 | ready_threads = 0; |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5844 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5845 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 5846 | count++; |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5847 | |
| 5848 | list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node) |
| 5849 | ready_threads++; |
| 5850 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5851 | seq_printf(m, " threads: %d\n", count); |
| 5852 | seq_printf(m, " requested threads: %d+%d/%d\n" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5853 | " ready threads %d\n" |
| 5854 | " free async space %zd\n", proc->requested_threads, |
| 5855 | proc->requested_threads_started, proc->max_threads, |
Martijn Coenen | 22d64e432 | 2017-06-02 11:15:44 -0700 | [diff] [blame] | 5856 | ready_threads, |
Todd Kjos | b482790 | 2017-05-25 15:52:17 -0700 | [diff] [blame] | 5857 | free_async_space); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5858 | count = 0; |
| 5859 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) |
| 5860 | count++; |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5861 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5862 | seq_printf(m, " nodes: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5863 | count = 0; |
| 5864 | strong = 0; |
| 5865 | weak = 0; |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5866 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5867 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 5868 | struct binder_ref *ref = rb_entry(n, struct binder_ref, |
| 5869 | rb_node_desc); |
| 5870 | count++; |
Todd Kjos | b0117bb | 2017-05-08 09:16:27 -0700 | [diff] [blame] | 5871 | strong += ref->data.strong; |
| 5872 | weak += ref->data.weak; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5873 | } |
Todd Kjos | 5346bf3 | 2016-10-20 16:43:34 -0700 | [diff] [blame] | 5874 | binder_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5875 | 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] | 5876 | |
Todd Kjos | d325d37 | 2016-10-10 10:40:53 -0700 | [diff] [blame] | 5877 | count = binder_alloc_get_allocated_count(&proc->alloc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5878 | seq_printf(m, " buffers: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5879 | |
Sherry Yang | 9100442 | 2017-08-22 17:26:57 -0700 | [diff] [blame] | 5880 | binder_alloc_print_pages(m, &proc->alloc); |
| 5881 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5882 | count = 0; |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5883 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5884 | list_for_each_entry(w, &proc->todo, entry) { |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5885 | if (w->type == BINDER_WORK_TRANSACTION) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5886 | count++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5887 | } |
Todd Kjos | 1c89e6b | 2016-10-20 10:33:00 -0700 | [diff] [blame] | 5888 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5889 | seq_printf(m, " pending transactions: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5890 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5891 | print_binder_stats(m, " ", &proc->stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5892 | } |
| 5893 | |
| 5894 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5895 | static int binder_state_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5896 | { |
| 5897 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5898 | struct binder_node *node; |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5899 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5900 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5901 | seq_puts(m, "binder state:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5902 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5903 | spin_lock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5904 | if (!hlist_empty(&binder_dead_nodes)) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5905 | seq_puts(m, "dead nodes:\n"); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5906 | hlist_for_each_entry(node, &binder_dead_nodes, dead_node) { |
| 5907 | /* |
| 5908 | * take a temporary reference on the node so it |
| 5909 | * survives and isn't removed from the list |
| 5910 | * while we print it. |
| 5911 | */ |
| 5912 | node->tmp_refs++; |
| 5913 | spin_unlock(&binder_dead_nodes_lock); |
| 5914 | if (last_node) |
| 5915 | binder_put_node(last_node); |
| 5916 | binder_node_lock(node); |
Todd Kjos | 425d23f | 2017-06-12 12:07:26 -0700 | [diff] [blame] | 5917 | print_binder_node_nilocked(m, node); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5918 | binder_node_unlock(node); |
| 5919 | last_node = node; |
| 5920 | spin_lock(&binder_dead_nodes_lock); |
| 5921 | } |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5922 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | cbcbbd6 | 2017-06-08 13:45:59 -0700 | [diff] [blame] | 5923 | if (last_node) |
| 5924 | binder_put_node(last_node); |
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 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5927 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5928 | print_binder_proc(m, proc, 1); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5929 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5930 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5931 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5932 | } |
| 5933 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5934 | static int binder_stats_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5935 | { |
| 5936 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5937 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5938 | seq_puts(m, "binder stats:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5939 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5940 | print_binder_stats(m, "", &binder_stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5941 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5942 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5943 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5944 | print_binder_proc_stats(m, proc); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5945 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5946 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5947 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5948 | } |
| 5949 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5950 | static int binder_transactions_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5951 | { |
| 5952 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5953 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5954 | seq_puts(m, "binder transactions:\n"); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5955 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5956 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5957 | print_binder_proc(m, proc, 0); |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5958 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | 218b697 | 2016-11-14 11:37:41 -0800 | [diff] [blame] | 5959 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5960 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5961 | } |
| 5962 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5963 | static int binder_proc_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5964 | { |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5965 | struct binder_proc *itr; |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5966 | int pid = (unsigned long)m->private; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5967 | |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5968 | mutex_lock(&binder_procs_lock); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5969 | hlist_for_each_entry(itr, &binder_procs, proc_node) { |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5970 | if (itr->pid == pid) { |
| 5971 | seq_puts(m, "binder proc state:\n"); |
| 5972 | print_binder_proc(m, itr, 1); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5973 | } |
| 5974 | } |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 5975 | mutex_unlock(&binder_procs_lock); |
| 5976 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5977 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5978 | } |
| 5979 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5980 | static void print_binder_transaction_log_entry(struct seq_file *m, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5981 | struct binder_transaction_log_entry *e) |
| 5982 | { |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5983 | int debug_id = READ_ONCE(e->debug_id_done); |
| 5984 | /* |
| 5985 | * read barrier to guarantee debug_id_done read before |
| 5986 | * we print the log values |
| 5987 | */ |
| 5988 | smp_rmb(); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5989 | seq_printf(m, |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5990 | "%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] | 5991 | e->debug_id, (e->call_type == 2) ? "reply" : |
| 5992 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, |
Martijn Coenen | 63b9f3b | 2016-10-17 15:17:31 +0200 | [diff] [blame] | 5993 | e->from_thread, e->to_proc, e->to_thread, e->context_name, |
Todd Kjos | e598d17 | 2017-03-22 17:19:52 -0700 | [diff] [blame] | 5994 | e->to_node, e->target_handle, e->data_size, e->offsets_size, |
| 5995 | e->return_error, e->return_error_param, |
| 5996 | e->return_error_line); |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 5997 | /* |
| 5998 | * read-barrier to guarantee read of debug_id_done after |
| 5999 | * done printing the fields of the entry |
| 6000 | */ |
| 6001 | smp_rmb(); |
| 6002 | seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ? |
| 6003 | "\n" : " (incomplete)\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6004 | } |
| 6005 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6006 | 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] | 6007 | { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6008 | struct binder_transaction_log *log = m->private; |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 6009 | unsigned int log_cur = atomic_read(&log->cur); |
| 6010 | unsigned int count; |
| 6011 | unsigned int cur; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6012 | int i; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6013 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 6014 | count = log_cur + 1; |
| 6015 | cur = count < ARRAY_SIZE(log->entry) && !log->full ? |
| 6016 | 0 : count % ARRAY_SIZE(log->entry); |
| 6017 | if (count > ARRAY_SIZE(log->entry) || log->full) |
| 6018 | count = ARRAY_SIZE(log->entry); |
| 6019 | for (i = 0; i < count; i++) { |
| 6020 | unsigned int index = cur++ % ARRAY_SIZE(log->entry); |
| 6021 | |
| 6022 | print_binder_transaction_log_entry(m, &log->entry[index]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6023 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6024 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6025 | } |
| 6026 | |
| 6027 | static const struct file_operations binder_fops = { |
| 6028 | .owner = THIS_MODULE, |
| 6029 | .poll = binder_poll, |
| 6030 | .unlocked_ioctl = binder_ioctl, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 6031 | .compat_ioctl = binder_ioctl, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6032 | .mmap = binder_mmap, |
| 6033 | .open = binder_open, |
| 6034 | .flush = binder_flush, |
| 6035 | .release = binder_release, |
| 6036 | }; |
| 6037 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6038 | BINDER_DEBUG_ENTRY(state); |
| 6039 | BINDER_DEBUG_ENTRY(stats); |
| 6040 | BINDER_DEBUG_ENTRY(transactions); |
| 6041 | BINDER_DEBUG_ENTRY(transaction_log); |
| 6042 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6043 | static int __init init_binder_device(const char *name) |
| 6044 | { |
| 6045 | int ret; |
| 6046 | struct binder_device *binder_device; |
| 6047 | |
| 6048 | binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL); |
| 6049 | if (!binder_device) |
| 6050 | return -ENOMEM; |
| 6051 | |
| 6052 | binder_device->miscdev.fops = &binder_fops; |
| 6053 | binder_device->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 6054 | binder_device->miscdev.name = name; |
| 6055 | |
| 6056 | binder_device->context.binder_context_mgr_uid = INVALID_UID; |
| 6057 | binder_device->context.name = name; |
Todd Kjos | 8d9f6f3 | 2016-10-17 12:33:15 -0700 | [diff] [blame] | 6058 | mutex_init(&binder_device->context.context_mgr_node_lock); |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6059 | |
| 6060 | ret = misc_register(&binder_device->miscdev); |
| 6061 | if (ret < 0) { |
| 6062 | kfree(binder_device); |
| 6063 | return ret; |
| 6064 | } |
| 6065 | |
| 6066 | hlist_add_head(&binder_device->hlist, &binder_devices); |
| 6067 | |
| 6068 | return ret; |
| 6069 | } |
| 6070 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6071 | static int __init binder_init(void) |
| 6072 | { |
| 6073 | int ret; |
Christian Brauner | 558ee93 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 6074 | char *device_name, *device_names, *device_tmp; |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6075 | struct binder_device *device; |
| 6076 | struct hlist_node *tmp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6077 | |
Tetsuo Handa | f8cb822 | 2017-11-29 22:29:47 +0900 | [diff] [blame] | 6078 | ret = binder_alloc_shrinker_init(); |
| 6079 | if (ret) |
| 6080 | return ret; |
Sherry Yang | 5828d70 | 2017-07-29 13:24:11 -0700 | [diff] [blame] | 6081 | |
Todd Kjos | 1cfe627 | 2017-05-24 13:33:28 -0700 | [diff] [blame] | 6082 | atomic_set(&binder_transaction_log.cur, ~0U); |
| 6083 | atomic_set(&binder_transaction_log_failed.cur, ~0U); |
| 6084 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6085 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); |
| 6086 | if (binder_debugfs_dir_entry_root) |
| 6087 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", |
| 6088 | binder_debugfs_dir_entry_root); |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6089 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6090 | if (binder_debugfs_dir_entry_root) { |
| 6091 | debugfs_create_file("state", |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6092 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6093 | binder_debugfs_dir_entry_root, |
| 6094 | NULL, |
| 6095 | &binder_state_fops); |
| 6096 | debugfs_create_file("stats", |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6097 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6098 | binder_debugfs_dir_entry_root, |
| 6099 | NULL, |
| 6100 | &binder_stats_fops); |
| 6101 | debugfs_create_file("transactions", |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6102 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6103 | binder_debugfs_dir_entry_root, |
| 6104 | NULL, |
| 6105 | &binder_transactions_fops); |
| 6106 | debugfs_create_file("transaction_log", |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6107 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6108 | binder_debugfs_dir_entry_root, |
| 6109 | &binder_transaction_log, |
| 6110 | &binder_transaction_log_fops); |
| 6111 | debugfs_create_file("failed_transaction_log", |
Harsh Shandilya | 174562a | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6112 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6113 | binder_debugfs_dir_entry_root, |
| 6114 | &binder_transaction_log_failed, |
| 6115 | &binder_transaction_log_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6116 | } |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6117 | |
| 6118 | /* |
| 6119 | * Copy the module_parameter string, because we don't want to |
| 6120 | * tokenize it in-place. |
| 6121 | */ |
| 6122 | device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL); |
| 6123 | if (!device_names) { |
| 6124 | ret = -ENOMEM; |
| 6125 | goto err_alloc_device_names_failed; |
| 6126 | } |
| 6127 | strcpy(device_names, binder_devices_param); |
| 6128 | |
Christian Brauner | 558ee93 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 6129 | device_tmp = device_names; |
| 6130 | while ((device_name = strsep(&device_tmp, ","))) { |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6131 | ret = init_binder_device(device_name); |
| 6132 | if (ret) |
| 6133 | goto err_init_binder_device_failed; |
| 6134 | } |
| 6135 | |
| 6136 | return ret; |
| 6137 | |
| 6138 | err_init_binder_device_failed: |
| 6139 | hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) { |
| 6140 | misc_deregister(&device->miscdev); |
| 6141 | hlist_del(&device->hlist); |
| 6142 | kfree(device); |
| 6143 | } |
Christian Brauner | 558ee93 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 6144 | |
| 6145 | kfree(device_names); |
| 6146 | |
Martijn Coenen | 6b7c712 | 2016-09-30 16:08:09 +0200 | [diff] [blame] | 6147 | err_alloc_device_names_failed: |
| 6148 | debugfs_remove_recursive(binder_debugfs_dir_entry_root); |
| 6149 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6150 | return ret; |
| 6151 | } |
| 6152 | |
| 6153 | device_initcall(binder_init); |
| 6154 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 6155 | #define CREATE_TRACE_POINTS |
| 6156 | #include "binder_trace.h" |
| 6157 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6158 | MODULE_LICENSE("GPL v2"); |