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 <asm/cacheflush.h> |
| 55 | #include <linux/fdtable.h> |
| 56 | #include <linux/file.h> |
Colin Cross | e2610b2 | 2013-05-06 23:50:15 +0000 | [diff] [blame] | 57 | #include <linux/freezer.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 58 | #include <linux/fs.h> |
| 59 | #include <linux/list.h> |
| 60 | #include <linux/miscdevice.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 61 | #include <linux/module.h> |
| 62 | #include <linux/mutex.h> |
| 63 | #include <linux/nsproxy.h> |
| 64 | #include <linux/poll.h> |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 65 | #include <linux/debugfs.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 66 | #include <linux/rbtree.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 67 | #include <linux/sched/signal.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 68 | #include <linux/sched/mm.h> |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 69 | #include <linux/seq_file.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 70 | #include <linux/uaccess.h> |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 71 | #include <linux/pid_namespace.h> |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 72 | #include <linux/security.h> |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 73 | #include <linux/spinlock.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> |
Todd Kjos | 0c972a0 | 2017-06-29 12:01:41 -0700 | [diff] [blame] | 76 | #include "binder_alloc.h" |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 77 | #include "binder_trace.h" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 78 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 79 | static HLIST_HEAD(binder_deferred_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 80 | static DEFINE_MUTEX(binder_deferred_lock); |
| 81 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 82 | static HLIST_HEAD(binder_devices); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 83 | static HLIST_HEAD(binder_procs); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 84 | static DEFINE_MUTEX(binder_procs_lock); |
| 85 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 86 | static HLIST_HEAD(binder_dead_nodes); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 87 | static DEFINE_SPINLOCK(binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 88 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 89 | static struct dentry *binder_debugfs_dir_entry_root; |
| 90 | static struct dentry *binder_debugfs_dir_entry_proc; |
Todd Kjos | 656a800 | 2017-06-29 12:01:45 -0700 | [diff] [blame] | 91 | static atomic_t binder_last_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 92 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 93 | #define BINDER_DEBUG_ENTRY(name) \ |
| 94 | static int binder_##name##_open(struct inode *inode, struct file *file) \ |
| 95 | { \ |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 96 | return single_open(file, binder_##name##_show, inode->i_private); \ |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 97 | } \ |
| 98 | \ |
| 99 | static const struct file_operations binder_##name##_fops = { \ |
| 100 | .owner = THIS_MODULE, \ |
| 101 | .open = binder_##name##_open, \ |
| 102 | .read = seq_read, \ |
| 103 | .llseek = seq_lseek, \ |
| 104 | .release = single_release, \ |
| 105 | } |
| 106 | |
| 107 | static int binder_proc_show(struct seq_file *m, void *unused); |
| 108 | BINDER_DEBUG_ENTRY(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 109 | |
| 110 | /* This is only defined in include/asm-arm/sizes.h */ |
| 111 | #ifndef SZ_1K |
| 112 | #define SZ_1K 0x400 |
| 113 | #endif |
| 114 | |
| 115 | #ifndef SZ_4M |
| 116 | #define SZ_4M 0x400000 |
| 117 | #endif |
| 118 | |
| 119 | #define FORBIDDEN_MMAP_FLAGS (VM_WRITE) |
| 120 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 121 | enum { |
| 122 | BINDER_DEBUG_USER_ERROR = 1U << 0, |
| 123 | BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1, |
| 124 | BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2, |
| 125 | BINDER_DEBUG_OPEN_CLOSE = 1U << 3, |
| 126 | BINDER_DEBUG_DEAD_BINDER = 1U << 4, |
| 127 | BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5, |
| 128 | BINDER_DEBUG_READ_WRITE = 1U << 6, |
| 129 | BINDER_DEBUG_USER_REFS = 1U << 7, |
| 130 | BINDER_DEBUG_THREADS = 1U << 8, |
| 131 | BINDER_DEBUG_TRANSACTION = 1U << 9, |
| 132 | BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10, |
| 133 | BINDER_DEBUG_FREE_BUFFER = 1U << 11, |
| 134 | BINDER_DEBUG_INTERNAL_REFS = 1U << 12, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 135 | BINDER_DEBUG_PRIORITY_CAP = 1U << 13, |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 136 | BINDER_DEBUG_SPINLOCKS = 1U << 14, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 137 | }; |
| 138 | static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR | |
| 139 | BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION; |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 140 | module_param_named(debug_mask, binder_debug_mask, uint, 0644); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 141 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 142 | static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES; |
| 143 | module_param_named(devices, binder_devices_param, charp, 0444); |
| 144 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 145 | static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait); |
| 146 | static int binder_stop_on_user_error; |
| 147 | |
| 148 | static int binder_set_stop_on_user_error(const char *val, |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 149 | const struct kernel_param *kp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 150 | { |
| 151 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 152 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 153 | ret = param_set_int(val, kp); |
| 154 | if (binder_stop_on_user_error < 2) |
| 155 | wake_up(&binder_user_error_wait); |
| 156 | return ret; |
| 157 | } |
| 158 | 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] | 159 | param_get_int, &binder_stop_on_user_error, 0644); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 160 | |
| 161 | #define binder_debug(mask, x...) \ |
| 162 | do { \ |
| 163 | if (binder_debug_mask & mask) \ |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 164 | pr_info(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 165 | } while (0) |
| 166 | |
| 167 | #define binder_user_error(x...) \ |
| 168 | do { \ |
| 169 | if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \ |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 170 | pr_info(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 171 | if (binder_stop_on_user_error) \ |
| 172 | binder_stop_on_user_error = 2; \ |
| 173 | } while (0) |
| 174 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 175 | #define to_flat_binder_object(hdr) \ |
| 176 | container_of(hdr, struct flat_binder_object, hdr) |
| 177 | |
| 178 | #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr) |
| 179 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 180 | #define to_binder_buffer_object(hdr) \ |
| 181 | container_of(hdr, struct binder_buffer_object, hdr) |
| 182 | |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 183 | #define to_binder_fd_array_object(hdr) \ |
| 184 | container_of(hdr, struct binder_fd_array_object, hdr) |
| 185 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 186 | enum binder_stat_types { |
| 187 | BINDER_STAT_PROC, |
| 188 | BINDER_STAT_THREAD, |
| 189 | BINDER_STAT_NODE, |
| 190 | BINDER_STAT_REF, |
| 191 | BINDER_STAT_DEATH, |
| 192 | BINDER_STAT_TRANSACTION, |
| 193 | BINDER_STAT_TRANSACTION_COMPLETE, |
| 194 | BINDER_STAT_COUNT |
| 195 | }; |
| 196 | |
| 197 | struct binder_stats { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 198 | atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1]; |
| 199 | atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1]; |
| 200 | atomic_t obj_created[BINDER_STAT_COUNT]; |
| 201 | atomic_t obj_deleted[BINDER_STAT_COUNT]; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | static struct binder_stats binder_stats; |
| 205 | |
| 206 | static inline void binder_stats_deleted(enum binder_stat_types type) |
| 207 | { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 208 | atomic_inc(&binder_stats.obj_deleted[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static inline void binder_stats_created(enum binder_stat_types type) |
| 212 | { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 213 | atomic_inc(&binder_stats.obj_created[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | struct binder_transaction_log_entry { |
| 217 | int debug_id; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 218 | int debug_id_done; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 219 | int call_type; |
| 220 | int from_proc; |
| 221 | int from_thread; |
| 222 | int target_handle; |
| 223 | int to_proc; |
| 224 | int to_thread; |
| 225 | int to_node; |
| 226 | int data_size; |
| 227 | int offsets_size; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 228 | int return_error_line; |
| 229 | uint32_t return_error; |
| 230 | uint32_t return_error_param; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 231 | const char *context_name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 232 | }; |
| 233 | struct binder_transaction_log { |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 234 | atomic_t cur; |
| 235 | bool full; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 236 | struct binder_transaction_log_entry entry[32]; |
| 237 | }; |
| 238 | static struct binder_transaction_log binder_transaction_log; |
| 239 | static struct binder_transaction_log binder_transaction_log_failed; |
| 240 | |
| 241 | static struct binder_transaction_log_entry *binder_transaction_log_add( |
| 242 | struct binder_transaction_log *log) |
| 243 | { |
| 244 | struct binder_transaction_log_entry *e; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 245 | unsigned int cur = atomic_inc_return(&log->cur); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 246 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 247 | if (cur >= ARRAY_SIZE(log->entry)) |
Gustavo A. R. Silva | 197410a | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 248 | log->full = true; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 249 | e = &log->entry[cur % ARRAY_SIZE(log->entry)]; |
| 250 | WRITE_ONCE(e->debug_id_done, 0); |
| 251 | /* |
| 252 | * write-barrier to synchronize access to e->debug_id_done. |
| 253 | * We make sure the initialized 0 value is seen before |
| 254 | * memset() other fields are zeroed by memset. |
| 255 | */ |
| 256 | smp_wmb(); |
| 257 | memset(e, 0, sizeof(*e)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 258 | return e; |
| 259 | } |
| 260 | |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 261 | struct binder_context { |
| 262 | struct binder_node *binder_context_mgr_node; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 263 | struct mutex context_mgr_node_lock; |
| 264 | |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 265 | kuid_t binder_context_mgr_uid; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 266 | const char *name; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 267 | }; |
| 268 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 269 | struct binder_device { |
| 270 | struct hlist_node hlist; |
| 271 | struct miscdevice miscdev; |
| 272 | struct binder_context context; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 273 | }; |
| 274 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 275 | /** |
| 276 | * struct binder_work - work enqueued on a worklist |
| 277 | * @entry: node enqueued on list |
| 278 | * @type: type of work to be performed |
| 279 | * |
| 280 | * There are separate work lists for proc, thread, and node (async). |
| 281 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 282 | struct binder_work { |
| 283 | struct list_head entry; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 284 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 285 | enum { |
| 286 | BINDER_WORK_TRANSACTION = 1, |
| 287 | BINDER_WORK_TRANSACTION_COMPLETE, |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 288 | BINDER_WORK_RETURN_ERROR, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 289 | BINDER_WORK_NODE, |
| 290 | BINDER_WORK_DEAD_BINDER, |
| 291 | BINDER_WORK_DEAD_BINDER_AND_CLEAR, |
| 292 | BINDER_WORK_CLEAR_DEATH_NOTIFICATION, |
| 293 | } type; |
| 294 | }; |
| 295 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 296 | struct binder_error { |
| 297 | struct binder_work work; |
| 298 | uint32_t cmd; |
| 299 | }; |
| 300 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 301 | /** |
| 302 | * struct binder_node - binder node bookkeeping |
| 303 | * @debug_id: unique ID for debugging |
| 304 | * (invariant after initialized) |
| 305 | * @lock: lock for node fields |
| 306 | * @work: worklist element for node work |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 307 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 308 | * @rb_node: element for proc->nodes tree |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 309 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 310 | * @dead_node: element for binder_dead_nodes list |
| 311 | * (protected by binder_dead_nodes_lock) |
| 312 | * @proc: binder_proc that owns this node |
| 313 | * (invariant after initialized) |
| 314 | * @refs: list of references on this node |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 315 | * (protected by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 316 | * @internal_strong_refs: used to take strong references when |
| 317 | * initiating a transaction |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 318 | * (protected by @proc->inner_lock if @proc |
| 319 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 320 | * @local_weak_refs: weak user refs from local process |
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_strong_refs: strong 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 | * @tmp_refs: temporary kernel refs |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 327 | * (protected by @proc->inner_lock while @proc |
| 328 | * is valid, and by binder_dead_nodes_lock |
| 329 | * if @proc is NULL. During inc/dec and node release |
| 330 | * it is also protected by @lock to provide safety |
| 331 | * as the node dies and @proc becomes NULL) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 332 | * @ptr: userspace pointer for node |
| 333 | * (invariant, no lock needed) |
| 334 | * @cookie: userspace cookie for node |
| 335 | * (invariant, no lock needed) |
| 336 | * @has_strong_ref: userspace notified of strong ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 337 | * (protected by @proc->inner_lock if @proc |
| 338 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 339 | * @pending_strong_ref: userspace has acked notification 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 | * @has_weak_ref: userspace notified of weak 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 | * @pending_weak_ref: userspace has acked notification 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 | * @has_async_transaction: async transaction to node in progress |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 349 | * (protected by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 350 | * @accept_fds: file descriptor operations supported for node |
| 351 | * (invariant after initialized) |
| 352 | * @min_priority: minimum scheduling priority |
| 353 | * (invariant after initialized) |
| 354 | * @async_todo: list of async work items |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 355 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 356 | * |
| 357 | * Bookkeeping structure for binder nodes. |
| 358 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 359 | struct binder_node { |
| 360 | int debug_id; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 361 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 362 | struct binder_work work; |
| 363 | union { |
| 364 | struct rb_node rb_node; |
| 365 | struct hlist_node dead_node; |
| 366 | }; |
| 367 | struct binder_proc *proc; |
| 368 | struct hlist_head refs; |
| 369 | int internal_strong_refs; |
| 370 | int local_weak_refs; |
| 371 | int local_strong_refs; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 372 | int tmp_refs; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 373 | binder_uintptr_t ptr; |
| 374 | binder_uintptr_t cookie; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 375 | struct { |
| 376 | /* |
| 377 | * bitfield elements protected by |
| 378 | * proc inner_lock |
| 379 | */ |
| 380 | u8 has_strong_ref:1; |
| 381 | u8 pending_strong_ref:1; |
| 382 | u8 has_weak_ref:1; |
| 383 | u8 pending_weak_ref:1; |
| 384 | }; |
| 385 | struct { |
| 386 | /* |
| 387 | * invariant after initialization |
| 388 | */ |
| 389 | u8 accept_fds:1; |
| 390 | u8 min_priority; |
| 391 | }; |
| 392 | bool has_async_transaction; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 393 | struct list_head async_todo; |
| 394 | }; |
| 395 | |
| 396 | struct binder_ref_death { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 397 | /** |
| 398 | * @work: worklist element for death notifications |
| 399 | * (protected by inner_lock of the proc that |
| 400 | * this ref belongs to) |
| 401 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 402 | struct binder_work work; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 403 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 404 | }; |
| 405 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 406 | /** |
| 407 | * struct binder_ref_data - binder_ref counts and id |
| 408 | * @debug_id: unique ID for the ref |
| 409 | * @desc: unique userspace handle for ref |
| 410 | * @strong: strong ref count (debugging only if not locked) |
| 411 | * @weak: weak ref count (debugging only if not locked) |
| 412 | * |
| 413 | * Structure to hold ref count and ref id information. Since |
| 414 | * the actual ref can only be accessed with a lock, this structure |
| 415 | * is used to return information about the ref to callers of |
| 416 | * ref inc/dec functions. |
| 417 | */ |
| 418 | struct binder_ref_data { |
| 419 | int debug_id; |
| 420 | uint32_t desc; |
| 421 | int strong; |
| 422 | int weak; |
| 423 | }; |
| 424 | |
| 425 | /** |
| 426 | * struct binder_ref - struct to track references on nodes |
| 427 | * @data: binder_ref_data containing id, handle, and current refcounts |
| 428 | * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree |
| 429 | * @rb_node_node: node for lookup by @node in proc's rb_tree |
| 430 | * @node_entry: list entry for node->refs list in target node |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 431 | * (protected by @node->lock) |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 432 | * @proc: binder_proc containing ref |
| 433 | * @node: binder_node of target node. When cleaning up a |
| 434 | * ref for deletion in binder_cleanup_ref, a non-NULL |
| 435 | * @node indicates the node must be freed |
| 436 | * @death: pointer to death notification (ref_death) if requested |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 437 | * (protected by @node->lock) |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 438 | * |
| 439 | * Structure to track references from procA to target node (on procB). This |
| 440 | * structure is unsafe to access without holding @proc->outer_lock. |
| 441 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 442 | struct binder_ref { |
| 443 | /* Lookups needed: */ |
| 444 | /* node + proc => ref (transaction) */ |
| 445 | /* desc + proc => ref (transaction, inc/dec ref) */ |
| 446 | /* node => refs + procs (proc exit) */ |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 447 | struct binder_ref_data data; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 448 | struct rb_node rb_node_desc; |
| 449 | struct rb_node rb_node_node; |
| 450 | struct hlist_node node_entry; |
| 451 | struct binder_proc *proc; |
| 452 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 453 | struct binder_ref_death *death; |
| 454 | }; |
| 455 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 456 | enum binder_deferred_state { |
| 457 | BINDER_DEFERRED_PUT_FILES = 0x01, |
| 458 | BINDER_DEFERRED_FLUSH = 0x02, |
| 459 | BINDER_DEFERRED_RELEASE = 0x04, |
| 460 | }; |
| 461 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 462 | /** |
| 463 | * struct binder_proc - binder process bookkeeping |
| 464 | * @proc_node: element for binder_procs list |
| 465 | * @threads: rbtree of binder_threads in this proc |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 466 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 467 | * @nodes: rbtree of binder nodes associated with |
| 468 | * this proc ordered by node->ptr |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 469 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 470 | * @refs_by_desc: rbtree of refs ordered by ref->desc |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 471 | * (protected by @outer_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 472 | * @refs_by_node: rbtree of refs ordered by ref->node |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 473 | * (protected by @outer_lock) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 474 | * @waiting_threads: threads currently waiting for proc work |
| 475 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 476 | * @pid PID of group_leader of process |
| 477 | * (invariant after initialized) |
| 478 | * @tsk task_struct for group_leader of process |
| 479 | * (invariant after initialized) |
| 480 | * @files files_struct for process |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 481 | * (protected by @files_lock) |
| 482 | * @files_lock mutex to protect @files |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 483 | * @deferred_work_node: element for binder_deferred_list |
| 484 | * (protected by binder_deferred_lock) |
| 485 | * @deferred_work: bitmap of deferred work to perform |
| 486 | * (protected by binder_deferred_lock) |
| 487 | * @is_dead: process is dead and awaiting free |
| 488 | * when outstanding transactions are cleaned up |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 489 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 490 | * @todo: list of work for this process |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 491 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 492 | * @stats: per-process binder statistics |
| 493 | * (atomics, no lock needed) |
| 494 | * @delivered_death: list of delivered death notification |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 495 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 496 | * @max_threads: cap on number of binder threads |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 497 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 498 | * @requested_threads: number of binder threads requested but not |
| 499 | * yet started. In current implementation, can |
| 500 | * only be 0 or 1. |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 501 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 502 | * @requested_threads_started: number binder threads started |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 503 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 504 | * @tmp_ref: temporary reference to indicate proc is in use |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 505 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 506 | * @default_priority: default scheduler priority |
| 507 | * (invariant after initialized) |
| 508 | * @debugfs_entry: debugfs node |
| 509 | * @alloc: binder allocator bookkeeping |
| 510 | * @context: binder_context for this proc |
| 511 | * (invariant after initialized) |
| 512 | * @inner_lock: can nest under outer_lock and/or node lock |
| 513 | * @outer_lock: no nesting under innor or node lock |
| 514 | * Lock order: 1) outer, 2) node, 3) inner |
| 515 | * |
| 516 | * Bookkeeping structure for binder processes |
| 517 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 518 | struct binder_proc { |
| 519 | struct hlist_node proc_node; |
| 520 | struct rb_root threads; |
| 521 | struct rb_root nodes; |
| 522 | struct rb_root refs_by_desc; |
| 523 | struct rb_root refs_by_node; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 524 | struct list_head waiting_threads; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 525 | int pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 526 | struct task_struct *tsk; |
| 527 | struct files_struct *files; |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 528 | struct mutex files_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 529 | struct hlist_node deferred_work_node; |
| 530 | int deferred_work; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 531 | bool is_dead; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 532 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 533 | struct list_head todo; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 534 | struct binder_stats stats; |
| 535 | struct list_head delivered_death; |
| 536 | int max_threads; |
| 537 | int requested_threads; |
| 538 | int requested_threads_started; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 539 | int tmp_ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 540 | long default_priority; |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 541 | struct dentry *debugfs_entry; |
Todd Kjos | fdfb4a9 | 2017-06-29 12:01:38 -0700 | [diff] [blame] | 542 | struct binder_alloc alloc; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 543 | struct binder_context *context; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 544 | spinlock_t inner_lock; |
| 545 | spinlock_t outer_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 546 | }; |
| 547 | |
| 548 | enum { |
| 549 | BINDER_LOOPER_STATE_REGISTERED = 0x01, |
| 550 | BINDER_LOOPER_STATE_ENTERED = 0x02, |
| 551 | BINDER_LOOPER_STATE_EXITED = 0x04, |
| 552 | BINDER_LOOPER_STATE_INVALID = 0x08, |
| 553 | BINDER_LOOPER_STATE_WAITING = 0x10, |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 554 | BINDER_LOOPER_STATE_POLL = 0x20, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 555 | }; |
| 556 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 557 | /** |
| 558 | * struct binder_thread - binder thread bookkeeping |
| 559 | * @proc: binder process for this thread |
| 560 | * (invariant after initialization) |
| 561 | * @rb_node: element for proc->threads rbtree |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 562 | * (protected by @proc->inner_lock) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 563 | * @waiting_thread_node: element for @proc->waiting_threads list |
| 564 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 565 | * @pid: PID for this thread |
| 566 | * (invariant after initialization) |
| 567 | * @looper: bitmap of looping state |
| 568 | * (only accessed by this thread) |
| 569 | * @looper_needs_return: looping thread needs to exit driver |
| 570 | * (no lock needed) |
| 571 | * @transaction_stack: stack of in-progress transactions for this thread |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 572 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 573 | * @todo: list of work to do for this thread |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 574 | * (protected by @proc->inner_lock) |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 575 | * @process_todo: whether work in @todo should be processed |
| 576 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 577 | * @return_error: transaction errors reported by this thread |
| 578 | * (only accessed by this thread) |
| 579 | * @reply_error: transaction errors reported by target thread |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 580 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 581 | * @wait: wait queue for thread work |
| 582 | * @stats: per-thread statistics |
| 583 | * (atomics, no lock needed) |
| 584 | * @tmp_ref: temporary reference to indicate thread is in use |
| 585 | * (atomic since @proc->inner_lock cannot |
| 586 | * always be acquired) |
| 587 | * @is_dead: thread is dead and awaiting free |
| 588 | * when outstanding transactions are cleaned up |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 589 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 590 | * |
| 591 | * Bookkeeping structure for binder threads. |
| 592 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 593 | struct binder_thread { |
| 594 | struct binder_proc *proc; |
| 595 | struct rb_node rb_node; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 596 | struct list_head waiting_thread_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 597 | int pid; |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 598 | int looper; /* only modified by this thread */ |
| 599 | bool looper_need_return; /* can be written by other thread */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 600 | struct binder_transaction *transaction_stack; |
| 601 | struct list_head todo; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 602 | bool process_todo; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 603 | struct binder_error return_error; |
| 604 | struct binder_error reply_error; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 605 | wait_queue_head_t wait; |
| 606 | struct binder_stats stats; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 607 | atomic_t tmp_ref; |
| 608 | bool is_dead; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 609 | }; |
| 610 | |
| 611 | struct binder_transaction { |
| 612 | int debug_id; |
| 613 | struct binder_work work; |
| 614 | struct binder_thread *from; |
| 615 | struct binder_transaction *from_parent; |
| 616 | struct binder_proc *to_proc; |
| 617 | struct binder_thread *to_thread; |
| 618 | struct binder_transaction *to_parent; |
| 619 | unsigned need_reply:1; |
| 620 | /* unsigned is_dead:1; */ /* not used at the moment */ |
| 621 | |
| 622 | struct binder_buffer *buffer; |
| 623 | unsigned int code; |
| 624 | unsigned int flags; |
| 625 | long priority; |
| 626 | long saved_priority; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 627 | kuid_t sender_euid; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 628 | /** |
| 629 | * @lock: protects @from, @to_proc, and @to_thread |
| 630 | * |
| 631 | * @from, @to_proc, and @to_thread can be set to NULL |
| 632 | * during thread teardown |
| 633 | */ |
| 634 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 635 | }; |
| 636 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 637 | /** |
| 638 | * binder_proc_lock() - Acquire outer lock for given binder_proc |
| 639 | * @proc: struct binder_proc to acquire |
| 640 | * |
| 641 | * Acquires proc->outer_lock. Used to protect binder_ref |
| 642 | * structures associated with the given proc. |
| 643 | */ |
| 644 | #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__) |
| 645 | static void |
| 646 | _binder_proc_lock(struct binder_proc *proc, int line) |
| 647 | { |
| 648 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 649 | "%s: line=%d\n", __func__, line); |
| 650 | spin_lock(&proc->outer_lock); |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * binder_proc_unlock() - Release spinlock for given binder_proc |
| 655 | * @proc: struct binder_proc to acquire |
| 656 | * |
| 657 | * Release lock acquired via binder_proc_lock() |
| 658 | */ |
| 659 | #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__) |
| 660 | static void |
| 661 | _binder_proc_unlock(struct binder_proc *proc, int line) |
| 662 | { |
| 663 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 664 | "%s: line=%d\n", __func__, line); |
| 665 | spin_unlock(&proc->outer_lock); |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * binder_inner_proc_lock() - Acquire inner lock for given binder_proc |
| 670 | * @proc: struct binder_proc to acquire |
| 671 | * |
| 672 | * Acquires proc->inner_lock. Used to protect todo lists |
| 673 | */ |
| 674 | #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__) |
| 675 | static void |
| 676 | _binder_inner_proc_lock(struct binder_proc *proc, int line) |
| 677 | { |
| 678 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 679 | "%s: line=%d\n", __func__, line); |
| 680 | spin_lock(&proc->inner_lock); |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * binder_inner_proc_unlock() - Release inner lock for given binder_proc |
| 685 | * @proc: struct binder_proc to acquire |
| 686 | * |
| 687 | * Release lock acquired via binder_inner_proc_lock() |
| 688 | */ |
| 689 | #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__) |
| 690 | static void |
| 691 | _binder_inner_proc_unlock(struct binder_proc *proc, int line) |
| 692 | { |
| 693 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 694 | "%s: line=%d\n", __func__, line); |
| 695 | spin_unlock(&proc->inner_lock); |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * binder_node_lock() - Acquire spinlock for given binder_node |
| 700 | * @node: struct binder_node to acquire |
| 701 | * |
| 702 | * Acquires node->lock. Used to protect binder_node fields |
| 703 | */ |
| 704 | #define binder_node_lock(node) _binder_node_lock(node, __LINE__) |
| 705 | static void |
| 706 | _binder_node_lock(struct binder_node *node, int line) |
| 707 | { |
| 708 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 709 | "%s: line=%d\n", __func__, line); |
| 710 | spin_lock(&node->lock); |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * binder_node_unlock() - Release spinlock for given binder_proc |
| 715 | * @node: struct binder_node to acquire |
| 716 | * |
| 717 | * Release lock acquired via binder_node_lock() |
| 718 | */ |
| 719 | #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__) |
| 720 | static void |
| 721 | _binder_node_unlock(struct binder_node *node, int line) |
| 722 | { |
| 723 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 724 | "%s: line=%d\n", __func__, line); |
| 725 | spin_unlock(&node->lock); |
| 726 | } |
| 727 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 728 | /** |
| 729 | * binder_node_inner_lock() - Acquire node and inner locks |
| 730 | * @node: struct binder_node to acquire |
| 731 | * |
| 732 | * Acquires node->lock. If node->proc also acquires |
| 733 | * proc->inner_lock. Used to protect binder_node fields |
| 734 | */ |
| 735 | #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__) |
| 736 | static void |
| 737 | _binder_node_inner_lock(struct binder_node *node, int line) |
| 738 | { |
| 739 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 740 | "%s: line=%d\n", __func__, line); |
| 741 | spin_lock(&node->lock); |
| 742 | if (node->proc) |
| 743 | binder_inner_proc_lock(node->proc); |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * binder_node_unlock() - Release node and inner locks |
| 748 | * @node: struct binder_node to acquire |
| 749 | * |
| 750 | * Release lock acquired via binder_node_lock() |
| 751 | */ |
| 752 | #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__) |
| 753 | static void |
| 754 | _binder_node_inner_unlock(struct binder_node *node, int line) |
| 755 | { |
| 756 | struct binder_proc *proc = node->proc; |
| 757 | |
| 758 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 759 | "%s: line=%d\n", __func__, line); |
| 760 | if (proc) |
| 761 | binder_inner_proc_unlock(proc); |
| 762 | spin_unlock(&node->lock); |
| 763 | } |
| 764 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 765 | static bool binder_worklist_empty_ilocked(struct list_head *list) |
| 766 | { |
| 767 | return list_empty(list); |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * binder_worklist_empty() - Check if no items on the work list |
| 772 | * @proc: binder_proc associated with list |
| 773 | * @list: list to check |
| 774 | * |
| 775 | * Return: true if there are no items on list, else false |
| 776 | */ |
| 777 | static bool binder_worklist_empty(struct binder_proc *proc, |
| 778 | struct list_head *list) |
| 779 | { |
| 780 | bool ret; |
| 781 | |
| 782 | binder_inner_proc_lock(proc); |
| 783 | ret = binder_worklist_empty_ilocked(list); |
| 784 | binder_inner_proc_unlock(proc); |
| 785 | return ret; |
| 786 | } |
| 787 | |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 788 | /** |
| 789 | * binder_enqueue_work_ilocked() - Add an item to the work list |
| 790 | * @work: struct binder_work to add to list |
| 791 | * @target_list: list to add work to |
| 792 | * |
| 793 | * Adds the work to the specified list. Asserts that work |
| 794 | * is not already on a list. |
| 795 | * |
| 796 | * Requires the proc->inner_lock to be held. |
| 797 | */ |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 798 | static void |
| 799 | binder_enqueue_work_ilocked(struct binder_work *work, |
| 800 | struct list_head *target_list) |
| 801 | { |
| 802 | BUG_ON(target_list == NULL); |
| 803 | BUG_ON(work->entry.next && !list_empty(&work->entry)); |
| 804 | list_add_tail(&work->entry, target_list); |
| 805 | } |
| 806 | |
| 807 | /** |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 808 | * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work |
| 809 | * @thread: thread to queue work to |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 810 | * @work: struct binder_work to add to list |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 811 | * |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 812 | * Adds the work to the todo list of the thread. Doesn't set the process_todo |
| 813 | * flag, which means that (if it wasn't already set) the thread will go to |
| 814 | * sleep without handling this work when it calls read. |
| 815 | * |
| 816 | * Requires the proc->inner_lock to be held. |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 817 | */ |
| 818 | static void |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 819 | binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread, |
| 820 | struct binder_work *work) |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 821 | { |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 822 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list |
| 827 | * @thread: thread to queue work to |
| 828 | * @work: struct binder_work to add to list |
| 829 | * |
| 830 | * Adds the work to the todo list of the thread, and enables processing |
| 831 | * of the todo queue. |
| 832 | * |
| 833 | * Requires the proc->inner_lock to be held. |
| 834 | */ |
| 835 | static void |
| 836 | binder_enqueue_thread_work_ilocked(struct binder_thread *thread, |
| 837 | struct binder_work *work) |
| 838 | { |
| 839 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 840 | thread->process_todo = true; |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * binder_enqueue_thread_work() - Add an item to the thread work list |
| 845 | * @thread: thread to queue work to |
| 846 | * @work: struct binder_work to add to list |
| 847 | * |
| 848 | * Adds the work to the todo list of the thread, and enables processing |
| 849 | * of the todo queue. |
| 850 | */ |
| 851 | static void |
| 852 | binder_enqueue_thread_work(struct binder_thread *thread, |
| 853 | struct binder_work *work) |
| 854 | { |
| 855 | binder_inner_proc_lock(thread->proc); |
| 856 | binder_enqueue_thread_work_ilocked(thread, work); |
| 857 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | static void |
| 861 | binder_dequeue_work_ilocked(struct binder_work *work) |
| 862 | { |
| 863 | list_del_init(&work->entry); |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * binder_dequeue_work() - Removes an item from the work list |
| 868 | * @proc: binder_proc associated with list |
| 869 | * @work: struct binder_work to remove from list |
| 870 | * |
| 871 | * Removes the specified work item from whatever list it is on. |
| 872 | * Can safely be called if work is not on any list. |
| 873 | */ |
| 874 | static void |
| 875 | binder_dequeue_work(struct binder_proc *proc, struct binder_work *work) |
| 876 | { |
| 877 | binder_inner_proc_lock(proc); |
| 878 | binder_dequeue_work_ilocked(work); |
| 879 | binder_inner_proc_unlock(proc); |
| 880 | } |
| 881 | |
| 882 | static struct binder_work *binder_dequeue_work_head_ilocked( |
| 883 | struct list_head *list) |
| 884 | { |
| 885 | struct binder_work *w; |
| 886 | |
| 887 | w = list_first_entry_or_null(list, struct binder_work, entry); |
| 888 | if (w) |
| 889 | list_del_init(&w->entry); |
| 890 | return w; |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * binder_dequeue_work_head() - Dequeues the item at head of list |
| 895 | * @proc: binder_proc associated with list |
| 896 | * @list: list to dequeue head |
| 897 | * |
| 898 | * Removes the head of the list if there are items on the list |
| 899 | * |
| 900 | * Return: pointer dequeued binder_work, NULL if list was empty |
| 901 | */ |
| 902 | static struct binder_work *binder_dequeue_work_head( |
| 903 | struct binder_proc *proc, |
| 904 | struct list_head *list) |
| 905 | { |
| 906 | struct binder_work *w; |
| 907 | |
| 908 | binder_inner_proc_lock(proc); |
| 909 | w = binder_dequeue_work_head_ilocked(list); |
| 910 | binder_inner_proc_unlock(proc); |
| 911 | return w; |
| 912 | } |
| 913 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 914 | static void |
| 915 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 916 | static void binder_free_thread(struct binder_thread *thread); |
| 917 | static void binder_free_proc(struct binder_proc *proc); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 918 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 919 | |
Sachin Kamat | efde99c | 2012-08-17 16:39:36 +0530 | [diff] [blame] | 920 | 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] | 921 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 922 | unsigned long rlim_cur; |
| 923 | unsigned long irqs; |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 924 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 925 | |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 926 | mutex_lock(&proc->files_lock); |
| 927 | if (proc->files == NULL) { |
| 928 | ret = -ESRCH; |
| 929 | goto err; |
| 930 | } |
| 931 | if (!lock_task_sighand(proc->tsk, &irqs)) { |
| 932 | ret = -EMFILE; |
| 933 | goto err; |
| 934 | } |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 935 | rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE); |
| 936 | unlock_task_sighand(proc->tsk, &irqs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 937 | |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 938 | ret = __alloc_fd(proc->files, 0, rlim_cur, flags); |
| 939 | err: |
| 940 | mutex_unlock(&proc->files_lock); |
| 941 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /* |
| 945 | * copied from fd_install |
| 946 | */ |
| 947 | static void task_fd_install( |
| 948 | struct binder_proc *proc, unsigned int fd, struct file *file) |
| 949 | { |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 950 | mutex_lock(&proc->files_lock); |
Al Viro | f869e8a | 2012-08-15 21:06:33 -0400 | [diff] [blame] | 951 | if (proc->files) |
| 952 | __fd_install(proc->files, fd, file); |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 953 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* |
| 957 | * copied from sys_close |
| 958 | */ |
| 959 | static long task_close_fd(struct binder_proc *proc, unsigned int fd) |
| 960 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 961 | int retval; |
| 962 | |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 963 | mutex_lock(&proc->files_lock); |
| 964 | if (proc->files == NULL) { |
| 965 | retval = -ESRCH; |
| 966 | goto err; |
| 967 | } |
Al Viro | 483ce1d | 2012-08-19 12:04:24 -0400 | [diff] [blame] | 968 | retval = __close_fd(proc->files, fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 969 | /* can't restart close syscall because file table entry was cleared */ |
| 970 | if (unlikely(retval == -ERESTARTSYS || |
| 971 | retval == -ERESTARTNOINTR || |
| 972 | retval == -ERESTARTNOHAND || |
| 973 | retval == -ERESTART_RESTARTBLOCK)) |
| 974 | retval = -EINTR; |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 975 | err: |
| 976 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 977 | return retval; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 978 | } |
| 979 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 980 | static bool binder_has_work_ilocked(struct binder_thread *thread, |
| 981 | bool do_proc_work) |
| 982 | { |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 983 | return thread->process_todo || |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 984 | thread->looper_need_return || |
| 985 | (do_proc_work && |
| 986 | !binder_worklist_empty_ilocked(&thread->proc->todo)); |
| 987 | } |
| 988 | |
| 989 | static bool binder_has_work(struct binder_thread *thread, bool do_proc_work) |
| 990 | { |
| 991 | bool has_work; |
| 992 | |
| 993 | binder_inner_proc_lock(thread->proc); |
| 994 | has_work = binder_has_work_ilocked(thread, do_proc_work); |
| 995 | binder_inner_proc_unlock(thread->proc); |
| 996 | |
| 997 | return has_work; |
| 998 | } |
| 999 | |
| 1000 | static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread) |
| 1001 | { |
| 1002 | return !thread->transaction_stack && |
| 1003 | binder_worklist_empty_ilocked(&thread->todo) && |
| 1004 | (thread->looper & (BINDER_LOOPER_STATE_ENTERED | |
| 1005 | BINDER_LOOPER_STATE_REGISTERED)); |
| 1006 | } |
| 1007 | |
| 1008 | static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc, |
| 1009 | bool sync) |
| 1010 | { |
| 1011 | struct rb_node *n; |
| 1012 | struct binder_thread *thread; |
| 1013 | |
| 1014 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 1015 | thread = rb_entry(n, struct binder_thread, rb_node); |
| 1016 | if (thread->looper & BINDER_LOOPER_STATE_POLL && |
| 1017 | binder_available_for_proc_work_ilocked(thread)) { |
| 1018 | if (sync) |
| 1019 | wake_up_interruptible_sync(&thread->wait); |
| 1020 | else |
| 1021 | wake_up_interruptible(&thread->wait); |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1026 | /** |
| 1027 | * binder_select_thread_ilocked() - selects a thread for doing proc work. |
| 1028 | * @proc: process to select a thread from |
| 1029 | * |
| 1030 | * Note that calling this function moves the thread off the waiting_threads |
| 1031 | * list, so it can only be woken up by the caller of this function, or a |
| 1032 | * signal. Therefore, callers *should* always wake up the thread this function |
| 1033 | * returns. |
| 1034 | * |
| 1035 | * Return: If there's a thread currently waiting for process work, |
| 1036 | * returns that thread. Otherwise returns NULL. |
| 1037 | */ |
| 1038 | static struct binder_thread * |
| 1039 | binder_select_thread_ilocked(struct binder_proc *proc) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1040 | { |
| 1041 | struct binder_thread *thread; |
| 1042 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1043 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1044 | thread = list_first_entry_or_null(&proc->waiting_threads, |
| 1045 | struct binder_thread, |
| 1046 | waiting_thread_node); |
| 1047 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1048 | if (thread) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1049 | list_del_init(&thread->waiting_thread_node); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1050 | |
| 1051 | return thread; |
| 1052 | } |
| 1053 | |
| 1054 | /** |
| 1055 | * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work. |
| 1056 | * @proc: process to wake up a thread in |
| 1057 | * @thread: specific thread to wake-up (may be NULL) |
| 1058 | * @sync: whether to do a synchronous wake-up |
| 1059 | * |
| 1060 | * This function wakes up a thread in the @proc process. |
| 1061 | * The caller may provide a specific thread to wake-up in |
| 1062 | * the @thread parameter. If @thread is NULL, this function |
| 1063 | * will wake up threads that have called poll(). |
| 1064 | * |
| 1065 | * Note that for this function to work as expected, callers |
| 1066 | * should first call binder_select_thread() to find a thread |
| 1067 | * to handle the work (if they don't have a thread already), |
| 1068 | * and pass the result into the @thread parameter. |
| 1069 | */ |
| 1070 | static void binder_wakeup_thread_ilocked(struct binder_proc *proc, |
| 1071 | struct binder_thread *thread, |
| 1072 | bool sync) |
| 1073 | { |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1074 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1075 | |
| 1076 | if (thread) { |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1077 | if (sync) |
| 1078 | wake_up_interruptible_sync(&thread->wait); |
| 1079 | else |
| 1080 | wake_up_interruptible(&thread->wait); |
| 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | /* Didn't find a thread waiting for proc work; this can happen |
| 1085 | * in two scenarios: |
| 1086 | * 1. All threads are busy handling transactions |
| 1087 | * In that case, one of those threads should call back into |
| 1088 | * the kernel driver soon and pick up this work. |
| 1089 | * 2. Threads are using the (e)poll interface, in which case |
| 1090 | * they may be blocked on the waitqueue without having been |
| 1091 | * added to waiting_threads. For this case, we just iterate |
| 1092 | * over all threads not handling transaction work, and |
| 1093 | * wake them all up. We wake all because we don't know whether |
| 1094 | * a thread that called into (e)poll is handling non-binder |
| 1095 | * work currently. |
| 1096 | */ |
| 1097 | binder_wakeup_poll_threads_ilocked(proc, sync); |
| 1098 | } |
| 1099 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1100 | static void binder_wakeup_proc_ilocked(struct binder_proc *proc) |
| 1101 | { |
| 1102 | struct binder_thread *thread = binder_select_thread_ilocked(proc); |
| 1103 | |
| 1104 | binder_wakeup_thread_ilocked(proc, thread, /* sync = */false); |
| 1105 | } |
| 1106 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1107 | static void binder_set_nice(long nice) |
| 1108 | { |
| 1109 | long min_nice; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1110 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1111 | if (can_nice(current, nice)) { |
| 1112 | set_user_nice(current, nice); |
| 1113 | return; |
| 1114 | } |
Krzysztof Opasiak | c3643b6 | 2017-07-05 19:24:44 +0200 | [diff] [blame] | 1115 | min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1116 | binder_debug(BINDER_DEBUG_PRIORITY_CAP, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1117 | "%d: nice value %ld not allowed use %ld instead\n", |
| 1118 | current->pid, nice, min_nice); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1119 | set_user_nice(current, min_nice); |
Dongsheng Yang | 8698a74 | 2014-03-11 18:09:12 +0800 | [diff] [blame] | 1120 | if (min_nice <= MAX_NICE) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1121 | return; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1122 | binder_user_error("%d RLIMIT_NICE not set\n", current->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1123 | } |
| 1124 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1125 | static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc, |
| 1126 | binder_uintptr_t ptr) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1127 | { |
| 1128 | struct rb_node *n = proc->nodes.rb_node; |
| 1129 | struct binder_node *node; |
| 1130 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1131 | assert_spin_locked(&proc->inner_lock); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1132 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1133 | while (n) { |
| 1134 | node = rb_entry(n, struct binder_node, rb_node); |
| 1135 | |
| 1136 | if (ptr < node->ptr) |
| 1137 | n = n->rb_left; |
| 1138 | else if (ptr > node->ptr) |
| 1139 | n = n->rb_right; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1140 | else { |
| 1141 | /* |
| 1142 | * take an implicit weak reference |
| 1143 | * to ensure node stays alive until |
| 1144 | * call to binder_put_node() |
| 1145 | */ |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1146 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1147 | return node; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1148 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1149 | } |
| 1150 | return NULL; |
| 1151 | } |
| 1152 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1153 | static struct binder_node *binder_get_node(struct binder_proc *proc, |
| 1154 | binder_uintptr_t ptr) |
| 1155 | { |
| 1156 | struct binder_node *node; |
| 1157 | |
| 1158 | binder_inner_proc_lock(proc); |
| 1159 | node = binder_get_node_ilocked(proc, ptr); |
| 1160 | binder_inner_proc_unlock(proc); |
| 1161 | return node; |
| 1162 | } |
| 1163 | |
| 1164 | static struct binder_node *binder_init_node_ilocked( |
| 1165 | struct binder_proc *proc, |
| 1166 | struct binder_node *new_node, |
| 1167 | struct flat_binder_object *fp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1168 | { |
| 1169 | struct rb_node **p = &proc->nodes.rb_node; |
| 1170 | struct rb_node *parent = NULL; |
| 1171 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1172 | binder_uintptr_t ptr = fp ? fp->binder : 0; |
| 1173 | binder_uintptr_t cookie = fp ? fp->cookie : 0; |
| 1174 | __u32 flags = fp ? fp->flags : 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1175 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1176 | assert_spin_locked(&proc->inner_lock); |
| 1177 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1178 | while (*p) { |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1179 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1180 | parent = *p; |
| 1181 | node = rb_entry(parent, struct binder_node, rb_node); |
| 1182 | |
| 1183 | if (ptr < node->ptr) |
| 1184 | p = &(*p)->rb_left; |
| 1185 | else if (ptr > node->ptr) |
| 1186 | p = &(*p)->rb_right; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1187 | else { |
| 1188 | /* |
| 1189 | * A matching node is already in |
| 1190 | * the rb tree. Abandon the init |
| 1191 | * and return it. |
| 1192 | */ |
| 1193 | binder_inc_node_tmpref_ilocked(node); |
| 1194 | return node; |
| 1195 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1196 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1197 | node = new_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1198 | binder_stats_created(BINDER_STAT_NODE); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1199 | node->tmp_refs++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1200 | rb_link_node(&node->rb_node, parent, p); |
| 1201 | rb_insert_color(&node->rb_node, &proc->nodes); |
Todd Kjos | 656a800 | 2017-06-29 12:01:45 -0700 | [diff] [blame] | 1202 | node->debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1203 | node->proc = proc; |
| 1204 | node->ptr = ptr; |
| 1205 | node->cookie = cookie; |
| 1206 | node->work.type = BINDER_WORK_NODE; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1207 | node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK; |
| 1208 | node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 1209 | spin_lock_init(&node->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1210 | INIT_LIST_HEAD(&node->work.entry); |
| 1211 | INIT_LIST_HEAD(&node->async_todo); |
| 1212 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1213 | "%d:%d node %d u%016llx c%016llx created\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1214 | proc->pid, current->pid, node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1215 | (u64)node->ptr, (u64)node->cookie); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1216 | |
| 1217 | return node; |
| 1218 | } |
| 1219 | |
| 1220 | static struct binder_node *binder_new_node(struct binder_proc *proc, |
| 1221 | struct flat_binder_object *fp) |
| 1222 | { |
| 1223 | struct binder_node *node; |
| 1224 | struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 1225 | |
| 1226 | if (!new_node) |
| 1227 | return NULL; |
| 1228 | binder_inner_proc_lock(proc); |
| 1229 | node = binder_init_node_ilocked(proc, new_node, fp); |
| 1230 | binder_inner_proc_unlock(proc); |
| 1231 | if (node != new_node) |
| 1232 | /* |
| 1233 | * The node was already added by another thread |
| 1234 | */ |
| 1235 | kfree(new_node); |
| 1236 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1237 | return node; |
| 1238 | } |
| 1239 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1240 | static void binder_free_node(struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1241 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1242 | kfree(node); |
| 1243 | binder_stats_deleted(BINDER_STAT_NODE); |
| 1244 | } |
| 1245 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1246 | static int binder_inc_node_nilocked(struct binder_node *node, int strong, |
| 1247 | int internal, |
| 1248 | struct list_head *target_list) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1249 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1250 | struct binder_proc *proc = node->proc; |
| 1251 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1252 | assert_spin_locked(&node->lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1253 | if (proc) |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1254 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1255 | if (strong) { |
| 1256 | if (internal) { |
| 1257 | if (target_list == NULL && |
| 1258 | node->internal_strong_refs == 0 && |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 1259 | !(node->proc && |
| 1260 | node == node->proc->context->binder_context_mgr_node && |
| 1261 | node->has_strong_ref)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1262 | pr_err("invalid inc strong node for %d\n", |
| 1263 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1264 | return -EINVAL; |
| 1265 | } |
| 1266 | node->internal_strong_refs++; |
| 1267 | } else |
| 1268 | node->local_strong_refs++; |
| 1269 | if (!node->has_strong_ref && target_list) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1270 | binder_dequeue_work_ilocked(&node->work); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 1271 | /* |
| 1272 | * Note: this function is the only place where we queue |
| 1273 | * directly to a thread->todo without using the |
| 1274 | * corresponding binder_enqueue_thread_work() helper |
| 1275 | * functions; in this case it's ok to not set the |
| 1276 | * process_todo flag, since we know this node work will |
| 1277 | * always be followed by other work that starts queue |
| 1278 | * processing: in case of synchronous transactions, a |
| 1279 | * BR_REPLY or BR_ERROR; in case of oneway |
| 1280 | * transactions, a BR_TRANSACTION_COMPLETE. |
| 1281 | */ |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1282 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1283 | } |
| 1284 | } else { |
| 1285 | if (!internal) |
| 1286 | node->local_weak_refs++; |
| 1287 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { |
| 1288 | if (target_list == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1289 | pr_err("invalid inc weak node for %d\n", |
| 1290 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1291 | return -EINVAL; |
| 1292 | } |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 1293 | /* |
| 1294 | * See comment above |
| 1295 | */ |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1296 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1302 | static int binder_inc_node(struct binder_node *node, int strong, int internal, |
| 1303 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1304 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1305 | int ret; |
| 1306 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1307 | binder_node_inner_lock(node); |
| 1308 | ret = binder_inc_node_nilocked(node, strong, internal, target_list); |
| 1309 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1310 | |
| 1311 | return ret; |
| 1312 | } |
| 1313 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1314 | static bool binder_dec_node_nilocked(struct binder_node *node, |
| 1315 | int strong, int internal) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1316 | { |
| 1317 | struct binder_proc *proc = node->proc; |
| 1318 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1319 | assert_spin_locked(&node->lock); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1320 | if (proc) |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1321 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1322 | if (strong) { |
| 1323 | if (internal) |
| 1324 | node->internal_strong_refs--; |
| 1325 | else |
| 1326 | node->local_strong_refs--; |
| 1327 | if (node->local_strong_refs || node->internal_strong_refs) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1328 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1329 | } else { |
| 1330 | if (!internal) |
| 1331 | node->local_weak_refs--; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1332 | if (node->local_weak_refs || node->tmp_refs || |
| 1333 | !hlist_empty(&node->refs)) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1334 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1335 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1336 | |
| 1337 | if (proc && (node->has_strong_ref || node->has_weak_ref)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1338 | if (list_empty(&node->work.entry)) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1339 | binder_enqueue_work_ilocked(&node->work, &proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1340 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1341 | } |
| 1342 | } else { |
| 1343 | if (hlist_empty(&node->refs) && !node->local_strong_refs && |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1344 | !node->local_weak_refs && !node->tmp_refs) { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1345 | if (proc) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1346 | binder_dequeue_work_ilocked(&node->work); |
| 1347 | rb_erase(&node->rb_node, &proc->nodes); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1348 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1349 | "refless node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1350 | node->debug_id); |
| 1351 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1352 | BUG_ON(!list_empty(&node->work.entry)); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 1353 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1354 | /* |
| 1355 | * tmp_refs could have changed so |
| 1356 | * check it again |
| 1357 | */ |
| 1358 | if (node->tmp_refs) { |
| 1359 | spin_unlock(&binder_dead_nodes_lock); |
| 1360 | return false; |
| 1361 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1362 | hlist_del(&node->dead_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 1363 | spin_unlock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1364 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1365 | "dead node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1366 | node->debug_id); |
| 1367 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1368 | return true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1369 | } |
| 1370 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1371 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1372 | } |
| 1373 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1374 | static void binder_dec_node(struct binder_node *node, int strong, int internal) |
| 1375 | { |
| 1376 | bool free_node; |
| 1377 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1378 | binder_node_inner_lock(node); |
| 1379 | free_node = binder_dec_node_nilocked(node, strong, internal); |
| 1380 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1381 | if (free_node) |
| 1382 | binder_free_node(node); |
| 1383 | } |
| 1384 | |
| 1385 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1386 | { |
| 1387 | /* |
| 1388 | * No call to binder_inc_node() is needed since we |
| 1389 | * don't need to inform userspace of any changes to |
| 1390 | * tmp_refs |
| 1391 | */ |
| 1392 | node->tmp_refs++; |
| 1393 | } |
| 1394 | |
| 1395 | /** |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1396 | * binder_inc_node_tmpref() - take a temporary reference on node |
| 1397 | * @node: node to reference |
| 1398 | * |
| 1399 | * Take reference on node to prevent the node from being freed |
| 1400 | * while referenced only by a local variable. The inner lock is |
| 1401 | * needed to serialize with the node work on the queue (which |
| 1402 | * isn't needed after the node is dead). If the node is dead |
| 1403 | * (node->proc is NULL), use binder_dead_nodes_lock to protect |
| 1404 | * node->tmp_refs against dead-node-only cases where the node |
| 1405 | * lock cannot be acquired (eg traversing the dead node list to |
| 1406 | * print nodes) |
| 1407 | */ |
| 1408 | static void binder_inc_node_tmpref(struct binder_node *node) |
| 1409 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1410 | binder_node_lock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1411 | if (node->proc) |
| 1412 | binder_inner_proc_lock(node->proc); |
| 1413 | else |
| 1414 | spin_lock(&binder_dead_nodes_lock); |
| 1415 | binder_inc_node_tmpref_ilocked(node); |
| 1416 | if (node->proc) |
| 1417 | binder_inner_proc_unlock(node->proc); |
| 1418 | else |
| 1419 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1420 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /** |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1424 | * binder_dec_node_tmpref() - remove a temporary reference on node |
| 1425 | * @node: node to reference |
| 1426 | * |
| 1427 | * Release temporary reference on node taken via binder_inc_node_tmpref() |
| 1428 | */ |
| 1429 | static void binder_dec_node_tmpref(struct binder_node *node) |
| 1430 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1431 | bool free_node; |
| 1432 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1433 | binder_node_inner_lock(node); |
| 1434 | if (!node->proc) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1435 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1436 | node->tmp_refs--; |
| 1437 | BUG_ON(node->tmp_refs < 0); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1438 | if (!node->proc) |
| 1439 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1440 | /* |
| 1441 | * Call binder_dec_node() to check if all refcounts are 0 |
| 1442 | * and cleanup is needed. Calling with strong=0 and internal=1 |
| 1443 | * causes no actual reference to be released in binder_dec_node(). |
| 1444 | * If that changes, a change is needed here too. |
| 1445 | */ |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1446 | free_node = binder_dec_node_nilocked(node, 0, 1); |
| 1447 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1448 | if (free_node) |
| 1449 | binder_free_node(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | static void binder_put_node(struct binder_node *node) |
| 1453 | { |
| 1454 | binder_dec_node_tmpref(node); |
| 1455 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1456 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1457 | static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc, |
| 1458 | u32 desc, bool need_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1459 | { |
| 1460 | struct rb_node *n = proc->refs_by_desc.rb_node; |
| 1461 | struct binder_ref *ref; |
| 1462 | |
| 1463 | while (n) { |
| 1464 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1465 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1466 | if (desc < ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1467 | n = n->rb_left; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1468 | } else if (desc > ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1469 | n = n->rb_right; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1470 | } else if (need_strong_ref && !ref->data.strong) { |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1471 | binder_user_error("tried to use weak ref as strong ref\n"); |
| 1472 | return NULL; |
| 1473 | } else { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1474 | return ref; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1475 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1476 | } |
| 1477 | return NULL; |
| 1478 | } |
| 1479 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1480 | /** |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1481 | * 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] | 1482 | * @proc: binder_proc that owns the ref |
| 1483 | * @node: binder_node of target |
| 1484 | * @new_ref: newly allocated binder_ref to be initialized or %NULL |
| 1485 | * |
| 1486 | * Look up the ref for the given node and return it if it exists |
| 1487 | * |
| 1488 | * If it doesn't exist and the caller provides a newly allocated |
| 1489 | * ref, initialize the fields of the newly allocated ref and insert |
| 1490 | * into the given proc rb_trees and node refs list. |
| 1491 | * |
| 1492 | * Return: the ref for node. It is possible that another thread |
| 1493 | * allocated/initialized the ref first in which case the |
| 1494 | * returned ref would be different than the passed-in |
| 1495 | * new_ref. new_ref must be kfree'd by the caller in |
| 1496 | * this case. |
| 1497 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1498 | static struct binder_ref *binder_get_ref_for_node_olocked( |
| 1499 | struct binder_proc *proc, |
| 1500 | struct binder_node *node, |
| 1501 | struct binder_ref *new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1502 | { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1503 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1504 | struct rb_node **p = &proc->refs_by_node.rb_node; |
| 1505 | struct rb_node *parent = NULL; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1506 | struct binder_ref *ref; |
| 1507 | struct rb_node *n; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1508 | |
| 1509 | while (*p) { |
| 1510 | parent = *p; |
| 1511 | ref = rb_entry(parent, struct binder_ref, rb_node_node); |
| 1512 | |
| 1513 | if (node < ref->node) |
| 1514 | p = &(*p)->rb_left; |
| 1515 | else if (node > ref->node) |
| 1516 | p = &(*p)->rb_right; |
| 1517 | else |
| 1518 | return ref; |
| 1519 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1520 | if (!new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1521 | return NULL; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1522 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1523 | binder_stats_created(BINDER_STAT_REF); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1524 | new_ref->data.debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1525 | new_ref->proc = proc; |
| 1526 | new_ref->node = node; |
| 1527 | rb_link_node(&new_ref->rb_node_node, parent, p); |
| 1528 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); |
| 1529 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1530 | 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] | 1531 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 1532 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1533 | if (ref->data.desc > new_ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1534 | break; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1535 | new_ref->data.desc = ref->data.desc + 1; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | p = &proc->refs_by_desc.rb_node; |
| 1539 | while (*p) { |
| 1540 | parent = *p; |
| 1541 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); |
| 1542 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1543 | 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_left; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1545 | else if (new_ref->data.desc > ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1546 | p = &(*p)->rb_right; |
| 1547 | else |
| 1548 | BUG(); |
| 1549 | } |
| 1550 | rb_link_node(&new_ref->rb_node_desc, parent, p); |
| 1551 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1552 | |
| 1553 | binder_node_lock(node); |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1554 | hlist_add_head(&new_ref->node_entry, &node->refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1555 | |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1556 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1557 | "%d new ref %d desc %d for node %d\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1558 | proc->pid, new_ref->data.debug_id, new_ref->data.desc, |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1559 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1560 | binder_node_unlock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1561 | return new_ref; |
| 1562 | } |
| 1563 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1564 | static void binder_cleanup_ref_olocked(struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1565 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1566 | bool delete_node = false; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1567 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1568 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1569 | "%d delete ref %d desc %d for node %d\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1570 | ref->proc->pid, ref->data.debug_id, ref->data.desc, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1571 | ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1572 | |
| 1573 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); |
| 1574 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1575 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1576 | binder_node_inner_lock(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1577 | if (ref->data.strong) |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1578 | binder_dec_node_nilocked(ref->node, 1, 1); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1579 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1580 | hlist_del(&ref->node_entry); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1581 | delete_node = binder_dec_node_nilocked(ref->node, 0, 1); |
| 1582 | binder_node_inner_unlock(ref->node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1583 | /* |
| 1584 | * Clear ref->node unless we want the caller to free the node |
| 1585 | */ |
| 1586 | if (!delete_node) { |
| 1587 | /* |
| 1588 | * The caller uses ref->node to determine |
| 1589 | * whether the node needs to be freed. Clear |
| 1590 | * it since the node is still alive. |
| 1591 | */ |
| 1592 | ref->node = NULL; |
| 1593 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1594 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1595 | if (ref->death) { |
| 1596 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1597 | "%d delete ref %d desc %d has death notification\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1598 | ref->proc->pid, ref->data.debug_id, |
| 1599 | ref->data.desc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1600 | binder_dequeue_work(ref->proc, &ref->death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1601 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 1602 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1603 | binder_stats_deleted(BINDER_STAT_REF); |
| 1604 | } |
| 1605 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1606 | /** |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1607 | * binder_inc_ref_olocked() - increment the ref for given handle |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1608 | * @ref: ref to be incremented |
| 1609 | * @strong: if true, strong increment, else weak |
| 1610 | * @target_list: list to queue node work on |
| 1611 | * |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1612 | * Increment the ref. @ref->proc->outer_lock must be held on entry |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1613 | * |
| 1614 | * Return: 0, if successful, else errno |
| 1615 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1616 | static int binder_inc_ref_olocked(struct binder_ref *ref, int strong, |
| 1617 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1618 | { |
| 1619 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1620 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1621 | if (strong) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1622 | if (ref->data.strong == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1623 | ret = binder_inc_node(ref->node, 1, 1, target_list); |
| 1624 | if (ret) |
| 1625 | return ret; |
| 1626 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1627 | ref->data.strong++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1628 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1629 | if (ref->data.weak == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1630 | ret = binder_inc_node(ref->node, 0, 1, target_list); |
| 1631 | if (ret) |
| 1632 | return ret; |
| 1633 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1634 | ref->data.weak++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1635 | } |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1639 | /** |
| 1640 | * binder_dec_ref() - dec the ref for given handle |
| 1641 | * @ref: ref to be decremented |
| 1642 | * @strong: if true, strong decrement, else weak |
| 1643 | * |
| 1644 | * Decrement the ref. |
| 1645 | * |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1646 | * Return: true if ref is cleaned up and ready to be freed |
| 1647 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1648 | 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] | 1649 | { |
| 1650 | if (strong) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1651 | if (ref->data.strong == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1652 | 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] | 1653 | ref->proc->pid, ref->data.debug_id, |
| 1654 | ref->data.desc, ref->data.strong, |
| 1655 | ref->data.weak); |
| 1656 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1657 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1658 | ref->data.strong--; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1659 | if (ref->data.strong == 0) |
| 1660 | binder_dec_node(ref->node, strong, 1); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1661 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1662 | if (ref->data.weak == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1663 | 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] | 1664 | ref->proc->pid, ref->data.debug_id, |
| 1665 | ref->data.desc, ref->data.strong, |
| 1666 | ref->data.weak); |
| 1667 | return false; |
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 | ref->data.weak--; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1670 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1671 | if (ref->data.strong == 0 && ref->data.weak == 0) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1672 | binder_cleanup_ref_olocked(ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1673 | return true; |
| 1674 | } |
| 1675 | return false; |
| 1676 | } |
| 1677 | |
| 1678 | /** |
| 1679 | * binder_get_node_from_ref() - get the node from the given proc/desc |
| 1680 | * @proc: proc containing the ref |
| 1681 | * @desc: the handle associated with the ref |
| 1682 | * @need_strong_ref: if true, only return node if ref is strong |
| 1683 | * @rdata: the id/refcount data for the ref |
| 1684 | * |
| 1685 | * Given a proc and ref handle, return the associated binder_node |
| 1686 | * |
| 1687 | * Return: a binder_node or NULL if not found or not strong when strong required |
| 1688 | */ |
| 1689 | static struct binder_node *binder_get_node_from_ref( |
| 1690 | struct binder_proc *proc, |
| 1691 | u32 desc, bool need_strong_ref, |
| 1692 | struct binder_ref_data *rdata) |
| 1693 | { |
| 1694 | struct binder_node *node; |
| 1695 | struct binder_ref *ref; |
| 1696 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1697 | binder_proc_lock(proc); |
| 1698 | ref = binder_get_ref_olocked(proc, desc, need_strong_ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1699 | if (!ref) |
| 1700 | goto err_no_ref; |
| 1701 | node = ref->node; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1702 | /* |
| 1703 | * Take an implicit reference on the node to ensure |
| 1704 | * it stays alive until the call to binder_put_node() |
| 1705 | */ |
| 1706 | binder_inc_node_tmpref(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1707 | if (rdata) |
| 1708 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1709 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1710 | |
| 1711 | return node; |
| 1712 | |
| 1713 | err_no_ref: |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1714 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1715 | return NULL; |
| 1716 | } |
| 1717 | |
| 1718 | /** |
| 1719 | * binder_free_ref() - free the binder_ref |
| 1720 | * @ref: ref to free |
| 1721 | * |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1722 | * Free the binder_ref. Free the binder_node indicated by ref->node |
| 1723 | * (if non-NULL) and the binder_ref_death indicated by ref->death. |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1724 | */ |
| 1725 | static void binder_free_ref(struct binder_ref *ref) |
| 1726 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1727 | if (ref->node) |
| 1728 | binder_free_node(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1729 | kfree(ref->death); |
| 1730 | kfree(ref); |
| 1731 | } |
| 1732 | |
| 1733 | /** |
| 1734 | * binder_update_ref_for_handle() - inc/dec the ref for given handle |
| 1735 | * @proc: proc containing the ref |
| 1736 | * @desc: the handle associated with the ref |
| 1737 | * @increment: true=inc reference, false=dec reference |
| 1738 | * @strong: true=strong reference, false=weak reference |
| 1739 | * @rdata: the id/refcount data for the ref |
| 1740 | * |
| 1741 | * Given a proc and ref handle, increment or decrement the ref |
| 1742 | * according to "increment" arg. |
| 1743 | * |
| 1744 | * Return: 0 if successful, else errno |
| 1745 | */ |
| 1746 | static int binder_update_ref_for_handle(struct binder_proc *proc, |
| 1747 | uint32_t desc, bool increment, bool strong, |
| 1748 | struct binder_ref_data *rdata) |
| 1749 | { |
| 1750 | int ret = 0; |
| 1751 | struct binder_ref *ref; |
| 1752 | bool delete_ref = false; |
| 1753 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1754 | binder_proc_lock(proc); |
| 1755 | ref = binder_get_ref_olocked(proc, desc, strong); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1756 | if (!ref) { |
| 1757 | ret = -EINVAL; |
| 1758 | goto err_no_ref; |
| 1759 | } |
| 1760 | if (increment) |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1761 | ret = binder_inc_ref_olocked(ref, strong, NULL); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1762 | else |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1763 | delete_ref = binder_dec_ref_olocked(ref, strong); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1764 | |
| 1765 | if (rdata) |
| 1766 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1767 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1768 | |
| 1769 | if (delete_ref) |
| 1770 | binder_free_ref(ref); |
| 1771 | return ret; |
| 1772 | |
| 1773 | err_no_ref: |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1774 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1775 | return ret; |
| 1776 | } |
| 1777 | |
| 1778 | /** |
| 1779 | * binder_dec_ref_for_handle() - dec the ref for given handle |
| 1780 | * @proc: proc containing the ref |
| 1781 | * @desc: the handle associated with the ref |
| 1782 | * @strong: true=strong reference, false=weak reference |
| 1783 | * @rdata: the id/refcount data for the ref |
| 1784 | * |
| 1785 | * Just calls binder_update_ref_for_handle() to decrement the ref. |
| 1786 | * |
| 1787 | * Return: 0 if successful, else errno |
| 1788 | */ |
| 1789 | static int binder_dec_ref_for_handle(struct binder_proc *proc, |
| 1790 | uint32_t desc, bool strong, struct binder_ref_data *rdata) |
| 1791 | { |
| 1792 | return binder_update_ref_for_handle(proc, desc, false, strong, rdata); |
| 1793 | } |
| 1794 | |
| 1795 | |
| 1796 | /** |
| 1797 | * binder_inc_ref_for_node() - increment the ref for given proc/node |
| 1798 | * @proc: proc containing the ref |
| 1799 | * @node: target node |
| 1800 | * @strong: true=strong reference, false=weak reference |
| 1801 | * @target_list: worklist to use if node is incremented |
| 1802 | * @rdata: the id/refcount data for the ref |
| 1803 | * |
| 1804 | * Given a proc and node, increment the ref. Create the ref if it |
| 1805 | * doesn't already exist |
| 1806 | * |
| 1807 | * Return: 0 if successful, else errno |
| 1808 | */ |
| 1809 | static int binder_inc_ref_for_node(struct binder_proc *proc, |
| 1810 | struct binder_node *node, |
| 1811 | bool strong, |
| 1812 | struct list_head *target_list, |
| 1813 | struct binder_ref_data *rdata) |
| 1814 | { |
| 1815 | struct binder_ref *ref; |
| 1816 | struct binder_ref *new_ref = NULL; |
| 1817 | int ret = 0; |
| 1818 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1819 | binder_proc_lock(proc); |
| 1820 | ref = binder_get_ref_for_node_olocked(proc, node, NULL); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1821 | if (!ref) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1822 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1823 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
| 1824 | if (!new_ref) |
| 1825 | return -ENOMEM; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1826 | binder_proc_lock(proc); |
| 1827 | ref = binder_get_ref_for_node_olocked(proc, node, new_ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1828 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1829 | ret = binder_inc_ref_olocked(ref, strong, target_list); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1830 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1831 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1832 | if (new_ref && ref != new_ref) |
| 1833 | /* |
| 1834 | * Another thread created the ref first so |
| 1835 | * free the one we allocated |
| 1836 | */ |
| 1837 | kfree(new_ref); |
| 1838 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1839 | } |
| 1840 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1841 | static void binder_pop_transaction_ilocked(struct binder_thread *target_thread, |
| 1842 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1843 | { |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1844 | BUG_ON(!target_thread); |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1845 | assert_spin_locked(&target_thread->proc->inner_lock); |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1846 | BUG_ON(target_thread->transaction_stack != t); |
| 1847 | BUG_ON(target_thread->transaction_stack->from != target_thread); |
| 1848 | target_thread->transaction_stack = |
| 1849 | target_thread->transaction_stack->from_parent; |
| 1850 | t->from = NULL; |
| 1851 | } |
| 1852 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1853 | /** |
| 1854 | * binder_thread_dec_tmpref() - decrement thread->tmp_ref |
| 1855 | * @thread: thread to decrement |
| 1856 | * |
| 1857 | * A thread needs to be kept alive while being used to create or |
| 1858 | * handle a transaction. binder_get_txn_from() is used to safely |
| 1859 | * extract t->from from a binder_transaction and keep the thread |
| 1860 | * indicated by t->from from being freed. When done with that |
| 1861 | * binder_thread, this function is called to decrement the |
| 1862 | * tmp_ref and free if appropriate (thread has been released |
| 1863 | * and no transaction being processed by the driver) |
| 1864 | */ |
| 1865 | static void binder_thread_dec_tmpref(struct binder_thread *thread) |
| 1866 | { |
| 1867 | /* |
| 1868 | * atomic is used to protect the counter value while |
| 1869 | * it cannot reach zero or thread->is_dead is false |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1870 | */ |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1871 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1872 | atomic_dec(&thread->tmp_ref); |
| 1873 | if (thread->is_dead && !atomic_read(&thread->tmp_ref)) { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1874 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1875 | binder_free_thread(thread); |
| 1876 | return; |
| 1877 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1878 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | /** |
| 1882 | * binder_proc_dec_tmpref() - decrement proc->tmp_ref |
| 1883 | * @proc: proc to decrement |
| 1884 | * |
| 1885 | * A binder_proc needs to be kept alive while being used to create or |
| 1886 | * handle a transaction. proc->tmp_ref is incremented when |
| 1887 | * creating a new transaction or the binder_proc is currently in-use |
| 1888 | * by threads that are being released. When done with the binder_proc, |
| 1889 | * this function is called to decrement the counter and free the |
| 1890 | * proc if appropriate (proc has been released, all threads have |
| 1891 | * been released and not currenly in-use to process a transaction). |
| 1892 | */ |
| 1893 | static void binder_proc_dec_tmpref(struct binder_proc *proc) |
| 1894 | { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1895 | binder_inner_proc_lock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1896 | proc->tmp_ref--; |
| 1897 | if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) && |
| 1898 | !proc->tmp_ref) { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1899 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1900 | binder_free_proc(proc); |
| 1901 | return; |
| 1902 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1903 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | /** |
| 1907 | * binder_get_txn_from() - safely extract the "from" thread in transaction |
| 1908 | * @t: binder transaction for t->from |
| 1909 | * |
| 1910 | * Atomically return the "from" thread and increment the tmp_ref |
| 1911 | * count for the thread to ensure it stays alive until |
| 1912 | * binder_thread_dec_tmpref() is called. |
| 1913 | * |
| 1914 | * Return: the value of t->from |
| 1915 | */ |
| 1916 | static struct binder_thread *binder_get_txn_from( |
| 1917 | struct binder_transaction *t) |
| 1918 | { |
| 1919 | struct binder_thread *from; |
| 1920 | |
| 1921 | spin_lock(&t->lock); |
| 1922 | from = t->from; |
| 1923 | if (from) |
| 1924 | atomic_inc(&from->tmp_ref); |
| 1925 | spin_unlock(&t->lock); |
| 1926 | return from; |
| 1927 | } |
| 1928 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1929 | /** |
| 1930 | * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock |
| 1931 | * @t: binder transaction for t->from |
| 1932 | * |
| 1933 | * Same as binder_get_txn_from() except it also acquires the proc->inner_lock |
| 1934 | * to guarantee that the thread cannot be released while operating on it. |
| 1935 | * The caller must call binder_inner_proc_unlock() to release the inner lock |
| 1936 | * as well as call binder_dec_thread_txn() to release the reference. |
| 1937 | * |
| 1938 | * Return: the value of t->from |
| 1939 | */ |
| 1940 | static struct binder_thread *binder_get_txn_from_and_acq_inner( |
| 1941 | struct binder_transaction *t) |
| 1942 | { |
| 1943 | struct binder_thread *from; |
| 1944 | |
| 1945 | from = binder_get_txn_from(t); |
| 1946 | if (!from) |
| 1947 | return NULL; |
| 1948 | binder_inner_proc_lock(from->proc); |
| 1949 | if (t->from) { |
| 1950 | BUG_ON(from != t->from); |
| 1951 | return from; |
| 1952 | } |
| 1953 | binder_inner_proc_unlock(from->proc); |
| 1954 | binder_thread_dec_tmpref(from); |
| 1955 | return NULL; |
| 1956 | } |
| 1957 | |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1958 | static void binder_free_transaction(struct binder_transaction *t) |
| 1959 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1960 | if (t->buffer) |
| 1961 | t->buffer->transaction = NULL; |
| 1962 | kfree(t); |
| 1963 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 1964 | } |
| 1965 | |
| 1966 | static void binder_send_failed_reply(struct binder_transaction *t, |
| 1967 | uint32_t error_code) |
| 1968 | { |
| 1969 | struct binder_thread *target_thread; |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 1970 | struct binder_transaction *next; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1971 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1972 | BUG_ON(t->flags & TF_ONE_WAY); |
| 1973 | while (1) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1974 | target_thread = binder_get_txn_from_and_acq_inner(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1975 | if (target_thread) { |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1976 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 1977 | "send failed reply for transaction %d to %d:%d\n", |
| 1978 | t->debug_id, |
| 1979 | target_thread->proc->pid, |
| 1980 | target_thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1981 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1982 | binder_pop_transaction_ilocked(target_thread, t); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1983 | if (target_thread->reply_error.cmd == BR_OK) { |
| 1984 | target_thread->reply_error.cmd = error_code; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 1985 | binder_enqueue_thread_work_ilocked( |
| 1986 | target_thread, |
| 1987 | &target_thread->reply_error.work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1988 | wake_up_interruptible(&target_thread->wait); |
| 1989 | } else { |
Todd Kjos | e46a3b3 | 2018-02-07 12:38:47 -0800 | [diff] [blame] | 1990 | /* |
| 1991 | * Cannot get here for normal operation, but |
| 1992 | * we can if multiple synchronous transactions |
| 1993 | * are sent without blocking for responses. |
| 1994 | * Just ignore the 2nd error in this case. |
| 1995 | */ |
| 1996 | pr_warn("Unexpected reply error: %u\n", |
| 1997 | target_thread->reply_error.cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1998 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1999 | binder_inner_proc_unlock(target_thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2000 | binder_thread_dec_tmpref(target_thread); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 2001 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2002 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2003 | } |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2004 | next = t->from_parent; |
| 2005 | |
| 2006 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2007 | "send failed reply for transaction %d, target dead\n", |
| 2008 | t->debug_id); |
| 2009 | |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 2010 | binder_free_transaction(t); |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2011 | if (next == NULL) { |
| 2012 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2013 | "reply failed, no target thread at root\n"); |
| 2014 | return; |
| 2015 | } |
| 2016 | t = next; |
| 2017 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2018 | "reply failed, no target thread -- retry %d\n", |
| 2019 | t->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2020 | } |
| 2021 | } |
| 2022 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2023 | /** |
Martijn Coenen | fb2c445 | 2017-11-13 10:06:08 +0100 | [diff] [blame] | 2024 | * binder_cleanup_transaction() - cleans up undelivered transaction |
| 2025 | * @t: transaction that needs to be cleaned up |
| 2026 | * @reason: reason the transaction wasn't delivered |
| 2027 | * @error_code: error to return to caller (if synchronous call) |
| 2028 | */ |
| 2029 | static void binder_cleanup_transaction(struct binder_transaction *t, |
| 2030 | const char *reason, |
| 2031 | uint32_t error_code) |
| 2032 | { |
| 2033 | if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) { |
| 2034 | binder_send_failed_reply(t, error_code); |
| 2035 | } else { |
| 2036 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 2037 | "undelivered transaction %d, %s\n", |
| 2038 | t->debug_id, reason); |
| 2039 | binder_free_transaction(t); |
| 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | /** |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2044 | * binder_validate_object() - checks for a valid metadata object in a buffer. |
| 2045 | * @buffer: binder_buffer that we're parsing. |
| 2046 | * @offset: offset in the buffer at which to validate an object. |
| 2047 | * |
| 2048 | * Return: If there's a valid metadata object at @offset in @buffer, the |
| 2049 | * size of that object. Otherwise, it returns zero. |
| 2050 | */ |
| 2051 | static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset) |
| 2052 | { |
| 2053 | /* Check if we can read a header first */ |
| 2054 | struct binder_object_header *hdr; |
| 2055 | size_t object_size = 0; |
| 2056 | |
Dan Carpenter | 361f2dd | 2018-03-29 12:14:40 +0300 | [diff] [blame] | 2057 | if (buffer->data_size < sizeof(*hdr) || |
| 2058 | offset > buffer->data_size - sizeof(*hdr) || |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2059 | !IS_ALIGNED(offset, sizeof(u32))) |
| 2060 | return 0; |
| 2061 | |
| 2062 | /* Ok, now see if we can read a complete object. */ |
| 2063 | hdr = (struct binder_object_header *)(buffer->data + offset); |
| 2064 | switch (hdr->type) { |
| 2065 | case BINDER_TYPE_BINDER: |
| 2066 | case BINDER_TYPE_WEAK_BINDER: |
| 2067 | case BINDER_TYPE_HANDLE: |
| 2068 | case BINDER_TYPE_WEAK_HANDLE: |
| 2069 | object_size = sizeof(struct flat_binder_object); |
| 2070 | break; |
| 2071 | case BINDER_TYPE_FD: |
| 2072 | object_size = sizeof(struct binder_fd_object); |
| 2073 | break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2074 | case BINDER_TYPE_PTR: |
| 2075 | object_size = sizeof(struct binder_buffer_object); |
| 2076 | break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2077 | case BINDER_TYPE_FDA: |
| 2078 | object_size = sizeof(struct binder_fd_array_object); |
| 2079 | break; |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2080 | default: |
| 2081 | return 0; |
| 2082 | } |
| 2083 | if (offset <= buffer->data_size - object_size && |
| 2084 | buffer->data_size >= object_size) |
| 2085 | return object_size; |
| 2086 | else |
| 2087 | return 0; |
| 2088 | } |
| 2089 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2090 | /** |
| 2091 | * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer. |
| 2092 | * @b: binder_buffer containing the object |
| 2093 | * @index: index in offset array at which the binder_buffer_object is |
| 2094 | * located |
| 2095 | * @start: points to the start of the offset array |
| 2096 | * @num_valid: the number of valid offsets in the offset array |
| 2097 | * |
| 2098 | * Return: If @index is within the valid range of the offset array |
| 2099 | * described by @start and @num_valid, and if there's a valid |
| 2100 | * binder_buffer_object at the offset found in index @index |
| 2101 | * of the offset array, that object is returned. Otherwise, |
| 2102 | * %NULL is returned. |
| 2103 | * Note that the offset found in index @index itself is not |
| 2104 | * verified; this function assumes that @num_valid elements |
| 2105 | * from @start were previously verified to have valid offsets. |
| 2106 | */ |
| 2107 | static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b, |
| 2108 | binder_size_t index, |
| 2109 | binder_size_t *start, |
| 2110 | binder_size_t num_valid) |
| 2111 | { |
| 2112 | struct binder_buffer_object *buffer_obj; |
| 2113 | binder_size_t *offp; |
| 2114 | |
| 2115 | if (index >= num_valid) |
| 2116 | return NULL; |
| 2117 | |
| 2118 | offp = start + index; |
| 2119 | buffer_obj = (struct binder_buffer_object *)(b->data + *offp); |
| 2120 | if (buffer_obj->hdr.type != BINDER_TYPE_PTR) |
| 2121 | return NULL; |
| 2122 | |
| 2123 | return buffer_obj; |
| 2124 | } |
| 2125 | |
| 2126 | /** |
| 2127 | * binder_validate_fixup() - validates pointer/fd fixups happen in order. |
| 2128 | * @b: transaction buffer |
| 2129 | * @objects_start start of objects buffer |
| 2130 | * @buffer: binder_buffer_object in which to fix up |
| 2131 | * @offset: start offset in @buffer to fix up |
| 2132 | * @last_obj: last binder_buffer_object that we fixed up in |
| 2133 | * @last_min_offset: minimum fixup offset in @last_obj |
| 2134 | * |
| 2135 | * Return: %true if a fixup in buffer @buffer at offset @offset is |
| 2136 | * allowed. |
| 2137 | * |
| 2138 | * For safety reasons, we only allow fixups inside a buffer to happen |
| 2139 | * at increasing offsets; additionally, we only allow fixup on the last |
| 2140 | * buffer object that was verified, or one of its parents. |
| 2141 | * |
| 2142 | * Example of what is allowed: |
| 2143 | * |
| 2144 | * A |
| 2145 | * B (parent = A, offset = 0) |
| 2146 | * C (parent = A, offset = 16) |
| 2147 | * D (parent = C, offset = 0) |
| 2148 | * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset) |
| 2149 | * |
| 2150 | * Examples of what is not allowed: |
| 2151 | * |
| 2152 | * Decreasing offsets within the same parent: |
| 2153 | * A |
| 2154 | * C (parent = A, offset = 16) |
| 2155 | * B (parent = A, offset = 0) // decreasing offset within A |
| 2156 | * |
| 2157 | * Referring to a parent that wasn't the last object or any of its parents: |
| 2158 | * A |
| 2159 | * B (parent = A, offset = 0) |
| 2160 | * C (parent = A, offset = 0) |
| 2161 | * C (parent = A, offset = 16) |
| 2162 | * D (parent = B, offset = 0) // B is not A or any of A's parents |
| 2163 | */ |
| 2164 | static bool binder_validate_fixup(struct binder_buffer *b, |
| 2165 | binder_size_t *objects_start, |
| 2166 | struct binder_buffer_object *buffer, |
| 2167 | binder_size_t fixup_offset, |
| 2168 | struct binder_buffer_object *last_obj, |
| 2169 | binder_size_t last_min_offset) |
| 2170 | { |
| 2171 | if (!last_obj) { |
| 2172 | /* Nothing to fix up in */ |
| 2173 | return false; |
| 2174 | } |
| 2175 | |
| 2176 | while (last_obj != buffer) { |
| 2177 | /* |
| 2178 | * Safe to retrieve the parent of last_obj, since it |
| 2179 | * was already previously verified by the driver. |
| 2180 | */ |
| 2181 | if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0) |
| 2182 | return false; |
| 2183 | last_min_offset = last_obj->parent_offset + sizeof(uintptr_t); |
| 2184 | last_obj = (struct binder_buffer_object *) |
| 2185 | (b->data + *(objects_start + last_obj->parent)); |
| 2186 | } |
| 2187 | return (fixup_offset >= last_min_offset); |
| 2188 | } |
| 2189 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2190 | static void binder_transaction_buffer_release(struct binder_proc *proc, |
| 2191 | struct binder_buffer *buffer, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2192 | binder_size_t *failed_at) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2193 | { |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2194 | binder_size_t *offp, *off_start, *off_end; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2195 | int debug_id = buffer->debug_id; |
| 2196 | |
| 2197 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | 8ca86f1 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 2198 | "%d buffer release %d, size %zd-%zd, failed at %pK\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2199 | proc->pid, buffer->debug_id, |
| 2200 | buffer->data_size, buffer->offsets_size, failed_at); |
| 2201 | |
| 2202 | if (buffer->target_node) |
| 2203 | binder_dec_node(buffer->target_node, 1, 0); |
| 2204 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2205 | off_start = (binder_size_t *)(buffer->data + |
| 2206 | ALIGN(buffer->data_size, sizeof(void *))); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2207 | if (failed_at) |
| 2208 | off_end = failed_at; |
| 2209 | else |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2210 | off_end = (void *)off_start + buffer->offsets_size; |
| 2211 | for (offp = off_start; offp < off_end; offp++) { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2212 | struct binder_object_header *hdr; |
| 2213 | size_t object_size = binder_validate_object(buffer, *offp); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2214 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2215 | if (object_size == 0) { |
| 2216 | 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] | 2217 | debug_id, (u64)*offp, buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2218 | continue; |
| 2219 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2220 | hdr = (struct binder_object_header *)(buffer->data + *offp); |
| 2221 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2222 | case BINDER_TYPE_BINDER: |
| 2223 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2224 | struct flat_binder_object *fp; |
| 2225 | struct binder_node *node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2226 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2227 | fp = to_flat_binder_object(hdr); |
| 2228 | node = binder_get_node(proc, fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2229 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2230 | pr_err("transaction release %d bad node %016llx\n", |
| 2231 | debug_id, (u64)fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2232 | break; |
| 2233 | } |
| 2234 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2235 | " node %d u%016llx\n", |
| 2236 | node->debug_id, (u64)node->ptr); |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2237 | binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER, |
| 2238 | 0); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2239 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2240 | } break; |
| 2241 | case BINDER_TYPE_HANDLE: |
| 2242 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2243 | struct flat_binder_object *fp; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2244 | struct binder_ref_data rdata; |
| 2245 | int ret; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 2246 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2247 | fp = to_flat_binder_object(hdr); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2248 | ret = binder_dec_ref_for_handle(proc, fp->handle, |
| 2249 | hdr->type == BINDER_TYPE_HANDLE, &rdata); |
| 2250 | |
| 2251 | if (ret) { |
| 2252 | pr_err("transaction release %d bad handle %d, ret = %d\n", |
| 2253 | debug_id, fp->handle, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2254 | break; |
| 2255 | } |
| 2256 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2257 | " ref %d desc %d\n", |
| 2258 | rdata.debug_id, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2259 | } break; |
| 2260 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2261 | case BINDER_TYPE_FD: { |
| 2262 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
| 2263 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2264 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2265 | " fd %d\n", fp->fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2266 | if (failed_at) |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2267 | task_close_fd(proc, fp->fd); |
| 2268 | } break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2269 | case BINDER_TYPE_PTR: |
| 2270 | /* |
| 2271 | * Nothing to do here, this will get cleaned up when the |
| 2272 | * transaction buffer gets freed |
| 2273 | */ |
| 2274 | break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2275 | case BINDER_TYPE_FDA: { |
| 2276 | struct binder_fd_array_object *fda; |
| 2277 | struct binder_buffer_object *parent; |
| 2278 | uintptr_t parent_buffer; |
| 2279 | u32 *fd_array; |
| 2280 | size_t fd_index; |
| 2281 | binder_size_t fd_buf_size; |
| 2282 | |
| 2283 | fda = to_binder_fd_array_object(hdr); |
| 2284 | parent = binder_validate_ptr(buffer, fda->parent, |
| 2285 | off_start, |
| 2286 | offp - off_start); |
| 2287 | if (!parent) { |
Arvind Yadav | f7f84fd | 2017-09-25 12:52:11 +0530 | [diff] [blame] | 2288 | pr_err("transaction release %d bad parent offset\n", |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2289 | debug_id); |
| 2290 | continue; |
| 2291 | } |
| 2292 | /* |
| 2293 | * Since the parent was already fixed up, convert it |
| 2294 | * back to kernel address space to access it |
| 2295 | */ |
| 2296 | parent_buffer = parent->buffer - |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2297 | binder_alloc_get_user_buffer_offset( |
| 2298 | &proc->alloc); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2299 | |
| 2300 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2301 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2302 | pr_err("transaction release %d invalid number of fds (%lld)\n", |
| 2303 | debug_id, (u64)fda->num_fds); |
| 2304 | continue; |
| 2305 | } |
| 2306 | if (fd_buf_size > parent->length || |
| 2307 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2308 | /* No space for all file descriptors here. */ |
| 2309 | pr_err("transaction release %d not enough space for %lld fds in buffer\n", |
| 2310 | debug_id, (u64)fda->num_fds); |
| 2311 | continue; |
| 2312 | } |
Arnd Bergmann | 1c363ea | 2017-09-05 10:56:13 +0200 | [diff] [blame] | 2313 | fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2314 | for (fd_index = 0; fd_index < fda->num_fds; fd_index++) |
| 2315 | task_close_fd(proc, fd_array[fd_index]); |
| 2316 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2317 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 2318 | pr_err("transaction release %d bad object type %x\n", |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2319 | debug_id, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2320 | break; |
| 2321 | } |
| 2322 | } |
| 2323 | } |
| 2324 | |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2325 | static int binder_translate_binder(struct flat_binder_object *fp, |
| 2326 | struct binder_transaction *t, |
| 2327 | struct binder_thread *thread) |
| 2328 | { |
| 2329 | struct binder_node *node; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2330 | struct binder_proc *proc = thread->proc; |
| 2331 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2332 | struct binder_ref_data rdata; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2333 | int ret = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2334 | |
| 2335 | node = binder_get_node(proc, fp->binder); |
| 2336 | if (!node) { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2337 | node = binder_new_node(proc, fp); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2338 | if (!node) |
| 2339 | return -ENOMEM; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2340 | } |
| 2341 | if (fp->cookie != node->cookie) { |
| 2342 | binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n", |
| 2343 | proc->pid, thread->pid, (u64)fp->binder, |
| 2344 | node->debug_id, (u64)fp->cookie, |
| 2345 | (u64)node->cookie); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2346 | ret = -EINVAL; |
| 2347 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2348 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2349 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2350 | ret = -EPERM; |
| 2351 | goto done; |
| 2352 | } |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2353 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2354 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2355 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2356 | &thread->todo, &rdata); |
| 2357 | if (ret) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2358 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2359 | |
| 2360 | if (fp->hdr.type == BINDER_TYPE_BINDER) |
| 2361 | fp->hdr.type = BINDER_TYPE_HANDLE; |
| 2362 | else |
| 2363 | fp->hdr.type = BINDER_TYPE_WEAK_HANDLE; |
| 2364 | fp->binder = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2365 | fp->handle = rdata.desc; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2366 | fp->cookie = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2367 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2368 | trace_binder_transaction_node_to_ref(t, node, &rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2369 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2370 | " node %d u%016llx -> ref %d desc %d\n", |
| 2371 | node->debug_id, (u64)node->ptr, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2372 | rdata.debug_id, rdata.desc); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2373 | done: |
| 2374 | binder_put_node(node); |
| 2375 | return ret; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | static int binder_translate_handle(struct flat_binder_object *fp, |
| 2379 | struct binder_transaction *t, |
| 2380 | struct binder_thread *thread) |
| 2381 | { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2382 | struct binder_proc *proc = thread->proc; |
| 2383 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2384 | struct binder_node *node; |
| 2385 | struct binder_ref_data src_rdata; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2386 | int ret = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2387 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2388 | node = binder_get_node_from_ref(proc, fp->handle, |
| 2389 | fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata); |
| 2390 | if (!node) { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2391 | binder_user_error("%d:%d got transaction with invalid handle, %d\n", |
| 2392 | proc->pid, thread->pid, fp->handle); |
| 2393 | return -EINVAL; |
| 2394 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2395 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2396 | ret = -EPERM; |
| 2397 | goto done; |
| 2398 | } |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2399 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2400 | binder_node_lock(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2401 | if (node->proc == target_proc) { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2402 | if (fp->hdr.type == BINDER_TYPE_HANDLE) |
| 2403 | fp->hdr.type = BINDER_TYPE_BINDER; |
| 2404 | else |
| 2405 | fp->hdr.type = BINDER_TYPE_WEAK_BINDER; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2406 | fp->binder = node->ptr; |
| 2407 | fp->cookie = node->cookie; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2408 | if (node->proc) |
| 2409 | binder_inner_proc_lock(node->proc); |
| 2410 | binder_inc_node_nilocked(node, |
| 2411 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2412 | 0, NULL); |
| 2413 | if (node->proc) |
| 2414 | binder_inner_proc_unlock(node->proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2415 | trace_binder_transaction_ref_to_node(t, node, &src_rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2416 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2417 | " ref %d desc %d -> node %d u%016llx\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2418 | src_rdata.debug_id, src_rdata.desc, node->debug_id, |
| 2419 | (u64)node->ptr); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2420 | binder_node_unlock(node); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2421 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2422 | struct binder_ref_data dest_rdata; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2423 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2424 | binder_node_unlock(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2425 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2426 | fp->hdr.type == BINDER_TYPE_HANDLE, |
| 2427 | NULL, &dest_rdata); |
| 2428 | if (ret) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2429 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2430 | |
| 2431 | fp->binder = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2432 | fp->handle = dest_rdata.desc; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2433 | fp->cookie = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2434 | trace_binder_transaction_ref_to_ref(t, node, &src_rdata, |
| 2435 | &dest_rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2436 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2437 | " ref %d desc %d -> ref %d desc %d (node %d)\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2438 | src_rdata.debug_id, src_rdata.desc, |
| 2439 | dest_rdata.debug_id, dest_rdata.desc, |
| 2440 | node->debug_id); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2441 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2442 | done: |
| 2443 | binder_put_node(node); |
| 2444 | return ret; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | static int binder_translate_fd(int fd, |
| 2448 | struct binder_transaction *t, |
| 2449 | struct binder_thread *thread, |
| 2450 | struct binder_transaction *in_reply_to) |
| 2451 | { |
| 2452 | struct binder_proc *proc = thread->proc; |
| 2453 | struct binder_proc *target_proc = t->to_proc; |
| 2454 | int target_fd; |
| 2455 | struct file *file; |
| 2456 | int ret; |
| 2457 | bool target_allows_fd; |
| 2458 | |
| 2459 | if (in_reply_to) |
| 2460 | target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS); |
| 2461 | else |
| 2462 | target_allows_fd = t->buffer->target_node->accept_fds; |
| 2463 | if (!target_allows_fd) { |
| 2464 | binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n", |
| 2465 | proc->pid, thread->pid, |
| 2466 | in_reply_to ? "reply" : "transaction", |
| 2467 | fd); |
| 2468 | ret = -EPERM; |
| 2469 | goto err_fd_not_accepted; |
| 2470 | } |
| 2471 | |
| 2472 | file = fget(fd); |
| 2473 | if (!file) { |
| 2474 | binder_user_error("%d:%d got transaction with invalid fd, %d\n", |
| 2475 | proc->pid, thread->pid, fd); |
| 2476 | ret = -EBADF; |
| 2477 | goto err_fget; |
| 2478 | } |
| 2479 | ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file); |
| 2480 | if (ret < 0) { |
| 2481 | ret = -EPERM; |
| 2482 | goto err_security; |
| 2483 | } |
| 2484 | |
| 2485 | target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC); |
| 2486 | if (target_fd < 0) { |
| 2487 | ret = -ENOMEM; |
| 2488 | goto err_get_unused_fd; |
| 2489 | } |
| 2490 | task_fd_install(target_proc, target_fd, file); |
| 2491 | trace_binder_transaction_fd(t, fd, target_fd); |
| 2492 | binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n", |
| 2493 | fd, target_fd); |
| 2494 | |
| 2495 | return target_fd; |
| 2496 | |
| 2497 | err_get_unused_fd: |
| 2498 | err_security: |
| 2499 | fput(file); |
| 2500 | err_fget: |
| 2501 | err_fd_not_accepted: |
| 2502 | return ret; |
| 2503 | } |
| 2504 | |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2505 | static int binder_translate_fd_array(struct binder_fd_array_object *fda, |
| 2506 | struct binder_buffer_object *parent, |
| 2507 | struct binder_transaction *t, |
| 2508 | struct binder_thread *thread, |
| 2509 | struct binder_transaction *in_reply_to) |
| 2510 | { |
| 2511 | binder_size_t fdi, fd_buf_size, num_installed_fds; |
| 2512 | int target_fd; |
| 2513 | uintptr_t parent_buffer; |
| 2514 | u32 *fd_array; |
| 2515 | struct binder_proc *proc = thread->proc; |
| 2516 | struct binder_proc *target_proc = t->to_proc; |
| 2517 | |
| 2518 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2519 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2520 | binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n", |
| 2521 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2522 | return -EINVAL; |
| 2523 | } |
| 2524 | if (fd_buf_size > parent->length || |
| 2525 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2526 | /* No space for all file descriptors here. */ |
| 2527 | binder_user_error("%d:%d not enough space to store %lld fds in buffer\n", |
| 2528 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2529 | return -EINVAL; |
| 2530 | } |
| 2531 | /* |
| 2532 | * Since the parent was already fixed up, convert it |
| 2533 | * back to the kernel address space to access it |
| 2534 | */ |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2535 | parent_buffer = parent->buffer - |
| 2536 | binder_alloc_get_user_buffer_offset(&target_proc->alloc); |
Arnd Bergmann | 1c363ea | 2017-09-05 10:56:13 +0200 | [diff] [blame] | 2537 | fd_array = (u32 *)(parent_buffer + (uintptr_t)fda->parent_offset); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2538 | if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) { |
| 2539 | binder_user_error("%d:%d parent offset not aligned correctly.\n", |
| 2540 | proc->pid, thread->pid); |
| 2541 | return -EINVAL; |
| 2542 | } |
| 2543 | for (fdi = 0; fdi < fda->num_fds; fdi++) { |
| 2544 | target_fd = binder_translate_fd(fd_array[fdi], t, thread, |
| 2545 | in_reply_to); |
| 2546 | if (target_fd < 0) |
| 2547 | goto err_translate_fd_failed; |
| 2548 | fd_array[fdi] = target_fd; |
| 2549 | } |
| 2550 | return 0; |
| 2551 | |
| 2552 | err_translate_fd_failed: |
| 2553 | /* |
| 2554 | * Failed to allocate fd or security error, free fds |
| 2555 | * installed so far. |
| 2556 | */ |
| 2557 | num_installed_fds = fdi; |
| 2558 | for (fdi = 0; fdi < num_installed_fds; fdi++) |
| 2559 | task_close_fd(target_proc, fd_array[fdi]); |
| 2560 | return target_fd; |
| 2561 | } |
| 2562 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2563 | static int binder_fixup_parent(struct binder_transaction *t, |
| 2564 | struct binder_thread *thread, |
| 2565 | struct binder_buffer_object *bp, |
| 2566 | binder_size_t *off_start, |
| 2567 | binder_size_t num_valid, |
| 2568 | struct binder_buffer_object *last_fixup_obj, |
| 2569 | binder_size_t last_fixup_min_off) |
| 2570 | { |
| 2571 | struct binder_buffer_object *parent; |
| 2572 | u8 *parent_buffer; |
| 2573 | struct binder_buffer *b = t->buffer; |
| 2574 | struct binder_proc *proc = thread->proc; |
| 2575 | struct binder_proc *target_proc = t->to_proc; |
| 2576 | |
| 2577 | if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT)) |
| 2578 | return 0; |
| 2579 | |
| 2580 | parent = binder_validate_ptr(b, bp->parent, off_start, num_valid); |
| 2581 | if (!parent) { |
| 2582 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 2583 | proc->pid, thread->pid); |
| 2584 | return -EINVAL; |
| 2585 | } |
| 2586 | |
| 2587 | if (!binder_validate_fixup(b, off_start, |
| 2588 | parent, bp->parent_offset, |
| 2589 | last_fixup_obj, |
| 2590 | last_fixup_min_off)) { |
| 2591 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 2592 | proc->pid, thread->pid); |
| 2593 | return -EINVAL; |
| 2594 | } |
| 2595 | |
| 2596 | if (parent->length < sizeof(binder_uintptr_t) || |
| 2597 | bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) { |
| 2598 | /* No space for a pointer here! */ |
| 2599 | binder_user_error("%d:%d got transaction with invalid parent offset\n", |
| 2600 | proc->pid, thread->pid); |
| 2601 | return -EINVAL; |
| 2602 | } |
Arnd Bergmann | 1c363ea | 2017-09-05 10:56:13 +0200 | [diff] [blame] | 2603 | parent_buffer = (u8 *)((uintptr_t)parent->buffer - |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2604 | binder_alloc_get_user_buffer_offset( |
| 2605 | &target_proc->alloc)); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2606 | *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer; |
| 2607 | |
| 2608 | return 0; |
| 2609 | } |
| 2610 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2611 | /** |
| 2612 | * binder_proc_transaction() - sends a transaction to a process and wakes it up |
| 2613 | * @t: transaction to send |
| 2614 | * @proc: process to send the transaction to |
| 2615 | * @thread: thread in @proc to send the transaction to (may be NULL) |
| 2616 | * |
| 2617 | * This function queues a transaction to the specified process. It will try |
| 2618 | * to find a thread in the target process to handle the transaction and |
| 2619 | * wake it up. If no thread is found, the work is queued to the proc |
| 2620 | * waitqueue. |
| 2621 | * |
| 2622 | * If the @thread parameter is not NULL, the transaction is always queued |
| 2623 | * to the waitlist of that specific thread. |
| 2624 | * |
| 2625 | * Return: true if the transactions was successfully queued |
| 2626 | * false if the target process or thread is dead |
| 2627 | */ |
| 2628 | static bool binder_proc_transaction(struct binder_transaction *t, |
| 2629 | struct binder_proc *proc, |
| 2630 | struct binder_thread *thread) |
| 2631 | { |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2632 | struct binder_node *node = t->buffer->target_node; |
| 2633 | bool oneway = !!(t->flags & TF_ONE_WAY); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2634 | bool pending_async = false; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2635 | |
| 2636 | BUG_ON(!node); |
| 2637 | binder_node_lock(node); |
| 2638 | if (oneway) { |
| 2639 | BUG_ON(thread); |
| 2640 | if (node->has_async_transaction) { |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2641 | pending_async = true; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2642 | } else { |
Gustavo A. R. Silva | 197410a | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 2643 | node->has_async_transaction = true; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2644 | } |
| 2645 | } |
| 2646 | |
| 2647 | binder_inner_proc_lock(proc); |
| 2648 | |
| 2649 | if (proc->is_dead || (thread && thread->is_dead)) { |
| 2650 | binder_inner_proc_unlock(proc); |
| 2651 | binder_node_unlock(node); |
| 2652 | return false; |
| 2653 | } |
| 2654 | |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2655 | if (!thread && !pending_async) |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2656 | thread = binder_select_thread_ilocked(proc); |
| 2657 | |
| 2658 | if (thread) |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2659 | binder_enqueue_thread_work_ilocked(thread, &t->work); |
| 2660 | else if (!pending_async) |
| 2661 | binder_enqueue_work_ilocked(&t->work, &proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2662 | else |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2663 | binder_enqueue_work_ilocked(&t->work, &node->async_todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2664 | |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 2665 | if (!pending_async) |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2666 | binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */); |
| 2667 | |
| 2668 | binder_inner_proc_unlock(proc); |
| 2669 | binder_node_unlock(node); |
| 2670 | |
| 2671 | return true; |
| 2672 | } |
| 2673 | |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2674 | /** |
| 2675 | * binder_get_node_refs_for_txn() - Get required refs on node for txn |
| 2676 | * @node: struct binder_node for which to get refs |
| 2677 | * @proc: returns @node->proc if valid |
| 2678 | * @error: if no @proc then returns BR_DEAD_REPLY |
| 2679 | * |
| 2680 | * User-space normally keeps the node alive when creating a transaction |
| 2681 | * since it has a reference to the target. The local strong ref keeps it |
| 2682 | * alive if the sending process dies before the target process processes |
| 2683 | * the transaction. If the source process is malicious or has a reference |
| 2684 | * counting bug, relying on the local strong ref can fail. |
| 2685 | * |
| 2686 | * Since user-space can cause the local strong ref to go away, we also take |
| 2687 | * a tmpref on the node to ensure it survives while we are constructing |
| 2688 | * the transaction. We also need a tmpref on the proc while we are |
| 2689 | * constructing the transaction, so we take that here as well. |
| 2690 | * |
| 2691 | * Return: The target_node with refs taken or NULL if no @node->proc is NULL. |
| 2692 | * Also sets @proc if valid. If the @node->proc is NULL indicating that the |
| 2693 | * target proc has died, @error is set to BR_DEAD_REPLY |
| 2694 | */ |
| 2695 | static struct binder_node *binder_get_node_refs_for_txn( |
| 2696 | struct binder_node *node, |
| 2697 | struct binder_proc **procp, |
| 2698 | uint32_t *error) |
| 2699 | { |
| 2700 | struct binder_node *target_node = NULL; |
| 2701 | |
| 2702 | binder_node_inner_lock(node); |
| 2703 | if (node->proc) { |
| 2704 | target_node = node; |
| 2705 | binder_inc_node_nilocked(node, 1, 0, NULL); |
| 2706 | binder_inc_node_tmpref_ilocked(node); |
| 2707 | node->proc->tmp_ref++; |
| 2708 | *procp = node->proc; |
| 2709 | } else |
| 2710 | *error = BR_DEAD_REPLY; |
| 2711 | binder_node_inner_unlock(node); |
| 2712 | |
| 2713 | return target_node; |
| 2714 | } |
| 2715 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2716 | static void binder_transaction(struct binder_proc *proc, |
| 2717 | struct binder_thread *thread, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2718 | struct binder_transaction_data *tr, int reply, |
| 2719 | binder_size_t extra_buffers_size) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2720 | { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2721 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2722 | struct binder_transaction *t; |
| 2723 | struct binder_work *tcomplete; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2724 | binder_size_t *offp, *off_end, *off_start; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 2725 | binder_size_t off_min; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2726 | u8 *sg_bufp, *sg_buf_end; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2727 | struct binder_proc *target_proc = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2728 | struct binder_thread *target_thread = NULL; |
| 2729 | struct binder_node *target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2730 | struct binder_transaction *in_reply_to = NULL; |
| 2731 | struct binder_transaction_log_entry *e; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2732 | uint32_t return_error = 0; |
| 2733 | uint32_t return_error_param = 0; |
| 2734 | uint32_t return_error_line = 0; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2735 | struct binder_buffer_object *last_fixup_obj = NULL; |
| 2736 | binder_size_t last_fixup_min_off = 0; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 2737 | struct binder_context *context = proc->context; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2738 | int t_debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2739 | |
| 2740 | e = binder_transaction_log_add(&binder_transaction_log); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2741 | e->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2742 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); |
| 2743 | e->from_proc = proc->pid; |
| 2744 | e->from_thread = thread->pid; |
| 2745 | e->target_handle = tr->target.handle; |
| 2746 | e->data_size = tr->data_size; |
| 2747 | e->offsets_size = tr->offsets_size; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 2748 | e->context_name = proc->context->name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2749 | |
| 2750 | if (reply) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2751 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2752 | in_reply_to = thread->transaction_stack; |
| 2753 | if (in_reply_to == NULL) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2754 | binder_inner_proc_unlock(proc); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2755 | 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] | 2756 | proc->pid, thread->pid); |
| 2757 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2758 | return_error_param = -EPROTO; |
| 2759 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2760 | goto err_empty_call_stack; |
| 2761 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2762 | if (in_reply_to->to_thread != thread) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2763 | spin_lock(&in_reply_to->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2764 | 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] | 2765 | proc->pid, thread->pid, in_reply_to->debug_id, |
| 2766 | in_reply_to->to_proc ? |
| 2767 | in_reply_to->to_proc->pid : 0, |
| 2768 | in_reply_to->to_thread ? |
| 2769 | in_reply_to->to_thread->pid : 0); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2770 | spin_unlock(&in_reply_to->lock); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2771 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2772 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2773 | return_error_param = -EPROTO; |
| 2774 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2775 | in_reply_to = NULL; |
| 2776 | goto err_bad_call_stack; |
| 2777 | } |
| 2778 | thread->transaction_stack = in_reply_to->to_parent; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2779 | binder_inner_proc_unlock(proc); |
| 2780 | binder_set_nice(in_reply_to->saved_priority); |
| 2781 | 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] | 2782 | if (target_thread == NULL) { |
| 2783 | return_error = BR_DEAD_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2784 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2785 | goto err_dead_binder; |
| 2786 | } |
| 2787 | if (target_thread->transaction_stack != in_reply_to) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2788 | 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] | 2789 | proc->pid, thread->pid, |
| 2790 | target_thread->transaction_stack ? |
| 2791 | target_thread->transaction_stack->debug_id : 0, |
| 2792 | in_reply_to->debug_id); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2793 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2794 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2795 | return_error_param = -EPROTO; |
| 2796 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2797 | in_reply_to = NULL; |
| 2798 | target_thread = NULL; |
| 2799 | goto err_dead_binder; |
| 2800 | } |
| 2801 | target_proc = target_thread->proc; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2802 | target_proc->tmp_ref++; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2803 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2804 | } else { |
| 2805 | if (tr->target.handle) { |
| 2806 | struct binder_ref *ref; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2807 | |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2808 | /* |
| 2809 | * There must already be a strong ref |
| 2810 | * on this node. If so, do a strong |
| 2811 | * increment on the node to ensure it |
| 2812 | * stays alive until the transaction is |
| 2813 | * done. |
| 2814 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 2815 | binder_proc_lock(proc); |
| 2816 | ref = binder_get_ref_olocked(proc, tr->target.handle, |
| 2817 | true); |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2818 | if (ref) { |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2819 | target_node = binder_get_node_refs_for_txn( |
| 2820 | ref->node, &target_proc, |
| 2821 | &return_error); |
| 2822 | } else { |
| 2823 | binder_user_error("%d:%d got transaction to invalid handle\n", |
| 2824 | proc->pid, thread->pid); |
| 2825 | return_error = BR_FAILED_REPLY; |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2826 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 2827 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2828 | } else { |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 2829 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 2830 | target_node = context->binder_context_mgr_node; |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2831 | if (target_node) |
| 2832 | target_node = binder_get_node_refs_for_txn( |
| 2833 | target_node, &target_proc, |
| 2834 | &return_error); |
| 2835 | else |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2836 | return_error = BR_DEAD_REPLY; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 2837 | mutex_unlock(&context->context_mgr_node_lock); |
Martijn Coenen | 7aa135f | 2018-03-28 11:14:50 +0200 | [diff] [blame] | 2838 | if (target_node && target_proc == proc) { |
| 2839 | binder_user_error("%d:%d got transaction to context manager from process owning it\n", |
| 2840 | proc->pid, thread->pid); |
| 2841 | return_error = BR_FAILED_REPLY; |
| 2842 | return_error_param = -EINVAL; |
| 2843 | return_error_line = __LINE__; |
| 2844 | goto err_invalid_target_handle; |
| 2845 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2846 | } |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2847 | if (!target_node) { |
| 2848 | /* |
| 2849 | * return_error is set above |
| 2850 | */ |
| 2851 | return_error_param = -EINVAL; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2852 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2853 | goto err_dead_binder; |
| 2854 | } |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 2855 | e->to_node = target_node->debug_id; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2856 | if (security_binder_transaction(proc->tsk, |
| 2857 | target_proc->tsk) < 0) { |
| 2858 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2859 | return_error_param = -EPERM; |
| 2860 | return_error_line = __LINE__; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2861 | goto err_invalid_target_handle; |
| 2862 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2863 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2864 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { |
| 2865 | struct binder_transaction *tmp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2866 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2867 | tmp = thread->transaction_stack; |
| 2868 | if (tmp->to_thread != thread) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2869 | spin_lock(&tmp->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2870 | 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] | 2871 | proc->pid, thread->pid, tmp->debug_id, |
| 2872 | tmp->to_proc ? tmp->to_proc->pid : 0, |
| 2873 | tmp->to_thread ? |
| 2874 | tmp->to_thread->pid : 0); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2875 | spin_unlock(&tmp->lock); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2876 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2877 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2878 | return_error_param = -EPROTO; |
| 2879 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2880 | goto err_bad_call_stack; |
| 2881 | } |
| 2882 | while (tmp) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2883 | struct binder_thread *from; |
| 2884 | |
| 2885 | spin_lock(&tmp->lock); |
| 2886 | from = tmp->from; |
| 2887 | if (from && from->proc == target_proc) { |
| 2888 | atomic_inc(&from->tmp_ref); |
| 2889 | target_thread = from; |
| 2890 | spin_unlock(&tmp->lock); |
| 2891 | break; |
| 2892 | } |
| 2893 | spin_unlock(&tmp->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2894 | tmp = tmp->from_parent; |
| 2895 | } |
| 2896 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2897 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2898 | } |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2899 | if (target_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2900 | e->to_thread = target_thread->pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2901 | e->to_proc = target_proc->pid; |
| 2902 | |
| 2903 | /* TODO: reuse incoming transaction for reply */ |
| 2904 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 2905 | if (t == NULL) { |
| 2906 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2907 | return_error_param = -ENOMEM; |
| 2908 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2909 | goto err_alloc_t_failed; |
| 2910 | } |
| 2911 | binder_stats_created(BINDER_STAT_TRANSACTION); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2912 | spin_lock_init(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2913 | |
| 2914 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); |
| 2915 | if (tcomplete == NULL) { |
| 2916 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2917 | return_error_param = -ENOMEM; |
| 2918 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2919 | goto err_alloc_tcomplete_failed; |
| 2920 | } |
| 2921 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); |
| 2922 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2923 | t->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2924 | |
| 2925 | if (reply) |
| 2926 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2927 | "%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] | 2928 | proc->pid, thread->pid, t->debug_id, |
| 2929 | target_proc->pid, target_thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2930 | (u64)tr->data.ptr.buffer, |
| 2931 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2932 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 2933 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2934 | else |
| 2935 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2936 | "%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] | 2937 | proc->pid, thread->pid, t->debug_id, |
| 2938 | target_proc->pid, target_node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2939 | (u64)tr->data.ptr.buffer, |
| 2940 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2941 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 2942 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2943 | |
| 2944 | if (!reply && !(tr->flags & TF_ONE_WAY)) |
| 2945 | t->from = thread; |
| 2946 | else |
| 2947 | t->from = NULL; |
Tair Rzayev | 57bab7c | 2014-05-31 22:43:34 +0300 | [diff] [blame] | 2948 | t->sender_euid = task_euid(proc->tsk); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2949 | t->to_proc = target_proc; |
| 2950 | t->to_thread = target_thread; |
| 2951 | t->code = tr->code; |
| 2952 | t->flags = tr->flags; |
| 2953 | t->priority = task_nice(current); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2954 | |
| 2955 | trace_binder_transaction(reply, t, target_node); |
| 2956 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2957 | t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2958 | tr->offsets_size, extra_buffers_size, |
| 2959 | !reply && (t->flags & TF_ONE_WAY)); |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2960 | if (IS_ERR(t->buffer)) { |
| 2961 | /* |
| 2962 | * -ESRCH indicates VMA cleared. The target is dying. |
| 2963 | */ |
| 2964 | return_error_param = PTR_ERR(t->buffer); |
| 2965 | return_error = return_error_param == -ESRCH ? |
| 2966 | BR_DEAD_REPLY : BR_FAILED_REPLY; |
| 2967 | return_error_line = __LINE__; |
| 2968 | t->buffer = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2969 | goto err_binder_alloc_buf_failed; |
| 2970 | } |
| 2971 | t->buffer->allow_user_free = 0; |
| 2972 | t->buffer->debug_id = t->debug_id; |
| 2973 | t->buffer->transaction = t; |
| 2974 | t->buffer->target_node = target_node; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2975 | trace_binder_transaction_alloc_buf(t->buffer); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2976 | off_start = (binder_size_t *)(t->buffer->data + |
| 2977 | ALIGN(tr->data_size, sizeof(void *))); |
| 2978 | offp = off_start; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2979 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2980 | if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t) |
| 2981 | tr->data.ptr.buffer, tr->data_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2982 | binder_user_error("%d:%d got transaction with invalid data ptr\n", |
| 2983 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2984 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2985 | return_error_param = -EFAULT; |
| 2986 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2987 | goto err_copy_data_failed; |
| 2988 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2989 | if (copy_from_user(offp, (const void __user *)(uintptr_t) |
| 2990 | tr->data.ptr.offsets, tr->offsets_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2991 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 2992 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2993 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2994 | return_error_param = -EFAULT; |
| 2995 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2996 | goto err_copy_data_failed; |
| 2997 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2998 | if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) { |
| 2999 | binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n", |
| 3000 | proc->pid, thread->pid, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3001 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3002 | return_error_param = -EINVAL; |
| 3003 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3004 | goto err_bad_offset; |
| 3005 | } |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3006 | if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) { |
| 3007 | binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n", |
| 3008 | proc->pid, thread->pid, |
| 3009 | (u64)extra_buffers_size); |
| 3010 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3011 | return_error_param = -EINVAL; |
| 3012 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3013 | goto err_bad_offset; |
| 3014 | } |
| 3015 | off_end = (void *)off_start + tr->offsets_size; |
| 3016 | sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *))); |
| 3017 | sg_buf_end = sg_bufp + extra_buffers_size; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3018 | off_min = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3019 | for (; offp < off_end; offp++) { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3020 | struct binder_object_header *hdr; |
| 3021 | size_t object_size = binder_validate_object(t->buffer, *offp); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3022 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3023 | if (object_size == 0 || *offp < off_min) { |
| 3024 | 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] | 3025 | proc->pid, thread->pid, (u64)*offp, |
| 3026 | (u64)off_min, |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3027 | (u64)t->buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3028 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3029 | return_error_param = -EINVAL; |
| 3030 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3031 | goto err_bad_offset; |
| 3032 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3033 | |
| 3034 | hdr = (struct binder_object_header *)(t->buffer->data + *offp); |
| 3035 | off_min = *offp + object_size; |
| 3036 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3037 | case BINDER_TYPE_BINDER: |
| 3038 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3039 | struct flat_binder_object *fp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3040 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3041 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3042 | ret = binder_translate_binder(fp, t, thread); |
| 3043 | if (ret < 0) { |
Christian Engelmayer | 7d42043 | 2014-05-07 21:44:53 +0200 | [diff] [blame] | 3044 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3045 | return_error_param = ret; |
| 3046 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3047 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3048 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3049 | } break; |
| 3050 | case BINDER_TYPE_HANDLE: |
| 3051 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3052 | struct flat_binder_object *fp; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 3053 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3054 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3055 | ret = binder_translate_handle(fp, t, thread); |
| 3056 | if (ret < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3057 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3058 | return_error_param = ret; |
| 3059 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3060 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3061 | } |
| 3062 | } break; |
| 3063 | |
| 3064 | case BINDER_TYPE_FD: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3065 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3066 | int target_fd = binder_translate_fd(fp->fd, t, thread, |
| 3067 | in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3068 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3069 | if (target_fd < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3070 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3071 | return_error_param = target_fd; |
| 3072 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3073 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3074 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3075 | fp->pad_binder = 0; |
| 3076 | fp->fd = target_fd; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3077 | } break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3078 | case BINDER_TYPE_FDA: { |
| 3079 | struct binder_fd_array_object *fda = |
| 3080 | to_binder_fd_array_object(hdr); |
| 3081 | struct binder_buffer_object *parent = |
| 3082 | binder_validate_ptr(t->buffer, fda->parent, |
| 3083 | off_start, |
| 3084 | offp - off_start); |
| 3085 | if (!parent) { |
| 3086 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 3087 | proc->pid, thread->pid); |
| 3088 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3089 | return_error_param = -EINVAL; |
| 3090 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3091 | goto err_bad_parent; |
| 3092 | } |
| 3093 | if (!binder_validate_fixup(t->buffer, off_start, |
| 3094 | parent, fda->parent_offset, |
| 3095 | last_fixup_obj, |
| 3096 | last_fixup_min_off)) { |
| 3097 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 3098 | proc->pid, thread->pid); |
| 3099 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3100 | return_error_param = -EINVAL; |
| 3101 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3102 | goto err_bad_parent; |
| 3103 | } |
| 3104 | ret = binder_translate_fd_array(fda, parent, t, thread, |
| 3105 | in_reply_to); |
| 3106 | if (ret < 0) { |
| 3107 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3108 | return_error_param = ret; |
| 3109 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3110 | goto err_translate_failed; |
| 3111 | } |
| 3112 | last_fixup_obj = parent; |
| 3113 | last_fixup_min_off = |
| 3114 | fda->parent_offset + sizeof(u32) * fda->num_fds; |
| 3115 | } break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3116 | case BINDER_TYPE_PTR: { |
| 3117 | struct binder_buffer_object *bp = |
| 3118 | to_binder_buffer_object(hdr); |
| 3119 | size_t buf_left = sg_buf_end - sg_bufp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3120 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3121 | if (bp->length > buf_left) { |
| 3122 | binder_user_error("%d:%d got transaction with too large buffer\n", |
| 3123 | proc->pid, thread->pid); |
| 3124 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3125 | return_error_param = -EINVAL; |
| 3126 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3127 | goto err_bad_offset; |
| 3128 | } |
| 3129 | if (copy_from_user(sg_bufp, |
| 3130 | (const void __user *)(uintptr_t) |
| 3131 | bp->buffer, bp->length)) { |
| 3132 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3133 | proc->pid, thread->pid); |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3134 | return_error_param = -EFAULT; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3135 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3136 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3137 | goto err_copy_data_failed; |
| 3138 | } |
| 3139 | /* Fixup buffer pointer to target proc address space */ |
| 3140 | bp->buffer = (uintptr_t)sg_bufp + |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 3141 | binder_alloc_get_user_buffer_offset( |
| 3142 | &target_proc->alloc); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3143 | sg_bufp += ALIGN(bp->length, sizeof(u64)); |
| 3144 | |
| 3145 | ret = binder_fixup_parent(t, thread, bp, off_start, |
| 3146 | offp - off_start, |
| 3147 | last_fixup_obj, |
| 3148 | last_fixup_min_off); |
| 3149 | if (ret < 0) { |
| 3150 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3151 | return_error_param = ret; |
| 3152 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3153 | goto err_translate_failed; |
| 3154 | } |
| 3155 | last_fixup_obj = bp; |
| 3156 | last_fixup_min_off = 0; |
| 3157 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3158 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 3159 | 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] | 3160 | proc->pid, thread->pid, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3161 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3162 | return_error_param = -EINVAL; |
| 3163 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3164 | goto err_bad_object_type; |
| 3165 | } |
| 3166 | } |
Todd Kjos | ccae6f6 | 2017-06-29 12:01:48 -0700 | [diff] [blame] | 3167 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3168 | t->work.type = BINDER_WORK_TRANSACTION; |
Todd Kjos | ccae6f6 | 2017-06-29 12:01:48 -0700 | [diff] [blame] | 3169 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3170 | if (reply) { |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3171 | binder_enqueue_thread_work(thread, tcomplete); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3172 | binder_inner_proc_lock(target_proc); |
| 3173 | if (target_thread->is_dead) { |
| 3174 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3175 | goto err_dead_proc_or_thread; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3176 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3177 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3178 | binder_pop_transaction_ilocked(target_thread, in_reply_to); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3179 | binder_enqueue_thread_work_ilocked(target_thread, &t->work); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3180 | binder_inner_proc_unlock(target_proc); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3181 | wake_up_interruptible_sync(&target_thread->wait); |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 3182 | binder_free_transaction(in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3183 | } else if (!(t->flags & TF_ONE_WAY)) { |
| 3184 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3185 | binder_inner_proc_lock(proc); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3186 | /* |
| 3187 | * Defer the TRANSACTION_COMPLETE, so we don't return to |
| 3188 | * userspace immediately; this allows the target process to |
| 3189 | * immediately start processing this transaction, reducing |
| 3190 | * latency. We will then return the TRANSACTION_COMPLETE when |
| 3191 | * the target replies (or there is an error). |
| 3192 | */ |
| 3193 | binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3194 | t->need_reply = 1; |
| 3195 | t->from_parent = thread->transaction_stack; |
| 3196 | thread->transaction_stack = t; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3197 | binder_inner_proc_unlock(proc); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3198 | if (!binder_proc_transaction(t, target_proc, target_thread)) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3199 | binder_inner_proc_lock(proc); |
| 3200 | binder_pop_transaction_ilocked(thread, t); |
| 3201 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3202 | goto err_dead_proc_or_thread; |
| 3203 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3204 | } else { |
| 3205 | BUG_ON(target_node == NULL); |
| 3206 | BUG_ON(t->buffer->async_transaction != 1); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3207 | binder_enqueue_thread_work(thread, tcomplete); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3208 | if (!binder_proc_transaction(t, target_proc, NULL)) |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3209 | goto err_dead_proc_or_thread; |
Riley Andrews | 00b40d6 | 2017-06-29 12:01:37 -0700 | [diff] [blame] | 3210 | } |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3211 | if (target_thread) |
| 3212 | binder_thread_dec_tmpref(target_thread); |
| 3213 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3214 | if (target_node) |
| 3215 | binder_dec_node_tmpref(target_node); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 3216 | /* |
| 3217 | * write barrier to synchronize with initialization |
| 3218 | * of log entry |
| 3219 | */ |
| 3220 | smp_wmb(); |
| 3221 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3222 | return; |
| 3223 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3224 | err_dead_proc_or_thread: |
| 3225 | return_error = BR_DEAD_REPLY; |
| 3226 | return_error_line = __LINE__; |
Xu YiPing | d53bebd | 2017-09-05 10:21:52 -0700 | [diff] [blame] | 3227 | binder_dequeue_work(proc, tcomplete); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3228 | err_translate_failed: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3229 | err_bad_object_type: |
| 3230 | err_bad_offset: |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3231 | err_bad_parent: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3232 | err_copy_data_failed: |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3233 | trace_binder_transaction_failed_buffer_release(t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3234 | binder_transaction_buffer_release(target_proc, t->buffer, offp); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3235 | if (target_node) |
| 3236 | binder_dec_node_tmpref(target_node); |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3237 | target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3238 | t->buffer->transaction = NULL; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 3239 | binder_alloc_free_buf(&target_proc->alloc, t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3240 | err_binder_alloc_buf_failed: |
| 3241 | kfree(tcomplete); |
| 3242 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3243 | err_alloc_tcomplete_failed: |
| 3244 | kfree(t); |
| 3245 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 3246 | err_alloc_t_failed: |
| 3247 | err_bad_call_stack: |
| 3248 | err_empty_call_stack: |
| 3249 | err_dead_binder: |
| 3250 | err_invalid_target_handle: |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3251 | if (target_thread) |
| 3252 | binder_thread_dec_tmpref(target_thread); |
| 3253 | if (target_proc) |
| 3254 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3255 | if (target_node) { |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3256 | binder_dec_node(target_node, 1, 0); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3257 | binder_dec_node_tmpref(target_node); |
| 3258 | } |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3259 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3260 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3261 | "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n", |
| 3262 | proc->pid, thread->pid, return_error, return_error_param, |
| 3263 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3264 | return_error_line); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3265 | |
| 3266 | { |
| 3267 | struct binder_transaction_log_entry *fe; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3268 | |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3269 | e->return_error = return_error; |
| 3270 | e->return_error_param = return_error_param; |
| 3271 | e->return_error_line = return_error_line; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3272 | fe = binder_transaction_log_add(&binder_transaction_log_failed); |
| 3273 | *fe = *e; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 3274 | /* |
| 3275 | * write barrier to synchronize with initialization |
| 3276 | * of log entry |
| 3277 | */ |
| 3278 | smp_wmb(); |
| 3279 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
| 3280 | WRITE_ONCE(fe->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3281 | } |
| 3282 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3283 | BUG_ON(thread->return_error.cmd != BR_OK); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3284 | if (in_reply_to) { |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3285 | thread->return_error.cmd = BR_TRANSACTION_COMPLETE; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3286 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3287 | binder_send_failed_reply(in_reply_to, return_error); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3288 | } else { |
| 3289 | thread->return_error.cmd = return_error; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3290 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3291 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3292 | } |
| 3293 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3294 | static int binder_thread_write(struct binder_proc *proc, |
| 3295 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3296 | binder_uintptr_t binder_buffer, size_t size, |
| 3297 | binder_size_t *consumed) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3298 | { |
| 3299 | uint32_t cmd; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 3300 | struct binder_context *context = proc->context; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3301 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3302 | void __user *ptr = buffer + *consumed; |
| 3303 | void __user *end = buffer + size; |
| 3304 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3305 | while (ptr < end && thread->return_error.cmd == BR_OK) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3306 | int ret; |
| 3307 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3308 | if (get_user(cmd, (uint32_t __user *)ptr)) |
| 3309 | return -EFAULT; |
| 3310 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3311 | trace_binder_command(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3312 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 3313 | atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]); |
| 3314 | atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]); |
| 3315 | atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3316 | } |
| 3317 | switch (cmd) { |
| 3318 | case BC_INCREFS: |
| 3319 | case BC_ACQUIRE: |
| 3320 | case BC_RELEASE: |
| 3321 | case BC_DECREFS: { |
| 3322 | uint32_t target; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3323 | const char *debug_string; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3324 | bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE; |
| 3325 | bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE; |
| 3326 | struct binder_ref_data rdata; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3327 | |
| 3328 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3329 | return -EFAULT; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3330 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3331 | ptr += sizeof(uint32_t); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3332 | ret = -1; |
| 3333 | if (increment && !target) { |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3334 | struct binder_node *ctx_mgr_node; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3335 | mutex_lock(&context->context_mgr_node_lock); |
| 3336 | ctx_mgr_node = context->binder_context_mgr_node; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3337 | if (ctx_mgr_node) |
| 3338 | ret = binder_inc_ref_for_node( |
| 3339 | proc, ctx_mgr_node, |
| 3340 | strong, NULL, &rdata); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3341 | mutex_unlock(&context->context_mgr_node_lock); |
| 3342 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3343 | if (ret) |
| 3344 | ret = binder_update_ref_for_handle( |
| 3345 | proc, target, increment, strong, |
| 3346 | &rdata); |
| 3347 | if (!ret && rdata.desc != target) { |
| 3348 | binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n", |
| 3349 | proc->pid, thread->pid, |
| 3350 | target, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3351 | } |
| 3352 | switch (cmd) { |
| 3353 | case BC_INCREFS: |
| 3354 | debug_string = "IncRefs"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3355 | break; |
| 3356 | case BC_ACQUIRE: |
| 3357 | debug_string = "Acquire"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3358 | break; |
| 3359 | case BC_RELEASE: |
| 3360 | debug_string = "Release"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3361 | break; |
| 3362 | case BC_DECREFS: |
| 3363 | default: |
| 3364 | debug_string = "DecRefs"; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3365 | break; |
| 3366 | } |
| 3367 | if (ret) { |
| 3368 | binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n", |
| 3369 | proc->pid, thread->pid, debug_string, |
| 3370 | strong, target, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3371 | break; |
| 3372 | } |
| 3373 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3374 | "%d:%d %s ref %d desc %d s %d w %d\n", |
| 3375 | proc->pid, thread->pid, debug_string, |
| 3376 | rdata.debug_id, rdata.desc, rdata.strong, |
| 3377 | rdata.weak); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3378 | break; |
| 3379 | } |
| 3380 | case BC_INCREFS_DONE: |
| 3381 | case BC_ACQUIRE_DONE: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3382 | binder_uintptr_t node_ptr; |
| 3383 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3384 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3385 | bool free_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3386 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3387 | if (get_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3388 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3389 | ptr += sizeof(binder_uintptr_t); |
| 3390 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3391 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3392 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3393 | node = binder_get_node(proc, node_ptr); |
| 3394 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3395 | binder_user_error("%d:%d %s u%016llx no match\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3396 | proc->pid, thread->pid, |
| 3397 | cmd == BC_INCREFS_DONE ? |
| 3398 | "BC_INCREFS_DONE" : |
| 3399 | "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3400 | (u64)node_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3401 | break; |
| 3402 | } |
| 3403 | if (cookie != node->cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3404 | 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] | 3405 | proc->pid, thread->pid, |
| 3406 | cmd == BC_INCREFS_DONE ? |
| 3407 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3408 | (u64)node_ptr, node->debug_id, |
| 3409 | (u64)cookie, (u64)node->cookie); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3410 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3411 | break; |
| 3412 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3413 | binder_node_inner_lock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3414 | if (cmd == BC_ACQUIRE_DONE) { |
| 3415 | if (node->pending_strong_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3416 | 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] | 3417 | proc->pid, thread->pid, |
| 3418 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3419 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3420 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3421 | break; |
| 3422 | } |
| 3423 | node->pending_strong_ref = 0; |
| 3424 | } else { |
| 3425 | if (node->pending_weak_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3426 | 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] | 3427 | proc->pid, thread->pid, |
| 3428 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3429 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3430 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3431 | break; |
| 3432 | } |
| 3433 | node->pending_weak_ref = 0; |
| 3434 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3435 | free_node = binder_dec_node_nilocked(node, |
| 3436 | cmd == BC_ACQUIRE_DONE, 0); |
| 3437 | WARN_ON(free_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3438 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3439 | "%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] | 3440 | proc->pid, thread->pid, |
| 3441 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3442 | node->debug_id, node->local_strong_refs, |
| 3443 | node->local_weak_refs, node->tmp_refs); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3444 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3445 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3446 | break; |
| 3447 | } |
| 3448 | case BC_ATTEMPT_ACQUIRE: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3449 | pr_err("BC_ATTEMPT_ACQUIRE not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3450 | return -EINVAL; |
| 3451 | case BC_ACQUIRE_RESULT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3452 | pr_err("BC_ACQUIRE_RESULT not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3453 | return -EINVAL; |
| 3454 | |
| 3455 | case BC_FREE_BUFFER: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3456 | binder_uintptr_t data_ptr; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3457 | struct binder_buffer *buffer; |
| 3458 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3459 | if (get_user(data_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3460 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3461 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3462 | |
Todd Kjos | 53d311cf | 2017-06-29 12:01:51 -0700 | [diff] [blame] | 3463 | buffer = binder_alloc_prepare_to_free(&proc->alloc, |
| 3464 | data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3465 | if (buffer == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3466 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n", |
| 3467 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3468 | break; |
| 3469 | } |
| 3470 | if (!buffer->allow_user_free) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3471 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n", |
| 3472 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3473 | break; |
| 3474 | } |
| 3475 | binder_debug(BINDER_DEBUG_FREE_BUFFER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3476 | "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n", |
| 3477 | proc->pid, thread->pid, (u64)data_ptr, |
| 3478 | buffer->debug_id, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3479 | buffer->transaction ? "active" : "finished"); |
| 3480 | |
| 3481 | if (buffer->transaction) { |
| 3482 | buffer->transaction->buffer = NULL; |
| 3483 | buffer->transaction = NULL; |
| 3484 | } |
| 3485 | if (buffer->async_transaction && buffer->target_node) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3486 | struct binder_node *buf_node; |
| 3487 | struct binder_work *w; |
| 3488 | |
| 3489 | buf_node = buffer->target_node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3490 | binder_node_inner_lock(buf_node); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3491 | BUG_ON(!buf_node->has_async_transaction); |
| 3492 | BUG_ON(buf_node->proc != proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3493 | w = binder_dequeue_work_head_ilocked( |
| 3494 | &buf_node->async_todo); |
Martijn Coenen | 3a6430c | 2017-08-31 10:04:29 +0200 | [diff] [blame] | 3495 | if (!w) { |
Gustavo A. R. Silva | 197410a | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 3496 | buf_node->has_async_transaction = false; |
Martijn Coenen | 3a6430c | 2017-08-31 10:04:29 +0200 | [diff] [blame] | 3497 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3498 | binder_enqueue_work_ilocked( |
Martijn Coenen | 3a6430c | 2017-08-31 10:04:29 +0200 | [diff] [blame] | 3499 | w, &proc->todo); |
| 3500 | binder_wakeup_proc_ilocked(proc); |
| 3501 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3502 | binder_node_inner_unlock(buf_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3503 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3504 | trace_binder_transaction_buffer_release(buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3505 | binder_transaction_buffer_release(proc, buffer, NULL); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 3506 | binder_alloc_free_buf(&proc->alloc, buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3507 | break; |
| 3508 | } |
| 3509 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3510 | case BC_TRANSACTION_SG: |
| 3511 | case BC_REPLY_SG: { |
| 3512 | struct binder_transaction_data_sg tr; |
| 3513 | |
| 3514 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3515 | return -EFAULT; |
| 3516 | ptr += sizeof(tr); |
| 3517 | binder_transaction(proc, thread, &tr.transaction_data, |
| 3518 | cmd == BC_REPLY_SG, tr.buffers_size); |
| 3519 | break; |
| 3520 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3521 | case BC_TRANSACTION: |
| 3522 | case BC_REPLY: { |
| 3523 | struct binder_transaction_data tr; |
| 3524 | |
| 3525 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3526 | return -EFAULT; |
| 3527 | ptr += sizeof(tr); |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 3528 | binder_transaction(proc, thread, &tr, |
| 3529 | cmd == BC_REPLY, 0); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3530 | break; |
| 3531 | } |
| 3532 | |
| 3533 | case BC_REGISTER_LOOPER: |
| 3534 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3535 | "%d:%d BC_REGISTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3536 | proc->pid, thread->pid); |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3537 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3538 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { |
| 3539 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3540 | 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] | 3541 | proc->pid, thread->pid); |
| 3542 | } else if (proc->requested_threads == 0) { |
| 3543 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3544 | 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] | 3545 | proc->pid, thread->pid); |
| 3546 | } else { |
| 3547 | proc->requested_threads--; |
| 3548 | proc->requested_threads_started++; |
| 3549 | } |
| 3550 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3551 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3552 | break; |
| 3553 | case BC_ENTER_LOOPER: |
| 3554 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3555 | "%d:%d BC_ENTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3556 | proc->pid, thread->pid); |
| 3557 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { |
| 3558 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3559 | 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] | 3560 | proc->pid, thread->pid); |
| 3561 | } |
| 3562 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; |
| 3563 | break; |
| 3564 | case BC_EXIT_LOOPER: |
| 3565 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3566 | "%d:%d BC_EXIT_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3567 | proc->pid, thread->pid); |
| 3568 | thread->looper |= BINDER_LOOPER_STATE_EXITED; |
| 3569 | break; |
| 3570 | |
| 3571 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 3572 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 3573 | uint32_t target; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3574 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3575 | struct binder_ref *ref; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3576 | struct binder_ref_death *death = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3577 | |
| 3578 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3579 | return -EFAULT; |
| 3580 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3581 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3582 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3583 | ptr += sizeof(binder_uintptr_t); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3584 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3585 | /* |
| 3586 | * Allocate memory for death notification |
| 3587 | * before taking lock |
| 3588 | */ |
| 3589 | death = kzalloc(sizeof(*death), GFP_KERNEL); |
| 3590 | if (death == NULL) { |
| 3591 | WARN_ON(thread->return_error.cmd != |
| 3592 | BR_OK); |
| 3593 | thread->return_error.cmd = BR_ERROR; |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3594 | binder_enqueue_thread_work( |
| 3595 | thread, |
| 3596 | &thread->return_error.work); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3597 | binder_debug( |
| 3598 | BINDER_DEBUG_FAILED_TRANSACTION, |
| 3599 | "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n", |
| 3600 | proc->pid, thread->pid); |
| 3601 | break; |
| 3602 | } |
| 3603 | } |
| 3604 | binder_proc_lock(proc); |
| 3605 | ref = binder_get_ref_olocked(proc, target, false); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3606 | if (ref == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3607 | binder_user_error("%d:%d %s invalid ref %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3608 | proc->pid, thread->pid, |
| 3609 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3610 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3611 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 3612 | target); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3613 | binder_proc_unlock(proc); |
| 3614 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3615 | break; |
| 3616 | } |
| 3617 | |
| 3618 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3619 | "%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] | 3620 | proc->pid, thread->pid, |
| 3621 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3622 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3623 | "BC_CLEAR_DEATH_NOTIFICATION", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3624 | (u64)cookie, ref->data.debug_id, |
| 3625 | ref->data.desc, ref->data.strong, |
| 3626 | ref->data.weak, ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3627 | |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3628 | binder_node_lock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3629 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3630 | if (ref->death) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3631 | 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] | 3632 | proc->pid, thread->pid); |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3633 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3634 | binder_proc_unlock(proc); |
| 3635 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3636 | break; |
| 3637 | } |
| 3638 | binder_stats_created(BINDER_STAT_DEATH); |
| 3639 | INIT_LIST_HEAD(&death->work.entry); |
| 3640 | death->cookie = cookie; |
| 3641 | ref->death = death; |
| 3642 | if (ref->node->proc == NULL) { |
| 3643 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
Martijn Coenen | bb74562 | 2017-08-31 10:04:28 +0200 | [diff] [blame] | 3644 | |
| 3645 | binder_inner_proc_lock(proc); |
| 3646 | binder_enqueue_work_ilocked( |
| 3647 | &ref->death->work, &proc->todo); |
| 3648 | binder_wakeup_proc_ilocked(proc); |
| 3649 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3650 | } |
| 3651 | } else { |
| 3652 | if (ref->death == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3653 | 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] | 3654 | proc->pid, thread->pid); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3655 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3656 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3657 | break; |
| 3658 | } |
| 3659 | death = ref->death; |
| 3660 | if (death->cookie != cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3661 | 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] | 3662 | proc->pid, thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3663 | (u64)death->cookie, |
| 3664 | (u64)cookie); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3665 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3666 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3667 | break; |
| 3668 | } |
| 3669 | ref->death = NULL; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3670 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3671 | if (list_empty(&death->work.entry)) { |
| 3672 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3673 | if (thread->looper & |
| 3674 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3675 | BINDER_LOOPER_STATE_ENTERED)) |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3676 | binder_enqueue_thread_work_ilocked( |
| 3677 | thread, |
| 3678 | &death->work); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3679 | else { |
| 3680 | binder_enqueue_work_ilocked( |
| 3681 | &death->work, |
| 3682 | &proc->todo); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 3683 | binder_wakeup_proc_ilocked( |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3684 | proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3685 | } |
| 3686 | } else { |
| 3687 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); |
| 3688 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; |
| 3689 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3690 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3691 | } |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3692 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3693 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3694 | } break; |
| 3695 | case BC_DEAD_BINDER_DONE: { |
| 3696 | struct binder_work *w; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3697 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3698 | struct binder_ref_death *death = NULL; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3699 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3700 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3701 | return -EFAULT; |
| 3702 | |
Lisa Du | 7a64cd8 | 2016-02-17 09:32:52 +0800 | [diff] [blame] | 3703 | ptr += sizeof(cookie); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3704 | binder_inner_proc_lock(proc); |
| 3705 | list_for_each_entry(w, &proc->delivered_death, |
| 3706 | entry) { |
| 3707 | struct binder_ref_death *tmp_death = |
| 3708 | container_of(w, |
| 3709 | struct binder_ref_death, |
| 3710 | work); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3711 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3712 | if (tmp_death->cookie == cookie) { |
| 3713 | death = tmp_death; |
| 3714 | break; |
| 3715 | } |
| 3716 | } |
| 3717 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Todd Kjos | 8ca86f1 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 3718 | "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3719 | proc->pid, thread->pid, (u64)cookie, |
| 3720 | death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3721 | if (death == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3722 | binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n", |
| 3723 | proc->pid, thread->pid, (u64)cookie); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3724 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3725 | break; |
| 3726 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3727 | binder_dequeue_work_ilocked(&death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3728 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { |
| 3729 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3730 | if (thread->looper & |
| 3731 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3732 | BINDER_LOOPER_STATE_ENTERED)) |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3733 | binder_enqueue_thread_work_ilocked( |
| 3734 | thread, &death->work); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3735 | else { |
| 3736 | binder_enqueue_work_ilocked( |
| 3737 | &death->work, |
| 3738 | &proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3739 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3740 | } |
| 3741 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3742 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3743 | } break; |
| 3744 | |
| 3745 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3746 | pr_err("%d:%d unknown command %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3747 | proc->pid, thread->pid, cmd); |
| 3748 | return -EINVAL; |
| 3749 | } |
| 3750 | *consumed = ptr - buffer; |
| 3751 | } |
| 3752 | return 0; |
| 3753 | } |
| 3754 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3755 | static void binder_stat_br(struct binder_proc *proc, |
| 3756 | struct binder_thread *thread, uint32_t cmd) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3757 | { |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3758 | trace_binder_return(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3759 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 3760 | atomic_inc(&binder_stats.br[_IOC_NR(cmd)]); |
| 3761 | atomic_inc(&proc->stats.br[_IOC_NR(cmd)]); |
| 3762 | atomic_inc(&thread->stats.br[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3763 | } |
| 3764 | } |
| 3765 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3766 | static int binder_put_node_cmd(struct binder_proc *proc, |
| 3767 | struct binder_thread *thread, |
| 3768 | void __user **ptrp, |
| 3769 | binder_uintptr_t node_ptr, |
| 3770 | binder_uintptr_t node_cookie, |
| 3771 | int node_debug_id, |
| 3772 | uint32_t cmd, const char *cmd_name) |
| 3773 | { |
| 3774 | void __user *ptr = *ptrp; |
| 3775 | |
| 3776 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 3777 | return -EFAULT; |
| 3778 | ptr += sizeof(uint32_t); |
| 3779 | |
| 3780 | if (put_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
| 3781 | return -EFAULT; |
| 3782 | ptr += sizeof(binder_uintptr_t); |
| 3783 | |
| 3784 | if (put_user(node_cookie, (binder_uintptr_t __user *)ptr)) |
| 3785 | return -EFAULT; |
| 3786 | ptr += sizeof(binder_uintptr_t); |
| 3787 | |
| 3788 | binder_stat_br(proc, thread, cmd); |
| 3789 | binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n", |
| 3790 | proc->pid, thread->pid, cmd_name, node_debug_id, |
| 3791 | (u64)node_ptr, (u64)node_cookie); |
| 3792 | |
| 3793 | *ptrp = ptr; |
| 3794 | return 0; |
| 3795 | } |
| 3796 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 3797 | static int binder_wait_for_work(struct binder_thread *thread, |
| 3798 | bool do_proc_work) |
| 3799 | { |
| 3800 | DEFINE_WAIT(wait); |
| 3801 | struct binder_proc *proc = thread->proc; |
| 3802 | int ret = 0; |
| 3803 | |
| 3804 | freezer_do_not_count(); |
| 3805 | binder_inner_proc_lock(proc); |
| 3806 | for (;;) { |
| 3807 | prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE); |
| 3808 | if (binder_has_work_ilocked(thread, do_proc_work)) |
| 3809 | break; |
| 3810 | if (do_proc_work) |
| 3811 | list_add(&thread->waiting_thread_node, |
| 3812 | &proc->waiting_threads); |
| 3813 | binder_inner_proc_unlock(proc); |
| 3814 | schedule(); |
| 3815 | binder_inner_proc_lock(proc); |
| 3816 | list_del_init(&thread->waiting_thread_node); |
| 3817 | if (signal_pending(current)) { |
| 3818 | ret = -ERESTARTSYS; |
| 3819 | break; |
| 3820 | } |
| 3821 | } |
| 3822 | finish_wait(&thread->wait, &wait); |
| 3823 | binder_inner_proc_unlock(proc); |
| 3824 | freezer_count(); |
| 3825 | |
| 3826 | return ret; |
| 3827 | } |
| 3828 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3829 | static int binder_thread_read(struct binder_proc *proc, |
| 3830 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3831 | binder_uintptr_t binder_buffer, size_t size, |
| 3832 | binder_size_t *consumed, int non_block) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3833 | { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3834 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3835 | void __user *ptr = buffer + *consumed; |
| 3836 | void __user *end = buffer + size; |
| 3837 | |
| 3838 | int ret = 0; |
| 3839 | int wait_for_proc_work; |
| 3840 | |
| 3841 | if (*consumed == 0) { |
| 3842 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) |
| 3843 | return -EFAULT; |
| 3844 | ptr += sizeof(uint32_t); |
| 3845 | } |
| 3846 | |
| 3847 | retry: |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3848 | binder_inner_proc_lock(proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 3849 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3850 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3851 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3852 | thread->looper |= BINDER_LOOPER_STATE_WAITING; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3853 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3854 | trace_binder_wait_for_work(wait_for_proc_work, |
| 3855 | !!thread->transaction_stack, |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3856 | !binder_worklist_empty(proc, &thread->todo)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3857 | if (wait_for_proc_work) { |
| 3858 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 3859 | BINDER_LOOPER_STATE_ENTERED))) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3860 | 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] | 3861 | proc->pid, thread->pid, thread->looper); |
| 3862 | wait_event_interruptible(binder_user_error_wait, |
| 3863 | binder_stop_on_user_error < 2); |
| 3864 | } |
| 3865 | binder_set_nice(proc->default_priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3866 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3867 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 3868 | if (non_block) { |
| 3869 | if (!binder_has_work(thread, wait_for_proc_work)) |
| 3870 | ret = -EAGAIN; |
| 3871 | } else { |
| 3872 | ret = binder_wait_for_work(thread, wait_for_proc_work); |
| 3873 | } |
| 3874 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3875 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; |
| 3876 | |
| 3877 | if (ret) |
| 3878 | return ret; |
| 3879 | |
| 3880 | while (1) { |
| 3881 | uint32_t cmd; |
| 3882 | struct binder_transaction_data tr; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3883 | struct binder_work *w = NULL; |
| 3884 | struct list_head *list = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3885 | struct binder_transaction *t = NULL; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3886 | struct binder_thread *t_from; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3887 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3888 | binder_inner_proc_lock(proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3889 | if (!binder_worklist_empty_ilocked(&thread->todo)) |
| 3890 | list = &thread->todo; |
| 3891 | else if (!binder_worklist_empty_ilocked(&proc->todo) && |
| 3892 | wait_for_proc_work) |
| 3893 | list = &proc->todo; |
| 3894 | else { |
| 3895 | binder_inner_proc_unlock(proc); |
| 3896 | |
Dmitry Voytik | 395262a | 2014-09-08 18:16:34 +0400 | [diff] [blame] | 3897 | /* no data added */ |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 3898 | if (ptr - buffer == 4 && !thread->looper_need_return) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3899 | goto retry; |
| 3900 | break; |
| 3901 | } |
| 3902 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3903 | if (end - ptr < sizeof(tr) + 4) { |
| 3904 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3905 | break; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3906 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3907 | w = binder_dequeue_work_head_ilocked(list); |
Martijn Coenen | 148ade2 | 2017-11-15 09:21:35 +0100 | [diff] [blame] | 3908 | if (binder_worklist_empty_ilocked(&thread->todo)) |
| 3909 | thread->process_todo = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3910 | |
| 3911 | switch (w->type) { |
| 3912 | case BINDER_WORK_TRANSACTION: { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3913 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3914 | t = container_of(w, struct binder_transaction, work); |
| 3915 | } break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3916 | case BINDER_WORK_RETURN_ERROR: { |
| 3917 | struct binder_error *e = container_of( |
| 3918 | w, struct binder_error, work); |
| 3919 | |
| 3920 | WARN_ON(e->cmd == BR_OK); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3921 | binder_inner_proc_unlock(proc); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3922 | if (put_user(e->cmd, (uint32_t __user *)ptr)) |
| 3923 | return -EFAULT; |
| 3924 | e->cmd = BR_OK; |
| 3925 | ptr += sizeof(uint32_t); |
| 3926 | |
Todd Kjos | 4f9adc8 | 2017-08-08 15:48:36 -0700 | [diff] [blame] | 3927 | binder_stat_br(proc, thread, e->cmd); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3928 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3929 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3930 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3931 | cmd = BR_TRANSACTION_COMPLETE; |
| 3932 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 3933 | return -EFAULT; |
| 3934 | ptr += sizeof(uint32_t); |
| 3935 | |
| 3936 | binder_stat_br(proc, thread, cmd); |
| 3937 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3938 | "%d:%d BR_TRANSACTION_COMPLETE\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3939 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3940 | kfree(w); |
| 3941 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3942 | } break; |
| 3943 | case BINDER_WORK_NODE: { |
| 3944 | struct binder_node *node = container_of(w, struct binder_node, work); |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3945 | int strong, weak; |
| 3946 | binder_uintptr_t node_ptr = node->ptr; |
| 3947 | binder_uintptr_t node_cookie = node->cookie; |
| 3948 | int node_debug_id = node->debug_id; |
| 3949 | int has_weak_ref; |
| 3950 | int has_strong_ref; |
| 3951 | void __user *orig_ptr = ptr; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3952 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3953 | BUG_ON(proc != node->proc); |
| 3954 | strong = node->internal_strong_refs || |
| 3955 | node->local_strong_refs; |
| 3956 | weak = !hlist_empty(&node->refs) || |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3957 | node->local_weak_refs || |
| 3958 | node->tmp_refs || strong; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3959 | has_strong_ref = node->has_strong_ref; |
| 3960 | has_weak_ref = node->has_weak_ref; |
| 3961 | |
| 3962 | if (weak && !has_weak_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3963 | node->has_weak_ref = 1; |
| 3964 | node->pending_weak_ref = 1; |
| 3965 | node->local_weak_refs++; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3966 | } |
| 3967 | if (strong && !has_strong_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3968 | node->has_strong_ref = 1; |
| 3969 | node->pending_strong_ref = 1; |
| 3970 | node->local_strong_refs++; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3971 | } |
| 3972 | if (!strong && has_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3973 | node->has_strong_ref = 0; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3974 | if (!weak && has_weak_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3975 | node->has_weak_ref = 0; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3976 | if (!weak && !strong) { |
| 3977 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 3978 | "%d:%d node %d u%016llx c%016llx deleted\n", |
| 3979 | proc->pid, thread->pid, |
| 3980 | node_debug_id, |
| 3981 | (u64)node_ptr, |
| 3982 | (u64)node_cookie); |
| 3983 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3984 | binder_inner_proc_unlock(proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3985 | binder_node_lock(node); |
| 3986 | /* |
| 3987 | * Acquire the node lock before freeing the |
| 3988 | * node to serialize with other threads that |
| 3989 | * may have been holding the node lock while |
| 3990 | * decrementing this node (avoids race where |
| 3991 | * this thread frees while the other thread |
| 3992 | * is unlocking the node after the final |
| 3993 | * decrement) |
| 3994 | */ |
| 3995 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3996 | binder_free_node(node); |
| 3997 | } else |
| 3998 | binder_inner_proc_unlock(proc); |
| 3999 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4000 | if (weak && !has_weak_ref) |
| 4001 | ret = binder_put_node_cmd( |
| 4002 | proc, thread, &ptr, node_ptr, |
| 4003 | node_cookie, node_debug_id, |
| 4004 | BR_INCREFS, "BR_INCREFS"); |
| 4005 | if (!ret && strong && !has_strong_ref) |
| 4006 | ret = binder_put_node_cmd( |
| 4007 | proc, thread, &ptr, node_ptr, |
| 4008 | node_cookie, node_debug_id, |
| 4009 | BR_ACQUIRE, "BR_ACQUIRE"); |
| 4010 | if (!ret && !strong && has_strong_ref) |
| 4011 | ret = binder_put_node_cmd( |
| 4012 | proc, thread, &ptr, node_ptr, |
| 4013 | node_cookie, node_debug_id, |
| 4014 | BR_RELEASE, "BR_RELEASE"); |
| 4015 | if (!ret && !weak && has_weak_ref) |
| 4016 | ret = binder_put_node_cmd( |
| 4017 | proc, thread, &ptr, node_ptr, |
| 4018 | node_cookie, node_debug_id, |
| 4019 | BR_DECREFS, "BR_DECREFS"); |
| 4020 | if (orig_ptr == ptr) |
| 4021 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4022 | "%d:%d node %d u%016llx c%016llx state unchanged\n", |
| 4023 | proc->pid, thread->pid, |
| 4024 | node_debug_id, |
| 4025 | (u64)node_ptr, |
| 4026 | (u64)node_cookie); |
| 4027 | if (ret) |
| 4028 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4029 | } break; |
| 4030 | case BINDER_WORK_DEAD_BINDER: |
| 4031 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4032 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4033 | struct binder_ref_death *death; |
| 4034 | uint32_t cmd; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4035 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4036 | |
| 4037 | death = container_of(w, struct binder_ref_death, work); |
| 4038 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) |
| 4039 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; |
| 4040 | else |
| 4041 | cmd = BR_DEAD_BINDER; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4042 | cookie = death->cookie; |
| 4043 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4044 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4045 | "%d:%d %s %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4046 | proc->pid, thread->pid, |
| 4047 | cmd == BR_DEAD_BINDER ? |
| 4048 | "BR_DEAD_BINDER" : |
| 4049 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4050 | (u64)cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4051 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4052 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4053 | kfree(death); |
| 4054 | binder_stats_deleted(BINDER_STAT_DEATH); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4055 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4056 | binder_enqueue_work_ilocked( |
| 4057 | w, &proc->delivered_death); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4058 | binder_inner_proc_unlock(proc); |
| 4059 | } |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4060 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4061 | return -EFAULT; |
| 4062 | ptr += sizeof(uint32_t); |
| 4063 | if (put_user(cookie, |
| 4064 | (binder_uintptr_t __user *)ptr)) |
| 4065 | return -EFAULT; |
| 4066 | ptr += sizeof(binder_uintptr_t); |
| 4067 | binder_stat_br(proc, thread, cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4068 | if (cmd == BR_DEAD_BINDER) |
| 4069 | goto done; /* DEAD_BINDER notifications can cause transactions */ |
| 4070 | } break; |
| 4071 | } |
| 4072 | |
| 4073 | if (!t) |
| 4074 | continue; |
| 4075 | |
| 4076 | BUG_ON(t->buffer == NULL); |
| 4077 | if (t->buffer->target_node) { |
| 4078 | struct binder_node *target_node = t->buffer->target_node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4079 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4080 | tr.target.ptr = target_node->ptr; |
| 4081 | tr.cookie = target_node->cookie; |
| 4082 | t->saved_priority = task_nice(current); |
| 4083 | if (t->priority < target_node->min_priority && |
| 4084 | !(t->flags & TF_ONE_WAY)) |
| 4085 | binder_set_nice(t->priority); |
| 4086 | else if (!(t->flags & TF_ONE_WAY) || |
| 4087 | t->saved_priority > target_node->min_priority) |
| 4088 | binder_set_nice(target_node->min_priority); |
| 4089 | cmd = BR_TRANSACTION; |
| 4090 | } else { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4091 | tr.target.ptr = 0; |
| 4092 | tr.cookie = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4093 | cmd = BR_REPLY; |
| 4094 | } |
| 4095 | tr.code = t->code; |
| 4096 | tr.flags = t->flags; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 4097 | tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4098 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4099 | t_from = binder_get_txn_from(t); |
| 4100 | if (t_from) { |
| 4101 | struct task_struct *sender = t_from->proc->tsk; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4102 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4103 | tr.sender_pid = task_tgid_nr_ns(sender, |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 4104 | task_active_pid_ns(current)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4105 | } else { |
| 4106 | tr.sender_pid = 0; |
| 4107 | } |
| 4108 | |
| 4109 | tr.data_size = t->buffer->data_size; |
| 4110 | tr.offsets_size = t->buffer->offsets_size; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4111 | tr.data.ptr.buffer = (binder_uintptr_t) |
| 4112 | ((uintptr_t)t->buffer->data + |
| 4113 | binder_alloc_get_user_buffer_offset(&proc->alloc)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4114 | tr.data.ptr.offsets = tr.data.ptr.buffer + |
| 4115 | ALIGN(t->buffer->data_size, |
| 4116 | sizeof(void *)); |
| 4117 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4118 | if (put_user(cmd, (uint32_t __user *)ptr)) { |
| 4119 | if (t_from) |
| 4120 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | fb2c445 | 2017-11-13 10:06:08 +0100 | [diff] [blame] | 4121 | |
| 4122 | binder_cleanup_transaction(t, "put_user failed", |
| 4123 | BR_FAILED_REPLY); |
| 4124 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4125 | return -EFAULT; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4126 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4127 | ptr += sizeof(uint32_t); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4128 | if (copy_to_user(ptr, &tr, sizeof(tr))) { |
| 4129 | if (t_from) |
| 4130 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | fb2c445 | 2017-11-13 10:06:08 +0100 | [diff] [blame] | 4131 | |
| 4132 | binder_cleanup_transaction(t, "copy_to_user failed", |
| 4133 | BR_FAILED_REPLY); |
| 4134 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4135 | return -EFAULT; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4136 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4137 | ptr += sizeof(tr); |
| 4138 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4139 | trace_binder_transaction_received(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4140 | binder_stat_br(proc, thread, cmd); |
| 4141 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4142 | "%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] | 4143 | proc->pid, thread->pid, |
| 4144 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : |
| 4145 | "BR_REPLY", |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4146 | t->debug_id, t_from ? t_from->proc->pid : 0, |
| 4147 | t_from ? t_from->pid : 0, cmd, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4148 | t->buffer->data_size, t->buffer->offsets_size, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4149 | (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4150 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4151 | if (t_from) |
| 4152 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4153 | t->buffer->allow_user_free = 1; |
| 4154 | if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4155 | binder_inner_proc_lock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4156 | t->to_parent = thread->transaction_stack; |
| 4157 | t->to_thread = thread; |
| 4158 | thread->transaction_stack = t; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4159 | binder_inner_proc_unlock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4160 | } else { |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 4161 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4162 | } |
| 4163 | break; |
| 4164 | } |
| 4165 | |
| 4166 | done: |
| 4167 | |
| 4168 | *consumed = ptr - buffer; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4169 | binder_inner_proc_lock(proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4170 | if (proc->requested_threads == 0 && |
| 4171 | list_empty(&thread->proc->waiting_threads) && |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4172 | proc->requested_threads_started < proc->max_threads && |
| 4173 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4174 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ |
| 4175 | /*spawn a new thread if we leave this out */) { |
| 4176 | proc->requested_threads++; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4177 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4178 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4179 | "%d:%d BR_SPAWN_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4180 | proc->pid, thread->pid); |
| 4181 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) |
| 4182 | return -EFAULT; |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 4183 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4184 | } else |
| 4185 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4186 | return 0; |
| 4187 | } |
| 4188 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4189 | static void binder_release_work(struct binder_proc *proc, |
| 4190 | struct list_head *list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4191 | { |
| 4192 | struct binder_work *w; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4193 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4194 | while (1) { |
| 4195 | w = binder_dequeue_work_head(proc, list); |
| 4196 | if (!w) |
| 4197 | return; |
| 4198 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4199 | switch (w->type) { |
| 4200 | case BINDER_WORK_TRANSACTION: { |
| 4201 | struct binder_transaction *t; |
| 4202 | |
| 4203 | t = container_of(w, struct binder_transaction, work); |
Martijn Coenen | fb2c445 | 2017-11-13 10:06:08 +0100 | [diff] [blame] | 4204 | |
| 4205 | binder_cleanup_transaction(t, "process died.", |
| 4206 | BR_DEAD_REPLY); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4207 | } break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4208 | case BINDER_WORK_RETURN_ERROR: { |
| 4209 | struct binder_error *e = container_of( |
| 4210 | w, struct binder_error, work); |
| 4211 | |
| 4212 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 4213 | "undelivered TRANSACTION_ERROR: %u\n", |
| 4214 | e->cmd); |
| 4215 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4216 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4217 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4218 | "undelivered TRANSACTION_COMPLETE\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4219 | kfree(w); |
| 4220 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4221 | } break; |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4222 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4223 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4224 | struct binder_ref_death *death; |
| 4225 | |
| 4226 | death = container_of(w, struct binder_ref_death, work); |
| 4227 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4228 | "undelivered death notification, %016llx\n", |
| 4229 | (u64)death->cookie); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4230 | kfree(death); |
| 4231 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 4232 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4233 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4234 | pr_err("unexpected work type, %d, not freed\n", |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4235 | w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4236 | break; |
| 4237 | } |
| 4238 | } |
| 4239 | |
| 4240 | } |
| 4241 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4242 | static struct binder_thread *binder_get_thread_ilocked( |
| 4243 | struct binder_proc *proc, struct binder_thread *new_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4244 | { |
| 4245 | struct binder_thread *thread = NULL; |
| 4246 | struct rb_node *parent = NULL; |
| 4247 | struct rb_node **p = &proc->threads.rb_node; |
| 4248 | |
| 4249 | while (*p) { |
| 4250 | parent = *p; |
| 4251 | thread = rb_entry(parent, struct binder_thread, rb_node); |
| 4252 | |
| 4253 | if (current->pid < thread->pid) |
| 4254 | p = &(*p)->rb_left; |
| 4255 | else if (current->pid > thread->pid) |
| 4256 | p = &(*p)->rb_right; |
| 4257 | else |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4258 | return thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4259 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4260 | if (!new_thread) |
| 4261 | return NULL; |
| 4262 | thread = new_thread; |
| 4263 | binder_stats_created(BINDER_STAT_THREAD); |
| 4264 | thread->proc = proc; |
| 4265 | thread->pid = current->pid; |
| 4266 | atomic_set(&thread->tmp_ref, 0); |
| 4267 | init_waitqueue_head(&thread->wait); |
| 4268 | INIT_LIST_HEAD(&thread->todo); |
| 4269 | rb_link_node(&thread->rb_node, parent, p); |
| 4270 | rb_insert_color(&thread->rb_node, &proc->threads); |
| 4271 | thread->looper_need_return = true; |
| 4272 | thread->return_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4273 | thread->return_error.cmd = BR_OK; |
| 4274 | thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4275 | thread->reply_error.cmd = BR_OK; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4276 | INIT_LIST_HEAD(&new_thread->waiting_thread_node); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4277 | return thread; |
| 4278 | } |
| 4279 | |
| 4280 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) |
| 4281 | { |
| 4282 | struct binder_thread *thread; |
| 4283 | struct binder_thread *new_thread; |
| 4284 | |
| 4285 | binder_inner_proc_lock(proc); |
| 4286 | thread = binder_get_thread_ilocked(proc, NULL); |
| 4287 | binder_inner_proc_unlock(proc); |
| 4288 | if (!thread) { |
| 4289 | new_thread = kzalloc(sizeof(*thread), GFP_KERNEL); |
| 4290 | if (new_thread == NULL) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4291 | return NULL; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4292 | binder_inner_proc_lock(proc); |
| 4293 | thread = binder_get_thread_ilocked(proc, new_thread); |
| 4294 | binder_inner_proc_unlock(proc); |
| 4295 | if (thread != new_thread) |
| 4296 | kfree(new_thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4297 | } |
| 4298 | return thread; |
| 4299 | } |
| 4300 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4301 | static void binder_free_proc(struct binder_proc *proc) |
| 4302 | { |
| 4303 | BUG_ON(!list_empty(&proc->todo)); |
| 4304 | BUG_ON(!list_empty(&proc->delivered_death)); |
| 4305 | binder_alloc_deferred_release(&proc->alloc); |
| 4306 | put_task_struct(proc->tsk); |
| 4307 | binder_stats_deleted(BINDER_STAT_PROC); |
| 4308 | kfree(proc); |
| 4309 | } |
| 4310 | |
| 4311 | static void binder_free_thread(struct binder_thread *thread) |
| 4312 | { |
| 4313 | BUG_ON(!list_empty(&thread->todo)); |
| 4314 | binder_stats_deleted(BINDER_STAT_THREAD); |
| 4315 | binder_proc_dec_tmpref(thread->proc); |
| 4316 | kfree(thread); |
| 4317 | } |
| 4318 | |
| 4319 | static int binder_thread_release(struct binder_proc *proc, |
| 4320 | struct binder_thread *thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4321 | { |
| 4322 | struct binder_transaction *t; |
| 4323 | struct binder_transaction *send_reply = NULL; |
| 4324 | int active_transactions = 0; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4325 | struct binder_transaction *last_t = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4326 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4327 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4328 | /* |
| 4329 | * take a ref on the proc so it survives |
| 4330 | * after we remove this thread from proc->threads. |
| 4331 | * The corresponding dec is when we actually |
| 4332 | * free the thread in binder_free_thread() |
| 4333 | */ |
| 4334 | proc->tmp_ref++; |
| 4335 | /* |
| 4336 | * take a ref on this thread to ensure it |
| 4337 | * survives while we are releasing it |
| 4338 | */ |
| 4339 | atomic_inc(&thread->tmp_ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4340 | rb_erase(&thread->rb_node, &proc->threads); |
| 4341 | t = thread->transaction_stack; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4342 | if (t) { |
| 4343 | spin_lock(&t->lock); |
| 4344 | if (t->to_thread == thread) |
| 4345 | send_reply = t; |
| 4346 | } |
| 4347 | thread->is_dead = true; |
| 4348 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4349 | while (t) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4350 | last_t = t; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4351 | active_transactions++; |
| 4352 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4353 | "release %d:%d transaction %d %s, still active\n", |
| 4354 | proc->pid, thread->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4355 | t->debug_id, |
| 4356 | (t->to_thread == thread) ? "in" : "out"); |
| 4357 | |
| 4358 | if (t->to_thread == thread) { |
| 4359 | t->to_proc = NULL; |
| 4360 | t->to_thread = NULL; |
| 4361 | if (t->buffer) { |
| 4362 | t->buffer->transaction = NULL; |
| 4363 | t->buffer = NULL; |
| 4364 | } |
| 4365 | t = t->to_parent; |
| 4366 | } else if (t->from == thread) { |
| 4367 | t->from = NULL; |
| 4368 | t = t->from_parent; |
| 4369 | } else |
| 4370 | BUG(); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4371 | spin_unlock(&last_t->lock); |
| 4372 | if (t) |
| 4373 | spin_lock(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4374 | } |
Martijn Coenen | f5cb779 | 2018-01-05 11:27:07 +0100 | [diff] [blame] | 4375 | |
| 4376 | /* |
| 4377 | * If this thread used poll, make sure we remove the waitqueue |
| 4378 | * from any epoll data structures holding it with POLLFREE. |
| 4379 | * waitqueue_active() is safe to use here because we're holding |
| 4380 | * the inner lock. |
| 4381 | */ |
| 4382 | if ((thread->looper & BINDER_LOOPER_STATE_POLL) && |
| 4383 | waitqueue_active(&thread->wait)) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 4384 | wake_up_poll(&thread->wait, EPOLLHUP | POLLFREE); |
Martijn Coenen | f5cb779 | 2018-01-05 11:27:07 +0100 | [diff] [blame] | 4385 | } |
| 4386 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4387 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4388 | |
Martijn Coenen | 5eeb2ca | 2018-02-16 09:47:15 +0100 | [diff] [blame] | 4389 | /* |
| 4390 | * This is needed to avoid races between wake_up_poll() above and |
| 4391 | * and ep_remove_waitqueue() called for other reasons (eg the epoll file |
| 4392 | * descriptor being closed); ep_remove_waitqueue() holds an RCU read |
| 4393 | * lock, so we can be sure it's done after calling synchronize_rcu(). |
| 4394 | */ |
| 4395 | if (thread->looper & BINDER_LOOPER_STATE_POLL) |
| 4396 | synchronize_rcu(); |
| 4397 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4398 | if (send_reply) |
| 4399 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4400 | binder_release_work(proc, &thread->todo); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4401 | binder_thread_dec_tmpref(thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4402 | return active_transactions; |
| 4403 | } |
| 4404 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 4405 | static __poll_t binder_poll(struct file *filp, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4406 | struct poll_table_struct *wait) |
| 4407 | { |
| 4408 | struct binder_proc *proc = filp->private_data; |
| 4409 | struct binder_thread *thread = NULL; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4410 | bool wait_for_proc_work; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4411 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4412 | thread = binder_get_thread(proc); |
Eric Biggers | f889826 | 2018-01-30 23:11:24 -0800 | [diff] [blame] | 4413 | if (!thread) |
| 4414 | return POLLERR; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4415 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4416 | binder_inner_proc_lock(thread->proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4417 | thread->looper |= BINDER_LOOPER_STATE_POLL; |
| 4418 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
| 4419 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4420 | binder_inner_proc_unlock(thread->proc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4421 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4422 | poll_wait(filp, &thread->wait, wait); |
| 4423 | |
Martijn Coenen | 66b83a4 | 2017-10-09 14:26:56 +0200 | [diff] [blame] | 4424 | if (binder_has_work(thread, wait_for_proc_work)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 4425 | return EPOLLIN; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4426 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4427 | return 0; |
| 4428 | } |
| 4429 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4430 | static int binder_ioctl_write_read(struct file *filp, |
| 4431 | unsigned int cmd, unsigned long arg, |
| 4432 | struct binder_thread *thread) |
| 4433 | { |
| 4434 | int ret = 0; |
| 4435 | struct binder_proc *proc = filp->private_data; |
| 4436 | unsigned int size = _IOC_SIZE(cmd); |
| 4437 | void __user *ubuf = (void __user *)arg; |
| 4438 | struct binder_write_read bwr; |
| 4439 | |
| 4440 | if (size != sizeof(struct binder_write_read)) { |
| 4441 | ret = -EINVAL; |
| 4442 | goto out; |
| 4443 | } |
| 4444 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { |
| 4445 | ret = -EFAULT; |
| 4446 | goto out; |
| 4447 | } |
| 4448 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4449 | "%d:%d write %lld at %016llx, read %lld at %016llx\n", |
| 4450 | proc->pid, thread->pid, |
| 4451 | (u64)bwr.write_size, (u64)bwr.write_buffer, |
| 4452 | (u64)bwr.read_size, (u64)bwr.read_buffer); |
| 4453 | |
| 4454 | if (bwr.write_size > 0) { |
| 4455 | ret = binder_thread_write(proc, thread, |
| 4456 | bwr.write_buffer, |
| 4457 | bwr.write_size, |
| 4458 | &bwr.write_consumed); |
| 4459 | trace_binder_write_done(ret); |
| 4460 | if (ret < 0) { |
| 4461 | bwr.read_consumed = 0; |
| 4462 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4463 | ret = -EFAULT; |
| 4464 | goto out; |
| 4465 | } |
| 4466 | } |
| 4467 | if (bwr.read_size > 0) { |
| 4468 | ret = binder_thread_read(proc, thread, bwr.read_buffer, |
| 4469 | bwr.read_size, |
| 4470 | &bwr.read_consumed, |
| 4471 | filp->f_flags & O_NONBLOCK); |
| 4472 | trace_binder_read_done(ret); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4473 | binder_inner_proc_lock(proc); |
| 4474 | if (!binder_worklist_empty_ilocked(&proc->todo)) |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 4475 | binder_wakeup_proc_ilocked(proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4476 | binder_inner_proc_unlock(proc); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4477 | if (ret < 0) { |
| 4478 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4479 | ret = -EFAULT; |
| 4480 | goto out; |
| 4481 | } |
| 4482 | } |
| 4483 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4484 | "%d:%d wrote %lld of %lld, read return %lld of %lld\n", |
| 4485 | proc->pid, thread->pid, |
| 4486 | (u64)bwr.write_consumed, (u64)bwr.write_size, |
| 4487 | (u64)bwr.read_consumed, (u64)bwr.read_size); |
| 4488 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { |
| 4489 | ret = -EFAULT; |
| 4490 | goto out; |
| 4491 | } |
| 4492 | out: |
| 4493 | return ret; |
| 4494 | } |
| 4495 | |
| 4496 | static int binder_ioctl_set_ctx_mgr(struct file *filp) |
| 4497 | { |
| 4498 | int ret = 0; |
| 4499 | struct binder_proc *proc = filp->private_data; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4500 | struct binder_context *context = proc->context; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4501 | struct binder_node *new_node; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4502 | kuid_t curr_euid = current_euid(); |
| 4503 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4504 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4505 | if (context->binder_context_mgr_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4506 | pr_err("BINDER_SET_CONTEXT_MGR already set\n"); |
| 4507 | ret = -EBUSY; |
| 4508 | goto out; |
| 4509 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 4510 | ret = security_binder_set_context_mgr(proc->tsk); |
| 4511 | if (ret < 0) |
| 4512 | goto out; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4513 | if (uid_valid(context->binder_context_mgr_uid)) { |
| 4514 | if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4515 | pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n", |
| 4516 | from_kuid(&init_user_ns, curr_euid), |
| 4517 | from_kuid(&init_user_ns, |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4518 | context->binder_context_mgr_uid)); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4519 | ret = -EPERM; |
| 4520 | goto out; |
| 4521 | } |
| 4522 | } else { |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4523 | context->binder_context_mgr_uid = curr_euid; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4524 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4525 | new_node = binder_new_node(proc, NULL); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4526 | if (!new_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4527 | ret = -ENOMEM; |
| 4528 | goto out; |
| 4529 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4530 | binder_node_lock(new_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4531 | new_node->local_weak_refs++; |
| 4532 | new_node->local_strong_refs++; |
| 4533 | new_node->has_strong_ref = 1; |
| 4534 | new_node->has_weak_ref = 1; |
| 4535 | context->binder_context_mgr_node = new_node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4536 | binder_node_unlock(new_node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4537 | binder_put_node(new_node); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4538 | out: |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4539 | mutex_unlock(&context->context_mgr_node_lock); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4540 | return ret; |
| 4541 | } |
| 4542 | |
Colin Cross | abcc615 | 2017-08-31 10:04:24 +0200 | [diff] [blame] | 4543 | static int binder_ioctl_get_node_debug_info(struct binder_proc *proc, |
| 4544 | struct binder_node_debug_info *info) |
| 4545 | { |
| 4546 | struct rb_node *n; |
| 4547 | binder_uintptr_t ptr = info->ptr; |
| 4548 | |
| 4549 | memset(info, 0, sizeof(*info)); |
| 4550 | |
| 4551 | binder_inner_proc_lock(proc); |
| 4552 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { |
| 4553 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 4554 | rb_node); |
| 4555 | if (node->ptr > ptr) { |
| 4556 | info->ptr = node->ptr; |
| 4557 | info->cookie = node->cookie; |
| 4558 | info->has_strong_ref = node->has_strong_ref; |
| 4559 | info->has_weak_ref = node->has_weak_ref; |
| 4560 | break; |
| 4561 | } |
| 4562 | } |
| 4563 | binder_inner_proc_unlock(proc); |
| 4564 | |
| 4565 | return 0; |
| 4566 | } |
| 4567 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4568 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 4569 | { |
| 4570 | int ret; |
| 4571 | struct binder_proc *proc = filp->private_data; |
| 4572 | struct binder_thread *thread; |
| 4573 | unsigned int size = _IOC_SIZE(cmd); |
| 4574 | void __user *ubuf = (void __user *)arg; |
| 4575 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4576 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", |
| 4577 | proc->pid, current->pid, cmd, arg);*/ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4578 | |
Sherry Yang | 4175e2b | 2017-08-23 08:46:40 -0700 | [diff] [blame] | 4579 | binder_selftest_alloc(&proc->alloc); |
| 4580 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4581 | trace_binder_ioctl(cmd, arg); |
| 4582 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4583 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4584 | if (ret) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4585 | goto err_unlocked; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4586 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4587 | thread = binder_get_thread(proc); |
| 4588 | if (thread == NULL) { |
| 4589 | ret = -ENOMEM; |
| 4590 | goto err; |
| 4591 | } |
| 4592 | |
| 4593 | switch (cmd) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4594 | case BINDER_WRITE_READ: |
| 4595 | ret = binder_ioctl_write_read(filp, cmd, arg, thread); |
| 4596 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4597 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4598 | break; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4599 | case BINDER_SET_MAX_THREADS: { |
| 4600 | int max_threads; |
| 4601 | |
| 4602 | if (copy_from_user(&max_threads, ubuf, |
| 4603 | sizeof(max_threads))) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4604 | ret = -EINVAL; |
| 4605 | goto err; |
| 4606 | } |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4607 | binder_inner_proc_lock(proc); |
| 4608 | proc->max_threads = max_threads; |
| 4609 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4610 | break; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4611 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4612 | case BINDER_SET_CONTEXT_MGR: |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4613 | ret = binder_ioctl_set_ctx_mgr(filp); |
| 4614 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4615 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4616 | break; |
| 4617 | case BINDER_THREAD_EXIT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4618 | binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4619 | proc->pid, thread->pid); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4620 | binder_thread_release(proc, thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4621 | thread = NULL; |
| 4622 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4623 | case BINDER_VERSION: { |
| 4624 | struct binder_version __user *ver = ubuf; |
| 4625 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4626 | if (size != sizeof(struct binder_version)) { |
| 4627 | ret = -EINVAL; |
| 4628 | goto err; |
| 4629 | } |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4630 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, |
| 4631 | &ver->protocol_version)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4632 | ret = -EINVAL; |
| 4633 | goto err; |
| 4634 | } |
| 4635 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4636 | } |
Colin Cross | abcc615 | 2017-08-31 10:04:24 +0200 | [diff] [blame] | 4637 | case BINDER_GET_NODE_DEBUG_INFO: { |
| 4638 | struct binder_node_debug_info info; |
| 4639 | |
| 4640 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 4641 | ret = -EFAULT; |
| 4642 | goto err; |
| 4643 | } |
| 4644 | |
| 4645 | ret = binder_ioctl_get_node_debug_info(proc, &info); |
| 4646 | if (ret < 0) |
| 4647 | goto err; |
| 4648 | |
| 4649 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 4650 | ret = -EFAULT; |
| 4651 | goto err; |
| 4652 | } |
| 4653 | break; |
| 4654 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4655 | default: |
| 4656 | ret = -EINVAL; |
| 4657 | goto err; |
| 4658 | } |
| 4659 | ret = 0; |
| 4660 | err: |
| 4661 | if (thread) |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 4662 | thread->looper_need_return = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4663 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4664 | if (ret && ret != -ERESTARTSYS) |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4665 | 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] | 4666 | err_unlocked: |
| 4667 | trace_binder_ioctl_done(ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4668 | return ret; |
| 4669 | } |
| 4670 | |
| 4671 | static void binder_vma_open(struct vm_area_struct *vma) |
| 4672 | { |
| 4673 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4674 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4675 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4676 | "%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] | 4677 | proc->pid, vma->vm_start, vma->vm_end, |
| 4678 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4679 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | static void binder_vma_close(struct vm_area_struct *vma) |
| 4683 | { |
| 4684 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4685 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4686 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4687 | "%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] | 4688 | proc->pid, vma->vm_start, vma->vm_end, |
| 4689 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4690 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4691 | binder_alloc_vma_close(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4692 | binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES); |
| 4693 | } |
| 4694 | |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 4695 | static int binder_vm_fault(struct vm_fault *vmf) |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4696 | { |
| 4697 | return VM_FAULT_SIGBUS; |
| 4698 | } |
| 4699 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 4700 | static const struct vm_operations_struct binder_vm_ops = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4701 | .open = binder_vma_open, |
| 4702 | .close = binder_vma_close, |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4703 | .fault = binder_vm_fault, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4704 | }; |
| 4705 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4706 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) |
| 4707 | { |
| 4708 | int ret; |
| 4709 | struct binder_proc *proc = filp->private_data; |
| 4710 | const char *failure_string; |
| 4711 | |
| 4712 | if (proc->tsk != current->group_leader) |
| 4713 | return -EINVAL; |
| 4714 | |
| 4715 | if ((vma->vm_end - vma->vm_start) > SZ_4M) |
| 4716 | vma->vm_end = vma->vm_start + SZ_4M; |
| 4717 | |
| 4718 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 4719 | "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 4720 | __func__, proc->pid, vma->vm_start, vma->vm_end, |
| 4721 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4722 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 4723 | |
| 4724 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { |
| 4725 | ret = -EPERM; |
| 4726 | failure_string = "bad vm_flags"; |
| 4727 | goto err_bad_arg; |
| 4728 | } |
| 4729 | vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE; |
| 4730 | vma->vm_ops = &binder_vm_ops; |
| 4731 | vma->vm_private_data = proc; |
| 4732 | |
| 4733 | ret = binder_alloc_mmap_handler(&proc->alloc, vma); |
| 4734 | if (ret) |
| 4735 | return ret; |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 4736 | mutex_lock(&proc->files_lock); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4737 | proc->files = get_files_struct(current); |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 4738 | mutex_unlock(&proc->files_lock); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4739 | return 0; |
| 4740 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4741 | err_bad_arg: |
Elad Wexler | 00c41cd | 2017-12-29 11:03:37 +0200 | [diff] [blame] | 4742 | pr_err("%s: %d %lx-%lx %s failed %d\n", __func__, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4743 | proc->pid, vma->vm_start, vma->vm_end, failure_string, ret); |
| 4744 | return ret; |
| 4745 | } |
| 4746 | |
| 4747 | static int binder_open(struct inode *nodp, struct file *filp) |
| 4748 | { |
| 4749 | struct binder_proc *proc; |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 4750 | struct binder_device *binder_dev; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4751 | |
Elad Wexler | 00c41cd | 2017-12-29 11:03:37 +0200 | [diff] [blame] | 4752 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4753 | current->group_leader->pid, current->pid); |
| 4754 | |
| 4755 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 4756 | if (proc == NULL) |
| 4757 | return -ENOMEM; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 4758 | spin_lock_init(&proc->inner_lock); |
| 4759 | spin_lock_init(&proc->outer_lock); |
Todd Kjos | c4ea41b | 2017-06-29 12:01:36 -0700 | [diff] [blame] | 4760 | get_task_struct(current->group_leader); |
| 4761 | proc->tsk = current->group_leader; |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 4762 | mutex_init(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4763 | INIT_LIST_HEAD(&proc->todo); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4764 | proc->default_priority = task_nice(current); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 4765 | binder_dev = container_of(filp->private_data, struct binder_device, |
| 4766 | miscdev); |
| 4767 | proc->context = &binder_dev->context; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4768 | binder_alloc_init(&proc->alloc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4769 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4770 | binder_stats_created(BINDER_STAT_PROC); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4771 | proc->pid = current->group_leader->pid; |
| 4772 | INIT_LIST_HEAD(&proc->delivered_death); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4773 | INIT_LIST_HEAD(&proc->waiting_threads); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4774 | filp->private_data = proc; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4775 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4776 | mutex_lock(&binder_procs_lock); |
| 4777 | hlist_add_head(&proc->proc_node, &binder_procs); |
| 4778 | mutex_unlock(&binder_procs_lock); |
| 4779 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4780 | if (binder_debugfs_dir_entry_proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4781 | char strbuf[11]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4782 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4783 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 4784 | /* |
| 4785 | * proc debug entries are shared between contexts, so |
| 4786 | * this will fail if the process tries to open the driver |
| 4787 | * again with a different context. The priting code will |
| 4788 | * anyway print all contexts that a given PID has, so this |
| 4789 | * is not a problem. |
| 4790 | */ |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 4791 | proc->debugfs_entry = debugfs_create_file(strbuf, 0444, |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 4792 | binder_debugfs_dir_entry_proc, |
| 4793 | (void *)(unsigned long)proc->pid, |
| 4794 | &binder_proc_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4795 | } |
| 4796 | |
| 4797 | return 0; |
| 4798 | } |
| 4799 | |
| 4800 | static int binder_flush(struct file *filp, fl_owner_t id) |
| 4801 | { |
| 4802 | struct binder_proc *proc = filp->private_data; |
| 4803 | |
| 4804 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); |
| 4805 | |
| 4806 | return 0; |
| 4807 | } |
| 4808 | |
| 4809 | static void binder_deferred_flush(struct binder_proc *proc) |
| 4810 | { |
| 4811 | struct rb_node *n; |
| 4812 | int wake_count = 0; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4813 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4814 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4815 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 4816 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4817 | |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 4818 | thread->looper_need_return = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4819 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { |
| 4820 | wake_up_interruptible(&thread->wait); |
| 4821 | wake_count++; |
| 4822 | } |
| 4823 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4824 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4825 | |
| 4826 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 4827 | "binder_flush: %d woke %d threads\n", proc->pid, |
| 4828 | wake_count); |
| 4829 | } |
| 4830 | |
| 4831 | static int binder_release(struct inode *nodp, struct file *filp) |
| 4832 | { |
| 4833 | struct binder_proc *proc = filp->private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4834 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4835 | debugfs_remove(proc->debugfs_entry); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4836 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); |
| 4837 | |
| 4838 | return 0; |
| 4839 | } |
| 4840 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4841 | static int binder_node_release(struct binder_node *node, int refs) |
| 4842 | { |
| 4843 | struct binder_ref *ref; |
| 4844 | int death = 0; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4845 | struct binder_proc *proc = node->proc; |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4846 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4847 | binder_release_work(proc, &node->async_todo); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4848 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4849 | binder_node_lock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4850 | binder_inner_proc_lock(proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4851 | binder_dequeue_work_ilocked(&node->work); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4852 | /* |
| 4853 | * The caller must have taken a temporary ref on the node, |
| 4854 | */ |
| 4855 | BUG_ON(!node->tmp_refs); |
| 4856 | if (hlist_empty(&node->refs) && node->tmp_refs == 1) { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4857 | binder_inner_proc_unlock(proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4858 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4859 | binder_free_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4860 | |
| 4861 | return refs; |
| 4862 | } |
| 4863 | |
| 4864 | node->proc = NULL; |
| 4865 | node->local_strong_refs = 0; |
| 4866 | node->local_weak_refs = 0; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4867 | binder_inner_proc_unlock(proc); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4868 | |
| 4869 | spin_lock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4870 | hlist_add_head(&node->dead_node, &binder_dead_nodes); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4871 | spin_unlock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4872 | |
| 4873 | hlist_for_each_entry(ref, &node->refs, node_entry) { |
| 4874 | refs++; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4875 | /* |
| 4876 | * Need the node lock to synchronize |
| 4877 | * with new notification requests and the |
| 4878 | * inner lock to synchronize with queued |
| 4879 | * death notifications. |
| 4880 | */ |
| 4881 | binder_inner_proc_lock(ref->proc); |
| 4882 | if (!ref->death) { |
| 4883 | binder_inner_proc_unlock(ref->proc); |
Arve Hjønnevåg | e194fd8 | 2014-02-17 13:58:29 -0800 | [diff] [blame] | 4884 | continue; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4885 | } |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4886 | |
| 4887 | death++; |
| 4888 | |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4889 | BUG_ON(!list_empty(&ref->death->work.entry)); |
| 4890 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 4891 | binder_enqueue_work_ilocked(&ref->death->work, |
| 4892 | &ref->proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 4893 | binder_wakeup_proc_ilocked(ref->proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4894 | binder_inner_proc_unlock(ref->proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4895 | } |
| 4896 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4897 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 4898 | "node %d now dead, refs %d, death %d\n", |
| 4899 | node->debug_id, refs, death); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4900 | binder_node_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4901 | binder_put_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4902 | |
| 4903 | return refs; |
| 4904 | } |
| 4905 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4906 | static void binder_deferred_release(struct binder_proc *proc) |
| 4907 | { |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4908 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4909 | struct rb_node *n; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4910 | int threads, nodes, incoming_refs, outgoing_refs, active_transactions; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4911 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4912 | BUG_ON(proc->files); |
| 4913 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4914 | mutex_lock(&binder_procs_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4915 | hlist_del(&proc->proc_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4916 | mutex_unlock(&binder_procs_lock); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4917 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4918 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4919 | if (context->binder_context_mgr_node && |
| 4920 | context->binder_context_mgr_node->proc == proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4921 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 4922 | "%s: %d context_mgr_node gone\n", |
| 4923 | __func__, proc->pid); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4924 | context->binder_context_mgr_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4925 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4926 | mutex_unlock(&context->context_mgr_node_lock); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4927 | binder_inner_proc_lock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4928 | /* |
| 4929 | * Make sure proc stays alive after we |
| 4930 | * remove all the threads |
| 4931 | */ |
| 4932 | proc->tmp_ref++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4933 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4934 | proc->is_dead = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4935 | threads = 0; |
| 4936 | active_transactions = 0; |
| 4937 | while ((n = rb_first(&proc->threads))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4938 | struct binder_thread *thread; |
| 4939 | |
| 4940 | thread = rb_entry(n, struct binder_thread, rb_node); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4941 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4942 | threads++; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4943 | active_transactions += binder_thread_release(proc, thread); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4944 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4945 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4946 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4947 | nodes = 0; |
| 4948 | incoming_refs = 0; |
| 4949 | while ((n = rb_first(&proc->nodes))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4950 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4951 | |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4952 | node = rb_entry(n, struct binder_node, rb_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4953 | nodes++; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4954 | /* |
| 4955 | * take a temporary ref on the node before |
| 4956 | * calling binder_node_release() which will either |
| 4957 | * kfree() the node or call binder_put_node() |
| 4958 | */ |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4959 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4960 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4961 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4962 | incoming_refs = binder_node_release(node, incoming_refs); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4963 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4964 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4965 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4966 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4967 | outgoing_refs = 0; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4968 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4969 | while ((n = rb_first(&proc->refs_by_desc))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4970 | struct binder_ref *ref; |
| 4971 | |
| 4972 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4973 | outgoing_refs++; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4974 | binder_cleanup_ref_olocked(ref); |
| 4975 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 4976 | binder_free_ref(ref); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4977 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4978 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4979 | binder_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4980 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4981 | binder_release_work(proc, &proc->todo); |
| 4982 | binder_release_work(proc, &proc->delivered_death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4983 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4984 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4985 | "%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] | 4986 | __func__, proc->pid, threads, nodes, incoming_refs, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4987 | outgoing_refs, active_transactions); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4988 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4989 | binder_proc_dec_tmpref(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4990 | } |
| 4991 | |
| 4992 | static void binder_deferred_func(struct work_struct *work) |
| 4993 | { |
| 4994 | struct binder_proc *proc; |
| 4995 | struct files_struct *files; |
| 4996 | |
| 4997 | int defer; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4998 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4999 | do { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5000 | mutex_lock(&binder_deferred_lock); |
| 5001 | if (!hlist_empty(&binder_deferred_list)) { |
| 5002 | proc = hlist_entry(binder_deferred_list.first, |
| 5003 | struct binder_proc, deferred_work_node); |
| 5004 | hlist_del_init(&proc->deferred_work_node); |
| 5005 | defer = proc->deferred_work; |
| 5006 | proc->deferred_work = 0; |
| 5007 | } else { |
| 5008 | proc = NULL; |
| 5009 | defer = 0; |
| 5010 | } |
| 5011 | mutex_unlock(&binder_deferred_lock); |
| 5012 | |
| 5013 | files = NULL; |
| 5014 | if (defer & BINDER_DEFERRED_PUT_FILES) { |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5015 | mutex_lock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5016 | files = proc->files; |
| 5017 | if (files) |
| 5018 | proc->files = NULL; |
Todd Kjos | 7f3dc00 | 2017-11-27 09:32:33 -0800 | [diff] [blame] | 5019 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | if (defer & BINDER_DEFERRED_FLUSH) |
| 5023 | binder_deferred_flush(proc); |
| 5024 | |
| 5025 | if (defer & BINDER_DEFERRED_RELEASE) |
| 5026 | binder_deferred_release(proc); /* frees proc */ |
| 5027 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5028 | if (files) |
| 5029 | put_files_struct(files); |
| 5030 | } while (proc); |
| 5031 | } |
| 5032 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); |
| 5033 | |
| 5034 | static void |
| 5035 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) |
| 5036 | { |
| 5037 | mutex_lock(&binder_deferred_lock); |
| 5038 | proc->deferred_work |= defer; |
| 5039 | if (hlist_unhashed(&proc->deferred_work_node)) { |
| 5040 | hlist_add_head(&proc->deferred_work_node, |
| 5041 | &binder_deferred_list); |
Bhaktipriya Shridhar | 1beba52 | 2016-08-13 22:16:24 +0530 | [diff] [blame] | 5042 | schedule_work(&binder_deferred_work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5043 | } |
| 5044 | mutex_unlock(&binder_deferred_lock); |
| 5045 | } |
| 5046 | |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5047 | static void print_binder_transaction_ilocked(struct seq_file *m, |
| 5048 | struct binder_proc *proc, |
| 5049 | const char *prefix, |
| 5050 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5051 | { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5052 | struct binder_proc *to_proc; |
| 5053 | struct binder_buffer *buffer = t->buffer; |
| 5054 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5055 | spin_lock(&t->lock); |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5056 | to_proc = t->to_proc; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5057 | seq_printf(m, |
Todd Kjos | 8ca86f1 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 5058 | "%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] | 5059 | prefix, t->debug_id, t, |
| 5060 | t->from ? t->from->proc->pid : 0, |
| 5061 | t->from ? t->from->pid : 0, |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5062 | to_proc ? to_proc->pid : 0, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5063 | t->to_thread ? t->to_thread->pid : 0, |
| 5064 | t->code, t->flags, t->priority, t->need_reply); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5065 | spin_unlock(&t->lock); |
| 5066 | |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5067 | if (proc != to_proc) { |
| 5068 | /* |
| 5069 | * Can only safely deref buffer if we are holding the |
| 5070 | * correct proc inner lock for this node |
| 5071 | */ |
| 5072 | seq_puts(m, "\n"); |
| 5073 | return; |
| 5074 | } |
| 5075 | |
| 5076 | if (buffer == NULL) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5077 | seq_puts(m, " buffer free\n"); |
| 5078 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5079 | } |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5080 | if (buffer->target_node) |
| 5081 | seq_printf(m, " node %d", buffer->target_node->debug_id); |
Todd Kjos | 8ca86f1 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 5082 | seq_printf(m, " size %zd:%zd data %pK\n", |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5083 | buffer->data_size, buffer->offsets_size, |
| 5084 | buffer->data); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5085 | } |
| 5086 | |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5087 | static void print_binder_work_ilocked(struct seq_file *m, |
| 5088 | struct binder_proc *proc, |
| 5089 | const char *prefix, |
| 5090 | const char *transaction_prefix, |
| 5091 | struct binder_work *w) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5092 | { |
| 5093 | struct binder_node *node; |
| 5094 | struct binder_transaction *t; |
| 5095 | |
| 5096 | switch (w->type) { |
| 5097 | case BINDER_WORK_TRANSACTION: |
| 5098 | t = container_of(w, struct binder_transaction, work); |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5099 | print_binder_transaction_ilocked( |
| 5100 | m, proc, transaction_prefix, t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5101 | break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 5102 | case BINDER_WORK_RETURN_ERROR: { |
| 5103 | struct binder_error *e = container_of( |
| 5104 | w, struct binder_error, work); |
| 5105 | |
| 5106 | seq_printf(m, "%stransaction error: %u\n", |
| 5107 | prefix, e->cmd); |
| 5108 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5109 | case BINDER_WORK_TRANSACTION_COMPLETE: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5110 | seq_printf(m, "%stransaction complete\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5111 | break; |
| 5112 | case BINDER_WORK_NODE: |
| 5113 | node = container_of(w, struct binder_node, work); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5114 | seq_printf(m, "%snode work %d: u%016llx c%016llx\n", |
| 5115 | prefix, node->debug_id, |
| 5116 | (u64)node->ptr, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5117 | break; |
| 5118 | case BINDER_WORK_DEAD_BINDER: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5119 | seq_printf(m, "%shas dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5120 | break; |
| 5121 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5122 | seq_printf(m, "%shas cleared dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5123 | break; |
| 5124 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5125 | seq_printf(m, "%shas cleared death notification\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5126 | break; |
| 5127 | default: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5128 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5129 | break; |
| 5130 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5131 | } |
| 5132 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5133 | static void print_binder_thread_ilocked(struct seq_file *m, |
| 5134 | struct binder_thread *thread, |
| 5135 | int print_always) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5136 | { |
| 5137 | struct binder_transaction *t; |
| 5138 | struct binder_work *w; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5139 | size_t start_pos = m->count; |
| 5140 | size_t header_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5141 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5142 | 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] | 5143 | thread->pid, thread->looper, |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5144 | thread->looper_need_return, |
| 5145 | atomic_read(&thread->tmp_ref)); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5146 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5147 | t = thread->transaction_stack; |
| 5148 | while (t) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5149 | if (t->from == thread) { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5150 | print_binder_transaction_ilocked(m, thread->proc, |
| 5151 | " outgoing transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5152 | t = t->from_parent; |
| 5153 | } else if (t->to_thread == thread) { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5154 | print_binder_transaction_ilocked(m, thread->proc, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5155 | " incoming transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5156 | t = t->to_parent; |
| 5157 | } else { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5158 | print_binder_transaction_ilocked(m, thread->proc, |
| 5159 | " bad transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5160 | t = NULL; |
| 5161 | } |
| 5162 | } |
| 5163 | list_for_each_entry(w, &thread->todo, entry) { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5164 | print_binder_work_ilocked(m, thread->proc, " ", |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5165 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5166 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5167 | if (!print_always && m->count == header_pos) |
| 5168 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5169 | } |
| 5170 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5171 | static void print_binder_node_nilocked(struct seq_file *m, |
| 5172 | struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5173 | { |
| 5174 | struct binder_ref *ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5175 | struct binder_work *w; |
| 5176 | int count; |
| 5177 | |
| 5178 | count = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5179 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5180 | count++; |
| 5181 | |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5182 | 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] | 5183 | node->debug_id, (u64)node->ptr, (u64)node->cookie, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5184 | node->has_strong_ref, node->has_weak_ref, |
| 5185 | node->local_strong_refs, node->local_weak_refs, |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5186 | node->internal_strong_refs, count, node->tmp_refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5187 | if (count) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5188 | seq_puts(m, " proc"); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5189 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5190 | seq_printf(m, " %d", ref->proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5191 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5192 | seq_puts(m, "\n"); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5193 | if (node->proc) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5194 | list_for_each_entry(w, &node->async_todo, entry) |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5195 | print_binder_work_ilocked(m, node->proc, " ", |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5196 | " pending async transaction", w); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5197 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5198 | } |
| 5199 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5200 | static void print_binder_ref_olocked(struct seq_file *m, |
| 5201 | struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5202 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5203 | binder_node_lock(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 5204 | seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n", |
| 5205 | ref->data.debug_id, ref->data.desc, |
| 5206 | ref->node->proc ? "" : "dead ", |
| 5207 | ref->node->debug_id, ref->data.strong, |
| 5208 | ref->data.weak, ref->death); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5209 | binder_node_unlock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5210 | } |
| 5211 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5212 | static void print_binder_proc(struct seq_file *m, |
| 5213 | struct binder_proc *proc, int print_all) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5214 | { |
| 5215 | struct binder_work *w; |
| 5216 | struct rb_node *n; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5217 | size_t start_pos = m->count; |
| 5218 | size_t header_pos; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5219 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5220 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5221 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5222 | seq_printf(m, "context %s\n", proc->context->name); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5223 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5224 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5225 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5226 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5227 | 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] | 5228 | rb_node), print_all); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5229 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5230 | 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] | 5231 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 5232 | rb_node); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5233 | /* |
| 5234 | * take a temporary reference on the node so it |
| 5235 | * survives and isn't removed from the tree |
| 5236 | * while we print it. |
| 5237 | */ |
| 5238 | binder_inc_node_tmpref_ilocked(node); |
| 5239 | /* Need to drop inner lock to take node lock */ |
| 5240 | binder_inner_proc_unlock(proc); |
| 5241 | if (last_node) |
| 5242 | binder_put_node(last_node); |
| 5243 | binder_node_inner_lock(node); |
| 5244 | print_binder_node_nilocked(m, node); |
| 5245 | binder_node_inner_unlock(node); |
| 5246 | last_node = node; |
| 5247 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5248 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5249 | binder_inner_proc_unlock(proc); |
| 5250 | if (last_node) |
| 5251 | binder_put_node(last_node); |
| 5252 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5253 | if (print_all) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5254 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5255 | for (n = rb_first(&proc->refs_by_desc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5256 | n != NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5257 | n = rb_next(n)) |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5258 | print_binder_ref_olocked(m, rb_entry(n, |
| 5259 | struct binder_ref, |
| 5260 | rb_node_desc)); |
| 5261 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5262 | } |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5263 | binder_alloc_print_allocated(m, &proc->alloc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5264 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5265 | list_for_each_entry(w, &proc->todo, entry) |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5266 | print_binder_work_ilocked(m, proc, " ", |
| 5267 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5268 | list_for_each_entry(w, &proc->delivered_death, entry) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5269 | seq_puts(m, " has delivered dead binder\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5270 | break; |
| 5271 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5272 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5273 | if (!print_all && m->count == header_pos) |
| 5274 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5275 | } |
| 5276 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5277 | static const char * const binder_return_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5278 | "BR_ERROR", |
| 5279 | "BR_OK", |
| 5280 | "BR_TRANSACTION", |
| 5281 | "BR_REPLY", |
| 5282 | "BR_ACQUIRE_RESULT", |
| 5283 | "BR_DEAD_REPLY", |
| 5284 | "BR_TRANSACTION_COMPLETE", |
| 5285 | "BR_INCREFS", |
| 5286 | "BR_ACQUIRE", |
| 5287 | "BR_RELEASE", |
| 5288 | "BR_DECREFS", |
| 5289 | "BR_ATTEMPT_ACQUIRE", |
| 5290 | "BR_NOOP", |
| 5291 | "BR_SPAWN_LOOPER", |
| 5292 | "BR_FINISHED", |
| 5293 | "BR_DEAD_BINDER", |
| 5294 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 5295 | "BR_FAILED_REPLY" |
| 5296 | }; |
| 5297 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5298 | static const char * const binder_command_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5299 | "BC_TRANSACTION", |
| 5300 | "BC_REPLY", |
| 5301 | "BC_ACQUIRE_RESULT", |
| 5302 | "BC_FREE_BUFFER", |
| 5303 | "BC_INCREFS", |
| 5304 | "BC_ACQUIRE", |
| 5305 | "BC_RELEASE", |
| 5306 | "BC_DECREFS", |
| 5307 | "BC_INCREFS_DONE", |
| 5308 | "BC_ACQUIRE_DONE", |
| 5309 | "BC_ATTEMPT_ACQUIRE", |
| 5310 | "BC_REGISTER_LOOPER", |
| 5311 | "BC_ENTER_LOOPER", |
| 5312 | "BC_EXIT_LOOPER", |
| 5313 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 5314 | "BC_CLEAR_DEATH_NOTIFICATION", |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 5315 | "BC_DEAD_BINDER_DONE", |
| 5316 | "BC_TRANSACTION_SG", |
| 5317 | "BC_REPLY_SG", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5318 | }; |
| 5319 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 5320 | static const char * const binder_objstat_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5321 | "proc", |
| 5322 | "thread", |
| 5323 | "node", |
| 5324 | "ref", |
| 5325 | "death", |
| 5326 | "transaction", |
| 5327 | "transaction_complete" |
| 5328 | }; |
| 5329 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5330 | static void print_binder_stats(struct seq_file *m, const char *prefix, |
| 5331 | struct binder_stats *stats) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5332 | { |
| 5333 | int i; |
| 5334 | |
| 5335 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5336 | ARRAY_SIZE(binder_command_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5337 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5338 | int temp = atomic_read(&stats->bc[i]); |
| 5339 | |
| 5340 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5341 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5342 | binder_command_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5343 | } |
| 5344 | |
| 5345 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5346 | ARRAY_SIZE(binder_return_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5347 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5348 | int temp = atomic_read(&stats->br[i]); |
| 5349 | |
| 5350 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5351 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5352 | binder_return_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5353 | } |
| 5354 | |
| 5355 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5356 | ARRAY_SIZE(binder_objstat_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5357 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5358 | ARRAY_SIZE(stats->obj_deleted)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5359 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5360 | int created = atomic_read(&stats->obj_created[i]); |
| 5361 | int deleted = atomic_read(&stats->obj_deleted[i]); |
| 5362 | |
| 5363 | if (created || deleted) |
| 5364 | seq_printf(m, "%s%s: active %d total %d\n", |
| 5365 | prefix, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5366 | binder_objstat_strings[i], |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5367 | created - deleted, |
| 5368 | created); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5369 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5370 | } |
| 5371 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5372 | static void print_binder_proc_stats(struct seq_file *m, |
| 5373 | struct binder_proc *proc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5374 | { |
| 5375 | struct binder_work *w; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5376 | struct binder_thread *thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5377 | struct rb_node *n; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5378 | int count, strong, weak, ready_threads; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5379 | size_t free_async_space = |
| 5380 | binder_alloc_get_free_async_space(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5381 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5382 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5383 | seq_printf(m, "context %s\n", proc->context->name); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5384 | count = 0; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5385 | ready_threads = 0; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5386 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5387 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 5388 | count++; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5389 | |
| 5390 | list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node) |
| 5391 | ready_threads++; |
| 5392 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5393 | seq_printf(m, " threads: %d\n", count); |
| 5394 | seq_printf(m, " requested threads: %d+%d/%d\n" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5395 | " ready threads %d\n" |
| 5396 | " free async space %zd\n", proc->requested_threads, |
| 5397 | proc->requested_threads_started, proc->max_threads, |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5398 | ready_threads, |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5399 | free_async_space); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5400 | count = 0; |
| 5401 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) |
| 5402 | count++; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5403 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5404 | seq_printf(m, " nodes: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5405 | count = 0; |
| 5406 | strong = 0; |
| 5407 | weak = 0; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5408 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5409 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 5410 | struct binder_ref *ref = rb_entry(n, struct binder_ref, |
| 5411 | rb_node_desc); |
| 5412 | count++; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 5413 | strong += ref->data.strong; |
| 5414 | weak += ref->data.weak; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5415 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5416 | binder_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5417 | 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] | 5418 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5419 | count = binder_alloc_get_allocated_count(&proc->alloc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5420 | seq_printf(m, " buffers: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5421 | |
Sherry Yang | 8ef4665 | 2017-08-31 11:56:36 -0700 | [diff] [blame] | 5422 | binder_alloc_print_pages(m, &proc->alloc); |
| 5423 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5424 | count = 0; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5425 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5426 | list_for_each_entry(w, &proc->todo, entry) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5427 | if (w->type == BINDER_WORK_TRANSACTION) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5428 | count++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5429 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5430 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5431 | seq_printf(m, " pending transactions: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5432 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5433 | print_binder_stats(m, " ", &proc->stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5434 | } |
| 5435 | |
| 5436 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5437 | static int binder_state_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5438 | { |
| 5439 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5440 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5441 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5442 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5443 | seq_puts(m, "binder state:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5444 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5445 | spin_lock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5446 | if (!hlist_empty(&binder_dead_nodes)) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5447 | seq_puts(m, "dead nodes:\n"); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5448 | hlist_for_each_entry(node, &binder_dead_nodes, dead_node) { |
| 5449 | /* |
| 5450 | * take a temporary reference on the node so it |
| 5451 | * survives and isn't removed from the list |
| 5452 | * while we print it. |
| 5453 | */ |
| 5454 | node->tmp_refs++; |
| 5455 | spin_unlock(&binder_dead_nodes_lock); |
| 5456 | if (last_node) |
| 5457 | binder_put_node(last_node); |
| 5458 | binder_node_lock(node); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5459 | print_binder_node_nilocked(m, node); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5460 | binder_node_unlock(node); |
| 5461 | last_node = node; |
| 5462 | spin_lock(&binder_dead_nodes_lock); |
| 5463 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5464 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5465 | if (last_node) |
| 5466 | binder_put_node(last_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5467 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5468 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5469 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5470 | print_binder_proc(m, proc, 1); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5471 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 5472 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5473 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5474 | } |
| 5475 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5476 | static int binder_stats_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5477 | { |
| 5478 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5479 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5480 | seq_puts(m, "binder stats:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5481 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5482 | print_binder_stats(m, "", &binder_stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5483 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5484 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5485 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5486 | print_binder_proc_stats(m, proc); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5487 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 5488 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5489 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5490 | } |
| 5491 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5492 | static int binder_transactions_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5493 | { |
| 5494 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5495 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5496 | seq_puts(m, "binder transactions:\n"); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5497 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5498 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5499 | print_binder_proc(m, proc, 0); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5500 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 5501 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5502 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5503 | } |
| 5504 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5505 | static int binder_proc_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5506 | { |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5507 | struct binder_proc *itr; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5508 | int pid = (unsigned long)m->private; |
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); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5511 | hlist_for_each_entry(itr, &binder_procs, proc_node) { |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5512 | if (itr->pid == pid) { |
| 5513 | seq_puts(m, "binder proc state:\n"); |
| 5514 | print_binder_proc(m, itr, 1); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5515 | } |
| 5516 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5517 | mutex_unlock(&binder_procs_lock); |
| 5518 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5519 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5520 | } |
| 5521 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5522 | static void print_binder_transaction_log_entry(struct seq_file *m, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5523 | struct binder_transaction_log_entry *e) |
| 5524 | { |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5525 | int debug_id = READ_ONCE(e->debug_id_done); |
| 5526 | /* |
| 5527 | * read barrier to guarantee debug_id_done read before |
| 5528 | * we print the log values |
| 5529 | */ |
| 5530 | smp_rmb(); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5531 | seq_printf(m, |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5532 | "%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] | 5533 | e->debug_id, (e->call_type == 2) ? "reply" : |
| 5534 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5535 | e->from_thread, e->to_proc, e->to_thread, e->context_name, |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 5536 | e->to_node, e->target_handle, e->data_size, e->offsets_size, |
| 5537 | e->return_error, e->return_error_param, |
| 5538 | e->return_error_line); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5539 | /* |
| 5540 | * read-barrier to guarantee read of debug_id_done after |
| 5541 | * done printing the fields of the entry |
| 5542 | */ |
| 5543 | smp_rmb(); |
| 5544 | seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ? |
| 5545 | "\n" : " (incomplete)\n"); |
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 int binder_transaction_log_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5549 | { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5550 | struct binder_transaction_log *log = m->private; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5551 | unsigned int log_cur = atomic_read(&log->cur); |
| 5552 | unsigned int count; |
| 5553 | unsigned int cur; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5554 | int i; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5555 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5556 | count = log_cur + 1; |
| 5557 | cur = count < ARRAY_SIZE(log->entry) && !log->full ? |
| 5558 | 0 : count % ARRAY_SIZE(log->entry); |
| 5559 | if (count > ARRAY_SIZE(log->entry) || log->full) |
| 5560 | count = ARRAY_SIZE(log->entry); |
| 5561 | for (i = 0; i < count; i++) { |
| 5562 | unsigned int index = cur++ % ARRAY_SIZE(log->entry); |
| 5563 | |
| 5564 | print_binder_transaction_log_entry(m, &log->entry[index]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5565 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5566 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5567 | } |
| 5568 | |
| 5569 | static const struct file_operations binder_fops = { |
| 5570 | .owner = THIS_MODULE, |
| 5571 | .poll = binder_poll, |
| 5572 | .unlocked_ioctl = binder_ioctl, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5573 | .compat_ioctl = binder_ioctl, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5574 | .mmap = binder_mmap, |
| 5575 | .open = binder_open, |
| 5576 | .flush = binder_flush, |
| 5577 | .release = binder_release, |
| 5578 | }; |
| 5579 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5580 | BINDER_DEBUG_ENTRY(state); |
| 5581 | BINDER_DEBUG_ENTRY(stats); |
| 5582 | BINDER_DEBUG_ENTRY(transactions); |
| 5583 | BINDER_DEBUG_ENTRY(transaction_log); |
| 5584 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5585 | static int __init init_binder_device(const char *name) |
| 5586 | { |
| 5587 | int ret; |
| 5588 | struct binder_device *binder_device; |
| 5589 | |
| 5590 | binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL); |
| 5591 | if (!binder_device) |
| 5592 | return -ENOMEM; |
| 5593 | |
| 5594 | binder_device->miscdev.fops = &binder_fops; |
| 5595 | binder_device->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 5596 | binder_device->miscdev.name = name; |
| 5597 | |
| 5598 | binder_device->context.binder_context_mgr_uid = INVALID_UID; |
| 5599 | binder_device->context.name = name; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5600 | mutex_init(&binder_device->context.context_mgr_node_lock); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5601 | |
| 5602 | ret = misc_register(&binder_device->miscdev); |
| 5603 | if (ret < 0) { |
| 5604 | kfree(binder_device); |
| 5605 | return ret; |
| 5606 | } |
| 5607 | |
| 5608 | hlist_add_head(&binder_device->hlist, &binder_devices); |
| 5609 | |
| 5610 | return ret; |
| 5611 | } |
| 5612 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5613 | static int __init binder_init(void) |
| 5614 | { |
| 5615 | int ret; |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 5616 | char *device_name, *device_names, *device_tmp; |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5617 | struct binder_device *device; |
| 5618 | struct hlist_node *tmp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5619 | |
Tetsuo Handa | 533dfb2 | 2017-11-29 22:29:47 +0900 | [diff] [blame] | 5620 | ret = binder_alloc_shrinker_init(); |
| 5621 | if (ret) |
| 5622 | return ret; |
Sherry Yang | f2517eb | 2017-08-23 08:46:42 -0700 | [diff] [blame] | 5623 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5624 | atomic_set(&binder_transaction_log.cur, ~0U); |
| 5625 | atomic_set(&binder_transaction_log_failed.cur, ~0U); |
| 5626 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5627 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); |
| 5628 | if (binder_debugfs_dir_entry_root) |
| 5629 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", |
| 5630 | binder_debugfs_dir_entry_root); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5631 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5632 | if (binder_debugfs_dir_entry_root) { |
| 5633 | debugfs_create_file("state", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5634 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5635 | binder_debugfs_dir_entry_root, |
| 5636 | NULL, |
| 5637 | &binder_state_fops); |
| 5638 | debugfs_create_file("stats", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5639 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5640 | binder_debugfs_dir_entry_root, |
| 5641 | NULL, |
| 5642 | &binder_stats_fops); |
| 5643 | debugfs_create_file("transactions", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5644 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5645 | binder_debugfs_dir_entry_root, |
| 5646 | NULL, |
| 5647 | &binder_transactions_fops); |
| 5648 | debugfs_create_file("transaction_log", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5649 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5650 | binder_debugfs_dir_entry_root, |
| 5651 | &binder_transaction_log, |
| 5652 | &binder_transaction_log_fops); |
| 5653 | debugfs_create_file("failed_transaction_log", |
Harsh Shandilya | 21d02dd | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5654 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5655 | binder_debugfs_dir_entry_root, |
| 5656 | &binder_transaction_log_failed, |
| 5657 | &binder_transaction_log_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5658 | } |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5659 | |
| 5660 | /* |
| 5661 | * Copy the module_parameter string, because we don't want to |
| 5662 | * tokenize it in-place. |
| 5663 | */ |
| 5664 | device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL); |
| 5665 | if (!device_names) { |
| 5666 | ret = -ENOMEM; |
| 5667 | goto err_alloc_device_names_failed; |
| 5668 | } |
| 5669 | strcpy(device_names, binder_devices_param); |
| 5670 | |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 5671 | device_tmp = device_names; |
| 5672 | while ((device_name = strsep(&device_tmp, ","))) { |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5673 | ret = init_binder_device(device_name); |
| 5674 | if (ret) |
| 5675 | goto err_init_binder_device_failed; |
| 5676 | } |
| 5677 | |
| 5678 | return ret; |
| 5679 | |
| 5680 | err_init_binder_device_failed: |
| 5681 | hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) { |
| 5682 | misc_deregister(&device->miscdev); |
| 5683 | hlist_del(&device->hlist); |
| 5684 | kfree(device); |
| 5685 | } |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 5686 | |
| 5687 | kfree(device_names); |
| 5688 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5689 | err_alloc_device_names_failed: |
| 5690 | debugfs_remove_recursive(binder_debugfs_dir_entry_root); |
| 5691 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5692 | return ret; |
| 5693 | } |
| 5694 | |
| 5695 | device_initcall(binder_init); |
| 5696 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5697 | #define CREATE_TRACE_POINTS |
| 5698 | #include "binder_trace.h" |
| 5699 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5700 | MODULE_LICENSE("GPL v2"); |