Skip to contentSkip to conversation

fstests: test reads/writes from hugepages-backed buffers

PATCH Superseded Joanne Koong 4 participants 15 messages Dec 18, 2024 – Dec 27, 2024 VFS

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

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/

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

 common/rc             |  10 +++++
 ltp/fsx.c             | 100 ++++++++++++++++++++++++++++++++++++++----
 tests/generic/758     |  23 ++++++++++
 tests/generic/758.out |   4 ++
 tests/generic/759     |  24 ++++++++++
 tests/generic/759.out |   4 ++
 6 files changed, 157 insertions(+), 8 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 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.

---
 ltp/fsx.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 92 insertions(+), 8 deletions(-)
ltp/fsx.c +92 -8
index 41933354..3656fd9f 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,56 @@ keep_running(void)
 	return numops-- != 0;
 }
 
+static long
+get_hugepage_size(void)
+{
+	const char *str = "Hugepagesize:";
+	long hugepage_size = -1;
+	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, strlen(str)) == 0) {
+			sscanf(buffer + strlen(str), "%ld", &hugepage_size);
+			break;
+		}
+	}
+	fclose(file);
+	if (hugepage_size == -1) {
+		prterr("get_hugepage_size: failed to find "
+			"hugepage size in /proc/meminfo\n");
+		return -1;
+	}
+
+	/* convert from KiB to bytes  */
+	return hugepage_size * 1024;
+}
+
+static void *
+init_hugepages_buf(unsigned len, long hugepage_size)
+{
+	void *buf;
+	long buf_size = roundup(len, hugepage_size);
+
+	if (posix_memalign(&buf, hugepage_size, buf_size)) {
+		prterr("posix_memalign for buf");
+		return NULL;
+	}
+	memset(buf, '\0', len);
+	if (madvise(buf, buf_size, MADV_COLLAPSE)) {
+		prterr("madvise collapse for buf");
+		free(buf);
+		return NULL;
+	}
+
+	return buf;
+}
+
 static struct option longopts[] = {
 	{"replay-ops", required_argument, 0, 256},
 	{"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2935,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 +2968,9 @@ main(int argc, char **argv)
 		case 'g':
 			filldata = *optarg;
 			break;
+		case 'h':
+			hugepages = 1;
+			break;
 		case 'i':
 			integrity = 1;
 			logdev = strdup(optarg);
@@ -3232,12 +3287,41 @@ main(int argc, char **argv)
 	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);
+	if (hugepages) {
+		long hugepage_size;
+
+		hugepage_size = get_hugepage_size();
+		if (hugepage_size == -1) {
+			prterr("get_hugepage_size()");
+			exit(99);
+		}
+
+		if (writebdy != 1 && writebdy != hugepage_size)
+			prt("ignoring write alignment (since -h is enabled)");
+
+		if (readbdy != 1 && readbdy != hugepage_size)
+			prt("ignoring read alignment (since -h is enabled)");
+
+		good_buf = init_hugepages_buf(maxfilelen, hugepage_size);
+		if (!good_buf) {
+			prterr("init_hugepages_buf failed for good_buf");
+			exit(100);
+		}
+
+		temp_buf = init_hugepages_buf(maxoplen, hugepage_size);
+		if (!temp_buf) {
+			prterr("init_hugepages_buf failed for temp_buf");
+			exit(101);
+		}
+	} else {
+		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);
+	}
 	if (lite) {	/* zero entire existing file */
 		ssize_t written;
 
Signature · 1 line
2.47.1
#2 lore raw

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

Quoted 6 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.

---

Firstly, thanks for taking the time to add this. This seems like a nice idea. It might be nice to have an extra sentence or two in the commit log on the purpose/motivation. For example, has this been used to detect a certain class of problem?

A few other quick comments below...

Quoted 67 lines
 ltp/fsx.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 92 insertions(+), 8 deletions(-)
ltp/fsx.c +34 -1
index 41933354..3656fd9f 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,56 @@ keep_running(void)
 	return numops-- != 0;
 }
 
+static long
+get_hugepage_size(void)
+{
+	const char *str = "Hugepagesize:";
+	long hugepage_size = -1;
+	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, strlen(str)) == 0) {
+			sscanf(buffer + strlen(str), "%ld", &hugepage_size);
+			break;
+		}
+	}
+	fclose(file);
+	if (hugepage_size == -1) {
+		prterr("get_hugepage_size: failed to find "
+			"hugepage size in /proc/meminfo\n");
+		return -1;
+	}
+
+	/* convert from KiB to bytes  */
+	return hugepage_size * 1024;
+}
+
+static void *

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

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

I'm assuming it doesn't matter, but did you want to use buf_size here to clear the whole buffer?

Quoted 45 lines
ltp/fsx.c +17 -7
+	if (madvise(buf, buf_size, MADV_COLLAPSE)) {
+		prterr("madvise collapse for buf");
+		free(buf);
+		return NULL;
+	}
+
+	return buf;
+}
+
 static struct option longopts[] = {
 	{"replay-ops", required_argument, 0, 256},
 	{"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2935,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 +2968,9 @@ main(int argc, char **argv)
 		case 'g':
 			filldata = *optarg;
 			break;
+		case 'h':
+			hugepages = 1;
+			break;
 		case 'i':
 			integrity = 1;
 			logdev = strdup(optarg);
@@ -3232,12 +3287,41 @@ main(int argc, char **argv)
 	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);
+	if (hugepages) {
+		long hugepage_size;
+
+		hugepage_size = get_hugepage_size();

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

Quoted 10 lines
ltp/fsx.c +10 -0
+		if (hugepage_size == -1) {
+			prterr("get_hugepage_size()");
+			exit(99);
+		}
+
+		if (writebdy != 1 && writebdy != hugepage_size)
+			prt("ignoring write alignment (since -h is enabled)");
+
+		if (readbdy != 1 && readbdy != hugepage_size)
+			prt("ignoring read alignment (since -h is enabled)");

I'm a little unclear on what these warnings mean. The alignments are still used in the read/write paths afaics. The non-huge mode seems to only really care about the max size of the buffers in this code.

If your test doesn't actually use read/write alignments and the goal is just to keep things simple, perhaps it would be cleaner to add something like an if (hugepages && (writebdy != 1 || readbdy != 1)) check after option processing and exit out as an unsupported combination..?

BTW, it might also be nice to factor out this whole section of buffer initialization code (including original_buf) into an init_buffers() or some such. That could be done as a prep patch, but just a suggestion either way.

Brian

Quoted 28 lines
ltp/fsx.c +21 -0
+
+		good_buf = init_hugepages_buf(maxfilelen, hugepage_size);
+		if (!good_buf) {
+			prterr("init_hugepages_buf failed for good_buf");
+			exit(100);
+		}
+
+		temp_buf = init_hugepages_buf(maxoplen, hugepage_size);
+		if (!temp_buf) {
+			prterr("init_hugepages_buf failed for temp_buf");
+			exit(101);
+		}
+	} else {
+		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);
+	}
 	if (lite) {	/* zero entire existing file */
 		ssize_t written;
 
Signature · 1 line
2.47.1
#3 lore raw

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

Quoted 6 lines · Brian Foster
Quoted 4 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.

Quoted 7 lines
Quoted 2 lines

---

Firstly, thanks for taking the time to add this. This seems like a nice idea. It might be nice to have an extra sentence or two in the commit log on the purpose/motivation. For example, has this been used to detect a certain class of problem?

Hi Brian,

Thanks for reviewing this. That's a good idea - I'll include the sentence from the cover letter to this commit message as well: "This is motivated by a recent bug that was due to faulty handling for userspace buffers backed by hugepages."

Quoted 20 lines

A few other quick comments below...

Quoted 17 lines
 ltp/fsx.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 92 insertions(+), 8 deletions(-)
ltp/fsx.c +8 -0
index 41933354..3656fd9f 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
+}
+
+static void *
+init_hugepages_buf(unsigned len, long hugepage_size)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size);
+
Quoted 8 lines

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

