This test case aims to verify the basic functionalities of mshare.
Create a mshare file and use ioctl to create mapping for host mm with supportive flags, then create processes to map mshare file to their memory space, and eventually verify the correctiness of sharing memory.
To ensure these tests can run on any server or device with minimal memory usage, we follow the steps below:
1. The ftruncate size must be a multiple of the alignment size. 2. In the ioctl(MSHAREFS_CREATE_MAPPING) syscall, which determines the memory size occupied by an mshare instance, we use 4K/8K for normal pages and 2M/4M for hugetlb pages. 3. The size used in the mmap syscall must match the ftruncate size.
Signed-off-by: Yongting Lin linyongting@bytedance.com --- tools/testing/selftests/mshare/basic.c | 82 +++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/mshare/basic.c b/tools/testing/selftests/mshare/basic.c index 35739b1133f7..54a132a8116c 100644 --- a/tools/testing/selftests/mshare/basic.c +++ b/tools/testing/selftests/mshare/basic.c @@ -3,9 +3,87 @@ #include "../kselftest_harness.h" #include "util.c"
-TEST(basic) +#define STRING "I am Msharefs" + +FIXTURE(basic) +{ + char filename[128]; + size_t align_size; +}; + +FIXTURE_VARIANT(basic) { + size_t allocate_size; + /* flags for ioctl */ + int map_flags; +}; + +FIXTURE_VARIANT_ADD(basic, ANON_4k) { + .allocate_size = KB(4), + .map_flags = MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, +}; + +FIXTURE_VARIANT_ADD(basic, HUGETLB_2m) { + .allocate_size = MB(2), + .map_flags = MAP_ANONYMOUS | MAP_HUGETLB | MAP_SHARED | MAP_FIXED, +}; + +FIXTURE_VARIANT_ADD(basic, ANON_8k) { + .allocate_size = KB(8), + .map_flags = MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, +}; + +FIXTURE_VARIANT_ADD(basic, HUGETLB_4m) { + .allocate_size = MB(4), + .map_flags = MAP_ANONYMOUS | MAP_HUGETLB | MAP_SHARED | MAP_FIXED, +}; + +FIXTURE_SETUP(basic) +{ + int fd; + + self->align_size = mshare_get_info(); + + fd = create_mshare_file(self->filename, sizeof(self->filename)); + ftruncate(fd, self->align_size); + + if (variant->map_flags & MAP_HUGETLB) + ksft_print_msg("Tip: Please enable hugepages before running this test.\n" + "For example: sysctl -w vm.nr_hugepages=2\n"); + + ASSERT_EQ(mshare_ioctl_mapping(fd, variant->allocate_size, variant->map_flags), 0); + close(fd); +} + +FIXTURE_TEARDOWN(basic) +{ + ASSERT_EQ(unlink(self->filename), 0); +} + +TEST_F(basic, shared_mem) { - printf("Hello mshare\n"); + int fd; + void *addr; + pid_t pid = fork(); + + ASSERT_NE(pid, -1); + + fd = open(self->filename, O_RDWR, 0600); + ASSERT_NE(fd, -1); + + addr = mmap(NULL, self->align_size, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); + ASSERT_NE(addr, MAP_FAILED); + + if (pid == 0) { + /* Child process write date the shared memory */ + memcpy(addr, STRING, sizeof(STRING)); + exit(0); + } + + ASSERT_NE(waitpid(pid, NULL, 0), -1); + + /* Parent process should retrieve the data from the shared memory */ + ASSERT_EQ(memcmp(addr, STRING, sizeof(STRING)), 0); }
TEST_HARNESS_MAIN