Skip to contentSkip to conversation

Hugetlb: Migrating hugetlb tests from libhugetlbfs

RFC PATCH In review Tarun Sahu 2 participants 5 messages Sep 8, 2022 – Sep 12, 2022

Subsystems (2)
lore
Share & tools
lore

Tarun Sahu

author
#0 lore raw

RFC PATCH 1/1 Hugetlb: Migrating hugetlb tests from libhugetlbfs

Libhugetlbfs is not being maintained actively, and some distro is dropping support for it. There are some tests that are good for testing hugetlb functionality in kernel, which can be migrated to either kernel kselftests or LTP.
I am submitting this patch to get comments from community on the following
1. The test framework in ltp is most suitable for the tests that are in libhugetlbfs/tests/ which follow similar test framework. And there is already a section for hugetlb specific tests in LTP. So it makes more sense and less effort to migrate the test to LTP. Though I recommend migrating these tests to LTP, I would like to discuss tests should be migrated to LTP or kselftests.
2. Libhugetlbfs tests has license GNU Lesser GPL 2.1 or later, while kernel and LTP has license GPL2 or later, so can the test be migrated to kernel/kselftests or LTP.

The below patch is libhugetlbfs/tests/direct.c which has been migrated to ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c

---
 runtest/hugetlb                               |   2 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap07.c  | 106 ++++++++++++++++++
 3 files changed, 109 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c
runtest/hugetlb +2 -0
index f719217ab..ee02835d3 100644
@@ -3,6 +3,8 @@ hugemmap02 hugemmap02
 hugemmap04 hugemmap04
 hugemmap05 hugemmap05
 hugemmap06 hugemmap06
+hugemmap07 hugemmap07
+
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
testcases/kernel/mem/.gitignore +1 -0
index ff2910533..df5256ec8 100644
@@ -4,6 +4,7 @@
 /hugetlb/hugemmap/hugemmap04
 /hugetlb/hugemmap/hugemmap05
 /hugetlb/hugemmap/hugemmap06
+/hugetlb/hugemmap/hugemmap07
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +106 -0

106 lines not on this page · raw message

Signature · 1 line
2.31.1
#1 lore raw

Hi!

Quoted 39 lines
---
 runtest/hugetlb                               |   2 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap07.c  | 106 ++++++++++++++++++
 3 files changed, 109 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c
runtest/hugetlb +2 -0
index f719217ab..ee02835d3 100644
@@ -3,6 +3,8 @@ hugemmap02 hugemmap02
 hugemmap04 hugemmap04
 hugemmap05 hugemmap05
 hugemmap06 hugemmap06
+hugemmap07 hugemmap07
+
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
testcases/kernel/mem/.gitignore +1 -0
index ff2910533..df5256ec8 100644
@@ -4,6 +4,7 @@
 /hugetlb/hugemmap/hugemmap04
 /hugetlb/hugemmap/hugemmap05
 /hugetlb/hugemmap/hugemmap06
+/hugetlb/hugemmap/hugemmap07
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +0 -0
new file mode 100644
index 000000000..798735ed0

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 3

Quoted 3 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +3 -0
@@ -0,0 +1,106 @@
+/*
+ * License/Copyright Details
+ */

There should be a SPDX licence identifier here instead.

Also testcase should include a special ascii-doc formatted comment here that describes the purpose of the test.

Quoted 5 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +5 -0
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 19

Quoted 10 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +10 -0
+#include <sys/types.h>
+
+#include "tst_test.h"
+
+#define P0 "ffffffff"
+#define IOSZ 4096
+char buf[IOSZ] __attribute__((aligned(IOSZ)));
+static int  fildes = -1, nfildes = -1;
+static char TEMPFILE[MAXPATHLEN];
+static char NTEMPFILE[MAXPATHLEN];

Uppercase is reserved for macros in C.

Have you run 'make check' to check for common mistakes before submitting?

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 21

Quoted 1 line
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +1 -0
+void test_directio(void)

should be static

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 30

Quoted 9 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +9 -0
+{
+	long hpage_size;
+	void *p;
+	int ret;
+
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:");
+
+	fildes = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0600);
+	nfildes = SAFE_OPEN(NTEMPFILE, O_CREAT|O_EXCL|O_RDWR|O_DIRECT, 0600);

I would say that fd and nfd in the original code was were better names, shorter and to the point. See also:

https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 34