Quoted 5 lines
ltp/fsx.c +5 -0
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
+             return NULL;
+     }
+     memset(buf, '\0', len);

I'm assuming it doesn't matter, but did you want to use buf_size here to clear the whole buffer?

I only saw buf being used up to len in the rest of the code so I didn't think it was necessary, but I also don't feel strongly about this and am happy to change this to clear the entire buffer if preferred.

Quoted 38 lines
Quoted 23 lines
ltp/fsx.c +12 -6
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
@@ -3232,12 +3287,41 @@ main(int argc, char **argv)
      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);
+     if (hugepages) {
+             long hugepage_size;
+
+             hugepage_size = get_hugepage_size();

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

Quoted 10 lines
ltp/fsx.c +10 -0
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(99);
+             }
+
+             if (writebdy != 1 && writebdy != hugepage_size)
+                     prt("ignoring write alignment (since -h is enabled)");
+
+             if (readbdy != 1 && readbdy != hugepage_size)
+                     prt("ignoring read alignment (since -h is enabled)");

I'm a little unclear on what these warnings mean. The alignments are still used in the read/write paths afaics. The non-huge mode seems to only really care about the max size of the buffers in this code.

Quoted 4 lines

If your test doesn't actually use read/write alignments and the goal is just to keep things simple, perhaps it would be cleaner to add something like an if (hugepages && (writebdy != 1 || readbdy != 1)) check after option processing and exit out as an unsupported combination..?

My understanding of the 'writebdy' and 'readbdy' options are that they're for making reads/writes aligned to the passed-in value, which depends on the starting address of the buffer being aligned to that value as well. However for hugepages buffers, they must be aligned to the system hugepage size (eg 2 MiB) or the madvise(... MADV_COLLAPSE) call will fail. As such, it is not guaranteed that the requested alignment will actually be abided by. For that reason, I thought it'd be useful to print this out to the user so they know requested alignments will be ignored, but it didn't seem severe enough of an issue to error out and exit altogether. But maybe it'd be less confusing for the user if this instead does just error out if the alignment isn't a multiple of the hugepage size.

Quoted 5 lines

BTW, it might also be nice to factor out this whole section of buffer initialization code (including original_buf) into an init_buffers() or some such. That could be done as a prep patch, but just a suggestion either way.

Good idea - i'll do this refactoring for v2.

Thanks,
Joanne

Quoted 32 lines

Brian

Quoted 28 lines
ltp/fsx.c +21 -1
+
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(100);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(101);
+             }
+     } else {
+             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);
+     }
      if (lite) {     /* zero entire existing file */
              ssize_t written;
 
--

2.47.1

#4 lore raw

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

Quoted 15 lines · Joanne Koong
Quoted 6 lines · Brian Foster
Quoted 4 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.

Quoted 7 lines
Quoted 2 lines

---

Firstly, thanks for taking the time to add this. This seems like a nice idea. It might be nice to have an extra sentence or two in the commit log on the purpose/motivation. For example, has this been used to detect a certain class of problem?

Quoted 7 lines

Hi Brian,

Thanks for reviewing this. That's a good idea - I'll include the sentence from the cover letter to this commit message as well: "This is motivated by a recent bug that was due to faulty handling for userspace buffers backed by hugepages."

Thanks. Got a link or anything, for my own curiosity?

Also, I presume the followup fstest is a reproducer?

Quoted 29 lines
Quoted 20 lines

A few other quick comments below...

Quoted 17 lines
 ltp/fsx.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 92 insertions(+), 8 deletions(-)
ltp/fsx.c +8 -0
index 41933354..3656fd9f 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
+}
+
+static void *
+init_hugepages_buf(unsigned len, long hugepage_size)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size);
+
Quoted 8 lines

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

Quoted 5 lines
ltp/fsx.c +5 -0
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
+             return NULL;
+     }
+     memset(buf, '\0', len);

I'm assuming it doesn't matter, but did you want to use buf_size here to clear the whole buffer?

Quoted 5 lines

I only saw buf being used up to len in the rest of the code so I didn't think it was necessary, but I also don't feel strongly about this and am happy to change this to clear the entire buffer if preferred.

Yeah.. at first it looked like a bug to me, then I realized the same thing later. I suspect it might be wise to just clear it entirely to avoid any future landmines, but that could just be my internal bias talking too. No big deal either way.

Quoted 43 lines
Quoted 38 lines
Quoted 23 lines
ltp/fsx.c +12 -6
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
@@ -3232,12 +3287,41 @@ main(int argc, char **argv)
      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);
+     if (hugepages) {
+             long hugepage_size;
+
+             hugepage_size = get_hugepage_size();

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

Quoted 10 lines
ltp/fsx.c +10 -0
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(99);
+             }
+
+             if (writebdy != 1 && writebdy != hugepage_size)
+                     prt("ignoring write alignment (since -h is enabled)");
+
+             if (readbdy != 1 && readbdy != hugepage_size)
+                     prt("ignoring read alignment (since -h is enabled)");

