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