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