I'm a little unclear on what these warnings mean. The alignments are still used in the read/write paths afaics. The non-huge mode seems to only really care about the max size of the buffers in this code.

Quoted 4 lines

If your test doesn't actually use read/write alignments and the goal is just to keep things simple, perhaps it would be cleaner to add something like an if (hugepages && (writebdy != 1 || readbdy != 1)) check after option processing and exit out as an unsupported combination..?

Quoted 13 lines

My understanding of the 'writebdy' and 'readbdy' options are that they're for making reads/writes aligned to the passed-in value, which depends on the starting address of the buffer being aligned to that value as well. However for hugepages buffers, they must be aligned to the system hugepage size (eg 2 MiB) or the madvise(... MADV_COLLAPSE) call will fail. As such, it is not guaranteed that the requested alignment will actually be abided by. For that reason, I thought it'd be useful to print this out to the user so they know requested alignments will be ignored, but it didn't seem severe enough of an issue to error out and exit altogether. But maybe it'd be less confusing for the user if this instead does just error out if the alignment isn't a multiple of the hugepage size.

Ahh, I see. I missed the round_ptr_up() adjustments. That makes more sense now.

IMO it would be a little cleaner to just bail out earlier as such. But either way, I suppose if you could add a small comment with this alignment context you've explained above with the error checks then that is good enough for me. Thanks!

Brian

Quoted 44 lines
Quoted 5 lines

BTW, it might also be nice to factor out this whole section of buffer initialization code (including original_buf) into an init_buffers() or some such. That could be done as a prep patch, but just a suggestion either way.

Good idea - i'll do this refactoring for v2.

Thanks,
Joanne

Quoted 32 lines

Brian

Quoted 28 lines
ltp/fsx.c +21 -1
+
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(100);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(101);
+             }
+     } else {
+             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);
+     }
      if (lite) {     /* zero entire existing file */
              ssize_t written;
 
--

2.47.1

#5 lore raw

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

Quoted 25 lines · Brian Foster
Quoted 15 lines · Joanne Koong
Quoted 6 lines · Brian Foster
Quoted 4 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.

Quoted 7 lines
Quoted 2 lines

---

Firstly, thanks for taking the time to add this. This seems like a nice idea. It might be nice to have an extra sentence or two in the commit log on the purpose/motivation. For example, has this been used to detect a certain class of problem?

Quoted 7 lines

Hi Brian,

Thanks for reviewing this. That's a good idea - I'll include the sentence from the cover letter to this commit message as well: "This is motivated by a recent bug that was due to faulty handling for userspace buffers backed by hugepages."

Quoted 3 lines

Thanks. Got a link or anything, for my own curiosity?

Also, I presume the followup fstest is a reproducer?

This is the link to the bug and the ensuing discussion:
https://lore.kernel.org/linux-fsdevel/p3iss6hssbvtdutnwmuddvdadubrhfkdoosgmbewvo674f7f3y@cwnwffjqltzw/
. The tldr is that even if a filesystem does not support hugepages, it could still encounter hugepages in the direct io case for large folios backing userspace buffers. I missed this in commit 3b97c3652d91, which resulted in incorrect data being forwarded in fuse. There's currently no fstest checking against hugepages-backed userspace buffers, so this patchset is a followup for this case and would have caught the bug.

Quoted 36 lines
Quoted 30 lines
Quoted 21 lines

A few other quick comments below...

Quoted 17 lines
 ltp/fsx.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 92 insertions(+), 8 deletions(-)
ltp/fsx.c +8 -0
index 41933354..3656fd9f 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
+}
+
+static void *
+init_hugepages_buf(unsigned len, long hugepage_size)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size);
+
Quoted 8 lines

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

Quoted 5 lines
ltp/fsx.c +5 -0
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
+             return NULL;
+     }
+     memset(buf, '\0', len);

I'm assuming it doesn't matter, but did you want to use buf_size here to clear the whole buffer?

Quoted 5 lines

I only saw buf being used up to len in the rest of the code so I didn't think it was necessary, but I also don't feel strongly about this and am happy to change this to clear the entire buffer if preferred.

Quoted 5 lines

Yeah.. at first it looked like a bug to me, then I realized the same thing later. I suspect it might be wise to just clear it entirely to avoid any future landmines, but that could just be my internal bias talking too. No big deal either way.

Sounds great, I'll clear it entirely in v2.

Quoted 57 lines
Quoted 43 lines
Quoted 38 lines
Quoted 23 lines
ltp/fsx.c +12 -6
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
@@ -3232,12 +3287,41 @@ main(int argc, char **argv)
      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);
+     if (hugepages) {
+             long hugepage_size;
+
+             hugepage_size = get_hugepage_size();

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

Quoted 10 lines
ltp/fsx.c +10 -0
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(99);
+             }
+
+             if (writebdy != 1 && writebdy != hugepage_size)
+                     prt("ignoring write alignment (since -h is enabled)");
+
+             if (readbdy != 1 && readbdy != hugepage_size)
+                     prt("ignoring read alignment (since -h is enabled)");

I'm a little unclear on what these warnings mean. The alignments are still used in the read/write paths afaics. The non-huge mode seems to only really care about the max size of the buffers in this code.

Quoted 4 lines

If your test doesn't actually use read/write alignments and the goal is just to keep things simple, perhaps it would be cleaner to add something like an if (hugepages && (writebdy != 1 || readbdy != 1)) check after option processing and exit out as an unsupported combination..?

Quoted 13 lines

My understanding of the 'writebdy' and 'readbdy' options are that they're for making reads/writes aligned to the passed-in value, which depends on the starting address of the buffer being aligned to that value as well. However for hugepages buffers, they must be aligned to the system hugepage size (eg 2 MiB) or the madvise(... MADV_COLLAPSE) call will fail. As such, it is not guaranteed that the requested alignment will actually be abided by. For that reason, I thought it'd be useful to print this out to the user so they know requested alignments will be ignored, but it didn't seem severe enough of an issue to error out and exit altogether. But maybe it'd be less confusing for the user if this instead does just error out if the alignment isn't a multiple of the hugepage size.

Quoted 8 lines

Ahh, I see. I missed the round_ptr_up() adjustments. That makes more sense now.

IMO it would be a little cleaner to just bail out earlier as such. But either way, I suppose if you could add a small comment with this alignment context you've explained above with the error checks then that is good enough for me. Thanks!

