Skip to contentSkip to conversation

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

PATCH Applied Vineet Agarwal 3 participants 3 messages May 4

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

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

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

Previously, the write() calls used <= 0 to detect failure. This condition is never true for partial writes (ret > 0 but ret < len), so partial writes were silently treated as success.

Fix this by verifying that write() returns the full expected length and treating any mismatch as failure.

Changes in v4:
- Simplify write() checks as suggested by Mike Rapoport
- Remove temporary variables and use direct comparisons
- Use a single len variable

---
 .../selftests/mm/ksm_functional_tests.c       | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +11 -8
index 8d874c4754f3..31c06c72203f 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;
+	size_t len;
 
 	ksm_fd = open("/sys/kernel/mm/ksm/run", O_RDWR);
 	if (ksm_fd < 0)
@@ -506,11 +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;
 
-	if (write(pages_to_scan_fd, pages_to_scan, strlen(pages_to_scan)) <= 0)
-		return -errno;
+	len = strlen(pages_to_scan);
+	if (write(pages_to_scan_fd, pages_to_scan, len) != len)
+		return -1;
 
-	if (write(sleep_millisecs_fd, sleep_ms, strlen(sleep_ms)) <= 0)
-		return -errno;
+	len = strlen(sleep_ms);
+	if (write(sleep_millisecs_fd, sleep_ms, len) != len)
+		return -1;
 
 	return 0;
 }
@@ -526,11 +529,11 @@ 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)
-		return -errno;
+	if (write(pages_to_scan_fd, "100", 3) != 3)
+		return -1;
 
-	if (write(sleep_millisecs_fd, "20", 2) <= 0)
-		return -errno;
+	if (write(sleep_millisecs_fd, "20", 2) != 2)
+		return -1;
 
 	return 0;
 }
Signature · 1 line
2.54.0
reviewer Acked-by on v4 1/1
#1 lore raw
Quoted 10 lines · Vineet Agarwal

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

Previously, the write() calls used <= 0 to detect failure. This condition is never true for partial writes (ret > 0 but ret < len), so partial writes were silently treated as success.

Fix this by verifying that write() returns the full expected length and treating any mismatch as failure.

Quoted 8 lines

Changes in v4:
- Simplify write() checks as suggested by Mike Rapoport
- Remove temporary variables and use direct comparisons
- Use a single len variable

---
 .../selftests/mm/ksm_functional_tests.c       | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
Signature · 2 lines
Sincerely yours,
Mike.
maintainer Acked-by on v4 1/1
#2 lore raw
In reply to Vineet Agarwal
Quoted 11 lines · Vineet Agarwal

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

Previously, the write() calls used <= 0 to detect failure. This condition is never true for partial writes (ret > 0 but ret < len), so partial writes were silently treated as success.

Fix this by verifying that write() returns the full expected length and treating any mismatch as failure.

Thanks!

Signature · 3 lines
Cheers,

David