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> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 74 | |
Greg Kroah-Hartman | 9246a4a | 2014-10-16 15:26:51 +0200 | [diff] [blame] | 75 | #include <uapi/linux/android/binder.h> |
Guenter Roeck | f371a7c | 2018-07-23 14:41:38 -0700 | [diff] [blame] | 76 | |
| 77 | #include <asm/cacheflush.h> |
| 78 | |
Todd Kjos | 0c972a0 | 2017-06-29 12:01:41 -0700 | [diff] [blame] | 79 | #include "binder_alloc.h" |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 80 | #include "binder_trace.h" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 81 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 82 | static HLIST_HEAD(binder_deferred_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 83 | static DEFINE_MUTEX(binder_deferred_lock); |
| 84 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 85 | static HLIST_HEAD(binder_devices); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 86 | static HLIST_HEAD(binder_procs); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 87 | static DEFINE_MUTEX(binder_procs_lock); |
| 88 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 89 | static HLIST_HEAD(binder_dead_nodes); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 90 | static DEFINE_SPINLOCK(binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 91 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 92 | static struct dentry *binder_debugfs_dir_entry_root; |
| 93 | static struct dentry *binder_debugfs_dir_entry_proc; |
Todd Kjos | 656a800 | 2017-06-29 12:01:45 -0700 | [diff] [blame] | 94 | static atomic_t binder_last_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 95 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 96 | #define BINDER_DEBUG_ENTRY(name) \ |
| 97 | static int binder_##name##_open(struct inode *inode, struct file *file) \ |
| 98 | { \ |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 99 | return single_open(file, binder_##name##_show, inode->i_private); \ |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 100 | } \ |
| 101 | \ |
| 102 | static const struct file_operations binder_##name##_fops = { \ |
| 103 | .owner = THIS_MODULE, \ |
| 104 | .open = binder_##name##_open, \ |
| 105 | .read = seq_read, \ |
| 106 | .llseek = seq_lseek, \ |
| 107 | .release = single_release, \ |
| 108 | } |
| 109 | |
| 110 | static int binder_proc_show(struct seq_file *m, void *unused); |
| 111 | BINDER_DEBUG_ENTRY(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 112 | |
| 113 | /* This is only defined in include/asm-arm/sizes.h */ |
| 114 | #ifndef SZ_1K |
| 115 | #define SZ_1K 0x400 |
| 116 | #endif |
| 117 | |
| 118 | #ifndef SZ_4M |
| 119 | #define SZ_4M 0x400000 |
| 120 | #endif |
| 121 | |
| 122 | #define FORBIDDEN_MMAP_FLAGS (VM_WRITE) |
| 123 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 124 | enum { |
| 125 | BINDER_DEBUG_USER_ERROR = 1U << 0, |
| 126 | BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1, |
| 127 | BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2, |
| 128 | BINDER_DEBUG_OPEN_CLOSE = 1U << 3, |
| 129 | BINDER_DEBUG_DEAD_BINDER = 1U << 4, |
| 130 | BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5, |
| 131 | BINDER_DEBUG_READ_WRITE = 1U << 6, |
| 132 | BINDER_DEBUG_USER_REFS = 1U << 7, |
| 133 | BINDER_DEBUG_THREADS = 1U << 8, |
| 134 | BINDER_DEBUG_TRANSACTION = 1U << 9, |
| 135 | BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10, |
| 136 | BINDER_DEBUG_FREE_BUFFER = 1U << 11, |
| 137 | BINDER_DEBUG_INTERNAL_REFS = 1U << 12, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 138 | BINDER_DEBUG_PRIORITY_CAP = 1U << 13, |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 139 | BINDER_DEBUG_SPINLOCKS = 1U << 14, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 140 | }; |
| 141 | static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR | |
| 142 | BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION; |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 143 | module_param_named(debug_mask, binder_debug_mask, uint, 0644); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 144 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 145 | static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES; |
| 146 | module_param_named(devices, binder_devices_param, charp, 0444); |
| 147 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 148 | static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait); |
| 149 | static int binder_stop_on_user_error; |
| 150 | |
| 151 | static int binder_set_stop_on_user_error(const char *val, |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 152 | const struct kernel_param *kp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 153 | { |
| 154 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 155 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 156 | ret = param_set_int(val, kp); |
| 157 | if (binder_stop_on_user_error < 2) |
| 158 | wake_up(&binder_user_error_wait); |
| 159 | return ret; |
| 160 | } |
| 161 | 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] | 162 | param_get_int, &binder_stop_on_user_error, 0644); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 163 | |
| 164 | #define binder_debug(mask, x...) \ |
| 165 | do { \ |
| 166 | if (binder_debug_mask & mask) \ |
Sherry Yang | 128f380 | 2018-08-07 12:57:13 -0700 | [diff] [blame] | 167 | pr_info_ratelimited(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 168 | } while (0) |
| 169 | |
| 170 | #define binder_user_error(x...) \ |
| 171 | do { \ |
| 172 | if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \ |
Sherry Yang | 128f380 | 2018-08-07 12:57:13 -0700 | [diff] [blame] | 173 | pr_info_ratelimited(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 174 | if (binder_stop_on_user_error) \ |
| 175 | binder_stop_on_user_error = 2; \ |
| 176 | } while (0) |
| 177 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 178 | #define to_flat_binder_object(hdr) \ |
| 179 | container_of(hdr, struct flat_binder_object, hdr) |
| 180 | |
| 181 | #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr) |
| 182 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 183 | #define to_binder_buffer_object(hdr) \ |
| 184 | container_of(hdr, struct binder_buffer_object, hdr) |
| 185 | |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 186 | #define to_binder_fd_array_object(hdr) \ |
| 187 | container_of(hdr, struct binder_fd_array_object, hdr) |
| 188 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 189 | enum binder_stat_types { |
| 190 | BINDER_STAT_PROC, |
| 191 | BINDER_STAT_THREAD, |
| 192 | BINDER_STAT_NODE, |
| 193 | BINDER_STAT_REF, |
| 194 | BINDER_STAT_DEATH, |
| 195 | BINDER_STAT_TRANSACTION, |
| 196 | BINDER_STAT_TRANSACTION_COMPLETE, |
| 197 | BINDER_STAT_COUNT |
| 198 | }; |
| 199 | |
| 200 | struct binder_stats { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 201 | atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1]; |
| 202 | atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1]; |
| 203 | atomic_t obj_created[BINDER_STAT_COUNT]; |
| 204 | atomic_t obj_deleted[BINDER_STAT_COUNT]; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | static struct binder_stats binder_stats; |
| 208 | |
| 209 | static inline void binder_stats_deleted(enum binder_stat_types type) |
| 210 | { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 211 | atomic_inc(&binder_stats.obj_deleted[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static inline void binder_stats_created(enum binder_stat_types type) |
| 215 | { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 216 | atomic_inc(&binder_stats.obj_created[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | struct binder_transaction_log_entry { |
| 220 | int debug_id; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 221 | int debug_id_done; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 222 | int call_type; |
| 223 | int from_proc; |
| 224 | int from_thread; |
| 225 | int target_handle; |
| 226 | int to_proc; |
| 227 | int to_thread; |
| 228 | int to_node; |
| 229 | int data_size; |
| 230 | int offsets_size; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 231 | int return_error_line; |
| 232 | uint32_t return_error; |
| 233 | uint32_t return_error_param; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 234 | const char *context_name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 235 | }; |
| 236 | struct binder_transaction_log { |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 237 | atomic_t cur; |
| 238 | bool full; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 239 | struct binder_transaction_log_entry entry[32]; |
| 240 | }; |
| 241 | static struct binder_transaction_log binder_transaction_log; |
| 242 | static struct binder_transaction_log binder_transaction_log_failed; |
| 243 | |
| 244 | static struct binder_transaction_log_entry *binder_transaction_log_add( |
| 245 | struct binder_transaction_log *log) |
| 246 | { |
| 247 | struct binder_transaction_log_entry *e; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 248 | unsigned int cur = atomic_inc_return(&log->cur); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 249 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 250 | if (cur >= ARRAY_SIZE(log->entry)) |
Gustavo A. R. Silva | 197410a | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 251 | log->full = true; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 252 | e = &log->entry[cur % ARRAY_SIZE(log->entry)]; |
| 253 | WRITE_ONCE(e->debug_id_done, 0); |
| 254 | /* |
| 255 | * write-barrier to synchronize access to e->debug_id_done. |
| 256 | * We make sure the initialized 0 value is seen before |
| 257 | * memset() other fields are zeroed by memset. |
| 258 | */ |
| 259 | smp_wmb(); |
| 260 | memset(e, 0, sizeof(*e)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 261 | return e; |
| 262 | } |
| 263 | |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 264 | struct binder_context { |
| 265 | struct binder_node *binder_context_mgr_node; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 266 | struct mutex context_mgr_node_lock; |
| 267 | |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 268 | kuid_t binder_context_mgr_uid; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 269 | const char *name; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 270 | }; |
| 271 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 272 | struct binder_device { |
| 273 | struct hlist_node hlist; |
| 274 | struct miscdevice miscdev; |
| 275 | struct binder_context context; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 276 | }; |
| 277 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 278 | /** |
| 279 | * struct binder_work - work enqueued on a worklist |
| 280 | * @entry: node enqueued on list |
| 281 | * @type: type of work to be performed |
| 282 | * |
| 283 | * There are separate work lists for proc, thread, and node (async). |
| 284 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 285 | struct binder_work { |
| 286 | struct list_head entry; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 287 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 288 | enum { |
| 289 | BINDER_WORK_TRANSACTION = 1, |
| 290 | BINDER_WORK_TRANSACTION_COMPLETE, |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 291 | BINDER_WORK_RETURN_ERROR, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 292 | BINDER_WORK_NODE, |
| 293 | BINDER_WORK_DEAD_BINDER, |
| 294 | BINDER_WORK_DEAD_BINDER_AND_CLEAR, |
| 295 | BINDER_WORK_CLEAR_DEATH_NOTIFICATION, |
| 296 | } type; |
| 297 | }; |
| 298 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 299 | struct binder_error { |
| 300 | struct binder_work work; |
| 301 | uint32_t cmd; |
| 302 | }; |
| 303 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 304 | /** |
| 305 | * struct binder_node - binder node bookkeeping |
| 306 | * @debug_id: unique ID for debugging |
| 307 | * (invariant after initialized) |
| 308 | * @lock: lock for node fields |
| 309 | * @work: worklist element for node work |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 310 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 311 | * @rb_node: element for proc->nodes tree |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 312 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 313 | * @dead_node: element for binder_dead_nodes list |
| 314 | * (protected by binder_dead_nodes_lock) |
| 315 | * @proc: binder_proc that owns this node |
| 316 | * (invariant after initialized) |
| 317 | * @refs: list of references on this node |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 318 | * (protected by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 319 | * @internal_strong_refs: used to take strong references when |
| 320 | * initiating a transaction |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 321 | * (protected by @proc->inner_lock if @proc |
| 322 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 323 | * @local_weak_refs: weak user refs from local process |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 324 | * (protected by @proc->inner_lock if @proc |
| 325 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 326 | * @local_strong_refs: strong user refs from local process |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 327 | * (protected by @proc->inner_lock if @proc |
| 328 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 329 | * @tmp_refs: temporary kernel refs |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 330 | * (protected by @proc->inner_lock while @proc |
| 331 | * is valid, and by binder_dead_nodes_lock |
| 332 | * if @proc is NULL. During inc/dec and node release |
| 333 | * it is also protected by @lock to provide safety |
| 334 | * as the node dies and @proc becomes NULL) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 335 | * @ptr: userspace pointer for node |
| 336 | * (invariant, no lock needed) |
| 337 | * @cookie: userspace cookie for node |
| 338 | * (invariant, no lock needed) |
| 339 | * @has_strong_ref: userspace notified of strong ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 340 | * (protected by @proc->inner_lock if @proc |
| 341 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 342 | * @pending_strong_ref: userspace has acked notification of strong ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 343 | * (protected by @proc->inner_lock if @proc |
| 344 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 345 | * @has_weak_ref: userspace notified of weak ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 346 | * (protected by @proc->inner_lock if @proc |
| 347 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 348 | * @pending_weak_ref: userspace has acked notification of weak ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 349 | * (protected by @proc->inner_lock if @proc |
| 350 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 351 | * @has_async_transaction: async transaction to node in progress |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 352 | * (protected by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 353 | * @accept_fds: file descriptor operations supported for node |
| 354 | * (invariant after initialized) |
| 355 | * @min_priority: minimum scheduling priority |
| 356 | * (invariant after initialized) |
| 357 | * @async_todo: list of async work items |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 358 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 359 | * |
| 360 | * Bookkeeping structure for binder nodes. |
| 361 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 362 | struct binder_node { |
| 363 | int debug_id; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 364 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 365 | struct binder_work work; |
| 366 | union { |
| 367 | struct rb_node rb_node; |
| 368 | struct hlist_node dead_node; |
| 369 | }; |
| 370 | struct binder_proc *proc; |
| 371 | struct hlist_head refs; |
| 372 | int internal_strong_refs; |
| 373 | int local_weak_refs; |
| 374 | int local_strong_refs; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 375 | int tmp_refs; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 376 | binder_uintptr_t ptr; |
| 377 | binder_uintptr_t cookie; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 378 | struct { |
| 379 | /* |
| 380 | * bitfield elements protected by |
| 381 | * proc inner_lock |
| 382 | */ |
| 383 | u8 has_strong_ref:1; |
| 384 | u8 pending_strong_ref:1; |
| 385 | u8 has_weak_ref:1; |
| 386 | u8 pending_weak_ref:1; |
| 387 | }; |
| 388 | struct { |
| 389 | /* |
| 390 | * invariant after initialization |
| 391 | */ |
| 392 | u8 accept_fds:1; |
| 393 | u8 min_priority; |
| 394 | }; |
| 395 | bool has_async_transaction; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 396 | struct list_head async_todo; |
| 397 | }; |
| 398 | |
| 399 | struct binder_ref_death { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 400 | /** |
| 401 | * @work: worklist element for death notifications |
| 402 | * (protected by inner_lock of the proc that |
| 403 | * this ref belongs to) |
| 404 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 405 | struct binder_work work; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 406 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 407 | }; |
| 408 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 409 | /** |
| 410 | * struct binder_ref_data - binder_ref counts and id |
| 411 | * @debug_id: unique ID for the ref |
| 412 | * @desc: unique userspace handle for ref |
| 413 | * @strong: strong ref count (debugging only if not locked) |
| 414 | * @weak: weak ref count (debugging only if not locked) |
| 415 | * |
| 416 | * Structure to hold ref count and ref id information. Since |
| 417 | * the actual ref can only be accessed with a lock, this structure |
| 418 | * is used to return information about the ref to callers of |
| 419 | * ref inc/dec functions. |
| 420 | */ |
| 421 | struct binder_ref_data { |
| 422 | int debug_id; |
| 423 | uint32_t desc; |
| 424 | int strong; |
| 425 | int weak; |
| 426 | }; |
| 427 | |
| 428 | /** |
| 429 | * struct binder_ref - struct to track references on nodes |
| 430 | * @data: binder_ref_data containing id, handle, and current refcounts |
| 431 | * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree |
| 432 | * @rb_node_node: node for lookup by @node in proc's rb_tree |
| 433 | * @node_entry: list entry for node->refs list in target node |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 434 | * (protected by @node->lock) |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 435 | * @proc: binder_proc containing ref |
| 436 | * @node: binder_node of target node. When cleaning up a |
| 437 | * ref for deletion in binder_cleanup_ref, a non-NULL |
| 438 | * @node indicates the node must be freed |
| 439 | * @death: pointer to death notification (ref_death) if requested |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 440 | * (protected by @node->lock) |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 441 | * |
| 442 | * Structure to track references from procA to target node (on procB). This |
| 443 | * structure is unsafe to access without holding @proc->outer_lock. |
| 444 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 445 | struct binder_ref { |
| 446 | /* Lookups needed: */ |
| 447 | /* node + proc => ref (transaction) */ |
| 448 | /* desc + proc => ref (transaction, inc/dec ref) */ |
| 449 | /* node => refs + procs (proc exit) */ |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 450 | struct binder_ref_data data; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 451 | struct rb_node rb_node_desc; |
| 452 | struct rb_node rb_node_node; |
| 453 | struct hlist_node node_entry; |
| 454 | struct binder_proc *proc; |
| 455 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 456 | struct binder_ref_death *death; |
| 457 | }; |
| 458 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 459 | enum binder_deferred_state { |
| 460 | BINDER_DEFERRED_PUT_FILES = 0x01, |
| 461 | BINDER_DEFERRED_FLUSH = 0x02, |
| 462 | BINDER_DEFERRED_RELEASE = 0x04, |
| 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) |
| 483 | * @files files_struct for process |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 484 | * (protected by @files_lock) |
| 485 | * @files_lock mutex to protect @files |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 486 | * @deferred_work_node: element for binder_deferred_list |
| 487 | * (protected by binder_deferred_lock) |
| 488 | * @deferred_work: bitmap of deferred work to perform |
| 489 | * (protected by binder_deferred_lock) |
| 490 | * @is_dead: process is dead and awaiting free |
| 491 | * when outstanding transactions are cleaned up |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 492 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 493 | * @todo: list of work for this process |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 494 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 495 | * @stats: per-process binder statistics |
| 496 | * (atomics, no lock needed) |
| 497 | * @delivered_death: list of delivered death notification |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 498 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 499 | * @max_threads: cap on number of binder threads |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 500 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 501 | * @requested_threads: number of binder threads requested but not |
| 502 | * yet started. In current implementation, can |
| 503 | * only be 0 or 1. |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 504 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 505 | * @requested_threads_started: number binder threads started |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 506 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 507 | * @tmp_ref: temporary reference to indicate proc is in use |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 508 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 509 | * @default_priority: default scheduler priority |
| 510 | * (invariant after initialized) |
| 511 | * @debugfs_entry: debugfs node |
| 512 | * @alloc: binder allocator bookkeeping |
| 513 | * @context: binder_context for this proc |
| 514 | * (invariant after initialized) |
| 515 | * @inner_lock: can nest under outer_lock and/or node lock |
| 516 | * @outer_lock: no nesting under innor or node lock |
| 517 | * Lock order: 1) outer, 2) node, 3) inner |
| 518 | * |
| 519 | * Bookkeeping structure for binder processes |
| 520 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 521 | struct binder_proc { |
| 522 | struct hlist_node proc_node; |
| 523 | struct rb_root threads; |
| 524 | struct rb_root nodes; |
| 525 | struct rb_root refs_by_desc; |
| 526 | struct rb_root refs_by_node; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 527 | struct list_head waiting_threads; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 528 | int pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 529 | struct task_struct *tsk; |
| 530 | struct files_struct *files; |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 531 | struct mutex files_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 532 | struct hlist_node deferred_work_node; |
| 533 | int deferred_work; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 534 | bool is_dead; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 535 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 536 | struct list_head todo; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 537 | struct binder_stats stats; |
| 538 | struct list_head delivered_death; |
| 539 | int max_threads; |
| 540 | int requested_threads; |
| 541 | int requested_threads_started; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 542 | int tmp_ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 543 | long default_priority; |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 544 | struct dentry *debugfs_entry; |
Todd Kjos | fdfb4a9 | 2017-06-29 12:01:38 -0700 | [diff] [blame] | 545 | struct binder_alloc alloc; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 546 | struct binder_context *context; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 547 | spinlock_t inner_lock; |
| 548 | spinlock_t outer_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 549 | }; |
| 550 | |
| 551 | enum { |
| 552 | BINDER_LOOPER_STATE_REGISTERED = 0x01, |
| 553 | BINDER_LOOPER_STATE_ENTERED = 0x02, |
| 554 | BINDER_LOOPER_STATE_EXITED = 0x04, |
| 555 | BINDER_LOOPER_STATE_INVALID = 0x08, |
| 556 | BINDER_LOOPER_STATE_WAITING = 0x10, |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 557 | BINDER_LOOPER_STATE_POLL = 0x20, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 558 | }; |
| 559 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 560 | /** |
| 561 | * struct binder_thread - binder thread bookkeeping |
| 562 | * @proc: binder process for this thread |
| 563 | * (invariant after initialization) |
| 564 | * @rb_node: element for proc->threads rbtree |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 565 | * (protected by @proc->inner_lock) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 566 | * @waiting_thread_node: element for @proc->waiting_threads list |
| 567 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 568 | * @pid: PID for this thread |
| 569 | * (invariant after initialization) |
| 570 | * @looper: bitmap of looping state |
| 571 | * (only accessed by this thread) |
| 572 | * @looper_needs_return: looping thread needs to exit driver |
| 573 | * (no lock needed) |
| 574 | * @transaction_stack: stack of in-progress transactions for this thread |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 575 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 576 | * @todo: list of work to do for this thread |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 577 | * (protected by @proc->inner_lock) |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 578 | * @process_todo: whether work in @todo should be processed |
| 579 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 580 | * @return_error: transaction errors reported by this thread |
| 581 | * (only accessed by this thread) |
| 582 | * @reply_error: transaction errors reported by target thread |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 583 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 584 | * @wait: wait queue for thread work |
| 585 | * @stats: per-thread statistics |
| 586 | * (atomics, no lock needed) |
| 587 | * @tmp_ref: temporary reference to indicate thread is in use |
| 588 | * (atomic since @proc->inner_lock cannot |
| 589 | * always be acquired) |
| 590 | * @is_dead: thread is dead and awaiting free |
| 591 | * when outstanding transactions are cleaned up |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 592 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 593 | * |
| 594 | * Bookkeeping structure for binder threads. |
| 595 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 596 | struct binder_thread { |
| 597 | struct binder_proc *proc; |
| 598 | struct rb_node rb_node; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 599 | struct list_head waiting_thread_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 600 | int pid; |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 601 | int looper; /* only modified by this thread */ |
| 602 | bool looper_need_return; /* can be written by other thread */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 603 | struct binder_transaction *transaction_stack; |
| 604 | struct list_head todo; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 605 | bool process_todo; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 606 | struct binder_error return_error; |
| 607 | struct binder_error reply_error; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 608 | wait_queue_head_t wait; |
| 609 | struct binder_stats stats; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 610 | atomic_t tmp_ref; |
| 611 | bool is_dead; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 612 | }; |
| 613 | |
| 614 | struct binder_transaction { |
| 615 | int debug_id; |
| 616 | struct binder_work work; |
| 617 | struct binder_thread *from; |
| 618 | struct binder_transaction *from_parent; |
| 619 | struct binder_proc *to_proc; |
| 620 | struct binder_thread *to_thread; |
| 621 | struct binder_transaction *to_parent; |
| 622 | unsigned need_reply:1; |
| 623 | /* unsigned is_dead:1; */ /* not used at the moment */ |
| 624 | |
| 625 | struct binder_buffer *buffer; |
| 626 | unsigned int code; |
| 627 | unsigned int flags; |
| 628 | long priority; |
| 629 | long saved_priority; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 630 | kuid_t sender_euid; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 631 | /** |
| 632 | * @lock: protects @from, @to_proc, and @to_thread |
| 633 | * |
| 634 | * @from, @to_proc, and @to_thread can be set to NULL |
| 635 | * during thread teardown |
| 636 | */ |
| 637 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 638 | }; |
| 639 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 640 | /** |
| 641 | * binder_proc_lock() - Acquire outer lock for given binder_proc |
| 642 | * @proc: struct binder_proc to acquire |
| 643 | * |
| 644 | * Acquires proc->outer_lock. Used to protect binder_ref |
| 645 | * structures associated with the given proc. |
| 646 | */ |
| 647 | #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__) |
| 648 | static void |
| 649 | _binder_proc_lock(struct binder_proc *proc, int line) |
| 650 | { |
| 651 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 652 | "%s: line=%d\n", __func__, line); |
| 653 | spin_lock(&proc->outer_lock); |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * binder_proc_unlock() - Release spinlock for given binder_proc |
| 658 | * @proc: struct binder_proc to acquire |
| 659 | * |
| 660 | * Release lock acquired via binder_proc_lock() |
| 661 | */ |
| 662 | #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__) |
| 663 | static void |
| 664 | _binder_proc_unlock(struct binder_proc *proc, int line) |
| 665 | { |
| 666 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 667 | "%s: line=%d\n", __func__, line); |
| 668 | spin_unlock(&proc->outer_lock); |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * binder_inner_proc_lock() - Acquire inner lock for given binder_proc |
| 673 | * @proc: struct binder_proc to acquire |
| 674 | * |
| 675 | * Acquires proc->inner_lock. Used to protect todo lists |
| 676 | */ |
| 677 | #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__) |
| 678 | static void |
| 679 | _binder_inner_proc_lock(struct binder_proc *proc, int line) |
| 680 | { |
| 681 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 682 | "%s: line=%d\n", __func__, line); |
| 683 | spin_lock(&proc->inner_lock); |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * binder_inner_proc_unlock() - Release inner lock for given binder_proc |
| 688 | * @proc: struct binder_proc to acquire |
| 689 | * |
| 690 | * Release lock acquired via binder_inner_proc_lock() |
| 691 | */ |
| 692 | #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__) |
| 693 | static void |
| 694 | _binder_inner_proc_unlock(struct binder_proc *proc, int line) |
| 695 | { |
| 696 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 697 | "%s: line=%d\n", __func__, line); |
| 698 | spin_unlock(&proc->inner_lock); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * binder_node_lock() - Acquire spinlock for given binder_node |
| 703 | * @node: struct binder_node to acquire |
| 704 | * |
| 705 | * Acquires node->lock. Used to protect binder_node fields |
| 706 | */ |
| 707 | #define binder_node_lock(node) _binder_node_lock(node, __LINE__) |
| 708 | static void |
| 709 | _binder_node_lock(struct binder_node *node, int line) |
| 710 | { |
| 711 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 712 | "%s: line=%d\n", __func__, line); |
| 713 | spin_lock(&node->lock); |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * binder_node_unlock() - Release spinlock for given binder_proc |
| 718 | * @node: struct binder_node to acquire |
| 719 | * |
| 720 | * Release lock acquired via binder_node_lock() |
| 721 | */ |
| 722 | #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__) |
| 723 | static void |
| 724 | _binder_node_unlock(struct binder_node *node, int line) |
| 725 | { |
| 726 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 727 | "%s: line=%d\n", __func__, line); |
| 728 | spin_unlock(&node->lock); |
| 729 | } |
| 730 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 731 | /** |
| 732 | * binder_node_inner_lock() - Acquire node and inner locks |
| 733 | * @node: struct binder_node to acquire |
| 734 | * |
| 735 | * Acquires node->lock. If node->proc also acquires |
| 736 | * proc->inner_lock. Used to protect binder_node fields |
| 737 | */ |
| 738 | #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__) |
| 739 | static void |
| 740 | _binder_node_inner_lock(struct binder_node *node, int line) |
| 741 | { |
| 742 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 743 | "%s: line=%d\n", __func__, line); |
| 744 | spin_lock(&node->lock); |
| 745 | if (node->proc) |
| 746 | binder_inner_proc_lock(node->proc); |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * binder_node_unlock() - Release node and inner locks |
| 751 | * @node: struct binder_node to acquire |
| 752 | * |
| 753 | * Release lock acquired via binder_node_lock() |
| 754 | */ |
| 755 | #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__) |
| 756 | static void |
| 757 | _binder_node_inner_unlock(struct binder_node *node, int line) |
| 758 | { |
| 759 | struct binder_proc *proc = node->proc; |
| 760 | |
| 761 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 762 | "%s: line=%d\n", __func__, line); |
| 763 | if (proc) |
| 764 | binder_inner_proc_unlock(proc); |
| 765 | spin_unlock(&node->lock); |
| 766 | } |
| 767 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 768 | static bool binder_worklist_empty_ilocked(struct list_head *list) |
| 769 | { |
| 770 | return list_empty(list); |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * binder_worklist_empty() - Check if no items on the work list |
| 775 | * @proc: binder_proc associated with list |
| 776 | * @list: list to check |
| 777 | * |
| 778 | * Return: true if there are no items on list, else false |
| 779 | */ |
| 780 | static bool binder_worklist_empty(struct binder_proc *proc, |
| 781 | struct list_head *list) |
| 782 | { |
| 783 | bool ret; |
| 784 | |
| 785 | binder_inner_proc_lock(proc); |
| 786 | ret = binder_worklist_empty_ilocked(list); |
| 787 | binder_inner_proc_unlock(proc); |
| 788 | return ret; |
| 789 | } |
| 790 | |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 791 | /** |
| 792 | * binder_enqueue_work_ilocked() - Add an item to the work list |
| 793 | * @work: struct binder_work to add to list |
| 794 | * @target_list: list to add work to |
| 795 | * |
| 796 | * Adds the work to the specified list. Asserts that work |
| 797 | * is not already on a list. |
| 798 | * |
| 799 | * Requires the proc->inner_lock to be held. |
| 800 | */ |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 801 | static void |
| 802 | binder_enqueue_work_ilocked(struct binder_work *work, |
| 803 | struct list_head *target_list) |
| 804 | { |
| 805 | BUG_ON(target_list == NULL); |
| 806 | BUG_ON(work->entry.next && !list_empty(&work->entry)); |
| 807 | list_add_tail(&work->entry, target_list); |
| 808 | } |
| 809 | |
| 810 | /** |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 811 | * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work |
| 812 | * @thread: thread to queue work to |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 813 | * @work: struct binder_work to add to list |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 814 | * |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 815 | * Adds the work to the todo list of the thread. Doesn't set the process_todo |
| 816 | * flag, which means that (if it wasn't already set) the thread will go to |
| 817 | * sleep without handling this work when it calls read. |
| 818 | * |
| 819 | * Requires the proc->inner_lock to be held. |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 820 | */ |
| 821 | static void |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 822 | binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread, |
| 823 | struct binder_work *work) |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 824 | { |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame^] | 825 | WARN_ON(!list_empty(&thread->waiting_thread_node)); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 826 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list |
| 831 | * @thread: thread to queue work to |
| 832 | * @work: struct binder_work to add to list |
| 833 | * |
| 834 | * Adds the work to the todo list of the thread, and enables processing |
| 835 | * of the todo queue. |
| 836 | * |
| 837 | * Requires the proc->inner_lock to be held. |
| 838 | */ |
| 839 | static void |
| 840 | binder_enqueue_thread_work_ilocked(struct binder_thread *thread, |
| 841 | struct binder_work *work) |
| 842 | { |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame^] | 843 | WARN_ON(!list_empty(&thread->waiting_thread_node)); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 844 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 845 | thread->process_todo = true; |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * binder_enqueue_thread_work() - Add an item to the thread work list |
| 850 | * @thread: thread to queue work to |
| 851 | * @work: struct binder_work to add to list |
| 852 | * |
| 853 | * Adds the work to the todo list of the thread, and enables processing |
| 854 | * of the todo queue. |
| 855 | */ |
| 856 | static void |
| 857 | binder_enqueue_thread_work(struct binder_thread *thread, |
| 858 | struct binder_work *work) |
| 859 | { |
| 860 | binder_inner_proc_lock(thread->proc); |
| 861 | binder_enqueue_thread_work_ilocked(thread, work); |
| 862 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static void |
| 866 | binder_dequeue_work_ilocked(struct binder_work *work) |
| 867 | { |
| 868 | list_del_init(&work->entry); |
| 869 | } |
| 870 | |
| 871 | /** |
| 872 | * binder_dequeue_work() - Removes an item from the work list |
| 873 | * @proc: binder_proc associated with list |
| 874 | * @work: struct binder_work to remove from list |
| 875 | * |
| 876 | * Removes the specified work item from whatever list it is on. |
| 877 | * Can safely be called if work is not on any list. |
| 878 | */ |
| 879 | static void |
| 880 | binder_dequeue_work(struct binder_proc *proc, struct binder_work *work) |
| 881 | { |
| 882 | binder_inner_proc_lock(proc); |
| 883 | binder_dequeue_work_ilocked(work); |
| 884 | binder_inner_proc_unlock(proc); |
| 885 | } |
| 886 | |
| 887 | static struct binder_work *binder_dequeue_work_head_ilocked( |
| 888 | struct list_head *list) |
| 889 | { |
| 890 | struct binder_work *w; |
| 891 | |
| 892 | w = list_first_entry_or_null(list, struct binder_work, entry); |
| 893 | if (w) |
| 894 | list_del_init(&w->entry); |
| 895 | return w; |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * binder_dequeue_work_head() - Dequeues the item at head of list |
| 900 | * @proc: binder_proc associated with list |
| 901 | * @list: list to dequeue head |
| 902 | * |
| 903 | * Removes the head of the list if there are items on the list |
| 904 | * |
| 905 | * Return: pointer dequeued binder_work, NULL if list was empty |
| 906 | */ |
| 907 | static struct binder_work *binder_dequeue_work_head( |
| 908 | struct binder_proc *proc, |
| 909 | struct list_head *list) |
| 910 | { |
| 911 | struct binder_work *w; |
| 912 | |
| 913 | binder_inner_proc_lock(proc); |
| 914 | w = binder_dequeue_work_head_ilocked(list); |
| 915 | binder_inner_proc_unlock(proc); |
| 916 | return w; |
| 917 | } |
| 918 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 919 | static void |
| 920 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 921 | static void binder_free_thread(struct binder_thread *thread); |
| 922 | static void binder_free_proc(struct binder_proc *proc); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 923 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 924 | |
Sachin Kamat | efde99c | 2012-08-17 16:39:36 +0530 | [diff] [blame] | 925 | static int task_get_unused_fd_flags(struct binder_proc *proc, int flags) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 926 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 927 | unsigned long rlim_cur; |
| 928 | unsigned long irqs; |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 929 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 930 | |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 931 | mutex_lock(&proc->files_lock); |
| 932 | if (proc->files == NULL) { |
| 933 | ret = -ESRCH; |
| 934 | goto err; |
| 935 | } |
| 936 | if (!lock_task_sighand(proc->tsk, &irqs)) { |
| 937 | ret = -EMFILE; |
| 938 | goto err; |
| 939 | } |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 940 | rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE); |
| 941 | unlock_task_sighand(proc->tsk, &irqs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 942 | |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 943 | ret = __alloc_fd(proc->files, 0, rlim_cur, flags); |
| 944 | err: |
| 945 | mutex_unlock(&proc->files_lock); |
| 946 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /* |
| 950 | * copied from fd_install |
| 951 | */ |
| 952 | static void task_fd_install( |
| 953 | struct binder_proc *proc, unsigned int fd, struct file *file) |
| 954 | { |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 955 | mutex_lock(&proc->files_lock); |
Al Viro | f869e8a | 2012-08-15 21:06:33 -0400 | [diff] [blame] | 956 | if (proc->files) |
| 957 | __fd_install(proc->files, fd, file); |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 958 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /* |
| 962 | * copied from sys_close |
| 963 | */ |
| 964 | static long task_close_fd(struct binder_proc *proc, unsigned int fd) |
| 965 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 966 | int retval; |
| 967 | |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 968 | mutex_lock(&proc->files_lock); |
| 969 | if (proc->files == NULL) { |
| 970 | retval = -ESRCH; |
| 971 | goto err; |
| 972 | } |
Al Viro | 483ce1d | 2012-08-19 12:04:24 -0400 | [diff] [blame] | 973 | retval = __close_fd(proc->files, fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 974 | /* can't restart close syscall because file table entry was cleared */ |
| 975 | if (unlikely(retval == -ERESTARTSYS || |
| 976 | retval == -ERESTARTNOINTR || |
| 977 | retval == -ERESTARTNOHAND || |
| 978 | retval == -ERESTART_RESTARTBLOCK)) |
| 979 | retval = -EINTR; |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 980 | err: |
| 981 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 982 | return retval; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 983 | } |
| 984 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 985 | static bool binder_has_work_ilocked(struct binder_thread *thread, |
| 986 | bool do_proc_work) |
| 987 | { |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 988 | return thread->process_todo || |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 989 | thread->looper_need_return || |
| 990 | (do_proc_work && |
| 991 | !binder_worklist_empty_ilocked(&thread->proc->todo)); |
| 992 | } |
| 993 | |
| 994 | static bool binder_has_work(struct binder_thread *thread, bool do_proc_work) |
| 995 | { |
| 996 | bool has_work; |
| 997 | |
| 998 | binder_inner_proc_lock(thread->proc); |
| 999 | has_work = binder_has_work_ilocked(thread, do_proc_work); |
| 1000 | binder_inner_proc_unlock(thread->proc); |
| 1001 | |
| 1002 | return has_work; |
| 1003 | } |
| 1004 | |
| 1005 | static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread) |
| 1006 | { |
| 1007 | return !thread->transaction_stack && |
| 1008 | binder_worklist_empty_ilocked(&thread->todo) && |
| 1009 | (thread->looper & (BINDER_LOOPER_STATE_ENTERED | |
| 1010 | BINDER_LOOPER_STATE_REGISTERED)); |
| 1011 | } |
| 1012 | |
| 1013 | static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc, |
| 1014 | bool sync) |
| 1015 | { |
| 1016 | struct rb_node *n; |
| 1017 | struct binder_thread *thread; |
| 1018 | |
| 1019 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 1020 | thread = rb_entry(n, struct binder_thread, rb_node); |
| 1021 | if (thread->looper & BINDER_LOOPER_STATE_POLL && |
| 1022 | binder_available_for_proc_work_ilocked(thread)) { |
| 1023 | if (sync) |
| 1024 | wake_up_interruptible_sync(&thread->wait); |
| 1025 | else |
| 1026 | wake_up_interruptible(&thread->wait); |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1031 | /** |
| 1032 | * binder_select_thread_ilocked() - selects a thread for doing proc work. |
| 1033 | * @proc: process to select a thread from |
| 1034 | * |
| 1035 | * Note that calling this function moves the thread off the waiting_threads |
| 1036 | * list, so it can only be woken up by the caller of this function, or a |
| 1037 | * signal. Therefore, callers *should* always wake up the thread this function |
| 1038 | * returns. |
| 1039 | * |
| 1040 | * Return: If there's a thread currently waiting for process work, |
| 1041 | * returns that thread. Otherwise returns NULL. |
| 1042 | */ |
| 1043 | static struct binder_thread * |
| 1044 | binder_select_thread_ilocked(struct binder_proc *proc) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1045 | { |
| 1046 | struct binder_thread *thread; |
| 1047 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1048 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1049 | thread = list_first_entry_or_null(&proc->waiting_threads, |
| 1050 | struct binder_thread, |
| 1051 | waiting_thread_node); |
| 1052 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1053 | if (thread) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1054 | list_del_init(&thread->waiting_thread_node); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1055 | |
| 1056 | return thread; |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work. |
| 1061 | * @proc: process to wake up a thread in |
| 1062 | * @thread: specific thread to wake-up (may be NULL) |
| 1063 | * @sync: whether to do a synchronous wake-up |
| 1064 | * |
| 1065 | * This function wakes up a thread in the @proc process. |
| 1066 | * The caller may provide a specific thread to wake-up in |
| 1067 | * the @thread parameter. If @thread is NULL, this function |
| 1068 | * will wake up threads that have called poll(). |
| 1069 | * |
| 1070 | * Note that for this function to work as expected, callers |
| 1071 | * should first call binder_select_thread() to find a thread |
| 1072 | * to handle the work (if they don't have a thread already), |
| 1073 | * and pass the result into the @thread parameter. |
| 1074 | */ |
| 1075 | static void binder_wakeup_thread_ilocked(struct binder_proc *proc, |
| 1076 | struct binder_thread *thread, |
| 1077 | bool sync) |
| 1078 | { |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1079 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1080 | |
| 1081 | if (thread) { |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1082 | if (sync) |
| 1083 | wake_up_interruptible_sync(&thread->wait); |
| 1084 | else |
| 1085 | wake_up_interruptible(&thread->wait); |
| 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | /* Didn't find a thread waiting for proc work; this can happen |
| 1090 | * in two scenarios: |
| 1091 | * 1. All threads are busy handling transactions |
| 1092 | * In that case, one of those threads should call back into |
| 1093 | * the kernel driver soon and pick up this work. |
| 1094 | * 2. Threads are using the (e)poll interface, in which case |
| 1095 | * they may be blocked on the waitqueue without having been |
| 1096 | * added to waiting_threads. For this case, we just iterate |
| 1097 | * over all threads not handling transaction work, and |
| 1098 | * wake them all up. We wake all because we don't know whether |
| 1099 | * a thread that called into (e)poll is handling non-binder |
| 1100 | * work currently. |
| 1101 | */ |
| 1102 | binder_wakeup_poll_threads_ilocked(proc, sync); |
| 1103 | } |
| 1104 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1105 | static void binder_wakeup_proc_ilocked(struct binder_proc *proc) |
| 1106 | { |
| 1107 | struct binder_thread *thread = binder_select_thread_ilocked(proc); |
| 1108 | |
| 1109 | binder_wakeup_thread_ilocked(proc, thread, /* sync = */false); |
| 1110 | } |
| 1111 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1112 | static void binder_set_nice(long nice) |
| 1113 | { |
| 1114 | long min_nice; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1115 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1116 | if (can_nice(current, nice)) { |
| 1117 | set_user_nice(current, nice); |
| 1118 | return; |
| 1119 | } |
Krzysztof Opasiak | c3643b6 | 2017-07-05 19:24:44 +0200 | [diff] [blame] | 1120 | min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1121 | binder_debug(BINDER_DEBUG_PRIORITY_CAP, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1122 | "%d: nice value %ld not allowed use %ld instead\n", |
| 1123 | current->pid, nice, min_nice); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1124 | set_user_nice(current, min_nice); |
Dongsheng Yang | 8698a74 | 2014-03-11 18:09:12 +0800 | [diff] [blame] | 1125 | if (min_nice <= MAX_NICE) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1126 | return; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1127 | binder_user_error("%d RLIMIT_NICE not set\n", current->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1128 | } |
| 1129 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1130 | static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc, |
| 1131 | binder_uintptr_t ptr) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1132 | { |
| 1133 | struct rb_node *n = proc->nodes.rb_node; |
| 1134 | struct binder_node *node; |
| 1135 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1136 | assert_spin_locked(&proc->inner_lock); |
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 | while (n) { |
| 1139 | node = rb_entry(n, struct binder_node, rb_node); |
| 1140 | |
| 1141 | if (ptr < node->ptr) |
| 1142 | n = n->rb_left; |
| 1143 | else if (ptr > node->ptr) |
| 1144 | n = n->rb_right; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1145 | else { |
| 1146 | /* |
| 1147 | * take an implicit weak reference |
| 1148 | * to ensure node stays alive until |
| 1149 | * call to binder_put_node() |
| 1150 | */ |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1151 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1152 | return node; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1153 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1154 | } |
| 1155 | return NULL; |
| 1156 | } |
| 1157 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1158 | static struct binder_node *binder_get_node(struct binder_proc *proc, |
| 1159 | binder_uintptr_t ptr) |
| 1160 | { |
| 1161 | struct binder_node *node; |
| 1162 | |
| 1163 | binder_inner_proc_lock(proc); |
| 1164 | node = binder_get_node_ilocked(proc, ptr); |
| 1165 | binder_inner_proc_unlock(proc); |
| 1166 | return node; |
| 1167 | } |
| 1168 | |
| 1169 | static struct binder_node *binder_init_node_ilocked( |
| 1170 | struct binder_proc *proc, |
| 1171 | struct binder_node *new_node, |
| 1172 | struct flat_binder_object *fp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1173 | { |
| 1174 | struct rb_node **p = &proc->nodes.rb_node; |
| 1175 | struct rb_node *parent = NULL; |
| 1176 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1177 | binder_uintptr_t ptr = fp ? fp->binder : 0; |
| 1178 | binder_uintptr_t cookie = fp ? fp->cookie : 0; |
| 1179 | __u32 flags = fp ? fp->flags : 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1180 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1181 | assert_spin_locked(&proc->inner_lock); |
| 1182 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1183 | while (*p) { |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1184 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1185 | parent = *p; |
| 1186 | node = rb_entry(parent, struct binder_node, rb_node); |
| 1187 | |
| 1188 | if (ptr < node->ptr) |
| 1189 | p = &(*p)->rb_left; |
| 1190 | else if (ptr > node->ptr) |
| 1191 | p = &(*p)->rb_right; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1192 | else { |
| 1193 | /* |
| 1194 | * A matching node is already in |
| 1195 | * the rb tree. Abandon the init |
| 1196 | * and return it. |
| 1197 | */ |
| 1198 | binder_inc_node_tmpref_ilocked(node); |
| 1199 | return node; |
| 1200 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1201 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1202 | node = new_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1203 | binder_stats_created(BINDER_STAT_NODE); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1204 | node->tmp_refs++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1205 | rb_link_node(&node->rb_node, parent, p); |
| 1206 | rb_insert_color(&node->rb_node, &proc->nodes); |
Todd Kjos | 656a800 | 2017-06-29 12:01:45 -0700 | [diff] [blame] | 1207 | node->debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1208 | node->proc = proc; |
| 1209 | node->ptr = ptr; |
| 1210 | node->cookie = cookie; |
| 1211 | node->work.type = BINDER_WORK_NODE; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1212 | node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK; |
| 1213 | node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 1214 | spin_lock_init(&node->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1215 | INIT_LIST_HEAD(&node->work.entry); |
| 1216 | INIT_LIST_HEAD(&node->async_todo); |
| 1217 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1218 | "%d:%d node %d u%016llx c%016llx created\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1219 | proc->pid, current->pid, node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1220 | (u64)node->ptr, (u64)node->cookie); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1221 | |
| 1222 | return node; |
| 1223 | } |
| 1224 | |
| 1225 | static struct binder_node *binder_new_node(struct binder_proc *proc, |
| 1226 | struct flat_binder_object *fp) |
| 1227 | { |
| 1228 | struct binder_node *node; |
| 1229 | struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 1230 | |
| 1231 | if (!new_node) |
| 1232 | return NULL; |
| 1233 | binder_inner_proc_lock(proc); |
| 1234 | node = binder_init_node_ilocked(proc, new_node, fp); |
| 1235 | binder_inner_proc_unlock(proc); |
| 1236 | if (node != new_node) |
| 1237 | /* |
| 1238 | * The node was already added by another thread |
| 1239 | */ |
| 1240 | kfree(new_node); |
| 1241 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1242 | return node; |
| 1243 | } |
| 1244 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1245 | static void binder_free_node(struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1246 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1247 | kfree(node); |
| 1248 | binder_stats_deleted(BINDER_STAT_NODE); |
| 1249 | } |
| 1250 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1251 | static int binder_inc_node_nilocked(struct binder_node *node, int strong, |
| 1252 | int internal, |
| 1253 | struct list_head *target_list) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1254 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1255 | struct binder_proc *proc = node->proc; |
| 1256 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1257 | assert_spin_locked(&node->lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1258 | if (proc) |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1259 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1260 | if (strong) { |
| 1261 | if (internal) { |
| 1262 | if (target_list == NULL && |
| 1263 | node->internal_strong_refs == 0 && |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 1264 | !(node->proc && |
| 1265 | node == node->proc->context->binder_context_mgr_node && |
| 1266 | node->has_strong_ref)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1267 | pr_err("invalid inc strong node for %d\n", |
| 1268 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1269 | return -EINVAL; |
| 1270 | } |
| 1271 | node->internal_strong_refs++; |
| 1272 | } else |
| 1273 | node->local_strong_refs++; |
| 1274 | if (!node->has_strong_ref && target_list) { |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame^] | 1275 | struct binder_thread *thread = container_of(target_list, |
| 1276 | struct binder_thread, todo); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1277 | binder_dequeue_work_ilocked(&node->work); |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame^] | 1278 | BUG_ON(&thread->todo != target_list); |
| 1279 | binder_enqueue_deferred_thread_work_ilocked(thread, |
| 1280 | &node->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1281 | } |
| 1282 | } else { |
| 1283 | if (!internal) |
| 1284 | node->local_weak_refs++; |
| 1285 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { |
| 1286 | if (target_list == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1287 | pr_err("invalid inc weak node for %d\n", |
| 1288 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1289 | return -EINVAL; |
| 1290 | } |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 1291 | /* |
| 1292 | * See comment above |
| 1293 | */ |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1294 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1300 | static int binder_inc_node(struct binder_node *node, int strong, int internal, |
| 1301 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1302 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1303 | int ret; |
| 1304 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1305 | binder_node_inner_lock(node); |
| 1306 | ret = binder_inc_node_nilocked(node, strong, internal, target_list); |
| 1307 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1308 | |
| 1309 | return ret; |
| 1310 | } |
| 1311 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1312 | static bool binder_dec_node_nilocked(struct binder_node *node, |
| 1313 | int strong, int internal) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1314 | { |
| 1315 | struct binder_proc *proc = node->proc; |
| 1316 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1317 | assert_spin_locked(&node->lock); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1318 | if (proc) |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1319 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1320 | if (strong) { |
| 1321 | if (internal) |
| 1322 | node->internal_strong_refs--; |
| 1323 | else |
| 1324 | node->local_strong_refs--; |
| 1325 | if (node->local_strong_refs || node->internal_strong_refs) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1326 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1327 | } else { |
| 1328 | if (!internal) |
| 1329 | node->local_weak_refs--; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1330 | if (node->local_weak_refs || node->tmp_refs || |
| 1331 | !hlist_empty(&node->refs)) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1332 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1333 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1334 | |
| 1335 | if (proc && (node->has_strong_ref || node->has_weak_ref)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1336 | if (list_empty(&node->work.entry)) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1337 | binder_enqueue_work_ilocked(&node->work, &proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1338 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1339 | } |
| 1340 | } else { |
| 1341 | if (hlist_empty(&node->refs) && !node->local_strong_refs && |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1342 | !node->local_weak_refs && !node->tmp_refs) { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1343 | if (proc) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1344 | binder_dequeue_work_ilocked(&node->work); |
| 1345 | rb_erase(&node->rb_node, &proc->nodes); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1346 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1347 | "refless node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1348 | node->debug_id); |
| 1349 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1350 | BUG_ON(!list_empty(&node->work.entry)); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 1351 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1352 | /* |
| 1353 | * tmp_refs could have changed so |
| 1354 | * check it again |
| 1355 | */ |
| 1356 | if (node->tmp_refs) { |
| 1357 | spin_unlock(&binder_dead_nodes_lock); |
| 1358 | return false; |
| 1359 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1360 | hlist_del(&node->dead_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 1361 | spin_unlock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1362 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1363 | "dead node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1364 | node->debug_id); |
| 1365 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1366 | return true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1367 | } |
| 1368 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1369 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1370 | } |
| 1371 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1372 | static void binder_dec_node(struct binder_node *node, int strong, int internal) |
| 1373 | { |
| 1374 | bool free_node; |
| 1375 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1376 | binder_node_inner_lock(node); |
| 1377 | free_node = binder_dec_node_nilocked(node, strong, internal); |
| 1378 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1379 | if (free_node) |
| 1380 | binder_free_node(node); |
| 1381 | } |
| 1382 | |
| 1383 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1384 | { |
| 1385 | /* |
| 1386 | * No call to binder_inc_node() is needed since we |
| 1387 | * don't need to inform userspace of any changes to |
| 1388 | * tmp_refs |
| 1389 | */ |
| 1390 | node->tmp_refs++; |
| 1391 | } |
| 1392 | |
| 1393 | /** |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1394 | * binder_inc_node_tmpref() - take a temporary reference on node |
| 1395 | * @node: node to reference |
| 1396 | * |
| 1397 | * Take reference on node to prevent the node from being freed |
| 1398 | * while referenced only by a local variable. The inner lock is |
| 1399 | * needed to serialize with the node work on the queue (which |
| 1400 | * isn't needed after the node is dead). If the node is dead |
| 1401 | * (node->proc is NULL), use binder_dead_nodes_lock to protect |
| 1402 | * node->tmp_refs against dead-node-only cases where the node |
| 1403 | * lock cannot be acquired (eg traversing the dead node list to |
| 1404 | * print nodes) |
| 1405 | */ |
| 1406 | static void binder_inc_node_tmpref(struct binder_node *node) |
| 1407 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1408 | binder_node_lock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1409 | if (node->proc) |
| 1410 | binder_inner_proc_lock(node->proc); |
| 1411 | else |
| 1412 | spin_lock(&binder_dead_nodes_lock); |
| 1413 | binder_inc_node_tmpref_ilocked(node); |
| 1414 | if (node->proc) |
| 1415 | binder_inner_proc_unlock(node->proc); |
| 1416 | else |
| 1417 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1418 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | /** |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1422 | * binder_dec_node_tmpref() - remove a temporary reference on node |
| 1423 | * @node: node to reference |
| 1424 | * |
| 1425 | * Release temporary reference on node taken via binder_inc_node_tmpref() |
| 1426 | */ |
| 1427 | static void binder_dec_node_tmpref(struct binder_node *node) |
| 1428 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1429 | bool free_node; |
| 1430 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1431 | binder_node_inner_lock(node); |
| 1432 | if (!node->proc) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1433 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1434 | node->tmp_refs--; |
| 1435 | BUG_ON(node->tmp_refs < 0); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1436 | if (!node->proc) |
| 1437 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1438 | /* |
| 1439 | * Call binder_dec_node() to check if all refcounts are 0 |
| 1440 | * and cleanup is needed. Calling with strong=0 and internal=1 |
| 1441 | * causes no actual reference to be released in binder_dec_node(). |
| 1442 | * If that changes, a change is needed here too. |
| 1443 | */ |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1444 | free_node = binder_dec_node_nilocked(node, 0, 1); |
| 1445 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1446 | if (free_node) |
| 1447 | binder_free_node(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | static void binder_put_node(struct binder_node *node) |
| 1451 | { |
| 1452 | binder_dec_node_tmpref(node); |
| 1453 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1454 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1455 | static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc, |
| 1456 | u32 desc, bool need_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1457 | { |
| 1458 | struct rb_node *n = proc->refs_by_desc.rb_node; |
| 1459 | struct binder_ref *ref; |
| 1460 | |
| 1461 | while (n) { |
| 1462 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1463 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1464 | if (desc < ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1465 | n = n->rb_left; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1466 | } else if (desc > ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1467 | n = n->rb_right; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1468 | } else if (need_strong_ref && !ref->data.strong) { |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1469 | binder_user_error("tried to use weak ref as strong ref\n"); |
| 1470 | return NULL; |
| 1471 | } else { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1472 | return ref; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1473 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1474 | } |
| 1475 | return NULL; |
| 1476 | } |
| 1477 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1478 | /** |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1479 | * 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] | 1480 | * @proc: binder_proc that owns the ref |
| 1481 | * @node: binder_node of target |
| 1482 | * @new_ref: newly allocated binder_ref to be initialized or %NULL |
| 1483 | * |
| 1484 | * Look up the ref for the given node and return it if it exists |
| 1485 | * |
| 1486 | * If it doesn't exist and the caller provides a newly allocated |
| 1487 | * ref, initialize the fields of the newly allocated ref and insert |
| 1488 | * into the given proc rb_trees and node refs list. |
| 1489 | * |
| 1490 | * Return: the ref for node. It is possible that another thread |
| 1491 | * allocated/initialized the ref first in which case the |
| 1492 | * returned ref would be different than the passed-in |
| 1493 | * new_ref. new_ref must be kfree'd by the caller in |
| 1494 | * this case. |
| 1495 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1496 | static struct binder_ref *binder_get_ref_for_node_olocked( |
| 1497 | struct binder_proc *proc, |
| 1498 | struct binder_node *node, |
| 1499 | struct binder_ref *new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1500 | { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1501 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1502 | struct rb_node **p = &proc->refs_by_node.rb_node; |
| 1503 | struct rb_node *parent = NULL; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1504 | struct binder_ref *ref; |
| 1505 | struct rb_node *n; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1506 | |
| 1507 | while (*p) { |
| 1508 | parent = *p; |
| 1509 | ref = rb_entry(parent, struct binder_ref, rb_node_node); |
| 1510 | |
| 1511 | if (node < ref->node) |
| 1512 | p = &(*p)->rb_left; |
| 1513 | else if (node > ref->node) |
| 1514 | p = &(*p)->rb_right; |
| 1515 | else |
| 1516 | return ref; |
| 1517 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1518 | if (!new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1519 | return NULL; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1520 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1521 | binder_stats_created(BINDER_STAT_REF); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1522 | new_ref->data.debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1523 | new_ref->proc = proc; |
| 1524 | new_ref->node = node; |
| 1525 | rb_link_node(&new_ref->rb_node_node, parent, p); |
| 1526 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); |
| 1527 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1528 | 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] | 1529 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 1530 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1531 | if (ref->data.desc > new_ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1532 | break; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1533 | new_ref->data.desc = ref->data.desc + 1; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | p = &proc->refs_by_desc.rb_node; |
| 1537 | while (*p) { |
| 1538 | parent = *p; |
| 1539 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); |
| 1540 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1541 | if (new_ref->data.desc < ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1542 | p = &(*p)->rb_left; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1543 | else if (new_ref->data.desc > ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1544 | p = &(*p)->rb_right; |
| 1545 | else |
| 1546 | BUG(); |
| 1547 | } |
| 1548 | rb_link_node(&new_ref->rb_node_desc, parent, p); |
| 1549 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1550 | |
| 1551 | binder_node_lock(node); |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1552 | hlist_add_head(&new_ref->node_entry, &node->refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1553 | |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1554 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1555 | "%d new ref %d desc %d for node %d\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1556 | proc->pid, new_ref->data.debug_id, new_ref->data.desc, |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1557 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1558 | binder_node_unlock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1559 | return new_ref; |
| 1560 | } |
| 1561 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1562 | static void binder_cleanup_ref_olocked(struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1563 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1564 | bool delete_node = false; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1565 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1566 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1567 | "%d delete ref %d desc %d for node %d\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1568 | ref->proc->pid, ref->data.debug_id, ref->data.desc, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1569 | ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1570 | |
| 1571 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); |
| 1572 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1573 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1574 | binder_node_inner_lock(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1575 | if (ref->data.strong) |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1576 | binder_dec_node_nilocked(ref->node, 1, 1); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1577 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1578 | hlist_del(&ref->node_entry); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1579 | delete_node = binder_dec_node_nilocked(ref->node, 0, 1); |
| 1580 | binder_node_inner_unlock(ref->node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1581 | /* |
| 1582 | * Clear ref->node unless we want the caller to free the node |
| 1583 | */ |
| 1584 | if (!delete_node) { |
| 1585 | /* |
| 1586 | * The caller uses ref->node to determine |
| 1587 | * whether the node needs to be freed. Clear |
| 1588 | * it since the node is still alive. |
| 1589 | */ |
| 1590 | ref->node = NULL; |
| 1591 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1592 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1593 | if (ref->death) { |
| 1594 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1595 | "%d delete ref %d desc %d has death notification\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1596 | ref->proc->pid, ref->data.debug_id, |
| 1597 | ref->data.desc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1598 | binder_dequeue_work(ref->proc, &ref->death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1599 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 1600 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1601 | binder_stats_deleted(BINDER_STAT_REF); |
| 1602 | } |
| 1603 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1604 | /** |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1605 | * binder_inc_ref_olocked() - increment the ref for given handle |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1606 | * @ref: ref to be incremented |
| 1607 | * @strong: if true, strong increment, else weak |
| 1608 | * @target_list: list to queue node work on |
| 1609 | * |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1610 | * Increment the ref. @ref->proc->outer_lock must be held on entry |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1611 | * |
| 1612 | * Return: 0, if successful, else errno |
| 1613 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1614 | static int binder_inc_ref_olocked(struct binder_ref *ref, int strong, |
| 1615 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1616 | { |
| 1617 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1618 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1619 | if (strong) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1620 | if (ref->data.strong == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1621 | ret = binder_inc_node(ref->node, 1, 1, target_list); |
| 1622 | if (ret) |
| 1623 | return ret; |
| 1624 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1625 | ref->data.strong++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1626 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1627 | if (ref->data.weak == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1628 | ret = binder_inc_node(ref->node, 0, 1, target_list); |
| 1629 | if (ret) |
| 1630 | return ret; |
| 1631 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1632 | ref->data.weak++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1633 | } |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1637 | /** |
| 1638 | * binder_dec_ref() - dec the ref for given handle |
| 1639 | * @ref: ref to be decremented |
| 1640 | * @strong: if true, strong decrement, else weak |
| 1641 | * |
| 1642 | * Decrement the ref. |
| 1643 | * |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1644 | * Return: true if ref is cleaned up and ready to be freed |
| 1645 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1646 | 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] | 1647 | { |
| 1648 | if (strong) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1649 | if (ref->data.strong == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1650 | 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] | 1651 | ref->proc->pid, ref->data.debug_id, |
| 1652 | ref->data.desc, ref->data.strong, |
| 1653 | ref->data.weak); |
| 1654 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1655 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1656 | ref->data.strong--; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1657 | if (ref->data.strong == 0) |
| 1658 | binder_dec_node(ref->node, strong, 1); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1659 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1660 | if (ref->data.weak == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1661 | 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] | 1662 | ref->proc->pid, ref->data.debug_id, |
| 1663 | ref->data.desc, ref->data.strong, |
| 1664 | ref->data.weak); |
| 1665 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1666 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1667 | ref->data.weak--; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1668 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1669 | if (ref->data.strong == 0 && ref->data.weak == 0) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1670 | binder_cleanup_ref_olocked(ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1671 | return true; |
| 1672 | } |
| 1673 | return false; |
| 1674 | } |
| 1675 | |
| 1676 | /** |
| 1677 | * binder_get_node_from_ref() - get the node from the given proc/desc |
| 1678 | * @proc: proc containing the ref |
| 1679 | * @desc: the handle associated with the ref |
| 1680 | * @need_strong_ref: if true, only return node if ref is strong |
| 1681 | * @rdata: the id/refcount data for the ref |
| 1682 | * |
| 1683 | * Given a proc and ref handle, return the associated binder_node |
| 1684 | * |
| 1685 | * Return: a binder_node or NULL if not found or not strong when strong required |
| 1686 | */ |
| 1687 | static struct binder_node *binder_get_node_from_ref( |
| 1688 | struct binder_proc *proc, |
| 1689 | u32 desc, bool need_strong_ref, |
| 1690 | struct binder_ref_data *rdata) |
| 1691 | { |
| 1692 | struct binder_node *node; |
| 1693 | struct binder_ref *ref; |
| 1694 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1695 | binder_proc_lock(proc); |
| 1696 | ref = binder_get_ref_olocked(proc, desc, need_strong_ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1697 | if (!ref) |
| 1698 | goto err_no_ref; |
| 1699 | node = ref->node; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1700 | /* |
| 1701 | * Take an implicit reference on the node to ensure |
| 1702 | * it stays alive until the call to binder_put_node() |
| 1703 | */ |
| 1704 | binder_inc_node_tmpref(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1705 | if (rdata) |
| 1706 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1707 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1708 | |
| 1709 | return node; |
| 1710 | |
| 1711 | err_no_ref: |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1712 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1713 | return NULL; |
| 1714 | } |
| 1715 | |
| 1716 | /** |
| 1717 | * binder_free_ref() - free the binder_ref |
| 1718 | * @ref: ref to free |
| 1719 | * |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1720 | * Free the binder_ref. Free the binder_node indicated by ref->node |
| 1721 | * (if non-NULL) and the binder_ref_death indicated by ref->death. |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1722 | */ |
| 1723 | static void binder_free_ref(struct binder_ref *ref) |
| 1724 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1725 | if (ref->node) |
| 1726 | binder_free_node(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1727 | kfree(ref->death); |
| 1728 | kfree(ref); |
| 1729 | } |
| 1730 | |
| 1731 | /** |
| 1732 | * binder_update_ref_for_handle() - inc/dec the ref for given handle |
| 1733 | * @proc: proc containing the ref |
| 1734 | * @desc: the handle associated with the ref |
| 1735 | * @increment: true=inc reference, false=dec reference |
| 1736 | * @strong: true=strong reference, false=weak reference |
| 1737 | * @rdata: the id/refcount data for the ref |
| 1738 | * |
| 1739 | * Given a proc and ref handle, increment or decrement the ref |
| 1740 | * according to "increment" arg. |
| 1741 | * |
| 1742 | * Return: 0 if successful, else errno |
| 1743 | */ |
| 1744 | static int binder_update_ref_for_handle(struct binder_proc *proc, |
| 1745 | uint32_t desc, bool increment, bool strong, |
| 1746 | struct binder_ref_data *rdata) |
| 1747 | { |
| 1748 | int ret = 0; |
| 1749 | struct binder_ref *ref; |
| 1750 | bool delete_ref = false; |
| 1751 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1752 | binder_proc_lock(proc); |
| 1753 | ref = binder_get_ref_olocked(proc, desc, strong); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1754 | if (!ref) { |
| 1755 | ret = -EINVAL; |
| 1756 | goto err_no_ref; |
| 1757 | } |
| 1758 | if (increment) |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1759 | ret = binder_inc_ref_olocked(ref, strong, NULL); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1760 | else |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1761 | delete_ref = binder_dec_ref_olocked(ref, strong); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1762 | |
| 1763 | if (rdata) |
| 1764 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1765 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1766 | |
| 1767 | if (delete_ref) |
| 1768 | binder_free_ref(ref); |
| 1769 | return ret; |
| 1770 | |
| 1771 | err_no_ref: |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1772 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1773 | return ret; |
| 1774 | } |
| 1775 | |
| 1776 | /** |
| 1777 | * binder_dec_ref_for_handle() - dec the ref for given handle |
| 1778 | * @proc: proc containing the ref |
| 1779 | * @desc: the handle associated with the ref |
| 1780 | * @strong: true=strong reference, false=weak reference |
| 1781 | * @rdata: the id/refcount data for the ref |
| 1782 | * |
| 1783 | * Just calls binder_update_ref_for_handle() to decrement the ref. |
| 1784 | * |
| 1785 | * Return: 0 if successful, else errno |
| 1786 | */ |
| 1787 | static int binder_dec_ref_for_handle(struct binder_proc *proc, |
| 1788 | uint32_t desc, bool strong, struct binder_ref_data *rdata) |
| 1789 | { |
| 1790 | return binder_update_ref_for_handle(proc, desc, false, strong, rdata); |
| 1791 | } |
| 1792 | |
| 1793 | |
| 1794 | /** |
| 1795 | * binder_inc_ref_for_node() - increment the ref for given proc/node |
| 1796 | * @proc: proc containing the ref |
| 1797 | * @node: target node |
| 1798 | * @strong: true=strong reference, false=weak reference |
| 1799 | * @target_list: worklist to use if node is incremented |
| 1800 | * @rdata: the id/refcount data for the ref |
| 1801 | * |
| 1802 | * Given a proc and node, increment the ref. Create the ref if it |
| 1803 | * doesn't already exist |
| 1804 | * |
| 1805 | * Return: 0 if successful, else errno |
| 1806 | */ |
| 1807 | static int binder_inc_ref_for_node(struct binder_proc *proc, |
| 1808 | struct binder_node *node, |
| 1809 | bool strong, |
| 1810 | struct list_head *target_list, |
| 1811 | struct binder_ref_data *rdata) |
| 1812 | { |
| 1813 | struct binder_ref *ref; |
| 1814 | struct binder_ref *new_ref = NULL; |
| 1815 | int ret = 0; |
| 1816 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1817 | binder_proc_lock(proc); |
| 1818 | ref = binder_get_ref_for_node_olocked(proc, node, NULL); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1819 | if (!ref) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1820 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1821 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
| 1822 | if (!new_ref) |
| 1823 | return -ENOMEM; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1824 | binder_proc_lock(proc); |
| 1825 | ref = binder_get_ref_for_node_olocked(proc, node, new_ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1826 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1827 | ret = binder_inc_ref_olocked(ref, strong, target_list); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1828 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1829 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1830 | if (new_ref && ref != new_ref) |
| 1831 | /* |
| 1832 | * Another thread created the ref first so |
| 1833 | * free the one we allocated |
| 1834 | */ |
| 1835 | kfree(new_ref); |
| 1836 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1837 | } |
| 1838 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1839 | static void binder_pop_transaction_ilocked(struct binder_thread *target_thread, |
| 1840 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1841 | { |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1842 | BUG_ON(!target_thread); |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1843 | assert_spin_locked(&target_thread->proc->inner_lock); |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1844 | BUG_ON(target_thread->transaction_stack != t); |
| 1845 | BUG_ON(target_thread->transaction_stack->from != target_thread); |
| 1846 | target_thread->transaction_stack = |
| 1847 | target_thread->transaction_stack->from_parent; |
| 1848 | t->from = NULL; |
| 1849 | } |
| 1850 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1851 | /** |
| 1852 | * binder_thread_dec_tmpref() - decrement thread->tmp_ref |
| 1853 | * @thread: thread to decrement |
| 1854 | * |
| 1855 | * A thread needs to be kept alive while being used to create or |
| 1856 | * handle a transaction. binder_get_txn_from() is used to safely |
| 1857 | * extract t->from from a binder_transaction and keep the thread |
| 1858 | * indicated by t->from from being freed. When done with that |
| 1859 | * binder_thread, this function is called to decrement the |
| 1860 | * tmp_ref and free if appropriate (thread has been released |
| 1861 | * and no transaction being processed by the driver) |
| 1862 | */ |
| 1863 | static void binder_thread_dec_tmpref(struct binder_thread *thread) |
| 1864 | { |
| 1865 | /* |
| 1866 | * atomic is used to protect the counter value while |
| 1867 | * it cannot reach zero or thread->is_dead is false |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1868 | */ |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1869 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1870 | atomic_dec(&thread->tmp_ref); |
| 1871 | if (thread->is_dead && !atomic_read(&thread->tmp_ref)) { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1872 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1873 | binder_free_thread(thread); |
| 1874 | return; |
| 1875 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1876 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | /** |
| 1880 | * binder_proc_dec_tmpref() - decrement proc->tmp_ref |
| 1881 | * @proc: proc to decrement |
| 1882 | * |
| 1883 | * A binder_proc needs to be kept alive while being used to create or |
| 1884 | * handle a transaction. proc->tmp_ref is incremented when |
| 1885 | * creating a new transaction or the binder_proc is currently in-use |
| 1886 | * by threads that are being released. When done with the binder_proc, |
| 1887 | * this function is called to decrement the counter and free the |
| 1888 | * proc if appropriate (proc has been released, all threads have |
| 1889 | * been released and not currenly in-use to process a transaction). |
| 1890 | */ |
| 1891 | static void binder_proc_dec_tmpref(struct binder_proc *proc) |
| 1892 | { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1893 | binder_inner_proc_lock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1894 | proc->tmp_ref--; |
| 1895 | if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) && |
| 1896 | !proc->tmp_ref) { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1897 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1898 | binder_free_proc(proc); |
| 1899 | return; |
| 1900 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1901 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | /** |
| 1905 | * binder_get_txn_from() - safely extract the "from" thread in transaction |
| 1906 | * @t: binder transaction for t->from |
| 1907 | * |
| 1908 | * Atomically return the "from" thread and increment the tmp_ref |
| 1909 | * count for the thread to ensure it stays alive until |
| 1910 | * binder_thread_dec_tmpref() is called. |
| 1911 | * |
| 1912 | * Return: the value of t->from |
| 1913 | */ |
| 1914 | static struct binder_thread *binder_get_txn_from( |
| 1915 | struct binder_transaction *t) |
| 1916 | { |
| 1917 | struct binder_thread *from; |
| 1918 | |
| 1919 | spin_lock(&t->lock); |
| 1920 | from = t->from; |
| 1921 | if (from) |
| 1922 | atomic_inc(&from->tmp_ref); |
| 1923 | spin_unlock(&t->lock); |
| 1924 | return from; |
| 1925 | } |
| 1926 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1927 | /** |
| 1928 | * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock |
| 1929 | * @t: binder transaction for t->from |
| 1930 | * |
| 1931 | * Same as binder_get_txn_from() except it also acquires the proc->inner_lock |
| 1932 | * to guarantee that the thread cannot be released while operating on it. |
| 1933 | * The caller must call binder_inner_proc_unlock() to release the inner lock |
| 1934 | * as well as call binder_dec_thread_txn() to release the reference. |
| 1935 | * |
| 1936 | * Return: the value of t->from |
| 1937 | */ |
| 1938 | static struct binder_thread *binder_get_txn_from_and_acq_inner( |
| 1939 | struct binder_transaction *t) |
| 1940 | { |
| 1941 | struct binder_thread *from; |
| 1942 | |
| 1943 | from = binder_get_txn_from(t); |
| 1944 | if (!from) |
| 1945 | return NULL; |
| 1946 | binder_inner_proc_lock(from->proc); |
| 1947 | if (t->from) { |
| 1948 | BUG_ON(from != t->from); |
| 1949 | return from; |
| 1950 | } |
| 1951 | binder_inner_proc_unlock(from->proc); |
| 1952 | binder_thread_dec_tmpref(from); |
| 1953 | return NULL; |
| 1954 | } |
| 1955 | |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1956 | static void binder_free_transaction(struct binder_transaction *t) |
| 1957 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1958 | if (t->buffer) |
| 1959 | t->buffer->transaction = NULL; |
| 1960 | kfree(t); |
| 1961 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 1962 | } |
| 1963 | |
| 1964 | static void binder_send_failed_reply(struct binder_transaction *t, |
| 1965 | uint32_t error_code) |
| 1966 | { |
| 1967 | struct binder_thread *target_thread; |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 1968 | struct binder_transaction *next; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1969 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1970 | BUG_ON(t->flags & TF_ONE_WAY); |
| 1971 | while (1) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1972 | target_thread = binder_get_txn_from_and_acq_inner(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1973 | if (target_thread) { |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1974 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 1975 | "send failed reply for transaction %d to %d:%d\n", |
| 1976 | t->debug_id, |
| 1977 | target_thread->proc->pid, |
| 1978 | target_thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1979 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1980 | binder_pop_transaction_ilocked(target_thread, t); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1981 | if (target_thread->reply_error.cmd == BR_OK) { |
| 1982 | target_thread->reply_error.cmd = error_code; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 1983 | binder_enqueue_thread_work_ilocked( |
| 1984 | target_thread, |
| 1985 | &target_thread->reply_error.work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1986 | wake_up_interruptible(&target_thread->wait); |
| 1987 | } else { |
Todd Kjos | e46a3b3 | 2018-02-07 12:38:47 -0800 | [diff] [blame] | 1988 | /* |
| 1989 | * Cannot get here for normal operation, but |
| 1990 | * we can if multiple synchronous transactions |
| 1991 | * are sent without blocking for responses. |
| 1992 | * Just ignore the 2nd error in this case. |
| 1993 | */ |
| 1994 | pr_warn("Unexpected reply error: %u\n", |
| 1995 | target_thread->reply_error.cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1996 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1997 | binder_inner_proc_unlock(target_thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1998 | binder_thread_dec_tmpref(target_thread); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1999 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2000 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2001 | } |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2002 | next = t->from_parent; |
| 2003 | |
| 2004 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2005 | "send failed reply for transaction %d, target dead\n", |
| 2006 | t->debug_id); |
| 2007 | |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 2008 | binder_free_transaction(t); |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2009 | if (next == NULL) { |
| 2010 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2011 | "reply failed, no target thread at root\n"); |
| 2012 | return; |
| 2013 | } |
| 2014 | t = next; |
| 2015 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2016 | "reply failed, no target thread -- retry %d\n", |
| 2017 | t->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2021 | /** |
Martijn Coenen | fb2c445 | 2017-11-13 10:06:08 +0100 | [diff] [blame] | 2022 | * binder_cleanup_transaction() - cleans up undelivered transaction |
| 2023 | * @t: transaction that needs to be cleaned up |
| 2024 | * @reason: reason the transaction wasn't delivered |
| 2025 | * @error_code: error to return to caller (if synchronous call) |
| 2026 | */ |
| 2027 | static void binder_cleanup_transaction(struct binder_transaction *t, |
| 2028 | const char *reason, |
| 2029 | uint32_t error_code) |
| 2030 | { |
| 2031 | if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) { |
| 2032 | binder_send_failed_reply(t, error_code); |
| 2033 | } else { |
| 2034 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 2035 | "undelivered transaction %d, %s\n", |
| 2036 | t->debug_id, reason); |
| 2037 | binder_free_transaction(t); |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | /** |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2042 | * binder_validate_object() - checks for a valid metadata object in a buffer. |
| 2043 | * @buffer: binder_buffer that we're parsing. |
| 2044 | * @offset: offset in the buffer at which to validate an object. |
| 2045 | * |
| 2046 | * Return: If there's a valid metadata object at @offset in @buffer, the |
| 2047 | * size of that object. Otherwise, it returns zero. |
| 2048 | */ |
| 2049 | static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset) |
| 2050 | { |
| 2051 | /* Check if we can read a header first */ |
| 2052 | struct binder_object_header *hdr; |
| 2053 | size_t object_size = 0; |
| 2054 | |
Dan Carpenter | 361f2dd | 2018-03-29 12:14:40 +0300 | [diff] [blame] | 2055 | if (buffer->data_size < sizeof(*hdr) || |
| 2056 | offset > buffer->data_size - sizeof(*hdr) || |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2057 | !IS_ALIGNED(offset, sizeof(u32))) |
| 2058 | return 0; |
| 2059 | |
| 2060 | /* Ok, now see if we can read a complete object. */ |
| 2061 | hdr = (struct binder_object_header *)(buffer->data + offset); |
| 2062 | switch (hdr->type) { |
| 2063 | case BINDER_TYPE_BINDER: |
| 2064 | case BINDER_TYPE_WEAK_BINDER: |
| 2065 | case BINDER_TYPE_HANDLE: |
| 2066 | case BINDER_TYPE_WEAK_HANDLE: |
| 2067 | object_size = sizeof(struct flat_binder_object); |
| 2068 | break; |
| 2069 | case BINDER_TYPE_FD: |
| 2070 | object_size = sizeof(struct binder_fd_object); |
| 2071 | break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2072 | case BINDER_TYPE_PTR: |
| 2073 | object_size = sizeof(struct binder_buffer_object); |
| 2074 | break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2075 | case BINDER_TYPE_FDA: |
| 2076 | object_size = sizeof(struct binder_fd_array_object); |
| 2077 | break; |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2078 | default: |
| 2079 | return 0; |
| 2080 | } |
| 2081 | if (offset <= buffer->data_size - object_size && |
| 2082 | buffer->data_size >= object_size) |
| 2083 | return object_size; |
| 2084 | else |
| 2085 | return 0; |
| 2086 | } |
| 2087 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2088 | /** |
| 2089 | * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer. |
| 2090 | * @b: binder_buffer containing the object |
| 2091 | * @index: index in offset array at which the binder_buffer_object is |
| 2092 | * located |
| 2093 | * @start: points to the start of the offset array |
| 2094 | * @num_valid: the number of valid offsets in the offset array |
| 2095 | * |
| 2096 | * Return: If @index is within the valid range of the offset array |
| 2097 | * described by @start and @num_valid, and if there's a valid |
| 2098 | * binder_buffer_object at the offset found in index @index |
| 2099 | * of the offset array, that object is returned. Otherwise, |
| 2100 | * %NULL is returned. |
| 2101 | * Note that the offset found in index @index itself is not |
| 2102 | * verified; this function assumes that @num_valid elements |
| 2103 | * from @start were previously verified to have valid offsets. |
| 2104 | */ |
| 2105 | static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b, |
| 2106 | binder_size_t index, |
| 2107 | binder_size_t *start, |
| 2108 | binder_size_t num_valid) |
| 2109 | { |
| 2110 | struct binder_buffer_object *buffer_obj; |
| 2111 | binder_size_t *offp; |
| 2112 | |
| 2113 | if (index >= num_valid) |
| 2114 | return NULL; |
| 2115 | |
| 2116 | offp = start + index; |
| 2117 | buffer_obj = (struct binder_buffer_object *)(b->data + *offp); |
| 2118 | if (buffer_obj->hdr.type != BINDER_TYPE_PTR) |
| 2119 | return NULL; |
| 2120 | |
| 2121 | return buffer_obj; |
| 2122 | } |
| 2123 | |
| 2124 | /** |
| 2125 | * binder_validate_fixup() - validates pointer/fd fixups happen in order. |
| 2126 | * @b: transaction buffer |
| 2127 | * @objects_start start of objects buffer |
| 2128 | * @buffer: binder_buffer_object in which to fix up |
| 2129 | * @offset: start offset in @buffer to fix up |
| 2130 | * @last_obj: last binder_buffer_object that we fixed up in |
| 2131 | * @last_min_offset: minimum fixup offset in @last_obj |
| 2132 | * |
| 2133 | * Return: %true if a fixup in buffer @buffer at offset @offset is |
| 2134 | * allowed. |
| 2135 | * |
| 2136 | * For safety reasons, we only allow fixups inside a buffer to happen |
| 2137 | * at increasing offsets; additionally, we only allow fixup on the last |
| 2138 | * buffer object that was verified, or one of its parents. |
| 2139 | * |
| 2140 | * Example of what is allowed: |
| 2141 | * |
| 2142 | * A |
| 2143 | * B (parent = A, offset = 0) |
| 2144 | * C (parent = A, offset = 16) |
| 2145 | * D (parent = C, offset = 0) |
| 2146 | * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset) |
| 2147 | * |
| 2148 | * Examples of what is not allowed: |
| 2149 | * |
| 2150 | * Decreasing offsets within the same parent: |
| 2151 | * A |
| 2152 | * C (parent = A, offset = 16) |
| 2153 | * B (parent = A, offset = 0) // decreasing offset within A |
| 2154 | * |
| 2155 | * Referring to a parent that wasn't the last object or any of its parents: |
| 2156 | * A |
| 2157 | * B (parent = A, offset = 0) |
| 2158 | * C (parent = A, offset = 0) |
| 2159 | * C (parent = A, offset = 16) |
| 2160 | * D (parent = B, offset = 0) // B is not A or any of A's parents |
| 2161 | */ |
| 2162 | static bool binder_validate_fixup(struct binder_buffer *b, |
| 2163 | binder_size_t *objects_start, |
| 2164 | struct binder_buffer_object *buffer, |
| 2165 | binder_size_t fixup_offset, |
| 2166 | struct binder_buffer_object *last_obj, |
| 2167 | binder_size_t last_min_offset) |
| 2168 | { |
| 2169 | if (!last_obj) { |
| 2170 | /* Nothing to fix up in */ |
| 2171 | return false; |
| 2172 | } |
| 2173 | |
| 2174 | while (last_obj != buffer) { |
| 2175 | /* |
| 2176 | * Safe to retrieve the parent of last_obj, since it |
| 2177 | * was already previously verified by the driver. |
| 2178 | */ |
| 2179 | if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0) |
| 2180 | return false; |
| 2181 | last_min_offset = last_obj->parent_offset + sizeof(uintptr_t); |
| 2182 | last_obj = (struct binder_buffer_object *) |
| 2183 | (b->data + *(objects_start + last_obj->parent)); |
| 2184 | } |
| 2185 | return (fixup_offset >= last_min_offset); |
| 2186 | } |
| 2187 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2188 | static void binder_transaction_buffer_release(struct binder_proc *proc, |
| 2189 | struct binder_buffer *buffer, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2190 | binder_size_t *failed_at) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2191 | { |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2192 | binder_size_t *offp, *off_start, *off_end; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2193 | int debug_id = buffer->debug_id; |
| 2194 | |
| 2195 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | 8ca86f1 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 2196 | "%d buffer release %d, size %zd-%zd, failed at %pK\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2197 | proc->pid, buffer->debug_id, |
| 2198 | buffer->data_size, buffer->offsets_size, failed_at); |
| 2199 | |
| 2200 | if (buffer->target_node) |
| 2201 | binder_dec_node(buffer->target_node, 1, 0); |
| 2202 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2203 | off_start = (binder_size_t *)(buffer->data + |
| 2204 | ALIGN(buffer->data_size, sizeof(void *))); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2205 | if (failed_at) |
| 2206 | off_end = failed_at; |
| 2207 | else |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2208 | off_end = (void *)off_start + buffer->offsets_size; |
| 2209 | for (offp = off_start; offp < off_end; offp++) { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2210 | struct binder_object_header *hdr; |
| 2211 | size_t object_size = binder_validate_object(buffer, *offp); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2212 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2213 | if (object_size == 0) { |
| 2214 | 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] | 2215 | debug_id, (u64)*offp, buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2216 | continue; |
| 2217 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2218 | hdr = (struct binder_object_header *)(buffer->data + *offp); |
| 2219 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2220 | case BINDER_TYPE_BINDER: |
| 2221 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2222 | struct flat_binder_object *fp; |
| 2223 | struct binder_node *node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2224 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2225 | fp = to_flat_binder_object(hdr); |
| 2226 | node = binder_get_node(proc, fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2227 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2228 | pr_err("transaction release %d bad node %016llx\n", |
| 2229 | debug_id, (u64)fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2230 | break; |
| 2231 | } |
| 2232 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2233 | " node %d u%016llx\n", |
| 2234 | node->debug_id, (u64)node->ptr); |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2235 | binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER, |
| 2236 | 0); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2237 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2238 | } break; |
| 2239 | case BINDER_TYPE_HANDLE: |
| 2240 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2241 | struct flat_binder_object *fp; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2242 | struct binder_ref_data rdata; |
| 2243 | int ret; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 2244 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2245 | fp = to_flat_binder_object(hdr); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2246 | ret = binder_dec_ref_for_handle(proc, fp->handle, |
| 2247 | hdr->type == BINDER_TYPE_HANDLE, &rdata); |
| 2248 | |
| 2249 | if (ret) { |
| 2250 | pr_err("transaction release %d bad handle %d, ret = %d\n", |
| 2251 | debug_id, fp->handle, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2252 | break; |
| 2253 | } |
| 2254 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2255 | " ref %d desc %d\n", |
| 2256 | rdata.debug_id, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2257 | } break; |
| 2258 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2259 | case BINDER_TYPE_FD: { |
| 2260 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
| 2261 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2262 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2263 | " fd %d\n", fp->fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2264 | if (failed_at) |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2265 | task_close_fd(proc, fp->fd); |
| 2266 | } break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2267 | case BINDER_TYPE_PTR: |
| 2268 | /* |
| 2269 | * Nothing to do here, this will get cleaned up when the |
| 2270 | * transaction buffer gets freed |
| 2271 | */ |
| 2272 | break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2273 | case BINDER_TYPE_FDA: { |
| 2274 | struct binder_fd_array_object *fda; |
| 2275 | struct binder_buffer_object *parent; |
| 2276 | uintptr_t parent_buffer; |
| 2277 | u32 *fd_array; |
| 2278 | size_t fd_index; |
| 2279 | binder_size_t fd_buf_size; |
| 2280 | |
| 2281 | fda = to_binder_fd_array_object(hdr); |
| 2282 | parent = binder_validate_ptr(buffer, fda->parent, |
| 2283 | off_start, |
| 2284 | offp - off_start); |
| 2285 | if (!parent) { |
Arvind Yadav | f7f84fd | 2017-09-25 12:52:11 +0530 | [diff] [blame] | 2286 | pr_err("transaction release %d bad parent offset\n", |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2287 | debug_id); |
| 2288 | continue; |
| 2289 | } |
| 2290 | /* |
| 2291 | * Since the parent was already fixed up, convert it |
| 2292 | * back to kernel address space to access it |
| 2293 | */ |
| 2294 | parent_buffer = parent->buffer - |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2295 | binder_alloc_get_user_buffer_offset( |
| 2296 | &proc->alloc); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2297 | |
| 2298 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2299 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2300 | pr_err("transaction release %d invalid number of fds (%lld)\n", |
| 2301 | debug_id, (u64)fda->num_fds); |
| 2302 | continue; |
| 2303 | } |
| 2304 | if (fd_buf_size > parent->length || |
| 2305 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2306 | /* No space for all file descriptors here. */ |
| 2307 | pr_err("transaction release %d not enough space for %lld fds in buffer\n", |
| 2308 | debug_id, (u64)fda->num_fds); |
| 2309 | continue; |
| 2310 | } |
Arnd Bergmann | 1c363eae | 2017-09-05 10:56:13 +0200 | [diff] [blame] | 2311 | fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2312 | for (fd_index = 0; fd_index < fda->num_fds; fd_index++) |
| 2313 | task_close_fd(proc, fd_array[fd_index]); |
| 2314 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2315 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 2316 | pr_err("transaction release %d bad object type %x\n", |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2317 | debug_id, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2318 | break; |
| 2319 | } |
| 2320 | } |
| 2321 | } |
| 2322 | |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2323 | static int binder_translate_binder(struct flat_binder_object *fp, |
| 2324 | struct binder_transaction *t, |
| 2325 | struct binder_thread *thread) |
| 2326 | { |
| 2327 | struct binder_node *node; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2328 | struct binder_proc *proc = thread->proc; |
| 2329 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2330 | struct binder_ref_data rdata; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2331 | int ret = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2332 | |
| 2333 | node = binder_get_node(proc, fp->binder); |
| 2334 | if (!node) { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2335 | node = binder_new_node(proc, fp); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2336 | if (!node) |
| 2337 | return -ENOMEM; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2338 | } |
| 2339 | if (fp->cookie != node->cookie) { |
| 2340 | binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n", |
| 2341 | proc->pid, thread->pid, (u64)fp->binder, |
| 2342 | node->debug_id, (u64)fp->cookie, |
| 2343 | (u64)node->cookie); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2344 | ret = -EINVAL; |
| 2345 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2346 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2347 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2348 | ret = -EPERM; |
| 2349 | goto done; |
| 2350 | } |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2351 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2352 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2353 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2354 | &thread->todo, &rdata); |
| 2355 | if (ret) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2356 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2357 | |
| 2358 | if (fp->hdr.type == BINDER_TYPE_BINDER) |
| 2359 | fp->hdr.type = BINDER_TYPE_HANDLE; |
| 2360 | else |
| 2361 | fp->hdr.type = BINDER_TYPE_WEAK_HANDLE; |
| 2362 | fp->binder = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2363 | fp->handle = rdata.desc; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2364 | fp->cookie = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2365 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2366 | trace_binder_transaction_node_to_ref(t, node, &rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2367 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2368 | " node %d u%016llx -> ref %d desc %d\n", |
| 2369 | node->debug_id, (u64)node->ptr, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2370 | rdata.debug_id, rdata.desc); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2371 | done: |
| 2372 | binder_put_node(node); |
| 2373 | return ret; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | static int binder_translate_handle(struct flat_binder_object *fp, |
| 2377 | struct binder_transaction *t, |
| 2378 | struct binder_thread *thread) |
| 2379 | { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2380 | struct binder_proc *proc = thread->proc; |
| 2381 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2382 | struct binder_node *node; |
| 2383 | struct binder_ref_data src_rdata; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2384 | int ret = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2385 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2386 | node = binder_get_node_from_ref(proc, fp->handle, |
| 2387 | fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata); |
| 2388 | if (!node) { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2389 | binder_user_error("%d:%d got transaction with invalid handle, %d\n", |
| 2390 | proc->pid, thread->pid, fp->handle); |
| 2391 | return -EINVAL; |
| 2392 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2393 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2394 | ret = -EPERM; |
| 2395 | goto done; |
| 2396 | } |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2397 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2398 | binder_node_lock(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2399 | if (node->proc == target_proc) { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2400 | if (fp->hdr.type == BINDER_TYPE_HANDLE) |
| 2401 | fp->hdr.type = BINDER_TYPE_BINDER; |
| 2402 | else |
| 2403 | fp->hdr.type = BINDER_TYPE_WEAK_BINDER; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2404 | fp->binder = node->ptr; |
| 2405 | fp->cookie = node->cookie; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2406 | if (node->proc) |
| 2407 | binder_inner_proc_lock(node->proc); |
| 2408 | binder_inc_node_nilocked(node, |
| 2409 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2410 | 0, NULL); |
| 2411 | if (node->proc) |
| 2412 | binder_inner_proc_unlock(node->proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2413 | trace_binder_transaction_ref_to_node(t, node, &src_rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2414 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2415 | " ref %d desc %d -> node %d u%016llx\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2416 | src_rdata.debug_id, src_rdata.desc, node->debug_id, |
| 2417 | (u64)node->ptr); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2418 | binder_node_unlock(node); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2419 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2420 | struct binder_ref_data dest_rdata; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2421 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2422 | binder_node_unlock(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2423 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2424 | fp->hdr.type == BINDER_TYPE_HANDLE, |
| 2425 | NULL, &dest_rdata); |
| 2426 | if (ret) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2427 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2428 | |
| 2429 | fp->binder = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2430 | fp->handle = dest_rdata.desc; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2431 | fp->cookie = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2432 | trace_binder_transaction_ref_to_ref(t, node, &src_rdata, |
| 2433 | &dest_rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2434 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2435 | " ref %d desc %d -> ref %d desc %d (node %d)\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2436 | src_rdata.debug_id, src_rdata.desc, |
| 2437 | dest_rdata.debug_id, dest_rdata.desc, |
| 2438 | node->debug_id); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2439 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2440 | done: |
| 2441 | binder_put_node(node); |
| 2442 | return ret; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | static int binder_translate_fd(int fd, |
| 2446 | struct binder_transaction *t, |
| 2447 | struct binder_thread *thread, |
| 2448 | struct binder_transaction *in_reply_to) |
| 2449 | { |
| 2450 | struct binder_proc *proc = thread->proc; |
| 2451 | struct binder_proc *target_proc = t->to_proc; |
| 2452 | int target_fd; |
| 2453 | struct file *file; |
| 2454 | int ret; |
| 2455 | bool target_allows_fd; |
| 2456 | |
| 2457 | if (in_reply_to) |
| 2458 | target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS); |
| 2459 | else |
| 2460 | target_allows_fd = t->buffer->target_node->accept_fds; |
| 2461 | if (!target_allows_fd) { |
| 2462 | binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n", |
| 2463 | proc->pid, thread->pid, |
| 2464 | in_reply_to ? "reply" : "transaction", |
| 2465 | fd); |
| 2466 | ret = -EPERM; |
| 2467 | goto err_fd_not_accepted; |
| 2468 | } |
| 2469 | |
| 2470 | file = fget(fd); |
| 2471 | if (!file) { |
| 2472 | binder_user_error("%d:%d got transaction with invalid fd, %d\n", |
| 2473 | proc->pid, thread->pid, fd); |
| 2474 | ret = -EBADF; |
| 2475 | goto err_fget; |
| 2476 | } |
| 2477 | ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file); |
| 2478 | if (ret < 0) { |
| 2479 | ret = -EPERM; |
| 2480 | goto err_security; |
| 2481 | } |
| 2482 | |
| 2483 | target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC); |
| 2484 | if (target_fd < 0) { |
| 2485 | ret = -ENOMEM; |
| 2486 | goto err_get_unused_fd; |
| 2487 | } |
| 2488 | task_fd_install(target_proc, target_fd, file); |
| 2489 | trace_binder_transaction_fd(t, fd, target_fd); |
| 2490 | binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n", |
| 2491 | fd, target_fd); |
| 2492 | |
| 2493 | return target_fd; |
| 2494 | |
| 2495 | err_get_unused_fd: |
| 2496 | err_security: |
| 2497 | fput(file); |
| 2498 | err_fget: |
| 2499 | err_fd_not_accepted: |
| 2500 | return ret; |
| 2501 | } |
| 2502 | |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2503 | static int binder_translate_fd_array(struct binder_fd_array_object *fda, |
| 2504 | struct binder_buffer_object *parent, |
| 2505 | struct binder_transaction *t, |
| 2506 | struct binder_thread *thread, |
| 2507 | struct binder_transaction *in_reply_to) |
| 2508 | { |
| 2509 | binder_size_t fdi, fd_buf_size, num_installed_fds; |
| 2510 | int target_fd; |
| 2511 | uintptr_t parent_buffer; |
| 2512 | u32 *fd_array; |
| 2513 | struct binder_proc *proc = thread->proc; |
| 2514 | struct binder_proc *target_proc = t->to_proc; |
| 2515 | |
| 2516 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2517 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2518 | binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n", |
| 2519 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2520 | return -EINVAL; |
| 2521 | } |
| 2522 | if (fd_buf_size > parent->length || |
| 2523 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2524 | /* No space for all file descriptors here. */ |
| 2525 | binder_user_error("%d:%d not enough space to store %lld fds in buffer\n", |
| 2526 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2527 | return -EINVAL; |
| 2528 | } |
| 2529 | /* |
| 2530 | * Since the parent was already fixed up, convert it |
| 2531 | * back to the kernel address space to access it |
| 2532 | */ |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2533 | parent_buffer = parent->buffer - |
| 2534 | binder_alloc_get_user_buffer_offset(&target_proc->alloc); |
Arnd Bergmann | 1c363eae | 2017-09-05 10:56:13 +0200 | [diff] [blame] | 2535 | fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2536 | if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) { |
| 2537 | binder_user_error("%d:%d parent offset not aligned correctly.\n", |
| 2538 | proc->pid, thread->pid); |
| 2539 | return -EINVAL; |
| 2540 | } |
| 2541 | for (fdi = 0; fdi < fda->num_fds; fdi++) { |
| 2542 | target_fd = binder_translate_fd(fd_array[fdi], t, thread, |
| 2543 | in_reply_to); |
| 2544 | if (target_fd < 0) |
| 2545 | goto err_translate_fd_failed; |
| 2546 | fd_array[fdi] = target_fd; |
| 2547 | } |
| 2548 | return 0; |
| 2549 | |
| 2550 | err_translate_fd_failed: |
| 2551 | /* |
| 2552 | * Failed to allocate fd or security error, free fds |
| 2553 | * installed so far. |
| 2554 | */ |
| 2555 | num_installed_fds = fdi; |
| 2556 | for (fdi = 0; fdi < num_installed_fds; fdi++) |
| 2557 | task_close_fd(target_proc, fd_array[fdi]); |
| 2558 | return target_fd; |
| 2559 | } |
| 2560 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2561 | static int binder_fixup_parent(struct binder_transaction *t, |
| 2562 | struct binder_thread *thread, |
| 2563 | struct binder_buffer_object *bp, |
| 2564 | binder_size_t *off_start, |
| 2565 | binder_size_t num_valid, |
| 2566 | struct binder_buffer_object *last_fixup_obj, |
| 2567 | binder_size_t last_fixup_min_off) |
| 2568 | { |
| 2569 | struct binder_buffer_object *parent; |
| 2570 | u8 *parent_buffer; |
| 2571 | struct binder_buffer *b = t->buffer; |
| 2572 | struct binder_proc *proc = thread->proc; |
| 2573 | struct binder_proc *target_proc = t->to_proc; |
| 2574 | |
| 2575 | if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT)) |
| 2576 | return 0; |
| 2577 | |
| 2578 | parent = binder_validate_ptr(b, bp->parent, off_start, num_valid); |
| 2579 | if (!parent) { |
| 2580 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 2581 | proc->pid, thread->pid); |
| 2582 | return -EINVAL; |
| 2583 | } |
| 2584 | |
| 2585 | if (!binder_validate_fixup(b, off_start, |
| 2586 | parent, bp->parent_offset, |
| 2587 | last_fixup_obj, |
| 2588 | last_fixup_min_off)) { |
| 2589 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 2590 | proc->pid, thread->pid); |
| 2591 | return -EINVAL; |
| 2592 | } |
| 2593 | |
| 2594 | if (parent->length < sizeof(binder_uintptr_t) || |
| 2595 | bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) { |
| 2596 | /* No space for a pointer here! */ |
| 2597 | binder_user_error("%d:%d got transaction with invalid parent offset\n", |
| 2598 | proc->pid, thread->pid); |
| 2599 | return -EINVAL; |
| 2600 | } |
Arnd Bergmann | 1c363eae | 2017-09-05 10:56:13 +0200 | [diff] [blame] | 2601 | parent_buffer = (u8 *)((uintptr_t)parent->buffer - |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2602 | binder_alloc_get_user_buffer_offset( |
| 2603 | &target_proc->alloc)); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2604 | *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer; |
| 2605 | |
| 2606 | return 0; |
| 2607 | } |
| 2608 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2609 | /** |
| 2610 | * binder_proc_transaction() - sends a transaction to a process and wakes it up |
| 2611 | * @t: transaction to send |
| 2612 | * @proc: process to send the transaction to |
| 2613 | * @thread: thread in @proc to send the transaction to (may be NULL) |
| 2614 | * |
| 2615 | * This function queues a transaction to the specified process. It will try |
| 2616 | * to find a thread in the target process to handle the transaction and |
| 2617 | * wake it up. If no thread is found, the work is queued to the proc |
| 2618 | * waitqueue. |
| 2619 | * |
| 2620 | * If the @thread parameter is not NULL, the transaction is always queued |
| 2621 | * to the waitlist of that specific thread. |
| 2622 | * |
| 2623 | * Return: true if the transactions was successfully queued |
| 2624 | * false if the target process or thread is dead |
| 2625 | */ |
| 2626 | static bool binder_proc_transaction(struct binder_transaction *t, |
| 2627 | struct binder_proc *proc, |
| 2628 | struct binder_thread *thread) |
| 2629 | { |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2630 | struct binder_node *node = t->buffer->target_node; |
| 2631 | bool oneway = !!(t->flags & TF_ONE_WAY); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2632 | bool pending_async = false; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2633 | |
| 2634 | BUG_ON(!node); |
| 2635 | binder_node_lock(node); |
| 2636 | if (oneway) { |
| 2637 | BUG_ON(thread); |
| 2638 | if (node->has_async_transaction) { |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2639 | pending_async = true; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2640 | } else { |
Gustavo A. R. Silva | 197410a | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 2641 | node->has_async_transaction = true; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | binder_inner_proc_lock(proc); |
| 2646 | |
| 2647 | if (proc->is_dead || (thread && thread->is_dead)) { |
| 2648 | binder_inner_proc_unlock(proc); |
| 2649 | binder_node_unlock(node); |
| 2650 | return false; |
| 2651 | } |
| 2652 | |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2653 | if (!thread && !pending_async) |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2654 | thread = binder_select_thread_ilocked(proc); |
| 2655 | |
| 2656 | if (thread) |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2657 | binder_enqueue_thread_work_ilocked(thread, &t->work); |
| 2658 | else if (!pending_async) |
| 2659 | binder_enqueue_work_ilocked(&t->work, &proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2660 | else |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2661 | binder_enqueue_work_ilocked(&t->work, &node->async_todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2662 | |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2663 | if (!pending_async) |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2664 | binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */); |
| 2665 | |
| 2666 | binder_inner_proc_unlock(proc); |
| 2667 | binder_node_unlock(node); |
| 2668 | |
| 2669 | return true; |
| 2670 | } |
| 2671 | |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2672 | /** |
| 2673 | * binder_get_node_refs_for_txn() - Get required refs on node for txn |
| 2674 | * @node: struct binder_node for which to get refs |
| 2675 | * @proc: returns @node->proc if valid |
| 2676 | * @error: if no @proc then returns BR_DEAD_REPLY |
| 2677 | * |
| 2678 | * User-space normally keeps the node alive when creating a transaction |
| 2679 | * since it has a reference to the target. The local strong ref keeps it |
| 2680 | * alive if the sending process dies before the target process processes |
| 2681 | * the transaction. If the source process is malicious or has a reference |
| 2682 | * counting bug, relying on the local strong ref can fail. |
| 2683 | * |
| 2684 | * Since user-space can cause the local strong ref to go away, we also take |
| 2685 | * a tmpref on the node to ensure it survives while we are constructing |
| 2686 | * the transaction. We also need a tmpref on the proc while we are |
| 2687 | * constructing the transaction, so we take that here as well. |
| 2688 | * |
| 2689 | * Return: The target_node with refs taken or NULL if no @node->proc is NULL. |
| 2690 | * Also sets @proc if valid. If the @node->proc is NULL indicating that the |
| 2691 | * target proc has died, @error is set to BR_DEAD_REPLY |
| 2692 | */ |
| 2693 | static struct binder_node *binder_get_node_refs_for_txn( |
| 2694 | struct binder_node *node, |
| 2695 | struct binder_proc **procp, |
| 2696 | uint32_t *error) |
| 2697 | { |
| 2698 | struct binder_node *target_node = NULL; |
| 2699 | |
| 2700 | binder_node_inner_lock(node); |
| 2701 | if (node->proc) { |
| 2702 | target_node = node; |
| 2703 | binder_inc_node_nilocked(node, 1, 0, NULL); |
| 2704 | binder_inc_node_tmpref_ilocked(node); |
| 2705 | node->proc->tmp_ref++; |
| 2706 | *procp = node->proc; |
| 2707 | } else |
| 2708 | *error = BR_DEAD_REPLY; |
| 2709 | binder_node_inner_unlock(node); |
| 2710 | |
| 2711 | return target_node; |
| 2712 | } |
| 2713 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2714 | static void binder_transaction(struct binder_proc *proc, |
| 2715 | struct binder_thread *thread, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2716 | struct binder_transaction_data *tr, int reply, |
| 2717 | binder_size_t extra_buffers_size) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2718 | { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2719 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2720 | struct binder_transaction *t; |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame^] | 2721 | struct binder_work *w; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2722 | struct binder_work *tcomplete; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2723 | binder_size_t *offp, *off_end, *off_start; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 2724 | binder_size_t off_min; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2725 | u8 *sg_bufp, *sg_buf_end; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2726 | struct binder_proc *target_proc = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2727 | struct binder_thread *target_thread = NULL; |
| 2728 | struct binder_node *target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2729 | struct binder_transaction *in_reply_to = NULL; |
| 2730 | struct binder_transaction_log_entry *e; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2731 | uint32_t return_error = 0; |
| 2732 | uint32_t return_error_param = 0; |
| 2733 | uint32_t return_error_line = 0; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2734 | struct binder_buffer_object *last_fixup_obj = NULL; |
| 2735 | binder_size_t last_fixup_min_off = 0; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 2736 | struct binder_context *context = proc->context; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2737 | int t_debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2738 | |
| 2739 | e = binder_transaction_log_add(&binder_transaction_log); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2740 | e->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2741 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); |
| 2742 | e->from_proc = proc->pid; |
| 2743 | e->from_thread = thread->pid; |
| 2744 | e->target_handle = tr->target.handle; |
| 2745 | e->data_size = tr->data_size; |
| 2746 | e->offsets_size = tr->offsets_size; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 2747 | e->context_name = proc->context->name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2748 | |
| 2749 | if (reply) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2750 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2751 | in_reply_to = thread->transaction_stack; |
| 2752 | if (in_reply_to == NULL) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2753 | binder_inner_proc_unlock(proc); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2754 | 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] | 2755 | proc->pid, thread->pid); |
| 2756 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2757 | return_error_param = -EPROTO; |
| 2758 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2759 | goto err_empty_call_stack; |
| 2760 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2761 | if (in_reply_to->to_thread != thread) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2762 | spin_lock(&in_reply_to->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2763 | 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] | 2764 | proc->pid, thread->pid, in_reply_to->debug_id, |
| 2765 | in_reply_to->to_proc ? |
| 2766 | in_reply_to->to_proc->pid : 0, |
| 2767 | in_reply_to->to_thread ? |
| 2768 | in_reply_to->to_thread->pid : 0); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2769 | spin_unlock(&in_reply_to->lock); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2770 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2771 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2772 | return_error_param = -EPROTO; |
| 2773 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2774 | in_reply_to = NULL; |
| 2775 | goto err_bad_call_stack; |
| 2776 | } |
| 2777 | thread->transaction_stack = in_reply_to->to_parent; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2778 | binder_inner_proc_unlock(proc); |
| 2779 | binder_set_nice(in_reply_to->saved_priority); |
| 2780 | 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] | 2781 | if (target_thread == NULL) { |
| 2782 | return_error = BR_DEAD_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2783 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2784 | goto err_dead_binder; |
| 2785 | } |
| 2786 | if (target_thread->transaction_stack != in_reply_to) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2787 | 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] | 2788 | proc->pid, thread->pid, |
| 2789 | target_thread->transaction_stack ? |
| 2790 | target_thread->transaction_stack->debug_id : 0, |
| 2791 | in_reply_to->debug_id); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2792 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2793 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2794 | return_error_param = -EPROTO; |
| 2795 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2796 | in_reply_to = NULL; |
| 2797 | target_thread = NULL; |
| 2798 | goto err_dead_binder; |
| 2799 | } |
| 2800 | target_proc = target_thread->proc; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2801 | target_proc->tmp_ref++; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2802 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2803 | } else { |
| 2804 | if (tr->target.handle) { |
| 2805 | struct binder_ref *ref; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2806 | |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2807 | /* |
| 2808 | * There must already be a strong ref |
| 2809 | * on this node. If so, do a strong |
| 2810 | * increment on the node to ensure it |
| 2811 | * stays alive until the transaction is |
| 2812 | * done. |
| 2813 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 2814 | binder_proc_lock(proc); |
| 2815 | ref = binder_get_ref_olocked(proc, tr->target.handle, |
| 2816 | true); |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2817 | if (ref) { |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2818 | target_node = binder_get_node_refs_for_txn( |
| 2819 | ref->node, &target_proc, |
| 2820 | &return_error); |
| 2821 | } else { |
| 2822 | binder_user_error("%d:%d got transaction to invalid handle\n", |
| 2823 | proc->pid, thread->pid); |
| 2824 | return_error = BR_FAILED_REPLY; |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2825 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 2826 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2827 | } else { |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 2828 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 2829 | target_node = context->binder_context_mgr_node; |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2830 | if (target_node) |
| 2831 | target_node = binder_get_node_refs_for_txn( |
| 2832 | target_node, &target_proc, |
| 2833 | &return_error); |
| 2834 | else |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2835 | return_error = BR_DEAD_REPLY; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 2836 | mutex_unlock(&context->context_mgr_node_lock); |
Martijn Coenen | 7aa135f | 2018-03-28 11:14:50 +0200 | [diff] [blame] | 2837 | if (target_node && target_proc == proc) { |
| 2838 | binder_user_error("%d:%d got transaction to context manager from process owning it\n", |
| 2839 | proc->pid, thread->pid); |
| 2840 | return_error = BR_FAILED_REPLY; |
| 2841 | return_error_param = -EINVAL; |
| 2842 | return_error_line = __LINE__; |
| 2843 | goto err_invalid_target_handle; |
| 2844 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2845 | } |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2846 | if (!target_node) { |
| 2847 | /* |
| 2848 | * return_error is set above |
| 2849 | */ |
| 2850 | return_error_param = -EINVAL; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2851 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2852 | goto err_dead_binder; |
| 2853 | } |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2854 | e->to_node = target_node->debug_id; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2855 | if (security_binder_transaction(proc->tsk, |
| 2856 | target_proc->tsk) < 0) { |
| 2857 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2858 | return_error_param = -EPERM; |
| 2859 | return_error_line = __LINE__; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2860 | goto err_invalid_target_handle; |
| 2861 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2862 | binder_inner_proc_lock(proc); |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame^] | 2863 | |
| 2864 | w = list_first_entry_or_null(&thread->todo, |
| 2865 | struct binder_work, entry); |
| 2866 | if (!(tr->flags & TF_ONE_WAY) && w && |
| 2867 | w->type == BINDER_WORK_TRANSACTION) { |
| 2868 | /* |
| 2869 | * Do not allow new outgoing transaction from a |
| 2870 | * thread that has a transaction at the head of |
| 2871 | * its todo list. Only need to check the head |
| 2872 | * because binder_select_thread_ilocked picks a |
| 2873 | * thread from proc->waiting_threads to enqueue |
| 2874 | * the transaction, and nothing is queued to the |
| 2875 | * todo list while the thread is on waiting_threads. |
| 2876 | */ |
| 2877 | binder_user_error("%d:%d new transaction not allowed when there is a transaction on thread todo\n", |
| 2878 | proc->pid, thread->pid); |
| 2879 | binder_inner_proc_unlock(proc); |
| 2880 | return_error = BR_FAILED_REPLY; |
| 2881 | return_error_param = -EPROTO; |
| 2882 | return_error_line = __LINE__; |
| 2883 | goto err_bad_todo_list; |
| 2884 | } |
| 2885 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2886 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { |
| 2887 | struct binder_transaction *tmp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2888 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2889 | tmp = thread->transaction_stack; |
| 2890 | if (tmp->to_thread != thread) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2891 | spin_lock(&tmp->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2892 | 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] | 2893 | proc->pid, thread->pid, tmp->debug_id, |
| 2894 | tmp->to_proc ? tmp->to_proc->pid : 0, |
| 2895 | tmp->to_thread ? |
| 2896 | tmp->to_thread->pid : 0); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2897 | spin_unlock(&tmp->lock); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2898 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2899 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2900 | return_error_param = -EPROTO; |
| 2901 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2902 | goto err_bad_call_stack; |
| 2903 | } |
| 2904 | while (tmp) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2905 | struct binder_thread *from; |
| 2906 | |
| 2907 | spin_lock(&tmp->lock); |
| 2908 | from = tmp->from; |
| 2909 | if (from && from->proc == target_proc) { |
| 2910 | atomic_inc(&from->tmp_ref); |
| 2911 | target_thread = from; |
| 2912 | spin_unlock(&tmp->lock); |
| 2913 | break; |
| 2914 | } |
| 2915 | spin_unlock(&tmp->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2916 | tmp = tmp->from_parent; |
| 2917 | } |
| 2918 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2919 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2920 | } |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2921 | if (target_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2922 | e->to_thread = target_thread->pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2923 | e->to_proc = target_proc->pid; |
| 2924 | |
| 2925 | /* TODO: reuse incoming transaction for reply */ |
| 2926 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 2927 | if (t == NULL) { |
| 2928 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2929 | return_error_param = -ENOMEM; |
| 2930 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2931 | goto err_alloc_t_failed; |
| 2932 | } |
| 2933 | binder_stats_created(BINDER_STAT_TRANSACTION); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2934 | spin_lock_init(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2935 | |
| 2936 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); |
| 2937 | if (tcomplete == NULL) { |
| 2938 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2939 | return_error_param = -ENOMEM; |
| 2940 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2941 | goto err_alloc_tcomplete_failed; |
| 2942 | } |
| 2943 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); |
| 2944 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2945 | t->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2946 | |
| 2947 | if (reply) |
| 2948 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2949 | "%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] | 2950 | proc->pid, thread->pid, t->debug_id, |
| 2951 | target_proc->pid, target_thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2952 | (u64)tr->data.ptr.buffer, |
| 2953 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2954 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 2955 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2956 | else |
| 2957 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2958 | "%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] | 2959 | proc->pid, thread->pid, t->debug_id, |
| 2960 | target_proc->pid, target_node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2961 | (u64)tr->data.ptr.buffer, |
| 2962 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2963 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 2964 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2965 | |
| 2966 | if (!reply && !(tr->flags & TF_ONE_WAY)) |
| 2967 | t->from = thread; |
| 2968 | else |
| 2969 | t->from = NULL; |
Tair Rzayev | 57bab7c | 2014-05-31 22:43:34 +0300 | [diff] [blame] | 2970 | t->sender_euid = task_euid(proc->tsk); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2971 | t->to_proc = target_proc; |
| 2972 | t->to_thread = target_thread; |
| 2973 | t->code = tr->code; |
| 2974 | t->flags = tr->flags; |
| 2975 | t->priority = task_nice(current); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2976 | |
| 2977 | trace_binder_transaction(reply, t, target_node); |
| 2978 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2979 | t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2980 | tr->offsets_size, extra_buffers_size, |
| 2981 | !reply && (t->flags & TF_ONE_WAY)); |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2982 | if (IS_ERR(t->buffer)) { |
| 2983 | /* |
| 2984 | * -ESRCH indicates VMA cleared. The target is dying. |
| 2985 | */ |
| 2986 | return_error_param = PTR_ERR(t->buffer); |
| 2987 | return_error = return_error_param == -ESRCH ? |
| 2988 | BR_DEAD_REPLY : BR_FAILED_REPLY; |
| 2989 | return_error_line = __LINE__; |
| 2990 | t->buffer = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2991 | goto err_binder_alloc_buf_failed; |
| 2992 | } |
| 2993 | t->buffer->allow_user_free = 0; |
| 2994 | t->buffer->debug_id = t->debug_id; |
| 2995 | t->buffer->transaction = t; |
| 2996 | t->buffer->target_node = target_node; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2997 | trace_binder_transaction_alloc_buf(t->buffer); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2998 | off_start = (binder_size_t *)(t->buffer->data + |
| 2999 | ALIGN(tr->data_size, sizeof(void *))); |
| 3000 | offp = off_start; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3001 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3002 | if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t) |
| 3003 | tr->data.ptr.buffer, tr->data_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3004 | binder_user_error("%d:%d got transaction with invalid data ptr\n", |
| 3005 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3006 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3007 | return_error_param = -EFAULT; |
| 3008 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3009 | goto err_copy_data_failed; |
| 3010 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3011 | if (copy_from_user(offp, (const void __user *)(uintptr_t) |
| 3012 | tr->data.ptr.offsets, tr->offsets_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3013 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3014 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3015 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3016 | return_error_param = -EFAULT; |
| 3017 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3018 | goto err_copy_data_failed; |
| 3019 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3020 | if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) { |
| 3021 | binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n", |
| 3022 | proc->pid, thread->pid, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3023 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3024 | return_error_param = -EINVAL; |
| 3025 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3026 | goto err_bad_offset; |
| 3027 | } |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3028 | if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) { |
| 3029 | binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n", |
| 3030 | proc->pid, thread->pid, |
| 3031 | (u64)extra_buffers_size); |
| 3032 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3033 | return_error_param = -EINVAL; |
| 3034 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3035 | goto err_bad_offset; |
| 3036 | } |
| 3037 | off_end = (void *)off_start + tr->offsets_size; |
| 3038 | sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *))); |
| 3039 | sg_buf_end = sg_bufp + extra_buffers_size; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3040 | off_min = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3041 | for (; offp < off_end; offp++) { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3042 | struct binder_object_header *hdr; |
| 3043 | size_t object_size = binder_validate_object(t->buffer, *offp); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3044 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3045 | if (object_size == 0 || *offp < off_min) { |
| 3046 | 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] | 3047 | proc->pid, thread->pid, (u64)*offp, |
| 3048 | (u64)off_min, |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3049 | (u64)t->buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3050 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3051 | return_error_param = -EINVAL; |
| 3052 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3053 | goto err_bad_offset; |
| 3054 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3055 | |
| 3056 | hdr = (struct binder_object_header *)(t->buffer->data + *offp); |
| 3057 | off_min = *offp + object_size; |
| 3058 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3059 | case BINDER_TYPE_BINDER: |
| 3060 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3061 | struct flat_binder_object *fp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3062 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3063 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3064 | ret = binder_translate_binder(fp, t, thread); |
| 3065 | if (ret < 0) { |
Christian Engelmayer | 7d42043 | 2014-05-07 21:44:53 +0200 | [diff] [blame] | 3066 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3067 | return_error_param = ret; |
| 3068 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3069 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3070 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3071 | } break; |
| 3072 | case BINDER_TYPE_HANDLE: |
| 3073 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3074 | struct flat_binder_object *fp; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 3075 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3076 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3077 | ret = binder_translate_handle(fp, t, thread); |
| 3078 | if (ret < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3079 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3080 | return_error_param = ret; |
| 3081 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3082 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3083 | } |
| 3084 | } break; |
| 3085 | |
| 3086 | case BINDER_TYPE_FD: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3087 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3088 | int target_fd = binder_translate_fd(fp->fd, t, thread, |
| 3089 | in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3090 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3091 | if (target_fd < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3092 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3093 | return_error_param = target_fd; |
| 3094 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3095 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3096 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3097 | fp->pad_binder = 0; |
| 3098 | fp->fd = target_fd; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3099 | } break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3100 | case BINDER_TYPE_FDA: { |
| 3101 | struct binder_fd_array_object *fda = |
| 3102 | to_binder_fd_array_object(hdr); |
| 3103 | struct binder_buffer_object *parent = |
| 3104 | binder_validate_ptr(t->buffer, fda->parent, |
| 3105 | off_start, |
| 3106 | offp - off_start); |
| 3107 | if (!parent) { |
| 3108 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 3109 | proc->pid, thread->pid); |
| 3110 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3111 | return_error_param = -EINVAL; |
| 3112 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3113 | goto err_bad_parent; |
| 3114 | } |
| 3115 | if (!binder_validate_fixup(t->buffer, off_start, |
| 3116 | parent, fda->parent_offset, |
| 3117 | last_fixup_obj, |
| 3118 | last_fixup_min_off)) { |
| 3119 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 3120 | proc->pid, thread->pid); |
| 3121 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3122 | return_error_param = -EINVAL; |
| 3123 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3124 | goto err_bad_parent; |
| 3125 | } |
| 3126 | ret = binder_translate_fd_array(fda, parent, t, thread, |
| 3127 | in_reply_to); |
| 3128 | if (ret < 0) { |
| 3129 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3130 | return_error_param = ret; |
| 3131 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3132 | goto err_translate_failed; |
| 3133 | } |
| 3134 | last_fixup_obj = parent; |
| 3135 | last_fixup_min_off = |
| 3136 | fda->parent_offset + sizeof(u32) * fda->num_fds; |
| 3137 | } break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3138 | case BINDER_TYPE_PTR: { |
| 3139 | struct binder_buffer_object *bp = |
| 3140 | to_binder_buffer_object(hdr); |
| 3141 | size_t buf_left = sg_buf_end - sg_bufp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3142 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3143 | if (bp->length > buf_left) { |
| 3144 | binder_user_error("%d:%d got transaction with too large buffer\n", |
| 3145 | proc->pid, thread->pid); |
| 3146 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3147 | return_error_param = -EINVAL; |
| 3148 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3149 | goto err_bad_offset; |
| 3150 | } |
| 3151 | if (copy_from_user(sg_bufp, |
| 3152 | (const void __user *)(uintptr_t) |
| 3153 | bp->buffer, bp->length)) { |
| 3154 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3155 | proc->pid, thread->pid); |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3156 | return_error_param = -EFAULT; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3157 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3158 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3159 | goto err_copy_data_failed; |
| 3160 | } |
| 3161 | /* Fixup buffer pointer to target proc address space */ |
| 3162 | bp->buffer = (uintptr_t)sg_bufp + |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 3163 | binder_alloc_get_user_buffer_offset( |
| 3164 | &target_proc->alloc); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3165 | sg_bufp += ALIGN(bp->length, sizeof(u64)); |
| 3166 | |
| 3167 | ret = binder_fixup_parent(t, thread, bp, off_start, |
| 3168 | offp - off_start, |
| 3169 | last_fixup_obj, |
| 3170 | last_fixup_min_off); |
| 3171 | if (ret < 0) { |
| 3172 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3173 | return_error_param = ret; |
| 3174 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3175 | goto err_translate_failed; |
| 3176 | } |
| 3177 | last_fixup_obj = bp; |
| 3178 | last_fixup_min_off = 0; |
| 3179 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3180 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 3181 | 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] | 3182 | proc->pid, thread->pid, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3183 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3184 | return_error_param = -EINVAL; |
| 3185 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3186 | goto err_bad_object_type; |
| 3187 | } |
| 3188 | } |
Todd Kjos | ccae6f6 | 2017-06-29 12:01:48 -0700 | [diff] [blame] | 3189 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3190 | t->work.type = BINDER_WORK_TRANSACTION; |
Todd Kjos | ccae6f6 | 2017-06-29 12:01:48 -0700 | [diff] [blame] | 3191 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3192 | if (reply) { |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3193 | binder_enqueue_thread_work(thread, tcomplete); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3194 | binder_inner_proc_lock(target_proc); |
| 3195 | if (target_thread->is_dead) { |
| 3196 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3197 | goto err_dead_proc_or_thread; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3198 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3199 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3200 | binder_pop_transaction_ilocked(target_thread, in_reply_to); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3201 | binder_enqueue_thread_work_ilocked(target_thread, &t->work); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3202 | binder_inner_proc_unlock(target_proc); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3203 | wake_up_interruptible_sync(&target_thread->wait); |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 3204 | binder_free_transaction(in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3205 | } else if (!(t->flags & TF_ONE_WAY)) { |
| 3206 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3207 | binder_inner_proc_lock(proc); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3208 | /* |
| 3209 | * Defer the TRANSACTION_COMPLETE, so we don't return to |
| 3210 | * userspace immediately; this allows the target process to |
| 3211 | * immediately start processing this transaction, reducing |
| 3212 | * latency. We will then return the TRANSACTION_COMPLETE when |
| 3213 | * the target replies (or there is an error). |
| 3214 | */ |
| 3215 | binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3216 | t->need_reply = 1; |
| 3217 | t->from_parent = thread->transaction_stack; |
| 3218 | thread->transaction_stack = t; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3219 | binder_inner_proc_unlock(proc); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3220 | if (!binder_proc_transaction(t, target_proc, target_thread)) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3221 | binder_inner_proc_lock(proc); |
| 3222 | binder_pop_transaction_ilocked(thread, t); |
| 3223 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3224 | goto err_dead_proc_or_thread; |
| 3225 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3226 | } else { |
| 3227 | BUG_ON(target_node == NULL); |
| 3228 | BUG_ON(t->buffer->async_transaction != 1); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3229 | binder_enqueue_thread_work(thread, tcomplete); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3230 | if (!binder_proc_transaction(t, target_proc, NULL)) |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3231 | goto err_dead_proc_or_thread; |
Riley Andrews | 00b40d6 | 2017-06-29 12:01:37 -0700 | [diff] [blame] | 3232 | } |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3233 | if (target_thread) |
| 3234 | binder_thread_dec_tmpref(target_thread); |
| 3235 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3236 | if (target_node) |
| 3237 | binder_dec_node_tmpref(target_node); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 3238 | /* |
| 3239 | * write barrier to synchronize with initialization |
| 3240 | * of log entry |
| 3241 | */ |
| 3242 | smp_wmb(); |
| 3243 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3244 | return; |
| 3245 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3246 | err_dead_proc_or_thread: |
| 3247 | return_error = BR_DEAD_REPLY; |
| 3248 | return_error_line = __LINE__; |
Xu YiPing | d53bebd | 2017-09-05 10:21:52 -0700 | [diff] [blame] | 3249 | binder_dequeue_work(proc, tcomplete); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3250 | err_translate_failed: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3251 | err_bad_object_type: |
| 3252 | err_bad_offset: |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3253 | err_bad_parent: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3254 | err_copy_data_failed: |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3255 | trace_binder_transaction_failed_buffer_release(t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3256 | binder_transaction_buffer_release(target_proc, t->buffer, offp); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3257 | if (target_node) |
| 3258 | binder_dec_node_tmpref(target_node); |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3259 | target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3260 | t->buffer->transaction = NULL; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 3261 | binder_alloc_free_buf(&target_proc->alloc, t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3262 | err_binder_alloc_buf_failed: |
| 3263 | kfree(tcomplete); |
| 3264 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3265 | err_alloc_tcomplete_failed: |
| 3266 | kfree(t); |
| 3267 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 3268 | err_alloc_t_failed: |
Sherry Yang | 44b7396 | 2018-08-13 17:28:53 -0700 | [diff] [blame^] | 3269 | err_bad_todo_list: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3270 | err_bad_call_stack: |
| 3271 | err_empty_call_stack: |
| 3272 | err_dead_binder: |
| 3273 | err_invalid_target_handle: |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3274 | if (target_thread) |
| 3275 | binder_thread_dec_tmpref(target_thread); |
| 3276 | if (target_proc) |
| 3277 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3278 | if (target_node) { |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3279 | binder_dec_node(target_node, 1, 0); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3280 | binder_dec_node_tmpref(target_node); |
| 3281 | } |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3282 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3283 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3284 | "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n", |
| 3285 | proc->pid, thread->pid, return_error, return_error_param, |
| 3286 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3287 | return_error_line); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3288 | |
| 3289 | { |
| 3290 | struct binder_transaction_log_entry *fe; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3291 | |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3292 | e->return_error = return_error; |
| 3293 | e->return_error_param = return_error_param; |
| 3294 | e->return_error_line = return_error_line; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3295 | fe = binder_transaction_log_add(&binder_transaction_log_failed); |
| 3296 | *fe = *e; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 3297 | /* |
| 3298 | * write barrier to synchronize with initialization |
| 3299 | * of log entry |
| 3300 | */ |
| 3301 | smp_wmb(); |
| 3302 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
| 3303 | WRITE_ONCE(fe->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3304 | } |
| 3305 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3306 | BUG_ON(thread->return_error.cmd != BR_OK); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3307 | if (in_reply_to) { |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3308 | thread->return_error.cmd = BR_TRANSACTION_COMPLETE; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3309 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3310 | binder_send_failed_reply(in_reply_to, return_error); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3311 | } else { |
| 3312 | thread->return_error.cmd = return_error; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3313 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3314 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3315 | } |
| 3316 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3317 | static int binder_thread_write(struct binder_proc *proc, |
| 3318 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3319 | binder_uintptr_t binder_buffer, size_t size, |
| 3320 | binder_size_t *consumed) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3321 | { |
| 3322 | uint32_t cmd; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 3323 | struct binder_context *context = proc->context; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3324 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3325 | void __user *ptr = buffer + *consumed; |
| 3326 | void __user *end = buffer + size; |
| 3327 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3328 | while (ptr < end && thread->return_error.cmd == BR_OK) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3329 | int ret; |
| 3330 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3331 | if (get_user(cmd, (uint32_t __user *)ptr)) |
| 3332 | return -EFAULT; |
| 3333 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3334 | trace_binder_command(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3335 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 3336 | atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]); |
| 3337 | atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]); |
| 3338 | atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3339 | } |
| 3340 | switch (cmd) { |
| 3341 | case BC_INCREFS: |
| 3342 | case BC_ACQUIRE: |
| 3343 | case BC_RELEASE: |
| 3344 | case BC_DECREFS: { |
| 3345 | uint32_t target; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3346 | const char *debug_string; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3347 | bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE; |
| 3348 | bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE; |
| 3349 | struct binder_ref_data rdata; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3350 | |
| 3351 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3352 | return -EFAULT; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3353 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3354 | ptr += sizeof(uint32_t); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3355 | ret = -1; |
| 3356 | if (increment && !target) { |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3357 | struct binder_node *ctx_mgr_node; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3358 | mutex_lock(&context->context_mgr_node_lock); |
| 3359 | ctx_mgr_node = context->binder_context_mgr_node; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3360 | if (ctx_mgr_node) |
| 3361 | ret = binder_inc_ref_for_node( |
| 3362 | proc, ctx_mgr_node, |
| 3363 | strong, NULL, &rdata); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3364 | mutex_unlock(&context->context_mgr_node_lock); |
| 3365 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3366 | if (ret) |
| 3367 | ret = binder_update_ref_for_handle( |
| 3368 | proc, target, increment, strong, |
| 3369 | &rdata); |
| 3370 | if (!ret && rdata.desc != target) { |
| 3371 | binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n", |
| 3372 | proc->pid, thread->pid, |
| 3373 | target, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3374 | } |
| 3375 | switch (cmd) { |
| 3376 | case BC_INCREFS: |
| 3377 | debug_string = "IncRefs"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3378 | break; |
| 3379 | case BC_ACQUIRE: |
| 3380 | debug_string = "Acquire"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3381 | break; |
| 3382 | case BC_RELEASE: |
| 3383 | debug_string = "Release"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3384 | break; |
| 3385 | case BC_DECREFS: |
| 3386 | default: |
| 3387 | debug_string = "DecRefs"; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3388 | break; |
| 3389 | } |
| 3390 | if (ret) { |
| 3391 | binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n", |
| 3392 | proc->pid, thread->pid, debug_string, |
| 3393 | strong, target, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3394 | break; |
| 3395 | } |
| 3396 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3397 | "%d:%d %s ref %d desc %d s %d w %d\n", |
| 3398 | proc->pid, thread->pid, debug_string, |
| 3399 | rdata.debug_id, rdata.desc, rdata.strong, |
| 3400 | rdata.weak); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3401 | break; |
| 3402 | } |
| 3403 | case BC_INCREFS_DONE: |
| 3404 | case BC_ACQUIRE_DONE: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3405 | binder_uintptr_t node_ptr; |
| 3406 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3407 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3408 | bool free_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3409 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3410 | if (get_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3411 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3412 | ptr += sizeof(binder_uintptr_t); |
| 3413 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3414 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3415 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3416 | node = binder_get_node(proc, node_ptr); |
| 3417 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3418 | binder_user_error("%d:%d %s u%016llx no match\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3419 | proc->pid, thread->pid, |
| 3420 | cmd == BC_INCREFS_DONE ? |
| 3421 | "BC_INCREFS_DONE" : |
| 3422 | "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3423 | (u64)node_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3424 | break; |
| 3425 | } |
| 3426 | if (cookie != node->cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3427 | 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] | 3428 | proc->pid, thread->pid, |
| 3429 | cmd == BC_INCREFS_DONE ? |
| 3430 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3431 | (u64)node_ptr, node->debug_id, |
| 3432 | (u64)cookie, (u64)node->cookie); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3433 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3434 | break; |
| 3435 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3436 | binder_node_inner_lock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3437 | if (cmd == BC_ACQUIRE_DONE) { |
| 3438 | if (node->pending_strong_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3439 | 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] | 3440 | proc->pid, thread->pid, |
| 3441 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3442 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3443 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3444 | break; |
| 3445 | } |
| 3446 | node->pending_strong_ref = 0; |
| 3447 | } else { |
| 3448 | if (node->pending_weak_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3449 | 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] | 3450 | proc->pid, thread->pid, |
| 3451 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3452 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3453 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3454 | break; |
| 3455 | } |
| 3456 | node->pending_weak_ref = 0; |
| 3457 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3458 | free_node = binder_dec_node_nilocked(node, |
| 3459 | cmd == BC_ACQUIRE_DONE, 0); |
| 3460 | WARN_ON(free_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3461 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3462 | "%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] | 3463 | proc->pid, thread->pid, |
| 3464 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3465 | node->debug_id, node->local_strong_refs, |
| 3466 | node->local_weak_refs, node->tmp_refs); |
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 | case BC_ATTEMPT_ACQUIRE: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3472 | pr_err("BC_ATTEMPT_ACQUIRE not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3473 | return -EINVAL; |
| 3474 | case BC_ACQUIRE_RESULT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3475 | pr_err("BC_ACQUIRE_RESULT not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3476 | return -EINVAL; |
| 3477 | |
| 3478 | case BC_FREE_BUFFER: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3479 | binder_uintptr_t data_ptr; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3480 | struct binder_buffer *buffer; |
| 3481 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3482 | if (get_user(data_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3483 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3484 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3485 | |
Todd Kjos | 53d311cf | 2017-06-29 12:01:51 -0700 | [diff] [blame] | 3486 | buffer = binder_alloc_prepare_to_free(&proc->alloc, |
| 3487 | data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3488 | if (buffer == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3489 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n", |
| 3490 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3491 | break; |
| 3492 | } |
| 3493 | if (!buffer->allow_user_free) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3494 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n", |
| 3495 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3496 | break; |
| 3497 | } |
| 3498 | binder_debug(BINDER_DEBUG_FREE_BUFFER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3499 | "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n", |
| 3500 | proc->pid, thread->pid, (u64)data_ptr, |
| 3501 | buffer->debug_id, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3502 | buffer->transaction ? "active" : "finished"); |
| 3503 | |
| 3504 | if (buffer->transaction) { |
| 3505 | buffer->transaction->buffer = NULL; |
| 3506 | buffer->transaction = NULL; |
| 3507 | } |
| 3508 | if (buffer->async_transaction && buffer->target_node) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3509 | struct binder_node *buf_node; |
| 3510 | struct binder_work *w; |
| 3511 | |
| 3512 | buf_node = buffer->target_node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3513 | binder_node_inner_lock(buf_node); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3514 | BUG_ON(!buf_node->has_async_transaction); |
| 3515 | BUG_ON(buf_node->proc != proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3516 | w = binder_dequeue_work_head_ilocked( |
| 3517 | &buf_node->async_todo); |
Martijn Coenen | 3a6430c | 2017-08-31 10:04:29 +0200 | [diff] [blame] | 3518 | if (!w) { |
Gustavo A. R. Silva | 197410a | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 3519 | buf_node->has_async_transaction = false; |
Martijn Coenen | 3a6430c | 2017-08-31 10:04:29 +0200 | [diff] [blame] | 3520 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3521 | binder_enqueue_work_ilocked( |
Martijn Coenen | 3a6430c | 2017-08-31 10:04:29 +0200 | [diff] [blame] | 3522 | w, &proc->todo); |
| 3523 | binder_wakeup_proc_ilocked(proc); |
| 3524 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3525 | binder_node_inner_unlock(buf_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3526 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3527 | trace_binder_transaction_buffer_release(buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3528 | binder_transaction_buffer_release(proc, buffer, NULL); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 3529 | binder_alloc_free_buf(&proc->alloc, buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3530 | break; |
| 3531 | } |
| 3532 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3533 | case BC_TRANSACTION_SG: |
| 3534 | case BC_REPLY_SG: { |
| 3535 | struct binder_transaction_data_sg tr; |
| 3536 | |
| 3537 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3538 | return -EFAULT; |
| 3539 | ptr += sizeof(tr); |
| 3540 | binder_transaction(proc, thread, &tr.transaction_data, |
| 3541 | cmd == BC_REPLY_SG, tr.buffers_size); |
| 3542 | break; |
| 3543 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3544 | case BC_TRANSACTION: |
| 3545 | case BC_REPLY: { |
| 3546 | struct binder_transaction_data tr; |
| 3547 | |
| 3548 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3549 | return -EFAULT; |
| 3550 | ptr += sizeof(tr); |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 3551 | binder_transaction(proc, thread, &tr, |
| 3552 | cmd == BC_REPLY, 0); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3553 | break; |
| 3554 | } |
| 3555 | |
| 3556 | case BC_REGISTER_LOOPER: |
| 3557 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3558 | "%d:%d BC_REGISTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3559 | proc->pid, thread->pid); |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3560 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3561 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { |
| 3562 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3563 | 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] | 3564 | proc->pid, thread->pid); |
| 3565 | } else if (proc->requested_threads == 0) { |
| 3566 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3567 | 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] | 3568 | proc->pid, thread->pid); |
| 3569 | } else { |
| 3570 | proc->requested_threads--; |
| 3571 | proc->requested_threads_started++; |
| 3572 | } |
| 3573 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3574 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3575 | break; |
| 3576 | case BC_ENTER_LOOPER: |
| 3577 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3578 | "%d:%d BC_ENTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3579 | proc->pid, thread->pid); |
| 3580 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { |
| 3581 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3582 | 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] | 3583 | proc->pid, thread->pid); |
| 3584 | } |
| 3585 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; |
| 3586 | break; |
| 3587 | case BC_EXIT_LOOPER: |
| 3588 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3589 | "%d:%d BC_EXIT_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3590 | proc->pid, thread->pid); |
| 3591 | thread->looper |= BINDER_LOOPER_STATE_EXITED; |
| 3592 | break; |
| 3593 | |
| 3594 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 3595 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 3596 | uint32_t target; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3597 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3598 | struct binder_ref *ref; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3599 | struct binder_ref_death *death = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3600 | |
| 3601 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3602 | return -EFAULT; |
| 3603 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3604 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3605 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3606 | ptr += sizeof(binder_uintptr_t); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3607 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3608 | /* |
| 3609 | * Allocate memory for death notification |
| 3610 | * before taking lock |
| 3611 | */ |
| 3612 | death = kzalloc(sizeof(*death), GFP_KERNEL); |
| 3613 | if (death == NULL) { |
| 3614 | WARN_ON(thread->return_error.cmd != |
| 3615 | BR_OK); |
| 3616 | thread->return_error.cmd = BR_ERROR; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3617 | binder_enqueue_thread_work( |
| 3618 | thread, |
| 3619 | &thread->return_error.work); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3620 | binder_debug( |
| 3621 | BINDER_DEBUG_FAILED_TRANSACTION, |
| 3622 | "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n", |
| 3623 | proc->pid, thread->pid); |
| 3624 | break; |
| 3625 | } |
| 3626 | } |
| 3627 | binder_proc_lock(proc); |
| 3628 | ref = binder_get_ref_olocked(proc, target, false); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3629 | if (ref == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3630 | binder_user_error("%d:%d %s invalid ref %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3631 | proc->pid, thread->pid, |
| 3632 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3633 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3634 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 3635 | target); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3636 | binder_proc_unlock(proc); |
| 3637 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3638 | break; |
| 3639 | } |
| 3640 | |
| 3641 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3642 | "%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] | 3643 | proc->pid, thread->pid, |
| 3644 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3645 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3646 | "BC_CLEAR_DEATH_NOTIFICATION", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3647 | (u64)cookie, ref->data.debug_id, |
| 3648 | ref->data.desc, ref->data.strong, |
| 3649 | ref->data.weak, ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3650 | |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3651 | binder_node_lock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3652 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3653 | if (ref->death) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3654 | 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] | 3655 | proc->pid, thread->pid); |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3656 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3657 | binder_proc_unlock(proc); |
| 3658 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3659 | break; |
| 3660 | } |
| 3661 | binder_stats_created(BINDER_STAT_DEATH); |
| 3662 | INIT_LIST_HEAD(&death->work.entry); |
| 3663 | death->cookie = cookie; |
| 3664 | ref->death = death; |
| 3665 | if (ref->node->proc == NULL) { |
| 3666 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
Martijn Coenen | bb74562 | 2017-08-31 10:04:28 +0200 | [diff] [blame] | 3667 | |
| 3668 | binder_inner_proc_lock(proc); |
| 3669 | binder_enqueue_work_ilocked( |
| 3670 | &ref->death->work, &proc->todo); |
| 3671 | binder_wakeup_proc_ilocked(proc); |
| 3672 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3673 | } |
| 3674 | } else { |
| 3675 | if (ref->death == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3676 | 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] | 3677 | proc->pid, thread->pid); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3678 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3679 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3680 | break; |
| 3681 | } |
| 3682 | death = ref->death; |
| 3683 | if (death->cookie != cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3684 | 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] | 3685 | proc->pid, thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3686 | (u64)death->cookie, |
| 3687 | (u64)cookie); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3688 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3689 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3690 | break; |
| 3691 | } |
| 3692 | ref->death = NULL; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3693 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3694 | if (list_empty(&death->work.entry)) { |
| 3695 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3696 | if (thread->looper & |
| 3697 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3698 | BINDER_LOOPER_STATE_ENTERED)) |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3699 | binder_enqueue_thread_work_ilocked( |
| 3700 | thread, |
| 3701 | &death->work); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3702 | else { |
| 3703 | binder_enqueue_work_ilocked( |
| 3704 | &death->work, |
| 3705 | &proc->todo); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 3706 | binder_wakeup_proc_ilocked( |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3707 | proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3708 | } |
| 3709 | } else { |
| 3710 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); |
| 3711 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; |
| 3712 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3713 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3714 | } |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3715 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3716 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3717 | } break; |
| 3718 | case BC_DEAD_BINDER_DONE: { |
| 3719 | struct binder_work *w; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3720 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3721 | struct binder_ref_death *death = NULL; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3722 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3723 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3724 | return -EFAULT; |
| 3725 | |
Lisa Du | 7a64cd8 | 2016-02-17 09:32:52 +0800 | [diff] [blame] | 3726 | ptr += sizeof(cookie); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3727 | binder_inner_proc_lock(proc); |
| 3728 | list_for_each_entry(w, &proc->delivered_death, |
| 3729 | entry) { |
| 3730 | struct binder_ref_death *tmp_death = |
| 3731 | container_of(w, |
| 3732 | struct binder_ref_death, |
| 3733 | work); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3734 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3735 | if (tmp_death->cookie == cookie) { |
| 3736 | death = tmp_death; |
| 3737 | break; |
| 3738 | } |
| 3739 | } |
| 3740 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Todd Kjos | 8ca86f1 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 3741 | "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3742 | proc->pid, thread->pid, (u64)cookie, |
| 3743 | death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3744 | if (death == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3745 | binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n", |
| 3746 | proc->pid, thread->pid, (u64)cookie); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3747 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3748 | break; |
| 3749 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3750 | binder_dequeue_work_ilocked(&death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3751 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { |
| 3752 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3753 | if (thread->looper & |
| 3754 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3755 | BINDER_LOOPER_STATE_ENTERED)) |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3756 | binder_enqueue_thread_work_ilocked( |
| 3757 | thread, &death->work); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3758 | else { |
| 3759 | binder_enqueue_work_ilocked( |
| 3760 | &death->work, |
| 3761 | &proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3762 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3763 | } |
| 3764 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3765 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3766 | } break; |
| 3767 | |
| 3768 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3769 | pr_err("%d:%d unknown command %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3770 | proc->pid, thread->pid, cmd); |
| 3771 | return -EINVAL; |
| 3772 | } |
| 3773 | *consumed = ptr - buffer; |
| 3774 | } |
| 3775 | return 0; |
| 3776 | } |
| 3777 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3778 | static void binder_stat_br(struct binder_proc *proc, |
| 3779 | struct binder_thread *thread, uint32_t cmd) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3780 | { |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3781 | trace_binder_return(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3782 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 3783 | atomic_inc(&binder_stats.br[_IOC_NR(cmd)]); |
| 3784 | atomic_inc(&proc->stats.br[_IOC_NR(cmd)]); |
| 3785 | atomic_inc(&thread->stats.br[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3786 | } |
| 3787 | } |
| 3788 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3789 | static int binder_put_node_cmd(struct binder_proc *proc, |
| 3790 | struct binder_thread *thread, |
| 3791 | void __user **ptrp, |
| 3792 | binder_uintptr_t node_ptr, |
| 3793 | binder_uintptr_t node_cookie, |
| 3794 | int node_debug_id, |
| 3795 | uint32_t cmd, const char *cmd_name) |
| 3796 | { |
| 3797 | void __user *ptr = *ptrp; |
| 3798 | |
| 3799 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 3800 | return -EFAULT; |
| 3801 | ptr += sizeof(uint32_t); |
| 3802 | |
| 3803 | if (put_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
| 3804 | return -EFAULT; |
| 3805 | ptr += sizeof(binder_uintptr_t); |
| 3806 | |
| 3807 | if (put_user(node_cookie, (binder_uintptr_t __user *)ptr)) |
| 3808 | return -EFAULT; |
| 3809 | ptr += sizeof(binder_uintptr_t); |
| 3810 | |
| 3811 | binder_stat_br(proc, thread, cmd); |
| 3812 | binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n", |
| 3813 | proc->pid, thread->pid, cmd_name, node_debug_id, |
| 3814 | (u64)node_ptr, (u64)node_cookie); |
| 3815 | |
| 3816 | *ptrp = ptr; |
| 3817 | return 0; |
| 3818 | } |
| 3819 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 3820 | static int binder_wait_for_work(struct binder_thread *thread, |
| 3821 | bool do_proc_work) |
| 3822 | { |
| 3823 | DEFINE_WAIT(wait); |
| 3824 | struct binder_proc *proc = thread->proc; |
| 3825 | int ret = 0; |
| 3826 | |
| 3827 | freezer_do_not_count(); |
| 3828 | binder_inner_proc_lock(proc); |
| 3829 | for (;;) { |
| 3830 | prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE); |
| 3831 | if (binder_has_work_ilocked(thread, do_proc_work)) |
| 3832 | break; |
| 3833 | if (do_proc_work) |
| 3834 | list_add(&thread->waiting_thread_node, |
| 3835 | &proc->waiting_threads); |
| 3836 | binder_inner_proc_unlock(proc); |
| 3837 | schedule(); |
| 3838 | binder_inner_proc_lock(proc); |
| 3839 | list_del_init(&thread->waiting_thread_node); |
| 3840 | if (signal_pending(current)) { |
| 3841 | ret = -ERESTARTSYS; |
| 3842 | break; |
| 3843 | } |
| 3844 | } |
| 3845 | finish_wait(&thread->wait, &wait); |
| 3846 | binder_inner_proc_unlock(proc); |
| 3847 | freezer_count(); |
| 3848 | |
| 3849 | return ret; |
| 3850 | } |
| 3851 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3852 | static int binder_thread_read(struct binder_proc *proc, |
| 3853 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3854 | binder_uintptr_t binder_buffer, size_t size, |
| 3855 | binder_size_t *consumed, int non_block) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3856 | { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3857 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3858 | void __user *ptr = buffer + *consumed; |
| 3859 | void __user *end = buffer + size; |
| 3860 | |
| 3861 | int ret = 0; |
| 3862 | int wait_for_proc_work; |
| 3863 | |
| 3864 | if (*consumed == 0) { |
| 3865 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) |
| 3866 | return -EFAULT; |
| 3867 | ptr += sizeof(uint32_t); |
| 3868 | } |
| 3869 | |
| 3870 | retry: |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3871 | binder_inner_proc_lock(proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 3872 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3873 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3874 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3875 | thread->looper |= BINDER_LOOPER_STATE_WAITING; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3876 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3877 | trace_binder_wait_for_work(wait_for_proc_work, |
| 3878 | !!thread->transaction_stack, |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3879 | !binder_worklist_empty(proc, &thread->todo)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3880 | if (wait_for_proc_work) { |
| 3881 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 3882 | BINDER_LOOPER_STATE_ENTERED))) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3883 | 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] | 3884 | proc->pid, thread->pid, thread->looper); |
| 3885 | wait_event_interruptible(binder_user_error_wait, |
| 3886 | binder_stop_on_user_error < 2); |
| 3887 | } |
| 3888 | binder_set_nice(proc->default_priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3889 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3890 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 3891 | if (non_block) { |
| 3892 | if (!binder_has_work(thread, wait_for_proc_work)) |
| 3893 | ret = -EAGAIN; |
| 3894 | } else { |
| 3895 | ret = binder_wait_for_work(thread, wait_for_proc_work); |
| 3896 | } |
| 3897 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3898 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; |
| 3899 | |
| 3900 | if (ret) |
| 3901 | return ret; |
| 3902 | |
| 3903 | while (1) { |
| 3904 | uint32_t cmd; |
| 3905 | struct binder_transaction_data tr; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3906 | struct binder_work *w = NULL; |
| 3907 | struct list_head *list = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3908 | struct binder_transaction *t = NULL; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3909 | struct binder_thread *t_from; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3910 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3911 | binder_inner_proc_lock(proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3912 | if (!binder_worklist_empty_ilocked(&thread->todo)) |
| 3913 | list = &thread->todo; |
| 3914 | else if (!binder_worklist_empty_ilocked(&proc->todo) && |
| 3915 | wait_for_proc_work) |
| 3916 | list = &proc->todo; |
| 3917 | else { |
| 3918 | binder_inner_proc_unlock(proc); |
| 3919 | |
Dmitry Voytik | 395262a | 2014-09-08 18:16:34 +0400 | [diff] [blame] | 3920 | /* no data added */ |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 3921 | if (ptr - buffer == 4 && !thread->looper_need_return) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3922 | goto retry; |
| 3923 | break; |
| 3924 | } |
| 3925 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3926 | if (end - ptr < sizeof(tr) + 4) { |
| 3927 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3928 | break; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3929 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3930 | w = binder_dequeue_work_head_ilocked(list); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3931 | if (binder_worklist_empty_ilocked(&thread->todo)) |
| 3932 | thread->process_todo = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3933 | |
| 3934 | switch (w->type) { |
| 3935 | case BINDER_WORK_TRANSACTION: { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3936 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3937 | t = container_of(w, struct binder_transaction, work); |
| 3938 | } break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3939 | case BINDER_WORK_RETURN_ERROR: { |
| 3940 | struct binder_error *e = container_of( |
| 3941 | w, struct binder_error, work); |
| 3942 | |
| 3943 | WARN_ON(e->cmd == BR_OK); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3944 | binder_inner_proc_unlock(proc); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3945 | if (put_user(e->cmd, (uint32_t __user *)ptr)) |
| 3946 | return -EFAULT; |
宋金时 | 838d556 | 2018-05-10 02:05:03 +0000 | [diff] [blame] | 3947 | cmd = e->cmd; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3948 | e->cmd = BR_OK; |
| 3949 | ptr += sizeof(uint32_t); |
| 3950 | |
宋金时 | 838d556 | 2018-05-10 02:05:03 +0000 | [diff] [blame] | 3951 | binder_stat_br(proc, thread, cmd); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3952 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3953 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3954 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3955 | cmd = BR_TRANSACTION_COMPLETE; |
| 3956 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 3957 | return -EFAULT; |
| 3958 | ptr += sizeof(uint32_t); |
| 3959 | |
| 3960 | binder_stat_br(proc, thread, cmd); |
| 3961 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3962 | "%d:%d BR_TRANSACTION_COMPLETE\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3963 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3964 | kfree(w); |
| 3965 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3966 | } break; |
| 3967 | case BINDER_WORK_NODE: { |
| 3968 | struct binder_node *node = container_of(w, struct binder_node, work); |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3969 | int strong, weak; |
| 3970 | binder_uintptr_t node_ptr = node->ptr; |
| 3971 | binder_uintptr_t node_cookie = node->cookie; |
| 3972 | int node_debug_id = node->debug_id; |
| 3973 | int has_weak_ref; |
| 3974 | int has_strong_ref; |
| 3975 | void __user *orig_ptr = ptr; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3976 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3977 | BUG_ON(proc != node->proc); |
| 3978 | strong = node->internal_strong_refs || |
| 3979 | node->local_strong_refs; |
| 3980 | weak = !hlist_empty(&node->refs) || |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3981 | node->local_weak_refs || |
| 3982 | node->tmp_refs || strong; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3983 | has_strong_ref = node->has_strong_ref; |
| 3984 | has_weak_ref = node->has_weak_ref; |
| 3985 | |
| 3986 | if (weak && !has_weak_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3987 | node->has_weak_ref = 1; |
| 3988 | node->pending_weak_ref = 1; |
| 3989 | node->local_weak_refs++; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3990 | } |
| 3991 | if (strong && !has_strong_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3992 | node->has_strong_ref = 1; |
| 3993 | node->pending_strong_ref = 1; |
| 3994 | node->local_strong_refs++; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3995 | } |
| 3996 | if (!strong && has_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3997 | node->has_strong_ref = 0; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3998 | if (!weak && has_weak_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3999 | node->has_weak_ref = 0; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4000 | if (!weak && !strong) { |
| 4001 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4002 | "%d:%d node %d u%016llx c%016llx deleted\n", |
| 4003 | proc->pid, thread->pid, |
| 4004 | node_debug_id, |
| 4005 | (u64)node_ptr, |
| 4006 | (u64)node_cookie); |
| 4007 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4008 | binder_inner_proc_unlock(proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4009 | binder_node_lock(node); |
| 4010 | /* |
| 4011 | * Acquire the node lock before freeing the |
| 4012 | * node to serialize with other threads that |
| 4013 | * may have been holding the node lock while |
| 4014 | * decrementing this node (avoids race where |
| 4015 | * this thread frees while the other thread |
| 4016 | * is unlocking the node after the final |
| 4017 | * decrement) |
| 4018 | */ |
| 4019 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4020 | binder_free_node(node); |
| 4021 | } else |
| 4022 | binder_inner_proc_unlock(proc); |
| 4023 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4024 | if (weak && !has_weak_ref) |
| 4025 | ret = binder_put_node_cmd( |
| 4026 | proc, thread, &ptr, node_ptr, |
| 4027 | node_cookie, node_debug_id, |
| 4028 | BR_INCREFS, "BR_INCREFS"); |
| 4029 | if (!ret && strong && !has_strong_ref) |
| 4030 | ret = binder_put_node_cmd( |
| 4031 | proc, thread, &ptr, node_ptr, |
| 4032 | node_cookie, node_debug_id, |
| 4033 | BR_ACQUIRE, "BR_ACQUIRE"); |
| 4034 | if (!ret && !strong && has_strong_ref) |
| 4035 | ret = binder_put_node_cmd( |
| 4036 | proc, thread, &ptr, node_ptr, |
| 4037 | node_cookie, node_debug_id, |
| 4038 | BR_RELEASE, "BR_RELEASE"); |
| 4039 | if (!ret && !weak && has_weak_ref) |
| 4040 | ret = binder_put_node_cmd( |
| 4041 | proc, thread, &ptr, node_ptr, |
| 4042 | node_cookie, node_debug_id, |
| 4043 | BR_DECREFS, "BR_DECREFS"); |
| 4044 | if (orig_ptr == ptr) |
| 4045 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4046 | "%d:%d node %d u%016llx c%016llx state unchanged\n", |
| 4047 | proc->pid, thread->pid, |
| 4048 | node_debug_id, |
| 4049 | (u64)node_ptr, |
| 4050 | (u64)node_cookie); |
| 4051 | if (ret) |
| 4052 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4053 | } break; |
| 4054 | case BINDER_WORK_DEAD_BINDER: |
| 4055 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4056 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4057 | struct binder_ref_death *death; |
| 4058 | uint32_t cmd; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4059 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4060 | |
| 4061 | death = container_of(w, struct binder_ref_death, work); |
| 4062 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) |
| 4063 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; |
| 4064 | else |
| 4065 | cmd = BR_DEAD_BINDER; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4066 | cookie = death->cookie; |
| 4067 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4068 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4069 | "%d:%d %s %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4070 | proc->pid, thread->pid, |
| 4071 | cmd == BR_DEAD_BINDER ? |
| 4072 | "BR_DEAD_BINDER" : |
| 4073 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4074 | (u64)cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4075 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4076 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4077 | kfree(death); |
| 4078 | binder_stats_deleted(BINDER_STAT_DEATH); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4079 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4080 | binder_enqueue_work_ilocked( |
| 4081 | w, &proc->delivered_death); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4082 | binder_inner_proc_unlock(proc); |
| 4083 | } |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4084 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4085 | return -EFAULT; |
| 4086 | ptr += sizeof(uint32_t); |
| 4087 | if (put_user(cookie, |
| 4088 | (binder_uintptr_t __user *)ptr)) |
| 4089 | return -EFAULT; |
| 4090 | ptr += sizeof(binder_uintptr_t); |
| 4091 | binder_stat_br(proc, thread, cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4092 | if (cmd == BR_DEAD_BINDER) |
| 4093 | goto done; /* DEAD_BINDER notifications can cause transactions */ |
| 4094 | } break; |
| 4095 | } |
| 4096 | |
| 4097 | if (!t) |
| 4098 | continue; |
| 4099 | |
| 4100 | BUG_ON(t->buffer == NULL); |
| 4101 | if (t->buffer->target_node) { |
| 4102 | struct binder_node *target_node = t->buffer->target_node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4103 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4104 | tr.target.ptr = target_node->ptr; |
| 4105 | tr.cookie = target_node->cookie; |
| 4106 | t->saved_priority = task_nice(current); |
| 4107 | if (t->priority < target_node->min_priority && |
| 4108 | !(t->flags & TF_ONE_WAY)) |
| 4109 | binder_set_nice(t->priority); |
| 4110 | else if (!(t->flags & TF_ONE_WAY) || |
| 4111 | t->saved_priority > target_node->min_priority) |
| 4112 | binder_set_nice(target_node->min_priority); |
| 4113 | cmd = BR_TRANSACTION; |
| 4114 | } else { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4115 | tr.target.ptr = 0; |
| 4116 | tr.cookie = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4117 | cmd = BR_REPLY; |
| 4118 | } |
| 4119 | tr.code = t->code; |
| 4120 | tr.flags = t->flags; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 4121 | tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4122 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4123 | t_from = binder_get_txn_from(t); |
| 4124 | if (t_from) { |
| 4125 | struct task_struct *sender = t_from->proc->tsk; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4126 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4127 | tr.sender_pid = task_tgid_nr_ns(sender, |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 4128 | task_active_pid_ns(current)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4129 | } else { |
| 4130 | tr.sender_pid = 0; |
| 4131 | } |
| 4132 | |
| 4133 | tr.data_size = t->buffer->data_size; |
| 4134 | tr.offsets_size = t->buffer->offsets_size; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4135 | tr.data.ptr.buffer = (binder_uintptr_t) |
| 4136 | ((uintptr_t)t->buffer->data + |
| 4137 | binder_alloc_get_user_buffer_offset(&proc->alloc)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4138 | tr.data.ptr.offsets = tr.data.ptr.buffer + |
| 4139 | ALIGN(t->buffer->data_size, |
| 4140 | sizeof(void *)); |
| 4141 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4142 | if (put_user(cmd, (uint32_t __user *)ptr)) { |
| 4143 | if (t_from) |
| 4144 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | fb2c445 | 2017-11-13 10:06:08 +0100 | [diff] [blame] | 4145 | |
| 4146 | binder_cleanup_transaction(t, "put_user failed", |
| 4147 | BR_FAILED_REPLY); |
| 4148 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4149 | return -EFAULT; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4150 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4151 | ptr += sizeof(uint32_t); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4152 | if (copy_to_user(ptr, &tr, sizeof(tr))) { |
| 4153 | if (t_from) |
| 4154 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | fb2c445 | 2017-11-13 10:06:08 +0100 | [diff] [blame] | 4155 | |
| 4156 | binder_cleanup_transaction(t, "copy_to_user failed", |
| 4157 | BR_FAILED_REPLY); |
| 4158 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4159 | return -EFAULT; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4160 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4161 | ptr += sizeof(tr); |
| 4162 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4163 | trace_binder_transaction_received(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4164 | binder_stat_br(proc, thread, cmd); |
| 4165 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4166 | "%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] | 4167 | proc->pid, thread->pid, |
| 4168 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : |
| 4169 | "BR_REPLY", |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4170 | t->debug_id, t_from ? t_from->proc->pid : 0, |
| 4171 | t_from ? t_from->pid : 0, cmd, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4172 | t->buffer->data_size, t->buffer->offsets_size, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4173 | (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4174 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4175 | if (t_from) |
| 4176 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4177 | t->buffer->allow_user_free = 1; |
| 4178 | if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4179 | binder_inner_proc_lock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4180 | t->to_parent = thread->transaction_stack; |
| 4181 | t->to_thread = thread; |
| 4182 | thread->transaction_stack = t; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4183 | binder_inner_proc_unlock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4184 | } else { |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 4185 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4186 | } |
| 4187 | break; |
| 4188 | } |
| 4189 | |
| 4190 | done: |
| 4191 | |
| 4192 | *consumed = ptr - buffer; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4193 | binder_inner_proc_lock(proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4194 | if (proc->requested_threads == 0 && |
| 4195 | list_empty(&thread->proc->waiting_threads) && |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4196 | proc->requested_threads_started < proc->max_threads && |
| 4197 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4198 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ |
| 4199 | /*spawn a new thread if we leave this out */) { |
| 4200 | proc->requested_threads++; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4201 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4202 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4203 | "%d:%d BR_SPAWN_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4204 | proc->pid, thread->pid); |
| 4205 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) |
| 4206 | return -EFAULT; |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 4207 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4208 | } else |
| 4209 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4210 | return 0; |
| 4211 | } |
| 4212 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4213 | static void binder_release_work(struct binder_proc *proc, |
| 4214 | struct list_head *list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4215 | { |
| 4216 | struct binder_work *w; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4217 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4218 | while (1) { |
| 4219 | w = binder_dequeue_work_head(proc, list); |
| 4220 | if (!w) |
| 4221 | return; |
| 4222 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4223 | switch (w->type) { |
| 4224 | case BINDER_WORK_TRANSACTION: { |
| 4225 | struct binder_transaction *t; |
| 4226 | |
| 4227 | t = container_of(w, struct binder_transaction, work); |
Martijn Coenen | fb2c445 | 2017-11-13 10:06:08 +0100 | [diff] [blame] | 4228 | |
| 4229 | binder_cleanup_transaction(t, "process died.", |
| 4230 | BR_DEAD_REPLY); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4231 | } break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4232 | case BINDER_WORK_RETURN_ERROR: { |
| 4233 | struct binder_error *e = container_of( |
| 4234 | w, struct binder_error, work); |
| 4235 | |
| 4236 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 4237 | "undelivered TRANSACTION_ERROR: %u\n", |
| 4238 | e->cmd); |
| 4239 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4240 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4241 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4242 | "undelivered TRANSACTION_COMPLETE\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4243 | kfree(w); |
| 4244 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4245 | } break; |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4246 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4247 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4248 | struct binder_ref_death *death; |
| 4249 | |
| 4250 | death = container_of(w, struct binder_ref_death, work); |
| 4251 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4252 | "undelivered death notification, %016llx\n", |
| 4253 | (u64)death->cookie); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4254 | kfree(death); |
| 4255 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 4256 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4257 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4258 | pr_err("unexpected work type, %d, not freed\n", |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4259 | w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4260 | break; |
| 4261 | } |
| 4262 | } |
| 4263 | |
| 4264 | } |
| 4265 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4266 | static struct binder_thread *binder_get_thread_ilocked( |
| 4267 | struct binder_proc *proc, struct binder_thread *new_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4268 | { |
| 4269 | struct binder_thread *thread = NULL; |
| 4270 | struct rb_node *parent = NULL; |
| 4271 | struct rb_node **p = &proc->threads.rb_node; |
| 4272 | |
| 4273 | while (*p) { |
| 4274 | parent = *p; |
| 4275 | thread = rb_entry(parent, struct binder_thread, rb_node); |
| 4276 | |
| 4277 | if (current->pid < thread->pid) |
| 4278 | p = &(*p)->rb_left; |
| 4279 | else if (current->pid > thread->pid) |
| 4280 | p = &(*p)->rb_right; |
| 4281 | else |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4282 | return thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4283 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4284 | if (!new_thread) |
| 4285 | return NULL; |
| 4286 | thread = new_thread; |
| 4287 | binder_stats_created(BINDER_STAT_THREAD); |
| 4288 | thread->proc = proc; |
| 4289 | thread->pid = current->pid; |
| 4290 | atomic_set(&thread->tmp_ref, 0); |
| 4291 | init_waitqueue_head(&thread->wait); |
| 4292 | INIT_LIST_HEAD(&thread->todo); |
| 4293 | rb_link_node(&thread->rb_node, parent, p); |
| 4294 | rb_insert_color(&thread->rb_node, &proc->threads); |
| 4295 | thread->looper_need_return = true; |
| 4296 | thread->return_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4297 | thread->return_error.cmd = BR_OK; |
| 4298 | thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4299 | thread->reply_error.cmd = BR_OK; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4300 | INIT_LIST_HEAD(&new_thread->waiting_thread_node); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4301 | return thread; |
| 4302 | } |
| 4303 | |
| 4304 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) |
| 4305 | { |
| 4306 | struct binder_thread *thread; |
| 4307 | struct binder_thread *new_thread; |
| 4308 | |
| 4309 | binder_inner_proc_lock(proc); |
| 4310 | thread = binder_get_thread_ilocked(proc, NULL); |
| 4311 | binder_inner_proc_unlock(proc); |
| 4312 | if (!thread) { |
| 4313 | new_thread = kzalloc(sizeof(*thread), GFP_KERNEL); |
| 4314 | if (new_thread == NULL) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4315 | return NULL; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4316 | binder_inner_proc_lock(proc); |
| 4317 | thread = binder_get_thread_ilocked(proc, new_thread); |
| 4318 | binder_inner_proc_unlock(proc); |
| 4319 | if (thread != new_thread) |
| 4320 | kfree(new_thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4321 | } |
| 4322 | return thread; |
| 4323 | } |
| 4324 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4325 | static void binder_free_proc(struct binder_proc *proc) |
| 4326 | { |
| 4327 | BUG_ON(!list_empty(&proc->todo)); |
| 4328 | BUG_ON(!list_empty(&proc->delivered_death)); |
| 4329 | binder_alloc_deferred_release(&proc->alloc); |
| 4330 | put_task_struct(proc->tsk); |
| 4331 | binder_stats_deleted(BINDER_STAT_PROC); |
| 4332 | kfree(proc); |
| 4333 | } |
| 4334 | |
| 4335 | static void binder_free_thread(struct binder_thread *thread) |
| 4336 | { |
| 4337 | BUG_ON(!list_empty(&thread->todo)); |
| 4338 | binder_stats_deleted(BINDER_STAT_THREAD); |
| 4339 | binder_proc_dec_tmpref(thread->proc); |
| 4340 | kfree(thread); |
| 4341 | } |
| 4342 | |
| 4343 | static int binder_thread_release(struct binder_proc *proc, |
| 4344 | struct binder_thread *thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4345 | { |
| 4346 | struct binder_transaction *t; |
| 4347 | struct binder_transaction *send_reply = NULL; |
| 4348 | int active_transactions = 0; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4349 | struct binder_transaction *last_t = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4350 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4351 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4352 | /* |
| 4353 | * take a ref on the proc so it survives |
| 4354 | * after we remove this thread from proc->threads. |
| 4355 | * The corresponding dec is when we actually |
| 4356 | * free the thread in binder_free_thread() |
| 4357 | */ |
| 4358 | proc->tmp_ref++; |
| 4359 | /* |
| 4360 | * take a ref on this thread to ensure it |
| 4361 | * survives while we are releasing it |
| 4362 | */ |
| 4363 | atomic_inc(&thread->tmp_ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4364 | rb_erase(&thread->rb_node, &proc->threads); |
| 4365 | t = thread->transaction_stack; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4366 | if (t) { |
| 4367 | spin_lock(&t->lock); |
| 4368 | if (t->to_thread == thread) |
| 4369 | send_reply = t; |
| 4370 | } |
| 4371 | thread->is_dead = true; |
| 4372 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4373 | while (t) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4374 | last_t = t; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4375 | active_transactions++; |
| 4376 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4377 | "release %d:%d transaction %d %s, still active\n", |
| 4378 | proc->pid, thread->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4379 | t->debug_id, |
| 4380 | (t->to_thread == thread) ? "in" : "out"); |
| 4381 | |
| 4382 | if (t->to_thread == thread) { |
| 4383 | t->to_proc = NULL; |
| 4384 | t->to_thread = NULL; |
| 4385 | if (t->buffer) { |
| 4386 | t->buffer->transaction = NULL; |
| 4387 | t->buffer = NULL; |
| 4388 | } |
| 4389 | t = t->to_parent; |
| 4390 | } else if (t->from == thread) { |
| 4391 | t->from = NULL; |
| 4392 | t = t->from_parent; |
| 4393 | } else |
| 4394 | BUG(); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4395 | spin_unlock(&last_t->lock); |
| 4396 | if (t) |
| 4397 | spin_lock(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4398 | } |
Martijn Coenen | f5cb779 | 2018-01-05 11:27:07 +0100 | [diff] [blame] | 4399 | |
| 4400 | /* |
| 4401 | * If this thread used poll, make sure we remove the waitqueue |
| 4402 | * from any epoll data structures holding it with POLLFREE. |
| 4403 | * waitqueue_active() is safe to use here because we're holding |
| 4404 | * the inner lock. |
| 4405 | */ |
| 4406 | if ((thread->looper & BINDER_LOOPER_STATE_POLL) && |
| 4407 | waitqueue_active(&thread->wait)) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 4408 | wake_up_poll(&thread->wait, EPOLLHUP | POLLFREE); |
Martijn Coenen | f5cb779 | 2018-01-05 11:27:07 +0100 | [diff] [blame] | 4409 | } |
| 4410 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4411 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4412 | |
Martijn Coenen | 5eeb2ca | 2018-02-16 09:47:15 +0100 | [diff] [blame] | 4413 | /* |
| 4414 | * This is needed to avoid races between wake_up_poll() above and |
| 4415 | * and ep_remove_waitqueue() called for other reasons (eg the epoll file |
| 4416 | * descriptor being closed); ep_remove_waitqueue() holds an RCU read |
| 4417 | * lock, so we can be sure it's done after calling synchronize_rcu(). |
| 4418 | */ |
| 4419 | if (thread->looper & BINDER_LOOPER_STATE_POLL) |
| 4420 | synchronize_rcu(); |
| 4421 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4422 | if (send_reply) |
| 4423 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4424 | binder_release_work(proc, &thread->todo); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4425 | binder_thread_dec_tmpref(thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4426 | return active_transactions; |
| 4427 | } |
| 4428 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 4429 | static __poll_t binder_poll(struct file *filp, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4430 | struct poll_table_struct *wait) |
| 4431 | { |
| 4432 | struct binder_proc *proc = filp->private_data; |
| 4433 | struct binder_thread *thread = NULL; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4434 | bool wait_for_proc_work; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4435 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4436 | thread = binder_get_thread(proc); |
Eric Biggers | f889826 | 2018-01-30 23:11:24 -0800 | [diff] [blame] | 4437 | if (!thread) |
| 4438 | return POLLERR; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4439 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4440 | binder_inner_proc_lock(thread->proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4441 | thread->looper |= BINDER_LOOPER_STATE_POLL; |
| 4442 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
| 4443 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4444 | binder_inner_proc_unlock(thread->proc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4445 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4446 | poll_wait(filp, &thread->wait, wait); |
| 4447 | |
Martijn Coenen | 66b83a4 | 2017-10-09 14:26:56 +0200 | [diff] [blame] | 4448 | if (binder_has_work(thread, wait_for_proc_work)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 4449 | return EPOLLIN; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4450 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4451 | return 0; |
| 4452 | } |
| 4453 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4454 | static int binder_ioctl_write_read(struct file *filp, |
| 4455 | unsigned int cmd, unsigned long arg, |
| 4456 | struct binder_thread *thread) |
| 4457 | { |
| 4458 | int ret = 0; |
| 4459 | struct binder_proc *proc = filp->private_data; |
| 4460 | unsigned int size = _IOC_SIZE(cmd); |
| 4461 | void __user *ubuf = (void __user *)arg; |
| 4462 | struct binder_write_read bwr; |
| 4463 | |
| 4464 | if (size != sizeof(struct binder_write_read)) { |
| 4465 | ret = -EINVAL; |
| 4466 | goto out; |
| 4467 | } |
| 4468 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { |
| 4469 | ret = -EFAULT; |
| 4470 | goto out; |
| 4471 | } |
| 4472 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4473 | "%d:%d write %lld at %016llx, read %lld at %016llx\n", |
| 4474 | proc->pid, thread->pid, |
| 4475 | (u64)bwr.write_size, (u64)bwr.write_buffer, |
| 4476 | (u64)bwr.read_size, (u64)bwr.read_buffer); |
| 4477 | |
| 4478 | if (bwr.write_size > 0) { |
| 4479 | ret = binder_thread_write(proc, thread, |
| 4480 | bwr.write_buffer, |
| 4481 | bwr.write_size, |
| 4482 | &bwr.write_consumed); |
| 4483 | trace_binder_write_done(ret); |
| 4484 | if (ret < 0) { |
| 4485 | bwr.read_consumed = 0; |
| 4486 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4487 | ret = -EFAULT; |
| 4488 | goto out; |
| 4489 | } |
| 4490 | } |
| 4491 | if (bwr.read_size > 0) { |
| 4492 | ret = binder_thread_read(proc, thread, bwr.read_buffer, |
| 4493 | bwr.read_size, |
| 4494 | &bwr.read_consumed, |
| 4495 | filp->f_flags & O_NONBLOCK); |
| 4496 | trace_binder_read_done(ret); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4497 | binder_inner_proc_lock(proc); |
| 4498 | if (!binder_worklist_empty_ilocked(&proc->todo)) |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 4499 | binder_wakeup_proc_ilocked(proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4500 | binder_inner_proc_unlock(proc); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4501 | if (ret < 0) { |
| 4502 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4503 | ret = -EFAULT; |
| 4504 | goto out; |
| 4505 | } |
| 4506 | } |
| 4507 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4508 | "%d:%d wrote %lld of %lld, read return %lld of %lld\n", |
| 4509 | proc->pid, thread->pid, |
| 4510 | (u64)bwr.write_consumed, (u64)bwr.write_size, |
| 4511 | (u64)bwr.read_consumed, (u64)bwr.read_size); |
| 4512 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { |
| 4513 | ret = -EFAULT; |
| 4514 | goto out; |
| 4515 | } |
| 4516 | out: |
| 4517 | return ret; |
| 4518 | } |
| 4519 | |
| 4520 | static int binder_ioctl_set_ctx_mgr(struct file *filp) |
| 4521 | { |
| 4522 | int ret = 0; |
| 4523 | struct binder_proc *proc = filp->private_data; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4524 | struct binder_context *context = proc->context; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4525 | struct binder_node *new_node; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4526 | kuid_t curr_euid = current_euid(); |
| 4527 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4528 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4529 | if (context->binder_context_mgr_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4530 | pr_err("BINDER_SET_CONTEXT_MGR already set\n"); |
| 4531 | ret = -EBUSY; |
| 4532 | goto out; |
| 4533 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 4534 | ret = security_binder_set_context_mgr(proc->tsk); |
| 4535 | if (ret < 0) |
| 4536 | goto out; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4537 | if (uid_valid(context->binder_context_mgr_uid)) { |
| 4538 | if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4539 | pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n", |
| 4540 | from_kuid(&init_user_ns, curr_euid), |
| 4541 | from_kuid(&init_user_ns, |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4542 | context->binder_context_mgr_uid)); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4543 | ret = -EPERM; |
| 4544 | goto out; |
| 4545 | } |
| 4546 | } else { |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4547 | context->binder_context_mgr_uid = curr_euid; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4548 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4549 | new_node = binder_new_node(proc, NULL); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4550 | if (!new_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4551 | ret = -ENOMEM; |
| 4552 | goto out; |
| 4553 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4554 | binder_node_lock(new_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4555 | new_node->local_weak_refs++; |
| 4556 | new_node->local_strong_refs++; |
| 4557 | new_node->has_strong_ref = 1; |
| 4558 | new_node->has_weak_ref = 1; |
| 4559 | context->binder_context_mgr_node = new_node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4560 | binder_node_unlock(new_node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4561 | binder_put_node(new_node); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4562 | out: |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4563 | mutex_unlock(&context->context_mgr_node_lock); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4564 | return ret; |
| 4565 | } |
| 4566 | |
Colin Cross | abcc615 | 2017-08-31 10:04:24 +0200 | [diff] [blame] | 4567 | static int binder_ioctl_get_node_debug_info(struct binder_proc *proc, |
| 4568 | struct binder_node_debug_info *info) |
| 4569 | { |
| 4570 | struct rb_node *n; |
| 4571 | binder_uintptr_t ptr = info->ptr; |
| 4572 | |
| 4573 | memset(info, 0, sizeof(*info)); |
| 4574 | |
| 4575 | binder_inner_proc_lock(proc); |
| 4576 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { |
| 4577 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 4578 | rb_node); |
| 4579 | if (node->ptr > ptr) { |
| 4580 | info->ptr = node->ptr; |
| 4581 | info->cookie = node->cookie; |
| 4582 | info->has_strong_ref = node->has_strong_ref; |
| 4583 | info->has_weak_ref = node->has_weak_ref; |
| 4584 | break; |
| 4585 | } |
| 4586 | } |
| 4587 | binder_inner_proc_unlock(proc); |
| 4588 | |
| 4589 | return 0; |
| 4590 | } |
| 4591 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4592 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 4593 | { |
| 4594 | int ret; |
| 4595 | struct binder_proc *proc = filp->private_data; |
| 4596 | struct binder_thread *thread; |
| 4597 | unsigned int size = _IOC_SIZE(cmd); |
| 4598 | void __user *ubuf = (void __user *)arg; |
| 4599 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4600 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", |
| 4601 | proc->pid, current->pid, cmd, arg);*/ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4602 | |
Sherry Yang | 4175e2b | 2017-08-23 08:46:40 -0700 | [diff] [blame] | 4603 | binder_selftest_alloc(&proc->alloc); |
| 4604 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4605 | trace_binder_ioctl(cmd, arg); |
| 4606 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4607 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4608 | if (ret) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4609 | goto err_unlocked; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4610 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4611 | thread = binder_get_thread(proc); |
| 4612 | if (thread == NULL) { |
| 4613 | ret = -ENOMEM; |
| 4614 | goto err; |
| 4615 | } |
| 4616 | |
| 4617 | switch (cmd) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4618 | case BINDER_WRITE_READ: |
| 4619 | ret = binder_ioctl_write_read(filp, cmd, arg, thread); |
| 4620 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4621 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4622 | break; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4623 | case BINDER_SET_MAX_THREADS: { |
| 4624 | int max_threads; |
| 4625 | |
| 4626 | if (copy_from_user(&max_threads, ubuf, |
| 4627 | sizeof(max_threads))) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4628 | ret = -EINVAL; |
| 4629 | goto err; |
| 4630 | } |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4631 | binder_inner_proc_lock(proc); |
| 4632 | proc->max_threads = max_threads; |
| 4633 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4634 | break; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4635 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4636 | case BINDER_SET_CONTEXT_MGR: |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4637 | ret = binder_ioctl_set_ctx_mgr(filp); |
| 4638 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4639 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4640 | break; |
| 4641 | case BINDER_THREAD_EXIT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4642 | binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4643 | proc->pid, thread->pid); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4644 | binder_thread_release(proc, thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4645 | thread = NULL; |
| 4646 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4647 | case BINDER_VERSION: { |
| 4648 | struct binder_version __user *ver = ubuf; |
| 4649 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4650 | if (size != sizeof(struct binder_version)) { |
| 4651 | ret = -EINVAL; |
| 4652 | goto err; |
| 4653 | } |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4654 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, |
| 4655 | &ver->protocol_version)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4656 | ret = -EINVAL; |
| 4657 | goto err; |
| 4658 | } |
| 4659 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4660 | } |
Colin Cross | abcc615 | 2017-08-31 10:04:24 +0200 | [diff] [blame] | 4661 | case BINDER_GET_NODE_DEBUG_INFO: { |
| 4662 | struct binder_node_debug_info info; |
| 4663 | |
| 4664 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 4665 | ret = -EFAULT; |
| 4666 | goto err; |
| 4667 | } |
| 4668 | |
| 4669 | ret = binder_ioctl_get_node_debug_info(proc, &info); |
| 4670 | if (ret < 0) |
| 4671 | goto err; |
| 4672 | |
| 4673 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 4674 | ret = -EFAULT; |
| 4675 | goto err; |
| 4676 | } |
| 4677 | break; |
| 4678 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4679 | default: |
| 4680 | ret = -EINVAL; |
| 4681 | goto err; |
| 4682 | } |
| 4683 | ret = 0; |
| 4684 | err: |
| 4685 | if (thread) |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 4686 | thread->looper_need_return = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4687 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4688 | if (ret && ret != -ERESTARTSYS) |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4689 | 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] | 4690 | err_unlocked: |
| 4691 | trace_binder_ioctl_done(ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4692 | return ret; |
| 4693 | } |
| 4694 | |
| 4695 | static void binder_vma_open(struct vm_area_struct *vma) |
| 4696 | { |
| 4697 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4698 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4699 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4700 | "%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] | 4701 | proc->pid, vma->vm_start, vma->vm_end, |
| 4702 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4703 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4704 | } |
| 4705 | |
| 4706 | static void binder_vma_close(struct vm_area_struct *vma) |
| 4707 | { |
| 4708 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4709 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4710 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4711 | "%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] | 4712 | proc->pid, vma->vm_start, vma->vm_end, |
| 4713 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4714 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4715 | binder_alloc_vma_close(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4716 | binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES); |
| 4717 | } |
| 4718 | |
Souptick Joarder | e19f70a | 2018-04-23 21:54:00 +0530 | [diff] [blame] | 4719 | static vm_fault_t binder_vm_fault(struct vm_fault *vmf) |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4720 | { |
| 4721 | return VM_FAULT_SIGBUS; |
| 4722 | } |
| 4723 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 4724 | static const struct vm_operations_struct binder_vm_ops = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4725 | .open = binder_vma_open, |
| 4726 | .close = binder_vma_close, |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4727 | .fault = binder_vm_fault, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4728 | }; |
| 4729 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4730 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) |
| 4731 | { |
| 4732 | int ret; |
| 4733 | struct binder_proc *proc = filp->private_data; |
| 4734 | const char *failure_string; |
| 4735 | |
| 4736 | if (proc->tsk != current->group_leader) |
| 4737 | return -EINVAL; |
| 4738 | |
| 4739 | if ((vma->vm_end - vma->vm_start) > SZ_4M) |
| 4740 | vma->vm_end = vma->vm_start + SZ_4M; |
| 4741 | |
| 4742 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 4743 | "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 4744 | __func__, proc->pid, vma->vm_start, vma->vm_end, |
| 4745 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4746 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 4747 | |
| 4748 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { |
| 4749 | ret = -EPERM; |
| 4750 | failure_string = "bad vm_flags"; |
| 4751 | goto err_bad_arg; |
| 4752 | } |
Minchan Kim | 720c2419 | 2018-05-07 23:15:37 +0900 | [diff] [blame] | 4753 | vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP; |
| 4754 | vma->vm_flags &= ~VM_MAYWRITE; |
| 4755 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4756 | vma->vm_ops = &binder_vm_ops; |
| 4757 | vma->vm_private_data = proc; |
| 4758 | |
| 4759 | ret = binder_alloc_mmap_handler(&proc->alloc, vma); |
| 4760 | if (ret) |
| 4761 | return ret; |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 4762 | mutex_lock(&proc->files_lock); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4763 | proc->files = get_files_struct(current); |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 4764 | mutex_unlock(&proc->files_lock); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4765 | return 0; |
| 4766 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4767 | err_bad_arg: |
Elad Wexler | 00c41cd | 2017-12-29 11:03:37 +0200 | [diff] [blame] | 4768 | pr_err("%s: %d %lx-%lx %s failed %d\n", __func__, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4769 | proc->pid, vma->vm_start, vma->vm_end, failure_string, ret); |
| 4770 | return ret; |
| 4771 | } |
| 4772 | |
| 4773 | static int binder_open(struct inode *nodp, struct file *filp) |
| 4774 | { |
| 4775 | struct binder_proc *proc; |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 4776 | struct binder_device *binder_dev; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4777 | |
Elad Wexler | 00c41cd | 2017-12-29 11:03:37 +0200 | [diff] [blame] | 4778 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4779 | current->group_leader->pid, current->pid); |
| 4780 | |
| 4781 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 4782 | if (proc == NULL) |
| 4783 | return -ENOMEM; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 4784 | spin_lock_init(&proc->inner_lock); |
| 4785 | spin_lock_init(&proc->outer_lock); |
Todd Kjos | c4ea41b | 2017-06-29 12:01:36 -0700 | [diff] [blame] | 4786 | get_task_struct(current->group_leader); |
| 4787 | proc->tsk = current->group_leader; |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 4788 | mutex_init(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4789 | INIT_LIST_HEAD(&proc->todo); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4790 | proc->default_priority = task_nice(current); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 4791 | binder_dev = container_of(filp->private_data, struct binder_device, |
| 4792 | miscdev); |
| 4793 | proc->context = &binder_dev->context; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4794 | binder_alloc_init(&proc->alloc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4795 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4796 | binder_stats_created(BINDER_STAT_PROC); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4797 | proc->pid = current->group_leader->pid; |
| 4798 | INIT_LIST_HEAD(&proc->delivered_death); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4799 | INIT_LIST_HEAD(&proc->waiting_threads); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4800 | filp->private_data = proc; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4801 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4802 | mutex_lock(&binder_procs_lock); |
| 4803 | hlist_add_head(&proc->proc_node, &binder_procs); |
| 4804 | mutex_unlock(&binder_procs_lock); |
| 4805 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4806 | if (binder_debugfs_dir_entry_proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4807 | char strbuf[11]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4808 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4809 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 4810 | /* |
| 4811 | * proc debug entries are shared between contexts, so |
| 4812 | * this will fail if the process tries to open the driver |
| 4813 | * again with a different context. The priting code will |
| 4814 | * anyway print all contexts that a given PID has, so this |
| 4815 | * is not a problem. |
| 4816 | */ |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 4817 | proc->debugfs_entry = debugfs_create_file(strbuf, 0444, |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 4818 | binder_debugfs_dir_entry_proc, |
| 4819 | (void *)(unsigned long)proc->pid, |
| 4820 | &binder_proc_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4821 | } |
| 4822 | |
| 4823 | return 0; |
| 4824 | } |
| 4825 | |
| 4826 | static int binder_flush(struct file *filp, fl_owner_t id) |
| 4827 | { |
| 4828 | struct binder_proc *proc = filp->private_data; |
| 4829 | |
| 4830 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); |
| 4831 | |
| 4832 | return 0; |
| 4833 | } |
| 4834 | |
| 4835 | static void binder_deferred_flush(struct binder_proc *proc) |
| 4836 | { |
| 4837 | struct rb_node *n; |
| 4838 | int wake_count = 0; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4839 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4840 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4841 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 4842 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4843 | |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 4844 | thread->looper_need_return = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4845 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { |
| 4846 | wake_up_interruptible(&thread->wait); |
| 4847 | wake_count++; |
| 4848 | } |
| 4849 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4850 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4851 | |
| 4852 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 4853 | "binder_flush: %d woke %d threads\n", proc->pid, |
| 4854 | wake_count); |
| 4855 | } |
| 4856 | |
| 4857 | static int binder_release(struct inode *nodp, struct file *filp) |
| 4858 | { |
| 4859 | struct binder_proc *proc = filp->private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4860 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4861 | debugfs_remove(proc->debugfs_entry); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4862 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); |
| 4863 | |
| 4864 | return 0; |
| 4865 | } |
| 4866 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4867 | static int binder_node_release(struct binder_node *node, int refs) |
| 4868 | { |
| 4869 | struct binder_ref *ref; |
| 4870 | int death = 0; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4871 | struct binder_proc *proc = node->proc; |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4872 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4873 | binder_release_work(proc, &node->async_todo); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4874 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4875 | binder_node_lock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4876 | binder_inner_proc_lock(proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4877 | binder_dequeue_work_ilocked(&node->work); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4878 | /* |
| 4879 | * The caller must have taken a temporary ref on the node, |
| 4880 | */ |
| 4881 | BUG_ON(!node->tmp_refs); |
| 4882 | if (hlist_empty(&node->refs) && node->tmp_refs == 1) { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4883 | binder_inner_proc_unlock(proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4884 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4885 | binder_free_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4886 | |
| 4887 | return refs; |
| 4888 | } |
| 4889 | |
| 4890 | node->proc = NULL; |
| 4891 | node->local_strong_refs = 0; |
| 4892 | node->local_weak_refs = 0; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4893 | binder_inner_proc_unlock(proc); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4894 | |
| 4895 | spin_lock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4896 | hlist_add_head(&node->dead_node, &binder_dead_nodes); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4897 | spin_unlock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4898 | |
| 4899 | hlist_for_each_entry(ref, &node->refs, node_entry) { |
| 4900 | refs++; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4901 | /* |
| 4902 | * Need the node lock to synchronize |
| 4903 | * with new notification requests and the |
| 4904 | * inner lock to synchronize with queued |
| 4905 | * death notifications. |
| 4906 | */ |
| 4907 | binder_inner_proc_lock(ref->proc); |
| 4908 | if (!ref->death) { |
| 4909 | binder_inner_proc_unlock(ref->proc); |
Arve Hjønnevåg | e194fd8 | 2014-02-17 13:58:29 -0800 | [diff] [blame] | 4910 | continue; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4911 | } |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4912 | |
| 4913 | death++; |
| 4914 | |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4915 | BUG_ON(!list_empty(&ref->death->work.entry)); |
| 4916 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 4917 | binder_enqueue_work_ilocked(&ref->death->work, |
| 4918 | &ref->proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 4919 | binder_wakeup_proc_ilocked(ref->proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4920 | binder_inner_proc_unlock(ref->proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4921 | } |
| 4922 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4923 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 4924 | "node %d now dead, refs %d, death %d\n", |
| 4925 | node->debug_id, refs, death); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4926 | binder_node_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4927 | binder_put_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4928 | |
| 4929 | return refs; |
| 4930 | } |
| 4931 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4932 | static void binder_deferred_release(struct binder_proc *proc) |
| 4933 | { |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4934 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4935 | struct rb_node *n; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4936 | int threads, nodes, incoming_refs, outgoing_refs, active_transactions; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4937 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4938 | BUG_ON(proc->files); |
| 4939 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4940 | mutex_lock(&binder_procs_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4941 | hlist_del(&proc->proc_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4942 | mutex_unlock(&binder_procs_lock); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4943 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4944 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4945 | if (context->binder_context_mgr_node && |
| 4946 | context->binder_context_mgr_node->proc == proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4947 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 4948 | "%s: %d context_mgr_node gone\n", |
| 4949 | __func__, proc->pid); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4950 | context->binder_context_mgr_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4951 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4952 | mutex_unlock(&context->context_mgr_node_lock); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4953 | binder_inner_proc_lock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4954 | /* |
| 4955 | * Make sure proc stays alive after we |
| 4956 | * remove all the threads |
| 4957 | */ |
| 4958 | proc->tmp_ref++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4959 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4960 | proc->is_dead = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4961 | threads = 0; |
| 4962 | active_transactions = 0; |
| 4963 | while ((n = rb_first(&proc->threads))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4964 | struct binder_thread *thread; |
| 4965 | |
| 4966 | thread = rb_entry(n, struct binder_thread, rb_node); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4967 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4968 | threads++; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4969 | active_transactions += binder_thread_release(proc, thread); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4970 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4971 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4972 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4973 | nodes = 0; |
| 4974 | incoming_refs = 0; |
| 4975 | while ((n = rb_first(&proc->nodes))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4976 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4977 | |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4978 | node = rb_entry(n, struct binder_node, rb_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4979 | nodes++; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4980 | /* |
| 4981 | * take a temporary ref on the node before |
| 4982 | * calling binder_node_release() which will either |
| 4983 | * kfree() the node or call binder_put_node() |
| 4984 | */ |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4985 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4986 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4987 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4988 | incoming_refs = binder_node_release(node, incoming_refs); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4989 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4990 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4991 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4992 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4993 | outgoing_refs = 0; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4994 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4995 | while ((n = rb_first(&proc->refs_by_desc))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4996 | struct binder_ref *ref; |
| 4997 | |
| 4998 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4999 | outgoing_refs++; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5000 | binder_cleanup_ref_olocked(ref); |
| 5001 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 5002 | binder_free_ref(ref); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5003 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5004 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5005 | binder_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5006 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5007 | binder_release_work(proc, &proc->todo); |
| 5008 | binder_release_work(proc, &proc->delivered_death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5009 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5010 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5011 | "%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] | 5012 | __func__, proc->pid, threads, nodes, incoming_refs, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5013 | outgoing_refs, active_transactions); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5014 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5015 | binder_proc_dec_tmpref(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5016 | } |
| 5017 | |
| 5018 | static void binder_deferred_func(struct work_struct *work) |
| 5019 | { |
| 5020 | struct binder_proc *proc; |
| 5021 | struct files_struct *files; |
| 5022 | |
| 5023 | int defer; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5024 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5025 | do { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5026 | mutex_lock(&binder_deferred_lock); |
| 5027 | if (!hlist_empty(&binder_deferred_list)) { |
| 5028 | proc = hlist_entry(binder_deferred_list.first, |
| 5029 | struct binder_proc, deferred_work_node); |
| 5030 | hlist_del_init(&proc->deferred_work_node); |
| 5031 | defer = proc->deferred_work; |
| 5032 | proc->deferred_work = 0; |
| 5033 | } else { |
| 5034 | proc = NULL; |
| 5035 | defer = 0; |
| 5036 | } |
| 5037 | mutex_unlock(&binder_deferred_lock); |
| 5038 | |
| 5039 | files = NULL; |
| 5040 | if (defer & BINDER_DEFERRED_PUT_FILES) { |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5041 | mutex_lock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5042 | files = proc->files; |
| 5043 | if (files) |
| 5044 | proc->files = NULL; |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5045 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5046 | } |
| 5047 | |
| 5048 | if (defer & BINDER_DEFERRED_FLUSH) |
| 5049 | binder_deferred_flush(proc); |
| 5050 | |
| 5051 | if (defer & BINDER_DEFERRED_RELEASE) |
| 5052 | binder_deferred_release(proc); /* frees proc */ |
| 5053 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5054 | if (files) |
| 5055 | put_files_struct(files); |
| 5056 | } while (proc); |
| 5057 | } |
| 5058 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); |
| 5059 | |
| 5060 | static void |
| 5061 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) |
| 5062 | { |
| 5063 | mutex_lock(&binder_deferred_lock); |
| 5064 | proc->deferred_work |= defer; |
| 5065 | if (hlist_unhashed(&proc->deferred_work_node)) { |
| 5066 | hlist_add_head(&proc->deferred_work_node, |
| 5067 | &binder_deferred_list); |
Bhaktipriya Shridhar | 1beba52 | 2016-08-13 22:16:24 +0530 | [diff] [blame] | 5068 | schedule_work(&binder_deferred_work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5069 | } |
| 5070 | mutex_unlock(&binder_deferred_lock); |
| 5071 | } |
| 5072 | |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5073 | static void print_binder_transaction_ilocked(struct seq_file *m, |
| 5074 | struct binder_proc *proc, |
| 5075 | const char *prefix, |
| 5076 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5077 | { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5078 | struct binder_proc *to_proc; |
| 5079 | struct binder_buffer *buffer = t->buffer; |
| 5080 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5081 | spin_lock(&t->lock); |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5082 | to_proc = t->to_proc; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5083 | seq_printf(m, |
Todd Kjos | 8ca86f1 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 5084 | "%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] | 5085 | prefix, t->debug_id, t, |
| 5086 | t->from ? t->from->proc->pid : 0, |
| 5087 | t->from ? t->from->pid : 0, |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5088 | to_proc ? to_proc->pid : 0, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5089 | t->to_thread ? t->to_thread->pid : 0, |
| 5090 | t->code, t->flags, t->priority, t->need_reply); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5091 | spin_unlock(&t->lock); |
| 5092 | |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5093 | if (proc != to_proc) { |
| 5094 | /* |
| 5095 | * Can only safely deref buffer if we are holding the |
| 5096 | * correct proc inner lock for this node |
| 5097 | */ |
| 5098 | seq_puts(m, "\n"); |
| 5099 | return; |
| 5100 | } |
| 5101 | |
| 5102 | if (buffer == NULL) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5103 | seq_puts(m, " buffer free\n"); |
| 5104 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5105 | } |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5106 | if (buffer->target_node) |
| 5107 | seq_printf(m, " node %d", buffer->target_node->debug_id); |
Todd Kjos | 8ca86f1 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 5108 | seq_printf(m, " size %zd:%zd data %pK\n", |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5109 | buffer->data_size, buffer->offsets_size, |
| 5110 | buffer->data); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5111 | } |
| 5112 | |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5113 | static void print_binder_work_ilocked(struct seq_file *m, |
| 5114 | struct binder_proc *proc, |
| 5115 | const char *prefix, |
| 5116 | const char *transaction_prefix, |
| 5117 | struct binder_work *w) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5118 | { |
| 5119 | struct binder_node *node; |
| 5120 | struct binder_transaction *t; |
| 5121 | |
| 5122 | switch (w->type) { |
| 5123 | case BINDER_WORK_TRANSACTION: |
| 5124 | t = container_of(w, struct binder_transaction, work); |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5125 | print_binder_transaction_ilocked( |
| 5126 | m, proc, transaction_prefix, t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5127 | break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 5128 | case BINDER_WORK_RETURN_ERROR: { |
| 5129 | struct binder_error *e = container_of( |
| 5130 | w, struct binder_error, work); |
| 5131 | |
| 5132 | seq_printf(m, "%stransaction error: %u\n", |
| 5133 | prefix, e->cmd); |
| 5134 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5135 | case BINDER_WORK_TRANSACTION_COMPLETE: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5136 | seq_printf(m, "%stransaction complete\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5137 | break; |
| 5138 | case BINDER_WORK_NODE: |
| 5139 | node = container_of(w, struct binder_node, work); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5140 | seq_printf(m, "%snode work %d: u%016llx c%016llx\n", |
| 5141 | prefix, node->debug_id, |
| 5142 | (u64)node->ptr, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5143 | break; |
| 5144 | case BINDER_WORK_DEAD_BINDER: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5145 | seq_printf(m, "%shas dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5146 | break; |
| 5147 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5148 | seq_printf(m, "%shas cleared dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5149 | break; |
| 5150 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5151 | seq_printf(m, "%shas cleared death notification\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5152 | break; |
| 5153 | default: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5154 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5155 | break; |
| 5156 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5157 | } |
| 5158 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5159 | static void print_binder_thread_ilocked(struct seq_file *m, |
| 5160 | struct binder_thread *thread, |
| 5161 | int print_always) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5162 | { |
| 5163 | struct binder_transaction *t; |
| 5164 | struct binder_work *w; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5165 | size_t start_pos = m->count; |
| 5166 | size_t header_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5167 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5168 | 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] | 5169 | thread->pid, thread->looper, |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5170 | thread->looper_need_return, |
| 5171 | atomic_read(&thread->tmp_ref)); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5172 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5173 | t = thread->transaction_stack; |
| 5174 | while (t) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5175 | if (t->from == thread) { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5176 | print_binder_transaction_ilocked(m, thread->proc, |
| 5177 | " outgoing transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5178 | t = t->from_parent; |
| 5179 | } else if (t->to_thread == thread) { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5180 | print_binder_transaction_ilocked(m, thread->proc, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5181 | " incoming transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5182 | t = t->to_parent; |
| 5183 | } else { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5184 | print_binder_transaction_ilocked(m, thread->proc, |
| 5185 | " bad transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5186 | t = NULL; |
| 5187 | } |
| 5188 | } |
| 5189 | list_for_each_entry(w, &thread->todo, entry) { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5190 | print_binder_work_ilocked(m, thread->proc, " ", |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5191 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5192 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5193 | if (!print_always && m->count == header_pos) |
| 5194 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5195 | } |
| 5196 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5197 | static void print_binder_node_nilocked(struct seq_file *m, |
| 5198 | struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5199 | { |
| 5200 | struct binder_ref *ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5201 | struct binder_work *w; |
| 5202 | int count; |
| 5203 | |
| 5204 | count = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5205 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5206 | count++; |
| 5207 | |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5208 | 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] | 5209 | node->debug_id, (u64)node->ptr, (u64)node->cookie, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5210 | node->has_strong_ref, node->has_weak_ref, |
| 5211 | node->local_strong_refs, node->local_weak_refs, |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5212 | node->internal_strong_refs, count, node->tmp_refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5213 | if (count) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5214 | seq_puts(m, " proc"); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5215 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5216 | seq_printf(m, " %d", ref->proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5217 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5218 | seq_puts(m, "\n"); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5219 | if (node->proc) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5220 | list_for_each_entry(w, &node->async_todo, entry) |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5221 | print_binder_work_ilocked(m, node->proc, " ", |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5222 | " pending async transaction", w); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5223 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5224 | } |
| 5225 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5226 | static void print_binder_ref_olocked(struct seq_file *m, |
| 5227 | struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5228 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5229 | binder_node_lock(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 5230 | seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n", |
| 5231 | ref->data.debug_id, ref->data.desc, |
| 5232 | ref->node->proc ? "" : "dead ", |
| 5233 | ref->node->debug_id, ref->data.strong, |
| 5234 | ref->data.weak, ref->death); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5235 | binder_node_unlock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5236 | } |
| 5237 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5238 | static void print_binder_proc(struct seq_file *m, |
| 5239 | struct binder_proc *proc, int print_all) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5240 | { |
| 5241 | struct binder_work *w; |
| 5242 | struct rb_node *n; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5243 | size_t start_pos = m->count; |
| 5244 | size_t header_pos; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5245 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5246 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5247 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5248 | seq_printf(m, "context %s\n", proc->context->name); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5249 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5250 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5251 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5252 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5253 | 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] | 5254 | rb_node), print_all); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5255 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5256 | 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] | 5257 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 5258 | rb_node); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5259 | /* |
| 5260 | * take a temporary reference on the node so it |
| 5261 | * survives and isn't removed from the tree |
| 5262 | * while we print it. |
| 5263 | */ |
| 5264 | binder_inc_node_tmpref_ilocked(node); |
| 5265 | /* Need to drop inner lock to take node lock */ |
| 5266 | binder_inner_proc_unlock(proc); |
| 5267 | if (last_node) |
| 5268 | binder_put_node(last_node); |
| 5269 | binder_node_inner_lock(node); |
| 5270 | print_binder_node_nilocked(m, node); |
| 5271 | binder_node_inner_unlock(node); |
| 5272 | last_node = node; |
| 5273 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5274 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5275 | binder_inner_proc_unlock(proc); |
| 5276 | if (last_node) |
| 5277 | binder_put_node(last_node); |
| 5278 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5279 | if (print_all) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5280 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5281 | for (n = rb_first(&proc->refs_by_desc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5282 | n != NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5283 | n = rb_next(n)) |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5284 | print_binder_ref_olocked(m, rb_entry(n, |
| 5285 | struct binder_ref, |
| 5286 | rb_node_desc)); |
| 5287 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5288 | } |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5289 | binder_alloc_print_allocated(m, &proc->alloc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5290 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5291 | list_for_each_entry(w, &proc->todo, entry) |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5292 | print_binder_work_ilocked(m, proc, " ", |
| 5293 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5294 | list_for_each_entry(w, &proc->delivered_death, entry) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5295 | seq_puts(m, " has delivered dead binder\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5296 | break; |
| 5297 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5298 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5299 | if (!print_all && m->count == header_pos) |
| 5300 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5301 | } |
| 5302 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5303 | static const char * const binder_return_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5304 | "BR_ERROR", |
| 5305 | "BR_OK", |
| 5306 | "BR_TRANSACTION", |
| 5307 | "BR_REPLY", |
| 5308 | "BR_ACQUIRE_RESULT", |
| 5309 | "BR_DEAD_REPLY", |
| 5310 | "BR_TRANSACTION_COMPLETE", |
| 5311 | "BR_INCREFS", |
| 5312 | "BR_ACQUIRE", |
| 5313 | "BR_RELEASE", |
| 5314 | "BR_DECREFS", |
| 5315 | "BR_ATTEMPT_ACQUIRE", |
| 5316 | "BR_NOOP", |
| 5317 | "BR_SPAWN_LOOPER", |
| 5318 | "BR_FINISHED", |
| 5319 | "BR_DEAD_BINDER", |
| 5320 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 5321 | "BR_FAILED_REPLY" |
| 5322 | }; |
| 5323 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5324 | static const char * const binder_command_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5325 | "BC_TRANSACTION", |
| 5326 | "BC_REPLY", |
| 5327 | "BC_ACQUIRE_RESULT", |
| 5328 | "BC_FREE_BUFFER", |
| 5329 | "BC_INCREFS", |
| 5330 | "BC_ACQUIRE", |
| 5331 | "BC_RELEASE", |
| 5332 | "BC_DECREFS", |
| 5333 | "BC_INCREFS_DONE", |
| 5334 | "BC_ACQUIRE_DONE", |
| 5335 | "BC_ATTEMPT_ACQUIRE", |
| 5336 | "BC_REGISTER_LOOPER", |
| 5337 | "BC_ENTER_LOOPER", |
| 5338 | "BC_EXIT_LOOPER", |
| 5339 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 5340 | "BC_CLEAR_DEATH_NOTIFICATION", |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 5341 | "BC_DEAD_BINDER_DONE", |
| 5342 | "BC_TRANSACTION_SG", |
| 5343 | "BC_REPLY_SG", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5344 | }; |
| 5345 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5346 | static const char * const binder_objstat_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5347 | "proc", |
| 5348 | "thread", |
| 5349 | "node", |
| 5350 | "ref", |
| 5351 | "death", |
| 5352 | "transaction", |
| 5353 | "transaction_complete" |
| 5354 | }; |
| 5355 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5356 | static void print_binder_stats(struct seq_file *m, const char *prefix, |
| 5357 | struct binder_stats *stats) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5358 | { |
| 5359 | int i; |
| 5360 | |
| 5361 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5362 | ARRAY_SIZE(binder_command_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5363 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5364 | int temp = atomic_read(&stats->bc[i]); |
| 5365 | |
| 5366 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5367 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5368 | binder_command_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5372 | ARRAY_SIZE(binder_return_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5373 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5374 | int temp = atomic_read(&stats->br[i]); |
| 5375 | |
| 5376 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5377 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5378 | binder_return_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5382 | ARRAY_SIZE(binder_objstat_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5383 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5384 | ARRAY_SIZE(stats->obj_deleted)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5385 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5386 | int created = atomic_read(&stats->obj_created[i]); |
| 5387 | int deleted = atomic_read(&stats->obj_deleted[i]); |
| 5388 | |
| 5389 | if (created || deleted) |
| 5390 | seq_printf(m, "%s%s: active %d total %d\n", |
| 5391 | prefix, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5392 | binder_objstat_strings[i], |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5393 | created - deleted, |
| 5394 | created); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5395 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5396 | } |
| 5397 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5398 | static void print_binder_proc_stats(struct seq_file *m, |
| 5399 | struct binder_proc *proc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5400 | { |
| 5401 | struct binder_work *w; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5402 | struct binder_thread *thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5403 | struct rb_node *n; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5404 | int count, strong, weak, ready_threads; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5405 | size_t free_async_space = |
| 5406 | binder_alloc_get_free_async_space(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5407 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5408 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5409 | seq_printf(m, "context %s\n", proc->context->name); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5410 | count = 0; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5411 | ready_threads = 0; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5412 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5413 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 5414 | count++; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5415 | |
| 5416 | list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node) |
| 5417 | ready_threads++; |
| 5418 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5419 | seq_printf(m, " threads: %d\n", count); |
| 5420 | seq_printf(m, " requested threads: %d+%d/%d\n" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5421 | " ready threads %d\n" |
| 5422 | " free async space %zd\n", proc->requested_threads, |
| 5423 | proc->requested_threads_started, proc->max_threads, |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5424 | ready_threads, |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5425 | free_async_space); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5426 | count = 0; |
| 5427 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) |
| 5428 | count++; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5429 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5430 | seq_printf(m, " nodes: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5431 | count = 0; |
| 5432 | strong = 0; |
| 5433 | weak = 0; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5434 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5435 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 5436 | struct binder_ref *ref = rb_entry(n, struct binder_ref, |
| 5437 | rb_node_desc); |
| 5438 | count++; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 5439 | strong += ref->data.strong; |
| 5440 | weak += ref->data.weak; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5441 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5442 | binder_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5443 | 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] | 5444 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5445 | count = binder_alloc_get_allocated_count(&proc->alloc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5446 | seq_printf(m, " buffers: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5447 | |
Sherry Yang | 8ef4665 | 2017-08-31 11:56:36 -0700 | [diff] [blame] | 5448 | binder_alloc_print_pages(m, &proc->alloc); |
| 5449 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5450 | count = 0; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5451 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5452 | list_for_each_entry(w, &proc->todo, entry) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5453 | if (w->type == BINDER_WORK_TRANSACTION) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5454 | count++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5455 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5456 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5457 | seq_printf(m, " pending transactions: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5458 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5459 | print_binder_stats(m, " ", &proc->stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5460 | } |
| 5461 | |
| 5462 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5463 | static int binder_state_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5464 | { |
| 5465 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5466 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5467 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5468 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5469 | seq_puts(m, "binder state:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5470 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5471 | spin_lock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5472 | if (!hlist_empty(&binder_dead_nodes)) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5473 | seq_puts(m, "dead nodes:\n"); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5474 | hlist_for_each_entry(node, &binder_dead_nodes, dead_node) { |
| 5475 | /* |
| 5476 | * take a temporary reference on the node so it |
| 5477 | * survives and isn't removed from the list |
| 5478 | * while we print it. |
| 5479 | */ |
| 5480 | node->tmp_refs++; |
| 5481 | spin_unlock(&binder_dead_nodes_lock); |
| 5482 | if (last_node) |
| 5483 | binder_put_node(last_node); |
| 5484 | binder_node_lock(node); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5485 | print_binder_node_nilocked(m, node); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5486 | binder_node_unlock(node); |
| 5487 | last_node = node; |
| 5488 | spin_lock(&binder_dead_nodes_lock); |
| 5489 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5490 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5491 | if (last_node) |
| 5492 | binder_put_node(last_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5493 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5494 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5495 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5496 | print_binder_proc(m, proc, 1); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5497 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 5498 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5499 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5500 | } |
| 5501 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5502 | static int binder_stats_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5503 | { |
| 5504 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5505 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5506 | seq_puts(m, "binder stats:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5507 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5508 | print_binder_stats(m, "", &binder_stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5509 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5510 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5511 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5512 | print_binder_proc_stats(m, proc); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5513 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 5514 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5515 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5516 | } |
| 5517 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5518 | static int binder_transactions_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5519 | { |
| 5520 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5521 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5522 | seq_puts(m, "binder transactions:\n"); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5523 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5524 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5525 | print_binder_proc(m, proc, 0); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5526 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 5527 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5528 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5529 | } |
| 5530 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5531 | static int binder_proc_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5532 | { |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5533 | struct binder_proc *itr; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5534 | int pid = (unsigned long)m->private; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5535 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5536 | mutex_lock(&binder_procs_lock); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5537 | hlist_for_each_entry(itr, &binder_procs, proc_node) { |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5538 | if (itr->pid == pid) { |
| 5539 | seq_puts(m, "binder proc state:\n"); |
| 5540 | print_binder_proc(m, itr, 1); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5541 | } |
| 5542 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5543 | mutex_unlock(&binder_procs_lock); |
| 5544 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5545 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5546 | } |
| 5547 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5548 | static void print_binder_transaction_log_entry(struct seq_file *m, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5549 | struct binder_transaction_log_entry *e) |
| 5550 | { |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5551 | int debug_id = READ_ONCE(e->debug_id_done); |
| 5552 | /* |
| 5553 | * read barrier to guarantee debug_id_done read before |
| 5554 | * we print the log values |
| 5555 | */ |
| 5556 | smp_rmb(); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5557 | seq_printf(m, |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5558 | "%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] | 5559 | e->debug_id, (e->call_type == 2) ? "reply" : |
| 5560 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5561 | e->from_thread, e->to_proc, e->to_thread, e->context_name, |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 5562 | e->to_node, e->target_handle, e->data_size, e->offsets_size, |
| 5563 | e->return_error, e->return_error_param, |
| 5564 | e->return_error_line); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5565 | /* |
| 5566 | * read-barrier to guarantee read of debug_id_done after |
| 5567 | * done printing the fields of the entry |
| 5568 | */ |
| 5569 | smp_rmb(); |
| 5570 | seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ? |
| 5571 | "\n" : " (incomplete)\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5572 | } |
| 5573 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5574 | 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] | 5575 | { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5576 | struct binder_transaction_log *log = m->private; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5577 | unsigned int log_cur = atomic_read(&log->cur); |
| 5578 | unsigned int count; |
| 5579 | unsigned int cur; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5580 | int i; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5581 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5582 | count = log_cur + 1; |
| 5583 | cur = count < ARRAY_SIZE(log->entry) && !log->full ? |
| 5584 | 0 : count % ARRAY_SIZE(log->entry); |
| 5585 | if (count > ARRAY_SIZE(log->entry) || log->full) |
| 5586 | count = ARRAY_SIZE(log->entry); |
| 5587 | for (i = 0; i < count; i++) { |
| 5588 | unsigned int index = cur++ % ARRAY_SIZE(log->entry); |
| 5589 | |
| 5590 | print_binder_transaction_log_entry(m, &log->entry[index]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5591 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5592 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5593 | } |
| 5594 | |
| 5595 | static const struct file_operations binder_fops = { |
| 5596 | .owner = THIS_MODULE, |
| 5597 | .poll = binder_poll, |
| 5598 | .unlocked_ioctl = binder_ioctl, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5599 | .compat_ioctl = binder_ioctl, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5600 | .mmap = binder_mmap, |
| 5601 | .open = binder_open, |
| 5602 | .flush = binder_flush, |
| 5603 | .release = binder_release, |
| 5604 | }; |
| 5605 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5606 | BINDER_DEBUG_ENTRY(state); |
| 5607 | BINDER_DEBUG_ENTRY(stats); |
| 5608 | BINDER_DEBUG_ENTRY(transactions); |
| 5609 | BINDER_DEBUG_ENTRY(transaction_log); |
| 5610 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5611 | static int __init init_binder_device(const char *name) |
| 5612 | { |
| 5613 | int ret; |
| 5614 | struct binder_device *binder_device; |
| 5615 | |
| 5616 | binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL); |
| 5617 | if (!binder_device) |
| 5618 | return -ENOMEM; |
| 5619 | |
| 5620 | binder_device->miscdev.fops = &binder_fops; |
| 5621 | binder_device->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 5622 | binder_device->miscdev.name = name; |
| 5623 | |
| 5624 | binder_device->context.binder_context_mgr_uid = INVALID_UID; |
| 5625 | binder_device->context.name = name; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5626 | mutex_init(&binder_device->context.context_mgr_node_lock); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5627 | |
| 5628 | ret = misc_register(&binder_device->miscdev); |
| 5629 | if (ret < 0) { |
| 5630 | kfree(binder_device); |
| 5631 | return ret; |
| 5632 | } |
| 5633 | |
| 5634 | hlist_add_head(&binder_device->hlist, &binder_devices); |
| 5635 | |
| 5636 | return ret; |
| 5637 | } |
| 5638 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5639 | static int __init binder_init(void) |
| 5640 | { |
| 5641 | int ret; |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 5642 | char *device_name, *device_names, *device_tmp; |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5643 | struct binder_device *device; |
| 5644 | struct hlist_node *tmp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5645 | |
Tetsuo Handa | 533dfb2 | 2017-11-29 22:29:47 +0900 | [diff] [blame] | 5646 | ret = binder_alloc_shrinker_init(); |
| 5647 | if (ret) |
| 5648 | return ret; |
Sherry Yang | f2517eb | 2017-08-23 08:46:42 -0700 | [diff] [blame] | 5649 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5650 | atomic_set(&binder_transaction_log.cur, ~0U); |
| 5651 | atomic_set(&binder_transaction_log_failed.cur, ~0U); |
| 5652 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5653 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); |
| 5654 | if (binder_debugfs_dir_entry_root) |
| 5655 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", |
| 5656 | binder_debugfs_dir_entry_root); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5657 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5658 | if (binder_debugfs_dir_entry_root) { |
| 5659 | debugfs_create_file("state", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5660 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5661 | binder_debugfs_dir_entry_root, |
| 5662 | NULL, |
| 5663 | &binder_state_fops); |
| 5664 | debugfs_create_file("stats", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5665 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5666 | binder_debugfs_dir_entry_root, |
| 5667 | NULL, |
| 5668 | &binder_stats_fops); |
| 5669 | debugfs_create_file("transactions", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5670 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5671 | binder_debugfs_dir_entry_root, |
| 5672 | NULL, |
| 5673 | &binder_transactions_fops); |
| 5674 | debugfs_create_file("transaction_log", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5675 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5676 | binder_debugfs_dir_entry_root, |
| 5677 | &binder_transaction_log, |
| 5678 | &binder_transaction_log_fops); |
| 5679 | debugfs_create_file("failed_transaction_log", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5680 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5681 | binder_debugfs_dir_entry_root, |
| 5682 | &binder_transaction_log_failed, |
| 5683 | &binder_transaction_log_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5684 | } |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5685 | |
| 5686 | /* |
| 5687 | * Copy the module_parameter string, because we don't want to |
| 5688 | * tokenize it in-place. |
| 5689 | */ |
| 5690 | device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL); |
| 5691 | if (!device_names) { |
| 5692 | ret = -ENOMEM; |
| 5693 | goto err_alloc_device_names_failed; |
| 5694 | } |
| 5695 | strcpy(device_names, binder_devices_param); |
| 5696 | |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 5697 | device_tmp = device_names; |
| 5698 | while ((device_name = strsep(&device_tmp, ","))) { |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5699 | ret = init_binder_device(device_name); |
| 5700 | if (ret) |
| 5701 | goto err_init_binder_device_failed; |
| 5702 | } |
| 5703 | |
| 5704 | return ret; |
| 5705 | |
| 5706 | err_init_binder_device_failed: |
| 5707 | hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) { |
| 5708 | misc_deregister(&device->miscdev); |
| 5709 | hlist_del(&device->hlist); |
| 5710 | kfree(device); |
| 5711 | } |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 5712 | |
| 5713 | kfree(device_names); |
| 5714 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5715 | err_alloc_device_names_failed: |
| 5716 | debugfs_remove_recursive(binder_debugfs_dir_entry_root); |
| 5717 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5718 | return ret; |
| 5719 | } |
| 5720 | |
| 5721 | device_initcall(binder_init); |
| 5722 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5723 | #define CREATE_TRACE_POINTS |
| 5724 | #include "binder_trace.h" |
| 5725 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5726 | MODULE_LICENSE("GPL v2"); |