Sounds great, will do for v2.

Thanks,
Joanne

Quoted 47 lines

Brian

Quoted 44 lines
Quoted 5 lines

BTW, it might also be nice to factor out this whole section of buffer initialization code (including original_buf) into an init_buffers() or some such. That could be done as a prep patch, but just a suggestion either way.

Good idea - i'll do this refactoring for v2.

Thanks,
Joanne

Quoted 32 lines

Brian

Quoted 28 lines
ltp/fsx.c +21 -1
+
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(100);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(101);
+             }
+     } else {
+             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);
+     }
      if (lite) {     /* zero entire existing file */
              ssize_t written;
 
--

2.47.1

#6 lore raw

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

In reply to Brian Foster
Quoted 126 lines · Brian Foster
Quoted 15 lines · Joanne Koong
Quoted 6 lines · Brian Foster
Quoted 4 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.

Quoted 7 lines
Quoted 2 lines

---

Firstly, thanks for taking the time to add this. This seems like a nice idea. It might be nice to have an extra sentence or two in the commit log on the purpose/motivation. For example, has this been used to detect a certain class of problem?

Quoted 7 lines

Hi Brian,

Thanks for reviewing this. That's a good idea - I'll include the sentence from the cover letter to this commit message as well: "This is motivated by a recent bug that was due to faulty handling for userspace buffers backed by hugepages."

Thanks. Got a link or anything, for my own curiosity?

Also, I presume the followup fstest is a reproducer?

Quoted 29 lines
Quoted 20 lines

A few other quick comments below...

Quoted 17 lines
 ltp/fsx.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 92 insertions(+), 8 deletions(-)
ltp/fsx.c +8 -0
index 41933354..3656fd9f 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
 int  aio = 0;
+}
+
+static void *
+init_hugepages_buf(unsigned len, long hugepage_size)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size);
+
Quoted 8 lines

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

Quoted 5 lines
ltp/fsx.c +5 -0
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
+             return NULL;
+     }
+     memset(buf, '\0', len);

I'm assuming it doesn't matter, but did you want to use buf_size here to clear the whole buffer?

Quoted 5 lines

I only saw buf being used up to len in the rest of the code so I didn't think it was necessary, but I also don't feel strongly about this and am happy to change this to clear the entire buffer if preferred.

Yeah.. at first it looked like a bug to me, then I realized the same thing later. I suspect it might be wise to just clear it entirely to avoid any future landmines, but that could just be my internal bias talking too. No big deal either way.

Quoted 43 lines
Quoted 38 lines
Quoted 23 lines
ltp/fsx.c +12 -6
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
@@ -3232,12 +3287,41 @@ main(int argc, char **argv)
      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);
+     if (hugepages) {
+             long hugepage_size;
+
+             hugepage_size = get_hugepage_size();

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

Quoted 10 lines
ltp/fsx.c +10 -0
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(99);
+             }
+
+             if (writebdy != 1 && writebdy != hugepage_size)
+                     prt("ignoring write alignment (since -h is enabled)");
+
+             if (readbdy != 1 && readbdy != hugepage_size)
+                     prt("ignoring read alignment (since -h is enabled)");

I'm a little unclear on what these warnings mean. The alignments are still used in the read/write paths afaics. The non-huge mode seems to only really care about the max size of the buffers in this code.

Quoted 4 lines

If your test doesn't actually use read/write alignments and the goal is just to keep things simple, perhaps it would be cleaner to add something like an if (hugepages && (writebdy != 1 || readbdy != 1)) check after option processing and exit out as an unsupported combination..?

Quoted 13 lines

My understanding of the 'writebdy' and 'readbdy' options are that they're for making reads/writes aligned to the passed-in value, which depends on the starting address of the buffer being aligned to that value as well. However for hugepages buffers, they must be aligned to the system hugepage size (eg 2 MiB) or the madvise(... MADV_COLLAPSE) call will fail. As such, it is not guaranteed that the requested alignment will actually be abided by. For that reason, I thought it'd be useful to print this out to the user so they know requested alignments will be ignored, but it didn't seem severe enough of an issue to error out and exit altogether. But maybe it'd be less confusing for the user if this instead does just error out if the alignment isn't a multiple of the hugepage size.

Quoted 2 lines

Ahh, I see. I missed the round_ptr_up() adjustments. That makes more sense now.

For v2, I'll be integrating writebdy and readbdy into the hugepages buffers. I mistakenly thought that "madvise(, ... MADV_COLLAPSE)" requires a buffer size that is aligned to the hugepage size, but AFAICT, it only requires that the buffer size is at least as large as the hugepage size.

Quoted 30 lines

IMO it would be a little cleaner to just bail out earlier as such. But either way, I suppose if you could add a small comment with this alignment context you've explained above with the error checks then that is good enough for me. Thanks!

Brian

Quoted 21 lines
Quoted 5 lines

BTW, it might also be nice to factor out this whole section of buffer initialization code (including original_buf) into an init_buffers() or some such. That could be done as a prep patch, but just a suggestion either way.

Good idea - i'll do this refactoring for v2.

Thanks,
Joanne

Quoted 9 lines

Brian

Quoted 5 lines
ltp/fsx.c +1 -1
+
--

2.47.1

#7 lore raw

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

In reply to Joanne Koong
Quoted 128 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.

---
 ltp/fsx.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 92 insertions(+), 8 deletions(-)
ltp/fsx.c +61 -8
index 41933354..3656fd9f 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,56 @@ keep_running(void)
 	return numops-- != 0;
 }
 