Quoted 3 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +3 -0
+	p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fildes, 0);
+	if (p == MAP_FAILED)
+		tst_brk(TFAIL | TERRNO, "mmap() Failed on %s", TEMPFILE);

We do have SAFE_MMAP() as well.

Quoted 11 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +11 -0
+	memcpy(p, P0, 8);
+
+	/* Direct write from huge page */
+	ret = write(nfildes, p, IOSZ);
+	if (ret == -1)
+		tst_brk(TFAIL | TERRNO, "Direct-IO write from huge page");
+	if (ret != IOSZ)
+		tst_brk(TFAIL, "Short direct-IO write from huge page");
+	if (lseek(nfildes, 0, SEEK_SET) == -1)
+		tst_brk(TFAIL | TERRNO, "lseek");
+

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 56

Quoted 10 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +10 -0
+	/* Check for accuracy */
+	ret = read(nfildes, buf, IOSZ);
+	if (ret == -1)
+		tst_brk(TFAIL | TERRNO, "Direct-IO read to normal memory");
+	if (ret != IOSZ)
+		tst_brk(TFAIL, "Short direct-IO read to normal memory");
+	if (memcmp(P0, buf, 8))
+		tst_brk(TFAIL, "Memory mismatch after Direct-IO write");
+	if (lseek(nfildes, 0, SEEK_SET) == -1)
+		tst_brk(TFAIL | TERRNO, "lseek");

And we have SAFE_WRITE(), SAFE_READ(), and SAFE_LSEEK() as well.

Also tst_brk(TFAIL, "") usage is deprecated and should not be used in new tests.

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 69

Quoted 12 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +12 -0
+	/* Direct read to huge page */
+	memset(p, 0, IOSZ);
+	ret = read(nfildes, p, IOSZ);
+	if (ret == -1)
+		tst_brk(TFAIL | TERRNO, "Direct-IO read to huge page");
+	if (ret != IOSZ)
+		tst_brk(TFAIL, "Short direct-IO read to huge page");
+
+	/* Check for accuracy */
+	if (memcmp(p, P0, 8))
+		tst_brk(TFAIL, "Memory mismatch after Direct-IO read");
+	tst_res(TPASS, "Successfully tested Hugepage Direct I/O");

You should close filedescriptors and unmap memory here, otherwise the test will fail with large enough -i parameter.

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 72

Quoted 3 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +3 -0
+}
+
+void setup(void)

should be static.

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 81

Quoted 9 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +9 -0
+{
+	if (tst_hugepages == 0)
+		tst_brk(TCONF, "Not enough hugepages for testing.");
+
+	if (!Hopt)
+		Hopt = tst_get_tmpdir();
+	SAFE_MOUNT("none", Hopt, "hugetlbfs", 0, NULL);
+
+	snprintf(TEMPFILE, sizeof(TEMPFILE), "%s/mmapfile%d", Hopt, getpid());

Ideally all files created outside of the test temporary directory should be prefixed with "ltp_"

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 82

Quoted 1 line
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +1 -0
+	snprintf(NTEMPFILE, sizeof(NTEMPFILE), "%s/nmmapfile%d", "/home/", getpid());

Please do not create any files outside of the test temporary directory, also as the temporary directory is unique already, there is no need to actually create the second tempfile name like this. All we need to do is to is something as:

#define NTEMPFILE "tempfile"

Quoted 30 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +24 -0
+}
+
+void cleanup(void)
+{
+	close(fildes);
+	close(nfildes);
+	remove(TEMPFILE);
+	remove(NTEMPFILE);
+	umount2(Hopt, MNT_DETACH);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.options = (struct tst_option[]) {
+		{"H:", &Hopt,   "Location of hugetlbfs, i.e.  -H /var/hugetlbfs"},
+		{"s:", &nr_opt, "Set the number of the been allocated hugepages"},
+		{}
+	},
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_directio,
+	.hugepages = {2, TST_REQUEST},
+};
Signature · 5 lines
2.31.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#2 lore raw

Hi Cyril, Thanks for reviewing the patch.
Below I have added my
comments. Will update changes in V2.

Quoted 50 lines · Cyril Hrubis

Hi!

Quoted 43 lines
---
 runtest/hugetlb                               |   2 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap07.c  | 106
++++++++++++++++++
 3 files changed, 109 insertions(+)
 create mode 100644

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c

