Lines Matching +full:buffer +full:- +full:size

1 // SPDX-License-Identifier: GPL-2.0-only
6 * Copyright (C) 2007-2017 Google, Inc.
50 static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer) in binder_buffer_next() argument
52 return list_entry(buffer->entry.next, struct binder_buffer, entry); in binder_buffer_next()
55 static struct binder_buffer *binder_buffer_prev(struct binder_buffer *buffer) in binder_buffer_prev() argument
57 return list_entry(buffer->entry.prev, struct binder_buffer, entry); in binder_buffer_prev()
61 struct binder_buffer *buffer) in binder_alloc_buffer_size() argument
63 if (list_is_last(&buffer->entry, &alloc->buffers)) in binder_alloc_buffer_size()
64 return alloc->buffer + alloc->buffer_size - buffer->user_data; in binder_alloc_buffer_size()
65 return binder_buffer_next(buffer)->user_data - buffer->user_data; in binder_alloc_buffer_size()
71 struct rb_node **p = &alloc->free_buffers.rb_node; in binder_insert_free_buffer()
73 struct binder_buffer *buffer; in binder_insert_free_buffer() local
77 BUG_ON(!new_buffer->free); in binder_insert_free_buffer()
82 "%d: add free buffer, size %zd, at %pK\n", in binder_insert_free_buffer()
83 alloc->pid, new_buffer_size, new_buffer); in binder_insert_free_buffer()
87 buffer = rb_entry(parent, struct binder_buffer, rb_node); in binder_insert_free_buffer()
88 BUG_ON(!buffer->free); in binder_insert_free_buffer()
90 buffer_size = binder_alloc_buffer_size(alloc, buffer); in binder_insert_free_buffer()
93 p = &parent->rb_left; in binder_insert_free_buffer()
95 p = &parent->rb_right; in binder_insert_free_buffer()
97 rb_link_node(&new_buffer->rb_node, parent, p); in binder_insert_free_buffer()
98 rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers); in binder_insert_free_buffer()
104 struct rb_node **p = &alloc->allocated_buffers.rb_node; in binder_insert_allocated_buffer_locked()
106 struct binder_buffer *buffer; in binder_insert_allocated_buffer_locked() local
108 BUG_ON(new_buffer->free); in binder_insert_allocated_buffer_locked()
112 buffer = rb_entry(parent, struct binder_buffer, rb_node); in binder_insert_allocated_buffer_locked()
113 BUG_ON(buffer->free); in binder_insert_allocated_buffer_locked()
115 if (new_buffer->user_data < buffer->user_data) in binder_insert_allocated_buffer_locked()
116 p = &parent->rb_left; in binder_insert_allocated_buffer_locked()
117 else if (new_buffer->user_data > buffer->user_data) in binder_insert_allocated_buffer_locked()
118 p = &parent->rb_right; in binder_insert_allocated_buffer_locked()
122 rb_link_node(&new_buffer->rb_node, parent, p); in binder_insert_allocated_buffer_locked()
123 rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers); in binder_insert_allocated_buffer_locked()
130 struct rb_node *n = alloc->allocated_buffers.rb_node; in binder_alloc_prepare_to_free_locked()
131 struct binder_buffer *buffer; in binder_alloc_prepare_to_free_locked() local
134 buffer = rb_entry(n, struct binder_buffer, rb_node); in binder_alloc_prepare_to_free_locked()
135 BUG_ON(buffer->free); in binder_alloc_prepare_to_free_locked()
137 if (user_ptr < buffer->user_data) { in binder_alloc_prepare_to_free_locked()
138 n = n->rb_left; in binder_alloc_prepare_to_free_locked()
139 } else if (user_ptr > buffer->user_data) { in binder_alloc_prepare_to_free_locked()
140 n = n->rb_right; in binder_alloc_prepare_to_free_locked()
144 * free the buffer when in use by kernel or in binder_alloc_prepare_to_free_locked()
147 if (!buffer->allow_user_free) in binder_alloc_prepare_to_free_locked()
148 return ERR_PTR(-EPERM); in binder_alloc_prepare_to_free_locked()
149 buffer->allow_user_free = 0; in binder_alloc_prepare_to_free_locked()
150 return buffer; in binder_alloc_prepare_to_free_locked()
157 * binder_alloc_prepare_to_free() - get buffer given user ptr
159 * @user_ptr: User pointer to buffer data
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
165 * Return: Pointer to buffer or NULL
170 struct binder_buffer *buffer; in binder_alloc_prepare_to_free() local
172 spin_lock(&alloc->lock); in binder_alloc_prepare_to_free()
173 buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr); in binder_alloc_prepare_to_free()
174 spin_unlock(&alloc->lock); in binder_alloc_prepare_to_free()
175 return buffer; in binder_alloc_prepare_to_free()
183 smp_store_release(&lru_page->page_ptr, page); in binder_set_installed_page()
190 return smp_load_acquire(&lru_page->page_ptr); in binder_get_installed_page()
205 index = (page_addr - alloc->buffer) / PAGE_SIZE; in binder_lru_freelist_add()
206 page = &alloc->pages[index]; in binder_lru_freelist_add()
213 ret = list_lru_add_obj(&binder_freelist, &page->lru); in binder_lru_freelist_add()
227 if (!mmget_not_zero(alloc->mm)) in binder_install_single_page()
228 return -ESRCH; in binder_install_single_page()
234 mmap_write_lock(alloc->mm); in binder_install_single_page()
238 if (!alloc->vma) { in binder_install_single_page()
239 pr_err("%d: %s failed, no vma\n", alloc->pid, __func__); in binder_install_single_page()
240 ret = -ESRCH; in binder_install_single_page()
246 pr_err("%d: failed to allocate page\n", alloc->pid); in binder_install_single_page()
247 ret = -ENOMEM; in binder_install_single_page()
251 ret = vm_insert_page(alloc->vma, addr, page); in binder_install_single_page()
254 alloc->pid, __func__, addr - alloc->buffer, ret); in binder_install_single_page()
256 ret = -ENOMEM; in binder_install_single_page()
263 mmap_write_unlock(alloc->mm); in binder_install_single_page()
264 mmput_async(alloc->mm); in binder_install_single_page()
269 struct binder_buffer *buffer, in binder_install_buffer_pages() argument
270 size_t size) in binder_install_buffer_pages() argument
276 start = buffer->user_data & PAGE_MASK; in binder_install_buffer_pages()
277 final = PAGE_ALIGN(buffer->user_data + size); in binder_install_buffer_pages()
283 index = (page_addr - alloc->buffer) / PAGE_SIZE; in binder_install_buffer_pages()
284 page = &alloc->pages[index]; in binder_install_buffer_pages()
314 index = (page_addr - alloc->buffer) / PAGE_SIZE; in binder_lru_freelist_del()
315 page = &alloc->pages[index]; in binder_lru_freelist_del()
317 if (page->page_ptr) { in binder_lru_freelist_del()
320 on_lru = list_lru_del_obj(&binder_freelist, &page->lru); in binder_lru_freelist_del()
327 if (index + 1 > alloc->pages_high) in binder_lru_freelist_del()
328 alloc->pages_high = index + 1; in binder_lru_freelist_del()
336 smp_store_release(&alloc->vma, vma); in binder_alloc_set_vma()
343 return smp_load_acquire(&alloc->vma); in binder_alloc_get_vma()
349 struct binder_buffer *buffer; in debug_no_space_locked() local
358 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) { in debug_no_space_locked()
359 buffer = rb_entry(n, struct binder_buffer, rb_node); in debug_no_space_locked()
360 buffer_size = binder_alloc_buffer_size(alloc, buffer); in debug_no_space_locked()
367 for (n = rb_first(&alloc->free_buffers); n; n = rb_next(n)) { in debug_no_space_locked()
368 buffer = rb_entry(n, struct binder_buffer, rb_node); in debug_no_space_locked()
369 buffer_size = binder_alloc_buffer_size(alloc, buffer); in debug_no_space_locked()
386 * Find the amount and size of buffers allocated by the current caller; in debug_low_async_space_locked()
392 struct binder_buffer *buffer; in debug_low_async_space_locked() local
394 int pid = current->tgid; in debug_low_async_space_locked()
400 * space left (which is less than 10% of total buffer size). in debug_low_async_space_locked()
402 if (alloc->free_async_space >= alloc->buffer_size / 10) { in debug_low_async_space_locked()
403 alloc->oneway_spam_detected = false; in debug_low_async_space_locked()
407 for (n = rb_first(&alloc->allocated_buffers); n != NULL; in debug_low_async_space_locked()
409 buffer = rb_entry(n, struct binder_buffer, rb_node); in debug_low_async_space_locked()
410 if (buffer->pid != pid) in debug_low_async_space_locked()
412 if (!buffer->async_transaction) in debug_low_async_space_locked()
414 total_alloc_size += binder_alloc_buffer_size(alloc, buffer); in debug_low_async_space_locked()
420 * async space (which is 25% of total buffer size). Oneway spam is only in debug_low_async_space_locked()
423 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) { in debug_low_async_space_locked()
425 "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n", in debug_low_async_space_locked()
426 alloc->pid, pid, num_buffers, total_alloc_size); in debug_low_async_space_locked()
427 if (!alloc->oneway_spam_detected) { in debug_low_async_space_locked()
428 alloc->oneway_spam_detected = true; in debug_low_async_space_locked()
439 size_t size, in binder_alloc_new_buf_locked() argument
442 struct rb_node *n = alloc->free_buffers.rb_node; in binder_alloc_new_buf_locked()
444 struct binder_buffer *buffer; in binder_alloc_new_buf_locked() local
449 if (is_async && alloc->free_async_space < size) { in binder_alloc_new_buf_locked()
451 "%d: binder_alloc_buf size %zd failed, no async space left\n", in binder_alloc_new_buf_locked()
452 alloc->pid, size); in binder_alloc_new_buf_locked()
453 buffer = ERR_PTR(-ENOSPC); in binder_alloc_new_buf_locked()
458 buffer = rb_entry(n, struct binder_buffer, rb_node); in binder_alloc_new_buf_locked()
459 BUG_ON(!buffer->free); in binder_alloc_new_buf_locked()
460 buffer_size = binder_alloc_buffer_size(alloc, buffer); in binder_alloc_new_buf_locked()
462 if (size < buffer_size) { in binder_alloc_new_buf_locked()
464 n = n->rb_left; in binder_alloc_new_buf_locked()
465 } else if (size > buffer_size) { in binder_alloc_new_buf_locked()
466 n = n->rb_right; in binder_alloc_new_buf_locked()
475 "%d: binder_alloc_buf size %zd failed, no address space\n", in binder_alloc_new_buf_locked()
476 alloc->pid, size); in binder_alloc_new_buf_locked()
478 buffer = ERR_PTR(-ENOSPC); in binder_alloc_new_buf_locked()
482 if (buffer_size != size) { in binder_alloc_new_buf_locked()
483 /* Found an oversized buffer and needs to be split */ in binder_alloc_new_buf_locked()
484 buffer = rb_entry(best_fit, struct binder_buffer, rb_node); in binder_alloc_new_buf_locked()
485 buffer_size = binder_alloc_buffer_size(alloc, buffer); in binder_alloc_new_buf_locked()
487 WARN_ON(n || buffer_size == size); in binder_alloc_new_buf_locked()
488 new_buffer->user_data = buffer->user_data + size; in binder_alloc_new_buf_locked()
489 list_add(&new_buffer->entry, &buffer->entry); in binder_alloc_new_buf_locked()
490 new_buffer->free = 1; in binder_alloc_new_buf_locked()
496 "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n", in binder_alloc_new_buf_locked()
497 alloc->pid, size, buffer, buffer_size); in binder_alloc_new_buf_locked()
502 * adjacent in-use buffer. In such case, the page has been already in binder_alloc_new_buf_locked()
505 next_used_page = (buffer->user_data + buffer_size) & PAGE_MASK; in binder_alloc_new_buf_locked()
506 curr_last_page = PAGE_ALIGN(buffer->user_data + size); in binder_alloc_new_buf_locked()
507 binder_lru_freelist_del(alloc, PAGE_ALIGN(buffer->user_data), in binder_alloc_new_buf_locked()
510 rb_erase(&buffer->rb_node, &alloc->free_buffers); in binder_alloc_new_buf_locked()
511 buffer->free = 0; in binder_alloc_new_buf_locked()
512 buffer->allow_user_free = 0; in binder_alloc_new_buf_locked()
513 binder_insert_allocated_buffer_locked(alloc, buffer); in binder_alloc_new_buf_locked()
514 buffer->async_transaction = is_async; in binder_alloc_new_buf_locked()
515 buffer->oneway_spam_suspect = false; in binder_alloc_new_buf_locked()
517 alloc->free_async_space -= size; in binder_alloc_new_buf_locked()
519 "%d: binder_alloc_buf size %zd async free %zd\n", in binder_alloc_new_buf_locked()
520 alloc->pid, size, alloc->free_async_space); in binder_alloc_new_buf_locked()
522 buffer->oneway_spam_suspect = true; in binder_alloc_new_buf_locked()
528 return buffer; in binder_alloc_new_buf_locked()
531 /* Calculate the sanitized total size, returns 0 for invalid request */
538 /* Align to pointer size and check for overflows */ in sanitized_size()
547 /* Pad 0-sized buffers so they get a unique address */ in sanitized_size()
554 * binder_alloc_new_buf() - Allocate a new binder buffer
556 * @data_size: size of user data buffer
557 * @offsets_size: user specified buffer offset
558 * @extra_buffers_size: size of extra space for meta-data (eg, security context)
559 * @is_async: buffer for async transaction
561 * Allocate a new buffer given the requested sizes. Returns
562 * the kernel version of the buffer pointer. The size allocated
564 * pointer-sized boundary)
566 * Return: The allocated buffer or %ERR_PTR(-errno) if error
574 struct binder_buffer *buffer, *next; in binder_alloc_new_buf() local
575 size_t size; in binder_alloc_new_buf() local
582 alloc->pid); in binder_alloc_new_buf()
583 return ERR_PTR(-ESRCH); in binder_alloc_new_buf()
586 size = sanitized_size(data_size, offsets_size, extra_buffers_size); in binder_alloc_new_buf()
587 if (unlikely(!size)) { in binder_alloc_new_buf()
589 "%d: got transaction with invalid size %zd-%zd-%zd\n", in binder_alloc_new_buf()
590 alloc->pid, data_size, offsets_size, in binder_alloc_new_buf()
592 return ERR_PTR(-EINVAL); in binder_alloc_new_buf()
595 /* Preallocate the next buffer */ in binder_alloc_new_buf()
598 return ERR_PTR(-ENOMEM); in binder_alloc_new_buf()
600 spin_lock(&alloc->lock); in binder_alloc_new_buf()
601 buffer = binder_alloc_new_buf_locked(alloc, next, size, is_async); in binder_alloc_new_buf()
602 if (IS_ERR(buffer)) { in binder_alloc_new_buf()
603 spin_unlock(&alloc->lock); in binder_alloc_new_buf()
607 buffer->data_size = data_size; in binder_alloc_new_buf()
608 buffer->offsets_size = offsets_size; in binder_alloc_new_buf()
609 buffer->extra_buffers_size = extra_buffers_size; in binder_alloc_new_buf()
610 buffer->pid = current->tgid; in binder_alloc_new_buf()
611 spin_unlock(&alloc->lock); in binder_alloc_new_buf()
613 ret = binder_install_buffer_pages(alloc, buffer, size); in binder_alloc_new_buf()
615 binder_alloc_free_buf(alloc, buffer); in binder_alloc_new_buf()
616 buffer = ERR_PTR(ret); in binder_alloc_new_buf()
619 return buffer; in binder_alloc_new_buf()
622 static unsigned long buffer_start_page(struct binder_buffer *buffer) in buffer_start_page() argument
624 return buffer->user_data & PAGE_MASK; in buffer_start_page()
627 static unsigned long prev_buffer_end_page(struct binder_buffer *buffer) in prev_buffer_end_page() argument
629 return (buffer->user_data - 1) & PAGE_MASK; in prev_buffer_end_page()
633 struct binder_buffer *buffer) in binder_delete_free_buffer() argument
637 if (PAGE_ALIGNED(buffer->user_data)) in binder_delete_free_buffer()
640 BUG_ON(alloc->buffers.next == &buffer->entry); in binder_delete_free_buffer()
641 prev = binder_buffer_prev(buffer); in binder_delete_free_buffer()
642 BUG_ON(!prev->free); in binder_delete_free_buffer()
643 if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) in binder_delete_free_buffer()
646 if (!list_is_last(&buffer->entry, &alloc->buffers)) { in binder_delete_free_buffer()
647 next = binder_buffer_next(buffer); in binder_delete_free_buffer()
648 if (buffer_start_page(next) == buffer_start_page(buffer)) in binder_delete_free_buffer()
652 binder_lru_freelist_add(alloc, buffer_start_page(buffer), in binder_delete_free_buffer()
653 buffer_start_page(buffer) + PAGE_SIZE); in binder_delete_free_buffer()
655 list_del(&buffer->entry); in binder_delete_free_buffer()
656 kfree(buffer); in binder_delete_free_buffer()
660 struct binder_buffer *buffer) in binder_free_buf_locked() argument
662 size_t size, buffer_size; in binder_free_buf_locked() local
664 buffer_size = binder_alloc_buffer_size(alloc, buffer); in binder_free_buf_locked()
666 size = ALIGN(buffer->data_size, sizeof(void *)) + in binder_free_buf_locked()
667 ALIGN(buffer->offsets_size, sizeof(void *)) + in binder_free_buf_locked()
668 ALIGN(buffer->extra_buffers_size, sizeof(void *)); in binder_free_buf_locked()
671 "%d: binder_free_buf %pK size %zd buffer_size %zd\n", in binder_free_buf_locked()
672 alloc->pid, buffer, size, buffer_size); in binder_free_buf_locked()
674 BUG_ON(buffer->free); in binder_free_buf_locked()
675 BUG_ON(size > buffer_size); in binder_free_buf_locked()
676 BUG_ON(buffer->transaction != NULL); in binder_free_buf_locked()
677 BUG_ON(buffer->user_data < alloc->buffer); in binder_free_buf_locked()
678 BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size); in binder_free_buf_locked()
680 if (buffer->async_transaction) { in binder_free_buf_locked()
681 alloc->free_async_space += buffer_size; in binder_free_buf_locked()
683 "%d: binder_free_buf size %zd async free %zd\n", in binder_free_buf_locked()
684 alloc->pid, size, alloc->free_async_space); in binder_free_buf_locked()
687 binder_lru_freelist_add(alloc, PAGE_ALIGN(buffer->user_data), in binder_free_buf_locked()
688 (buffer->user_data + buffer_size) & PAGE_MASK); in binder_free_buf_locked()
690 rb_erase(&buffer->rb_node, &alloc->allocated_buffers); in binder_free_buf_locked()
691 buffer->free = 1; in binder_free_buf_locked()
692 if (!list_is_last(&buffer->entry, &alloc->buffers)) { in binder_free_buf_locked()
693 struct binder_buffer *next = binder_buffer_next(buffer); in binder_free_buf_locked()
695 if (next->free) { in binder_free_buf_locked()
696 rb_erase(&next->rb_node, &alloc->free_buffers); in binder_free_buf_locked()
700 if (alloc->buffers.next != &buffer->entry) { in binder_free_buf_locked()
701 struct binder_buffer *prev = binder_buffer_prev(buffer); in binder_free_buf_locked()
703 if (prev->free) { in binder_free_buf_locked()
704 binder_delete_free_buffer(alloc, buffer); in binder_free_buf_locked()
705 rb_erase(&prev->rb_node, &alloc->free_buffers); in binder_free_buf_locked()
706 buffer = prev; in binder_free_buf_locked()
709 binder_insert_free_buffer(alloc, buffer); in binder_free_buf_locked()
713 * binder_alloc_get_page() - get kernel pointer for given buffer offset
715 * @buffer: binder buffer to be accessed
716 * @buffer_offset: offset into @buffer data
720 * at @buffer_offset into @buffer->user_data. If @pgoffp is not
721 * NULL, the byte-offset into the page is written there.
724 * to a valid address within the @buffer and that @buffer is
726 * guaranteed that the corresponding elements of @alloc->pages[]
732 struct binder_buffer *buffer, in binder_alloc_get_page() argument
737 (buffer->user_data - alloc->buffer); in binder_alloc_get_page()
742 lru_page = &alloc->pages[index]; in binder_alloc_get_page()
744 return lru_page->page_ptr; in binder_alloc_get_page()
748 * binder_alloc_clear_buf() - zero out buffer
750 * @buffer: binder buffer to be cleared
752 * memset the given buffer to 0
755 struct binder_buffer *buffer) in binder_alloc_clear_buf() argument
757 size_t bytes = binder_alloc_buffer_size(alloc, buffer); in binder_alloc_clear_buf()
761 unsigned long size; in binder_alloc_clear_buf() local
765 page = binder_alloc_get_page(alloc, buffer, in binder_alloc_clear_buf()
767 size = min_t(size_t, bytes, PAGE_SIZE - pgoff); in binder_alloc_clear_buf()
768 memset_page(page, pgoff, 0, size); in binder_alloc_clear_buf()
769 bytes -= size; in binder_alloc_clear_buf()
770 buffer_offset += size; in binder_alloc_clear_buf()
775 * binder_alloc_free_buf() - free a binder buffer
777 * @buffer: kernel pointer to buffer
779 * Free the buffer allocated via binder_alloc_new_buf()
782 struct binder_buffer *buffer) in binder_alloc_free_buf() argument
788 * increase contention for the alloc->lock if clear_on_free in binder_alloc_free_buf()
792 if (buffer->clear_on_free) { in binder_alloc_free_buf()
793 binder_alloc_clear_buf(alloc, buffer); in binder_alloc_free_buf()
794 buffer->clear_on_free = false; in binder_alloc_free_buf()
796 spin_lock(&alloc->lock); in binder_alloc_free_buf()
797 binder_free_buf_locked(alloc, buffer); in binder_alloc_free_buf()
798 spin_unlock(&alloc->lock); in binder_alloc_free_buf()
802 * binder_alloc_mmap_handler() - map virtual address space for proc
811 * -EBUSY = address space already mapped
812 * -ENOMEM = failed to map memory to given address space
817 struct binder_buffer *buffer; in binder_alloc_mmap_handler() local
821 if (unlikely(vma->vm_mm != alloc->mm)) { in binder_alloc_mmap_handler()
822 ret = -EINVAL; in binder_alloc_mmap_handler()
823 failure_string = "invalid vma->vm_mm"; in binder_alloc_mmap_handler()
828 if (alloc->buffer_size) { in binder_alloc_mmap_handler()
829 ret = -EBUSY; in binder_alloc_mmap_handler()
833 alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start, in binder_alloc_mmap_handler()
837 alloc->buffer = vma->vm_start; in binder_alloc_mmap_handler()
839 alloc->pages = kvcalloc(alloc->buffer_size / PAGE_SIZE, in binder_alloc_mmap_handler()
840 sizeof(alloc->pages[0]), in binder_alloc_mmap_handler()
842 if (alloc->pages == NULL) { in binder_alloc_mmap_handler()
843 ret = -ENOMEM; in binder_alloc_mmap_handler()
848 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) { in binder_alloc_mmap_handler()
849 alloc->pages[i].alloc = alloc; in binder_alloc_mmap_handler()
850 INIT_LIST_HEAD(&alloc->pages[i].lru); in binder_alloc_mmap_handler()
853 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); in binder_alloc_mmap_handler()
854 if (!buffer) { in binder_alloc_mmap_handler()
855 ret = -ENOMEM; in binder_alloc_mmap_handler()
856 failure_string = "alloc buffer struct"; in binder_alloc_mmap_handler()
860 buffer->user_data = alloc->buffer; in binder_alloc_mmap_handler()
861 list_add(&buffer->entry, &alloc->buffers); in binder_alloc_mmap_handler()
862 buffer->free = 1; in binder_alloc_mmap_handler()
863 binder_insert_free_buffer(alloc, buffer); in binder_alloc_mmap_handler()
864 alloc->free_async_space = alloc->buffer_size / 2; in binder_alloc_mmap_handler()
872 kvfree(alloc->pages); in binder_alloc_mmap_handler()
873 alloc->pages = NULL; in binder_alloc_mmap_handler()
875 alloc->buffer = 0; in binder_alloc_mmap_handler()
877 alloc->buffer_size = 0; in binder_alloc_mmap_handler()
882 "%s: %d %lx-%lx %s failed %d\n", __func__, in binder_alloc_mmap_handler()
883 alloc->pid, vma->vm_start, vma->vm_end, in binder_alloc_mmap_handler()
893 struct binder_buffer *buffer; in binder_alloc_deferred_release() local
896 spin_lock(&alloc->lock); in binder_alloc_deferred_release()
897 BUG_ON(alloc->vma); in binder_alloc_deferred_release()
899 while ((n = rb_first(&alloc->allocated_buffers))) { in binder_alloc_deferred_release()
900 buffer = rb_entry(n, struct binder_buffer, rb_node); in binder_alloc_deferred_release()
903 BUG_ON(buffer->transaction); in binder_alloc_deferred_release()
905 if (buffer->clear_on_free) { in binder_alloc_deferred_release()
906 binder_alloc_clear_buf(alloc, buffer); in binder_alloc_deferred_release()
907 buffer->clear_on_free = false; in binder_alloc_deferred_release()
909 binder_free_buf_locked(alloc, buffer); in binder_alloc_deferred_release()
913 while (!list_empty(&alloc->buffers)) { in binder_alloc_deferred_release()
914 buffer = list_first_entry(&alloc->buffers, in binder_alloc_deferred_release()
916 WARN_ON(!buffer->free); in binder_alloc_deferred_release()
918 list_del(&buffer->entry); in binder_alloc_deferred_release()
919 WARN_ON_ONCE(!list_empty(&alloc->buffers)); in binder_alloc_deferred_release()
920 kfree(buffer); in binder_alloc_deferred_release()
924 if (alloc->pages) { in binder_alloc_deferred_release()
927 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) { in binder_alloc_deferred_release()
930 if (!alloc->pages[i].page_ptr) in binder_alloc_deferred_release()
934 &alloc->pages[i].lru); in binder_alloc_deferred_release()
937 __func__, alloc->pid, i, in binder_alloc_deferred_release()
939 __free_page(alloc->pages[i].page_ptr); in binder_alloc_deferred_release()
943 spin_unlock(&alloc->lock); in binder_alloc_deferred_release()
944 kvfree(alloc->pages); in binder_alloc_deferred_release()
945 if (alloc->mm) in binder_alloc_deferred_release()
946 mmdrop(alloc->mm); in binder_alloc_deferred_release()
950 __func__, alloc->pid, buffers, page_count); in binder_alloc_deferred_release()
954 * binder_alloc_print_allocated() - print buffer info
958 * Prints information about every buffer associated with
964 struct binder_buffer *buffer; in binder_alloc_print_allocated() local
967 spin_lock(&alloc->lock); in binder_alloc_print_allocated()
968 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) { in binder_alloc_print_allocated()
969 buffer = rb_entry(n, struct binder_buffer, rb_node); in binder_alloc_print_allocated()
970 seq_printf(m, " buffer %d: %lx size %zd:%zd:%zd %s\n", in binder_alloc_print_allocated()
971 buffer->debug_id, in binder_alloc_print_allocated()
972 buffer->user_data - alloc->buffer, in binder_alloc_print_allocated()
973 buffer->data_size, buffer->offsets_size, in binder_alloc_print_allocated()
974 buffer->extra_buffers_size, in binder_alloc_print_allocated()
975 buffer->transaction ? "active" : "delivered"); in binder_alloc_print_allocated()
977 spin_unlock(&alloc->lock); in binder_alloc_print_allocated()
981 * binder_alloc_print_pages() - print page usage
994 spin_lock(&alloc->lock); in binder_alloc_print_pages()
1000 for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) { in binder_alloc_print_pages()
1001 page = &alloc->pages[i]; in binder_alloc_print_pages()
1002 if (!page->page_ptr) in binder_alloc_print_pages()
1004 else if (list_empty(&page->lru)) in binder_alloc_print_pages()
1010 spin_unlock(&alloc->lock); in binder_alloc_print_pages()
1012 seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high); in binder_alloc_print_pages()
1016 * binder_alloc_get_allocated_count() - return count of buffers
1026 spin_lock(&alloc->lock); in binder_alloc_get_allocated_count()
1027 for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n)) in binder_alloc_get_allocated_count()
1029 spin_unlock(&alloc->lock); in binder_alloc_get_allocated_count()
1035 * binder_alloc_vma_close() - invalidate address space
1039 * Clears alloc->vma to prevent new incoming transactions from
1048 * binder_alloc_free_page() - shrinker callback to free pages
1063 struct binder_alloc *alloc = page->alloc; in binder_alloc_free_page()
1064 struct mm_struct *mm = alloc->mm; in binder_alloc_free_page()
1074 if (!spin_trylock(&alloc->lock)) in binder_alloc_free_page()
1076 if (!page->page_ptr) in binder_alloc_free_page()
1079 index = page - alloc->pages; in binder_alloc_free_page()
1080 page_addr = alloc->buffer + index * PAGE_SIZE; in binder_alloc_free_page()
1088 page_to_free = page->page_ptr; in binder_alloc_free_page()
1089 page->page_ptr = NULL; in binder_alloc_free_page()
1094 spin_unlock(&alloc->lock); in binder_alloc_free_page()
1114 spin_unlock(&alloc->lock); in binder_alloc_free_page()
1133 NULL, sc->nr_to_scan); in binder_shrink_scan()
1139 * binder_alloc_init() - called by binder_open() for per-proc initialization
1147 alloc->pid = current->group_leader->pid; in binder_alloc_init()
1148 alloc->mm = current->mm; in binder_alloc_init()
1149 mmgrab(alloc->mm); in binder_alloc_init()
1150 spin_lock_init(&alloc->lock); in binder_alloc_init()
1151 INIT_LIST_HEAD(&alloc->buffers); in binder_alloc_init()
1162 binder_shrinker = shrinker_alloc(0, "android-binder"); in binder_alloc_shrinker_init()
1165 return -ENOMEM; in binder_alloc_shrinker_init()
1168 binder_shrinker->count_objects = binder_shrink_count; in binder_alloc_shrinker_init()
1169 binder_shrinker->scan_objects = binder_shrink_scan; in binder_alloc_shrinker_init()
1183 * check_buffer() - verify that buffer/offset is safe to access
1185 * @buffer: binder buffer to be accessed
1186 * @offset: offset into @buffer data
1189 * Check that the @offset/@bytes are within the size of the given
1190 * @buffer and that the buffer is currently active and not freeable.
1192 * allowed to touch the buffer in two cases:
1194 * 1) when the buffer is being created:
1195 * (buffer->free == 0 && buffer->allow_user_free == 0)
1196 * 2) when the buffer is being torn down:
1197 * (buffer->free == 0 && buffer->transaction == NULL).
1199 * Return: true if the buffer is safe to access
1202 struct binder_buffer *buffer, in check_buffer() argument
1205 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer); in check_buffer()
1208 offset <= buffer_size - bytes && in check_buffer()
1210 !buffer->free && in check_buffer()
1211 (!buffer->allow_user_free || !buffer->transaction); in check_buffer()
1215 * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
1217 * @buffer: binder buffer to be accessed
1218 * @buffer_offset: offset into @buffer data
1219 * @from: userspace pointer to source buffer
1222 * Copy bytes from source userspace to target buffer.
1228 struct binder_buffer *buffer, in binder_alloc_copy_user_to_buffer() argument
1233 if (!check_buffer(alloc, buffer, buffer_offset, bytes)) in binder_alloc_copy_user_to_buffer()
1237 unsigned long size; in binder_alloc_copy_user_to_buffer() local
1243 page = binder_alloc_get_page(alloc, buffer, in binder_alloc_copy_user_to_buffer()
1245 size = min_t(size_t, bytes, PAGE_SIZE - pgoff); in binder_alloc_copy_user_to_buffer()
1247 ret = copy_from_user(kptr, from, size); in binder_alloc_copy_user_to_buffer()
1250 return bytes - size + ret; in binder_alloc_copy_user_to_buffer()
1251 bytes -= size; in binder_alloc_copy_user_to_buffer()
1252 from += size; in binder_alloc_copy_user_to_buffer()
1253 buffer_offset += size; in binder_alloc_copy_user_to_buffer()
1260 struct binder_buffer *buffer, in binder_alloc_do_buffer_copy() argument
1265 /* All copies must be 32-bit aligned and 32-bit size */ in binder_alloc_do_buffer_copy()
1266 if (!check_buffer(alloc, buffer, buffer_offset, bytes)) in binder_alloc_do_buffer_copy()
1267 return -EINVAL; in binder_alloc_do_buffer_copy()
1270 unsigned long size; in binder_alloc_do_buffer_copy() local
1274 page = binder_alloc_get_page(alloc, buffer, in binder_alloc_do_buffer_copy()
1276 size = min_t(size_t, bytes, PAGE_SIZE - pgoff); in binder_alloc_do_buffer_copy()
1278 memcpy_to_page(page, pgoff, ptr, size); in binder_alloc_do_buffer_copy()
1280 memcpy_from_page(ptr, page, pgoff, size); in binder_alloc_do_buffer_copy()
1281 bytes -= size; in binder_alloc_do_buffer_copy()
1283 ptr = ptr + size; in binder_alloc_do_buffer_copy()
1284 buffer_offset += size; in binder_alloc_do_buffer_copy()
1290 struct binder_buffer *buffer, in binder_alloc_copy_to_buffer() argument
1295 return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset, in binder_alloc_copy_to_buffer()
1301 struct binder_buffer *buffer, in binder_alloc_copy_from_buffer() argument
1305 return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset, in binder_alloc_copy_from_buffer()