+static long
+get_hugepage_size(void)
+{
+	const char *str = "Hugepagesize:";
+	long hugepage_size = -1;
+	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, strlen(str)) == 0) {
+			sscanf(buffer + strlen(str), "%ld", &hugepage_size);
+			break;
+		}
+	}
+	fclose(file);
+	if (hugepage_size == -1) {
+		prterr("get_hugepage_size: failed to find "
+			"hugepage size in /proc/meminfo\n");
+		return -1;
+	}
+
+	/* convert from KiB to bytes  */
+	return hugepage_size * 1024;
+}
+
+static void *
+init_hugepages_buf(unsigned len, long hugepage_size)
+{
+	void *buf;
+	long buf_size = roundup(len, hugepage_size);
+
+	if (posix_memalign(&buf, hugepage_size, buf_size)) {
+		prterr("posix_memalign for buf");
+		return NULL;
+	}
+	memset(buf, '\0', len);
+	if (madvise(buf, buf_size, MADV_COLLAPSE)) {
+		prterr("madvise collapse for buf");
+		free(buf);
+		return NULL;
+	}
+
+	return buf;
+}
+
 static struct option longopts[] = {
 	{"replay-ops", required_argument, 0, 256},
 	{"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2935,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 +2968,9 @@ main(int argc, char **argv)
 		case 'g':
 			filldata = *optarg;
 			break;
+		case 'h':
+			hugepages = 1;
+			break;
 		case 'i':
 			integrity = 1;
 			logdev = strdup(optarg);
@@ -3232,12 +3287,41 @@ main(int argc, char **argv)
 	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);
+	if (hugepages) {
+		long hugepage_size;
+
+		hugepage_size = get_hugepage_size();

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

Quoted 10 lines
ltp/fsx.c +10 -0
+		if (hugepage_size == -1) {
+			prterr("get_hugepage_size()");
+			exit(99);
+		}
+
+		if (writebdy != 1 && writebdy != hugepage_size)
+			prt("ignoring write alignment (since -h is enabled)");
+
+		if (readbdy != 1 && readbdy != hugepage_size)
+			prt("ignoring read alignment (since -h is enabled)");

What if readbdy is a multiple of the hugepage size?

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

Quoted 5 lines
ltp/fsx.c +5 -0
+		good_buf = init_hugepages_buf(maxfilelen, hugepage_size);
+		if (!good_buf) {
+			prterr("init_hugepages_buf failed for good_buf");
+			exit(100);
+		}

Why is it necessary for the good_buf to be backed by a hugepage? I thought good_buf was only used to compare file contents?

--D

Quoted 22 lines
ltp/fsx.c +15 -0
+
+		temp_buf = init_hugepages_buf(maxoplen, hugepage_size);
+		if (!temp_buf) {
+			prterr("init_hugepages_buf failed for temp_buf");
+			exit(101);
+		}
+	} else {
+		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);
+	}
 	if (lite) {	/* zero entire existing file */
 		ssize_t written;
 
Signature · 1 line
2.47.1
#8 lore raw

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

Quoted 56 lines · Darrick J. Wong
Quoted 54 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.

---
 ltp/fsx.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 92 insertions(+), 8 deletions(-)
ltp/fsx.c +13 -7
index 41933354..3656fd9f 100644
+
 static struct option longopts[] = {
      {"replay-ops", required_argument, 0, 256},
      {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2935,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 +2968,9 @@ main(int argc, char **argv)
              case 'g':
                      filldata = *optarg;
                      break;
+             case 'h':
+                     hugepages = 1;
+                     break;
              case 'i':
                      integrity = 1;
                      logdev = strdup(optarg);
@@ -3232,12 +3287,41 @@ main(int argc, char **argv)
      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);
+     if (hugepages) {
+             long hugepage_size;
+
+             hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(99);
+             }
Quoted 8 lines

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

Quoted 6 lines
ltp/fsx.c +6 -0
+
+             if (writebdy != 1 && writebdy != hugepage_size)
+                     prt("ignoring write alignment (since -h is enabled)");
+
+             if (readbdy != 1 && readbdy != hugepage_size)
+                     prt("ignoring read alignment (since -h is enabled)");

What if readbdy is a multiple of the hugepage size?

Good point, the user could potentially request an alignment that's a multiple. I'll account for this in v2.

Quoted 9 lines

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

Quoted 6 lines
ltp/fsx.c +5 -0
+             good_buf = init_hugepages_buf(maxfilelen, hugepage_size);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for good_buf");
+                     exit(100);
+             }

Why is it necessary for the good_buf to be backed by a hugepage? I thought good_buf was only used to compare file contents?

good_buf is used too as the source buffer for the write in dowrite().

Thanks,
Joanne

Quoted 25 lines

--D

Quoted 22 lines
ltp/fsx.c +15 -1
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for temp_buf");
+                     exit(101);
+             }
+     } else {
+             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);
+     }
      if (lite) {     /* zero entire existing file */
              ssize_t written;
 
--

2.47.1

#9 lore raw

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

In reply to Joanne Koong
Quoted 52 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.

---
 ltp/fsx.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++---
--
 1 file changed, 92 insertions(+), 8 deletions(-)
ltp/fsx.c +3 -1
index 41933354..3656fd9f 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\
ltp/fsx.c +0 -0
 	-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\

ltp/fsx.c +5 -0
@@ -2856,6 +2858,56 @@ keep_running(void)
 	return numops-- != 0;
 }
 
+static long
+get_hugepage_size(void)
+{
+	const char *str = "Hugepagesize:";
+	long hugepage_size = -1;

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

Quoted 10 lines
ltp/fsx.c +10 -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, strlen(str)) == 0) {

Extremely minor: Since str is a fixed string, why not calculate the length outside the loop and not re-use strlen(str) multiple times?

Quoted 4 lines
ltp/fsx.c +3 -0
+			sscanf(buffer + strlen(str), "%ld",
 &hugepage_size);
+			break;
+		}

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

Quoted 10 lines
ltp/fsx.c +10 -0
+	}
+	fclose(file);
+	if (hugepage_size == -1) {
+		prterr("get_hugepage_size: failed to find "
+			"hugepage size in /proc/meminfo\n");
+		return -1;
+	}
+
+	/* convert from KiB to bytes  */
+	return hugepage_size * 1024;

Minor: << 10 might be faster instead of '*' ?

Quoted 86 lines
ltp/fsx.c +22 -0
+}
+
+static void *
+init_hugepages_buf(unsigned len, long hugepage_size)
+{
+	void *buf;
+	long buf_size = roundup(len, hugepage_size);
+
+	if (posix_memalign(&buf, hugepage_size, buf_size)) {
+		prterr("posix_memalign for buf");
+		return NULL;
+	}
+	memset(buf, '\0', len);
+	if (madvise(buf, buf_size, MADV_COLLAPSE)) {
+		prterr("madvise collapse for buf");
+		free(buf);
+		return NULL;
+	}
+
+	return buf;
+}
+
 static struct option longopts[] = {
 	{"replay-ops", required_argument, 0, 256},
ltp/fsx.c +1 -1
 	{"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2935,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:xy
 ABD:EFJKHzCILN:OP:RS:UWXZ",
+				 "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:x
 yABD:EFJKHzCILN:OP:RS:UWXZ",
ltp/fsx.c +21 -6
 				 longopts, NULL)) != EOF)
 		switch (ch) {
 		case 'b':
@@ -2916,6 +2968,9 @@ main(int argc, char **argv)
 		case 'g':
 			filldata = *optarg;
 			break;
+		case 'h':
+			hugepages = 1;
+			break;
 		case 'i':
 			integrity = 1;
 			logdev = strdup(optarg);
@@ -3232,12 +3287,41 @@ main(int argc, char **argv)
 	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);
