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 | 9630fe8 | 2017-06-29 12:02:00 -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 | 1b77e9d | 2017-08-31 10:04:18 +0200 | [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 | 9630fe8 | 2017-06-29 12:02:00 -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 <linux/fdtable.h> |
| 55 | #include <linux/file.h> |
Colin Cross | e2610b2 | 2013-05-06 23:50:15 +0000 | [diff] [blame] | 56 | #include <linux/freezer.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 57 | #include <linux/fs.h> |
| 58 | #include <linux/list.h> |
| 59 | #include <linux/miscdevice.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 60 | #include <linux/module.h> |
| 61 | #include <linux/mutex.h> |
| 62 | #include <linux/nsproxy.h> |
| 63 | #include <linux/poll.h> |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 64 | #include <linux/debugfs.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 65 | #include <linux/rbtree.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 66 | #include <linux/sched/signal.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 67 | #include <linux/sched/mm.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 | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 72 | #include <linux/spinlock.h> |
Sherry Yang | 128f380 | 2018-08-07 12:57:13 -0700 | [diff] [blame] | 73 | #include <linux/ratelimit.h> |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 74 | #include <linux/syscalls.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 75 | |
Greg Kroah-Hartman | 9246a4a | 2014-10-16 15:26:51 +0200 | [diff] [blame] | 76 | #include <uapi/linux/android/binder.h> |
Guenter Roeck | f371a7c | 2018-07-23 14:41:38 -0700 | [diff] [blame] | 77 | |
| 78 | #include <asm/cacheflush.h> |
| 79 | |
Todd Kjos | 0c972a0 | 2017-06-29 12:01:41 -0700 | [diff] [blame] | 80 | #include "binder_alloc.h" |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 81 | #include "binder_trace.h" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 82 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 83 | static HLIST_HEAD(binder_deferred_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 84 | static DEFINE_MUTEX(binder_deferred_lock); |
| 85 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 86 | static HLIST_HEAD(binder_devices); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 87 | static HLIST_HEAD(binder_procs); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 88 | static DEFINE_MUTEX(binder_procs_lock); |
| 89 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 90 | static HLIST_HEAD(binder_dead_nodes); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 91 | static DEFINE_SPINLOCK(binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 92 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 93 | static struct dentry *binder_debugfs_dir_entry_root; |
| 94 | static struct dentry *binder_debugfs_dir_entry_proc; |
Todd Kjos | 656a800 | 2017-06-29 12:01:45 -0700 | [diff] [blame] | 95 | static atomic_t binder_last_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 96 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 97 | #define BINDER_DEBUG_ENTRY(name) \ |
| 98 | static int binder_##name##_open(struct inode *inode, struct file *file) \ |
| 99 | { \ |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 100 | return single_open(file, binder_##name##_show, inode->i_private); \ |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 101 | } \ |
| 102 | \ |
| 103 | static const struct file_operations binder_##name##_fops = { \ |
| 104 | .owner = THIS_MODULE, \ |
| 105 | .open = binder_##name##_open, \ |
| 106 | .read = seq_read, \ |
| 107 | .llseek = seq_lseek, \ |
| 108 | .release = single_release, \ |
| 109 | } |
| 110 | |
| 111 | static int binder_proc_show(struct seq_file *m, void *unused); |
| 112 | BINDER_DEBUG_ENTRY(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 113 | |
| 114 | /* This is only defined in include/asm-arm/sizes.h */ |
| 115 | #ifndef SZ_1K |
| 116 | #define SZ_1K 0x400 |
| 117 | #endif |
| 118 | |
| 119 | #ifndef SZ_4M |
| 120 | #define SZ_4M 0x400000 |
| 121 | #endif |
| 122 | |
| 123 | #define FORBIDDEN_MMAP_FLAGS (VM_WRITE) |
| 124 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 125 | enum { |
| 126 | BINDER_DEBUG_USER_ERROR = 1U << 0, |
| 127 | BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1, |
| 128 | BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2, |
| 129 | BINDER_DEBUG_OPEN_CLOSE = 1U << 3, |
| 130 | BINDER_DEBUG_DEAD_BINDER = 1U << 4, |
| 131 | BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5, |
| 132 | BINDER_DEBUG_READ_WRITE = 1U << 6, |
| 133 | BINDER_DEBUG_USER_REFS = 1U << 7, |
| 134 | BINDER_DEBUG_THREADS = 1U << 8, |
| 135 | BINDER_DEBUG_TRANSACTION = 1U << 9, |
| 136 | BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10, |
| 137 | BINDER_DEBUG_FREE_BUFFER = 1U << 11, |
| 138 | BINDER_DEBUG_INTERNAL_REFS = 1U << 12, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 139 | BINDER_DEBUG_PRIORITY_CAP = 1U << 13, |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 140 | BINDER_DEBUG_SPINLOCKS = 1U << 14, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 141 | }; |
| 142 | static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR | |
| 143 | BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION; |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 144 | module_param_named(debug_mask, binder_debug_mask, uint, 0644); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 145 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 146 | static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES; |
| 147 | module_param_named(devices, binder_devices_param, charp, 0444); |
| 148 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 149 | static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait); |
| 150 | static int binder_stop_on_user_error; |
| 151 | |
| 152 | static int binder_set_stop_on_user_error(const char *val, |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 153 | const struct kernel_param *kp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 154 | { |
| 155 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 156 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 157 | ret = param_set_int(val, kp); |
| 158 | if (binder_stop_on_user_error < 2) |
| 159 | wake_up(&binder_user_error_wait); |
| 160 | return ret; |
| 161 | } |
| 162 | module_param_call(stop_on_user_error, binder_set_stop_on_user_error, |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 163 | param_get_int, &binder_stop_on_user_error, 0644); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 164 | |
| 165 | #define binder_debug(mask, x...) \ |
| 166 | do { \ |
| 167 | if (binder_debug_mask & mask) \ |
Sherry Yang | 128f380 | 2018-08-07 12:57:13 -0700 | [diff] [blame] | 168 | pr_info_ratelimited(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 169 | } while (0) |
| 170 | |
| 171 | #define binder_user_error(x...) \ |
| 172 | do { \ |
| 173 | if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \ |
Sherry Yang | 128f380 | 2018-08-07 12:57:13 -0700 | [diff] [blame] | 174 | pr_info_ratelimited(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 175 | if (binder_stop_on_user_error) \ |
| 176 | binder_stop_on_user_error = 2; \ |
| 177 | } while (0) |
| 178 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 179 | #define to_flat_binder_object(hdr) \ |
| 180 | container_of(hdr, struct flat_binder_object, hdr) |
| 181 | |
| 182 | #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr) |
| 183 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 184 | #define to_binder_buffer_object(hdr) \ |
| 185 | container_of(hdr, struct binder_buffer_object, hdr) |
| 186 | |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 187 | #define to_binder_fd_array_object(hdr) \ |
| 188 | container_of(hdr, struct binder_fd_array_object, hdr) |
| 189 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 190 | enum binder_stat_types { |
| 191 | BINDER_STAT_PROC, |
| 192 | BINDER_STAT_THREAD, |
| 193 | BINDER_STAT_NODE, |
| 194 | BINDER_STAT_REF, |
| 195 | BINDER_STAT_DEATH, |
| 196 | BINDER_STAT_TRANSACTION, |
| 197 | BINDER_STAT_TRANSACTION_COMPLETE, |
| 198 | BINDER_STAT_COUNT |
| 199 | }; |
| 200 | |
| 201 | struct binder_stats { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 202 | atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1]; |
| 203 | atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1]; |
| 204 | atomic_t obj_created[BINDER_STAT_COUNT]; |
| 205 | atomic_t obj_deleted[BINDER_STAT_COUNT]; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | static struct binder_stats binder_stats; |
| 209 | |
| 210 | static inline void binder_stats_deleted(enum binder_stat_types type) |
| 211 | { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 212 | atomic_inc(&binder_stats.obj_deleted[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static inline void binder_stats_created(enum binder_stat_types type) |
| 216 | { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 217 | atomic_inc(&binder_stats.obj_created[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | struct binder_transaction_log_entry { |
| 221 | int debug_id; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 222 | int debug_id_done; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 223 | int call_type; |
| 224 | int from_proc; |
| 225 | int from_thread; |
| 226 | int target_handle; |
| 227 | int to_proc; |
| 228 | int to_thread; |
| 229 | int to_node; |
| 230 | int data_size; |
| 231 | int offsets_size; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 232 | int return_error_line; |
| 233 | uint32_t return_error; |
| 234 | uint32_t return_error_param; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 235 | const char *context_name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 236 | }; |
| 237 | struct binder_transaction_log { |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 238 | atomic_t cur; |
| 239 | bool full; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 240 | struct binder_transaction_log_entry entry[32]; |
| 241 | }; |
| 242 | static struct binder_transaction_log binder_transaction_log; |
| 243 | static struct binder_transaction_log binder_transaction_log_failed; |
| 244 | |
| 245 | static struct binder_transaction_log_entry *binder_transaction_log_add( |
| 246 | struct binder_transaction_log *log) |
| 247 | { |
| 248 | struct binder_transaction_log_entry *e; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 249 | unsigned int cur = atomic_inc_return(&log->cur); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 250 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 251 | if (cur >= ARRAY_SIZE(log->entry)) |
Gustavo A. R. Silva | 197410a | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 252 | log->full = true; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 253 | e = &log->entry[cur % ARRAY_SIZE(log->entry)]; |
| 254 | WRITE_ONCE(e->debug_id_done, 0); |
| 255 | /* |
| 256 | * write-barrier to synchronize access to e->debug_id_done. |
| 257 | * We make sure the initialized 0 value is seen before |
| 258 | * memset() other fields are zeroed by memset. |
| 259 | */ |
| 260 | smp_wmb(); |
| 261 | memset(e, 0, sizeof(*e)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 262 | return e; |
| 263 | } |
| 264 | |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 265 | struct binder_context { |
| 266 | struct binder_node *binder_context_mgr_node; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 267 | struct mutex context_mgr_node_lock; |
| 268 | |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 269 | kuid_t binder_context_mgr_uid; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 270 | const char *name; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 271 | }; |
| 272 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 273 | struct binder_device { |
| 274 | struct hlist_node hlist; |
| 275 | struct miscdevice miscdev; |
| 276 | struct binder_context context; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 277 | }; |
| 278 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 279 | /** |
| 280 | * struct binder_work - work enqueued on a worklist |
| 281 | * @entry: node enqueued on list |
| 282 | * @type: type of work to be performed |
| 283 | * |
| 284 | * There are separate work lists for proc, thread, and node (async). |
| 285 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 286 | struct binder_work { |
| 287 | struct list_head entry; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 288 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 289 | enum { |
| 290 | BINDER_WORK_TRANSACTION = 1, |
| 291 | BINDER_WORK_TRANSACTION_COMPLETE, |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 292 | BINDER_WORK_RETURN_ERROR, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 293 | BINDER_WORK_NODE, |
| 294 | BINDER_WORK_DEAD_BINDER, |
| 295 | BINDER_WORK_DEAD_BINDER_AND_CLEAR, |
| 296 | BINDER_WORK_CLEAR_DEATH_NOTIFICATION, |
| 297 | } type; |
| 298 | }; |
| 299 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 300 | struct binder_error { |
| 301 | struct binder_work work; |
| 302 | uint32_t cmd; |
| 303 | }; |
| 304 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 305 | /** |
| 306 | * struct binder_node - binder node bookkeeping |
| 307 | * @debug_id: unique ID for debugging |
| 308 | * (invariant after initialized) |
| 309 | * @lock: lock for node fields |
| 310 | * @work: worklist element for node work |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 311 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 312 | * @rb_node: element for proc->nodes tree |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 313 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 314 | * @dead_node: element for binder_dead_nodes list |
| 315 | * (protected by binder_dead_nodes_lock) |
| 316 | * @proc: binder_proc that owns this node |
| 317 | * (invariant after initialized) |
| 318 | * @refs: list of references on this node |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 319 | * (protected by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 320 | * @internal_strong_refs: used to take strong references when |
| 321 | * initiating a transaction |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 322 | * (protected by @proc->inner_lock if @proc |
| 323 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 324 | * @local_weak_refs: weak user refs from local process |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 325 | * (protected by @proc->inner_lock if @proc |
| 326 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 327 | * @local_strong_refs: strong user refs from local process |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 328 | * (protected by @proc->inner_lock if @proc |
| 329 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 330 | * @tmp_refs: temporary kernel refs |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 331 | * (protected by @proc->inner_lock while @proc |
| 332 | * is valid, and by binder_dead_nodes_lock |
| 333 | * if @proc is NULL. During inc/dec and node release |
| 334 | * it is also protected by @lock to provide safety |
| 335 | * as the node dies and @proc becomes NULL) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 336 | * @ptr: userspace pointer for node |
| 337 | * (invariant, no lock needed) |
| 338 | * @cookie: userspace cookie for node |
| 339 | * (invariant, no lock needed) |
| 340 | * @has_strong_ref: userspace notified of strong ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 341 | * (protected by @proc->inner_lock if @proc |
| 342 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 343 | * @pending_strong_ref: userspace has acked notification of strong ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 344 | * (protected by @proc->inner_lock if @proc |
| 345 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 346 | * @has_weak_ref: userspace notified of weak ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 347 | * (protected by @proc->inner_lock if @proc |
| 348 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 349 | * @pending_weak_ref: userspace has acked notification of weak ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 350 | * (protected by @proc->inner_lock if @proc |
| 351 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 352 | * @has_async_transaction: async transaction to node in progress |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 353 | * (protected by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 354 | * @accept_fds: file descriptor operations supported for node |
| 355 | * (invariant after initialized) |
| 356 | * @min_priority: minimum scheduling priority |
| 357 | * (invariant after initialized) |
| 358 | * @async_todo: list of async work items |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 359 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 360 | * |
| 361 | * Bookkeeping structure for binder nodes. |
| 362 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 363 | struct binder_node { |
| 364 | int debug_id; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 365 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 366 | struct binder_work work; |
| 367 | union { |
| 368 | struct rb_node rb_node; |
| 369 | struct hlist_node dead_node; |
| 370 | }; |
| 371 | struct binder_proc *proc; |
| 372 | struct hlist_head refs; |
| 373 | int internal_strong_refs; |
| 374 | int local_weak_refs; |
| 375 | int local_strong_refs; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 376 | int tmp_refs; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 377 | binder_uintptr_t ptr; |
| 378 | binder_uintptr_t cookie; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 379 | struct { |
| 380 | /* |
| 381 | * bitfield elements protected by |
| 382 | * proc inner_lock |
| 383 | */ |
| 384 | u8 has_strong_ref:1; |
| 385 | u8 pending_strong_ref:1; |
| 386 | u8 has_weak_ref:1; |
| 387 | u8 pending_weak_ref:1; |
| 388 | }; |
| 389 | struct { |
| 390 | /* |
| 391 | * invariant after initialization |
| 392 | */ |
| 393 | u8 accept_fds:1; |
| 394 | u8 min_priority; |
| 395 | }; |
| 396 | bool has_async_transaction; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 397 | struct list_head async_todo; |
| 398 | }; |
| 399 | |
| 400 | struct binder_ref_death { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 401 | /** |
| 402 | * @work: worklist element for death notifications |
| 403 | * (protected by inner_lock of the proc that |
| 404 | * this ref belongs to) |
| 405 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 406 | struct binder_work work; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 407 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 408 | }; |
| 409 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 410 | /** |
| 411 | * struct binder_ref_data - binder_ref counts and id |
| 412 | * @debug_id: unique ID for the ref |
| 413 | * @desc: unique userspace handle for ref |
| 414 | * @strong: strong ref count (debugging only if not locked) |
| 415 | * @weak: weak ref count (debugging only if not locked) |
| 416 | * |
| 417 | * Structure to hold ref count and ref id information. Since |
| 418 | * the actual ref can only be accessed with a lock, this structure |
| 419 | * is used to return information about the ref to callers of |
| 420 | * ref inc/dec functions. |
| 421 | */ |
| 422 | struct binder_ref_data { |
| 423 | int debug_id; |
| 424 | uint32_t desc; |
| 425 | int strong; |
| 426 | int weak; |
| 427 | }; |
| 428 | |
| 429 | /** |
| 430 | * struct binder_ref - struct to track references on nodes |
| 431 | * @data: binder_ref_data containing id, handle, and current refcounts |
| 432 | * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree |
| 433 | * @rb_node_node: node for lookup by @node in proc's rb_tree |
| 434 | * @node_entry: list entry for node->refs list in target node |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 435 | * (protected by @node->lock) |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 436 | * @proc: binder_proc containing ref |
| 437 | * @node: binder_node of target node. When cleaning up a |
| 438 | * ref for deletion in binder_cleanup_ref, a non-NULL |
| 439 | * @node indicates the node must be freed |
| 440 | * @death: pointer to death notification (ref_death) if requested |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 441 | * (protected by @node->lock) |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 442 | * |
| 443 | * Structure to track references from procA to target node (on procB). This |
| 444 | * structure is unsafe to access without holding @proc->outer_lock. |
| 445 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 446 | struct binder_ref { |
| 447 | /* Lookups needed: */ |
| 448 | /* node + proc => ref (transaction) */ |
| 449 | /* desc + proc => ref (transaction, inc/dec ref) */ |
| 450 | /* node => refs + procs (proc exit) */ |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 451 | struct binder_ref_data data; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 452 | struct rb_node rb_node_desc; |
| 453 | struct rb_node rb_node_node; |
| 454 | struct hlist_node node_entry; |
| 455 | struct binder_proc *proc; |
| 456 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 457 | struct binder_ref_death *death; |
| 458 | }; |
| 459 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 460 | enum binder_deferred_state { |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 461 | BINDER_DEFERRED_FLUSH = 0x01, |
| 462 | BINDER_DEFERRED_RELEASE = 0x02, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 463 | }; |
| 464 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 465 | /** |
| 466 | * struct binder_proc - binder process bookkeeping |
| 467 | * @proc_node: element for binder_procs list |
| 468 | * @threads: rbtree of binder_threads in this proc |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 469 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 470 | * @nodes: rbtree of binder nodes associated with |
| 471 | * this proc ordered by node->ptr |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 472 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 473 | * @refs_by_desc: rbtree of refs ordered by ref->desc |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 474 | * (protected by @outer_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 475 | * @refs_by_node: rbtree of refs ordered by ref->node |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 476 | * (protected by @outer_lock) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 477 | * @waiting_threads: threads currently waiting for proc work |
| 478 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 479 | * @pid PID of group_leader of process |
| 480 | * (invariant after initialized) |
| 481 | * @tsk task_struct for group_leader of process |
| 482 | * (invariant after initialized) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 483 | * @deferred_work_node: element for binder_deferred_list |
| 484 | * (protected by binder_deferred_lock) |
| 485 | * @deferred_work: bitmap of deferred work to perform |
| 486 | * (protected by binder_deferred_lock) |
| 487 | * @is_dead: process is dead and awaiting free |
| 488 | * when outstanding transactions are cleaned up |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 489 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 490 | * @todo: list of work for this process |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 491 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 492 | * @stats: per-process binder statistics |
| 493 | * (atomics, no lock needed) |
| 494 | * @delivered_death: list of delivered death notification |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 495 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 496 | * @max_threads: cap on number of binder threads |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 497 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 498 | * @requested_threads: number of binder threads requested but not |
| 499 | * yet started. In current implementation, can |
| 500 | * only be 0 or 1. |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 501 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 502 | * @requested_threads_started: number binder threads started |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 503 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 504 | * @tmp_ref: temporary reference to indicate proc is in use |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 505 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 506 | * @default_priority: default scheduler priority |
| 507 | * (invariant after initialized) |
| 508 | * @debugfs_entry: debugfs node |
| 509 | * @alloc: binder allocator bookkeeping |
| 510 | * @context: binder_context for this proc |
| 511 | * (invariant after initialized) |
| 512 | * @inner_lock: can nest under outer_lock and/or node lock |
| 513 | * @outer_lock: no nesting under innor or node lock |
| 514 | * Lock order: 1) outer, 2) node, 3) inner |
| 515 | * |
| 516 | * Bookkeeping structure for binder processes |
| 517 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 518 | struct binder_proc { |
| 519 | struct hlist_node proc_node; |
| 520 | struct rb_root threads; |
| 521 | struct rb_root nodes; |
| 522 | struct rb_root refs_by_desc; |
| 523 | struct rb_root refs_by_node; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 524 | struct list_head waiting_threads; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 525 | int pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 526 | struct task_struct *tsk; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 527 | struct hlist_node deferred_work_node; |
| 528 | int deferred_work; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 529 | bool is_dead; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 530 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 531 | struct list_head todo; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 532 | struct binder_stats stats; |
| 533 | struct list_head delivered_death; |
| 534 | int max_threads; |
| 535 | int requested_threads; |
| 536 | int requested_threads_started; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 537 | int tmp_ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 538 | long default_priority; |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 539 | struct dentry *debugfs_entry; |
Todd Kjos | fdfb4a9 | 2017-06-29 12:01:38 -0700 | [diff] [blame] | 540 | struct binder_alloc alloc; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 541 | struct binder_context *context; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 542 | spinlock_t inner_lock; |
| 543 | spinlock_t outer_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 544 | }; |
| 545 | |
| 546 | enum { |
| 547 | BINDER_LOOPER_STATE_REGISTERED = 0x01, |
| 548 | BINDER_LOOPER_STATE_ENTERED = 0x02, |
| 549 | BINDER_LOOPER_STATE_EXITED = 0x04, |
| 550 | BINDER_LOOPER_STATE_INVALID = 0x08, |
| 551 | BINDER_LOOPER_STATE_WAITING = 0x10, |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 552 | BINDER_LOOPER_STATE_POLL = 0x20, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 553 | }; |
| 554 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 555 | /** |
| 556 | * struct binder_thread - binder thread bookkeeping |
| 557 | * @proc: binder process for this thread |
| 558 | * (invariant after initialization) |
| 559 | * @rb_node: element for proc->threads rbtree |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 560 | * (protected by @proc->inner_lock) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 561 | * @waiting_thread_node: element for @proc->waiting_threads list |
| 562 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 563 | * @pid: PID for this thread |
| 564 | * (invariant after initialization) |
| 565 | * @looper: bitmap of looping state |
| 566 | * (only accessed by this thread) |
| 567 | * @looper_needs_return: looping thread needs to exit driver |
| 568 | * (no lock needed) |
| 569 | * @transaction_stack: stack of in-progress transactions for this thread |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 570 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 571 | * @todo: list of work to do for this thread |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 572 | * (protected by @proc->inner_lock) |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 573 | * @process_todo: whether work in @todo should be processed |
| 574 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 575 | * @return_error: transaction errors reported by this thread |
| 576 | * (only accessed by this thread) |
| 577 | * @reply_error: transaction errors reported by target thread |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 578 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 579 | * @wait: wait queue for thread work |
| 580 | * @stats: per-thread statistics |
| 581 | * (atomics, no lock needed) |
| 582 | * @tmp_ref: temporary reference to indicate thread is in use |
| 583 | * (atomic since @proc->inner_lock cannot |
| 584 | * always be acquired) |
| 585 | * @is_dead: thread is dead and awaiting free |
| 586 | * when outstanding transactions are cleaned up |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 587 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 588 | * |
| 589 | * Bookkeeping structure for binder threads. |
| 590 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 591 | struct binder_thread { |
| 592 | struct binder_proc *proc; |
| 593 | struct rb_node rb_node; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 594 | struct list_head waiting_thread_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 595 | int pid; |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 596 | int looper; /* only modified by this thread */ |
| 597 | bool looper_need_return; /* can be written by other thread */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 598 | struct binder_transaction *transaction_stack; |
| 599 | struct list_head todo; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 600 | bool process_todo; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 601 | struct binder_error return_error; |
| 602 | struct binder_error reply_error; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 603 | wait_queue_head_t wait; |
| 604 | struct binder_stats stats; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 605 | atomic_t tmp_ref; |
| 606 | bool is_dead; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 607 | }; |
| 608 | |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 609 | /** |
| 610 | * struct binder_txn_fd_fixup - transaction fd fixup list element |
| 611 | * @fixup_entry: list entry |
| 612 | * @file: struct file to be associated with new fd |
| 613 | * @offset: offset in buffer data to this fixup |
| 614 | * |
| 615 | * List element for fd fixups in a transaction. Since file |
| 616 | * descriptors need to be allocated in the context of the |
| 617 | * target process, we pass each fd to be processed in this |
| 618 | * struct. |
| 619 | */ |
| 620 | struct binder_txn_fd_fixup { |
| 621 | struct list_head fixup_entry; |
| 622 | struct file *file; |
| 623 | size_t offset; |
| 624 | }; |
| 625 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 626 | struct binder_transaction { |
| 627 | int debug_id; |
| 628 | struct binder_work work; |
| 629 | struct binder_thread *from; |
| 630 | struct binder_transaction *from_parent; |
| 631 | struct binder_proc *to_proc; |
| 632 | struct binder_thread *to_thread; |
| 633 | struct binder_transaction *to_parent; |
| 634 | unsigned need_reply:1; |
| 635 | /* unsigned is_dead:1; */ /* not used at the moment */ |
| 636 | |
| 637 | struct binder_buffer *buffer; |
| 638 | unsigned int code; |
| 639 | unsigned int flags; |
| 640 | long priority; |
| 641 | long saved_priority; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 642 | kuid_t sender_euid; |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 643 | struct list_head fd_fixups; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 644 | /** |
| 645 | * @lock: protects @from, @to_proc, and @to_thread |
| 646 | * |
| 647 | * @from, @to_proc, and @to_thread can be set to NULL |
| 648 | * during thread teardown |
| 649 | */ |
| 650 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 651 | }; |
| 652 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 653 | /** |
| 654 | * binder_proc_lock() - Acquire outer lock for given binder_proc |
| 655 | * @proc: struct binder_proc to acquire |
| 656 | * |
| 657 | * Acquires proc->outer_lock. Used to protect binder_ref |
| 658 | * structures associated with the given proc. |
| 659 | */ |
| 660 | #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__) |
| 661 | static void |
| 662 | _binder_proc_lock(struct binder_proc *proc, int line) |
| 663 | { |
| 664 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 665 | "%s: line=%d\n", __func__, line); |
| 666 | spin_lock(&proc->outer_lock); |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * binder_proc_unlock() - Release spinlock for given binder_proc |
| 671 | * @proc: struct binder_proc to acquire |
| 672 | * |
| 673 | * Release lock acquired via binder_proc_lock() |
| 674 | */ |
| 675 | #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__) |
| 676 | static void |
| 677 | _binder_proc_unlock(struct binder_proc *proc, int line) |
| 678 | { |
| 679 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 680 | "%s: line=%d\n", __func__, line); |
| 681 | spin_unlock(&proc->outer_lock); |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * binder_inner_proc_lock() - Acquire inner lock for given binder_proc |
| 686 | * @proc: struct binder_proc to acquire |
| 687 | * |
| 688 | * Acquires proc->inner_lock. Used to protect todo lists |
| 689 | */ |
| 690 | #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__) |
| 691 | static void |
| 692 | _binder_inner_proc_lock(struct binder_proc *proc, int line) |
| 693 | { |
| 694 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 695 | "%s: line=%d\n", __func__, line); |
| 696 | spin_lock(&proc->inner_lock); |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * binder_inner_proc_unlock() - Release inner lock for given binder_proc |
| 701 | * @proc: struct binder_proc to acquire |
| 702 | * |
| 703 | * Release lock acquired via binder_inner_proc_lock() |
| 704 | */ |
| 705 | #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__) |
| 706 | static void |
| 707 | _binder_inner_proc_unlock(struct binder_proc *proc, int line) |
| 708 | { |
| 709 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 710 | "%s: line=%d\n", __func__, line); |
| 711 | spin_unlock(&proc->inner_lock); |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * binder_node_lock() - Acquire spinlock for given binder_node |
| 716 | * @node: struct binder_node to acquire |
| 717 | * |
| 718 | * Acquires node->lock. Used to protect binder_node fields |
| 719 | */ |
| 720 | #define binder_node_lock(node) _binder_node_lock(node, __LINE__) |
| 721 | static void |
| 722 | _binder_node_lock(struct binder_node *node, int line) |
| 723 | { |
| 724 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 725 | "%s: line=%d\n", __func__, line); |
| 726 | spin_lock(&node->lock); |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * binder_node_unlock() - Release spinlock for given binder_proc |
| 731 | * @node: struct binder_node to acquire |
| 732 | * |
| 733 | * Release lock acquired via binder_node_lock() |
| 734 | */ |
| 735 | #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__) |
| 736 | static void |
| 737 | _binder_node_unlock(struct binder_node *node, int line) |
| 738 | { |
| 739 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 740 | "%s: line=%d\n", __func__, line); |
| 741 | spin_unlock(&node->lock); |
| 742 | } |
| 743 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 744 | /** |
| 745 | * binder_node_inner_lock() - Acquire node and inner locks |
| 746 | * @node: struct binder_node to acquire |
| 747 | * |
| 748 | * Acquires node->lock. If node->proc also acquires |
| 749 | * proc->inner_lock. Used to protect binder_node fields |
| 750 | */ |
| 751 | #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__) |
| 752 | static void |
| 753 | _binder_node_inner_lock(struct binder_node *node, int line) |
| 754 | { |
| 755 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 756 | "%s: line=%d\n", __func__, line); |
| 757 | spin_lock(&node->lock); |
| 758 | if (node->proc) |
| 759 | binder_inner_proc_lock(node->proc); |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * binder_node_unlock() - Release node and inner locks |
| 764 | * @node: struct binder_node to acquire |
| 765 | * |
| 766 | * Release lock acquired via binder_node_lock() |
| 767 | */ |
| 768 | #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__) |
| 769 | static void |
| 770 | _binder_node_inner_unlock(struct binder_node *node, int line) |
| 771 | { |
| 772 | struct binder_proc *proc = node->proc; |
| 773 | |
| 774 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 775 | "%s: line=%d\n", __func__, line); |
| 776 | if (proc) |
| 777 | binder_inner_proc_unlock(proc); |
| 778 | spin_unlock(&node->lock); |
| 779 | } |
| 780 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 781 | static bool binder_worklist_empty_ilocked(struct list_head *list) |
| 782 | { |
| 783 | return list_empty(list); |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * binder_worklist_empty() - Check if no items on the work list |
| 788 | * @proc: binder_proc associated with list |
| 789 | * @list: list to check |
| 790 | * |
| 791 | * Return: true if there are no items on list, else false |
| 792 | */ |
| 793 | static bool binder_worklist_empty(struct binder_proc *proc, |
| 794 | struct list_head *list) |
| 795 | { |
| 796 | bool ret; |
| 797 | |
| 798 | binder_inner_proc_lock(proc); |
| 799 | ret = binder_worklist_empty_ilocked(list); |
| 800 | binder_inner_proc_unlock(proc); |
| 801 | return ret; |
| 802 | } |
| 803 | |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 804 | /** |
| 805 | * binder_enqueue_work_ilocked() - Add an item to the work list |
| 806 | * @work: struct binder_work to add to list |
| 807 | * @target_list: list to add work to |
| 808 | * |
| 809 | * Adds the work to the specified list. Asserts that work |
| 810 | * is not already on a list. |
| 811 | * |
| 812 | * Requires the proc->inner_lock to be held. |
| 813 | */ |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 814 | static void |
| 815 | binder_enqueue_work_ilocked(struct binder_work *work, |
| 816 | struct list_head *target_list) |
| 817 | { |
| 818 | BUG_ON(target_list == NULL); |
| 819 | BUG_ON(work->entry.next && !list_empty(&work->entry)); |
| 820 | list_add_tail(&work->entry, target_list); |
| 821 | } |
| 822 | |
| 823 | /** |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 824 | * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work |
| 825 | * @thread: thread to queue work to |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 826 | * @work: struct binder_work to add to list |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 827 | * |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 828 | * Adds the work to the todo list of the thread. Doesn't set the process_todo |
| 829 | * flag, which means that (if it wasn't already set) the thread will go to |
| 830 | * sleep without handling this work when it calls read. |
| 831 | * |
| 832 | * Requires the proc->inner_lock to be held. |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 833 | */ |
| 834 | static void |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 835 | binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread, |
| 836 | struct binder_work *work) |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 837 | { |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame] | 838 | WARN_ON(!list_empty(&thread->waiting_thread_node)); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 839 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list |
| 844 | * @thread: thread to queue work to |
| 845 | * @work: struct binder_work to add to list |
| 846 | * |
| 847 | * Adds the work to the todo list of the thread, and enables processing |
| 848 | * of the todo queue. |
| 849 | * |
| 850 | * Requires the proc->inner_lock to be held. |
| 851 | */ |
| 852 | static void |
| 853 | binder_enqueue_thread_work_ilocked(struct binder_thread *thread, |
| 854 | struct binder_work *work) |
| 855 | { |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame] | 856 | WARN_ON(!list_empty(&thread->waiting_thread_node)); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 857 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 858 | thread->process_todo = true; |
| 859 | } |
| 860 | |
| 861 | /** |
| 862 | * binder_enqueue_thread_work() - Add an item to the thread work list |
| 863 | * @thread: thread to queue work to |
| 864 | * @work: struct binder_work to add to list |
| 865 | * |
| 866 | * Adds the work to the todo list of the thread, and enables processing |
| 867 | * of the todo queue. |
| 868 | */ |
| 869 | static void |
| 870 | binder_enqueue_thread_work(struct binder_thread *thread, |
| 871 | struct binder_work *work) |
| 872 | { |
| 873 | binder_inner_proc_lock(thread->proc); |
| 874 | binder_enqueue_thread_work_ilocked(thread, work); |
| 875 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | static void |
| 879 | binder_dequeue_work_ilocked(struct binder_work *work) |
| 880 | { |
| 881 | list_del_init(&work->entry); |
| 882 | } |
| 883 | |
| 884 | /** |
| 885 | * binder_dequeue_work() - Removes an item from the work list |
| 886 | * @proc: binder_proc associated with list |
| 887 | * @work: struct binder_work to remove from list |
| 888 | * |
| 889 | * Removes the specified work item from whatever list it is on. |
| 890 | * Can safely be called if work is not on any list. |
| 891 | */ |
| 892 | static void |
| 893 | binder_dequeue_work(struct binder_proc *proc, struct binder_work *work) |
| 894 | { |
| 895 | binder_inner_proc_lock(proc); |
| 896 | binder_dequeue_work_ilocked(work); |
| 897 | binder_inner_proc_unlock(proc); |
| 898 | } |
| 899 | |
| 900 | static struct binder_work *binder_dequeue_work_head_ilocked( |
| 901 | struct list_head *list) |
| 902 | { |
| 903 | struct binder_work *w; |
| 904 | |
| 905 | w = list_first_entry_or_null(list, struct binder_work, entry); |
| 906 | if (w) |
| 907 | list_del_init(&w->entry); |
| 908 | return w; |
| 909 | } |
| 910 | |
| 911 | /** |
| 912 | * binder_dequeue_work_head() - Dequeues the item at head of list |
| 913 | * @proc: binder_proc associated with list |
| 914 | * @list: list to dequeue head |
| 915 | * |
| 916 | * Removes the head of the list if there are items on the list |
| 917 | * |
| 918 | * Return: pointer dequeued binder_work, NULL if list was empty |
| 919 | */ |
| 920 | static struct binder_work *binder_dequeue_work_head( |
| 921 | struct binder_proc *proc, |
| 922 | struct list_head *list) |
| 923 | { |
| 924 | struct binder_work *w; |
| 925 | |
| 926 | binder_inner_proc_lock(proc); |
| 927 | w = binder_dequeue_work_head_ilocked(list); |
| 928 | binder_inner_proc_unlock(proc); |
| 929 | return w; |
| 930 | } |
| 931 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 932 | static void |
| 933 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 934 | static void binder_free_thread(struct binder_thread *thread); |
| 935 | static void binder_free_proc(struct binder_proc *proc); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 936 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 937 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 938 | static bool binder_has_work_ilocked(struct binder_thread *thread, |
| 939 | bool do_proc_work) |
| 940 | { |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 941 | return thread->process_todo || |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 942 | thread->looper_need_return || |
| 943 | (do_proc_work && |
| 944 | !binder_worklist_empty_ilocked(&thread->proc->todo)); |
| 945 | } |
| 946 | |
| 947 | static bool binder_has_work(struct binder_thread *thread, bool do_proc_work) |
| 948 | { |
| 949 | bool has_work; |
| 950 | |
| 951 | binder_inner_proc_lock(thread->proc); |
| 952 | has_work = binder_has_work_ilocked(thread, do_proc_work); |
| 953 | binder_inner_proc_unlock(thread->proc); |
| 954 | |
| 955 | return has_work; |
| 956 | } |
| 957 | |
| 958 | static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread) |
| 959 | { |
| 960 | return !thread->transaction_stack && |
| 961 | binder_worklist_empty_ilocked(&thread->todo) && |
| 962 | (thread->looper & (BINDER_LOOPER_STATE_ENTERED | |
| 963 | BINDER_LOOPER_STATE_REGISTERED)); |
| 964 | } |
| 965 | |
| 966 | static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc, |
| 967 | bool sync) |
| 968 | { |
| 969 | struct rb_node *n; |
| 970 | struct binder_thread *thread; |
| 971 | |
| 972 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 973 | thread = rb_entry(n, struct binder_thread, rb_node); |
| 974 | if (thread->looper & BINDER_LOOPER_STATE_POLL && |
| 975 | binder_available_for_proc_work_ilocked(thread)) { |
| 976 | if (sync) |
| 977 | wake_up_interruptible_sync(&thread->wait); |
| 978 | else |
| 979 | wake_up_interruptible(&thread->wait); |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 984 | /** |
| 985 | * binder_select_thread_ilocked() - selects a thread for doing proc work. |
| 986 | * @proc: process to select a thread from |
| 987 | * |
| 988 | * Note that calling this function moves the thread off the waiting_threads |
| 989 | * list, so it can only be woken up by the caller of this function, or a |
| 990 | * signal. Therefore, callers *should* always wake up the thread this function |
| 991 | * returns. |
| 992 | * |
| 993 | * Return: If there's a thread currently waiting for process work, |
| 994 | * returns that thread. Otherwise returns NULL. |
| 995 | */ |
| 996 | static struct binder_thread * |
| 997 | binder_select_thread_ilocked(struct binder_proc *proc) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 998 | { |
| 999 | struct binder_thread *thread; |
| 1000 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1001 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1002 | thread = list_first_entry_or_null(&proc->waiting_threads, |
| 1003 | struct binder_thread, |
| 1004 | waiting_thread_node); |
| 1005 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1006 | if (thread) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1007 | list_del_init(&thread->waiting_thread_node); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1008 | |
| 1009 | return thread; |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work. |
| 1014 | * @proc: process to wake up a thread in |
| 1015 | * @thread: specific thread to wake-up (may be NULL) |
| 1016 | * @sync: whether to do a synchronous wake-up |
| 1017 | * |
| 1018 | * This function wakes up a thread in the @proc process. |
| 1019 | * The caller may provide a specific thread to wake-up in |
| 1020 | * the @thread parameter. If @thread is NULL, this function |
| 1021 | * will wake up threads that have called poll(). |
| 1022 | * |
| 1023 | * Note that for this function to work as expected, callers |
| 1024 | * should first call binder_select_thread() to find a thread |
| 1025 | * to handle the work (if they don't have a thread already), |
| 1026 | * and pass the result into the @thread parameter. |
| 1027 | */ |
| 1028 | static void binder_wakeup_thread_ilocked(struct binder_proc *proc, |
| 1029 | struct binder_thread *thread, |
| 1030 | bool sync) |
| 1031 | { |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1032 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1033 | |
| 1034 | if (thread) { |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1035 | if (sync) |
| 1036 | wake_up_interruptible_sync(&thread->wait); |
| 1037 | else |
| 1038 | wake_up_interruptible(&thread->wait); |
| 1039 | return; |
| 1040 | } |
| 1041 | |
| 1042 | /* Didn't find a thread waiting for proc work; this can happen |
| 1043 | * in two scenarios: |
| 1044 | * 1. All threads are busy handling transactions |
| 1045 | * In that case, one of those threads should call back into |
| 1046 | * the kernel driver soon and pick up this work. |
| 1047 | * 2. Threads are using the (e)poll interface, in which case |
| 1048 | * they may be blocked on the waitqueue without having been |
| 1049 | * added to waiting_threads. For this case, we just iterate |
| 1050 | * over all threads not handling transaction work, and |
| 1051 | * wake them all up. We wake all because we don't know whether |
| 1052 | * a thread that called into (e)poll is handling non-binder |
| 1053 | * work currently. |
| 1054 | */ |
| 1055 | binder_wakeup_poll_threads_ilocked(proc, sync); |
| 1056 | } |
| 1057 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1058 | static void binder_wakeup_proc_ilocked(struct binder_proc *proc) |
| 1059 | { |
| 1060 | struct binder_thread *thread = binder_select_thread_ilocked(proc); |
| 1061 | |
| 1062 | binder_wakeup_thread_ilocked(proc, thread, /* sync = */false); |
| 1063 | } |
| 1064 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1065 | static void binder_set_nice(long nice) |
| 1066 | { |
| 1067 | long min_nice; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1068 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1069 | if (can_nice(current, nice)) { |
| 1070 | set_user_nice(current, nice); |
| 1071 | return; |
| 1072 | } |
Krzysztof Opasiak | c3643b6 | 2017-07-05 19:24:44 +0200 | [diff] [blame] | 1073 | min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1074 | binder_debug(BINDER_DEBUG_PRIORITY_CAP, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1075 | "%d: nice value %ld not allowed use %ld instead\n", |
| 1076 | current->pid, nice, min_nice); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1077 | set_user_nice(current, min_nice); |
Dongsheng Yang | 8698a74 | 2014-03-11 18:09:12 +0800 | [diff] [blame] | 1078 | if (min_nice <= MAX_NICE) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1079 | return; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1080 | binder_user_error("%d RLIMIT_NICE not set\n", current->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1081 | } |
| 1082 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1083 | static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc, |
| 1084 | binder_uintptr_t ptr) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1085 | { |
| 1086 | struct rb_node *n = proc->nodes.rb_node; |
| 1087 | struct binder_node *node; |
| 1088 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1089 | assert_spin_locked(&proc->inner_lock); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1090 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1091 | while (n) { |
| 1092 | node = rb_entry(n, struct binder_node, rb_node); |
| 1093 | |
| 1094 | if (ptr < node->ptr) |
| 1095 | n = n->rb_left; |
| 1096 | else if (ptr > node->ptr) |
| 1097 | n = n->rb_right; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1098 | else { |
| 1099 | /* |
| 1100 | * take an implicit weak reference |
| 1101 | * to ensure node stays alive until |
| 1102 | * call to binder_put_node() |
| 1103 | */ |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1104 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1105 | return node; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1106 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1107 | } |
| 1108 | return NULL; |
| 1109 | } |
| 1110 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1111 | static struct binder_node *binder_get_node(struct binder_proc *proc, |
| 1112 | binder_uintptr_t ptr) |
| 1113 | { |
| 1114 | struct binder_node *node; |
| 1115 | |
| 1116 | binder_inner_proc_lock(proc); |
| 1117 | node = binder_get_node_ilocked(proc, ptr); |
| 1118 | binder_inner_proc_unlock(proc); |
| 1119 | return node; |
| 1120 | } |
| 1121 | |
| 1122 | static struct binder_node *binder_init_node_ilocked( |
| 1123 | struct binder_proc *proc, |
| 1124 | struct binder_node *new_node, |
| 1125 | struct flat_binder_object *fp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1126 | { |
| 1127 | struct rb_node **p = &proc->nodes.rb_node; |
| 1128 | struct rb_node *parent = NULL; |
| 1129 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1130 | binder_uintptr_t ptr = fp ? fp->binder : 0; |
| 1131 | binder_uintptr_t cookie = fp ? fp->cookie : 0; |
| 1132 | __u32 flags = fp ? fp->flags : 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1133 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1134 | assert_spin_locked(&proc->inner_lock); |
| 1135 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1136 | while (*p) { |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1137 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1138 | parent = *p; |
| 1139 | node = rb_entry(parent, struct binder_node, rb_node); |
| 1140 | |
| 1141 | if (ptr < node->ptr) |
| 1142 | p = &(*p)->rb_left; |
| 1143 | else if (ptr > node->ptr) |
| 1144 | p = &(*p)->rb_right; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1145 | else { |
| 1146 | /* |
| 1147 | * A matching node is already in |
| 1148 | * the rb tree. Abandon the init |
| 1149 | * and return it. |
| 1150 | */ |
| 1151 | binder_inc_node_tmpref_ilocked(node); |
| 1152 | return node; |
| 1153 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1154 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1155 | node = new_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1156 | binder_stats_created(BINDER_STAT_NODE); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1157 | node->tmp_refs++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1158 | rb_link_node(&node->rb_node, parent, p); |
| 1159 | rb_insert_color(&node->rb_node, &proc->nodes); |
Todd Kjos | 656a800 | 2017-06-29 12:01:45 -0700 | [diff] [blame] | 1160 | node->debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1161 | node->proc = proc; |
| 1162 | node->ptr = ptr; |
| 1163 | node->cookie = cookie; |
| 1164 | node->work.type = BINDER_WORK_NODE; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1165 | node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK; |
| 1166 | node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 1167 | spin_lock_init(&node->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1168 | INIT_LIST_HEAD(&node->work.entry); |
| 1169 | INIT_LIST_HEAD(&node->async_todo); |
| 1170 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1171 | "%d:%d node %d u%016llx c%016llx created\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1172 | proc->pid, current->pid, node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1173 | (u64)node->ptr, (u64)node->cookie); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1174 | |
| 1175 | return node; |
| 1176 | } |
| 1177 | |
| 1178 | static struct binder_node *binder_new_node(struct binder_proc *proc, |
| 1179 | struct flat_binder_object *fp) |
| 1180 | { |
| 1181 | struct binder_node *node; |
| 1182 | struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 1183 | |
| 1184 | if (!new_node) |
| 1185 | return NULL; |
| 1186 | binder_inner_proc_lock(proc); |
| 1187 | node = binder_init_node_ilocked(proc, new_node, fp); |
| 1188 | binder_inner_proc_unlock(proc); |
| 1189 | if (node != new_node) |
| 1190 | /* |
| 1191 | * The node was already added by another thread |
| 1192 | */ |
| 1193 | kfree(new_node); |
| 1194 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1195 | return node; |
| 1196 | } |
| 1197 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1198 | static void binder_free_node(struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1199 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1200 | kfree(node); |
| 1201 | binder_stats_deleted(BINDER_STAT_NODE); |
| 1202 | } |
| 1203 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1204 | static int binder_inc_node_nilocked(struct binder_node *node, int strong, |
| 1205 | int internal, |
| 1206 | struct list_head *target_list) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1207 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1208 | struct binder_proc *proc = node->proc; |
| 1209 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1210 | assert_spin_locked(&node->lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1211 | if (proc) |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1212 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1213 | if (strong) { |
| 1214 | if (internal) { |
| 1215 | if (target_list == NULL && |
| 1216 | node->internal_strong_refs == 0 && |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 1217 | !(node->proc && |
| 1218 | node == node->proc->context->binder_context_mgr_node && |
| 1219 | node->has_strong_ref)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1220 | pr_err("invalid inc strong node for %d\n", |
| 1221 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1222 | return -EINVAL; |
| 1223 | } |
| 1224 | node->internal_strong_refs++; |
| 1225 | } else |
| 1226 | node->local_strong_refs++; |
| 1227 | if (!node->has_strong_ref && target_list) { |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame] | 1228 | struct binder_thread *thread = container_of(target_list, |
| 1229 | struct binder_thread, todo); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1230 | binder_dequeue_work_ilocked(&node->work); |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame] | 1231 | BUG_ON(&thread->todo != target_list); |
| 1232 | binder_enqueue_deferred_thread_work_ilocked(thread, |
| 1233 | &node->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1234 | } |
| 1235 | } else { |
| 1236 | if (!internal) |
| 1237 | node->local_weak_refs++; |
| 1238 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { |
| 1239 | if (target_list == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1240 | pr_err("invalid inc weak node for %d\n", |
| 1241 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1242 | return -EINVAL; |
| 1243 | } |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 1244 | /* |
| 1245 | * See comment above |
| 1246 | */ |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1247 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1253 | static int binder_inc_node(struct binder_node *node, int strong, int internal, |
| 1254 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1255 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1256 | int ret; |
| 1257 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1258 | binder_node_inner_lock(node); |
| 1259 | ret = binder_inc_node_nilocked(node, strong, internal, target_list); |
| 1260 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1261 | |
| 1262 | return ret; |
| 1263 | } |
| 1264 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1265 | static bool binder_dec_node_nilocked(struct binder_node *node, |
| 1266 | int strong, int internal) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1267 | { |
| 1268 | struct binder_proc *proc = node->proc; |
| 1269 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1270 | assert_spin_locked(&node->lock); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1271 | if (proc) |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1272 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1273 | if (strong) { |
| 1274 | if (internal) |
| 1275 | node->internal_strong_refs--; |
| 1276 | else |
| 1277 | node->local_strong_refs--; |
| 1278 | if (node->local_strong_refs || node->internal_strong_refs) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1279 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1280 | } else { |
| 1281 | if (!internal) |
| 1282 | node->local_weak_refs--; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1283 | if (node->local_weak_refs || node->tmp_refs || |
| 1284 | !hlist_empty(&node->refs)) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1285 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1286 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1287 | |
| 1288 | if (proc && (node->has_strong_ref || node->has_weak_ref)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1289 | if (list_empty(&node->work.entry)) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1290 | binder_enqueue_work_ilocked(&node->work, &proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1291 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1292 | } |
| 1293 | } else { |
| 1294 | if (hlist_empty(&node->refs) && !node->local_strong_refs && |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1295 | !node->local_weak_refs && !node->tmp_refs) { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1296 | if (proc) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1297 | binder_dequeue_work_ilocked(&node->work); |
| 1298 | rb_erase(&node->rb_node, &proc->nodes); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1299 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1300 | "refless node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1301 | node->debug_id); |
| 1302 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1303 | BUG_ON(!list_empty(&node->work.entry)); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 1304 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1305 | /* |
| 1306 | * tmp_refs could have changed so |
| 1307 | * check it again |
| 1308 | */ |
| 1309 | if (node->tmp_refs) { |
| 1310 | spin_unlock(&binder_dead_nodes_lock); |
| 1311 | return false; |
| 1312 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1313 | hlist_del(&node->dead_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 1314 | spin_unlock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1315 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1316 | "dead node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1317 | node->debug_id); |
| 1318 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1319 | return true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1320 | } |
| 1321 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1322 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1323 | } |
| 1324 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1325 | static void binder_dec_node(struct binder_node *node, int strong, int internal) |
| 1326 | { |
| 1327 | bool free_node; |
| 1328 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1329 | binder_node_inner_lock(node); |
| 1330 | free_node = binder_dec_node_nilocked(node, strong, internal); |
| 1331 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1332 | if (free_node) |
| 1333 | binder_free_node(node); |
| 1334 | } |
| 1335 | |
| 1336 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1337 | { |
| 1338 | /* |
| 1339 | * No call to binder_inc_node() is needed since we |
| 1340 | * don't need to inform userspace of any changes to |
| 1341 | * tmp_refs |
| 1342 | */ |
| 1343 | node->tmp_refs++; |
| 1344 | } |
| 1345 | |
| 1346 | /** |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1347 | * binder_inc_node_tmpref() - take a temporary reference on node |
| 1348 | * @node: node to reference |
| 1349 | * |
| 1350 | * Take reference on node to prevent the node from being freed |
| 1351 | * while referenced only by a local variable. The inner lock is |
| 1352 | * needed to serialize with the node work on the queue (which |
| 1353 | * isn't needed after the node is dead). If the node is dead |
| 1354 | * (node->proc is NULL), use binder_dead_nodes_lock to protect |
| 1355 | * node->tmp_refs against dead-node-only cases where the node |
| 1356 | * lock cannot be acquired (eg traversing the dead node list to |
| 1357 | * print nodes) |
| 1358 | */ |
| 1359 | static void binder_inc_node_tmpref(struct binder_node *node) |
| 1360 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1361 | binder_node_lock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1362 | if (node->proc) |
| 1363 | binder_inner_proc_lock(node->proc); |
| 1364 | else |
| 1365 | spin_lock(&binder_dead_nodes_lock); |
| 1366 | binder_inc_node_tmpref_ilocked(node); |
| 1367 | if (node->proc) |
| 1368 | binder_inner_proc_unlock(node->proc); |
| 1369 | else |
| 1370 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1371 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /** |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1375 | * binder_dec_node_tmpref() - remove a temporary reference on node |
| 1376 | * @node: node to reference |
| 1377 | * |
| 1378 | * Release temporary reference on node taken via binder_inc_node_tmpref() |
| 1379 | */ |
| 1380 | static void binder_dec_node_tmpref(struct binder_node *node) |
| 1381 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1382 | bool free_node; |
| 1383 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1384 | binder_node_inner_lock(node); |
| 1385 | if (!node->proc) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1386 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1387 | node->tmp_refs--; |
| 1388 | BUG_ON(node->tmp_refs < 0); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1389 | if (!node->proc) |
| 1390 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1391 | /* |
| 1392 | * Call binder_dec_node() to check if all refcounts are 0 |
| 1393 | * and cleanup is needed. Calling with strong=0 and internal=1 |
| 1394 | * causes no actual reference to be released in binder_dec_node(). |
| 1395 | * If that changes, a change is needed here too. |
| 1396 | */ |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1397 | free_node = binder_dec_node_nilocked(node, 0, 1); |
| 1398 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1399 | if (free_node) |
| 1400 | binder_free_node(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | static void binder_put_node(struct binder_node *node) |
| 1404 | { |
| 1405 | binder_dec_node_tmpref(node); |
| 1406 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1407 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1408 | static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc, |
| 1409 | u32 desc, bool need_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1410 | { |
| 1411 | struct rb_node *n = proc->refs_by_desc.rb_node; |
| 1412 | struct binder_ref *ref; |
| 1413 | |
| 1414 | while (n) { |
| 1415 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1416 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1417 | if (desc < ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1418 | n = n->rb_left; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1419 | } else if (desc > ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1420 | n = n->rb_right; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1421 | } else if (need_strong_ref && !ref->data.strong) { |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1422 | binder_user_error("tried to use weak ref as strong ref\n"); |
| 1423 | return NULL; |
| 1424 | } else { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1425 | return ref; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1426 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1427 | } |
| 1428 | return NULL; |
| 1429 | } |
| 1430 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1431 | /** |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1432 | * binder_get_ref_for_node_olocked() - get the ref associated with given node |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1433 | * @proc: binder_proc that owns the ref |
| 1434 | * @node: binder_node of target |
| 1435 | * @new_ref: newly allocated binder_ref to be initialized or %NULL |
| 1436 | * |
| 1437 | * Look up the ref for the given node and return it if it exists |
| 1438 | * |
| 1439 | * If it doesn't exist and the caller provides a newly allocated |
| 1440 | * ref, initialize the fields of the newly allocated ref and insert |
| 1441 | * into the given proc rb_trees and node refs list. |
| 1442 | * |
| 1443 | * Return: the ref for node. It is possible that another thread |
| 1444 | * allocated/initialized the ref first in which case the |
| 1445 | * returned ref would be different than the passed-in |
| 1446 | * new_ref. new_ref must be kfree'd by the caller in |
| 1447 | * this case. |
| 1448 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1449 | static struct binder_ref *binder_get_ref_for_node_olocked( |
| 1450 | struct binder_proc *proc, |
| 1451 | struct binder_node *node, |
| 1452 | struct binder_ref *new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1453 | { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1454 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1455 | struct rb_node **p = &proc->refs_by_node.rb_node; |
| 1456 | struct rb_node *parent = NULL; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1457 | struct binder_ref *ref; |
| 1458 | struct rb_node *n; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1459 | |
| 1460 | while (*p) { |
| 1461 | parent = *p; |
| 1462 | ref = rb_entry(parent, struct binder_ref, rb_node_node); |
| 1463 | |
| 1464 | if (node < ref->node) |
| 1465 | p = &(*p)->rb_left; |
| 1466 | else if (node > ref->node) |
| 1467 | p = &(*p)->rb_right; |
| 1468 | else |
| 1469 | return ref; |
| 1470 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1471 | if (!new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1472 | return NULL; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1473 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1474 | binder_stats_created(BINDER_STAT_REF); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1475 | new_ref->data.debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1476 | new_ref->proc = proc; |
| 1477 | new_ref->node = node; |
| 1478 | rb_link_node(&new_ref->rb_node_node, parent, p); |
| 1479 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); |
| 1480 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1481 | 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] | 1482 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 1483 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1484 | if (ref->data.desc > new_ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1485 | break; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1486 | new_ref->data.desc = ref->data.desc + 1; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | p = &proc->refs_by_desc.rb_node; |
| 1490 | while (*p) { |
| 1491 | parent = *p; |
| 1492 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); |
| 1493 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1494 | if (new_ref->data.desc < ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1495 | p = &(*p)->rb_left; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1496 | else if (new_ref->data.desc > ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1497 | p = &(*p)->rb_right; |
| 1498 | else |
| 1499 | BUG(); |
| 1500 | } |
| 1501 | rb_link_node(&new_ref->rb_node_desc, parent, p); |
| 1502 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1503 | |
| 1504 | binder_node_lock(node); |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1505 | hlist_add_head(&new_ref->node_entry, &node->refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1506 | |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1507 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1508 | "%d new ref %d desc %d for node %d\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1509 | proc->pid, new_ref->data.debug_id, new_ref->data.desc, |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1510 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1511 | binder_node_unlock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1512 | return new_ref; |
| 1513 | } |
| 1514 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1515 | static void binder_cleanup_ref_olocked(struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1516 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1517 | bool delete_node = false; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1518 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1519 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1520 | "%d delete ref %d desc %d for node %d\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1521 | ref->proc->pid, ref->data.debug_id, ref->data.desc, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1522 | ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1523 | |
| 1524 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); |
| 1525 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1526 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1527 | binder_node_inner_lock(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1528 | if (ref->data.strong) |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1529 | binder_dec_node_nilocked(ref->node, 1, 1); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1530 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1531 | hlist_del(&ref->node_entry); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1532 | delete_node = binder_dec_node_nilocked(ref->node, 0, 1); |
| 1533 | binder_node_inner_unlock(ref->node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1534 | /* |
| 1535 | * Clear ref->node unless we want the caller to free the node |
| 1536 | */ |
| 1537 | if (!delete_node) { |
| 1538 | /* |
| 1539 | * The caller uses ref->node to determine |
| 1540 | * whether the node needs to be freed. Clear |
| 1541 | * it since the node is still alive. |
| 1542 | */ |
| 1543 | ref->node = NULL; |
| 1544 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1545 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1546 | if (ref->death) { |
| 1547 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1548 | "%d delete ref %d desc %d has death notification\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1549 | ref->proc->pid, ref->data.debug_id, |
| 1550 | ref->data.desc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1551 | binder_dequeue_work(ref->proc, &ref->death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1552 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 1553 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1554 | binder_stats_deleted(BINDER_STAT_REF); |
| 1555 | } |
| 1556 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1557 | /** |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1558 | * binder_inc_ref_olocked() - increment the ref for given handle |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1559 | * @ref: ref to be incremented |
| 1560 | * @strong: if true, strong increment, else weak |
| 1561 | * @target_list: list to queue node work on |
| 1562 | * |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1563 | * Increment the ref. @ref->proc->outer_lock must be held on entry |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1564 | * |
| 1565 | * Return: 0, if successful, else errno |
| 1566 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1567 | static int binder_inc_ref_olocked(struct binder_ref *ref, int strong, |
| 1568 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1569 | { |
| 1570 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1571 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1572 | if (strong) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1573 | if (ref->data.strong == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1574 | ret = binder_inc_node(ref->node, 1, 1, target_list); |
| 1575 | if (ret) |
| 1576 | return ret; |
| 1577 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1578 | ref->data.strong++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1579 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1580 | if (ref->data.weak == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1581 | ret = binder_inc_node(ref->node, 0, 1, target_list); |
| 1582 | if (ret) |
| 1583 | return ret; |
| 1584 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1585 | ref->data.weak++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1586 | } |
| 1587 | return 0; |
| 1588 | } |
| 1589 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1590 | /** |
| 1591 | * binder_dec_ref() - dec the ref for given handle |
| 1592 | * @ref: ref to be decremented |
| 1593 | * @strong: if true, strong decrement, else weak |
| 1594 | * |
| 1595 | * Decrement the ref. |
| 1596 | * |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1597 | * Return: true if ref is cleaned up and ready to be freed |
| 1598 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1599 | 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] | 1600 | { |
| 1601 | if (strong) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1602 | if (ref->data.strong == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1603 | binder_user_error("%d invalid dec strong, ref %d desc %d s %d w %d\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1604 | ref->proc->pid, ref->data.debug_id, |
| 1605 | ref->data.desc, ref->data.strong, |
| 1606 | ref->data.weak); |
| 1607 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1608 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1609 | ref->data.strong--; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1610 | if (ref->data.strong == 0) |
| 1611 | binder_dec_node(ref->node, strong, 1); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1612 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1613 | if (ref->data.weak == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1614 | binder_user_error("%d invalid dec weak, ref %d desc %d s %d w %d\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1615 | ref->proc->pid, ref->data.debug_id, |
| 1616 | ref->data.desc, ref->data.strong, |
| 1617 | ref->data.weak); |
| 1618 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1619 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1620 | ref->data.weak--; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1621 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1622 | if (ref->data.strong == 0 && ref->data.weak == 0) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1623 | binder_cleanup_ref_olocked(ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1624 | return true; |
| 1625 | } |
| 1626 | return false; |
| 1627 | } |
| 1628 | |
| 1629 | /** |
| 1630 | * binder_get_node_from_ref() - get the node from the given proc/desc |
| 1631 | * @proc: proc containing the ref |
| 1632 | * @desc: the handle associated with the ref |
| 1633 | * @need_strong_ref: if true, only return node if ref is strong |
| 1634 | * @rdata: the id/refcount data for the ref |
| 1635 | * |
| 1636 | * Given a proc and ref handle, return the associated binder_node |
| 1637 | * |
| 1638 | * Return: a binder_node or NULL if not found or not strong when strong required |
| 1639 | */ |
| 1640 | static struct binder_node *binder_get_node_from_ref( |
| 1641 | struct binder_proc *proc, |
| 1642 | u32 desc, bool need_strong_ref, |
| 1643 | struct binder_ref_data *rdata) |
| 1644 | { |
| 1645 | struct binder_node *node; |
| 1646 | struct binder_ref *ref; |
| 1647 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1648 | binder_proc_lock(proc); |
| 1649 | ref = binder_get_ref_olocked(proc, desc, need_strong_ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1650 | if (!ref) |
| 1651 | goto err_no_ref; |
| 1652 | node = ref->node; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1653 | /* |
| 1654 | * Take an implicit reference on the node to ensure |
| 1655 | * it stays alive until the call to binder_put_node() |
| 1656 | */ |
| 1657 | binder_inc_node_tmpref(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1658 | if (rdata) |
| 1659 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1660 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1661 | |
| 1662 | return node; |
| 1663 | |
| 1664 | err_no_ref: |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1665 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1666 | return NULL; |
| 1667 | } |
| 1668 | |
| 1669 | /** |
| 1670 | * binder_free_ref() - free the binder_ref |
| 1671 | * @ref: ref to free |
| 1672 | * |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1673 | * Free the binder_ref. Free the binder_node indicated by ref->node |
| 1674 | * (if non-NULL) and the binder_ref_death indicated by ref->death. |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1675 | */ |
| 1676 | static void binder_free_ref(struct binder_ref *ref) |
| 1677 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1678 | if (ref->node) |
| 1679 | binder_free_node(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1680 | kfree(ref->death); |
| 1681 | kfree(ref); |
| 1682 | } |
| 1683 | |
| 1684 | /** |
| 1685 | * binder_update_ref_for_handle() - inc/dec the ref for given handle |
| 1686 | * @proc: proc containing the ref |
| 1687 | * @desc: the handle associated with the ref |
| 1688 | * @increment: true=inc reference, false=dec reference |
| 1689 | * @strong: true=strong reference, false=weak reference |
| 1690 | * @rdata: the id/refcount data for the ref |
| 1691 | * |
| 1692 | * Given a proc and ref handle, increment or decrement the ref |
| 1693 | * according to "increment" arg. |
| 1694 | * |
| 1695 | * Return: 0 if successful, else errno |
| 1696 | */ |
| 1697 | static int binder_update_ref_for_handle(struct binder_proc *proc, |
| 1698 | uint32_t desc, bool increment, bool strong, |
| 1699 | struct binder_ref_data *rdata) |
| 1700 | { |
| 1701 | int ret = 0; |
| 1702 | struct binder_ref *ref; |
| 1703 | bool delete_ref = false; |
| 1704 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1705 | binder_proc_lock(proc); |
| 1706 | ref = binder_get_ref_olocked(proc, desc, strong); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1707 | if (!ref) { |
| 1708 | ret = -EINVAL; |
| 1709 | goto err_no_ref; |
| 1710 | } |
| 1711 | if (increment) |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1712 | ret = binder_inc_ref_olocked(ref, strong, NULL); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1713 | else |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1714 | delete_ref = binder_dec_ref_olocked(ref, strong); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1715 | |
| 1716 | if (rdata) |
| 1717 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1718 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1719 | |
| 1720 | if (delete_ref) |
| 1721 | binder_free_ref(ref); |
| 1722 | return ret; |
| 1723 | |
| 1724 | err_no_ref: |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1725 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1726 | return ret; |
| 1727 | } |
| 1728 | |
| 1729 | /** |
| 1730 | * binder_dec_ref_for_handle() - dec the ref for given handle |
| 1731 | * @proc: proc containing the ref |
| 1732 | * @desc: the handle associated with the ref |
| 1733 | * @strong: true=strong reference, false=weak reference |
| 1734 | * @rdata: the id/refcount data for the ref |
| 1735 | * |
| 1736 | * Just calls binder_update_ref_for_handle() to decrement the ref. |
| 1737 | * |
| 1738 | * Return: 0 if successful, else errno |
| 1739 | */ |
| 1740 | static int binder_dec_ref_for_handle(struct binder_proc *proc, |
| 1741 | uint32_t desc, bool strong, struct binder_ref_data *rdata) |
| 1742 | { |
| 1743 | return binder_update_ref_for_handle(proc, desc, false, strong, rdata); |
| 1744 | } |
| 1745 | |
| 1746 | |
| 1747 | /** |
| 1748 | * binder_inc_ref_for_node() - increment the ref for given proc/node |
| 1749 | * @proc: proc containing the ref |
| 1750 | * @node: target node |
| 1751 | * @strong: true=strong reference, false=weak reference |
| 1752 | * @target_list: worklist to use if node is incremented |
| 1753 | * @rdata: the id/refcount data for the ref |
| 1754 | * |
| 1755 | * Given a proc and node, increment the ref. Create the ref if it |
| 1756 | * doesn't already exist |
| 1757 | * |
| 1758 | * Return: 0 if successful, else errno |
| 1759 | */ |
| 1760 | static int binder_inc_ref_for_node(struct binder_proc *proc, |
| 1761 | struct binder_node *node, |
| 1762 | bool strong, |
| 1763 | struct list_head *target_list, |
| 1764 | struct binder_ref_data *rdata) |
| 1765 | { |
| 1766 | struct binder_ref *ref; |
| 1767 | struct binder_ref *new_ref = NULL; |
| 1768 | int ret = 0; |
| 1769 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1770 | binder_proc_lock(proc); |
| 1771 | ref = binder_get_ref_for_node_olocked(proc, node, NULL); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1772 | if (!ref) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1773 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1774 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
| 1775 | if (!new_ref) |
| 1776 | return -ENOMEM; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1777 | binder_proc_lock(proc); |
| 1778 | ref = binder_get_ref_for_node_olocked(proc, node, new_ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1779 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1780 | ret = binder_inc_ref_olocked(ref, strong, target_list); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1781 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1782 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1783 | if (new_ref && ref != new_ref) |
| 1784 | /* |
| 1785 | * Another thread created the ref first so |
| 1786 | * free the one we allocated |
| 1787 | */ |
| 1788 | kfree(new_ref); |
| 1789 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1790 | } |
| 1791 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1792 | static void binder_pop_transaction_ilocked(struct binder_thread *target_thread, |
| 1793 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1794 | { |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1795 | BUG_ON(!target_thread); |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1796 | assert_spin_locked(&target_thread->proc->inner_lock); |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1797 | BUG_ON(target_thread->transaction_stack != t); |
| 1798 | BUG_ON(target_thread->transaction_stack->from != target_thread); |
| 1799 | target_thread->transaction_stack = |
| 1800 | target_thread->transaction_stack->from_parent; |
| 1801 | t->from = NULL; |
| 1802 | } |
| 1803 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1804 | /** |
| 1805 | * binder_thread_dec_tmpref() - decrement thread->tmp_ref |
| 1806 | * @thread: thread to decrement |
| 1807 | * |
| 1808 | * A thread needs to be kept alive while being used to create or |
| 1809 | * handle a transaction. binder_get_txn_from() is used to safely |
| 1810 | * extract t->from from a binder_transaction and keep the thread |
| 1811 | * indicated by t->from from being freed. When done with that |
| 1812 | * binder_thread, this function is called to decrement the |
| 1813 | * tmp_ref and free if appropriate (thread has been released |
| 1814 | * and no transaction being processed by the driver) |
| 1815 | */ |
| 1816 | static void binder_thread_dec_tmpref(struct binder_thread *thread) |
| 1817 | { |
| 1818 | /* |
| 1819 | * atomic is used to protect the counter value while |
| 1820 | * it cannot reach zero or thread->is_dead is false |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1821 | */ |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1822 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1823 | atomic_dec(&thread->tmp_ref); |
| 1824 | if (thread->is_dead && !atomic_read(&thread->tmp_ref)) { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1825 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1826 | binder_free_thread(thread); |
| 1827 | return; |
| 1828 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1829 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | /** |
| 1833 | * binder_proc_dec_tmpref() - decrement proc->tmp_ref |
| 1834 | * @proc: proc to decrement |
| 1835 | * |
| 1836 | * A binder_proc needs to be kept alive while being used to create or |
| 1837 | * handle a transaction. proc->tmp_ref is incremented when |
| 1838 | * creating a new transaction or the binder_proc is currently in-use |
| 1839 | * by threads that are being released. When done with the binder_proc, |
| 1840 | * this function is called to decrement the counter and free the |
| 1841 | * proc if appropriate (proc has been released, all threads have |
| 1842 | * been released and not currenly in-use to process a transaction). |
| 1843 | */ |
| 1844 | static void binder_proc_dec_tmpref(struct binder_proc *proc) |
| 1845 | { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1846 | binder_inner_proc_lock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1847 | proc->tmp_ref--; |
| 1848 | if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) && |
| 1849 | !proc->tmp_ref) { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1850 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1851 | binder_free_proc(proc); |
| 1852 | return; |
| 1853 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1854 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | /** |
| 1858 | * binder_get_txn_from() - safely extract the "from" thread in transaction |
| 1859 | * @t: binder transaction for t->from |
| 1860 | * |
| 1861 | * Atomically return the "from" thread and increment the tmp_ref |
| 1862 | * count for the thread to ensure it stays alive until |
| 1863 | * binder_thread_dec_tmpref() is called. |
| 1864 | * |
| 1865 | * Return: the value of t->from |
| 1866 | */ |
| 1867 | static struct binder_thread *binder_get_txn_from( |
| 1868 | struct binder_transaction *t) |
| 1869 | { |
| 1870 | struct binder_thread *from; |
| 1871 | |
| 1872 | spin_lock(&t->lock); |
| 1873 | from = t->from; |
| 1874 | if (from) |
| 1875 | atomic_inc(&from->tmp_ref); |
| 1876 | spin_unlock(&t->lock); |
| 1877 | return from; |
| 1878 | } |
| 1879 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1880 | /** |
| 1881 | * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock |
| 1882 | * @t: binder transaction for t->from |
| 1883 | * |
| 1884 | * Same as binder_get_txn_from() except it also acquires the proc->inner_lock |
| 1885 | * to guarantee that the thread cannot be released while operating on it. |
| 1886 | * The caller must call binder_inner_proc_unlock() to release the inner lock |
| 1887 | * as well as call binder_dec_thread_txn() to release the reference. |
| 1888 | * |
| 1889 | * Return: the value of t->from |
| 1890 | */ |
| 1891 | static struct binder_thread *binder_get_txn_from_and_acq_inner( |
| 1892 | struct binder_transaction *t) |
| 1893 | { |
| 1894 | struct binder_thread *from; |
| 1895 | |
| 1896 | from = binder_get_txn_from(t); |
| 1897 | if (!from) |
| 1898 | return NULL; |
| 1899 | binder_inner_proc_lock(from->proc); |
| 1900 | if (t->from) { |
| 1901 | BUG_ON(from != t->from); |
| 1902 | return from; |
| 1903 | } |
| 1904 | binder_inner_proc_unlock(from->proc); |
| 1905 | binder_thread_dec_tmpref(from); |
| 1906 | return NULL; |
| 1907 | } |
| 1908 | |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 1909 | /** |
| 1910 | * binder_free_txn_fixups() - free unprocessed fd fixups |
| 1911 | * @t: binder transaction for t->from |
| 1912 | * |
| 1913 | * If the transaction is being torn down prior to being |
| 1914 | * processed by the target process, free all of the |
| 1915 | * fd fixups and fput the file structs. It is safe to |
| 1916 | * call this function after the fixups have been |
| 1917 | * processed -- in that case, the list will be empty. |
| 1918 | */ |
| 1919 | static void binder_free_txn_fixups(struct binder_transaction *t) |
| 1920 | { |
| 1921 | struct binder_txn_fd_fixup *fixup, *tmp; |
| 1922 | |
| 1923 | list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) { |
| 1924 | fput(fixup->file); |
| 1925 | list_del(&fixup->fixup_entry); |
| 1926 | kfree(fixup); |
| 1927 | } |
| 1928 | } |
| 1929 | |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1930 | static void binder_free_transaction(struct binder_transaction *t) |
| 1931 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1932 | if (t->buffer) |
| 1933 | t->buffer->transaction = NULL; |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 1934 | binder_free_txn_fixups(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1935 | kfree(t); |
| 1936 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 1937 | } |
| 1938 | |
| 1939 | static void binder_send_failed_reply(struct binder_transaction *t, |
| 1940 | uint32_t error_code) |
| 1941 | { |
| 1942 | struct binder_thread *target_thread; |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 1943 | struct binder_transaction *next; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1944 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1945 | BUG_ON(t->flags & TF_ONE_WAY); |
| 1946 | while (1) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1947 | target_thread = binder_get_txn_from_and_acq_inner(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1948 | if (target_thread) { |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1949 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 1950 | "send failed reply for transaction %d to %d:%d\n", |
| 1951 | t->debug_id, |
| 1952 | target_thread->proc->pid, |
| 1953 | target_thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1954 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1955 | binder_pop_transaction_ilocked(target_thread, t); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1956 | if (target_thread->reply_error.cmd == BR_OK) { |
| 1957 | target_thread->reply_error.cmd = error_code; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 1958 | binder_enqueue_thread_work_ilocked( |
| 1959 | target_thread, |
| 1960 | &target_thread->reply_error.work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1961 | wake_up_interruptible(&target_thread->wait); |
| 1962 | } else { |
Todd Kjos | e46a3b3 | 2018-02-07 12:38:47 -0800 | [diff] [blame] | 1963 | /* |
| 1964 | * Cannot get here for normal operation, but |
| 1965 | * we can if multiple synchronous transactions |
| 1966 | * are sent without blocking for responses. |
| 1967 | * Just ignore the 2nd error in this case. |
| 1968 | */ |
| 1969 | pr_warn("Unexpected reply error: %u\n", |
| 1970 | target_thread->reply_error.cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1971 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1972 | binder_inner_proc_unlock(target_thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1973 | binder_thread_dec_tmpref(target_thread); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1974 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1975 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1976 | } |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 1977 | next = t->from_parent; |
| 1978 | |
| 1979 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 1980 | "send failed reply for transaction %d, target dead\n", |
| 1981 | t->debug_id); |
| 1982 | |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1983 | binder_free_transaction(t); |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 1984 | if (next == NULL) { |
| 1985 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 1986 | "reply failed, no target thread at root\n"); |
| 1987 | return; |
| 1988 | } |
| 1989 | t = next; |
| 1990 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 1991 | "reply failed, no target thread -- retry %d\n", |
| 1992 | t->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1993 | } |
| 1994 | } |
| 1995 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 1996 | /** |
Martijn Coenen | fb2c445 | 2017-11-13 10:06:08 +0100 | [diff] [blame] | 1997 | * binder_cleanup_transaction() - cleans up undelivered transaction |
| 1998 | * @t: transaction that needs to be cleaned up |
| 1999 | * @reason: reason the transaction wasn't delivered |
| 2000 | * @error_code: error to return to caller (if synchronous call) |
| 2001 | */ |
| 2002 | static void binder_cleanup_transaction(struct binder_transaction *t, |
| 2003 | const char *reason, |
| 2004 | uint32_t error_code) |
| 2005 | { |
| 2006 | if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) { |
| 2007 | binder_send_failed_reply(t, error_code); |
| 2008 | } else { |
| 2009 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 2010 | "undelivered transaction %d, %s\n", |
| 2011 | t->debug_id, reason); |
| 2012 | binder_free_transaction(t); |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | /** |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2017 | * binder_validate_object() - checks for a valid metadata object in a buffer. |
| 2018 | * @buffer: binder_buffer that we're parsing. |
| 2019 | * @offset: offset in the buffer at which to validate an object. |
| 2020 | * |
| 2021 | * Return: If there's a valid metadata object at @offset in @buffer, the |
| 2022 | * size of that object. Otherwise, it returns zero. |
| 2023 | */ |
| 2024 | static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset) |
| 2025 | { |
| 2026 | /* Check if we can read a header first */ |
| 2027 | struct binder_object_header *hdr; |
| 2028 | size_t object_size = 0; |
| 2029 | |
Dan Carpenter | 361f2dd | 2018-03-29 12:14:40 +0300 | [diff] [blame] | 2030 | if (buffer->data_size < sizeof(*hdr) || |
| 2031 | offset > buffer->data_size - sizeof(*hdr) || |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2032 | !IS_ALIGNED(offset, sizeof(u32))) |
| 2033 | return 0; |
| 2034 | |
| 2035 | /* Ok, now see if we can read a complete object. */ |
| 2036 | hdr = (struct binder_object_header *)(buffer->data + offset); |
| 2037 | switch (hdr->type) { |
| 2038 | case BINDER_TYPE_BINDER: |
| 2039 | case BINDER_TYPE_WEAK_BINDER: |
| 2040 | case BINDER_TYPE_HANDLE: |
| 2041 | case BINDER_TYPE_WEAK_HANDLE: |
| 2042 | object_size = sizeof(struct flat_binder_object); |
| 2043 | break; |
| 2044 | case BINDER_TYPE_FD: |
| 2045 | object_size = sizeof(struct binder_fd_object); |
| 2046 | break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2047 | case BINDER_TYPE_PTR: |
| 2048 | object_size = sizeof(struct binder_buffer_object); |
| 2049 | break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2050 | case BINDER_TYPE_FDA: |
| 2051 | object_size = sizeof(struct binder_fd_array_object); |
| 2052 | break; |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2053 | default: |
| 2054 | return 0; |
| 2055 | } |
| 2056 | if (offset <= buffer->data_size - object_size && |
| 2057 | buffer->data_size >= object_size) |
| 2058 | return object_size; |
| 2059 | else |
| 2060 | return 0; |
| 2061 | } |
| 2062 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2063 | /** |
| 2064 | * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer. |
| 2065 | * @b: binder_buffer containing the object |
| 2066 | * @index: index in offset array at which the binder_buffer_object is |
| 2067 | * located |
| 2068 | * @start: points to the start of the offset array |
| 2069 | * @num_valid: the number of valid offsets in the offset array |
| 2070 | * |
| 2071 | * Return: If @index is within the valid range of the offset array |
| 2072 | * described by @start and @num_valid, and if there's a valid |
| 2073 | * binder_buffer_object at the offset found in index @index |
| 2074 | * of the offset array, that object is returned. Otherwise, |
| 2075 | * %NULL is returned. |
| 2076 | * Note that the offset found in index @index itself is not |
| 2077 | * verified; this function assumes that @num_valid elements |
| 2078 | * from @start were previously verified to have valid offsets. |
| 2079 | */ |
| 2080 | static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b, |
| 2081 | binder_size_t index, |
| 2082 | binder_size_t *start, |
| 2083 | binder_size_t num_valid) |
| 2084 | { |
| 2085 | struct binder_buffer_object *buffer_obj; |
| 2086 | binder_size_t *offp; |
| 2087 | |
| 2088 | if (index >= num_valid) |
| 2089 | return NULL; |
| 2090 | |
| 2091 | offp = start + index; |
| 2092 | buffer_obj = (struct binder_buffer_object *)(b->data + *offp); |
| 2093 | if (buffer_obj->hdr.type != BINDER_TYPE_PTR) |
| 2094 | return NULL; |
| 2095 | |
| 2096 | return buffer_obj; |
| 2097 | } |
| 2098 | |
| 2099 | /** |
| 2100 | * binder_validate_fixup() - validates pointer/fd fixups happen in order. |
| 2101 | * @b: transaction buffer |
| 2102 | * @objects_start start of objects buffer |
| 2103 | * @buffer: binder_buffer_object in which to fix up |
| 2104 | * @offset: start offset in @buffer to fix up |
| 2105 | * @last_obj: last binder_buffer_object that we fixed up in |
| 2106 | * @last_min_offset: minimum fixup offset in @last_obj |
| 2107 | * |
| 2108 | * Return: %true if a fixup in buffer @buffer at offset @offset is |
| 2109 | * allowed. |
| 2110 | * |
| 2111 | * For safety reasons, we only allow fixups inside a buffer to happen |
| 2112 | * at increasing offsets; additionally, we only allow fixup on the last |
| 2113 | * buffer object that was verified, or one of its parents. |
| 2114 | * |
| 2115 | * Example of what is allowed: |
| 2116 | * |
| 2117 | * A |
| 2118 | * B (parent = A, offset = 0) |
| 2119 | * C (parent = A, offset = 16) |
| 2120 | * D (parent = C, offset = 0) |
| 2121 | * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset) |
| 2122 | * |
| 2123 | * Examples of what is not allowed: |
| 2124 | * |
| 2125 | * Decreasing offsets within the same parent: |
| 2126 | * A |
| 2127 | * C (parent = A, offset = 16) |
| 2128 | * B (parent = A, offset = 0) // decreasing offset within A |
| 2129 | * |
| 2130 | * Referring to a parent that wasn't the last object or any of its parents: |
| 2131 | * A |
| 2132 | * B (parent = A, offset = 0) |
| 2133 | * C (parent = A, offset = 0) |
| 2134 | * C (parent = A, offset = 16) |
| 2135 | * D (parent = B, offset = 0) // B is not A or any of A's parents |
| 2136 | */ |
| 2137 | static bool binder_validate_fixup(struct binder_buffer *b, |
| 2138 | binder_size_t *objects_start, |
| 2139 | struct binder_buffer_object *buffer, |
| 2140 | binder_size_t fixup_offset, |
| 2141 | struct binder_buffer_object *last_obj, |
| 2142 | binder_size_t last_min_offset) |
| 2143 | { |
| 2144 | if (!last_obj) { |
| 2145 | /* Nothing to fix up in */ |
| 2146 | return false; |
| 2147 | } |
| 2148 | |
| 2149 | while (last_obj != buffer) { |
| 2150 | /* |
| 2151 | * Safe to retrieve the parent of last_obj, since it |
| 2152 | * was already previously verified by the driver. |
| 2153 | */ |
| 2154 | if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0) |
| 2155 | return false; |
| 2156 | last_min_offset = last_obj->parent_offset + sizeof(uintptr_t); |
| 2157 | last_obj = (struct binder_buffer_object *) |
| 2158 | (b->data + *(objects_start + last_obj->parent)); |
| 2159 | } |
| 2160 | return (fixup_offset >= last_min_offset); |
| 2161 | } |
| 2162 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2163 | static void binder_transaction_buffer_release(struct binder_proc *proc, |
| 2164 | struct binder_buffer *buffer, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2165 | binder_size_t *failed_at) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2166 | { |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2167 | binder_size_t *offp, *off_start, *off_end; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2168 | int debug_id = buffer->debug_id; |
| 2169 | |
| 2170 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | 8ca86f1 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 2171 | "%d buffer release %d, size %zd-%zd, failed at %pK\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2172 | proc->pid, buffer->debug_id, |
| 2173 | buffer->data_size, buffer->offsets_size, failed_at); |
| 2174 | |
| 2175 | if (buffer->target_node) |
| 2176 | binder_dec_node(buffer->target_node, 1, 0); |
| 2177 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2178 | off_start = (binder_size_t *)(buffer->data + |
| 2179 | ALIGN(buffer->data_size, sizeof(void *))); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2180 | if (failed_at) |
| 2181 | off_end = failed_at; |
| 2182 | else |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2183 | off_end = (void *)off_start + buffer->offsets_size; |
| 2184 | for (offp = off_start; offp < off_end; offp++) { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2185 | struct binder_object_header *hdr; |
| 2186 | size_t object_size = binder_validate_object(buffer, *offp); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2187 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2188 | if (object_size == 0) { |
| 2189 | pr_err("transaction release %d bad object at offset %lld, size %zd\n", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2190 | debug_id, (u64)*offp, buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2191 | continue; |
| 2192 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2193 | hdr = (struct binder_object_header *)(buffer->data + *offp); |
| 2194 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2195 | case BINDER_TYPE_BINDER: |
| 2196 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2197 | struct flat_binder_object *fp; |
| 2198 | struct binder_node *node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2199 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2200 | fp = to_flat_binder_object(hdr); |
| 2201 | node = binder_get_node(proc, fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2202 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2203 | pr_err("transaction release %d bad node %016llx\n", |
| 2204 | debug_id, (u64)fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2205 | break; |
| 2206 | } |
| 2207 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2208 | " node %d u%016llx\n", |
| 2209 | node->debug_id, (u64)node->ptr); |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2210 | binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER, |
| 2211 | 0); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2212 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2213 | } break; |
| 2214 | case BINDER_TYPE_HANDLE: |
| 2215 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2216 | struct flat_binder_object *fp; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2217 | struct binder_ref_data rdata; |
| 2218 | int ret; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 2219 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2220 | fp = to_flat_binder_object(hdr); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2221 | ret = binder_dec_ref_for_handle(proc, fp->handle, |
| 2222 | hdr->type == BINDER_TYPE_HANDLE, &rdata); |
| 2223 | |
| 2224 | if (ret) { |
| 2225 | pr_err("transaction release %d bad handle %d, ret = %d\n", |
| 2226 | debug_id, fp->handle, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2227 | break; |
| 2228 | } |
| 2229 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2230 | " ref %d desc %d\n", |
| 2231 | rdata.debug_id, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2232 | } break; |
| 2233 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2234 | case BINDER_TYPE_FD: { |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2235 | /* |
| 2236 | * No need to close the file here since user-space |
| 2237 | * closes it for for successfully delivered |
| 2238 | * transactions. For transactions that weren't |
| 2239 | * delivered, the new fd was never allocated so |
| 2240 | * there is no need to close and the fput on the |
| 2241 | * file is done when the transaction is torn |
| 2242 | * down. |
| 2243 | */ |
| 2244 | WARN_ON(failed_at && |
| 2245 | proc->tsk == current->group_leader); |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2246 | } break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2247 | case BINDER_TYPE_PTR: |
| 2248 | /* |
| 2249 | * Nothing to do here, this will get cleaned up when the |
| 2250 | * transaction buffer gets freed |
| 2251 | */ |
| 2252 | break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2253 | case BINDER_TYPE_FDA: { |
| 2254 | struct binder_fd_array_object *fda; |
| 2255 | struct binder_buffer_object *parent; |
| 2256 | uintptr_t parent_buffer; |
| 2257 | u32 *fd_array; |
| 2258 | size_t fd_index; |
| 2259 | binder_size_t fd_buf_size; |
| 2260 | |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2261 | if (proc->tsk != current->group_leader) { |
| 2262 | /* |
| 2263 | * Nothing to do if running in sender context |
| 2264 | * The fd fixups have not been applied so no |
| 2265 | * fds need to be closed. |
| 2266 | */ |
| 2267 | continue; |
| 2268 | } |
| 2269 | |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2270 | fda = to_binder_fd_array_object(hdr); |
| 2271 | parent = binder_validate_ptr(buffer, fda->parent, |
| 2272 | off_start, |
| 2273 | offp - off_start); |
| 2274 | if (!parent) { |
Arvind Yadav | f7f84fd | 2017-09-25 12:52:11 +0530 | [diff] [blame] | 2275 | pr_err("transaction release %d bad parent offset\n", |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2276 | debug_id); |
| 2277 | continue; |
| 2278 | } |
| 2279 | /* |
| 2280 | * Since the parent was already fixed up, convert it |
| 2281 | * back to kernel address space to access it |
| 2282 | */ |
| 2283 | parent_buffer = parent->buffer - |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2284 | binder_alloc_get_user_buffer_offset( |
| 2285 | &proc->alloc); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2286 | |
| 2287 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2288 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2289 | pr_err("transaction release %d invalid number of fds (%lld)\n", |
| 2290 | debug_id, (u64)fda->num_fds); |
| 2291 | continue; |
| 2292 | } |
| 2293 | if (fd_buf_size > parent->length || |
| 2294 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2295 | /* No space for all file descriptors here. */ |
| 2296 | pr_err("transaction release %d not enough space for %lld fds in buffer\n", |
| 2297 | debug_id, (u64)fda->num_fds); |
| 2298 | continue; |
| 2299 | } |
Arnd Bergmann | 1c363eae | 2017-09-05 10:56:13 +0200 | [diff] [blame] | 2300 | fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2301 | for (fd_index = 0; fd_index < fda->num_fds; fd_index++) |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2302 | ksys_close(fd_array[fd_index]); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2303 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2304 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 2305 | pr_err("transaction release %d bad object type %x\n", |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2306 | debug_id, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2307 | break; |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 | |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2312 | static int binder_translate_binder(struct flat_binder_object *fp, |
| 2313 | struct binder_transaction *t, |
| 2314 | struct binder_thread *thread) |
| 2315 | { |
| 2316 | struct binder_node *node; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2317 | struct binder_proc *proc = thread->proc; |
| 2318 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2319 | struct binder_ref_data rdata; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2320 | int ret = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2321 | |
| 2322 | node = binder_get_node(proc, fp->binder); |
| 2323 | if (!node) { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2324 | node = binder_new_node(proc, fp); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2325 | if (!node) |
| 2326 | return -ENOMEM; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2327 | } |
| 2328 | if (fp->cookie != node->cookie) { |
| 2329 | binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n", |
| 2330 | proc->pid, thread->pid, (u64)fp->binder, |
| 2331 | node->debug_id, (u64)fp->cookie, |
| 2332 | (u64)node->cookie); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2333 | ret = -EINVAL; |
| 2334 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2335 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2336 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2337 | ret = -EPERM; |
| 2338 | goto done; |
| 2339 | } |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2340 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2341 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2342 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2343 | &thread->todo, &rdata); |
| 2344 | if (ret) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2345 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2346 | |
| 2347 | if (fp->hdr.type == BINDER_TYPE_BINDER) |
| 2348 | fp->hdr.type = BINDER_TYPE_HANDLE; |
| 2349 | else |
| 2350 | fp->hdr.type = BINDER_TYPE_WEAK_HANDLE; |
| 2351 | fp->binder = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2352 | fp->handle = rdata.desc; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2353 | fp->cookie = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2354 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2355 | trace_binder_transaction_node_to_ref(t, node, &rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2356 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2357 | " node %d u%016llx -> ref %d desc %d\n", |
| 2358 | node->debug_id, (u64)node->ptr, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2359 | rdata.debug_id, rdata.desc); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2360 | done: |
| 2361 | binder_put_node(node); |
| 2362 | return ret; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | static int binder_translate_handle(struct flat_binder_object *fp, |
| 2366 | struct binder_transaction *t, |
| 2367 | struct binder_thread *thread) |
| 2368 | { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2369 | struct binder_proc *proc = thread->proc; |
| 2370 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2371 | struct binder_node *node; |
| 2372 | struct binder_ref_data src_rdata; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2373 | int ret = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2374 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2375 | node = binder_get_node_from_ref(proc, fp->handle, |
| 2376 | fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata); |
| 2377 | if (!node) { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2378 | binder_user_error("%d:%d got transaction with invalid handle, %d\n", |
| 2379 | proc->pid, thread->pid, fp->handle); |
| 2380 | return -EINVAL; |
| 2381 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2382 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2383 | ret = -EPERM; |
| 2384 | goto done; |
| 2385 | } |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2386 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2387 | binder_node_lock(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2388 | if (node->proc == target_proc) { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2389 | if (fp->hdr.type == BINDER_TYPE_HANDLE) |
| 2390 | fp->hdr.type = BINDER_TYPE_BINDER; |
| 2391 | else |
| 2392 | fp->hdr.type = BINDER_TYPE_WEAK_BINDER; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2393 | fp->binder = node->ptr; |
| 2394 | fp->cookie = node->cookie; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2395 | if (node->proc) |
| 2396 | binder_inner_proc_lock(node->proc); |
| 2397 | binder_inc_node_nilocked(node, |
| 2398 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2399 | 0, NULL); |
| 2400 | if (node->proc) |
| 2401 | binder_inner_proc_unlock(node->proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2402 | trace_binder_transaction_ref_to_node(t, node, &src_rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2403 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2404 | " ref %d desc %d -> node %d u%016llx\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2405 | src_rdata.debug_id, src_rdata.desc, node->debug_id, |
| 2406 | (u64)node->ptr); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2407 | binder_node_unlock(node); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2408 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2409 | struct binder_ref_data dest_rdata; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2410 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2411 | binder_node_unlock(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2412 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2413 | fp->hdr.type == BINDER_TYPE_HANDLE, |
| 2414 | NULL, &dest_rdata); |
| 2415 | if (ret) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2416 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2417 | |
| 2418 | fp->binder = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2419 | fp->handle = dest_rdata.desc; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2420 | fp->cookie = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2421 | trace_binder_transaction_ref_to_ref(t, node, &src_rdata, |
| 2422 | &dest_rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2423 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2424 | " ref %d desc %d -> ref %d desc %d (node %d)\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2425 | src_rdata.debug_id, src_rdata.desc, |
| 2426 | dest_rdata.debug_id, dest_rdata.desc, |
| 2427 | node->debug_id); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2428 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2429 | done: |
| 2430 | binder_put_node(node); |
| 2431 | return ret; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2432 | } |
| 2433 | |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2434 | static int binder_translate_fd(u32 *fdp, |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2435 | struct binder_transaction *t, |
| 2436 | struct binder_thread *thread, |
| 2437 | struct binder_transaction *in_reply_to) |
| 2438 | { |
| 2439 | struct binder_proc *proc = thread->proc; |
| 2440 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2441 | struct binder_txn_fd_fixup *fixup; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2442 | struct file *file; |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2443 | int ret = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2444 | bool target_allows_fd; |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2445 | int fd = *fdp; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2446 | |
| 2447 | if (in_reply_to) |
| 2448 | target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS); |
| 2449 | else |
| 2450 | target_allows_fd = t->buffer->target_node->accept_fds; |
| 2451 | if (!target_allows_fd) { |
| 2452 | binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n", |
| 2453 | proc->pid, thread->pid, |
| 2454 | in_reply_to ? "reply" : "transaction", |
| 2455 | fd); |
| 2456 | ret = -EPERM; |
| 2457 | goto err_fd_not_accepted; |
| 2458 | } |
| 2459 | |
| 2460 | file = fget(fd); |
| 2461 | if (!file) { |
| 2462 | binder_user_error("%d:%d got transaction with invalid fd, %d\n", |
| 2463 | proc->pid, thread->pid, fd); |
| 2464 | ret = -EBADF; |
| 2465 | goto err_fget; |
| 2466 | } |
| 2467 | ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file); |
| 2468 | if (ret < 0) { |
| 2469 | ret = -EPERM; |
| 2470 | goto err_security; |
| 2471 | } |
| 2472 | |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2473 | /* |
| 2474 | * Add fixup record for this transaction. The allocation |
| 2475 | * of the fd in the target needs to be done from a |
| 2476 | * target thread. |
| 2477 | */ |
| 2478 | fixup = kzalloc(sizeof(*fixup), GFP_KERNEL); |
| 2479 | if (!fixup) { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2480 | ret = -ENOMEM; |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2481 | goto err_alloc; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2482 | } |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2483 | fixup->file = file; |
| 2484 | fixup->offset = (uintptr_t)fdp - (uintptr_t)t->buffer->data; |
| 2485 | trace_binder_transaction_fd_send(t, fd, fixup->offset); |
| 2486 | list_add_tail(&fixup->fixup_entry, &t->fd_fixups); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2487 | |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2488 | return ret; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2489 | |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2490 | err_alloc: |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2491 | err_security: |
| 2492 | fput(file); |
| 2493 | err_fget: |
| 2494 | err_fd_not_accepted: |
| 2495 | return ret; |
| 2496 | } |
| 2497 | |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2498 | static int binder_translate_fd_array(struct binder_fd_array_object *fda, |
| 2499 | struct binder_buffer_object *parent, |
| 2500 | struct binder_transaction *t, |
| 2501 | struct binder_thread *thread, |
| 2502 | struct binder_transaction *in_reply_to) |
| 2503 | { |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2504 | binder_size_t fdi, fd_buf_size; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2505 | uintptr_t parent_buffer; |
| 2506 | u32 *fd_array; |
| 2507 | struct binder_proc *proc = thread->proc; |
| 2508 | struct binder_proc *target_proc = t->to_proc; |
| 2509 | |
| 2510 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2511 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2512 | binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n", |
| 2513 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2514 | return -EINVAL; |
| 2515 | } |
| 2516 | if (fd_buf_size > parent->length || |
| 2517 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2518 | /* No space for all file descriptors here. */ |
| 2519 | binder_user_error("%d:%d not enough space to store %lld fds in buffer\n", |
| 2520 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2521 | return -EINVAL; |
| 2522 | } |
| 2523 | /* |
| 2524 | * Since the parent was already fixed up, convert it |
| 2525 | * back to the kernel address space to access it |
| 2526 | */ |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2527 | parent_buffer = parent->buffer - |
| 2528 | binder_alloc_get_user_buffer_offset(&target_proc->alloc); |
Arnd Bergmann | 1c363eae | 2017-09-05 10:56:13 +0200 | [diff] [blame] | 2529 | fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2530 | if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) { |
| 2531 | binder_user_error("%d:%d parent offset not aligned correctly.\n", |
| 2532 | proc->pid, thread->pid); |
| 2533 | return -EINVAL; |
| 2534 | } |
| 2535 | for (fdi = 0; fdi < fda->num_fds; fdi++) { |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2536 | int ret = binder_translate_fd(&fd_array[fdi], t, thread, |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2537 | in_reply_to); |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2538 | if (ret < 0) |
| 2539 | return ret; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2540 | } |
| 2541 | return 0; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2542 | } |
| 2543 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2544 | static int binder_fixup_parent(struct binder_transaction *t, |
| 2545 | struct binder_thread *thread, |
| 2546 | struct binder_buffer_object *bp, |
| 2547 | binder_size_t *off_start, |
| 2548 | binder_size_t num_valid, |
| 2549 | struct binder_buffer_object *last_fixup_obj, |
| 2550 | binder_size_t last_fixup_min_off) |
| 2551 | { |
| 2552 | struct binder_buffer_object *parent; |
| 2553 | u8 *parent_buffer; |
| 2554 | struct binder_buffer *b = t->buffer; |
| 2555 | struct binder_proc *proc = thread->proc; |
| 2556 | struct binder_proc *target_proc = t->to_proc; |
| 2557 | |
| 2558 | if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT)) |
| 2559 | return 0; |
| 2560 | |
| 2561 | parent = binder_validate_ptr(b, bp->parent, off_start, num_valid); |
| 2562 | if (!parent) { |
| 2563 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 2564 | proc->pid, thread->pid); |
| 2565 | return -EINVAL; |
| 2566 | } |
| 2567 | |
| 2568 | if (!binder_validate_fixup(b, off_start, |
| 2569 | parent, bp->parent_offset, |
| 2570 | last_fixup_obj, |
| 2571 | last_fixup_min_off)) { |
| 2572 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 2573 | proc->pid, thread->pid); |
| 2574 | return -EINVAL; |
| 2575 | } |
| 2576 | |
| 2577 | if (parent->length < sizeof(binder_uintptr_t) || |
| 2578 | bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) { |
| 2579 | /* No space for a pointer here! */ |
| 2580 | binder_user_error("%d:%d got transaction with invalid parent offset\n", |
| 2581 | proc->pid, thread->pid); |
| 2582 | return -EINVAL; |
| 2583 | } |
Arnd Bergmann | 1c363eae | 2017-09-05 10:56:13 +0200 | [diff] [blame] | 2584 | parent_buffer = (u8 *)((uintptr_t)parent->buffer - |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2585 | binder_alloc_get_user_buffer_offset( |
| 2586 | &target_proc->alloc)); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2587 | *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer; |
| 2588 | |
| 2589 | return 0; |
| 2590 | } |
| 2591 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2592 | /** |
| 2593 | * binder_proc_transaction() - sends a transaction to a process and wakes it up |
| 2594 | * @t: transaction to send |
| 2595 | * @proc: process to send the transaction to |
| 2596 | * @thread: thread in @proc to send the transaction to (may be NULL) |
| 2597 | * |
| 2598 | * This function queues a transaction to the specified process. It will try |
| 2599 | * to find a thread in the target process to handle the transaction and |
| 2600 | * wake it up. If no thread is found, the work is queued to the proc |
| 2601 | * waitqueue. |
| 2602 | * |
| 2603 | * If the @thread parameter is not NULL, the transaction is always queued |
| 2604 | * to the waitlist of that specific thread. |
| 2605 | * |
| 2606 | * Return: true if the transactions was successfully queued |
| 2607 | * false if the target process or thread is dead |
| 2608 | */ |
| 2609 | static bool binder_proc_transaction(struct binder_transaction *t, |
| 2610 | struct binder_proc *proc, |
| 2611 | struct binder_thread *thread) |
| 2612 | { |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2613 | struct binder_node *node = t->buffer->target_node; |
| 2614 | bool oneway = !!(t->flags & TF_ONE_WAY); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2615 | bool pending_async = false; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2616 | |
| 2617 | BUG_ON(!node); |
| 2618 | binder_node_lock(node); |
| 2619 | if (oneway) { |
| 2620 | BUG_ON(thread); |
| 2621 | if (node->has_async_transaction) { |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2622 | pending_async = true; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2623 | } else { |
Gustavo A. R. Silva | 197410a | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 2624 | node->has_async_transaction = true; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2625 | } |
| 2626 | } |
| 2627 | |
| 2628 | binder_inner_proc_lock(proc); |
| 2629 | |
| 2630 | if (proc->is_dead || (thread && thread->is_dead)) { |
| 2631 | binder_inner_proc_unlock(proc); |
| 2632 | binder_node_unlock(node); |
| 2633 | return false; |
| 2634 | } |
| 2635 | |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2636 | if (!thread && !pending_async) |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2637 | thread = binder_select_thread_ilocked(proc); |
| 2638 | |
| 2639 | if (thread) |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2640 | binder_enqueue_thread_work_ilocked(thread, &t->work); |
| 2641 | else if (!pending_async) |
| 2642 | binder_enqueue_work_ilocked(&t->work, &proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2643 | else |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2644 | binder_enqueue_work_ilocked(&t->work, &node->async_todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2645 | |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2646 | if (!pending_async) |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2647 | binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */); |
| 2648 | |
| 2649 | binder_inner_proc_unlock(proc); |
| 2650 | binder_node_unlock(node); |
| 2651 | |
| 2652 | return true; |
| 2653 | } |
| 2654 | |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2655 | /** |
| 2656 | * binder_get_node_refs_for_txn() - Get required refs on node for txn |
| 2657 | * @node: struct binder_node for which to get refs |
| 2658 | * @proc: returns @node->proc if valid |
| 2659 | * @error: if no @proc then returns BR_DEAD_REPLY |
| 2660 | * |
| 2661 | * User-space normally keeps the node alive when creating a transaction |
| 2662 | * since it has a reference to the target. The local strong ref keeps it |
| 2663 | * alive if the sending process dies before the target process processes |
| 2664 | * the transaction. If the source process is malicious or has a reference |
| 2665 | * counting bug, relying on the local strong ref can fail. |
| 2666 | * |
| 2667 | * Since user-space can cause the local strong ref to go away, we also take |
| 2668 | * a tmpref on the node to ensure it survives while we are constructing |
| 2669 | * the transaction. We also need a tmpref on the proc while we are |
| 2670 | * constructing the transaction, so we take that here as well. |
| 2671 | * |
| 2672 | * Return: The target_node with refs taken or NULL if no @node->proc is NULL. |
| 2673 | * Also sets @proc if valid. If the @node->proc is NULL indicating that the |
| 2674 | * target proc has died, @error is set to BR_DEAD_REPLY |
| 2675 | */ |
| 2676 | static struct binder_node *binder_get_node_refs_for_txn( |
| 2677 | struct binder_node *node, |
| 2678 | struct binder_proc **procp, |
| 2679 | uint32_t *error) |
| 2680 | { |
| 2681 | struct binder_node *target_node = NULL; |
| 2682 | |
| 2683 | binder_node_inner_lock(node); |
| 2684 | if (node->proc) { |
| 2685 | target_node = node; |
| 2686 | binder_inc_node_nilocked(node, 1, 0, NULL); |
| 2687 | binder_inc_node_tmpref_ilocked(node); |
| 2688 | node->proc->tmp_ref++; |
| 2689 | *procp = node->proc; |
| 2690 | } else |
| 2691 | *error = BR_DEAD_REPLY; |
| 2692 | binder_node_inner_unlock(node); |
| 2693 | |
| 2694 | return target_node; |
| 2695 | } |
| 2696 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2697 | static void binder_transaction(struct binder_proc *proc, |
| 2698 | struct binder_thread *thread, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2699 | struct binder_transaction_data *tr, int reply, |
| 2700 | binder_size_t extra_buffers_size) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2701 | { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2702 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2703 | struct binder_transaction *t; |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame] | 2704 | struct binder_work *w; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2705 | struct binder_work *tcomplete; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2706 | binder_size_t *offp, *off_end, *off_start; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 2707 | binder_size_t off_min; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2708 | u8 *sg_bufp, *sg_buf_end; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2709 | struct binder_proc *target_proc = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2710 | struct binder_thread *target_thread = NULL; |
| 2711 | struct binder_node *target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2712 | struct binder_transaction *in_reply_to = NULL; |
| 2713 | struct binder_transaction_log_entry *e; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2714 | uint32_t return_error = 0; |
| 2715 | uint32_t return_error_param = 0; |
| 2716 | uint32_t return_error_line = 0; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2717 | struct binder_buffer_object *last_fixup_obj = NULL; |
| 2718 | binder_size_t last_fixup_min_off = 0; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 2719 | struct binder_context *context = proc->context; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2720 | int t_debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2721 | |
| 2722 | e = binder_transaction_log_add(&binder_transaction_log); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2723 | e->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2724 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); |
| 2725 | e->from_proc = proc->pid; |
| 2726 | e->from_thread = thread->pid; |
| 2727 | e->target_handle = tr->target.handle; |
| 2728 | e->data_size = tr->data_size; |
| 2729 | e->offsets_size = tr->offsets_size; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 2730 | e->context_name = proc->context->name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2731 | |
| 2732 | if (reply) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2733 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2734 | in_reply_to = thread->transaction_stack; |
| 2735 | if (in_reply_to == NULL) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2736 | binder_inner_proc_unlock(proc); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2737 | 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] | 2738 | proc->pid, thread->pid); |
| 2739 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2740 | return_error_param = -EPROTO; |
| 2741 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2742 | goto err_empty_call_stack; |
| 2743 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2744 | if (in_reply_to->to_thread != thread) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2745 | spin_lock(&in_reply_to->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2746 | 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] | 2747 | proc->pid, thread->pid, in_reply_to->debug_id, |
| 2748 | in_reply_to->to_proc ? |
| 2749 | in_reply_to->to_proc->pid : 0, |
| 2750 | in_reply_to->to_thread ? |
| 2751 | in_reply_to->to_thread->pid : 0); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2752 | spin_unlock(&in_reply_to->lock); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2753 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2754 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2755 | return_error_param = -EPROTO; |
| 2756 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2757 | in_reply_to = NULL; |
| 2758 | goto err_bad_call_stack; |
| 2759 | } |
| 2760 | thread->transaction_stack = in_reply_to->to_parent; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2761 | binder_inner_proc_unlock(proc); |
| 2762 | binder_set_nice(in_reply_to->saved_priority); |
| 2763 | 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] | 2764 | if (target_thread == NULL) { |
| 2765 | return_error = BR_DEAD_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2766 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2767 | goto err_dead_binder; |
| 2768 | } |
| 2769 | if (target_thread->transaction_stack != in_reply_to) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2770 | 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] | 2771 | proc->pid, thread->pid, |
| 2772 | target_thread->transaction_stack ? |
| 2773 | target_thread->transaction_stack->debug_id : 0, |
| 2774 | in_reply_to->debug_id); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2775 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2776 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2777 | return_error_param = -EPROTO; |
| 2778 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2779 | in_reply_to = NULL; |
| 2780 | target_thread = NULL; |
| 2781 | goto err_dead_binder; |
| 2782 | } |
| 2783 | target_proc = target_thread->proc; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2784 | target_proc->tmp_ref++; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2785 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2786 | } else { |
| 2787 | if (tr->target.handle) { |
| 2788 | struct binder_ref *ref; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2789 | |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2790 | /* |
| 2791 | * There must already be a strong ref |
| 2792 | * on this node. If so, do a strong |
| 2793 | * increment on the node to ensure it |
| 2794 | * stays alive until the transaction is |
| 2795 | * done. |
| 2796 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 2797 | binder_proc_lock(proc); |
| 2798 | ref = binder_get_ref_olocked(proc, tr->target.handle, |
| 2799 | true); |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2800 | if (ref) { |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2801 | target_node = binder_get_node_refs_for_txn( |
| 2802 | ref->node, &target_proc, |
| 2803 | &return_error); |
| 2804 | } else { |
| 2805 | binder_user_error("%d:%d got transaction to invalid handle\n", |
| 2806 | proc->pid, thread->pid); |
| 2807 | return_error = BR_FAILED_REPLY; |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2808 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 2809 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2810 | } else { |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 2811 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 2812 | target_node = context->binder_context_mgr_node; |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2813 | if (target_node) |
| 2814 | target_node = binder_get_node_refs_for_txn( |
| 2815 | target_node, &target_proc, |
| 2816 | &return_error); |
| 2817 | else |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2818 | return_error = BR_DEAD_REPLY; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 2819 | mutex_unlock(&context->context_mgr_node_lock); |
Martijn Coenen | 7aa135f | 2018-03-28 11:14:50 +0200 | [diff] [blame] | 2820 | if (target_node && target_proc == proc) { |
| 2821 | binder_user_error("%d:%d got transaction to context manager from process owning it\n", |
| 2822 | proc->pid, thread->pid); |
| 2823 | return_error = BR_FAILED_REPLY; |
| 2824 | return_error_param = -EINVAL; |
| 2825 | return_error_line = __LINE__; |
| 2826 | goto err_invalid_target_handle; |
| 2827 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2828 | } |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2829 | if (!target_node) { |
| 2830 | /* |
| 2831 | * return_error is set above |
| 2832 | */ |
| 2833 | return_error_param = -EINVAL; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2834 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2835 | goto err_dead_binder; |
| 2836 | } |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2837 | e->to_node = target_node->debug_id; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2838 | if (security_binder_transaction(proc->tsk, |
| 2839 | target_proc->tsk) < 0) { |
| 2840 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2841 | return_error_param = -EPERM; |
| 2842 | return_error_line = __LINE__; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2843 | goto err_invalid_target_handle; |
| 2844 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2845 | binder_inner_proc_lock(proc); |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame] | 2846 | |
| 2847 | w = list_first_entry_or_null(&thread->todo, |
| 2848 | struct binder_work, entry); |
| 2849 | if (!(tr->flags & TF_ONE_WAY) && w && |
| 2850 | w->type == BINDER_WORK_TRANSACTION) { |
| 2851 | /* |
| 2852 | * Do not allow new outgoing transaction from a |
| 2853 | * thread that has a transaction at the head of |
| 2854 | * its todo list. Only need to check the head |
| 2855 | * because binder_select_thread_ilocked picks a |
| 2856 | * thread from proc->waiting_threads to enqueue |
| 2857 | * the transaction, and nothing is queued to the |
| 2858 | * todo list while the thread is on waiting_threads. |
| 2859 | */ |
| 2860 | binder_user_error("%d:%d new transaction not allowed when there is a transaction on thread todo\n", |
| 2861 | proc->pid, thread->pid); |
| 2862 | binder_inner_proc_unlock(proc); |
| 2863 | return_error = BR_FAILED_REPLY; |
| 2864 | return_error_param = -EPROTO; |
| 2865 | return_error_line = __LINE__; |
| 2866 | goto err_bad_todo_list; |
| 2867 | } |
| 2868 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2869 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { |
| 2870 | struct binder_transaction *tmp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2871 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2872 | tmp = thread->transaction_stack; |
| 2873 | if (tmp->to_thread != thread) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2874 | spin_lock(&tmp->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2875 | 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] | 2876 | proc->pid, thread->pid, tmp->debug_id, |
| 2877 | tmp->to_proc ? tmp->to_proc->pid : 0, |
| 2878 | tmp->to_thread ? |
| 2879 | tmp->to_thread->pid : 0); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2880 | spin_unlock(&tmp->lock); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2881 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2882 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2883 | return_error_param = -EPROTO; |
| 2884 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2885 | goto err_bad_call_stack; |
| 2886 | } |
| 2887 | while (tmp) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2888 | struct binder_thread *from; |
| 2889 | |
| 2890 | spin_lock(&tmp->lock); |
| 2891 | from = tmp->from; |
| 2892 | if (from && from->proc == target_proc) { |
| 2893 | atomic_inc(&from->tmp_ref); |
| 2894 | target_thread = from; |
| 2895 | spin_unlock(&tmp->lock); |
| 2896 | break; |
| 2897 | } |
| 2898 | spin_unlock(&tmp->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2899 | tmp = tmp->from_parent; |
| 2900 | } |
| 2901 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2902 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2903 | } |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2904 | if (target_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2905 | e->to_thread = target_thread->pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2906 | e->to_proc = target_proc->pid; |
| 2907 | |
| 2908 | /* TODO: reuse incoming transaction for reply */ |
| 2909 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 2910 | if (t == NULL) { |
| 2911 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2912 | return_error_param = -ENOMEM; |
| 2913 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2914 | goto err_alloc_t_failed; |
| 2915 | } |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 2916 | INIT_LIST_HEAD(&t->fd_fixups); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2917 | binder_stats_created(BINDER_STAT_TRANSACTION); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2918 | spin_lock_init(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2919 | |
| 2920 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); |
| 2921 | if (tcomplete == NULL) { |
| 2922 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2923 | return_error_param = -ENOMEM; |
| 2924 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2925 | goto err_alloc_tcomplete_failed; |
| 2926 | } |
| 2927 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); |
| 2928 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2929 | t->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2930 | |
| 2931 | if (reply) |
| 2932 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2933 | "%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] | 2934 | proc->pid, thread->pid, t->debug_id, |
| 2935 | target_proc->pid, target_thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2936 | (u64)tr->data.ptr.buffer, |
| 2937 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2938 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 2939 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2940 | else |
| 2941 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2942 | "%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] | 2943 | proc->pid, thread->pid, t->debug_id, |
| 2944 | target_proc->pid, target_node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2945 | (u64)tr->data.ptr.buffer, |
| 2946 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2947 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 2948 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2949 | |
| 2950 | if (!reply && !(tr->flags & TF_ONE_WAY)) |
| 2951 | t->from = thread; |
| 2952 | else |
| 2953 | t->from = NULL; |
Tair Rzayev | 57bab7c | 2014-05-31 22:43:34 +0300 | [diff] [blame] | 2954 | t->sender_euid = task_euid(proc->tsk); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2955 | t->to_proc = target_proc; |
| 2956 | t->to_thread = target_thread; |
| 2957 | t->code = tr->code; |
| 2958 | t->flags = tr->flags; |
| 2959 | t->priority = task_nice(current); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2960 | |
| 2961 | trace_binder_transaction(reply, t, target_node); |
| 2962 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2963 | t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2964 | tr->offsets_size, extra_buffers_size, |
| 2965 | !reply && (t->flags & TF_ONE_WAY)); |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2966 | if (IS_ERR(t->buffer)) { |
| 2967 | /* |
| 2968 | * -ESRCH indicates VMA cleared. The target is dying. |
| 2969 | */ |
| 2970 | return_error_param = PTR_ERR(t->buffer); |
| 2971 | return_error = return_error_param == -ESRCH ? |
| 2972 | BR_DEAD_REPLY : BR_FAILED_REPLY; |
| 2973 | return_error_line = __LINE__; |
| 2974 | t->buffer = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2975 | goto err_binder_alloc_buf_failed; |
| 2976 | } |
| 2977 | t->buffer->allow_user_free = 0; |
| 2978 | t->buffer->debug_id = t->debug_id; |
| 2979 | t->buffer->transaction = t; |
| 2980 | t->buffer->target_node = target_node; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2981 | trace_binder_transaction_alloc_buf(t->buffer); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2982 | off_start = (binder_size_t *)(t->buffer->data + |
| 2983 | ALIGN(tr->data_size, sizeof(void *))); |
| 2984 | offp = off_start; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2985 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2986 | if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t) |
| 2987 | tr->data.ptr.buffer, tr->data_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2988 | binder_user_error("%d:%d got transaction with invalid data ptr\n", |
| 2989 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2990 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2991 | return_error_param = -EFAULT; |
| 2992 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2993 | goto err_copy_data_failed; |
| 2994 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2995 | if (copy_from_user(offp, (const void __user *)(uintptr_t) |
| 2996 | tr->data.ptr.offsets, tr->offsets_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2997 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 2998 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2999 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3000 | return_error_param = -EFAULT; |
| 3001 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3002 | goto err_copy_data_failed; |
| 3003 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3004 | if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) { |
| 3005 | binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n", |
| 3006 | proc->pid, thread->pid, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3007 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3008 | return_error_param = -EINVAL; |
| 3009 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3010 | goto err_bad_offset; |
| 3011 | } |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3012 | if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) { |
| 3013 | binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n", |
| 3014 | proc->pid, thread->pid, |
| 3015 | (u64)extra_buffers_size); |
| 3016 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3017 | return_error_param = -EINVAL; |
| 3018 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3019 | goto err_bad_offset; |
| 3020 | } |
| 3021 | off_end = (void *)off_start + tr->offsets_size; |
| 3022 | sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *))); |
| 3023 | sg_buf_end = sg_bufp + extra_buffers_size; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3024 | off_min = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3025 | for (; offp < off_end; offp++) { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3026 | struct binder_object_header *hdr; |
| 3027 | size_t object_size = binder_validate_object(t->buffer, *offp); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3028 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3029 | if (object_size == 0 || *offp < off_min) { |
| 3030 | binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n", |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3031 | proc->pid, thread->pid, (u64)*offp, |
| 3032 | (u64)off_min, |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3033 | (u64)t->buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3034 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3035 | return_error_param = -EINVAL; |
| 3036 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3037 | goto err_bad_offset; |
| 3038 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3039 | |
| 3040 | hdr = (struct binder_object_header *)(t->buffer->data + *offp); |
| 3041 | off_min = *offp + object_size; |
| 3042 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3043 | case BINDER_TYPE_BINDER: |
| 3044 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3045 | struct flat_binder_object *fp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3046 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3047 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3048 | ret = binder_translate_binder(fp, t, thread); |
| 3049 | if (ret < 0) { |
Christian Engelmayer | 7d42043 | 2014-05-07 21:44:53 +0200 | [diff] [blame] | 3050 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3051 | return_error_param = ret; |
| 3052 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3053 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3054 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3055 | } break; |
| 3056 | case BINDER_TYPE_HANDLE: |
| 3057 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3058 | struct flat_binder_object *fp; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 3059 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3060 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3061 | ret = binder_translate_handle(fp, t, thread); |
| 3062 | if (ret < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3063 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3064 | return_error_param = ret; |
| 3065 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3066 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3067 | } |
| 3068 | } break; |
| 3069 | |
| 3070 | case BINDER_TYPE_FD: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3071 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 3072 | int ret = binder_translate_fd(&fp->fd, t, thread, |
| 3073 | in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3074 | |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 3075 | if (ret < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3076 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 3077 | return_error_param = ret; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3078 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3079 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3080 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3081 | fp->pad_binder = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3082 | } break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3083 | case BINDER_TYPE_FDA: { |
| 3084 | struct binder_fd_array_object *fda = |
| 3085 | to_binder_fd_array_object(hdr); |
| 3086 | struct binder_buffer_object *parent = |
| 3087 | binder_validate_ptr(t->buffer, fda->parent, |
| 3088 | off_start, |
| 3089 | offp - off_start); |
| 3090 | if (!parent) { |
| 3091 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 3092 | proc->pid, thread->pid); |
| 3093 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3094 | return_error_param = -EINVAL; |
| 3095 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3096 | goto err_bad_parent; |
| 3097 | } |
| 3098 | if (!binder_validate_fixup(t->buffer, off_start, |
| 3099 | parent, fda->parent_offset, |
| 3100 | last_fixup_obj, |
| 3101 | last_fixup_min_off)) { |
| 3102 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 3103 | proc->pid, thread->pid); |
| 3104 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3105 | return_error_param = -EINVAL; |
| 3106 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3107 | goto err_bad_parent; |
| 3108 | } |
| 3109 | ret = binder_translate_fd_array(fda, parent, t, thread, |
| 3110 | in_reply_to); |
| 3111 | if (ret < 0) { |
| 3112 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3113 | return_error_param = ret; |
| 3114 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3115 | goto err_translate_failed; |
| 3116 | } |
| 3117 | last_fixup_obj = parent; |
| 3118 | last_fixup_min_off = |
| 3119 | fda->parent_offset + sizeof(u32) * fda->num_fds; |
| 3120 | } break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3121 | case BINDER_TYPE_PTR: { |
| 3122 | struct binder_buffer_object *bp = |
| 3123 | to_binder_buffer_object(hdr); |
| 3124 | size_t buf_left = sg_buf_end - sg_bufp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3125 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3126 | if (bp->length > buf_left) { |
| 3127 | binder_user_error("%d:%d got transaction with too large buffer\n", |
| 3128 | proc->pid, thread->pid); |
| 3129 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3130 | return_error_param = -EINVAL; |
| 3131 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3132 | goto err_bad_offset; |
| 3133 | } |
| 3134 | if (copy_from_user(sg_bufp, |
| 3135 | (const void __user *)(uintptr_t) |
| 3136 | bp->buffer, bp->length)) { |
| 3137 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3138 | proc->pid, thread->pid); |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3139 | return_error_param = -EFAULT; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3140 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3141 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3142 | goto err_copy_data_failed; |
| 3143 | } |
| 3144 | /* Fixup buffer pointer to target proc address space */ |
| 3145 | bp->buffer = (uintptr_t)sg_bufp + |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 3146 | binder_alloc_get_user_buffer_offset( |
| 3147 | &target_proc->alloc); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3148 | sg_bufp += ALIGN(bp->length, sizeof(u64)); |
| 3149 | |
| 3150 | ret = binder_fixup_parent(t, thread, bp, off_start, |
| 3151 | offp - off_start, |
| 3152 | last_fixup_obj, |
| 3153 | last_fixup_min_off); |
| 3154 | if (ret < 0) { |
| 3155 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3156 | return_error_param = ret; |
| 3157 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3158 | goto err_translate_failed; |
| 3159 | } |
| 3160 | last_fixup_obj = bp; |
| 3161 | last_fixup_min_off = 0; |
| 3162 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3163 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 3164 | binder_user_error("%d:%d got transaction with invalid object type, %x\n", |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3165 | proc->pid, thread->pid, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3166 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3167 | return_error_param = -EINVAL; |
| 3168 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3169 | goto err_bad_object_type; |
| 3170 | } |
| 3171 | } |
Todd Kjos | ccae6f6 | 2017-06-29 12:01:48 -0700 | [diff] [blame] | 3172 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3173 | t->work.type = BINDER_WORK_TRANSACTION; |
Todd Kjos | ccae6f6 | 2017-06-29 12:01:48 -0700 | [diff] [blame] | 3174 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3175 | if (reply) { |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3176 | binder_enqueue_thread_work(thread, tcomplete); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3177 | binder_inner_proc_lock(target_proc); |
| 3178 | if (target_thread->is_dead) { |
| 3179 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3180 | goto err_dead_proc_or_thread; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3181 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3182 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3183 | binder_pop_transaction_ilocked(target_thread, in_reply_to); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3184 | binder_enqueue_thread_work_ilocked(target_thread, &t->work); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3185 | binder_inner_proc_unlock(target_proc); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3186 | wake_up_interruptible_sync(&target_thread->wait); |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 3187 | binder_free_transaction(in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3188 | } else if (!(t->flags & TF_ONE_WAY)) { |
| 3189 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3190 | binder_inner_proc_lock(proc); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3191 | /* |
| 3192 | * Defer the TRANSACTION_COMPLETE, so we don't return to |
| 3193 | * userspace immediately; this allows the target process to |
| 3194 | * immediately start processing this transaction, reducing |
| 3195 | * latency. We will then return the TRANSACTION_COMPLETE when |
| 3196 | * the target replies (or there is an error). |
| 3197 | */ |
| 3198 | binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3199 | t->need_reply = 1; |
| 3200 | t->from_parent = thread->transaction_stack; |
| 3201 | thread->transaction_stack = t; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3202 | binder_inner_proc_unlock(proc); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3203 | if (!binder_proc_transaction(t, target_proc, target_thread)) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3204 | binder_inner_proc_lock(proc); |
| 3205 | binder_pop_transaction_ilocked(thread, t); |
| 3206 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3207 | goto err_dead_proc_or_thread; |
| 3208 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3209 | } else { |
| 3210 | BUG_ON(target_node == NULL); |
| 3211 | BUG_ON(t->buffer->async_transaction != 1); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3212 | binder_enqueue_thread_work(thread, tcomplete); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3213 | if (!binder_proc_transaction(t, target_proc, NULL)) |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3214 | goto err_dead_proc_or_thread; |
Riley Andrews | 00b40d6 | 2017-06-29 12:01:37 -0700 | [diff] [blame] | 3215 | } |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3216 | if (target_thread) |
| 3217 | binder_thread_dec_tmpref(target_thread); |
| 3218 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3219 | if (target_node) |
| 3220 | binder_dec_node_tmpref(target_node); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 3221 | /* |
| 3222 | * write barrier to synchronize with initialization |
| 3223 | * of log entry |
| 3224 | */ |
| 3225 | smp_wmb(); |
| 3226 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3227 | return; |
| 3228 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3229 | err_dead_proc_or_thread: |
| 3230 | return_error = BR_DEAD_REPLY; |
| 3231 | return_error_line = __LINE__; |
Xu YiPing | d53bebd | 2017-09-05 10:21:52 -0700 | [diff] [blame] | 3232 | binder_dequeue_work(proc, tcomplete); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3233 | err_translate_failed: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3234 | err_bad_object_type: |
| 3235 | err_bad_offset: |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3236 | err_bad_parent: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3237 | err_copy_data_failed: |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 3238 | binder_free_txn_fixups(t); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3239 | trace_binder_transaction_failed_buffer_release(t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3240 | binder_transaction_buffer_release(target_proc, t->buffer, offp); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3241 | if (target_node) |
| 3242 | binder_dec_node_tmpref(target_node); |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3243 | target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3244 | t->buffer->transaction = NULL; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 3245 | binder_alloc_free_buf(&target_proc->alloc, t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3246 | err_binder_alloc_buf_failed: |
| 3247 | kfree(tcomplete); |
| 3248 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3249 | err_alloc_tcomplete_failed: |
| 3250 | kfree(t); |
| 3251 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 3252 | err_alloc_t_failed: |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame] | 3253 | err_bad_todo_list: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3254 | err_bad_call_stack: |
| 3255 | err_empty_call_stack: |
| 3256 | err_dead_binder: |
| 3257 | err_invalid_target_handle: |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3258 | if (target_thread) |
| 3259 | binder_thread_dec_tmpref(target_thread); |
| 3260 | if (target_proc) |
| 3261 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3262 | if (target_node) { |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3263 | binder_dec_node(target_node, 1, 0); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3264 | binder_dec_node_tmpref(target_node); |
| 3265 | } |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3266 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3267 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3268 | "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n", |
| 3269 | proc->pid, thread->pid, return_error, return_error_param, |
| 3270 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3271 | return_error_line); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3272 | |
| 3273 | { |
| 3274 | struct binder_transaction_log_entry *fe; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3275 | |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3276 | e->return_error = return_error; |
| 3277 | e->return_error_param = return_error_param; |
| 3278 | e->return_error_line = return_error_line; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3279 | fe = binder_transaction_log_add(&binder_transaction_log_failed); |
| 3280 | *fe = *e; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 3281 | /* |
| 3282 | * write barrier to synchronize with initialization |
| 3283 | * of log entry |
| 3284 | */ |
| 3285 | smp_wmb(); |
| 3286 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
| 3287 | WRITE_ONCE(fe->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3288 | } |
| 3289 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3290 | BUG_ON(thread->return_error.cmd != BR_OK); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3291 | if (in_reply_to) { |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3292 | thread->return_error.cmd = BR_TRANSACTION_COMPLETE; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3293 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3294 | binder_send_failed_reply(in_reply_to, return_error); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3295 | } else { |
| 3296 | thread->return_error.cmd = return_error; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3297 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3298 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3299 | } |
| 3300 | |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 3301 | /** |
| 3302 | * binder_free_buf() - free the specified buffer |
| 3303 | * @proc: binder proc that owns buffer |
| 3304 | * @buffer: buffer to be freed |
| 3305 | * |
| 3306 | * If buffer for an async transaction, enqueue the next async |
| 3307 | * transaction from the node. |
| 3308 | * |
| 3309 | * Cleanup buffer and free it. |
| 3310 | */ |
Wei Yongjun | f4608ce | 2018-09-25 14:30:36 +0000 | [diff] [blame] | 3311 | static void |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 3312 | binder_free_buf(struct binder_proc *proc, struct binder_buffer *buffer) |
| 3313 | { |
| 3314 | if (buffer->transaction) { |
| 3315 | buffer->transaction->buffer = NULL; |
| 3316 | buffer->transaction = NULL; |
| 3317 | } |
| 3318 | if (buffer->async_transaction && buffer->target_node) { |
| 3319 | struct binder_node *buf_node; |
| 3320 | struct binder_work *w; |
| 3321 | |
| 3322 | buf_node = buffer->target_node; |
| 3323 | binder_node_inner_lock(buf_node); |
| 3324 | BUG_ON(!buf_node->has_async_transaction); |
| 3325 | BUG_ON(buf_node->proc != proc); |
| 3326 | w = binder_dequeue_work_head_ilocked( |
| 3327 | &buf_node->async_todo); |
| 3328 | if (!w) { |
| 3329 | buf_node->has_async_transaction = false; |
| 3330 | } else { |
| 3331 | binder_enqueue_work_ilocked( |
| 3332 | w, &proc->todo); |
| 3333 | binder_wakeup_proc_ilocked(proc); |
| 3334 | } |
| 3335 | binder_node_inner_unlock(buf_node); |
| 3336 | } |
| 3337 | trace_binder_transaction_buffer_release(buffer); |
| 3338 | binder_transaction_buffer_release(proc, buffer, NULL); |
| 3339 | binder_alloc_free_buf(&proc->alloc, buffer); |
| 3340 | } |
| 3341 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3342 | static int binder_thread_write(struct binder_proc *proc, |
| 3343 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3344 | binder_uintptr_t binder_buffer, size_t size, |
| 3345 | binder_size_t *consumed) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3346 | { |
| 3347 | uint32_t cmd; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 3348 | struct binder_context *context = proc->context; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3349 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3350 | void __user *ptr = buffer + *consumed; |
| 3351 | void __user *end = buffer + size; |
| 3352 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3353 | while (ptr < end && thread->return_error.cmd == BR_OK) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3354 | int ret; |
| 3355 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3356 | if (get_user(cmd, (uint32_t __user *)ptr)) |
| 3357 | return -EFAULT; |
| 3358 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3359 | trace_binder_command(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3360 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 3361 | atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]); |
| 3362 | atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]); |
| 3363 | atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3364 | } |
| 3365 | switch (cmd) { |
| 3366 | case BC_INCREFS: |
| 3367 | case BC_ACQUIRE: |
| 3368 | case BC_RELEASE: |
| 3369 | case BC_DECREFS: { |
| 3370 | uint32_t target; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3371 | const char *debug_string; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3372 | bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE; |
| 3373 | bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE; |
| 3374 | struct binder_ref_data rdata; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3375 | |
| 3376 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3377 | return -EFAULT; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3378 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3379 | ptr += sizeof(uint32_t); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3380 | ret = -1; |
| 3381 | if (increment && !target) { |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3382 | struct binder_node *ctx_mgr_node; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3383 | mutex_lock(&context->context_mgr_node_lock); |
| 3384 | ctx_mgr_node = context->binder_context_mgr_node; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3385 | if (ctx_mgr_node) |
| 3386 | ret = binder_inc_ref_for_node( |
| 3387 | proc, ctx_mgr_node, |
| 3388 | strong, NULL, &rdata); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3389 | mutex_unlock(&context->context_mgr_node_lock); |
| 3390 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3391 | if (ret) |
| 3392 | ret = binder_update_ref_for_handle( |
| 3393 | proc, target, increment, strong, |
| 3394 | &rdata); |
| 3395 | if (!ret && rdata.desc != target) { |
| 3396 | binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n", |
| 3397 | proc->pid, thread->pid, |
| 3398 | target, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3399 | } |
| 3400 | switch (cmd) { |
| 3401 | case BC_INCREFS: |
| 3402 | debug_string = "IncRefs"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3403 | break; |
| 3404 | case BC_ACQUIRE: |
| 3405 | debug_string = "Acquire"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3406 | break; |
| 3407 | case BC_RELEASE: |
| 3408 | debug_string = "Release"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3409 | break; |
| 3410 | case BC_DECREFS: |
| 3411 | default: |
| 3412 | debug_string = "DecRefs"; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3413 | break; |
| 3414 | } |
| 3415 | if (ret) { |
| 3416 | binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n", |
| 3417 | proc->pid, thread->pid, debug_string, |
| 3418 | strong, target, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3419 | break; |
| 3420 | } |
| 3421 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3422 | "%d:%d %s ref %d desc %d s %d w %d\n", |
| 3423 | proc->pid, thread->pid, debug_string, |
| 3424 | rdata.debug_id, rdata.desc, rdata.strong, |
| 3425 | rdata.weak); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3426 | break; |
| 3427 | } |
| 3428 | case BC_INCREFS_DONE: |
| 3429 | case BC_ACQUIRE_DONE: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3430 | binder_uintptr_t node_ptr; |
| 3431 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3432 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3433 | bool free_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3434 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3435 | if (get_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3436 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3437 | ptr += sizeof(binder_uintptr_t); |
| 3438 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3439 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3440 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3441 | node = binder_get_node(proc, node_ptr); |
| 3442 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3443 | binder_user_error("%d:%d %s u%016llx no match\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3444 | proc->pid, thread->pid, |
| 3445 | cmd == BC_INCREFS_DONE ? |
| 3446 | "BC_INCREFS_DONE" : |
| 3447 | "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3448 | (u64)node_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3449 | break; |
| 3450 | } |
| 3451 | if (cookie != node->cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3452 | 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] | 3453 | proc->pid, thread->pid, |
| 3454 | cmd == BC_INCREFS_DONE ? |
| 3455 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3456 | (u64)node_ptr, node->debug_id, |
| 3457 | (u64)cookie, (u64)node->cookie); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3458 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3459 | break; |
| 3460 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3461 | binder_node_inner_lock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3462 | if (cmd == BC_ACQUIRE_DONE) { |
| 3463 | if (node->pending_strong_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3464 | 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] | 3465 | proc->pid, thread->pid, |
| 3466 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3467 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3468 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3469 | break; |
| 3470 | } |
| 3471 | node->pending_strong_ref = 0; |
| 3472 | } else { |
| 3473 | if (node->pending_weak_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3474 | 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] | 3475 | proc->pid, thread->pid, |
| 3476 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3477 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3478 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3479 | break; |
| 3480 | } |
| 3481 | node->pending_weak_ref = 0; |
| 3482 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3483 | free_node = binder_dec_node_nilocked(node, |
| 3484 | cmd == BC_ACQUIRE_DONE, 0); |
| 3485 | WARN_ON(free_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3486 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3487 | "%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] | 3488 | proc->pid, thread->pid, |
| 3489 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3490 | node->debug_id, node->local_strong_refs, |
| 3491 | node->local_weak_refs, node->tmp_refs); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3492 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3493 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3494 | break; |
| 3495 | } |
| 3496 | case BC_ATTEMPT_ACQUIRE: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3497 | pr_err("BC_ATTEMPT_ACQUIRE not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3498 | return -EINVAL; |
| 3499 | case BC_ACQUIRE_RESULT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3500 | pr_err("BC_ACQUIRE_RESULT not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3501 | return -EINVAL; |
| 3502 | |
| 3503 | case BC_FREE_BUFFER: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3504 | binder_uintptr_t data_ptr; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3505 | struct binder_buffer *buffer; |
| 3506 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3507 | if (get_user(data_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3508 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3509 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3510 | |
Todd Kjos | 53d311cf | 2017-06-29 12:01:51 -0700 | [diff] [blame] | 3511 | buffer = binder_alloc_prepare_to_free(&proc->alloc, |
| 3512 | data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3513 | if (buffer == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3514 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n", |
| 3515 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3516 | break; |
| 3517 | } |
| 3518 | if (!buffer->allow_user_free) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3519 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n", |
| 3520 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3521 | break; |
| 3522 | } |
| 3523 | binder_debug(BINDER_DEBUG_FREE_BUFFER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3524 | "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n", |
| 3525 | proc->pid, thread->pid, (u64)data_ptr, |
| 3526 | buffer->debug_id, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3527 | buffer->transaction ? "active" : "finished"); |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 3528 | binder_free_buf(proc, buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3529 | break; |
| 3530 | } |
| 3531 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3532 | case BC_TRANSACTION_SG: |
| 3533 | case BC_REPLY_SG: { |
| 3534 | struct binder_transaction_data_sg tr; |
| 3535 | |
| 3536 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3537 | return -EFAULT; |
| 3538 | ptr += sizeof(tr); |
| 3539 | binder_transaction(proc, thread, &tr.transaction_data, |
| 3540 | cmd == BC_REPLY_SG, tr.buffers_size); |
| 3541 | break; |
| 3542 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3543 | case BC_TRANSACTION: |
| 3544 | case BC_REPLY: { |
| 3545 | struct binder_transaction_data tr; |
| 3546 | |
| 3547 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3548 | return -EFAULT; |
| 3549 | ptr += sizeof(tr); |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 3550 | binder_transaction(proc, thread, &tr, |
| 3551 | cmd == BC_REPLY, 0); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3552 | break; |
| 3553 | } |
| 3554 | |
| 3555 | case BC_REGISTER_LOOPER: |
| 3556 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3557 | "%d:%d BC_REGISTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3558 | proc->pid, thread->pid); |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3559 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3560 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { |
| 3561 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3562 | 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] | 3563 | proc->pid, thread->pid); |
| 3564 | } else if (proc->requested_threads == 0) { |
| 3565 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3566 | 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] | 3567 | proc->pid, thread->pid); |
| 3568 | } else { |
| 3569 | proc->requested_threads--; |
| 3570 | proc->requested_threads_started++; |
| 3571 | } |
| 3572 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3573 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3574 | break; |
| 3575 | case BC_ENTER_LOOPER: |
| 3576 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3577 | "%d:%d BC_ENTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3578 | proc->pid, thread->pid); |
| 3579 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { |
| 3580 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3581 | 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] | 3582 | proc->pid, thread->pid); |
| 3583 | } |
| 3584 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; |
| 3585 | break; |
| 3586 | case BC_EXIT_LOOPER: |
| 3587 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3588 | "%d:%d BC_EXIT_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3589 | proc->pid, thread->pid); |
| 3590 | thread->looper |= BINDER_LOOPER_STATE_EXITED; |
| 3591 | break; |
| 3592 | |
| 3593 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 3594 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 3595 | uint32_t target; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3596 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3597 | struct binder_ref *ref; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3598 | struct binder_ref_death *death = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3599 | |
| 3600 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3601 | return -EFAULT; |
| 3602 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3603 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3604 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3605 | ptr += sizeof(binder_uintptr_t); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3606 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3607 | /* |
| 3608 | * Allocate memory for death notification |
| 3609 | * before taking lock |
| 3610 | */ |
| 3611 | death = kzalloc(sizeof(*death), GFP_KERNEL); |
| 3612 | if (death == NULL) { |
| 3613 | WARN_ON(thread->return_error.cmd != |
| 3614 | BR_OK); |
| 3615 | thread->return_error.cmd = BR_ERROR; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3616 | binder_enqueue_thread_work( |
| 3617 | thread, |
| 3618 | &thread->return_error.work); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3619 | binder_debug( |
| 3620 | BINDER_DEBUG_FAILED_TRANSACTION, |
| 3621 | "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n", |
| 3622 | proc->pid, thread->pid); |
| 3623 | break; |
| 3624 | } |
| 3625 | } |
| 3626 | binder_proc_lock(proc); |
| 3627 | ref = binder_get_ref_olocked(proc, target, false); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3628 | if (ref == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3629 | binder_user_error("%d:%d %s invalid ref %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3630 | proc->pid, thread->pid, |
| 3631 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3632 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3633 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 3634 | target); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3635 | binder_proc_unlock(proc); |
| 3636 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3637 | break; |
| 3638 | } |
| 3639 | |
| 3640 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3641 | "%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] | 3642 | proc->pid, thread->pid, |
| 3643 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3644 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3645 | "BC_CLEAR_DEATH_NOTIFICATION", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3646 | (u64)cookie, ref->data.debug_id, |
| 3647 | ref->data.desc, ref->data.strong, |
| 3648 | ref->data.weak, ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3649 | |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3650 | binder_node_lock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3651 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3652 | if (ref->death) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3653 | 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] | 3654 | proc->pid, thread->pid); |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3655 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3656 | binder_proc_unlock(proc); |
| 3657 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3658 | break; |
| 3659 | } |
| 3660 | binder_stats_created(BINDER_STAT_DEATH); |
| 3661 | INIT_LIST_HEAD(&death->work.entry); |
| 3662 | death->cookie = cookie; |
| 3663 | ref->death = death; |
| 3664 | if (ref->node->proc == NULL) { |
| 3665 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
Martijn Coenen | bb74562 | 2017-08-31 10:04:28 +0200 | [diff] [blame] | 3666 | |
| 3667 | binder_inner_proc_lock(proc); |
| 3668 | binder_enqueue_work_ilocked( |
| 3669 | &ref->death->work, &proc->todo); |
| 3670 | binder_wakeup_proc_ilocked(proc); |
| 3671 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3672 | } |
| 3673 | } else { |
| 3674 | if (ref->death == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3675 | 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] | 3676 | proc->pid, thread->pid); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3677 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3678 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3679 | break; |
| 3680 | } |
| 3681 | death = ref->death; |
| 3682 | if (death->cookie != cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3683 | 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] | 3684 | proc->pid, thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3685 | (u64)death->cookie, |
| 3686 | (u64)cookie); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3687 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3688 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3689 | break; |
| 3690 | } |
| 3691 | ref->death = NULL; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3692 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3693 | if (list_empty(&death->work.entry)) { |
| 3694 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3695 | if (thread->looper & |
| 3696 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3697 | BINDER_LOOPER_STATE_ENTERED)) |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3698 | binder_enqueue_thread_work_ilocked( |
| 3699 | thread, |
| 3700 | &death->work); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3701 | else { |
| 3702 | binder_enqueue_work_ilocked( |
| 3703 | &death->work, |
| 3704 | &proc->todo); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 3705 | binder_wakeup_proc_ilocked( |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3706 | proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3707 | } |
| 3708 | } else { |
| 3709 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); |
| 3710 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; |
| 3711 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3712 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3713 | } |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3714 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3715 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3716 | } break; |
| 3717 | case BC_DEAD_BINDER_DONE: { |
| 3718 | struct binder_work *w; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3719 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3720 | struct binder_ref_death *death = NULL; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3721 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3722 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3723 | return -EFAULT; |
| 3724 | |
Lisa Du | 7a64cd8 | 2016-02-17 09:32:52 +0800 | [diff] [blame] | 3725 | ptr += sizeof(cookie); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3726 | binder_inner_proc_lock(proc); |
| 3727 | list_for_each_entry(w, &proc->delivered_death, |
| 3728 | entry) { |
| 3729 | struct binder_ref_death *tmp_death = |
| 3730 | container_of(w, |
| 3731 | struct binder_ref_death, |
| 3732 | work); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3733 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3734 | if (tmp_death->cookie == cookie) { |
| 3735 | death = tmp_death; |
| 3736 | break; |
| 3737 | } |
| 3738 | } |
| 3739 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Todd Kjos | 8ca86f1 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 3740 | "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3741 | proc->pid, thread->pid, (u64)cookie, |
| 3742 | death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3743 | if (death == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3744 | binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n", |
| 3745 | proc->pid, thread->pid, (u64)cookie); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3746 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3747 | break; |
| 3748 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3749 | binder_dequeue_work_ilocked(&death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3750 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { |
| 3751 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3752 | if (thread->looper & |
| 3753 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3754 | BINDER_LOOPER_STATE_ENTERED)) |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3755 | binder_enqueue_thread_work_ilocked( |
| 3756 | thread, &death->work); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3757 | else { |
| 3758 | binder_enqueue_work_ilocked( |
| 3759 | &death->work, |
| 3760 | &proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3761 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3762 | } |
| 3763 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3764 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3765 | } break; |
| 3766 | |
| 3767 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3768 | pr_err("%d:%d unknown command %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3769 | proc->pid, thread->pid, cmd); |
| 3770 | return -EINVAL; |
| 3771 | } |
| 3772 | *consumed = ptr - buffer; |
| 3773 | } |
| 3774 | return 0; |
| 3775 | } |
| 3776 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3777 | static void binder_stat_br(struct binder_proc *proc, |
| 3778 | struct binder_thread *thread, uint32_t cmd) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3779 | { |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3780 | trace_binder_return(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3781 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 3782 | atomic_inc(&binder_stats.br[_IOC_NR(cmd)]); |
| 3783 | atomic_inc(&proc->stats.br[_IOC_NR(cmd)]); |
| 3784 | atomic_inc(&thread->stats.br[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3785 | } |
| 3786 | } |
| 3787 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3788 | static int binder_put_node_cmd(struct binder_proc *proc, |
| 3789 | struct binder_thread *thread, |
| 3790 | void __user **ptrp, |
| 3791 | binder_uintptr_t node_ptr, |
| 3792 | binder_uintptr_t node_cookie, |
| 3793 | int node_debug_id, |
| 3794 | uint32_t cmd, const char *cmd_name) |
| 3795 | { |
| 3796 | void __user *ptr = *ptrp; |
| 3797 | |
| 3798 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 3799 | return -EFAULT; |
| 3800 | ptr += sizeof(uint32_t); |
| 3801 | |
| 3802 | if (put_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
| 3803 | return -EFAULT; |
| 3804 | ptr += sizeof(binder_uintptr_t); |
| 3805 | |
| 3806 | if (put_user(node_cookie, (binder_uintptr_t __user *)ptr)) |
| 3807 | return -EFAULT; |
| 3808 | ptr += sizeof(binder_uintptr_t); |
| 3809 | |
| 3810 | binder_stat_br(proc, thread, cmd); |
| 3811 | binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n", |
| 3812 | proc->pid, thread->pid, cmd_name, node_debug_id, |
| 3813 | (u64)node_ptr, (u64)node_cookie); |
| 3814 | |
| 3815 | *ptrp = ptr; |
| 3816 | return 0; |
| 3817 | } |
| 3818 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 3819 | static int binder_wait_for_work(struct binder_thread *thread, |
| 3820 | bool do_proc_work) |
| 3821 | { |
| 3822 | DEFINE_WAIT(wait); |
| 3823 | struct binder_proc *proc = thread->proc; |
| 3824 | int ret = 0; |
| 3825 | |
| 3826 | freezer_do_not_count(); |
| 3827 | binder_inner_proc_lock(proc); |
| 3828 | for (;;) { |
| 3829 | prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE); |
| 3830 | if (binder_has_work_ilocked(thread, do_proc_work)) |
| 3831 | break; |
| 3832 | if (do_proc_work) |
| 3833 | list_add(&thread->waiting_thread_node, |
| 3834 | &proc->waiting_threads); |
| 3835 | binder_inner_proc_unlock(proc); |
| 3836 | schedule(); |
| 3837 | binder_inner_proc_lock(proc); |
| 3838 | list_del_init(&thread->waiting_thread_node); |
| 3839 | if (signal_pending(current)) { |
| 3840 | ret = -ERESTARTSYS; |
| 3841 | break; |
| 3842 | } |
| 3843 | } |
| 3844 | finish_wait(&thread->wait, &wait); |
| 3845 | binder_inner_proc_unlock(proc); |
| 3846 | freezer_count(); |
| 3847 | |
| 3848 | return ret; |
| 3849 | } |
| 3850 | |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 3851 | /** |
| 3852 | * binder_apply_fd_fixups() - finish fd translation |
| 3853 | * @t: binder transaction with list of fd fixups |
| 3854 | * |
| 3855 | * Now that we are in the context of the transaction target |
| 3856 | * process, we can allocate and install fds. Process the |
| 3857 | * list of fds to translate and fixup the buffer with the |
| 3858 | * new fds. |
| 3859 | * |
| 3860 | * If we fail to allocate an fd, then free the resources by |
| 3861 | * fput'ing files that have not been processed and ksys_close'ing |
| 3862 | * any fds that have already been allocated. |
| 3863 | */ |
| 3864 | static int binder_apply_fd_fixups(struct binder_transaction *t) |
| 3865 | { |
| 3866 | struct binder_txn_fd_fixup *fixup, *tmp; |
| 3867 | int ret = 0; |
| 3868 | |
| 3869 | list_for_each_entry(fixup, &t->fd_fixups, fixup_entry) { |
| 3870 | int fd = get_unused_fd_flags(O_CLOEXEC); |
| 3871 | u32 *fdp; |
| 3872 | |
| 3873 | if (fd < 0) { |
| 3874 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 3875 | "failed fd fixup txn %d fd %d\n", |
| 3876 | t->debug_id, fd); |
| 3877 | ret = -ENOMEM; |
| 3878 | break; |
| 3879 | } |
| 3880 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 3881 | "fd fixup txn %d fd %d\n", |
| 3882 | t->debug_id, fd); |
| 3883 | trace_binder_transaction_fd_recv(t, fd, fixup->offset); |
| 3884 | fd_install(fd, fixup->file); |
| 3885 | fixup->file = NULL; |
| 3886 | fdp = (u32 *)(t->buffer->data + fixup->offset); |
| 3887 | /* |
| 3888 | * This store can cause problems for CPUs with a |
| 3889 | * VIVT cache (eg ARMv5) since the cache cannot |
| 3890 | * detect virtual aliases to the same physical cacheline. |
| 3891 | * To support VIVT, this address and the user-space VA |
| 3892 | * would both need to be flushed. Since this kernel |
| 3893 | * VA is not constructed via page_to_virt(), we can't |
| 3894 | * use flush_dcache_page() on it, so we'd have to use |
| 3895 | * an internal function. If devices with VIVT ever |
| 3896 | * need to run Android, we'll either need to go back |
| 3897 | * to patching the translated fd from the sender side |
| 3898 | * (using the non-standard kernel functions), or rework |
| 3899 | * how the kernel uses the buffer to use page_to_virt() |
| 3900 | * addresses instead of allocating in our own vm area. |
| 3901 | * |
| 3902 | * For now, we disable compilation if CONFIG_CPU_CACHE_VIVT. |
| 3903 | */ |
| 3904 | *fdp = fd; |
| 3905 | } |
| 3906 | list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) { |
| 3907 | if (fixup->file) { |
| 3908 | fput(fixup->file); |
| 3909 | } else if (ret) { |
| 3910 | u32 *fdp = (u32 *)(t->buffer->data + fixup->offset); |
| 3911 | |
| 3912 | ksys_close(*fdp); |
| 3913 | } |
| 3914 | list_del(&fixup->fixup_entry); |
| 3915 | kfree(fixup); |
| 3916 | } |
| 3917 | |
| 3918 | return ret; |
| 3919 | } |
| 3920 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3921 | static int binder_thread_read(struct binder_proc *proc, |
| 3922 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3923 | binder_uintptr_t binder_buffer, size_t size, |
| 3924 | binder_size_t *consumed, int non_block) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3925 | { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3926 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3927 | void __user *ptr = buffer + *consumed; |
| 3928 | void __user *end = buffer + size; |
| 3929 | |
| 3930 | int ret = 0; |
| 3931 | int wait_for_proc_work; |
| 3932 | |
| 3933 | if (*consumed == 0) { |
| 3934 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) |
| 3935 | return -EFAULT; |
| 3936 | ptr += sizeof(uint32_t); |
| 3937 | } |
| 3938 | |
| 3939 | retry: |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3940 | binder_inner_proc_lock(proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 3941 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3942 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3943 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3944 | thread->looper |= BINDER_LOOPER_STATE_WAITING; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3945 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3946 | trace_binder_wait_for_work(wait_for_proc_work, |
| 3947 | !!thread->transaction_stack, |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3948 | !binder_worklist_empty(proc, &thread->todo)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3949 | if (wait_for_proc_work) { |
| 3950 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 3951 | BINDER_LOOPER_STATE_ENTERED))) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3952 | 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] | 3953 | proc->pid, thread->pid, thread->looper); |
| 3954 | wait_event_interruptible(binder_user_error_wait, |
| 3955 | binder_stop_on_user_error < 2); |
| 3956 | } |
| 3957 | binder_set_nice(proc->default_priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3958 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3959 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 3960 | if (non_block) { |
| 3961 | if (!binder_has_work(thread, wait_for_proc_work)) |
| 3962 | ret = -EAGAIN; |
| 3963 | } else { |
| 3964 | ret = binder_wait_for_work(thread, wait_for_proc_work); |
| 3965 | } |
| 3966 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3967 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; |
| 3968 | |
| 3969 | if (ret) |
| 3970 | return ret; |
| 3971 | |
| 3972 | while (1) { |
| 3973 | uint32_t cmd; |
| 3974 | struct binder_transaction_data tr; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3975 | struct binder_work *w = NULL; |
| 3976 | struct list_head *list = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3977 | struct binder_transaction *t = NULL; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3978 | struct binder_thread *t_from; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3979 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3980 | binder_inner_proc_lock(proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3981 | if (!binder_worklist_empty_ilocked(&thread->todo)) |
| 3982 | list = &thread->todo; |
| 3983 | else if (!binder_worklist_empty_ilocked(&proc->todo) && |
| 3984 | wait_for_proc_work) |
| 3985 | list = &proc->todo; |
| 3986 | else { |
| 3987 | binder_inner_proc_unlock(proc); |
| 3988 | |
Dmitry Voytik | 395262a | 2014-09-08 18:16:34 +0400 | [diff] [blame] | 3989 | /* no data added */ |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 3990 | if (ptr - buffer == 4 && !thread->looper_need_return) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3991 | goto retry; |
| 3992 | break; |
| 3993 | } |
| 3994 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3995 | if (end - ptr < sizeof(tr) + 4) { |
| 3996 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3997 | break; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3998 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3999 | w = binder_dequeue_work_head_ilocked(list); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 4000 | if (binder_worklist_empty_ilocked(&thread->todo)) |
| 4001 | thread->process_todo = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4002 | |
| 4003 | switch (w->type) { |
| 4004 | case BINDER_WORK_TRANSACTION: { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4005 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4006 | t = container_of(w, struct binder_transaction, work); |
| 4007 | } break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4008 | case BINDER_WORK_RETURN_ERROR: { |
| 4009 | struct binder_error *e = container_of( |
| 4010 | w, struct binder_error, work); |
| 4011 | |
| 4012 | WARN_ON(e->cmd == BR_OK); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4013 | binder_inner_proc_unlock(proc); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4014 | if (put_user(e->cmd, (uint32_t __user *)ptr)) |
| 4015 | return -EFAULT; |
宋金时 | 838d556 | 2018-05-10 02:05:03 +0000 | [diff] [blame] | 4016 | cmd = e->cmd; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4017 | e->cmd = BR_OK; |
| 4018 | ptr += sizeof(uint32_t); |
| 4019 | |
宋金时 | 838d556 | 2018-05-10 02:05:03 +0000 | [diff] [blame] | 4020 | binder_stat_br(proc, thread, cmd); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4021 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4022 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4023 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4024 | cmd = BR_TRANSACTION_COMPLETE; |
| 4025 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4026 | return -EFAULT; |
| 4027 | ptr += sizeof(uint32_t); |
| 4028 | |
| 4029 | binder_stat_br(proc, thread, cmd); |
| 4030 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4031 | "%d:%d BR_TRANSACTION_COMPLETE\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4032 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4033 | kfree(w); |
| 4034 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4035 | } break; |
| 4036 | case BINDER_WORK_NODE: { |
| 4037 | struct binder_node *node = container_of(w, struct binder_node, work); |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4038 | int strong, weak; |
| 4039 | binder_uintptr_t node_ptr = node->ptr; |
| 4040 | binder_uintptr_t node_cookie = node->cookie; |
| 4041 | int node_debug_id = node->debug_id; |
| 4042 | int has_weak_ref; |
| 4043 | int has_strong_ref; |
| 4044 | void __user *orig_ptr = ptr; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4045 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4046 | BUG_ON(proc != node->proc); |
| 4047 | strong = node->internal_strong_refs || |
| 4048 | node->local_strong_refs; |
| 4049 | weak = !hlist_empty(&node->refs) || |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4050 | node->local_weak_refs || |
| 4051 | node->tmp_refs || strong; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4052 | has_strong_ref = node->has_strong_ref; |
| 4053 | has_weak_ref = node->has_weak_ref; |
| 4054 | |
| 4055 | if (weak && !has_weak_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4056 | node->has_weak_ref = 1; |
| 4057 | node->pending_weak_ref = 1; |
| 4058 | node->local_weak_refs++; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4059 | } |
| 4060 | if (strong && !has_strong_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4061 | node->has_strong_ref = 1; |
| 4062 | node->pending_strong_ref = 1; |
| 4063 | node->local_strong_refs++; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4064 | } |
| 4065 | if (!strong && has_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4066 | node->has_strong_ref = 0; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4067 | if (!weak && has_weak_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4068 | node->has_weak_ref = 0; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4069 | if (!weak && !strong) { |
| 4070 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4071 | "%d:%d node %d u%016llx c%016llx deleted\n", |
| 4072 | proc->pid, thread->pid, |
| 4073 | node_debug_id, |
| 4074 | (u64)node_ptr, |
| 4075 | (u64)node_cookie); |
| 4076 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4077 | binder_inner_proc_unlock(proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4078 | binder_node_lock(node); |
| 4079 | /* |
| 4080 | * Acquire the node lock before freeing the |
| 4081 | * node to serialize with other threads that |
| 4082 | * may have been holding the node lock while |
| 4083 | * decrementing this node (avoids race where |
| 4084 | * this thread frees while the other thread |
| 4085 | * is unlocking the node after the final |
| 4086 | * decrement) |
| 4087 | */ |
| 4088 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4089 | binder_free_node(node); |
| 4090 | } else |
| 4091 | binder_inner_proc_unlock(proc); |
| 4092 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4093 | if (weak && !has_weak_ref) |
| 4094 | ret = binder_put_node_cmd( |
| 4095 | proc, thread, &ptr, node_ptr, |
| 4096 | node_cookie, node_debug_id, |
| 4097 | BR_INCREFS, "BR_INCREFS"); |
| 4098 | if (!ret && strong && !has_strong_ref) |
| 4099 | ret = binder_put_node_cmd( |
| 4100 | proc, thread, &ptr, node_ptr, |
| 4101 | node_cookie, node_debug_id, |
| 4102 | BR_ACQUIRE, "BR_ACQUIRE"); |
| 4103 | if (!ret && !strong && has_strong_ref) |
| 4104 | ret = binder_put_node_cmd( |
| 4105 | proc, thread, &ptr, node_ptr, |
| 4106 | node_cookie, node_debug_id, |
| 4107 | BR_RELEASE, "BR_RELEASE"); |
| 4108 | if (!ret && !weak && has_weak_ref) |
| 4109 | ret = binder_put_node_cmd( |
| 4110 | proc, thread, &ptr, node_ptr, |
| 4111 | node_cookie, node_debug_id, |
| 4112 | BR_DECREFS, "BR_DECREFS"); |
| 4113 | if (orig_ptr == ptr) |
| 4114 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4115 | "%d:%d node %d u%016llx c%016llx state unchanged\n", |
| 4116 | proc->pid, thread->pid, |
| 4117 | node_debug_id, |
| 4118 | (u64)node_ptr, |
| 4119 | (u64)node_cookie); |
| 4120 | if (ret) |
| 4121 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4122 | } break; |
| 4123 | case BINDER_WORK_DEAD_BINDER: |
| 4124 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4125 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4126 | struct binder_ref_death *death; |
| 4127 | uint32_t cmd; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4128 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4129 | |
| 4130 | death = container_of(w, struct binder_ref_death, work); |
| 4131 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) |
| 4132 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; |
| 4133 | else |
| 4134 | cmd = BR_DEAD_BINDER; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4135 | cookie = death->cookie; |
| 4136 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4137 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4138 | "%d:%d %s %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4139 | proc->pid, thread->pid, |
| 4140 | cmd == BR_DEAD_BINDER ? |
| 4141 | "BR_DEAD_BINDER" : |
| 4142 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4143 | (u64)cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4144 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4145 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4146 | kfree(death); |
| 4147 | binder_stats_deleted(BINDER_STAT_DEATH); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4148 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4149 | binder_enqueue_work_ilocked( |
| 4150 | w, &proc->delivered_death); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4151 | binder_inner_proc_unlock(proc); |
| 4152 | } |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4153 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4154 | return -EFAULT; |
| 4155 | ptr += sizeof(uint32_t); |
| 4156 | if (put_user(cookie, |
| 4157 | (binder_uintptr_t __user *)ptr)) |
| 4158 | return -EFAULT; |
| 4159 | ptr += sizeof(binder_uintptr_t); |
| 4160 | binder_stat_br(proc, thread, cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4161 | if (cmd == BR_DEAD_BINDER) |
| 4162 | goto done; /* DEAD_BINDER notifications can cause transactions */ |
| 4163 | } break; |
| 4164 | } |
| 4165 | |
| 4166 | if (!t) |
| 4167 | continue; |
| 4168 | |
| 4169 | BUG_ON(t->buffer == NULL); |
| 4170 | if (t->buffer->target_node) { |
| 4171 | struct binder_node *target_node = t->buffer->target_node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4172 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4173 | tr.target.ptr = target_node->ptr; |
| 4174 | tr.cookie = target_node->cookie; |
| 4175 | t->saved_priority = task_nice(current); |
| 4176 | if (t->priority < target_node->min_priority && |
| 4177 | !(t->flags & TF_ONE_WAY)) |
| 4178 | binder_set_nice(t->priority); |
| 4179 | else if (!(t->flags & TF_ONE_WAY) || |
| 4180 | t->saved_priority > target_node->min_priority) |
| 4181 | binder_set_nice(target_node->min_priority); |
| 4182 | cmd = BR_TRANSACTION; |
| 4183 | } else { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4184 | tr.target.ptr = 0; |
| 4185 | tr.cookie = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4186 | cmd = BR_REPLY; |
| 4187 | } |
| 4188 | tr.code = t->code; |
| 4189 | tr.flags = t->flags; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 4190 | tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4191 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4192 | t_from = binder_get_txn_from(t); |
| 4193 | if (t_from) { |
| 4194 | struct task_struct *sender = t_from->proc->tsk; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4195 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4196 | tr.sender_pid = task_tgid_nr_ns(sender, |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 4197 | task_active_pid_ns(current)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4198 | } else { |
| 4199 | tr.sender_pid = 0; |
| 4200 | } |
| 4201 | |
Todd Kjos | 44d8047 | 2018-08-28 13:46:25 -0700 | [diff] [blame] | 4202 | ret = binder_apply_fd_fixups(t); |
| 4203 | if (ret) { |
| 4204 | struct binder_buffer *buffer = t->buffer; |
| 4205 | bool oneway = !!(t->flags & TF_ONE_WAY); |
| 4206 | int tid = t->debug_id; |
| 4207 | |
| 4208 | if (t_from) |
| 4209 | binder_thread_dec_tmpref(t_from); |
| 4210 | buffer->transaction = NULL; |
| 4211 | binder_cleanup_transaction(t, "fd fixups failed", |
| 4212 | BR_FAILED_REPLY); |
| 4213 | binder_free_buf(proc, buffer); |
| 4214 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 4215 | "%d:%d %stransaction %d fd fixups failed %d/%d, line %d\n", |
| 4216 | proc->pid, thread->pid, |
| 4217 | oneway ? "async " : |
| 4218 | (cmd == BR_REPLY ? "reply " : ""), |
| 4219 | tid, BR_FAILED_REPLY, ret, __LINE__); |
| 4220 | if (cmd == BR_REPLY) { |
| 4221 | cmd = BR_FAILED_REPLY; |
| 4222 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4223 | return -EFAULT; |
| 4224 | ptr += sizeof(uint32_t); |
| 4225 | binder_stat_br(proc, thread, cmd); |
| 4226 | break; |
| 4227 | } |
| 4228 | continue; |
| 4229 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4230 | tr.data_size = t->buffer->data_size; |
| 4231 | tr.offsets_size = t->buffer->offsets_size; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4232 | tr.data.ptr.buffer = (binder_uintptr_t) |
| 4233 | ((uintptr_t)t->buffer->data + |
| 4234 | binder_alloc_get_user_buffer_offset(&proc->alloc)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4235 | tr.data.ptr.offsets = tr.data.ptr.buffer + |
| 4236 | ALIGN(t->buffer->data_size, |
| 4237 | sizeof(void *)); |
| 4238 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4239 | if (put_user(cmd, (uint32_t __user *)ptr)) { |
| 4240 | if (t_from) |
| 4241 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | fb2c445 | 2017-11-13 10:06:08 +0100 | [diff] [blame] | 4242 | |
| 4243 | binder_cleanup_transaction(t, "put_user failed", |
| 4244 | BR_FAILED_REPLY); |
| 4245 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4246 | return -EFAULT; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4247 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4248 | ptr += sizeof(uint32_t); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4249 | if (copy_to_user(ptr, &tr, sizeof(tr))) { |
| 4250 | if (t_from) |
| 4251 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | fb2c445 | 2017-11-13 10:06:08 +0100 | [diff] [blame] | 4252 | |
| 4253 | binder_cleanup_transaction(t, "copy_to_user failed", |
| 4254 | BR_FAILED_REPLY); |
| 4255 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4256 | return -EFAULT; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4257 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4258 | ptr += sizeof(tr); |
| 4259 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4260 | trace_binder_transaction_received(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4261 | binder_stat_br(proc, thread, cmd); |
| 4262 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4263 | "%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] | 4264 | proc->pid, thread->pid, |
| 4265 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : |
| 4266 | "BR_REPLY", |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4267 | t->debug_id, t_from ? t_from->proc->pid : 0, |
| 4268 | t_from ? t_from->pid : 0, cmd, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4269 | t->buffer->data_size, t->buffer->offsets_size, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4270 | (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4271 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4272 | if (t_from) |
| 4273 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4274 | t->buffer->allow_user_free = 1; |
| 4275 | if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4276 | binder_inner_proc_lock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4277 | t->to_parent = thread->transaction_stack; |
| 4278 | t->to_thread = thread; |
| 4279 | thread->transaction_stack = t; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4280 | binder_inner_proc_unlock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4281 | } else { |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 4282 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4283 | } |
| 4284 | break; |
| 4285 | } |
| 4286 | |
| 4287 | done: |
| 4288 | |
| 4289 | *consumed = ptr - buffer; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4290 | binder_inner_proc_lock(proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4291 | if (proc->requested_threads == 0 && |
| 4292 | list_empty(&thread->proc->waiting_threads) && |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4293 | proc->requested_threads_started < proc->max_threads && |
| 4294 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4295 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ |
| 4296 | /*spawn a new thread if we leave this out */) { |
| 4297 | proc->requested_threads++; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4298 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4299 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4300 | "%d:%d BR_SPAWN_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4301 | proc->pid, thread->pid); |
| 4302 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) |
| 4303 | return -EFAULT; |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 4304 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4305 | } else |
| 4306 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4307 | return 0; |
| 4308 | } |
| 4309 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4310 | static void binder_release_work(struct binder_proc *proc, |
| 4311 | struct list_head *list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4312 | { |
| 4313 | struct binder_work *w; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4314 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4315 | while (1) { |
| 4316 | w = binder_dequeue_work_head(proc, list); |
| 4317 | if (!w) |
| 4318 | return; |
| 4319 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4320 | switch (w->type) { |
| 4321 | case BINDER_WORK_TRANSACTION: { |
| 4322 | struct binder_transaction *t; |
| 4323 | |
| 4324 | t = container_of(w, struct binder_transaction, work); |
Martijn Coenen | fb2c445 | 2017-11-13 10:06:08 +0100 | [diff] [blame] | 4325 | |
| 4326 | binder_cleanup_transaction(t, "process died.", |
| 4327 | BR_DEAD_REPLY); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4328 | } break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4329 | case BINDER_WORK_RETURN_ERROR: { |
| 4330 | struct binder_error *e = container_of( |
| 4331 | w, struct binder_error, work); |
| 4332 | |
| 4333 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 4334 | "undelivered TRANSACTION_ERROR: %u\n", |
| 4335 | e->cmd); |
| 4336 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4337 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4338 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4339 | "undelivered TRANSACTION_COMPLETE\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4340 | kfree(w); |
| 4341 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4342 | } break; |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4343 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4344 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4345 | struct binder_ref_death *death; |
| 4346 | |
| 4347 | death = container_of(w, struct binder_ref_death, work); |
| 4348 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4349 | "undelivered death notification, %016llx\n", |
| 4350 | (u64)death->cookie); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4351 | kfree(death); |
| 4352 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 4353 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4354 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4355 | pr_err("unexpected work type, %d, not freed\n", |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4356 | w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4357 | break; |
| 4358 | } |
| 4359 | } |
| 4360 | |
| 4361 | } |
| 4362 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4363 | static struct binder_thread *binder_get_thread_ilocked( |
| 4364 | struct binder_proc *proc, struct binder_thread *new_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4365 | { |
| 4366 | struct binder_thread *thread = NULL; |
| 4367 | struct rb_node *parent = NULL; |
| 4368 | struct rb_node **p = &proc->threads.rb_node; |
| 4369 | |
| 4370 | while (*p) { |
| 4371 | parent = *p; |
| 4372 | thread = rb_entry(parent, struct binder_thread, rb_node); |
| 4373 | |
| 4374 | if (current->pid < thread->pid) |
| 4375 | p = &(*p)->rb_left; |
| 4376 | else if (current->pid > thread->pid) |
| 4377 | p = &(*p)->rb_right; |
| 4378 | else |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4379 | return thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4380 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4381 | if (!new_thread) |
| 4382 | return NULL; |
| 4383 | thread = new_thread; |
| 4384 | binder_stats_created(BINDER_STAT_THREAD); |
| 4385 | thread->proc = proc; |
| 4386 | thread->pid = current->pid; |
| 4387 | atomic_set(&thread->tmp_ref, 0); |
| 4388 | init_waitqueue_head(&thread->wait); |
| 4389 | INIT_LIST_HEAD(&thread->todo); |
| 4390 | rb_link_node(&thread->rb_node, parent, p); |
| 4391 | rb_insert_color(&thread->rb_node, &proc->threads); |
| 4392 | thread->looper_need_return = true; |
| 4393 | thread->return_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4394 | thread->return_error.cmd = BR_OK; |
| 4395 | thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4396 | thread->reply_error.cmd = BR_OK; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4397 | INIT_LIST_HEAD(&new_thread->waiting_thread_node); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4398 | return thread; |
| 4399 | } |
| 4400 | |
| 4401 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) |
| 4402 | { |
| 4403 | struct binder_thread *thread; |
| 4404 | struct binder_thread *new_thread; |
| 4405 | |
| 4406 | binder_inner_proc_lock(proc); |
| 4407 | thread = binder_get_thread_ilocked(proc, NULL); |
| 4408 | binder_inner_proc_unlock(proc); |
| 4409 | if (!thread) { |
| 4410 | new_thread = kzalloc(sizeof(*thread), GFP_KERNEL); |
| 4411 | if (new_thread == NULL) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4412 | return NULL; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4413 | binder_inner_proc_lock(proc); |
| 4414 | thread = binder_get_thread_ilocked(proc, new_thread); |
| 4415 | binder_inner_proc_unlock(proc); |
| 4416 | if (thread != new_thread) |
| 4417 | kfree(new_thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4418 | } |
| 4419 | return thread; |
| 4420 | } |
| 4421 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4422 | static void binder_free_proc(struct binder_proc *proc) |
| 4423 | { |
| 4424 | BUG_ON(!list_empty(&proc->todo)); |
| 4425 | BUG_ON(!list_empty(&proc->delivered_death)); |
| 4426 | binder_alloc_deferred_release(&proc->alloc); |
| 4427 | put_task_struct(proc->tsk); |
| 4428 | binder_stats_deleted(BINDER_STAT_PROC); |
| 4429 | kfree(proc); |
| 4430 | } |
| 4431 | |
| 4432 | static void binder_free_thread(struct binder_thread *thread) |
| 4433 | { |
| 4434 | BUG_ON(!list_empty(&thread->todo)); |
| 4435 | binder_stats_deleted(BINDER_STAT_THREAD); |
| 4436 | binder_proc_dec_tmpref(thread->proc); |
| 4437 | kfree(thread); |
| 4438 | } |
| 4439 | |
| 4440 | static int binder_thread_release(struct binder_proc *proc, |
| 4441 | struct binder_thread *thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4442 | { |
| 4443 | struct binder_transaction *t; |
| 4444 | struct binder_transaction *send_reply = NULL; |
| 4445 | int active_transactions = 0; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4446 | struct binder_transaction *last_t = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4447 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4448 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4449 | /* |
| 4450 | * take a ref on the proc so it survives |
| 4451 | * after we remove this thread from proc->threads. |
| 4452 | * The corresponding dec is when we actually |
| 4453 | * free the thread in binder_free_thread() |
| 4454 | */ |
| 4455 | proc->tmp_ref++; |
| 4456 | /* |
| 4457 | * take a ref on this thread to ensure it |
| 4458 | * survives while we are releasing it |
| 4459 | */ |
| 4460 | atomic_inc(&thread->tmp_ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4461 | rb_erase(&thread->rb_node, &proc->threads); |
| 4462 | t = thread->transaction_stack; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4463 | if (t) { |
| 4464 | spin_lock(&t->lock); |
| 4465 | if (t->to_thread == thread) |
| 4466 | send_reply = t; |
| 4467 | } |
| 4468 | thread->is_dead = true; |
| 4469 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4470 | while (t) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4471 | last_t = t; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4472 | active_transactions++; |
| 4473 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4474 | "release %d:%d transaction %d %s, still active\n", |
| 4475 | proc->pid, thread->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4476 | t->debug_id, |
| 4477 | (t->to_thread == thread) ? "in" : "out"); |
| 4478 | |
| 4479 | if (t->to_thread == thread) { |
| 4480 | t->to_proc = NULL; |
| 4481 | t->to_thread = NULL; |
| 4482 | if (t->buffer) { |
| 4483 | t->buffer->transaction = NULL; |
| 4484 | t->buffer = NULL; |
| 4485 | } |
| 4486 | t = t->to_parent; |
| 4487 | } else if (t->from == thread) { |
| 4488 | t->from = NULL; |
| 4489 | t = t->from_parent; |
| 4490 | } else |
| 4491 | BUG(); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4492 | spin_unlock(&last_t->lock); |
| 4493 | if (t) |
| 4494 | spin_lock(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4495 | } |
Martijn Coenen | f5cb779 | 2018-01-05 11:27:07 +0100 | [diff] [blame] | 4496 | |
| 4497 | /* |
| 4498 | * If this thread used poll, make sure we remove the waitqueue |
| 4499 | * from any epoll data structures holding it with POLLFREE. |
| 4500 | * waitqueue_active() is safe to use here because we're holding |
| 4501 | * the inner lock. |
| 4502 | */ |
| 4503 | if ((thread->looper & BINDER_LOOPER_STATE_POLL) && |
| 4504 | waitqueue_active(&thread->wait)) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 4505 | wake_up_poll(&thread->wait, EPOLLHUP | POLLFREE); |
Martijn Coenen | f5cb779 | 2018-01-05 11:27:07 +0100 | [diff] [blame] | 4506 | } |
| 4507 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4508 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4509 | |
Martijn Coenen | 5eeb2ca | 2018-02-16 09:47:15 +0100 | [diff] [blame] | 4510 | /* |
| 4511 | * This is needed to avoid races between wake_up_poll() above and |
| 4512 | * and ep_remove_waitqueue() called for other reasons (eg the epoll file |
| 4513 | * descriptor being closed); ep_remove_waitqueue() holds an RCU read |
| 4514 | * lock, so we can be sure it's done after calling synchronize_rcu(). |
| 4515 | */ |
| 4516 | if (thread->looper & BINDER_LOOPER_STATE_POLL) |
| 4517 | synchronize_rcu(); |
| 4518 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4519 | if (send_reply) |
| 4520 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4521 | binder_release_work(proc, &thread->todo); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4522 | binder_thread_dec_tmpref(thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4523 | return active_transactions; |
| 4524 | } |
| 4525 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 4526 | static __poll_t binder_poll(struct file *filp, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4527 | struct poll_table_struct *wait) |
| 4528 | { |
| 4529 | struct binder_proc *proc = filp->private_data; |
| 4530 | struct binder_thread *thread = NULL; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4531 | bool wait_for_proc_work; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4532 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4533 | thread = binder_get_thread(proc); |
Eric Biggers | f889826 | 2018-01-30 23:11:24 -0800 | [diff] [blame] | 4534 | if (!thread) |
| 4535 | return POLLERR; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4536 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4537 | binder_inner_proc_lock(thread->proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4538 | thread->looper |= BINDER_LOOPER_STATE_POLL; |
| 4539 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
| 4540 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4541 | binder_inner_proc_unlock(thread->proc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4542 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4543 | poll_wait(filp, &thread->wait, wait); |
| 4544 | |
Martijn Coenen | 66b83a4 | 2017-10-09 14:26:56 +0200 | [diff] [blame] | 4545 | if (binder_has_work(thread, wait_for_proc_work)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 4546 | return EPOLLIN; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4547 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4548 | return 0; |
| 4549 | } |
| 4550 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4551 | static int binder_ioctl_write_read(struct file *filp, |
| 4552 | unsigned int cmd, unsigned long arg, |
| 4553 | struct binder_thread *thread) |
| 4554 | { |
| 4555 | int ret = 0; |
| 4556 | struct binder_proc *proc = filp->private_data; |
| 4557 | unsigned int size = _IOC_SIZE(cmd); |
| 4558 | void __user *ubuf = (void __user *)arg; |
| 4559 | struct binder_write_read bwr; |
| 4560 | |
| 4561 | if (size != sizeof(struct binder_write_read)) { |
| 4562 | ret = -EINVAL; |
| 4563 | goto out; |
| 4564 | } |
| 4565 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { |
| 4566 | ret = -EFAULT; |
| 4567 | goto out; |
| 4568 | } |
| 4569 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4570 | "%d:%d write %lld at %016llx, read %lld at %016llx\n", |
| 4571 | proc->pid, thread->pid, |
| 4572 | (u64)bwr.write_size, (u64)bwr.write_buffer, |
| 4573 | (u64)bwr.read_size, (u64)bwr.read_buffer); |
| 4574 | |
| 4575 | if (bwr.write_size > 0) { |
| 4576 | ret = binder_thread_write(proc, thread, |
| 4577 | bwr.write_buffer, |
| 4578 | bwr.write_size, |
| 4579 | &bwr.write_consumed); |
| 4580 | trace_binder_write_done(ret); |
| 4581 | if (ret < 0) { |
| 4582 | bwr.read_consumed = 0; |
| 4583 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4584 | ret = -EFAULT; |
| 4585 | goto out; |
| 4586 | } |
| 4587 | } |
| 4588 | if (bwr.read_size > 0) { |
| 4589 | ret = binder_thread_read(proc, thread, bwr.read_buffer, |
| 4590 | bwr.read_size, |
| 4591 | &bwr.read_consumed, |
| 4592 | filp->f_flags & O_NONBLOCK); |
| 4593 | trace_binder_read_done(ret); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4594 | binder_inner_proc_lock(proc); |
| 4595 | if (!binder_worklist_empty_ilocked(&proc->todo)) |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 4596 | binder_wakeup_proc_ilocked(proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4597 | binder_inner_proc_unlock(proc); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4598 | if (ret < 0) { |
| 4599 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4600 | ret = -EFAULT; |
| 4601 | goto out; |
| 4602 | } |
| 4603 | } |
| 4604 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4605 | "%d:%d wrote %lld of %lld, read return %lld of %lld\n", |
| 4606 | proc->pid, thread->pid, |
| 4607 | (u64)bwr.write_consumed, (u64)bwr.write_size, |
| 4608 | (u64)bwr.read_consumed, (u64)bwr.read_size); |
| 4609 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { |
| 4610 | ret = -EFAULT; |
| 4611 | goto out; |
| 4612 | } |
| 4613 | out: |
| 4614 | return ret; |
| 4615 | } |
| 4616 | |
| 4617 | static int binder_ioctl_set_ctx_mgr(struct file *filp) |
| 4618 | { |
| 4619 | int ret = 0; |
| 4620 | struct binder_proc *proc = filp->private_data; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4621 | struct binder_context *context = proc->context; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4622 | struct binder_node *new_node; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4623 | kuid_t curr_euid = current_euid(); |
| 4624 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4625 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4626 | if (context->binder_context_mgr_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4627 | pr_err("BINDER_SET_CONTEXT_MGR already set\n"); |
| 4628 | ret = -EBUSY; |
| 4629 | goto out; |
| 4630 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 4631 | ret = security_binder_set_context_mgr(proc->tsk); |
| 4632 | if (ret < 0) |
| 4633 | goto out; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4634 | if (uid_valid(context->binder_context_mgr_uid)) { |
| 4635 | if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4636 | pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n", |
| 4637 | from_kuid(&init_user_ns, curr_euid), |
| 4638 | from_kuid(&init_user_ns, |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4639 | context->binder_context_mgr_uid)); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4640 | ret = -EPERM; |
| 4641 | goto out; |
| 4642 | } |
| 4643 | } else { |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4644 | context->binder_context_mgr_uid = curr_euid; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4645 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4646 | new_node = binder_new_node(proc, NULL); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4647 | if (!new_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4648 | ret = -ENOMEM; |
| 4649 | goto out; |
| 4650 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4651 | binder_node_lock(new_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4652 | new_node->local_weak_refs++; |
| 4653 | new_node->local_strong_refs++; |
| 4654 | new_node->has_strong_ref = 1; |
| 4655 | new_node->has_weak_ref = 1; |
| 4656 | context->binder_context_mgr_node = new_node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4657 | binder_node_unlock(new_node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4658 | binder_put_node(new_node); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4659 | out: |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4660 | mutex_unlock(&context->context_mgr_node_lock); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4661 | return ret; |
| 4662 | } |
| 4663 | |
Martijn Coenen | b7e6a89 | 2018-09-07 15:38:37 +0200 | [diff] [blame] | 4664 | static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc, |
| 4665 | struct binder_node_info_for_ref *info) |
| 4666 | { |
| 4667 | struct binder_node *node; |
| 4668 | struct binder_context *context = proc->context; |
| 4669 | __u32 handle = info->handle; |
| 4670 | |
| 4671 | if (info->strong_count || info->weak_count || info->reserved1 || |
| 4672 | info->reserved2 || info->reserved3) { |
| 4673 | binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.", |
| 4674 | proc->pid); |
| 4675 | return -EINVAL; |
| 4676 | } |
| 4677 | |
| 4678 | /* This ioctl may only be used by the context manager */ |
| 4679 | mutex_lock(&context->context_mgr_node_lock); |
| 4680 | if (!context->binder_context_mgr_node || |
| 4681 | context->binder_context_mgr_node->proc != proc) { |
| 4682 | mutex_unlock(&context->context_mgr_node_lock); |
| 4683 | return -EPERM; |
| 4684 | } |
| 4685 | mutex_unlock(&context->context_mgr_node_lock); |
| 4686 | |
| 4687 | node = binder_get_node_from_ref(proc, handle, true, NULL); |
| 4688 | if (!node) |
| 4689 | return -EINVAL; |
| 4690 | |
| 4691 | info->strong_count = node->local_strong_refs + |
| 4692 | node->internal_strong_refs; |
| 4693 | info->weak_count = node->local_weak_refs; |
| 4694 | |
| 4695 | binder_put_node(node); |
| 4696 | |
| 4697 | return 0; |
| 4698 | } |
| 4699 | |
Colin Cross | abcc615 | 2017-08-31 10:04:24 +0200 | [diff] [blame] | 4700 | static int binder_ioctl_get_node_debug_info(struct binder_proc *proc, |
| 4701 | struct binder_node_debug_info *info) |
| 4702 | { |
| 4703 | struct rb_node *n; |
| 4704 | binder_uintptr_t ptr = info->ptr; |
| 4705 | |
| 4706 | memset(info, 0, sizeof(*info)); |
| 4707 | |
| 4708 | binder_inner_proc_lock(proc); |
| 4709 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { |
| 4710 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 4711 | rb_node); |
| 4712 | if (node->ptr > ptr) { |
| 4713 | info->ptr = node->ptr; |
| 4714 | info->cookie = node->cookie; |
| 4715 | info->has_strong_ref = node->has_strong_ref; |
| 4716 | info->has_weak_ref = node->has_weak_ref; |
| 4717 | break; |
| 4718 | } |
| 4719 | } |
| 4720 | binder_inner_proc_unlock(proc); |
| 4721 | |
| 4722 | return 0; |
| 4723 | } |
| 4724 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4725 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 4726 | { |
| 4727 | int ret; |
| 4728 | struct binder_proc *proc = filp->private_data; |
| 4729 | struct binder_thread *thread; |
| 4730 | unsigned int size = _IOC_SIZE(cmd); |
| 4731 | void __user *ubuf = (void __user *)arg; |
| 4732 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4733 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", |
| 4734 | proc->pid, current->pid, cmd, arg);*/ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4735 | |
Sherry Yang | 4175e2b | 2017-08-23 08:46:40 -0700 | [diff] [blame] | 4736 | binder_selftest_alloc(&proc->alloc); |
| 4737 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4738 | trace_binder_ioctl(cmd, arg); |
| 4739 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4740 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4741 | if (ret) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4742 | goto err_unlocked; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4743 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4744 | thread = binder_get_thread(proc); |
| 4745 | if (thread == NULL) { |
| 4746 | ret = -ENOMEM; |
| 4747 | goto err; |
| 4748 | } |
| 4749 | |
| 4750 | switch (cmd) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4751 | case BINDER_WRITE_READ: |
| 4752 | ret = binder_ioctl_write_read(filp, cmd, arg, thread); |
| 4753 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4754 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4755 | break; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4756 | case BINDER_SET_MAX_THREADS: { |
| 4757 | int max_threads; |
| 4758 | |
| 4759 | if (copy_from_user(&max_threads, ubuf, |
| 4760 | sizeof(max_threads))) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4761 | ret = -EINVAL; |
| 4762 | goto err; |
| 4763 | } |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4764 | binder_inner_proc_lock(proc); |
| 4765 | proc->max_threads = max_threads; |
| 4766 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4767 | break; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4768 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4769 | case BINDER_SET_CONTEXT_MGR: |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4770 | ret = binder_ioctl_set_ctx_mgr(filp); |
| 4771 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4772 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4773 | break; |
| 4774 | case BINDER_THREAD_EXIT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4775 | binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4776 | proc->pid, thread->pid); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4777 | binder_thread_release(proc, thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4778 | thread = NULL; |
| 4779 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4780 | case BINDER_VERSION: { |
| 4781 | struct binder_version __user *ver = ubuf; |
| 4782 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4783 | if (size != sizeof(struct binder_version)) { |
| 4784 | ret = -EINVAL; |
| 4785 | goto err; |
| 4786 | } |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4787 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, |
| 4788 | &ver->protocol_version)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4789 | ret = -EINVAL; |
| 4790 | goto err; |
| 4791 | } |
| 4792 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4793 | } |
Martijn Coenen | b7e6a89 | 2018-09-07 15:38:37 +0200 | [diff] [blame] | 4794 | case BINDER_GET_NODE_INFO_FOR_REF: { |
| 4795 | struct binder_node_info_for_ref info; |
| 4796 | |
| 4797 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 4798 | ret = -EFAULT; |
| 4799 | goto err; |
| 4800 | } |
| 4801 | |
| 4802 | ret = binder_ioctl_get_node_info_for_ref(proc, &info); |
| 4803 | if (ret < 0) |
| 4804 | goto err; |
| 4805 | |
| 4806 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 4807 | ret = -EFAULT; |
| 4808 | goto err; |
| 4809 | } |
| 4810 | |
| 4811 | break; |
| 4812 | } |
Colin Cross | abcc615 | 2017-08-31 10:04:24 +0200 | [diff] [blame] | 4813 | case BINDER_GET_NODE_DEBUG_INFO: { |
| 4814 | struct binder_node_debug_info info; |
| 4815 | |
| 4816 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 4817 | ret = -EFAULT; |
| 4818 | goto err; |
| 4819 | } |
| 4820 | |
| 4821 | ret = binder_ioctl_get_node_debug_info(proc, &info); |
| 4822 | if (ret < 0) |
| 4823 | goto err; |
| 4824 | |
| 4825 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 4826 | ret = -EFAULT; |
| 4827 | goto err; |
| 4828 | } |
| 4829 | break; |
| 4830 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4831 | default: |
| 4832 | ret = -EINVAL; |
| 4833 | goto err; |
| 4834 | } |
| 4835 | ret = 0; |
| 4836 | err: |
| 4837 | if (thread) |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 4838 | thread->looper_need_return = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4839 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4840 | if (ret && ret != -ERESTARTSYS) |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4841 | 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] | 4842 | err_unlocked: |
| 4843 | trace_binder_ioctl_done(ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4844 | return ret; |
| 4845 | } |
| 4846 | |
| 4847 | static void binder_vma_open(struct vm_area_struct *vma) |
| 4848 | { |
| 4849 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4850 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4851 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4852 | "%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] | 4853 | proc->pid, vma->vm_start, vma->vm_end, |
| 4854 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4855 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | static void binder_vma_close(struct vm_area_struct *vma) |
| 4859 | { |
| 4860 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4861 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4862 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4863 | "%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] | 4864 | proc->pid, vma->vm_start, vma->vm_end, |
| 4865 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4866 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4867 | binder_alloc_vma_close(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4868 | } |
| 4869 | |
Souptick Joarder | e19f70a | 2018-04-23 21:54:00 +0530 | [diff] [blame] | 4870 | static vm_fault_t binder_vm_fault(struct vm_fault *vmf) |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4871 | { |
| 4872 | return VM_FAULT_SIGBUS; |
| 4873 | } |
| 4874 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 4875 | static const struct vm_operations_struct binder_vm_ops = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4876 | .open = binder_vma_open, |
| 4877 | .close = binder_vma_close, |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4878 | .fault = binder_vm_fault, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4879 | }; |
| 4880 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4881 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) |
| 4882 | { |
| 4883 | int ret; |
| 4884 | struct binder_proc *proc = filp->private_data; |
| 4885 | const char *failure_string; |
| 4886 | |
| 4887 | if (proc->tsk != current->group_leader) |
| 4888 | return -EINVAL; |
| 4889 | |
| 4890 | if ((vma->vm_end - vma->vm_start) > SZ_4M) |
| 4891 | vma->vm_end = vma->vm_start + SZ_4M; |
| 4892 | |
| 4893 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 4894 | "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 4895 | __func__, proc->pid, vma->vm_start, vma->vm_end, |
| 4896 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4897 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 4898 | |
| 4899 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { |
| 4900 | ret = -EPERM; |
| 4901 | failure_string = "bad vm_flags"; |
| 4902 | goto err_bad_arg; |
| 4903 | } |
Minchan Kim | 720c2419 | 2018-05-07 23:15:37 +0900 | [diff] [blame] | 4904 | vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP; |
| 4905 | vma->vm_flags &= ~VM_MAYWRITE; |
| 4906 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4907 | vma->vm_ops = &binder_vm_ops; |
| 4908 | vma->vm_private_data = proc; |
| 4909 | |
| 4910 | ret = binder_alloc_mmap_handler(&proc->alloc, vma); |
| 4911 | if (ret) |
| 4912 | return ret; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4913 | return 0; |
| 4914 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4915 | err_bad_arg: |
Elad Wexler | 00c41cd | 2017-12-29 11:03:37 +0200 | [diff] [blame] | 4916 | pr_err("%s: %d %lx-%lx %s failed %d\n", __func__, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4917 | proc->pid, vma->vm_start, vma->vm_end, failure_string, ret); |
| 4918 | return ret; |
| 4919 | } |
| 4920 | |
| 4921 | static int binder_open(struct inode *nodp, struct file *filp) |
| 4922 | { |
| 4923 | struct binder_proc *proc; |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 4924 | struct binder_device *binder_dev; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4925 | |
Elad Wexler | 00c41cd | 2017-12-29 11:03:37 +0200 | [diff] [blame] | 4926 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4927 | current->group_leader->pid, current->pid); |
| 4928 | |
| 4929 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 4930 | if (proc == NULL) |
| 4931 | return -ENOMEM; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 4932 | spin_lock_init(&proc->inner_lock); |
| 4933 | spin_lock_init(&proc->outer_lock); |
Todd Kjos | c4ea41b | 2017-06-29 12:01:36 -0700 | [diff] [blame] | 4934 | get_task_struct(current->group_leader); |
| 4935 | proc->tsk = current->group_leader; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4936 | INIT_LIST_HEAD(&proc->todo); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4937 | proc->default_priority = task_nice(current); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 4938 | binder_dev = container_of(filp->private_data, struct binder_device, |
| 4939 | miscdev); |
| 4940 | proc->context = &binder_dev->context; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4941 | binder_alloc_init(&proc->alloc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4942 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4943 | binder_stats_created(BINDER_STAT_PROC); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4944 | proc->pid = current->group_leader->pid; |
| 4945 | INIT_LIST_HEAD(&proc->delivered_death); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4946 | INIT_LIST_HEAD(&proc->waiting_threads); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4947 | filp->private_data = proc; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4948 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4949 | mutex_lock(&binder_procs_lock); |
| 4950 | hlist_add_head(&proc->proc_node, &binder_procs); |
| 4951 | mutex_unlock(&binder_procs_lock); |
| 4952 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4953 | if (binder_debugfs_dir_entry_proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4954 | char strbuf[11]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4955 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4956 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 4957 | /* |
| 4958 | * proc debug entries are shared between contexts, so |
| 4959 | * this will fail if the process tries to open the driver |
| 4960 | * again with a different context. The priting code will |
| 4961 | * anyway print all contexts that a given PID has, so this |
| 4962 | * is not a problem. |
| 4963 | */ |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 4964 | proc->debugfs_entry = debugfs_create_file(strbuf, 0444, |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 4965 | binder_debugfs_dir_entry_proc, |
| 4966 | (void *)(unsigned long)proc->pid, |
| 4967 | &binder_proc_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4968 | } |
| 4969 | |
| 4970 | return 0; |
| 4971 | } |
| 4972 | |
| 4973 | static int binder_flush(struct file *filp, fl_owner_t id) |
| 4974 | { |
| 4975 | struct binder_proc *proc = filp->private_data; |
| 4976 | |
| 4977 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); |
| 4978 | |
| 4979 | return 0; |
| 4980 | } |
| 4981 | |
| 4982 | static void binder_deferred_flush(struct binder_proc *proc) |
| 4983 | { |
| 4984 | struct rb_node *n; |
| 4985 | int wake_count = 0; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4986 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4987 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4988 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 4989 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4990 | |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 4991 | thread->looper_need_return = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4992 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { |
| 4993 | wake_up_interruptible(&thread->wait); |
| 4994 | wake_count++; |
| 4995 | } |
| 4996 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4997 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4998 | |
| 4999 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 5000 | "binder_flush: %d woke %d threads\n", proc->pid, |
| 5001 | wake_count); |
| 5002 | } |
| 5003 | |
| 5004 | static int binder_release(struct inode *nodp, struct file *filp) |
| 5005 | { |
| 5006 | struct binder_proc *proc = filp->private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5007 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5008 | debugfs_remove(proc->debugfs_entry); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5009 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); |
| 5010 | |
| 5011 | return 0; |
| 5012 | } |
| 5013 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5014 | static int binder_node_release(struct binder_node *node, int refs) |
| 5015 | { |
| 5016 | struct binder_ref *ref; |
| 5017 | int death = 0; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 5018 | struct binder_proc *proc = node->proc; |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5019 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5020 | binder_release_work(proc, &node->async_todo); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 5021 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5022 | binder_node_lock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 5023 | binder_inner_proc_lock(proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5024 | binder_dequeue_work_ilocked(&node->work); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5025 | /* |
| 5026 | * The caller must have taken a temporary ref on the node, |
| 5027 | */ |
| 5028 | BUG_ON(!node->tmp_refs); |
| 5029 | if (hlist_empty(&node->refs) && node->tmp_refs == 1) { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 5030 | binder_inner_proc_unlock(proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5031 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 5032 | binder_free_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5033 | |
| 5034 | return refs; |
| 5035 | } |
| 5036 | |
| 5037 | node->proc = NULL; |
| 5038 | node->local_strong_refs = 0; |
| 5039 | node->local_weak_refs = 0; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 5040 | binder_inner_proc_unlock(proc); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5041 | |
| 5042 | spin_lock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5043 | hlist_add_head(&node->dead_node, &binder_dead_nodes); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5044 | spin_unlock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5045 | |
| 5046 | hlist_for_each_entry(ref, &node->refs, node_entry) { |
| 5047 | refs++; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 5048 | /* |
| 5049 | * Need the node lock to synchronize |
| 5050 | * with new notification requests and the |
| 5051 | * inner lock to synchronize with queued |
| 5052 | * death notifications. |
| 5053 | */ |
| 5054 | binder_inner_proc_lock(ref->proc); |
| 5055 | if (!ref->death) { |
| 5056 | binder_inner_proc_unlock(ref->proc); |
Arve Hjønnevåg | e194fd8 | 2014-02-17 13:58:29 -0800 | [diff] [blame] | 5057 | continue; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 5058 | } |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5059 | |
| 5060 | death++; |
| 5061 | |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 5062 | BUG_ON(!list_empty(&ref->death->work.entry)); |
| 5063 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 5064 | binder_enqueue_work_ilocked(&ref->death->work, |
| 5065 | &ref->proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 5066 | binder_wakeup_proc_ilocked(ref->proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5067 | binder_inner_proc_unlock(ref->proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5068 | } |
| 5069 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5070 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 5071 | "node %d now dead, refs %d, death %d\n", |
| 5072 | node->debug_id, refs, death); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5073 | binder_node_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5074 | binder_put_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5075 | |
| 5076 | return refs; |
| 5077 | } |
| 5078 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5079 | static void binder_deferred_release(struct binder_proc *proc) |
| 5080 | { |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 5081 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5082 | struct rb_node *n; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5083 | int threads, nodes, incoming_refs, outgoing_refs, active_transactions; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5084 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5085 | mutex_lock(&binder_procs_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5086 | hlist_del(&proc->proc_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5087 | mutex_unlock(&binder_procs_lock); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5088 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5089 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 5090 | if (context->binder_context_mgr_node && |
| 5091 | context->binder_context_mgr_node->proc == proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5092 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 5093 | "%s: %d context_mgr_node gone\n", |
| 5094 | __func__, proc->pid); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 5095 | context->binder_context_mgr_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5096 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5097 | mutex_unlock(&context->context_mgr_node_lock); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5098 | binder_inner_proc_lock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5099 | /* |
| 5100 | * Make sure proc stays alive after we |
| 5101 | * remove all the threads |
| 5102 | */ |
| 5103 | proc->tmp_ref++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5104 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5105 | proc->is_dead = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5106 | threads = 0; |
| 5107 | active_transactions = 0; |
| 5108 | while ((n = rb_first(&proc->threads))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5109 | struct binder_thread *thread; |
| 5110 | |
| 5111 | thread = rb_entry(n, struct binder_thread, rb_node); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5112 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5113 | threads++; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5114 | active_transactions += binder_thread_release(proc, thread); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5115 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5116 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5117 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5118 | nodes = 0; |
| 5119 | incoming_refs = 0; |
| 5120 | while ((n = rb_first(&proc->nodes))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5121 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5122 | |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5123 | node = rb_entry(n, struct binder_node, rb_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5124 | nodes++; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5125 | /* |
| 5126 | * take a temporary ref on the node before |
| 5127 | * calling binder_node_release() which will either |
| 5128 | * kfree() the node or call binder_put_node() |
| 5129 | */ |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5130 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5131 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5132 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5133 | incoming_refs = binder_node_release(node, incoming_refs); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5134 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5135 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5136 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5137 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5138 | outgoing_refs = 0; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5139 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5140 | while ((n = rb_first(&proc->refs_by_desc))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5141 | struct binder_ref *ref; |
| 5142 | |
| 5143 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5144 | outgoing_refs++; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5145 | binder_cleanup_ref_olocked(ref); |
| 5146 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 5147 | binder_free_ref(ref); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5148 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5149 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5150 | binder_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5151 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5152 | binder_release_work(proc, &proc->todo); |
| 5153 | binder_release_work(proc, &proc->delivered_death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5154 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5155 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5156 | "%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] | 5157 | __func__, proc->pid, threads, nodes, incoming_refs, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5158 | outgoing_refs, active_transactions); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5159 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5160 | binder_proc_dec_tmpref(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5161 | } |
| 5162 | |
| 5163 | static void binder_deferred_func(struct work_struct *work) |
| 5164 | { |
| 5165 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5166 | |
| 5167 | int defer; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5168 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5169 | do { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5170 | mutex_lock(&binder_deferred_lock); |
| 5171 | if (!hlist_empty(&binder_deferred_list)) { |
| 5172 | proc = hlist_entry(binder_deferred_list.first, |
| 5173 | struct binder_proc, deferred_work_node); |
| 5174 | hlist_del_init(&proc->deferred_work_node); |
| 5175 | defer = proc->deferred_work; |
| 5176 | proc->deferred_work = 0; |
| 5177 | } else { |
| 5178 | proc = NULL; |
| 5179 | defer = 0; |
| 5180 | } |
| 5181 | mutex_unlock(&binder_deferred_lock); |
| 5182 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5183 | if (defer & BINDER_DEFERRED_FLUSH) |
| 5184 | binder_deferred_flush(proc); |
| 5185 | |
| 5186 | if (defer & BINDER_DEFERRED_RELEASE) |
| 5187 | binder_deferred_release(proc); /* frees proc */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5188 | } while (proc); |
| 5189 | } |
| 5190 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); |
| 5191 | |
| 5192 | static void |
| 5193 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) |
| 5194 | { |
| 5195 | mutex_lock(&binder_deferred_lock); |
| 5196 | proc->deferred_work |= defer; |
| 5197 | if (hlist_unhashed(&proc->deferred_work_node)) { |
| 5198 | hlist_add_head(&proc->deferred_work_node, |
| 5199 | &binder_deferred_list); |
Bhaktipriya Shridhar | 1beba52 | 2016-08-13 22:16:24 +0530 | [diff] [blame] | 5200 | schedule_work(&binder_deferred_work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5201 | } |
| 5202 | mutex_unlock(&binder_deferred_lock); |
| 5203 | } |
| 5204 | |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5205 | static void print_binder_transaction_ilocked(struct seq_file *m, |
| 5206 | struct binder_proc *proc, |
| 5207 | const char *prefix, |
| 5208 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5209 | { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5210 | struct binder_proc *to_proc; |
| 5211 | struct binder_buffer *buffer = t->buffer; |
| 5212 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5213 | spin_lock(&t->lock); |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5214 | to_proc = t->to_proc; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5215 | seq_printf(m, |
Todd Kjos | 8ca86f1 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 5216 | "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %ld r%d", |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5217 | prefix, t->debug_id, t, |
| 5218 | t->from ? t->from->proc->pid : 0, |
| 5219 | t->from ? t->from->pid : 0, |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5220 | to_proc ? to_proc->pid : 0, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5221 | t->to_thread ? t->to_thread->pid : 0, |
| 5222 | t->code, t->flags, t->priority, t->need_reply); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5223 | spin_unlock(&t->lock); |
| 5224 | |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5225 | if (proc != to_proc) { |
| 5226 | /* |
| 5227 | * Can only safely deref buffer if we are holding the |
| 5228 | * correct proc inner lock for this node |
| 5229 | */ |
| 5230 | seq_puts(m, "\n"); |
| 5231 | return; |
| 5232 | } |
| 5233 | |
| 5234 | if (buffer == NULL) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5235 | seq_puts(m, " buffer free\n"); |
| 5236 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5237 | } |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5238 | if (buffer->target_node) |
| 5239 | seq_printf(m, " node %d", buffer->target_node->debug_id); |
Todd Kjos | 8ca86f1 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 5240 | seq_printf(m, " size %zd:%zd data %pK\n", |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5241 | buffer->data_size, buffer->offsets_size, |
| 5242 | buffer->data); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5243 | } |
| 5244 | |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5245 | static void print_binder_work_ilocked(struct seq_file *m, |
| 5246 | struct binder_proc *proc, |
| 5247 | const char *prefix, |
| 5248 | const char *transaction_prefix, |
| 5249 | struct binder_work *w) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5250 | { |
| 5251 | struct binder_node *node; |
| 5252 | struct binder_transaction *t; |
| 5253 | |
| 5254 | switch (w->type) { |
| 5255 | case BINDER_WORK_TRANSACTION: |
| 5256 | t = container_of(w, struct binder_transaction, work); |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5257 | print_binder_transaction_ilocked( |
| 5258 | m, proc, transaction_prefix, t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5259 | break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 5260 | case BINDER_WORK_RETURN_ERROR: { |
| 5261 | struct binder_error *e = container_of( |
| 5262 | w, struct binder_error, work); |
| 5263 | |
| 5264 | seq_printf(m, "%stransaction error: %u\n", |
| 5265 | prefix, e->cmd); |
| 5266 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5267 | case BINDER_WORK_TRANSACTION_COMPLETE: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5268 | seq_printf(m, "%stransaction complete\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5269 | break; |
| 5270 | case BINDER_WORK_NODE: |
| 5271 | node = container_of(w, struct binder_node, work); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5272 | seq_printf(m, "%snode work %d: u%016llx c%016llx\n", |
| 5273 | prefix, node->debug_id, |
| 5274 | (u64)node->ptr, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5275 | break; |
| 5276 | case BINDER_WORK_DEAD_BINDER: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5277 | seq_printf(m, "%shas dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5278 | break; |
| 5279 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5280 | seq_printf(m, "%shas cleared dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5281 | break; |
| 5282 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5283 | seq_printf(m, "%shas cleared death notification\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5284 | break; |
| 5285 | default: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5286 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5287 | break; |
| 5288 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5289 | } |
| 5290 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5291 | static void print_binder_thread_ilocked(struct seq_file *m, |
| 5292 | struct binder_thread *thread, |
| 5293 | int print_always) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5294 | { |
| 5295 | struct binder_transaction *t; |
| 5296 | struct binder_work *w; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5297 | size_t start_pos = m->count; |
| 5298 | size_t header_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5299 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5300 | seq_printf(m, " thread %d: l %02x need_return %d tr %d\n", |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 5301 | thread->pid, thread->looper, |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5302 | thread->looper_need_return, |
| 5303 | atomic_read(&thread->tmp_ref)); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5304 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5305 | t = thread->transaction_stack; |
| 5306 | while (t) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5307 | if (t->from == thread) { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5308 | print_binder_transaction_ilocked(m, thread->proc, |
| 5309 | " outgoing transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5310 | t = t->from_parent; |
| 5311 | } else if (t->to_thread == thread) { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5312 | print_binder_transaction_ilocked(m, thread->proc, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5313 | " incoming transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5314 | t = t->to_parent; |
| 5315 | } else { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5316 | print_binder_transaction_ilocked(m, thread->proc, |
| 5317 | " bad transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5318 | t = NULL; |
| 5319 | } |
| 5320 | } |
| 5321 | list_for_each_entry(w, &thread->todo, entry) { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5322 | print_binder_work_ilocked(m, thread->proc, " ", |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5323 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5324 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5325 | if (!print_always && m->count == header_pos) |
| 5326 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5327 | } |
| 5328 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5329 | static void print_binder_node_nilocked(struct seq_file *m, |
| 5330 | struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5331 | { |
| 5332 | struct binder_ref *ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5333 | struct binder_work *w; |
| 5334 | int count; |
| 5335 | |
| 5336 | count = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5337 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5338 | count++; |
| 5339 | |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5340 | seq_printf(m, " node %d: u%016llx c%016llx hs %d hw %d ls %d lw %d is %d iw %d tr %d", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5341 | node->debug_id, (u64)node->ptr, (u64)node->cookie, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5342 | node->has_strong_ref, node->has_weak_ref, |
| 5343 | node->local_strong_refs, node->local_weak_refs, |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5344 | node->internal_strong_refs, count, node->tmp_refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5345 | if (count) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5346 | seq_puts(m, " proc"); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5347 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5348 | seq_printf(m, " %d", ref->proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5349 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5350 | seq_puts(m, "\n"); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5351 | if (node->proc) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5352 | list_for_each_entry(w, &node->async_todo, entry) |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5353 | print_binder_work_ilocked(m, node->proc, " ", |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5354 | " pending async transaction", w); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5355 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5356 | } |
| 5357 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5358 | static void print_binder_ref_olocked(struct seq_file *m, |
| 5359 | struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5360 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5361 | binder_node_lock(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 5362 | seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n", |
| 5363 | ref->data.debug_id, ref->data.desc, |
| 5364 | ref->node->proc ? "" : "dead ", |
| 5365 | ref->node->debug_id, ref->data.strong, |
| 5366 | ref->data.weak, ref->death); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5367 | binder_node_unlock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5368 | } |
| 5369 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5370 | static void print_binder_proc(struct seq_file *m, |
| 5371 | struct binder_proc *proc, int print_all) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5372 | { |
| 5373 | struct binder_work *w; |
| 5374 | struct rb_node *n; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5375 | size_t start_pos = m->count; |
| 5376 | size_t header_pos; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5377 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5378 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5379 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5380 | seq_printf(m, "context %s\n", proc->context->name); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5381 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5382 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5383 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5384 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5385 | 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] | 5386 | rb_node), print_all); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5387 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5388 | 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] | 5389 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 5390 | rb_node); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5391 | /* |
| 5392 | * take a temporary reference on the node so it |
| 5393 | * survives and isn't removed from the tree |
| 5394 | * while we print it. |
| 5395 | */ |
| 5396 | binder_inc_node_tmpref_ilocked(node); |
| 5397 | /* Need to drop inner lock to take node lock */ |
| 5398 | binder_inner_proc_unlock(proc); |
| 5399 | if (last_node) |
| 5400 | binder_put_node(last_node); |
| 5401 | binder_node_inner_lock(node); |
| 5402 | print_binder_node_nilocked(m, node); |
| 5403 | binder_node_inner_unlock(node); |
| 5404 | last_node = node; |
| 5405 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5406 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5407 | binder_inner_proc_unlock(proc); |
| 5408 | if (last_node) |
| 5409 | binder_put_node(last_node); |
| 5410 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5411 | if (print_all) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5412 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5413 | for (n = rb_first(&proc->refs_by_desc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5414 | n != NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5415 | n = rb_next(n)) |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5416 | print_binder_ref_olocked(m, rb_entry(n, |
| 5417 | struct binder_ref, |
| 5418 | rb_node_desc)); |
| 5419 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5420 | } |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5421 | binder_alloc_print_allocated(m, &proc->alloc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5422 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5423 | list_for_each_entry(w, &proc->todo, entry) |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5424 | print_binder_work_ilocked(m, proc, " ", |
| 5425 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5426 | list_for_each_entry(w, &proc->delivered_death, entry) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5427 | seq_puts(m, " has delivered dead binder\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5428 | break; |
| 5429 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5430 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5431 | if (!print_all && m->count == header_pos) |
| 5432 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5433 | } |
| 5434 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5435 | static const char * const binder_return_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5436 | "BR_ERROR", |
| 5437 | "BR_OK", |
| 5438 | "BR_TRANSACTION", |
| 5439 | "BR_REPLY", |
| 5440 | "BR_ACQUIRE_RESULT", |
| 5441 | "BR_DEAD_REPLY", |
| 5442 | "BR_TRANSACTION_COMPLETE", |
| 5443 | "BR_INCREFS", |
| 5444 | "BR_ACQUIRE", |
| 5445 | "BR_RELEASE", |
| 5446 | "BR_DECREFS", |
| 5447 | "BR_ATTEMPT_ACQUIRE", |
| 5448 | "BR_NOOP", |
| 5449 | "BR_SPAWN_LOOPER", |
| 5450 | "BR_FINISHED", |
| 5451 | "BR_DEAD_BINDER", |
| 5452 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 5453 | "BR_FAILED_REPLY" |
| 5454 | }; |
| 5455 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5456 | static const char * const binder_command_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5457 | "BC_TRANSACTION", |
| 5458 | "BC_REPLY", |
| 5459 | "BC_ACQUIRE_RESULT", |
| 5460 | "BC_FREE_BUFFER", |
| 5461 | "BC_INCREFS", |
| 5462 | "BC_ACQUIRE", |
| 5463 | "BC_RELEASE", |
| 5464 | "BC_DECREFS", |
| 5465 | "BC_INCREFS_DONE", |
| 5466 | "BC_ACQUIRE_DONE", |
| 5467 | "BC_ATTEMPT_ACQUIRE", |
| 5468 | "BC_REGISTER_LOOPER", |
| 5469 | "BC_ENTER_LOOPER", |
| 5470 | "BC_EXIT_LOOPER", |
| 5471 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 5472 | "BC_CLEAR_DEATH_NOTIFICATION", |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 5473 | "BC_DEAD_BINDER_DONE", |
| 5474 | "BC_TRANSACTION_SG", |
| 5475 | "BC_REPLY_SG", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5476 | }; |
| 5477 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5478 | static const char * const binder_objstat_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5479 | "proc", |
| 5480 | "thread", |
| 5481 | "node", |
| 5482 | "ref", |
| 5483 | "death", |
| 5484 | "transaction", |
| 5485 | "transaction_complete" |
| 5486 | }; |
| 5487 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5488 | static void print_binder_stats(struct seq_file *m, const char *prefix, |
| 5489 | struct binder_stats *stats) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5490 | { |
| 5491 | int i; |
| 5492 | |
| 5493 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5494 | ARRAY_SIZE(binder_command_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5495 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5496 | int temp = atomic_read(&stats->bc[i]); |
| 5497 | |
| 5498 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5499 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5500 | binder_command_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5504 | ARRAY_SIZE(binder_return_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5505 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5506 | int temp = atomic_read(&stats->br[i]); |
| 5507 | |
| 5508 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5509 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5510 | binder_return_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5514 | ARRAY_SIZE(binder_objstat_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5515 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5516 | ARRAY_SIZE(stats->obj_deleted)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5517 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5518 | int created = atomic_read(&stats->obj_created[i]); |
| 5519 | int deleted = atomic_read(&stats->obj_deleted[i]); |
| 5520 | |
| 5521 | if (created || deleted) |
| 5522 | seq_printf(m, "%s%s: active %d total %d\n", |
| 5523 | prefix, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5524 | binder_objstat_strings[i], |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5525 | created - deleted, |
| 5526 | created); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5527 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5528 | } |
| 5529 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5530 | static void print_binder_proc_stats(struct seq_file *m, |
| 5531 | struct binder_proc *proc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5532 | { |
| 5533 | struct binder_work *w; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5534 | struct binder_thread *thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5535 | struct rb_node *n; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5536 | int count, strong, weak, ready_threads; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5537 | size_t free_async_space = |
| 5538 | binder_alloc_get_free_async_space(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5539 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5540 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5541 | seq_printf(m, "context %s\n", proc->context->name); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5542 | count = 0; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5543 | ready_threads = 0; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5544 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5545 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 5546 | count++; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5547 | |
| 5548 | list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node) |
| 5549 | ready_threads++; |
| 5550 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5551 | seq_printf(m, " threads: %d\n", count); |
| 5552 | seq_printf(m, " requested threads: %d+%d/%d\n" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5553 | " ready threads %d\n" |
| 5554 | " free async space %zd\n", proc->requested_threads, |
| 5555 | proc->requested_threads_started, proc->max_threads, |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5556 | ready_threads, |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5557 | free_async_space); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5558 | count = 0; |
| 5559 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) |
| 5560 | count++; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5561 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5562 | seq_printf(m, " nodes: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5563 | count = 0; |
| 5564 | strong = 0; |
| 5565 | weak = 0; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5566 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5567 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 5568 | struct binder_ref *ref = rb_entry(n, struct binder_ref, |
| 5569 | rb_node_desc); |
| 5570 | count++; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 5571 | strong += ref->data.strong; |
| 5572 | weak += ref->data.weak; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5573 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5574 | binder_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5575 | 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] | 5576 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5577 | count = binder_alloc_get_allocated_count(&proc->alloc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5578 | seq_printf(m, " buffers: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5579 | |
Sherry Yang | 8ef4665 | 2017-08-31 11:56:36 -0700 | [diff] [blame] | 5580 | binder_alloc_print_pages(m, &proc->alloc); |
| 5581 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5582 | count = 0; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5583 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5584 | list_for_each_entry(w, &proc->todo, entry) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5585 | if (w->type == BINDER_WORK_TRANSACTION) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5586 | count++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5587 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5588 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5589 | seq_printf(m, " pending transactions: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5590 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5591 | print_binder_stats(m, " ", &proc->stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5592 | } |
| 5593 | |
| 5594 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5595 | static int binder_state_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5596 | { |
| 5597 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5598 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5599 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5600 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5601 | seq_puts(m, "binder state:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5602 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5603 | spin_lock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5604 | if (!hlist_empty(&binder_dead_nodes)) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5605 | seq_puts(m, "dead nodes:\n"); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5606 | hlist_for_each_entry(node, &binder_dead_nodes, dead_node) { |
| 5607 | /* |
| 5608 | * take a temporary reference on the node so it |
| 5609 | * survives and isn't removed from the list |
| 5610 | * while we print it. |
| 5611 | */ |
| 5612 | node->tmp_refs++; |
| 5613 | spin_unlock(&binder_dead_nodes_lock); |
| 5614 | if (last_node) |
| 5615 | binder_put_node(last_node); |
| 5616 | binder_node_lock(node); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5617 | print_binder_node_nilocked(m, node); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5618 | binder_node_unlock(node); |
| 5619 | last_node = node; |
| 5620 | spin_lock(&binder_dead_nodes_lock); |
| 5621 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5622 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5623 | if (last_node) |
| 5624 | binder_put_node(last_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5625 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5626 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5627 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5628 | print_binder_proc(m, proc, 1); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5629 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 5630 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5631 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5632 | } |
| 5633 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5634 | static int binder_stats_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5635 | { |
| 5636 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5637 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5638 | seq_puts(m, "binder stats:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5639 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5640 | print_binder_stats(m, "", &binder_stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5641 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5642 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5643 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5644 | print_binder_proc_stats(m, proc); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5645 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 5646 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5647 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5648 | } |
| 5649 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5650 | static int binder_transactions_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5651 | { |
| 5652 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5653 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5654 | seq_puts(m, "binder transactions:\n"); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5655 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5656 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5657 | print_binder_proc(m, proc, 0); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5658 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 5659 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5660 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5661 | } |
| 5662 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5663 | static int binder_proc_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5664 | { |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5665 | struct binder_proc *itr; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5666 | int pid = (unsigned long)m->private; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5667 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5668 | mutex_lock(&binder_procs_lock); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5669 | hlist_for_each_entry(itr, &binder_procs, proc_node) { |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5670 | if (itr->pid == pid) { |
| 5671 | seq_puts(m, "binder proc state:\n"); |
| 5672 | print_binder_proc(m, itr, 1); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5673 | } |
| 5674 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5675 | mutex_unlock(&binder_procs_lock); |
| 5676 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5677 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5678 | } |
| 5679 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5680 | static void print_binder_transaction_log_entry(struct seq_file *m, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5681 | struct binder_transaction_log_entry *e) |
| 5682 | { |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5683 | int debug_id = READ_ONCE(e->debug_id_done); |
| 5684 | /* |
| 5685 | * read barrier to guarantee debug_id_done read before |
| 5686 | * we print the log values |
| 5687 | */ |
| 5688 | smp_rmb(); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5689 | seq_printf(m, |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5690 | "%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] | 5691 | e->debug_id, (e->call_type == 2) ? "reply" : |
| 5692 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5693 | e->from_thread, e->to_proc, e->to_thread, e->context_name, |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 5694 | e->to_node, e->target_handle, e->data_size, e->offsets_size, |
| 5695 | e->return_error, e->return_error_param, |
| 5696 | e->return_error_line); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5697 | /* |
| 5698 | * read-barrier to guarantee read of debug_id_done after |
| 5699 | * done printing the fields of the entry |
| 5700 | */ |
| 5701 | smp_rmb(); |
| 5702 | seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ? |
| 5703 | "\n" : " (incomplete)\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5704 | } |
| 5705 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5706 | 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] | 5707 | { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5708 | struct binder_transaction_log *log = m->private; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5709 | unsigned int log_cur = atomic_read(&log->cur); |
| 5710 | unsigned int count; |
| 5711 | unsigned int cur; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5712 | int i; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5713 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5714 | count = log_cur + 1; |
| 5715 | cur = count < ARRAY_SIZE(log->entry) && !log->full ? |
| 5716 | 0 : count % ARRAY_SIZE(log->entry); |
| 5717 | if (count > ARRAY_SIZE(log->entry) || log->full) |
| 5718 | count = ARRAY_SIZE(log->entry); |
| 5719 | for (i = 0; i < count; i++) { |
| 5720 | unsigned int index = cur++ % ARRAY_SIZE(log->entry); |
| 5721 | |
| 5722 | print_binder_transaction_log_entry(m, &log->entry[index]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5723 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5724 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5725 | } |
| 5726 | |
| 5727 | static const struct file_operations binder_fops = { |
| 5728 | .owner = THIS_MODULE, |
| 5729 | .poll = binder_poll, |
| 5730 | .unlocked_ioctl = binder_ioctl, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5731 | .compat_ioctl = binder_ioctl, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5732 | .mmap = binder_mmap, |
| 5733 | .open = binder_open, |
| 5734 | .flush = binder_flush, |
| 5735 | .release = binder_release, |
| 5736 | }; |
| 5737 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5738 | BINDER_DEBUG_ENTRY(state); |
| 5739 | BINDER_DEBUG_ENTRY(stats); |
| 5740 | BINDER_DEBUG_ENTRY(transactions); |
| 5741 | BINDER_DEBUG_ENTRY(transaction_log); |
| 5742 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5743 | static int __init init_binder_device(const char *name) |
| 5744 | { |
| 5745 | int ret; |
| 5746 | struct binder_device *binder_device; |
| 5747 | |
| 5748 | binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL); |
| 5749 | if (!binder_device) |
| 5750 | return -ENOMEM; |
| 5751 | |
| 5752 | binder_device->miscdev.fops = &binder_fops; |
| 5753 | binder_device->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 5754 | binder_device->miscdev.name = name; |
| 5755 | |
| 5756 | binder_device->context.binder_context_mgr_uid = INVALID_UID; |
| 5757 | binder_device->context.name = name; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5758 | mutex_init(&binder_device->context.context_mgr_node_lock); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5759 | |
| 5760 | ret = misc_register(&binder_device->miscdev); |
| 5761 | if (ret < 0) { |
| 5762 | kfree(binder_device); |
| 5763 | return ret; |
| 5764 | } |
| 5765 | |
| 5766 | hlist_add_head(&binder_device->hlist, &binder_devices); |
| 5767 | |
| 5768 | return ret; |
| 5769 | } |
| 5770 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5771 | static int __init binder_init(void) |
| 5772 | { |
| 5773 | int ret; |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 5774 | char *device_name, *device_names, *device_tmp; |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5775 | struct binder_device *device; |
| 5776 | struct hlist_node *tmp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5777 | |
Tetsuo Handa | 533dfb2 | 2017-11-29 22:29:47 +0900 | [diff] [blame] | 5778 | ret = binder_alloc_shrinker_init(); |
| 5779 | if (ret) |
| 5780 | return ret; |
Sherry Yang | f2517eb | 2017-08-23 08:46:42 -0700 | [diff] [blame] | 5781 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5782 | atomic_set(&binder_transaction_log.cur, ~0U); |
| 5783 | atomic_set(&binder_transaction_log_failed.cur, ~0U); |
| 5784 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5785 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); |
| 5786 | if (binder_debugfs_dir_entry_root) |
| 5787 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", |
| 5788 | binder_debugfs_dir_entry_root); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5789 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5790 | if (binder_debugfs_dir_entry_root) { |
| 5791 | debugfs_create_file("state", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5792 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5793 | binder_debugfs_dir_entry_root, |
| 5794 | NULL, |
| 5795 | &binder_state_fops); |
| 5796 | debugfs_create_file("stats", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5797 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5798 | binder_debugfs_dir_entry_root, |
| 5799 | NULL, |
| 5800 | &binder_stats_fops); |
| 5801 | debugfs_create_file("transactions", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5802 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5803 | binder_debugfs_dir_entry_root, |
| 5804 | NULL, |
| 5805 | &binder_transactions_fops); |
| 5806 | debugfs_create_file("transaction_log", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5807 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5808 | binder_debugfs_dir_entry_root, |
| 5809 | &binder_transaction_log, |
| 5810 | &binder_transaction_log_fops); |
| 5811 | debugfs_create_file("failed_transaction_log", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5812 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5813 | binder_debugfs_dir_entry_root, |
| 5814 | &binder_transaction_log_failed, |
| 5815 | &binder_transaction_log_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5816 | } |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5817 | |
| 5818 | /* |
| 5819 | * Copy the module_parameter string, because we don't want to |
| 5820 | * tokenize it in-place. |
| 5821 | */ |
Rasmus Villemoes | 6b6642d | 2018-09-07 10:01:46 +0200 | [diff] [blame] | 5822 | device_names = kstrdup(binder_devices_param, GFP_KERNEL); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5823 | if (!device_names) { |
| 5824 | ret = -ENOMEM; |
| 5825 | goto err_alloc_device_names_failed; |
| 5826 | } |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5827 | |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 5828 | device_tmp = device_names; |
| 5829 | while ((device_name = strsep(&device_tmp, ","))) { |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5830 | ret = init_binder_device(device_name); |
| 5831 | if (ret) |
| 5832 | goto err_init_binder_device_failed; |
| 5833 | } |
| 5834 | |
| 5835 | return ret; |
| 5836 | |
| 5837 | err_init_binder_device_failed: |
| 5838 | hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) { |
| 5839 | misc_deregister(&device->miscdev); |
| 5840 | hlist_del(&device->hlist); |
| 5841 | kfree(device); |
| 5842 | } |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 5843 | |
| 5844 | kfree(device_names); |
| 5845 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5846 | err_alloc_device_names_failed: |
| 5847 | debugfs_remove_recursive(binder_debugfs_dir_entry_root); |
| 5848 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5849 | return ret; |
| 5850 | } |
| 5851 | |
| 5852 | device_initcall(binder_init); |
| 5853 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5854 | #define CREATE_TRACE_POINTS |
| 5855 | #include "binder_trace.h" |
| 5856 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5857 | MODULE_LICENSE("GPL v2"); |