1 // SPDX-License-Identifier: GPL-2.0-only 2 /* binder_alloc.c 3 * 4 * Android IPC Subsystem 5 * 6 * Copyright (C) 2007-2017 Google, Inc. 7 */ 8 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/list.h> 12 #include <linux/sched/mm.h> 13 #include <linux/module.h> 14 #include <linux/rtmutex.h> 15 #include <linux/rbtree.h> 16 #include <linux/seq_file.h> 17 #include <linux/vmalloc.h> 18 #include <linux/slab.h> 19 #include <linux/sched.h> 20 #include <linux/list_lru.h> 21 #include <linux/ratelimit.h> 22 #include <asm/cacheflush.h> 23 #include <linux/uaccess.h> 24 #include <linux/highmem.h> 25 #include <linux/sizes.h> 26 #include "binder_alloc.h" 27 #include "binder_trace.h" 28 29 struct list_lru binder_freelist; 30 31 static DEFINE_MUTEX(binder_alloc_mmap_lock); 32 33 enum { 34 BINDER_DEBUG_USER_ERROR = 1U << 0, 35 BINDER_DEBUG_OPEN_CLOSE = 1U << 1, 36 BINDER_DEBUG_BUFFER_ALLOC = 1U << 2, 37 BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 3, 38 }; 39 static uint32_t binder_alloc_debug_mask = BINDER_DEBUG_USER_ERROR; 40 41 module_param_named(debug_mask, binder_alloc_debug_mask, 42 uint, 0644); 43 44 #define binder_alloc_debug(mask, x...) \ 45 do { \ 46 if (binder_alloc_debug_mask & mask) \ 47 pr_info_ratelimited(x); \ 48 } while (0) 49 50 static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer) 51 { 52 return list_entry(buffer->entry.next, struct binder_buffer, entry); 53 } 54 55 static struct binder_buffer *binder_buffer_prev(struct binder_buffer *buffer) 56 { 57 return list_entry(buffer->entry.prev, struct binder_buffer, entry); 58 } 59 60 static size_t binder_alloc_buffer_size(struct binder_alloc *alloc, 61 struct binder_buffer *buffer) 62 { 63 if (list_is_last(&buffer->entry, &alloc->buffers)) 64 return alloc->vm_start + alloc->buffer_size - buffer->user_data; 65 return binder_buffer_next(buffer)->user_data - buffer->user_data; 66 } 67 68 static void binder_insert_free_buffer(struct binder_alloc *alloc, 69 struct binder_buffer *new_buffer) 70 { 71 struct rb_node **p = &alloc->free_buffers.rb_node; 72 struct rb_node *parent = NULL; 73 struct binder_buffer *buffer; 74 size_t buffer_size; 75 size_t new_buffer_size; 76 77 BUG_ON(!new_buffer->free); 78 79 new_buffer_size = binder_alloc_buffer_size(alloc, new_buffer); 80 81 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, 82 "%d: add free buffer, size %zd, at %pK\n", 83 alloc->pid, new_buffer_size, new_buffer); 84 85 while (*p) { 86 parent = *p; 87 buffer = rb_entry(parent, struct binder_buffer, rb_node); 88 BUG_ON(!buffer->free); 89 90 buffer_size = binder_alloc_buffer_size(alloc, buffer); 91 92 if (new_buffer_size < buffer_size) 93 p = &parent->rb_left; 94 else 95 p = &parent->rb_right; 96 } 97 rb_link_node(&new_buffer->rb_node, parent, p); 98 rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers); 99 } 100 101 static void binder_insert_allocated_buffer_locked( 102 struct binder_alloc *alloc, struct binder_buffer *new_buffer) 103 { 104 struct rb_node **p = &alloc->allocated_buffers.rb_node; 105 struct rb_node *parent = NULL; 106 struct binder_buffer *buffer; 107 108 BUG_ON(new_buffer->free); 109 110 while (*p) { 111 parent = *p; 112 buffer = rb_entry(parent, struct binder_buffer, rb_node); 113 BUG_ON(buffer->free); 114 115 if (new_buffer->user_data < buffer->user_data) 116 p = &parent->rb_left; 117 else if (new_buffer->user_data > buffer->user_data) 118 p = &parent->rb_right; 119 else 120 BUG(); 121 } 122 rb_link_node(&new_buffer->rb_node, parent, p); 123 rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers); 124 } 125 126 static struct binder_buffer *binder_alloc_prepare_to_free_locked( 127 struct binder_alloc *alloc, 128 unsigned long user_ptr) 129 { 130 struct rb_node *n = alloc->allocated_buffers.rb_node; 131 struct binder_buffer *buffer; 132 133 while (n) { 134 buffer = rb_entry(n, struct binder_buffer, rb_node); 135 BUG_ON(buffer->free); 136 137 if (user_ptr < buffer->user_data) { 138 n = n->rb_left; 139 } else if (user_ptr > buffer->user_data) { 140 n = n->rb_right; 141 } else { 142 /* 143 * Guard against user threads attempting to 144 * free the buffer when in use by kernel or 145 * after it's already been freed. 146 */ 147 if (!buffer->allow_user_free) 148 return ERR_PTR(-EPERM); 149 buffer->allow_user_free = 0; 150 return buffer; 151 } 152 } 153 return NULL; 154 } 155 156 /** 157 * binder_alloc_prepare_to_free() - get buffer given user ptr 158 * @alloc: binder_alloc for this proc 159 * @user_ptr: User pointer to buffer data 160 * 161 * Validate userspace pointer to buffer data and return buffer corresponding to 162 * that user pointer. Search the rb tree for buffer that matches user data 163 * pointer. 164 * 165 * Return: Pointer to buffer or NULL 166 */ 167 struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc, 168 unsigned long user_ptr) 169 { 170 struct binder_buffer *buffer; 171 172 mutex_lock(&alloc->mutex); 173 buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr); 174 mutex_unlock(&alloc->mutex); 175 return buffer; 176 } 177 178 static inline void 179 binder_set_installed_page(struct binder_alloc *alloc, 180 unsigned long index, 181 struct page *page) 182 { 183 /* Pairs with acquire in binder_get_installed_page() */ 184 smp_store_release(&alloc->pages[index], page); 185 } 186 187 static inline struct page * 188 binder_get_installed_page(struct binder_alloc *alloc, unsigned long index) 189 { 190 /* Pairs with release in binder_set_installed_page() */ 191 return smp_load_acquire(&alloc->pages[index]); 192 } 193 194 static void binder_lru_freelist_add(struct binder_alloc *alloc, 195 unsigned long start, unsigned long end) 196 { 197 unsigned long page_addr; 198 struct page *page; 199 200 trace_binder_update_page_range(alloc, false, start, end); 201 202 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) { 203 size_t index; 204 int ret; 205 206 index = (page_addr - alloc->vm_start) / PAGE_SIZE; 207 page = binder_get_installed_page(alloc, index); 208 if (!page) 209 continue; 210 211 trace_binder_free_lru_start(alloc, index); 212 213 ret = list_lru_add(&binder_freelist, 214 page_to_lru(page), 215 page_to_nid(page), 216 NULL); 217 WARN_ON(!ret); 218 219 trace_binder_free_lru_end(alloc, index); 220 } 221 } 222 223 static inline 224 void binder_alloc_set_mapped(struct binder_alloc *alloc, bool state) 225 { 226 /* pairs with smp_load_acquire in binder_alloc_is_mapped() */ 227 smp_store_release(&alloc->mapped, state); 228 } 229 230 static inline bool binder_alloc_is_mapped(struct binder_alloc *alloc) 231 { 232 /* pairs with smp_store_release in binder_alloc_set_mapped() */ 233 return smp_load_acquire(&alloc->mapped); 234 } 235 236 static struct page *binder_page_lookup(struct binder_alloc *alloc, 237 unsigned long addr) 238 { 239 struct mm_struct *mm = alloc->mm; 240 struct page *page; 241 long npages = 0; 242 243 /* 244 * Find an existing page in the remote mm. If missing, 245 * don't attempt to fault-in just propagate an error. 246 */ 247 mmap_read_lock(mm); 248 if (binder_alloc_is_mapped(alloc)) 249 npages = get_user_pages_remote(mm, addr, 1, FOLL_NOFAULT, 250 &page, NULL); 251 mmap_read_unlock(mm); 252 253 return npages > 0 ? page : NULL; 254 } 255 256 static int binder_page_insert(struct binder_alloc *alloc, 257 unsigned long addr, 258 struct page *page) 259 { 260 struct mm_struct *mm = alloc->mm; 261 struct vm_area_struct *vma; 262 int ret = -ESRCH; 263 264 /* attempt per-vma lock first */ 265 vma = lock_vma_under_rcu(mm, addr); 266 if (vma) { 267 if (binder_alloc_is_mapped(alloc)) 268 ret = vm_insert_page(vma, addr, page); 269 vma_end_read(vma); 270 return ret; 271 } 272 273 /* fall back to mmap_lock */ 274 mmap_read_lock(mm); 275 vma = vma_lookup(mm, addr); 276 if (vma && binder_alloc_is_mapped(alloc)) 277 ret = vm_insert_page(vma, addr, page); 278 mmap_read_unlock(mm); 279 280 return ret; 281 } 282 283 static struct page *binder_page_alloc(struct binder_alloc *alloc, 284 unsigned long index) 285 { 286 struct binder_shrinker_mdata *mdata; 287 struct page *page; 288 289 page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO); 290 if (!page) 291 return NULL; 292 293 /* allocate and install shrinker metadata under page->private */ 294 mdata = kzalloc(sizeof(*mdata), GFP_KERNEL); 295 if (!mdata) { 296 __free_page(page); 297 return NULL; 298 } 299 300 mdata->alloc = alloc; 301 mdata->page_index = index; 302 INIT_LIST_HEAD(&mdata->lru); 303 set_page_private(page, (unsigned long)mdata); 304 305 return page; 306 } 307 308 static void binder_free_page(struct page *page) 309 { 310 kfree((struct binder_shrinker_mdata *)page_private(page)); 311 __free_page(page); 312 } 313 314 static int binder_install_single_page(struct binder_alloc *alloc, 315 unsigned long index, 316 unsigned long addr) 317 { 318 struct page *page; 319 int ret; 320 321 if (!mmget_not_zero(alloc->mm)) 322 return -ESRCH; 323 324 page = binder_page_alloc(alloc, index); 325 if (!page) { 326 ret = -ENOMEM; 327 goto out; 328 } 329 330 ret = binder_page_insert(alloc, addr, page); 331 switch (ret) { 332 case -EBUSY: 333 /* 334 * EBUSY is ok. Someone installed the pte first but the 335 * alloc->pages[index] has not been updated yet. Discard 336 * our page and look up the one already installed. 337 */ 338 ret = 0; 339 binder_free_page(page); 340 page = binder_page_lookup(alloc, addr); 341 if (!page) { 342 pr_err("%d: failed to find page at offset %lx\n", 343 alloc->pid, addr - alloc->vm_start); 344 ret = -ESRCH; 345 break; 346 } 347 fallthrough; 348 case 0: 349 /* Mark page installation complete and safe to use */ 350 binder_set_installed_page(alloc, index, page); 351 break; 352 default: 353 binder_free_page(page); 354 pr_err("%d: %s failed to insert page at offset %lx with %d\n", 355 alloc->pid, __func__, addr - alloc->vm_start, ret); 356 ret = -ENOMEM; 357 break; 358 } 359 out: 360 mmput_async(alloc->mm); 361 return ret; 362 } 363 364 static int binder_install_buffer_pages(struct binder_alloc *alloc, 365 struct binder_buffer *buffer, 366 size_t size) 367 { 368 unsigned long start, final; 369 unsigned long page_addr; 370 371 start = buffer->user_data & PAGE_MASK; 372 final = PAGE_ALIGN(buffer->user_data + size); 373 374 for (page_addr = start; page_addr < final; page_addr += PAGE_SIZE) { 375 unsigned long index; 376 int ret; 377 378 index = (page_addr - alloc->vm_start) / PAGE_SIZE; 379 if (binder_get_installed_page(alloc, index)) 380 continue; 381 382 trace_binder_alloc_page_start(alloc, index); 383 384 ret = binder_install_single_page(alloc, index, page_addr); 385 if (ret) 386 return ret; 387 388 trace_binder_alloc_page_end(alloc, index); 389 } 390 391 return 0; 392 } 393 394 /* The range of pages should exclude those shared with other buffers */ 395 static void binder_lru_freelist_del(struct binder_alloc *alloc, 396 unsigned long start, unsigned long end) 397 { 398 unsigned long page_addr; 399 struct page *page; 400 401 trace_binder_update_page_range(alloc, true, start, end); 402 403 for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) { 404 unsigned long index; 405 bool on_lru; 406 407 index = (page_addr - alloc->vm_start) / PAGE_SIZE; 408 page = binder_get_installed_page(alloc, index); 409 410 if (page) { 411 trace_binder_alloc_lru_start(alloc, index); 412 413 on_lru = list_lru_del(&binder_freelist, 414 page_to_lru(page), 415 page_to_nid(page), 416 NULL); 417 WARN_ON(!on_lru); 418 419 trace_binder_alloc_lru_end(alloc, index); 420 continue; 421 } 422 423 if (index + 1 > alloc->pages_high) 424 alloc->pages_high = index + 1; 425 } 426 } 427 428 static void debug_no_space_locked(struct binder_alloc *alloc) 429 { 430 size_t largest_alloc_size = 0; 431 struct binder_buffer *buffer; 432 size_t allocated_buffers = 0; 433 size_t largest_free_size = 0; 434 size_t total_alloc_size = 0; 435 size_t total_free_size = 0; 436 size_t free_buffers = 0; 437 size_t buffer_size; 438 struct rb_node *n; 439 440 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) { 441 buffer = rb_entry(n, struct binder_buffer, rb_node); 442 buffer_size = binder_alloc_buffer_size(alloc, buffer); 443 allocated_buffers++; 444 total_alloc_size += buffer_size; 445 if (buffer_size > largest_alloc_size) 446 largest_alloc_size = buffer_size; 447 } 448 449 for (n = rb_first(&alloc->free_buffers); n; n = rb_next(n)) { 450 buffer = rb_entry(n, struct binder_buffer, rb_node); 451 buffer_size = binder_alloc_buffer_size(alloc, buffer); 452 free_buffers++; 453 total_free_size += buffer_size; 454 if (buffer_size > largest_free_size) 455 largest_free_size = buffer_size; 456 } 457 458 binder_alloc_debug(BINDER_DEBUG_USER_ERROR, 459 "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n", 460 total_alloc_size, allocated_buffers, 461 largest_alloc_size, total_free_size, 462 free_buffers, largest_free_size); 463 } 464 465 static bool debug_low_async_space_locked(struct binder_alloc *alloc) 466 { 467 /* 468 * Find the amount and size of buffers allocated by the current caller; 469 * The idea is that once we cross the threshold, whoever is responsible 470 * for the low async space is likely to try to send another async txn, 471 * and at some point we'll catch them in the act. This is more efficient 472 * than keeping a map per pid. 473 */ 474 struct binder_buffer *buffer; 475 size_t total_alloc_size = 0; 476 int pid = current->tgid; 477 size_t num_buffers = 0; 478 struct rb_node *n; 479 480 /* 481 * Only start detecting spammers once we have less than 20% of async 482 * space left (which is less than 10% of total buffer size). 483 */ 484 if (alloc->free_async_space >= alloc->buffer_size / 10) { 485 alloc->oneway_spam_detected = false; 486 return false; 487 } 488 489 for (n = rb_first(&alloc->allocated_buffers); n != NULL; 490 n = rb_next(n)) { 491 buffer = rb_entry(n, struct binder_buffer, rb_node); 492 if (buffer->pid != pid) 493 continue; 494 if (!buffer->async_transaction) 495 continue; 496 total_alloc_size += binder_alloc_buffer_size(alloc, buffer); 497 num_buffers++; 498 } 499 500 /* 501 * Warn if this pid has more than 50 transactions, or more than 50% of 502 * async space (which is 25% of total buffer size). Oneway spam is only 503 * detected when the threshold is exceeded. 504 */ 505 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) { 506 binder_alloc_debug(BINDER_DEBUG_USER_ERROR, 507 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n", 508 alloc->pid, pid, num_buffers, total_alloc_size); 509 if (!alloc->oneway_spam_detected) { 510 alloc->oneway_spam_detected = true; 511 return true; 512 } 513 } 514 return false; 515 } 516 517 /* Callers preallocate @new_buffer, it is freed by this function if unused */ 518 static struct binder_buffer *binder_alloc_new_buf_locked( 519 struct binder_alloc *alloc, 520 struct binder_buffer *new_buffer, 521 size_t size, 522 int is_async) 523 { 524 struct rb_node *n = alloc->free_buffers.rb_node; 525 struct rb_node *best_fit = NULL; 526 struct binder_buffer *buffer; 527 unsigned long next_used_page; 528 unsigned long curr_last_page; 529 size_t buffer_size; 530 531 if (is_async && alloc->free_async_space < size) { 532 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, 533 "%d: binder_alloc_buf size %zd failed, no async space left\n", 534 alloc->pid, size); 535 buffer = ERR_PTR(-ENOSPC); 536 goto out; 537 } 538 539 while (n) { 540 buffer = rb_entry(n, struct binder_buffer, rb_node); 541 BUG_ON(!buffer->free); 542 buffer_size = binder_alloc_buffer_size(alloc, buffer); 543 544 if (size < buffer_size) { 545 best_fit = n; 546 n = n->rb_left; 547 } else if (size > buffer_size) { 548 n = n->rb_right; 549 } else { 550 best_fit = n; 551 break; 552 } 553 } 554 555 if (unlikely(!best_fit)) { 556 binder_alloc_debug(BINDER_DEBUG_USER_ERROR, 557 "%d: binder_alloc_buf size %zd failed, no address space\n", 558 alloc->pid, size); 559 debug_no_space_locked(alloc); 560 buffer = ERR_PTR(-ENOSPC); 561 goto out; 562 } 563 564 if (buffer_size != size) { 565 /* Found an oversized buffer and needs to be split */ 566 buffer = rb_entry(best_fit, struct binder_buffer, rb_node); 567 buffer_size = binder_alloc_buffer_size(alloc, buffer); 568 569 WARN_ON(n || buffer_size == size); 570 new_buffer->user_data = buffer->user_data + size; 571 list_add(&new_buffer->entry, &buffer->entry); 572 new_buffer->free = 1; 573 binder_insert_free_buffer(alloc, new_buffer); 574 new_buffer = NULL; 575 } 576 577 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, 578 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n", 579 alloc->pid, size, buffer, buffer_size); 580 581 /* 582 * Now we remove the pages from the freelist. A clever calculation 583 * with buffer_size determines if the last page is shared with an 584 * adjacent in-use buffer. In such case, the page has been already 585 * removed from the freelist so we trim our range short. 586 */ 587 next_used_page = (buffer->user_data + buffer_size) & PAGE_MASK; 588 curr_last_page = PAGE_ALIGN(buffer->user_data + size); 589 binder_lru_freelist_del(alloc, PAGE_ALIGN(buffer->user_data), 590 min(next_used_page, curr_last_page)); 591 592 rb_erase(&buffer->rb_node, &alloc->free_buffers); 593 buffer->free = 0; 594 buffer->allow_user_free = 0; 595 binder_insert_allocated_buffer_locked(alloc, buffer); 596 buffer->async_transaction = is_async; 597 buffer->oneway_spam_suspect = false; 598 if (is_async) { 599 alloc->free_async_space -= size; 600 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC, 601 "%d: binder_alloc_buf size %zd async free %zd\n", 602 alloc->pid, size, alloc->free_async_space); 603 if (debug_low_async_space_locked(alloc)) 604 buffer->oneway_spam_suspect = true; 605 } 606 607 out: 608 /* Discard possibly unused new_buffer */ 609 kfree(new_buffer); 610 return buffer; 611 } 612 613 /* Calculate the sanitized total size, returns 0 for invalid request */ 614 static inline size_t sanitized_size(size_t data_size, 615 size_t offsets_size, 616 size_t extra_buffers_size) 617 { 618 size_t total, tmp; 619 620 /* Align to pointer size and check for overflows */ 621 tmp = ALIGN(data_size, sizeof(void *)) + 622 ALIGN(offsets_size, sizeof(void *)); 623 if (tmp < data_size || tmp < offsets_size) 624 return 0; 625 total = tmp + ALIGN(extra_buffers_size, sizeof(void *)); 626 if (total < tmp || total < extra_buffers_size) 627 return 0; 628 629 /* Pad 0-sized buffers so they get a unique address */ 630 total = max(total, sizeof(void *)); 631 632 return total; 633 } 634 635 /** 636 * binder_alloc_new_buf() - Allocate a new binder buffer 637 * @alloc: binder_alloc for this proc 638 * @data_size: size of user data buffer 639 * @offsets_size: user specified buffer offset 640 * @extra_buffers_size: size of extra space for meta-data (eg, security context) 641 * @is_async: buffer for async transaction 642 * 643 * Allocate a new buffer given the requested sizes. Returns 644 * the kernel version of the buffer pointer. The size allocated 645 * is the sum of the three given sizes (each rounded up to 646 * pointer-sized boundary) 647 * 648 * Return: The allocated buffer or %ERR_PTR(-errno) if error 649 */ 650 struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc, 651 size_t data_size, 652 size_t offsets_size, 653 size_t extra_buffers_size, 654 int is_async) 655 { 656 struct binder_buffer *buffer, *next; 657 size_t size; 658 int ret; 659 660 /* Check binder_alloc is fully initialized */ 661 if (!binder_alloc_is_mapped(alloc)) { 662 binder_alloc_debug(BINDER_DEBUG_USER_ERROR, 663 "%d: binder_alloc_buf, no vma\n", 664 alloc->pid); 665 return ERR_PTR(-ESRCH); 666 } 667 668 size = sanitized_size(data_size, offsets_size, extra_buffers_size); 669 if (unlikely(!size)) { 670 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, 671 "%d: got transaction with invalid size %zd-%zd-%zd\n", 672 alloc->pid, data_size, offsets_size, 673 extra_buffers_size); 674 return ERR_PTR(-EINVAL); 675 } 676 677 /* Preallocate the next buffer */ 678 next = kzalloc(sizeof(*next), GFP_KERNEL); 679 if (!next) 680 return ERR_PTR(-ENOMEM); 681 682 mutex_lock(&alloc->mutex); 683 buffer = binder_alloc_new_buf_locked(alloc, next, size, is_async); 684 if (IS_ERR(buffer)) { 685 mutex_unlock(&alloc->mutex); 686 goto out; 687 } 688 689 buffer->data_size = data_size; 690 buffer->offsets_size = offsets_size; 691 buffer->extra_buffers_size = extra_buffers_size; 692 buffer->pid = current->tgid; 693 mutex_unlock(&alloc->mutex); 694 695 ret = binder_install_buffer_pages(alloc, buffer, size); 696 if (ret) { 697 binder_alloc_free_buf(alloc, buffer); 698 buffer = ERR_PTR(ret); 699 } 700 out: 701 return buffer; 702 } 703 704 static unsigned long buffer_start_page(struct binder_buffer *buffer) 705 { 706 return buffer->user_data & PAGE_MASK; 707 } 708 709 static unsigned long prev_buffer_end_page(struct binder_buffer *buffer) 710 { 711 return (buffer->user_data - 1) & PAGE_MASK; 712 } 713 714 static void binder_delete_free_buffer(struct binder_alloc *alloc, 715 struct binder_buffer *buffer) 716 { 717 struct binder_buffer *prev, *next; 718 719 if (PAGE_ALIGNED(buffer->user_data)) 720 goto skip_freelist; 721 722 BUG_ON(alloc->buffers.next == &buffer->entry); 723 prev = binder_buffer_prev(buffer); 724 BUG_ON(!prev->free); 725 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) 726 goto skip_freelist; 727 728 if (!list_is_last(&buffer->entry, &alloc->buffers)) { 729 next = binder_buffer_next(buffer); 730 if (buffer_start_page(next) == buffer_start_page(buffer)) 731 goto skip_freelist; 732 } 733 734 binder_lru_freelist_add(alloc, buffer_start_page(buffer), 735 buffer_start_page(buffer) + PAGE_SIZE); 736 skip_freelist: 737 list_del(&buffer->entry); 738 kfree(buffer); 739 } 740 741 static void binder_free_buf_locked(struct binder_alloc *alloc, 742 struct binder_buffer *buffer) 743 { 744 size_t size, buffer_size; 745 746 buffer_size = binder_alloc_buffer_size(alloc, buffer); 747 748 size = ALIGN(buffer->data_size, sizeof(void *)) + 749 ALIGN(buffer->offsets_size, sizeof(void *)) + 750 ALIGN(buffer->extra_buffers_size, sizeof(void *)); 751 752 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, 753 "%d: binder_free_buf %pK size %zd buffer_size %zd\n", 754 alloc->pid, buffer, size, buffer_size); 755 756 BUG_ON(buffer->free); 757 BUG_ON(size > buffer_size); 758 BUG_ON(buffer->transaction != NULL); 759 BUG_ON(buffer->user_data < alloc->vm_start); 760 BUG_ON(buffer->user_data > alloc->vm_start + alloc->buffer_size); 761 762 if (buffer->async_transaction) { 763 alloc->free_async_space += buffer_size; 764 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC, 765 "%d: binder_free_buf size %zd async free %zd\n", 766 alloc->pid, size, alloc->free_async_space); 767 } 768 769 binder_lru_freelist_add(alloc, PAGE_ALIGN(buffer->user_data), 770 (buffer->user_data + buffer_size) & PAGE_MASK); 771 772 rb_erase(&buffer->rb_node, &alloc->allocated_buffers); 773 buffer->free = 1; 774 if (!list_is_last(&buffer->entry, &alloc->buffers)) { 775 struct binder_buffer *next = binder_buffer_next(buffer); 776 777 if (next->free) { 778 rb_erase(&next->rb_node, &alloc->free_buffers); 779 binder_delete_free_buffer(alloc, next); 780 } 781 } 782 if (alloc->buffers.next != &buffer->entry) { 783 struct binder_buffer *prev = binder_buffer_prev(buffer); 784 785 if (prev->free) { 786 binder_delete_free_buffer(alloc, buffer); 787 rb_erase(&prev->rb_node, &alloc->free_buffers); 788 buffer = prev; 789 } 790 } 791 binder_insert_free_buffer(alloc, buffer); 792 } 793 794 /** 795 * binder_alloc_get_page() - get kernel pointer for given buffer offset 796 * @alloc: binder_alloc for this proc 797 * @buffer: binder buffer to be accessed 798 * @buffer_offset: offset into @buffer data 799 * @pgoffp: address to copy final page offset to 800 * 801 * Lookup the struct page corresponding to the address 802 * at @buffer_offset into @buffer->user_data. If @pgoffp is not 803 * NULL, the byte-offset into the page is written there. 804 * 805 * The caller is responsible to ensure that the offset points 806 * to a valid address within the @buffer and that @buffer is 807 * not freeable by the user. Since it can't be freed, we are 808 * guaranteed that the corresponding elements of @alloc->pages[] 809 * cannot change. 810 * 811 * Return: struct page 812 */ 813 static struct page *binder_alloc_get_page(struct binder_alloc *alloc, 814 struct binder_buffer *buffer, 815 binder_size_t buffer_offset, 816 pgoff_t *pgoffp) 817 { 818 binder_size_t buffer_space_offset = buffer_offset + 819 (buffer->user_data - alloc->vm_start); 820 pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK; 821 size_t index = buffer_space_offset >> PAGE_SHIFT; 822 823 *pgoffp = pgoff; 824 825 return alloc->pages[index]; 826 } 827 828 /** 829 * binder_alloc_clear_buf() - zero out buffer 830 * @alloc: binder_alloc for this proc 831 * @buffer: binder buffer to be cleared 832 * 833 * memset the given buffer to 0 834 */ 835 static void binder_alloc_clear_buf(struct binder_alloc *alloc, 836 struct binder_buffer *buffer) 837 { 838 size_t bytes = binder_alloc_buffer_size(alloc, buffer); 839 binder_size_t buffer_offset = 0; 840 841 while (bytes) { 842 unsigned long size; 843 struct page *page; 844 pgoff_t pgoff; 845 846 page = binder_alloc_get_page(alloc, buffer, 847 buffer_offset, &pgoff); 848 size = min_t(size_t, bytes, PAGE_SIZE - pgoff); 849 memset_page(page, pgoff, 0, size); 850 bytes -= size; 851 buffer_offset += size; 852 } 853 } 854 855 /** 856 * binder_alloc_free_buf() - free a binder buffer 857 * @alloc: binder_alloc for this proc 858 * @buffer: kernel pointer to buffer 859 * 860 * Free the buffer allocated via binder_alloc_new_buf() 861 */ 862 void binder_alloc_free_buf(struct binder_alloc *alloc, 863 struct binder_buffer *buffer) 864 { 865 /* 866 * We could eliminate the call to binder_alloc_clear_buf() 867 * from binder_alloc_deferred_release() by moving this to 868 * binder_free_buf_locked(). However, that could 869 * increase contention for the alloc mutex if clear_on_free 870 * is used frequently for large buffers. The mutex is not 871 * needed for correctness here. 872 */ 873 if (buffer->clear_on_free) { 874 binder_alloc_clear_buf(alloc, buffer); 875 buffer->clear_on_free = false; 876 } 877 mutex_lock(&alloc->mutex); 878 binder_free_buf_locked(alloc, buffer); 879 mutex_unlock(&alloc->mutex); 880 } 881 882 /** 883 * binder_alloc_mmap_handler() - map virtual address space for proc 884 * @alloc: alloc structure for this proc 885 * @vma: vma passed to mmap() 886 * 887 * Called by binder_mmap() to initialize the space specified in 888 * vma for allocating binder buffers 889 * 890 * Return: 891 * 0 = success 892 * -EBUSY = address space already mapped 893 * -ENOMEM = failed to map memory to given address space 894 */ 895 int binder_alloc_mmap_handler(struct binder_alloc *alloc, 896 struct vm_area_struct *vma) 897 { 898 struct binder_buffer *buffer; 899 const char *failure_string; 900 int ret; 901 902 if (unlikely(vma->vm_mm != alloc->mm)) { 903 ret = -EINVAL; 904 failure_string = "invalid vma->vm_mm"; 905 goto err_invalid_mm; 906 } 907 908 mutex_lock(&binder_alloc_mmap_lock); 909 if (alloc->buffer_size) { 910 ret = -EBUSY; 911 failure_string = "already mapped"; 912 goto err_already_mapped; 913 } 914 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start, 915 SZ_4M); 916 mutex_unlock(&binder_alloc_mmap_lock); 917 918 alloc->vm_start = vma->vm_start; 919 920 alloc->pages = kvcalloc(alloc->buffer_size / PAGE_SIZE, 921 sizeof(alloc->pages[0]), 922 GFP_KERNEL); 923 if (!alloc->pages) { 924 ret = -ENOMEM; 925 failure_string = "alloc page array"; 926 goto err_alloc_pages_failed; 927 } 928 929 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); 930 if (!buffer) { 931 ret = -ENOMEM; 932 failure_string = "alloc buffer struct"; 933 goto err_alloc_buf_struct_failed; 934 } 935 936 buffer->user_data = alloc->vm_start; 937 list_add(&buffer->entry, &alloc->buffers); 938 buffer->free = 1; 939 binder_insert_free_buffer(alloc, buffer); 940 alloc->free_async_space = alloc->buffer_size / 2; 941 942 /* Signal binder_alloc is fully initialized */ 943 binder_alloc_set_mapped(alloc, true); 944 945 return 0; 946 947 err_alloc_buf_struct_failed: 948 kvfree(alloc->pages); 949 alloc->pages = NULL; 950 err_alloc_pages_failed: 951 alloc->vm_start = 0; 952 mutex_lock(&binder_alloc_mmap_lock); 953 alloc->buffer_size = 0; 954 err_already_mapped: 955 mutex_unlock(&binder_alloc_mmap_lock); 956 err_invalid_mm: 957 binder_alloc_debug(BINDER_DEBUG_USER_ERROR, 958 "%s: %d %lx-%lx %s failed %d\n", __func__, 959 alloc->pid, vma->vm_start, vma->vm_end, 960 failure_string, ret); 961 return ret; 962 } 963 964 965 void binder_alloc_deferred_release(struct binder_alloc *alloc) 966 { 967 struct rb_node *n; 968 int buffers, page_count; 969 struct binder_buffer *buffer; 970 971 buffers = 0; 972 mutex_lock(&alloc->mutex); 973 BUG_ON(alloc->mapped); 974 975 while ((n = rb_first(&alloc->allocated_buffers))) { 976 buffer = rb_entry(n, struct binder_buffer, rb_node); 977 978 /* Transaction should already have been freed */ 979 BUG_ON(buffer->transaction); 980 981 if (buffer->clear_on_free) { 982 binder_alloc_clear_buf(alloc, buffer); 983 buffer->clear_on_free = false; 984 } 985 binder_free_buf_locked(alloc, buffer); 986 buffers++; 987 } 988 989 while (!list_empty(&alloc->buffers)) { 990 buffer = list_first_entry(&alloc->buffers, 991 struct binder_buffer, entry); 992 WARN_ON(!buffer->free); 993 994 list_del(&buffer->entry); 995 WARN_ON_ONCE(!list_empty(&alloc->buffers)); 996 kfree(buffer); 997 } 998 999 page_count = 0; 1000 if (alloc->pages) { 1001 int i; 1002 1003 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) { 1004 struct page *page; 1005 bool on_lru; 1006 1007 page = binder_get_installed_page(alloc, i); 1008 if (!page) 1009 continue; 1010 1011 on_lru = list_lru_del(&binder_freelist, 1012 page_to_lru(page), 1013 page_to_nid(page), 1014 NULL); 1015 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, 1016 "%s: %d: page %d %s\n", 1017 __func__, alloc->pid, i, 1018 on_lru ? "on lru" : "active"); 1019 binder_free_page(page); 1020 page_count++; 1021 } 1022 } 1023 mutex_unlock(&alloc->mutex); 1024 kvfree(alloc->pages); 1025 if (alloc->mm) 1026 mmdrop(alloc->mm); 1027 1028 binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE, 1029 "%s: %d buffers %d, pages %d\n", 1030 __func__, alloc->pid, buffers, page_count); 1031 } 1032 1033 /** 1034 * binder_alloc_print_allocated() - print buffer info 1035 * @m: seq_file for output via seq_printf() 1036 * @alloc: binder_alloc for this proc 1037 * 1038 * Prints information about every buffer associated with 1039 * the binder_alloc state to the given seq_file 1040 */ 1041 void binder_alloc_print_allocated(struct seq_file *m, 1042 struct binder_alloc *alloc) 1043 { 1044 struct binder_buffer *buffer; 1045 struct rb_node *n; 1046 1047 mutex_lock(&alloc->mutex); 1048 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) { 1049 buffer = rb_entry(n, struct binder_buffer, rb_node); 1050 seq_printf(m, " buffer %d: %lx size %zd:%zd:%zd %s\n", 1051 buffer->debug_id, 1052 buffer->user_data - alloc->vm_start, 1053 buffer->data_size, buffer->offsets_size, 1054 buffer->extra_buffers_size, 1055 buffer->transaction ? "active" : "delivered"); 1056 } 1057 mutex_unlock(&alloc->mutex); 1058 } 1059 1060 /** 1061 * binder_alloc_print_pages() - print page usage 1062 * @m: seq_file for output via seq_printf() 1063 * @alloc: binder_alloc for this proc 1064 */ 1065 void binder_alloc_print_pages(struct seq_file *m, 1066 struct binder_alloc *alloc) 1067 { 1068 struct page *page; 1069 int i; 1070 int active = 0; 1071 int lru = 0; 1072 int free = 0; 1073 1074 mutex_lock(&alloc->mutex); 1075 /* 1076 * Make sure the binder_alloc is fully initialized, otherwise we might 1077 * read inconsistent state. 1078 */ 1079 if (binder_alloc_is_mapped(alloc)) { 1080 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) { 1081 page = binder_get_installed_page(alloc, i); 1082 if (!page) 1083 free++; 1084 else if (list_empty(page_to_lru(page))) 1085 active++; 1086 else 1087 lru++; 1088 } 1089 } 1090 mutex_unlock(&alloc->mutex); 1091 seq_printf(m, " pages: %d:%d:%d\n", active, lru, free); 1092 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high); 1093 } 1094 1095 /** 1096 * binder_alloc_get_allocated_count() - return count of buffers 1097 * @alloc: binder_alloc for this proc 1098 * 1099 * Return: count of allocated buffers 1100 */ 1101 int binder_alloc_get_allocated_count(struct binder_alloc *alloc) 1102 { 1103 struct rb_node *n; 1104 int count = 0; 1105 1106 mutex_lock(&alloc->mutex); 1107 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n)) 1108 count++; 1109 mutex_unlock(&alloc->mutex); 1110 return count; 1111 } 1112 1113 1114 /** 1115 * binder_alloc_vma_close() - invalidate address space 1116 * @alloc: binder_alloc for this proc 1117 * 1118 * Called from binder_vma_close() when releasing address space. 1119 * Clears alloc->mapped to prevent new incoming transactions from 1120 * allocating more buffers. 1121 */ 1122 void binder_alloc_vma_close(struct binder_alloc *alloc) 1123 { 1124 binder_alloc_set_mapped(alloc, false); 1125 } 1126 1127 /** 1128 * binder_alloc_free_page() - shrinker callback to free pages 1129 * @item: item to free 1130 * @lru: list_lru instance of the item 1131 * @cb_arg: callback argument 1132 * 1133 * Called from list_lru_walk() in binder_shrink_scan() to free 1134 * up pages when the system is under memory pressure. 1135 */ 1136 enum lru_status binder_alloc_free_page(struct list_head *item, 1137 struct list_lru_one *lru, 1138 void *cb_arg) 1139 __must_hold(&lru->lock) 1140 { 1141 struct binder_shrinker_mdata *mdata = container_of(item, typeof(*mdata), lru); 1142 struct binder_alloc *alloc = mdata->alloc; 1143 struct mm_struct *mm = alloc->mm; 1144 struct vm_area_struct *vma; 1145 struct page *page_to_free; 1146 unsigned long page_addr; 1147 size_t index; 1148 1149 if (!mmget_not_zero(mm)) 1150 goto err_mmget; 1151 if (!mmap_read_trylock(mm)) 1152 goto err_mmap_read_lock_failed; 1153 if (!mutex_trylock(&alloc->mutex)) 1154 goto err_get_alloc_mutex_failed; 1155 1156 index = mdata->page_index; 1157 page_addr = alloc->vm_start + index * PAGE_SIZE; 1158 1159 vma = vma_lookup(mm, page_addr); 1160 /* 1161 * Since a binder_alloc can only be mapped once, we ensure 1162 * the vma corresponds to this mapping by checking whether 1163 * the binder_alloc is still mapped. 1164 */ 1165 if (vma && !binder_alloc_is_mapped(alloc)) 1166 goto err_invalid_vma; 1167 1168 trace_binder_unmap_kernel_start(alloc, index); 1169 1170 page_to_free = alloc->pages[index]; 1171 binder_set_installed_page(alloc, index, NULL); 1172 1173 trace_binder_unmap_kernel_end(alloc, index); 1174 1175 list_lru_isolate(lru, item); 1176 spin_unlock(&lru->lock); 1177 1178 if (vma) { 1179 trace_binder_unmap_user_start(alloc, index); 1180 1181 zap_page_range_single(vma, page_addr, PAGE_SIZE, NULL); 1182 1183 trace_binder_unmap_user_end(alloc, index); 1184 } 1185 1186 mutex_unlock(&alloc->mutex); 1187 mmap_read_unlock(mm); 1188 mmput_async(mm); 1189 binder_free_page(page_to_free); 1190 1191 return LRU_REMOVED_RETRY; 1192 1193 err_invalid_vma: 1194 mutex_unlock(&alloc->mutex); 1195 err_get_alloc_mutex_failed: 1196 mmap_read_unlock(mm); 1197 err_mmap_read_lock_failed: 1198 mmput_async(mm); 1199 err_mmget: 1200 return LRU_SKIP; 1201 } 1202 1203 static unsigned long 1204 binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc) 1205 { 1206 return list_lru_count(&binder_freelist); 1207 } 1208 1209 static unsigned long 1210 binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) 1211 { 1212 return list_lru_walk(&binder_freelist, binder_alloc_free_page, 1213 NULL, sc->nr_to_scan); 1214 } 1215 1216 static struct shrinker *binder_shrinker; 1217 1218 /** 1219 * binder_alloc_init() - called by binder_open() for per-proc initialization 1220 * @alloc: binder_alloc for this proc 1221 * 1222 * Called from binder_open() to initialize binder_alloc fields for 1223 * new binder proc 1224 */ 1225 void binder_alloc_init(struct binder_alloc *alloc) 1226 { 1227 alloc->pid = current->group_leader->pid; 1228 alloc->mm = current->mm; 1229 mmgrab(alloc->mm); 1230 mutex_init(&alloc->mutex); 1231 INIT_LIST_HEAD(&alloc->buffers); 1232 } 1233 1234 int binder_alloc_shrinker_init(void) 1235 { 1236 int ret; 1237 1238 ret = list_lru_init(&binder_freelist); 1239 if (ret) 1240 return ret; 1241 1242 binder_shrinker = shrinker_alloc(0, "android-binder"); 1243 if (!binder_shrinker) { 1244 list_lru_destroy(&binder_freelist); 1245 return -ENOMEM; 1246 } 1247 1248 binder_shrinker->count_objects = binder_shrink_count; 1249 binder_shrinker->scan_objects = binder_shrink_scan; 1250 1251 shrinker_register(binder_shrinker); 1252 1253 return 0; 1254 } 1255 1256 void binder_alloc_shrinker_exit(void) 1257 { 1258 shrinker_free(binder_shrinker); 1259 list_lru_destroy(&binder_freelist); 1260 } 1261 1262 /** 1263 * check_buffer() - verify that buffer/offset is safe to access 1264 * @alloc: binder_alloc for this proc 1265 * @buffer: binder buffer to be accessed 1266 * @offset: offset into @buffer data 1267 * @bytes: bytes to access from offset 1268 * 1269 * Check that the @offset/@bytes are within the size of the given 1270 * @buffer and that the buffer is currently active and not freeable. 1271 * Offsets must also be multiples of sizeof(u32). The kernel is 1272 * allowed to touch the buffer in two cases: 1273 * 1274 * 1) when the buffer is being created: 1275 * (buffer->free == 0 && buffer->allow_user_free == 0) 1276 * 2) when the buffer is being torn down: 1277 * (buffer->free == 0 && buffer->transaction == NULL). 1278 * 1279 * Return: true if the buffer is safe to access 1280 */ 1281 static inline bool check_buffer(struct binder_alloc *alloc, 1282 struct binder_buffer *buffer, 1283 binder_size_t offset, size_t bytes) 1284 { 1285 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer); 1286 1287 return buffer_size >= bytes && 1288 offset <= buffer_size - bytes && 1289 IS_ALIGNED(offset, sizeof(u32)) && 1290 !buffer->free && 1291 (!buffer->allow_user_free || !buffer->transaction); 1292 } 1293 1294 /** 1295 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user 1296 * @alloc: binder_alloc for this proc 1297 * @buffer: binder buffer to be accessed 1298 * @buffer_offset: offset into @buffer data 1299 * @from: userspace pointer to source buffer 1300 * @bytes: bytes to copy 1301 * 1302 * Copy bytes from source userspace to target buffer. 1303 * 1304 * Return: bytes remaining to be copied 1305 */ 1306 unsigned long 1307 binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc, 1308 struct binder_buffer *buffer, 1309 binder_size_t buffer_offset, 1310 const void __user *from, 1311 size_t bytes) 1312 { 1313 if (!check_buffer(alloc, buffer, buffer_offset, bytes)) 1314 return bytes; 1315 1316 while (bytes) { 1317 unsigned long size; 1318 unsigned long ret; 1319 struct page *page; 1320 pgoff_t pgoff; 1321 void *kptr; 1322 1323 page = binder_alloc_get_page(alloc, buffer, 1324 buffer_offset, &pgoff); 1325 size = min_t(size_t, bytes, PAGE_SIZE - pgoff); 1326 kptr = kmap_local_page(page) + pgoff; 1327 ret = copy_from_user(kptr, from, size); 1328 kunmap_local(kptr); 1329 if (ret) 1330 return bytes - size + ret; 1331 bytes -= size; 1332 from += size; 1333 buffer_offset += size; 1334 } 1335 return 0; 1336 } 1337 1338 static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc, 1339 bool to_buffer, 1340 struct binder_buffer *buffer, 1341 binder_size_t buffer_offset, 1342 void *ptr, 1343 size_t bytes) 1344 { 1345 /* All copies must be 32-bit aligned and 32-bit size */ 1346 if (!check_buffer(alloc, buffer, buffer_offset, bytes)) 1347 return -EINVAL; 1348 1349 while (bytes) { 1350 unsigned long size; 1351 struct page *page; 1352 pgoff_t pgoff; 1353 1354 page = binder_alloc_get_page(alloc, buffer, 1355 buffer_offset, &pgoff); 1356 size = min_t(size_t, bytes, PAGE_SIZE - pgoff); 1357 if (to_buffer) 1358 memcpy_to_page(page, pgoff, ptr, size); 1359 else 1360 memcpy_from_page(ptr, page, pgoff, size); 1361 bytes -= size; 1362 pgoff = 0; 1363 ptr = ptr + size; 1364 buffer_offset += size; 1365 } 1366 return 0; 1367 } 1368 1369 int binder_alloc_copy_to_buffer(struct binder_alloc *alloc, 1370 struct binder_buffer *buffer, 1371 binder_size_t buffer_offset, 1372 void *src, 1373 size_t bytes) 1374 { 1375 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset, 1376 src, bytes); 1377 } 1378 1379 int binder_alloc_copy_from_buffer(struct binder_alloc *alloc, 1380 void *dest, 1381 struct binder_buffer *buffer, 1382 binder_size_t buffer_offset, 1383 size_t bytes) 1384 { 1385 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset, 1386 dest, bytes); 1387 } 1388