Skip to contentSkip to conversation

selftests/mm: ksm-functional-tests: fix partial writes

PATCH Superseded Vineet Agarwal 2 participants 3 messages May 2 – May 3

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/mm: ksm-functional-tests: fix partial writes

Some write() calls only check for <= 0, which fails to detect partial writes. A positive return value smaller than the requested length is currently treated as success, potentially resulting in truncated writes to KSM sysfs interfaces.

Other write() usages in the same file already verify that the full length is written, making the current checks inconsistent.

Fix by verifying that write() returns the full expected length.

This also aligns with recent improvements in vm_util write_file(), which enforce strict validation of write() results.

---
 tools/testing/selftests/mm/ksm_functional_tests.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +10 -4
index 8d874c4754f3..9a8c852acd68 100644
@@ -498,6 +498,7 @@ static void test_prctl_fork(void)
 static int start_ksmd_and_set_frequency(char *pages_to_scan, char *sleep_ms)
 {
 	int ksm_fd;
+	ssize_t ret;
 
 	ksm_fd = open("/sys/kernel/mm/ksm/run", O_RDWR);
 	if (ksm_fd < 0)
@@ -506,10 +507,12 @@ static int start_ksmd_and_set_frequency(char *pages_to_scan, char *sleep_ms)
 	if (write(ksm_fd, "1", 1) != 1)
 		return -errno;
 
-	if (write(pages_to_scan_fd, pages_to_scan, strlen(pages_to_scan)) <= 0)
+	ret = write(pages_to_scan_fd, pages_to_scan, strlen(pages_to_scan));
+	if (ret < 0 || ret != strlen(pages_to_scan))
 		return -errno;
 
-	if (write(sleep_millisecs_fd, sleep_ms, strlen(sleep_ms)) <= 0)
+	ret = write(sleep_millisecs_fd, sleep_ms, strlen(sleep_ms));
+	if (ret < 0 || ret != strlen(sleep_ms))
 		return -errno;
 
 	return 0;
@@ -518,6 +521,7 @@ static int start_ksmd_and_set_frequency(char *pages_to_scan, char *sleep_ms)
 static int stop_ksmd_and_restore_frequency(void)
 {
 	int ksm_fd;
+	ssize_t ret;
 
 	ksm_fd = open("/sys/kernel/mm/ksm/run", O_RDWR);
 	if (ksm_fd < 0)
@@ -526,10 +530,12 @@ static int stop_ksmd_and_restore_frequency(void)
 	if (write(ksm_fd, "2", 1) != 1)
 		return -errno;
 
-	if (write(pages_to_scan_fd, "100", 3) <= 0)
+	ret = write(pages_to_scan_fd, "100", 3);
+	if (ret < 0 || ret != 3)
 		return -errno;
 
-	if (write(sleep_millisecs_fd, "20", 2) <= 0)
+	ret = write(sleep_millisecs_fd, "20", 2);
+	if (ret < 0 || ret != 2)
 		return -errno;
 
 	return 0;
Signature · 1 line
2.54.0
#1 lore raw

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

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.

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(-)
tools/testing/selftests/mm/ksm_functional_tests.c +9 -3
index 9a8c852acd68..c80254cda926 100644
@@ -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)
Signature · 1 line
2.54.0
reviewer
#2 lore raw

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

Quoted 10 lines · Vineet Agarwal

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

Quoted 8 lines

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

Changes in v2:
- Fix incorrect use of -errno on partial writes
- Return -EIO when write() completes partially

Please start a new thread for a new version of the patch.

Quoted 9 lines
---
 tools/testing/selftests/mm/ksm_functional_tests.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +0 -0
index 9a8c852acd68..c80254cda926 100644

tools/testing/selftests/mm/ksm_functional_tests.c static int start_ksmd_and_set_frequency(char *pages_to_scan, char *sleep_ms) · line 510

Quoted 6 lines
tools/testing/selftests/mm/ksm_functional_tests.c +1 -2
@@ -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);

The v2 diff should be against the base, not against v1.

tools/testing/selftests/mm/ksm_functional_tests.c static int start_ksmd_and_set_frequency(char *pages_to_scan, char *sleep_ms) · line 516

Quoted 6 lines
tools/testing/selftests/mm/ksm_functional_tests.c +5 -0
+
+	ret = write(pages_to_scan_fd, pages_to_scan, len);
+	if (ret < 0)
 		return -errno;
+	if (ret != len)
+		return -EIO;

Just return -1 if write() != strlen(), the actual error code does not matter anyway.

Quoted 17 lines
tools/testing/selftests/mm/ksm_functional_tests.c +3 -1
 	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)
Signature · 1 line
2.54.0
Signature · 2 lines
Sincerely yours,
Mike.