linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Yongting Lin <linyongting@bytedance.com>
To: anthony.yznaga@oracle.com, khalid@kernel.org, shuah@kernel.org,
	linyongting@bytedance.com
Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	akpm@linux-foundation.org, linux-mm@kvack.org
Subject: [PATCH 3/8] mshare: selftests: Add some helper function for mshare filesystem
Date: Mon, 25 Aug 2025 22:57:14 +0800	[thread overview]
Message-ID: <20250825145719.29455-12-linyongting@bytedance.com> (raw)
In-Reply-To: <20250825145719.29455-1-linyongting@bytedance.com>

Before create basic test cases, we need to have some helper functions
to help setup the tests.

These helper functions consist of:
  Mount and unmount the mshare filesystem
  Create a temporary file which be performed test on it later
  Map and unmap mshare region via the ioctl syscall

Signed-off-by: Yongting Lin <linyongting@bytedance.com>
---
 tools/testing/selftests/mshare/basic.c |   1 +
 tools/testing/selftests/mshare/util.c  | 123 +++++++++++++++++++++++++
 2 files changed, 124 insertions(+)
 create mode 100644 tools/testing/selftests/mshare/util.c

diff --git a/tools/testing/selftests/mshare/basic.c b/tools/testing/selftests/mshare/basic.c
index 482af948878d..35739b1133f7 100644
--- a/tools/testing/selftests/mshare/basic.c
+++ b/tools/testing/selftests/mshare/basic.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include "../kselftest_harness.h"
+#include "util.c"
 
 TEST(basic)
 {
diff --git a/tools/testing/selftests/mshare/util.c b/tools/testing/selftests/mshare/util.c
new file mode 100644
index 000000000000..75f6ff25aa2c
--- /dev/null
+++ b/tools/testing/selftests/mshare/util.c
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/msharefs.h>
+#include <stdio.h>
+#include <mntent.h>
+#include <sys/mount.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+/*
+ *  Helper functions for mounting msharefs
+ */
+
+#define MOUNT_POINT "/sys/fs/mshare"
+#define FS_TYPE     "msharefs"
+
+bool is_msharefs_mounted(void)
+{
+	FILE *fp;
+	struct mntent *ent;
+	bool found = false;
+
+	fp = setmntent("/proc/mounts", "r");
+	if (!fp) {
+		perror("setmntent");
+		exit(1);
+	}
+
+	while ((ent = getmntent(fp)) != NULL) {
+		if (strcmp(ent->mnt_dir, MOUNT_POINT) == 0 &&
+			strcmp(ent->mnt_type, FS_TYPE) == 0) {
+			found = true;
+			break;
+		}
+	}
+
+	endmntent(fp);
+	return found;
+}
+
+bool msharefs_premounted;
+
+__attribute__((constructor))
+void mount_sharefs(void)
+{
+	msharefs_premounted = is_msharefs_mounted();
+	if (msharefs_premounted)
+		return;
+
+	if (mount(FS_TYPE, MOUNT_POINT, FS_TYPE, 0, NULL) != 0) {
+		perror("mount");
+		exit(1);
+	}
+}
+
+__attribute__((destructor))
+void umount_sharefs(void)
+{
+	if (!msharefs_premounted && umount(MOUNT_POINT) != 0) {
+		perror("umount");
+		exit(1);
+	}
+}
+
+/*
+ *  Helper functions for mshare files
+ */
+
+#define MSHARE_INFO MOUNT_POINT "/mshare_info"
+#define MSHARE_TEST MOUNT_POINT "/mshare-test-XXXXXX"
+
+size_t mshare_get_info(void)
+{
+	char req[128];
+	size_t size;
+	int fd;
+
+	fd = open(MSHARE_INFO, O_RDONLY);
+	if (fd == -1)
+		return -1;
+
+	read(fd, req, sizeof(req));
+	size = atoll(req);
+	close(fd);
+
+	return size;
+}
+
+int create_mshare_file(char *filename, size_t len)
+{
+	int fd;
+
+	strncpy(filename, MSHARE_TEST, len - 1);
+	fd = mkstemp(filename);
+
+	return fd;
+}
+
+
+int mshare_ioctl_mapping(int fd, size_t size, int flags)
+{
+	struct mshare_create mcreate;
+
+	mcreate.region_offset = 0;
+	mcreate.size = size;
+	mcreate.offset = 0;
+	mcreate.prot = PROT_READ | PROT_WRITE;
+	mcreate.flags = flags;
+	mcreate.fd = -1;
+
+	return ioctl(fd, MSHAREFS_CREATE_MAPPING, &mcreate);
+}
+
+int mshare_ioctl_munmap(int fd, size_t size)
+{
+	struct mshare_unmap munmap;
+
+	munmap.region_offset = 0;
+	munmap.size = size;
+
+	return ioctl(fd, MSHAREFS_UNMAP, &munmap);
+}
-- 
2.20.1



  parent reply	other threads:[~2025-08-25 14:58 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-25 14:57 [PATCH 0/8] Add selftests for mshare Yongting Lin
2025-08-25 14:57 ` [PATCH 1/8] mshare: Add selftests Yongting Lin
2025-08-25 14:57 ` [PATCH 2/8] mshare: selftests: Adding config fragment Yongting Lin
2025-08-25 14:57 ` [PATCH 3/8] mshare: selftests: Add some helper function for mshare filesystem Yongting Lin
2025-08-25 14:57 ` [PATCH 4/8] mshare: selftests: Add test case shared memory Yongting Lin
2025-08-29  0:59   ` Anthony Yznaga
2025-09-01 12:50     ` [External] " Yongting Lin
2025-09-02 21:34       ` Anthony Yznaga
2025-08-25 14:57 ` [PATCH 5/8] mshare: selftests: Add test case ioctl unmap Yongting Lin
2025-08-25 14:57 ` [PATCH 6/8] mshare: selftests: Add some helper functions for reading and controlling cgroup Yongting Lin
2025-08-25 14:57 ` [PATCH 7/8] mshare: selftests: Add test case to demostrate the swaping of mshare memory Yongting Lin
2025-08-25 14:57 ` [PATCH 8/8] mshare: selftests: Add test case to demostrate that mshare doesn't support THP Yongting Lin
2025-08-29  1:04   ` Anthony Yznaga
2025-08-25 14:57 ` [PATCH 1/8] mshare: Add selftests Yongting Lin
2025-08-25 14:57 ` [PATCH 2/8] mshare: selftests: Adding config fragment Yongting Lin
2025-08-25 14:57 ` Yongting Lin [this message]
2025-08-25 14:57 ` [PATCH 4/8] mshare: selftests: Add test case shared memory Yongting Lin
2025-08-25 14:57 ` [PATCH 5/8] mshare: selftests: Add test case ioctl unmap Yongting Lin
2025-08-25 14:57 ` [PATCH 6/8] mshare: selftests: Add some helper functions for reading and controlling cgroup Yongting Lin
2025-08-25 14:57 ` [PATCH 7/8] mshare: selftests: Add test case to demostrate the swaping of mshare memory Yongting Lin
2025-08-25 14:57 ` [PATCH 8/8] mshare: selftests: Add test case to demostrate that mshare doesn't support THP Yongting Lin
2025-08-26 11:16 ` [PATCH 0/8] Add selftests for mshare 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=20250825145719.29455-12-linyongting@bytedance.com \
    --to=linyongting@bytedance.com \
    --cc=akpm@linux-foundation.org \
    --cc=anthony.yznaga@oracle.com \
    --cc=khalid@kernel.org \
    --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;
as well as URLs for NNTP newsgroup(s).