android: binder: move allocator metadata and add shrinker
v2 cover android: binder: move allocator metadata and add shrinker
This patch set moves internal kernel data in the binder driver out of mmap regions that is readable by user space. A shrinker is added to the driver to dynamically manage the memory used by binder transactions and only free pages when the system is under memory pressure. This patch set also adds tests and refactoring in binder allocator.
android: binder: Refactor prev and next buffer into a helper function
android: binder: Add allocator selftest
android: binder: Move buffer out of area shared with user space
android: binder: Add global lru shrinker to binder
android: binder: Add shrinker tracepoints
drivers/android/Kconfig | 10 + drivers/android/Makefile | 1 + drivers/android/binder.c | 4 + drivers/android/binder_alloc.c | 365 ++++++++++++++++------ drivers/android/binder_alloc.h | 30 +- drivers/android/binder_alloc_selftest.c | 310 ++++++++++++++++++ drivers/android/binder_trace.h | 55 ++++ 7 files changed, 678 insertions(+), 97 deletions(-)
PATCH v2 1/5 android: binder: Refactor prev and next buffer into a helper function
Use helper functions buffer_next and buffer_prev instead of list_entry to get the next and previous buffers.
--- drivers/android/binder_alloc.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
drivers/android/binder_alloc.c +15 -9
@@ -48,14 +48,23 @@ module_param_named(debug_mask, binder_alloc_debug_mask,pr_info (x); \ }while (0 ) +static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer) +{ + return list_entry(buffer->entry.next, struct binder_buffer, entry); +} + +static struct binder_buffer *binder_buffer_prev (struct binder_buffer *buffer) +{ + return list_entry(buffer->entry.prev, struct binder_buffer, entry); +} +static size_t binder_alloc_buffer_size (struct binder_alloc *alloc,struct binder_buffer *buffer) {if (list_is_last (&buffer->entry, &alloc->buffers))return alloc->buffer + alloc->buffer_size - (void *)buffer->data; -return (size_t )list_entry (buffer->entry.next, - struct binder_buffer, entry) - (size_t)buffer->data; +return (size_t )binder_buffer_next (buffer) - (size_t )buffer->data; } static void binder_insert_free_buffer(struct binder_alloc *alloc,
@@ -470,7 +479,7 @@ static void binder_delete_free_buffer(struct binder_alloc *alloc,int free_page_start =1 ;BUG_ON (alloc->buffers.next == &buffer->entry); - prev = list_entry(buffer->entry.prev, struct binder_buffer, entry); + prev =binder_buffer_prev (buffer);BUG_ON (!prev->free);if (buffer_end_page (prev) ==buffer_start_page (buffer)) { free_page_start =0 ;
@@ -482,8 +491,7 @@ static void binder_delete_free_buffer(struct binder_alloc *alloc, }if (!list_is_last (&buffer->entry, &alloc->buffers)) { - next =list_entry (buffer->entry.next, - struct binder_buffer, entry); + next =binder_buffer_next (buffer);if (buffer_start_page (next) ==buffer_end_page (buffer)) { free_page_end =0 ; if (buffer_start_page(next) ==
@@ -544,8 +552,7 @@ static void binder_free_buf_locked(struct binder_alloc *alloc,rb_erase (&buffer->rb_node, &alloc->allocated_buffers); buffer->free =1 ;if (!list_is_last (&buffer->entry, &alloc->buffers)) { -struct binder_buffer *next =list_entry (buffer->entry.next, - struct binder_buffer, entry); +struct binder_buffer *next =binder_buffer_next (buffer);if (next->free) {rb_erase (&next->rb_node, &alloc->free_buffers);
@@ -553,8 +560,7 @@ static void binder_free_buf_locked(struct binder_alloc *alloc, } }if (alloc->buffers.next != &buffer->entry) { -struct binder_buffer *prev =list_entry (buffer->entry.prev, - struct binder_buffer, entry); +struct binder_buffer *prev =binder_buffer_prev (buffer);if (prev->free) {binder_delete_free_buffer (alloc, buffer);
Signature · 1 line
2.14.1.342.g6490525c54-goog
Greg Kroah-Hartman
maintainerRe: [PATCH v2 1/5] android: binder: Refactor prev and next buffer into a helper function
Quoted 7 lines · Sherry Yang
Use helper functions
buffer_nextandbuffer_previnstead oflist_entryto get the next and previous buffers.Signed-off-by Sherry Yang <[email protected]>--- drivers/android/binder_alloc.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
These all have been acked by the other google/android developers, right?
thanks,
greg k-h
PATCH v2 2/5 android: binder: Add allocator selftest
In reply to Sherry Yangbinder_alloc_selftest tests that alloc_new_buf handles page allocation and deallocation properly when allocate and free buffers. The test allocates 5 buffers of various sizes to cover all possible page alignment cases, and frees the buffers using a list of exhaustive freeing order.
Test: boot the device with ANDROID_BINDER_IPC_SELFTEST config option enabled. Allocator selftest passes.
--- drivers/android/Kconfig | 10 ++ drivers/android/Makefile | 1 + drivers/android/binder.c | 2 + drivers/android/binder_alloc.h | 5 + drivers/android/binder_alloc_selftest.c | 271 ++++++++++++++++++++++++++++++++ 5 files changed, 289 insertions(+) create mode 100644 drivers/android/binder_alloc_selftest.c
drivers/android/Kconfig +10 -0
@@ -44,6 +44,16 @@ config ANDROID_BINDER_IPC_32BIT Note that enabling this will break newer Android user-space. +config ANDROID_BINDER_IPC_SELFTEST + bool "Android Binder IPC Driver Selftest" + depends on ANDROID_BINDER_IPC + ---help--- + This feature allows binder selftest to run. + + Binder selftest checks the allocation and free of binder buffers + exhaustively with combinations of various buffer sizes and + alignments. + endif # if ANDROID endmenu
drivers/android/Makefile +1 -0
@@ -1,3 +1,4 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o binder_alloc.o +obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) += binder_alloc_selftest.o
drivers/android/binder.c +2 -0
@@ -4225,6 +4225,8 @@ static long binder_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)/*pr_info("binder_ioctl: %d:%d %x %lx\n", proc->pid, current->pid, cmd, arg);*/ +binder_selftest_alloc (&proc->alloc); +trace_binder_ioctl (cmd, arg); ret =wait_event_interruptible (binder_user_error_wait, binder_stop_on_user_error <2 );
drivers/android/binder_alloc.h +5 -0
@@ -102,6 +102,11 @@ struct binder_alloc {int pid; }; +#ifdef CONFIG_ANDROID_BINDER_IPC_SELFTEST +void binder_selftest_alloc (struct binder_alloc *alloc); +#else +static inline void binder_selftest_alloc (struct binder_alloc *alloc) {} +#endif extern struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc, size_t data_size, size_t offsets_size,
drivers/android/binder_alloc_selftest.c new file +271 -0
271 lines not on this page · raw message
Signature · 1 line
2.14.1.342.g6490525c54-goog
PATCH v2 3/5 android: binder: Move buffer out of area shared with user space
In reply to Sherry YangBinder driver allocates buffer meta data in a region that is mapped in user space. These meta data contain pointers in the kernel.
This patch allocates buffer meta data on the kernel heap that is not mapped in user space, and uses a pointer to refer to the data mapped.
--- drivers/android/binder_alloc.c | 144 +++++++++++++++++++------------- drivers/android/binder_alloc.h | 2 +- drivers/android/binder_alloc_selftest.c | 11 ++- 3 files changed, 90 insertions(+), 67 deletions(-)
drivers/android/binder_alloc.c +84 -60
@@ -62,9 +62,9 @@ static size_t binder_alloc_buffer_size(struct binder_alloc *alloc, struct binder_buffer *buffer) {if (list_is_last (&buffer->entry, &alloc->buffers)) -return alloc->buffer + - alloc->buffer_size - (void *)buffer->data; -return (size_t )binder_buffer_next (buffer) - (size_t )buffer->data; +return (u8 *)alloc->buffer + + alloc->buffer_size - (u8 *)buffer->data; +return (u8 *)binder_buffer_next (buffer)->data - (u8 *)buffer->data; } static void binder_insert_free_buffer(struct binder_alloc *alloc,
@@ -114,9 +114,9 @@ static void binder_insert_allocated_buffer_locked( buffer = rb_entry(parent, struct binder_buffer, rb_node);BUG_ON (buffer->free); -if (new_buffer < buffer) +if (new_buffer->data < buffer->data) p = &parent->rb_left; -else if (new_buffer > buffer) +else if (new_buffer->data > buffer->data) p = &parent->rb_right;else BUG ();
@@ -131,18 +131,17 @@ static struct binder_buffer *binder_alloc_prepare_to_free_locked( {struct rb_node *n = alloc->allocated_buffers.rb_node;struct binder_buffer *buffer; -struct binder_buffer *kern_ptr; +void *kern_ptr; - kern_ptr = (struct binder_buffer *)(user_ptr - alloc->user_buffer_offset - - offsetof(struct binder_buffer , data)); + kern_ptr = (void *)(user_ptr - alloc->user_buffer_offset);while (n) { buffer = rb_entry(n, struct binder_buffer, rb_node);BUG_ON (buffer->free); -if (kern_ptr < buffer) +if (kern_ptr < buffer->data) n = n->rb_left; -else if (kern_ptr > buffer) +else if (kern_ptr > buffer->data) n = n->rb_right;else {/*
@@ -330,6 +329,9 @@ struct binder_buffer *binder_alloc_new_buf_locked(struct binder_alloc *alloc,return ERR_PTR (-ENOSPC ); } +/* Pad 0-size buffers so they get assigned unique addresses */ + size =max (size,sizeof (void *)); +while (n) { buffer = rb_entry(n, struct binder_buffer, rb_node);BUG_ON (!buffer->free);
@@ -389,14 +391,9 @@ struct binder_buffer *binder_alloc_new_buf_locked(struct binder_alloc *alloc, has_page_addr = (void *)(((uintptr_t )buffer->data + buffer_size) &PAGE_MASK ); -if (n ==NULL ) { -if (size +sizeof (struct binder_buffer ) +4 >= buffer_size) - buffer_size = size;/* no room for other buffers */ -else - buffer_size = size +sizeof (struct binder_buffer ); - } +WARN_ON (n && buffer_size != size); end_page_addr = - (void *)PAGE_ALIGN ((uintptr_t )buffer->data + buffer_size); + (void *)PAGE_ALIGN ((uintptr_t )buffer->data + size);if (end_page_addr > has_page_addr) end_page_addr = has_page_addr; ret = binder_update_page_range(alloc, 1,
@@ -404,17 +401,25 @@ struct binder_buffer *binder_alloc_new_buf_locked(struct binder_alloc *alloc,if (ret)return ERR_PTR (ret); -rb_erase (best_fit, &alloc->free_buffers); - buffer->free =0 ; - buffer->free_in_progress =0 ; -binder_insert_allocated_buffer_locked (alloc, buffer);if (buffer_size != size) { -struct binder_buffer *new_buffer = (void *)buffer->data + size; +struct binder_buffer *new_buffer; + new_buffer =kzalloc (sizeof (*buffer),GFP_KERNEL ); +if (!new_buffer) { +pr_err ("%s: %d failed to alloc new buffer struct\n" , + __func__, alloc->pid); + goto err_alloc_buf_struct_failed; + } + new_buffer->data = (u8 *)buffer->data + size;list_add (&new_buffer->entry, &buffer->entry); new_buffer->free =1 ;binder_insert_free_buffer (alloc, new_buffer); } + +rb_erase (best_fit, &alloc->free_buffers); + buffer->free =0 ; + buffer->free_in_progress =0 ; +binder_insert_allocated_buffer_locked (alloc, buffer);binder_alloc_debug (BINDER_DEBUG_BUFFER_ALLOC ,"%d: binder_alloc_buf size %zd got %pK\n" , alloc->pid, size, buffer);
@@ -429,6 +434,12 @@ struct binder_buffer *binder_alloc_new_buf_locked(struct binder_alloc *alloc, alloc->pid, size, alloc->free_async_space); }return buffer; + +err_alloc_buf_struct_failed: +binder_update_page_range (alloc,0 , + (void *)PAGE_ALIGN ((uintptr_t )buffer->data), + end_page_addr,NULL ); +return ERR_PTR (-ENOMEM ); }/**
@@ -463,56 +474,59 @@ struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,static void *buffer_start_page (struct binder_buffer *buffer) { -return (void *)((uintptr_t )buffer &PAGE_MASK ); +return (void *)((uintptr_t )buffer->data &PAGE_MASK ); } -static void *buffer_end_page (struct binder_buffer *buffer) +static void *prev_buffer_end_page (struct binder_buffer *buffer) { -return (void *)(((uintptr_t )(buffer +1 ) -1 ) &PAGE_MASK ); +return (void *)(((uintptr_t )(buffer->data) -1 ) &PAGE_MASK ); }static void binder_delete_free_buffer (struct binder_alloc *alloc,struct binder_buffer *buffer) {struct binder_buffer *prev, *next =NULL ; -int free_page_end =1 ; -int free_page_start =1 ; - +bool to_free = true;BUG_ON (alloc->buffers.next == &buffer->entry); prev =binder_buffer_prev (buffer);BUG_ON (!prev->free); -if (buffer_end_page (prev) ==buffer_start_page (buffer)) { - free_page_start =0 ; -if (buffer_end_page (prev) ==buffer_end_page (buffer)) - free_page_end =0 ; +if (prev_buffer_end_page (prev) ==buffer_start_page (buffer)) { + to_free = false;binder_alloc_debug (BINDER_DEBUG_BUFFER_ALLOC , -"%d: merge free, buffer %pK share page with %pK\n" , - alloc->pid, buffer, prev); +"%d: merge free, buffer %pK share page with %pK\n" , + alloc->pid, buffer->data, prev->data); }if (!list_is_last (&buffer->entry, &alloc->buffers)) { next =binder_buffer_next (buffer); -if (buffer_start_page (next) ==buffer_end_page (buffer)) { - free_page_end =0 ; -if (buffer_start_page (next) == -buffer_start_page (buffer)) - free_page_start =0 ; +if (buffer_start_page (next) ==buffer_start_page (buffer)) { + to_free = false;binder_alloc_debug (BINDER_DEBUG_BUFFER_ALLOC , -"%d: merge free, buffer %pK share page with %pK\n" , - alloc->pid, buffer, prev); +"%d: merge free, buffer %pK share page with %pK\n" , + alloc->pid, + buffer->data, + next->data); } } -list_del (&buffer->entry); -if (free_page_start || free_page_end) { + +if (PAGE_ALIGNED (buffer->data)) { +binder_alloc_debug (BINDER_DEBUG_BUFFER_ALLOC , +"%d: merge free, buffer start %pK is page aligned\n" , + alloc->pid, buffer->data); + to_free = false; + } + +if (to_free) {binder_alloc_debug (BINDER_DEBUG_BUFFER_ALLOC , -"%d: merge free, buffer %pK do not share page%s%s with %pK or %pK\n" , - alloc->pid, buffer, free_page_start ?"" :" end" , - free_page_end ?"" :" start" , prev, next); -binder_update_page_range (alloc,0 , free_page_start ? -buffer_start_page (buffer) :buffer_end_page (buffer), - (free_page_end ?buffer_end_page (buffer) : -buffer_start_page (buffer)) +PAGE_SIZE ,NULL ); +"%d: merge free, buffer %pK do not share page with %pK or %pK\n" , + alloc->pid, buffer->data, + prev->data, next->data); +binder_update_page_range (alloc,0 ,buffer_start_page (buffer), +buffer_start_page (buffer) +PAGE_SIZE , +NULL ); } +list_del (&buffer->entry); +kfree (buffer); } static void binder_free_buf_locked(struct binder_alloc *alloc,
@@ -533,8 +547,8 @@ static void binder_free_buf_locked(struct binder_alloc *alloc,BUG_ON (buffer->free);BUG_ON (size > buffer_size);BUG_ON (buffer->transaction !=NULL ); -BUG_ON ((void *)buffer < alloc->buffer); -BUG_ON ((void *)buffer > alloc->buffer + alloc->buffer_size); +BUG_ON (buffer->data < alloc->buffer); +BUG_ON (buffer->data > alloc->buffer + alloc->buffer_size);if (buffer->async_transaction) { alloc->free_async_space += size +sizeof (struct binder_buffer );
@@ -646,13 +660,14 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc, } alloc->buffer_size = vma->vm_end - vma->vm_start; -if (binder_update_page_range (alloc,1 , alloc->buffer, - alloc->buffer +PAGE_SIZE , vma)) { + buffer =kzalloc (sizeof (*buffer),GFP_KERNEL ); +if (!buffer) { ret = -ENOMEM ; - failure_string ="alloc small buf" ; - goto err_alloc_small_buf_failed; + failure_string ="alloc buffer struct" ; + goto err_alloc_buf_struct_failed; } - buffer = alloc->buffer; + + buffer->data = alloc->buffer;INIT_LIST_HEAD (&alloc->buffers);list_add (&buffer->entry, &alloc->buffers); buffer->free =1 ;
@@ -664,7 +679,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,return 0 ; -err_alloc_small_buf_failed: +err_alloc_buf_struct_failed:kfree (alloc->pages); alloc->pages =NULL ; err_alloc_pages_failed:
@@ -684,14 +699,13 @@ void binder_alloc_deferred_release(struct binder_alloc *alloc) {struct rb_node *n;int buffers, page_count; +struct binder_buffer *buffer;BUG_ON (alloc->vma); buffers =0 ;mutex_lock (&alloc->mutex);while ((n =rb_first (&alloc->allocated_buffers))) { -struct binder_buffer *buffer; - buffer = rb_entry(n, struct binder_buffer, rb_node);/* Transaction should already have been freed */
@@ -701,6 +715,16 @@ void binder_alloc_deferred_release(struct binder_alloc *alloc) buffers++; } +while (!list_empty (&alloc->buffers)) { + buffer =list_first_entry (&alloc->buffers, + struct binder_buffer, entry); +WARN_ON (!buffer->free); + +list_del (&buffer->entry); +WARN_ON_ONCE (!list_empty (&alloc->buffers)); +kfree (buffer); + } + page_count =0 ;if (alloc->pages) {int i;
drivers/android/binder_alloc.h +1 -1
@@ -57,7 +57,7 @@ struct binder_buffer {size_t data_size;size_t offsets_size;size_t extra_buffers_size; -uint8_t data[0 ]; +void *data; };/**
drivers/android/binder_alloc_selftest.c +5 -6
@@ -105,8 +105,9 @@ static bool check_buffer_pages_allocated(struct binder_alloc *alloc,void *page_addr, *end;int page_index; - end = (void *)PAGE_ALIGN ((uintptr_t )buffer + size); -for (page_addr = buffer; page_addr < end; page_addr +=PAGE_SIZE ) { + end = (void *)PAGE_ALIGN ((uintptr_t )buffer->data + size); + page_addr = buffer->data; +for (; page_addr < end; page_addr +=PAGE_SIZE ) { page_index = (page_addr - alloc->buffer) /PAGE_SIZE ;if (!alloc->pages[page_index]) { pr_err("incorrect alloc state at page index %d\n",
@@ -209,8 +210,7 @@ static void binder_selftest_alloc_size(struct binder_alloc *alloc,* Only BUFFER_NUM - 1 buffer sizes are adjustable since * we need one giant buffer before getting to the last page. */ - back_sizes[0 ] += alloc->buffer_size - end_offset[BUFFER_NUM -1 ] - -sizeof (struct binder_buffer ) *BUFFER_NUM ; + back_sizes[0 ] += alloc->buffer_size - end_offset[BUFFER_NUM -1 ];binder_selftest_free_seq (alloc, front_sizes, seq,0 );binder_selftest_free_seq (alloc, back_sizes, seq,0 ); }
@@ -228,8 +228,7 @@ static void binder_selftest_alloc_offset(struct binder_alloc *alloc, prev = index ==0 ?0 : end_offset[index -1 ]; end = prev; -BUILD_BUG_ON ((BUFFER_MIN_SIZE +sizeof (struct binder_buffer )) - *BUFFER_NUM >=PAGE_SIZE ); +BUILD_BUG_ON (BUFFER_MIN_SIZE *BUFFER_NUM >=PAGE_SIZE );for (align =SAME_PAGE_UNALIGNED ; align <LOOP_END ; align++) {if (align %2 )
Signature · 1 line
2.14.1.342.g6490525c54-goog
Re: [PATCH v2 3/5] android: binder: Move buffer out of area shared with user space
Quoted 6 lines · Sherry Yang
Binder driver allocates buffer meta data in a region that is mapped in user space. These meta data contain pointers in the kernel.
This patch allocates buffer meta data on the kernel heap that is not mapped in user space, and uses a pointer to refer to the data mapped.
This feels like it has a security impact, right? The original code is an info leak?
drivers/android/binder_alloc.c int binder_alloc_mmap_handler(struct binder_alloc *alloc, · line 685
Quoted 9 lines
drivers/android/binder_alloc.c +1
-1@@ -664,7 +679,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,return 0 ; -err_alloc_small_buf_failed: +err_alloc_buf_struct_failed:kfree (alloc->pages); alloc->pages =NULL ; err_alloc_pages_failed:
Not really really related to your patch, I was just looking at the error handling here. It looks like this with your patch applied.
682 err_alloc_buf_struct_failed: 683 kfree(alloc->pages); 684 alloc->pages = NULL; 685 err_alloc_pages_failed: 686 mutex_lock(&binder_alloc_mmap_lock); 687 vfree(alloc->buffer);
The vfree() here is supposed to release the resources from get_vm_area(). Why do people not use free_vm_area() instead? It feels like we're freeing "area->addr" but leaking "area" itself but perhaps I have misunderstood something.
688 alloc->buffer = NULL;
689 err_get_vm_area_failed:
690 err_already_mapped:
691 mutex_unlock(&binder_alloc_mmap_lock);
692 pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
693 alloc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
694 return ret;regards,
dan carpenter
PATCH v2 4/5 android: binder: Add global lru shrinker to binder
In reply to Sherry YangHold on to the pages allocated and mapped for transaction buffers until the system is under memory pressure. When that happens, use linux shrinker to free pages. Without using shrinker, patch "android: binder: Move buffer out of area shared with user space" will cause a significant slow down for small transactions that fit into the first page because free list buffer header used to be inlined with buffer data.
In addition to prevent the performance regression for small transactions, this patch improves the performance for transactions that take up more than one page.
Modify alloc selftest to work with the shrinker change.
Test: Run memory intensive applications (Chrome and Camera) to trigger shrinker callbacks. Binder frees memory as expected.
Test: Run binderThroughputTest with high memory pressure option enabled.
--- drivers/android/binder.c | 2 + drivers/android/binder_alloc.c | 172 +++++++++++++++++++++++++++----- drivers/android/binder_alloc.h | 23 ++++- drivers/android/binder_alloc_selftest.c | 68 ++++++++++--- 4 files changed, 225 insertions(+), 40 deletions(-)
drivers/android/binder.c +2 -0
@@ -5243,6 +5243,8 @@ static int __init binder_init(void)struct binder_device *device;struct hlist_node *tmp; +binder_alloc_shrinker_init (); +atomic_set (&binder_transaction_log.cur, ~0U );atomic_set (&binder_transaction_log_failed.cur, ~0U );
drivers/android/binder_alloc.c +149 -23
251 lines not on this page · raw message
drivers/android/binder_alloc.h +20 -3
@@ -21,7 +21,9 @@#include <linux/rtmutex.h> #include <linux/vmalloc.h> #include <linux/slab.h> +#include <linux/list_lru.h> +extern struct list_lru binder_alloc_lru;struct binder_transaction ;/**
@@ -60,6 +62,18 @@ struct binder_buffer {void *data; }; +/** +* struct binder_lru_page - page object used for binder shrinker +* @page_ptr: pointer to physical page in mmap'd space +* @lru: entry in binder_alloc_lru +* @alloc: binder_alloc for a proc +*/ +struct binder_lru_page { +struct list_head lru; +struct page *page_ptr; +struct binder_alloc *alloc; +}; +/** * struct binder_alloc - per-binder proc state for binder allocator * @vma: vm_area_struct passed to mmap_handler
@@ -75,8 +89,7 @@ struct binder_buffer {* @allocated_buffers: rb tree of allocated buffers sorted by address * @free_async_space: VA space available for async buffers. This is * initialized at mmap time to 1/2 the full VA space -* @pages: array of physical page addresses for each -* page of mmap'd space +* @pages: array of binder_lru_page * @buffer_size: size of address space specified via mmap * @pid: pid for associated binder_proc (invariant after init) *
@@ -96,7 +109,7 @@ struct binder_alloc {struct rb_root free_buffers;struct rb_root allocated_buffers;size_t free_async_space; -struct page **pages; +struct binder_lru_page *pages;size_t buffer_size;uint32_t buffer_free;int pid;
@@ -107,12 +120,16 @@ void binder_selftest_alloc(struct binder_alloc *alloc);#else static inline void binder_selftest_alloc (struct binder_alloc *alloc) {}#endif +enum lru_status binder_alloc_free_page (struct list_head *item, +struct list_lru_one *lru, +spinlock_t *lock,void *cb_arg);extern struct binder_buffer *binder_alloc_new_buf (struct binder_alloc *alloc,size_t data_size,size_t offsets_size,size_t extra_buffers_size,int is_async);extern void binder_alloc_init (struct binder_alloc *alloc); +void binder_alloc_shrinker_init (void );extern void binder_alloc_vma_close (struct binder_alloc *alloc);extern struct binder_buffer * binder_alloc_prepare_to_free(struct binder_alloc *alloc,
drivers/android/binder_alloc_selftest.c +54 -14
125 lines not on this page · raw message
Signature · 1 line
2.14.1.342.g6490525c54-goog
PATCH v2 5/5 android: binder: Add shrinker tracepoints
In reply to Sherry YangAdd tracepoints in binder transaction allocator to record lru hits and alloc/free page.
--- drivers/android/binder_alloc.c | 27 +++++++++++++++++++-- drivers/android/binder_trace.h | 55 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-)
drivers/android/binder_alloc.c +25 -2
@@ -237,18 +237,25 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,for (page_addr = start; page_addr < end; page_addr +=PAGE_SIZE ) {int ret;bool on_lru; +size_t index; - page = &alloc->pages[(page_addr - alloc->buffer) /PAGE_SIZE ]; + index = (page_addr - alloc->buffer) /PAGE_SIZE ; + page = &alloc->pages[index];if (page->page_ptr) { +trace_binder_alloc_lru_start (alloc, index); + on_lru =list_lru_del (&binder_alloc_lru, &page->lru);WARN_ON (!on_lru); + +trace_binder_alloc_lru_end (alloc, index);continue ; }if (WARN_ON (!vma)) goto err_page_ptr_cleared; +trace_binder_alloc_page_start (alloc, index); page->page_ptr =alloc_page (GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
@@ -278,6 +285,8 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate, alloc->pid, user_page_addr); goto err_vm_insert_page_failed; } + +trace_binder_alloc_page_end (alloc, index);/* vm_insert_page does not seem to increment the refcount */ }if (mm) {
@@ -290,11 +299,17 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,for (page_addr = end -PAGE_SIZE ; page_addr >= start; page_addr -=PAGE_SIZE ) {bool ret; +size_t index; - page = &alloc->pages[(page_addr - alloc->buffer) /PAGE_SIZE ]; + index = (page_addr - alloc->buffer) /PAGE_SIZE ; + page = &alloc->pages[index]; + +trace_binder_free_lru_start (alloc, index); ret =list_lru_add (&binder_alloc_lru, &page->lru);WARN_ON (!ret); + +trace_binder_free_lru_end (alloc, index);continue ; err_vm_insert_page_failed:
@@ -888,18 +903,26 @@ enum lru_status binder_alloc_free_page(struct list_head *item,if (!down_write_trylock (&mm->mmap_sem)) goto err_down_write_mmap_sem_failed; +trace_binder_unmap_user_start (alloc, index); +zap_page_range (alloc->vma, page_addr + alloc->user_buffer_offset,PAGE_SIZE ); +trace_binder_unmap_user_end (alloc, index); +up_write (&mm->mmap_sem);mmput (mm); } +trace_binder_unmap_kernel_start (alloc, index); +unmap_kernel_range (page_addr,PAGE_SIZE );__free_page (page->page_ptr); page->page_ptr =NULL ; +trace_binder_unmap_kernel_end (alloc, index); +list_lru_isolate (lru, item);mutex_unlock (&alloc->mutex);
drivers/android/binder_trace.h +55 -0
61 lines not on this page · raw message
Signature · 1 line
2.14.1.342.g6490525c54-goog