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