runtest/hugetlb +2 -0
index f719217ab..ee02835d3 100644
@@ -3,6 +3,8 @@ hugemmap02 hugemmap02
 hugemmap04 hugemmap04
 hugemmap05 hugemmap05
 hugemmap06 hugemmap06
+hugemmap07 hugemmap07
+
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
testcases/kernel/mem/.gitignore +1 -0
index ff2910533..df5256ec8 100644
@@ -4,6 +4,7 @@
 /hugetlb/hugemmap/hugemmap04
 /hugetlb/hugemmap/hugemmap05
 /hugetlb/hugemmap/hugemmap06
+/hugetlb/hugemmap/hugemmap07
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +0 -0
new file mode 100644
index 000000000..798735ed0

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 3

Quoted 3 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +3 -0
@@ -0,0 +1,106 @@
+/*
+ * License/Copyright Details
+ */

There should be a SPDX licence identifier here instead.

Quoted 3 lines

Also testcase should include a special ascii-doc formatted comment here
that describes the purpose of the test.

As mentioned in the patch description, there is a conflict in license, That is why, I have avoided to put any of them in the header. Once confirmed within the community, I can add the original license here. (GPL2.1+) as
https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines
this says only to add code with GPL2.0+.

Quoted 19 lines
Quoted 6 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +5 -0
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 19

Quoted 10 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +10 -0
+#include <sys/types.h>
+
+#include "tst_test.h"
+
+#define P0 "ffffffff"
+#define IOSZ 4096
+char buf[IOSZ] __attribute__((aligned(IOSZ)));
+static int  fildes = -1, nfildes = -1;
+static char TEMPFILE[MAXPATHLEN];
+static char NTEMPFILE[MAXPATHLEN];

Uppercase is reserved for macros in C.

Quoted 7 lines

Have you run 'make check' to check for common mistakes before submitting?

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 21

Quoted 1 line
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +1 -0
+void test_directio(void)

should be static

Ok Will update in V2.

Quoted 16 lines
Quoted 11 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +9 -0
+{
+	long hpage_size;
+	void *p;
+	int ret;
+
+	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:");
+
+	fildes = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0600);
+	nfildes = SAFE_OPEN(NTEMPFILE, O_CREAT|O_EXCL|O_RDWR|O_DIRECT,

0600);

I would say that fd and nfd in the original code was were better names,
shorter and to the point. See also:

Quoted 1 line

https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines

I agree, I tried to follow what was in hugemmap01..06 test cases to keep things similar all across the tests. I will update it in v2.

Quoted 8 lines
Quoted 6 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +1 -0
+	p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,

fildes, 0);

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +2 -0
+	if (p == MAP_FAILED)
+		tst_brk(TFAIL | TERRNO, "mmap() Failed on %s",

TEMPFILE);

We do have SAFE_MMAP() as well.

Yeah, I will update them in v2.

