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