+	if (hugepages) {
+		long hugepage_size;
+
+		hugepage_size = get_hugepage_size();
+		if (hugepage_size == -1) {
+			prterr("get_hugepage_size()");
+			exit(99);
+		}
+
+		if (writebdy != 1 && writebdy != hugepage_size)
+			prt("ignoring write alignment (since -h is
 enabled)");
+
+		if (readbdy != 1 && readbdy != hugepage_size)
+			prt("ignoring read alignment (since -h is
 enabled)");
+
+		good_buf = init_hugepages_buf(maxfilelen,
 hugepage_size);
+		if (!good_buf) {
+			prterr("init_hugepages_buf failed for

good_buf");

ltp/fsx.c +6 -0
+			exit(100);
+		}
+
+		temp_buf = init_hugepages_buf(maxoplen, hugepage_size);
+		if (!temp_buf) {
+			prterr("init_hugepages_buf failed for

temp_buf");

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

Quoted 5 lines
ltp/fsx.c +5 -0
+			exit(101);
+		}
+	} else {
+		good_buf = (char *) malloc(maxfilelen + writebdy);
+		good_buf = round_ptr_up(good_buf, writebdy, 0);

Not sure if it would matter but aren't we seeing a small memory leak here since good_buf's original will be lost after rounding up?

Quoted 9 lines
ltp/fsx.c +6 -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);
+	}
 	if (lite) {	/* zero entire existing file */
 		ssize_t written;
#10 lore raw

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

Quoted 27 lines · Nirjhar Roy
Quoted 25 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.

---
 ltp/fsx.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++---
--
 1 file changed, 92 insertions(+), 8 deletions(-)
ltp/fsx.c +9 -0
index 41933354..3656fd9f 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
+static long
+get_hugepage_size(void)
+{
+     const char *str = "Hugepagesize:";
+     long hugepage_size = -1;
+     char buffer[64];
+     FILE *file;
+
+     file = fopen("/proc/meminfo", "r");
Quoted 8 lines

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

Quoted 6 lines
ltp/fsx.c +6 -0
+     if (!file) {
+             prterr("get_hugepage_size: fopen /proc/meminfo");
+             return -1;
+     }
+     while (fgets(buffer, sizeof(buffer), file)) {
+             if (strncmp(buffer, str, strlen(str)) == 0) {

Extremely minor: Since str is a fixed string, why not calculate the length outside the loop and not re-use strlen(str) multiple times?

Thinking about this some more, maybe it'd be best to define it as const char str[] = "Hugepagesize:" as an array of chars and use sizeof which would be at compile-time instead of runtime.
I'll do this for v2.

Quoted 14 lines
Quoted 4 lines

+ sscanf(buffer + strlen(str), "%ld",
&hugepage_size);

ltp/fsx.c +2 -0
+                     break;
+             }

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

Quoted 10 lines
ltp/fsx.c +10 -0
+     }
+     fclose(file);
+     if (hugepage_size == -1) {
+             prterr("get_hugepage_size: failed to find "
+                     "hugepage size in /proc/meminfo\n");
+             return -1;
+     }
+
+     /* convert from KiB to bytes  */
+     return hugepage_size * 1024;
Quoted 1 line

Minor: << 10 might be faster instead of '*' ?

Will do for v2.

Quoted 85 lines
Quoted 85 lines
ltp/fsx.c +23 -1
+}
+
+static void *
+init_hugepages_buf(unsigned len, long hugepage_size)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size);
+
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
+             return NULL;
+     }
+     memset(buf, '\0', len);
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
+
 static struct option longopts[] = {
      {"replay-ops", required_argument, 0, 256},
      {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2935,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:xy
 ABD:EFJKHzCILN:OP:RS:UWXZ",
+                              "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:x
 yABD:EFJKHzCILN:OP:RS:UWXZ",
                               longopts, NULL)) != EOF)
              switch (ch) {
              case 'b':
ltp/fsx.c +21 -6
@@ -2916,6 +2968,9 @@ main(int argc, char **argv)
              case 'g':
                      filldata = *optarg;
                      break;
+             case 'h':
+                     hugepages = 1;
+                     break;
              case 'i':
                      integrity = 1;
                      logdev = strdup(optarg);
@@ -3232,12 +3287,41 @@ main(int argc, char **argv)
      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);
+     if (hugepages) {
+             long hugepage_size;
+
+             hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(99);
+             }
+
+             if (writebdy != 1 && writebdy != hugepage_size)
+                     prt("ignoring write alignment (since -h is
 enabled)");
+
+             if (readbdy != 1 && readbdy != hugepage_size)
+                     prt("ignoring read alignment (since -h is
 enabled)");
+
+             good_buf = init_hugepages_buf(maxfilelen,
 hugepage_size);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for

good_buf");

ltp/fsx.c +6 -0
+                     exit(100);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for
Quoted 8 lines

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

Quoted 6 lines

temp_buf");

