Skip to content

Commit

Permalink
Implement LKL MMU Kunit test suite.
Browse files Browse the repository at this point in the history
The test suite implements just a single test case verifying that
`vmalloc` works as expected.

```
make -C tools/lkl MMU=1 MMU_KUNIT=1 clean-conf all
tools/lkl/tests/boot
```

Change-Id: Ie5065343ac24c438b95788d67b34de48ae8bdc5e
  • Loading branch information
rodionov committed Dec 25, 2024
1 parent 66c6e6f commit fdaaaa2
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
5 changes: 5 additions & 0 deletions arch/lkl/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ config LKL_TASK_UNMAPPED_BASE
Starting virtual address for LKL user-space mmap. It is assumed that
the host mmap is able to map requested amount of memory starting from
this address.

config LKL_MMU_KUNIT_TEST
bool "Kunit tests for LKL MMU"
default n
depends on KUNIT
endif

config COREDUMP
Expand Down
4 changes: 2 additions & 2 deletions arch/lkl/mm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ KASAN_SANITIZE_kasan.o := n

obj-y = bootmem.o
obj-$(CONFIG_KASAN) += kasan.o

obj-$(CONFIG_MMU) += mmu_mem.o
obj-$(CONFIG_MMU) += mmu_mem.o
obj-$(CONFIG_LKL_MMU_KUNIT_TEST) += mmu_test.o
51 changes: 51 additions & 0 deletions arch/lkl/mm/mmu_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: GPL-2.0

#include <kunit/test.h>

#include <linux/highmem.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/vmalloc.h>

#include <asm/page.h>

static void vmalloc_test(struct kunit *test)
{
unsigned long nr_pages = 255;
void *ptr = vmalloc (nr_pages * PAGE_SIZE);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);

for (int i = 0; i < nr_pages; i ++)
memset(ptr + i * PAGE_SIZE, i, PAGE_SIZE);

for (int i = 0; i < nr_pages; i ++) {
struct page * pg = vmalloc_to_page(ptr + i * PAGE_SIZE);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pg);

unsigned char *va = (unsigned char *)kmap_local_page(pg);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, va);
KUNIT_ASSERT_PTR_NE(test, va, ptr + i * PAGE_SIZE);

KUNIT_ASSERT_TRUE(test, va[0] == (unsigned char)i);

kunmap_local(va);
}

vfree(ptr);
}

static struct kunit_case mmu_kunit_test_cases[] = {
KUNIT_CASE(vmalloc_test),
{}
};

static struct kunit_suite lkl_mmu_kunit_test_suite = {
.name = "lkl_mmu",
.test_cases = mmu_kunit_test_cases,
};

kunit_test_suite(lkl_mmu_kunit_test_suite);

MODULE_LICENSE("GPL");
7 changes: 7 additions & 0 deletions tools/lkl/Makefile.autoconf
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,20 @@ define do_autoconf_fuzzing
$(if $(LKL_LINE_COV),$(call set_kernel_config,LKL_LINE_COV,y))
endef

define mmu_test_enable
$(call set_autoconf_var,LKL_MMU_TEST,y)
$(call set_kernel_config,KUNIT,y)
$(call set_kernel_config,LKL_MMU_KUNIT_TEST,y)
endef

define do_autoconf_mmu
$(call set_autoconf_var,MMU,y)
$(call set_kernel_config,MMU,y)
# Don't need swap in LKL. CONFIG_SHMEM generates a compile-time assertion
# when build with gcc, however, works fine with clang. The issues is in macro
# HPAGE_PMD_SIZE which should be optimized out but is not in case of gcc.
$(call set_kernel_config,SHMEM,n)
$(if $(MMU_KUNIT), $(call mmu_test_enable))
endef

define do_autoconf
Expand Down
34 changes: 33 additions & 1 deletion tools/lkl/tests/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,36 @@ static int lkl_test_private_mmap(void)
}
#endif // LKL_HOST_CONFIG_MMU

#define CMD_LINE "mem=32M loglevel=8 " KASAN_CMD_LINE
#ifdef LKL_HOST_CONFIG_LKL_MMU_TEST
static int lkl_test_kunit_mmu(void)
{
char *log = strdup(boot_log);
char *line = NULL;
int n;
char c,d;

line = strtok(log, "\n");
while (line) {
if (sscanf(line, "[ %*f] ok %d lkl_m%c%c", &n, &c, &d) == 3 &&
c == 'm' && d == 'u') {
lkl_test_logf("%s", line);
return TEST_SUCCESS;
}

line = strtok(NULL, "\n");
}

free(log);

return TEST_FAILURE;
}

#define LKL_MMU_TEST_CMD_LINE "kunit.filter_glob=lkl_mmu "
#else
#define LKL_MMU_TEST_CMD_LINE
#endif // LKL_HOST_CONFIG_LKL_MMU_TEST

#define CMD_LINE "mem=32M loglevel=8 " KASAN_CMD_LINE LKL_MMU_TEST_CMD_LINE

static int lkl_test_start_kernel(void)
{
Expand Down Expand Up @@ -660,6 +689,9 @@ struct lkl_test tests[] = {
#ifdef LKL_HOST_CONFIG_MMU
LKL_TEST(shared_mmap),
LKL_TEST(private_mmap),
#endif
#ifdef LKL_HOST_CONFIG_LKL_MMU_TEST
LKL_TEST(kunit_mmu),
#endif
LKL_TEST(stop_kernel),
};
Expand Down

0 comments on commit fdaaaa2

Please sign in to comment.