Skip to contentSkip to conversation

selftests/mm: check file initialization writes in split_huge_page_test

PATCH In review Vineet Agarwal 4 participants 7 messages May 12 – May 20

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/mm: check file initialization writes in split_huge_page_test

create_pagecache_thp_and_fd() fills the backing file for the pagecache THP tests using repeated write() calls, but the return value is never checked.

If a write fails or completes only partially, the test may continue with an incompletely initialized file and produce misleading results.

Check the result of write() and fail the test if the expected number of bytes was not written.

---
 tools/testing/selftests/mm/split_huge_page_test.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +10 -3
index 500d07c4938b..eab69b0f59a0 100644
@@ -609,9 +609,16 @@ static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size,
 	assert(fd_size % sizeof(buf) == 0);
 	for (i = 0; i < sizeof(buf); i++)
 		buf[i] = (unsigned char)i;
-	for (i = 0; i < fd_size; i += sizeof(buf))
-		write(*fd, buf, sizeof(buf));
-
+	for (i = 0; i < fd_size; i += sizeof(buf)) {
+		ssize_t written;
+
+		written = write(*fd, buf, sizeof(buf));
+		if (written != sizeof(buf)) {
+			ksft_perror("write testfile");
+			close(*fd);
+			goto err_out_unlink;
+		}
+	}
 	close(*fd);
 	sync();
 	*fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
Signature · 1 line
2.54.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 20 lines · Vineet Agarwal

create_pagecache_thp_and_fd() fills the backing file for the pagecache THP tests using repeated write() calls, but the return value is never checked.

If a write fails or completes only partially, the test may continue with an incompletely initialized file and produce misleading results.

Check the result of write() and fail the test if the expected number of bytes was not written.

---
 tools/testing/selftests/mm/split_huge_page_test.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +0 -0
index 500d07c4938b..eab69b0f59a0 100644

tools/testing/selftests/mm/split_huge_page_test.c static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, · line 613

Quoted 8 lines
tools/testing/selftests/mm/split_huge_page_test.c +2 -3
@@ -609,9 +609,16 @@ static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size,
 	assert(fd_size % sizeof(buf) == 0);
 	for (i = 0; i < sizeof(buf); i++)
 		buf[i] = (unsigned char)i;
-	for (i = 0; i < fd_size; i += sizeof(buf))
-		write(*fd, buf, sizeof(buf));
-
+	for (i = 0; i < fd_size; i += sizeof(buf)) {
+		ssize_t written;

Do you really need the temporary variable?

tools/testing/selftests/mm/split_huge_page_test.c static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, · line 624

Quoted 11 lines
tools/testing/selftests/mm/split_huge_page_test.c +8 -0
+
+		written = write(*fd, buf, sizeof(buf));
+		if (written != sizeof(buf)) {
+			ksft_perror("write testfile");
+			close(*fd);
+			goto err_out_unlink;
+		}
+	}
 	close(*fd);
 	sync();
 	*fd = open("/proc/sys/vm/drop_caches", O_WRONLY);

Apart from that LGTM

Signature · 3 lines
Cheers,

David
#2 lore raw
In reply to Vineet Agarwal
Quoted 24 lines · Vineet Agarwal

create_pagecache_thp_and_fd() fills the backing file for the pagecache THP tests using repeated write() calls, but the return value is never checked.

If a write fails or completes only partially, the test may continue with an incompletely initialized file and produce misleading results.

Check the result of write() and fail the test if the expected number of bytes was not written.

---
tools/testing/selftests/mm/split_huge_page_test.c | 13 ++++++++++---

1 file changed, 10 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +0 -1
index 500d07c4938b..eab69b0f59a0 100644
@@ -609,9 +609,16 @@ static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size,
 	assert(fd_size % sizeof(buf) == 0);
 	for (i = 0; i < sizeof(buf); i++)
 		buf[i] = (unsigned char)i;
-	for (i = 0; i < fd_size; i += sizeof(buf))

tools/testing/selftests/mm/split_huge_page_test.c static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, · line 619

Quoted 10 lines
tools/testing/selftests/mm/split_huge_page_test.c +8 -2
-		write(*fd, buf, sizeof(buf));
-
+	for (i = 0; i < fd_size; i += sizeof(buf)) {
+		ssize_t written;
+
+		written = write(*fd, buf, sizeof(buf));
+		if (written != sizeof(buf)) {
+			ksft_perror("write testfile");
+			close(*fd);
+			goto err_out_unlink;

Maybe we can "goto err_out_close" and remove the close() here?

Apart from this, I found on error writing to /proc/sys/vm/drop_caches in below, it just goto err_out_unlink, which left fd open.

How about fix that at the same time.

Generally, the change look good.

Quoted 8 lines
tools/testing/selftests/mm/split_huge_page_test.c +2 -0
+		}
+	}
 	close(*fd);
 	sync();
 	*fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
Signature · 1 line
2.54.0
Signature · 2 lines
Wei Yang
Help you, Help me
maintainer
#3 lore raw
Quoted 29 lines · On Fri, 15 May 2026 09:22:11 +0000 Wei Yang <[email protected]> wrote:
Quoted 28 lines · Vineet Agarwal

create_pagecache_thp_and_fd() fills the backing file for the pagecache THP tests using repeated write() calls, but the return value is never checked.

If a write fails or completes only partially, the test may continue with an incompletely initialized file and produce misleading results.

Check the result of write() and fail the test if the expected number of bytes was not written.

---
tools/testing/selftests/mm/split_huge_page_test.c | 13 ++++++++++---

