Lines Matching refs:alloc
60 static size_t binder_alloc_buffer_size(struct binder_alloc *alloc, 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()
68 static void binder_insert_free_buffer(struct binder_alloc *alloc, in binder_insert_free_buffer() argument
71 struct rb_node **p = &alloc->free_buffers.rb_node; in binder_insert_free_buffer()
79 new_buffer_size = binder_alloc_buffer_size(alloc, new_buffer); in binder_insert_free_buffer()
83 alloc->pid, new_buffer_size, new_buffer); in binder_insert_free_buffer()
90 buffer_size = binder_alloc_buffer_size(alloc, buffer); in binder_insert_free_buffer()
98 rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers); in binder_insert_free_buffer()
102 struct binder_alloc *alloc, struct binder_buffer *new_buffer) in binder_insert_allocated_buffer_locked() argument
104 struct rb_node **p = &alloc->allocated_buffers.rb_node; in binder_insert_allocated_buffer_locked()
123 rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers); in binder_insert_allocated_buffer_locked()
127 struct binder_alloc *alloc, in binder_alloc_prepare_to_free_locked() argument
130 struct rb_node *n = alloc->allocated_buffers.rb_node; in binder_alloc_prepare_to_free_locked()
167 struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc, in binder_alloc_prepare_to_free() argument
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()
193 static void binder_lru_freelist_add(struct binder_alloc *alloc, in binder_lru_freelist_add() argument
199 trace_binder_update_page_range(alloc, false, start, end); in binder_lru_freelist_add()
205 index = (page_addr - alloc->buffer) / PAGE_SIZE; in binder_lru_freelist_add()
206 page = &alloc->pages[index]; in binder_lru_freelist_add()
211 trace_binder_free_lru_start(alloc, index); in binder_lru_freelist_add()
216 trace_binder_free_lru_end(alloc, index); in binder_lru_freelist_add()
220 static int binder_install_single_page(struct binder_alloc *alloc, in binder_install_single_page() argument
227 if (!mmget_not_zero(alloc->mm)) 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()
246 pr_err("%d: failed to allocate page\n", alloc->pid); 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()
263 mmap_write_unlock(alloc->mm); in binder_install_single_page()
264 mmput_async(alloc->mm); in binder_install_single_page()
268 static int binder_install_buffer_pages(struct binder_alloc *alloc, in binder_install_buffer_pages() argument
283 index = (page_addr - alloc->buffer) / PAGE_SIZE; in binder_install_buffer_pages()
284 page = &alloc->pages[index]; in binder_install_buffer_pages()
289 trace_binder_alloc_page_start(alloc, index); in binder_install_buffer_pages()
291 ret = binder_install_single_page(alloc, page, page_addr); in binder_install_buffer_pages()
295 trace_binder_alloc_page_end(alloc, index); in binder_install_buffer_pages()
302 static void binder_lru_freelist_del(struct binder_alloc *alloc, in binder_lru_freelist_del() argument
308 trace_binder_update_page_range(alloc, true, start, end); in binder_lru_freelist_del()
314 index = (page_addr - alloc->buffer) / PAGE_SIZE; in binder_lru_freelist_del()
315 page = &alloc->pages[index]; in binder_lru_freelist_del()
318 trace_binder_alloc_lru_start(alloc, index); in binder_lru_freelist_del()
323 trace_binder_alloc_lru_end(alloc, index); 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()
332 static inline void binder_alloc_set_vma(struct binder_alloc *alloc, in binder_alloc_set_vma() argument
336 smp_store_release(&alloc->vma, vma); in binder_alloc_set_vma()
340 struct binder_alloc *alloc) in binder_alloc_get_vma() argument
343 return smp_load_acquire(&alloc->vma); in binder_alloc_get_vma()
346 static void debug_no_space_locked(struct binder_alloc *alloc) in debug_no_space_locked() argument
358 for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) { 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()
369 buffer_size = binder_alloc_buffer_size(alloc, buffer); in debug_no_space_locked()
383 static bool debug_low_async_space_locked(struct binder_alloc *alloc) in debug_low_async_space_locked() argument
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()
414 total_alloc_size += binder_alloc_buffer_size(alloc, buffer); in debug_low_async_space_locked()
423 if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) { 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()
437 struct binder_alloc *alloc, in binder_alloc_new_buf_locked() argument
442 struct rb_node *n = alloc->free_buffers.rb_node; in binder_alloc_new_buf_locked()
449 if (is_async && alloc->free_async_space < size) { in binder_alloc_new_buf_locked()
452 alloc->pid, size); in binder_alloc_new_buf_locked()
460 buffer_size = binder_alloc_buffer_size(alloc, buffer); in binder_alloc_new_buf_locked()
476 alloc->pid, size); in binder_alloc_new_buf_locked()
477 debug_no_space_locked(alloc); in binder_alloc_new_buf_locked()
485 buffer_size = binder_alloc_buffer_size(alloc, buffer); in binder_alloc_new_buf_locked()
491 binder_insert_free_buffer(alloc, new_buffer); in binder_alloc_new_buf_locked()
497 alloc->pid, size, buffer, buffer_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()
513 binder_insert_allocated_buffer_locked(alloc, buffer); in binder_alloc_new_buf_locked()
517 alloc->free_async_space -= size; in binder_alloc_new_buf_locked()
520 alloc->pid, size, alloc->free_async_space); in binder_alloc_new_buf_locked()
521 if (debug_low_async_space_locked(alloc)) in binder_alloc_new_buf_locked()
568 struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc, in binder_alloc_new_buf() argument
579 if (!binder_alloc_get_vma(alloc)) { in binder_alloc_new_buf()
582 alloc->pid); in binder_alloc_new_buf()
590 alloc->pid, data_size, offsets_size, 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()
603 spin_unlock(&alloc->lock); 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()
632 static void binder_delete_free_buffer(struct binder_alloc *alloc, in binder_delete_free_buffer() argument
640 BUG_ON(alloc->buffers.next == &buffer->entry); in binder_delete_free_buffer()
646 if (!list_is_last(&buffer->entry, &alloc->buffers)) { in binder_delete_free_buffer()
652 binder_lru_freelist_add(alloc, buffer_start_page(buffer), in binder_delete_free_buffer()
659 static void binder_free_buf_locked(struct binder_alloc *alloc, in binder_free_buf_locked() argument
664 buffer_size = binder_alloc_buffer_size(alloc, buffer); in binder_free_buf_locked()
672 alloc->pid, buffer, size, buffer_size); 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()
681 alloc->free_async_space += buffer_size; 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()
690 rb_erase(&buffer->rb_node, &alloc->allocated_buffers); in binder_free_buf_locked()
692 if (!list_is_last(&buffer->entry, &alloc->buffers)) { in binder_free_buf_locked()
696 rb_erase(&next->rb_node, &alloc->free_buffers); in binder_free_buf_locked()
697 binder_delete_free_buffer(alloc, next); in binder_free_buf_locked()
700 if (alloc->buffers.next != &buffer->entry) { 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()
709 binder_insert_free_buffer(alloc, buffer); in binder_free_buf_locked()
731 static struct page *binder_alloc_get_page(struct binder_alloc *alloc, 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()
754 static void binder_alloc_clear_buf(struct binder_alloc *alloc, in binder_alloc_clear_buf() argument
757 size_t bytes = binder_alloc_buffer_size(alloc, buffer); in binder_alloc_clear_buf()
765 page = binder_alloc_get_page(alloc, buffer, in binder_alloc_clear_buf()
781 void binder_alloc_free_buf(struct binder_alloc *alloc, in binder_alloc_free_buf() argument
793 binder_alloc_clear_buf(alloc, buffer); 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()
814 int binder_alloc_mmap_handler(struct binder_alloc *alloc, in binder_alloc_mmap_handler() argument
821 if (unlikely(vma->vm_mm != alloc->mm)) { in binder_alloc_mmap_handler()
828 if (alloc->buffer_size) { 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()
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()
860 buffer->user_data = alloc->buffer; in binder_alloc_mmap_handler()
861 list_add(&buffer->entry, &alloc->buffers); 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()
867 binder_alloc_set_vma(alloc, vma); 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()
883 alloc->pid, vma->vm_start, vma->vm_end, in binder_alloc_mmap_handler()
889 void binder_alloc_deferred_release(struct binder_alloc *alloc) in binder_alloc_deferred_release() argument
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()
906 binder_alloc_clear_buf(alloc, buffer); 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()
919 WARN_ON_ONCE(!list_empty(&alloc->buffers)); 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()
962 struct binder_alloc *alloc) in binder_alloc_print_allocated() argument
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()
972 buffer->user_data - alloc->buffer, in binder_alloc_print_allocated()
977 spin_unlock(&alloc->lock); in binder_alloc_print_allocated()
986 struct binder_alloc *alloc) in binder_alloc_print_pages() argument
994 spin_lock(&alloc->lock); in binder_alloc_print_pages()
999 if (binder_alloc_get_vma(alloc) != NULL) { 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()
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()
1021 int binder_alloc_get_allocated_count(struct binder_alloc *alloc) in binder_alloc_get_allocated_count() argument
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()
1042 void binder_alloc_vma_close(struct binder_alloc *alloc) in binder_alloc_vma_close() argument
1044 binder_alloc_set_vma(alloc, NULL); in binder_alloc_vma_close()
1063 struct binder_alloc *alloc = page->alloc; in binder_alloc_free_page() local
1064 struct mm_struct *mm = alloc->mm; in binder_alloc_free_page()
1074 if (!spin_trylock(&alloc->lock)) 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()
1083 if (vma && vma != binder_alloc_get_vma(alloc)) in binder_alloc_free_page()
1086 trace_binder_unmap_kernel_start(alloc, index); in binder_alloc_free_page()
1091 trace_binder_unmap_kernel_end(alloc, index); in binder_alloc_free_page()
1094 spin_unlock(&alloc->lock); in binder_alloc_free_page()
1098 trace_binder_unmap_user_start(alloc, index); in binder_alloc_free_page()
1102 trace_binder_unmap_user_end(alloc, index); in binder_alloc_free_page()
1114 spin_unlock(&alloc->lock); in binder_alloc_free_page()
1145 void binder_alloc_init(struct binder_alloc *alloc) in binder_alloc_init() argument
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()
1201 static inline bool check_buffer(struct binder_alloc *alloc, in check_buffer() argument
1205 size_t buffer_size = binder_alloc_buffer_size(alloc, buffer); in check_buffer()
1227 binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc, in binder_alloc_copy_user_to_buffer() argument
1233 if (!check_buffer(alloc, buffer, buffer_offset, bytes)) in binder_alloc_copy_user_to_buffer()
1243 page = binder_alloc_get_page(alloc, buffer, in binder_alloc_copy_user_to_buffer()
1258 static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc, in binder_alloc_do_buffer_copy() argument
1266 if (!check_buffer(alloc, buffer, buffer_offset, bytes)) in binder_alloc_do_buffer_copy()
1274 page = binder_alloc_get_page(alloc, buffer, in binder_alloc_do_buffer_copy()
1289 int binder_alloc_copy_to_buffer(struct binder_alloc *alloc, 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()
1299 int binder_alloc_copy_from_buffer(struct binder_alloc *alloc, 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()