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 |
| 31 | * (proc->threads, proc->nodes) and all todo lists associated |
| 32 | * with the binder_proc (proc->todo, thread->todo, |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 33 | * proc->delivered_death and node->async_todo), as well as |
| 34 | * 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 | #ifdef CONFIG_ANDROID_BINDER_IPC_32BIT |
| 76 | #define BINDER_IPC_32BIT 1 |
| 77 | #endif |
| 78 | |
| 79 | #include <uapi/linux/android/binder.h> |
Todd Kjos | 0c972a0 | 2017-06-29 12:01:41 -0700 | [diff] [blame] | 80 | #include "binder_alloc.h" |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 81 | #include "binder_trace.h" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 82 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 83 | static HLIST_HEAD(binder_deferred_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 84 | static DEFINE_MUTEX(binder_deferred_lock); |
| 85 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 86 | static HLIST_HEAD(binder_devices); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 87 | static HLIST_HEAD(binder_procs); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 88 | static DEFINE_MUTEX(binder_procs_lock); |
| 89 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 90 | static HLIST_HEAD(binder_dead_nodes); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 91 | static DEFINE_SPINLOCK(binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 92 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 93 | static struct dentry *binder_debugfs_dir_entry_root; |
| 94 | static struct dentry *binder_debugfs_dir_entry_proc; |
Todd Kjos | 656a800 | 2017-06-29 12:01:45 -0700 | [diff] [blame] | 95 | static atomic_t binder_last_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 96 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 97 | #define BINDER_DEBUG_ENTRY(name) \ |
| 98 | static int binder_##name##_open(struct inode *inode, struct file *file) \ |
| 99 | { \ |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 100 | return single_open(file, binder_##name##_show, inode->i_private); \ |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 101 | } \ |
| 102 | \ |
| 103 | static const struct file_operations binder_##name##_fops = { \ |
| 104 | .owner = THIS_MODULE, \ |
| 105 | .open = binder_##name##_open, \ |
| 106 | .read = seq_read, \ |
| 107 | .llseek = seq_lseek, \ |
| 108 | .release = single_release, \ |
| 109 | } |
| 110 | |
| 111 | static int binder_proc_show(struct seq_file *m, void *unused); |
| 112 | BINDER_DEBUG_ENTRY(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 113 | |
| 114 | /* This is only defined in include/asm-arm/sizes.h */ |
| 115 | #ifndef SZ_1K |
| 116 | #define SZ_1K 0x400 |
| 117 | #endif |
| 118 | |
| 119 | #ifndef SZ_4M |
| 120 | #define SZ_4M 0x400000 |
| 121 | #endif |
| 122 | |
| 123 | #define FORBIDDEN_MMAP_FLAGS (VM_WRITE) |
| 124 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 125 | enum { |
| 126 | BINDER_DEBUG_USER_ERROR = 1U << 0, |
| 127 | BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1, |
| 128 | BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2, |
| 129 | BINDER_DEBUG_OPEN_CLOSE = 1U << 3, |
| 130 | BINDER_DEBUG_DEAD_BINDER = 1U << 4, |
| 131 | BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5, |
| 132 | BINDER_DEBUG_READ_WRITE = 1U << 6, |
| 133 | BINDER_DEBUG_USER_REFS = 1U << 7, |
| 134 | BINDER_DEBUG_THREADS = 1U << 8, |
| 135 | BINDER_DEBUG_TRANSACTION = 1U << 9, |
| 136 | BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10, |
| 137 | BINDER_DEBUG_FREE_BUFFER = 1U << 11, |
| 138 | BINDER_DEBUG_INTERNAL_REFS = 1U << 12, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 139 | BINDER_DEBUG_PRIORITY_CAP = 1U << 13, |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 140 | BINDER_DEBUG_SPINLOCKS = 1U << 14, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 141 | }; |
| 142 | static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR | |
| 143 | BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION; |
| 144 | module_param_named(debug_mask, binder_debug_mask, uint, S_IWUSR | S_IRUGO); |
| 145 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 146 | static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES; |
| 147 | module_param_named(devices, binder_devices_param, charp, 0444); |
| 148 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 149 | static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait); |
| 150 | static int binder_stop_on_user_error; |
| 151 | |
| 152 | static int binder_set_stop_on_user_error(const char *val, |
| 153 | struct kernel_param *kp) |
| 154 | { |
| 155 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 156 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 157 | ret = param_set_int(val, kp); |
| 158 | if (binder_stop_on_user_error < 2) |
| 159 | wake_up(&binder_user_error_wait); |
| 160 | return ret; |
| 161 | } |
| 162 | module_param_call(stop_on_user_error, binder_set_stop_on_user_error, |
| 163 | param_get_int, &binder_stop_on_user_error, S_IWUSR | S_IRUGO); |
| 164 | |
| 165 | #define binder_debug(mask, x...) \ |
| 166 | do { \ |
| 167 | if (binder_debug_mask & mask) \ |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 168 | pr_info(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 169 | } while (0) |
| 170 | |
| 171 | #define binder_user_error(x...) \ |
| 172 | do { \ |
| 173 | if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \ |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 174 | pr_info(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 175 | if (binder_stop_on_user_error) \ |
| 176 | binder_stop_on_user_error = 2; \ |
| 177 | } while (0) |
| 178 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 179 | #define to_flat_binder_object(hdr) \ |
| 180 | container_of(hdr, struct flat_binder_object, hdr) |
| 181 | |
| 182 | #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr) |
| 183 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 184 | #define to_binder_buffer_object(hdr) \ |
| 185 | container_of(hdr, struct binder_buffer_object, hdr) |
| 186 | |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 187 | #define to_binder_fd_array_object(hdr) \ |
| 188 | container_of(hdr, struct binder_fd_array_object, hdr) |
| 189 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 190 | enum binder_stat_types { |
| 191 | BINDER_STAT_PROC, |
| 192 | BINDER_STAT_THREAD, |
| 193 | BINDER_STAT_NODE, |
| 194 | BINDER_STAT_REF, |
| 195 | BINDER_STAT_DEATH, |
| 196 | BINDER_STAT_TRANSACTION, |
| 197 | BINDER_STAT_TRANSACTION_COMPLETE, |
| 198 | BINDER_STAT_COUNT |
| 199 | }; |
| 200 | |
| 201 | struct binder_stats { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 202 | atomic_t br[_IOC_NR(BR_FAILED_REPLY) + 1]; |
| 203 | atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1]; |
| 204 | atomic_t obj_created[BINDER_STAT_COUNT]; |
| 205 | atomic_t obj_deleted[BINDER_STAT_COUNT]; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | static struct binder_stats binder_stats; |
| 209 | |
| 210 | static inline void binder_stats_deleted(enum binder_stat_types type) |
| 211 | { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 212 | atomic_inc(&binder_stats.obj_deleted[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static inline void binder_stats_created(enum binder_stat_types type) |
| 216 | { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 217 | atomic_inc(&binder_stats.obj_created[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | struct binder_transaction_log_entry { |
| 221 | int debug_id; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 222 | int debug_id_done; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 223 | int call_type; |
| 224 | int from_proc; |
| 225 | int from_thread; |
| 226 | int target_handle; |
| 227 | int to_proc; |
| 228 | int to_thread; |
| 229 | int to_node; |
| 230 | int data_size; |
| 231 | int offsets_size; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 232 | int return_error_line; |
| 233 | uint32_t return_error; |
| 234 | uint32_t return_error_param; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 235 | const char *context_name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 236 | }; |
| 237 | struct binder_transaction_log { |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 238 | atomic_t cur; |
| 239 | bool full; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 240 | struct binder_transaction_log_entry entry[32]; |
| 241 | }; |
| 242 | static struct binder_transaction_log binder_transaction_log; |
| 243 | static struct binder_transaction_log binder_transaction_log_failed; |
| 244 | |
| 245 | static struct binder_transaction_log_entry *binder_transaction_log_add( |
| 246 | struct binder_transaction_log *log) |
| 247 | { |
| 248 | struct binder_transaction_log_entry *e; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 249 | unsigned int cur = atomic_inc_return(&log->cur); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 250 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 251 | if (cur >= ARRAY_SIZE(log->entry)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 252 | log->full = 1; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 253 | e = &log->entry[cur % ARRAY_SIZE(log->entry)]; |
| 254 | WRITE_ONCE(e->debug_id_done, 0); |
| 255 | /* |
| 256 | * write-barrier to synchronize access to e->debug_id_done. |
| 257 | * We make sure the initialized 0 value is seen before |
| 258 | * memset() other fields are zeroed by memset. |
| 259 | */ |
| 260 | smp_wmb(); |
| 261 | memset(e, 0, sizeof(*e)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 262 | return e; |
| 263 | } |
| 264 | |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 265 | struct binder_context { |
| 266 | struct binder_node *binder_context_mgr_node; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 267 | struct mutex context_mgr_node_lock; |
| 268 | |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 269 | kuid_t binder_context_mgr_uid; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 270 | const char *name; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 271 | }; |
| 272 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 273 | struct binder_device { |
| 274 | struct hlist_node hlist; |
| 275 | struct miscdevice miscdev; |
| 276 | struct binder_context context; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 277 | }; |
| 278 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 279 | /** |
| 280 | * struct binder_work - work enqueued on a worklist |
| 281 | * @entry: node enqueued on list |
| 282 | * @type: type of work to be performed |
| 283 | * |
| 284 | * There are separate work lists for proc, thread, and node (async). |
| 285 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 286 | struct binder_work { |
| 287 | struct list_head entry; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 288 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 289 | enum { |
| 290 | BINDER_WORK_TRANSACTION = 1, |
| 291 | BINDER_WORK_TRANSACTION_COMPLETE, |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 292 | BINDER_WORK_RETURN_ERROR, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 293 | BINDER_WORK_NODE, |
| 294 | BINDER_WORK_DEAD_BINDER, |
| 295 | BINDER_WORK_DEAD_BINDER_AND_CLEAR, |
| 296 | BINDER_WORK_CLEAR_DEATH_NOTIFICATION, |
| 297 | } type; |
| 298 | }; |
| 299 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 300 | struct binder_error { |
| 301 | struct binder_work work; |
| 302 | uint32_t cmd; |
| 303 | }; |
| 304 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 305 | /** |
| 306 | * struct binder_node - binder node bookkeeping |
| 307 | * @debug_id: unique ID for debugging |
| 308 | * (invariant after initialized) |
| 309 | * @lock: lock for node fields |
| 310 | * @work: worklist element for node work |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 311 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 312 | * @rb_node: element for proc->nodes tree |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 313 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 314 | * @dead_node: element for binder_dead_nodes list |
| 315 | * (protected by binder_dead_nodes_lock) |
| 316 | * @proc: binder_proc that owns this node |
| 317 | * (invariant after initialized) |
| 318 | * @refs: list of references on this node |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 319 | * (protected by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 320 | * @internal_strong_refs: used to take strong references when |
| 321 | * initiating a transaction |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 322 | * (protected by @proc->inner_lock if @proc |
| 323 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 324 | * @local_weak_refs: weak user refs from local process |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 325 | * (protected by @proc->inner_lock if @proc |
| 326 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 327 | * @local_strong_refs: strong user refs from local process |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 328 | * (protected by @proc->inner_lock if @proc |
| 329 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 330 | * @tmp_refs: temporary kernel refs |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 331 | * (protected by @proc->inner_lock while @proc |
| 332 | * is valid, and by binder_dead_nodes_lock |
| 333 | * if @proc is NULL. During inc/dec and node release |
| 334 | * it is also protected by @lock to provide safety |
| 335 | * as the node dies and @proc becomes NULL) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 336 | * @ptr: userspace pointer for node |
| 337 | * (invariant, no lock needed) |
| 338 | * @cookie: userspace cookie for node |
| 339 | * (invariant, no lock needed) |
| 340 | * @has_strong_ref: userspace notified of strong ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 341 | * (protected by @proc->inner_lock if @proc |
| 342 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 343 | * @pending_strong_ref: userspace has acked notification of strong ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 344 | * (protected by @proc->inner_lock if @proc |
| 345 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 346 | * @has_weak_ref: userspace notified of weak ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 347 | * (protected by @proc->inner_lock if @proc |
| 348 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 349 | * @pending_weak_ref: userspace has acked notification of weak ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 350 | * (protected by @proc->inner_lock if @proc |
| 351 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 352 | * @has_async_transaction: async transaction to node in progress |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 353 | * (protected by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 354 | * @accept_fds: file descriptor operations supported for node |
| 355 | * (invariant after initialized) |
| 356 | * @min_priority: minimum scheduling priority |
| 357 | * (invariant after initialized) |
| 358 | * @async_todo: list of async work items |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 359 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 360 | * |
| 361 | * Bookkeeping structure for binder nodes. |
| 362 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 363 | struct binder_node { |
| 364 | int debug_id; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 365 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 366 | struct binder_work work; |
| 367 | union { |
| 368 | struct rb_node rb_node; |
| 369 | struct hlist_node dead_node; |
| 370 | }; |
| 371 | struct binder_proc *proc; |
| 372 | struct hlist_head refs; |
| 373 | int internal_strong_refs; |
| 374 | int local_weak_refs; |
| 375 | int local_strong_refs; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 376 | int tmp_refs; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 377 | binder_uintptr_t ptr; |
| 378 | binder_uintptr_t cookie; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 379 | struct { |
| 380 | /* |
| 381 | * bitfield elements protected by |
| 382 | * proc inner_lock |
| 383 | */ |
| 384 | u8 has_strong_ref:1; |
| 385 | u8 pending_strong_ref:1; |
| 386 | u8 has_weak_ref:1; |
| 387 | u8 pending_weak_ref:1; |
| 388 | }; |
| 389 | struct { |
| 390 | /* |
| 391 | * invariant after initialization |
| 392 | */ |
| 393 | u8 accept_fds:1; |
| 394 | u8 min_priority; |
| 395 | }; |
| 396 | bool has_async_transaction; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 397 | struct list_head async_todo; |
| 398 | }; |
| 399 | |
| 400 | struct binder_ref_death { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 401 | /** |
| 402 | * @work: worklist element for death notifications |
| 403 | * (protected by inner_lock of the proc that |
| 404 | * this ref belongs to) |
| 405 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 406 | struct binder_work work; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 407 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 408 | }; |
| 409 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 410 | /** |
| 411 | * struct binder_ref_data - binder_ref counts and id |
| 412 | * @debug_id: unique ID for the ref |
| 413 | * @desc: unique userspace handle for ref |
| 414 | * @strong: strong ref count (debugging only if not locked) |
| 415 | * @weak: weak ref count (debugging only if not locked) |
| 416 | * |
| 417 | * Structure to hold ref count and ref id information. Since |
| 418 | * the actual ref can only be accessed with a lock, this structure |
| 419 | * is used to return information about the ref to callers of |
| 420 | * ref inc/dec functions. |
| 421 | */ |
| 422 | struct binder_ref_data { |
| 423 | int debug_id; |
| 424 | uint32_t desc; |
| 425 | int strong; |
| 426 | int weak; |
| 427 | }; |
| 428 | |
| 429 | /** |
| 430 | * struct binder_ref - struct to track references on nodes |
| 431 | * @data: binder_ref_data containing id, handle, and current refcounts |
| 432 | * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree |
| 433 | * @rb_node_node: node for lookup by @node in proc's rb_tree |
| 434 | * @node_entry: list entry for node->refs list in target node |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 435 | * (protected by @node->lock) |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 436 | * @proc: binder_proc containing ref |
| 437 | * @node: binder_node of target node. When cleaning up a |
| 438 | * ref for deletion in binder_cleanup_ref, a non-NULL |
| 439 | * @node indicates the node must be freed |
| 440 | * @death: pointer to death notification (ref_death) if requested |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 441 | * (protected by @node->lock) |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 442 | * |
| 443 | * Structure to track references from procA to target node (on procB). This |
| 444 | * structure is unsafe to access without holding @proc->outer_lock. |
| 445 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 446 | struct binder_ref { |
| 447 | /* Lookups needed: */ |
| 448 | /* node + proc => ref (transaction) */ |
| 449 | /* desc + proc => ref (transaction, inc/dec ref) */ |
| 450 | /* node => refs + procs (proc exit) */ |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 451 | struct binder_ref_data data; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 452 | struct rb_node rb_node_desc; |
| 453 | struct rb_node rb_node_node; |
| 454 | struct hlist_node node_entry; |
| 455 | struct binder_proc *proc; |
| 456 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 457 | struct binder_ref_death *death; |
| 458 | }; |
| 459 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 460 | enum binder_deferred_state { |
| 461 | BINDER_DEFERRED_PUT_FILES = 0x01, |
| 462 | BINDER_DEFERRED_FLUSH = 0x02, |
| 463 | BINDER_DEFERRED_RELEASE = 0x04, |
| 464 | }; |
| 465 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 466 | /** |
| 467 | * struct binder_proc - binder process bookkeeping |
| 468 | * @proc_node: element for binder_procs list |
| 469 | * @threads: rbtree of binder_threads in this proc |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 470 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 471 | * @nodes: rbtree of binder nodes associated with |
| 472 | * this proc ordered by node->ptr |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 473 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 474 | * @refs_by_desc: rbtree of refs ordered by ref->desc |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 475 | * (protected by @outer_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 476 | * @refs_by_node: rbtree of refs ordered by ref->node |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 477 | * (protected by @outer_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 478 | * @pid PID of group_leader of process |
| 479 | * (invariant after initialized) |
| 480 | * @tsk task_struct for group_leader of process |
| 481 | * (invariant after initialized) |
| 482 | * @files files_struct for process |
| 483 | * (invariant after initialized) |
| 484 | * @deferred_work_node: element for binder_deferred_list |
| 485 | * (protected by binder_deferred_lock) |
| 486 | * @deferred_work: bitmap of deferred work to perform |
| 487 | * (protected by binder_deferred_lock) |
| 488 | * @is_dead: process is dead and awaiting free |
| 489 | * when outstanding transactions are cleaned up |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 490 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 491 | * @todo: list of work for this process |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 492 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 493 | * @wait: wait queue head to wait for proc work |
| 494 | * (invariant after initialized) |
| 495 | * @stats: per-process binder statistics |
| 496 | * (atomics, no lock needed) |
| 497 | * @delivered_death: list of delivered death notification |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 498 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 499 | * @max_threads: cap on number of binder threads |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 500 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 501 | * @requested_threads: number of binder threads requested but not |
| 502 | * yet started. In current implementation, can |
| 503 | * only be 0 or 1. |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 504 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 505 | * @requested_threads_started: number binder threads started |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 506 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 507 | * @ready_threads: number of threads waiting for proc work |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 508 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 509 | * @tmp_ref: temporary reference to indicate proc is in use |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 510 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 511 | * @default_priority: default scheduler priority |
| 512 | * (invariant after initialized) |
| 513 | * @debugfs_entry: debugfs node |
| 514 | * @alloc: binder allocator bookkeeping |
| 515 | * @context: binder_context for this proc |
| 516 | * (invariant after initialized) |
| 517 | * @inner_lock: can nest under outer_lock and/or node lock |
| 518 | * @outer_lock: no nesting under innor or node lock |
| 519 | * Lock order: 1) outer, 2) node, 3) inner |
| 520 | * |
| 521 | * Bookkeeping structure for binder processes |
| 522 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 523 | struct binder_proc { |
| 524 | struct hlist_node proc_node; |
| 525 | struct rb_root threads; |
| 526 | struct rb_root nodes; |
| 527 | struct rb_root refs_by_desc; |
| 528 | struct rb_root refs_by_node; |
| 529 | int pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 530 | struct task_struct *tsk; |
| 531 | struct files_struct *files; |
| 532 | struct hlist_node deferred_work_node; |
| 533 | int deferred_work; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 534 | bool is_dead; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 535 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 536 | struct list_head todo; |
| 537 | wait_queue_head_t wait; |
| 538 | struct binder_stats stats; |
| 539 | struct list_head delivered_death; |
| 540 | int max_threads; |
| 541 | int requested_threads; |
| 542 | int requested_threads_started; |
| 543 | int ready_threads; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 544 | int tmp_ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 545 | long default_priority; |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 546 | struct dentry *debugfs_entry; |
Todd Kjos | fdfb4a9 | 2017-06-29 12:01:38 -0700 | [diff] [blame] | 547 | struct binder_alloc alloc; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 548 | struct binder_context *context; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 549 | spinlock_t inner_lock; |
| 550 | spinlock_t outer_lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 551 | }; |
| 552 | |
| 553 | enum { |
| 554 | BINDER_LOOPER_STATE_REGISTERED = 0x01, |
| 555 | BINDER_LOOPER_STATE_ENTERED = 0x02, |
| 556 | BINDER_LOOPER_STATE_EXITED = 0x04, |
| 557 | BINDER_LOOPER_STATE_INVALID = 0x08, |
| 558 | BINDER_LOOPER_STATE_WAITING = 0x10, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 559 | }; |
| 560 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 561 | /** |
| 562 | * struct binder_thread - binder thread bookkeeping |
| 563 | * @proc: binder process for this thread |
| 564 | * (invariant after initialization) |
| 565 | * @rb_node: element for proc->threads rbtree |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 566 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 567 | * @pid: PID for this thread |
| 568 | * (invariant after initialization) |
| 569 | * @looper: bitmap of looping state |
| 570 | * (only accessed by this thread) |
| 571 | * @looper_needs_return: looping thread needs to exit driver |
| 572 | * (no lock needed) |
| 573 | * @transaction_stack: stack of in-progress transactions for this thread |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 574 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 575 | * @todo: list of work to do for this thread |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 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; |
| 596 | int pid; |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 597 | int looper; /* only modified by this thread */ |
| 598 | bool looper_need_return; /* can be written by other thread */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 599 | struct binder_transaction *transaction_stack; |
| 600 | struct list_head todo; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 601 | struct binder_error return_error; |
| 602 | struct binder_error reply_error; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 603 | wait_queue_head_t wait; |
| 604 | struct binder_stats stats; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 605 | atomic_t tmp_ref; |
| 606 | bool is_dead; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 607 | }; |
| 608 | |
| 609 | struct binder_transaction { |
| 610 | int debug_id; |
| 611 | struct binder_work work; |
| 612 | struct binder_thread *from; |
| 613 | struct binder_transaction *from_parent; |
| 614 | struct binder_proc *to_proc; |
| 615 | struct binder_thread *to_thread; |
| 616 | struct binder_transaction *to_parent; |
| 617 | unsigned need_reply:1; |
| 618 | /* unsigned is_dead:1; */ /* not used at the moment */ |
| 619 | |
| 620 | struct binder_buffer *buffer; |
| 621 | unsigned int code; |
| 622 | unsigned int flags; |
| 623 | long priority; |
| 624 | long saved_priority; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 625 | kuid_t sender_euid; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 626 | /** |
| 627 | * @lock: protects @from, @to_proc, and @to_thread |
| 628 | * |
| 629 | * @from, @to_proc, and @to_thread can be set to NULL |
| 630 | * during thread teardown |
| 631 | */ |
| 632 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 633 | }; |
| 634 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 635 | /** |
| 636 | * binder_proc_lock() - Acquire outer lock for given binder_proc |
| 637 | * @proc: struct binder_proc to acquire |
| 638 | * |
| 639 | * Acquires proc->outer_lock. Used to protect binder_ref |
| 640 | * structures associated with the given proc. |
| 641 | */ |
| 642 | #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__) |
| 643 | static void |
| 644 | _binder_proc_lock(struct binder_proc *proc, int line) |
| 645 | { |
| 646 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 647 | "%s: line=%d\n", __func__, line); |
| 648 | spin_lock(&proc->outer_lock); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * binder_proc_unlock() - Release spinlock for given binder_proc |
| 653 | * @proc: struct binder_proc to acquire |
| 654 | * |
| 655 | * Release lock acquired via binder_proc_lock() |
| 656 | */ |
| 657 | #define binder_proc_unlock(_proc) _binder_proc_unlock(_proc, __LINE__) |
| 658 | static void |
| 659 | _binder_proc_unlock(struct binder_proc *proc, int line) |
| 660 | { |
| 661 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 662 | "%s: line=%d\n", __func__, line); |
| 663 | spin_unlock(&proc->outer_lock); |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * binder_inner_proc_lock() - Acquire inner lock for given binder_proc |
| 668 | * @proc: struct binder_proc to acquire |
| 669 | * |
| 670 | * Acquires proc->inner_lock. Used to protect todo lists |
| 671 | */ |
| 672 | #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__) |
| 673 | static void |
| 674 | _binder_inner_proc_lock(struct binder_proc *proc, int line) |
| 675 | { |
| 676 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 677 | "%s: line=%d\n", __func__, line); |
| 678 | spin_lock(&proc->inner_lock); |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * binder_inner_proc_unlock() - Release inner lock for given binder_proc |
| 683 | * @proc: struct binder_proc to acquire |
| 684 | * |
| 685 | * Release lock acquired via binder_inner_proc_lock() |
| 686 | */ |
| 687 | #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__) |
| 688 | static void |
| 689 | _binder_inner_proc_unlock(struct binder_proc *proc, int line) |
| 690 | { |
| 691 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 692 | "%s: line=%d\n", __func__, line); |
| 693 | spin_unlock(&proc->inner_lock); |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * binder_node_lock() - Acquire spinlock for given binder_node |
| 698 | * @node: struct binder_node to acquire |
| 699 | * |
| 700 | * Acquires node->lock. Used to protect binder_node fields |
| 701 | */ |
| 702 | #define binder_node_lock(node) _binder_node_lock(node, __LINE__) |
| 703 | static void |
| 704 | _binder_node_lock(struct binder_node *node, int line) |
| 705 | { |
| 706 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 707 | "%s: line=%d\n", __func__, line); |
| 708 | spin_lock(&node->lock); |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * binder_node_unlock() - Release spinlock for given binder_proc |
| 713 | * @node: struct binder_node to acquire |
| 714 | * |
| 715 | * Release lock acquired via binder_node_lock() |
| 716 | */ |
| 717 | #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__) |
| 718 | static void |
| 719 | _binder_node_unlock(struct binder_node *node, int line) |
| 720 | { |
| 721 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 722 | "%s: line=%d\n", __func__, line); |
| 723 | spin_unlock(&node->lock); |
| 724 | } |
| 725 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 726 | /** |
| 727 | * binder_node_inner_lock() - Acquire node and inner locks |
| 728 | * @node: struct binder_node to acquire |
| 729 | * |
| 730 | * Acquires node->lock. If node->proc also acquires |
| 731 | * proc->inner_lock. Used to protect binder_node fields |
| 732 | */ |
| 733 | #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__) |
| 734 | static void |
| 735 | _binder_node_inner_lock(struct binder_node *node, int line) |
| 736 | { |
| 737 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 738 | "%s: line=%d\n", __func__, line); |
| 739 | spin_lock(&node->lock); |
| 740 | if (node->proc) |
| 741 | binder_inner_proc_lock(node->proc); |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * binder_node_unlock() - Release node and inner locks |
| 746 | * @node: struct binder_node to acquire |
| 747 | * |
| 748 | * Release lock acquired via binder_node_lock() |
| 749 | */ |
| 750 | #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__) |
| 751 | static void |
| 752 | _binder_node_inner_unlock(struct binder_node *node, int line) |
| 753 | { |
| 754 | struct binder_proc *proc = node->proc; |
| 755 | |
| 756 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 757 | "%s: line=%d\n", __func__, line); |
| 758 | if (proc) |
| 759 | binder_inner_proc_unlock(proc); |
| 760 | spin_unlock(&node->lock); |
| 761 | } |
| 762 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 763 | static bool binder_worklist_empty_ilocked(struct list_head *list) |
| 764 | { |
| 765 | return list_empty(list); |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * binder_worklist_empty() - Check if no items on the work list |
| 770 | * @proc: binder_proc associated with list |
| 771 | * @list: list to check |
| 772 | * |
| 773 | * Return: true if there are no items on list, else false |
| 774 | */ |
| 775 | static bool binder_worklist_empty(struct binder_proc *proc, |
| 776 | struct list_head *list) |
| 777 | { |
| 778 | bool ret; |
| 779 | |
| 780 | binder_inner_proc_lock(proc); |
| 781 | ret = binder_worklist_empty_ilocked(list); |
| 782 | binder_inner_proc_unlock(proc); |
| 783 | return ret; |
| 784 | } |
| 785 | |
| 786 | static void |
| 787 | binder_enqueue_work_ilocked(struct binder_work *work, |
| 788 | struct list_head *target_list) |
| 789 | { |
| 790 | BUG_ON(target_list == NULL); |
| 791 | BUG_ON(work->entry.next && !list_empty(&work->entry)); |
| 792 | list_add_tail(&work->entry, target_list); |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * binder_enqueue_work() - Add an item to the work list |
| 797 | * @proc: binder_proc associated with list |
| 798 | * @work: struct binder_work to add to list |
| 799 | * @target_list: list to add work to |
| 800 | * |
| 801 | * Adds the work to the specified list. Asserts that work |
| 802 | * is not already on a list. |
| 803 | */ |
| 804 | static void |
| 805 | binder_enqueue_work(struct binder_proc *proc, |
| 806 | struct binder_work *work, |
| 807 | struct list_head *target_list) |
| 808 | { |
| 809 | binder_inner_proc_lock(proc); |
| 810 | binder_enqueue_work_ilocked(work, target_list); |
| 811 | binder_inner_proc_unlock(proc); |
| 812 | } |
| 813 | |
| 814 | static void |
| 815 | binder_dequeue_work_ilocked(struct binder_work *work) |
| 816 | { |
| 817 | list_del_init(&work->entry); |
| 818 | } |
| 819 | |
| 820 | /** |
| 821 | * binder_dequeue_work() - Removes an item from the work list |
| 822 | * @proc: binder_proc associated with list |
| 823 | * @work: struct binder_work to remove from list |
| 824 | * |
| 825 | * Removes the specified work item from whatever list it is on. |
| 826 | * Can safely be called if work is not on any list. |
| 827 | */ |
| 828 | static void |
| 829 | binder_dequeue_work(struct binder_proc *proc, struct binder_work *work) |
| 830 | { |
| 831 | binder_inner_proc_lock(proc); |
| 832 | binder_dequeue_work_ilocked(work); |
| 833 | binder_inner_proc_unlock(proc); |
| 834 | } |
| 835 | |
| 836 | static struct binder_work *binder_dequeue_work_head_ilocked( |
| 837 | struct list_head *list) |
| 838 | { |
| 839 | struct binder_work *w; |
| 840 | |
| 841 | w = list_first_entry_or_null(list, struct binder_work, entry); |
| 842 | if (w) |
| 843 | list_del_init(&w->entry); |
| 844 | return w; |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * binder_dequeue_work_head() - Dequeues the item at head of list |
| 849 | * @proc: binder_proc associated with list |
| 850 | * @list: list to dequeue head |
| 851 | * |
| 852 | * Removes the head of the list if there are items on the list |
| 853 | * |
| 854 | * Return: pointer dequeued binder_work, NULL if list was empty |
| 855 | */ |
| 856 | static struct binder_work *binder_dequeue_work_head( |
| 857 | struct binder_proc *proc, |
| 858 | struct list_head *list) |
| 859 | { |
| 860 | struct binder_work *w; |
| 861 | |
| 862 | binder_inner_proc_lock(proc); |
| 863 | w = binder_dequeue_work_head_ilocked(list); |
| 864 | binder_inner_proc_unlock(proc); |
| 865 | return w; |
| 866 | } |
| 867 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 868 | static void |
| 869 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 870 | static void binder_free_thread(struct binder_thread *thread); |
| 871 | static void binder_free_proc(struct binder_proc *proc); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 872 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 873 | |
Sachin Kamat | efde99c | 2012-08-17 16:39:36 +0530 | [diff] [blame] | 874 | 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] | 875 | { |
| 876 | struct files_struct *files = proc->files; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 877 | unsigned long rlim_cur; |
| 878 | unsigned long irqs; |
| 879 | |
| 880 | if (files == NULL) |
| 881 | return -ESRCH; |
| 882 | |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 883 | if (!lock_task_sighand(proc->tsk, &irqs)) |
| 884 | return -EMFILE; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 885 | |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 886 | rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE); |
| 887 | unlock_task_sighand(proc->tsk, &irqs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 888 | |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 889 | return __alloc_fd(files, 0, rlim_cur, flags); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /* |
| 893 | * copied from fd_install |
| 894 | */ |
| 895 | static void task_fd_install( |
| 896 | struct binder_proc *proc, unsigned int fd, struct file *file) |
| 897 | { |
Al Viro | f869e8a | 2012-08-15 21:06:33 -0400 | [diff] [blame] | 898 | if (proc->files) |
| 899 | __fd_install(proc->files, fd, file); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /* |
| 903 | * copied from sys_close |
| 904 | */ |
| 905 | static long task_close_fd(struct binder_proc *proc, unsigned int fd) |
| 906 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 907 | int retval; |
| 908 | |
Al Viro | 483ce1d | 2012-08-19 12:04:24 -0400 | [diff] [blame] | 909 | if (proc->files == NULL) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 910 | return -ESRCH; |
| 911 | |
Al Viro | 483ce1d | 2012-08-19 12:04:24 -0400 | [diff] [blame] | 912 | retval = __close_fd(proc->files, fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 913 | /* can't restart close syscall because file table entry was cleared */ |
| 914 | if (unlikely(retval == -ERESTARTSYS || |
| 915 | retval == -ERESTARTNOINTR || |
| 916 | retval == -ERESTARTNOHAND || |
| 917 | retval == -ERESTART_RESTARTBLOCK)) |
| 918 | retval = -EINTR; |
| 919 | |
| 920 | return retval; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | static void binder_set_nice(long nice) |
| 924 | { |
| 925 | long min_nice; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 926 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 927 | if (can_nice(current, nice)) { |
| 928 | set_user_nice(current, nice); |
| 929 | return; |
| 930 | } |
Krzysztof Opasiak | c3643b6 | 2017-07-05 19:24:44 +0200 | [diff] [blame] | 931 | min_nice = rlimit_to_nice(rlimit(RLIMIT_NICE)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 932 | binder_debug(BINDER_DEBUG_PRIORITY_CAP, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 933 | "%d: nice value %ld not allowed use %ld instead\n", |
| 934 | current->pid, nice, min_nice); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 935 | set_user_nice(current, min_nice); |
Dongsheng Yang | 8698a74 | 2014-03-11 18:09:12 +0800 | [diff] [blame] | 936 | if (min_nice <= MAX_NICE) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 937 | return; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 938 | binder_user_error("%d RLIMIT_NICE not set\n", current->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 939 | } |
| 940 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 941 | static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc, |
| 942 | binder_uintptr_t ptr) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 943 | { |
| 944 | struct rb_node *n = proc->nodes.rb_node; |
| 945 | struct binder_node *node; |
| 946 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 947 | BUG_ON(!spin_is_locked(&proc->inner_lock)); |
| 948 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 949 | while (n) { |
| 950 | node = rb_entry(n, struct binder_node, rb_node); |
| 951 | |
| 952 | if (ptr < node->ptr) |
| 953 | n = n->rb_left; |
| 954 | else if (ptr > node->ptr) |
| 955 | n = n->rb_right; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 956 | else { |
| 957 | /* |
| 958 | * take an implicit weak reference |
| 959 | * to ensure node stays alive until |
| 960 | * call to binder_put_node() |
| 961 | */ |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 962 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 963 | return node; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 964 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 965 | } |
| 966 | return NULL; |
| 967 | } |
| 968 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 969 | static struct binder_node *binder_get_node(struct binder_proc *proc, |
| 970 | binder_uintptr_t ptr) |
| 971 | { |
| 972 | struct binder_node *node; |
| 973 | |
| 974 | binder_inner_proc_lock(proc); |
| 975 | node = binder_get_node_ilocked(proc, ptr); |
| 976 | binder_inner_proc_unlock(proc); |
| 977 | return node; |
| 978 | } |
| 979 | |
| 980 | static struct binder_node *binder_init_node_ilocked( |
| 981 | struct binder_proc *proc, |
| 982 | struct binder_node *new_node, |
| 983 | struct flat_binder_object *fp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 984 | { |
| 985 | struct rb_node **p = &proc->nodes.rb_node; |
| 986 | struct rb_node *parent = NULL; |
| 987 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 988 | binder_uintptr_t ptr = fp ? fp->binder : 0; |
| 989 | binder_uintptr_t cookie = fp ? fp->cookie : 0; |
| 990 | __u32 flags = fp ? fp->flags : 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 991 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 992 | BUG_ON(!spin_is_locked(&proc->inner_lock)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 993 | while (*p) { |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 994 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 995 | parent = *p; |
| 996 | node = rb_entry(parent, struct binder_node, rb_node); |
| 997 | |
| 998 | if (ptr < node->ptr) |
| 999 | p = &(*p)->rb_left; |
| 1000 | else if (ptr > node->ptr) |
| 1001 | p = &(*p)->rb_right; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1002 | else { |
| 1003 | /* |
| 1004 | * A matching node is already in |
| 1005 | * the rb tree. Abandon the init |
| 1006 | * and return it. |
| 1007 | */ |
| 1008 | binder_inc_node_tmpref_ilocked(node); |
| 1009 | return node; |
| 1010 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1011 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1012 | node = new_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1013 | binder_stats_created(BINDER_STAT_NODE); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1014 | node->tmp_refs++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1015 | rb_link_node(&node->rb_node, parent, p); |
| 1016 | rb_insert_color(&node->rb_node, &proc->nodes); |
Todd Kjos | 656a800 | 2017-06-29 12:01:45 -0700 | [diff] [blame] | 1017 | node->debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1018 | node->proc = proc; |
| 1019 | node->ptr = ptr; |
| 1020 | node->cookie = cookie; |
| 1021 | node->work.type = BINDER_WORK_NODE; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1022 | node->min_priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK; |
| 1023 | node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 1024 | spin_lock_init(&node->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1025 | INIT_LIST_HEAD(&node->work.entry); |
| 1026 | INIT_LIST_HEAD(&node->async_todo); |
| 1027 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1028 | "%d:%d node %d u%016llx c%016llx created\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1029 | proc->pid, current->pid, node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1030 | (u64)node->ptr, (u64)node->cookie); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1031 | |
| 1032 | return node; |
| 1033 | } |
| 1034 | |
| 1035 | static struct binder_node *binder_new_node(struct binder_proc *proc, |
| 1036 | struct flat_binder_object *fp) |
| 1037 | { |
| 1038 | struct binder_node *node; |
| 1039 | struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 1040 | |
| 1041 | if (!new_node) |
| 1042 | return NULL; |
| 1043 | binder_inner_proc_lock(proc); |
| 1044 | node = binder_init_node_ilocked(proc, new_node, fp); |
| 1045 | binder_inner_proc_unlock(proc); |
| 1046 | if (node != new_node) |
| 1047 | /* |
| 1048 | * The node was already added by another thread |
| 1049 | */ |
| 1050 | kfree(new_node); |
| 1051 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1052 | return node; |
| 1053 | } |
| 1054 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1055 | static void binder_free_node(struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1056 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1057 | kfree(node); |
| 1058 | binder_stats_deleted(BINDER_STAT_NODE); |
| 1059 | } |
| 1060 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1061 | static int binder_inc_node_nilocked(struct binder_node *node, int strong, |
| 1062 | int internal, |
| 1063 | struct list_head *target_list) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1064 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1065 | struct binder_proc *proc = node->proc; |
| 1066 | |
| 1067 | BUG_ON(!spin_is_locked(&node->lock)); |
| 1068 | if (proc) |
| 1069 | BUG_ON(!spin_is_locked(&proc->inner_lock)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1070 | if (strong) { |
| 1071 | if (internal) { |
| 1072 | if (target_list == NULL && |
| 1073 | node->internal_strong_refs == 0 && |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 1074 | !(node->proc && |
| 1075 | node == node->proc->context->binder_context_mgr_node && |
| 1076 | node->has_strong_ref)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1077 | pr_err("invalid inc strong node for %d\n", |
| 1078 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1079 | return -EINVAL; |
| 1080 | } |
| 1081 | node->internal_strong_refs++; |
| 1082 | } else |
| 1083 | node->local_strong_refs++; |
| 1084 | if (!node->has_strong_ref && target_list) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1085 | binder_dequeue_work_ilocked(&node->work); |
| 1086 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1087 | } |
| 1088 | } else { |
| 1089 | if (!internal) |
| 1090 | node->local_weak_refs++; |
| 1091 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { |
| 1092 | if (target_list == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1093 | pr_err("invalid inc weak node for %d\n", |
| 1094 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1095 | return -EINVAL; |
| 1096 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1097 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1103 | static int binder_inc_node(struct binder_node *node, int strong, int internal, |
| 1104 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1105 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1106 | int ret; |
| 1107 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1108 | binder_node_inner_lock(node); |
| 1109 | ret = binder_inc_node_nilocked(node, strong, internal, target_list); |
| 1110 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1111 | |
| 1112 | return ret; |
| 1113 | } |
| 1114 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1115 | static bool binder_dec_node_nilocked(struct binder_node *node, |
| 1116 | int strong, int internal) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1117 | { |
| 1118 | struct binder_proc *proc = node->proc; |
| 1119 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1120 | BUG_ON(!spin_is_locked(&node->lock)); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1121 | if (proc) |
| 1122 | BUG_ON(!spin_is_locked(&proc->inner_lock)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1123 | if (strong) { |
| 1124 | if (internal) |
| 1125 | node->internal_strong_refs--; |
| 1126 | else |
| 1127 | node->local_strong_refs--; |
| 1128 | if (node->local_strong_refs || node->internal_strong_refs) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1129 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1130 | } else { |
| 1131 | if (!internal) |
| 1132 | node->local_weak_refs--; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1133 | if (node->local_weak_refs || node->tmp_refs || |
| 1134 | !hlist_empty(&node->refs)) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1135 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1136 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1137 | |
| 1138 | if (proc && (node->has_strong_ref || node->has_weak_ref)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1139 | if (list_empty(&node->work.entry)) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1140 | binder_enqueue_work_ilocked(&node->work, &proc->todo); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1141 | wake_up_interruptible(&node->proc->wait); |
| 1142 | } |
| 1143 | } else { |
| 1144 | if (hlist_empty(&node->refs) && !node->local_strong_refs && |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1145 | !node->local_weak_refs && !node->tmp_refs) { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1146 | if (proc) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1147 | binder_dequeue_work_ilocked(&node->work); |
| 1148 | rb_erase(&node->rb_node, &proc->nodes); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1149 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1150 | "refless node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1151 | node->debug_id); |
| 1152 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1153 | BUG_ON(!list_empty(&node->work.entry)); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 1154 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1155 | /* |
| 1156 | * tmp_refs could have changed so |
| 1157 | * check it again |
| 1158 | */ |
| 1159 | if (node->tmp_refs) { |
| 1160 | spin_unlock(&binder_dead_nodes_lock); |
| 1161 | return false; |
| 1162 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1163 | hlist_del(&node->dead_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 1164 | spin_unlock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1165 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1166 | "dead node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1167 | node->debug_id); |
| 1168 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1169 | return true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1170 | } |
| 1171 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1172 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1173 | } |
| 1174 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1175 | static void binder_dec_node(struct binder_node *node, int strong, int internal) |
| 1176 | { |
| 1177 | bool free_node; |
| 1178 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1179 | binder_node_inner_lock(node); |
| 1180 | free_node = binder_dec_node_nilocked(node, strong, internal); |
| 1181 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1182 | if (free_node) |
| 1183 | binder_free_node(node); |
| 1184 | } |
| 1185 | |
| 1186 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1187 | { |
| 1188 | /* |
| 1189 | * No call to binder_inc_node() is needed since we |
| 1190 | * don't need to inform userspace of any changes to |
| 1191 | * tmp_refs |
| 1192 | */ |
| 1193 | node->tmp_refs++; |
| 1194 | } |
| 1195 | |
| 1196 | /** |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1197 | * binder_inc_node_tmpref() - take a temporary reference on node |
| 1198 | * @node: node to reference |
| 1199 | * |
| 1200 | * Take reference on node to prevent the node from being freed |
| 1201 | * while referenced only by a local variable. The inner lock is |
| 1202 | * needed to serialize with the node work on the queue (which |
| 1203 | * isn't needed after the node is dead). If the node is dead |
| 1204 | * (node->proc is NULL), use binder_dead_nodes_lock to protect |
| 1205 | * node->tmp_refs against dead-node-only cases where the node |
| 1206 | * lock cannot be acquired (eg traversing the dead node list to |
| 1207 | * print nodes) |
| 1208 | */ |
| 1209 | static void binder_inc_node_tmpref(struct binder_node *node) |
| 1210 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1211 | binder_node_lock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1212 | if (node->proc) |
| 1213 | binder_inner_proc_lock(node->proc); |
| 1214 | else |
| 1215 | spin_lock(&binder_dead_nodes_lock); |
| 1216 | binder_inc_node_tmpref_ilocked(node); |
| 1217 | if (node->proc) |
| 1218 | binder_inner_proc_unlock(node->proc); |
| 1219 | else |
| 1220 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1221 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | /** |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1225 | * binder_dec_node_tmpref() - remove a temporary reference on node |
| 1226 | * @node: node to reference |
| 1227 | * |
| 1228 | * Release temporary reference on node taken via binder_inc_node_tmpref() |
| 1229 | */ |
| 1230 | static void binder_dec_node_tmpref(struct binder_node *node) |
| 1231 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1232 | bool free_node; |
| 1233 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1234 | binder_node_inner_lock(node); |
| 1235 | if (!node->proc) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1236 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1237 | node->tmp_refs--; |
| 1238 | BUG_ON(node->tmp_refs < 0); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1239 | if (!node->proc) |
| 1240 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1241 | /* |
| 1242 | * Call binder_dec_node() to check if all refcounts are 0 |
| 1243 | * and cleanup is needed. Calling with strong=0 and internal=1 |
| 1244 | * causes no actual reference to be released in binder_dec_node(). |
| 1245 | * If that changes, a change is needed here too. |
| 1246 | */ |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1247 | free_node = binder_dec_node_nilocked(node, 0, 1); |
| 1248 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1249 | if (free_node) |
| 1250 | binder_free_node(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | static void binder_put_node(struct binder_node *node) |
| 1254 | { |
| 1255 | binder_dec_node_tmpref(node); |
| 1256 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1257 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1258 | static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc, |
| 1259 | u32 desc, bool need_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1260 | { |
| 1261 | struct rb_node *n = proc->refs_by_desc.rb_node; |
| 1262 | struct binder_ref *ref; |
| 1263 | |
| 1264 | while (n) { |
| 1265 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1266 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1267 | if (desc < ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1268 | n = n->rb_left; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1269 | } else if (desc > ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1270 | n = n->rb_right; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1271 | } else if (need_strong_ref && !ref->data.strong) { |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1272 | binder_user_error("tried to use weak ref as strong ref\n"); |
| 1273 | return NULL; |
| 1274 | } else { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1275 | return ref; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1276 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1277 | } |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1281 | /** |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1282 | * 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] | 1283 | * @proc: binder_proc that owns the ref |
| 1284 | * @node: binder_node of target |
| 1285 | * @new_ref: newly allocated binder_ref to be initialized or %NULL |
| 1286 | * |
| 1287 | * Look up the ref for the given node and return it if it exists |
| 1288 | * |
| 1289 | * If it doesn't exist and the caller provides a newly allocated |
| 1290 | * ref, initialize the fields of the newly allocated ref and insert |
| 1291 | * into the given proc rb_trees and node refs list. |
| 1292 | * |
| 1293 | * Return: the ref for node. It is possible that another thread |
| 1294 | * allocated/initialized the ref first in which case the |
| 1295 | * returned ref would be different than the passed-in |
| 1296 | * new_ref. new_ref must be kfree'd by the caller in |
| 1297 | * this case. |
| 1298 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1299 | static struct binder_ref *binder_get_ref_for_node_olocked( |
| 1300 | struct binder_proc *proc, |
| 1301 | struct binder_node *node, |
| 1302 | struct binder_ref *new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1303 | { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1304 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1305 | struct rb_node **p = &proc->refs_by_node.rb_node; |
| 1306 | struct rb_node *parent = NULL; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1307 | struct binder_ref *ref; |
| 1308 | struct rb_node *n; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1309 | |
| 1310 | while (*p) { |
| 1311 | parent = *p; |
| 1312 | ref = rb_entry(parent, struct binder_ref, rb_node_node); |
| 1313 | |
| 1314 | if (node < ref->node) |
| 1315 | p = &(*p)->rb_left; |
| 1316 | else if (node > ref->node) |
| 1317 | p = &(*p)->rb_right; |
| 1318 | else |
| 1319 | return ref; |
| 1320 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1321 | if (!new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1322 | return NULL; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1323 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1324 | binder_stats_created(BINDER_STAT_REF); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1325 | new_ref->data.debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1326 | new_ref->proc = proc; |
| 1327 | new_ref->node = node; |
| 1328 | rb_link_node(&new_ref->rb_node_node, parent, p); |
| 1329 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); |
| 1330 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1331 | 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] | 1332 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 1333 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1334 | if (ref->data.desc > new_ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1335 | break; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1336 | new_ref->data.desc = ref->data.desc + 1; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | p = &proc->refs_by_desc.rb_node; |
| 1340 | while (*p) { |
| 1341 | parent = *p; |
| 1342 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); |
| 1343 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1344 | if (new_ref->data.desc < ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1345 | p = &(*p)->rb_left; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1346 | else if (new_ref->data.desc > ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1347 | p = &(*p)->rb_right; |
| 1348 | else |
| 1349 | BUG(); |
| 1350 | } |
| 1351 | rb_link_node(&new_ref->rb_node_desc, parent, p); |
| 1352 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1353 | |
| 1354 | binder_node_lock(node); |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1355 | hlist_add_head(&new_ref->node_entry, &node->refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1356 | |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1357 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1358 | "%d new ref %d desc %d for node %d\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1359 | proc->pid, new_ref->data.debug_id, new_ref->data.desc, |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1360 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1361 | binder_node_unlock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1362 | return new_ref; |
| 1363 | } |
| 1364 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1365 | static void binder_cleanup_ref_olocked(struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1366 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1367 | bool delete_node = false; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1368 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1369 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1370 | "%d delete ref %d desc %d for node %d\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1371 | ref->proc->pid, ref->data.debug_id, ref->data.desc, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1372 | ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1373 | |
| 1374 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); |
| 1375 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1376 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1377 | binder_node_inner_lock(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1378 | if (ref->data.strong) |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1379 | binder_dec_node_nilocked(ref->node, 1, 1); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1380 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1381 | hlist_del(&ref->node_entry); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1382 | delete_node = binder_dec_node_nilocked(ref->node, 0, 1); |
| 1383 | binder_node_inner_unlock(ref->node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1384 | /* |
| 1385 | * Clear ref->node unless we want the caller to free the node |
| 1386 | */ |
| 1387 | if (!delete_node) { |
| 1388 | /* |
| 1389 | * The caller uses ref->node to determine |
| 1390 | * whether the node needs to be freed. Clear |
| 1391 | * it since the node is still alive. |
| 1392 | */ |
| 1393 | ref->node = NULL; |
| 1394 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1395 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1396 | if (ref->death) { |
| 1397 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1398 | "%d delete ref %d desc %d has death notification\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1399 | ref->proc->pid, ref->data.debug_id, |
| 1400 | ref->data.desc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1401 | binder_dequeue_work(ref->proc, &ref->death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1402 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 1403 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1404 | binder_stats_deleted(BINDER_STAT_REF); |
| 1405 | } |
| 1406 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1407 | /** |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1408 | * binder_inc_ref_olocked() - increment the ref for given handle |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1409 | * @ref: ref to be incremented |
| 1410 | * @strong: if true, strong increment, else weak |
| 1411 | * @target_list: list to queue node work on |
| 1412 | * |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1413 | * Increment the ref. @ref->proc->outer_lock must be held on entry |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1414 | * |
| 1415 | * Return: 0, if successful, else errno |
| 1416 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1417 | static int binder_inc_ref_olocked(struct binder_ref *ref, int strong, |
| 1418 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1419 | { |
| 1420 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1421 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1422 | if (strong) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1423 | if (ref->data.strong == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1424 | ret = binder_inc_node(ref->node, 1, 1, target_list); |
| 1425 | if (ret) |
| 1426 | return ret; |
| 1427 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1428 | ref->data.strong++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1429 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1430 | if (ref->data.weak == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1431 | ret = binder_inc_node(ref->node, 0, 1, target_list); |
| 1432 | if (ret) |
| 1433 | return ret; |
| 1434 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1435 | ref->data.weak++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1436 | } |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1440 | /** |
| 1441 | * binder_dec_ref() - dec the ref for given handle |
| 1442 | * @ref: ref to be decremented |
| 1443 | * @strong: if true, strong decrement, else weak |
| 1444 | * |
| 1445 | * Decrement the ref. |
| 1446 | * |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1447 | * Return: true if ref is cleaned up and ready to be freed |
| 1448 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1449 | 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] | 1450 | { |
| 1451 | if (strong) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1452 | if (ref->data.strong == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1453 | 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] | 1454 | ref->proc->pid, ref->data.debug_id, |
| 1455 | ref->data.desc, ref->data.strong, |
| 1456 | ref->data.weak); |
| 1457 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1458 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1459 | ref->data.strong--; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1460 | if (ref->data.strong == 0) |
| 1461 | binder_dec_node(ref->node, strong, 1); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1462 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1463 | if (ref->data.weak == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1464 | 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] | 1465 | ref->proc->pid, ref->data.debug_id, |
| 1466 | ref->data.desc, ref->data.strong, |
| 1467 | ref->data.weak); |
| 1468 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1469 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1470 | ref->data.weak--; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1471 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1472 | if (ref->data.strong == 0 && ref->data.weak == 0) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1473 | binder_cleanup_ref_olocked(ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1474 | return true; |
| 1475 | } |
| 1476 | return false; |
| 1477 | } |
| 1478 | |
| 1479 | /** |
| 1480 | * binder_get_node_from_ref() - get the node from the given proc/desc |
| 1481 | * @proc: proc containing the ref |
| 1482 | * @desc: the handle associated with the ref |
| 1483 | * @need_strong_ref: if true, only return node if ref is strong |
| 1484 | * @rdata: the id/refcount data for the ref |
| 1485 | * |
| 1486 | * Given a proc and ref handle, return the associated binder_node |
| 1487 | * |
| 1488 | * Return: a binder_node or NULL if not found or not strong when strong required |
| 1489 | */ |
| 1490 | static struct binder_node *binder_get_node_from_ref( |
| 1491 | struct binder_proc *proc, |
| 1492 | u32 desc, bool need_strong_ref, |
| 1493 | struct binder_ref_data *rdata) |
| 1494 | { |
| 1495 | struct binder_node *node; |
| 1496 | struct binder_ref *ref; |
| 1497 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1498 | binder_proc_lock(proc); |
| 1499 | ref = binder_get_ref_olocked(proc, desc, need_strong_ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1500 | if (!ref) |
| 1501 | goto err_no_ref; |
| 1502 | node = ref->node; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1503 | /* |
| 1504 | * Take an implicit reference on the node to ensure |
| 1505 | * it stays alive until the call to binder_put_node() |
| 1506 | */ |
| 1507 | binder_inc_node_tmpref(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1508 | if (rdata) |
| 1509 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1510 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1511 | |
| 1512 | return node; |
| 1513 | |
| 1514 | err_no_ref: |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1515 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1516 | return NULL; |
| 1517 | } |
| 1518 | |
| 1519 | /** |
| 1520 | * binder_free_ref() - free the binder_ref |
| 1521 | * @ref: ref to free |
| 1522 | * |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1523 | * Free the binder_ref. Free the binder_node indicated by ref->node |
| 1524 | * (if non-NULL) and the binder_ref_death indicated by ref->death. |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1525 | */ |
| 1526 | static void binder_free_ref(struct binder_ref *ref) |
| 1527 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1528 | if (ref->node) |
| 1529 | binder_free_node(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1530 | kfree(ref->death); |
| 1531 | kfree(ref); |
| 1532 | } |
| 1533 | |
| 1534 | /** |
| 1535 | * binder_update_ref_for_handle() - inc/dec the ref for given handle |
| 1536 | * @proc: proc containing the ref |
| 1537 | * @desc: the handle associated with the ref |
| 1538 | * @increment: true=inc reference, false=dec reference |
| 1539 | * @strong: true=strong reference, false=weak reference |
| 1540 | * @rdata: the id/refcount data for the ref |
| 1541 | * |
| 1542 | * Given a proc and ref handle, increment or decrement the ref |
| 1543 | * according to "increment" arg. |
| 1544 | * |
| 1545 | * Return: 0 if successful, else errno |
| 1546 | */ |
| 1547 | static int binder_update_ref_for_handle(struct binder_proc *proc, |
| 1548 | uint32_t desc, bool increment, bool strong, |
| 1549 | struct binder_ref_data *rdata) |
| 1550 | { |
| 1551 | int ret = 0; |
| 1552 | struct binder_ref *ref; |
| 1553 | bool delete_ref = false; |
| 1554 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1555 | binder_proc_lock(proc); |
| 1556 | ref = binder_get_ref_olocked(proc, desc, strong); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1557 | if (!ref) { |
| 1558 | ret = -EINVAL; |
| 1559 | goto err_no_ref; |
| 1560 | } |
| 1561 | if (increment) |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1562 | ret = binder_inc_ref_olocked(ref, strong, NULL); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1563 | else |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1564 | delete_ref = binder_dec_ref_olocked(ref, strong); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1565 | |
| 1566 | if (rdata) |
| 1567 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1568 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1569 | |
| 1570 | if (delete_ref) |
| 1571 | binder_free_ref(ref); |
| 1572 | return ret; |
| 1573 | |
| 1574 | err_no_ref: |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1575 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1576 | return ret; |
| 1577 | } |
| 1578 | |
| 1579 | /** |
| 1580 | * binder_dec_ref_for_handle() - dec the ref for given handle |
| 1581 | * @proc: proc containing the ref |
| 1582 | * @desc: the handle associated with the ref |
| 1583 | * @strong: true=strong reference, false=weak reference |
| 1584 | * @rdata: the id/refcount data for the ref |
| 1585 | * |
| 1586 | * Just calls binder_update_ref_for_handle() to decrement the ref. |
| 1587 | * |
| 1588 | * Return: 0 if successful, else errno |
| 1589 | */ |
| 1590 | static int binder_dec_ref_for_handle(struct binder_proc *proc, |
| 1591 | uint32_t desc, bool strong, struct binder_ref_data *rdata) |
| 1592 | { |
| 1593 | return binder_update_ref_for_handle(proc, desc, false, strong, rdata); |
| 1594 | } |
| 1595 | |
| 1596 | |
| 1597 | /** |
| 1598 | * binder_inc_ref_for_node() - increment the ref for given proc/node |
| 1599 | * @proc: proc containing the ref |
| 1600 | * @node: target node |
| 1601 | * @strong: true=strong reference, false=weak reference |
| 1602 | * @target_list: worklist to use if node is incremented |
| 1603 | * @rdata: the id/refcount data for the ref |
| 1604 | * |
| 1605 | * Given a proc and node, increment the ref. Create the ref if it |
| 1606 | * doesn't already exist |
| 1607 | * |
| 1608 | * Return: 0 if successful, else errno |
| 1609 | */ |
| 1610 | static int binder_inc_ref_for_node(struct binder_proc *proc, |
| 1611 | struct binder_node *node, |
| 1612 | bool strong, |
| 1613 | struct list_head *target_list, |
| 1614 | struct binder_ref_data *rdata) |
| 1615 | { |
| 1616 | struct binder_ref *ref; |
| 1617 | struct binder_ref *new_ref = NULL; |
| 1618 | int ret = 0; |
| 1619 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1620 | binder_proc_lock(proc); |
| 1621 | ref = binder_get_ref_for_node_olocked(proc, node, NULL); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1622 | if (!ref) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1623 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1624 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
| 1625 | if (!new_ref) |
| 1626 | return -ENOMEM; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1627 | binder_proc_lock(proc); |
| 1628 | ref = binder_get_ref_for_node_olocked(proc, node, new_ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1629 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1630 | ret = binder_inc_ref_olocked(ref, strong, target_list); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1631 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1632 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1633 | if (new_ref && ref != new_ref) |
| 1634 | /* |
| 1635 | * Another thread created the ref first so |
| 1636 | * free the one we allocated |
| 1637 | */ |
| 1638 | kfree(new_ref); |
| 1639 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1640 | } |
| 1641 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1642 | static void binder_pop_transaction_ilocked(struct binder_thread *target_thread, |
| 1643 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1644 | { |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1645 | BUG_ON(!target_thread); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1646 | BUG_ON(!spin_is_locked(&target_thread->proc->inner_lock)); |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1647 | BUG_ON(target_thread->transaction_stack != t); |
| 1648 | BUG_ON(target_thread->transaction_stack->from != target_thread); |
| 1649 | target_thread->transaction_stack = |
| 1650 | target_thread->transaction_stack->from_parent; |
| 1651 | t->from = NULL; |
| 1652 | } |
| 1653 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1654 | /** |
| 1655 | * binder_thread_dec_tmpref() - decrement thread->tmp_ref |
| 1656 | * @thread: thread to decrement |
| 1657 | * |
| 1658 | * A thread needs to be kept alive while being used to create or |
| 1659 | * handle a transaction. binder_get_txn_from() is used to safely |
| 1660 | * extract t->from from a binder_transaction and keep the thread |
| 1661 | * indicated by t->from from being freed. When done with that |
| 1662 | * binder_thread, this function is called to decrement the |
| 1663 | * tmp_ref and free if appropriate (thread has been released |
| 1664 | * and no transaction being processed by the driver) |
| 1665 | */ |
| 1666 | static void binder_thread_dec_tmpref(struct binder_thread *thread) |
| 1667 | { |
| 1668 | /* |
| 1669 | * atomic is used to protect the counter value while |
| 1670 | * it cannot reach zero or thread->is_dead is false |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1671 | */ |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1672 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1673 | atomic_dec(&thread->tmp_ref); |
| 1674 | if (thread->is_dead && !atomic_read(&thread->tmp_ref)) { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1675 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1676 | binder_free_thread(thread); |
| 1677 | return; |
| 1678 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1679 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | /** |
| 1683 | * binder_proc_dec_tmpref() - decrement proc->tmp_ref |
| 1684 | * @proc: proc to decrement |
| 1685 | * |
| 1686 | * A binder_proc needs to be kept alive while being used to create or |
| 1687 | * handle a transaction. proc->tmp_ref is incremented when |
| 1688 | * creating a new transaction or the binder_proc is currently in-use |
| 1689 | * by threads that are being released. When done with the binder_proc, |
| 1690 | * this function is called to decrement the counter and free the |
| 1691 | * proc if appropriate (proc has been released, all threads have |
| 1692 | * been released and not currenly in-use to process a transaction). |
| 1693 | */ |
| 1694 | static void binder_proc_dec_tmpref(struct binder_proc *proc) |
| 1695 | { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1696 | binder_inner_proc_lock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1697 | proc->tmp_ref--; |
| 1698 | if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) && |
| 1699 | !proc->tmp_ref) { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1700 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1701 | binder_free_proc(proc); |
| 1702 | return; |
| 1703 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 1704 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | /** |
| 1708 | * binder_get_txn_from() - safely extract the "from" thread in transaction |
| 1709 | * @t: binder transaction for t->from |
| 1710 | * |
| 1711 | * Atomically return the "from" thread and increment the tmp_ref |
| 1712 | * count for the thread to ensure it stays alive until |
| 1713 | * binder_thread_dec_tmpref() is called. |
| 1714 | * |
| 1715 | * Return: the value of t->from |
| 1716 | */ |
| 1717 | static struct binder_thread *binder_get_txn_from( |
| 1718 | struct binder_transaction *t) |
| 1719 | { |
| 1720 | struct binder_thread *from; |
| 1721 | |
| 1722 | spin_lock(&t->lock); |
| 1723 | from = t->from; |
| 1724 | if (from) |
| 1725 | atomic_inc(&from->tmp_ref); |
| 1726 | spin_unlock(&t->lock); |
| 1727 | return from; |
| 1728 | } |
| 1729 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1730 | /** |
| 1731 | * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock |
| 1732 | * @t: binder transaction for t->from |
| 1733 | * |
| 1734 | * Same as binder_get_txn_from() except it also acquires the proc->inner_lock |
| 1735 | * to guarantee that the thread cannot be released while operating on it. |
| 1736 | * The caller must call binder_inner_proc_unlock() to release the inner lock |
| 1737 | * as well as call binder_dec_thread_txn() to release the reference. |
| 1738 | * |
| 1739 | * Return: the value of t->from |
| 1740 | */ |
| 1741 | static struct binder_thread *binder_get_txn_from_and_acq_inner( |
| 1742 | struct binder_transaction *t) |
| 1743 | { |
| 1744 | struct binder_thread *from; |
| 1745 | |
| 1746 | from = binder_get_txn_from(t); |
| 1747 | if (!from) |
| 1748 | return NULL; |
| 1749 | binder_inner_proc_lock(from->proc); |
| 1750 | if (t->from) { |
| 1751 | BUG_ON(from != t->from); |
| 1752 | return from; |
| 1753 | } |
| 1754 | binder_inner_proc_unlock(from->proc); |
| 1755 | binder_thread_dec_tmpref(from); |
| 1756 | return NULL; |
| 1757 | } |
| 1758 | |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1759 | static void binder_free_transaction(struct binder_transaction *t) |
| 1760 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1761 | if (t->buffer) |
| 1762 | t->buffer->transaction = NULL; |
| 1763 | kfree(t); |
| 1764 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 1765 | } |
| 1766 | |
| 1767 | static void binder_send_failed_reply(struct binder_transaction *t, |
| 1768 | uint32_t error_code) |
| 1769 | { |
| 1770 | struct binder_thread *target_thread; |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 1771 | struct binder_transaction *next; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1772 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1773 | BUG_ON(t->flags & TF_ONE_WAY); |
| 1774 | while (1) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1775 | target_thread = binder_get_txn_from_and_acq_inner(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1776 | if (target_thread) { |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1777 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 1778 | "send failed reply for transaction %d to %d:%d\n", |
| 1779 | t->debug_id, |
| 1780 | target_thread->proc->pid, |
| 1781 | target_thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1782 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1783 | binder_pop_transaction_ilocked(target_thread, t); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1784 | if (target_thread->reply_error.cmd == BR_OK) { |
| 1785 | target_thread->reply_error.cmd = error_code; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1786 | binder_enqueue_work_ilocked( |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1787 | &target_thread->reply_error.work, |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1788 | &target_thread->todo); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1789 | wake_up_interruptible(&target_thread->wait); |
| 1790 | } else { |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1791 | WARN(1, "Unexpected reply error: %u\n", |
| 1792 | target_thread->reply_error.cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1793 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 1794 | binder_inner_proc_unlock(target_thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 1795 | binder_thread_dec_tmpref(target_thread); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 1796 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1797 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1798 | } |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 1799 | next = t->from_parent; |
| 1800 | |
| 1801 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 1802 | "send failed reply for transaction %d, target dead\n", |
| 1803 | t->debug_id); |
| 1804 | |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 1805 | binder_free_transaction(t); |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 1806 | if (next == NULL) { |
| 1807 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 1808 | "reply failed, no target thread at root\n"); |
| 1809 | return; |
| 1810 | } |
| 1811 | t = next; |
| 1812 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 1813 | "reply failed, no target thread -- retry %d\n", |
| 1814 | t->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1815 | } |
| 1816 | } |
| 1817 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 1818 | /** |
| 1819 | * binder_validate_object() - checks for a valid metadata object in a buffer. |
| 1820 | * @buffer: binder_buffer that we're parsing. |
| 1821 | * @offset: offset in the buffer at which to validate an object. |
| 1822 | * |
| 1823 | * Return: If there's a valid metadata object at @offset in @buffer, the |
| 1824 | * size of that object. Otherwise, it returns zero. |
| 1825 | */ |
| 1826 | static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset) |
| 1827 | { |
| 1828 | /* Check if we can read a header first */ |
| 1829 | struct binder_object_header *hdr; |
| 1830 | size_t object_size = 0; |
| 1831 | |
| 1832 | if (offset > buffer->data_size - sizeof(*hdr) || |
| 1833 | buffer->data_size < sizeof(*hdr) || |
| 1834 | !IS_ALIGNED(offset, sizeof(u32))) |
| 1835 | return 0; |
| 1836 | |
| 1837 | /* Ok, now see if we can read a complete object. */ |
| 1838 | hdr = (struct binder_object_header *)(buffer->data + offset); |
| 1839 | switch (hdr->type) { |
| 1840 | case BINDER_TYPE_BINDER: |
| 1841 | case BINDER_TYPE_WEAK_BINDER: |
| 1842 | case BINDER_TYPE_HANDLE: |
| 1843 | case BINDER_TYPE_WEAK_HANDLE: |
| 1844 | object_size = sizeof(struct flat_binder_object); |
| 1845 | break; |
| 1846 | case BINDER_TYPE_FD: |
| 1847 | object_size = sizeof(struct binder_fd_object); |
| 1848 | break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 1849 | case BINDER_TYPE_PTR: |
| 1850 | object_size = sizeof(struct binder_buffer_object); |
| 1851 | break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 1852 | case BINDER_TYPE_FDA: |
| 1853 | object_size = sizeof(struct binder_fd_array_object); |
| 1854 | break; |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 1855 | default: |
| 1856 | return 0; |
| 1857 | } |
| 1858 | if (offset <= buffer->data_size - object_size && |
| 1859 | buffer->data_size >= object_size) |
| 1860 | return object_size; |
| 1861 | else |
| 1862 | return 0; |
| 1863 | } |
| 1864 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 1865 | /** |
| 1866 | * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer. |
| 1867 | * @b: binder_buffer containing the object |
| 1868 | * @index: index in offset array at which the binder_buffer_object is |
| 1869 | * located |
| 1870 | * @start: points to the start of the offset array |
| 1871 | * @num_valid: the number of valid offsets in the offset array |
| 1872 | * |
| 1873 | * Return: If @index is within the valid range of the offset array |
| 1874 | * described by @start and @num_valid, and if there's a valid |
| 1875 | * binder_buffer_object at the offset found in index @index |
| 1876 | * of the offset array, that object is returned. Otherwise, |
| 1877 | * %NULL is returned. |
| 1878 | * Note that the offset found in index @index itself is not |
| 1879 | * verified; this function assumes that @num_valid elements |
| 1880 | * from @start were previously verified to have valid offsets. |
| 1881 | */ |
| 1882 | static struct binder_buffer_object *binder_validate_ptr(struct binder_buffer *b, |
| 1883 | binder_size_t index, |
| 1884 | binder_size_t *start, |
| 1885 | binder_size_t num_valid) |
| 1886 | { |
| 1887 | struct binder_buffer_object *buffer_obj; |
| 1888 | binder_size_t *offp; |
| 1889 | |
| 1890 | if (index >= num_valid) |
| 1891 | return NULL; |
| 1892 | |
| 1893 | offp = start + index; |
| 1894 | buffer_obj = (struct binder_buffer_object *)(b->data + *offp); |
| 1895 | if (buffer_obj->hdr.type != BINDER_TYPE_PTR) |
| 1896 | return NULL; |
| 1897 | |
| 1898 | return buffer_obj; |
| 1899 | } |
| 1900 | |
| 1901 | /** |
| 1902 | * binder_validate_fixup() - validates pointer/fd fixups happen in order. |
| 1903 | * @b: transaction buffer |
| 1904 | * @objects_start start of objects buffer |
| 1905 | * @buffer: binder_buffer_object in which to fix up |
| 1906 | * @offset: start offset in @buffer to fix up |
| 1907 | * @last_obj: last binder_buffer_object that we fixed up in |
| 1908 | * @last_min_offset: minimum fixup offset in @last_obj |
| 1909 | * |
| 1910 | * Return: %true if a fixup in buffer @buffer at offset @offset is |
| 1911 | * allowed. |
| 1912 | * |
| 1913 | * For safety reasons, we only allow fixups inside a buffer to happen |
| 1914 | * at increasing offsets; additionally, we only allow fixup on the last |
| 1915 | * buffer object that was verified, or one of its parents. |
| 1916 | * |
| 1917 | * Example of what is allowed: |
| 1918 | * |
| 1919 | * A |
| 1920 | * B (parent = A, offset = 0) |
| 1921 | * C (parent = A, offset = 16) |
| 1922 | * D (parent = C, offset = 0) |
| 1923 | * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset) |
| 1924 | * |
| 1925 | * Examples of what is not allowed: |
| 1926 | * |
| 1927 | * Decreasing offsets within the same parent: |
| 1928 | * A |
| 1929 | * C (parent = A, offset = 16) |
| 1930 | * B (parent = A, offset = 0) // decreasing offset within A |
| 1931 | * |
| 1932 | * Referring to a parent that wasn't the last object or any of its parents: |
| 1933 | * A |
| 1934 | * B (parent = A, offset = 0) |
| 1935 | * C (parent = A, offset = 0) |
| 1936 | * C (parent = A, offset = 16) |
| 1937 | * D (parent = B, offset = 0) // B is not A or any of A's parents |
| 1938 | */ |
| 1939 | static bool binder_validate_fixup(struct binder_buffer *b, |
| 1940 | binder_size_t *objects_start, |
| 1941 | struct binder_buffer_object *buffer, |
| 1942 | binder_size_t fixup_offset, |
| 1943 | struct binder_buffer_object *last_obj, |
| 1944 | binder_size_t last_min_offset) |
| 1945 | { |
| 1946 | if (!last_obj) { |
| 1947 | /* Nothing to fix up in */ |
| 1948 | return false; |
| 1949 | } |
| 1950 | |
| 1951 | while (last_obj != buffer) { |
| 1952 | /* |
| 1953 | * Safe to retrieve the parent of last_obj, since it |
| 1954 | * was already previously verified by the driver. |
| 1955 | */ |
| 1956 | if ((last_obj->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0) |
| 1957 | return false; |
| 1958 | last_min_offset = last_obj->parent_offset + sizeof(uintptr_t); |
| 1959 | last_obj = (struct binder_buffer_object *) |
| 1960 | (b->data + *(objects_start + last_obj->parent)); |
| 1961 | } |
| 1962 | return (fixup_offset >= last_min_offset); |
| 1963 | } |
| 1964 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1965 | static void binder_transaction_buffer_release(struct binder_proc *proc, |
| 1966 | struct binder_buffer *buffer, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1967 | binder_size_t *failed_at) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1968 | { |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 1969 | binder_size_t *offp, *off_start, *off_end; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1970 | int debug_id = buffer->debug_id; |
| 1971 | |
| 1972 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1973 | "%d buffer release %d, size %zd-%zd, failed at %p\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1974 | proc->pid, buffer->debug_id, |
| 1975 | buffer->data_size, buffer->offsets_size, failed_at); |
| 1976 | |
| 1977 | if (buffer->target_node) |
| 1978 | binder_dec_node(buffer->target_node, 1, 0); |
| 1979 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 1980 | off_start = (binder_size_t *)(buffer->data + |
| 1981 | ALIGN(buffer->data_size, sizeof(void *))); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1982 | if (failed_at) |
| 1983 | off_end = failed_at; |
| 1984 | else |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 1985 | off_end = (void *)off_start + buffer->offsets_size; |
| 1986 | for (offp = off_start; offp < off_end; offp++) { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 1987 | struct binder_object_header *hdr; |
| 1988 | size_t object_size = binder_validate_object(buffer, *offp); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1989 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 1990 | if (object_size == 0) { |
| 1991 | 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] | 1992 | debug_id, (u64)*offp, buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1993 | continue; |
| 1994 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 1995 | hdr = (struct binder_object_header *)(buffer->data + *offp); |
| 1996 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1997 | case BINDER_TYPE_BINDER: |
| 1998 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 1999 | struct flat_binder_object *fp; |
| 2000 | struct binder_node *node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2001 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2002 | fp = to_flat_binder_object(hdr); |
| 2003 | node = binder_get_node(proc, fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2004 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2005 | pr_err("transaction release %d bad node %016llx\n", |
| 2006 | debug_id, (u64)fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2007 | break; |
| 2008 | } |
| 2009 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2010 | " node %d u%016llx\n", |
| 2011 | node->debug_id, (u64)node->ptr); |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2012 | binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER, |
| 2013 | 0); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2014 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2015 | } break; |
| 2016 | case BINDER_TYPE_HANDLE: |
| 2017 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2018 | struct flat_binder_object *fp; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2019 | struct binder_ref_data rdata; |
| 2020 | int ret; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 2021 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2022 | fp = to_flat_binder_object(hdr); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2023 | ret = binder_dec_ref_for_handle(proc, fp->handle, |
| 2024 | hdr->type == BINDER_TYPE_HANDLE, &rdata); |
| 2025 | |
| 2026 | if (ret) { |
| 2027 | pr_err("transaction release %d bad handle %d, ret = %d\n", |
| 2028 | debug_id, fp->handle, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2029 | break; |
| 2030 | } |
| 2031 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2032 | " ref %d desc %d\n", |
| 2033 | rdata.debug_id, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2034 | } break; |
| 2035 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2036 | case BINDER_TYPE_FD: { |
| 2037 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
| 2038 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2039 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2040 | " fd %d\n", fp->fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2041 | if (failed_at) |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2042 | task_close_fd(proc, fp->fd); |
| 2043 | } break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2044 | case BINDER_TYPE_PTR: |
| 2045 | /* |
| 2046 | * Nothing to do here, this will get cleaned up when the |
| 2047 | * transaction buffer gets freed |
| 2048 | */ |
| 2049 | break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2050 | case BINDER_TYPE_FDA: { |
| 2051 | struct binder_fd_array_object *fda; |
| 2052 | struct binder_buffer_object *parent; |
| 2053 | uintptr_t parent_buffer; |
| 2054 | u32 *fd_array; |
| 2055 | size_t fd_index; |
| 2056 | binder_size_t fd_buf_size; |
| 2057 | |
| 2058 | fda = to_binder_fd_array_object(hdr); |
| 2059 | parent = binder_validate_ptr(buffer, fda->parent, |
| 2060 | off_start, |
| 2061 | offp - off_start); |
| 2062 | if (!parent) { |
| 2063 | pr_err("transaction release %d bad parent offset", |
| 2064 | debug_id); |
| 2065 | continue; |
| 2066 | } |
| 2067 | /* |
| 2068 | * Since the parent was already fixed up, convert it |
| 2069 | * back to kernel address space to access it |
| 2070 | */ |
| 2071 | parent_buffer = parent->buffer - |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2072 | binder_alloc_get_user_buffer_offset( |
| 2073 | &proc->alloc); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2074 | |
| 2075 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2076 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2077 | pr_err("transaction release %d invalid number of fds (%lld)\n", |
| 2078 | debug_id, (u64)fda->num_fds); |
| 2079 | continue; |
| 2080 | } |
| 2081 | if (fd_buf_size > parent->length || |
| 2082 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2083 | /* No space for all file descriptors here. */ |
| 2084 | pr_err("transaction release %d not enough space for %lld fds in buffer\n", |
| 2085 | debug_id, (u64)fda->num_fds); |
| 2086 | continue; |
| 2087 | } |
| 2088 | fd_array = (u32 *)(parent_buffer + fda->parent_offset); |
| 2089 | for (fd_index = 0; fd_index < fda->num_fds; fd_index++) |
| 2090 | task_close_fd(proc, fd_array[fd_index]); |
| 2091 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2092 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 2093 | pr_err("transaction release %d bad object type %x\n", |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2094 | debug_id, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2095 | break; |
| 2096 | } |
| 2097 | } |
| 2098 | } |
| 2099 | |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2100 | static int binder_translate_binder(struct flat_binder_object *fp, |
| 2101 | struct binder_transaction *t, |
| 2102 | struct binder_thread *thread) |
| 2103 | { |
| 2104 | struct binder_node *node; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2105 | struct binder_proc *proc = thread->proc; |
| 2106 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2107 | struct binder_ref_data rdata; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2108 | int ret = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2109 | |
| 2110 | node = binder_get_node(proc, fp->binder); |
| 2111 | if (!node) { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2112 | node = binder_new_node(proc, fp); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2113 | if (!node) |
| 2114 | return -ENOMEM; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2115 | } |
| 2116 | if (fp->cookie != node->cookie) { |
| 2117 | binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n", |
| 2118 | proc->pid, thread->pid, (u64)fp->binder, |
| 2119 | node->debug_id, (u64)fp->cookie, |
| 2120 | (u64)node->cookie); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2121 | ret = -EINVAL; |
| 2122 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2123 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2124 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2125 | ret = -EPERM; |
| 2126 | goto done; |
| 2127 | } |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2128 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2129 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2130 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2131 | &thread->todo, &rdata); |
| 2132 | if (ret) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2133 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2134 | |
| 2135 | if (fp->hdr.type == BINDER_TYPE_BINDER) |
| 2136 | fp->hdr.type = BINDER_TYPE_HANDLE; |
| 2137 | else |
| 2138 | fp->hdr.type = BINDER_TYPE_WEAK_HANDLE; |
| 2139 | fp->binder = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2140 | fp->handle = rdata.desc; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2141 | fp->cookie = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2142 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2143 | trace_binder_transaction_node_to_ref(t, node, &rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2144 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2145 | " node %d u%016llx -> ref %d desc %d\n", |
| 2146 | node->debug_id, (u64)node->ptr, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2147 | rdata.debug_id, rdata.desc); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2148 | done: |
| 2149 | binder_put_node(node); |
| 2150 | return ret; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | static int binder_translate_handle(struct flat_binder_object *fp, |
| 2154 | struct binder_transaction *t, |
| 2155 | struct binder_thread *thread) |
| 2156 | { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2157 | struct binder_proc *proc = thread->proc; |
| 2158 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2159 | struct binder_node *node; |
| 2160 | struct binder_ref_data src_rdata; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2161 | int ret = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2162 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2163 | node = binder_get_node_from_ref(proc, fp->handle, |
| 2164 | fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata); |
| 2165 | if (!node) { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2166 | binder_user_error("%d:%d got transaction with invalid handle, %d\n", |
| 2167 | proc->pid, thread->pid, fp->handle); |
| 2168 | return -EINVAL; |
| 2169 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2170 | if (security_binder_transfer_binder(proc->tsk, target_proc->tsk)) { |
| 2171 | ret = -EPERM; |
| 2172 | goto done; |
| 2173 | } |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2174 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2175 | binder_node_lock(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2176 | if (node->proc == target_proc) { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2177 | if (fp->hdr.type == BINDER_TYPE_HANDLE) |
| 2178 | fp->hdr.type = BINDER_TYPE_BINDER; |
| 2179 | else |
| 2180 | fp->hdr.type = BINDER_TYPE_WEAK_BINDER; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2181 | fp->binder = node->ptr; |
| 2182 | fp->cookie = node->cookie; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2183 | if (node->proc) |
| 2184 | binder_inner_proc_lock(node->proc); |
| 2185 | binder_inc_node_nilocked(node, |
| 2186 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2187 | 0, NULL); |
| 2188 | if (node->proc) |
| 2189 | binder_inner_proc_unlock(node->proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2190 | trace_binder_transaction_ref_to_node(t, node, &src_rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2191 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2192 | " ref %d desc %d -> node %d u%016llx\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2193 | src_rdata.debug_id, src_rdata.desc, node->debug_id, |
| 2194 | (u64)node->ptr); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2195 | binder_node_unlock(node); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2196 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2197 | int ret; |
| 2198 | struct binder_ref_data dest_rdata; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2199 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2200 | binder_node_unlock(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2201 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2202 | fp->hdr.type == BINDER_TYPE_HANDLE, |
| 2203 | NULL, &dest_rdata); |
| 2204 | if (ret) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2205 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2206 | |
| 2207 | fp->binder = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2208 | fp->handle = dest_rdata.desc; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2209 | fp->cookie = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2210 | trace_binder_transaction_ref_to_ref(t, node, &src_rdata, |
| 2211 | &dest_rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2212 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2213 | " ref %d desc %d -> ref %d desc %d (node %d)\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2214 | src_rdata.debug_id, src_rdata.desc, |
| 2215 | dest_rdata.debug_id, dest_rdata.desc, |
| 2216 | node->debug_id); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2217 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2218 | done: |
| 2219 | binder_put_node(node); |
| 2220 | return ret; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | static int binder_translate_fd(int fd, |
| 2224 | struct binder_transaction *t, |
| 2225 | struct binder_thread *thread, |
| 2226 | struct binder_transaction *in_reply_to) |
| 2227 | { |
| 2228 | struct binder_proc *proc = thread->proc; |
| 2229 | struct binder_proc *target_proc = t->to_proc; |
| 2230 | int target_fd; |
| 2231 | struct file *file; |
| 2232 | int ret; |
| 2233 | bool target_allows_fd; |
| 2234 | |
| 2235 | if (in_reply_to) |
| 2236 | target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS); |
| 2237 | else |
| 2238 | target_allows_fd = t->buffer->target_node->accept_fds; |
| 2239 | if (!target_allows_fd) { |
| 2240 | binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n", |
| 2241 | proc->pid, thread->pid, |
| 2242 | in_reply_to ? "reply" : "transaction", |
| 2243 | fd); |
| 2244 | ret = -EPERM; |
| 2245 | goto err_fd_not_accepted; |
| 2246 | } |
| 2247 | |
| 2248 | file = fget(fd); |
| 2249 | if (!file) { |
| 2250 | binder_user_error("%d:%d got transaction with invalid fd, %d\n", |
| 2251 | proc->pid, thread->pid, fd); |
| 2252 | ret = -EBADF; |
| 2253 | goto err_fget; |
| 2254 | } |
| 2255 | ret = security_binder_transfer_file(proc->tsk, target_proc->tsk, file); |
| 2256 | if (ret < 0) { |
| 2257 | ret = -EPERM; |
| 2258 | goto err_security; |
| 2259 | } |
| 2260 | |
| 2261 | target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC); |
| 2262 | if (target_fd < 0) { |
| 2263 | ret = -ENOMEM; |
| 2264 | goto err_get_unused_fd; |
| 2265 | } |
| 2266 | task_fd_install(target_proc, target_fd, file); |
| 2267 | trace_binder_transaction_fd(t, fd, target_fd); |
| 2268 | binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n", |
| 2269 | fd, target_fd); |
| 2270 | |
| 2271 | return target_fd; |
| 2272 | |
| 2273 | err_get_unused_fd: |
| 2274 | err_security: |
| 2275 | fput(file); |
| 2276 | err_fget: |
| 2277 | err_fd_not_accepted: |
| 2278 | return ret; |
| 2279 | } |
| 2280 | |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2281 | static int binder_translate_fd_array(struct binder_fd_array_object *fda, |
| 2282 | struct binder_buffer_object *parent, |
| 2283 | struct binder_transaction *t, |
| 2284 | struct binder_thread *thread, |
| 2285 | struct binder_transaction *in_reply_to) |
| 2286 | { |
| 2287 | binder_size_t fdi, fd_buf_size, num_installed_fds; |
| 2288 | int target_fd; |
| 2289 | uintptr_t parent_buffer; |
| 2290 | u32 *fd_array; |
| 2291 | struct binder_proc *proc = thread->proc; |
| 2292 | struct binder_proc *target_proc = t->to_proc; |
| 2293 | |
| 2294 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2295 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2296 | binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n", |
| 2297 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2298 | return -EINVAL; |
| 2299 | } |
| 2300 | if (fd_buf_size > parent->length || |
| 2301 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2302 | /* No space for all file descriptors here. */ |
| 2303 | binder_user_error("%d:%d not enough space to store %lld fds in buffer\n", |
| 2304 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2305 | return -EINVAL; |
| 2306 | } |
| 2307 | /* |
| 2308 | * Since the parent was already fixed up, convert it |
| 2309 | * back to the kernel address space to access it |
| 2310 | */ |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2311 | parent_buffer = parent->buffer - |
| 2312 | binder_alloc_get_user_buffer_offset(&target_proc->alloc); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2313 | fd_array = (u32 *)(parent_buffer + fda->parent_offset); |
| 2314 | if (!IS_ALIGNED((unsigned long)fd_array, sizeof(u32))) { |
| 2315 | binder_user_error("%d:%d parent offset not aligned correctly.\n", |
| 2316 | proc->pid, thread->pid); |
| 2317 | return -EINVAL; |
| 2318 | } |
| 2319 | for (fdi = 0; fdi < fda->num_fds; fdi++) { |
| 2320 | target_fd = binder_translate_fd(fd_array[fdi], t, thread, |
| 2321 | in_reply_to); |
| 2322 | if (target_fd < 0) |
| 2323 | goto err_translate_fd_failed; |
| 2324 | fd_array[fdi] = target_fd; |
| 2325 | } |
| 2326 | return 0; |
| 2327 | |
| 2328 | err_translate_fd_failed: |
| 2329 | /* |
| 2330 | * Failed to allocate fd or security error, free fds |
| 2331 | * installed so far. |
| 2332 | */ |
| 2333 | num_installed_fds = fdi; |
| 2334 | for (fdi = 0; fdi < num_installed_fds; fdi++) |
| 2335 | task_close_fd(target_proc, fd_array[fdi]); |
| 2336 | return target_fd; |
| 2337 | } |
| 2338 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2339 | static int binder_fixup_parent(struct binder_transaction *t, |
| 2340 | struct binder_thread *thread, |
| 2341 | struct binder_buffer_object *bp, |
| 2342 | binder_size_t *off_start, |
| 2343 | binder_size_t num_valid, |
| 2344 | struct binder_buffer_object *last_fixup_obj, |
| 2345 | binder_size_t last_fixup_min_off) |
| 2346 | { |
| 2347 | struct binder_buffer_object *parent; |
| 2348 | u8 *parent_buffer; |
| 2349 | struct binder_buffer *b = t->buffer; |
| 2350 | struct binder_proc *proc = thread->proc; |
| 2351 | struct binder_proc *target_proc = t->to_proc; |
| 2352 | |
| 2353 | if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT)) |
| 2354 | return 0; |
| 2355 | |
| 2356 | parent = binder_validate_ptr(b, bp->parent, off_start, num_valid); |
| 2357 | if (!parent) { |
| 2358 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 2359 | proc->pid, thread->pid); |
| 2360 | return -EINVAL; |
| 2361 | } |
| 2362 | |
| 2363 | if (!binder_validate_fixup(b, off_start, |
| 2364 | parent, bp->parent_offset, |
| 2365 | last_fixup_obj, |
| 2366 | last_fixup_min_off)) { |
| 2367 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 2368 | proc->pid, thread->pid); |
| 2369 | return -EINVAL; |
| 2370 | } |
| 2371 | |
| 2372 | if (parent->length < sizeof(binder_uintptr_t) || |
| 2373 | bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) { |
| 2374 | /* No space for a pointer here! */ |
| 2375 | binder_user_error("%d:%d got transaction with invalid parent offset\n", |
| 2376 | proc->pid, thread->pid); |
| 2377 | return -EINVAL; |
| 2378 | } |
| 2379 | parent_buffer = (u8 *)(parent->buffer - |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2380 | binder_alloc_get_user_buffer_offset( |
| 2381 | &target_proc->alloc)); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2382 | *(binder_uintptr_t *)(parent_buffer + bp->parent_offset) = bp->buffer; |
| 2383 | |
| 2384 | return 0; |
| 2385 | } |
| 2386 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2387 | static void binder_transaction(struct binder_proc *proc, |
| 2388 | struct binder_thread *thread, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2389 | struct binder_transaction_data *tr, int reply, |
| 2390 | binder_size_t extra_buffers_size) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2391 | { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2392 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2393 | struct binder_transaction *t; |
| 2394 | struct binder_work *tcomplete; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2395 | binder_size_t *offp, *off_end, *off_start; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 2396 | binder_size_t off_min; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2397 | u8 *sg_bufp, *sg_buf_end; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2398 | struct binder_proc *target_proc = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2399 | struct binder_thread *target_thread = NULL; |
| 2400 | struct binder_node *target_node = NULL; |
| 2401 | struct list_head *target_list; |
| 2402 | wait_queue_head_t *target_wait; |
| 2403 | struct binder_transaction *in_reply_to = NULL; |
| 2404 | struct binder_transaction_log_entry *e; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2405 | uint32_t return_error = 0; |
| 2406 | uint32_t return_error_param = 0; |
| 2407 | uint32_t return_error_line = 0; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2408 | struct binder_buffer_object *last_fixup_obj = NULL; |
| 2409 | binder_size_t last_fixup_min_off = 0; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 2410 | struct binder_context *context = proc->context; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2411 | int t_debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2412 | |
| 2413 | e = binder_transaction_log_add(&binder_transaction_log); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2414 | e->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2415 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); |
| 2416 | e->from_proc = proc->pid; |
| 2417 | e->from_thread = thread->pid; |
| 2418 | e->target_handle = tr->target.handle; |
| 2419 | e->data_size = tr->data_size; |
| 2420 | e->offsets_size = tr->offsets_size; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 2421 | e->context_name = proc->context->name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2422 | |
| 2423 | if (reply) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2424 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2425 | in_reply_to = thread->transaction_stack; |
| 2426 | if (in_reply_to == NULL) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2427 | binder_inner_proc_unlock(proc); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2428 | 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] | 2429 | proc->pid, thread->pid); |
| 2430 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2431 | return_error_param = -EPROTO; |
| 2432 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2433 | goto err_empty_call_stack; |
| 2434 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2435 | if (in_reply_to->to_thread != thread) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2436 | spin_lock(&in_reply_to->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2437 | 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] | 2438 | proc->pid, thread->pid, in_reply_to->debug_id, |
| 2439 | in_reply_to->to_proc ? |
| 2440 | in_reply_to->to_proc->pid : 0, |
| 2441 | in_reply_to->to_thread ? |
| 2442 | in_reply_to->to_thread->pid : 0); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2443 | spin_unlock(&in_reply_to->lock); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2444 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2445 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2446 | return_error_param = -EPROTO; |
| 2447 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2448 | in_reply_to = NULL; |
| 2449 | goto err_bad_call_stack; |
| 2450 | } |
| 2451 | thread->transaction_stack = in_reply_to->to_parent; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2452 | binder_inner_proc_unlock(proc); |
| 2453 | binder_set_nice(in_reply_to->saved_priority); |
| 2454 | 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] | 2455 | if (target_thread == NULL) { |
| 2456 | return_error = BR_DEAD_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2457 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2458 | goto err_dead_binder; |
| 2459 | } |
| 2460 | if (target_thread->transaction_stack != in_reply_to) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2461 | 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] | 2462 | proc->pid, thread->pid, |
| 2463 | target_thread->transaction_stack ? |
| 2464 | target_thread->transaction_stack->debug_id : 0, |
| 2465 | in_reply_to->debug_id); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2466 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2467 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2468 | return_error_param = -EPROTO; |
| 2469 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2470 | in_reply_to = NULL; |
| 2471 | target_thread = NULL; |
| 2472 | goto err_dead_binder; |
| 2473 | } |
| 2474 | target_proc = target_thread->proc; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2475 | target_proc->tmp_ref++; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2476 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2477 | } else { |
| 2478 | if (tr->target.handle) { |
| 2479 | struct binder_ref *ref; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2480 | |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2481 | /* |
| 2482 | * There must already be a strong ref |
| 2483 | * on this node. If so, do a strong |
| 2484 | * increment on the node to ensure it |
| 2485 | * stays alive until the transaction is |
| 2486 | * done. |
| 2487 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 2488 | binder_proc_lock(proc); |
| 2489 | ref = binder_get_ref_olocked(proc, tr->target.handle, |
| 2490 | true); |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2491 | if (ref) { |
| 2492 | binder_inc_node(ref->node, 1, 0, NULL); |
| 2493 | target_node = ref->node; |
| 2494 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 2495 | binder_proc_unlock(proc); |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2496 | if (target_node == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2497 | binder_user_error("%d:%d got transaction to invalid handle\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2498 | proc->pid, thread->pid); |
| 2499 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2500 | return_error_param = -EINVAL; |
| 2501 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2502 | goto err_invalid_target_handle; |
| 2503 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2504 | } else { |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 2505 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 2506 | target_node = context->binder_context_mgr_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2507 | if (target_node == NULL) { |
| 2508 | return_error = BR_DEAD_REPLY; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 2509 | mutex_unlock(&context->context_mgr_node_lock); |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2510 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2511 | goto err_no_context_mgr_node; |
| 2512 | } |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2513 | binder_inc_node(target_node, 1, 0, NULL); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 2514 | mutex_unlock(&context->context_mgr_node_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2515 | } |
| 2516 | e->to_node = target_node->debug_id; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2517 | binder_node_lock(target_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2518 | target_proc = target_node->proc; |
| 2519 | if (target_proc == NULL) { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2520 | binder_node_unlock(target_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2521 | return_error = BR_DEAD_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2522 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2523 | goto err_dead_binder; |
| 2524 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 2525 | binder_inner_proc_lock(target_proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2526 | target_proc->tmp_ref++; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 2527 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2528 | binder_node_unlock(target_node); |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2529 | if (security_binder_transaction(proc->tsk, |
| 2530 | target_proc->tsk) < 0) { |
| 2531 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2532 | return_error_param = -EPERM; |
| 2533 | return_error_line = __LINE__; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2534 | goto err_invalid_target_handle; |
| 2535 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2536 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2537 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { |
| 2538 | struct binder_transaction *tmp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2539 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2540 | tmp = thread->transaction_stack; |
| 2541 | if (tmp->to_thread != thread) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2542 | spin_lock(&tmp->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2543 | 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] | 2544 | proc->pid, thread->pid, tmp->debug_id, |
| 2545 | tmp->to_proc ? tmp->to_proc->pid : 0, |
| 2546 | tmp->to_thread ? |
| 2547 | tmp->to_thread->pid : 0); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2548 | spin_unlock(&tmp->lock); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2549 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2550 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2551 | return_error_param = -EPROTO; |
| 2552 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2553 | goto err_bad_call_stack; |
| 2554 | } |
| 2555 | while (tmp) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2556 | struct binder_thread *from; |
| 2557 | |
| 2558 | spin_lock(&tmp->lock); |
| 2559 | from = tmp->from; |
| 2560 | if (from && from->proc == target_proc) { |
| 2561 | atomic_inc(&from->tmp_ref); |
| 2562 | target_thread = from; |
| 2563 | spin_unlock(&tmp->lock); |
| 2564 | break; |
| 2565 | } |
| 2566 | spin_unlock(&tmp->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2567 | tmp = tmp->from_parent; |
| 2568 | } |
| 2569 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2570 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2571 | } |
| 2572 | if (target_thread) { |
| 2573 | e->to_thread = target_thread->pid; |
| 2574 | target_list = &target_thread->todo; |
| 2575 | target_wait = &target_thread->wait; |
| 2576 | } else { |
| 2577 | target_list = &target_proc->todo; |
| 2578 | target_wait = &target_proc->wait; |
| 2579 | } |
| 2580 | e->to_proc = target_proc->pid; |
| 2581 | |
| 2582 | /* TODO: reuse incoming transaction for reply */ |
| 2583 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 2584 | if (t == NULL) { |
| 2585 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2586 | return_error_param = -ENOMEM; |
| 2587 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2588 | goto err_alloc_t_failed; |
| 2589 | } |
| 2590 | binder_stats_created(BINDER_STAT_TRANSACTION); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2591 | spin_lock_init(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2592 | |
| 2593 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); |
| 2594 | if (tcomplete == NULL) { |
| 2595 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2596 | return_error_param = -ENOMEM; |
| 2597 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2598 | goto err_alloc_tcomplete_failed; |
| 2599 | } |
| 2600 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); |
| 2601 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2602 | t->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2603 | |
| 2604 | if (reply) |
| 2605 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2606 | "%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] | 2607 | proc->pid, thread->pid, t->debug_id, |
| 2608 | target_proc->pid, target_thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2609 | (u64)tr->data.ptr.buffer, |
| 2610 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2611 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 2612 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2613 | else |
| 2614 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2615 | "%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] | 2616 | proc->pid, thread->pid, t->debug_id, |
| 2617 | target_proc->pid, target_node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2618 | (u64)tr->data.ptr.buffer, |
| 2619 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2620 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 2621 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2622 | |
| 2623 | if (!reply && !(tr->flags & TF_ONE_WAY)) |
| 2624 | t->from = thread; |
| 2625 | else |
| 2626 | t->from = NULL; |
Tair Rzayev | 57bab7c | 2014-05-31 22:43:34 +0300 | [diff] [blame] | 2627 | t->sender_euid = task_euid(proc->tsk); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2628 | t->to_proc = target_proc; |
| 2629 | t->to_thread = target_thread; |
| 2630 | t->code = tr->code; |
| 2631 | t->flags = tr->flags; |
| 2632 | t->priority = task_nice(current); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2633 | |
| 2634 | trace_binder_transaction(reply, t, target_node); |
| 2635 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2636 | t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 2637 | tr->offsets_size, extra_buffers_size, |
| 2638 | !reply && (t->flags & TF_ONE_WAY)); |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2639 | if (IS_ERR(t->buffer)) { |
| 2640 | /* |
| 2641 | * -ESRCH indicates VMA cleared. The target is dying. |
| 2642 | */ |
| 2643 | return_error_param = PTR_ERR(t->buffer); |
| 2644 | return_error = return_error_param == -ESRCH ? |
| 2645 | BR_DEAD_REPLY : BR_FAILED_REPLY; |
| 2646 | return_error_line = __LINE__; |
| 2647 | t->buffer = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2648 | goto err_binder_alloc_buf_failed; |
| 2649 | } |
| 2650 | t->buffer->allow_user_free = 0; |
| 2651 | t->buffer->debug_id = t->debug_id; |
| 2652 | t->buffer->transaction = t; |
| 2653 | t->buffer->target_node = target_node; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2654 | trace_binder_transaction_alloc_buf(t->buffer); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2655 | off_start = (binder_size_t *)(t->buffer->data + |
| 2656 | ALIGN(tr->data_size, sizeof(void *))); |
| 2657 | offp = off_start; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2658 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2659 | if (copy_from_user(t->buffer->data, (const void __user *)(uintptr_t) |
| 2660 | tr->data.ptr.buffer, tr->data_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2661 | binder_user_error("%d:%d got transaction with invalid data ptr\n", |
| 2662 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2663 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2664 | return_error_param = -EFAULT; |
| 2665 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2666 | goto err_copy_data_failed; |
| 2667 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2668 | if (copy_from_user(offp, (const void __user *)(uintptr_t) |
| 2669 | tr->data.ptr.offsets, tr->offsets_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 2670 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 2671 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2672 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2673 | return_error_param = -EFAULT; |
| 2674 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2675 | goto err_copy_data_failed; |
| 2676 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2677 | if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) { |
| 2678 | binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n", |
| 2679 | proc->pid, thread->pid, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2680 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2681 | return_error_param = -EINVAL; |
| 2682 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2683 | goto err_bad_offset; |
| 2684 | } |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2685 | if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) { |
| 2686 | binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n", |
| 2687 | proc->pid, thread->pid, |
| 2688 | (u64)extra_buffers_size); |
| 2689 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2690 | return_error_param = -EINVAL; |
| 2691 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2692 | goto err_bad_offset; |
| 2693 | } |
| 2694 | off_end = (void *)off_start + tr->offsets_size; |
| 2695 | sg_bufp = (u8 *)(PTR_ALIGN(off_end, sizeof(void *))); |
| 2696 | sg_buf_end = sg_bufp + extra_buffers_size; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 2697 | off_min = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2698 | for (; offp < off_end; offp++) { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2699 | struct binder_object_header *hdr; |
| 2700 | size_t object_size = binder_validate_object(t->buffer, *offp); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2701 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2702 | if (object_size == 0 || *offp < off_min) { |
| 2703 | 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] | 2704 | proc->pid, thread->pid, (u64)*offp, |
| 2705 | (u64)off_min, |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2706 | (u64)t->buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2707 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2708 | return_error_param = -EINVAL; |
| 2709 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2710 | goto err_bad_offset; |
| 2711 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2712 | |
| 2713 | hdr = (struct binder_object_header *)(t->buffer->data + *offp); |
| 2714 | off_min = *offp + object_size; |
| 2715 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2716 | case BINDER_TYPE_BINDER: |
| 2717 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2718 | struct flat_binder_object *fp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2719 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2720 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2721 | ret = binder_translate_binder(fp, t, thread); |
| 2722 | if (ret < 0) { |
Christian Engelmayer | 7d42043 | 2014-05-07 21:44:53 +0200 | [diff] [blame] | 2723 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2724 | return_error_param = ret; |
| 2725 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2726 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2727 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2728 | } break; |
| 2729 | case BINDER_TYPE_HANDLE: |
| 2730 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2731 | struct flat_binder_object *fp; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 2732 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2733 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2734 | ret = binder_translate_handle(fp, t, thread); |
| 2735 | if (ret < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2736 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2737 | return_error_param = ret; |
| 2738 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2739 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2740 | } |
| 2741 | } break; |
| 2742 | |
| 2743 | case BINDER_TYPE_FD: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2744 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2745 | int target_fd = binder_translate_fd(fp->fd, t, thread, |
| 2746 | in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2747 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2748 | if (target_fd < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2749 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2750 | return_error_param = target_fd; |
| 2751 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2752 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2753 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2754 | fp->pad_binder = 0; |
| 2755 | fp->fd = target_fd; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2756 | } break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2757 | case BINDER_TYPE_FDA: { |
| 2758 | struct binder_fd_array_object *fda = |
| 2759 | to_binder_fd_array_object(hdr); |
| 2760 | struct binder_buffer_object *parent = |
| 2761 | binder_validate_ptr(t->buffer, fda->parent, |
| 2762 | off_start, |
| 2763 | offp - off_start); |
| 2764 | if (!parent) { |
| 2765 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 2766 | proc->pid, thread->pid); |
| 2767 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2768 | return_error_param = -EINVAL; |
| 2769 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2770 | goto err_bad_parent; |
| 2771 | } |
| 2772 | if (!binder_validate_fixup(t->buffer, off_start, |
| 2773 | parent, fda->parent_offset, |
| 2774 | last_fixup_obj, |
| 2775 | last_fixup_min_off)) { |
| 2776 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 2777 | proc->pid, thread->pid); |
| 2778 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2779 | return_error_param = -EINVAL; |
| 2780 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2781 | goto err_bad_parent; |
| 2782 | } |
| 2783 | ret = binder_translate_fd_array(fda, parent, t, thread, |
| 2784 | in_reply_to); |
| 2785 | if (ret < 0) { |
| 2786 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2787 | return_error_param = ret; |
| 2788 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2789 | goto err_translate_failed; |
| 2790 | } |
| 2791 | last_fixup_obj = parent; |
| 2792 | last_fixup_min_off = |
| 2793 | fda->parent_offset + sizeof(u32) * fda->num_fds; |
| 2794 | } break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2795 | case BINDER_TYPE_PTR: { |
| 2796 | struct binder_buffer_object *bp = |
| 2797 | to_binder_buffer_object(hdr); |
| 2798 | size_t buf_left = sg_buf_end - sg_bufp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2799 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2800 | if (bp->length > buf_left) { |
| 2801 | binder_user_error("%d:%d got transaction with too large buffer\n", |
| 2802 | proc->pid, thread->pid); |
| 2803 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2804 | return_error_param = -EINVAL; |
| 2805 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2806 | goto err_bad_offset; |
| 2807 | } |
| 2808 | if (copy_from_user(sg_bufp, |
| 2809 | (const void __user *)(uintptr_t) |
| 2810 | bp->buffer, bp->length)) { |
| 2811 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 2812 | proc->pid, thread->pid); |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2813 | return_error_param = -EFAULT; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2814 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2815 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2816 | goto err_copy_data_failed; |
| 2817 | } |
| 2818 | /* Fixup buffer pointer to target proc address space */ |
| 2819 | bp->buffer = (uintptr_t)sg_bufp + |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2820 | binder_alloc_get_user_buffer_offset( |
| 2821 | &target_proc->alloc); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2822 | sg_bufp += ALIGN(bp->length, sizeof(u64)); |
| 2823 | |
| 2824 | ret = binder_fixup_parent(t, thread, bp, off_start, |
| 2825 | offp - off_start, |
| 2826 | last_fixup_obj, |
| 2827 | last_fixup_min_off); |
| 2828 | if (ret < 0) { |
| 2829 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2830 | return_error_param = ret; |
| 2831 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2832 | goto err_translate_failed; |
| 2833 | } |
| 2834 | last_fixup_obj = bp; |
| 2835 | last_fixup_min_off = 0; |
| 2836 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2837 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 2838 | 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] | 2839 | proc->pid, thread->pid, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2840 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2841 | return_error_param = -EINVAL; |
| 2842 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2843 | goto err_bad_object_type; |
| 2844 | } |
| 2845 | } |
Todd Kjos | ccae6f6 | 2017-06-29 12:01:48 -0700 | [diff] [blame] | 2846 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 2847 | binder_enqueue_work(proc, tcomplete, &thread->todo); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2848 | t->work.type = BINDER_WORK_TRANSACTION; |
Todd Kjos | ccae6f6 | 2017-06-29 12:01:48 -0700 | [diff] [blame] | 2849 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2850 | if (reply) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2851 | binder_inner_proc_lock(target_proc); |
| 2852 | if (target_thread->is_dead) { |
| 2853 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2854 | goto err_dead_proc_or_thread; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2855 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2856 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2857 | binder_pop_transaction_ilocked(target_thread, in_reply_to); |
| 2858 | binder_enqueue_work_ilocked(&t->work, target_list); |
| 2859 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 2860 | binder_free_transaction(in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2861 | } else if (!(t->flags & TF_ONE_WAY)) { |
| 2862 | BUG_ON(t->buffer->async_transaction != 0); |
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 | t->need_reply = 1; |
| 2865 | t->from_parent = thread->transaction_stack; |
| 2866 | thread->transaction_stack = t; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2867 | binder_inner_proc_unlock(proc); |
| 2868 | binder_inner_proc_lock(target_proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2869 | if (target_proc->is_dead || |
| 2870 | (target_thread && target_thread->is_dead)) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2871 | binder_inner_proc_unlock(target_proc); |
| 2872 | binder_inner_proc_lock(proc); |
| 2873 | binder_pop_transaction_ilocked(thread, t); |
| 2874 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2875 | goto err_dead_proc_or_thread; |
| 2876 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2877 | binder_enqueue_work_ilocked(&t->work, target_list); |
| 2878 | binder_inner_proc_unlock(target_proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2879 | } else { |
| 2880 | BUG_ON(target_node == NULL); |
| 2881 | BUG_ON(t->buffer->async_transaction != 1); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2882 | binder_node_lock(target_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2883 | if (target_node->has_async_transaction) { |
| 2884 | target_list = &target_node->async_todo; |
| 2885 | target_wait = NULL; |
| 2886 | } else |
| 2887 | target_node->has_async_transaction = 1; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2888 | /* |
| 2889 | * Test/set of has_async_transaction |
| 2890 | * must be atomic with enqueue on |
| 2891 | * async_todo |
| 2892 | */ |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2893 | binder_inner_proc_lock(target_proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2894 | if (target_proc->is_dead || |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2895 | (target_thread && target_thread->is_dead)) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2896 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2897 | binder_node_unlock(target_node); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2898 | goto err_dead_proc_or_thread; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2899 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2900 | binder_enqueue_work_ilocked(&t->work, target_list); |
| 2901 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2902 | binder_node_unlock(target_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2903 | } |
Riley Andrews | 00b40d6 | 2017-06-29 12:01:37 -0700 | [diff] [blame] | 2904 | if (target_wait) { |
Todd Kjos | ccae6f6 | 2017-06-29 12:01:48 -0700 | [diff] [blame] | 2905 | if (reply || !(tr->flags & TF_ONE_WAY)) |
Riley Andrews | 00b40d6 | 2017-06-29 12:01:37 -0700 | [diff] [blame] | 2906 | wake_up_interruptible_sync(target_wait); |
| 2907 | else |
| 2908 | wake_up_interruptible(target_wait); |
| 2909 | } |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2910 | if (target_thread) |
| 2911 | binder_thread_dec_tmpref(target_thread); |
| 2912 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2913 | /* |
| 2914 | * write barrier to synchronize with initialization |
| 2915 | * of log entry |
| 2916 | */ |
| 2917 | smp_wmb(); |
| 2918 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2919 | return; |
| 2920 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2921 | err_dead_proc_or_thread: |
| 2922 | return_error = BR_DEAD_REPLY; |
| 2923 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2924 | err_translate_failed: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2925 | err_bad_object_type: |
| 2926 | err_bad_offset: |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2927 | err_bad_parent: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2928 | err_copy_data_failed: |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 2929 | trace_binder_transaction_failed_buffer_release(t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2930 | binder_transaction_buffer_release(target_proc, t->buffer, offp); |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2931 | target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2932 | t->buffer->transaction = NULL; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 2933 | binder_alloc_free_buf(&target_proc->alloc, t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2934 | err_binder_alloc_buf_failed: |
| 2935 | kfree(tcomplete); |
| 2936 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 2937 | err_alloc_tcomplete_failed: |
| 2938 | kfree(t); |
| 2939 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 2940 | err_alloc_t_failed: |
| 2941 | err_bad_call_stack: |
| 2942 | err_empty_call_stack: |
| 2943 | err_dead_binder: |
| 2944 | err_invalid_target_handle: |
| 2945 | err_no_context_mgr_node: |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2946 | if (target_thread) |
| 2947 | binder_thread_dec_tmpref(target_thread); |
| 2948 | if (target_proc) |
| 2949 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 2950 | if (target_node) |
| 2951 | binder_dec_node(target_node, 1, 0); |
| 2952 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2953 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2954 | "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n", |
| 2955 | proc->pid, thread->pid, return_error, return_error_param, |
| 2956 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 2957 | return_error_line); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2958 | |
| 2959 | { |
| 2960 | struct binder_transaction_log_entry *fe; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2961 | |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 2962 | e->return_error = return_error; |
| 2963 | e->return_error_param = return_error_param; |
| 2964 | e->return_error_line = return_error_line; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2965 | fe = binder_transaction_log_add(&binder_transaction_log_failed); |
| 2966 | *fe = *e; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 2967 | /* |
| 2968 | * write barrier to synchronize with initialization |
| 2969 | * of log entry |
| 2970 | */ |
| 2971 | smp_wmb(); |
| 2972 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
| 2973 | WRITE_ONCE(fe->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2974 | } |
| 2975 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 2976 | BUG_ON(thread->return_error.cmd != BR_OK); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2977 | if (in_reply_to) { |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 2978 | thread->return_error.cmd = BR_TRANSACTION_COMPLETE; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 2979 | binder_enqueue_work(thread->proc, |
| 2980 | &thread->return_error.work, |
| 2981 | &thread->todo); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2982 | binder_send_failed_reply(in_reply_to, return_error); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 2983 | } else { |
| 2984 | thread->return_error.cmd = return_error; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 2985 | binder_enqueue_work(thread->proc, |
| 2986 | &thread->return_error.work, |
| 2987 | &thread->todo); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 2988 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2989 | } |
| 2990 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 2991 | static int binder_thread_write(struct binder_proc *proc, |
| 2992 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2993 | binder_uintptr_t binder_buffer, size_t size, |
| 2994 | binder_size_t *consumed) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2995 | { |
| 2996 | uint32_t cmd; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 2997 | struct binder_context *context = proc->context; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2998 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2999 | void __user *ptr = buffer + *consumed; |
| 3000 | void __user *end = buffer + size; |
| 3001 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3002 | while (ptr < end && thread->return_error.cmd == BR_OK) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3003 | int ret; |
| 3004 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3005 | if (get_user(cmd, (uint32_t __user *)ptr)) |
| 3006 | return -EFAULT; |
| 3007 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3008 | trace_binder_command(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3009 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 3010 | atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]); |
| 3011 | atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]); |
| 3012 | atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3013 | } |
| 3014 | switch (cmd) { |
| 3015 | case BC_INCREFS: |
| 3016 | case BC_ACQUIRE: |
| 3017 | case BC_RELEASE: |
| 3018 | case BC_DECREFS: { |
| 3019 | uint32_t target; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3020 | const char *debug_string; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3021 | bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE; |
| 3022 | bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE; |
| 3023 | struct binder_ref_data rdata; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3024 | |
| 3025 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3026 | return -EFAULT; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3027 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3028 | ptr += sizeof(uint32_t); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3029 | ret = -1; |
| 3030 | if (increment && !target) { |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3031 | struct binder_node *ctx_mgr_node; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3032 | mutex_lock(&context->context_mgr_node_lock); |
| 3033 | ctx_mgr_node = context->binder_context_mgr_node; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3034 | if (ctx_mgr_node) |
| 3035 | ret = binder_inc_ref_for_node( |
| 3036 | proc, ctx_mgr_node, |
| 3037 | strong, NULL, &rdata); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3038 | mutex_unlock(&context->context_mgr_node_lock); |
| 3039 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3040 | if (ret) |
| 3041 | ret = binder_update_ref_for_handle( |
| 3042 | proc, target, increment, strong, |
| 3043 | &rdata); |
| 3044 | if (!ret && rdata.desc != target) { |
| 3045 | binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n", |
| 3046 | proc->pid, thread->pid, |
| 3047 | target, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3048 | } |
| 3049 | switch (cmd) { |
| 3050 | case BC_INCREFS: |
| 3051 | debug_string = "IncRefs"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3052 | break; |
| 3053 | case BC_ACQUIRE: |
| 3054 | debug_string = "Acquire"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3055 | break; |
| 3056 | case BC_RELEASE: |
| 3057 | debug_string = "Release"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3058 | break; |
| 3059 | case BC_DECREFS: |
| 3060 | default: |
| 3061 | debug_string = "DecRefs"; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3062 | break; |
| 3063 | } |
| 3064 | if (ret) { |
| 3065 | binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n", |
| 3066 | proc->pid, thread->pid, debug_string, |
| 3067 | strong, target, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3068 | break; |
| 3069 | } |
| 3070 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3071 | "%d:%d %s ref %d desc %d s %d w %d\n", |
| 3072 | proc->pid, thread->pid, debug_string, |
| 3073 | rdata.debug_id, rdata.desc, rdata.strong, |
| 3074 | rdata.weak); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3075 | break; |
| 3076 | } |
| 3077 | case BC_INCREFS_DONE: |
| 3078 | case BC_ACQUIRE_DONE: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3079 | binder_uintptr_t node_ptr; |
| 3080 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3081 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3082 | bool free_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3083 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3084 | if (get_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3085 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3086 | ptr += sizeof(binder_uintptr_t); |
| 3087 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3088 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3089 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3090 | node = binder_get_node(proc, node_ptr); |
| 3091 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3092 | binder_user_error("%d:%d %s u%016llx no match\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3093 | proc->pid, thread->pid, |
| 3094 | cmd == BC_INCREFS_DONE ? |
| 3095 | "BC_INCREFS_DONE" : |
| 3096 | "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3097 | (u64)node_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3098 | break; |
| 3099 | } |
| 3100 | if (cookie != node->cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3101 | 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] | 3102 | proc->pid, thread->pid, |
| 3103 | cmd == BC_INCREFS_DONE ? |
| 3104 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3105 | (u64)node_ptr, node->debug_id, |
| 3106 | (u64)cookie, (u64)node->cookie); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3107 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3108 | break; |
| 3109 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3110 | binder_node_inner_lock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3111 | if (cmd == BC_ACQUIRE_DONE) { |
| 3112 | if (node->pending_strong_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3113 | 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] | 3114 | proc->pid, thread->pid, |
| 3115 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3116 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3117 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3118 | break; |
| 3119 | } |
| 3120 | node->pending_strong_ref = 0; |
| 3121 | } else { |
| 3122 | if (node->pending_weak_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3123 | 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] | 3124 | proc->pid, thread->pid, |
| 3125 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3126 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3127 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3128 | break; |
| 3129 | } |
| 3130 | node->pending_weak_ref = 0; |
| 3131 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3132 | free_node = binder_dec_node_nilocked(node, |
| 3133 | cmd == BC_ACQUIRE_DONE, 0); |
| 3134 | WARN_ON(free_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3135 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3136 | "%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] | 3137 | proc->pid, thread->pid, |
| 3138 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3139 | node->debug_id, node->local_strong_refs, |
| 3140 | node->local_weak_refs, node->tmp_refs); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3141 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3142 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3143 | break; |
| 3144 | } |
| 3145 | case BC_ATTEMPT_ACQUIRE: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3146 | pr_err("BC_ATTEMPT_ACQUIRE not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3147 | return -EINVAL; |
| 3148 | case BC_ACQUIRE_RESULT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3149 | pr_err("BC_ACQUIRE_RESULT not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3150 | return -EINVAL; |
| 3151 | |
| 3152 | case BC_FREE_BUFFER: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3153 | binder_uintptr_t data_ptr; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3154 | struct binder_buffer *buffer; |
| 3155 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3156 | if (get_user(data_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3157 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3158 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3159 | |
Todd Kjos | 53d311cf | 2017-06-29 12:01:51 -0700 | [diff] [blame] | 3160 | buffer = binder_alloc_prepare_to_free(&proc->alloc, |
| 3161 | data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3162 | if (buffer == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3163 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n", |
| 3164 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3165 | break; |
| 3166 | } |
| 3167 | if (!buffer->allow_user_free) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3168 | binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n", |
| 3169 | proc->pid, thread->pid, (u64)data_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3170 | break; |
| 3171 | } |
| 3172 | binder_debug(BINDER_DEBUG_FREE_BUFFER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3173 | "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n", |
| 3174 | proc->pid, thread->pid, (u64)data_ptr, |
| 3175 | buffer->debug_id, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3176 | buffer->transaction ? "active" : "finished"); |
| 3177 | |
| 3178 | if (buffer->transaction) { |
| 3179 | buffer->transaction->buffer = NULL; |
| 3180 | buffer->transaction = NULL; |
| 3181 | } |
| 3182 | if (buffer->async_transaction && buffer->target_node) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3183 | struct binder_node *buf_node; |
| 3184 | struct binder_work *w; |
| 3185 | |
| 3186 | buf_node = buffer->target_node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3187 | binder_node_inner_lock(buf_node); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3188 | BUG_ON(!buf_node->has_async_transaction); |
| 3189 | BUG_ON(buf_node->proc != proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3190 | w = binder_dequeue_work_head_ilocked( |
| 3191 | &buf_node->async_todo); |
| 3192 | if (!w) |
| 3193 | buf_node->has_async_transaction = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3194 | else |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3195 | binder_enqueue_work_ilocked( |
| 3196 | w, &thread->todo); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3197 | binder_node_inner_unlock(buf_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3198 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3199 | trace_binder_transaction_buffer_release(buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3200 | binder_transaction_buffer_release(proc, buffer, NULL); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 3201 | binder_alloc_free_buf(&proc->alloc, buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3202 | break; |
| 3203 | } |
| 3204 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3205 | case BC_TRANSACTION_SG: |
| 3206 | case BC_REPLY_SG: { |
| 3207 | struct binder_transaction_data_sg tr; |
| 3208 | |
| 3209 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3210 | return -EFAULT; |
| 3211 | ptr += sizeof(tr); |
| 3212 | binder_transaction(proc, thread, &tr.transaction_data, |
| 3213 | cmd == BC_REPLY_SG, tr.buffers_size); |
| 3214 | break; |
| 3215 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3216 | case BC_TRANSACTION: |
| 3217 | case BC_REPLY: { |
| 3218 | struct binder_transaction_data tr; |
| 3219 | |
| 3220 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 3221 | return -EFAULT; |
| 3222 | ptr += sizeof(tr); |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 3223 | binder_transaction(proc, thread, &tr, |
| 3224 | cmd == BC_REPLY, 0); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3225 | break; |
| 3226 | } |
| 3227 | |
| 3228 | case BC_REGISTER_LOOPER: |
| 3229 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3230 | "%d:%d BC_REGISTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3231 | proc->pid, thread->pid); |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3232 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3233 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { |
| 3234 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3235 | 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] | 3236 | proc->pid, thread->pid); |
| 3237 | } else if (proc->requested_threads == 0) { |
| 3238 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3239 | 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] | 3240 | proc->pid, thread->pid); |
| 3241 | } else { |
| 3242 | proc->requested_threads--; |
| 3243 | proc->requested_threads_started++; |
| 3244 | } |
| 3245 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3246 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3247 | break; |
| 3248 | case BC_ENTER_LOOPER: |
| 3249 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3250 | "%d:%d BC_ENTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3251 | proc->pid, thread->pid); |
| 3252 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { |
| 3253 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3254 | 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] | 3255 | proc->pid, thread->pid); |
| 3256 | } |
| 3257 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; |
| 3258 | break; |
| 3259 | case BC_EXIT_LOOPER: |
| 3260 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3261 | "%d:%d BC_EXIT_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3262 | proc->pid, thread->pid); |
| 3263 | thread->looper |= BINDER_LOOPER_STATE_EXITED; |
| 3264 | break; |
| 3265 | |
| 3266 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 3267 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 3268 | uint32_t target; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3269 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3270 | struct binder_ref *ref; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3271 | struct binder_ref_death *death = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3272 | |
| 3273 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3274 | return -EFAULT; |
| 3275 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3276 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3277 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3278 | ptr += sizeof(binder_uintptr_t); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3279 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3280 | /* |
| 3281 | * Allocate memory for death notification |
| 3282 | * before taking lock |
| 3283 | */ |
| 3284 | death = kzalloc(sizeof(*death), GFP_KERNEL); |
| 3285 | if (death == NULL) { |
| 3286 | WARN_ON(thread->return_error.cmd != |
| 3287 | BR_OK); |
| 3288 | thread->return_error.cmd = BR_ERROR; |
| 3289 | binder_enqueue_work( |
| 3290 | thread->proc, |
| 3291 | &thread->return_error.work, |
| 3292 | &thread->todo); |
| 3293 | binder_debug( |
| 3294 | BINDER_DEBUG_FAILED_TRANSACTION, |
| 3295 | "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n", |
| 3296 | proc->pid, thread->pid); |
| 3297 | break; |
| 3298 | } |
| 3299 | } |
| 3300 | binder_proc_lock(proc); |
| 3301 | ref = binder_get_ref_olocked(proc, target, false); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3302 | if (ref == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3303 | binder_user_error("%d:%d %s invalid ref %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3304 | proc->pid, thread->pid, |
| 3305 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3306 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3307 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 3308 | target); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3309 | binder_proc_unlock(proc); |
| 3310 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3311 | break; |
| 3312 | } |
| 3313 | |
| 3314 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3315 | "%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] | 3316 | proc->pid, thread->pid, |
| 3317 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 3318 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 3319 | "BC_CLEAR_DEATH_NOTIFICATION", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3320 | (u64)cookie, ref->data.debug_id, |
| 3321 | ref->data.desc, ref->data.strong, |
| 3322 | ref->data.weak, ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3323 | |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3324 | binder_node_lock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3325 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 3326 | if (ref->death) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3327 | 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] | 3328 | proc->pid, thread->pid); |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3329 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3330 | binder_proc_unlock(proc); |
| 3331 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3332 | break; |
| 3333 | } |
| 3334 | binder_stats_created(BINDER_STAT_DEATH); |
| 3335 | INIT_LIST_HEAD(&death->work.entry); |
| 3336 | death->cookie = cookie; |
| 3337 | ref->death = death; |
| 3338 | if (ref->node->proc == NULL) { |
| 3339 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3340 | if (thread->looper & |
| 3341 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3342 | BINDER_LOOPER_STATE_ENTERED)) |
| 3343 | binder_enqueue_work( |
| 3344 | proc, |
| 3345 | &ref->death->work, |
| 3346 | &thread->todo); |
| 3347 | else { |
| 3348 | binder_enqueue_work( |
| 3349 | proc, |
| 3350 | &ref->death->work, |
| 3351 | &proc->todo); |
| 3352 | wake_up_interruptible( |
| 3353 | &proc->wait); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3354 | } |
| 3355 | } |
| 3356 | } else { |
| 3357 | if (ref->death == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3358 | 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] | 3359 | proc->pid, thread->pid); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3360 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3361 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3362 | break; |
| 3363 | } |
| 3364 | death = ref->death; |
| 3365 | if (death->cookie != cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3366 | 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] | 3367 | proc->pid, thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3368 | (u64)death->cookie, |
| 3369 | (u64)cookie); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3370 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3371 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3372 | break; |
| 3373 | } |
| 3374 | ref->death = NULL; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3375 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3376 | if (list_empty(&death->work.entry)) { |
| 3377 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3378 | if (thread->looper & |
| 3379 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3380 | BINDER_LOOPER_STATE_ENTERED)) |
| 3381 | binder_enqueue_work_ilocked( |
| 3382 | &death->work, |
| 3383 | &thread->todo); |
| 3384 | else { |
| 3385 | binder_enqueue_work_ilocked( |
| 3386 | &death->work, |
| 3387 | &proc->todo); |
| 3388 | wake_up_interruptible( |
| 3389 | &proc->wait); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3390 | } |
| 3391 | } else { |
| 3392 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); |
| 3393 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; |
| 3394 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3395 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3396 | } |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3397 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3398 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3399 | } break; |
| 3400 | case BC_DEAD_BINDER_DONE: { |
| 3401 | struct binder_work *w; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3402 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3403 | struct binder_ref_death *death = NULL; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3404 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3405 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3406 | return -EFAULT; |
| 3407 | |
Lisa Du | 7a64cd8 | 2016-02-17 09:32:52 +0800 | [diff] [blame] | 3408 | ptr += sizeof(cookie); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3409 | binder_inner_proc_lock(proc); |
| 3410 | list_for_each_entry(w, &proc->delivered_death, |
| 3411 | entry) { |
| 3412 | struct binder_ref_death *tmp_death = |
| 3413 | container_of(w, |
| 3414 | struct binder_ref_death, |
| 3415 | work); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3416 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3417 | if (tmp_death->cookie == cookie) { |
| 3418 | death = tmp_death; |
| 3419 | break; |
| 3420 | } |
| 3421 | } |
| 3422 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3423 | "%d:%d BC_DEAD_BINDER_DONE %016llx found %p\n", |
| 3424 | proc->pid, thread->pid, (u64)cookie, |
| 3425 | death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3426 | if (death == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3427 | binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n", |
| 3428 | proc->pid, thread->pid, (u64)cookie); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3429 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3430 | break; |
| 3431 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3432 | binder_dequeue_work_ilocked(&death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3433 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { |
| 3434 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3435 | if (thread->looper & |
| 3436 | (BINDER_LOOPER_STATE_REGISTERED | |
| 3437 | BINDER_LOOPER_STATE_ENTERED)) |
| 3438 | binder_enqueue_work_ilocked( |
| 3439 | &death->work, &thread->todo); |
| 3440 | else { |
| 3441 | binder_enqueue_work_ilocked( |
| 3442 | &death->work, |
| 3443 | &proc->todo); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3444 | wake_up_interruptible(&proc->wait); |
| 3445 | } |
| 3446 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3447 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3448 | } break; |
| 3449 | |
| 3450 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3451 | pr_err("%d:%d unknown command %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3452 | proc->pid, thread->pid, cmd); |
| 3453 | return -EINVAL; |
| 3454 | } |
| 3455 | *consumed = ptr - buffer; |
| 3456 | } |
| 3457 | return 0; |
| 3458 | } |
| 3459 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3460 | static void binder_stat_br(struct binder_proc *proc, |
| 3461 | struct binder_thread *thread, uint32_t cmd) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3462 | { |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3463 | trace_binder_return(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3464 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 3465 | atomic_inc(&binder_stats.br[_IOC_NR(cmd)]); |
| 3466 | atomic_inc(&proc->stats.br[_IOC_NR(cmd)]); |
| 3467 | atomic_inc(&thread->stats.br[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3468 | } |
| 3469 | } |
| 3470 | |
| 3471 | static int binder_has_proc_work(struct binder_proc *proc, |
| 3472 | struct binder_thread *thread) |
| 3473 | { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3474 | return !binder_worklist_empty(proc, &proc->todo) || |
| 3475 | thread->looper_need_return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3476 | } |
| 3477 | |
| 3478 | static int binder_has_thread_work(struct binder_thread *thread) |
| 3479 | { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3480 | return !binder_worklist_empty(thread->proc, &thread->todo) || |
| 3481 | thread->looper_need_return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3482 | } |
| 3483 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3484 | static int binder_put_node_cmd(struct binder_proc *proc, |
| 3485 | struct binder_thread *thread, |
| 3486 | void __user **ptrp, |
| 3487 | binder_uintptr_t node_ptr, |
| 3488 | binder_uintptr_t node_cookie, |
| 3489 | int node_debug_id, |
| 3490 | uint32_t cmd, const char *cmd_name) |
| 3491 | { |
| 3492 | void __user *ptr = *ptrp; |
| 3493 | |
| 3494 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 3495 | return -EFAULT; |
| 3496 | ptr += sizeof(uint32_t); |
| 3497 | |
| 3498 | if (put_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
| 3499 | return -EFAULT; |
| 3500 | ptr += sizeof(binder_uintptr_t); |
| 3501 | |
| 3502 | if (put_user(node_cookie, (binder_uintptr_t __user *)ptr)) |
| 3503 | return -EFAULT; |
| 3504 | ptr += sizeof(binder_uintptr_t); |
| 3505 | |
| 3506 | binder_stat_br(proc, thread, cmd); |
| 3507 | binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n", |
| 3508 | proc->pid, thread->pid, cmd_name, node_debug_id, |
| 3509 | (u64)node_ptr, (u64)node_cookie); |
| 3510 | |
| 3511 | *ptrp = ptr; |
| 3512 | return 0; |
| 3513 | } |
| 3514 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3515 | static int binder_thread_read(struct binder_proc *proc, |
| 3516 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3517 | binder_uintptr_t binder_buffer, size_t size, |
| 3518 | binder_size_t *consumed, int non_block) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3519 | { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3520 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3521 | void __user *ptr = buffer + *consumed; |
| 3522 | void __user *end = buffer + size; |
| 3523 | |
| 3524 | int ret = 0; |
| 3525 | int wait_for_proc_work; |
| 3526 | |
| 3527 | if (*consumed == 0) { |
| 3528 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) |
| 3529 | return -EFAULT; |
| 3530 | ptr += sizeof(uint32_t); |
| 3531 | } |
| 3532 | |
| 3533 | retry: |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3534 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3535 | wait_for_proc_work = thread->transaction_stack == NULL && |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3536 | binder_worklist_empty_ilocked(&thread->todo); |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3537 | if (wait_for_proc_work) |
| 3538 | proc->ready_threads++; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3539 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3540 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3541 | thread->looper |= BINDER_LOOPER_STATE_WAITING; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3542 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3543 | trace_binder_wait_for_work(wait_for_proc_work, |
| 3544 | !!thread->transaction_stack, |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3545 | !binder_worklist_empty(proc, &thread->todo)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3546 | if (wait_for_proc_work) { |
| 3547 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 3548 | BINDER_LOOPER_STATE_ENTERED))) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3549 | 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] | 3550 | proc->pid, thread->pid, thread->looper); |
| 3551 | wait_event_interruptible(binder_user_error_wait, |
| 3552 | binder_stop_on_user_error < 2); |
| 3553 | } |
| 3554 | binder_set_nice(proc->default_priority); |
| 3555 | if (non_block) { |
| 3556 | if (!binder_has_proc_work(proc, thread)) |
| 3557 | ret = -EAGAIN; |
| 3558 | } else |
Colin Cross | e2610b2 | 2013-05-06 23:50:15 +0000 | [diff] [blame] | 3559 | ret = wait_event_freezable_exclusive(proc->wait, binder_has_proc_work(proc, thread)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3560 | } else { |
| 3561 | if (non_block) { |
| 3562 | if (!binder_has_thread_work(thread)) |
| 3563 | ret = -EAGAIN; |
| 3564 | } else |
Colin Cross | e2610b2 | 2013-05-06 23:50:15 +0000 | [diff] [blame] | 3565 | ret = wait_event_freezable(thread->wait, binder_has_thread_work(thread)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3566 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3567 | |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3568 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3569 | if (wait_for_proc_work) |
| 3570 | proc->ready_threads--; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3571 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3572 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; |
| 3573 | |
| 3574 | if (ret) |
| 3575 | return ret; |
| 3576 | |
| 3577 | while (1) { |
| 3578 | uint32_t cmd; |
| 3579 | struct binder_transaction_data tr; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3580 | struct binder_work *w = NULL; |
| 3581 | struct list_head *list = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3582 | struct binder_transaction *t = NULL; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3583 | struct binder_thread *t_from; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3584 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3585 | binder_inner_proc_lock(proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3586 | if (!binder_worklist_empty_ilocked(&thread->todo)) |
| 3587 | list = &thread->todo; |
| 3588 | else if (!binder_worklist_empty_ilocked(&proc->todo) && |
| 3589 | wait_for_proc_work) |
| 3590 | list = &proc->todo; |
| 3591 | else { |
| 3592 | binder_inner_proc_unlock(proc); |
| 3593 | |
Dmitry Voytik | 395262a | 2014-09-08 18:16:34 +0400 | [diff] [blame] | 3594 | /* no data added */ |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 3595 | if (ptr - buffer == 4 && !thread->looper_need_return) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3596 | goto retry; |
| 3597 | break; |
| 3598 | } |
| 3599 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3600 | if (end - ptr < sizeof(tr) + 4) { |
| 3601 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3602 | break; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3603 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3604 | w = binder_dequeue_work_head_ilocked(list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3605 | |
| 3606 | switch (w->type) { |
| 3607 | case BINDER_WORK_TRANSACTION: { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3608 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3609 | t = container_of(w, struct binder_transaction, work); |
| 3610 | } break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3611 | case BINDER_WORK_RETURN_ERROR: { |
| 3612 | struct binder_error *e = container_of( |
| 3613 | w, struct binder_error, work); |
| 3614 | |
| 3615 | WARN_ON(e->cmd == BR_OK); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3616 | binder_inner_proc_unlock(proc); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3617 | if (put_user(e->cmd, (uint32_t __user *)ptr)) |
| 3618 | return -EFAULT; |
| 3619 | e->cmd = BR_OK; |
| 3620 | ptr += sizeof(uint32_t); |
| 3621 | |
Todd Kjos | 4f9adc8 | 2017-08-08 15:48:36 -0700 | [diff] [blame] | 3622 | binder_stat_br(proc, thread, e->cmd); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3623 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3624 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3625 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3626 | cmd = BR_TRANSACTION_COMPLETE; |
| 3627 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 3628 | return -EFAULT; |
| 3629 | ptr += sizeof(uint32_t); |
| 3630 | |
| 3631 | binder_stat_br(proc, thread, cmd); |
| 3632 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3633 | "%d:%d BR_TRANSACTION_COMPLETE\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3634 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3635 | kfree(w); |
| 3636 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3637 | } break; |
| 3638 | case BINDER_WORK_NODE: { |
| 3639 | struct binder_node *node = container_of(w, struct binder_node, work); |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3640 | int strong, weak; |
| 3641 | binder_uintptr_t node_ptr = node->ptr; |
| 3642 | binder_uintptr_t node_cookie = node->cookie; |
| 3643 | int node_debug_id = node->debug_id; |
| 3644 | int has_weak_ref; |
| 3645 | int has_strong_ref; |
| 3646 | void __user *orig_ptr = ptr; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3647 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3648 | BUG_ON(proc != node->proc); |
| 3649 | strong = node->internal_strong_refs || |
| 3650 | node->local_strong_refs; |
| 3651 | weak = !hlist_empty(&node->refs) || |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3652 | node->local_weak_refs || |
| 3653 | node->tmp_refs || strong; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3654 | has_strong_ref = node->has_strong_ref; |
| 3655 | has_weak_ref = node->has_weak_ref; |
| 3656 | |
| 3657 | if (weak && !has_weak_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3658 | node->has_weak_ref = 1; |
| 3659 | node->pending_weak_ref = 1; |
| 3660 | node->local_weak_refs++; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3661 | } |
| 3662 | if (strong && !has_strong_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3663 | node->has_strong_ref = 1; |
| 3664 | node->pending_strong_ref = 1; |
| 3665 | node->local_strong_refs++; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3666 | } |
| 3667 | if (!strong && has_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3668 | node->has_strong_ref = 0; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3669 | if (!weak && has_weak_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3670 | node->has_weak_ref = 0; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3671 | if (!weak && !strong) { |
| 3672 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 3673 | "%d:%d node %d u%016llx c%016llx deleted\n", |
| 3674 | proc->pid, thread->pid, |
| 3675 | node_debug_id, |
| 3676 | (u64)node_ptr, |
| 3677 | (u64)node_cookie); |
| 3678 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3679 | binder_inner_proc_unlock(proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3680 | binder_node_lock(node); |
| 3681 | /* |
| 3682 | * Acquire the node lock before freeing the |
| 3683 | * node to serialize with other threads that |
| 3684 | * may have been holding the node lock while |
| 3685 | * decrementing this node (avoids race where |
| 3686 | * this thread frees while the other thread |
| 3687 | * is unlocking the node after the final |
| 3688 | * decrement) |
| 3689 | */ |
| 3690 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3691 | binder_free_node(node); |
| 3692 | } else |
| 3693 | binder_inner_proc_unlock(proc); |
| 3694 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 3695 | if (weak && !has_weak_ref) |
| 3696 | ret = binder_put_node_cmd( |
| 3697 | proc, thread, &ptr, node_ptr, |
| 3698 | node_cookie, node_debug_id, |
| 3699 | BR_INCREFS, "BR_INCREFS"); |
| 3700 | if (!ret && strong && !has_strong_ref) |
| 3701 | ret = binder_put_node_cmd( |
| 3702 | proc, thread, &ptr, node_ptr, |
| 3703 | node_cookie, node_debug_id, |
| 3704 | BR_ACQUIRE, "BR_ACQUIRE"); |
| 3705 | if (!ret && !strong && has_strong_ref) |
| 3706 | ret = binder_put_node_cmd( |
| 3707 | proc, thread, &ptr, node_ptr, |
| 3708 | node_cookie, node_debug_id, |
| 3709 | BR_RELEASE, "BR_RELEASE"); |
| 3710 | if (!ret && !weak && has_weak_ref) |
| 3711 | ret = binder_put_node_cmd( |
| 3712 | proc, thread, &ptr, node_ptr, |
| 3713 | node_cookie, node_debug_id, |
| 3714 | BR_DECREFS, "BR_DECREFS"); |
| 3715 | if (orig_ptr == ptr) |
| 3716 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 3717 | "%d:%d node %d u%016llx c%016llx state unchanged\n", |
| 3718 | proc->pid, thread->pid, |
| 3719 | node_debug_id, |
| 3720 | (u64)node_ptr, |
| 3721 | (u64)node_cookie); |
| 3722 | if (ret) |
| 3723 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3724 | } break; |
| 3725 | case BINDER_WORK_DEAD_BINDER: |
| 3726 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 3727 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 3728 | struct binder_ref_death *death; |
| 3729 | uint32_t cmd; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3730 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3731 | |
| 3732 | death = container_of(w, struct binder_ref_death, work); |
| 3733 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) |
| 3734 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; |
| 3735 | else |
| 3736 | cmd = BR_DEAD_BINDER; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3737 | cookie = death->cookie; |
| 3738 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3739 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3740 | "%d:%d %s %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3741 | proc->pid, thread->pid, |
| 3742 | cmd == BR_DEAD_BINDER ? |
| 3743 | "BR_DEAD_BINDER" : |
| 3744 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3745 | (u64)cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3746 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3747 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3748 | kfree(death); |
| 3749 | binder_stats_deleted(BINDER_STAT_DEATH); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3750 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3751 | binder_enqueue_work_ilocked( |
| 3752 | w, &proc->delivered_death); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 3753 | binder_inner_proc_unlock(proc); |
| 3754 | } |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 3755 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 3756 | return -EFAULT; |
| 3757 | ptr += sizeof(uint32_t); |
| 3758 | if (put_user(cookie, |
| 3759 | (binder_uintptr_t __user *)ptr)) |
| 3760 | return -EFAULT; |
| 3761 | ptr += sizeof(binder_uintptr_t); |
| 3762 | binder_stat_br(proc, thread, cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3763 | if (cmd == BR_DEAD_BINDER) |
| 3764 | goto done; /* DEAD_BINDER notifications can cause transactions */ |
| 3765 | } break; |
| 3766 | } |
| 3767 | |
| 3768 | if (!t) |
| 3769 | continue; |
| 3770 | |
| 3771 | BUG_ON(t->buffer == NULL); |
| 3772 | if (t->buffer->target_node) { |
| 3773 | struct binder_node *target_node = t->buffer->target_node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3774 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3775 | tr.target.ptr = target_node->ptr; |
| 3776 | tr.cookie = target_node->cookie; |
| 3777 | t->saved_priority = task_nice(current); |
| 3778 | if (t->priority < target_node->min_priority && |
| 3779 | !(t->flags & TF_ONE_WAY)) |
| 3780 | binder_set_nice(t->priority); |
| 3781 | else if (!(t->flags & TF_ONE_WAY) || |
| 3782 | t->saved_priority > target_node->min_priority) |
| 3783 | binder_set_nice(target_node->min_priority); |
| 3784 | cmd = BR_TRANSACTION; |
| 3785 | } else { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3786 | tr.target.ptr = 0; |
| 3787 | tr.cookie = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3788 | cmd = BR_REPLY; |
| 3789 | } |
| 3790 | tr.code = t->code; |
| 3791 | tr.flags = t->flags; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 3792 | tr.sender_euid = from_kuid(current_user_ns(), t->sender_euid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3793 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3794 | t_from = binder_get_txn_from(t); |
| 3795 | if (t_from) { |
| 3796 | struct task_struct *sender = t_from->proc->tsk; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3797 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3798 | tr.sender_pid = task_tgid_nr_ns(sender, |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3799 | task_active_pid_ns(current)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3800 | } else { |
| 3801 | tr.sender_pid = 0; |
| 3802 | } |
| 3803 | |
| 3804 | tr.data_size = t->buffer->data_size; |
| 3805 | tr.offsets_size = t->buffer->offsets_size; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 3806 | tr.data.ptr.buffer = (binder_uintptr_t) |
| 3807 | ((uintptr_t)t->buffer->data + |
| 3808 | binder_alloc_get_user_buffer_offset(&proc->alloc)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3809 | tr.data.ptr.offsets = tr.data.ptr.buffer + |
| 3810 | ALIGN(t->buffer->data_size, |
| 3811 | sizeof(void *)); |
| 3812 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3813 | if (put_user(cmd, (uint32_t __user *)ptr)) { |
| 3814 | if (t_from) |
| 3815 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3816 | return -EFAULT; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3817 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3818 | ptr += sizeof(uint32_t); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3819 | if (copy_to_user(ptr, &tr, sizeof(tr))) { |
| 3820 | if (t_from) |
| 3821 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3822 | return -EFAULT; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3823 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3824 | ptr += sizeof(tr); |
| 3825 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3826 | trace_binder_transaction_received(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3827 | binder_stat_br(proc, thread, cmd); |
| 3828 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3829 | "%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] | 3830 | proc->pid, thread->pid, |
| 3831 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : |
| 3832 | "BR_REPLY", |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3833 | t->debug_id, t_from ? t_from->proc->pid : 0, |
| 3834 | t_from ? t_from->pid : 0, cmd, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3835 | t->buffer->data_size, t->buffer->offsets_size, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3836 | (u64)tr.data.ptr.buffer, (u64)tr.data.ptr.offsets); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3837 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3838 | if (t_from) |
| 3839 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3840 | t->buffer->allow_user_free = 1; |
| 3841 | if (cmd == BR_TRANSACTION && !(t->flags & TF_ONE_WAY)) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3842 | binder_inner_proc_lock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3843 | t->to_parent = thread->transaction_stack; |
| 3844 | t->to_thread = thread; |
| 3845 | thread->transaction_stack = t; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3846 | binder_inner_proc_unlock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3847 | } else { |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 3848 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3849 | } |
| 3850 | break; |
| 3851 | } |
| 3852 | |
| 3853 | done: |
| 3854 | |
| 3855 | *consumed = ptr - buffer; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3856 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3857 | if (proc->requested_threads + proc->ready_threads == 0 && |
| 3858 | proc->requested_threads_started < proc->max_threads && |
| 3859 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 3860 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ |
| 3861 | /*spawn a new thread if we leave this out */) { |
| 3862 | proc->requested_threads++; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3863 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3864 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3865 | "%d:%d BR_SPAWN_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3866 | proc->pid, thread->pid); |
| 3867 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) |
| 3868 | return -EFAULT; |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 3869 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 3870 | } else |
| 3871 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3872 | return 0; |
| 3873 | } |
| 3874 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3875 | static void binder_release_work(struct binder_proc *proc, |
| 3876 | struct list_head *list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3877 | { |
| 3878 | struct binder_work *w; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3879 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 3880 | while (1) { |
| 3881 | w = binder_dequeue_work_head(proc, list); |
| 3882 | if (!w) |
| 3883 | return; |
| 3884 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3885 | switch (w->type) { |
| 3886 | case BINDER_WORK_TRANSACTION: { |
| 3887 | struct binder_transaction *t; |
| 3888 | |
| 3889 | t = container_of(w, struct binder_transaction, work); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 3890 | if (t->buffer->target_node && |
| 3891 | !(t->flags & TF_ONE_WAY)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3892 | binder_send_failed_reply(t, BR_DEAD_REPLY); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 3893 | } else { |
| 3894 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3895 | "undelivered transaction %d\n", |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 3896 | t->debug_id); |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 3897 | binder_free_transaction(t); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 3898 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3899 | } break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3900 | case BINDER_WORK_RETURN_ERROR: { |
| 3901 | struct binder_error *e = container_of( |
| 3902 | w, struct binder_error, work); |
| 3903 | |
| 3904 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 3905 | "undelivered TRANSACTION_ERROR: %u\n", |
| 3906 | e->cmd); |
| 3907 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3908 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 3909 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3910 | "undelivered TRANSACTION_COMPLETE\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3911 | kfree(w); |
| 3912 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3913 | } break; |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 3914 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 3915 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 3916 | struct binder_ref_death *death; |
| 3917 | |
| 3918 | death = container_of(w, struct binder_ref_death, work); |
| 3919 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3920 | "undelivered death notification, %016llx\n", |
| 3921 | (u64)death->cookie); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 3922 | kfree(death); |
| 3923 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 3924 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3925 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3926 | pr_err("unexpected work type, %d, not freed\n", |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 3927 | w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3928 | break; |
| 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | } |
| 3933 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 3934 | static struct binder_thread *binder_get_thread_ilocked( |
| 3935 | struct binder_proc *proc, struct binder_thread *new_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3936 | { |
| 3937 | struct binder_thread *thread = NULL; |
| 3938 | struct rb_node *parent = NULL; |
| 3939 | struct rb_node **p = &proc->threads.rb_node; |
| 3940 | |
| 3941 | while (*p) { |
| 3942 | parent = *p; |
| 3943 | thread = rb_entry(parent, struct binder_thread, rb_node); |
| 3944 | |
| 3945 | if (current->pid < thread->pid) |
| 3946 | p = &(*p)->rb_left; |
| 3947 | else if (current->pid > thread->pid) |
| 3948 | p = &(*p)->rb_right; |
| 3949 | else |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 3950 | return thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3951 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 3952 | if (!new_thread) |
| 3953 | return NULL; |
| 3954 | thread = new_thread; |
| 3955 | binder_stats_created(BINDER_STAT_THREAD); |
| 3956 | thread->proc = proc; |
| 3957 | thread->pid = current->pid; |
| 3958 | atomic_set(&thread->tmp_ref, 0); |
| 3959 | init_waitqueue_head(&thread->wait); |
| 3960 | INIT_LIST_HEAD(&thread->todo); |
| 3961 | rb_link_node(&thread->rb_node, parent, p); |
| 3962 | rb_insert_color(&thread->rb_node, &proc->threads); |
| 3963 | thread->looper_need_return = true; |
| 3964 | thread->return_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 3965 | thread->return_error.cmd = BR_OK; |
| 3966 | thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 3967 | thread->reply_error.cmd = BR_OK; |
| 3968 | |
| 3969 | return thread; |
| 3970 | } |
| 3971 | |
| 3972 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) |
| 3973 | { |
| 3974 | struct binder_thread *thread; |
| 3975 | struct binder_thread *new_thread; |
| 3976 | |
| 3977 | binder_inner_proc_lock(proc); |
| 3978 | thread = binder_get_thread_ilocked(proc, NULL); |
| 3979 | binder_inner_proc_unlock(proc); |
| 3980 | if (!thread) { |
| 3981 | new_thread = kzalloc(sizeof(*thread), GFP_KERNEL); |
| 3982 | if (new_thread == NULL) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3983 | return NULL; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 3984 | binder_inner_proc_lock(proc); |
| 3985 | thread = binder_get_thread_ilocked(proc, new_thread); |
| 3986 | binder_inner_proc_unlock(proc); |
| 3987 | if (thread != new_thread) |
| 3988 | kfree(new_thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3989 | } |
| 3990 | return thread; |
| 3991 | } |
| 3992 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3993 | static void binder_free_proc(struct binder_proc *proc) |
| 3994 | { |
| 3995 | BUG_ON(!list_empty(&proc->todo)); |
| 3996 | BUG_ON(!list_empty(&proc->delivered_death)); |
| 3997 | binder_alloc_deferred_release(&proc->alloc); |
| 3998 | put_task_struct(proc->tsk); |
| 3999 | binder_stats_deleted(BINDER_STAT_PROC); |
| 4000 | kfree(proc); |
| 4001 | } |
| 4002 | |
| 4003 | static void binder_free_thread(struct binder_thread *thread) |
| 4004 | { |
| 4005 | BUG_ON(!list_empty(&thread->todo)); |
| 4006 | binder_stats_deleted(BINDER_STAT_THREAD); |
| 4007 | binder_proc_dec_tmpref(thread->proc); |
| 4008 | kfree(thread); |
| 4009 | } |
| 4010 | |
| 4011 | static int binder_thread_release(struct binder_proc *proc, |
| 4012 | struct binder_thread *thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4013 | { |
| 4014 | struct binder_transaction *t; |
| 4015 | struct binder_transaction *send_reply = NULL; |
| 4016 | int active_transactions = 0; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4017 | struct binder_transaction *last_t = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4018 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4019 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4020 | /* |
| 4021 | * take a ref on the proc so it survives |
| 4022 | * after we remove this thread from proc->threads. |
| 4023 | * The corresponding dec is when we actually |
| 4024 | * free the thread in binder_free_thread() |
| 4025 | */ |
| 4026 | proc->tmp_ref++; |
| 4027 | /* |
| 4028 | * take a ref on this thread to ensure it |
| 4029 | * survives while we are releasing it |
| 4030 | */ |
| 4031 | atomic_inc(&thread->tmp_ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4032 | rb_erase(&thread->rb_node, &proc->threads); |
| 4033 | t = thread->transaction_stack; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4034 | if (t) { |
| 4035 | spin_lock(&t->lock); |
| 4036 | if (t->to_thread == thread) |
| 4037 | send_reply = t; |
| 4038 | } |
| 4039 | thread->is_dead = true; |
| 4040 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4041 | while (t) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4042 | last_t = t; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4043 | active_transactions++; |
| 4044 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4045 | "release %d:%d transaction %d %s, still active\n", |
| 4046 | proc->pid, thread->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4047 | t->debug_id, |
| 4048 | (t->to_thread == thread) ? "in" : "out"); |
| 4049 | |
| 4050 | if (t->to_thread == thread) { |
| 4051 | t->to_proc = NULL; |
| 4052 | t->to_thread = NULL; |
| 4053 | if (t->buffer) { |
| 4054 | t->buffer->transaction = NULL; |
| 4055 | t->buffer = NULL; |
| 4056 | } |
| 4057 | t = t->to_parent; |
| 4058 | } else if (t->from == thread) { |
| 4059 | t->from = NULL; |
| 4060 | t = t->from_parent; |
| 4061 | } else |
| 4062 | BUG(); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4063 | spin_unlock(&last_t->lock); |
| 4064 | if (t) |
| 4065 | spin_lock(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4066 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4067 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4068 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4069 | if (send_reply) |
| 4070 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4071 | binder_release_work(proc, &thread->todo); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4072 | binder_thread_dec_tmpref(thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4073 | return active_transactions; |
| 4074 | } |
| 4075 | |
| 4076 | static unsigned int binder_poll(struct file *filp, |
| 4077 | struct poll_table_struct *wait) |
| 4078 | { |
| 4079 | struct binder_proc *proc = filp->private_data; |
| 4080 | struct binder_thread *thread = NULL; |
| 4081 | int wait_for_proc_work; |
| 4082 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4083 | thread = binder_get_thread(proc); |
| 4084 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4085 | binder_inner_proc_lock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4086 | wait_for_proc_work = thread->transaction_stack == NULL && |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4087 | binder_worklist_empty_ilocked(&thread->todo); |
| 4088 | binder_inner_proc_unlock(thread->proc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4089 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4090 | if (wait_for_proc_work) { |
| 4091 | if (binder_has_proc_work(proc, thread)) |
| 4092 | return POLLIN; |
| 4093 | poll_wait(filp, &proc->wait, wait); |
| 4094 | if (binder_has_proc_work(proc, thread)) |
| 4095 | return POLLIN; |
| 4096 | } else { |
| 4097 | if (binder_has_thread_work(thread)) |
| 4098 | return POLLIN; |
| 4099 | poll_wait(filp, &thread->wait, wait); |
| 4100 | if (binder_has_thread_work(thread)) |
| 4101 | return POLLIN; |
| 4102 | } |
| 4103 | return 0; |
| 4104 | } |
| 4105 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4106 | static int binder_ioctl_write_read(struct file *filp, |
| 4107 | unsigned int cmd, unsigned long arg, |
| 4108 | struct binder_thread *thread) |
| 4109 | { |
| 4110 | int ret = 0; |
| 4111 | struct binder_proc *proc = filp->private_data; |
| 4112 | unsigned int size = _IOC_SIZE(cmd); |
| 4113 | void __user *ubuf = (void __user *)arg; |
| 4114 | struct binder_write_read bwr; |
| 4115 | |
| 4116 | if (size != sizeof(struct binder_write_read)) { |
| 4117 | ret = -EINVAL; |
| 4118 | goto out; |
| 4119 | } |
| 4120 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { |
| 4121 | ret = -EFAULT; |
| 4122 | goto out; |
| 4123 | } |
| 4124 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4125 | "%d:%d write %lld at %016llx, read %lld at %016llx\n", |
| 4126 | proc->pid, thread->pid, |
| 4127 | (u64)bwr.write_size, (u64)bwr.write_buffer, |
| 4128 | (u64)bwr.read_size, (u64)bwr.read_buffer); |
| 4129 | |
| 4130 | if (bwr.write_size > 0) { |
| 4131 | ret = binder_thread_write(proc, thread, |
| 4132 | bwr.write_buffer, |
| 4133 | bwr.write_size, |
| 4134 | &bwr.write_consumed); |
| 4135 | trace_binder_write_done(ret); |
| 4136 | if (ret < 0) { |
| 4137 | bwr.read_consumed = 0; |
| 4138 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4139 | ret = -EFAULT; |
| 4140 | goto out; |
| 4141 | } |
| 4142 | } |
| 4143 | if (bwr.read_size > 0) { |
| 4144 | ret = binder_thread_read(proc, thread, bwr.read_buffer, |
| 4145 | bwr.read_size, |
| 4146 | &bwr.read_consumed, |
| 4147 | filp->f_flags & O_NONBLOCK); |
| 4148 | trace_binder_read_done(ret); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4149 | if (!binder_worklist_empty(proc, &proc->todo)) |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4150 | wake_up_interruptible(&proc->wait); |
| 4151 | if (ret < 0) { |
| 4152 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 4153 | ret = -EFAULT; |
| 4154 | goto out; |
| 4155 | } |
| 4156 | } |
| 4157 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 4158 | "%d:%d wrote %lld of %lld, read return %lld of %lld\n", |
| 4159 | proc->pid, thread->pid, |
| 4160 | (u64)bwr.write_consumed, (u64)bwr.write_size, |
| 4161 | (u64)bwr.read_consumed, (u64)bwr.read_size); |
| 4162 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { |
| 4163 | ret = -EFAULT; |
| 4164 | goto out; |
| 4165 | } |
| 4166 | out: |
| 4167 | return ret; |
| 4168 | } |
| 4169 | |
| 4170 | static int binder_ioctl_set_ctx_mgr(struct file *filp) |
| 4171 | { |
| 4172 | int ret = 0; |
| 4173 | struct binder_proc *proc = filp->private_data; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4174 | struct binder_context *context = proc->context; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4175 | struct binder_node *new_node; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4176 | kuid_t curr_euid = current_euid(); |
| 4177 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4178 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4179 | if (context->binder_context_mgr_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4180 | pr_err("BINDER_SET_CONTEXT_MGR already set\n"); |
| 4181 | ret = -EBUSY; |
| 4182 | goto out; |
| 4183 | } |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 4184 | ret = security_binder_set_context_mgr(proc->tsk); |
| 4185 | if (ret < 0) |
| 4186 | goto out; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4187 | if (uid_valid(context->binder_context_mgr_uid)) { |
| 4188 | if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4189 | pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n", |
| 4190 | from_kuid(&init_user_ns, curr_euid), |
| 4191 | from_kuid(&init_user_ns, |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4192 | context->binder_context_mgr_uid)); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4193 | ret = -EPERM; |
| 4194 | goto out; |
| 4195 | } |
| 4196 | } else { |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4197 | context->binder_context_mgr_uid = curr_euid; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4198 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4199 | new_node = binder_new_node(proc, NULL); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4200 | if (!new_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4201 | ret = -ENOMEM; |
| 4202 | goto out; |
| 4203 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4204 | binder_node_lock(new_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4205 | new_node->local_weak_refs++; |
| 4206 | new_node->local_strong_refs++; |
| 4207 | new_node->has_strong_ref = 1; |
| 4208 | new_node->has_weak_ref = 1; |
| 4209 | context->binder_context_mgr_node = new_node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4210 | binder_node_unlock(new_node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4211 | binder_put_node(new_node); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4212 | out: |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4213 | mutex_unlock(&context->context_mgr_node_lock); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4214 | return ret; |
| 4215 | } |
| 4216 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4217 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 4218 | { |
| 4219 | int ret; |
| 4220 | struct binder_proc *proc = filp->private_data; |
| 4221 | struct binder_thread *thread; |
| 4222 | unsigned int size = _IOC_SIZE(cmd); |
| 4223 | void __user *ubuf = (void __user *)arg; |
| 4224 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4225 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", |
| 4226 | proc->pid, current->pid, cmd, arg);*/ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4227 | |
Sherry Yang | 4175e2b | 2017-08-23 08:46:40 -0700 | [diff] [blame] | 4228 | binder_selftest_alloc(&proc->alloc); |
| 4229 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4230 | trace_binder_ioctl(cmd, arg); |
| 4231 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4232 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4233 | if (ret) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4234 | goto err_unlocked; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4235 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4236 | thread = binder_get_thread(proc); |
| 4237 | if (thread == NULL) { |
| 4238 | ret = -ENOMEM; |
| 4239 | goto err; |
| 4240 | } |
| 4241 | |
| 4242 | switch (cmd) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4243 | case BINDER_WRITE_READ: |
| 4244 | ret = binder_ioctl_write_read(filp, cmd, arg, thread); |
| 4245 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4246 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4247 | break; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4248 | case BINDER_SET_MAX_THREADS: { |
| 4249 | int max_threads; |
| 4250 | |
| 4251 | if (copy_from_user(&max_threads, ubuf, |
| 4252 | sizeof(max_threads))) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4253 | ret = -EINVAL; |
| 4254 | goto err; |
| 4255 | } |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4256 | binder_inner_proc_lock(proc); |
| 4257 | proc->max_threads = max_threads; |
| 4258 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4259 | break; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4260 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4261 | case BINDER_SET_CONTEXT_MGR: |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4262 | ret = binder_ioctl_set_ctx_mgr(filp); |
| 4263 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4264 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4265 | break; |
| 4266 | case BINDER_THREAD_EXIT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4267 | binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4268 | proc->pid, thread->pid); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4269 | binder_thread_release(proc, thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4270 | thread = NULL; |
| 4271 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4272 | case BINDER_VERSION: { |
| 4273 | struct binder_version __user *ver = ubuf; |
| 4274 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4275 | if (size != sizeof(struct binder_version)) { |
| 4276 | ret = -EINVAL; |
| 4277 | goto err; |
| 4278 | } |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4279 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, |
| 4280 | &ver->protocol_version)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4281 | ret = -EINVAL; |
| 4282 | goto err; |
| 4283 | } |
| 4284 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 4285 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4286 | default: |
| 4287 | ret = -EINVAL; |
| 4288 | goto err; |
| 4289 | } |
| 4290 | ret = 0; |
| 4291 | err: |
| 4292 | if (thread) |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 4293 | thread->looper_need_return = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4294 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 4295 | if (ret && ret != -ERESTARTSYS) |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4296 | 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] | 4297 | err_unlocked: |
| 4298 | trace_binder_ioctl_done(ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4299 | return ret; |
| 4300 | } |
| 4301 | |
| 4302 | static void binder_vma_open(struct vm_area_struct *vma) |
| 4303 | { |
| 4304 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4305 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4306 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4307 | "%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] | 4308 | proc->pid, vma->vm_start, vma->vm_end, |
| 4309 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4310 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | static void binder_vma_close(struct vm_area_struct *vma) |
| 4314 | { |
| 4315 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4316 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4317 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4318 | "%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] | 4319 | proc->pid, vma->vm_start, vma->vm_end, |
| 4320 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4321 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4322 | binder_alloc_vma_close(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4323 | binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES); |
| 4324 | } |
| 4325 | |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 4326 | static int binder_vm_fault(struct vm_fault *vmf) |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4327 | { |
| 4328 | return VM_FAULT_SIGBUS; |
| 4329 | } |
| 4330 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 4331 | static const struct vm_operations_struct binder_vm_ops = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4332 | .open = binder_vma_open, |
| 4333 | .close = binder_vma_close, |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 4334 | .fault = binder_vm_fault, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4335 | }; |
| 4336 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4337 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) |
| 4338 | { |
| 4339 | int ret; |
| 4340 | struct binder_proc *proc = filp->private_data; |
| 4341 | const char *failure_string; |
| 4342 | |
| 4343 | if (proc->tsk != current->group_leader) |
| 4344 | return -EINVAL; |
| 4345 | |
| 4346 | if ((vma->vm_end - vma->vm_start) > SZ_4M) |
| 4347 | vma->vm_end = vma->vm_start + SZ_4M; |
| 4348 | |
| 4349 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 4350 | "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 4351 | __func__, proc->pid, vma->vm_start, vma->vm_end, |
| 4352 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 4353 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 4354 | |
| 4355 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { |
| 4356 | ret = -EPERM; |
| 4357 | failure_string = "bad vm_flags"; |
| 4358 | goto err_bad_arg; |
| 4359 | } |
| 4360 | vma->vm_flags = (vma->vm_flags | VM_DONTCOPY) & ~VM_MAYWRITE; |
| 4361 | vma->vm_ops = &binder_vm_ops; |
| 4362 | vma->vm_private_data = proc; |
| 4363 | |
| 4364 | ret = binder_alloc_mmap_handler(&proc->alloc, vma); |
| 4365 | if (ret) |
| 4366 | return ret; |
| 4367 | proc->files = get_files_struct(current); |
| 4368 | return 0; |
| 4369 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4370 | err_bad_arg: |
Sherwin Soltani | 258767f | 2012-06-26 02:00:30 -0400 | [diff] [blame] | 4371 | pr_err("binder_mmap: %d %lx-%lx %s failed %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4372 | proc->pid, vma->vm_start, vma->vm_end, failure_string, ret); |
| 4373 | return ret; |
| 4374 | } |
| 4375 | |
| 4376 | static int binder_open(struct inode *nodp, struct file *filp) |
| 4377 | { |
| 4378 | struct binder_proc *proc; |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 4379 | struct binder_device *binder_dev; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4380 | |
| 4381 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "binder_open: %d:%d\n", |
| 4382 | current->group_leader->pid, current->pid); |
| 4383 | |
| 4384 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 4385 | if (proc == NULL) |
| 4386 | return -ENOMEM; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 4387 | spin_lock_init(&proc->inner_lock); |
| 4388 | spin_lock_init(&proc->outer_lock); |
Todd Kjos | c4ea41b | 2017-06-29 12:01:36 -0700 | [diff] [blame] | 4389 | get_task_struct(current->group_leader); |
| 4390 | proc->tsk = current->group_leader; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4391 | INIT_LIST_HEAD(&proc->todo); |
| 4392 | init_waitqueue_head(&proc->wait); |
| 4393 | proc->default_priority = task_nice(current); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 4394 | binder_dev = container_of(filp->private_data, struct binder_device, |
| 4395 | miscdev); |
| 4396 | proc->context = &binder_dev->context; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4397 | binder_alloc_init(&proc->alloc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4398 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4399 | binder_stats_created(BINDER_STAT_PROC); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4400 | proc->pid = current->group_leader->pid; |
| 4401 | INIT_LIST_HEAD(&proc->delivered_death); |
| 4402 | filp->private_data = proc; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4403 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4404 | mutex_lock(&binder_procs_lock); |
| 4405 | hlist_add_head(&proc->proc_node, &binder_procs); |
| 4406 | mutex_unlock(&binder_procs_lock); |
| 4407 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4408 | if (binder_debugfs_dir_entry_proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4409 | char strbuf[11]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4410 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4411 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 4412 | /* |
| 4413 | * proc debug entries are shared between contexts, so |
| 4414 | * this will fail if the process tries to open the driver |
| 4415 | * again with a different context. The priting code will |
| 4416 | * anyway print all contexts that a given PID has, so this |
| 4417 | * is not a problem. |
| 4418 | */ |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4419 | proc->debugfs_entry = debugfs_create_file(strbuf, S_IRUGO, |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 4420 | binder_debugfs_dir_entry_proc, |
| 4421 | (void *)(unsigned long)proc->pid, |
| 4422 | &binder_proc_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | return 0; |
| 4426 | } |
| 4427 | |
| 4428 | static int binder_flush(struct file *filp, fl_owner_t id) |
| 4429 | { |
| 4430 | struct binder_proc *proc = filp->private_data; |
| 4431 | |
| 4432 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); |
| 4433 | |
| 4434 | return 0; |
| 4435 | } |
| 4436 | |
| 4437 | static void binder_deferred_flush(struct binder_proc *proc) |
| 4438 | { |
| 4439 | struct rb_node *n; |
| 4440 | int wake_count = 0; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4441 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4442 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4443 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 4444 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4445 | |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 4446 | thread->looper_need_return = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4447 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { |
| 4448 | wake_up_interruptible(&thread->wait); |
| 4449 | wake_count++; |
| 4450 | } |
| 4451 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4452 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4453 | wake_up_interruptible_all(&proc->wait); |
| 4454 | |
| 4455 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 4456 | "binder_flush: %d woke %d threads\n", proc->pid, |
| 4457 | wake_count); |
| 4458 | } |
| 4459 | |
| 4460 | static int binder_release(struct inode *nodp, struct file *filp) |
| 4461 | { |
| 4462 | struct binder_proc *proc = filp->private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4463 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4464 | debugfs_remove(proc->debugfs_entry); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4465 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); |
| 4466 | |
| 4467 | return 0; |
| 4468 | } |
| 4469 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4470 | static int binder_node_release(struct binder_node *node, int refs) |
| 4471 | { |
| 4472 | struct binder_ref *ref; |
| 4473 | int death = 0; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4474 | struct binder_proc *proc = node->proc; |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4475 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4476 | binder_release_work(proc, &node->async_todo); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4477 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4478 | binder_node_lock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4479 | binder_inner_proc_lock(proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4480 | binder_dequeue_work_ilocked(&node->work); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4481 | /* |
| 4482 | * The caller must have taken a temporary ref on the node, |
| 4483 | */ |
| 4484 | BUG_ON(!node->tmp_refs); |
| 4485 | if (hlist_empty(&node->refs) && node->tmp_refs == 1) { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4486 | binder_inner_proc_unlock(proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4487 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4488 | binder_free_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4489 | |
| 4490 | return refs; |
| 4491 | } |
| 4492 | |
| 4493 | node->proc = NULL; |
| 4494 | node->local_strong_refs = 0; |
| 4495 | node->local_weak_refs = 0; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4496 | binder_inner_proc_unlock(proc); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4497 | |
| 4498 | spin_lock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4499 | hlist_add_head(&node->dead_node, &binder_dead_nodes); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4500 | spin_unlock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4501 | |
| 4502 | hlist_for_each_entry(ref, &node->refs, node_entry) { |
| 4503 | refs++; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4504 | /* |
| 4505 | * Need the node lock to synchronize |
| 4506 | * with new notification requests and the |
| 4507 | * inner lock to synchronize with queued |
| 4508 | * death notifications. |
| 4509 | */ |
| 4510 | binder_inner_proc_lock(ref->proc); |
| 4511 | if (!ref->death) { |
| 4512 | binder_inner_proc_unlock(ref->proc); |
Arve Hjønnevåg | e194fd8 | 2014-02-17 13:58:29 -0800 | [diff] [blame] | 4513 | continue; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4514 | } |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4515 | |
| 4516 | death++; |
| 4517 | |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4518 | BUG_ON(!list_empty(&ref->death->work.entry)); |
| 4519 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 4520 | binder_enqueue_work_ilocked(&ref->death->work, |
| 4521 | &ref->proc->todo); |
| 4522 | wake_up_interruptible(&ref->proc->wait); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4523 | binder_inner_proc_unlock(ref->proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4524 | } |
| 4525 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4526 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 4527 | "node %d now dead, refs %d, death %d\n", |
| 4528 | node->debug_id, refs, death); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4529 | binder_node_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4530 | binder_put_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4531 | |
| 4532 | return refs; |
| 4533 | } |
| 4534 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4535 | static void binder_deferred_release(struct binder_proc *proc) |
| 4536 | { |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4537 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4538 | struct rb_node *n; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4539 | int threads, nodes, incoming_refs, outgoing_refs, active_transactions; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4540 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4541 | BUG_ON(proc->files); |
| 4542 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4543 | mutex_lock(&binder_procs_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4544 | hlist_del(&proc->proc_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4545 | mutex_unlock(&binder_procs_lock); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4546 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4547 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4548 | if (context->binder_context_mgr_node && |
| 4549 | context->binder_context_mgr_node->proc == proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4550 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 4551 | "%s: %d context_mgr_node gone\n", |
| 4552 | __func__, proc->pid); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 4553 | context->binder_context_mgr_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4554 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 4555 | mutex_unlock(&context->context_mgr_node_lock); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4556 | binder_inner_proc_lock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4557 | /* |
| 4558 | * Make sure proc stays alive after we |
| 4559 | * remove all the threads |
| 4560 | */ |
| 4561 | proc->tmp_ref++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4562 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4563 | proc->is_dead = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4564 | threads = 0; |
| 4565 | active_transactions = 0; |
| 4566 | while ((n = rb_first(&proc->threads))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4567 | struct binder_thread *thread; |
| 4568 | |
| 4569 | thread = rb_entry(n, struct binder_thread, rb_node); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4570 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4571 | threads++; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4572 | active_transactions += binder_thread_release(proc, thread); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4573 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4574 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4575 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4576 | nodes = 0; |
| 4577 | incoming_refs = 0; |
| 4578 | while ((n = rb_first(&proc->nodes))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4579 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4580 | |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4581 | node = rb_entry(n, struct binder_node, rb_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4582 | nodes++; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4583 | /* |
| 4584 | * take a temporary ref on the node before |
| 4585 | * calling binder_node_release() which will either |
| 4586 | * kfree() the node or call binder_put_node() |
| 4587 | */ |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4588 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4589 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4590 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 4591 | incoming_refs = binder_node_release(node, incoming_refs); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4592 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4593 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4594 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4595 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4596 | outgoing_refs = 0; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4597 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4598 | while ((n = rb_first(&proc->refs_by_desc))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4599 | struct binder_ref *ref; |
| 4600 | |
| 4601 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4602 | outgoing_refs++; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4603 | binder_cleanup_ref_olocked(ref); |
| 4604 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 4605 | binder_free_ref(ref); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4606 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4607 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4608 | binder_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 4609 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4610 | binder_release_work(proc, &proc->todo); |
| 4611 | binder_release_work(proc, &proc->delivered_death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4612 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4613 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4614 | "%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] | 4615 | __func__, proc->pid, threads, nodes, incoming_refs, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4616 | outgoing_refs, active_transactions); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4617 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4618 | binder_proc_dec_tmpref(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | static void binder_deferred_func(struct work_struct *work) |
| 4622 | { |
| 4623 | struct binder_proc *proc; |
| 4624 | struct files_struct *files; |
| 4625 | |
| 4626 | int defer; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4627 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4628 | do { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4629 | mutex_lock(&binder_deferred_lock); |
| 4630 | if (!hlist_empty(&binder_deferred_list)) { |
| 4631 | proc = hlist_entry(binder_deferred_list.first, |
| 4632 | struct binder_proc, deferred_work_node); |
| 4633 | hlist_del_init(&proc->deferred_work_node); |
| 4634 | defer = proc->deferred_work; |
| 4635 | proc->deferred_work = 0; |
| 4636 | } else { |
| 4637 | proc = NULL; |
| 4638 | defer = 0; |
| 4639 | } |
| 4640 | mutex_unlock(&binder_deferred_lock); |
| 4641 | |
| 4642 | files = NULL; |
| 4643 | if (defer & BINDER_DEFERRED_PUT_FILES) { |
| 4644 | files = proc->files; |
| 4645 | if (files) |
| 4646 | proc->files = NULL; |
| 4647 | } |
| 4648 | |
| 4649 | if (defer & BINDER_DEFERRED_FLUSH) |
| 4650 | binder_deferred_flush(proc); |
| 4651 | |
| 4652 | if (defer & BINDER_DEFERRED_RELEASE) |
| 4653 | binder_deferred_release(proc); /* frees proc */ |
| 4654 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4655 | if (files) |
| 4656 | put_files_struct(files); |
| 4657 | } while (proc); |
| 4658 | } |
| 4659 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); |
| 4660 | |
| 4661 | static void |
| 4662 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) |
| 4663 | { |
| 4664 | mutex_lock(&binder_deferred_lock); |
| 4665 | proc->deferred_work |= defer; |
| 4666 | if (hlist_unhashed(&proc->deferred_work_node)) { |
| 4667 | hlist_add_head(&proc->deferred_work_node, |
| 4668 | &binder_deferred_list); |
Bhaktipriya Shridhar | 1beba52 | 2016-08-13 22:16:24 +0530 | [diff] [blame] | 4669 | schedule_work(&binder_deferred_work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4670 | } |
| 4671 | mutex_unlock(&binder_deferred_lock); |
| 4672 | } |
| 4673 | |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4674 | static void print_binder_transaction_ilocked(struct seq_file *m, |
| 4675 | struct binder_proc *proc, |
| 4676 | const char *prefix, |
| 4677 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4678 | { |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4679 | struct binder_proc *to_proc; |
| 4680 | struct binder_buffer *buffer = t->buffer; |
| 4681 | |
| 4682 | WARN_ON(!spin_is_locked(&proc->inner_lock)); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4683 | spin_lock(&t->lock); |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4684 | to_proc = t->to_proc; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4685 | seq_printf(m, |
| 4686 | "%s %d: %p from %d:%d to %d:%d code %x flags %x pri %ld r%d", |
| 4687 | prefix, t->debug_id, t, |
| 4688 | t->from ? t->from->proc->pid : 0, |
| 4689 | t->from ? t->from->pid : 0, |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4690 | to_proc ? to_proc->pid : 0, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4691 | t->to_thread ? t->to_thread->pid : 0, |
| 4692 | t->code, t->flags, t->priority, t->need_reply); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4693 | spin_unlock(&t->lock); |
| 4694 | |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4695 | if (proc != to_proc) { |
| 4696 | /* |
| 4697 | * Can only safely deref buffer if we are holding the |
| 4698 | * correct proc inner lock for this node |
| 4699 | */ |
| 4700 | seq_puts(m, "\n"); |
| 4701 | return; |
| 4702 | } |
| 4703 | |
| 4704 | if (buffer == NULL) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4705 | seq_puts(m, " buffer free\n"); |
| 4706 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4707 | } |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4708 | if (buffer->target_node) |
| 4709 | seq_printf(m, " node %d", buffer->target_node->debug_id); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4710 | seq_printf(m, " size %zd:%zd data %p\n", |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4711 | buffer->data_size, buffer->offsets_size, |
| 4712 | buffer->data); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4713 | } |
| 4714 | |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4715 | static void print_binder_work_ilocked(struct seq_file *m, |
| 4716 | struct binder_proc *proc, |
| 4717 | const char *prefix, |
| 4718 | const char *transaction_prefix, |
| 4719 | struct binder_work *w) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4720 | { |
| 4721 | struct binder_node *node; |
| 4722 | struct binder_transaction *t; |
| 4723 | |
| 4724 | switch (w->type) { |
| 4725 | case BINDER_WORK_TRANSACTION: |
| 4726 | t = container_of(w, struct binder_transaction, work); |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4727 | print_binder_transaction_ilocked( |
| 4728 | m, proc, transaction_prefix, t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4729 | break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4730 | case BINDER_WORK_RETURN_ERROR: { |
| 4731 | struct binder_error *e = container_of( |
| 4732 | w, struct binder_error, work); |
| 4733 | |
| 4734 | seq_printf(m, "%stransaction error: %u\n", |
| 4735 | prefix, e->cmd); |
| 4736 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4737 | case BINDER_WORK_TRANSACTION_COMPLETE: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4738 | seq_printf(m, "%stransaction complete\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4739 | break; |
| 4740 | case BINDER_WORK_NODE: |
| 4741 | node = container_of(w, struct binder_node, work); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4742 | seq_printf(m, "%snode work %d: u%016llx c%016llx\n", |
| 4743 | prefix, node->debug_id, |
| 4744 | (u64)node->ptr, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4745 | break; |
| 4746 | case BINDER_WORK_DEAD_BINDER: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4747 | seq_printf(m, "%shas dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4748 | break; |
| 4749 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4750 | seq_printf(m, "%shas cleared dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4751 | break; |
| 4752 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4753 | seq_printf(m, "%shas cleared death notification\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4754 | break; |
| 4755 | default: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4756 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4757 | break; |
| 4758 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4759 | } |
| 4760 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4761 | static void print_binder_thread_ilocked(struct seq_file *m, |
| 4762 | struct binder_thread *thread, |
| 4763 | int print_always) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4764 | { |
| 4765 | struct binder_transaction *t; |
| 4766 | struct binder_work *w; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4767 | size_t start_pos = m->count; |
| 4768 | size_t header_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4769 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4770 | WARN_ON(!spin_is_locked(&thread->proc->inner_lock)); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4771 | 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] | 4772 | thread->pid, thread->looper, |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4773 | thread->looper_need_return, |
| 4774 | atomic_read(&thread->tmp_ref)); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4775 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4776 | t = thread->transaction_stack; |
| 4777 | while (t) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4778 | if (t->from == thread) { |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4779 | print_binder_transaction_ilocked(m, thread->proc, |
| 4780 | " outgoing transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4781 | t = t->from_parent; |
| 4782 | } else if (t->to_thread == thread) { |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4783 | print_binder_transaction_ilocked(m, thread->proc, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4784 | " incoming transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4785 | t = t->to_parent; |
| 4786 | } else { |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4787 | print_binder_transaction_ilocked(m, thread->proc, |
| 4788 | " bad transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4789 | t = NULL; |
| 4790 | } |
| 4791 | } |
| 4792 | list_for_each_entry(w, &thread->todo, entry) { |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4793 | print_binder_work_ilocked(m, thread->proc, " ", |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4794 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4795 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4796 | if (!print_always && m->count == header_pos) |
| 4797 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4798 | } |
| 4799 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4800 | static void print_binder_node_nilocked(struct seq_file *m, |
| 4801 | struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4802 | { |
| 4803 | struct binder_ref *ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4804 | struct binder_work *w; |
| 4805 | int count; |
| 4806 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4807 | WARN_ON(!spin_is_locked(&node->lock)); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4808 | if (node->proc) |
| 4809 | WARN_ON(!spin_is_locked(&node->proc->inner_lock)); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4810 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4811 | count = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4812 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4813 | count++; |
| 4814 | |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4815 | 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] | 4816 | node->debug_id, (u64)node->ptr, (u64)node->cookie, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4817 | node->has_strong_ref, node->has_weak_ref, |
| 4818 | node->local_strong_refs, node->local_weak_refs, |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4819 | node->internal_strong_refs, count, node->tmp_refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4820 | if (count) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4821 | seq_puts(m, " proc"); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4822 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4823 | seq_printf(m, " %d", ref->proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4824 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4825 | seq_puts(m, "\n"); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4826 | if (node->proc) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4827 | list_for_each_entry(w, &node->async_todo, entry) |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4828 | print_binder_work_ilocked(m, node->proc, " ", |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4829 | " pending async transaction", w); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4830 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4831 | } |
| 4832 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4833 | static void print_binder_ref_olocked(struct seq_file *m, |
| 4834 | struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4835 | { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4836 | WARN_ON(!spin_is_locked(&ref->proc->outer_lock)); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4837 | binder_node_lock(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 4838 | seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n", |
| 4839 | ref->data.debug_id, ref->data.desc, |
| 4840 | ref->node->proc ? "" : "dead ", |
| 4841 | ref->node->debug_id, ref->data.strong, |
| 4842 | ref->data.weak, ref->death); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4843 | binder_node_unlock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4844 | } |
| 4845 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4846 | static void print_binder_proc(struct seq_file *m, |
| 4847 | struct binder_proc *proc, int print_all) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4848 | { |
| 4849 | struct binder_work *w; |
| 4850 | struct rb_node *n; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4851 | size_t start_pos = m->count; |
| 4852 | size_t header_pos; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4853 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4854 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4855 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 4856 | seq_printf(m, "context %s\n", proc->context->name); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4857 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4858 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4859 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4860 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4861 | 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] | 4862 | rb_node), print_all); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4863 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4864 | 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] | 4865 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 4866 | rb_node); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4867 | /* |
| 4868 | * take a temporary reference on the node so it |
| 4869 | * survives and isn't removed from the tree |
| 4870 | * while we print it. |
| 4871 | */ |
| 4872 | binder_inc_node_tmpref_ilocked(node); |
| 4873 | /* Need to drop inner lock to take node lock */ |
| 4874 | binder_inner_proc_unlock(proc); |
| 4875 | if (last_node) |
| 4876 | binder_put_node(last_node); |
| 4877 | binder_node_inner_lock(node); |
| 4878 | print_binder_node_nilocked(m, node); |
| 4879 | binder_node_inner_unlock(node); |
| 4880 | last_node = node; |
| 4881 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4882 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 4883 | binder_inner_proc_unlock(proc); |
| 4884 | if (last_node) |
| 4885 | binder_put_node(last_node); |
| 4886 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4887 | if (print_all) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4888 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4889 | for (n = rb_first(&proc->refs_by_desc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4890 | n != NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4891 | n = rb_next(n)) |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4892 | print_binder_ref_olocked(m, rb_entry(n, |
| 4893 | struct binder_ref, |
| 4894 | rb_node_desc)); |
| 4895 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4896 | } |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4897 | binder_alloc_print_allocated(m, &proc->alloc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4898 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4899 | list_for_each_entry(w, &proc->todo, entry) |
Todd Kjos | 5f2f6369 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 4900 | print_binder_work_ilocked(m, proc, " ", |
| 4901 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4902 | list_for_each_entry(w, &proc->delivered_death, entry) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4903 | seq_puts(m, " has delivered dead binder\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4904 | break; |
| 4905 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4906 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4907 | if (!print_all && m->count == header_pos) |
| 4908 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4909 | } |
| 4910 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 4911 | static const char * const binder_return_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4912 | "BR_ERROR", |
| 4913 | "BR_OK", |
| 4914 | "BR_TRANSACTION", |
| 4915 | "BR_REPLY", |
| 4916 | "BR_ACQUIRE_RESULT", |
| 4917 | "BR_DEAD_REPLY", |
| 4918 | "BR_TRANSACTION_COMPLETE", |
| 4919 | "BR_INCREFS", |
| 4920 | "BR_ACQUIRE", |
| 4921 | "BR_RELEASE", |
| 4922 | "BR_DECREFS", |
| 4923 | "BR_ATTEMPT_ACQUIRE", |
| 4924 | "BR_NOOP", |
| 4925 | "BR_SPAWN_LOOPER", |
| 4926 | "BR_FINISHED", |
| 4927 | "BR_DEAD_BINDER", |
| 4928 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
| 4929 | "BR_FAILED_REPLY" |
| 4930 | }; |
| 4931 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 4932 | static const char * const binder_command_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4933 | "BC_TRANSACTION", |
| 4934 | "BC_REPLY", |
| 4935 | "BC_ACQUIRE_RESULT", |
| 4936 | "BC_FREE_BUFFER", |
| 4937 | "BC_INCREFS", |
| 4938 | "BC_ACQUIRE", |
| 4939 | "BC_RELEASE", |
| 4940 | "BC_DECREFS", |
| 4941 | "BC_INCREFS_DONE", |
| 4942 | "BC_ACQUIRE_DONE", |
| 4943 | "BC_ATTEMPT_ACQUIRE", |
| 4944 | "BC_REGISTER_LOOPER", |
| 4945 | "BC_ENTER_LOOPER", |
| 4946 | "BC_EXIT_LOOPER", |
| 4947 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 4948 | "BC_CLEAR_DEATH_NOTIFICATION", |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 4949 | "BC_DEAD_BINDER_DONE", |
| 4950 | "BC_TRANSACTION_SG", |
| 4951 | "BC_REPLY_SG", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4952 | }; |
| 4953 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 4954 | static const char * const binder_objstat_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4955 | "proc", |
| 4956 | "thread", |
| 4957 | "node", |
| 4958 | "ref", |
| 4959 | "death", |
| 4960 | "transaction", |
| 4961 | "transaction_complete" |
| 4962 | }; |
| 4963 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4964 | static void print_binder_stats(struct seq_file *m, const char *prefix, |
| 4965 | struct binder_stats *stats) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4966 | { |
| 4967 | int i; |
| 4968 | |
| 4969 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4970 | ARRAY_SIZE(binder_command_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4971 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 4972 | int temp = atomic_read(&stats->bc[i]); |
| 4973 | |
| 4974 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4975 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 4976 | binder_command_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4977 | } |
| 4978 | |
| 4979 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4980 | ARRAY_SIZE(binder_return_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4981 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 4982 | int temp = atomic_read(&stats->br[i]); |
| 4983 | |
| 4984 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4985 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 4986 | binder_return_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4987 | } |
| 4988 | |
| 4989 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4990 | ARRAY_SIZE(binder_objstat_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4991 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 4992 | ARRAY_SIZE(stats->obj_deleted)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4993 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 4994 | int created = atomic_read(&stats->obj_created[i]); |
| 4995 | int deleted = atomic_read(&stats->obj_deleted[i]); |
| 4996 | |
| 4997 | if (created || deleted) |
| 4998 | seq_printf(m, "%s%s: active %d total %d\n", |
| 4999 | prefix, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5000 | binder_objstat_strings[i], |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 5001 | created - deleted, |
| 5002 | created); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5003 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5004 | } |
| 5005 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5006 | static void print_binder_proc_stats(struct seq_file *m, |
| 5007 | struct binder_proc *proc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5008 | { |
| 5009 | struct binder_work *w; |
| 5010 | struct rb_node *n; |
| 5011 | int count, strong, weak; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5012 | size_t free_async_space = |
| 5013 | binder_alloc_get_free_async_space(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5014 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5015 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5016 | seq_printf(m, "context %s\n", proc->context->name); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5017 | count = 0; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5018 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5019 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 5020 | count++; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5021 | seq_printf(m, " threads: %d\n", count); |
| 5022 | seq_printf(m, " requested threads: %d+%d/%d\n" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5023 | " ready threads %d\n" |
| 5024 | " free async space %zd\n", proc->requested_threads, |
| 5025 | proc->requested_threads_started, proc->max_threads, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5026 | proc->ready_threads, |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5027 | free_async_space); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5028 | count = 0; |
| 5029 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) |
| 5030 | count++; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5031 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5032 | seq_printf(m, " nodes: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5033 | count = 0; |
| 5034 | strong = 0; |
| 5035 | weak = 0; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5036 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5037 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 5038 | struct binder_ref *ref = rb_entry(n, struct binder_ref, |
| 5039 | rb_node_desc); |
| 5040 | count++; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 5041 | strong += ref->data.strong; |
| 5042 | weak += ref->data.weak; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5043 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5044 | binder_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5045 | 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] | 5046 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5047 | count = binder_alloc_get_allocated_count(&proc->alloc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5048 | seq_printf(m, " buffers: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5049 | |
Sherry Yang | 8ef4665 | 2017-08-31 11:56:36 -0700 | [diff] [blame^] | 5050 | binder_alloc_print_pages(m, &proc->alloc); |
| 5051 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5052 | count = 0; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5053 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5054 | list_for_each_entry(w, &proc->todo, entry) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5055 | if (w->type == BINDER_WORK_TRANSACTION) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5056 | count++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5057 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5058 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5059 | seq_printf(m, " pending transactions: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5060 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5061 | print_binder_stats(m, " ", &proc->stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5062 | } |
| 5063 | |
| 5064 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5065 | static int binder_state_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5066 | { |
| 5067 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5068 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5069 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5070 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5071 | seq_puts(m, "binder state:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5072 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5073 | spin_lock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5074 | if (!hlist_empty(&binder_dead_nodes)) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5075 | seq_puts(m, "dead nodes:\n"); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5076 | hlist_for_each_entry(node, &binder_dead_nodes, dead_node) { |
| 5077 | /* |
| 5078 | * take a temporary reference on the node so it |
| 5079 | * survives and isn't removed from the list |
| 5080 | * while we print it. |
| 5081 | */ |
| 5082 | node->tmp_refs++; |
| 5083 | spin_unlock(&binder_dead_nodes_lock); |
| 5084 | if (last_node) |
| 5085 | binder_put_node(last_node); |
| 5086 | binder_node_lock(node); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5087 | print_binder_node_nilocked(m, node); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5088 | binder_node_unlock(node); |
| 5089 | last_node = node; |
| 5090 | spin_lock(&binder_dead_nodes_lock); |
| 5091 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5092 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5093 | if (last_node) |
| 5094 | binder_put_node(last_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5095 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5096 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5097 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5098 | print_binder_proc(m, proc, 1); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5099 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 5100 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5101 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5102 | } |
| 5103 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5104 | static int binder_stats_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5105 | { |
| 5106 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5107 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5108 | seq_puts(m, "binder stats:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5109 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5110 | print_binder_stats(m, "", &binder_stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5111 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5112 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5113 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5114 | print_binder_proc_stats(m, proc); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5115 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 5116 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5117 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5118 | } |
| 5119 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5120 | static int binder_transactions_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5121 | { |
| 5122 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5123 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5124 | seq_puts(m, "binder transactions:\n"); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5125 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5126 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5127 | print_binder_proc(m, proc, 0); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5128 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 5129 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5130 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5131 | } |
| 5132 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5133 | static int binder_proc_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5134 | { |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5135 | struct binder_proc *itr; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5136 | int pid = (unsigned long)m->private; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5137 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5138 | mutex_lock(&binder_procs_lock); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5139 | hlist_for_each_entry(itr, &binder_procs, proc_node) { |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5140 | if (itr->pid == pid) { |
| 5141 | seq_puts(m, "binder proc state:\n"); |
| 5142 | print_binder_proc(m, itr, 1); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 5143 | } |
| 5144 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5145 | mutex_unlock(&binder_procs_lock); |
| 5146 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5147 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5148 | } |
| 5149 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5150 | static void print_binder_transaction_log_entry(struct seq_file *m, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5151 | struct binder_transaction_log_entry *e) |
| 5152 | { |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5153 | int debug_id = READ_ONCE(e->debug_id_done); |
| 5154 | /* |
| 5155 | * read barrier to guarantee debug_id_done read before |
| 5156 | * we print the log values |
| 5157 | */ |
| 5158 | smp_rmb(); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5159 | seq_printf(m, |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5160 | "%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] | 5161 | e->debug_id, (e->call_type == 2) ? "reply" : |
| 5162 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5163 | e->from_thread, e->to_proc, e->to_thread, e->context_name, |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 5164 | e->to_node, e->target_handle, e->data_size, e->offsets_size, |
| 5165 | e->return_error, e->return_error_param, |
| 5166 | e->return_error_line); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5167 | /* |
| 5168 | * read-barrier to guarantee read of debug_id_done after |
| 5169 | * done printing the fields of the entry |
| 5170 | */ |
| 5171 | smp_rmb(); |
| 5172 | seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ? |
| 5173 | "\n" : " (incomplete)\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5174 | } |
| 5175 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5176 | 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] | 5177 | { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5178 | struct binder_transaction_log *log = m->private; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5179 | unsigned int log_cur = atomic_read(&log->cur); |
| 5180 | unsigned int count; |
| 5181 | unsigned int cur; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5182 | int i; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5183 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5184 | count = log_cur + 1; |
| 5185 | cur = count < ARRAY_SIZE(log->entry) && !log->full ? |
| 5186 | 0 : count % ARRAY_SIZE(log->entry); |
| 5187 | if (count > ARRAY_SIZE(log->entry) || log->full) |
| 5188 | count = ARRAY_SIZE(log->entry); |
| 5189 | for (i = 0; i < count; i++) { |
| 5190 | unsigned int index = cur++ % ARRAY_SIZE(log->entry); |
| 5191 | |
| 5192 | print_binder_transaction_log_entry(m, &log->entry[index]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5193 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5194 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5195 | } |
| 5196 | |
| 5197 | static const struct file_operations binder_fops = { |
| 5198 | .owner = THIS_MODULE, |
| 5199 | .poll = binder_poll, |
| 5200 | .unlocked_ioctl = binder_ioctl, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 5201 | .compat_ioctl = binder_ioctl, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5202 | .mmap = binder_mmap, |
| 5203 | .open = binder_open, |
| 5204 | .flush = binder_flush, |
| 5205 | .release = binder_release, |
| 5206 | }; |
| 5207 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5208 | BINDER_DEBUG_ENTRY(state); |
| 5209 | BINDER_DEBUG_ENTRY(stats); |
| 5210 | BINDER_DEBUG_ENTRY(transactions); |
| 5211 | BINDER_DEBUG_ENTRY(transaction_log); |
| 5212 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5213 | static int __init init_binder_device(const char *name) |
| 5214 | { |
| 5215 | int ret; |
| 5216 | struct binder_device *binder_device; |
| 5217 | |
| 5218 | binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL); |
| 5219 | if (!binder_device) |
| 5220 | return -ENOMEM; |
| 5221 | |
| 5222 | binder_device->miscdev.fops = &binder_fops; |
| 5223 | binder_device->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 5224 | binder_device->miscdev.name = name; |
| 5225 | |
| 5226 | binder_device->context.binder_context_mgr_uid = INVALID_UID; |
| 5227 | binder_device->context.name = name; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5228 | mutex_init(&binder_device->context.context_mgr_node_lock); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5229 | |
| 5230 | ret = misc_register(&binder_device->miscdev); |
| 5231 | if (ret < 0) { |
| 5232 | kfree(binder_device); |
| 5233 | return ret; |
| 5234 | } |
| 5235 | |
| 5236 | hlist_add_head(&binder_device->hlist, &binder_devices); |
| 5237 | |
| 5238 | return ret; |
| 5239 | } |
| 5240 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5241 | static int __init binder_init(void) |
| 5242 | { |
| 5243 | int ret; |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 5244 | char *device_name, *device_names, *device_tmp; |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5245 | struct binder_device *device; |
| 5246 | struct hlist_node *tmp; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5247 | |
Sherry Yang | f2517eb | 2017-08-23 08:46:42 -0700 | [diff] [blame] | 5248 | binder_alloc_shrinker_init(); |
| 5249 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 5250 | atomic_set(&binder_transaction_log.cur, ~0U); |
| 5251 | atomic_set(&binder_transaction_log_failed.cur, ~0U); |
| 5252 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5253 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); |
| 5254 | if (binder_debugfs_dir_entry_root) |
| 5255 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", |
| 5256 | binder_debugfs_dir_entry_root); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5257 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5258 | if (binder_debugfs_dir_entry_root) { |
| 5259 | debugfs_create_file("state", |
| 5260 | S_IRUGO, |
| 5261 | binder_debugfs_dir_entry_root, |
| 5262 | NULL, |
| 5263 | &binder_state_fops); |
| 5264 | debugfs_create_file("stats", |
| 5265 | S_IRUGO, |
| 5266 | binder_debugfs_dir_entry_root, |
| 5267 | NULL, |
| 5268 | &binder_stats_fops); |
| 5269 | debugfs_create_file("transactions", |
| 5270 | S_IRUGO, |
| 5271 | binder_debugfs_dir_entry_root, |
| 5272 | NULL, |
| 5273 | &binder_transactions_fops); |
| 5274 | debugfs_create_file("transaction_log", |
| 5275 | S_IRUGO, |
| 5276 | binder_debugfs_dir_entry_root, |
| 5277 | &binder_transaction_log, |
| 5278 | &binder_transaction_log_fops); |
| 5279 | debugfs_create_file("failed_transaction_log", |
| 5280 | S_IRUGO, |
| 5281 | binder_debugfs_dir_entry_root, |
| 5282 | &binder_transaction_log_failed, |
| 5283 | &binder_transaction_log_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5284 | } |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5285 | |
| 5286 | /* |
| 5287 | * Copy the module_parameter string, because we don't want to |
| 5288 | * tokenize it in-place. |
| 5289 | */ |
| 5290 | device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL); |
| 5291 | if (!device_names) { |
| 5292 | ret = -ENOMEM; |
| 5293 | goto err_alloc_device_names_failed; |
| 5294 | } |
| 5295 | strcpy(device_names, binder_devices_param); |
| 5296 | |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 5297 | device_tmp = device_names; |
| 5298 | while ((device_name = strsep(&device_tmp, ","))) { |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5299 | ret = init_binder_device(device_name); |
| 5300 | if (ret) |
| 5301 | goto err_init_binder_device_failed; |
| 5302 | } |
| 5303 | |
| 5304 | return ret; |
| 5305 | |
| 5306 | err_init_binder_device_failed: |
| 5307 | hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) { |
| 5308 | misc_deregister(&device->miscdev); |
| 5309 | hlist_del(&device->hlist); |
| 5310 | kfree(device); |
| 5311 | } |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 5312 | |
| 5313 | kfree(device_names); |
| 5314 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5315 | err_alloc_device_names_failed: |
| 5316 | debugfs_remove_recursive(binder_debugfs_dir_entry_root); |
| 5317 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5318 | return ret; |
| 5319 | } |
| 5320 | |
| 5321 | device_initcall(binder_init); |
| 5322 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5323 | #define CREATE_TRACE_POINTS |
| 5324 | #include "binder_trace.h" |
| 5325 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5326 | MODULE_LICENSE("GPL v2"); |