1 file changed, 10 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +2 -3
index 500d07c4938b..eab69b0f59a0 100644
@@ -609,9 +609,16 @@ static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size,
 	assert(fd_size % sizeof(buf) == 0);
 	for (i = 0; i < sizeof(buf); i++)
 		buf[i] = (unsigned char)i;
-	for (i = 0; i < fd_size; i += sizeof(buf))
-		write(*fd, buf, sizeof(buf));
-
+	for (i = 0; i < fd_size; i += sizeof(buf)) {
+		ssize_t written;
Quoted 8 lines

tools/testing/selftests/mm/split_huge_page_test.c static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, · line 619

Quoted 6 lines
tools/testing/selftests/mm/split_huge_page_test.c +6 -0
+
+		written = write(*fd, buf, sizeof(buf));
+		if (written != sizeof(buf)) {
+			ksft_perror("write testfile");
+			close(*fd);
+			goto err_out_unlink;

Maybe we can "goto err_out_close" and remove the close() here?

AI review suggested the same:

	https://sashiko.dev/#/patchset/20260512074924.27721-1-agarwal.vineet2006%40gmail.com

And it complained about the handling of short writes in the added code.

Quoted 2 lines

Apart from this, I found on error writing to /proc/sys/vm/drop_caches in below, it just goto err_out_unlink, which left fd open.

Not understanding - cab you please describe more completely?

Quoted 4 lines

How about fix that at the same time.

Generally, the change look good.

#4 lore raw
Quoted 41 lines · Andrew Morton
Quoted 29 lines · On Fri, 15 May 2026 09:22:11 +0000 Wei Yang <[email protected]> wrote:
Quoted 28 lines · Vineet Agarwal

create_pagecache_thp_and_fd() fills the backing file for the pagecache THP tests using repeated write() calls, but the return value is never checked.

If a write fails or completes only partially, the test may continue with an incompletely initialized file and produce misleading results.

Check the result of write() and fail the test if the expected number of bytes was not written.

---
tools/testing/selftests/mm/split_huge_page_test.c | 13 ++++++++++---

1 file changed, 10 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/split_huge_page_test.c +2 -3
index 500d07c4938b..eab69b0f59a0 100644
@@ -609,9 +609,16 @@ static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size,
 	assert(fd_size % sizeof(buf) == 0);
 	for (i = 0; i < sizeof(buf); i++)
 		buf[i] = (unsigned char)i;
-	for (i = 0; i < fd_size; i += sizeof(buf))
-		write(*fd, buf, sizeof(buf));
-
+	for (i = 0; i < fd_size; i += sizeof(buf)) {
+		ssize_t written;
Quoted 8 lines

tools/testing/selftests/mm/split_huge_page_test.c static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, · line 619

Quoted 6 lines
tools/testing/selftests/mm/split_huge_page_test.c +6 -0
+
+		written = write(*fd, buf, sizeof(buf));
+		if (written != sizeof(buf)) {
+			ksft_perror("write testfile");
+			close(*fd);
+			goto err_out_unlink;

Maybe we can "goto err_out_close" and remove the close() here?

AI review suggested the same:

Quoted 9 lines
	https://sashiko.dev/#/patchset/20260512074924.27721-1-agarwal.vineet2006%40gmail.com

And it complained about the handling of short writes in the added code.

Quoted 2 lines

Apart from this, I found on error writing to /proc/sys/vm/drop_caches in below, it just goto err_out_unlink, which left fd open.

Not understanding - cab you please describe more completely?

I mean do this below.

tools/testing/selftests/mm/split_huge_page_test.c +1 -1
index 460fa1f606fd..f30402ece608 100644
@@ -469,7 +469,7 @@ static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size,
        }
        if (write(*fd, "3", 1) != 1) {
                ksft_perror("write to drop_caches");
-               goto err_out_unlink;
+               goto err_out_close;
        }
        close(*fd);
 

On error writing this fd, /proc/sys/vm/drop_caches, we forget to close it.

Quoted 4 lines
Quoted 4 lines

How about fix that at the same time.

Generally, the change look good.

Signature · 2 lines
Wei Yang
Help you, Help me
maintainer
#5 lore raw
Quoted 18 lines · On Tue, 19 May 2026 02:51:41 +0000 Wei Yang <[email protected]> wrote:
Quoted 2 lines

Not understanding - cab you please describe more completely?

I mean do this below.

tools/testing/selftests/mm/split_huge_page_test.c +1 -1
index 460fa1f606fd..f30402ece608 100644
@@ -469,7 +469,7 @@ static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size,
        }
        if (write(*fd, "3", 1) != 1) {
                ksft_perror("write to drop_caches");
-               goto err_out_unlink;
+               goto err_out_close;
        }
        close(*fd);
 
Quoted 1 line

On error writing this fd, /proc/sys/vm/drop_caches, we forget to close it.

Oh, OK, that's a buglet. Please send a formal fix sometime?

#6 lore raw
Quoted 20 lines · Andrew Morton
Quoted 18 lines · On Tue, 19 May 2026 02:51:41 +0000 Wei Yang <[email protected]> wrote:
Quoted 2 lines

Not understanding - cab you please describe more completely?

I mean do this below.

tools/testing/selftests/mm/split_huge_page_test.c +1 -1
index 460fa1f606fd..f30402ece608 100644
@@ -469,7 +469,7 @@ static int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size,
        }
        if (write(*fd, "3", 1) != 1) {
                ksft_perror("write to drop_caches");
-               goto err_out_unlink;
+               goto err_out_close;
        }
        close(*fd);
 
Quoted 3 lines
Quoted 1 line

On error writing this fd, /proc/sys/vm/drop_caches, we forget to close it.

Oh, OK, that's a buglet. Please send a formal fix sometime?

Sure, I am glad to.

Signature · 2 lines
Wei Yang
Help you, Help me