selftests/mm: ksm-functional-tests: fix partial write handling
Subsystems (3)
PATCH v3 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) could be treated as success because write() does not set errno in this case and the code returned -errno. This could result in returning 0 and incorrectly signaling success.
Fix this by verifying that write() returns the full expected length and treating any mismatch as failure.
Changes in v3:
- Simplify error handling as suggested by reviewer
- Return -1 when write() does not complete fully
- Rebase patch against base tree
--- .../selftests/mm/ksm_functional_tests.c | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +19 -8
@@ -498,6 +498,8 @@ 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; +size_t len, sleep_len; ksm_fd =open ("/sys/kernel/mm/ksm/run" ,O_RDWR );if (ksm_fd <0 )
@@ -506,11 +508,17 @@ 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 (sleep_millisecs_fd, sleep_ms,strlen (sleep_ms)) <=0 ) -return -errno; + ret =write (pages_to_scan_fd, pages_to_scan, len); +if (ret != len) +return -1 ; + + sleep_len =strlen (sleep_ms); + + ret =write (sleep_millisecs_fd, sleep_ms, sleep_len); +if (ret != sleep_len) +return -1 ;return 0 ; }
@@ -518,6 +526,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,11 +535,13 @@ 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; + ret =write (pages_to_scan_fd,"100" ,3 ); +if (ret !=3 ) +return -1 ; -if (write (sleep_millisecs_fd,"20" ,2 ) <=0 ) -return -errno; + ret =write (sleep_millisecs_fd,"20" ,2 ); +if (ret !=2 ) +return -1 ;return 0 ; }
Signature · 1 line
2.54.0
Andrew Morton
maintainerQuoted 9 lines · On Sun, 3 May 2026 15:38:40 +0530 Vineet Agarwal…
Update
write()checks to properly detect and handle partial writes.Previously, partial writes (ret > 0 && ret != len) could be treated as success because
write()does not set errno in this case and the code returned -errno. This could result in returning 0 and incorrectly signaling success.Fix this by verifying that
write()returns the full expected length and treating any mismatch as failure.
Thanks. AI review asked some questions:
https://sashiko.dev/#/patchset/[email protected]
Hi,
Thanks for taking a look.
Regarding the AI review comments:
-
The issue in the original code is that partial writes (ret > 0 && ret < len)
were not detected because the code only checked for write() <= 0. As a result,
partial writes were treated as success.
-
The current check ret != len intentionally covers both partial writes and error cases (ret < 0) in a simple way, which follows the earlier suggestion
to keep the logic straightforward for selftests.
-
For these tests, distinguishing specific error codes is not required, so returning -1 on any mismatch is sufficient.
Please let me know if you'd prefer any adjustments to the commit message.
Thanks,
Vineet
Quoted 18 lines · Andrew Morton
Quoted 9 lines · On Sun, 3 May 2026 15:38:40 +0530 Vineet Agarwal <…
Update
write()checks to properly detect and handle partial writes.Previously, partial writes (ret > 0 && ret != len) could be treated as success because
write()does not set errno in this case and the code returned -errno. This could result in returning 0 and incorrectly signaling success.Fix this by verifying that
write()returns the full expected length and treating any mismatch as failure.Thanks. AI review asked some questions:
Mike Rapoport
reviewerHi,
Quoted 25 lines · Vineet Agarwal
Update
write()checks to properly detect and handle partial writes.Previously, partial writes (ret > 0 && ret != len) could be treated as success because
write()does not set errno in this case and the code returned -errno. This could result in returning 0 and incorrectly signaling success.Fix this by verifying that
write()returns the full expected length and treating any mismatch as failure.Signed-off-by Vineet Agarwal <[email protected]>Changes in v3:
- Simplify error handling as suggested by reviewer
- Return -1 whenwrite()does not complete fully
- Rebase patch against base tree--- .../selftests/mm/ksm_functional_tests.c | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-)tools/testing/selftests/mm/ksm_functional_tests.c +0
-0
tools/testing/selftests/mm/ksm_functional_tests.c static void test_prctl_fork(void) · line 502
Quoted 5 lines
tools/testing/selftests/mm/ksm_functional_tests.c +2
-0@@ -498,6 +498,8 @@ 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; +size_t len, sleep_len;
ret and len should be of the same type and one len variable is enough.
tools/testing/selftests/mm/ksm_functional_tests.c static int start_ksmd_and_set_frequency(char *pages_to_scan, char *sleep_ms) · line 513
Quoted 13 lines
tools/testing/selftests/mm/ksm_functional_tests.c +2
-4ksm_fd =open ("/sys/kernel/mm/ksm/run" ,O_RDWR );if (ksm_fd <0 )@@ -506,11 +508,17 @@ 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 (sleep_millisecs_fd, sleep_ms,strlen (sleep_ms)) <=0 ) -return -errno; + ret =write (pages_to_scan_fd, pages_to_scan, len);
if (write(pages_to_scan_fd, pages_to_scan, len) != len) return -1;
would be enough here and below.
Quoted 3 lines
tools/testing/selftests/mm/ksm_functional_tests.c +3
-0+if (ret != len) +return -1 ; +
Signature · 2 lines
Sincerely yours, Mike.