ltp/fsx.c +5 -0
+                     exit(101);
+             }
+     } else {
+             good_buf = (char *) malloc(maxfilelen + writebdy);
+             good_buf = round_ptr_up(good_buf, writebdy, 0);

Not sure if it would matter but aren't we seeing a small memory leak here since good_buf's original will be lost after rounding up?

This is inherited from the original code but AFAICT, it relies on the memory being cleaned up at exit time (eg free() is never called on good_buf and temp_buf either).

Thanks,
Joanne

Quoted 10 lines
Quoted 9 lines
ltp/fsx.c +6 -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);
+     }
      if (lite) {     /* zero entire existing file */
              ssize_t written;
#11 lore raw

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

Quoted 35 lines · Joanne Koong
Quoted 26 lines · Nirjhar Roy
Quoted 25 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.

---
  ltp/fsx.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++---
--
  1 file changed, 92 insertions(+), 8 deletions(-)
ltp/fsx.c +9 -0
index 41933354..3656fd9f 100644
@@ -190,6 +190,7 @@ int       o_direct;                       /* -Z */
+static long
+get_hugepage_size(void)
+{
+     const char *str = "Hugepagesize:";
+     long hugepage_size = -1;
+     char buffer[64];
+     FILE *file;
+
+     file = fopen("/proc/meminfo", "r");
Quoted 8 lines

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

Quoted 6 lines
ltp/fsx.c +6 -0
+     if (!file) {
+             prterr("get_hugepage_size: fopen /proc/meminfo");
+             return -1;
+     }
+     while (fgets(buffer, sizeof(buffer), file)) {
+             if (strncmp(buffer, str, strlen(str)) == 0) {

Extremely minor: Since str is a fixed string, why not calculate the length outside the loop and not re-use strlen(str) multiple times?

Quoted 4 lines

Thinking about this some more, maybe it'd be best to define it as const char str[] = "Hugepagesize:" as an array of chars and use sizeof which would be at compile-time instead of runtime.
I'll do this for v2.

Yes, that is a good idea too. Thanks.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

+ sscanf(buffer + strlen(str), "%ld",
&hugepage_size);

ltp/fsx.c +12 -0
+                     break;
+             }
+     }
+     fclose(file);
+     if (hugepage_size == -1) {
+             prterr("get_hugepage_size: failed to find "
+                     "hugepage size in /proc/meminfo\n");
+             return -1;
+     }
+
+     /* convert from KiB to bytes  */
+     return hugepage_size * 1024;
Quoted 2 lines
Quoted 1 line

Minor: << 10 might be faster instead of '*' ?

Will do for v2.

Thanks.

Quoted 94 lines
Quoted 86 lines
Quoted 86 lines
ltp/fsx.c +23 -1
+}
+
+static void *
+init_hugepages_buf(unsigned len, long hugepage_size)
+{
+     void *buf;
+     long buf_size = roundup(len, hugepage_size);
+
+     if (posix_memalign(&buf, hugepage_size, buf_size)) {
+             prterr("posix_memalign for buf");
+             return NULL;
+     }
+     memset(buf, '\0', len);
+     if (madvise(buf, buf_size, MADV_COLLAPSE)) {
+             prterr("madvise collapse for buf");
+             free(buf);
+             return NULL;
+     }
+
+     return buf;
+}
+
  static struct option longopts[] = {
       {"replay-ops", required_argument, 0, 256},
       {"record-ops", optional_argument, 0, 255},
@@ -2883,7 +2935,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:xy
 ABD:EFJKHzCILN:OP:RS:UWXZ",
+                              "0b:c:de:fg:hi:j:kl:m:no:p:qr:s:t:uw:x
 yABD:EFJKHzCILN:OP:RS:UWXZ",
                                longopts, NULL)) != EOF)
               switch (ch) {
               case 'b':
ltp/fsx.c +21 -6
@@ -2916,6 +2968,9 @@ main(int argc, char **argv)
               case 'g':
                       filldata = *optarg;
                       break;
+             case 'h':
+                     hugepages = 1;
+                     break;
               case 'i':
                       integrity = 1;
                       logdev = strdup(optarg);
@@ -3232,12 +3287,41 @@ main(int argc, char **argv)
       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);
+     if (hugepages) {
+             long hugepage_size;
+
+             hugepage_size = get_hugepage_size();
+             if (hugepage_size == -1) {
+                     prterr("get_hugepage_size()");
+                     exit(99);
+             }
+
+             if (writebdy != 1 && writebdy != hugepage_size)
+                     prt("ignoring write alignment (since -h is
 enabled)");
+
+             if (readbdy != 1 && readbdy != hugepage_size)
+                     prt("ignoring read alignment (since -h is
 enabled)");
+
+             good_buf = init_hugepages_buf(maxfilelen,
 hugepage_size);
+             if (!good_buf) {
+                     prterr("init_hugepages_buf failed for

good_buf");

ltp/fsx.c +6 -0
+                     exit(100);
+             }
+
+             temp_buf = init_hugepages_buf(maxoplen, hugepage_size);
+             if (!temp_buf) {
+                     prterr("init_hugepages_buf failed for
Quoted 8 lines

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

Quoted 6 lines

temp_buf");

ltp/fsx.c +5 -0
+                     exit(101);
+             }
+     } else {
+             good_buf = (char *) malloc(maxfilelen + writebdy);
+             good_buf = round_ptr_up(good_buf, writebdy, 0);

Not sure if it would matter but aren't we seeing a small memory leak here since good_buf's original will be lost after rounding up?

Quoted 3 lines

This is inherited from the original code but AFAICT, it relies on the memory being cleaned up at exit time (eg free() is never called on good_buf and temp_buf either).

Okay, makes sense.

--

NR

Quoted 14 lines

Thanks,
Joanne

Quoted 9 lines
Quoted 9 lines
ltp/fsx.c +6 -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);
+     }
       if (lite) {     /* zero entire existing file */
               ssize_t written;
Signature · 4 lines
---
Nirjhar Roy
Linux Kernel Developer
IBM, Bangalore
#12 lore raw

PATCH 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             | 10 ++++++++++
 tests/generic/758     | 23 +++++++++++++++++++++++
 tests/generic/758.out |  4 ++++
 tests/generic/759     | 24 ++++++++++++++++++++++++
 tests/generic/759.out |  4 ++++
 5 files changed, 65 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 +10 -0
index 1b2e4508..33af7fa7 100644
@@ -3016,6 +3016,16 @@ _require_xfs_io_command()
 	fi
 }
 
+# check that the kernel and system supports huge pages
+_require_thp()
+{
+    thp_status=$(cat /sys/kernel/mm/transparent_hugepage/enabled)
+    if [[ $thp_status == *"[never]"* ]]; then
+	    _notrun "system doesn't support transparent hugepages"
+    fi
+    _require_kernel_config CONFIG_TRANSPARENT_HUGEPAGE
+}
+
 # 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 +23 -0
new file mode 100755
index 00000000..b3bd6e5b
@@ -0,0 +1,23 @@
+#! /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
+
+# Import common functions.
+. ./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 +24 -0
new file mode 100755
index 00000000..6dfe2b86
@@ -0,0 +1,24 @@
+#! /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
+
+# Import common functions.
+. ./common/filter
+
+_require_test
+_require_odirect
+_require_thp
+
+run_fsx -N 10000            -l 500000 -Z -R -W -h
+run_fsx -N 10000  -o 8192   -l 500000 -Z -R -W -h
+run_fsx -N 10000  -o 128000 -l 500000 -Z -R -W -h
+
+status=0
+exit
tests/generic/759.out new file +4 -0
new file mode 100644
index 00000000..18d21229
@@ -0,0 +1,4 @@
+QA output created by 759
+fsx -N 10000 -l 500000 -Z -R -W -h
+fsx -N 10000 -o 8192 -l 500000 -Z -R -W -h
+fsx -N 10000 -o 128000 -l 500000 -Z -R -W -h
Signature · 1 line
2.47.1
#13 lore raw

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

Quoted 22 lines · Joanne Koong

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

---
 common/rc             | 10 ++++++++++
 tests/generic/758     | 23 +++++++++++++++++++++++
 tests/generic/758.out |  4 ++++
 tests/generic/759     | 24 ++++++++++++++++++++++++
 tests/generic/759.out |  4 ++++
 5 files changed, 65 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 +0 -0
index 1b2e4508..33af7fa7 100644
@@ -3016,6 +3016,16 @@ _require_xfs_io_command()
 	fi

common/rc _require_xfs_io_command() · line 3026

Quoted 10 lines
common/rc +8 -0
 }
 
