Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1 | /* binder.c |
| 2 | * |
| 3 | * Android IPC Subsystem |
| 4 | * |
| 5 | * Copyright (C) 2007-2008 Google, Inc. |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 18 | /* |
| 19 | * Locking overview |
| 20 | * |
| 21 | * There are 3 main spinlocks which must be acquired in the |
| 22 | * order shown: |
| 23 | * |
| 24 | * 1) proc->outer_lock : protects binder_ref |
| 25 | * binder_proc_lock() and binder_proc_unlock() are |
| 26 | * used to acq/rel. |
| 27 | * 2) node->lock : protects most fields of binder_node. |
| 28 | * binder_node_lock() and binder_node_unlock() are |
| 29 | * used to acq/rel |
| 30 | * 3) proc->inner_lock : protects the thread and node lists |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 31 | * (proc->threads, proc->waiting_threads, proc->nodes) |
| 32 | * and all todo lists associated with the binder_proc |
| 33 | * (proc->todo, thread->todo, proc->delivered_death and |
| 34 | * node->async_todo), as well as thread->transaction_stack |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 35 | * binder_inner_proc_lock() and binder_inner_proc_unlock() |
| 36 | * are used to acq/rel |
| 37 | * |
| 38 | * Any lock under procA must never be nested under any lock at the same |
| 39 | * level or below on procB. |
| 40 | * |
| 41 | * Functions that require a lock held on entry indicate which lock |
| 42 | * in the suffix of the function name: |
| 43 | * |
| 44 | * foo_olocked() : requires node->outer_lock |
| 45 | * foo_nlocked() : requires node->lock |
| 46 | * foo_ilocked() : requires proc->inner_lock |
| 47 | * foo_oilocked(): requires proc->outer_lock and proc->inner_lock |
| 48 | * foo_nilocked(): requires node->lock and proc->inner_lock |
| 49 | * ... |
| 50 | */ |
| 51 | |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 52 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 53 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 54 | #include <asm/cacheflush.h> |
| 55 | #include <linux/fdtable.h> |
| 56 | #include <linux/file.h> |
Colin Cross | e2610b2 | 2013-05-06 23:50:15 +0000 | [diff] [blame] | 57 | #include <linux/freezer.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 58 | #include <linux/fs.h> |
| 59 | #include <linux/list.h> |
| 60 | #include <linux/miscdevice.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 61 | #include <linux/module.h> |
| 62 | #include <linux/mutex.h> |
| 63 | #include <linux/nsproxy.h> |
| 64 | #include <linux/poll.h> |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 65 | #include <linux/debugfs.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 66 | #include <linux/rbtree.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 67 | #include <linux/sched/signal.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 68 | #include <linux/sched/mm.h> |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 69 | #include <linux/seq_file.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 70 | #include <linux/uaccess.h> |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 71 | #include <linux/pid_namespace.h> |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 72 | #include <linux/security.h> |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 73 | #include <linux/spinlock.h> |
Sherry Yang | b3f8313 | 2018-08-07 12:57:13 -0700 | [diff] [blame] | 74 | #include <linux/ratelimit.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 75 | |
Greg Kroah-Hartman | 9246a4a | 2014-10-16 15:26:51 +0200 | [diff] [blame] | 76 | #include <uapi/linux/android/binder.h> |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 77 | #include <uapi/linux/sched/types.h> |
Todd Kjos | 0c972a0 | 2017-06-29 12:01:41 -0700 | [diff] [blame] | 78 | #include "binder_alloc.h" |
Christian Brauner | 34fbd92 | 2018-12-14 13:11:14 +0100 | [diff] [blame] | 79 | #include "binder_internal.h" |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 80 | #include "binder_trace.h" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 81 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 82 | static HLIST_HEAD(binder_deferred_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 83 | static DEFINE_MUTEX(binder_deferred_lock); |
| 84 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 85 | static HLIST_HEAD(binder_devices); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 86 | static HLIST_HEAD(binder_procs); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 87 | static DEFINE_MUTEX(binder_procs_lock); |
| 88 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 89 | static HLIST_HEAD(binder_dead_nodes); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 90 | static DEFINE_SPINLOCK(binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 91 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 92 | static struct dentry *binder_debugfs_dir_entry_root; |
| 93 | static struct dentry *binder_debugfs_dir_entry_proc; |
Todd Kjos | 656a800 | 2017-06-29 12:01:45 -0700 | [diff] [blame] | 94 | static atomic_t binder_last_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 95 | |
Yangtao Li | 8b2246a | 2018-11-30 20:26:30 -0500 | [diff] [blame] | 96 | static int proc_show(struct seq_file *m, void *unused); |
| 97 | DEFINE_SHOW_ATTRIBUTE(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 98 | |
| 99 | /* This is only defined in include/asm-arm/sizes.h */ |
| 100 | #ifndef SZ_1K |
| 101 | #define SZ_1K 0x400 |
| 102 | #endif |
| 103 | |
| 104 | #ifndef SZ_4M |
| 105 | #define SZ_4M 0x400000 |
| 106 | #endif |
| 107 | |
| 108 | #define FORBIDDEN_MMAP_FLAGS (VM_WRITE) |
| 109 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 110 | enum { |
| 111 | BINDER_DEBUG_USER_ERROR = 1U << 0, |
| 112 | BINDER_DEBUG_FAILED_TRANSACTION = 1U << 1, |
| 113 | BINDER_DEBUG_DEAD_TRANSACTION = 1U << 2, |
| 114 | BINDER_DEBUG_OPEN_CLOSE = 1U << 3, |
| 115 | BINDER_DEBUG_DEAD_BINDER = 1U << 4, |
| 116 | BINDER_DEBUG_DEATH_NOTIFICATION = 1U << 5, |
| 117 | BINDER_DEBUG_READ_WRITE = 1U << 6, |
| 118 | BINDER_DEBUG_USER_REFS = 1U << 7, |
| 119 | BINDER_DEBUG_THREADS = 1U << 8, |
| 120 | BINDER_DEBUG_TRANSACTION = 1U << 9, |
| 121 | BINDER_DEBUG_TRANSACTION_COMPLETE = 1U << 10, |
| 122 | BINDER_DEBUG_FREE_BUFFER = 1U << 11, |
| 123 | BINDER_DEBUG_INTERNAL_REFS = 1U << 12, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 124 | BINDER_DEBUG_PRIORITY_CAP = 1U << 13, |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 125 | BINDER_DEBUG_SPINLOCKS = 1U << 14, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 126 | }; |
| 127 | static uint32_t binder_debug_mask = BINDER_DEBUG_USER_ERROR | |
| 128 | BINDER_DEBUG_FAILED_TRANSACTION | BINDER_DEBUG_DEAD_TRANSACTION; |
Harsh Shandilya | c5b47d2 | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 129 | module_param_named(debug_mask, binder_debug_mask, uint, 0644); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 130 | |
Hridya Valsaraju | e8fb393 | 2019-08-08 15:27:25 -0700 | [diff] [blame] | 131 | char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES; |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 132 | module_param_named(devices, binder_devices_param, charp, 0444); |
| 133 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 134 | static DECLARE_WAIT_QUEUE_HEAD(binder_user_error_wait); |
| 135 | static int binder_stop_on_user_error; |
| 136 | |
| 137 | static int binder_set_stop_on_user_error(const char *val, |
Kees Cook | 549b203 | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 138 | const struct kernel_param *kp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 139 | { |
| 140 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 141 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 142 | ret = param_set_int(val, kp); |
| 143 | if (binder_stop_on_user_error < 2) |
| 144 | wake_up(&binder_user_error_wait); |
| 145 | return ret; |
| 146 | } |
| 147 | module_param_call(stop_on_user_error, binder_set_stop_on_user_error, |
Harsh Shandilya | c5b47d2 | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 148 | param_get_int, &binder_stop_on_user_error, 0644); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 149 | |
| 150 | #define binder_debug(mask, x...) \ |
| 151 | do { \ |
| 152 | if (binder_debug_mask & mask) \ |
Sherry Yang | b3f8313 | 2018-08-07 12:57:13 -0700 | [diff] [blame] | 153 | pr_info_ratelimited(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 154 | } while (0) |
| 155 | |
| 156 | #define binder_user_error(x...) \ |
| 157 | do { \ |
| 158 | if (binder_debug_mask & BINDER_DEBUG_USER_ERROR) \ |
Sherry Yang | b3f8313 | 2018-08-07 12:57:13 -0700 | [diff] [blame] | 159 | pr_info_ratelimited(x); \ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 160 | if (binder_stop_on_user_error) \ |
| 161 | binder_stop_on_user_error = 2; \ |
| 162 | } while (0) |
| 163 | |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 164 | #define binder_set_extended_error(ee, _id, _command, _param) \ |
| 165 | do { \ |
| 166 | (ee)->id = _id; \ |
| 167 | (ee)->command = _command; \ |
| 168 | (ee)->param = _param; \ |
| 169 | } while (0) |
| 170 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 171 | #define to_flat_binder_object(hdr) \ |
| 172 | container_of(hdr, struct flat_binder_object, hdr) |
| 173 | |
| 174 | #define to_binder_fd_object(hdr) container_of(hdr, struct binder_fd_object, hdr) |
| 175 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 176 | #define to_binder_buffer_object(hdr) \ |
| 177 | container_of(hdr, struct binder_buffer_object, hdr) |
| 178 | |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 179 | #define to_binder_fd_array_object(hdr) \ |
| 180 | container_of(hdr, struct binder_fd_array_object, hdr) |
| 181 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 182 | enum binder_stat_types { |
| 183 | BINDER_STAT_PROC, |
| 184 | BINDER_STAT_THREAD, |
| 185 | BINDER_STAT_NODE, |
| 186 | BINDER_STAT_REF, |
| 187 | BINDER_STAT_DEATH, |
| 188 | BINDER_STAT_TRANSACTION, |
| 189 | BINDER_STAT_TRANSACTION_COMPLETE, |
| 190 | BINDER_STAT_COUNT |
| 191 | }; |
| 192 | |
| 193 | struct binder_stats { |
Li Li | af1f984 | 2022-11-23 12:16:54 -0800 | [diff] [blame] | 194 | atomic_t br[_IOC_NR(BR_TRANSACTION_PENDING_FROZEN) + 1]; |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 195 | atomic_t bc[_IOC_NR(BC_REPLY_SG) + 1]; |
| 196 | atomic_t obj_created[BINDER_STAT_COUNT]; |
| 197 | atomic_t obj_deleted[BINDER_STAT_COUNT]; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | static struct binder_stats binder_stats; |
| 201 | |
| 202 | static inline void binder_stats_deleted(enum binder_stat_types type) |
| 203 | { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 204 | atomic_inc(&binder_stats.obj_deleted[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static inline void binder_stats_created(enum binder_stat_types type) |
| 208 | { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 209 | atomic_inc(&binder_stats.obj_created[type]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 210 | } |
| 211 | |
Hridya Valsaraju | 3741393 | 2019-09-03 09:16:54 -0700 | [diff] [blame] | 212 | struct binder_transaction_log binder_transaction_log; |
| 213 | struct binder_transaction_log binder_transaction_log_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 214 | |
| 215 | static struct binder_transaction_log_entry *binder_transaction_log_add( |
| 216 | struct binder_transaction_log *log) |
| 217 | { |
| 218 | struct binder_transaction_log_entry *e; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 219 | unsigned int cur = atomic_inc_return(&log->cur); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 220 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 221 | if (cur >= ARRAY_SIZE(log->entry)) |
Gustavo A. R. Silva | 10340bf | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 222 | log->full = true; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 223 | e = &log->entry[cur % ARRAY_SIZE(log->entry)]; |
| 224 | WRITE_ONCE(e->debug_id_done, 0); |
| 225 | /* |
| 226 | * write-barrier to synchronize access to e->debug_id_done. |
| 227 | * We make sure the initialized 0 value is seen before |
| 228 | * memset() other fields are zeroed by memset. |
| 229 | */ |
| 230 | smp_wmb(); |
| 231 | memset(e, 0, sizeof(*e)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 232 | return e; |
| 233 | } |
| 234 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 235 | /** |
| 236 | * struct binder_work - work enqueued on a worklist |
| 237 | * @entry: node enqueued on list |
| 238 | * @type: type of work to be performed |
| 239 | * |
| 240 | * There are separate work lists for proc, thread, and node (async). |
| 241 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 242 | struct binder_work { |
| 243 | struct list_head entry; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 244 | |
Todd Kjos | be84da1 | 2020-10-09 16:24:55 -0700 | [diff] [blame] | 245 | enum binder_work_type { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 246 | BINDER_WORK_TRANSACTION = 1, |
| 247 | BINDER_WORK_TRANSACTION_COMPLETE, |
Li Li | af1f984 | 2022-11-23 12:16:54 -0800 | [diff] [blame] | 248 | BINDER_WORK_TRANSACTION_PENDING, |
Hang Lu | 4dbc168 | 2021-04-09 17:40:46 +0800 | [diff] [blame] | 249 | BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT, |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 250 | BINDER_WORK_RETURN_ERROR, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 251 | BINDER_WORK_NODE, |
| 252 | BINDER_WORK_DEAD_BINDER, |
| 253 | BINDER_WORK_DEAD_BINDER_AND_CLEAR, |
| 254 | BINDER_WORK_CLEAR_DEATH_NOTIFICATION, |
| 255 | } type; |
| 256 | }; |
| 257 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 258 | struct binder_error { |
| 259 | struct binder_work work; |
| 260 | uint32_t cmd; |
| 261 | }; |
| 262 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 263 | /** |
| 264 | * struct binder_node - binder node bookkeeping |
| 265 | * @debug_id: unique ID for debugging |
| 266 | * (invariant after initialized) |
| 267 | * @lock: lock for node fields |
| 268 | * @work: worklist element for node work |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 269 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 270 | * @rb_node: element for proc->nodes tree |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 271 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 272 | * @dead_node: element for binder_dead_nodes list |
| 273 | * (protected by binder_dead_nodes_lock) |
| 274 | * @proc: binder_proc that owns this node |
| 275 | * (invariant after initialized) |
| 276 | * @refs: list of references on this node |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 277 | * (protected by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 278 | * @internal_strong_refs: used to take strong references when |
| 279 | * initiating a transaction |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 280 | * (protected by @proc->inner_lock if @proc |
| 281 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 282 | * @local_weak_refs: weak user refs from local process |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 283 | * (protected by @proc->inner_lock if @proc |
| 284 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 285 | * @local_strong_refs: strong user refs from local process |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 286 | * (protected by @proc->inner_lock if @proc |
| 287 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 288 | * @tmp_refs: temporary kernel refs |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 289 | * (protected by @proc->inner_lock while @proc |
| 290 | * is valid, and by binder_dead_nodes_lock |
| 291 | * if @proc is NULL. During inc/dec and node release |
| 292 | * it is also protected by @lock to provide safety |
| 293 | * as the node dies and @proc becomes NULL) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 294 | * @ptr: userspace pointer for node |
| 295 | * (invariant, no lock needed) |
| 296 | * @cookie: userspace cookie for node |
| 297 | * (invariant, no lock needed) |
| 298 | * @has_strong_ref: userspace notified of strong ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 299 | * (protected by @proc->inner_lock if @proc |
| 300 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 301 | * @pending_strong_ref: userspace has acked notification of strong ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 302 | * (protected by @proc->inner_lock if @proc |
| 303 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 304 | * @has_weak_ref: userspace notified of weak ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 305 | * (protected by @proc->inner_lock if @proc |
| 306 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 307 | * @pending_weak_ref: userspace has acked notification of weak ref |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 308 | * (protected by @proc->inner_lock if @proc |
| 309 | * and by @lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 310 | * @has_async_transaction: async transaction to node in progress |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 311 | * (protected by @lock) |
Martijn Coenen | 69308b3 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 312 | * @sched_policy: minimum scheduling policy for node |
| 313 | * (invariant after initialized) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 314 | * @accept_fds: file descriptor operations supported for node |
| 315 | * (invariant after initialized) |
| 316 | * @min_priority: minimum scheduling priority |
| 317 | * (invariant after initialized) |
Martijn Coenen | fb92c34 | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 318 | * @inherit_rt: inherit RT scheduling policy from caller |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 319 | * @txn_security_ctx: require sender's security context |
Martijn Coenen | fb92c34 | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 320 | * (invariant after initialized) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 321 | * @async_todo: list of async work items |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 322 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 323 | * |
| 324 | * Bookkeeping structure for binder nodes. |
| 325 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 326 | struct binder_node { |
| 327 | int debug_id; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 328 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 329 | struct binder_work work; |
| 330 | union { |
| 331 | struct rb_node rb_node; |
| 332 | struct hlist_node dead_node; |
| 333 | }; |
| 334 | struct binder_proc *proc; |
| 335 | struct hlist_head refs; |
| 336 | int internal_strong_refs; |
| 337 | int local_weak_refs; |
| 338 | int local_strong_refs; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 339 | int tmp_refs; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 340 | binder_uintptr_t ptr; |
| 341 | binder_uintptr_t cookie; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 342 | struct { |
| 343 | /* |
| 344 | * bitfield elements protected by |
| 345 | * proc inner_lock |
| 346 | */ |
| 347 | u8 has_strong_ref:1; |
| 348 | u8 pending_strong_ref:1; |
| 349 | u8 has_weak_ref:1; |
| 350 | u8 pending_weak_ref:1; |
| 351 | }; |
| 352 | struct { |
| 353 | /* |
| 354 | * invariant after initialization |
| 355 | */ |
Martijn Coenen | 69308b3 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 356 | u8 sched_policy:2; |
Martijn Coenen | fb92c34 | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 357 | u8 inherit_rt:1; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 358 | u8 accept_fds:1; |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 359 | u8 txn_security_ctx:1; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 360 | u8 min_priority; |
| 361 | }; |
| 362 | bool has_async_transaction; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 363 | struct list_head async_todo; |
| 364 | }; |
| 365 | |
| 366 | struct binder_ref_death { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 367 | /** |
| 368 | * @work: worklist element for death notifications |
| 369 | * (protected by inner_lock of the proc that |
| 370 | * this ref belongs to) |
| 371 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 372 | struct binder_work work; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 373 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 374 | }; |
| 375 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 376 | /** |
| 377 | * struct binder_ref_data - binder_ref counts and id |
| 378 | * @debug_id: unique ID for the ref |
| 379 | * @desc: unique userspace handle for ref |
| 380 | * @strong: strong ref count (debugging only if not locked) |
| 381 | * @weak: weak ref count (debugging only if not locked) |
| 382 | * |
| 383 | * Structure to hold ref count and ref id information. Since |
| 384 | * the actual ref can only be accessed with a lock, this structure |
| 385 | * is used to return information about the ref to callers of |
| 386 | * ref inc/dec functions. |
| 387 | */ |
| 388 | struct binder_ref_data { |
| 389 | int debug_id; |
| 390 | uint32_t desc; |
| 391 | int strong; |
| 392 | int weak; |
| 393 | }; |
| 394 | |
| 395 | /** |
| 396 | * struct binder_ref - struct to track references on nodes |
| 397 | * @data: binder_ref_data containing id, handle, and current refcounts |
| 398 | * @rb_node_desc: node for lookup by @data.desc in proc's rb_tree |
| 399 | * @rb_node_node: node for lookup by @node in proc's rb_tree |
| 400 | * @node_entry: list entry for node->refs list in target node |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 401 | * (protected by @node->lock) |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 402 | * @proc: binder_proc containing ref |
| 403 | * @node: binder_node of target node. When cleaning up a |
| 404 | * ref for deletion in binder_cleanup_ref, a non-NULL |
| 405 | * @node indicates the node must be freed |
| 406 | * @death: pointer to death notification (ref_death) if requested |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 407 | * (protected by @node->lock) |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 408 | * |
| 409 | * Structure to track references from procA to target node (on procB). This |
| 410 | * structure is unsafe to access without holding @proc->outer_lock. |
| 411 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 412 | struct binder_ref { |
| 413 | /* Lookups needed: */ |
| 414 | /* node + proc => ref (transaction) */ |
| 415 | /* desc + proc => ref (transaction, inc/dec ref) */ |
| 416 | /* node => refs + procs (proc exit) */ |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 417 | struct binder_ref_data data; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 418 | struct rb_node rb_node_desc; |
| 419 | struct rb_node rb_node_node; |
| 420 | struct hlist_node node_entry; |
| 421 | struct binder_proc *proc; |
| 422 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 423 | struct binder_ref_death *death; |
| 424 | }; |
| 425 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 426 | enum binder_deferred_state { |
| 427 | BINDER_DEFERRED_PUT_FILES = 0x01, |
| 428 | BINDER_DEFERRED_FLUSH = 0x02, |
| 429 | BINDER_DEFERRED_RELEASE = 0x04, |
| 430 | }; |
| 431 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 432 | /** |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 433 | * struct binder_priority - scheduler policy and priority |
| 434 | * @sched_policy scheduler policy |
| 435 | * @prio [100..139] for SCHED_NORMAL, [0..99] for FIFO/RT |
| 436 | * |
| 437 | * The binder driver supports inheriting the following scheduler policies: |
| 438 | * SCHED_NORMAL |
| 439 | * SCHED_BATCH |
| 440 | * SCHED_FIFO |
| 441 | * SCHED_RR |
| 442 | */ |
| 443 | struct binder_priority { |
| 444 | unsigned int sched_policy; |
| 445 | int prio; |
| 446 | }; |
| 447 | |
| 448 | /** |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 449 | * struct binder_proc - binder process bookkeeping |
| 450 | * @proc_node: element for binder_procs list |
| 451 | * @threads: rbtree of binder_threads in this proc |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 452 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 453 | * @nodes: rbtree of binder nodes associated with |
| 454 | * this proc ordered by node->ptr |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 455 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 456 | * @refs_by_desc: rbtree of refs ordered by ref->desc |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 457 | * (protected by @outer_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 458 | * @refs_by_node: rbtree of refs ordered by ref->node |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 459 | * (protected by @outer_lock) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 460 | * @waiting_threads: threads currently waiting for proc work |
| 461 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 462 | * @pid PID of group_leader of process |
| 463 | * (invariant after initialized) |
| 464 | * @tsk task_struct for group_leader of process |
| 465 | * (invariant after initialized) |
| 466 | * @files files_struct for process |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 467 | * (protected by @files_lock) |
| 468 | * @files_lock mutex to protect @files |
Todd Kjos | ae4a7b9 | 2021-10-12 09:56:12 -0700 | [diff] [blame] | 469 | * @cred struct cred associated with the `struct file` |
| 470 | * in binder_open() |
| 471 | * (invariant after initialized) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 472 | * @deferred_work_node: element for binder_deferred_list |
| 473 | * (protected by binder_deferred_lock) |
| 474 | * @deferred_work: bitmap of deferred work to perform |
| 475 | * (protected by binder_deferred_lock) |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 476 | * @outstanding_txns: number of transactions to be transmitted before |
| 477 | * processes in freeze_wait are woken up |
| 478 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 479 | * @is_dead: process is dead and awaiting free |
| 480 | * when outstanding transactions are cleaned up |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 481 | * (protected by @inner_lock) |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 482 | * @sync_recv: process received sync transactions since last frozen |
Li Li | 503f84d | 2021-09-07 16:12:53 -0700 | [diff] [blame] | 483 | * bit 0: received sync transaction after being frozen |
| 484 | * bit 1: new pending sync transaction during freezing |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 485 | * (protected by @inner_lock) |
| 486 | * @async_recv: process received async transactions since last frozen |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 487 | * (protected by @inner_lock) |
| 488 | * @freeze_wait: waitqueue of processes waiting for all outstanding |
| 489 | * transactions to be processed |
| 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 | * @stats: per-process binder statistics |
| 494 | * (atomics, no lock needed) |
| 495 | * @delivered_death: list of delivered death notification |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 496 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 497 | * @max_threads: cap on number of binder threads |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 498 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 499 | * @requested_threads: number of binder threads requested but not |
| 500 | * yet started. In current implementation, can |
| 501 | * only be 0 or 1. |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 502 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 503 | * @requested_threads_started: number binder threads started |
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 | * @tmp_ref: temporary reference to indicate proc is in use |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 506 | * (protected by @inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 507 | * @default_priority: default scheduler priority |
| 508 | * (invariant after initialized) |
| 509 | * @debugfs_entry: debugfs node |
| 510 | * @alloc: binder allocator bookkeeping |
| 511 | * @context: binder_context for this proc |
| 512 | * (invariant after initialized) |
| 513 | * @inner_lock: can nest under outer_lock and/or node lock |
| 514 | * @outer_lock: no nesting under innor or node lock |
| 515 | * Lock order: 1) outer, 2) node, 3) inner |
Hridya Valsaraju | 1405bf2 | 2019-09-03 09:16:55 -0700 | [diff] [blame] | 516 | * @binderfs_entry: process-specific binderfs log file |
Hang Lu | 4dbc168 | 2021-04-09 17:40:46 +0800 | [diff] [blame] | 517 | * @oneway_spam_detection_enabled: process enabled oneway spam detection |
| 518 | * or not |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 519 | * |
| 520 | * Bookkeeping structure for binder processes |
| 521 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 522 | struct binder_proc { |
| 523 | struct hlist_node proc_node; |
| 524 | struct rb_root threads; |
| 525 | struct rb_root nodes; |
| 526 | struct rb_root refs_by_desc; |
| 527 | struct rb_root refs_by_node; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 528 | struct list_head waiting_threads; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 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; |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 532 | struct mutex files_lock; |
Todd Kjos | ae4a7b9 | 2021-10-12 09:56:12 -0700 | [diff] [blame] | 533 | const struct cred *cred; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 534 | struct hlist_node deferred_work_node; |
| 535 | int deferred_work; |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 536 | int outstanding_txns; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 537 | bool is_dead; |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 538 | bool is_frozen; |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 539 | bool sync_recv; |
| 540 | bool async_recv; |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 541 | wait_queue_head_t freeze_wait; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 542 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 543 | struct list_head todo; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 544 | struct binder_stats stats; |
| 545 | struct list_head delivered_death; |
Carlos Llamas | f642f36 | 2024-04-21 17:37:49 +0000 | [diff] [blame] | 546 | u32 max_threads; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 547 | int requested_threads; |
| 548 | int requested_threads_started; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 549 | int tmp_ref; |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 550 | struct binder_priority default_priority; |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 551 | struct dentry *debugfs_entry; |
Todd Kjos | fdfb4a9 | 2017-06-29 12:01:38 -0700 | [diff] [blame] | 552 | struct binder_alloc alloc; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 553 | struct binder_context *context; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 554 | spinlock_t inner_lock; |
| 555 | spinlock_t outer_lock; |
Hridya Valsaraju | 1405bf2 | 2019-09-03 09:16:55 -0700 | [diff] [blame] | 556 | struct dentry *binderfs_entry; |
Hang Lu | 4dbc168 | 2021-04-09 17:40:46 +0800 | [diff] [blame] | 557 | bool oneway_spam_detection_enabled; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 558 | }; |
| 559 | |
| 560 | enum { |
| 561 | BINDER_LOOPER_STATE_REGISTERED = 0x01, |
| 562 | BINDER_LOOPER_STATE_ENTERED = 0x02, |
| 563 | BINDER_LOOPER_STATE_EXITED = 0x04, |
| 564 | BINDER_LOOPER_STATE_INVALID = 0x08, |
| 565 | BINDER_LOOPER_STATE_WAITING = 0x10, |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 566 | BINDER_LOOPER_STATE_POLL = 0x20, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 567 | }; |
| 568 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 569 | /** |
| 570 | * struct binder_thread - binder thread bookkeeping |
| 571 | * @proc: binder process for this thread |
| 572 | * (invariant after initialization) |
| 573 | * @rb_node: element for proc->threads rbtree |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 574 | * (protected by @proc->inner_lock) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 575 | * @waiting_thread_node: element for @proc->waiting_threads list |
| 576 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 577 | * @pid: PID for this thread |
| 578 | * (invariant after initialization) |
| 579 | * @looper: bitmap of looping state |
| 580 | * (only accessed by this thread) |
| 581 | * @looper_needs_return: looping thread needs to exit driver |
| 582 | * (no lock needed) |
| 583 | * @transaction_stack: stack of in-progress transactions for this thread |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 584 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 585 | * @todo: list of work to do for this thread |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 586 | * (protected by @proc->inner_lock) |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 587 | * @process_todo: whether work in @todo should be processed |
| 588 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 589 | * @return_error: transaction errors reported by this thread |
| 590 | * (only accessed by this thread) |
| 591 | * @reply_error: transaction errors reported by target thread |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 592 | * (protected by @proc->inner_lock) |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 593 | * @ee: extended error information from this thread |
| 594 | * (protected by @proc->inner_lock) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 595 | * @wait: wait queue for thread work |
| 596 | * @stats: per-thread statistics |
| 597 | * (atomics, no lock needed) |
| 598 | * @tmp_ref: temporary reference to indicate thread is in use |
| 599 | * (atomic since @proc->inner_lock cannot |
| 600 | * always be acquired) |
| 601 | * @is_dead: thread is dead and awaiting free |
| 602 | * when outstanding transactions are cleaned up |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 603 | * (protected by @proc->inner_lock) |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 604 | * @task: struct task_struct for this thread |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 605 | * |
| 606 | * Bookkeeping structure for binder threads. |
| 607 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 608 | struct binder_thread { |
| 609 | struct binder_proc *proc; |
| 610 | struct rb_node rb_node; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 611 | struct list_head waiting_thread_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 612 | int pid; |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 613 | int looper; /* only modified by this thread */ |
| 614 | bool looper_need_return; /* can be written by other thread */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 615 | struct binder_transaction *transaction_stack; |
| 616 | struct list_head todo; |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 617 | bool process_todo; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 618 | struct binder_error return_error; |
| 619 | struct binder_error reply_error; |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 620 | struct binder_extended_error ee; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 621 | wait_queue_head_t wait; |
| 622 | struct binder_stats stats; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 623 | atomic_t tmp_ref; |
| 624 | bool is_dead; |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 625 | struct task_struct *task; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 626 | }; |
| 627 | |
| 628 | struct binder_transaction { |
| 629 | int debug_id; |
| 630 | struct binder_work work; |
| 631 | struct binder_thread *from; |
| 632 | struct binder_transaction *from_parent; |
| 633 | struct binder_proc *to_proc; |
| 634 | struct binder_thread *to_thread; |
| 635 | struct binder_transaction *to_parent; |
| 636 | unsigned need_reply:1; |
| 637 | /* unsigned is_dead:1; */ /* not used at the moment */ |
| 638 | |
| 639 | struct binder_buffer *buffer; |
| 640 | unsigned int code; |
| 641 | unsigned int flags; |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 642 | struct binder_priority priority; |
| 643 | struct binder_priority saved_priority; |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 644 | bool set_priority_called; |
Eric W. Biederman | 4a2ebb9 | 2012-05-25 18:34:53 -0600 | [diff] [blame] | 645 | kuid_t sender_euid; |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 646 | binder_uintptr_t security_ctx; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 647 | /** |
| 648 | * @lock: protects @from, @to_proc, and @to_thread |
| 649 | * |
| 650 | * @from, @to_proc, and @to_thread can be set to NULL |
| 651 | * during thread teardown |
| 652 | */ |
| 653 | spinlock_t lock; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 654 | }; |
| 655 | |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 656 | /** |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 657 | * struct binder_object - union of flat binder object types |
| 658 | * @hdr: generic object header |
| 659 | * @fbo: binder object (nodes and refs) |
| 660 | * @fdo: file descriptor object |
| 661 | * @bbo: binder buffer pointer |
| 662 | * @fdao: file descriptor array |
| 663 | * |
| 664 | * Used for type-independent object copies |
| 665 | */ |
| 666 | struct binder_object { |
| 667 | union { |
| 668 | struct binder_object_header hdr; |
| 669 | struct flat_binder_object fbo; |
| 670 | struct binder_fd_object fdo; |
| 671 | struct binder_buffer_object bbo; |
| 672 | struct binder_fd_array_object fdao; |
| 673 | }; |
| 674 | }; |
| 675 | |
| 676 | /** |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 677 | * binder_proc_lock() - Acquire outer lock for given binder_proc |
| 678 | * @proc: struct binder_proc to acquire |
| 679 | * |
| 680 | * Acquires proc->outer_lock. Used to protect binder_ref |
| 681 | * structures associated with the given proc. |
| 682 | */ |
| 683 | #define binder_proc_lock(proc) _binder_proc_lock(proc, __LINE__) |
| 684 | static void |
| 685 | _binder_proc_lock(struct binder_proc *proc, int line) |
| 686 | { |
| 687 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 688 | "%s: line=%d\n", __func__, line); |
| 689 | spin_lock(&proc->outer_lock); |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * binder_proc_unlock() - Release spinlock for given binder_proc |
Randy Dunlap | 6388607 | 2023-01-17 10:37:45 -0800 | [diff] [blame] | 694 | * @proc: struct binder_proc to acquire |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 695 | * |
| 696 | * Release lock acquired via binder_proc_lock() |
| 697 | */ |
Randy Dunlap | 6388607 | 2023-01-17 10:37:45 -0800 | [diff] [blame] | 698 | #define binder_proc_unlock(proc) _binder_proc_unlock(proc, __LINE__) |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 699 | static void |
| 700 | _binder_proc_unlock(struct binder_proc *proc, int line) |
| 701 | { |
| 702 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 703 | "%s: line=%d\n", __func__, line); |
| 704 | spin_unlock(&proc->outer_lock); |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * binder_inner_proc_lock() - Acquire inner lock for given binder_proc |
| 709 | * @proc: struct binder_proc to acquire |
| 710 | * |
| 711 | * Acquires proc->inner_lock. Used to protect todo lists |
| 712 | */ |
| 713 | #define binder_inner_proc_lock(proc) _binder_inner_proc_lock(proc, __LINE__) |
| 714 | static void |
| 715 | _binder_inner_proc_lock(struct binder_proc *proc, int line) |
| 716 | { |
| 717 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 718 | "%s: line=%d\n", __func__, line); |
| 719 | spin_lock(&proc->inner_lock); |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * binder_inner_proc_unlock() - Release inner lock for given binder_proc |
| 724 | * @proc: struct binder_proc to acquire |
| 725 | * |
| 726 | * Release lock acquired via binder_inner_proc_lock() |
| 727 | */ |
| 728 | #define binder_inner_proc_unlock(proc) _binder_inner_proc_unlock(proc, __LINE__) |
| 729 | static void |
| 730 | _binder_inner_proc_unlock(struct binder_proc *proc, int line) |
| 731 | { |
| 732 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 733 | "%s: line=%d\n", __func__, line); |
| 734 | spin_unlock(&proc->inner_lock); |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * binder_node_lock() - Acquire spinlock for given binder_node |
| 739 | * @node: struct binder_node to acquire |
| 740 | * |
| 741 | * Acquires node->lock. Used to protect binder_node fields |
| 742 | */ |
| 743 | #define binder_node_lock(node) _binder_node_lock(node, __LINE__) |
| 744 | static void |
| 745 | _binder_node_lock(struct binder_node *node, int line) |
| 746 | { |
| 747 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 748 | "%s: line=%d\n", __func__, line); |
| 749 | spin_lock(&node->lock); |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * binder_node_unlock() - Release spinlock for given binder_proc |
| 754 | * @node: struct binder_node to acquire |
| 755 | * |
| 756 | * Release lock acquired via binder_node_lock() |
| 757 | */ |
| 758 | #define binder_node_unlock(node) _binder_node_unlock(node, __LINE__) |
| 759 | static void |
| 760 | _binder_node_unlock(struct binder_node *node, int line) |
| 761 | { |
| 762 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 763 | "%s: line=%d\n", __func__, line); |
| 764 | spin_unlock(&node->lock); |
| 765 | } |
| 766 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 767 | /** |
| 768 | * binder_node_inner_lock() - Acquire node and inner locks |
| 769 | * @node: struct binder_node to acquire |
| 770 | * |
| 771 | * Acquires node->lock. If node->proc also acquires |
| 772 | * proc->inner_lock. Used to protect binder_node fields |
| 773 | */ |
| 774 | #define binder_node_inner_lock(node) _binder_node_inner_lock(node, __LINE__) |
| 775 | static void |
| 776 | _binder_node_inner_lock(struct binder_node *node, int line) |
| 777 | { |
| 778 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 779 | "%s: line=%d\n", __func__, line); |
| 780 | spin_lock(&node->lock); |
| 781 | if (node->proc) |
| 782 | binder_inner_proc_lock(node->proc); |
| 783 | } |
| 784 | |
| 785 | /** |
Randy Dunlap | 6388607 | 2023-01-17 10:37:45 -0800 | [diff] [blame] | 786 | * binder_node_inner_unlock() - Release node and inner locks |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 787 | * @node: struct binder_node to acquire |
| 788 | * |
| 789 | * Release lock acquired via binder_node_lock() |
| 790 | */ |
| 791 | #define binder_node_inner_unlock(node) _binder_node_inner_unlock(node, __LINE__) |
| 792 | static void |
| 793 | _binder_node_inner_unlock(struct binder_node *node, int line) |
| 794 | { |
| 795 | struct binder_proc *proc = node->proc; |
| 796 | |
| 797 | binder_debug(BINDER_DEBUG_SPINLOCKS, |
| 798 | "%s: line=%d\n", __func__, line); |
| 799 | if (proc) |
| 800 | binder_inner_proc_unlock(proc); |
| 801 | spin_unlock(&node->lock); |
| 802 | } |
| 803 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 804 | static bool binder_worklist_empty_ilocked(struct list_head *list) |
| 805 | { |
| 806 | return list_empty(list); |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * binder_worklist_empty() - Check if no items on the work list |
| 811 | * @proc: binder_proc associated with list |
| 812 | * @list: list to check |
| 813 | * |
| 814 | * Return: true if there are no items on list, else false |
| 815 | */ |
| 816 | static bool binder_worklist_empty(struct binder_proc *proc, |
| 817 | struct list_head *list) |
| 818 | { |
| 819 | bool ret; |
| 820 | |
| 821 | binder_inner_proc_lock(proc); |
| 822 | ret = binder_worklist_empty_ilocked(list); |
| 823 | binder_inner_proc_unlock(proc); |
| 824 | return ret; |
| 825 | } |
| 826 | |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 827 | /** |
| 828 | * binder_enqueue_work_ilocked() - Add an item to the work list |
| 829 | * @work: struct binder_work to add to list |
| 830 | * @target_list: list to add work to |
| 831 | * |
| 832 | * Adds the work to the specified list. Asserts that work |
| 833 | * is not already on a list. |
| 834 | * |
| 835 | * Requires the proc->inner_lock to be held. |
| 836 | */ |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 837 | static void |
| 838 | binder_enqueue_work_ilocked(struct binder_work *work, |
| 839 | struct list_head *target_list) |
| 840 | { |
| 841 | BUG_ON(target_list == NULL); |
| 842 | BUG_ON(work->entry.next && !list_empty(&work->entry)); |
| 843 | list_add_tail(&work->entry, target_list); |
| 844 | } |
| 845 | |
| 846 | /** |
Martijn Coenen | ad4cc17 | 2017-11-13 09:55:21 +0100 | [diff] [blame] | 847 | * binder_enqueue_deferred_thread_work_ilocked() - Add deferred thread work |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 848 | * @thread: thread to queue work to |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 849 | * @work: struct binder_work to add to list |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 850 | * |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 851 | * Adds the work to the todo list of the thread. Doesn't set the process_todo |
| 852 | * flag, which means that (if it wasn't already set) the thread will go to |
| 853 | * sleep without handling this work when it calls read. |
| 854 | * |
| 855 | * Requires the proc->inner_lock to be held. |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 856 | */ |
| 857 | static void |
Martijn Coenen | ad4cc17 | 2017-11-13 09:55:21 +0100 | [diff] [blame] | 858 | binder_enqueue_deferred_thread_work_ilocked(struct binder_thread *thread, |
| 859 | struct binder_work *work) |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 860 | { |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 861 | binder_enqueue_work_ilocked(work, &thread->todo); |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * binder_enqueue_thread_work_ilocked() - Add an item to the thread work list |
| 866 | * @thread: thread to queue work to |
| 867 | * @work: struct binder_work to add to list |
| 868 | * |
| 869 | * Adds the work to the todo list of the thread, and enables processing |
| 870 | * of the todo queue. |
| 871 | * |
| 872 | * Requires the proc->inner_lock to be held. |
| 873 | */ |
| 874 | static void |
| 875 | binder_enqueue_thread_work_ilocked(struct binder_thread *thread, |
| 876 | struct binder_work *work) |
| 877 | { |
| 878 | binder_enqueue_work_ilocked(work, &thread->todo); |
Carlos Llamas | af4fc46 | 2024-01-31 21:53:46 +0000 | [diff] [blame] | 879 | |
| 880 | /* (e)poll-based threads require an explicit wakeup signal when |
| 881 | * queuing their own work; they rely on these events to consume |
| 882 | * messages without I/O block. Without it, threads risk waiting |
| 883 | * indefinitely without handling the work. |
| 884 | */ |
| 885 | if (thread->looper & BINDER_LOOPER_STATE_POLL && |
| 886 | thread->pid == current->pid && !thread->process_todo) |
| 887 | wake_up_interruptible_sync(&thread->wait); |
| 888 | |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 889 | thread->process_todo = true; |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * binder_enqueue_thread_work() - Add an item to the thread work list |
| 894 | * @thread: thread to queue work to |
| 895 | * @work: struct binder_work to add to list |
| 896 | * |
| 897 | * Adds the work to the todo list of the thread, and enables processing |
| 898 | * of the todo queue. |
| 899 | */ |
| 900 | static void |
| 901 | binder_enqueue_thread_work(struct binder_thread *thread, |
| 902 | struct binder_work *work) |
| 903 | { |
| 904 | binder_inner_proc_lock(thread->proc); |
| 905 | binder_enqueue_thread_work_ilocked(thread, work); |
| 906 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static void |
| 910 | binder_dequeue_work_ilocked(struct binder_work *work) |
| 911 | { |
| 912 | list_del_init(&work->entry); |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * binder_dequeue_work() - Removes an item from the work list |
| 917 | * @proc: binder_proc associated with list |
| 918 | * @work: struct binder_work to remove from list |
| 919 | * |
| 920 | * Removes the specified work item from whatever list it is on. |
| 921 | * Can safely be called if work is not on any list. |
| 922 | */ |
| 923 | static void |
| 924 | binder_dequeue_work(struct binder_proc *proc, struct binder_work *work) |
| 925 | { |
| 926 | binder_inner_proc_lock(proc); |
| 927 | binder_dequeue_work_ilocked(work); |
| 928 | binder_inner_proc_unlock(proc); |
| 929 | } |
| 930 | |
| 931 | static struct binder_work *binder_dequeue_work_head_ilocked( |
| 932 | struct list_head *list) |
| 933 | { |
| 934 | struct binder_work *w; |
| 935 | |
| 936 | w = list_first_entry_or_null(list, struct binder_work, entry); |
| 937 | if (w) |
| 938 | list_del_init(&w->entry); |
| 939 | return w; |
| 940 | } |
| 941 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 942 | static void |
| 943 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 944 | static void binder_free_thread(struct binder_thread *thread); |
| 945 | static void binder_free_proc(struct binder_proc *proc); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 946 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 947 | |
Sachin Kamat | efde99c | 2012-08-17 16:39:36 +0530 | [diff] [blame] | 948 | 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] | 949 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 950 | unsigned long rlim_cur; |
| 951 | unsigned long irqs; |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 952 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 953 | |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 954 | mutex_lock(&proc->files_lock); |
| 955 | if (proc->files == NULL) { |
| 956 | ret = -ESRCH; |
| 957 | goto err; |
| 958 | } |
| 959 | if (!lock_task_sighand(proc->tsk, &irqs)) { |
| 960 | ret = -EMFILE; |
| 961 | goto err; |
| 962 | } |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 963 | rlim_cur = task_rlimit(proc->tsk, RLIMIT_NOFILE); |
| 964 | unlock_task_sighand(proc->tsk, &irqs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 965 | |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 966 | ret = __alloc_fd(proc->files, 0, rlim_cur, flags); |
| 967 | err: |
| 968 | mutex_unlock(&proc->files_lock); |
| 969 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /* |
| 973 | * copied from fd_install |
| 974 | */ |
| 975 | static void task_fd_install( |
| 976 | struct binder_proc *proc, unsigned int fd, struct file *file) |
| 977 | { |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 978 | mutex_lock(&proc->files_lock); |
Al Viro | f869e8a | 2012-08-15 21:06:33 -0400 | [diff] [blame] | 979 | if (proc->files) |
| 980 | __fd_install(proc->files, fd, file); |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 981 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /* |
| 985 | * copied from sys_close |
| 986 | */ |
| 987 | static long task_close_fd(struct binder_proc *proc, unsigned int fd) |
| 988 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 989 | int retval; |
| 990 | |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 991 | mutex_lock(&proc->files_lock); |
| 992 | if (proc->files == NULL) { |
| 993 | retval = -ESRCH; |
| 994 | goto err; |
| 995 | } |
Al Viro | 483ce1d | 2012-08-19 12:04:24 -0400 | [diff] [blame] | 996 | retval = __close_fd(proc->files, fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 997 | /* can't restart close syscall because file table entry was cleared */ |
| 998 | if (unlikely(retval == -ERESTARTSYS || |
| 999 | retval == -ERESTARTNOINTR || |
| 1000 | retval == -ERESTARTNOHAND || |
| 1001 | retval == -ERESTART_RESTARTBLOCK)) |
| 1002 | retval = -EINTR; |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 1003 | err: |
| 1004 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1005 | return retval; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1006 | } |
| 1007 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1008 | static bool binder_has_work_ilocked(struct binder_thread *thread, |
| 1009 | bool do_proc_work) |
| 1010 | { |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 1011 | return thread->process_todo || |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1012 | thread->looper_need_return || |
| 1013 | (do_proc_work && |
| 1014 | !binder_worklist_empty_ilocked(&thread->proc->todo)); |
| 1015 | } |
| 1016 | |
| 1017 | static bool binder_has_work(struct binder_thread *thread, bool do_proc_work) |
| 1018 | { |
| 1019 | bool has_work; |
| 1020 | |
| 1021 | binder_inner_proc_lock(thread->proc); |
| 1022 | has_work = binder_has_work_ilocked(thread, do_proc_work); |
| 1023 | binder_inner_proc_unlock(thread->proc); |
| 1024 | |
| 1025 | return has_work; |
| 1026 | } |
| 1027 | |
| 1028 | static bool binder_available_for_proc_work_ilocked(struct binder_thread *thread) |
| 1029 | { |
| 1030 | return !thread->transaction_stack && |
| 1031 | binder_worklist_empty_ilocked(&thread->todo) && |
| 1032 | (thread->looper & (BINDER_LOOPER_STATE_ENTERED | |
| 1033 | BINDER_LOOPER_STATE_REGISTERED)); |
| 1034 | } |
| 1035 | |
| 1036 | static void binder_wakeup_poll_threads_ilocked(struct binder_proc *proc, |
| 1037 | bool sync) |
| 1038 | { |
| 1039 | struct rb_node *n; |
| 1040 | struct binder_thread *thread; |
| 1041 | |
| 1042 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 1043 | thread = rb_entry(n, struct binder_thread, rb_node); |
| 1044 | if (thread->looper & BINDER_LOOPER_STATE_POLL && |
| 1045 | binder_available_for_proc_work_ilocked(thread)) { |
| 1046 | if (sync) |
| 1047 | wake_up_interruptible_sync(&thread->wait); |
| 1048 | else |
| 1049 | wake_up_interruptible(&thread->wait); |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1054 | /** |
| 1055 | * binder_select_thread_ilocked() - selects a thread for doing proc work. |
| 1056 | * @proc: process to select a thread from |
| 1057 | * |
| 1058 | * Note that calling this function moves the thread off the waiting_threads |
| 1059 | * list, so it can only be woken up by the caller of this function, or a |
| 1060 | * signal. Therefore, callers *should* always wake up the thread this function |
| 1061 | * returns. |
| 1062 | * |
| 1063 | * Return: If there's a thread currently waiting for process work, |
| 1064 | * returns that thread. Otherwise returns NULL. |
| 1065 | */ |
| 1066 | static struct binder_thread * |
| 1067 | binder_select_thread_ilocked(struct binder_proc *proc) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1068 | { |
| 1069 | struct binder_thread *thread; |
| 1070 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1071 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1072 | thread = list_first_entry_or_null(&proc->waiting_threads, |
| 1073 | struct binder_thread, |
| 1074 | waiting_thread_node); |
| 1075 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1076 | if (thread) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1077 | list_del_init(&thread->waiting_thread_node); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1078 | |
| 1079 | return thread; |
| 1080 | } |
| 1081 | |
| 1082 | /** |
| 1083 | * binder_wakeup_thread_ilocked() - wakes up a thread for doing proc work. |
| 1084 | * @proc: process to wake up a thread in |
| 1085 | * @thread: specific thread to wake-up (may be NULL) |
| 1086 | * @sync: whether to do a synchronous wake-up |
| 1087 | * |
| 1088 | * This function wakes up a thread in the @proc process. |
| 1089 | * The caller may provide a specific thread to wake-up in |
| 1090 | * the @thread parameter. If @thread is NULL, this function |
| 1091 | * will wake up threads that have called poll(). |
| 1092 | * |
| 1093 | * Note that for this function to work as expected, callers |
| 1094 | * should first call binder_select_thread() to find a thread |
| 1095 | * to handle the work (if they don't have a thread already), |
| 1096 | * and pass the result into the @thread parameter. |
| 1097 | */ |
| 1098 | static void binder_wakeup_thread_ilocked(struct binder_proc *proc, |
| 1099 | struct binder_thread *thread, |
| 1100 | bool sync) |
| 1101 | { |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1102 | assert_spin_locked(&proc->inner_lock); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1103 | |
| 1104 | if (thread) { |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 1105 | if (sync) |
| 1106 | wake_up_interruptible_sync(&thread->wait); |
| 1107 | else |
| 1108 | wake_up_interruptible(&thread->wait); |
| 1109 | return; |
| 1110 | } |
| 1111 | |
| 1112 | /* Didn't find a thread waiting for proc work; this can happen |
| 1113 | * in two scenarios: |
| 1114 | * 1. All threads are busy handling transactions |
| 1115 | * In that case, one of those threads should call back into |
| 1116 | * the kernel driver soon and pick up this work. |
| 1117 | * 2. Threads are using the (e)poll interface, in which case |
| 1118 | * they may be blocked on the waitqueue without having been |
| 1119 | * added to waiting_threads. For this case, we just iterate |
| 1120 | * over all threads not handling transaction work, and |
| 1121 | * wake them all up. We wake all because we don't know whether |
| 1122 | * a thread that called into (e)poll is handling non-binder |
| 1123 | * work currently. |
| 1124 | */ |
| 1125 | binder_wakeup_poll_threads_ilocked(proc, sync); |
| 1126 | } |
| 1127 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1128 | static void binder_wakeup_proc_ilocked(struct binder_proc *proc) |
| 1129 | { |
| 1130 | struct binder_thread *thread = binder_select_thread_ilocked(proc); |
| 1131 | |
| 1132 | binder_wakeup_thread_ilocked(proc, thread, /* sync = */false); |
| 1133 | } |
| 1134 | |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1135 | static bool is_rt_policy(int policy) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1136 | { |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1137 | return policy == SCHED_FIFO || policy == SCHED_RR; |
| 1138 | } |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1139 | |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1140 | static bool is_fair_policy(int policy) |
| 1141 | { |
| 1142 | return policy == SCHED_NORMAL || policy == SCHED_BATCH; |
| 1143 | } |
| 1144 | |
| 1145 | static bool binder_supported_policy(int policy) |
| 1146 | { |
| 1147 | return is_fair_policy(policy) || is_rt_policy(policy); |
| 1148 | } |
| 1149 | |
| 1150 | static int to_userspace_prio(int policy, int kernel_priority) |
| 1151 | { |
| 1152 | if (is_fair_policy(policy)) |
| 1153 | return PRIO_TO_NICE(kernel_priority); |
| 1154 | else |
| 1155 | return MAX_USER_RT_PRIO - 1 - kernel_priority; |
| 1156 | } |
| 1157 | |
| 1158 | static int to_kernel_prio(int policy, int user_priority) |
| 1159 | { |
| 1160 | if (is_fair_policy(policy)) |
| 1161 | return NICE_TO_PRIO(user_priority); |
| 1162 | else |
| 1163 | return MAX_USER_RT_PRIO - 1 - user_priority; |
| 1164 | } |
| 1165 | |
Martijn Coenen | 22b061b | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1166 | static void binder_do_set_priority(struct task_struct *task, |
| 1167 | struct binder_priority desired, |
| 1168 | bool verify) |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1169 | { |
| 1170 | int priority; /* user-space prio value */ |
| 1171 | bool has_cap_nice; |
| 1172 | unsigned int policy = desired.sched_policy; |
| 1173 | |
| 1174 | if (task->policy == policy && task->normal_prio == desired.prio) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1175 | return; |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1176 | |
| 1177 | has_cap_nice = has_capability_noaudit(task, CAP_SYS_NICE); |
| 1178 | |
| 1179 | priority = to_userspace_prio(policy, desired.prio); |
| 1180 | |
Martijn Coenen | 22b061b | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1181 | if (verify && is_rt_policy(policy) && !has_cap_nice) { |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1182 | long max_rtprio = task_rlimit(task, RLIMIT_RTPRIO); |
| 1183 | |
| 1184 | if (max_rtprio == 0) { |
| 1185 | policy = SCHED_NORMAL; |
| 1186 | priority = MIN_NICE; |
| 1187 | } else if (priority > max_rtprio) { |
| 1188 | priority = max_rtprio; |
| 1189 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1190 | } |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1191 | |
Martijn Coenen | 22b061b | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1192 | if (verify && is_fair_policy(policy) && !has_cap_nice) { |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1193 | long min_nice = rlimit_to_nice(task_rlimit(task, RLIMIT_NICE)); |
| 1194 | |
| 1195 | if (min_nice > MAX_NICE) { |
| 1196 | binder_user_error("%d RLIMIT_NICE not set\n", |
| 1197 | task->pid); |
| 1198 | return; |
| 1199 | } else if (priority < min_nice) { |
| 1200 | priority = min_nice; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | if (policy != desired.sched_policy || |
| 1205 | to_kernel_prio(policy, priority) != desired.prio) |
| 1206 | binder_debug(BINDER_DEBUG_PRIORITY_CAP, |
| 1207 | "%d: priority %d not allowed, using %d instead\n", |
| 1208 | task->pid, desired.prio, |
| 1209 | to_kernel_prio(policy, priority)); |
| 1210 | |
Martijn Coenen | 67cf971 | 2017-05-08 09:33:22 -0700 | [diff] [blame] | 1211 | trace_binder_set_priority(task->tgid, task->pid, task->normal_prio, |
| 1212 | to_kernel_prio(policy, priority), |
| 1213 | desired.prio); |
| 1214 | |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 1215 | /* Set the actual priority */ |
| 1216 | if (task->policy != policy || is_rt_policy(policy)) { |
| 1217 | struct sched_param params; |
| 1218 | |
| 1219 | params.sched_priority = is_rt_policy(policy) ? priority : 0; |
| 1220 | |
| 1221 | sched_setscheduler_nocheck(task, |
| 1222 | policy | SCHED_RESET_ON_FORK, |
| 1223 | ¶ms); |
| 1224 | } |
| 1225 | if (is_fair_policy(policy)) |
| 1226 | set_user_nice(task, priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1227 | } |
| 1228 | |
Martijn Coenen | 22b061b | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 1229 | static void binder_set_priority(struct task_struct *task, |
| 1230 | struct binder_priority desired) |
| 1231 | { |
| 1232 | binder_do_set_priority(task, desired, /* verify = */ true); |
| 1233 | } |
| 1234 | |
| 1235 | static void binder_restore_priority(struct task_struct *task, |
| 1236 | struct binder_priority desired) |
| 1237 | { |
| 1238 | binder_do_set_priority(task, desired, /* verify = */ false); |
| 1239 | } |
| 1240 | |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1241 | static void binder_transaction_priority(struct task_struct *task, |
| 1242 | struct binder_transaction *t, |
Martijn Coenen | fb92c34 | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1243 | struct binder_priority node_prio, |
| 1244 | bool inherit_rt) |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1245 | { |
Ganesh Mahendran | 9b608f4 | 2017-09-27 15:12:25 +0800 | [diff] [blame] | 1246 | struct binder_priority desired_prio = t->priority; |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1247 | |
| 1248 | if (t->set_priority_called) |
| 1249 | return; |
| 1250 | |
| 1251 | t->set_priority_called = true; |
| 1252 | t->saved_priority.sched_policy = task->policy; |
| 1253 | t->saved_priority.prio = task->normal_prio; |
| 1254 | |
Martijn Coenen | fb92c34 | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1255 | if (!inherit_rt && is_rt_policy(desired_prio.sched_policy)) { |
| 1256 | desired_prio.prio = NICE_TO_PRIO(0); |
| 1257 | desired_prio.sched_policy = SCHED_NORMAL; |
Martijn Coenen | fb92c34 | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1258 | } |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 1259 | |
| 1260 | if (node_prio.prio < t->priority.prio || |
| 1261 | (node_prio.prio == t->priority.prio && |
| 1262 | node_prio.sched_policy == SCHED_FIFO)) { |
| 1263 | /* |
| 1264 | * In case the minimum priority on the node is |
| 1265 | * higher (lower value), use that priority. If |
| 1266 | * the priority is the same, but the node uses |
| 1267 | * SCHED_FIFO, prefer SCHED_FIFO, since it can |
| 1268 | * run unbounded, unlike SCHED_RR. |
| 1269 | */ |
| 1270 | desired_prio = node_prio; |
| 1271 | } |
| 1272 | |
| 1273 | binder_set_priority(task, desired_prio); |
| 1274 | } |
| 1275 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1276 | static struct binder_node *binder_get_node_ilocked(struct binder_proc *proc, |
| 1277 | binder_uintptr_t ptr) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1278 | { |
| 1279 | struct rb_node *n = proc->nodes.rb_node; |
| 1280 | struct binder_node *node; |
| 1281 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1282 | assert_spin_locked(&proc->inner_lock); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1283 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1284 | while (n) { |
| 1285 | node = rb_entry(n, struct binder_node, rb_node); |
| 1286 | |
| 1287 | if (ptr < node->ptr) |
| 1288 | n = n->rb_left; |
| 1289 | else if (ptr > node->ptr) |
| 1290 | n = n->rb_right; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1291 | else { |
| 1292 | /* |
| 1293 | * take an implicit weak reference |
| 1294 | * to ensure node stays alive until |
| 1295 | * call to binder_put_node() |
| 1296 | */ |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1297 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1298 | return node; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1299 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1300 | } |
| 1301 | return NULL; |
| 1302 | } |
| 1303 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1304 | static struct binder_node *binder_get_node(struct binder_proc *proc, |
| 1305 | binder_uintptr_t ptr) |
| 1306 | { |
| 1307 | struct binder_node *node; |
| 1308 | |
| 1309 | binder_inner_proc_lock(proc); |
| 1310 | node = binder_get_node_ilocked(proc, ptr); |
| 1311 | binder_inner_proc_unlock(proc); |
| 1312 | return node; |
| 1313 | } |
| 1314 | |
| 1315 | static struct binder_node *binder_init_node_ilocked( |
| 1316 | struct binder_proc *proc, |
| 1317 | struct binder_node *new_node, |
| 1318 | struct flat_binder_object *fp) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1319 | { |
| 1320 | struct rb_node **p = &proc->nodes.rb_node; |
| 1321 | struct rb_node *parent = NULL; |
| 1322 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1323 | binder_uintptr_t ptr = fp ? fp->binder : 0; |
| 1324 | binder_uintptr_t cookie = fp ? fp->cookie : 0; |
| 1325 | __u32 flags = fp ? fp->flags : 0; |
Martijn Coenen | 69308b3 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1326 | s8 priority; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1327 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1328 | assert_spin_locked(&proc->inner_lock); |
| 1329 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1330 | while (*p) { |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1331 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1332 | parent = *p; |
| 1333 | node = rb_entry(parent, struct binder_node, rb_node); |
| 1334 | |
| 1335 | if (ptr < node->ptr) |
| 1336 | p = &(*p)->rb_left; |
| 1337 | else if (ptr > node->ptr) |
| 1338 | p = &(*p)->rb_right; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1339 | else { |
| 1340 | /* |
| 1341 | * A matching node is already in |
| 1342 | * the rb tree. Abandon the init |
| 1343 | * and return it. |
| 1344 | */ |
| 1345 | binder_inc_node_tmpref_ilocked(node); |
| 1346 | return node; |
| 1347 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1348 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1349 | node = new_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1350 | binder_stats_created(BINDER_STAT_NODE); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1351 | node->tmp_refs++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1352 | rb_link_node(&node->rb_node, parent, p); |
| 1353 | rb_insert_color(&node->rb_node, &proc->nodes); |
Todd Kjos | 656a800 | 2017-06-29 12:01:45 -0700 | [diff] [blame] | 1354 | node->debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1355 | node->proc = proc; |
| 1356 | node->ptr = ptr; |
| 1357 | node->cookie = cookie; |
| 1358 | node->work.type = BINDER_WORK_NODE; |
Martijn Coenen | 69308b3 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1359 | priority = flags & FLAT_BINDER_FLAG_PRIORITY_MASK; |
Ganesh Mahendran | ff26505 | 2017-09-26 17:56:25 +0800 | [diff] [blame] | 1360 | node->sched_policy = (flags & FLAT_BINDER_FLAG_SCHED_POLICY_MASK) >> |
Martijn Coenen | 69308b3 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 1361 | FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT; |
| 1362 | node->min_priority = to_kernel_prio(node->sched_policy, priority); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1363 | node->accept_fds = !!(flags & FLAT_BINDER_FLAG_ACCEPTS_FDS); |
Martijn Coenen | fb92c34 | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 1364 | node->inherit_rt = !!(flags & FLAT_BINDER_FLAG_INHERIT_RT); |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 1365 | node->txn_security_ctx = !!(flags & FLAT_BINDER_FLAG_TXN_SECURITY_CTX); |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 1366 | spin_lock_init(&node->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1367 | INIT_LIST_HEAD(&node->work.entry); |
| 1368 | INIT_LIST_HEAD(&node->async_todo); |
| 1369 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1370 | "%d:%d node %d u%016llx c%016llx created\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1371 | proc->pid, current->pid, node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 1372 | (u64)node->ptr, (u64)node->cookie); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 1373 | |
| 1374 | return node; |
| 1375 | } |
| 1376 | |
| 1377 | static struct binder_node *binder_new_node(struct binder_proc *proc, |
| 1378 | struct flat_binder_object *fp) |
| 1379 | { |
| 1380 | struct binder_node *node; |
| 1381 | struct binder_node *new_node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 1382 | |
| 1383 | if (!new_node) |
| 1384 | return NULL; |
| 1385 | binder_inner_proc_lock(proc); |
| 1386 | node = binder_init_node_ilocked(proc, new_node, fp); |
| 1387 | binder_inner_proc_unlock(proc); |
| 1388 | if (node != new_node) |
| 1389 | /* |
| 1390 | * The node was already added by another thread |
| 1391 | */ |
| 1392 | kfree(new_node); |
| 1393 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1394 | return node; |
| 1395 | } |
| 1396 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1397 | static void binder_free_node(struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1398 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1399 | kfree(node); |
| 1400 | binder_stats_deleted(BINDER_STAT_NODE); |
| 1401 | } |
| 1402 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1403 | static int binder_inc_node_nilocked(struct binder_node *node, int strong, |
| 1404 | int internal, |
| 1405 | struct list_head *target_list) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1406 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1407 | struct binder_proc *proc = node->proc; |
| 1408 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1409 | assert_spin_locked(&node->lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1410 | if (proc) |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1411 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1412 | if (strong) { |
| 1413 | if (internal) { |
| 1414 | if (target_list == NULL && |
| 1415 | node->internal_strong_refs == 0 && |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 1416 | !(node->proc && |
| 1417 | node == node->proc->context->binder_context_mgr_node && |
| 1418 | node->has_strong_ref)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1419 | pr_err("invalid inc strong node for %d\n", |
| 1420 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1421 | return -EINVAL; |
| 1422 | } |
| 1423 | node->internal_strong_refs++; |
| 1424 | } else |
| 1425 | node->local_strong_refs++; |
| 1426 | if (!node->has_strong_ref && target_list) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1427 | binder_dequeue_work_ilocked(&node->work); |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 1428 | /* |
| 1429 | * Note: this function is the only place where we queue |
| 1430 | * directly to a thread->todo without using the |
| 1431 | * corresponding binder_enqueue_thread_work() helper |
| 1432 | * functions; in this case it's ok to not set the |
| 1433 | * process_todo flag, since we know this node work will |
| 1434 | * always be followed by other work that starts queue |
| 1435 | * processing: in case of synchronous transactions, a |
| 1436 | * BR_REPLY or BR_ERROR; in case of oneway |
| 1437 | * transactions, a BR_TRANSACTION_COMPLETE. |
| 1438 | */ |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1439 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1440 | } |
| 1441 | } else { |
| 1442 | if (!internal) |
| 1443 | node->local_weak_refs++; |
| 1444 | if (!node->has_weak_ref && list_empty(&node->work.entry)) { |
| 1445 | if (target_list == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1446 | pr_err("invalid inc weak node for %d\n", |
| 1447 | node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1448 | return -EINVAL; |
| 1449 | } |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 1450 | /* |
| 1451 | * See comment above |
| 1452 | */ |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1453 | binder_enqueue_work_ilocked(&node->work, target_list); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1454 | } |
| 1455 | } |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1459 | static int binder_inc_node(struct binder_node *node, int strong, int internal, |
| 1460 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1461 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1462 | int ret; |
| 1463 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1464 | binder_node_inner_lock(node); |
| 1465 | ret = binder_inc_node_nilocked(node, strong, internal, target_list); |
| 1466 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1467 | |
| 1468 | return ret; |
| 1469 | } |
| 1470 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1471 | static bool binder_dec_node_nilocked(struct binder_node *node, |
| 1472 | int strong, int internal) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1473 | { |
| 1474 | struct binder_proc *proc = node->proc; |
| 1475 | |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1476 | assert_spin_locked(&node->lock); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1477 | if (proc) |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 1478 | assert_spin_locked(&proc->inner_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1479 | if (strong) { |
| 1480 | if (internal) |
| 1481 | node->internal_strong_refs--; |
| 1482 | else |
| 1483 | node->local_strong_refs--; |
| 1484 | if (node->local_strong_refs || node->internal_strong_refs) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1485 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1486 | } else { |
| 1487 | if (!internal) |
| 1488 | node->local_weak_refs--; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1489 | if (node->local_weak_refs || node->tmp_refs || |
| 1490 | !hlist_empty(&node->refs)) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1491 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1492 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1493 | |
| 1494 | if (proc && (node->has_strong_ref || node->has_weak_ref)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1495 | if (list_empty(&node->work.entry)) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1496 | binder_enqueue_work_ilocked(&node->work, &proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 1497 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1498 | } |
| 1499 | } else { |
| 1500 | if (hlist_empty(&node->refs) && !node->local_strong_refs && |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1501 | !node->local_weak_refs && !node->tmp_refs) { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1502 | if (proc) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1503 | binder_dequeue_work_ilocked(&node->work); |
| 1504 | rb_erase(&node->rb_node, &proc->nodes); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1505 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1506 | "refless node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1507 | node->debug_id); |
| 1508 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1509 | BUG_ON(!list_empty(&node->work.entry)); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 1510 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1511 | /* |
| 1512 | * tmp_refs could have changed so |
| 1513 | * check it again |
| 1514 | */ |
| 1515 | if (node->tmp_refs) { |
| 1516 | spin_unlock(&binder_dead_nodes_lock); |
| 1517 | return false; |
| 1518 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1519 | hlist_del(&node->dead_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 1520 | spin_unlock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1521 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1522 | "dead node %d deleted\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1523 | node->debug_id); |
| 1524 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1525 | return true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1526 | } |
| 1527 | } |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1528 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1529 | } |
| 1530 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1531 | static void binder_dec_node(struct binder_node *node, int strong, int internal) |
| 1532 | { |
| 1533 | bool free_node; |
| 1534 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1535 | binder_node_inner_lock(node); |
| 1536 | free_node = binder_dec_node_nilocked(node, strong, internal); |
| 1537 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1538 | if (free_node) |
| 1539 | binder_free_node(node); |
| 1540 | } |
| 1541 | |
| 1542 | static void binder_inc_node_tmpref_ilocked(struct binder_node *node) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1543 | { |
| 1544 | /* |
| 1545 | * No call to binder_inc_node() is needed since we |
| 1546 | * don't need to inform userspace of any changes to |
| 1547 | * tmp_refs |
| 1548 | */ |
| 1549 | node->tmp_refs++; |
| 1550 | } |
| 1551 | |
| 1552 | /** |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1553 | * binder_inc_node_tmpref() - take a temporary reference on node |
| 1554 | * @node: node to reference |
| 1555 | * |
| 1556 | * Take reference on node to prevent the node from being freed |
| 1557 | * while referenced only by a local variable. The inner lock is |
| 1558 | * needed to serialize with the node work on the queue (which |
| 1559 | * isn't needed after the node is dead). If the node is dead |
| 1560 | * (node->proc is NULL), use binder_dead_nodes_lock to protect |
| 1561 | * node->tmp_refs against dead-node-only cases where the node |
| 1562 | * lock cannot be acquired (eg traversing the dead node list to |
| 1563 | * print nodes) |
| 1564 | */ |
| 1565 | static void binder_inc_node_tmpref(struct binder_node *node) |
| 1566 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1567 | binder_node_lock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1568 | if (node->proc) |
| 1569 | binder_inner_proc_lock(node->proc); |
| 1570 | else |
| 1571 | spin_lock(&binder_dead_nodes_lock); |
| 1572 | binder_inc_node_tmpref_ilocked(node); |
| 1573 | if (node->proc) |
| 1574 | binder_inner_proc_unlock(node->proc); |
| 1575 | else |
| 1576 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1577 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | /** |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1581 | * binder_dec_node_tmpref() - remove a temporary reference on node |
| 1582 | * @node: node to reference |
| 1583 | * |
| 1584 | * Release temporary reference on node taken via binder_inc_node_tmpref() |
| 1585 | */ |
| 1586 | static void binder_dec_node_tmpref(struct binder_node *node) |
| 1587 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1588 | bool free_node; |
| 1589 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1590 | binder_node_inner_lock(node); |
| 1591 | if (!node->proc) |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1592 | spin_lock(&binder_dead_nodes_lock); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1593 | node->tmp_refs--; |
| 1594 | BUG_ON(node->tmp_refs < 0); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1595 | if (!node->proc) |
| 1596 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1597 | /* |
| 1598 | * Call binder_dec_node() to check if all refcounts are 0 |
| 1599 | * and cleanup is needed. Calling with strong=0 and internal=1 |
| 1600 | * causes no actual reference to be released in binder_dec_node(). |
| 1601 | * If that changes, a change is needed here too. |
| 1602 | */ |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1603 | free_node = binder_dec_node_nilocked(node, 0, 1); |
| 1604 | binder_node_inner_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1605 | if (free_node) |
| 1606 | binder_free_node(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | static void binder_put_node(struct binder_node *node) |
| 1610 | { |
| 1611 | binder_dec_node_tmpref(node); |
| 1612 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1613 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1614 | static struct binder_ref *binder_get_ref_olocked(struct binder_proc *proc, |
| 1615 | u32 desc, bool need_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1616 | { |
| 1617 | struct rb_node *n = proc->refs_by_desc.rb_node; |
| 1618 | struct binder_ref *ref; |
| 1619 | |
| 1620 | while (n) { |
| 1621 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
| 1622 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1623 | if (desc < ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1624 | n = n->rb_left; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1625 | } else if (desc > ref->data.desc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1626 | n = n->rb_right; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1627 | } else if (need_strong_ref && !ref->data.strong) { |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1628 | binder_user_error("tried to use weak ref as strong ref\n"); |
| 1629 | return NULL; |
| 1630 | } else { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1631 | return ref; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 1632 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1633 | } |
| 1634 | return NULL; |
| 1635 | } |
| 1636 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1637 | /** |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1638 | * 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] | 1639 | * @proc: binder_proc that owns the ref |
| 1640 | * @node: binder_node of target |
| 1641 | * @new_ref: newly allocated binder_ref to be initialized or %NULL |
| 1642 | * |
| 1643 | * Look up the ref for the given node and return it if it exists |
| 1644 | * |
| 1645 | * If it doesn't exist and the caller provides a newly allocated |
| 1646 | * ref, initialize the fields of the newly allocated ref and insert |
| 1647 | * into the given proc rb_trees and node refs list. |
| 1648 | * |
| 1649 | * Return: the ref for node. It is possible that another thread |
| 1650 | * allocated/initialized the ref first in which case the |
| 1651 | * returned ref would be different than the passed-in |
| 1652 | * new_ref. new_ref must be kfree'd by the caller in |
| 1653 | * this case. |
| 1654 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1655 | static struct binder_ref *binder_get_ref_for_node_olocked( |
| 1656 | struct binder_proc *proc, |
| 1657 | struct binder_node *node, |
| 1658 | struct binder_ref *new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1659 | { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1660 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1661 | struct rb_node **p = &proc->refs_by_node.rb_node; |
| 1662 | struct rb_node *parent = NULL; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1663 | struct binder_ref *ref; |
| 1664 | struct rb_node *n; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1665 | |
| 1666 | while (*p) { |
| 1667 | parent = *p; |
| 1668 | ref = rb_entry(parent, struct binder_ref, rb_node_node); |
| 1669 | |
| 1670 | if (node < ref->node) |
| 1671 | p = &(*p)->rb_left; |
| 1672 | else if (node > ref->node) |
| 1673 | p = &(*p)->rb_right; |
| 1674 | else |
| 1675 | return ref; |
| 1676 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1677 | if (!new_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1678 | return NULL; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1679 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1680 | binder_stats_created(BINDER_STAT_REF); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1681 | new_ref->data.debug_id = atomic_inc_return(&binder_last_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1682 | new_ref->proc = proc; |
| 1683 | new_ref->node = node; |
| 1684 | rb_link_node(&new_ref->rb_node_node, parent, p); |
| 1685 | rb_insert_color(&new_ref->rb_node_node, &proc->refs_by_node); |
| 1686 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1687 | 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] | 1688 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 1689 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1690 | if (ref->data.desc > new_ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1691 | break; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1692 | new_ref->data.desc = ref->data.desc + 1; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | p = &proc->refs_by_desc.rb_node; |
| 1696 | while (*p) { |
| 1697 | parent = *p; |
| 1698 | ref = rb_entry(parent, struct binder_ref, rb_node_desc); |
| 1699 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1700 | if (new_ref->data.desc < ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1701 | p = &(*p)->rb_left; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1702 | else if (new_ref->data.desc > ref->data.desc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1703 | p = &(*p)->rb_right; |
| 1704 | else |
| 1705 | BUG(); |
| 1706 | } |
| 1707 | rb_link_node(&new_ref->rb_node_desc, parent, p); |
| 1708 | rb_insert_color(&new_ref->rb_node_desc, &proc->refs_by_desc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1709 | |
| 1710 | binder_node_lock(node); |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1711 | hlist_add_head(&new_ref->node_entry, &node->refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1712 | |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1713 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 1714 | "%d new ref %d desc %d for node %d\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1715 | proc->pid, new_ref->data.debug_id, new_ref->data.desc, |
Todd Kjos | e4cffcf | 2017-06-29 12:01:50 -0700 | [diff] [blame] | 1716 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1717 | binder_node_unlock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1718 | return new_ref; |
| 1719 | } |
| 1720 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1721 | static void binder_cleanup_ref_olocked(struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1722 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1723 | bool delete_node = false; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1724 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1725 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1726 | "%d delete ref %d desc %d for node %d\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1727 | ref->proc->pid, ref->data.debug_id, ref->data.desc, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1728 | ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1729 | |
| 1730 | rb_erase(&ref->rb_node_desc, &ref->proc->refs_by_desc); |
| 1731 | rb_erase(&ref->rb_node_node, &ref->proc->refs_by_node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1732 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1733 | binder_node_inner_lock(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1734 | if (ref->data.strong) |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1735 | binder_dec_node_nilocked(ref->node, 1, 1); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1736 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1737 | hlist_del(&ref->node_entry); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 1738 | delete_node = binder_dec_node_nilocked(ref->node, 0, 1); |
| 1739 | binder_node_inner_unlock(ref->node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1740 | /* |
| 1741 | * Clear ref->node unless we want the caller to free the node |
| 1742 | */ |
| 1743 | if (!delete_node) { |
| 1744 | /* |
| 1745 | * The caller uses ref->node to determine |
| 1746 | * whether the node needs to be freed. Clear |
| 1747 | * it since the node is still alive. |
| 1748 | */ |
| 1749 | ref->node = NULL; |
| 1750 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1751 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1752 | if (ref->death) { |
| 1753 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1754 | "%d delete ref %d desc %d has death notification\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1755 | ref->proc->pid, ref->data.debug_id, |
| 1756 | ref->data.desc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 1757 | binder_dequeue_work(ref->proc, &ref->death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1758 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 1759 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1760 | binder_stats_deleted(BINDER_STAT_REF); |
| 1761 | } |
| 1762 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1763 | /** |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1764 | * binder_inc_ref_olocked() - increment the ref for given handle |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1765 | * @ref: ref to be incremented |
| 1766 | * @strong: if true, strong increment, else weak |
| 1767 | * @target_list: list to queue node work on |
| 1768 | * |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1769 | * Increment the ref. @ref->proc->outer_lock must be held on entry |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1770 | * |
| 1771 | * Return: 0, if successful, else errno |
| 1772 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1773 | static int binder_inc_ref_olocked(struct binder_ref *ref, int strong, |
| 1774 | struct list_head *target_list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1775 | { |
| 1776 | int ret; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1777 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1778 | if (strong) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1779 | if (ref->data.strong == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1780 | ret = binder_inc_node(ref->node, 1, 1, target_list); |
| 1781 | if (ret) |
| 1782 | return ret; |
| 1783 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1784 | ref->data.strong++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1785 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1786 | if (ref->data.weak == 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1787 | ret = binder_inc_node(ref->node, 0, 1, target_list); |
| 1788 | if (ret) |
| 1789 | return ret; |
| 1790 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1791 | ref->data.weak++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1792 | } |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1796 | /** |
Randy Dunlap | 6388607 | 2023-01-17 10:37:45 -0800 | [diff] [blame] | 1797 | * binder_dec_ref_olocked() - dec the ref for given handle |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1798 | * @ref: ref to be decremented |
| 1799 | * @strong: if true, strong decrement, else weak |
| 1800 | * |
| 1801 | * Decrement the ref. |
| 1802 | * |
Randy Dunlap | 6388607 | 2023-01-17 10:37:45 -0800 | [diff] [blame] | 1803 | * Return: %true if ref is cleaned up and ready to be freed. |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1804 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1805 | 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] | 1806 | { |
| 1807 | if (strong) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1808 | if (ref->data.strong == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1809 | 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] | 1810 | ref->proc->pid, ref->data.debug_id, |
| 1811 | ref->data.desc, ref->data.strong, |
| 1812 | ref->data.weak); |
| 1813 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1814 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1815 | ref->data.strong--; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1816 | if (ref->data.strong == 0) |
| 1817 | binder_dec_node(ref->node, strong, 1); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1818 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1819 | if (ref->data.weak == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 1820 | 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] | 1821 | ref->proc->pid, ref->data.debug_id, |
| 1822 | ref->data.desc, ref->data.strong, |
| 1823 | ref->data.weak); |
| 1824 | return false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1825 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1826 | ref->data.weak--; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1827 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1828 | if (ref->data.strong == 0 && ref->data.weak == 0) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1829 | binder_cleanup_ref_olocked(ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1830 | return true; |
| 1831 | } |
| 1832 | return false; |
| 1833 | } |
| 1834 | |
| 1835 | /** |
| 1836 | * binder_get_node_from_ref() - get the node from the given proc/desc |
| 1837 | * @proc: proc containing the ref |
| 1838 | * @desc: the handle associated with the ref |
| 1839 | * @need_strong_ref: if true, only return node if ref is strong |
| 1840 | * @rdata: the id/refcount data for the ref |
| 1841 | * |
| 1842 | * Given a proc and ref handle, return the associated binder_node |
| 1843 | * |
| 1844 | * Return: a binder_node or NULL if not found or not strong when strong required |
| 1845 | */ |
| 1846 | static struct binder_node *binder_get_node_from_ref( |
| 1847 | struct binder_proc *proc, |
| 1848 | u32 desc, bool need_strong_ref, |
| 1849 | struct binder_ref_data *rdata) |
| 1850 | { |
| 1851 | struct binder_node *node; |
| 1852 | struct binder_ref *ref; |
| 1853 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1854 | binder_proc_lock(proc); |
| 1855 | ref = binder_get_ref_olocked(proc, desc, need_strong_ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1856 | if (!ref) |
| 1857 | goto err_no_ref; |
| 1858 | node = ref->node; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 1859 | /* |
| 1860 | * Take an implicit reference on the node to ensure |
| 1861 | * it stays alive until the call to binder_put_node() |
| 1862 | */ |
| 1863 | binder_inc_node_tmpref(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1864 | if (rdata) |
| 1865 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1866 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1867 | |
| 1868 | return node; |
| 1869 | |
| 1870 | err_no_ref: |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1871 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1872 | return NULL; |
| 1873 | } |
| 1874 | |
| 1875 | /** |
| 1876 | * binder_free_ref() - free the binder_ref |
| 1877 | * @ref: ref to free |
| 1878 | * |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1879 | * Free the binder_ref. Free the binder_node indicated by ref->node |
| 1880 | * (if non-NULL) and the binder_ref_death indicated by ref->death. |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1881 | */ |
| 1882 | static void binder_free_ref(struct binder_ref *ref) |
| 1883 | { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 1884 | if (ref->node) |
| 1885 | binder_free_node(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1886 | kfree(ref->death); |
| 1887 | kfree(ref); |
| 1888 | } |
| 1889 | |
| 1890 | /** |
| 1891 | * binder_update_ref_for_handle() - inc/dec the ref for given handle |
| 1892 | * @proc: proc containing the ref |
| 1893 | * @desc: the handle associated with the ref |
| 1894 | * @increment: true=inc reference, false=dec reference |
| 1895 | * @strong: true=strong reference, false=weak reference |
| 1896 | * @rdata: the id/refcount data for the ref |
| 1897 | * |
| 1898 | * Given a proc and ref handle, increment or decrement the ref |
| 1899 | * according to "increment" arg. |
| 1900 | * |
| 1901 | * Return: 0 if successful, else errno |
| 1902 | */ |
| 1903 | static int binder_update_ref_for_handle(struct binder_proc *proc, |
| 1904 | uint32_t desc, bool increment, bool strong, |
| 1905 | struct binder_ref_data *rdata) |
| 1906 | { |
| 1907 | int ret = 0; |
| 1908 | struct binder_ref *ref; |
| 1909 | bool delete_ref = false; |
| 1910 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1911 | binder_proc_lock(proc); |
| 1912 | ref = binder_get_ref_olocked(proc, desc, strong); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1913 | if (!ref) { |
| 1914 | ret = -EINVAL; |
| 1915 | goto err_no_ref; |
| 1916 | } |
| 1917 | if (increment) |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1918 | ret = binder_inc_ref_olocked(ref, strong, NULL); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1919 | else |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1920 | delete_ref = binder_dec_ref_olocked(ref, strong); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1921 | |
| 1922 | if (rdata) |
| 1923 | *rdata = ref->data; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1924 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1925 | |
| 1926 | if (delete_ref) |
| 1927 | binder_free_ref(ref); |
| 1928 | return ret; |
| 1929 | |
| 1930 | err_no_ref: |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1931 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1932 | return ret; |
| 1933 | } |
| 1934 | |
| 1935 | /** |
| 1936 | * binder_dec_ref_for_handle() - dec the ref for given handle |
| 1937 | * @proc: proc containing the ref |
| 1938 | * @desc: the handle associated with the ref |
| 1939 | * @strong: true=strong reference, false=weak reference |
| 1940 | * @rdata: the id/refcount data for the ref |
| 1941 | * |
| 1942 | * Just calls binder_update_ref_for_handle() to decrement the ref. |
| 1943 | * |
| 1944 | * Return: 0 if successful, else errno |
| 1945 | */ |
| 1946 | static int binder_dec_ref_for_handle(struct binder_proc *proc, |
| 1947 | uint32_t desc, bool strong, struct binder_ref_data *rdata) |
| 1948 | { |
| 1949 | return binder_update_ref_for_handle(proc, desc, false, strong, rdata); |
| 1950 | } |
| 1951 | |
| 1952 | |
| 1953 | /** |
| 1954 | * binder_inc_ref_for_node() - increment the ref for given proc/node |
| 1955 | * @proc: proc containing the ref |
| 1956 | * @node: target node |
| 1957 | * @strong: true=strong reference, false=weak reference |
| 1958 | * @target_list: worklist to use if node is incremented |
| 1959 | * @rdata: the id/refcount data for the ref |
| 1960 | * |
| 1961 | * Given a proc and node, increment the ref. Create the ref if it |
| 1962 | * doesn't already exist |
| 1963 | * |
| 1964 | * Return: 0 if successful, else errno |
| 1965 | */ |
| 1966 | static int binder_inc_ref_for_node(struct binder_proc *proc, |
| 1967 | struct binder_node *node, |
| 1968 | bool strong, |
| 1969 | struct list_head *target_list, |
| 1970 | struct binder_ref_data *rdata) |
| 1971 | { |
| 1972 | struct binder_ref *ref; |
| 1973 | struct binder_ref *new_ref = NULL; |
| 1974 | int ret = 0; |
| 1975 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1976 | binder_proc_lock(proc); |
| 1977 | ref = binder_get_ref_for_node_olocked(proc, node, NULL); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1978 | if (!ref) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1979 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1980 | new_ref = kzalloc(sizeof(*ref), GFP_KERNEL); |
| 1981 | if (!new_ref) |
| 1982 | return -ENOMEM; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1983 | binder_proc_lock(proc); |
| 1984 | ref = binder_get_ref_for_node_olocked(proc, node, new_ref); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1985 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 1986 | ret = binder_inc_ref_olocked(ref, strong, target_list); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 1987 | *rdata = ref->data; |
Carlos Llamas | 7c57e75 | 2022-08-01 18:25:11 +0000 | [diff] [blame] | 1988 | if (ret && ref == new_ref) { |
| 1989 | /* |
| 1990 | * Cleanup the failed reference here as the target |
| 1991 | * could now be dead and have already released its |
| 1992 | * references by now. Calling on the new reference |
| 1993 | * with strong=0 and a tmp_refs will not decrement |
| 1994 | * the node. The new_ref gets kfree'd below. |
| 1995 | */ |
| 1996 | binder_cleanup_ref_olocked(new_ref); |
| 1997 | ref = NULL; |
| 1998 | } |
| 1999 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 2000 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2001 | if (new_ref && ref != new_ref) |
| 2002 | /* |
| 2003 | * Another thread created the ref first so |
| 2004 | * free the one we allocated |
| 2005 | */ |
| 2006 | kfree(new_ref); |
| 2007 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2008 | } |
| 2009 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2010 | static void binder_pop_transaction_ilocked(struct binder_thread *target_thread, |
| 2011 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2012 | { |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 2013 | BUG_ON(!target_thread); |
Martijn Coenen | 858b271 | 2017-08-31 10:04:26 +0200 | [diff] [blame] | 2014 | assert_spin_locked(&target_thread->proc->inner_lock); |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 2015 | BUG_ON(target_thread->transaction_stack != t); |
| 2016 | BUG_ON(target_thread->transaction_stack->from != target_thread); |
| 2017 | target_thread->transaction_stack = |
| 2018 | target_thread->transaction_stack->from_parent; |
| 2019 | t->from = NULL; |
| 2020 | } |
| 2021 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2022 | /** |
| 2023 | * binder_thread_dec_tmpref() - decrement thread->tmp_ref |
| 2024 | * @thread: thread to decrement |
| 2025 | * |
| 2026 | * A thread needs to be kept alive while being used to create or |
| 2027 | * handle a transaction. binder_get_txn_from() is used to safely |
| 2028 | * extract t->from from a binder_transaction and keep the thread |
| 2029 | * indicated by t->from from being freed. When done with that |
| 2030 | * binder_thread, this function is called to decrement the |
| 2031 | * tmp_ref and free if appropriate (thread has been released |
| 2032 | * and no transaction being processed by the driver) |
| 2033 | */ |
| 2034 | static void binder_thread_dec_tmpref(struct binder_thread *thread) |
| 2035 | { |
| 2036 | /* |
| 2037 | * atomic is used to protect the counter value while |
| 2038 | * it cannot reach zero or thread->is_dead is false |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2039 | */ |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 2040 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2041 | atomic_dec(&thread->tmp_ref); |
| 2042 | if (thread->is_dead && !atomic_read(&thread->tmp_ref)) { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 2043 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2044 | binder_free_thread(thread); |
| 2045 | return; |
| 2046 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 2047 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | /** |
| 2051 | * binder_proc_dec_tmpref() - decrement proc->tmp_ref |
| 2052 | * @proc: proc to decrement |
| 2053 | * |
| 2054 | * A binder_proc needs to be kept alive while being used to create or |
| 2055 | * handle a transaction. proc->tmp_ref is incremented when |
| 2056 | * creating a new transaction or the binder_proc is currently in-use |
| 2057 | * by threads that are being released. When done with the binder_proc, |
| 2058 | * this function is called to decrement the counter and free the |
| 2059 | * proc if appropriate (proc has been released, all threads have |
| 2060 | * been released and not currenly in-use to process a transaction). |
| 2061 | */ |
| 2062 | static void binder_proc_dec_tmpref(struct binder_proc *proc) |
| 2063 | { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 2064 | binder_inner_proc_lock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2065 | proc->tmp_ref--; |
| 2066 | if (proc->is_dead && RB_EMPTY_ROOT(&proc->threads) && |
| 2067 | !proc->tmp_ref) { |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 2068 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2069 | binder_free_proc(proc); |
| 2070 | return; |
| 2071 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 2072 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | /** |
| 2076 | * binder_get_txn_from() - safely extract the "from" thread in transaction |
| 2077 | * @t: binder transaction for t->from |
| 2078 | * |
| 2079 | * Atomically return the "from" thread and increment the tmp_ref |
| 2080 | * count for the thread to ensure it stays alive until |
| 2081 | * binder_thread_dec_tmpref() is called. |
| 2082 | * |
| 2083 | * Return: the value of t->from |
| 2084 | */ |
| 2085 | static struct binder_thread *binder_get_txn_from( |
| 2086 | struct binder_transaction *t) |
| 2087 | { |
| 2088 | struct binder_thread *from; |
| 2089 | |
| 2090 | spin_lock(&t->lock); |
| 2091 | from = t->from; |
| 2092 | if (from) |
| 2093 | atomic_inc(&from->tmp_ref); |
| 2094 | spin_unlock(&t->lock); |
| 2095 | return from; |
| 2096 | } |
| 2097 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2098 | /** |
| 2099 | * binder_get_txn_from_and_acq_inner() - get t->from and acquire inner lock |
| 2100 | * @t: binder transaction for t->from |
| 2101 | * |
| 2102 | * Same as binder_get_txn_from() except it also acquires the proc->inner_lock |
| 2103 | * to guarantee that the thread cannot be released while operating on it. |
| 2104 | * The caller must call binder_inner_proc_unlock() to release the inner lock |
| 2105 | * as well as call binder_dec_thread_txn() to release the reference. |
| 2106 | * |
| 2107 | * Return: the value of t->from |
| 2108 | */ |
| 2109 | static struct binder_thread *binder_get_txn_from_and_acq_inner( |
| 2110 | struct binder_transaction *t) |
| 2111 | { |
| 2112 | struct binder_thread *from; |
| 2113 | |
| 2114 | from = binder_get_txn_from(t); |
| 2115 | if (!from) |
| 2116 | return NULL; |
| 2117 | binder_inner_proc_lock(from->proc); |
| 2118 | if (t->from) { |
| 2119 | BUG_ON(from != t->from); |
| 2120 | return from; |
| 2121 | } |
| 2122 | binder_inner_proc_unlock(from->proc); |
| 2123 | binder_thread_dec_tmpref(from); |
| 2124 | return NULL; |
| 2125 | } |
| 2126 | |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 2127 | static void binder_free_transaction(struct binder_transaction *t) |
| 2128 | { |
Todd Kjos | a4a3c07 | 2019-06-12 13:29:27 -0700 | [diff] [blame] | 2129 | struct binder_proc *target_proc = t->to_proc; |
| 2130 | |
| 2131 | if (target_proc) { |
| 2132 | binder_inner_proc_lock(target_proc); |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 2133 | target_proc->outstanding_txns--; |
| 2134 | if (target_proc->outstanding_txns < 0) |
| 2135 | pr_warn("%s: Unexpected outstanding_txns %d\n", |
| 2136 | __func__, target_proc->outstanding_txns); |
| 2137 | if (!target_proc->outstanding_txns && target_proc->is_frozen) |
| 2138 | wake_up_interruptible_all(&target_proc->freeze_wait); |
Todd Kjos | a4a3c07 | 2019-06-12 13:29:27 -0700 | [diff] [blame] | 2139 | if (t->buffer) |
| 2140 | t->buffer->transaction = NULL; |
| 2141 | binder_inner_proc_unlock(target_proc); |
| 2142 | } |
| 2143 | /* |
| 2144 | * If the transaction has no target_proc, then |
| 2145 | * t->buffer->transaction has already been cleared. |
| 2146 | */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2147 | kfree(t); |
| 2148 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 2149 | } |
| 2150 | |
| 2151 | static void binder_send_failed_reply(struct binder_transaction *t, |
| 2152 | uint32_t error_code) |
| 2153 | { |
| 2154 | struct binder_thread *target_thread; |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2155 | struct binder_transaction *next; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2156 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2157 | BUG_ON(t->flags & TF_ONE_WAY); |
| 2158 | while (1) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2159 | target_thread = binder_get_txn_from_and_acq_inner(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2160 | if (target_thread) { |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 2161 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2162 | "send failed reply for transaction %d to %d:%d\n", |
| 2163 | t->debug_id, |
| 2164 | target_thread->proc->pid, |
| 2165 | target_thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2166 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2167 | binder_pop_transaction_ilocked(target_thread, t); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 2168 | if (target_thread->reply_error.cmd == BR_OK) { |
| 2169 | target_thread->reply_error.cmd = error_code; |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2170 | binder_enqueue_thread_work_ilocked( |
| 2171 | target_thread, |
| 2172 | &target_thread->reply_error.work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2173 | wake_up_interruptible(&target_thread->wait); |
| 2174 | } else { |
Todd Kjos | 129926c | 2018-02-07 12:38:47 -0800 | [diff] [blame] | 2175 | /* |
| 2176 | * Cannot get here for normal operation, but |
| 2177 | * we can if multiple synchronous transactions |
| 2178 | * are sent without blocking for responses. |
| 2179 | * Just ignore the 2nd error in this case. |
| 2180 | */ |
| 2181 | pr_warn("Unexpected reply error: %u\n", |
| 2182 | target_thread->reply_error.cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2183 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 2184 | binder_inner_proc_unlock(target_thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 2185 | binder_thread_dec_tmpref(target_thread); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 2186 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2187 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2188 | } |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2189 | next = t->from_parent; |
| 2190 | |
| 2191 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
| 2192 | "send failed reply for transaction %d, target dead\n", |
| 2193 | t->debug_id); |
| 2194 | |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 2195 | binder_free_transaction(t); |
Lucas Tanure | d4ec15e | 2014-07-13 21:31:05 -0300 | [diff] [blame] | 2196 | if (next == NULL) { |
| 2197 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2198 | "reply failed, no target thread at root\n"); |
| 2199 | return; |
| 2200 | } |
| 2201 | t = next; |
| 2202 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 2203 | "reply failed, no target thread -- retry %d\n", |
| 2204 | t->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2205 | } |
| 2206 | } |
| 2207 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2208 | /** |
Martijn Coenen | fd82249 | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 2209 | * binder_cleanup_transaction() - cleans up undelivered transaction |
| 2210 | * @t: transaction that needs to be cleaned up |
| 2211 | * @reason: reason the transaction wasn't delivered |
| 2212 | * @error_code: error to return to caller (if synchronous call) |
| 2213 | */ |
| 2214 | static void binder_cleanup_transaction(struct binder_transaction *t, |
| 2215 | const char *reason, |
| 2216 | uint32_t error_code) |
| 2217 | { |
| 2218 | if (t->buffer->target_node && !(t->flags & TF_ONE_WAY)) { |
| 2219 | binder_send_failed_reply(t, error_code); |
| 2220 | } else { |
| 2221 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 2222 | "undelivered transaction %d, %s\n", |
| 2223 | t->debug_id, reason); |
| 2224 | binder_free_transaction(t); |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | /** |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2229 | * binder_get_object() - gets object and checks for valid metadata |
| 2230 | * @proc: binder_proc owning the buffer |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2231 | * @buffer: binder_buffer that we're parsing. |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2232 | * @offset: offset in the @buffer at which to validate an object. |
| 2233 | * @object: struct binder_object to read into |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2234 | * |
| 2235 | * Return: If there's a valid metadata object at @offset in @buffer, the |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2236 | * size of that object. Otherwise, it returns zero. The object |
| 2237 | * is read into the struct binder_object pointed to by @object. |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2238 | */ |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2239 | static size_t binder_get_object(struct binder_proc *proc, |
| 2240 | struct binder_buffer *buffer, |
| 2241 | unsigned long offset, |
| 2242 | struct binder_object *object) |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2243 | { |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2244 | size_t read_size; |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2245 | struct binder_object_header *hdr; |
| 2246 | size_t object_size = 0; |
| 2247 | |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2248 | read_size = min_t(size_t, sizeof(*object), buffer->data_size - offset); |
Todd Kjos | c1a2898 | 2019-03-19 09:53:01 -0700 | [diff] [blame] | 2249 | if (offset > buffer->data_size || read_size < sizeof(*hdr) || |
| 2250 | !IS_ALIGNED(offset, sizeof(u32))) |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2251 | return 0; |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2252 | binder_alloc_copy_from_buffer(&proc->alloc, object, buffer, |
| 2253 | offset, read_size); |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2254 | |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2255 | /* Ok, now see if we read a complete object. */ |
| 2256 | hdr = &object->hdr; |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2257 | switch (hdr->type) { |
| 2258 | case BINDER_TYPE_BINDER: |
| 2259 | case BINDER_TYPE_WEAK_BINDER: |
| 2260 | case BINDER_TYPE_HANDLE: |
| 2261 | case BINDER_TYPE_WEAK_HANDLE: |
| 2262 | object_size = sizeof(struct flat_binder_object); |
| 2263 | break; |
| 2264 | case BINDER_TYPE_FD: |
| 2265 | object_size = sizeof(struct binder_fd_object); |
| 2266 | break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2267 | case BINDER_TYPE_PTR: |
| 2268 | object_size = sizeof(struct binder_buffer_object); |
| 2269 | break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2270 | case BINDER_TYPE_FDA: |
| 2271 | object_size = sizeof(struct binder_fd_array_object); |
| 2272 | break; |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2273 | default: |
| 2274 | return 0; |
| 2275 | } |
| 2276 | if (offset <= buffer->data_size - object_size && |
| 2277 | buffer->data_size >= object_size) |
| 2278 | return object_size; |
| 2279 | else |
| 2280 | return 0; |
| 2281 | } |
| 2282 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2283 | /** |
| 2284 | * binder_validate_ptr() - validates binder_buffer_object in a binder_buffer. |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2285 | * @proc: binder_proc owning the buffer |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2286 | * @b: binder_buffer containing the object |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2287 | * @object: struct binder_object to read into |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2288 | * @index: index in offset array at which the binder_buffer_object is |
| 2289 | * located |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2290 | * @start_offset: points to the start of the offset array |
| 2291 | * @object_offsetp: offset of @object read from @b |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2292 | * @num_valid: the number of valid offsets in the offset array |
| 2293 | * |
| 2294 | * Return: If @index is within the valid range of the offset array |
| 2295 | * described by @start and @num_valid, and if there's a valid |
| 2296 | * binder_buffer_object at the offset found in index @index |
| 2297 | * of the offset array, that object is returned. Otherwise, |
| 2298 | * %NULL is returned. |
| 2299 | * Note that the offset found in index @index itself is not |
| 2300 | * verified; this function assumes that @num_valid elements |
| 2301 | * from @start were previously verified to have valid offsets. |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2302 | * If @object_offsetp is non-NULL, then the offset within |
| 2303 | * @b is written to it. |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2304 | */ |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2305 | static struct binder_buffer_object *binder_validate_ptr( |
| 2306 | struct binder_proc *proc, |
| 2307 | struct binder_buffer *b, |
| 2308 | struct binder_object *object, |
| 2309 | binder_size_t index, |
| 2310 | binder_size_t start_offset, |
| 2311 | binder_size_t *object_offsetp, |
| 2312 | binder_size_t num_valid) |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2313 | { |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2314 | size_t object_size; |
| 2315 | binder_size_t object_offset; |
| 2316 | unsigned long buffer_offset; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2317 | |
| 2318 | if (index >= num_valid) |
| 2319 | return NULL; |
| 2320 | |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2321 | buffer_offset = start_offset + sizeof(binder_size_t) * index; |
| 2322 | binder_alloc_copy_from_buffer(&proc->alloc, &object_offset, |
| 2323 | b, buffer_offset, sizeof(object_offset)); |
| 2324 | object_size = binder_get_object(proc, b, object_offset, object); |
| 2325 | if (!object_size || object->hdr.type != BINDER_TYPE_PTR) |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2326 | return NULL; |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2327 | if (object_offsetp) |
| 2328 | *object_offsetp = object_offset; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2329 | |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2330 | return &object->bbo; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | /** |
| 2334 | * binder_validate_fixup() - validates pointer/fd fixups happen in order. |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2335 | * @proc: binder_proc owning the buffer |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2336 | * @b: transaction buffer |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2337 | * @objects_start_offset: offset to start of objects buffer |
| 2338 | * @buffer_obj_offset: offset to binder_buffer_object in which to fix up |
| 2339 | * @fixup_offset: start offset in @buffer to fix up |
| 2340 | * @last_obj_offset: offset to last binder_buffer_object that we fixed |
| 2341 | * @last_min_offset: minimum fixup offset in object at @last_obj_offset |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2342 | * |
| 2343 | * Return: %true if a fixup in buffer @buffer at offset @offset is |
| 2344 | * allowed. |
| 2345 | * |
| 2346 | * For safety reasons, we only allow fixups inside a buffer to happen |
| 2347 | * at increasing offsets; additionally, we only allow fixup on the last |
| 2348 | * buffer object that was verified, or one of its parents. |
| 2349 | * |
| 2350 | * Example of what is allowed: |
| 2351 | * |
| 2352 | * A |
| 2353 | * B (parent = A, offset = 0) |
| 2354 | * C (parent = A, offset = 16) |
| 2355 | * D (parent = C, offset = 0) |
| 2356 | * E (parent = A, offset = 32) // min_offset is 16 (C.parent_offset) |
| 2357 | * |
| 2358 | * Examples of what is not allowed: |
| 2359 | * |
| 2360 | * Decreasing offsets within the same parent: |
| 2361 | * A |
| 2362 | * C (parent = A, offset = 16) |
| 2363 | * B (parent = A, offset = 0) // decreasing offset within A |
| 2364 | * |
| 2365 | * Referring to a parent that wasn't the last object or any of its parents: |
| 2366 | * A |
| 2367 | * B (parent = A, offset = 0) |
| 2368 | * C (parent = A, offset = 0) |
| 2369 | * C (parent = A, offset = 16) |
| 2370 | * D (parent = B, offset = 0) // B is not A or any of A's parents |
| 2371 | */ |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2372 | static bool binder_validate_fixup(struct binder_proc *proc, |
| 2373 | struct binder_buffer *b, |
| 2374 | binder_size_t objects_start_offset, |
| 2375 | binder_size_t buffer_obj_offset, |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2376 | binder_size_t fixup_offset, |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2377 | binder_size_t last_obj_offset, |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2378 | binder_size_t last_min_offset) |
| 2379 | { |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2380 | if (!last_obj_offset) { |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2381 | /* Nothing to fix up in */ |
| 2382 | return false; |
| 2383 | } |
| 2384 | |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2385 | while (last_obj_offset != buffer_obj_offset) { |
| 2386 | unsigned long buffer_offset; |
| 2387 | struct binder_object last_object; |
| 2388 | struct binder_buffer_object *last_bbo; |
| 2389 | size_t object_size = binder_get_object(proc, b, last_obj_offset, |
| 2390 | &last_object); |
| 2391 | if (object_size != sizeof(*last_bbo)) |
| 2392 | return false; |
| 2393 | |
| 2394 | last_bbo = &last_object.bbo; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2395 | /* |
| 2396 | * Safe to retrieve the parent of last_obj, since it |
| 2397 | * was already previously verified by the driver. |
| 2398 | */ |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2399 | if ((last_bbo->flags & BINDER_BUFFER_FLAG_HAS_PARENT) == 0) |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2400 | return false; |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2401 | last_min_offset = last_bbo->parent_offset + sizeof(uintptr_t); |
| 2402 | buffer_offset = objects_start_offset + |
| 2403 | sizeof(binder_size_t) * last_bbo->parent, |
| 2404 | binder_alloc_copy_from_buffer(&proc->alloc, &last_obj_offset, |
| 2405 | b, buffer_offset, |
| 2406 | sizeof(last_obj_offset)); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2407 | } |
| 2408 | return (fixup_offset >= last_min_offset); |
| 2409 | } |
| 2410 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2411 | static void binder_transaction_buffer_release(struct binder_proc *proc, |
| 2412 | struct binder_buffer *buffer, |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2413 | binder_size_t failed_at, |
| 2414 | bool is_failure) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2415 | { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2416 | int debug_id = buffer->debug_id; |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2417 | binder_size_t off_start_offset, buffer_offset, off_end_offset; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2418 | |
| 2419 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2420 | "%d buffer release %d, size %zd-%zd, failed at %llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2421 | proc->pid, buffer->debug_id, |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2422 | buffer->data_size, buffer->offsets_size, |
| 2423 | (unsigned long long)failed_at); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2424 | |
| 2425 | if (buffer->target_node) |
| 2426 | binder_dec_node(buffer->target_node, 1, 0); |
| 2427 | |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2428 | off_start_offset = ALIGN(buffer->data_size, sizeof(void *)); |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2429 | off_end_offset = is_failure ? failed_at : |
| 2430 | off_start_offset + buffer->offsets_size; |
| 2431 | for (buffer_offset = off_start_offset; buffer_offset < off_end_offset; |
| 2432 | buffer_offset += sizeof(binder_size_t)) { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2433 | struct binder_object_header *hdr; |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2434 | size_t object_size; |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2435 | struct binder_object object; |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2436 | binder_size_t object_offset; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2437 | |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2438 | binder_alloc_copy_from_buffer(&proc->alloc, &object_offset, |
| 2439 | buffer, buffer_offset, |
| 2440 | sizeof(object_offset)); |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2441 | object_size = binder_get_object(proc, buffer, |
| 2442 | object_offset, &object); |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2443 | if (object_size == 0) { |
| 2444 | pr_err("transaction release %d bad object at offset %lld, size %zd\n", |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2445 | debug_id, (u64)object_offset, buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2446 | continue; |
| 2447 | } |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 2448 | hdr = &object.hdr; |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2449 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2450 | case BINDER_TYPE_BINDER: |
| 2451 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2452 | struct flat_binder_object *fp; |
| 2453 | struct binder_node *node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 2454 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2455 | fp = to_flat_binder_object(hdr); |
| 2456 | node = binder_get_node(proc, fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2457 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2458 | pr_err("transaction release %d bad node %016llx\n", |
| 2459 | debug_id, (u64)fp->binder); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2460 | break; |
| 2461 | } |
| 2462 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 2463 | " node %d u%016llx\n", |
| 2464 | node->debug_id, (u64)node->ptr); |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2465 | binder_dec_node(node, hdr->type == BINDER_TYPE_BINDER, |
| 2466 | 0); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2467 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2468 | } break; |
| 2469 | case BINDER_TYPE_HANDLE: |
| 2470 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2471 | struct flat_binder_object *fp; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2472 | struct binder_ref_data rdata; |
| 2473 | int ret; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 2474 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2475 | fp = to_flat_binder_object(hdr); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2476 | ret = binder_dec_ref_for_handle(proc, fp->handle, |
| 2477 | hdr->type == BINDER_TYPE_HANDLE, &rdata); |
| 2478 | |
| 2479 | if (ret) { |
| 2480 | pr_err("transaction release %d bad handle %d, ret = %d\n", |
| 2481 | debug_id, fp->handle, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2482 | break; |
| 2483 | } |
| 2484 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2485 | " ref %d desc %d\n", |
| 2486 | rdata.debug_id, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2487 | } break; |
| 2488 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2489 | case BINDER_TYPE_FD: { |
| 2490 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
| 2491 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2492 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2493 | " fd %d\n", fp->fd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2494 | if (failed_at) |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2495 | task_close_fd(proc, fp->fd); |
| 2496 | } break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2497 | case BINDER_TYPE_PTR: |
| 2498 | /* |
| 2499 | * Nothing to do here, this will get cleaned up when the |
| 2500 | * transaction buffer gets freed |
| 2501 | */ |
| 2502 | break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2503 | case BINDER_TYPE_FDA: { |
| 2504 | struct binder_fd_array_object *fda; |
| 2505 | struct binder_buffer_object *parent; |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2506 | struct binder_object ptr_object; |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2507 | binder_size_t fda_offset; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2508 | size_t fd_index; |
| 2509 | binder_size_t fd_buf_size; |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2510 | binder_size_t num_valid; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2511 | |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2512 | num_valid = (buffer_offset - off_start_offset) / |
| 2513 | sizeof(binder_size_t); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2514 | fda = to_binder_fd_array_object(hdr); |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2515 | parent = binder_validate_ptr(proc, buffer, &ptr_object, |
| 2516 | fda->parent, |
| 2517 | off_start_offset, |
| 2518 | NULL, |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2519 | num_valid); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2520 | if (!parent) { |
| 2521 | pr_err("transaction release %d bad parent offset", |
| 2522 | debug_id); |
| 2523 | continue; |
| 2524 | } |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2525 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2526 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2527 | pr_err("transaction release %d invalid number of fds (%lld)\n", |
| 2528 | debug_id, (u64)fda->num_fds); |
| 2529 | continue; |
| 2530 | } |
| 2531 | if (fd_buf_size > parent->length || |
| 2532 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2533 | /* No space for all file descriptors here. */ |
| 2534 | pr_err("transaction release %d not enough space for %lld fds in buffer\n", |
| 2535 | debug_id, (u64)fda->num_fds); |
| 2536 | continue; |
| 2537 | } |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2538 | /* |
| 2539 | * the source data for binder_buffer_object is visible |
| 2540 | * to user-space and the @buffer element is the user |
| 2541 | * pointer to the buffer_object containing the fd_array. |
| 2542 | * Convert the address to an offset relative to |
| 2543 | * the base of the transaction buffer. |
| 2544 | */ |
| 2545 | fda_offset = |
| 2546 | (parent->buffer - (uintptr_t)buffer->user_data) + |
| 2547 | fda->parent_offset; |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2548 | for (fd_index = 0; fd_index < fda->num_fds; |
| 2549 | fd_index++) { |
| 2550 | u32 fd; |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2551 | binder_size_t offset = fda_offset + |
| 2552 | fd_index * sizeof(fd); |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2553 | |
| 2554 | binder_alloc_copy_from_buffer(&proc->alloc, |
| 2555 | &fd, |
| 2556 | buffer, |
| 2557 | offset, |
| 2558 | sizeof(fd)); |
| 2559 | task_close_fd(proc, fd); |
| 2560 | } |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2561 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2562 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 2563 | pr_err("transaction release %d bad object type %x\n", |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 2564 | debug_id, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 2565 | break; |
| 2566 | } |
| 2567 | } |
| 2568 | } |
| 2569 | |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2570 | static int binder_translate_binder(struct flat_binder_object *fp, |
| 2571 | struct binder_transaction *t, |
| 2572 | struct binder_thread *thread) |
| 2573 | { |
| 2574 | struct binder_node *node; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2575 | struct binder_proc *proc = thread->proc; |
| 2576 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2577 | struct binder_ref_data rdata; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2578 | int ret = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2579 | |
| 2580 | node = binder_get_node(proc, fp->binder); |
| 2581 | if (!node) { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2582 | node = binder_new_node(proc, fp); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2583 | if (!node) |
| 2584 | return -ENOMEM; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2585 | } |
| 2586 | if (fp->cookie != node->cookie) { |
| 2587 | binder_user_error("%d:%d sending u%016llx node %d, cookie mismatch %016llx != %016llx\n", |
| 2588 | proc->pid, thread->pid, (u64)fp->binder, |
| 2589 | node->debug_id, (u64)fp->cookie, |
| 2590 | (u64)node->cookie); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2591 | ret = -EINVAL; |
| 2592 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2593 | } |
Todd Kjos | 9693ca7 | 2021-10-12 09:56:13 -0700 | [diff] [blame] | 2594 | if (security_binder_transfer_binder(proc->cred, target_proc->cred)) { |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2595 | ret = -EPERM; |
| 2596 | goto done; |
| 2597 | } |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2598 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2599 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2600 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2601 | &thread->todo, &rdata); |
| 2602 | if (ret) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2603 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2604 | |
| 2605 | if (fp->hdr.type == BINDER_TYPE_BINDER) |
| 2606 | fp->hdr.type = BINDER_TYPE_HANDLE; |
| 2607 | else |
| 2608 | fp->hdr.type = BINDER_TYPE_WEAK_HANDLE; |
| 2609 | fp->binder = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2610 | fp->handle = rdata.desc; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2611 | fp->cookie = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2612 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2613 | trace_binder_transaction_node_to_ref(t, node, &rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2614 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2615 | " node %d u%016llx -> ref %d desc %d\n", |
| 2616 | node->debug_id, (u64)node->ptr, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2617 | rdata.debug_id, rdata.desc); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2618 | done: |
| 2619 | binder_put_node(node); |
| 2620 | return ret; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | static int binder_translate_handle(struct flat_binder_object *fp, |
| 2624 | struct binder_transaction *t, |
| 2625 | struct binder_thread *thread) |
| 2626 | { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2627 | struct binder_proc *proc = thread->proc; |
| 2628 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2629 | struct binder_node *node; |
| 2630 | struct binder_ref_data src_rdata; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2631 | int ret = 0; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2632 | |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2633 | node = binder_get_node_from_ref(proc, fp->handle, |
| 2634 | fp->hdr.type == BINDER_TYPE_HANDLE, &src_rdata); |
| 2635 | if (!node) { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2636 | binder_user_error("%d:%d got transaction with invalid handle, %d\n", |
| 2637 | proc->pid, thread->pid, fp->handle); |
| 2638 | return -EINVAL; |
| 2639 | } |
Todd Kjos | 9693ca7 | 2021-10-12 09:56:13 -0700 | [diff] [blame] | 2640 | if (security_binder_transfer_binder(proc->cred, target_proc->cred)) { |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2641 | ret = -EPERM; |
| 2642 | goto done; |
| 2643 | } |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2644 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2645 | binder_node_lock(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2646 | if (node->proc == target_proc) { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2647 | if (fp->hdr.type == BINDER_TYPE_HANDLE) |
| 2648 | fp->hdr.type = BINDER_TYPE_BINDER; |
| 2649 | else |
| 2650 | fp->hdr.type = BINDER_TYPE_WEAK_BINDER; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2651 | fp->binder = node->ptr; |
| 2652 | fp->cookie = node->cookie; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2653 | if (node->proc) |
| 2654 | binder_inner_proc_lock(node->proc); |
| 2655 | binder_inc_node_nilocked(node, |
| 2656 | fp->hdr.type == BINDER_TYPE_BINDER, |
| 2657 | 0, NULL); |
| 2658 | if (node->proc) |
| 2659 | binder_inner_proc_unlock(node->proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2660 | trace_binder_transaction_ref_to_node(t, node, &src_rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2661 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2662 | " ref %d desc %d -> node %d u%016llx\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2663 | src_rdata.debug_id, src_rdata.desc, node->debug_id, |
| 2664 | (u64)node->ptr); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2665 | binder_node_unlock(node); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2666 | } else { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2667 | struct binder_ref_data dest_rdata; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2668 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 2669 | binder_node_unlock(node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2670 | ret = binder_inc_ref_for_node(target_proc, node, |
| 2671 | fp->hdr.type == BINDER_TYPE_HANDLE, |
| 2672 | NULL, &dest_rdata); |
| 2673 | if (ret) |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2674 | goto done; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2675 | |
| 2676 | fp->binder = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2677 | fp->handle = dest_rdata.desc; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2678 | fp->cookie = 0; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2679 | trace_binder_transaction_ref_to_ref(t, node, &src_rdata, |
| 2680 | &dest_rdata); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2681 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 2682 | " ref %d desc %d -> ref %d desc %d (node %d)\n", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 2683 | src_rdata.debug_id, src_rdata.desc, |
| 2684 | dest_rdata.debug_id, dest_rdata.desc, |
| 2685 | node->debug_id); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2686 | } |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 2687 | done: |
| 2688 | binder_put_node(node); |
| 2689 | return ret; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | static int binder_translate_fd(int fd, |
| 2693 | struct binder_transaction *t, |
| 2694 | struct binder_thread *thread, |
| 2695 | struct binder_transaction *in_reply_to) |
| 2696 | { |
| 2697 | struct binder_proc *proc = thread->proc; |
| 2698 | struct binder_proc *target_proc = t->to_proc; |
| 2699 | int target_fd; |
| 2700 | struct file *file; |
| 2701 | int ret; |
| 2702 | bool target_allows_fd; |
| 2703 | |
| 2704 | if (in_reply_to) |
| 2705 | target_allows_fd = !!(in_reply_to->flags & TF_ACCEPT_FDS); |
| 2706 | else |
| 2707 | target_allows_fd = t->buffer->target_node->accept_fds; |
| 2708 | if (!target_allows_fd) { |
| 2709 | binder_user_error("%d:%d got %s with fd, %d, but target does not allow fds\n", |
| 2710 | proc->pid, thread->pid, |
| 2711 | in_reply_to ? "reply" : "transaction", |
| 2712 | fd); |
| 2713 | ret = -EPERM; |
| 2714 | goto err_fd_not_accepted; |
| 2715 | } |
| 2716 | |
| 2717 | file = fget(fd); |
| 2718 | if (!file) { |
| 2719 | binder_user_error("%d:%d got transaction with invalid fd, %d\n", |
| 2720 | proc->pid, thread->pid, fd); |
| 2721 | ret = -EBADF; |
| 2722 | goto err_fget; |
| 2723 | } |
Todd Kjos | 9693ca7 | 2021-10-12 09:56:13 -0700 | [diff] [blame] | 2724 | ret = security_binder_transfer_file(proc->cred, target_proc->cred, file); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 2725 | if (ret < 0) { |
| 2726 | ret = -EPERM; |
| 2727 | goto err_security; |
| 2728 | } |
| 2729 | |
| 2730 | target_fd = task_get_unused_fd_flags(target_proc, O_CLOEXEC); |
| 2731 | if (target_fd < 0) { |
| 2732 | ret = -ENOMEM; |
| 2733 | goto err_get_unused_fd; |
| 2734 | } |
| 2735 | task_fd_install(target_proc, target_fd, file); |
| 2736 | trace_binder_transaction_fd(t, fd, target_fd); |
| 2737 | binder_debug(BINDER_DEBUG_TRANSACTION, " fd %d -> %d\n", |
| 2738 | fd, target_fd); |
| 2739 | |
| 2740 | return target_fd; |
| 2741 | |
| 2742 | err_get_unused_fd: |
| 2743 | err_security: |
| 2744 | fput(file); |
| 2745 | err_fget: |
| 2746 | err_fd_not_accepted: |
| 2747 | return ret; |
| 2748 | } |
| 2749 | |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2750 | static int binder_translate_fd_array(struct binder_fd_array_object *fda, |
| 2751 | struct binder_buffer_object *parent, |
| 2752 | struct binder_transaction *t, |
| 2753 | struct binder_thread *thread, |
| 2754 | struct binder_transaction *in_reply_to) |
| 2755 | { |
| 2756 | binder_size_t fdi, fd_buf_size, num_installed_fds; |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2757 | binder_size_t fda_offset; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2758 | int target_fd; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2759 | struct binder_proc *proc = thread->proc; |
| 2760 | struct binder_proc *target_proc = t->to_proc; |
| 2761 | |
| 2762 | fd_buf_size = sizeof(u32) * fda->num_fds; |
| 2763 | if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { |
| 2764 | binder_user_error("%d:%d got transaction with invalid number of fds (%lld)\n", |
| 2765 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2766 | return -EINVAL; |
| 2767 | } |
| 2768 | if (fd_buf_size > parent->length || |
| 2769 | fda->parent_offset > parent->length - fd_buf_size) { |
| 2770 | /* No space for all file descriptors here. */ |
| 2771 | binder_user_error("%d:%d not enough space to store %lld fds in buffer\n", |
| 2772 | proc->pid, thread->pid, (u64)fda->num_fds); |
| 2773 | return -EINVAL; |
| 2774 | } |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2775 | /* |
| 2776 | * the source data for binder_buffer_object is visible |
| 2777 | * to user-space and the @buffer element is the user |
| 2778 | * pointer to the buffer_object containing the fd_array. |
| 2779 | * Convert the address to an offset relative to |
| 2780 | * the base of the transaction buffer. |
| 2781 | */ |
| 2782 | fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) + |
| 2783 | fda->parent_offset; |
| 2784 | if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) { |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2785 | binder_user_error("%d:%d parent offset not aligned correctly.\n", |
| 2786 | proc->pid, thread->pid); |
| 2787 | return -EINVAL; |
| 2788 | } |
| 2789 | for (fdi = 0; fdi < fda->num_fds; fdi++) { |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2790 | u32 fd; |
Todd Kjos | e7d4126 | 2019-03-27 16:12:31 -0700 | [diff] [blame] | 2791 | |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2792 | binder_size_t offset = fda_offset + fdi * sizeof(fd); |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2793 | |
| 2794 | binder_alloc_copy_from_buffer(&target_proc->alloc, |
| 2795 | &fd, t->buffer, |
| 2796 | offset, sizeof(fd)); |
| 2797 | target_fd = binder_translate_fd(fd, t, thread, in_reply_to); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2798 | if (target_fd < 0) |
| 2799 | goto err_translate_fd_failed; |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2800 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 2801 | t->buffer, offset, |
| 2802 | &target_fd, sizeof(fd)); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2803 | } |
| 2804 | return 0; |
| 2805 | |
| 2806 | err_translate_fd_failed: |
| 2807 | /* |
| 2808 | * Failed to allocate fd or security error, free fds |
| 2809 | * installed so far. |
| 2810 | */ |
| 2811 | num_installed_fds = fdi; |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2812 | for (fdi = 0; fdi < num_installed_fds; fdi++) { |
| 2813 | u32 fd; |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2814 | binder_size_t offset = fda_offset + fdi * sizeof(fd); |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 2815 | binder_alloc_copy_from_buffer(&target_proc->alloc, |
| 2816 | &fd, t->buffer, |
| 2817 | offset, sizeof(fd)); |
| 2818 | task_close_fd(target_proc, fd); |
| 2819 | } |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 2820 | return target_fd; |
| 2821 | } |
| 2822 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2823 | static int binder_fixup_parent(struct binder_transaction *t, |
| 2824 | struct binder_thread *thread, |
| 2825 | struct binder_buffer_object *bp, |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2826 | binder_size_t off_start_offset, |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2827 | binder_size_t num_valid, |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2828 | binder_size_t last_fixup_obj_off, |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2829 | binder_size_t last_fixup_min_off) |
| 2830 | { |
| 2831 | struct binder_buffer_object *parent; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2832 | struct binder_buffer *b = t->buffer; |
| 2833 | struct binder_proc *proc = thread->proc; |
| 2834 | struct binder_proc *target_proc = t->to_proc; |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2835 | struct binder_object object; |
| 2836 | binder_size_t buffer_offset; |
| 2837 | binder_size_t parent_offset; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2838 | |
| 2839 | if (!(bp->flags & BINDER_BUFFER_FLAG_HAS_PARENT)) |
| 2840 | return 0; |
| 2841 | |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2842 | parent = binder_validate_ptr(target_proc, b, &object, bp->parent, |
| 2843 | off_start_offset, &parent_offset, |
| 2844 | num_valid); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2845 | if (!parent) { |
| 2846 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 2847 | proc->pid, thread->pid); |
| 2848 | return -EINVAL; |
| 2849 | } |
| 2850 | |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2851 | if (!binder_validate_fixup(target_proc, b, off_start_offset, |
| 2852 | parent_offset, bp->parent_offset, |
| 2853 | last_fixup_obj_off, |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2854 | last_fixup_min_off)) { |
| 2855 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 2856 | proc->pid, thread->pid); |
| 2857 | return -EINVAL; |
| 2858 | } |
| 2859 | |
| 2860 | if (parent->length < sizeof(binder_uintptr_t) || |
| 2861 | bp->parent_offset > parent->length - sizeof(binder_uintptr_t)) { |
| 2862 | /* No space for a pointer here! */ |
| 2863 | binder_user_error("%d:%d got transaction with invalid parent offset\n", |
| 2864 | proc->pid, thread->pid); |
| 2865 | return -EINVAL; |
| 2866 | } |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2867 | buffer_offset = bp->parent_offset + |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 2868 | (uintptr_t)parent->buffer - (uintptr_t)b->user_data; |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 2869 | binder_alloc_copy_to_buffer(&target_proc->alloc, b, buffer_offset, |
| 2870 | &bp->buffer, sizeof(bp->buffer)); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 2871 | |
| 2872 | return 0; |
| 2873 | } |
| 2874 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2875 | /** |
Li Li | 83838b0 | 2022-05-26 15:00:18 -0700 | [diff] [blame] | 2876 | * binder_can_update_transaction() - Can a txn be superseded by an updated one? |
| 2877 | * @t1: the pending async txn in the frozen process |
| 2878 | * @t2: the new async txn to supersede the outdated pending one |
| 2879 | * |
| 2880 | * Return: true if t2 can supersede t1 |
| 2881 | * false if t2 can not supersede t1 |
| 2882 | */ |
| 2883 | static bool binder_can_update_transaction(struct binder_transaction *t1, |
| 2884 | struct binder_transaction *t2) |
| 2885 | { |
| 2886 | if ((t1->flags & t2->flags & (TF_ONE_WAY | TF_UPDATE_TXN)) != |
| 2887 | (TF_ONE_WAY | TF_UPDATE_TXN) || !t1->to_proc || !t2->to_proc) |
| 2888 | return false; |
| 2889 | if (t1->to_proc->tsk == t2->to_proc->tsk && t1->code == t2->code && |
| 2890 | t1->flags == t2->flags && t1->buffer->pid == t2->buffer->pid && |
| 2891 | t1->buffer->target_node->ptr == t2->buffer->target_node->ptr && |
| 2892 | t1->buffer->target_node->cookie == t2->buffer->target_node->cookie) |
| 2893 | return true; |
| 2894 | return false; |
| 2895 | } |
| 2896 | |
| 2897 | /** |
| 2898 | * binder_find_outdated_transaction_ilocked() - Find the outdated transaction |
| 2899 | * @t: new async transaction |
| 2900 | * @target_list: list to find outdated transaction |
| 2901 | * |
| 2902 | * Return: the outdated transaction if found |
| 2903 | * NULL if no outdated transacton can be found |
| 2904 | * |
| 2905 | * Requires the proc->inner_lock to be held. |
| 2906 | */ |
| 2907 | static struct binder_transaction * |
| 2908 | binder_find_outdated_transaction_ilocked(struct binder_transaction *t, |
| 2909 | struct list_head *target_list) |
| 2910 | { |
| 2911 | struct binder_work *w; |
| 2912 | |
| 2913 | list_for_each_entry(w, target_list, entry) { |
| 2914 | struct binder_transaction *t_queued; |
| 2915 | |
| 2916 | if (w->type != BINDER_WORK_TRANSACTION) |
| 2917 | continue; |
| 2918 | t_queued = container_of(w, struct binder_transaction, work); |
| 2919 | if (binder_can_update_transaction(t_queued, t)) |
| 2920 | return t_queued; |
| 2921 | } |
| 2922 | return NULL; |
| 2923 | } |
| 2924 | |
| 2925 | /** |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2926 | * binder_proc_transaction() - sends a transaction to a process and wakes it up |
| 2927 | * @t: transaction to send |
| 2928 | * @proc: process to send the transaction to |
| 2929 | * @thread: thread in @proc to send the transaction to (may be NULL) |
| 2930 | * |
| 2931 | * This function queues a transaction to the specified process. It will try |
| 2932 | * to find a thread in the target process to handle the transaction and |
| 2933 | * wake it up. If no thread is found, the work is queued to the proc |
| 2934 | * waitqueue. |
| 2935 | * |
| 2936 | * If the @thread parameter is not NULL, the transaction is always queued |
| 2937 | * to the waitlist of that specific thread. |
| 2938 | * |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 2939 | * Return: 0 if the transaction was successfully queued |
| 2940 | * BR_DEAD_REPLY if the target process or thread is dead |
Li Li | af1f984 | 2022-11-23 12:16:54 -0800 | [diff] [blame] | 2941 | * BR_FROZEN_REPLY if the target process or thread is frozen and |
| 2942 | * the sync transaction was rejected |
| 2943 | * BR_TRANSACTION_PENDING_FROZEN if the target process is frozen |
| 2944 | * and the async transaction was successfully queued |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2945 | */ |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 2946 | static int binder_proc_transaction(struct binder_transaction *t, |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2947 | struct binder_proc *proc, |
| 2948 | struct binder_thread *thread) |
| 2949 | { |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2950 | struct binder_node *node = t->buffer->target_node; |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2951 | struct binder_priority node_prio; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2952 | bool oneway = !!(t->flags & TF_ONE_WAY); |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2953 | bool pending_async = false; |
Li Li | 83838b0 | 2022-05-26 15:00:18 -0700 | [diff] [blame] | 2954 | struct binder_transaction *t_outdated = NULL; |
Li Li | af1f984 | 2022-11-23 12:16:54 -0800 | [diff] [blame] | 2955 | bool frozen = false; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2956 | |
| 2957 | BUG_ON(!node); |
| 2958 | binder_node_lock(node); |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2959 | node_prio.prio = node->min_priority; |
| 2960 | node_prio.sched_policy = node->sched_policy; |
| 2961 | |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2962 | if (oneway) { |
| 2963 | BUG_ON(thread); |
| 2964 | if (node->has_async_transaction) { |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2965 | pending_async = true; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2966 | } else { |
Gustavo A. R. Silva | 10340bf | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 2967 | node->has_async_transaction = true; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2968 | } |
| 2969 | } |
| 2970 | |
| 2971 | binder_inner_proc_lock(proc); |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 2972 | if (proc->is_frozen) { |
Li Li | af1f984 | 2022-11-23 12:16:54 -0800 | [diff] [blame] | 2973 | frozen = true; |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 2974 | proc->sync_recv |= !oneway; |
| 2975 | proc->async_recv |= oneway; |
| 2976 | } |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2977 | |
Li Li | af1f984 | 2022-11-23 12:16:54 -0800 | [diff] [blame] | 2978 | if ((frozen && !oneway) || proc->is_dead || |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 2979 | (thread && thread->is_dead)) { |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 2980 | bool proc_is_dead = proc->is_dead |
| 2981 | || (thread && thread->is_dead); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2982 | binder_inner_proc_unlock(proc); |
| 2983 | binder_node_unlock(node); |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 2984 | return proc_is_dead ? BR_DEAD_REPLY : BR_FROZEN_REPLY; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2985 | } |
| 2986 | |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2987 | if (!thread && !pending_async) |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 2988 | thread = binder_select_thread_ilocked(proc); |
| 2989 | |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2990 | if (thread) { |
Martijn Coenen | fb92c34 | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 2991 | binder_transaction_priority(thread->task, t, node_prio, |
| 2992 | node->inherit_rt); |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 2993 | binder_enqueue_thread_work_ilocked(thread, &t->work); |
| 2994 | } else if (!pending_async) { |
| 2995 | binder_enqueue_work_ilocked(&t->work, &proc->todo); |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 2996 | } else { |
Li Li | af1f984 | 2022-11-23 12:16:54 -0800 | [diff] [blame] | 2997 | if ((t->flags & TF_UPDATE_TXN) && frozen) { |
Li Li | 83838b0 | 2022-05-26 15:00:18 -0700 | [diff] [blame] | 2998 | t_outdated = binder_find_outdated_transaction_ilocked(t, |
| 2999 | &node->async_todo); |
| 3000 | if (t_outdated) { |
| 3001 | binder_debug(BINDER_DEBUG_TRANSACTION, |
| 3002 | "txn %d supersedes %d\n", |
| 3003 | t->debug_id, t_outdated->debug_id); |
| 3004 | list_del_init(&t_outdated->work.entry); |
| 3005 | proc->outstanding_txns--; |
| 3006 | } |
| 3007 | } |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3008 | binder_enqueue_work_ilocked(&t->work, &node->async_todo); |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 3009 | } |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3010 | |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3011 | if (!pending_async) |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3012 | binder_wakeup_thread_ilocked(proc, thread, !oneway /* sync */); |
| 3013 | |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 3014 | proc->outstanding_txns++; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3015 | binder_inner_proc_unlock(proc); |
| 3016 | binder_node_unlock(node); |
| 3017 | |
Li Li | 83838b0 | 2022-05-26 15:00:18 -0700 | [diff] [blame] | 3018 | /* |
| 3019 | * To reduce potential contention, free the outdated transaction and |
| 3020 | * buffer after releasing the locks. |
| 3021 | */ |
| 3022 | if (t_outdated) { |
| 3023 | struct binder_buffer *buffer = t_outdated->buffer; |
| 3024 | |
| 3025 | t_outdated->buffer = NULL; |
| 3026 | buffer->transaction = NULL; |
| 3027 | trace_binder_transaction_update_buffer_release(buffer); |
| 3028 | binder_transaction_buffer_release(proc, buffer, 0, false); |
| 3029 | binder_alloc_free_buf(&proc->alloc, buffer); |
| 3030 | kfree(t_outdated); |
| 3031 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 3032 | } |
| 3033 | |
Li Li | af1f984 | 2022-11-23 12:16:54 -0800 | [diff] [blame] | 3034 | if (oneway && frozen) |
| 3035 | return BR_TRANSACTION_PENDING_FROZEN; |
| 3036 | |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 3037 | return 0; |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3038 | } |
| 3039 | |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3040 | /** |
| 3041 | * binder_get_node_refs_for_txn() - Get required refs on node for txn |
| 3042 | * @node: struct binder_node for which to get refs |
Randy Dunlap | 6388607 | 2023-01-17 10:37:45 -0800 | [diff] [blame] | 3043 | * @procp: returns @node->proc if valid |
| 3044 | * @error: if no @procp then returns BR_DEAD_REPLY |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3045 | * |
| 3046 | * User-space normally keeps the node alive when creating a transaction |
| 3047 | * since it has a reference to the target. The local strong ref keeps it |
| 3048 | * alive if the sending process dies before the target process processes |
| 3049 | * the transaction. If the source process is malicious or has a reference |
| 3050 | * counting bug, relying on the local strong ref can fail. |
| 3051 | * |
| 3052 | * Since user-space can cause the local strong ref to go away, we also take |
| 3053 | * a tmpref on the node to ensure it survives while we are constructing |
| 3054 | * the transaction. We also need a tmpref on the proc while we are |
| 3055 | * constructing the transaction, so we take that here as well. |
| 3056 | * |
| 3057 | * Return: The target_node with refs taken or NULL if no @node->proc is NULL. |
Randy Dunlap | 6388607 | 2023-01-17 10:37:45 -0800 | [diff] [blame] | 3058 | * Also sets @procp if valid. If the @node->proc is NULL indicating that the |
| 3059 | * target proc has died, @error is set to BR_DEAD_REPLY. |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3060 | */ |
| 3061 | static struct binder_node *binder_get_node_refs_for_txn( |
| 3062 | struct binder_node *node, |
| 3063 | struct binder_proc **procp, |
| 3064 | uint32_t *error) |
| 3065 | { |
| 3066 | struct binder_node *target_node = NULL; |
| 3067 | |
| 3068 | binder_node_inner_lock(node); |
| 3069 | if (node->proc) { |
| 3070 | target_node = node; |
| 3071 | binder_inc_node_nilocked(node, 1, 0, NULL); |
| 3072 | binder_inc_node_tmpref_ilocked(node); |
| 3073 | node->proc->tmp_ref++; |
| 3074 | *procp = node->proc; |
| 3075 | } else |
| 3076 | *error = BR_DEAD_REPLY; |
| 3077 | binder_node_inner_unlock(node); |
| 3078 | |
| 3079 | return target_node; |
| 3080 | } |
| 3081 | |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 3082 | static void binder_set_txn_from_error(struct binder_transaction *t, int id, |
| 3083 | uint32_t command, int32_t param) |
| 3084 | { |
| 3085 | struct binder_thread *from = binder_get_txn_from_and_acq_inner(t); |
| 3086 | |
| 3087 | if (!from) { |
| 3088 | /* annotation for sparse */ |
| 3089 | __release(&from->proc->inner_lock); |
| 3090 | return; |
| 3091 | } |
| 3092 | |
| 3093 | /* don't override existing errors */ |
| 3094 | if (from->ee.command == BR_OK) |
| 3095 | binder_set_extended_error(&from->ee, id, command, param); |
| 3096 | binder_inner_proc_unlock(from->proc); |
| 3097 | binder_thread_dec_tmpref(from); |
| 3098 | } |
| 3099 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3100 | static void binder_transaction(struct binder_proc *proc, |
| 3101 | struct binder_thread *thread, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 3102 | struct binder_transaction_data *tr, int reply, |
| 3103 | binder_size_t extra_buffers_size) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3104 | { |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3105 | int ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3106 | struct binder_transaction *t; |
| 3107 | struct binder_work *tcomplete; |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3108 | binder_size_t buffer_offset = 0; |
| 3109 | binder_size_t off_start_offset, off_end_offset; |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3110 | binder_size_t off_min; |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3111 | binder_size_t sg_buf_offset, sg_buf_end_offset; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3112 | struct binder_proc *target_proc = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3113 | struct binder_thread *target_thread = NULL; |
| 3114 | struct binder_node *target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3115 | struct binder_transaction *in_reply_to = NULL; |
| 3116 | struct binder_transaction_log_entry *e; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3117 | uint32_t return_error = 0; |
| 3118 | uint32_t return_error_param = 0; |
| 3119 | uint32_t return_error_line = 0; |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3120 | binder_size_t last_fixup_obj_off = 0; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3121 | binder_size_t last_fixup_min_off = 0; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 3122 | struct binder_context *context = proc->context; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 3123 | int t_debug_id = atomic_inc_return(&binder_last_id); |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3124 | char *secctx = NULL; |
| 3125 | u32 secctx_sz = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3126 | |
| 3127 | e = binder_transaction_log_add(&binder_transaction_log); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 3128 | e->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3129 | e->call_type = reply ? 2 : !!(tr->flags & TF_ONE_WAY); |
| 3130 | e->from_proc = proc->pid; |
| 3131 | e->from_thread = thread->pid; |
| 3132 | e->target_handle = tr->target.handle; |
| 3133 | e->data_size = tr->data_size; |
| 3134 | e->offsets_size = tr->offsets_size; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 3135 | e->context_name = proc->context->name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3136 | |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 3137 | binder_inner_proc_lock(proc); |
| 3138 | binder_set_extended_error(&thread->ee, t_debug_id, BR_OK, 0); |
| 3139 | binder_inner_proc_unlock(proc); |
| 3140 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3141 | if (reply) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3142 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3143 | in_reply_to = thread->transaction_stack; |
| 3144 | if (in_reply_to == NULL) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3145 | binder_inner_proc_unlock(proc); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3146 | 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] | 3147 | proc->pid, thread->pid); |
| 3148 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3149 | return_error_param = -EPROTO; |
| 3150 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3151 | goto err_empty_call_stack; |
| 3152 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3153 | if (in_reply_to->to_thread != thread) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3154 | spin_lock(&in_reply_to->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3155 | 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] | 3156 | proc->pid, thread->pid, in_reply_to->debug_id, |
| 3157 | in_reply_to->to_proc ? |
| 3158 | in_reply_to->to_proc->pid : 0, |
| 3159 | in_reply_to->to_thread ? |
| 3160 | in_reply_to->to_thread->pid : 0); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3161 | spin_unlock(&in_reply_to->lock); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3162 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3163 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3164 | return_error_param = -EPROTO; |
| 3165 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3166 | in_reply_to = NULL; |
| 3167 | goto err_bad_call_stack; |
| 3168 | } |
| 3169 | thread->transaction_stack = in_reply_to->to_parent; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3170 | binder_inner_proc_unlock(proc); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3171 | 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] | 3172 | if (target_thread == NULL) { |
| 3173 | return_error = BR_DEAD_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3174 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3175 | goto err_dead_binder; |
| 3176 | } |
| 3177 | if (target_thread->transaction_stack != in_reply_to) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3178 | 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] | 3179 | proc->pid, thread->pid, |
| 3180 | target_thread->transaction_stack ? |
| 3181 | target_thread->transaction_stack->debug_id : 0, |
| 3182 | in_reply_to->debug_id); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3183 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3184 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3185 | return_error_param = -EPROTO; |
| 3186 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3187 | in_reply_to = NULL; |
| 3188 | target_thread = NULL; |
| 3189 | goto err_dead_binder; |
| 3190 | } |
| 3191 | target_proc = target_thread->proc; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3192 | target_proc->tmp_ref++; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3193 | binder_inner_proc_unlock(target_thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3194 | } else { |
| 3195 | if (tr->target.handle) { |
| 3196 | struct binder_ref *ref; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3197 | |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3198 | /* |
| 3199 | * There must already be a strong ref |
| 3200 | * on this node. If so, do a strong |
| 3201 | * increment on the node to ensure it |
| 3202 | * stays alive until the transaction is |
| 3203 | * done. |
| 3204 | */ |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3205 | binder_proc_lock(proc); |
| 3206 | ref = binder_get_ref_olocked(proc, tr->target.handle, |
| 3207 | true); |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3208 | if (ref) { |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3209 | target_node = binder_get_node_refs_for_txn( |
| 3210 | ref->node, &target_proc, |
| 3211 | &return_error); |
| 3212 | } else { |
| 3213 | binder_user_error("%d:%d got transaction to invalid handle\n", |
| 3214 | proc->pid, thread->pid); |
| 3215 | return_error = BR_FAILED_REPLY; |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3216 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 3217 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3218 | } else { |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3219 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 3220 | target_node = context->binder_context_mgr_node; |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3221 | if (target_node) |
| 3222 | target_node = binder_get_node_refs_for_txn( |
| 3223 | target_node, &target_proc, |
| 3224 | &return_error); |
| 3225 | else |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3226 | return_error = BR_DEAD_REPLY; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3227 | mutex_unlock(&context->context_mgr_node_lock); |
Hridya Valsaraju | 74402b5 | 2019-07-15 12:18:04 -0700 | [diff] [blame] | 3228 | if (target_node && target_proc->pid == proc->pid) { |
Martijn Coenen | fd0485e | 2018-03-28 11:14:50 +0200 | [diff] [blame] | 3229 | binder_user_error("%d:%d got transaction to context manager from process owning it\n", |
| 3230 | proc->pid, thread->pid); |
| 3231 | return_error = BR_FAILED_REPLY; |
| 3232 | return_error_param = -EINVAL; |
| 3233 | return_error_line = __LINE__; |
| 3234 | goto err_invalid_target_handle; |
| 3235 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3236 | } |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3237 | if (!target_node) { |
| 3238 | /* |
| 3239 | * return_error is set above |
| 3240 | */ |
| 3241 | return_error_param = -EINVAL; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3242 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3243 | goto err_dead_binder; |
| 3244 | } |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3245 | e->to_node = target_node->debug_id; |
Todd Kjos | 9693ca7 | 2021-10-12 09:56:13 -0700 | [diff] [blame] | 3246 | if (security_binder_transaction(proc->cred, |
| 3247 | target_proc->cred) < 0) { |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 3248 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3249 | return_error_param = -EPERM; |
| 3250 | return_error_line = __LINE__; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 3251 | goto err_invalid_target_handle; |
| 3252 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3253 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3254 | if (!(tr->flags & TF_ONE_WAY) && thread->transaction_stack) { |
| 3255 | struct binder_transaction *tmp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3256 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3257 | tmp = thread->transaction_stack; |
| 3258 | if (tmp->to_thread != thread) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3259 | spin_lock(&tmp->lock); |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3260 | 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] | 3261 | proc->pid, thread->pid, tmp->debug_id, |
| 3262 | tmp->to_proc ? tmp->to_proc->pid : 0, |
| 3263 | tmp->to_thread ? |
| 3264 | tmp->to_thread->pid : 0); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3265 | spin_unlock(&tmp->lock); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3266 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3267 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3268 | return_error_param = -EPROTO; |
| 3269 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3270 | goto err_bad_call_stack; |
| 3271 | } |
| 3272 | while (tmp) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3273 | struct binder_thread *from; |
| 3274 | |
| 3275 | spin_lock(&tmp->lock); |
| 3276 | from = tmp->from; |
| 3277 | if (from && from->proc == target_proc) { |
| 3278 | atomic_inc(&from->tmp_ref); |
| 3279 | target_thread = from; |
| 3280 | spin_unlock(&tmp->lock); |
| 3281 | break; |
| 3282 | } |
| 3283 | spin_unlock(&tmp->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3284 | tmp = tmp->from_parent; |
| 3285 | } |
| 3286 | } |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3287 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3288 | } |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3289 | if (target_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3290 | e->to_thread = target_thread->pid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3291 | e->to_proc = target_proc->pid; |
| 3292 | |
| 3293 | /* TODO: reuse incoming transaction for reply */ |
| 3294 | t = kzalloc(sizeof(*t), GFP_KERNEL); |
| 3295 | if (t == NULL) { |
| 3296 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3297 | return_error_param = -ENOMEM; |
| 3298 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3299 | goto err_alloc_t_failed; |
| 3300 | } |
| 3301 | binder_stats_created(BINDER_STAT_TRANSACTION); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3302 | spin_lock_init(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3303 | |
| 3304 | tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); |
| 3305 | if (tcomplete == NULL) { |
| 3306 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3307 | return_error_param = -ENOMEM; |
| 3308 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3309 | goto err_alloc_tcomplete_failed; |
| 3310 | } |
| 3311 | binder_stats_created(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3312 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 3313 | t->debug_id = t_debug_id; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3314 | |
| 3315 | if (reply) |
| 3316 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 3317 | "%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] | 3318 | proc->pid, thread->pid, t->debug_id, |
| 3319 | target_proc->pid, target_thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3320 | (u64)tr->data.ptr.buffer, |
| 3321 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 3322 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3323 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3324 | else |
| 3325 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 3326 | "%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] | 3327 | proc->pid, thread->pid, t->debug_id, |
| 3328 | target_proc->pid, target_node->debug_id, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3329 | (u64)tr->data.ptr.buffer, |
| 3330 | (u64)tr->data.ptr.offsets, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 3331 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3332 | (u64)extra_buffers_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3333 | |
| 3334 | if (!reply && !(tr->flags & TF_ONE_WAY)) |
| 3335 | t->from = thread; |
| 3336 | else |
| 3337 | t->from = NULL; |
Todd Kjos | 1c929ec | 2021-11-12 10:07:20 -0800 | [diff] [blame] | 3338 | t->sender_euid = task_euid(proc->tsk); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3339 | t->to_proc = target_proc; |
| 3340 | t->to_thread = target_thread; |
| 3341 | t->code = tr->code; |
| 3342 | t->flags = tr->flags; |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 3343 | if (!(t->flags & TF_ONE_WAY) && |
| 3344 | binder_supported_policy(current->policy)) { |
| 3345 | /* Inherit supported policies for synchronous transactions */ |
| 3346 | t->priority.sched_policy = current->policy; |
| 3347 | t->priority.prio = current->normal_prio; |
| 3348 | } else { |
| 3349 | /* Otherwise, fall back to the default priority */ |
| 3350 | t->priority = target_proc->default_priority; |
| 3351 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3352 | |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3353 | if (target_node && target_node->txn_security_ctx) { |
| 3354 | u32 secid; |
Todd Kjos | 5302574 | 2019-04-24 12:31:18 -0700 | [diff] [blame] | 3355 | size_t added_size; |
Carlos Llamas | 1212acf | 2021-07-20 17:23:46 +0000 | [diff] [blame] | 3356 | int max_retries = 100; |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3357 | |
| 3358 | security_task_getsecid(proc->tsk, &secid); |
Carlos Llamas | 1212acf | 2021-07-20 17:23:46 +0000 | [diff] [blame] | 3359 | retry_alloc: |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3360 | ret = security_secid_to_secctx(secid, &secctx, &secctx_sz); |
Carlos Llamas | 1212acf | 2021-07-20 17:23:46 +0000 | [diff] [blame] | 3361 | if (ret == -ENOMEM && max_retries-- > 0) { |
| 3362 | struct page *dummy_page; |
| 3363 | |
| 3364 | /* |
| 3365 | * security_secid_to_secctx() can fail because of a |
| 3366 | * GFP_ATOMIC allocation in which case -ENOMEM is |
| 3367 | * returned. This needs to be retried, but there is |
| 3368 | * currently no way to tell userspace to retry so we |
| 3369 | * do it here. We make sure there is still available |
| 3370 | * memory first and then retry. |
| 3371 | */ |
| 3372 | dummy_page = alloc_page(GFP_KERNEL); |
| 3373 | if (dummy_page) { |
| 3374 | __free_page(dummy_page); |
| 3375 | goto retry_alloc; |
| 3376 | } |
| 3377 | } |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3378 | if (ret) { |
| 3379 | return_error = BR_FAILED_REPLY; |
| 3380 | return_error_param = ret; |
| 3381 | return_error_line = __LINE__; |
| 3382 | goto err_get_secctx_failed; |
| 3383 | } |
Todd Kjos | 5302574 | 2019-04-24 12:31:18 -0700 | [diff] [blame] | 3384 | added_size = ALIGN(secctx_sz, sizeof(u64)); |
| 3385 | extra_buffers_size += added_size; |
| 3386 | if (extra_buffers_size < added_size) { |
| 3387 | /* integer overflow of extra_buffers_size */ |
| 3388 | return_error = BR_FAILED_REPLY; |
| 3389 | return_error_param = EINVAL; |
| 3390 | return_error_line = __LINE__; |
| 3391 | goto err_bad_extra_size; |
| 3392 | } |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3393 | } |
| 3394 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3395 | trace_binder_transaction(reply, t, target_node); |
| 3396 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 3397 | t->buffer = binder_alloc_new_buf(&target_proc->alloc, tr->data_size, |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 3398 | tr->offsets_size, extra_buffers_size, |
Martijn Coenen | 370fe26 | 2020-08-21 14:25:44 +0200 | [diff] [blame] | 3399 | !reply && (t->flags & TF_ONE_WAY), current->tgid); |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3400 | if (IS_ERR(t->buffer)) { |
| 3401 | /* |
| 3402 | * -ESRCH indicates VMA cleared. The target is dying. |
| 3403 | */ |
| 3404 | return_error_param = PTR_ERR(t->buffer); |
| 3405 | return_error = return_error_param == -ESRCH ? |
| 3406 | BR_DEAD_REPLY : BR_FAILED_REPLY; |
| 3407 | return_error_line = __LINE__; |
| 3408 | t->buffer = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3409 | goto err_binder_alloc_buf_failed; |
| 3410 | } |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3411 | if (secctx) { |
| 3412 | size_t buf_offset = ALIGN(tr->data_size, sizeof(void *)) + |
| 3413 | ALIGN(tr->offsets_size, sizeof(void *)) + |
| 3414 | ALIGN(extra_buffers_size, sizeof(void *)) - |
| 3415 | ALIGN(secctx_sz, sizeof(u64)); |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3416 | |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3417 | t->security_ctx = (uintptr_t)t->buffer->user_data + buf_offset; |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3418 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3419 | t->buffer, buf_offset, |
| 3420 | secctx, secctx_sz); |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3421 | security_release_secctx(secctx, secctx_sz); |
| 3422 | secctx = NULL; |
| 3423 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3424 | t->buffer->debug_id = t->debug_id; |
| 3425 | t->buffer->transaction = t; |
| 3426 | t->buffer->target_node = target_node; |
Todd Kjos | 451fffd | 2020-11-20 15:37:43 -0800 | [diff] [blame] | 3427 | t->buffer->clear_on_free = !!(t->flags & TF_CLEAR_BUF); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3428 | trace_binder_transaction_alloc_buf(t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3429 | |
Todd Kjos | af6f1cb | 2019-02-08 10:35:14 -0800 | [diff] [blame] | 3430 | if (binder_alloc_copy_user_to_buffer( |
| 3431 | &target_proc->alloc, |
| 3432 | t->buffer, 0, |
| 3433 | (const void __user *) |
| 3434 | (uintptr_t)tr->data.ptr.buffer, |
| 3435 | tr->data_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3436 | binder_user_error("%d:%d got transaction with invalid data ptr\n", |
| 3437 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3438 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3439 | return_error_param = -EFAULT; |
| 3440 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3441 | goto err_copy_data_failed; |
| 3442 | } |
Todd Kjos | af6f1cb | 2019-02-08 10:35:14 -0800 | [diff] [blame] | 3443 | if (binder_alloc_copy_user_to_buffer( |
| 3444 | &target_proc->alloc, |
| 3445 | t->buffer, |
| 3446 | ALIGN(tr->data_size, sizeof(void *)), |
| 3447 | (const void __user *) |
| 3448 | (uintptr_t)tr->data.ptr.offsets, |
| 3449 | tr->offsets_size)) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3450 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3451 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3452 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3453 | return_error_param = -EFAULT; |
| 3454 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3455 | goto err_copy_data_failed; |
| 3456 | } |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3457 | if (!IS_ALIGNED(tr->offsets_size, sizeof(binder_size_t))) { |
| 3458 | binder_user_error("%d:%d got transaction with invalid offsets size, %lld\n", |
| 3459 | proc->pid, thread->pid, (u64)tr->offsets_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3460 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3461 | return_error_param = -EINVAL; |
| 3462 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3463 | goto err_bad_offset; |
| 3464 | } |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3465 | if (!IS_ALIGNED(extra_buffers_size, sizeof(u64))) { |
| 3466 | binder_user_error("%d:%d got transaction with unaligned buffers size, %lld\n", |
| 3467 | proc->pid, thread->pid, |
| 3468 | (u64)extra_buffers_size); |
| 3469 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3470 | return_error_param = -EINVAL; |
| 3471 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3472 | goto err_bad_offset; |
| 3473 | } |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3474 | off_start_offset = ALIGN(tr->data_size, sizeof(void *)); |
| 3475 | buffer_offset = off_start_offset; |
| 3476 | off_end_offset = off_start_offset + tr->offsets_size; |
| 3477 | sg_buf_offset = ALIGN(off_end_offset, sizeof(void *)); |
Martijn Coenen | 4be9987 | 2019-07-09 13:09:23 +0200 | [diff] [blame] | 3478 | sg_buf_end_offset = sg_buf_offset + extra_buffers_size - |
| 3479 | ALIGN(secctx_sz, sizeof(u64)); |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3480 | off_min = 0; |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3481 | for (buffer_offset = off_start_offset; buffer_offset < off_end_offset; |
| 3482 | buffer_offset += sizeof(binder_size_t)) { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3483 | struct binder_object_header *hdr; |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3484 | size_t object_size; |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3485 | struct binder_object object; |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3486 | binder_size_t object_offset; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3487 | |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3488 | binder_alloc_copy_from_buffer(&target_proc->alloc, |
| 3489 | &object_offset, |
| 3490 | t->buffer, |
| 3491 | buffer_offset, |
| 3492 | sizeof(object_offset)); |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3493 | object_size = binder_get_object(target_proc, t->buffer, |
| 3494 | object_offset, &object); |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3495 | if (object_size == 0 || object_offset < off_min) { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3496 | binder_user_error("%d:%d got transaction with invalid offset (%lld, min %lld max %lld) or object.\n", |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3497 | proc->pid, thread->pid, |
| 3498 | (u64)object_offset, |
Arve Hjønnevåg | 212265e | 2016-02-09 21:05:32 -0800 | [diff] [blame] | 3499 | (u64)off_min, |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3500 | (u64)t->buffer->data_size); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3501 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3502 | return_error_param = -EINVAL; |
| 3503 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3504 | goto err_bad_offset; |
| 3505 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3506 | |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3507 | hdr = &object.hdr; |
Todd Kjos | 4d01f6b | 2019-02-08 10:35:15 -0800 | [diff] [blame] | 3508 | off_min = object_offset + object_size; |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3509 | switch (hdr->type) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3510 | case BINDER_TYPE_BINDER: |
| 3511 | case BINDER_TYPE_WEAK_BINDER: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3512 | struct flat_binder_object *fp; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3513 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3514 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3515 | ret = binder_translate_binder(fp, t, thread); |
| 3516 | if (ret < 0) { |
Christian Engelmayer | 7d42043 | 2014-05-07 21:44:53 +0200 | [diff] [blame] | 3517 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3518 | return_error_param = ret; |
| 3519 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3520 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3521 | } |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3522 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3523 | t->buffer, object_offset, |
| 3524 | fp, sizeof(*fp)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3525 | } break; |
| 3526 | case BINDER_TYPE_HANDLE: |
| 3527 | case BINDER_TYPE_WEAK_HANDLE: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3528 | struct flat_binder_object *fp; |
Arve Hjønnevåg | 0a3ffab | 2016-10-24 15:20:29 +0200 | [diff] [blame] | 3529 | |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3530 | fp = to_flat_binder_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3531 | ret = binder_translate_handle(fp, t, thread); |
| 3532 | if (ret < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3533 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3534 | return_error_param = ret; |
| 3535 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3536 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3537 | } |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3538 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3539 | t->buffer, object_offset, |
| 3540 | fp, sizeof(*fp)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3541 | } break; |
| 3542 | |
| 3543 | case BINDER_TYPE_FD: { |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3544 | struct binder_fd_object *fp = to_binder_fd_object(hdr); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3545 | int target_fd = binder_translate_fd(fp->fd, t, thread, |
| 3546 | in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3547 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3548 | if (target_fd < 0) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3549 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3550 | return_error_param = target_fd; |
| 3551 | return_error_line = __LINE__; |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3552 | goto err_translate_failed; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3553 | } |
Martijn Coenen | feba390 | 2017-02-03 14:40:45 -0800 | [diff] [blame] | 3554 | fp->pad_binder = 0; |
| 3555 | fp->fd = target_fd; |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3556 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3557 | t->buffer, object_offset, |
| 3558 | fp, sizeof(*fp)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3559 | } break; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3560 | case BINDER_TYPE_FDA: { |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3561 | struct binder_object ptr_object; |
| 3562 | binder_size_t parent_offset; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3563 | struct binder_fd_array_object *fda = |
| 3564 | to_binder_fd_array_object(hdr); |
Todd Kjos | fe7096a | 2019-12-13 12:25:31 -0800 | [diff] [blame] | 3565 | size_t num_valid = (buffer_offset - off_start_offset) / |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3566 | sizeof(binder_size_t); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3567 | struct binder_buffer_object *parent = |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3568 | binder_validate_ptr(target_proc, t->buffer, |
| 3569 | &ptr_object, fda->parent, |
| 3570 | off_start_offset, |
| 3571 | &parent_offset, |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3572 | num_valid); |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3573 | if (!parent) { |
| 3574 | binder_user_error("%d:%d got transaction with invalid parent offset or type\n", |
| 3575 | proc->pid, thread->pid); |
| 3576 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3577 | return_error_param = -EINVAL; |
| 3578 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3579 | goto err_bad_parent; |
| 3580 | } |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3581 | if (!binder_validate_fixup(target_proc, t->buffer, |
| 3582 | off_start_offset, |
| 3583 | parent_offset, |
| 3584 | fda->parent_offset, |
| 3585 | last_fixup_obj_off, |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3586 | last_fixup_min_off)) { |
| 3587 | binder_user_error("%d:%d got transaction with out-of-order buffer fixup\n", |
| 3588 | proc->pid, thread->pid); |
| 3589 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3590 | return_error_param = -EINVAL; |
| 3591 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3592 | goto err_bad_parent; |
| 3593 | } |
| 3594 | ret = binder_translate_fd_array(fda, parent, t, thread, |
| 3595 | in_reply_to); |
| 3596 | if (ret < 0) { |
| 3597 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3598 | return_error_param = ret; |
| 3599 | return_error_line = __LINE__; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3600 | goto err_translate_failed; |
| 3601 | } |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3602 | last_fixup_obj_off = parent_offset; |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3603 | last_fixup_min_off = |
| 3604 | fda->parent_offset + sizeof(u32) * fda->num_fds; |
| 3605 | } break; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3606 | case BINDER_TYPE_PTR: { |
| 3607 | struct binder_buffer_object *bp = |
| 3608 | to_binder_buffer_object(hdr); |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3609 | size_t buf_left = sg_buf_end_offset - sg_buf_offset; |
| 3610 | size_t num_valid; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3611 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3612 | if (bp->length > buf_left) { |
| 3613 | binder_user_error("%d:%d got transaction with too large buffer\n", |
| 3614 | proc->pid, thread->pid); |
| 3615 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3616 | return_error_param = -EINVAL; |
| 3617 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3618 | goto err_bad_offset; |
| 3619 | } |
Todd Kjos | af6f1cb | 2019-02-08 10:35:14 -0800 | [diff] [blame] | 3620 | if (binder_alloc_copy_user_to_buffer( |
| 3621 | &target_proc->alloc, |
| 3622 | t->buffer, |
| 3623 | sg_buf_offset, |
| 3624 | (const void __user *) |
| 3625 | (uintptr_t)bp->buffer, |
| 3626 | bp->length)) { |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3627 | binder_user_error("%d:%d got transaction with invalid offsets ptr\n", |
| 3628 | proc->pid, thread->pid); |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3629 | return_error_param = -EFAULT; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3630 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3631 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3632 | goto err_copy_data_failed; |
| 3633 | } |
| 3634 | /* Fixup buffer pointer to target proc address space */ |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3635 | bp->buffer = (uintptr_t) |
| 3636 | t->buffer->user_data + sg_buf_offset; |
| 3637 | sg_buf_offset += ALIGN(bp->length, sizeof(u64)); |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3638 | |
Todd Kjos | fe7096a | 2019-12-13 12:25:31 -0800 | [diff] [blame] | 3639 | num_valid = (buffer_offset - off_start_offset) / |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3640 | sizeof(binder_size_t); |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3641 | ret = binder_fixup_parent(t, thread, bp, |
| 3642 | off_start_offset, |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3643 | num_valid, |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3644 | last_fixup_obj_off, |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3645 | last_fixup_min_off); |
| 3646 | if (ret < 0) { |
| 3647 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3648 | return_error_param = ret; |
| 3649 | return_error_line = __LINE__; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3650 | goto err_translate_failed; |
| 3651 | } |
Todd Kjos | 5088f13 | 2019-02-08 10:35:16 -0800 | [diff] [blame] | 3652 | binder_alloc_copy_to_buffer(&target_proc->alloc, |
| 3653 | t->buffer, object_offset, |
| 3654 | bp, sizeof(*bp)); |
Todd Kjos | 5f46f33 | 2019-02-08 10:35:17 -0800 | [diff] [blame] | 3655 | last_fixup_obj_off = object_offset; |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 3656 | last_fixup_min_off = 0; |
| 3657 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3658 | default: |
Serban Constantinescu | 64dcfe6 | 2013-07-04 10:54:48 +0100 | [diff] [blame] | 3659 | 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] | 3660 | proc->pid, thread->pid, hdr->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3661 | return_error = BR_FAILED_REPLY; |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3662 | return_error_param = -EINVAL; |
| 3663 | return_error_line = __LINE__; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3664 | goto err_bad_object_type; |
| 3665 | } |
| 3666 | } |
Hang Lu | 4dbc168 | 2021-04-09 17:40:46 +0800 | [diff] [blame] | 3667 | if (t->buffer->oneway_spam_suspect) |
| 3668 | tcomplete->type = BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT; |
| 3669 | else |
| 3670 | tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3671 | t->work.type = BINDER_WORK_TRANSACTION; |
Todd Kjos | ccae6f6 | 2017-06-29 12:01:48 -0700 | [diff] [blame] | 3672 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3673 | if (reply) { |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3674 | binder_enqueue_thread_work(thread, tcomplete); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3675 | binder_inner_proc_lock(target_proc); |
Li Li | 503f84d | 2021-09-07 16:12:53 -0700 | [diff] [blame] | 3676 | if (target_thread->is_dead) { |
| 3677 | return_error = BR_DEAD_REPLY; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3678 | binder_inner_proc_unlock(target_proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3679 | goto err_dead_proc_or_thread; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3680 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3681 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3682 | binder_pop_transaction_ilocked(target_thread, in_reply_to); |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3683 | binder_enqueue_thread_work_ilocked(target_thread, &t->work); |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 3684 | target_proc->outstanding_txns++; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3685 | binder_inner_proc_unlock(target_proc); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 3686 | wake_up_interruptible_sync(&target_thread->wait); |
Martijn Coenen | 22b061b | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3687 | binder_restore_priority(current, in_reply_to->saved_priority); |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 3688 | binder_free_transaction(in_reply_to); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3689 | } else if (!(t->flags & TF_ONE_WAY)) { |
| 3690 | BUG_ON(t->buffer->async_transaction != 0); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3691 | binder_inner_proc_lock(proc); |
Martijn Coenen | ad4cc17 | 2017-11-13 09:55:21 +0100 | [diff] [blame] | 3692 | /* |
| 3693 | * Defer the TRANSACTION_COMPLETE, so we don't return to |
| 3694 | * userspace immediately; this allows the target process to |
| 3695 | * immediately start processing this transaction, reducing |
| 3696 | * latency. We will then return the TRANSACTION_COMPLETE when |
| 3697 | * the target replies (or there is an error). |
| 3698 | */ |
| 3699 | binder_enqueue_deferred_thread_work_ilocked(thread, tcomplete); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3700 | t->need_reply = 1; |
| 3701 | t->from_parent = thread->transaction_stack; |
| 3702 | thread->transaction_stack = t; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3703 | binder_inner_proc_unlock(proc); |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 3704 | return_error = binder_proc_transaction(t, |
| 3705 | target_proc, target_thread); |
| 3706 | if (return_error) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 3707 | binder_inner_proc_lock(proc); |
| 3708 | binder_pop_transaction_ilocked(thread, t); |
| 3709 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3710 | goto err_dead_proc_or_thread; |
| 3711 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3712 | } else { |
| 3713 | BUG_ON(target_node == NULL); |
| 3714 | BUG_ON(t->buffer->async_transaction != 1); |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 3715 | return_error = binder_proc_transaction(t, target_proc, NULL); |
Li Li | af1f984 | 2022-11-23 12:16:54 -0800 | [diff] [blame] | 3716 | /* |
| 3717 | * Let the caller know when async transaction reaches a frozen |
| 3718 | * process and is put in a pending queue, waiting for the target |
| 3719 | * process to be unfrozen. |
| 3720 | */ |
| 3721 | if (return_error == BR_TRANSACTION_PENDING_FROZEN) |
| 3722 | tcomplete->type = BINDER_WORK_TRANSACTION_PENDING; |
| 3723 | binder_enqueue_thread_work(thread, tcomplete); |
| 3724 | if (return_error && |
| 3725 | return_error != BR_TRANSACTION_PENDING_FROZEN) |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3726 | goto err_dead_proc_or_thread; |
Riley Andrews | 00b40d6 | 2017-06-29 12:01:37 -0700 | [diff] [blame] | 3727 | } |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3728 | if (target_thread) |
| 3729 | binder_thread_dec_tmpref(target_thread); |
| 3730 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3731 | if (target_node) |
| 3732 | binder_dec_node_tmpref(target_node); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 3733 | /* |
| 3734 | * write barrier to synchronize with initialization |
| 3735 | * of log entry |
| 3736 | */ |
| 3737 | smp_wmb(); |
| 3738 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3739 | return; |
| 3740 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3741 | err_dead_proc_or_thread: |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3742 | return_error_line = __LINE__; |
Xu YiPing | d53bebd | 2017-09-05 10:21:52 -0700 | [diff] [blame] | 3743 | binder_dequeue_work(proc, tcomplete); |
Martijn Coenen | a056af4 | 2017-02-03 14:40:49 -0800 | [diff] [blame] | 3744 | err_translate_failed: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3745 | err_bad_object_type: |
| 3746 | err_bad_offset: |
Martijn Coenen | def95c7 | 2017-02-03 14:40:52 -0800 | [diff] [blame] | 3747 | err_bad_parent: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3748 | err_copy_data_failed: |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3749 | trace_binder_transaction_failed_buffer_release(t->buffer); |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 3750 | binder_transaction_buffer_release(target_proc, t->buffer, |
| 3751 | buffer_offset, true); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3752 | if (target_node) |
| 3753 | binder_dec_node_tmpref(target_node); |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3754 | target_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3755 | t->buffer->transaction = NULL; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 3756 | binder_alloc_free_buf(&target_proc->alloc, t->buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3757 | err_binder_alloc_buf_failed: |
Todd Kjos | 5302574 | 2019-04-24 12:31:18 -0700 | [diff] [blame] | 3758 | err_bad_extra_size: |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 3759 | if (secctx) |
| 3760 | security_release_secctx(secctx, secctx_sz); |
| 3761 | err_get_secctx_failed: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3762 | kfree(tcomplete); |
| 3763 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 3764 | err_alloc_tcomplete_failed: |
| 3765 | kfree(t); |
| 3766 | binder_stats_deleted(BINDER_STAT_TRANSACTION); |
| 3767 | err_alloc_t_failed: |
| 3768 | err_bad_call_stack: |
| 3769 | err_empty_call_stack: |
| 3770 | err_dead_binder: |
| 3771 | err_invalid_target_handle: |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 3772 | if (target_thread) |
| 3773 | binder_thread_dec_tmpref(target_thread); |
| 3774 | if (target_proc) |
| 3775 | binder_proc_dec_tmpref(target_proc); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3776 | if (target_node) { |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3777 | binder_dec_node(target_node, 1, 0); |
Todd Kjos | 512cf46 | 2017-09-29 15:39:49 -0700 | [diff] [blame] | 3778 | binder_dec_node_tmpref(target_node); |
| 3779 | } |
Todd Kjos | eb34983 | 2017-06-29 12:01:56 -0700 | [diff] [blame] | 3780 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3781 | binder_debug(BINDER_DEBUG_FAILED_TRANSACTION, |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3782 | "%d:%d transaction failed %d/%d, size %lld-%lld line %d\n", |
| 3783 | proc->pid, thread->pid, return_error, return_error_param, |
| 3784 | (u64)tr->data_size, (u64)tr->offsets_size, |
| 3785 | return_error_line); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3786 | |
| 3787 | { |
| 3788 | struct binder_transaction_log_entry *fe; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 3789 | |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 3790 | e->return_error = return_error; |
| 3791 | e->return_error_param = return_error_param; |
| 3792 | e->return_error_line = return_error_line; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3793 | fe = binder_transaction_log_add(&binder_transaction_log_failed); |
| 3794 | *fe = *e; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 3795 | /* |
| 3796 | * write barrier to synchronize with initialization |
| 3797 | * of log entry |
| 3798 | */ |
| 3799 | smp_wmb(); |
| 3800 | WRITE_ONCE(e->debug_id_done, t_debug_id); |
| 3801 | WRITE_ONCE(fe->debug_id_done, t_debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3802 | } |
| 3803 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3804 | BUG_ON(thread->return_error.cmd != BR_OK); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3805 | if (in_reply_to) { |
Martijn Coenen | 22b061b | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 3806 | binder_restore_priority(current, in_reply_to->saved_priority); |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 3807 | binder_set_txn_from_error(in_reply_to, t_debug_id, |
| 3808 | return_error, return_error_param); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3809 | thread->return_error.cmd = BR_TRANSACTION_COMPLETE; |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3810 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3811 | binder_send_failed_reply(in_reply_to, return_error); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3812 | } else { |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 3813 | binder_inner_proc_lock(proc); |
| 3814 | binder_set_extended_error(&thread->ee, t_debug_id, |
| 3815 | return_error, return_error_param); |
| 3816 | binder_inner_proc_unlock(proc); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3817 | thread->return_error.cmd = return_error; |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 3818 | binder_enqueue_thread_work(thread, &thread->return_error.work); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3819 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3820 | } |
| 3821 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 3822 | static int binder_thread_write(struct binder_proc *proc, |
| 3823 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3824 | binder_uintptr_t binder_buffer, size_t size, |
| 3825 | binder_size_t *consumed) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3826 | { |
| 3827 | uint32_t cmd; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 3828 | struct binder_context *context = proc->context; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3829 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3830 | void __user *ptr = buffer + *consumed; |
| 3831 | void __user *end = buffer + size; |
| 3832 | |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 3833 | while (ptr < end && thread->return_error.cmd == BR_OK) { |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3834 | int ret; |
| 3835 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3836 | if (get_user(cmd, (uint32_t __user *)ptr)) |
| 3837 | return -EFAULT; |
| 3838 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 3839 | trace_binder_command(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3840 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.bc)) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 3841 | atomic_inc(&binder_stats.bc[_IOC_NR(cmd)]); |
| 3842 | atomic_inc(&proc->stats.bc[_IOC_NR(cmd)]); |
| 3843 | atomic_inc(&thread->stats.bc[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3844 | } |
| 3845 | switch (cmd) { |
| 3846 | case BC_INCREFS: |
| 3847 | case BC_ACQUIRE: |
| 3848 | case BC_RELEASE: |
| 3849 | case BC_DECREFS: { |
| 3850 | uint32_t target; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3851 | const char *debug_string; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3852 | bool strong = cmd == BC_ACQUIRE || cmd == BC_RELEASE; |
| 3853 | bool increment = cmd == BC_INCREFS || cmd == BC_ACQUIRE; |
| 3854 | struct binder_ref_data rdata; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3855 | |
| 3856 | if (get_user(target, (uint32_t __user *)ptr)) |
| 3857 | return -EFAULT; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3858 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3859 | ptr += sizeof(uint32_t); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3860 | ret = -1; |
| 3861 | if (increment && !target) { |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3862 | struct binder_node *ctx_mgr_node; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3863 | mutex_lock(&context->context_mgr_node_lock); |
| 3864 | ctx_mgr_node = context->binder_context_mgr_node; |
Todd Kjos | 5206f78 | 2020-08-28 08:55:25 -0700 | [diff] [blame] | 3865 | if (ctx_mgr_node) |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3866 | ret = binder_inc_ref_for_node( |
| 3867 | proc, ctx_mgr_node, |
| 3868 | strong, NULL, &rdata); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 3869 | mutex_unlock(&context->context_mgr_node_lock); |
| 3870 | } |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3871 | if (ret) |
| 3872 | ret = binder_update_ref_for_handle( |
| 3873 | proc, target, increment, strong, |
| 3874 | &rdata); |
| 3875 | if (!ret && rdata.desc != target) { |
| 3876 | binder_user_error("%d:%d tried to acquire reference to desc %d, got %d instead\n", |
| 3877 | proc->pid, thread->pid, |
| 3878 | target, rdata.desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3879 | } |
| 3880 | switch (cmd) { |
| 3881 | case BC_INCREFS: |
| 3882 | debug_string = "IncRefs"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3883 | break; |
| 3884 | case BC_ACQUIRE: |
| 3885 | debug_string = "Acquire"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3886 | break; |
| 3887 | case BC_RELEASE: |
| 3888 | debug_string = "Release"; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3889 | break; |
| 3890 | case BC_DECREFS: |
| 3891 | default: |
| 3892 | debug_string = "DecRefs"; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3893 | break; |
| 3894 | } |
| 3895 | if (ret) { |
| 3896 | binder_user_error("%d:%d %s %d refcount change on invalid ref %d ret %d\n", |
| 3897 | proc->pid, thread->pid, debug_string, |
| 3898 | strong, target, ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3899 | break; |
| 3900 | } |
| 3901 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 3902 | "%d:%d %s ref %d desc %d s %d w %d\n", |
| 3903 | proc->pid, thread->pid, debug_string, |
| 3904 | rdata.debug_id, rdata.desc, rdata.strong, |
| 3905 | rdata.weak); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3906 | break; |
| 3907 | } |
| 3908 | case BC_INCREFS_DONE: |
| 3909 | case BC_ACQUIRE_DONE: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3910 | binder_uintptr_t node_ptr; |
| 3911 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3912 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3913 | bool free_node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3914 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3915 | if (get_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3916 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3917 | ptr += sizeof(binder_uintptr_t); |
| 3918 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3919 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3920 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3921 | node = binder_get_node(proc, node_ptr); |
| 3922 | if (node == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3923 | binder_user_error("%d:%d %s u%016llx no match\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3924 | proc->pid, thread->pid, |
| 3925 | cmd == BC_INCREFS_DONE ? |
| 3926 | "BC_INCREFS_DONE" : |
| 3927 | "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3928 | (u64)node_ptr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3929 | break; |
| 3930 | } |
| 3931 | if (cookie != node->cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3932 | 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] | 3933 | proc->pid, thread->pid, |
| 3934 | cmd == BC_INCREFS_DONE ? |
| 3935 | "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3936 | (u64)node_ptr, node->debug_id, |
| 3937 | (u64)cookie, (u64)node->cookie); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3938 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3939 | break; |
| 3940 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3941 | binder_node_inner_lock(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3942 | if (cmd == BC_ACQUIRE_DONE) { |
| 3943 | if (node->pending_strong_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3944 | 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] | 3945 | proc->pid, thread->pid, |
| 3946 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3947 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3948 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3949 | break; |
| 3950 | } |
| 3951 | node->pending_strong_ref = 0; |
| 3952 | } else { |
| 3953 | if (node->pending_weak_ref == 0) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3954 | 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] | 3955 | proc->pid, thread->pid, |
| 3956 | node->debug_id); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3957 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3958 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3959 | break; |
| 3960 | } |
| 3961 | node->pending_weak_ref = 0; |
| 3962 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3963 | free_node = binder_dec_node_nilocked(node, |
| 3964 | cmd == BC_ACQUIRE_DONE, 0); |
| 3965 | WARN_ON(free_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3966 | binder_debug(BINDER_DEBUG_USER_REFS, |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3967 | "%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] | 3968 | proc->pid, thread->pid, |
| 3969 | cmd == BC_INCREFS_DONE ? "BC_INCREFS_DONE" : "BC_ACQUIRE_DONE", |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3970 | node->debug_id, node->local_strong_refs, |
| 3971 | node->local_weak_refs, node->tmp_refs); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 3972 | binder_node_inner_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 3973 | binder_put_node(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3974 | break; |
| 3975 | } |
| 3976 | case BC_ATTEMPT_ACQUIRE: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3977 | pr_err("BC_ATTEMPT_ACQUIRE not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3978 | return -EINVAL; |
| 3979 | case BC_ACQUIRE_RESULT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 3980 | pr_err("BC_ACQUIRE_RESULT not supported\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3981 | return -EINVAL; |
| 3982 | |
| 3983 | case BC_FREE_BUFFER: { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3984 | binder_uintptr_t data_ptr; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3985 | struct binder_buffer *buffer; |
| 3986 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3987 | if (get_user(data_ptr, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3988 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 3989 | ptr += sizeof(binder_uintptr_t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 3990 | |
Todd Kjos | 53d311cf | 2017-06-29 12:01:51 -0700 | [diff] [blame] | 3991 | buffer = binder_alloc_prepare_to_free(&proc->alloc, |
| 3992 | data_ptr); |
Todd Kjos | fd6cc33 | 2018-11-06 15:55:32 -0800 | [diff] [blame] | 3993 | if (IS_ERR_OR_NULL(buffer)) { |
| 3994 | if (PTR_ERR(buffer) == -EPERM) { |
| 3995 | binder_user_error( |
| 3996 | "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n", |
| 3997 | proc->pid, thread->pid, |
| 3998 | (u64)data_ptr); |
| 3999 | } else { |
| 4000 | binder_user_error( |
| 4001 | "%d:%d BC_FREE_BUFFER u%016llx no match\n", |
| 4002 | proc->pid, thread->pid, |
| 4003 | (u64)data_ptr); |
| 4004 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4005 | break; |
| 4006 | } |
| 4007 | binder_debug(BINDER_DEBUG_FREE_BUFFER, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4008 | "%d:%d BC_FREE_BUFFER u%016llx found buffer %d for %s transaction\n", |
| 4009 | proc->pid, thread->pid, (u64)data_ptr, |
| 4010 | buffer->debug_id, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4011 | buffer->transaction ? "active" : "finished"); |
| 4012 | |
Todd Kjos | a4a3c07 | 2019-06-12 13:29:27 -0700 | [diff] [blame] | 4013 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4014 | if (buffer->transaction) { |
| 4015 | buffer->transaction->buffer = NULL; |
| 4016 | buffer->transaction = NULL; |
| 4017 | } |
Todd Kjos | a4a3c07 | 2019-06-12 13:29:27 -0700 | [diff] [blame] | 4018 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4019 | if (buffer->async_transaction && buffer->target_node) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4020 | struct binder_node *buf_node; |
| 4021 | struct binder_work *w; |
| 4022 | |
| 4023 | buf_node = buffer->target_node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4024 | binder_node_inner_lock(buf_node); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4025 | BUG_ON(!buf_node->has_async_transaction); |
| 4026 | BUG_ON(buf_node->proc != proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4027 | w = binder_dequeue_work_head_ilocked( |
| 4028 | &buf_node->async_todo); |
Martijn Coenen | 3a6430c | 2017-08-31 10:04:29 +0200 | [diff] [blame] | 4029 | if (!w) { |
Gustavo A. R. Silva | 10340bf | 2018-01-23 12:04:27 -0600 | [diff] [blame] | 4030 | buf_node->has_async_transaction = false; |
Martijn Coenen | 3a6430c | 2017-08-31 10:04:29 +0200 | [diff] [blame] | 4031 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4032 | binder_enqueue_work_ilocked( |
Martijn Coenen | 3a6430c | 2017-08-31 10:04:29 +0200 | [diff] [blame] | 4033 | w, &proc->todo); |
| 4034 | binder_wakeup_proc_ilocked(proc); |
| 4035 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4036 | binder_node_inner_unlock(buf_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4037 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4038 | trace_binder_transaction_buffer_release(buffer); |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 4039 | binder_transaction_buffer_release(proc, buffer, 0, false); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 4040 | binder_alloc_free_buf(&proc->alloc, buffer); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4041 | break; |
| 4042 | } |
| 4043 | |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 4044 | case BC_TRANSACTION_SG: |
| 4045 | case BC_REPLY_SG: { |
| 4046 | struct binder_transaction_data_sg tr; |
| 4047 | |
| 4048 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 4049 | return -EFAULT; |
| 4050 | ptr += sizeof(tr); |
| 4051 | binder_transaction(proc, thread, &tr.transaction_data, |
| 4052 | cmd == BC_REPLY_SG, tr.buffers_size); |
| 4053 | break; |
| 4054 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4055 | case BC_TRANSACTION: |
| 4056 | case BC_REPLY: { |
| 4057 | struct binder_transaction_data tr; |
| 4058 | |
| 4059 | if (copy_from_user(&tr, ptr, sizeof(tr))) |
| 4060 | return -EFAULT; |
| 4061 | ptr += sizeof(tr); |
Martijn Coenen | 4bfac80 | 2017-02-03 14:40:50 -0800 | [diff] [blame] | 4062 | binder_transaction(proc, thread, &tr, |
| 4063 | cmd == BC_REPLY, 0); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4064 | break; |
| 4065 | } |
| 4066 | |
| 4067 | case BC_REGISTER_LOOPER: |
| 4068 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4069 | "%d:%d BC_REGISTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4070 | proc->pid, thread->pid); |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4071 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4072 | if (thread->looper & BINDER_LOOPER_STATE_ENTERED) { |
| 4073 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4074 | 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] | 4075 | proc->pid, thread->pid); |
| 4076 | } else if (proc->requested_threads == 0) { |
| 4077 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4078 | 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] | 4079 | proc->pid, thread->pid); |
| 4080 | } else { |
| 4081 | proc->requested_threads--; |
| 4082 | proc->requested_threads_started++; |
| 4083 | } |
| 4084 | thread->looper |= BINDER_LOOPER_STATE_REGISTERED; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4085 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4086 | break; |
| 4087 | case BC_ENTER_LOOPER: |
| 4088 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4089 | "%d:%d BC_ENTER_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4090 | proc->pid, thread->pid); |
| 4091 | if (thread->looper & BINDER_LOOPER_STATE_REGISTERED) { |
| 4092 | thread->looper |= BINDER_LOOPER_STATE_INVALID; |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4093 | 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] | 4094 | proc->pid, thread->pid); |
| 4095 | } |
| 4096 | thread->looper |= BINDER_LOOPER_STATE_ENTERED; |
| 4097 | break; |
| 4098 | case BC_EXIT_LOOPER: |
| 4099 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4100 | "%d:%d BC_EXIT_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4101 | proc->pid, thread->pid); |
| 4102 | thread->looper |= BINDER_LOOPER_STATE_EXITED; |
| 4103 | break; |
| 4104 | |
| 4105 | case BC_REQUEST_DEATH_NOTIFICATION: |
| 4106 | case BC_CLEAR_DEATH_NOTIFICATION: { |
| 4107 | uint32_t target; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4108 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4109 | struct binder_ref *ref; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4110 | struct binder_ref_death *death = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4111 | |
| 4112 | if (get_user(target, (uint32_t __user *)ptr)) |
| 4113 | return -EFAULT; |
| 4114 | ptr += sizeof(uint32_t); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4115 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4116 | return -EFAULT; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4117 | ptr += sizeof(binder_uintptr_t); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4118 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 4119 | /* |
| 4120 | * Allocate memory for death notification |
| 4121 | * before taking lock |
| 4122 | */ |
| 4123 | death = kzalloc(sizeof(*death), GFP_KERNEL); |
| 4124 | if (death == NULL) { |
| 4125 | WARN_ON(thread->return_error.cmd != |
| 4126 | BR_OK); |
| 4127 | thread->return_error.cmd = BR_ERROR; |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 4128 | binder_enqueue_thread_work( |
| 4129 | thread, |
| 4130 | &thread->return_error.work); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4131 | binder_debug( |
| 4132 | BINDER_DEBUG_FAILED_TRANSACTION, |
| 4133 | "%d:%d BC_REQUEST_DEATH_NOTIFICATION failed\n", |
| 4134 | proc->pid, thread->pid); |
| 4135 | break; |
| 4136 | } |
| 4137 | } |
| 4138 | binder_proc_lock(proc); |
| 4139 | ref = binder_get_ref_olocked(proc, target, false); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4140 | if (ref == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4141 | binder_user_error("%d:%d %s invalid ref %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4142 | proc->pid, thread->pid, |
| 4143 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 4144 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 4145 | "BC_CLEAR_DEATH_NOTIFICATION", |
| 4146 | target); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4147 | binder_proc_unlock(proc); |
| 4148 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4149 | break; |
| 4150 | } |
| 4151 | |
| 4152 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4153 | "%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] | 4154 | proc->pid, thread->pid, |
| 4155 | cmd == BC_REQUEST_DEATH_NOTIFICATION ? |
| 4156 | "BC_REQUEST_DEATH_NOTIFICATION" : |
| 4157 | "BC_CLEAR_DEATH_NOTIFICATION", |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 4158 | (u64)cookie, ref->data.debug_id, |
| 4159 | ref->data.desc, ref->data.strong, |
| 4160 | ref->data.weak, ref->node->debug_id); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4161 | |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4162 | binder_node_lock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4163 | if (cmd == BC_REQUEST_DEATH_NOTIFICATION) { |
| 4164 | if (ref->death) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4165 | 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] | 4166 | proc->pid, thread->pid); |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4167 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4168 | binder_proc_unlock(proc); |
| 4169 | kfree(death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4170 | break; |
| 4171 | } |
| 4172 | binder_stats_created(BINDER_STAT_DEATH); |
| 4173 | INIT_LIST_HEAD(&death->work.entry); |
| 4174 | death->cookie = cookie; |
| 4175 | ref->death = death; |
| 4176 | if (ref->node->proc == NULL) { |
| 4177 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
Martijn Coenen | bb74562 | 2017-08-31 10:04:28 +0200 | [diff] [blame] | 4178 | |
| 4179 | binder_inner_proc_lock(proc); |
| 4180 | binder_enqueue_work_ilocked( |
| 4181 | &ref->death->work, &proc->todo); |
| 4182 | binder_wakeup_proc_ilocked(proc); |
| 4183 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4184 | } |
| 4185 | } else { |
| 4186 | if (ref->death == NULL) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4187 | 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] | 4188 | proc->pid, thread->pid); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4189 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4190 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4191 | break; |
| 4192 | } |
| 4193 | death = ref->death; |
| 4194 | if (death->cookie != cookie) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4195 | 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] | 4196 | proc->pid, thread->pid, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4197 | (u64)death->cookie, |
| 4198 | (u64)cookie); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4199 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4200 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4201 | break; |
| 4202 | } |
| 4203 | ref->death = NULL; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4204 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4205 | if (list_empty(&death->work.entry)) { |
| 4206 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4207 | if (thread->looper & |
| 4208 | (BINDER_LOOPER_STATE_REGISTERED | |
| 4209 | BINDER_LOOPER_STATE_ENTERED)) |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 4210 | binder_enqueue_thread_work_ilocked( |
| 4211 | thread, |
| 4212 | &death->work); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4213 | else { |
| 4214 | binder_enqueue_work_ilocked( |
| 4215 | &death->work, |
| 4216 | &proc->todo); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4217 | binder_wakeup_proc_ilocked( |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 4218 | proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4219 | } |
| 4220 | } else { |
| 4221 | BUG_ON(death->work.type != BINDER_WORK_DEAD_BINDER); |
| 4222 | death->work.type = BINDER_WORK_DEAD_BINDER_AND_CLEAR; |
| 4223 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4224 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4225 | } |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4226 | binder_node_unlock(ref->node); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 4227 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4228 | } break; |
| 4229 | case BC_DEAD_BINDER_DONE: { |
| 4230 | struct binder_work *w; |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4231 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4232 | struct binder_ref_death *death = NULL; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4233 | |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4234 | if (get_user(cookie, (binder_uintptr_t __user *)ptr)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4235 | return -EFAULT; |
| 4236 | |
Lisa Du | 7a64cd8 | 2016-02-17 09:32:52 +0800 | [diff] [blame] | 4237 | ptr += sizeof(cookie); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4238 | binder_inner_proc_lock(proc); |
| 4239 | list_for_each_entry(w, &proc->delivered_death, |
| 4240 | entry) { |
| 4241 | struct binder_ref_death *tmp_death = |
| 4242 | container_of(w, |
| 4243 | struct binder_ref_death, |
| 4244 | work); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4245 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4246 | if (tmp_death->cookie == cookie) { |
| 4247 | death = tmp_death; |
| 4248 | break; |
| 4249 | } |
| 4250 | } |
| 4251 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Todd Kjos | b46af09 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 4252 | "%d:%d BC_DEAD_BINDER_DONE %016llx found %pK\n", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4253 | proc->pid, thread->pid, (u64)cookie, |
| 4254 | death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4255 | if (death == NULL) { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4256 | binder_user_error("%d:%d BC_DEAD_BINDER_DONE %016llx not found\n", |
| 4257 | proc->pid, thread->pid, (u64)cookie); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4258 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4259 | break; |
| 4260 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4261 | binder_dequeue_work_ilocked(&death->work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4262 | if (death->work.type == BINDER_WORK_DEAD_BINDER_AND_CLEAR) { |
| 4263 | death->work.type = BINDER_WORK_CLEAR_DEATH_NOTIFICATION; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4264 | if (thread->looper & |
| 4265 | (BINDER_LOOPER_STATE_REGISTERED | |
| 4266 | BINDER_LOOPER_STATE_ENTERED)) |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 4267 | binder_enqueue_thread_work_ilocked( |
| 4268 | thread, &death->work); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4269 | else { |
| 4270 | binder_enqueue_work_ilocked( |
| 4271 | &death->work, |
| 4272 | &proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 4273 | binder_wakeup_proc_ilocked(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4274 | } |
| 4275 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4276 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4277 | } break; |
| 4278 | |
| 4279 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4280 | pr_err("%d:%d unknown command %d\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4281 | proc->pid, thread->pid, cmd); |
| 4282 | return -EINVAL; |
| 4283 | } |
| 4284 | *consumed = ptr - buffer; |
| 4285 | } |
| 4286 | return 0; |
| 4287 | } |
| 4288 | |
Bojan Prtvar | fb07ebc | 2013-09-02 08:18:40 +0200 | [diff] [blame] | 4289 | static void binder_stat_br(struct binder_proc *proc, |
| 4290 | struct binder_thread *thread, uint32_t cmd) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4291 | { |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4292 | trace_binder_return(cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4293 | if (_IOC_NR(cmd) < ARRAY_SIZE(binder_stats.br)) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 4294 | atomic_inc(&binder_stats.br[_IOC_NR(cmd)]); |
| 4295 | atomic_inc(&proc->stats.br[_IOC_NR(cmd)]); |
| 4296 | atomic_inc(&thread->stats.br[_IOC_NR(cmd)]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4297 | } |
| 4298 | } |
| 4299 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4300 | static int binder_put_node_cmd(struct binder_proc *proc, |
| 4301 | struct binder_thread *thread, |
| 4302 | void __user **ptrp, |
| 4303 | binder_uintptr_t node_ptr, |
| 4304 | binder_uintptr_t node_cookie, |
| 4305 | int node_debug_id, |
| 4306 | uint32_t cmd, const char *cmd_name) |
| 4307 | { |
| 4308 | void __user *ptr = *ptrp; |
| 4309 | |
| 4310 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4311 | return -EFAULT; |
| 4312 | ptr += sizeof(uint32_t); |
| 4313 | |
| 4314 | if (put_user(node_ptr, (binder_uintptr_t __user *)ptr)) |
| 4315 | return -EFAULT; |
| 4316 | ptr += sizeof(binder_uintptr_t); |
| 4317 | |
| 4318 | if (put_user(node_cookie, (binder_uintptr_t __user *)ptr)) |
| 4319 | return -EFAULT; |
| 4320 | ptr += sizeof(binder_uintptr_t); |
| 4321 | |
| 4322 | binder_stat_br(proc, thread, cmd); |
| 4323 | binder_debug(BINDER_DEBUG_USER_REFS, "%d:%d %s %d u%016llx c%016llx\n", |
| 4324 | proc->pid, thread->pid, cmd_name, node_debug_id, |
| 4325 | (u64)node_ptr, (u64)node_cookie); |
| 4326 | |
| 4327 | *ptrp = ptr; |
| 4328 | return 0; |
| 4329 | } |
| 4330 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4331 | static int binder_wait_for_work(struct binder_thread *thread, |
| 4332 | bool do_proc_work) |
| 4333 | { |
| 4334 | DEFINE_WAIT(wait); |
| 4335 | struct binder_proc *proc = thread->proc; |
| 4336 | int ret = 0; |
| 4337 | |
| 4338 | freezer_do_not_count(); |
| 4339 | binder_inner_proc_lock(proc); |
| 4340 | for (;;) { |
| 4341 | prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE); |
| 4342 | if (binder_has_work_ilocked(thread, do_proc_work)) |
| 4343 | break; |
| 4344 | if (do_proc_work) |
| 4345 | list_add(&thread->waiting_thread_node, |
| 4346 | &proc->waiting_threads); |
| 4347 | binder_inner_proc_unlock(proc); |
| 4348 | schedule(); |
| 4349 | binder_inner_proc_lock(proc); |
| 4350 | list_del_init(&thread->waiting_thread_node); |
| 4351 | if (signal_pending(current)) { |
Marco Ballesio | c09a580 | 2020-09-16 09:46:36 -0700 | [diff] [blame] | 4352 | ret = -EINTR; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4353 | break; |
| 4354 | } |
| 4355 | } |
| 4356 | finish_wait(&thread->wait, &wait); |
| 4357 | binder_inner_proc_unlock(proc); |
| 4358 | freezer_count(); |
| 4359 | |
| 4360 | return ret; |
| 4361 | } |
| 4362 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4363 | static int binder_thread_read(struct binder_proc *proc, |
| 4364 | struct binder_thread *thread, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4365 | binder_uintptr_t binder_buffer, size_t size, |
| 4366 | binder_size_t *consumed, int non_block) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4367 | { |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4368 | void __user *buffer = (void __user *)(uintptr_t)binder_buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4369 | void __user *ptr = buffer + *consumed; |
| 4370 | void __user *end = buffer + size; |
| 4371 | |
| 4372 | int ret = 0; |
| 4373 | int wait_for_proc_work; |
| 4374 | |
| 4375 | if (*consumed == 0) { |
| 4376 | if (put_user(BR_NOOP, (uint32_t __user *)ptr)) |
| 4377 | return -EFAULT; |
| 4378 | ptr += sizeof(uint32_t); |
| 4379 | } |
| 4380 | |
| 4381 | retry: |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4382 | binder_inner_proc_lock(proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4383 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4384 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4385 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4386 | thread->looper |= BINDER_LOOPER_STATE_WAITING; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4387 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4388 | trace_binder_wait_for_work(wait_for_proc_work, |
| 4389 | !!thread->transaction_stack, |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4390 | !binder_worklist_empty(proc, &thread->todo)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4391 | if (wait_for_proc_work) { |
| 4392 | if (!(thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4393 | BINDER_LOOPER_STATE_ENTERED))) { |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4394 | 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] | 4395 | proc->pid, thread->pid, thread->looper); |
| 4396 | wait_event_interruptible(binder_user_error_wait, |
| 4397 | binder_stop_on_user_error < 2); |
| 4398 | } |
Martijn Coenen | 22b061b | 2017-05-26 10:48:56 -0700 | [diff] [blame] | 4399 | binder_restore_priority(current, proc->default_priority); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4400 | } |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4401 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4402 | if (non_block) { |
| 4403 | if (!binder_has_work(thread, wait_for_proc_work)) |
| 4404 | ret = -EAGAIN; |
| 4405 | } else { |
| 4406 | ret = binder_wait_for_work(thread, wait_for_proc_work); |
| 4407 | } |
| 4408 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4409 | thread->looper &= ~BINDER_LOOPER_STATE_WAITING; |
| 4410 | |
| 4411 | if (ret) |
| 4412 | return ret; |
| 4413 | |
| 4414 | while (1) { |
| 4415 | uint32_t cmd; |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4416 | struct binder_transaction_data_secctx tr; |
| 4417 | struct binder_transaction_data *trd = &tr.transaction_data; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4418 | struct binder_work *w = NULL; |
| 4419 | struct list_head *list = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4420 | struct binder_transaction *t = NULL; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4421 | struct binder_thread *t_from; |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4422 | size_t trsize = sizeof(*trd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4423 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4424 | binder_inner_proc_lock(proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4425 | if (!binder_worklist_empty_ilocked(&thread->todo)) |
| 4426 | list = &thread->todo; |
| 4427 | else if (!binder_worklist_empty_ilocked(&proc->todo) && |
| 4428 | wait_for_proc_work) |
| 4429 | list = &proc->todo; |
| 4430 | else { |
| 4431 | binder_inner_proc_unlock(proc); |
| 4432 | |
Dmitry Voytik | 395262a | 2014-09-08 18:16:34 +0400 | [diff] [blame] | 4433 | /* no data added */ |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 4434 | if (ptr - buffer == 4 && !thread->looper_need_return) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4435 | goto retry; |
| 4436 | break; |
| 4437 | } |
| 4438 | |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4439 | if (end - ptr < sizeof(tr) + 4) { |
| 4440 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4441 | break; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4442 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4443 | w = binder_dequeue_work_head_ilocked(list); |
Martijn Coenen | 8e65ef2 | 2017-10-19 15:04:46 +0200 | [diff] [blame] | 4444 | if (binder_worklist_empty_ilocked(&thread->todo)) |
| 4445 | thread->process_todo = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4446 | |
| 4447 | switch (w->type) { |
| 4448 | case BINDER_WORK_TRANSACTION: { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4449 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4450 | t = container_of(w, struct binder_transaction, work); |
| 4451 | } break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4452 | case BINDER_WORK_RETURN_ERROR: { |
| 4453 | struct binder_error *e = container_of( |
| 4454 | w, struct binder_error, work); |
| 4455 | |
| 4456 | WARN_ON(e->cmd == BR_OK); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4457 | binder_inner_proc_unlock(proc); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4458 | if (put_user(e->cmd, (uint32_t __user *)ptr)) |
| 4459 | return -EFAULT; |
宋金时 | 30814a1 | 2018-05-10 02:05:03 +0000 | [diff] [blame] | 4460 | cmd = e->cmd; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4461 | e->cmd = BR_OK; |
| 4462 | ptr += sizeof(uint32_t); |
| 4463 | |
Todd Kjos | 4f9adc8 | 2017-08-08 15:48:36 -0700 | [diff] [blame] | 4464 | binder_stat_br(proc, thread, e->cmd); |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4465 | } break; |
Hang Lu | 4dbc168 | 2021-04-09 17:40:46 +0800 | [diff] [blame] | 4466 | case BINDER_WORK_TRANSACTION_COMPLETE: |
Li Li | af1f984 | 2022-11-23 12:16:54 -0800 | [diff] [blame] | 4467 | case BINDER_WORK_TRANSACTION_PENDING: |
Hang Lu | 4dbc168 | 2021-04-09 17:40:46 +0800 | [diff] [blame] | 4468 | case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT: { |
| 4469 | if (proc->oneway_spam_detection_enabled && |
| 4470 | w->type == BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT) |
| 4471 | cmd = BR_ONEWAY_SPAM_SUSPECT; |
Li Li | af1f984 | 2022-11-23 12:16:54 -0800 | [diff] [blame] | 4472 | else if (w->type == BINDER_WORK_TRANSACTION_PENDING) |
| 4473 | cmd = BR_TRANSACTION_PENDING_FROZEN; |
Hang Lu | 4dbc168 | 2021-04-09 17:40:46 +0800 | [diff] [blame] | 4474 | else |
| 4475 | cmd = BR_TRANSACTION_COMPLETE; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4476 | binder_inner_proc_unlock(proc); |
Todd Kjos | 67f6f4a | 2019-06-21 10:54:15 -0700 | [diff] [blame] | 4477 | kfree(w); |
| 4478 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4479 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4480 | return -EFAULT; |
| 4481 | ptr += sizeof(uint32_t); |
| 4482 | |
| 4483 | binder_stat_br(proc, thread, cmd); |
| 4484 | binder_debug(BINDER_DEBUG_TRANSACTION_COMPLETE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4485 | "%d:%d BR_TRANSACTION_COMPLETE\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4486 | proc->pid, thread->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4487 | } break; |
| 4488 | case BINDER_WORK_NODE: { |
| 4489 | struct binder_node *node = container_of(w, struct binder_node, work); |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4490 | int strong, weak; |
| 4491 | binder_uintptr_t node_ptr = node->ptr; |
| 4492 | binder_uintptr_t node_cookie = node->cookie; |
| 4493 | int node_debug_id = node->debug_id; |
| 4494 | int has_weak_ref; |
| 4495 | int has_strong_ref; |
| 4496 | void __user *orig_ptr = ptr; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4497 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4498 | BUG_ON(proc != node->proc); |
| 4499 | strong = node->internal_strong_refs || |
| 4500 | node->local_strong_refs; |
| 4501 | weak = !hlist_empty(&node->refs) || |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 4502 | node->local_weak_refs || |
| 4503 | node->tmp_refs || strong; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4504 | has_strong_ref = node->has_strong_ref; |
| 4505 | has_weak_ref = node->has_weak_ref; |
| 4506 | |
| 4507 | if (weak && !has_weak_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4508 | node->has_weak_ref = 1; |
| 4509 | node->pending_weak_ref = 1; |
| 4510 | node->local_weak_refs++; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4511 | } |
| 4512 | if (strong && !has_strong_ref) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4513 | node->has_strong_ref = 1; |
| 4514 | node->pending_strong_ref = 1; |
| 4515 | node->local_strong_refs++; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4516 | } |
| 4517 | if (!strong && has_strong_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4518 | node->has_strong_ref = 0; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4519 | if (!weak && has_weak_ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4520 | node->has_weak_ref = 0; |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4521 | if (!weak && !strong) { |
| 4522 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4523 | "%d:%d node %d u%016llx c%016llx deleted\n", |
| 4524 | proc->pid, thread->pid, |
| 4525 | node_debug_id, |
| 4526 | (u64)node_ptr, |
| 4527 | (u64)node_cookie); |
| 4528 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4529 | binder_inner_proc_unlock(proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 4530 | binder_node_lock(node); |
| 4531 | /* |
| 4532 | * Acquire the node lock before freeing the |
| 4533 | * node to serialize with other threads that |
| 4534 | * may have been holding the node lock while |
| 4535 | * decrementing this node (avoids race where |
| 4536 | * this thread frees while the other thread |
| 4537 | * is unlocking the node after the final |
| 4538 | * decrement) |
| 4539 | */ |
| 4540 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4541 | binder_free_node(node); |
| 4542 | } else |
| 4543 | binder_inner_proc_unlock(proc); |
| 4544 | |
Todd Kjos | 26b47d8 | 2017-06-29 12:01:47 -0700 | [diff] [blame] | 4545 | if (weak && !has_weak_ref) |
| 4546 | ret = binder_put_node_cmd( |
| 4547 | proc, thread, &ptr, node_ptr, |
| 4548 | node_cookie, node_debug_id, |
| 4549 | BR_INCREFS, "BR_INCREFS"); |
| 4550 | if (!ret && strong && !has_strong_ref) |
| 4551 | ret = binder_put_node_cmd( |
| 4552 | proc, thread, &ptr, node_ptr, |
| 4553 | node_cookie, node_debug_id, |
| 4554 | BR_ACQUIRE, "BR_ACQUIRE"); |
| 4555 | if (!ret && !strong && has_strong_ref) |
| 4556 | ret = binder_put_node_cmd( |
| 4557 | proc, thread, &ptr, node_ptr, |
| 4558 | node_cookie, node_debug_id, |
| 4559 | BR_RELEASE, "BR_RELEASE"); |
| 4560 | if (!ret && !weak && has_weak_ref) |
| 4561 | ret = binder_put_node_cmd( |
| 4562 | proc, thread, &ptr, node_ptr, |
| 4563 | node_cookie, node_debug_id, |
| 4564 | BR_DECREFS, "BR_DECREFS"); |
| 4565 | if (orig_ptr == ptr) |
| 4566 | binder_debug(BINDER_DEBUG_INTERNAL_REFS, |
| 4567 | "%d:%d node %d u%016llx c%016llx state unchanged\n", |
| 4568 | proc->pid, thread->pid, |
| 4569 | node_debug_id, |
| 4570 | (u64)node_ptr, |
| 4571 | (u64)node_cookie); |
| 4572 | if (ret) |
| 4573 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4574 | } break; |
| 4575 | case BINDER_WORK_DEAD_BINDER: |
| 4576 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4577 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4578 | struct binder_ref_death *death; |
| 4579 | uint32_t cmd; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4580 | binder_uintptr_t cookie; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4581 | |
| 4582 | death = container_of(w, struct binder_ref_death, work); |
| 4583 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) |
| 4584 | cmd = BR_CLEAR_DEATH_NOTIFICATION_DONE; |
| 4585 | else |
| 4586 | cmd = BR_DEAD_BINDER; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4587 | cookie = death->cookie; |
| 4588 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4589 | binder_debug(BINDER_DEBUG_DEATH_NOTIFICATION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4590 | "%d:%d %s %016llx\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4591 | proc->pid, thread->pid, |
| 4592 | cmd == BR_DEAD_BINDER ? |
| 4593 | "BR_DEAD_BINDER" : |
| 4594 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4595 | (u64)cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4596 | if (w->type == BINDER_WORK_CLEAR_DEATH_NOTIFICATION) { |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4597 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4598 | kfree(death); |
| 4599 | binder_stats_deleted(BINDER_STAT_DEATH); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4600 | } else { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4601 | binder_enqueue_work_ilocked( |
| 4602 | w, &proc->delivered_death); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 4603 | binder_inner_proc_unlock(proc); |
| 4604 | } |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 4605 | if (put_user(cmd, (uint32_t __user *)ptr)) |
| 4606 | return -EFAULT; |
| 4607 | ptr += sizeof(uint32_t); |
| 4608 | if (put_user(cookie, |
| 4609 | (binder_uintptr_t __user *)ptr)) |
| 4610 | return -EFAULT; |
| 4611 | ptr += sizeof(binder_uintptr_t); |
| 4612 | binder_stat_br(proc, thread, cmd); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4613 | if (cmd == BR_DEAD_BINDER) |
| 4614 | goto done; /* DEAD_BINDER notifications can cause transactions */ |
| 4615 | } break; |
| 4616 | } |
| 4617 | |
| 4618 | if (!t) |
| 4619 | continue; |
| 4620 | |
| 4621 | BUG_ON(t->buffer == NULL); |
| 4622 | if (t->buffer->target_node) { |
| 4623 | struct binder_node *target_node = t->buffer->target_node; |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4624 | struct binder_priority node_prio; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4625 | |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4626 | trd->target.ptr = target_node->ptr; |
| 4627 | trd->cookie = target_node->cookie; |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4628 | node_prio.sched_policy = target_node->sched_policy; |
| 4629 | node_prio.prio = target_node->min_priority; |
Martijn Coenen | fb92c34 | 2017-06-23 10:13:43 -0700 | [diff] [blame] | 4630 | binder_transaction_priority(current, t, node_prio, |
| 4631 | target_node->inherit_rt); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4632 | cmd = BR_TRANSACTION; |
| 4633 | } else { |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4634 | trd->target.ptr = 0; |
| 4635 | trd->cookie = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4636 | cmd = BR_REPLY; |
| 4637 | } |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4638 | trd->code = t->code; |
| 4639 | trd->flags = t->flags; |
| 4640 | trd->sender_euid = from_kuid(current_user_ns(), t->sender_euid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4641 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4642 | t_from = binder_get_txn_from(t); |
| 4643 | if (t_from) { |
| 4644 | struct task_struct *sender = t_from->proc->tsk; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4645 | |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4646 | trd->sender_pid = |
| 4647 | task_tgid_nr_ns(sender, |
| 4648 | task_active_pid_ns(current)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4649 | } else { |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4650 | trd->sender_pid = 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4651 | } |
| 4652 | |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4653 | trd->data_size = t->buffer->data_size; |
| 4654 | trd->offsets_size = t->buffer->offsets_size; |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 4655 | trd->data.ptr.buffer = (uintptr_t)t->buffer->user_data; |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4656 | trd->data.ptr.offsets = trd->data.ptr.buffer + |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4657 | ALIGN(t->buffer->data_size, |
| 4658 | sizeof(void *)); |
| 4659 | |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4660 | tr.secctx = t->security_ctx; |
| 4661 | if (t->security_ctx) { |
| 4662 | cmd = BR_TRANSACTION_SEC_CTX; |
| 4663 | trsize = sizeof(tr); |
| 4664 | } |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4665 | if (put_user(cmd, (uint32_t __user *)ptr)) { |
| 4666 | if (t_from) |
| 4667 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | fd82249 | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4668 | |
| 4669 | binder_cleanup_transaction(t, "put_user failed", |
| 4670 | BR_FAILED_REPLY); |
| 4671 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4672 | return -EFAULT; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4673 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4674 | ptr += sizeof(uint32_t); |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4675 | if (copy_to_user(ptr, &tr, trsize)) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4676 | if (t_from) |
| 4677 | binder_thread_dec_tmpref(t_from); |
Martijn Coenen | fd82249 | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4678 | |
| 4679 | binder_cleanup_transaction(t, "copy_to_user failed", |
| 4680 | BR_FAILED_REPLY); |
| 4681 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4682 | return -EFAULT; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4683 | } |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4684 | ptr += trsize; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4685 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4686 | trace_binder_transaction_received(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4687 | binder_stat_br(proc, thread, cmd); |
| 4688 | binder_debug(BINDER_DEBUG_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4689 | "%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] | 4690 | proc->pid, thread->pid, |
| 4691 | (cmd == BR_TRANSACTION) ? "BR_TRANSACTION" : |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4692 | (cmd == BR_TRANSACTION_SEC_CTX) ? |
| 4693 | "BR_TRANSACTION_SEC_CTX" : "BR_REPLY", |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4694 | t->debug_id, t_from ? t_from->proc->pid : 0, |
| 4695 | t_from ? t_from->pid : 0, cmd, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4696 | t->buffer->data_size, t->buffer->offsets_size, |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4697 | (u64)trd->data.ptr.buffer, |
| 4698 | (u64)trd->data.ptr.offsets); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4699 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4700 | if (t_from) |
| 4701 | binder_thread_dec_tmpref(t_from); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4702 | t->buffer->allow_user_free = 1; |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 4703 | if (cmd != BR_REPLY && !(t->flags & TF_ONE_WAY)) { |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4704 | binder_inner_proc_lock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4705 | t->to_parent = thread->transaction_stack; |
| 4706 | t->to_thread = thread; |
| 4707 | thread->transaction_stack = t; |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4708 | binder_inner_proc_unlock(thread->proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4709 | } else { |
Todd Kjos | b6d282c | 2017-06-29 12:01:54 -0700 | [diff] [blame] | 4710 | binder_free_transaction(t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4711 | } |
| 4712 | break; |
| 4713 | } |
| 4714 | |
| 4715 | done: |
| 4716 | |
| 4717 | *consumed = ptr - buffer; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4718 | binder_inner_proc_lock(proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4719 | if (proc->requested_threads == 0 && |
| 4720 | list_empty(&thread->proc->waiting_threads) && |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4721 | proc->requested_threads_started < proc->max_threads && |
| 4722 | (thread->looper & (BINDER_LOOPER_STATE_REGISTERED | |
| 4723 | BINDER_LOOPER_STATE_ENTERED)) /* the user-space code fails to */ |
| 4724 | /*spawn a new thread if we leave this out */) { |
| 4725 | proc->requested_threads++; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4726 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4727 | binder_debug(BINDER_DEBUG_THREADS, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4728 | "%d:%d BR_SPAWN_LOOPER\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4729 | proc->pid, thread->pid); |
| 4730 | if (put_user(BR_SPAWN_LOOPER, (uint32_t __user *)buffer)) |
| 4731 | return -EFAULT; |
Arve Hjønnevåg | 89334ab | 2012-10-16 15:29:52 -0700 | [diff] [blame] | 4732 | binder_stat_br(proc, thread, BR_SPAWN_LOOPER); |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 4733 | } else |
| 4734 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4735 | return 0; |
| 4736 | } |
| 4737 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4738 | static void binder_release_work(struct binder_proc *proc, |
| 4739 | struct list_head *list) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4740 | { |
| 4741 | struct binder_work *w; |
Todd Kjos | be84da1 | 2020-10-09 16:24:55 -0700 | [diff] [blame] | 4742 | enum binder_work_type wtype; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 4743 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4744 | while (1) { |
Todd Kjos | be84da1 | 2020-10-09 16:24:55 -0700 | [diff] [blame] | 4745 | binder_inner_proc_lock(proc); |
| 4746 | w = binder_dequeue_work_head_ilocked(list); |
| 4747 | wtype = w ? w->type : 0; |
| 4748 | binder_inner_proc_unlock(proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4749 | if (!w) |
| 4750 | return; |
| 4751 | |
Todd Kjos | be84da1 | 2020-10-09 16:24:55 -0700 | [diff] [blame] | 4752 | switch (wtype) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4753 | case BINDER_WORK_TRANSACTION: { |
| 4754 | struct binder_transaction *t; |
| 4755 | |
| 4756 | t = container_of(w, struct binder_transaction, work); |
Martijn Coenen | fd82249 | 2017-08-24 15:23:36 +0200 | [diff] [blame] | 4757 | |
| 4758 | binder_cleanup_transaction(t, "process died.", |
| 4759 | BR_DEAD_REPLY); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4760 | } break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 4761 | case BINDER_WORK_RETURN_ERROR: { |
| 4762 | struct binder_error *e = container_of( |
| 4763 | w, struct binder_error, work); |
| 4764 | |
| 4765 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
| 4766 | "undelivered TRANSACTION_ERROR: %u\n", |
| 4767 | e->cmd); |
| 4768 | } break; |
Carlos Llamas | ea13493 | 2023-09-22 17:51:37 +0000 | [diff] [blame] | 4769 | case BINDER_WORK_TRANSACTION_PENDING: |
| 4770 | case BINDER_WORK_TRANSACTION_ONEWAY_SPAM_SUSPECT: |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4771 | case BINDER_WORK_TRANSACTION_COMPLETE: { |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4772 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4773 | "undelivered TRANSACTION_COMPLETE\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4774 | kfree(w); |
| 4775 | binder_stats_deleted(BINDER_STAT_TRANSACTION_COMPLETE); |
| 4776 | } break; |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4777 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
| 4778 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: { |
| 4779 | struct binder_ref_death *death; |
| 4780 | |
| 4781 | death = container_of(w, struct binder_ref_death, work); |
| 4782 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 4783 | "undelivered death notification, %016llx\n", |
| 4784 | (u64)death->cookie); |
Arve Hjønnevåg | 675d66b | 2012-10-16 15:29:54 -0700 | [diff] [blame] | 4785 | kfree(death); |
| 4786 | binder_stats_deleted(BINDER_STAT_DEATH); |
| 4787 | } break; |
Todd Kjos | be84da1 | 2020-10-09 16:24:55 -0700 | [diff] [blame] | 4788 | case BINDER_WORK_NODE: |
| 4789 | break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4790 | default: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4791 | pr_err("unexpected work type, %d, not freed\n", |
Todd Kjos | be84da1 | 2020-10-09 16:24:55 -0700 | [diff] [blame] | 4792 | wtype); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4793 | break; |
| 4794 | } |
| 4795 | } |
| 4796 | |
| 4797 | } |
| 4798 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4799 | static struct binder_thread *binder_get_thread_ilocked( |
| 4800 | struct binder_proc *proc, struct binder_thread *new_thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4801 | { |
| 4802 | struct binder_thread *thread = NULL; |
| 4803 | struct rb_node *parent = NULL; |
| 4804 | struct rb_node **p = &proc->threads.rb_node; |
| 4805 | |
| 4806 | while (*p) { |
| 4807 | parent = *p; |
| 4808 | thread = rb_entry(parent, struct binder_thread, rb_node); |
| 4809 | |
| 4810 | if (current->pid < thread->pid) |
| 4811 | p = &(*p)->rb_left; |
| 4812 | else if (current->pid > thread->pid) |
| 4813 | p = &(*p)->rb_right; |
| 4814 | else |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4815 | return thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4816 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4817 | if (!new_thread) |
| 4818 | return NULL; |
| 4819 | thread = new_thread; |
| 4820 | binder_stats_created(BINDER_STAT_THREAD); |
| 4821 | thread->proc = proc; |
| 4822 | thread->pid = current->pid; |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4823 | get_task_struct(current); |
| 4824 | thread->task = current; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4825 | atomic_set(&thread->tmp_ref, 0); |
| 4826 | init_waitqueue_head(&thread->wait); |
| 4827 | INIT_LIST_HEAD(&thread->todo); |
| 4828 | rb_link_node(&thread->rb_node, parent, p); |
| 4829 | rb_insert_color(&thread->rb_node, &proc->threads); |
| 4830 | thread->looper_need_return = true; |
| 4831 | thread->return_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4832 | thread->return_error.cmd = BR_OK; |
| 4833 | thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR; |
| 4834 | thread->reply_error.cmd = BR_OK; |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 4835 | thread->ee.command = BR_OK; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4836 | INIT_LIST_HEAD(&new_thread->waiting_thread_node); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4837 | return thread; |
| 4838 | } |
| 4839 | |
| 4840 | static struct binder_thread *binder_get_thread(struct binder_proc *proc) |
| 4841 | { |
| 4842 | struct binder_thread *thread; |
| 4843 | struct binder_thread *new_thread; |
| 4844 | |
| 4845 | binder_inner_proc_lock(proc); |
| 4846 | thread = binder_get_thread_ilocked(proc, NULL); |
| 4847 | binder_inner_proc_unlock(proc); |
| 4848 | if (!thread) { |
| 4849 | new_thread = kzalloc(sizeof(*thread), GFP_KERNEL); |
| 4850 | if (new_thread == NULL) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4851 | return NULL; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4852 | binder_inner_proc_lock(proc); |
| 4853 | thread = binder_get_thread_ilocked(proc, new_thread); |
| 4854 | binder_inner_proc_unlock(proc); |
| 4855 | if (thread != new_thread) |
| 4856 | kfree(new_thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4857 | } |
| 4858 | return thread; |
| 4859 | } |
| 4860 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4861 | static void binder_free_proc(struct binder_proc *proc) |
| 4862 | { |
Todd Kjos | 9cf08b8 | 2020-06-22 13:07:15 -0700 | [diff] [blame] | 4863 | struct binder_device *device; |
| 4864 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4865 | BUG_ON(!list_empty(&proc->todo)); |
| 4866 | BUG_ON(!list_empty(&proc->delivered_death)); |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 4867 | WARN_ON(proc->outstanding_txns); |
Todd Kjos | 9cf08b8 | 2020-06-22 13:07:15 -0700 | [diff] [blame] | 4868 | device = container_of(proc->context, struct binder_device, context); |
| 4869 | if (refcount_dec_and_test(&device->ref)) { |
| 4870 | kfree(proc->context->name); |
| 4871 | kfree(device); |
| 4872 | } |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4873 | binder_alloc_deferred_release(&proc->alloc); |
| 4874 | put_task_struct(proc->tsk); |
Todd Kjos | ae4a7b9 | 2021-10-12 09:56:12 -0700 | [diff] [blame] | 4875 | put_cred(proc->cred); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4876 | binder_stats_deleted(BINDER_STAT_PROC); |
| 4877 | kfree(proc); |
| 4878 | } |
| 4879 | |
| 4880 | static void binder_free_thread(struct binder_thread *thread) |
| 4881 | { |
| 4882 | BUG_ON(!list_empty(&thread->todo)); |
| 4883 | binder_stats_deleted(BINDER_STAT_THREAD); |
| 4884 | binder_proc_dec_tmpref(thread->proc); |
Martijn Coenen | 7a6edeb | 2017-06-07 10:02:12 -0700 | [diff] [blame] | 4885 | put_task_struct(thread->task); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4886 | kfree(thread); |
| 4887 | } |
| 4888 | |
| 4889 | static int binder_thread_release(struct binder_proc *proc, |
| 4890 | struct binder_thread *thread) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4891 | { |
| 4892 | struct binder_transaction *t; |
| 4893 | struct binder_transaction *send_reply = NULL; |
| 4894 | int active_transactions = 0; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4895 | struct binder_transaction *last_t = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4896 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4897 | binder_inner_proc_lock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4898 | /* |
| 4899 | * take a ref on the proc so it survives |
| 4900 | * after we remove this thread from proc->threads. |
| 4901 | * The corresponding dec is when we actually |
| 4902 | * free the thread in binder_free_thread() |
| 4903 | */ |
| 4904 | proc->tmp_ref++; |
| 4905 | /* |
| 4906 | * take a ref on this thread to ensure it |
| 4907 | * survives while we are releasing it |
| 4908 | */ |
| 4909 | atomic_inc(&thread->tmp_ref); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4910 | rb_erase(&thread->rb_node, &proc->threads); |
| 4911 | t = thread->transaction_stack; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4912 | if (t) { |
| 4913 | spin_lock(&t->lock); |
| 4914 | if (t->to_thread == thread) |
| 4915 | send_reply = t; |
| 4916 | } |
| 4917 | thread->is_dead = true; |
| 4918 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4919 | while (t) { |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4920 | last_t = t; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4921 | active_transactions++; |
| 4922 | binder_debug(BINDER_DEBUG_DEAD_TRANSACTION, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 4923 | "release %d:%d transaction %d %s, still active\n", |
| 4924 | proc->pid, thread->pid, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4925 | t->debug_id, |
| 4926 | (t->to_thread == thread) ? "in" : "out"); |
| 4927 | |
| 4928 | if (t->to_thread == thread) { |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 4929 | t->to_proc->outstanding_txns--; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4930 | t->to_proc = NULL; |
| 4931 | t->to_thread = NULL; |
| 4932 | if (t->buffer) { |
| 4933 | t->buffer->transaction = NULL; |
| 4934 | t->buffer = NULL; |
| 4935 | } |
| 4936 | t = t->to_parent; |
| 4937 | } else if (t->from == thread) { |
| 4938 | t->from = NULL; |
| 4939 | t = t->from_parent; |
| 4940 | } else |
| 4941 | BUG(); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4942 | spin_unlock(&last_t->lock); |
| 4943 | if (t) |
| 4944 | spin_lock(&t->lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4945 | } |
Martijn Coenen | 7a3cee4 | 2018-01-05 11:27:07 +0100 | [diff] [blame] | 4946 | |
| 4947 | /* |
Eric Biggers | 7bfd8e1 | 2021-12-10 16:19:25 -0800 | [diff] [blame] | 4948 | * If this thread used poll, make sure we remove the waitqueue from any |
| 4949 | * poll data structures holding it. |
Martijn Coenen | 7a3cee4 | 2018-01-05 11:27:07 +0100 | [diff] [blame] | 4950 | */ |
Eric Biggers | 7bfd8e1 | 2021-12-10 16:19:25 -0800 | [diff] [blame] | 4951 | if (thread->looper & BINDER_LOOPER_STATE_POLL) |
| 4952 | wake_up_pollfree(&thread->wait); |
Martijn Coenen | 7a3cee4 | 2018-01-05 11:27:07 +0100 | [diff] [blame] | 4953 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 4954 | binder_inner_proc_unlock(thread->proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4955 | |
Martijn Coenen | 441b5d1 | 2018-02-16 09:47:15 +0100 | [diff] [blame] | 4956 | /* |
Eric Biggers | 7bfd8e1 | 2021-12-10 16:19:25 -0800 | [diff] [blame] | 4957 | * This is needed to avoid races between wake_up_pollfree() above and |
| 4958 | * someone else removing the last entry from the queue for other reasons |
| 4959 | * (e.g. ep_remove_wait_queue() being called due to an epoll file |
| 4960 | * descriptor being closed). Such other users hold an RCU read lock, so |
| 4961 | * we can be sure they're done after we call synchronize_rcu(). |
Martijn Coenen | 441b5d1 | 2018-02-16 09:47:15 +0100 | [diff] [blame] | 4962 | */ |
| 4963 | if (thread->looper & BINDER_LOOPER_STATE_POLL) |
| 4964 | synchronize_rcu(); |
| 4965 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4966 | if (send_reply) |
| 4967 | binder_send_failed_reply(send_reply, BR_DEAD_REPLY); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 4968 | binder_release_work(proc, &thread->todo); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 4969 | binder_thread_dec_tmpref(thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4970 | return active_transactions; |
| 4971 | } |
| 4972 | |
| 4973 | static unsigned int binder_poll(struct file *filp, |
| 4974 | struct poll_table_struct *wait) |
| 4975 | { |
| 4976 | struct binder_proc *proc = filp->private_data; |
| 4977 | struct binder_thread *thread = NULL; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4978 | bool wait_for_proc_work; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4979 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4980 | thread = binder_get_thread(proc); |
Eric Biggers | 047ba51 | 2018-01-30 23:11:24 -0800 | [diff] [blame] | 4981 | if (!thread) |
| 4982 | return POLLERR; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4983 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4984 | binder_inner_proc_lock(thread->proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4985 | thread->looper |= BINDER_LOOPER_STATE_POLL; |
| 4986 | wait_for_proc_work = binder_available_for_proc_work_ilocked(thread); |
| 4987 | |
Martijn Coenen | 0b89d69 | 2017-06-29 12:02:06 -0700 | [diff] [blame] | 4988 | binder_inner_proc_unlock(thread->proc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 4989 | |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4990 | poll_wait(filp, &thread->wait, wait); |
| 4991 | |
Martijn Coenen | 66b83a4 | 2017-10-09 14:26:56 +0200 | [diff] [blame] | 4992 | if (binder_has_work(thread, wait_for_proc_work)) |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 4993 | return POLLIN; |
| 4994 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 4995 | return 0; |
| 4996 | } |
| 4997 | |
Jiazi.Li | 5be7e3c | 2022-11-15 20:03:51 +0800 | [diff] [blame] | 4998 | static int binder_ioctl_write_read(struct file *filp, unsigned long arg, |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 4999 | struct binder_thread *thread) |
| 5000 | { |
| 5001 | int ret = 0; |
| 5002 | struct binder_proc *proc = filp->private_data; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5003 | void __user *ubuf = (void __user *)arg; |
| 5004 | struct binder_write_read bwr; |
| 5005 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5006 | if (copy_from_user(&bwr, ubuf, sizeof(bwr))) { |
| 5007 | ret = -EFAULT; |
| 5008 | goto out; |
| 5009 | } |
| 5010 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 5011 | "%d:%d write %lld at %016llx, read %lld at %016llx\n", |
| 5012 | proc->pid, thread->pid, |
| 5013 | (u64)bwr.write_size, (u64)bwr.write_buffer, |
| 5014 | (u64)bwr.read_size, (u64)bwr.read_buffer); |
| 5015 | |
| 5016 | if (bwr.write_size > 0) { |
| 5017 | ret = binder_thread_write(proc, thread, |
| 5018 | bwr.write_buffer, |
| 5019 | bwr.write_size, |
| 5020 | &bwr.write_consumed); |
| 5021 | trace_binder_write_done(ret); |
| 5022 | if (ret < 0) { |
| 5023 | bwr.read_consumed = 0; |
| 5024 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 5025 | ret = -EFAULT; |
| 5026 | goto out; |
| 5027 | } |
| 5028 | } |
| 5029 | if (bwr.read_size > 0) { |
| 5030 | ret = binder_thread_read(proc, thread, bwr.read_buffer, |
| 5031 | bwr.read_size, |
| 5032 | &bwr.read_consumed, |
| 5033 | filp->f_flags & O_NONBLOCK); |
| 5034 | trace_binder_read_done(ret); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5035 | binder_inner_proc_lock(proc); |
| 5036 | if (!binder_worklist_empty_ilocked(&proc->todo)) |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 5037 | binder_wakeup_proc_ilocked(proc); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5038 | binder_inner_proc_unlock(proc); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5039 | if (ret < 0) { |
| 5040 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) |
| 5041 | ret = -EFAULT; |
| 5042 | goto out; |
| 5043 | } |
| 5044 | } |
| 5045 | binder_debug(BINDER_DEBUG_READ_WRITE, |
| 5046 | "%d:%d wrote %lld of %lld, read return %lld of %lld\n", |
| 5047 | proc->pid, thread->pid, |
| 5048 | (u64)bwr.write_consumed, (u64)bwr.write_size, |
| 5049 | (u64)bwr.read_consumed, (u64)bwr.read_size); |
| 5050 | if (copy_to_user(ubuf, &bwr, sizeof(bwr))) { |
| 5051 | ret = -EFAULT; |
| 5052 | goto out; |
| 5053 | } |
| 5054 | out: |
| 5055 | return ret; |
| 5056 | } |
| 5057 | |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 5058 | static int binder_ioctl_set_ctx_mgr(struct file *filp, |
| 5059 | struct flat_binder_object *fbo) |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5060 | { |
| 5061 | int ret = 0; |
| 5062 | struct binder_proc *proc = filp->private_data; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 5063 | struct binder_context *context = proc->context; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5064 | struct binder_node *new_node; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5065 | kuid_t curr_euid = current_euid(); |
| 5066 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5067 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 5068 | if (context->binder_context_mgr_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5069 | pr_err("BINDER_SET_CONTEXT_MGR already set\n"); |
| 5070 | ret = -EBUSY; |
| 5071 | goto out; |
| 5072 | } |
Todd Kjos | 9693ca7 | 2021-10-12 09:56:13 -0700 | [diff] [blame] | 5073 | ret = security_binder_set_context_mgr(proc->cred); |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 5074 | if (ret < 0) |
| 5075 | goto out; |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 5076 | if (uid_valid(context->binder_context_mgr_uid)) { |
| 5077 | if (!uid_eq(context->binder_context_mgr_uid, curr_euid)) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5078 | pr_err("BINDER_SET_CONTEXT_MGR bad uid %d != %d\n", |
| 5079 | from_kuid(&init_user_ns, curr_euid), |
| 5080 | from_kuid(&init_user_ns, |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 5081 | context->binder_context_mgr_uid)); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5082 | ret = -EPERM; |
| 5083 | goto out; |
| 5084 | } |
| 5085 | } else { |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 5086 | context->binder_context_mgr_uid = curr_euid; |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5087 | } |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 5088 | new_node = binder_new_node(proc, fbo); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5089 | if (!new_node) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5090 | ret = -ENOMEM; |
| 5091 | goto out; |
| 5092 | } |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5093 | binder_node_lock(new_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5094 | new_node->local_weak_refs++; |
| 5095 | new_node->local_strong_refs++; |
| 5096 | new_node->has_strong_ref = 1; |
| 5097 | new_node->has_weak_ref = 1; |
| 5098 | context->binder_context_mgr_node = new_node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5099 | binder_node_unlock(new_node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5100 | binder_put_node(new_node); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5101 | out: |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5102 | mutex_unlock(&context->context_mgr_node_lock); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5103 | return ret; |
| 5104 | } |
| 5105 | |
Martijn Coenen | 666c420 | 2018-08-25 13:50:56 -0700 | [diff] [blame] | 5106 | static int binder_ioctl_get_node_info_for_ref(struct binder_proc *proc, |
| 5107 | struct binder_node_info_for_ref *info) |
| 5108 | { |
| 5109 | struct binder_node *node; |
| 5110 | struct binder_context *context = proc->context; |
| 5111 | __u32 handle = info->handle; |
| 5112 | |
| 5113 | if (info->strong_count || info->weak_count || info->reserved1 || |
| 5114 | info->reserved2 || info->reserved3) { |
| 5115 | binder_user_error("%d BINDER_GET_NODE_INFO_FOR_REF: only handle may be non-zero.", |
| 5116 | proc->pid); |
| 5117 | return -EINVAL; |
| 5118 | } |
| 5119 | |
| 5120 | /* This ioctl may only be used by the context manager */ |
| 5121 | mutex_lock(&context->context_mgr_node_lock); |
| 5122 | if (!context->binder_context_mgr_node || |
| 5123 | context->binder_context_mgr_node->proc != proc) { |
| 5124 | mutex_unlock(&context->context_mgr_node_lock); |
| 5125 | return -EPERM; |
| 5126 | } |
| 5127 | mutex_unlock(&context->context_mgr_node_lock); |
| 5128 | |
| 5129 | node = binder_get_node_from_ref(proc, handle, true, NULL); |
| 5130 | if (!node) |
| 5131 | return -EINVAL; |
| 5132 | |
| 5133 | info->strong_count = node->local_strong_refs + |
| 5134 | node->internal_strong_refs; |
| 5135 | info->weak_count = node->local_weak_refs; |
| 5136 | |
| 5137 | binder_put_node(node); |
| 5138 | |
| 5139 | return 0; |
| 5140 | } |
| 5141 | |
Colin Cross | abcc615 | 2017-08-31 10:04:24 +0200 | [diff] [blame] | 5142 | static int binder_ioctl_get_node_debug_info(struct binder_proc *proc, |
| 5143 | struct binder_node_debug_info *info) |
| 5144 | { |
| 5145 | struct rb_node *n; |
| 5146 | binder_uintptr_t ptr = info->ptr; |
| 5147 | |
| 5148 | memset(info, 0, sizeof(*info)); |
| 5149 | |
| 5150 | binder_inner_proc_lock(proc); |
| 5151 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) { |
| 5152 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 5153 | rb_node); |
| 5154 | if (node->ptr > ptr) { |
| 5155 | info->ptr = node->ptr; |
| 5156 | info->cookie = node->cookie; |
| 5157 | info->has_strong_ref = node->has_strong_ref; |
| 5158 | info->has_weak_ref = node->has_weak_ref; |
| 5159 | break; |
| 5160 | } |
| 5161 | } |
| 5162 | binder_inner_proc_unlock(proc); |
| 5163 | |
| 5164 | return 0; |
| 5165 | } |
| 5166 | |
Li Li | 503f84d | 2021-09-07 16:12:53 -0700 | [diff] [blame] | 5167 | static bool binder_txns_pending_ilocked(struct binder_proc *proc) |
| 5168 | { |
| 5169 | struct rb_node *n; |
| 5170 | struct binder_thread *thread; |
| 5171 | |
| 5172 | if (proc->outstanding_txns > 0) |
| 5173 | return true; |
| 5174 | |
| 5175 | for (n = rb_first(&proc->threads); n; n = rb_next(n)) { |
| 5176 | thread = rb_entry(n, struct binder_thread, rb_node); |
| 5177 | if (thread->transaction_stack) |
| 5178 | return true; |
| 5179 | } |
| 5180 | return false; |
| 5181 | } |
| 5182 | |
Marco Ballesio | d819dde | 2021-01-07 17:02:50 -0800 | [diff] [blame] | 5183 | static int binder_ioctl_freeze(struct binder_freeze_info *info, |
| 5184 | struct binder_proc *target_proc) |
| 5185 | { |
| 5186 | int ret = 0; |
| 5187 | |
| 5188 | if (!info->enable) { |
| 5189 | binder_inner_proc_lock(target_proc); |
| 5190 | target_proc->sync_recv = false; |
| 5191 | target_proc->async_recv = false; |
| 5192 | target_proc->is_frozen = false; |
| 5193 | binder_inner_proc_unlock(target_proc); |
| 5194 | return 0; |
| 5195 | } |
| 5196 | |
| 5197 | /* |
| 5198 | * Freezing the target. Prevent new transactions by |
| 5199 | * setting frozen state. If timeout specified, wait |
| 5200 | * for transactions to drain. |
| 5201 | */ |
| 5202 | binder_inner_proc_lock(target_proc); |
| 5203 | target_proc->sync_recv = false; |
| 5204 | target_proc->async_recv = false; |
| 5205 | target_proc->is_frozen = true; |
| 5206 | binder_inner_proc_unlock(target_proc); |
| 5207 | |
| 5208 | if (info->timeout_ms > 0) |
| 5209 | ret = wait_event_interruptible_timeout( |
| 5210 | target_proc->freeze_wait, |
| 5211 | (!target_proc->outstanding_txns), |
| 5212 | msecs_to_jiffies(info->timeout_ms)); |
| 5213 | |
Li Li | 503f84d | 2021-09-07 16:12:53 -0700 | [diff] [blame] | 5214 | /* Check pending transactions that wait for reply */ |
| 5215 | if (ret >= 0) { |
| 5216 | binder_inner_proc_lock(target_proc); |
| 5217 | if (binder_txns_pending_ilocked(target_proc)) |
| 5218 | ret = -EAGAIN; |
| 5219 | binder_inner_proc_unlock(target_proc); |
| 5220 | } |
Marco Ballesio | d819dde | 2021-01-07 17:02:50 -0800 | [diff] [blame] | 5221 | |
| 5222 | if (ret < 0) { |
| 5223 | binder_inner_proc_lock(target_proc); |
| 5224 | target_proc->is_frozen = false; |
| 5225 | binder_inner_proc_unlock(target_proc); |
| 5226 | } |
| 5227 | |
| 5228 | return ret; |
| 5229 | } |
| 5230 | |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 5231 | static int binder_ioctl_get_freezer_info( |
| 5232 | struct binder_frozen_status_info *info) |
| 5233 | { |
| 5234 | struct binder_proc *target_proc; |
| 5235 | bool found = false; |
Li Li | 503f84d | 2021-09-07 16:12:53 -0700 | [diff] [blame] | 5236 | __u32 txns_pending; |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 5237 | |
Marco Ballesio | d819dde | 2021-01-07 17:02:50 -0800 | [diff] [blame] | 5238 | info->sync_recv = 0; |
| 5239 | info->async_recv = 0; |
| 5240 | |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 5241 | mutex_lock(&binder_procs_lock); |
| 5242 | hlist_for_each_entry(target_proc, &binder_procs, proc_node) { |
| 5243 | if (target_proc->pid == info->pid) { |
| 5244 | found = true; |
| 5245 | binder_inner_proc_lock(target_proc); |
Li Li | 503f84d | 2021-09-07 16:12:53 -0700 | [diff] [blame] | 5246 | txns_pending = binder_txns_pending_ilocked(target_proc); |
| 5247 | info->sync_recv |= target_proc->sync_recv | |
| 5248 | (txns_pending << 1); |
Marco Ballesio | d819dde | 2021-01-07 17:02:50 -0800 | [diff] [blame] | 5249 | info->async_recv |= target_proc->async_recv; |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 5250 | binder_inner_proc_unlock(target_proc); |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 5251 | } |
| 5252 | } |
| 5253 | mutex_unlock(&binder_procs_lock); |
| 5254 | |
| 5255 | if (!found) |
| 5256 | return -EINVAL; |
| 5257 | |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 5258 | return 0; |
| 5259 | } |
| 5260 | |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 5261 | static int binder_ioctl_get_extended_error(struct binder_thread *thread, |
| 5262 | void __user *ubuf) |
| 5263 | { |
Schspa Shi | 44c2e23 | 2022-05-18 09:17:54 +0800 | [diff] [blame] | 5264 | struct binder_extended_error ee; |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 5265 | |
| 5266 | binder_inner_proc_lock(thread->proc); |
Schspa Shi | 44c2e23 | 2022-05-18 09:17:54 +0800 | [diff] [blame] | 5267 | ee = thread->ee; |
| 5268 | binder_set_extended_error(&thread->ee, 0, BR_OK, 0); |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 5269 | binder_inner_proc_unlock(thread->proc); |
| 5270 | |
Schspa Shi | 44c2e23 | 2022-05-18 09:17:54 +0800 | [diff] [blame] | 5271 | if (copy_to_user(ubuf, &ee, sizeof(ee))) |
| 5272 | return -EFAULT; |
| 5273 | |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 5274 | return 0; |
| 5275 | } |
| 5276 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5277 | static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 5278 | { |
| 5279 | int ret; |
| 5280 | struct binder_proc *proc = filp->private_data; |
| 5281 | struct binder_thread *thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5282 | void __user *ubuf = (void __user *)arg; |
| 5283 | |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5284 | /*pr_info("binder_ioctl: %d:%d %x %lx\n", |
| 5285 | proc->pid, current->pid, cmd, arg);*/ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5286 | |
Sherry Yang | 4175e2b | 2017-08-23 08:46:40 -0700 | [diff] [blame] | 5287 | binder_selftest_alloc(&proc->alloc); |
| 5288 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5289 | trace_binder_ioctl(cmd, arg); |
| 5290 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5291 | ret = wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
| 5292 | if (ret) |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5293 | goto err_unlocked; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5294 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5295 | thread = binder_get_thread(proc); |
| 5296 | if (thread == NULL) { |
| 5297 | ret = -ENOMEM; |
| 5298 | goto err; |
| 5299 | } |
| 5300 | |
| 5301 | switch (cmd) { |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5302 | case BINDER_WRITE_READ: |
Jiazi.Li | 5be7e3c | 2022-11-15 20:03:51 +0800 | [diff] [blame] | 5303 | ret = binder_ioctl_write_read(filp, arg, thread); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5304 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5305 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5306 | break; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 5307 | case BINDER_SET_MAX_THREADS: { |
Carlos Llamas | f642f36 | 2024-04-21 17:37:49 +0000 | [diff] [blame] | 5308 | u32 max_threads; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 5309 | |
| 5310 | if (copy_from_user(&max_threads, ubuf, |
| 5311 | sizeof(max_threads))) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5312 | ret = -EINVAL; |
| 5313 | goto err; |
| 5314 | } |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 5315 | binder_inner_proc_lock(proc); |
| 5316 | proc->max_threads = max_threads; |
| 5317 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5318 | break; |
Todd Kjos | b3e6861 | 2017-06-29 12:02:07 -0700 | [diff] [blame] | 5319 | } |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 5320 | case BINDER_SET_CONTEXT_MGR_EXT: { |
| 5321 | struct flat_binder_object fbo; |
| 5322 | |
| 5323 | if (copy_from_user(&fbo, ubuf, sizeof(fbo))) { |
| 5324 | ret = -EINVAL; |
| 5325 | goto err; |
| 5326 | } |
| 5327 | ret = binder_ioctl_set_ctx_mgr(filp, &fbo); |
| 5328 | if (ret) |
| 5329 | goto err; |
| 5330 | break; |
| 5331 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5332 | case BINDER_SET_CONTEXT_MGR: |
Todd Kjos | 5312a99 | 2019-01-14 09:10:21 -0800 | [diff] [blame] | 5333 | ret = binder_ioctl_set_ctx_mgr(filp, NULL); |
Tair Rzayev | 78260ac | 2014-06-03 22:27:21 +0300 | [diff] [blame] | 5334 | if (ret) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5335 | goto err; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5336 | break; |
| 5337 | case BINDER_THREAD_EXIT: |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 5338 | binder_debug(BINDER_DEBUG_THREADS, "%d:%d exit\n", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5339 | proc->pid, thread->pid); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5340 | binder_thread_release(proc, thread); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5341 | thread = NULL; |
| 5342 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 5343 | case BINDER_VERSION: { |
| 5344 | struct binder_version __user *ver = ubuf; |
| 5345 | |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 5346 | if (put_user(BINDER_CURRENT_PROTOCOL_VERSION, |
| 5347 | &ver->protocol_version)) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5348 | ret = -EINVAL; |
| 5349 | goto err; |
| 5350 | } |
| 5351 | break; |
Mathieu Maret | 36c89c0 | 2014-04-15 12:03:05 +0200 | [diff] [blame] | 5352 | } |
Martijn Coenen | 666c420 | 2018-08-25 13:50:56 -0700 | [diff] [blame] | 5353 | case BINDER_GET_NODE_INFO_FOR_REF: { |
| 5354 | struct binder_node_info_for_ref info; |
| 5355 | |
| 5356 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 5357 | ret = -EFAULT; |
| 5358 | goto err; |
| 5359 | } |
| 5360 | |
| 5361 | ret = binder_ioctl_get_node_info_for_ref(proc, &info); |
| 5362 | if (ret < 0) |
| 5363 | goto err; |
| 5364 | |
| 5365 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 5366 | ret = -EFAULT; |
| 5367 | goto err; |
| 5368 | } |
| 5369 | |
| 5370 | break; |
| 5371 | } |
Colin Cross | abcc615 | 2017-08-31 10:04:24 +0200 | [diff] [blame] | 5372 | case BINDER_GET_NODE_DEBUG_INFO: { |
| 5373 | struct binder_node_debug_info info; |
| 5374 | |
| 5375 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 5376 | ret = -EFAULT; |
| 5377 | goto err; |
| 5378 | } |
| 5379 | |
| 5380 | ret = binder_ioctl_get_node_debug_info(proc, &info); |
| 5381 | if (ret < 0) |
| 5382 | goto err; |
| 5383 | |
| 5384 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 5385 | ret = -EFAULT; |
| 5386 | goto err; |
| 5387 | } |
| 5388 | break; |
| 5389 | } |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 5390 | case BINDER_FREEZE: { |
| 5391 | struct binder_freeze_info info; |
Marco Ballesio | 9ad13ab | 2021-01-22 09:26:14 -0800 | [diff] [blame] | 5392 | struct binder_proc **target_procs = NULL, *target_proc; |
| 5393 | int target_procs_count = 0, i = 0; |
| 5394 | |
| 5395 | ret = 0; |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 5396 | |
| 5397 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 5398 | ret = -EFAULT; |
| 5399 | goto err; |
| 5400 | } |
Marco Ballesio | 9ad13ab | 2021-01-22 09:26:14 -0800 | [diff] [blame] | 5401 | |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 5402 | mutex_lock(&binder_procs_lock); |
| 5403 | hlist_for_each_entry(target_proc, &binder_procs, proc_node) { |
Marco Ballesio | 9ad13ab | 2021-01-22 09:26:14 -0800 | [diff] [blame] | 5404 | if (target_proc->pid == info.pid) |
| 5405 | target_procs_count++; |
| 5406 | } |
Marco Ballesio | d819dde | 2021-01-07 17:02:50 -0800 | [diff] [blame] | 5407 | |
Marco Ballesio | 9ad13ab | 2021-01-22 09:26:14 -0800 | [diff] [blame] | 5408 | if (target_procs_count == 0) { |
| 5409 | mutex_unlock(&binder_procs_lock); |
| 5410 | ret = -EINVAL; |
| 5411 | goto err; |
| 5412 | } |
Marco Ballesio | d819dde | 2021-01-07 17:02:50 -0800 | [diff] [blame] | 5413 | |
Marco Ballesio | 9ad13ab | 2021-01-22 09:26:14 -0800 | [diff] [blame] | 5414 | target_procs = kmalloc(sizeof(struct binder_proc *) * |
| 5415 | target_procs_count, |
| 5416 | GFP_KERNEL); |
Marco Ballesio | d819dde | 2021-01-07 17:02:50 -0800 | [diff] [blame] | 5417 | |
Marco Ballesio | 9ad13ab | 2021-01-22 09:26:14 -0800 | [diff] [blame] | 5418 | if (!target_procs) { |
| 5419 | mutex_unlock(&binder_procs_lock); |
| 5420 | ret = -ENOMEM; |
| 5421 | goto err; |
| 5422 | } |
| 5423 | |
| 5424 | hlist_for_each_entry(target_proc, &binder_procs, proc_node) { |
| 5425 | if (target_proc->pid != info.pid) |
| 5426 | continue; |
| 5427 | |
| 5428 | binder_inner_proc_lock(target_proc); |
| 5429 | target_proc->tmp_ref++; |
| 5430 | binder_inner_proc_unlock(target_proc); |
| 5431 | |
| 5432 | target_procs[i++] = target_proc; |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 5433 | } |
| 5434 | mutex_unlock(&binder_procs_lock); |
Marco Ballesio | d819dde | 2021-01-07 17:02:50 -0800 | [diff] [blame] | 5435 | |
Marco Ballesio | 9ad13ab | 2021-01-22 09:26:14 -0800 | [diff] [blame] | 5436 | for (i = 0; i < target_procs_count; i++) { |
| 5437 | if (ret >= 0) |
| 5438 | ret = binder_ioctl_freeze(&info, |
| 5439 | target_procs[i]); |
| 5440 | |
| 5441 | binder_proc_dec_tmpref(target_procs[i]); |
| 5442 | } |
| 5443 | |
| 5444 | kfree(target_procs); |
| 5445 | |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 5446 | if (ret < 0) |
| 5447 | goto err; |
| 5448 | break; |
| 5449 | } |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 5450 | case BINDER_GET_FROZEN_INFO: { |
| 5451 | struct binder_frozen_status_info info; |
| 5452 | |
| 5453 | if (copy_from_user(&info, ubuf, sizeof(info))) { |
| 5454 | ret = -EFAULT; |
| 5455 | goto err; |
| 5456 | } |
| 5457 | |
| 5458 | ret = binder_ioctl_get_freezer_info(&info); |
| 5459 | if (ret < 0) |
| 5460 | goto err; |
| 5461 | |
| 5462 | if (copy_to_user(ubuf, &info, sizeof(info))) { |
| 5463 | ret = -EFAULT; |
| 5464 | goto err; |
| 5465 | } |
| 5466 | break; |
| 5467 | } |
Hang Lu | 4dbc168 | 2021-04-09 17:40:46 +0800 | [diff] [blame] | 5468 | case BINDER_ENABLE_ONEWAY_SPAM_DETECTION: { |
| 5469 | uint32_t enable; |
| 5470 | |
| 5471 | if (copy_from_user(&enable, ubuf, sizeof(enable))) { |
Luca Stefani | 68bbe37 | 2021-05-06 21:37:25 +0200 | [diff] [blame] | 5472 | ret = -EFAULT; |
Hang Lu | 4dbc168 | 2021-04-09 17:40:46 +0800 | [diff] [blame] | 5473 | goto err; |
| 5474 | } |
| 5475 | binder_inner_proc_lock(proc); |
| 5476 | proc->oneway_spam_detection_enabled = (bool)enable; |
| 5477 | binder_inner_proc_unlock(proc); |
| 5478 | break; |
| 5479 | } |
Carlos Llamas | ea1d78b | 2022-04-29 23:56:41 +0000 | [diff] [blame] | 5480 | case BINDER_GET_EXTENDED_ERROR: |
| 5481 | ret = binder_ioctl_get_extended_error(thread, ubuf); |
| 5482 | if (ret < 0) |
| 5483 | goto err; |
| 5484 | break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5485 | default: |
| 5486 | ret = -EINVAL; |
| 5487 | goto err; |
| 5488 | } |
| 5489 | ret = 0; |
| 5490 | err: |
| 5491 | if (thread) |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 5492 | thread->looper_need_return = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5493 | wait_event_interruptible(binder_user_error_wait, binder_stop_on_user_error < 2); |
Marco Ballesio | 49fd054 | 2021-01-08 08:06:32 -0800 | [diff] [blame] | 5494 | if (ret && ret != -EINTR) |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 5495 | 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] | 5496 | err_unlocked: |
| 5497 | trace_binder_ioctl_done(ret); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5498 | return ret; |
| 5499 | } |
| 5500 | |
| 5501 | static void binder_vma_open(struct vm_area_struct *vma) |
| 5502 | { |
| 5503 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5504 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5505 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 5506 | "%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] | 5507 | proc->pid, vma->vm_start, vma->vm_end, |
| 5508 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 5509 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5510 | } |
| 5511 | |
| 5512 | static void binder_vma_close(struct vm_area_struct *vma) |
| 5513 | { |
| 5514 | struct binder_proc *proc = vma->vm_private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5515 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5516 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Anmol Sarma | 56b468f | 2012-10-30 22:35:43 +0530 | [diff] [blame] | 5517 | "%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] | 5518 | proc->pid, vma->vm_start, vma->vm_end, |
| 5519 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 5520 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5521 | binder_alloc_vma_close(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5522 | binder_defer_work(proc, BINDER_DEFERRED_PUT_FILES); |
| 5523 | } |
| 5524 | |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 5525 | static int binder_vm_fault(struct vm_fault *vmf) |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 5526 | { |
| 5527 | return VM_FAULT_SIGBUS; |
| 5528 | } |
| 5529 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 5530 | static const struct vm_operations_struct binder_vm_ops = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5531 | .open = binder_vma_open, |
| 5532 | .close = binder_vma_close, |
Vinayak Menon | ddac7d5 | 2014-06-02 18:17:59 +0530 | [diff] [blame] | 5533 | .fault = binder_vm_fault, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5534 | }; |
| 5535 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5536 | static int binder_mmap(struct file *filp, struct vm_area_struct *vma) |
| 5537 | { |
| 5538 | int ret; |
| 5539 | struct binder_proc *proc = filp->private_data; |
| 5540 | const char *failure_string; |
| 5541 | |
| 5542 | if (proc->tsk != current->group_leader) |
| 5543 | return -EINVAL; |
| 5544 | |
| 5545 | if ((vma->vm_end - vma->vm_start) > SZ_4M) |
| 5546 | vma->vm_end = vma->vm_start + SZ_4M; |
| 5547 | |
| 5548 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 5549 | "%s: %d %lx-%lx (%ld K) vma %lx pagep %lx\n", |
| 5550 | __func__, proc->pid, vma->vm_start, vma->vm_end, |
| 5551 | (vma->vm_end - vma->vm_start) / SZ_1K, vma->vm_flags, |
| 5552 | (unsigned long)pgprot_val(vma->vm_page_prot)); |
| 5553 | |
| 5554 | if (vma->vm_flags & FORBIDDEN_MMAP_FLAGS) { |
| 5555 | ret = -EPERM; |
| 5556 | failure_string = "bad vm_flags"; |
| 5557 | goto err_bad_arg; |
| 5558 | } |
Minchan Kim | 864095d | 2018-05-07 23:15:37 +0900 | [diff] [blame] | 5559 | vma->vm_flags |= VM_DONTCOPY | VM_MIXEDMAP; |
| 5560 | vma->vm_flags &= ~VM_MAYWRITE; |
| 5561 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5562 | vma->vm_ops = &binder_vm_ops; |
| 5563 | vma->vm_private_data = proc; |
| 5564 | |
| 5565 | ret = binder_alloc_mmap_handler(&proc->alloc, vma); |
| 5566 | if (ret) |
| 5567 | return ret; |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 5568 | mutex_lock(&proc->files_lock); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5569 | proc->files = get_files_struct(current); |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 5570 | mutex_unlock(&proc->files_lock); |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5571 | return 0; |
| 5572 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5573 | err_bad_arg: |
Elad Wexler | 47e2dd3 | 2017-12-29 11:03:37 +0200 | [diff] [blame] | 5574 | pr_err("%s: %d %lx-%lx %s failed %d\n", __func__, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5575 | proc->pid, vma->vm_start, vma->vm_end, failure_string, ret); |
| 5576 | return ret; |
| 5577 | } |
| 5578 | |
| 5579 | static int binder_open(struct inode *nodp, struct file *filp) |
| 5580 | { |
| 5581 | struct binder_proc *proc; |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5582 | struct binder_device *binder_dev; |
Hridya Valsaraju | 1405bf2 | 2019-09-03 09:16:55 -0700 | [diff] [blame] | 5583 | struct binderfs_info *info; |
| 5584 | struct dentry *binder_binderfs_dir_entry_proc = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5585 | |
Elad Wexler | 47e2dd3 | 2017-12-29 11:03:37 +0200 | [diff] [blame] | 5586 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, "%s: %d:%d\n", __func__, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5587 | current->group_leader->pid, current->pid); |
| 5588 | |
| 5589 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); |
| 5590 | if (proc == NULL) |
| 5591 | return -ENOMEM; |
Todd Kjos | 9630fe8 | 2017-06-29 12:02:00 -0700 | [diff] [blame] | 5592 | spin_lock_init(&proc->inner_lock); |
| 5593 | spin_lock_init(&proc->outer_lock); |
Todd Kjos | c4ea41b | 2017-06-29 12:01:36 -0700 | [diff] [blame] | 5594 | get_task_struct(current->group_leader); |
| 5595 | proc->tsk = current->group_leader; |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 5596 | mutex_init(&proc->files_lock); |
Todd Kjos | ae4a7b9 | 2021-10-12 09:56:12 -0700 | [diff] [blame] | 5597 | proc->cred = get_cred(filp->f_cred); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5598 | INIT_LIST_HEAD(&proc->todo); |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 5599 | init_waitqueue_head(&proc->freeze_wait); |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 5600 | if (binder_supported_policy(current->policy)) { |
| 5601 | proc->default_priority.sched_policy = current->policy; |
| 5602 | proc->default_priority.prio = current->normal_prio; |
| 5603 | } else { |
| 5604 | proc->default_priority.sched_policy = SCHED_NORMAL; |
| 5605 | proc->default_priority.prio = NICE_TO_PRIO(0); |
| 5606 | } |
| 5607 | |
Christian Brauner | 34fbd92 | 2018-12-14 13:11:14 +0100 | [diff] [blame] | 5608 | /* binderfs stashes devices in i_private */ |
Hridya Valsaraju | 1405bf2 | 2019-09-03 09:16:55 -0700 | [diff] [blame] | 5609 | if (is_binderfs_device(nodp)) { |
Christian Brauner | 63e14d3 | 2020-03-03 17:43:40 +0100 | [diff] [blame] | 5610 | binder_dev = nodp->i_private; |
Hridya Valsaraju | 1405bf2 | 2019-09-03 09:16:55 -0700 | [diff] [blame] | 5611 | info = nodp->i_sb->s_fs_info; |
| 5612 | binder_binderfs_dir_entry_proc = info->proc_log_dir; |
| 5613 | } else { |
Christian Brauner | 34fbd92 | 2018-12-14 13:11:14 +0100 | [diff] [blame] | 5614 | binder_dev = container_of(filp->private_data, |
| 5615 | struct binder_device, miscdev); |
Hridya Valsaraju | 1405bf2 | 2019-09-03 09:16:55 -0700 | [diff] [blame] | 5616 | } |
Christian Brauner | bbfd216 | 2020-03-03 17:43:40 +0100 | [diff] [blame] | 5617 | refcount_inc(&binder_dev->ref); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 5618 | proc->context = &binder_dev->context; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5619 | binder_alloc_init(&proc->alloc); |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5620 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5621 | binder_stats_created(BINDER_STAT_PROC); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5622 | proc->pid = current->group_leader->pid; |
| 5623 | INIT_LIST_HEAD(&proc->delivered_death); |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 5624 | INIT_LIST_HEAD(&proc->waiting_threads); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5625 | filp->private_data = proc; |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 5626 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5627 | mutex_lock(&binder_procs_lock); |
| 5628 | hlist_add_head(&proc->proc_node, &binder_procs); |
| 5629 | mutex_unlock(&binder_procs_lock); |
| 5630 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5631 | if (binder_debugfs_dir_entry_proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5632 | char strbuf[11]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5633 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5634 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5635 | /* |
| 5636 | * proc debug entries are shared between contexts, so |
| 5637 | * this will fail if the process tries to open the driver |
| 5638 | * again with a different context. The priting code will |
| 5639 | * anyway print all contexts that a given PID has, so this |
| 5640 | * is not a problem. |
| 5641 | */ |
Harsh Shandilya | c5b47d2 | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 5642 | proc->debugfs_entry = debugfs_create_file(strbuf, 0444, |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 5643 | binder_debugfs_dir_entry_proc, |
| 5644 | (void *)(unsigned long)proc->pid, |
Yangtao Li | 8b2246a | 2018-11-30 20:26:30 -0500 | [diff] [blame] | 5645 | &proc_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5646 | } |
| 5647 | |
Hridya Valsaraju | 1405bf2 | 2019-09-03 09:16:55 -0700 | [diff] [blame] | 5648 | if (binder_binderfs_dir_entry_proc) { |
| 5649 | char strbuf[11]; |
| 5650 | struct dentry *binderfs_entry; |
| 5651 | |
| 5652 | snprintf(strbuf, sizeof(strbuf), "%u", proc->pid); |
| 5653 | /* |
| 5654 | * Similar to debugfs, the process specific log file is shared |
| 5655 | * between contexts. If the file has already been created for a |
| 5656 | * process, the following binderfs_create_file() call will |
| 5657 | * fail with error code EEXIST if another context of the same |
| 5658 | * process invoked binder_open(). This is ok since same as |
| 5659 | * debugfs, the log file will contain information on all |
| 5660 | * contexts of a given PID. |
| 5661 | */ |
| 5662 | binderfs_entry = binderfs_create_file(binder_binderfs_dir_entry_proc, |
| 5663 | strbuf, &proc_fops, (void *)(unsigned long)proc->pid); |
| 5664 | if (!IS_ERR(binderfs_entry)) { |
| 5665 | proc->binderfs_entry = binderfs_entry; |
| 5666 | } else { |
| 5667 | int error; |
| 5668 | |
| 5669 | error = PTR_ERR(binderfs_entry); |
| 5670 | if (error != -EEXIST) { |
| 5671 | pr_warn("Unable to create file %s in binderfs (error %d)\n", |
| 5672 | strbuf, error); |
| 5673 | } |
| 5674 | } |
| 5675 | } |
| 5676 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5677 | return 0; |
| 5678 | } |
| 5679 | |
| 5680 | static int binder_flush(struct file *filp, fl_owner_t id) |
| 5681 | { |
| 5682 | struct binder_proc *proc = filp->private_data; |
| 5683 | |
| 5684 | binder_defer_work(proc, BINDER_DEFERRED_FLUSH); |
| 5685 | |
| 5686 | return 0; |
| 5687 | } |
| 5688 | |
| 5689 | static void binder_deferred_flush(struct binder_proc *proc) |
| 5690 | { |
| 5691 | struct rb_node *n; |
| 5692 | int wake_count = 0; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5693 | |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5694 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5695 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) { |
| 5696 | struct binder_thread *thread = rb_entry(n, struct binder_thread, rb_node); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5697 | |
Todd Kjos | 08dabce | 2017-06-29 12:01:49 -0700 | [diff] [blame] | 5698 | thread->looper_need_return = true; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5699 | if (thread->looper & BINDER_LOOPER_STATE_WAITING) { |
| 5700 | wake_up_interruptible(&thread->wait); |
| 5701 | wake_count++; |
| 5702 | } |
| 5703 | } |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5704 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5705 | |
| 5706 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
| 5707 | "binder_flush: %d woke %d threads\n", proc->pid, |
| 5708 | wake_count); |
| 5709 | } |
| 5710 | |
| 5711 | static int binder_release(struct inode *nodp, struct file *filp) |
| 5712 | { |
| 5713 | struct binder_proc *proc = filp->private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5714 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5715 | debugfs_remove(proc->debugfs_entry); |
Hridya Valsaraju | 1405bf2 | 2019-09-03 09:16:55 -0700 | [diff] [blame] | 5716 | |
| 5717 | if (proc->binderfs_entry) { |
| 5718 | binderfs_remove_file(proc->binderfs_entry); |
| 5719 | proc->binderfs_entry = NULL; |
| 5720 | } |
| 5721 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5722 | binder_defer_work(proc, BINDER_DEFERRED_RELEASE); |
| 5723 | |
| 5724 | return 0; |
| 5725 | } |
| 5726 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5727 | static int binder_node_release(struct binder_node *node, int refs) |
| 5728 | { |
| 5729 | struct binder_ref *ref; |
| 5730 | int death = 0; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 5731 | struct binder_proc *proc = node->proc; |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5732 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5733 | binder_release_work(proc, &node->async_todo); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 5734 | |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5735 | binder_node_lock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 5736 | binder_inner_proc_lock(proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5737 | binder_dequeue_work_ilocked(&node->work); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5738 | /* |
| 5739 | * The caller must have taken a temporary ref on the node, |
| 5740 | */ |
| 5741 | BUG_ON(!node->tmp_refs); |
| 5742 | if (hlist_empty(&node->refs) && node->tmp_refs == 1) { |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 5743 | binder_inner_proc_unlock(proc); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5744 | binder_node_unlock(node); |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 5745 | binder_free_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5746 | |
| 5747 | return refs; |
| 5748 | } |
| 5749 | |
| 5750 | node->proc = NULL; |
| 5751 | node->local_strong_refs = 0; |
| 5752 | node->local_weak_refs = 0; |
Todd Kjos | ed29721 | 2017-06-29 12:02:01 -0700 | [diff] [blame] | 5753 | binder_inner_proc_unlock(proc); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5754 | |
| 5755 | spin_lock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5756 | hlist_add_head(&node->dead_node, &binder_dead_nodes); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5757 | spin_unlock(&binder_dead_nodes_lock); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5758 | |
| 5759 | hlist_for_each_entry(ref, &node->refs, node_entry) { |
| 5760 | refs++; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 5761 | /* |
| 5762 | * Need the node lock to synchronize |
| 5763 | * with new notification requests and the |
| 5764 | * inner lock to synchronize with queued |
| 5765 | * death notifications. |
| 5766 | */ |
| 5767 | binder_inner_proc_lock(ref->proc); |
| 5768 | if (!ref->death) { |
| 5769 | binder_inner_proc_unlock(ref->proc); |
Arve Hjønnevåg | e194fd8 | 2014-02-17 13:58:29 -0800 | [diff] [blame] | 5770 | continue; |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 5771 | } |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5772 | |
| 5773 | death++; |
| 5774 | |
Martijn Coenen | ab51ec6 | 2017-06-29 12:02:10 -0700 | [diff] [blame] | 5775 | BUG_ON(!list_empty(&ref->death->work.entry)); |
| 5776 | ref->death->work.type = BINDER_WORK_DEAD_BINDER; |
| 5777 | binder_enqueue_work_ilocked(&ref->death->work, |
| 5778 | &ref->proc->todo); |
Martijn Coenen | 408c68b | 2017-08-31 10:04:19 +0200 | [diff] [blame] | 5779 | binder_wakeup_proc_ilocked(ref->proc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5780 | binder_inner_proc_unlock(ref->proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5781 | } |
| 5782 | |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5783 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
| 5784 | "node %d now dead, refs %d, death %d\n", |
| 5785 | node->debug_id, refs, death); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 5786 | binder_node_unlock(node); |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5787 | binder_put_node(node); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5788 | |
| 5789 | return refs; |
| 5790 | } |
| 5791 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5792 | static void binder_deferred_release(struct binder_proc *proc) |
| 5793 | { |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 5794 | struct binder_context *context = proc->context; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5795 | struct rb_node *n; |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5796 | int threads, nodes, incoming_refs, outgoing_refs, active_transactions; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5797 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5798 | BUG_ON(proc->files); |
| 5799 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5800 | mutex_lock(&binder_procs_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5801 | hlist_del(&proc->proc_node); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5802 | mutex_unlock(&binder_procs_lock); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5803 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5804 | mutex_lock(&context->context_mgr_node_lock); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 5805 | if (context->binder_context_mgr_node && |
| 5806 | context->binder_context_mgr_node->proc == proc) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5807 | binder_debug(BINDER_DEBUG_DEAD_BINDER, |
Mirsal Ennaime | c07c933 | 2013-03-12 11:42:02 +0100 | [diff] [blame] | 5808 | "%s: %d context_mgr_node gone\n", |
| 5809 | __func__, proc->pid); |
Martijn Coenen | 342e5c9 | 2017-02-03 14:40:46 -0800 | [diff] [blame] | 5810 | context->binder_context_mgr_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5811 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 5812 | mutex_unlock(&context->context_mgr_node_lock); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5813 | binder_inner_proc_lock(proc); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5814 | /* |
| 5815 | * Make sure proc stays alive after we |
| 5816 | * remove all the threads |
| 5817 | */ |
| 5818 | proc->tmp_ref++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5819 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5820 | proc->is_dead = true; |
Marco Ballesio | 620ede4 | 2021-03-09 18:18:56 -0800 | [diff] [blame] | 5821 | proc->is_frozen = false; |
Marco Ballesio | 4549040 | 2020-09-10 13:39:20 -0700 | [diff] [blame] | 5822 | proc->sync_recv = false; |
| 5823 | proc->async_recv = false; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5824 | threads = 0; |
| 5825 | active_transactions = 0; |
| 5826 | while ((n = rb_first(&proc->threads))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5827 | struct binder_thread *thread; |
| 5828 | |
| 5829 | thread = rb_entry(n, struct binder_thread, rb_node); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5830 | binder_inner_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5831 | threads++; |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5832 | active_transactions += binder_thread_release(proc, thread); |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 5833 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5834 | } |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5835 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5836 | nodes = 0; |
| 5837 | incoming_refs = 0; |
| 5838 | while ((n = rb_first(&proc->nodes))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5839 | struct binder_node *node; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5840 | |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5841 | node = rb_entry(n, struct binder_node, rb_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5842 | nodes++; |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 5843 | /* |
| 5844 | * take a temporary ref on the node before |
| 5845 | * calling binder_node_release() which will either |
| 5846 | * kfree() the node or call binder_put_node() |
| 5847 | */ |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5848 | binder_inc_node_tmpref_ilocked(node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5849 | rb_erase(&node->rb_node, &proc->nodes); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5850 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 008fa74 | 2013-03-12 11:41:59 +0100 | [diff] [blame] | 5851 | incoming_refs = binder_node_release(node, incoming_refs); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5852 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5853 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 5854 | binder_inner_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5855 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5856 | outgoing_refs = 0; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5857 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5858 | while ((n = rb_first(&proc->refs_by_desc))) { |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5859 | struct binder_ref *ref; |
| 5860 | |
| 5861 | ref = rb_entry(n, struct binder_ref, rb_node_desc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5862 | outgoing_refs++; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5863 | binder_cleanup_ref_olocked(ref); |
| 5864 | binder_proc_unlock(proc); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 5865 | binder_free_ref(ref); |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5866 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5867 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 5868 | binder_proc_unlock(proc); |
Mirsal Ennaime | 53413e7 | 2013-03-12 11:42:00 +0100 | [diff] [blame] | 5869 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 5870 | binder_release_work(proc, &proc->todo); |
| 5871 | binder_release_work(proc, &proc->delivered_death); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5872 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5873 | binder_debug(BINDER_DEBUG_OPEN_CLOSE, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5874 | "%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] | 5875 | __func__, proc->pid, threads, nodes, incoming_refs, |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 5876 | outgoing_refs, active_transactions); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5877 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5878 | binder_proc_dec_tmpref(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5879 | } |
| 5880 | |
| 5881 | static void binder_deferred_func(struct work_struct *work) |
| 5882 | { |
| 5883 | struct binder_proc *proc; |
| 5884 | struct files_struct *files; |
| 5885 | |
| 5886 | int defer; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 5887 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5888 | do { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5889 | mutex_lock(&binder_deferred_lock); |
| 5890 | if (!hlist_empty(&binder_deferred_list)) { |
| 5891 | proc = hlist_entry(binder_deferred_list.first, |
| 5892 | struct binder_proc, deferred_work_node); |
| 5893 | hlist_del_init(&proc->deferred_work_node); |
| 5894 | defer = proc->deferred_work; |
| 5895 | proc->deferred_work = 0; |
| 5896 | } else { |
| 5897 | proc = NULL; |
| 5898 | defer = 0; |
| 5899 | } |
| 5900 | mutex_unlock(&binder_deferred_lock); |
| 5901 | |
| 5902 | files = NULL; |
| 5903 | if (defer & BINDER_DEFERRED_PUT_FILES) { |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 5904 | mutex_lock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5905 | files = proc->files; |
| 5906 | if (files) |
| 5907 | proc->files = NULL; |
Todd Kjos | 029876d | 2017-11-10 15:30:27 -0800 | [diff] [blame] | 5908 | mutex_unlock(&proc->files_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5909 | } |
| 5910 | |
| 5911 | if (defer & BINDER_DEFERRED_FLUSH) |
| 5912 | binder_deferred_flush(proc); |
| 5913 | |
| 5914 | if (defer & BINDER_DEFERRED_RELEASE) |
| 5915 | binder_deferred_release(proc); /* frees proc */ |
| 5916 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5917 | if (files) |
| 5918 | put_files_struct(files); |
| 5919 | } while (proc); |
| 5920 | } |
| 5921 | static DECLARE_WORK(binder_deferred_work, binder_deferred_func); |
| 5922 | |
| 5923 | static void |
| 5924 | binder_defer_work(struct binder_proc *proc, enum binder_deferred_state defer) |
| 5925 | { |
| 5926 | mutex_lock(&binder_deferred_lock); |
| 5927 | proc->deferred_work |= defer; |
| 5928 | if (hlist_unhashed(&proc->deferred_work_node)) { |
| 5929 | hlist_add_head(&proc->deferred_work_node, |
| 5930 | &binder_deferred_list); |
Bhaktipriya Shridhar | 1beba52 | 2016-08-13 22:16:24 +0530 | [diff] [blame] | 5931 | schedule_work(&binder_deferred_work); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5932 | } |
| 5933 | mutex_unlock(&binder_deferred_lock); |
| 5934 | } |
| 5935 | |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5936 | static void print_binder_transaction_ilocked(struct seq_file *m, |
| 5937 | struct binder_proc *proc, |
| 5938 | const char *prefix, |
| 5939 | struct binder_transaction *t) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5940 | { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5941 | struct binder_proc *to_proc; |
| 5942 | struct binder_buffer *buffer = t->buffer; |
| 5943 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5944 | spin_lock(&t->lock); |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5945 | to_proc = t->to_proc; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5946 | seq_printf(m, |
Greg Kroah-Hartman | af3b8e6 | 2018-02-26 09:22:41 +0100 | [diff] [blame] | 5947 | "%s %d: %pK from %d:%d to %d:%d code %x flags %x pri %d:%d r%d", |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5948 | prefix, t->debug_id, t, |
| 5949 | t->from ? t->from->proc->pid : 0, |
| 5950 | t->from ? t->from->pid : 0, |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5951 | to_proc ? to_proc->pid : 0, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5952 | t->to_thread ? t->to_thread->pid : 0, |
Martijn Coenen | 37b3441 | 2017-06-06 17:04:42 -0700 | [diff] [blame] | 5953 | t->code, t->flags, t->priority.sched_policy, |
| 5954 | t->priority.prio, t->need_reply); |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 5955 | spin_unlock(&t->lock); |
| 5956 | |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5957 | if (proc != to_proc) { |
| 5958 | /* |
| 5959 | * Can only safely deref buffer if we are holding the |
| 5960 | * correct proc inner lock for this node |
| 5961 | */ |
| 5962 | seq_puts(m, "\n"); |
| 5963 | return; |
| 5964 | } |
| 5965 | |
| 5966 | if (buffer == NULL) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 5967 | seq_puts(m, " buffer free\n"); |
| 5968 | return; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5969 | } |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5970 | if (buffer->target_node) |
| 5971 | seq_printf(m, " node %d", buffer->target_node->debug_id); |
Todd Kjos | b46af09 | 2018-02-07 13:57:37 -0800 | [diff] [blame] | 5972 | seq_printf(m, " size %zd:%zd data %pK\n", |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5973 | buffer->data_size, buffer->offsets_size, |
Todd Kjos | 9fcfd1e | 2019-02-08 10:35:20 -0800 | [diff] [blame] | 5974 | buffer->user_data); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5975 | } |
| 5976 | |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5977 | static void print_binder_work_ilocked(struct seq_file *m, |
| 5978 | struct binder_proc *proc, |
| 5979 | const char *prefix, |
| 5980 | const char *transaction_prefix, |
| 5981 | struct binder_work *w) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5982 | { |
| 5983 | struct binder_node *node; |
| 5984 | struct binder_transaction *t; |
| 5985 | |
| 5986 | switch (w->type) { |
| 5987 | case BINDER_WORK_TRANSACTION: |
| 5988 | t = container_of(w, struct binder_transaction, work); |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 5989 | print_binder_transaction_ilocked( |
| 5990 | m, proc, transaction_prefix, t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5991 | break; |
Todd Kjos | 26549d1 | 2017-06-29 12:01:55 -0700 | [diff] [blame] | 5992 | case BINDER_WORK_RETURN_ERROR: { |
| 5993 | struct binder_error *e = container_of( |
| 5994 | w, struct binder_error, work); |
| 5995 | |
| 5996 | seq_printf(m, "%stransaction error: %u\n", |
| 5997 | prefix, e->cmd); |
| 5998 | } break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 5999 | case BINDER_WORK_TRANSACTION_COMPLETE: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6000 | seq_printf(m, "%stransaction complete\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6001 | break; |
| 6002 | case BINDER_WORK_NODE: |
| 6003 | node = container_of(w, struct binder_node, work); |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 6004 | seq_printf(m, "%snode work %d: u%016llx c%016llx\n", |
| 6005 | prefix, node->debug_id, |
| 6006 | (u64)node->ptr, (u64)node->cookie); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6007 | break; |
| 6008 | case BINDER_WORK_DEAD_BINDER: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6009 | seq_printf(m, "%shas dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6010 | break; |
| 6011 | case BINDER_WORK_DEAD_BINDER_AND_CLEAR: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6012 | seq_printf(m, "%shas cleared dead binder\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6013 | break; |
| 6014 | case BINDER_WORK_CLEAR_DEATH_NOTIFICATION: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6015 | seq_printf(m, "%shas cleared death notification\n", prefix); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6016 | break; |
| 6017 | default: |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6018 | seq_printf(m, "%sunknown work: type %d\n", prefix, w->type); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6019 | break; |
| 6020 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6021 | } |
| 6022 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6023 | static void print_binder_thread_ilocked(struct seq_file *m, |
| 6024 | struct binder_thread *thread, |
| 6025 | int print_always) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6026 | { |
| 6027 | struct binder_transaction *t; |
| 6028 | struct binder_work *w; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6029 | size_t start_pos = m->count; |
| 6030 | size_t header_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6031 | |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 6032 | 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] | 6033 | thread->pid, thread->looper, |
Todd Kjos | 7a4408c | 2017-06-29 12:01:57 -0700 | [diff] [blame] | 6034 | thread->looper_need_return, |
| 6035 | atomic_read(&thread->tmp_ref)); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6036 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6037 | t = thread->transaction_stack; |
| 6038 | while (t) { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6039 | if (t->from == thread) { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 6040 | print_binder_transaction_ilocked(m, thread->proc, |
| 6041 | " outgoing transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6042 | t = t->from_parent; |
| 6043 | } else if (t->to_thread == thread) { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 6044 | print_binder_transaction_ilocked(m, thread->proc, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6045 | " incoming transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6046 | t = t->to_parent; |
| 6047 | } else { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 6048 | print_binder_transaction_ilocked(m, thread->proc, |
| 6049 | " bad transaction", t); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6050 | t = NULL; |
| 6051 | } |
| 6052 | } |
| 6053 | list_for_each_entry(w, &thread->todo, entry) { |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 6054 | print_binder_work_ilocked(m, thread->proc, " ", |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6055 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6056 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6057 | if (!print_always && m->count == header_pos) |
| 6058 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6059 | } |
| 6060 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 6061 | static void print_binder_node_nilocked(struct seq_file *m, |
| 6062 | struct binder_node *node) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6063 | { |
| 6064 | struct binder_ref *ref; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6065 | struct binder_work *w; |
| 6066 | int count; |
| 6067 | |
| 6068 | count = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 6069 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6070 | count++; |
| 6071 | |
Martijn Coenen | 69308b3 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 6072 | seq_printf(m, " node %d: u%016llx c%016llx pri %d:%d hs %d hw %d ls %d lw %d is %d iw %d tr %d", |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 6073 | node->debug_id, (u64)node->ptr, (u64)node->cookie, |
Martijn Coenen | 69308b3 | 2017-06-07 09:29:14 -0700 | [diff] [blame] | 6074 | node->sched_policy, node->min_priority, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6075 | node->has_strong_ref, node->has_weak_ref, |
| 6076 | node->local_strong_refs, node->local_weak_refs, |
Todd Kjos | adc1884 | 2017-06-29 12:01:59 -0700 | [diff] [blame] | 6077 | node->internal_strong_refs, count, node->tmp_refs); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6078 | if (count) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6079 | seq_puts(m, " proc"); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 6080 | hlist_for_each_entry(ref, &node->refs, node_entry) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6081 | seq_printf(m, " %d", ref->proc->pid); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6082 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6083 | seq_puts(m, "\n"); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6084 | if (node->proc) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6085 | list_for_each_entry(w, &node->async_todo, entry) |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 6086 | print_binder_work_ilocked(m, node->proc, " ", |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6087 | " pending async transaction", w); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6088 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6089 | } |
| 6090 | |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 6091 | static void print_binder_ref_olocked(struct seq_file *m, |
| 6092 | struct binder_ref *ref) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6093 | { |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 6094 | binder_node_lock(ref->node); |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 6095 | seq_printf(m, " ref %d: desc %d %snode %d s %d w %d d %pK\n", |
| 6096 | ref->data.debug_id, ref->data.desc, |
| 6097 | ref->node->proc ? "" : "dead ", |
| 6098 | ref->node->debug_id, ref->data.strong, |
| 6099 | ref->data.weak, ref->death); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 6100 | binder_node_unlock(ref->node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6101 | } |
| 6102 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6103 | static void print_binder_proc(struct seq_file *m, |
| 6104 | struct binder_proc *proc, int print_all) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6105 | { |
| 6106 | struct binder_work *w; |
| 6107 | struct rb_node *n; |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6108 | size_t start_pos = m->count; |
| 6109 | size_t header_pos; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 6110 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6111 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6112 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 6113 | seq_printf(m, "context %s\n", proc->context->name); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6114 | header_pos = m->count; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6115 | |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6116 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6117 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6118 | 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] | 6119 | rb_node), print_all); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 6120 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6121 | 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] | 6122 | struct binder_node *node = rb_entry(n, struct binder_node, |
| 6123 | rb_node); |
Todd Kjos | cd4b755 | 2018-12-05 15:19:26 -0800 | [diff] [blame] | 6124 | if (!print_all && !node->has_async_transaction) |
| 6125 | continue; |
| 6126 | |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 6127 | /* |
| 6128 | * take a temporary reference on the node so it |
| 6129 | * survives and isn't removed from the tree |
| 6130 | * while we print it. |
| 6131 | */ |
| 6132 | binder_inc_node_tmpref_ilocked(node); |
| 6133 | /* Need to drop inner lock to take node lock */ |
| 6134 | binder_inner_proc_unlock(proc); |
| 6135 | if (last_node) |
| 6136 | binder_put_node(last_node); |
| 6137 | binder_node_inner_lock(node); |
| 6138 | print_binder_node_nilocked(m, node); |
| 6139 | binder_node_inner_unlock(node); |
| 6140 | last_node = node; |
| 6141 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6142 | } |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 6143 | binder_inner_proc_unlock(proc); |
| 6144 | if (last_node) |
| 6145 | binder_put_node(last_node); |
| 6146 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6147 | if (print_all) { |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 6148 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6149 | for (n = rb_first(&proc->refs_by_desc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6150 | n != NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6151 | n = rb_next(n)) |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 6152 | print_binder_ref_olocked(m, rb_entry(n, |
| 6153 | struct binder_ref, |
| 6154 | rb_node_desc)); |
| 6155 | binder_proc_unlock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6156 | } |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 6157 | binder_alloc_print_allocated(m, &proc->alloc); |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6158 | binder_inner_proc_lock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6159 | list_for_each_entry(w, &proc->todo, entry) |
Todd Kjos | 5f2f636 | 2017-06-29 12:02:09 -0700 | [diff] [blame] | 6160 | print_binder_work_ilocked(m, proc, " ", |
| 6161 | " pending transaction", w); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6162 | list_for_each_entry(w, &proc->delivered_death, entry) { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6163 | seq_puts(m, " has delivered dead binder\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6164 | break; |
| 6165 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6166 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6167 | if (!print_all && m->count == header_pos) |
| 6168 | m->count = start_pos; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6169 | } |
| 6170 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 6171 | static const char * const binder_return_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6172 | "BR_ERROR", |
| 6173 | "BR_OK", |
| 6174 | "BR_TRANSACTION", |
| 6175 | "BR_REPLY", |
| 6176 | "BR_ACQUIRE_RESULT", |
| 6177 | "BR_DEAD_REPLY", |
| 6178 | "BR_TRANSACTION_COMPLETE", |
| 6179 | "BR_INCREFS", |
| 6180 | "BR_ACQUIRE", |
| 6181 | "BR_RELEASE", |
| 6182 | "BR_DECREFS", |
| 6183 | "BR_ATTEMPT_ACQUIRE", |
| 6184 | "BR_NOOP", |
| 6185 | "BR_SPAWN_LOOPER", |
| 6186 | "BR_FINISHED", |
| 6187 | "BR_DEAD_BINDER", |
| 6188 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", |
Hang Lu | 115e9e7 | 2021-04-09 17:40:45 +0800 | [diff] [blame] | 6189 | "BR_FAILED_REPLY", |
| 6190 | "BR_FROZEN_REPLY", |
Hang Lu | 4dbc168 | 2021-04-09 17:40:46 +0800 | [diff] [blame] | 6191 | "BR_ONEWAY_SPAM_SUSPECT", |
Li Li | af1f984 | 2022-11-23 12:16:54 -0800 | [diff] [blame] | 6192 | "BR_TRANSACTION_PENDING_FROZEN" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6193 | }; |
| 6194 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 6195 | static const char * const binder_command_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6196 | "BC_TRANSACTION", |
| 6197 | "BC_REPLY", |
| 6198 | "BC_ACQUIRE_RESULT", |
| 6199 | "BC_FREE_BUFFER", |
| 6200 | "BC_INCREFS", |
| 6201 | "BC_ACQUIRE", |
| 6202 | "BC_RELEASE", |
| 6203 | "BC_DECREFS", |
| 6204 | "BC_INCREFS_DONE", |
| 6205 | "BC_ACQUIRE_DONE", |
| 6206 | "BC_ATTEMPT_ACQUIRE", |
| 6207 | "BC_REGISTER_LOOPER", |
| 6208 | "BC_ENTER_LOOPER", |
| 6209 | "BC_EXIT_LOOPER", |
| 6210 | "BC_REQUEST_DEATH_NOTIFICATION", |
| 6211 | "BC_CLEAR_DEATH_NOTIFICATION", |
Martijn Coenen | 7980240 | 2017-02-03 14:40:51 -0800 | [diff] [blame] | 6212 | "BC_DEAD_BINDER_DONE", |
| 6213 | "BC_TRANSACTION_SG", |
| 6214 | "BC_REPLY_SG", |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6215 | }; |
| 6216 | |
Cruz Julian Bishop | 167bccb | 2012-12-22 09:00:45 +1000 | [diff] [blame] | 6217 | static const char * const binder_objstat_strings[] = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6218 | "proc", |
| 6219 | "thread", |
| 6220 | "node", |
| 6221 | "ref", |
| 6222 | "death", |
| 6223 | "transaction", |
| 6224 | "transaction_complete" |
| 6225 | }; |
| 6226 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6227 | static void print_binder_stats(struct seq_file *m, const char *prefix, |
| 6228 | struct binder_stats *stats) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6229 | { |
| 6230 | int i; |
| 6231 | |
| 6232 | BUILD_BUG_ON(ARRAY_SIZE(stats->bc) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6233 | ARRAY_SIZE(binder_command_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6234 | for (i = 0; i < ARRAY_SIZE(stats->bc); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 6235 | int temp = atomic_read(&stats->bc[i]); |
| 6236 | |
| 6237 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6238 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 6239 | binder_command_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6240 | } |
| 6241 | |
| 6242 | BUILD_BUG_ON(ARRAY_SIZE(stats->br) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6243 | ARRAY_SIZE(binder_return_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6244 | for (i = 0; i < ARRAY_SIZE(stats->br); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 6245 | int temp = atomic_read(&stats->br[i]); |
| 6246 | |
| 6247 | if (temp) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6248 | seq_printf(m, "%s%s: %d\n", prefix, |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 6249 | binder_return_strings[i], temp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6250 | } |
| 6251 | |
| 6252 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6253 | ARRAY_SIZE(binder_objstat_strings)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6254 | BUILD_BUG_ON(ARRAY_SIZE(stats->obj_created) != |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6255 | ARRAY_SIZE(stats->obj_deleted)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6256 | for (i = 0; i < ARRAY_SIZE(stats->obj_created); i++) { |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 6257 | int created = atomic_read(&stats->obj_created[i]); |
| 6258 | int deleted = atomic_read(&stats->obj_deleted[i]); |
| 6259 | |
| 6260 | if (created || deleted) |
| 6261 | seq_printf(m, "%s%s: active %d total %d\n", |
| 6262 | prefix, |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6263 | binder_objstat_strings[i], |
Badhri Jagan Sridharan | 0953c79 | 2017-06-29 12:01:44 -0700 | [diff] [blame] | 6264 | created - deleted, |
| 6265 | created); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6266 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6267 | } |
| 6268 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6269 | static void print_binder_proc_stats(struct seq_file *m, |
| 6270 | struct binder_proc *proc) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6271 | { |
| 6272 | struct binder_work *w; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 6273 | struct binder_thread *thread; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6274 | struct rb_node *n; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 6275 | int count, strong, weak, ready_threads; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 6276 | size_t free_async_space = |
| 6277 | binder_alloc_get_free_async_space(&proc->alloc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6278 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6279 | seq_printf(m, "proc %d\n", proc->pid); |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 6280 | seq_printf(m, "context %s\n", proc->context->name); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6281 | count = 0; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 6282 | ready_threads = 0; |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 6283 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6284 | for (n = rb_first(&proc->threads); n != NULL; n = rb_next(n)) |
| 6285 | count++; |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 6286 | |
| 6287 | list_for_each_entry(thread, &proc->waiting_threads, waiting_thread_node) |
| 6288 | ready_threads++; |
| 6289 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6290 | seq_printf(m, " threads: %d\n", count); |
| 6291 | seq_printf(m, " requested threads: %d+%d/%d\n" |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6292 | " ready threads %d\n" |
| 6293 | " free async space %zd\n", proc->requested_threads, |
| 6294 | proc->requested_threads_started, proc->max_threads, |
Martijn Coenen | 1b77e9d | 2017-08-31 10:04:18 +0200 | [diff] [blame] | 6295 | ready_threads, |
Todd Kjos | 7bd7b0e | 2017-06-29 12:02:05 -0700 | [diff] [blame] | 6296 | free_async_space); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6297 | count = 0; |
| 6298 | for (n = rb_first(&proc->nodes); n != NULL; n = rb_next(n)) |
| 6299 | count++; |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 6300 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6301 | seq_printf(m, " nodes: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6302 | count = 0; |
| 6303 | strong = 0; |
| 6304 | weak = 0; |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 6305 | binder_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6306 | for (n = rb_first(&proc->refs_by_desc); n != NULL; n = rb_next(n)) { |
| 6307 | struct binder_ref *ref = rb_entry(n, struct binder_ref, |
| 6308 | rb_node_desc); |
| 6309 | count++; |
Todd Kjos | 372e314 | 2017-06-29 12:01:58 -0700 | [diff] [blame] | 6310 | strong += ref->data.strong; |
| 6311 | weak += ref->data.weak; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6312 | } |
Todd Kjos | 2c1838d | 2017-06-29 12:02:08 -0700 | [diff] [blame] | 6313 | binder_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6314 | 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] | 6315 | |
Todd Kjos | 19c9872 | 2017-06-29 12:01:40 -0700 | [diff] [blame] | 6316 | count = binder_alloc_get_allocated_count(&proc->alloc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6317 | seq_printf(m, " buffers: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6318 | |
Sherry Yang | 8ef4665 | 2017-08-31 11:56:36 -0700 | [diff] [blame] | 6319 | binder_alloc_print_pages(m, &proc->alloc); |
| 6320 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6321 | count = 0; |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6322 | binder_inner_proc_lock(proc); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6323 | list_for_each_entry(w, &proc->todo, entry) { |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6324 | if (w->type == BINDER_WORK_TRANSACTION) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6325 | count++; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6326 | } |
Todd Kjos | 7219639 | 2017-06-29 12:02:02 -0700 | [diff] [blame] | 6327 | binder_inner_proc_unlock(proc); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6328 | seq_printf(m, " pending transactions: %d\n", count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6329 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6330 | print_binder_stats(m, " ", &proc->stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6331 | } |
| 6332 | |
| 6333 | |
Hridya Valsaraju | a575fb2 | 2019-11-01 15:22:42 -0700 | [diff] [blame] | 6334 | int binder_state_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6335 | { |
| 6336 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6337 | struct binder_node *node; |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 6338 | struct binder_node *last_node = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6339 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6340 | seq_puts(m, "binder state:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6341 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 6342 | spin_lock(&binder_dead_nodes_lock); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6343 | if (!hlist_empty(&binder_dead_nodes)) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6344 | seq_puts(m, "dead nodes:\n"); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 6345 | hlist_for_each_entry(node, &binder_dead_nodes, dead_node) { |
| 6346 | /* |
| 6347 | * take a temporary reference on the node so it |
| 6348 | * survives and isn't removed from the list |
| 6349 | * while we print it. |
| 6350 | */ |
| 6351 | node->tmp_refs++; |
| 6352 | spin_unlock(&binder_dead_nodes_lock); |
| 6353 | if (last_node) |
| 6354 | binder_put_node(last_node); |
| 6355 | binder_node_lock(node); |
Todd Kjos | da0fa9e | 2017-06-29 12:02:04 -0700 | [diff] [blame] | 6356 | print_binder_node_nilocked(m, node); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 6357 | binder_node_unlock(node); |
| 6358 | last_node = node; |
| 6359 | spin_lock(&binder_dead_nodes_lock); |
| 6360 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 6361 | spin_unlock(&binder_dead_nodes_lock); |
Todd Kjos | 673068e | 2017-06-29 12:02:03 -0700 | [diff] [blame] | 6362 | if (last_node) |
| 6363 | binder_put_node(last_node); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6364 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 6365 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 6366 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6367 | print_binder_proc(m, proc, 1); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 6368 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 6369 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6370 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6371 | } |
| 6372 | |
Hridya Valsaraju | a575fb2 | 2019-11-01 15:22:42 -0700 | [diff] [blame] | 6373 | int binder_stats_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6374 | { |
| 6375 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6376 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6377 | seq_puts(m, "binder stats:\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6378 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6379 | print_binder_stats(m, "", &binder_stats); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6380 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 6381 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 6382 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6383 | print_binder_proc_stats(m, proc); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 6384 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 6385 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6386 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6387 | } |
| 6388 | |
Hridya Valsaraju | a575fb2 | 2019-11-01 15:22:42 -0700 | [diff] [blame] | 6389 | int binder_transactions_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6390 | { |
| 6391 | struct binder_proc *proc; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6392 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6393 | seq_puts(m, "binder transactions:\n"); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 6394 | mutex_lock(&binder_procs_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 6395 | hlist_for_each_entry(proc, &binder_procs, proc_node) |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6396 | print_binder_proc(m, proc, 0); |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 6397 | mutex_unlock(&binder_procs_lock); |
Todd Kjos | a60b890 | 2017-06-29 12:02:11 -0700 | [diff] [blame] | 6398 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6399 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6400 | } |
| 6401 | |
Yangtao Li | 8b2246a | 2018-11-30 20:26:30 -0500 | [diff] [blame] | 6402 | static int proc_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6403 | { |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 6404 | struct binder_proc *itr; |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 6405 | int pid = (unsigned long)m->private; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6406 | |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 6407 | mutex_lock(&binder_procs_lock); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 6408 | hlist_for_each_entry(itr, &binder_procs, proc_node) { |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 6409 | if (itr->pid == pid) { |
| 6410 | seq_puts(m, "binder proc state:\n"); |
| 6411 | print_binder_proc(m, itr, 1); |
Riley Andrews | 83050a4 | 2016-02-09 21:05:33 -0800 | [diff] [blame] | 6412 | } |
| 6413 | } |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 6414 | mutex_unlock(&binder_procs_lock); |
| 6415 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6416 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6417 | } |
| 6418 | |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6419 | static void print_binder_transaction_log_entry(struct seq_file *m, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6420 | struct binder_transaction_log_entry *e) |
| 6421 | { |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 6422 | int debug_id = READ_ONCE(e->debug_id_done); |
| 6423 | /* |
| 6424 | * read barrier to guarantee debug_id_done read before |
| 6425 | * we print the log values |
| 6426 | */ |
| 6427 | smp_rmb(); |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6428 | seq_printf(m, |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 6429 | "%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] | 6430 | e->debug_id, (e->call_type == 2) ? "reply" : |
| 6431 | ((e->call_type == 1) ? "async" : "call "), e->from_proc, |
Martijn Coenen | 14db318 | 2017-02-03 14:40:47 -0800 | [diff] [blame] | 6432 | e->from_thread, e->to_proc, e->to_thread, e->context_name, |
Todd Kjos | 57ada2f | 2017-06-29 12:01:46 -0700 | [diff] [blame] | 6433 | e->to_node, e->target_handle, e->data_size, e->offsets_size, |
| 6434 | e->return_error, e->return_error_param, |
| 6435 | e->return_error_line); |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 6436 | /* |
| 6437 | * read-barrier to guarantee read of debug_id_done after |
| 6438 | * done printing the fields of the entry |
| 6439 | */ |
| 6440 | smp_rmb(); |
| 6441 | seq_printf(m, debug_id && debug_id == READ_ONCE(e->debug_id_done) ? |
| 6442 | "\n" : " (incomplete)\n"); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6443 | } |
| 6444 | |
Hridya Valsaraju | 3741393 | 2019-09-03 09:16:54 -0700 | [diff] [blame] | 6445 | int binder_transaction_log_show(struct seq_file *m, void *unused) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6446 | { |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6447 | struct binder_transaction_log *log = m->private; |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 6448 | unsigned int log_cur = atomic_read(&log->cur); |
| 6449 | unsigned int count; |
| 6450 | unsigned int cur; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6451 | int i; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6452 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 6453 | count = log_cur + 1; |
| 6454 | cur = count < ARRAY_SIZE(log->entry) && !log->full ? |
| 6455 | 0 : count % ARRAY_SIZE(log->entry); |
| 6456 | if (count > ARRAY_SIZE(log->entry) || log->full) |
| 6457 | count = ARRAY_SIZE(log->entry); |
| 6458 | for (i = 0; i < count; i++) { |
| 6459 | unsigned int index = cur++ % ARRAY_SIZE(log->entry); |
| 6460 | |
| 6461 | print_binder_transaction_log_entry(m, &log->entry[index]); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6462 | } |
Arve Hjønnevåg | 5249f48 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6463 | return 0; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6464 | } |
| 6465 | |
Christian Brauner | 34fbd92 | 2018-12-14 13:11:14 +0100 | [diff] [blame] | 6466 | const struct file_operations binder_fops = { |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6467 | .owner = THIS_MODULE, |
| 6468 | .poll = binder_poll, |
| 6469 | .unlocked_ioctl = binder_ioctl, |
Arve Hjønnevåg | da49889 | 2014-02-21 14:40:26 -0800 | [diff] [blame] | 6470 | .compat_ioctl = binder_ioctl, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6471 | .mmap = binder_mmap, |
| 6472 | .open = binder_open, |
| 6473 | .flush = binder_flush, |
| 6474 | .release = binder_release, |
| 6475 | }; |
| 6476 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 6477 | static int __init init_binder_device(const char *name) |
| 6478 | { |
| 6479 | int ret; |
| 6480 | struct binder_device *binder_device; |
| 6481 | |
| 6482 | binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL); |
| 6483 | if (!binder_device) |
| 6484 | return -ENOMEM; |
| 6485 | |
| 6486 | binder_device->miscdev.fops = &binder_fops; |
| 6487 | binder_device->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 6488 | binder_device->miscdev.name = name; |
| 6489 | |
Christian Brauner | bbfd216 | 2020-03-03 17:43:40 +0100 | [diff] [blame] | 6490 | refcount_set(&binder_device->ref, 1); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 6491 | binder_device->context.binder_context_mgr_uid = INVALID_UID; |
| 6492 | binder_device->context.name = name; |
Todd Kjos | c44b123 | 2017-06-29 12:01:43 -0700 | [diff] [blame] | 6493 | mutex_init(&binder_device->context.context_mgr_node_lock); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 6494 | |
| 6495 | ret = misc_register(&binder_device->miscdev); |
| 6496 | if (ret < 0) { |
| 6497 | kfree(binder_device); |
| 6498 | return ret; |
| 6499 | } |
| 6500 | |
| 6501 | hlist_add_head(&binder_device->hlist, &binder_devices); |
| 6502 | |
| 6503 | return ret; |
| 6504 | } |
| 6505 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6506 | static int __init binder_init(void) |
| 6507 | { |
| 6508 | int ret; |
Christian Brauner | bf4b5da | 2019-01-31 01:25:02 +0100 | [diff] [blame] | 6509 | char *device_name, *device_tmp; |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 6510 | struct binder_device *device; |
| 6511 | struct hlist_node *tmp; |
Christian Brauner | bf4b5da | 2019-01-31 01:25:02 +0100 | [diff] [blame] | 6512 | char *device_names = NULL; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6513 | |
Tetsuo Handa | 2db8e0b | 2017-11-29 22:29:47 +0900 | [diff] [blame] | 6514 | ret = binder_alloc_shrinker_init(); |
| 6515 | if (ret) |
| 6516 | return ret; |
Sherry Yang | f2517eb | 2017-08-23 08:46:42 -0700 | [diff] [blame] | 6517 | |
Todd Kjos | d99c733 | 2017-06-29 12:01:53 -0700 | [diff] [blame] | 6518 | atomic_set(&binder_transaction_log.cur, ~0U); |
| 6519 | atomic_set(&binder_transaction_log_failed.cur, ~0U); |
| 6520 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6521 | binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); |
| 6522 | if (binder_debugfs_dir_entry_root) |
| 6523 | binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", |
| 6524 | binder_debugfs_dir_entry_root); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 6525 | |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6526 | if (binder_debugfs_dir_entry_root) { |
| 6527 | debugfs_create_file("state", |
Harsh Shandilya | c5b47d2 | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6528 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6529 | binder_debugfs_dir_entry_root, |
| 6530 | NULL, |
Hridya Valsaraju | a575fb2 | 2019-11-01 15:22:42 -0700 | [diff] [blame] | 6531 | &binder_state_fops); |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6532 | debugfs_create_file("stats", |
Harsh Shandilya | c5b47d2 | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6533 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6534 | binder_debugfs_dir_entry_root, |
| 6535 | NULL, |
Hridya Valsaraju | a575fb2 | 2019-11-01 15:22:42 -0700 | [diff] [blame] | 6536 | &binder_stats_fops); |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6537 | debugfs_create_file("transactions", |
Harsh Shandilya | c5b47d2 | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6538 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6539 | binder_debugfs_dir_entry_root, |
| 6540 | NULL, |
Hridya Valsaraju | a575fb2 | 2019-11-01 15:22:42 -0700 | [diff] [blame] | 6541 | &binder_transactions_fops); |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6542 | debugfs_create_file("transaction_log", |
Harsh Shandilya | c5b47d2 | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6543 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6544 | binder_debugfs_dir_entry_root, |
| 6545 | &binder_transaction_log, |
Hridya Valsaraju | 3741393 | 2019-09-03 09:16:54 -0700 | [diff] [blame] | 6546 | &binder_transaction_log_fops); |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6547 | debugfs_create_file("failed_transaction_log", |
Harsh Shandilya | c5b47d2 | 2017-12-22 19:37:02 +0530 | [diff] [blame] | 6548 | 0444, |
Arve Hjønnevåg | 16b6655 | 2009-04-28 20:57:50 -0700 | [diff] [blame] | 6549 | binder_debugfs_dir_entry_root, |
| 6550 | &binder_transaction_log_failed, |
Hridya Valsaraju | 3741393 | 2019-09-03 09:16:54 -0700 | [diff] [blame] | 6551 | &binder_transaction_log_fops); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6552 | } |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 6553 | |
Hridya Valsaraju | e8fb393 | 2019-08-08 15:27:25 -0700 | [diff] [blame] | 6554 | if (!IS_ENABLED(CONFIG_ANDROID_BINDERFS) && |
| 6555 | strcmp(binder_devices_param, "") != 0) { |
Christian Brauner | 574e6b0 | 2019-01-26 11:23:20 +0100 | [diff] [blame] | 6556 | /* |
| 6557 | * Copy the module_parameter string, because we don't want to |
| 6558 | * tokenize it in-place. |
| 6559 | */ |
| 6560 | device_names = kstrdup(binder_devices_param, GFP_KERNEL); |
| 6561 | if (!device_names) { |
| 6562 | ret = -ENOMEM; |
| 6563 | goto err_alloc_device_names_failed; |
| 6564 | } |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 6565 | |
Christian Brauner | 574e6b0 | 2019-01-26 11:23:20 +0100 | [diff] [blame] | 6566 | device_tmp = device_names; |
| 6567 | while ((device_name = strsep(&device_tmp, ","))) { |
| 6568 | ret = init_binder_device(device_name); |
| 6569 | if (ret) |
| 6570 | goto err_init_binder_device_failed; |
| 6571 | } |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 6572 | } |
| 6573 | |
Christian Brauner | bf4b5da | 2019-01-31 01:25:02 +0100 | [diff] [blame] | 6574 | ret = init_binderfs(); |
| 6575 | if (ret) |
| 6576 | goto err_init_binder_device_failed; |
| 6577 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 6578 | return ret; |
| 6579 | |
| 6580 | err_init_binder_device_failed: |
| 6581 | hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) { |
| 6582 | misc_deregister(&device->miscdev); |
| 6583 | hlist_del(&device->hlist); |
| 6584 | kfree(device); |
| 6585 | } |
Christian Brauner | 22eb947 | 2017-08-21 16:13:28 +0200 | [diff] [blame] | 6586 | |
| 6587 | kfree(device_names); |
| 6588 | |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 6589 | err_alloc_device_names_failed: |
| 6590 | debugfs_remove_recursive(binder_debugfs_dir_entry_root); |
Qi Zheng | 486dd74 | 2023-06-25 15:49:37 +0000 | [diff] [blame] | 6591 | binder_alloc_shrinker_exit(); |
Martijn Coenen | ac4812c | 2017-02-03 14:40:48 -0800 | [diff] [blame] | 6592 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6593 | return ret; |
| 6594 | } |
| 6595 | |
| 6596 | device_initcall(binder_init); |
| 6597 | |
Arve Hjønnevåg | 975a1ac | 2012-10-16 15:29:53 -0700 | [diff] [blame] | 6598 | #define CREATE_TRACE_POINTS |
| 6599 | #include "binder_trace.h" |
| 6600 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 6601 | MODULE_LICENSE("GPL v2"); |