[PATCH v2] selftests/mm: ksm-functional-tests: fix partial write handling

Vineet Agarwal
2026-05-03T06:02:55Z

Update write() checks to properly detect and handle partial writes.

Previously, partial writes (ret > 0 && ret != len) would return
-errno, but write() does not set errno in this case. This could
result in returning 0 and incorrectly signaling success.

Fix this by:
- returning -errno only on actual failures (ret < 0)
- returning -EIO when a partial write is detected

This ensures partial writes are treated as errors and prevents
false success reporting in tests.

Signed-off-by: Vineet Agarwal <agarwal.vineet2006@gmail.com>

Changes in v2:
- Fix incorrect use of -errno on partial writes
- Return -EIO when write() completes partially
---
tools/testing/selftests/mm/ksm_functional_tests.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
index 9a8c852acd68..c80254cda926 100644
--- a/tools/testing/selftests/mm/ksm_functional_tests.c
+++ b/tools/testing/selftests/mm/ksm_functional_tests.c
@@ -507,9 +507,13 @@ static int start_ksmd_and_set_frequency(char *pages_to_scan, char *sleep_ms)
if (write(ksm_fd, "1", 1) != 1)
return -errno;

- ret = write(pages_to_scan_fd, pages_to_scan, strlen(pages_to_scan));
- if (ret < 0 || ret != strlen(pages_to_scan))
+ ssize_t len = strlen(pages_to_scan);
+
+ ret = write(pages_to_scan_fd, pages_to_scan, len);
+ if (ret < 0)
return -errno;
+ if (ret != len)
+ return -EIO;

ret = write(sleep_millisecs_fd, sleep_ms, strlen(sleep_ms));
if (ret < 0 || ret != strlen(sleep_ms))
@@ -531,8 +535,10 @@ static int stop_ksmd_and_restore_frequency(void)
return -errno;

ret = write(pages_to_scan_fd, "100", 3);
- if (ret < 0 || ret != 3)
+ if (ret < 0)
return -errno;
+ if (ret != 3)
+ return -EIO;

ret = write(sleep_millisecs_fd, "20", 2);
if (ret < 0 || ret != 2)
--
2.54.0