+# check that the kernel and system supports huge pages
+_require_thp()
+{
+    thp_status=$(cat /sys/kernel/mm/transparent_hugepage/enabled)
+    if [[ $thp_status == *"[never]"* ]]; then
+	    _notrun "system doesn't support transparent hugepages"
+    fi
+    _require_kernel_config CONFIG_TRANSPARENT_HUGEPAGE

Will /sys/kernel/mm/transparent_hugepage/ exist if CONFIG_TRANSPARENT_HUGEPAGE=n ?

Or put another way, if /sys/kernel/mm/transparent_hugepage/ doesn't exist, is that a sign that hugepages aren't enabled?

Quoted 13 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 +2 -0
new file mode 100755
index 00000000..b3bd6e5b
@@ -0,0 +1,23 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0

tests/generic/758 line 12

Quoted 10 lines
tests/generic/758 new file +10 -0
+#
+# FS QA Test No. 758
+#
+# fsx exercising reads/writes from userspace buffers
+# backed by hugepages
+#
+. ./common/preamble
+_begin_fstest rw auto quick
+
+# Import common functions.

No need for ^^ this comment. Same with 759.

--D

Quoted 65 lines
tests/generic/758 new file +11 -0
+. ./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 +24 -0
new file mode 100755
index 00000000..6dfe2b86
@@ -0,0 +1,24 @@
+#! /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
+
+# Import common functions.
+. ./common/filter
+
+_require_test
+_require_odirect
+_require_thp
+
+run_fsx -N 10000            -l 500000 -Z -R -W -h
+run_fsx -N 10000  -o 8192   -l 500000 -Z -R -W -h
+run_fsx -N 10000  -o 128000 -l 500000 -Z -R -W -h
+
+status=0
+exit
tests/generic/759.out new file +4 -0
new file mode 100644
index 00000000..18d21229
@@ -0,0 +1,4 @@
+QA output created by 759
+fsx -N 10000 -l 500000 -Z -R -W -h
+fsx -N 10000 -o 8192 -l 500000 -Z -R -W -h
+fsx -N 10000 -o 128000 -l 500000 -Z -R -W -h
Signature · 1 line
2.47.1
#14 lore raw

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

Quoted 38 lines · Darrick J. Wong
Quoted 22 lines · Joanne Koong

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

---
 common/rc             | 10 ++++++++++
 tests/generic/758     | 23 +++++++++++++++++++++++
 tests/generic/758.out |  4 ++++
 tests/generic/759     | 24 ++++++++++++++++++++++++
 tests/generic/759.out |  4 ++++
 5 files changed, 65 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 +0 -0
index 1b2e4508..33af7fa7 100644
@@ -3016,6 +3016,16 @@ _require_xfs_io_command()
      fi

common/rc _require_xfs_io_command() · line 3026

Quoted 10 lines
common/rc +8 -0
 }
 
+# check that the kernel and system supports huge pages
+_require_thp()
+{
+    thp_status=$(cat /sys/kernel/mm/transparent_hugepage/enabled)
+    if [[ $thp_status == *"[never]"* ]]; then
+         _notrun "system doesn't support transparent hugepages"
+    fi
+    _require_kernel_config CONFIG_TRANSPARENT_HUGEPAGE

Will /sys/kernel/mm/transparent_hugepage/ exist if CONFIG_TRANSPARENT_HUGEPAGE=n ?

Quoted 2 lines

Or put another way, if /sys/kernel/mm/transparent_hugepage/ doesn't exist, is that a sign that hugepages aren't enabled?

I just checked the logic in hugepage_init() and it looks like the sysfs initialization is gated by a has_transparent_hugepage() check against that config, so no, ' /sys/kernel/mm/transparent_hugepage/' won't be there if that config is set to n. Thanks for noting, I'll revise this check.

Quoted 27 lines
Quoted 14 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 +2 -0
new file mode 100755
index 00000000..b3bd6e5b
@@ -0,0 +1,23 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0

tests/generic/758 line 12

Quoted 10 lines
tests/generic/758 new file +10 -0
+#
+# FS QA Test No. 758
+#
+# fsx exercising reads/writes from userspace buffers
+# backed by hugepages
+#
+. ./common/preamble
+_begin_fstest rw auto quick
+
+# Import common functions.

No need for ^^ this comment. Same with 759.

Quoted 1 line

--D

Will remove these comments in v2.

Thanks,
Joanne

Quoted 66 lines
Quoted 66 lines
tests/generic/758 new file +11 -0
+. ./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 +24 -0
new file mode 100755
index 00000000..6dfe2b86
@@ -0,0 +1,24 @@
+#! /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
+
+# Import common functions.
+. ./common/filter
+
+_require_test
+_require_odirect
+_require_thp
+
+run_fsx -N 10000            -l 500000 -Z -R -W -h
+run_fsx -N 10000  -o 8192   -l 500000 -Z -R -W -h
+run_fsx -N 10000  -o 128000 -l 500000 -Z -R -W -h
+
+status=0
+exit
tests/generic/759.out new file +4 -1
new file mode 100644
index 00000000..18d21229
@@ -0,0 +1,4 @@
+QA output created by 759
+fsx -N 10000 -l 500000 -Z -R -W -h
+fsx -N 10000 -o 8192 -l 500000 -Z -R -W -h
+fsx -N 10000 -o 128000 -l 500000 -Z -R -W -h
--

2.47.1