Commit 5e024582 authored by Tiffany Yang's avatar Tiffany Yang Committed by Greg Kroah-Hartman
Browse files

binder: Scaffolding for binder_alloc KUnit tests



Add setup and teardown for testing binder allocator code with KUnit.
Include minimal test cases to verify that tests are initialized
correctly.

Tested-by: default avatarRae Moar <rmoar@google.com>
Signed-off-by: default avatarTiffany Yang <ynaffit@google.com>
Acked-by: default avatarCarlos Llamas <cmllamas@google.com>
Link: https://lore.kernel.org/r/20250714185321.2417234-5-ynaffit@google.com


Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent bdfa89c4
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -47,4 +47,15 @@ config ANDROID_BINDER_IPC_SELFTEST
	  exhaustively with combinations of various buffer sizes and
	  alignments.

config ANDROID_BINDER_ALLOC_KUNIT_TEST
	tristate "KUnit Tests for Android Binder Alloc" if !KUNIT_ALL_TESTS
	depends on ANDROID_BINDER_IPC && KUNIT
	default KUNIT_ALL_TESTS
	help
	  This feature builds the binder alloc KUnit tests.

	  Each test case runs using a pared-down binder_alloc struct and
	  test-specific freelist, which allows this KUnit module to be loaded
	  for testing without interfering with a running system.

endmenu
+1 −0
Original line number Diff line number Diff line
@@ -4,3 +4,4 @@ ccflags-y += -I$(src) # needed for trace events
obj-$(CONFIG_ANDROID_BINDERFS)		+= binderfs.o
obj-$(CONFIG_ANDROID_BINDER_IPC)	+= binder.o binder_alloc.o
obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) += binder_alloc_selftest.o
obj-$(CONFIG_ANDROID_BINDER_ALLOC_KUNIT_TEST)	+= tests/
+4 −1
Original line number Diff line number Diff line
@@ -68,6 +68,8 @@
#include <linux/sizes.h>
#include <linux/ktime.h>

#include <kunit/visibility.h>

#include <uapi/linux/android/binder.h>

#include <linux/cacheflush.h>
@@ -5947,10 +5949,11 @@ static void binder_vma_close(struct vm_area_struct *vma)
	binder_alloc_vma_close(&proc->alloc);
}

static vm_fault_t binder_vm_fault(struct vm_fault *vmf)
VISIBLE_IF_KUNIT vm_fault_t binder_vm_fault(struct vm_fault *vmf)
{
	return VM_FAULT_SIGBUS;
}
EXPORT_SYMBOL_IF_KUNIT(binder_vm_fault);

static const struct vm_operations_struct binder_vm_ops = {
	.open = binder_vma_open,
+10 −5
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include <linux/uaccess.h>
#include <linux/highmem.h>
#include <linux/sizes.h>
#include <kunit/visibility.h>
#include "binder_alloc.h"
#include "binder_trace.h"

@@ -57,13 +58,14 @@ 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,
VISIBLE_IF_KUNIT size_t binder_alloc_buffer_size(struct binder_alloc *alloc,
						 struct binder_buffer *buffer)
{
	if (list_is_last(&buffer->entry, &alloc->buffers))
		return alloc->vm_start + alloc->buffer_size - buffer->user_data;
	return binder_buffer_next(buffer)->user_data - buffer->user_data;
}
EXPORT_SYMBOL_IF_KUNIT(binder_alloc_buffer_size);

static void binder_insert_free_buffer(struct binder_alloc *alloc,
				      struct binder_buffer *new_buffer)
@@ -955,7 +957,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
			   failure_string, ret);
	return ret;
}

EXPORT_SYMBOL_IF_KUNIT(binder_alloc_mmap_handler);

void binder_alloc_deferred_release(struct binder_alloc *alloc)
{
@@ -1024,6 +1026,7 @@ void binder_alloc_deferred_release(struct binder_alloc *alloc)
		     "%s: %d buffers %d, pages %d\n",
		     __func__, alloc->pid, buffers, page_count);
}
EXPORT_SYMBOL_IF_KUNIT(binder_alloc_deferred_release);

/**
 * binder_alloc_print_allocated() - print buffer info
@@ -1116,6 +1119,7 @@ void binder_alloc_vma_close(struct binder_alloc *alloc)
{
	binder_alloc_set_mapped(alloc, false);
}
EXPORT_SYMBOL_IF_KUNIT(binder_alloc_vma_close);

/**
 * binder_alloc_free_page() - shrinker callback to free pages
@@ -1223,7 +1227,7 @@ binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)

static struct shrinker *binder_shrinker;

static void __binder_alloc_init(struct binder_alloc *alloc,
VISIBLE_IF_KUNIT void __binder_alloc_init(struct binder_alloc *alloc,
					  struct list_lru *freelist)
{
	alloc->pid = current->group_leader->pid;
@@ -1233,6 +1237,7 @@ static void __binder_alloc_init(struct binder_alloc *alloc,
	INIT_LIST_HEAD(&alloc->buffers);
	alloc->freelist = freelist;
}
EXPORT_SYMBOL_IF_KUNIT(__binder_alloc_init);

/**
 * binder_alloc_init() - called by binder_open() for per-proc initialization
+6 −0
Original line number Diff line number Diff line
@@ -184,5 +184,11 @@ int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
				  binder_size_t buffer_offset,
				  size_t bytes);

#if IS_ENABLED(CONFIG_KUNIT)
void __binder_alloc_init(struct binder_alloc *alloc, struct list_lru *freelist);
size_t binder_alloc_buffer_size(struct binder_alloc *alloc,
				struct binder_buffer *buffer);
#endif

#endif /* _LINUX_BINDER_ALLOC_H */
Loading