Skip to content

Commit 8eb52a1

Browse files
thejhgregkh
authored andcommitted
binder: Fix race between mmap() and binder_alloc_print_pages()
binder_alloc_print_pages() iterates over alloc->pages[0..alloc->buffer_size-1] under alloc->mutex. binder_alloc_mmap_handler() writes alloc->pages and alloc->buffer_size without holding that lock, and even writes them before the last bailout point. Unfortunately we can't take the alloc->mutex in the ->mmap() handler because mmap_sem can be taken while alloc->mutex is held. So instead, we have to locklessly check whether the binder_alloc has been fully initialized with binder_alloc_get_vma(), like in binder_alloc_new_buf_locked(). Fixes: 8ef4665 ("android: binder: Add page usage in binder stats") Cc: stable@vger.kernel.org Signed-off-by: Jann Horn <jannh@google.com> Acked-by: Christian Brauner <christian.brauner@ubuntu.com> Link: https://lore.kernel.org/r/20191018205631.248274-1-jannh@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 60f8a59 commit 8eb52a1

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

‎drivers/android/binder_alloc.c‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -840,14 +840,20 @@ void binder_alloc_print_pages(struct seq_file *m,
840840
int free = 0;
841841

842842
mutex_lock(&alloc->mutex);
843-
for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
844-
page = &alloc->pages[i];
845-
if (!page->page_ptr)
846-
free++;
847-
else if (list_empty(&page->lru))
848-
active++;
849-
else
850-
lru++;
843+
/*
844+
* Make sure the binder_alloc is fully initialized, otherwise we might
845+
* read inconsistent state.
846+
*/
847+
if (binder_alloc_get_vma(alloc) != NULL) {
848+
for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
849+
page = &alloc->pages[i];
850+
if (!page->page_ptr)
851+
free++;
852+
else if (list_empty(&page->lru))
853+
active++;
854+
else
855+
lru++;
856+
}
851857
}
852858
mutex_unlock(&alloc->mutex);
853859
seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);

0 commit comments

Comments
 (0)