From: Yongting Lin <linyongting@bytedance.com>
To: anthony.yznaga@oracle.com, khalid@kernel.org, shuah@kernel.org
Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
akpm@linux-foundation.org, linux-mm@kvack.org,
libo.gcs85@bytedance.com,
Yongting Lin <linyongting@bytedance.com>
Subject: [PATCH V2 4/8] mshare: selftests: Add test case shared memory
Date: Fri, 19 Sep 2025 21:06:16 +0800 [thread overview]
Message-ID: <20250919130620.56518-4-linyongting@bytedance.com> (raw)
In-Reply-To: <20250919130620.56518-1-linyongting@bytedance.com>
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
--
2.20.1
next prev parent reply other threads:[~2025-09-19 13:06 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-19 13:06 [PATCH V2 0/8] Add selftests for mshare Yongting Lin
2025-09-19 13:06 ` [PATCH V2 1/8] mshare: Add selftests Yongting Lin
2025-09-19 13:06 ` [PATCH V2 2/8] mshare: selftests: Adding config fragments Yongting Lin
2025-09-19 13:06 ` Yongting Lin [this message]
2025-09-19 13:06 ` [PATCH V2 5/8] mshare: selftests: Add test case ioctl unmap Yongting Lin
2025-09-19 13:06 ` [PATCH V2 6/8] mshare: selftests: Add some helper functions for configuring and retrieving cgroup Yongting Lin
2025-09-19 13:06 ` [PATCH V2 7/8] mshare: selftests: Add test case to demostrate the swapping of mshare memory Yongting Lin
2025-09-19 13:06 ` [PATCH V2 8/8] mshare: selftests: Add test case to demostrate that mshare partly supports THP Yongting Lin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250919130620.56518-4-linyongting@bytedance.com \
--to=linyongting@bytedance.com \
--cc=akpm@linux-foundation.org \
--cc=anthony.yznaga@oracle.com \
--cc=khalid@kernel.org \
--cc=libo.gcs85@bytedance.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox