Skip to contentSkip to conversation

fstests: test reads/writes from hugepages-backed buffers

PATCH Superseded Joanne Koong 3 participants 15 messages Jan 15, 2025 – Jan 17, 2025 VFS

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover fstests: test reads/writes from hugepages-backed buffers

There was a recent bug in rc1 [1] that was due to faulty handling for userspace buffers backed by hugepages.

This patchset adds generic tests for reads/writes from buffers backed by hugepages.

[1] https://lore.kernel.org/linux-fsdevel/p3iss6hssbvtdutnwmuddvdadubrhfkdoosgmbewvo674f7f3y@cwnwffjqltzw/

Changelog:
v2: https://lore.kernel.org/linux-fsdevel/[email protected]/
- Gate -h and MADV_COLLAPSE usage to get it compatible on old systems (Zorro)
- Use calloc instead of malloc/memset (Nirjhar)
- Update exit codes in 1st patch
- Add Brian's reviewed-bys

v1: https://lore.kernel.org/linux-fsdevel/[email protected]/
- Refactor out buffer initialization (Brian)
- Update commit messages of 1st patch (Brian)
- Use << 10 instead of * 1024 (Nirjhar)
- Replace CONFIG_TRANSPARENT_HUGEPAGE check with checking 'sys/kernel/mm/transparent_hugepage/' (Darrick)
- Integrate readbdy and writebdy options
- Update options of generic/759 to include psize/bsize

Joanne Koong (2):
fsx: support reads/writes from buffers backed by hugepages
generic: add tests for read/writes from hugepages-backed buffers

 common/rc             |  13 +++++
 ltp/fsx.c             | 119 ++++++++++++++++++++++++++++++++++++++----
 tests/generic/758     |  22 ++++++++
 tests/generic/758.out |   4 ++
 tests/generic/759     |  26 +++++++++
 tests/generic/759.out |   4 ++
 6 files changed, 177 insertions(+), 11 deletions(-)
 create mode 100755 tests/generic/758
 create mode 100644 tests/generic/758.out
 create mode 100755 tests/generic/759
 create mode 100644 tests/generic/759.out
Signature · 1 line
2.47.1
#1 lore raw

PATCH v3 1/2 fsx: support reads/writes from buffers backed by hugepages

Add support for reads/writes from buffers backed by hugepages. This can be enabled through the '-h' flag. This flag should only be used on systems where THP capabilities are enabled.

This is motivated by a recent bug that was due to faulty handling of userspace buffers backed by hugepages. This patch is a mitigation against problems like this in the future.

---
 ltp/fsx.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 11 deletions(-)
ltp/fsx.c +108 -11
index 41933354..8d3a2e2c 100644
@@ -190,6 +190,7 @@ int	o_direct;			/* -Z */
 int	aio = 0;
 int	uring = 0;
 int	mark_nr = 0;
+int	hugepages = 0;                  /* -h flag */
 
 int page_size;
 int page_mask;
@@ -2471,7 +2472,7 @@ void
 usage(void)
 {
 	fprintf(stdout, "usage: %s",
-		"fsx [-dfknqxyzBEFHIJKLORWXZ0]\n\
+		"fsx [-dfhknqxyzBEFHIJKLORWXZ0]\n\
 	   [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid]\n\
 	   [-l flen] [-m start:end] [-o oplen] [-p progressinterval]\n\
 	   [-r readbdy] [-s style] [-t truncbdy] [-w writebdy]\n\
@@ -2484,6 +2485,7 @@ usage(void)
 	-e: pollute post-eof on size changes (default 0)\n\
 	-f: flush and invalidate cache after I/O\n\
 	-g X: write character X instead of random generated data\n\
+	-h hugepages: use buffers backed by hugepages for reads/writes\n\
 	-i logdev: do integrity testing, logdev is the dm log writes device\n\
 	-j logid: prefix debug log messsages with this id\n\
 	-k: do not truncate existing file and use its size as upper bound on file size\n\
@@ -2856,6 +2858,101 @@ keep_running(void)
 	return numops-- != 0;
 }
 
+static long
+get_hugepage_size(void)
+{
+	const char str[] = "Hugepagesize:";
+	size_t str_len =  sizeof(str) - 1;
+	unsigned int hugepage_size = 0;
+	char buffer[64];
+	FILE *file;
+
+	file = fopen("/proc/meminfo", "r");
+	if (!file) {
+		prterr("get_hugepage_size: fopen /proc/meminfo");
+		return -1;
+	}
+	while (fgets(buffer, sizeof(buffer), file)) {
+		if (strncmp(buffer, str, str_len) == 0) {
+			sscanf(buffer + str_len, "%u", &hugepage_size);
+			break;
+		}
+	}
+	fclose(file);
+	if (!hugepage_size) {
+		prterr("get_hugepage_size: failed to find "
+			"hugepage size in /proc/meminfo\n");
+		return -1;
+	}
+
+	/* convert from KiB to bytes */
+	return hugepage_size << 10;
+}
+
+#ifdef MADV_COLLAPSE
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+	void *buf;
+	long buf_size = roundup(len, hugepage_size) + alignment;
+
+	if (posix_memalign(&buf, hugepage_size, buf_size)) {
+		prterr("posix_memalign for buf");
+		return NULL;
+	}
+	memset(buf, '\0', buf_size);
+	if (madvise(buf, buf_size, MADV_COLLAPSE)) {
+		prterr("madvise collapse for buf");
+		free(buf);
+		return NULL;
+	}
+
+	return buf;
+}
+#else
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+	return NULL;
+}
+#endif
+
+static void
+init_buffers(void)
+{
+	int i;
+
+	original_buf = (char *) malloc(maxfilelen);
+	for (i = 0; i < maxfilelen; i++)
+		original_buf[i] = random() % 256;
+	if (hugepages) {
+		long hugepage_size = get_hugepage_size();
+		if (hugepage_size == -1) {
+			prterr("get_hugepage_size()");
+			exit(102);
+		}
+		good_buf = init_hugepages_buf(maxfilelen, hugepage_size, writebdy);
+		if (!good_buf) {
+			prterr("init_hugepages_buf failed for good_buf");
+			exit(103);
+		}
+
+		temp_buf = init_hugepages_buf(maxoplen, hugepage_size, readbdy);
+		if (!temp_buf) {
+			prterr("init_hugepages_buf failed for temp_buf");
+			exit(103);
+		}
+	} else {
+		unsigned long good_buf_len = maxfilelen + writebdy;
+		unsigned long temp_buf_len = maxoplen + readbdy;
+
+		good_buf = calloc(1, good_buf_len);
+		temp_buf = calloc(1, temp_buf_len);
+	}
+	good_buf = round_ptr_up(good_buf, writebdy, 0);
+	temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+}
+
 static struct option longopts[] = {
 	{"replay-ops", required_argument, 0, 256},
 	{"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2980,7 @@ main(int argc, char **argv)
 	setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
 	while ((ch = getopt_long(argc, argv,
-				 "0b:c:de:fg:i:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
+				 "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
 				 longopts, NULL)) != EOF)
 		switch (ch) {
 		case 'b':
@@ -2916,6 +3013,14 @@ main(int argc, char **argv)
 		case 'g':
 			filldata = *optarg;
 			break;
+		case 'h':
+			#ifndef MADV_COLLAPSE
+				fprintf(stderr, "MADV_COLLAPSE not supported. "
+					"Can't support -h\n");
+				exit(86);
+			#endif
+			hugepages = 1;
+			break;
 		case 'i':
 			integrity = 1;
 			logdev = strdup(optarg);
@@ -3229,15 +3334,7 @@ main(int argc, char **argv)
 			exit(95);
 		}
 	}
-	original_buf = (char *) malloc(maxfilelen);
-	for (i = 0; i < maxfilelen; i++)
-		original_buf[i] = random() % 256;
-	good_buf = (char *) malloc(maxfilelen + writebdy);
-	good_buf = round_ptr_up(good_buf, writebdy, 0);
-	memset(good_buf, '\0', maxfilelen);
-	temp_buf = (char *) malloc(maxoplen + readbdy);
-	temp_buf = round_ptr_up(temp_buf, readbdy, 0);
-	memset(temp_buf, '\0', maxoplen);
+	init_buffers();
 	if (lite) {	/* zero entire existing file */
 		ssize_t written;
 
Signature · 1 line
2.47.1
#2 lore raw

Re: [PATCH v3 1/2] fsx: support reads/writes from buffers backed by hugepages

Quoted 36 lines · Joanne Koong

Add support for reads/writes from buffers backed by hugepages. This can be enabled through the '-h' flag. This flag should only be used on systems where THP capabilities are enabled.

This is motivated by a recent bug that was due to faulty handling of userspace buffers backed by hugepages. This patch is a mitigation against problems like this in the future.

---
 ltp/fsx.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 11 deletions(-)
ltp/fsx.c +2 -1
index 41933354..8d3a2e2c 100644
@@ -190,6 +190,7 @@ int	o_direct;			/* -Z */
 int	aio = 0;
 int	uring = 0;
 int	mark_nr = 0;
+int	hugepages = 0;                  /* -h flag */
 
 int page_size;
 int page_mask;
@@ -2471,7 +2472,7 @@ void
 usage(void)
 {
 	fprintf(stdout, "usage: %s",
-		"fsx [-dfknqxyzBEFHIJKLORWXZ0]\n\
+		"fsx [-dfhknqxyzBEFHIJKLORWXZ0]\n\
 	   [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid]\n\
 	   [-l flen] [-m start:end] [-o oplen] [-p progressinterval]\n\
 	   [-r readbdy] [-s style] [-t truncbdy] [-w writebdy]\n\

ltp/fsx.c usage(void) · line 2488

Quoted 4 lines
ltp/fsx.c +1 -0
@@ -2484,6 +2485,7 @@ usage(void)
 	-e: pollute post-eof on size changes (default 0)\n\
 	-f: flush and invalidate cache after I/O\n\
 	-g X: write character X instead of random generated data\n\
+	-h hugepages: use buffers backed by hugepages for reads/writes\n\

If this requires MADV_COLLAPSE, then perhaps the help text shouldn't describe the switch if the support wasn't compiled in?

e.g.

-g X: write character X instead of random generated data\n"
#ifdef MADV_COLLAPSE
" -h hugepages: use buffers backed by hugepages for reads/writes\n"
#endif
" -i logdev: do integrity testing, logdev is the dm log writes device\n\

(assuming I got the preprocessor and string construction goo right; I might be a few cards short of a deck due to zombie attack earlier)

Quoted 41 lines
ltp/fsx.c +34 -0
 	-i logdev: do integrity testing, logdev is the dm log writes device\n\
 	-j logid: prefix debug log messsages with this id\n\
 	-k: do not truncate existing file and use its size as upper bound on file size\n\
@@ -2856,6 +2858,101 @@ keep_running(void)
 	return numops-- != 0;
 }
 
+static long
+get_hugepage_size(void)
+{
+	const char str[] = "Hugepagesize:";
+	size_t str_len =  sizeof(str) - 1;
+	unsigned int hugepage_size = 0;
+	char buffer[64];
+	FILE *file;
+
+	file = fopen("/proc/meminfo", "r");
+	if (!file) {
+		prterr("get_hugepage_size: fopen /proc/meminfo");
+		return -1;
+	}
+	while (fgets(buffer, sizeof(buffer), file)) {
+		if (strncmp(buffer, str, str_len) == 0) {
+			sscanf(buffer + str_len, "%u", &hugepage_size);
+			break;
+		}
+	}
+	fclose(file);
+	if (!hugepage_size) {
+		prterr("get_hugepage_size: failed to find "
+			"hugepage size in /proc/meminfo\n");
+		return -1;
+	}
+
+	/* convert from KiB to bytes */
+	return hugepage_size << 10;
+}
+
+#ifdef MADV_COLLAPSE
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)

ltp/fsx.c keep_running(void) · line 2904

Quoted 10 lines
ltp/fsx.c +10 -0
+{
+	void *buf;
+	long buf_size = roundup(len, hugepage_size) + alignment;
+
+	if (posix_memalign(&buf, hugepage_size, buf_size)) {
+		prterr("posix_memalign for buf");
+		return NULL;
+	}
+	memset(buf, '\0', buf_size);
+	if (madvise(buf, buf_size, MADV_COLLAPSE)) {

If the fsx runs for a long period of time, will it be necessary to call MADV_COLLAPSE periodically to ensure that reclaim doesn't break up the hugepage?

Quoted 64 lines
ltp/fsx.c +52 -1
+		prterr("madvise collapse for buf");
+		free(buf);
+		return NULL;
+	}
+
+	return buf;
+}
+#else
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+	return NULL;
+}
+#endif
+
+static void
+init_buffers(void)
+{
+	int i;
+
+	original_buf = (char *) malloc(maxfilelen);
+	for (i = 0; i < maxfilelen; i++)
+		original_buf[i] = random() % 256;
+	if (hugepages) {
+		long hugepage_size = get_hugepage_size();
+		if (hugepage_size == -1) {
+			prterr("get_hugepage_size()");
+			exit(102);
+		}
+		good_buf = init_hugepages_buf(maxfilelen, hugepage_size, writebdy);
+		if (!good_buf) {
+			prterr("init_hugepages_buf failed for good_buf");
+			exit(103);
+		}
+
+		temp_buf = init_hugepages_buf(maxoplen, hugepage_size, readbdy);
+		if (!temp_buf) {
+			prterr("init_hugepages_buf failed for temp_buf");
+			exit(103);
+		}
+	} else {
+		unsigned long good_buf_len = maxfilelen + writebdy;
+		unsigned long temp_buf_len = maxoplen + readbdy;
+
+		good_buf = calloc(1, good_buf_len);
+		temp_buf = calloc(1, temp_buf_len);
+	}
+	good_buf = round_ptr_up(good_buf, writebdy, 0);
+	temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+}
+
 static struct option longopts[] = {
 	{"replay-ops", required_argument, 0, 256},
 	{"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2980,7 @@ main(int argc, char **argv)
 	setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
 	while ((ch = getopt_long(argc, argv,
-				 "0b:c:de:fg:i:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
+				 "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
 				 longopts, NULL)) != EOF)
 		switch (ch) {
 		case 'b':

ltp/fsx.c main(int argc, char **argv) · line 3017

Quoted 5 lines
ltp/fsx.c +2 -0
@@ -2916,6 +3013,14 @@ main(int argc, char **argv)
 		case 'g':
 			filldata = *optarg;
 			break;
+		case 'h':
+			#ifndef MADV_COLLAPSE

Preprocessor directives should start at column 0, like most of the rest of fstests.

--D

Quoted 30 lines
ltp/fsx.c +7 -9
+				fprintf(stderr, "MADV_COLLAPSE not supported. "
+					"Can't support -h\n");
+				exit(86);
+			#endif
+			hugepages = 1;
+			break;
 		case 'i':
 			integrity = 1;
 			logdev = strdup(optarg);
@@ -3229,15 +3334,7 @@ main(int argc, char **argv)
 			exit(95);
 		}
 	}
-	original_buf = (char *) malloc(maxfilelen);
-	for (i = 0; i < maxfilelen; i++)
-		original_buf[i] = random() % 256;
-	good_buf = (char *) malloc(maxfilelen + writebdy);
-	good_buf = round_ptr_up(good_buf, writebdy, 0);
-	memset(good_buf, '\0', maxfilelen);
-	temp_buf = (char *) malloc(maxoplen + readbdy);
-	temp_buf = round_ptr_up(temp_buf, readbdy, 0);
-	memset(temp_buf, '\0', maxoplen);
+	init_buffers();
 	if (lite) {	/* zero entire existing file */
 		ssize_t written;
 
Signature · 1 line
2.47.1
#3 lore raw

Re: [PATCH v3 1/2] fsx: support reads/writes from buffers backed by hugepages

Quoted 48 lines · Darrick J. Wong
Quoted 36 lines · Joanne Koong

Add support for reads/writes from buffers backed by hugepages. This can be enabled through the '-h' flag. This flag should only be used on systems where THP capabilities are enabled.

This is motivated by a recent bug that was due to faulty handling of userspace buffers backed by hugepages. This patch is a mitigation against problems like this in the future.

---
 ltp/fsx.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 11 deletions(-)
ltp/fsx.c +2 -1
index 41933354..8d3a2e2c 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
 int  uring = 0;
 int  mark_nr = 0;
+int  hugepages = 0;                  /* -h flag */
 
 int page_size;
 int page_mask;
@@ -2471,7 +2472,7 @@ void
 usage(void)
 {
      fprintf(stdout, "usage: %s",
-             "fsx [-dfknqxyzBEFHIJKLORWXZ0]\n\
+             "fsx [-dfhknqxyzBEFHIJKLORWXZ0]\n\
         [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid]\n\
         [-l flen] [-m start:end] [-o oplen] [-p progressinterval]\n\
         [-r readbdy] [-s style] [-t truncbdy] [-w writebdy]\n\

ltp/fsx.c usage(void) · line 2488

Quoted 4 lines
ltp/fsx.c +1 -0
@@ -2484,6 +2485,7 @@ usage(void)
      -e: pollute post-eof on size changes (default 0)\n\
      -f: flush and invalidate cache after I/O\n\
      -g X: write character X instead of random generated data\n\
+     -h hugepages: use buffers backed by hugepages for reads/writes\n\

If this requires MADV_COLLAPSE, then perhaps the help text shouldn't describe the switch if the support wasn't compiled in?

e.g.

Quoted 8 lines

-g X: write character X instead of random generated data\n"
#ifdef MADV_COLLAPSE
" -h hugepages: use buffers backed by hugepages for reads/writes\n"
#endif
" -i logdev: do integrity testing, logdev is the dm log writes device\n\

(assuming I got the preprocessor and string construction goo right; I might be a few cards short of a deck due to zombie attack earlier)

Sounds great, I'll #ifdef out the help text -h line. Hope you feel better.

Quoted 4 lines
Quoted 4 lines
ltp/fsx.c +0 -0
      -i logdev: do integrity testing, logdev is the dm log writes device\n\
      -j logid: prefix debug log messsages with this id\n\
      -k: do not truncate existing file and use its size as upper bound on file size\n\

[...]

Quoted 11 lines
Quoted 11 lines
ltp/fsx.c +11 -0
+}
+
+#ifdef MADV_COLLAPSE
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size) + alignment;
+
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
Quoted 9 lines

ltp/fsx.c keep_running(void) · line 2904

Quoted 4 lines
ltp/fsx.c +4 -0
+             return NULL;
+     }
+     memset(buf, '\0', buf_size);
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {

If the fsx runs for a long period of time, will it be necessary to call MADV_COLLAPSE periodically to ensure that reclaim doesn't break up the hugepage?

imo, I don't think so. My understanding is that this would be a rare edge case that happens when the system is constrained on memory, in which case subsequent calls to MADV_COLLAPSE would most likely fail anyways.

Thanks,
Joanne

Quoted 75 lines
Quoted 64 lines
ltp/fsx.c +52 -1
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
+#else
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     return NULL;
+}
+#endif
+
+static void
+init_buffers(void)
+{
+     int i;
+
+     original_buf = (char *) malloc(maxfilelen);
+     for (i = 0; i < maxfilelen; i++)
+             original_buf[i] = random() % 256;
+     if (hugepages) {
+             long hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(102);
+             }
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size, writebdy);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(103);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size, readbdy);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(103);
+             }
+     } else {
+             unsigned long good_buf_len = maxfilelen + writebdy;
+             unsigned long temp_buf_len = maxoplen + readbdy;
+
+             good_buf = calloc(1, good_buf_len);
+             temp_buf = calloc(1, temp_buf_len);
+     }
+     good_buf = round_ptr_up(good_buf, writebdy, 0);
+     temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+}
+
 static struct option longopts[] = {
      {"replay-ops", required_argument, 0, 256},
      {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2980,7 @@ main(int argc, char **argv)
      setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
      while ((ch = getopt_long(argc, argv,
-                              "0b:c:de:fg:i:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
+                              "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
                               longopts, NULL)) != EOF)
              switch (ch) {
              case 'b':

ltp/fsx.c main(int argc, char **argv) · line 3017

Quoted 5 lines
ltp/fsx.c +2 -0
@@ -2916,6 +3013,14 @@ main(int argc, char **argv)
              case 'g':
                      filldata = *optarg;
                      break;
+             case 'h':
+                     #ifndef MADV_COLLAPSE

Preprocessor directives should start at column 0, like most of the rest of fstests.

--D

#4 lore raw

Re: [PATCH v3 1/2] fsx: support reads/writes from buffers backed by hugepages

Quoted 85 lines · Joanne Koong
Quoted 48 lines · Darrick J. Wong
Quoted 36 lines · Joanne Koong

Add support for reads/writes from buffers backed by hugepages. This can be enabled through the '-h' flag. This flag should only be used on systems where THP capabilities are enabled.

This is motivated by a recent bug that was due to faulty handling of userspace buffers backed by hugepages. This patch is a mitigation against problems like this in the future.

---
 ltp/fsx.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 11 deletions(-)
ltp/fsx.c +2 -1
index 41933354..8d3a2e2c 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
 int  uring = 0;
 int  mark_nr = 0;
+int  hugepages = 0;                  /* -h flag */
 
 int page_size;
 int page_mask;
@@ -2471,7 +2472,7 @@ void
 usage(void)
 {
      fprintf(stdout, "usage: %s",
-             "fsx [-dfknqxyzBEFHIJKLORWXZ0]\n\
+             "fsx [-dfhknqxyzBEFHIJKLORWXZ0]\n\
         [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid]\n\
         [-l flen] [-m start:end] [-o oplen] [-p progressinterval]\n\
         [-r readbdy] [-s style] [-t truncbdy] [-w writebdy]\n\

ltp/fsx.c usage(void) · line 2488

Quoted 4 lines
ltp/fsx.c +1 -0
@@ -2484,6 +2485,7 @@ usage(void)
      -e: pollute post-eof on size changes (default 0)\n\
      -f: flush and invalidate cache after I/O\n\
      -g X: write character X instead of random generated data\n\
+     -h hugepages: use buffers backed by hugepages for reads/writes\n\

If this requires MADV_COLLAPSE, then perhaps the help text shouldn't describe the switch if the support wasn't compiled in?

e.g.

Quoted 8 lines

-g X: write character X instead of random generated data\n"
#ifdef MADV_COLLAPSE
" -h hugepages: use buffers backed by hugepages for reads/writes\n"
#endif
" -i logdev: do integrity testing, logdev is the dm log writes device\n\

(assuming I got the preprocessor and string construction goo right; I might be a few cards short of a deck due to zombie attack earlier)

Sounds great, I'll #ifdef out the help text -h line. Hope you feel better.

Quoted 4 lines
Quoted 4 lines
ltp/fsx.c +0 -0
      -i logdev: do integrity testing, logdev is the dm log writes device\n\
      -j logid: prefix debug log messsages with this id\n\
      -k: do not truncate existing file and use its size as upper bound on file size\n\

[...]

Quoted 11 lines
Quoted 11 lines
ltp/fsx.c +11 -0
+}
+
+#ifdef MADV_COLLAPSE
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size) + alignment;
+
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
Quoted 9 lines

ltp/fsx.c keep_running(void) · line 2904

Quoted 4 lines
ltp/fsx.c +4 -0
+             return NULL;
+     }
+     memset(buf, '\0', buf_size);
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {

If the fsx runs for a long period of time, will it be necessary to call MADV_COLLAPSE periodically to ensure that reclaim doesn't break up the hugepage?

Quoted 4 lines

imo, I don't think so. My understanding is that this would be a rare edge case that happens when the system is constrained on memory, in which case subsequent calls to MADV_COLLAPSE would most likely fail anyways.

Hrmmm... well I /do/ like to run memory constrained VMs to prod reclaim into stressing the filesystem more. But I guess there's no good way for fsx to know that something happened to it. Unless there's some even goofier way to force a hugepage, like shmem/hugetlbfs (ugh!) :)

Will have to ponder hugepage renewasl -- maybe we should madvise every few thousand fsxops just to be careful?

--D

Quoted 79 lines

Thanks,
Joanne

Quoted 75 lines
Quoted 64 lines
ltp/fsx.c +52 -1
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
+#else
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     return NULL;
+}
+#endif
+
+static void
+init_buffers(void)
+{
+     int i;
+
+     original_buf = (char *) malloc(maxfilelen);
+     for (i = 0; i < maxfilelen; i++)
+             original_buf[i] = random() % 256;
+     if (hugepages) {
+             long hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(102);
+             }
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size, writebdy);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(103);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size, readbdy);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(103);
+             }
+     } else {
+             unsigned long good_buf_len = maxfilelen + writebdy;
+             unsigned long temp_buf_len = maxoplen + readbdy;
+
+             good_buf = calloc(1, good_buf_len);
+             temp_buf = calloc(1, temp_buf_len);
+     }
+     good_buf = round_ptr_up(good_buf, writebdy, 0);
+     temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+}
+
 static struct option longopts[] = {
      {"replay-ops", required_argument, 0, 256},
      {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2980,7 @@ main(int argc, char **argv)
      setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
      while ((ch = getopt_long(argc, argv,
-                              "0b:c:de:fg:i:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
+                              "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
                               longopts, NULL)) != EOF)
              switch (ch) {
              case 'b':

ltp/fsx.c main(int argc, char **argv) · line 3017

Quoted 5 lines
ltp/fsx.c +2 -0
@@ -2916,6 +3013,14 @@ main(int argc, char **argv)
              case 'g':
                      filldata = *optarg;
                      break;
+             case 'h':
+                     #ifndef MADV_COLLAPSE

Preprocessor directives should start at column 0, like most of the rest of fstests.

--D

#5 lore raw

Re: [PATCH v3 1/2] fsx: support reads/writes from buffers backed by hugepages

Quoted 91 lines · Darrick J. Wong
Quoted 85 lines · Joanne Koong
Quoted 48 lines · Darrick J. Wong
Quoted 36 lines · Joanne Koong

Add support for reads/writes from buffers backed by hugepages. This can be enabled through the '-h' flag. This flag should only be used on systems where THP capabilities are enabled.

This is motivated by a recent bug that was due to faulty handling of userspace buffers backed by hugepages. This patch is a mitigation against problems like this in the future.

---
 ltp/fsx.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 11 deletions(-)
ltp/fsx.c +2 -1
index 41933354..8d3a2e2c 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
 int  uring = 0;
 int  mark_nr = 0;
+int  hugepages = 0;                  /* -h flag */
 
 int page_size;
 int page_mask;
@@ -2471,7 +2472,7 @@ void
 usage(void)
 {
      fprintf(stdout, "usage: %s",
-             "fsx [-dfknqxyzBEFHIJKLORWXZ0]\n\
+             "fsx [-dfhknqxyzBEFHIJKLORWXZ0]\n\
         [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid]\n\
         [-l flen] [-m start:end] [-o oplen] [-p progressinterval]\n\
         [-r readbdy] [-s style] [-t truncbdy] [-w writebdy]\n\

ltp/fsx.c usage(void) · line 2488

Quoted 4 lines
ltp/fsx.c +1 -0
@@ -2484,6 +2485,7 @@ usage(void)
      -e: pollute post-eof on size changes (default 0)\n\
      -f: flush and invalidate cache after I/O\n\
      -g X: write character X instead of random generated data\n\
+     -h hugepages: use buffers backed by hugepages for reads/writes\n\

If this requires MADV_COLLAPSE, then perhaps the help text shouldn't describe the switch if the support wasn't compiled in?

e.g.

Quoted 8 lines

-g X: write character X instead of random generated data\n"
#ifdef MADV_COLLAPSE
" -h hugepages: use buffers backed by hugepages for reads/writes\n"
#endif
" -i logdev: do integrity testing, logdev is the dm log writes device\n\

(assuming I got the preprocessor and string construction goo right; I might be a few cards short of a deck due to zombie attack earlier)

Sounds great, I'll #ifdef out the help text -h line. Hope you feel better.

Quoted 4 lines
Quoted 4 lines
ltp/fsx.c +0 -0
      -i logdev: do integrity testing, logdev is the dm log writes device\n\
      -j logid: prefix debug log messsages with this id\n\
      -k: do not truncate existing file and use its size as upper bound on file size\n\

[...]

Quoted 11 lines
Quoted 11 lines
ltp/fsx.c +11 -0
+}
+
+#ifdef MADV_COLLAPSE
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size) + alignment;
+
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
Quoted 9 lines

ltp/fsx.c keep_running(void) · line 2904

Quoted 4 lines
ltp/fsx.c +4 -0
+             return NULL;
+     }
+     memset(buf, '\0', buf_size);
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {

If the fsx runs for a long period of time, will it be necessary to call MADV_COLLAPSE periodically to ensure that reclaim doesn't break up the hugepage?

Quoted 4 lines

imo, I don't think so. My understanding is that this would be a rare edge case that happens when the system is constrained on memory, in which case subsequent calls to MADV_COLLAPSE would most likely fail anyways.

Quoted 8 lines

Hrmmm... well I /do/ like to run memory constrained VMs to prod reclaim into stressing the filesystem more. But I guess there's no good way for fsx to know that something happened to it. Unless there's some even goofier way to force a hugepage, like shmem/hugetlbfs (ugh!) :)

Will have to ponder hugepage renewasl -- maybe we should madvise every few thousand fsxops just to be careful?

I wonder.. is there test value in doing collapses to the target file as well, either as a standalone map/madvise command or a random thing hitched onto preexisting commands? If so, I could see how something like that could potentially lift the current init time only approach into something that occurs with frequency, which then could at the same time (again maybe randomly) reinvoke for internal buffers as well.

All that said, this is new functionality and IIUC provides functional test coverage for a valid issue. IMO, it would be nice to get this merged as a baseline feature and explore these sort of enhancements as followon work. Just my .02.

Brian

Quoted 82 lines

--D

Quoted 79 lines

Thanks,
Joanne

Quoted 75 lines
Quoted 64 lines
ltp/fsx.c +52 -1
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
+#else
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     return NULL;
+}
+#endif
+
+static void
+init_buffers(void)
+{
+     int i;
+
+     original_buf = (char *) malloc(maxfilelen);
+     for (i = 0; i < maxfilelen; i++)
+             original_buf[i] = random() % 256;
+     if (hugepages) {
+             long hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(102);
+             }
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size, writebdy);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(103);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size, readbdy);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(103);
+             }
+     } else {
+             unsigned long good_buf_len = maxfilelen + writebdy;
+             unsigned long temp_buf_len = maxoplen + readbdy;
+
+             good_buf = calloc(1, good_buf_len);
+             temp_buf = calloc(1, temp_buf_len);
+     }
+     good_buf = round_ptr_up(good_buf, writebdy, 0);
+     temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+}
+
 static struct option longopts[] = {
      {"replay-ops", required_argument, 0, 256},
      {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2980,7 @@ main(int argc, char **argv)
      setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
      while ((ch = getopt_long(argc, argv,
-                              "0b:c:de:fg:i:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
+                              "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
                               longopts, NULL)) != EOF)
              switch (ch) {
              case 'b':

ltp/fsx.c main(int argc, char **argv) · line 3017

Quoted 5 lines
ltp/fsx.c +2 -0
@@ -2916,6 +3013,14 @@ main(int argc, char **argv)
              case 'g':
                      filldata = *optarg;
                      break;
+             case 'h':
+                     #ifndef MADV_COLLAPSE

Preprocessor directives should start at column 0, like most of the rest of fstests.

--D

#6 lore raw

Re: [PATCH v3 1/2] fsx: support reads/writes from buffers backed by hugepages

Quoted 102 lines · Brian Foster
Quoted 91 lines · Darrick J. Wong
Quoted 85 lines · Joanne Koong
Quoted 48 lines · Darrick J. Wong
Quoted 36 lines · Joanne Koong

Add support for reads/writes from buffers backed by hugepages. This can be enabled through the '-h' flag. This flag should only be used on systems where THP capabilities are enabled.

This is motivated by a recent bug that was due to faulty handling of userspace buffers backed by hugepages. This patch is a mitigation against problems like this in the future.

---
 ltp/fsx.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 11 deletions(-)
ltp/fsx.c +2 -1
index 41933354..8d3a2e2c 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
 int  uring = 0;
 int  mark_nr = 0;
+int  hugepages = 0;                  /* -h flag */
 
 int page_size;
 int page_mask;
@@ -2471,7 +2472,7 @@ void
 usage(void)
 {
      fprintf(stdout, "usage: %s",
-             "fsx [-dfknqxyzBEFHIJKLORWXZ0]\n\
+             "fsx [-dfhknqxyzBEFHIJKLORWXZ0]\n\
         [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid]\n\
         [-l flen] [-m start:end] [-o oplen] [-p progressinterval]\n\
         [-r readbdy] [-s style] [-t truncbdy] [-w writebdy]\n\

ltp/fsx.c usage(void) · line 2488

Quoted 4 lines
ltp/fsx.c +1 -0
@@ -2484,6 +2485,7 @@ usage(void)
      -e: pollute post-eof on size changes (default 0)\n\
      -f: flush and invalidate cache after I/O\n\
      -g X: write character X instead of random generated data\n\
+     -h hugepages: use buffers backed by hugepages for reads/writes\n\

If this requires MADV_COLLAPSE, then perhaps the help text shouldn't describe the switch if the support wasn't compiled in?

e.g.

Quoted 8 lines

-g X: write character X instead of random generated data\n"
#ifdef MADV_COLLAPSE
" -h hugepages: use buffers backed by hugepages for reads/writes\n"
#endif
" -i logdev: do integrity testing, logdev is the dm log writes device\n\

(assuming I got the preprocessor and string construction goo right; I might be a few cards short of a deck due to zombie attack earlier)

Sounds great, I'll #ifdef out the help text -h line. Hope you feel better.

Quoted 4 lines
Quoted 4 lines
ltp/fsx.c +0 -0
      -i logdev: do integrity testing, logdev is the dm log writes device\n\
      -j logid: prefix debug log messsages with this id\n\
      -k: do not truncate existing file and use its size as upper bound on file size\n\

[...]

Quoted 11 lines
Quoted 11 lines
ltp/fsx.c +11 -0
+}
+
+#ifdef MADV_COLLAPSE
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size) + alignment;
+
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
Quoted 9 lines

ltp/fsx.c keep_running(void) · line 2904

Quoted 4 lines
ltp/fsx.c +4 -0
+             return NULL;
+     }
+     memset(buf, '\0', buf_size);
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {

If the fsx runs for a long period of time, will it be necessary to call MADV_COLLAPSE periodically to ensure that reclaim doesn't break up the hugepage?

Quoted 4 lines

imo, I don't think so. My understanding is that this would be a rare edge case that happens when the system is constrained on memory, in which case subsequent calls to MADV_COLLAPSE would most likely fail anyways.

Quoted 8 lines

Hrmmm... well I /do/ like to run memory constrained VMs to prod reclaim into stressing the filesystem more. But I guess there's no good way for fsx to know that something happened to it. Unless there's some even goofier way to force a hugepage, like shmem/hugetlbfs (ugh!) :)

Will have to ponder hugepage renewasl -- maybe we should madvise every few thousand fsxops just to be careful?

Quoted 6 lines

I wonder.. is there test value in doing collapses to the target file as well, either as a standalone map/madvise command or a random thing hitched onto preexisting commands? If so, I could see how something like that could potentially lift the current init time only approach into something that occurs with frequency, which then could at the same time (again maybe randomly) reinvoke for internal buffers as well.

My understanding is that if a filesystem has support enabled for large folios, then doing large writes/reads (which I believe is currently supported in fsx via the -o flag) will already automatically test the functionality of how the filesystem handles hugepages. I don't think this would be different from what doing a collapse on the target file would do.

Thanks,
Joanne

Quoted 91 lines

All that said, this is new functionality and IIUC provides functional test coverage for a valid issue. IMO, it would be nice to get this merged as a baseline feature and explore these sort of enhancements as followon work. Just my .02.

Brian

Quoted 82 lines

--D

Quoted 79 lines

Thanks,
Joanne

Quoted 75 lines
Quoted 64 lines
ltp/fsx.c +52 -1
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
+#else
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     return NULL;
+}
+#endif
+
+static void
+init_buffers(void)
+{
+     int i;
+
+     original_buf = (char *) malloc(maxfilelen);
+     for (i = 0; i < maxfilelen; i++)
+             original_buf[i] = random() % 256;
+     if (hugepages) {
+             long hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(102);
+             }
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size, writebdy);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(103);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size, readbdy);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(103);
+             }
+     } else {
+             unsigned long good_buf_len = maxfilelen + writebdy;
+             unsigned long temp_buf_len = maxoplen + readbdy;
+
+             good_buf = calloc(1, good_buf_len);
+             temp_buf = calloc(1, temp_buf_len);
+     }
+     good_buf = round_ptr_up(good_buf, writebdy, 0);
+     temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+}
+
 static struct option longopts[] = {
      {"replay-ops", required_argument, 0, 256},
      {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2980,7 @@ main(int argc, char **argv)
      setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
      while ((ch = getopt_long(argc, argv,
-                              "0b:c:de:fg:i:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
+                              "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
                               longopts, NULL)) != EOF)
              switch (ch) {
              case 'b':

ltp/fsx.c main(int argc, char **argv) · line 3017

Quoted 5 lines
ltp/fsx.c +2 -0
@@ -2916,6 +3013,14 @@ main(int argc, char **argv)
              case 'g':
                      filldata = *optarg;
                      break;
+             case 'h':
+                     #ifndef MADV_COLLAPSE

Preprocessor directives should start at column 0, like most of the rest of fstests.

--D

#7 lore raw

Re: [PATCH v3 1/2] fsx: support reads/writes from buffers backed by hugepages

Quoted 110 lines · Joanne Koong
Quoted 102 lines · Brian Foster
Quoted 91 lines · Darrick J. Wong
Quoted 85 lines · Joanne Koong
Quoted 48 lines · Darrick J. Wong
Quoted 36 lines · Joanne Koong

Add support for reads/writes from buffers backed by hugepages. This can be enabled through the '-h' flag. This flag should only be used on systems where THP capabilities are enabled.

This is motivated by a recent bug that was due to faulty handling of userspace buffers backed by hugepages. This patch is a mitigation against problems like this in the future.

---
 ltp/fsx.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 11 deletions(-)
ltp/fsx.c +2 -1
index 41933354..8d3a2e2c 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
 int  uring = 0;
 int  mark_nr = 0;
+int  hugepages = 0;                  /* -h flag */
 
 int page_size;
 int page_mask;
@@ -2471,7 +2472,7 @@ void
 usage(void)
 {
      fprintf(stdout, "usage: %s",
-             "fsx [-dfknqxyzBEFHIJKLORWXZ0]\n\
+             "fsx [-dfhknqxyzBEFHIJKLORWXZ0]\n\
         [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid]\n\
         [-l flen] [-m start:end] [-o oplen] [-p progressinterval]\n\
         [-r readbdy] [-s style] [-t truncbdy] [-w writebdy]\n\

ltp/fsx.c usage(void) · line 2488

Quoted 4 lines
ltp/fsx.c +1 -0
@@ -2484,6 +2485,7 @@ usage(void)
      -e: pollute post-eof on size changes (default 0)\n\
      -f: flush and invalidate cache after I/O\n\
      -g X: write character X instead of random generated data\n\
+     -h hugepages: use buffers backed by hugepages for reads/writes\n\

If this requires MADV_COLLAPSE, then perhaps the help text shouldn't describe the switch if the support wasn't compiled in?

e.g.

Quoted 8 lines

-g X: write character X instead of random generated data\n"
#ifdef MADV_COLLAPSE
" -h hugepages: use buffers backed by hugepages for reads/writes\n"
#endif
" -i logdev: do integrity testing, logdev is the dm log writes device\n\

(assuming I got the preprocessor and string construction goo right; I might be a few cards short of a deck due to zombie attack earlier)

Sounds great, I'll #ifdef out the help text -h line. Hope you feel better.

Quoted 4 lines
Quoted 4 lines
ltp/fsx.c +0 -0
      -i logdev: do integrity testing, logdev is the dm log writes device\n\
      -j logid: prefix debug log messsages with this id\n\
      -k: do not truncate existing file and use its size as upper bound on file size\n\

[...]

Quoted 11 lines
Quoted 11 lines
ltp/fsx.c +11 -0
+}
+
+#ifdef MADV_COLLAPSE
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size) + alignment;
+
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
Quoted 9 lines

ltp/fsx.c keep_running(void) · line 2904

Quoted 4 lines
ltp/fsx.c +4 -0
+             return NULL;
+     }
+     memset(buf, '\0', buf_size);
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {

If the fsx runs for a long period of time, will it be necessary to call MADV_COLLAPSE periodically to ensure that reclaim doesn't break up the hugepage?

Quoted 4 lines

imo, I don't think so. My understanding is that this would be a rare edge case that happens when the system is constrained on memory, in which case subsequent calls to MADV_COLLAPSE would most likely fail anyways.

Quoted 8 lines

Hrmmm... well I /do/ like to run memory constrained VMs to prod reclaim into stressing the filesystem more. But I guess there's no good way for fsx to know that something happened to it. Unless there's some even goofier way to force a hugepage, like shmem/hugetlbfs (ugh!) :)

Will have to ponder hugepage renewasl -- maybe we should madvise every few thousand fsxops just to be careful?

Quoted 6 lines

I wonder.. is there test value in doing collapses to the target file as well, either as a standalone map/madvise command or a random thing hitched onto preexisting commands? If so, I could see how something like that could potentially lift the current init time only approach into something that occurs with frequency, which then could at the same time (again maybe randomly) reinvoke for internal buffers as well.

Quoted 7 lines

My understanding is that if a filesystem has support enabled for large folios, then doing large writes/reads (which I believe is currently supported in fsx via the -o flag) will already automatically test the functionality of how the filesystem handles hugepages. I don't think this would be different from what doing a collapse on the target file would do.

Ah, that is a good point. So maybe not that useful to have something that would hook into writes. OTOH, fsx does a lot of random ops in the general case. I wonder how likely it is to sustain large folios in a typical long running test and whether explicit madvise calls thrown into the mix would make any difference at all.

I suppose there may also be an argument that doing collapses provides more test coverage than purely doing larger folio allocations at write time..? I don't know the code well enough to say whether there is any value there. FWIW, what I think is more interesting from the fsx side is the oddball sequences of operations that it can create to uncover similarly odd problems. IOW, in theory if we had a randomish "collapse target range before next operation," would that effectively provide more coverage with how the various supported ops interact with large folios over current behavior?

But anyways, this is all nebulous and strikes me more as maybe something interesting to play with as a potential future enhancement more than anything. BTW, is there any good way to measure use of large folios in general and/or on a particular file? I.e., collapse/split stats or some such thing..? Thanks.

Brian

Quoted 96 lines

Thanks,
Joanne

Quoted 91 lines

All that said, this is new functionality and IIUC provides functional test coverage for a valid issue. IMO, it would be nice to get this merged as a baseline feature and explore these sort of enhancements as followon work. Just my .02.

Brian

Quoted 82 lines

--D

Quoted 79 lines

Thanks,
Joanne

Quoted 75 lines
Quoted 64 lines
ltp/fsx.c +52 -1
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
+#else
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     return NULL;
+}
+#endif
+
+static void
+init_buffers(void)
+{
+     int i;
+
+     original_buf = (char *) malloc(maxfilelen);
+     for (i = 0; i < maxfilelen; i++)
+             original_buf[i] = random() % 256;
+     if (hugepages) {
+             long hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(102);
+             }
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size, writebdy);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(103);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size, readbdy);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(103);
+             }
+     } else {
+             unsigned long good_buf_len = maxfilelen + writebdy;
+             unsigned long temp_buf_len = maxoplen + readbdy;
+
+             good_buf = calloc(1, good_buf_len);
+             temp_buf = calloc(1, temp_buf_len);
+     }
+     good_buf = round_ptr_up(good_buf, writebdy, 0);
+     temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+}
+
 static struct option longopts[] = {
      {"replay-ops", required_argument, 0, 256},
      {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2980,7 @@ main(int argc, char **argv)
      setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
      while ((ch = getopt_long(argc, argv,
-                              "0b:c:de:fg:i:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
+                              "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
                               longopts, NULL)) != EOF)
              switch (ch) {
              case 'b':

ltp/fsx.c main(int argc, char **argv) · line 3017

Quoted 5 lines
ltp/fsx.c +2 -0
@@ -2916,6 +3013,14 @@ main(int argc, char **argv)
              case 'g':
                      filldata = *optarg;
                      break;
+             case 'h':
+                     #ifndef MADV_COLLAPSE

Preprocessor directives should start at column 0, like most of the rest of fstests.

--D

#8 lore raw

Re: [PATCH v3 1/2] fsx: support reads/writes from buffers backed by hugepages

Quoted 135 lines · Brian Foster
Quoted 110 lines · Joanne Koong
Quoted 102 lines · Brian Foster
Quoted 91 lines · Darrick J. Wong
Quoted 85 lines · Joanne Koong
Quoted 48 lines · Darrick J. Wong
Quoted 36 lines · Joanne Koong

Add support for reads/writes from buffers backed by hugepages. This can be enabled through the '-h' flag. This flag should only be used on systems where THP capabilities are enabled.

This is motivated by a recent bug that was due to faulty handling of userspace buffers backed by hugepages. This patch is a mitigation against problems like this in the future.

---
 ltp/fsx.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 11 deletions(-)
ltp/fsx.c +2 -1
index 41933354..8d3a2e2c 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
 int  uring = 0;
 int  mark_nr = 0;
+int  hugepages = 0;                  /* -h flag */
 
 int page_size;
 int page_mask;
@@ -2471,7 +2472,7 @@ void
 usage(void)
 {
      fprintf(stdout, "usage: %s",
-             "fsx [-dfknqxyzBEFHIJKLORWXZ0]\n\
+             "fsx [-dfhknqxyzBEFHIJKLORWXZ0]\n\
         [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid]\n\
         [-l flen] [-m start:end] [-o oplen] [-p progressinterval]\n\
         [-r readbdy] [-s style] [-t truncbdy] [-w writebdy]\n\

ltp/fsx.c usage(void) · line 2488

Quoted 4 lines
ltp/fsx.c +1 -0
@@ -2484,6 +2485,7 @@ usage(void)
      -e: pollute post-eof on size changes (default 0)\n\
      -f: flush and invalidate cache after I/O\n\
      -g X: write character X instead of random generated data\n\
+     -h hugepages: use buffers backed by hugepages for reads/writes\n\

If this requires MADV_COLLAPSE, then perhaps the help text shouldn't describe the switch if the support wasn't compiled in?

e.g.

Quoted 8 lines

-g X: write character X instead of random generated data\n"
#ifdef MADV_COLLAPSE
" -h hugepages: use buffers backed by hugepages for reads/writes\n"
#endif
" -i logdev: do integrity testing, logdev is the dm log writes device\n\

(assuming I got the preprocessor and string construction goo right; I might be a few cards short of a deck due to zombie attack earlier)

Sounds great, I'll #ifdef out the help text -h line. Hope you feel better.

Quoted 4 lines
Quoted 4 lines
ltp/fsx.c +0 -0
      -i logdev: do integrity testing, logdev is the dm log writes device\n\
      -j logid: prefix debug log messsages with this id\n\
      -k: do not truncate existing file and use its size as upper bound on file size\n\

[...]

Quoted 11 lines
Quoted 11 lines
ltp/fsx.c +11 -0
+}
+
+#ifdef MADV_COLLAPSE
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size) + alignment;
+
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
Quoted 9 lines

ltp/fsx.c

Quoted 4 lines
ltp/fsx.c +4 -0
+             return NULL;
+     }
+     memset(buf, '\0', buf_size);
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {

If the fsx runs for a long period of time, will it be necessary to call MADV_COLLAPSE periodically to ensure that reclaim doesn't break up the hugepage?

Quoted 4 lines

imo, I don't think so. My understanding is that this would be a rare edge case that happens when the system is constrained on memory, in which case subsequent calls to MADV_COLLAPSE would most likely fail anyways.

Quoted 8 lines

Hrmmm... well I /do/ like to run memory constrained VMs to prod reclaim into stressing the filesystem more. But I guess there's no good way for fsx to know that something happened to it. Unless there's some even goofier way to force a hugepage, like shmem/hugetlbfs (ugh!) :)

Will have to ponder hugepage renewasl -- maybe we should madvise every few thousand fsxops just to be careful?

Quoted 6 lines

I wonder.. is there test value in doing collapses to the target file as well, either as a standalone map/madvise command or a random thing hitched onto preexisting commands? If so, I could see how something like that could potentially lift the current init time only approach into something that occurs with frequency, which then could at the same time (again maybe randomly) reinvoke for internal buffers as well.

Quoted 7 lines

My understanding is that if a filesystem has support enabled for large folios, then doing large writes/reads (which I believe is currently supported in fsx via the -o flag) will already automatically test the functionality of how the filesystem handles hugepages. I don't think this would be different from what doing a collapse on the target file would do.

Ah, that is a good point. So maybe not that useful to have something that would hook into writes. OTOH, fsx does a lot of random ops in the general case. I wonder how likely it is to sustain large folios in a typical long running test and whether explicit madvise calls thrown into the mix would make any difference at all.

I suppose there may also be an argument that doing collapses provides more test coverage than purely doing larger folio allocations at write time..? I don't know the code well enough to say whether there is any value there. FWIW, what I think is more interesting from the fsx side is the oddball sequences of operations that it can create to uncover similarly odd problems. IOW, in theory if we had a randomish "collapse target range before next operation," would that effectively provide more coverage with how the various supported ops interact with large folios over current behavior?

Quoted 5 lines

But anyways, this is all nebulous and strikes me more as maybe something interesting to play with as a potential future enhancement more than anything. BTW, is there any good way to measure use of large folios in general and/or on a particular file? I.e., collapse/split stats or some such thing..? Thanks.

I only know of two -- hooking the mm_filemap_add_to_page_cache tracepoint, and running MADV_COLLAPSE to see if it returns an errno.

--D

Quoted 100 lines

Brian

Quoted 96 lines

Thanks,
Joanne

Quoted 91 lines

All that said, this is new functionality and IIUC provides functional test coverage for a valid issue. IMO, it would be nice to get this merged as a baseline feature and explore these sort of enhancements as followon work. Just my .02.

Brian

Quoted 82 lines

--D

Quoted 79 lines

Thanks,
Joanne

Quoted 75 lines
Quoted 64 lines
ltp/fsx.c +52 -1
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
+#else
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     return NULL;
+}
+#endif
+
+static void
+init_buffers(void)
+{
+     int i;
+
+     original_buf = (char *) malloc(maxfilelen);
+     for (i = 0; i < maxfilelen; i++)
+             original_buf[i] = random() % 256;
+     if (hugepages) {
+             long hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(102);
+             }
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size, writebdy);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(103);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size, readbdy);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(103);
+             }
+     } else {
+             unsigned long good_buf_len = maxfilelen + writebdy;
+             unsigned long temp_buf_len = maxoplen + readbdy;
+
+             good_buf = calloc(1, good_buf_len);
+             temp_buf = calloc(1, temp_buf_len);
+     }
+     good_buf = round_ptr_up(good_buf, writebdy, 0);
+     temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+}
+
 static struct option longopts[] = {
      {"replay-ops", required_argument, 0, 256},
      {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2980,7 @@ main(int argc, char **argv)
      setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
      while ((ch = getopt_long(argc, argv,
-                              "0b:c:de:fg:i:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
+                              "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
                               longopts, NULL)) != EOF)
              switch (ch) {
              case 'b':

ltp/fsx.c main(int argc, char **argv) · line 3017

Quoted 5 lines
ltp/fsx.c +2 -0
@@ -2916,6 +3013,14 @@ main(int argc, char **argv)
              case 'g':
                      filldata = *optarg;
                      break;
+             case 'h':
+                     #ifndef MADV_COLLAPSE

Preprocessor directives should start at column 0, like most of the rest of fstests.

--D

#9 lore raw

Re: [PATCH v3 1/2] fsx: support reads/writes from buffers backed by hugepages

In reply to Brian Foster
Quoted 120 lines · Brian Foster
Quoted 110 lines · Joanne Koong
Quoted 102 lines · Brian Foster
Quoted 91 lines · Darrick J. Wong
Quoted 85 lines · Joanne Koong
Quoted 48 lines · Darrick J. Wong
Quoted 36 lines · Joanne Koong

Add support for reads/writes from buffers backed by hugepages. This can be enabled through the '-h' flag. This flag should only be used on systems where THP capabilities are enabled.

This is motivated by a recent bug that was due to faulty handling of userspace buffers backed by hugepages. This patch is a mitigation against problems like this in the future.

---
 ltp/fsx.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 11 deletions(-)
ltp/fsx.c +2 -1
index 41933354..8d3a2e2c 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
 int  uring = 0;
 int  mark_nr = 0;
+int  hugepages = 0;                  /* -h flag */
 
 int page_size;
 int page_mask;
@@ -2471,7 +2472,7 @@ void
 usage(void)
 {
      fprintf(stdout, "usage: %s",
-             "fsx [-dfknqxyzBEFHIJKLORWXZ0]\n\
+             "fsx [-dfhknqxyzBEFHIJKLORWXZ0]\n\
         [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid]\n\
         [-l flen] [-m start:end] [-o oplen] [-p progressinterval]\n\
         [-r readbdy] [-s style] [-t truncbdy] [-w writebdy]\n\

ltp/fsx.c usage(void) · line 2488

Quoted 4 lines
ltp/fsx.c +1 -0
@@ -2484,6 +2485,7 @@ usage(void)
      -e: pollute post-eof on size changes (default 0)\n\
      -f: flush and invalidate cache after I/O\n\
      -g X: write character X instead of random generated data\n\
+     -h hugepages: use buffers backed by hugepages for reads/writes\n\

If this requires MADV_COLLAPSE, then perhaps the help text shouldn't describe the switch if the support wasn't compiled in?

e.g.

Quoted 8 lines

-g X: write character X instead of random generated data\n"
#ifdef MADV_COLLAPSE
" -h hugepages: use buffers backed by hugepages for reads/writes\n"
#endif
" -i logdev: do integrity testing, logdev is the dm log writes device\n\

(assuming I got the preprocessor and string construction goo right; I might be a few cards short of a deck due to zombie attack earlier)

Sounds great, I'll #ifdef out the help text -h line. Hope you feel better.

Quoted 4 lines
Quoted 4 lines
ltp/fsx.c +0 -0
      -i logdev: do integrity testing, logdev is the dm log writes device\n\
      -j logid: prefix debug log messsages with this id\n\
      -k: do not truncate existing file and use its size as upper bound on file size\n\

[...]

Quoted 11 lines
Quoted 11 lines
ltp/fsx.c +11 -0
+}
+
+#ifdef MADV_COLLAPSE
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size) + alignment;
+
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
Quoted 9 lines

ltp/fsx.c

Quoted 4 lines
ltp/fsx.c +4 -0
+             return NULL;
+     }
+     memset(buf, '\0', buf_size);
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {

If the fsx runs for a long period of time, will it be necessary to call MADV_COLLAPSE periodically to ensure that reclaim doesn't break up the hugepage?

Quoted 4 lines

imo, I don't think so. My understanding is that this would be a rare edge case that happens when the system is constrained on memory, in which case subsequent calls to MADV_COLLAPSE would most likely fail anyways.

Quoted 8 lines

Hrmmm... well I /do/ like to run memory constrained VMs to prod reclaim into stressing the filesystem more. But I guess there's no good way for fsx to know that something happened to it. Unless there's some even goofier way to force a hugepage, like shmem/hugetlbfs (ugh!) :)

Will have to ponder hugepage renewasl -- maybe we should madvise every few thousand fsxops just to be careful?

Quoted 6 lines

I wonder.. is there test value in doing collapses to the target file as well, either as a standalone map/madvise command or a random thing hitched onto preexisting commands? If so, I could see how something like that could potentially lift the current init time only approach into something that occurs with frequency, which then could at the same time (again maybe randomly) reinvoke for internal buffers as well.

Quoted 7 lines

My understanding is that if a filesystem has support enabled for large folios, then doing large writes/reads (which I believe is currently supported in fsx via the -o flag) will already automatically test the functionality of how the filesystem handles hugepages. I don't think this would be different from what doing a collapse on the target file would do.

Quoted 5 lines

Ah, that is a good point. So maybe not that useful to have something that would hook into writes. OTOH, fsx does a lot of random ops in the general case. I wonder how likely it is to sustain large folios in a typical long running test and whether explicit madvise calls thrown into the mix would make any difference at all.

I think this would run into the same case where if there's enough severe memory constraint to where reclaim has to break up hugepages in the page cache, then explicit madvise calls to collapse these pages back together would most likely fail.

Quoted 10 lines

I suppose there may also be an argument that doing collapses provides more test coverage than purely doing larger folio allocations at write time..? I don't know the code well enough to say whether there is any value there. FWIW, what I think is more interesting from the fsx side is the oddball sequences of operations that it can create to uncover similarly odd problems. IOW, in theory if we had a randomish "collapse target range before next operation," would that effectively provide more coverage with how the various supported ops interact with large folios over current behavior?

I don't think collapses would have any effect on test coverage. For filesystems that don't support hugepages, the collapse would fail and for filesystems that do support hugepages, the folio would already be a hugepage and the collapse would be a no-op.

Thanks,
Joanne

Quoted 106 lines

But anyways, this is all nebulous and strikes me more as maybe something interesting to play with as a potential future enhancement more than anything. BTW, is there any good way to measure use of large folios in general and/or on a particular file? I.e., collapse/split stats or some such thing..? Thanks.

Brian

Quoted 96 lines

Thanks,
Joanne

Quoted 91 lines

All that said, this is new functionality and IIUC provides functional test coverage for a valid issue. IMO, it would be nice to get this merged as a baseline feature and explore these sort of enhancements as followon work. Just my .02.

Brian

Quoted 82 lines

--D

Quoted 79 lines

Thanks,
Joanne

Quoted 75 lines
Quoted 64 lines
ltp/fsx.c +52 -1
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
+#else
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     return NULL;
+}
+#endif
+
+static void
+init_buffers(void)
+{
+     int i;
+
+     original_buf = (char *) malloc(maxfilelen);
+     for (i = 0; i < maxfilelen; i++)
+             original_buf[i] = random() % 256;
+     if (hugepages) {
+             long hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(102);
+             }
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size, writebdy);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(103);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size, readbdy);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(103);
+             }
+     } else {
+             unsigned long good_buf_len = maxfilelen + writebdy;
+             unsigned long temp_buf_len = maxoplen + readbdy;
+
+             good_buf = calloc(1, good_buf_len);
+             temp_buf = calloc(1, temp_buf_len);
+     }
+     good_buf = round_ptr_up(good_buf, writebdy, 0);
+     temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+}
+
 static struct option longopts[] = {
      {"replay-ops", required_argument, 0, 256},
      {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2980,7 @@ main(int argc, char **argv)
      setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
      while ((ch = getopt_long(argc, argv,
-                              "0b:c:de:fg:i:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
+                              "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
                               longopts, NULL)) != EOF)
              switch (ch) {
              case 'b':

ltp/fsx.c main(int argc, char **argv) · line 3017

Quoted 5 lines
ltp/fsx.c +2 -0
@@ -2916,6 +3013,14 @@ main(int argc, char **argv)
              case 'g':
                      filldata = *optarg;
                      break;
+             case 'h':
+                     #ifndef MADV_COLLAPSE

Preprocessor directives should start at column 0, like most of the rest of fstests.

--D

#10 lore raw

Re: [PATCH v3 1/2] fsx: support reads/writes from buffers backed by hugepages

In reply to Darrick J. Wong
Quoted 92 lines · Darrick J. Wong
Quoted 85 lines · Joanne Koong
Quoted 48 lines · Darrick J. Wong
Quoted 36 lines · Joanne Koong

Add support for reads/writes from buffers backed by hugepages. This can be enabled through the '-h' flag. This flag should only be used on systems where THP capabilities are enabled.

This is motivated by a recent bug that was due to faulty handling of userspace buffers backed by hugepages. This patch is a mitigation against problems like this in the future.

---
 ltp/fsx.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 11 deletions(-)
ltp/fsx.c +2 -1
index 41933354..8d3a2e2c 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
 int  uring = 0;
 int  mark_nr = 0;
+int  hugepages = 0;                  /* -h flag */
 
 int page_size;
 int page_mask;
@@ -2471,7 +2472,7 @@ void
 usage(void)
 {
      fprintf(stdout, "usage: %s",
-             "fsx [-dfknqxyzBEFHIJKLORWXZ0]\n\
+             "fsx [-dfhknqxyzBEFHIJKLORWXZ0]\n\
         [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid]\n\
         [-l flen] [-m start:end] [-o oplen] [-p progressinterval]\n\
         [-r readbdy] [-s style] [-t truncbdy] [-w writebdy]\n\

ltp/fsx.c usage(void) · line 2488

Quoted 4 lines
ltp/fsx.c +1 -0
@@ -2484,6 +2485,7 @@ usage(void)
      -e: pollute post-eof on size changes (default 0)\n\
      -f: flush and invalidate cache after I/O\n\
      -g X: write character X instead of random generated data\n\
+     -h hugepages: use buffers backed by hugepages for reads/writes\n\

If this requires MADV_COLLAPSE, then perhaps the help text shouldn't describe the switch if the support wasn't compiled in?

e.g.

Quoted 8 lines

-g X: write character X instead of random generated data\n"
#ifdef MADV_COLLAPSE
" -h hugepages: use buffers backed by hugepages for reads/writes\n"
#endif
" -i logdev: do integrity testing, logdev is the dm log writes device\n\

(assuming I got the preprocessor and string construction goo right; I might be a few cards short of a deck due to zombie attack earlier)

Sounds great, I'll #ifdef out the help text -h line. Hope you feel better.

Quoted 4 lines
Quoted 4 lines
ltp/fsx.c +0 -0
      -i logdev: do integrity testing, logdev is the dm log writes device\n\
      -j logid: prefix debug log messsages with this id\n\
      -k: do not truncate existing file and use its size as upper bound on file size\n\

[...]

Quoted 11 lines
Quoted 11 lines
ltp/fsx.c +11 -0
+}
+
+#ifdef MADV_COLLAPSE
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size) + alignment;
+
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
Quoted 9 lines

ltp/fsx.c keep_running(void) · line 2904

Quoted 4 lines
ltp/fsx.c +4 -0
+             return NULL;
+     }
+     memset(buf, '\0', buf_size);
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {

If the fsx runs for a long period of time, will it be necessary to call MADV_COLLAPSE periodically to ensure that reclaim doesn't break up the hugepage?

Quoted 4 lines

imo, I don't think so. My understanding is that this would be a rare edge case that happens when the system is constrained on memory, in which case subsequent calls to MADV_COLLAPSE would most likely fail anyways.

Quoted 4 lines

Hrmmm... well I /do/ like to run memory constrained VMs to prod reclaim into stressing the filesystem more. But I guess there's no good way for fsx to know that something happened to it. Unless there's some even goofier way to force a hugepage, like shmem/hugetlbfs (ugh!) :)

I can't think of a better way to force a hugepage either. I believe shmem and hugetlbfs would both require root privileges to do so, and if i'm not mistaken, shmem hugepages are still subject to being broken up by reclaim.

Quoted 3 lines

Will have to ponder hugepage renewasl -- maybe we should madvise every few thousand fsxops just to be careful?

I can add this in, but on memory constrained VMs, would this be effective? To me, it seems like in the majority of cases, subsequent attempts at collapsing the broken pages back into a hugepage would fail due to memory still being constrained. In which case, I guess we'd exit the test altogether? It kind of seems to me like if the user wants to test out hugepages functionality of their filesystem, then the onus is on them to run the test in an environment that can adequately and consistently support hugepages.

Thanks,
Joanne

Quoted 82 lines

--D

Quoted 79 lines

Thanks,
Joanne

Quoted 75 lines
Quoted 64 lines
ltp/fsx.c +52 -1
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
+#else
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     return NULL;
+}
+#endif
+
+static void
+init_buffers(void)
+{
+     int i;
+
+     original_buf = (char *) malloc(maxfilelen);
+     for (i = 0; i < maxfilelen; i++)
+             original_buf[i] = random() % 256;
+     if (hugepages) {
+             long hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(102);
+             }
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size, writebdy);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(103);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size, readbdy);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(103);
+             }
+     } else {
+             unsigned long good_buf_len = maxfilelen + writebdy;
+             unsigned long temp_buf_len = maxoplen + readbdy;
+
+             good_buf = calloc(1, good_buf_len);
+             temp_buf = calloc(1, temp_buf_len);
+     }
+     good_buf = round_ptr_up(good_buf, writebdy, 0);
+     temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+}
+
 static struct option longopts[] = {
      {"replay-ops", required_argument, 0, 256},
      {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2980,7 @@ main(int argc, char **argv)
      setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
      while ((ch = getopt_long(argc, argv,
-                              "0b:c:de:fg:i:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
+                              "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
                               longopts, NULL)) != EOF)
              switch (ch) {
              case 'b':

ltp/fsx.c main(int argc, char **argv) · line 3017

Quoted 5 lines
ltp/fsx.c +2 -0
@@ -2916,6 +3013,14 @@ main(int argc, char **argv)
              case 'g':
                      filldata = *optarg;
                      break;
+             case 'h':
+                     #ifndef MADV_COLLAPSE

Preprocessor directives should start at column 0, like most of the rest of fstests.

--D

#11 lore raw

Re: [PATCH v3 1/2] fsx: support reads/writes from buffers backed by hugepages

Quoted 104 lines · Joanne Koong
Quoted 92 lines · Darrick J. Wong
Quoted 85 lines · Joanne Koong
Quoted 48 lines · Darrick J. Wong
Quoted 36 lines · Joanne Koong

Add support for reads/writes from buffers backed by hugepages. This can be enabled through the '-h' flag. This flag should only be used on systems where THP capabilities are enabled.

This is motivated by a recent bug that was due to faulty handling of userspace buffers backed by hugepages. This patch is a mitigation against problems like this in the future.

---
 ltp/fsx.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 11 deletions(-)
ltp/fsx.c +2 -1
index 41933354..8d3a2e2c 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
 int  uring = 0;
 int  mark_nr = 0;
+int  hugepages = 0;                  /* -h flag */
 
 int page_size;
 int page_mask;
@@ -2471,7 +2472,7 @@ void
 usage(void)
 {
      fprintf(stdout, "usage: %s",
-             "fsx [-dfknqxyzBEFHIJKLORWXZ0]\n\
+             "fsx [-dfhknqxyzBEFHIJKLORWXZ0]\n\
         [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid]\n\
         [-l flen] [-m start:end] [-o oplen] [-p progressinterval]\n\
         [-r readbdy] [-s style] [-t truncbdy] [-w writebdy]\n\

ltp/fsx.c usage(void) · line 2488

Quoted 4 lines
ltp/fsx.c +1 -0
@@ -2484,6 +2485,7 @@ usage(void)
      -e: pollute post-eof on size changes (default 0)\n\
      -f: flush and invalidate cache after I/O\n\
      -g X: write character X instead of random generated data\n\
+     -h hugepages: use buffers backed by hugepages for reads/writes\n\

If this requires MADV_COLLAPSE, then perhaps the help text shouldn't describe the switch if the support wasn't compiled in?

e.g.

Quoted 8 lines

-g X: write character X instead of random generated data\n"
#ifdef MADV_COLLAPSE
" -h hugepages: use buffers backed by hugepages for reads/writes\n"
#endif
" -i logdev: do integrity testing, logdev is the dm log writes device\n\

(assuming I got the preprocessor and string construction goo right; I might be a few cards short of a deck due to zombie attack earlier)

Sounds great, I'll #ifdef out the help text -h line. Hope you feel better.

Quoted 4 lines
Quoted 4 lines
ltp/fsx.c +0 -0
      -i logdev: do integrity testing, logdev is the dm log writes device\n\
      -j logid: prefix debug log messsages with this id\n\
      -k: do not truncate existing file and use its size as upper bound on file size\n\

[...]

Quoted 11 lines
Quoted 11 lines
ltp/fsx.c +11 -0
+}
+
+#ifdef MADV_COLLAPSE
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size) + alignment;
+
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
Quoted 9 lines

ltp/fsx.c keep_running(void) · line 2904

Quoted 4 lines
ltp/fsx.c +4 -0
+             return NULL;
+     }
+     memset(buf, '\0', buf_size);
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {

If the fsx runs for a long period of time, will it be necessary to call MADV_COLLAPSE periodically to ensure that reclaim doesn't break up the hugepage?

Quoted 4 lines

imo, I don't think so. My understanding is that this would be a rare edge case that happens when the system is constrained on memory, in which case subsequent calls to MADV_COLLAPSE would most likely fail anyways.

Quoted 4 lines

Hrmmm... well I /do/ like to run memory constrained VMs to prod reclaim into stressing the filesystem more. But I guess there's no good way for fsx to know that something happened to it. Unless there's some even goofier way to force a hugepage, like shmem/hugetlbfs (ugh!) :)

I can't think of a better way to force a hugepage either. I believe shmem and hugetlbfs would both require root privileges to do so, and if i'm not mistaken, shmem hugepages are still subject to being broken up by reclaim.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines

Will have to ponder hugepage renewasl -- maybe we should madvise every few thousand fsxops just to be careful?

I can add this in, but on memory constrained VMs, would this be effective? To me, it seems like in the majority of cases, subsequent

They're not /so/ memory constrained that the initial collapsed page is likely to get split/reclaimed before your regression test finishes...

Quoted 3 lines

attempts at collapsing the broken pages back into a hugepage would fail due to memory still being constrained. In which case, I guess we'd exit the test altogether?

...but I was starting to wonder if this is something we'd actually want in a long soak test, just in case there are weird effects on the system after fsx has been running for a few days?

Quoted 4 lines

It kind of seems to me like if the user
wants to test out hugepages functionality of their filesystem, then the onus is on them to run the test in an environment that can adequately and consistently support hugepages.

I guess the hard part about analyzing that is is that long soak tests aren't usually supposed to die on account of memory fragmentation. Hence me wondering if there's an "easy" way to get one huge page and not let it go.

Anyway don't let my blathering hold this up; I think once you fix the help text and the #ifdef indenting around exit(86) this patch is good to go.

--D

Quoted 86 lines

Thanks,
Joanne

Quoted 82 lines

--D

Quoted 79 lines

Thanks,
Joanne

Quoted 75 lines
Quoted 64 lines
ltp/fsx.c +52 -1
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
+#else
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     return NULL;
+}
+#endif
+
+static void
+init_buffers(void)
+{
+     int i;
+
+     original_buf = (char *) malloc(maxfilelen);
+     for (i = 0; i < maxfilelen; i++)
+             original_buf[i] = random() % 256;
+     if (hugepages) {
+             long hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(102);
+             }
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size, writebdy);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(103);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size, readbdy);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(103);
+             }
+     } else {
+             unsigned long good_buf_len = maxfilelen + writebdy;
+             unsigned long temp_buf_len = maxoplen + readbdy;
+
+             good_buf = calloc(1, good_buf_len);
+             temp_buf = calloc(1, temp_buf_len);
+     }
+     good_buf = round_ptr_up(good_buf, writebdy, 0);
+     temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+}
+
 static struct option longopts[] = {
      {"replay-ops", required_argument, 0, 256},
      {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2980,7 @@ main(int argc, char **argv)
      setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
      while ((ch = getopt_long(argc, argv,
-                              "0b:c:de:fg:i:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
+                              "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
                               longopts, NULL)) != EOF)
              switch (ch) {
              case 'b':

ltp/fsx.c main(int argc, char **argv) · line 3017

Quoted 5 lines
ltp/fsx.c +2 -0
@@ -2916,6 +3013,14 @@ main(int argc, char **argv)
              case 'g':
                      filldata = *optarg;
                      break;
+             case 'h':
+                     #ifndef MADV_COLLAPSE

Preprocessor directives should start at column 0, like most of the rest of fstests.

--D

#12 lore raw

Re: [PATCH v3 1/2] fsx: support reads/writes from buffers backed by hugepages

Quoted 128 lines · Darrick J. Wong
Quoted 104 lines · Joanne Koong
Quoted 92 lines · Darrick J. Wong
Quoted 85 lines · Joanne Koong
Quoted 48 lines · Darrick J. Wong
Quoted 36 lines · Joanne Koong

Add support for reads/writes from buffers backed by hugepages. This can be enabled through the '-h' flag. This flag should only be used on systems where THP capabilities are enabled.

This is motivated by a recent bug that was due to faulty handling of userspace buffers backed by hugepages. This patch is a mitigation against problems like this in the future.

---
 ltp/fsx.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 108 insertions(+), 11 deletions(-)
ltp/fsx.c +2 -1
index 41933354..8d3a2e2c 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
 int  uring = 0;
 int  mark_nr = 0;
+int  hugepages = 0;                  /* -h flag */
 
 int page_size;
 int page_mask;
@@ -2471,7 +2472,7 @@ void
 usage(void)
 {
      fprintf(stdout, "usage: %s",
-             "fsx [-dfknqxyzBEFHIJKLORWXZ0]\n\
+             "fsx [-dfhknqxyzBEFHIJKLORWXZ0]\n\
         [-b opnum] [-c Prob] [-g filldata] [-i logdev] [-j logid]\n\
         [-l flen] [-m start:end] [-o oplen] [-p progressinterval]\n\
         [-r readbdy] [-s style] [-t truncbdy] [-w writebdy]\n\

ltp/fsx.c usage(void) · line 2488

Quoted 4 lines
ltp/fsx.c +1 -0
@@ -2484,6 +2485,7 @@ usage(void)
      -e: pollute post-eof on size changes (default 0)\n\
      -f: flush and invalidate cache after I/O\n\
      -g X: write character X instead of random generated data\n\
+     -h hugepages: use buffers backed by hugepages for reads/writes\n\

If this requires MADV_COLLAPSE, then perhaps the help text shouldn't describe the switch if the support wasn't compiled in?

e.g.

Quoted 8 lines

-g X: write character X instead of random generated data\n"
#ifdef MADV_COLLAPSE
" -h hugepages: use buffers backed by hugepages for reads/writes\n"
#endif
" -i logdev: do integrity testing, logdev is the dm log writes device\n\

(assuming I got the preprocessor and string construction goo right; I might be a few cards short of a deck due to zombie attack earlier)

Sounds great, I'll #ifdef out the help text -h line. Hope you feel better.

Quoted 4 lines
Quoted 4 lines
ltp/fsx.c +0 -0
      -i logdev: do integrity testing, logdev is the dm log writes device\n\
      -j logid: prefix debug log messsages with this id\n\
      -k: do not truncate existing file and use its size as upper bound on file size\n\

[...]

Quoted 11 lines
Quoted 11 lines
ltp/fsx.c +11 -0
+}
+
+#ifdef MADV_COLLAPSE
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size) + alignment;
+
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
Quoted 9 lines

ltp/fsx.c keep_running(void) · line 2904

Quoted 4 lines
ltp/fsx.c +4 -0
+             return NULL;
+     }
+     memset(buf, '\0', buf_size);
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {

If the fsx runs for a long period of time, will it be necessary to call MADV_COLLAPSE periodically to ensure that reclaim doesn't break up the hugepage?

Quoted 4 lines

imo, I don't think so. My understanding is that this would be a rare edge case that happens when the system is constrained on memory, in which case subsequent calls to MADV_COLLAPSE would most likely fail anyways.

Quoted 4 lines

Hrmmm... well I /do/ like to run memory constrained VMs to prod reclaim into stressing the filesystem more. But I guess there's no good way for fsx to know that something happened to it. Unless there's some even goofier way to force a hugepage, like shmem/hugetlbfs (ugh!) :)

I can't think of a better way to force a hugepage either. I believe shmem and hugetlbfs would both require root privileges to do so, and if i'm not mistaken, shmem hugepages are still subject to being broken up by reclaim.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines

Will have to ponder hugepage renewasl -- maybe we should madvise every few thousand fsxops just to be careful?

I can add this in, but on memory constrained VMs, would this be effective? To me, it seems like in the majority of cases, subsequent

They're not /so/ memory constrained that the initial collapsed page is likely to get split/reclaimed before your regression test finishes...

Quoted 3 lines

attempts at collapsing the broken pages back into a hugepage would fail due to memory still being constrained. In which case, I guess we'd exit the test altogether?

...but I was starting to wonder if this is something we'd actually want in a long soak test, just in case there are weird effects on the system after fsx has been running for a few days?

Quoted 4 lines

It kind of seems to me like if the user
wants to test out hugepages functionality of their filesystem, then the onus is on them to run the test in an environment that can adequately and consistently support hugepages.

Quoted 4 lines

I guess the hard part about analyzing that is is that long soak tests aren't usually supposed to die on account of memory fragmentation. Hence me wondering if there's an "easy" way to get one huge page and not let it go.

For a long soak situation, I think the periodic collapses every few thousands ops makes sense. Where the "-h" flag is interpreted as best-effort (eg the test still continues on even if the subsequent collapses fail instead of exiting the test altogether, we just make a best effort at collapsing). I'll add this change to v4.

Thanks,
Joanne

Quoted 93 lines

Anyway don't let my blathering hold this up; I think once you fix the help text and the #ifdef indenting around exit(86) this patch is good to go.

--D

Quoted 86 lines

Thanks,
Joanne

Quoted 82 lines

--D

Quoted 79 lines

Thanks,
Joanne

Quoted 75 lines
Quoted 64 lines
ltp/fsx.c +52 -1
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
+#else
+static void *
+init_hugepages_buf(unsigned len, int hugepage_size, int alignment)
+{
+     return NULL;
+}
+#endif
+
+static void
+init_buffers(void)
+{
+     int i;
+
+     original_buf = (char *) malloc(maxfilelen);
+     for (i = 0; i < maxfilelen; i++)
+             original_buf[i] = random() % 256;
+     if (hugepages) {
+             long hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(102);
+             }
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size, writebdy);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(103);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size, readbdy);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(103);
+             }
+     } else {
+             unsigned long good_buf_len = maxfilelen + writebdy;
+             unsigned long temp_buf_len = maxoplen + readbdy;
+
+             good_buf = calloc(1, good_buf_len);
+             temp_buf = calloc(1, temp_buf_len);
+     }
+     good_buf = round_ptr_up(good_buf, writebdy, 0);
+     temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+}
+
 static struct option longopts[] = {
      {"replay-ops", required_argument, 0, 256},
      {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2980,7 @@ main(int argc, char **argv)
      setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
 
      while ((ch = getopt_long(argc, argv,
-                              "0b:c:de:fg:i:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
+                              "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:xyABD:EFJKHzCILN:OP:RS:UWXZ",
                               longopts, NULL)) != EOF)
              switch (ch) {
              case 'b':

ltp/fsx.c main(int argc, char **argv) · line 3017

Quoted 5 lines
ltp/fsx.c +2 -0
@@ -2916,6 +3013,14 @@ main(int argc, char **argv)
              case 'g':
                      filldata = *optarg;
                      break;
+             case 'h':
+                     #ifndef MADV_COLLAPSE

Preprocessor directives should start at column 0, like most of the rest of fstests.

--D

#13 lore raw

PATCH v3 2/2 generic: add tests for read/writes from hugepages-backed buffers

In reply to Joanne Koong

Add generic tests 758 and 759 for testing reads/writes from buffers backed by hugepages.

---
 common/rc             | 13 +++++++++++++
 tests/generic/758     | 22 ++++++++++++++++++++++
 tests/generic/758.out |  4 ++++
 tests/generic/759     | 26 ++++++++++++++++++++++++++
 tests/generic/759.out |  4 ++++
 5 files changed, 69 insertions(+)
 create mode 100755 tests/generic/758
 create mode 100644 tests/generic/758.out
 create mode 100755 tests/generic/759
 create mode 100644 tests/generic/759.out
common/rc +13 -0
index 1b2e4508..825e350e 100644
@@ -3016,6 +3016,19 @@ _require_xfs_io_command()
 	fi
 }
 
+# check that the system supports transparent hugepages
+_require_thp()
+{
+    if [ ! -e /sys/kernel/mm/transparent_hugepage/enabled ]; then
+	    _notrun "system doesn't support transparent hugepages"
+    fi
+
+    thp_status=$(cat /sys/kernel/mm/transparent_hugepage/enabled)
+    if [[ $thp_status == *"[never]"* ]]; then
+	    _notrun "system doesn't have transparent hugepages enabled"
+    fi
+}
+
 # check that kernel and filesystem support direct I/O, and check if "$1" size
 # aligned (optional) is supported
 _require_odirect()
tests/generic/758 new file +22 -0
new file mode 100755
index 00000000..e7cd8cdc
@@ -0,0 +1,22 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# FS QA Test No. 758
+#
+# fsx exercising reads/writes from userspace buffers
+# backed by hugepages
+#
+. ./common/preamble
+_begin_fstest rw auto quick
+
+. ./common/filter
+
+_require_test
+_require_thp
+
+run_fsx -N 10000            -l 500000 -h
+run_fsx -N 10000  -o 8192   -l 500000 -h
+run_fsx -N 10000  -o 128000 -l 500000 -h
+
+status=0
+exit
tests/generic/758.out new file +4 -0
new file mode 100644
index 00000000..af04bb14
@@ -0,0 +1,4 @@
+QA output created by 758
+fsx -N 10000 -l 500000 -h
+fsx -N 10000 -o 8192 -l 500000 -h
+fsx -N 10000 -o 128000 -l 500000 -h
tests/generic/759 new file +26 -0
new file mode 100755
index 00000000..514e7603
@@ -0,0 +1,26 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# FS QA Test No. 759
+#
+# fsx exercising direct IO reads/writes from userspace buffers
+# backed by hugepages
+#
+. ./common/preamble
+_begin_fstest rw auto quick
+
+. ./common/filter
+
+_require_test
+_require_odirect
+_require_thp
+
+psize=`$here/src/feature -s`
+bsize=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
+
+run_fsx -N 10000            -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z -R -W -h
+run_fsx -N 10000  -o 8192   -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z -R -W -h
+run_fsx -N 10000  -o 128000 -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z -R -W -h
+
+status=0
+exit
tests/generic/759.out new file +4 -0
new file mode 100644
index 00000000..86bb66ef
@@ -0,0 +1,4 @@
+QA output created by 759
+fsx -N 10000 -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z -R -W -h
+fsx -N 10000 -o 8192 -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z -R -W -h
+fsx -N 10000 -o 128000 -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z -R -W -h
Signature · 1 line
2.47.1
Reviewed-by on v3 2/2
#14 lore raw

Re: [PATCH v3 2/2] generic: add tests for read/writes from hugepages-backed buffers

Quoted 26 lines · Joanne Koong

Add generic tests 758 and 759 for testing reads/writes from buffers backed by hugepages.

---
 common/rc             | 13 +++++++++++++
 tests/generic/758     | 22 ++++++++++++++++++++++
 tests/generic/758.out |  4 ++++
 tests/generic/759     | 26 ++++++++++++++++++++++++++
 tests/generic/759.out |  4 ++++
 5 files changed, 69 insertions(+)
 create mode 100755 tests/generic/758
 create mode 100644 tests/generic/758.out
 create mode 100755 tests/generic/759
 create mode 100644 tests/generic/759.out
common/rc +1 -0
index 1b2e4508..825e350e 100644
@@ -3016,6 +3016,19 @@ _require_xfs_io_command()
 	fi
 }
 
+# check that the system supports transparent hugepages

common/rc _require_xfs_io_command() · line 3029

Quoted 10 lines
common/rc +10 -0
+_require_thp()
+{
+    if [ ! -e /sys/kernel/mm/transparent_hugepage/enabled ]; then
+	    _notrun "system doesn't support transparent hugepages"
+    fi
+
+    thp_status=$(cat /sys/kernel/mm/transparent_hugepage/enabled)
+    if [[ $thp_status == *"[never]"* ]]; then
+	    _notrun "system doesn't have transparent hugepages enabled"
+    fi

Tabs, not spaces.

With that fixed,

--D

Quoted 89 lines
common/rc +2 -0
+}
+
 # check that kernel and filesystem support direct I/O, and check if "$1" size
 # aligned (optional) is supported
 _require_odirect()
tests/generic/758 new file +22 -0
new file mode 100755
index 00000000..e7cd8cdc
@@ -0,0 +1,22 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# FS QA Test No. 758
+#
+# fsx exercising reads/writes from userspace buffers
+# backed by hugepages
+#
+. ./common/preamble
+_begin_fstest rw auto quick
+
+. ./common/filter
+
+_require_test
+_require_thp
+
+run_fsx -N 10000            -l 500000 -h
+run_fsx -N 10000  -o 8192   -l 500000 -h
+run_fsx -N 10000  -o 128000 -l 500000 -h
+
+status=0
+exit
tests/generic/758.out new file +4 -0
new file mode 100644
index 00000000..af04bb14
@@ -0,0 +1,4 @@
+QA output created by 758
+fsx -N 10000 -l 500000 -h
+fsx -N 10000 -o 8192 -l 500000 -h
+fsx -N 10000 -o 128000 -l 500000 -h
tests/generic/759 new file +26 -0
new file mode 100755
index 00000000..514e7603
@@ -0,0 +1,26 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# FS QA Test No. 759
+#
+# fsx exercising direct IO reads/writes from userspace buffers
+# backed by hugepages
+#
+. ./common/preamble
+_begin_fstest rw auto quick
+
+. ./common/filter
+
+_require_test
+_require_odirect
+_require_thp
+
+psize=`$here/src/feature -s`
+bsize=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
+
+run_fsx -N 10000            -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z -R -W -h
+run_fsx -N 10000  -o 8192   -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z -R -W -h
+run_fsx -N 10000  -o 128000 -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z -R -W -h
+
+status=0
+exit
tests/generic/759.out new file +4 -0
new file mode 100644
index 00000000..86bb66ef
@@ -0,0 +1,4 @@
+QA output created by 759
+fsx -N 10000 -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z -R -W -h
+fsx -N 10000 -o 8192 -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z -R -W -h
+fsx -N 10000 -o 128000 -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z -R -W -h
Signature · 1 line
2.47.1