Quoted 20 lines
Quoted 20 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +6 -0
+	memcpy(p, P0, 8);
+
+	/* Direct write from huge page */
+	ret = write(nfildes, p, IOSZ);
+	if (ret == -1)
+		tst_brk(TFAIL | TERRNO, "Direct-IO write from huge

page");

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +9 -0
+	if (ret != IOSZ)
+		tst_brk(TFAIL, "Short direct-IO write from huge page");
+	if (lseek(nfildes, 0, SEEK_SET) == -1)
+		tst_brk(TFAIL | TERRNO, "lseek");
+
+	/* Check for accuracy */
+	ret = read(nfildes, buf, IOSZ);
+	if (ret == -1)
+		tst_brk(TFAIL | TERRNO, "Direct-IO read to normal

memory");

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +2 -0
+	if (ret != IOSZ)
+		tst_brk(TFAIL, "Short direct-IO read to normal
Quoted 8 lines

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c

Quoted 6 lines

memory");

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +2 -0
+	if (memcmp(P0, buf, 8))
+		tst_brk(TFAIL, "Memory mismatch after Direct-IO

write");

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +2 -0
+	if (lseek(nfildes, 0, SEEK_SET) == -1)
+		tst_brk(TFAIL | TERRNO, "lseek");

And we have SAFE_WRITE(), SAFE_READ(), and SAFE_LSEEK() as well.

ok Will update these too in v2.

Quoted 3 lines

Also tst_brk(TFAIL, "") usage is deprecated and should not be used in new tests.

Ok, Will update it to tst_res.

Quoted 16 lines

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 69

Quoted 13 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +12 -0
+	/* Direct read to huge page */
+	memset(p, 0, IOSZ);
+	ret = read(nfildes, p, IOSZ);
+	if (ret == -1)
+		tst_brk(TFAIL | TERRNO, "Direct-IO read to huge page");
+	if (ret != IOSZ)
+		tst_brk(TFAIL, "Short direct-IO read to huge page");
+
+	/* Check for accuracy */
+	if (memcmp(p, P0, 8))
+		tst_brk(TFAIL, "Memory mismatch after Direct-IO read");
+	tst_res(TPASS, "Successfully tested Hugepage Direct I/O");

You should close filedescriptors and unmap memory here, otherwise the test will fail with large enough -i parameter.

Ok, I will update it too in v2.

Quoted 6 lines

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c line 72

Quoted 4 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +3 -0
+}
+
+void setup(void)

should be static.

Ok Will update it in V2.

Quoted 15 lines
Quoted 11 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +9 -0
+{
+	if (tst_hugepages == 0)
+		tst_brk(TCONF, "Not enough hugepages for testing.");
+
+	if (!Hopt)
+		Hopt = tst_get_tmpdir();
+	SAFE_MOUNT("none", Hopt, "hugetlbfs", 0, NULL);
+
+	snprintf(TEMPFILE, sizeof(TEMPFILE), "%s/mmapfile%d", Hopt,

getpid());

Ideally all files created outside of the test temporary directory should
be prefixed with "ltp_"

ok, Will update it in V2.

Quoted 12 lines
Quoted 3 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +1 -0
+	snprintf(NTEMPFILE, sizeof(NTEMPFILE), "%s/nmmapfile%d",

"/home/", getpid());

Please do not create any files outside of the test temporary directory,
also as the temporary directory is unique already, there is no need to
actually create the second tempfile name like this. All we need to do is
to is something as:

Quoted 2 lines

#define NTEMPFILE "tempfile"

Ok Will update it in V2.

Quoted 32 lines
Quoted 32 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +16 -0
+}
+
+void cleanup(void)
+{
+	close(fildes);
+	close(nfildes);
+	remove(TEMPFILE);
+	remove(NTEMPFILE);
+	umount2(Hopt, MNT_DETACH);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.options = (struct tst_option[]) {
+		{"H:", &Hopt,   "Location of hugetlbfs, i.e.  -H

/var/hugetlbfs"},

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +1 -0
+		{"s:", &nr_opt, "Set the number of the been allocated

hugepages"},

testcases/kernel/mem/hugetlb/hugemmap/hugemmap07.c new file +7 -0
+		{}
+	},
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = test_directio,
+	.hugepages = {2, TST_REQUEST},
+};
Signature · 5 lines
2.31.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi!

Quoted 6 lines

As mentioned in the patch description, there is a conflict in license, That is why, I have avoided to put any of them in the header. Once confirmed within the community, I can add the original license here. (GPL2.1+) as
https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines
this says only to add code with GPL2.0+.

As far as I can tell there is no GPL2.1+ the only 2.1 version in existence is LGPL.

GPL2.1+ usually happens to be an error when someone takes library header with LGPL2.1+ license and removes the "Lesser" part.

However it looks like the whole libhugetlbfs is under LGPL2.1+ which kind of makes sense for a library, but not so much for the tests since these do not provide a library that can be linked againts at all.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#4 lore raw

Hi,

Thanks for confirming.

There is one more confirmation I required
before I submit a patch series on necessary libhugetlbfs tests, Between
LTP and Kselftests, Choosing LTP is right decision? (mentioned details in patch description)

Thanks

Quoted 22 lines · Cyril Hrubis

Hi!

Quoted 8 lines

As mentioned in the patch description, there is a conflict in license,
That is why, I have avoided to put any of them in the header. Once confirmed within the community, I can add the original license here.
(GPL2.1+) as
https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines
this says only to add code with GPL2.0+.

As far as I can tell there is no GPL2.1+ the only 2.1 version in existence is LGPL.

GPL2.1+ usually happens to be an error when someone takes library header
with LGPL2.1+ license and removes the "Lesser" part.

However it looks like the whole libhugetlbfs is under LGPL2.1+ which kind of makes sense for a library, but not so much for the tests since
these do not provide a library that can be linked againts at all.