* [PATCH v4] selftests/mm: ksm-functional-tests: fix partial write handling
@ 2026-05-04 8:13 Vineet Agarwal
2026-05-04 10:02 ` Mike Rapoport
2026-05-04 12:58 ` David Hildenbrand (Arm)
0 siblings, 2 replies; 3+ messages in thread
From: Vineet Agarwal @ 2026-05-04 8:13 UTC (permalink / raw)
To: shuah, akpm
Cc: linux-kselftest, linux-mm, linux-kernel, david, ljs, Liam.Howlett,
vbabka, rppt, surenb, mhocko, 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.
Signed-off-by: Vineet Agarwal <agarwal.vineet2006@gmail.com>
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(-)
diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
index 8d874c4754f3..31c06c72203f 100644
--- a/tools/testing/selftests/mm/ksm_functional_tests.c
+++ b/tools/testing/selftests/mm/ksm_functional_tests.c
@@ -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;
}
--
2.54.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v4] selftests/mm: ksm-functional-tests: fix partial write handling
2026-05-04 8:13 [PATCH v4] selftests/mm: ksm-functional-tests: fix partial write handling Vineet Agarwal
@ 2026-05-04 10:02 ` Mike Rapoport
2026-05-04 12:58 ` David Hildenbrand (Arm)
1 sibling, 0 replies; 3+ messages in thread
From: Mike Rapoport @ 2026-05-04 10:02 UTC (permalink / raw)
To: Vineet Agarwal
Cc: shuah, akpm, linux-kselftest, linux-mm, linux-kernel, david, ljs,
Liam.Howlett, vbabka, surenb, mhocko
On Mon, May 04, 2026 at 01:43:13PM +0530, Vineet Agarwal wrote:
> 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.
>
> Signed-off-by: Vineet Agarwal <agarwal.vineet2006@gmail.com>
Acked-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
>
> 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(-)
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v4] selftests/mm: ksm-functional-tests: fix partial write handling
2026-05-04 8:13 [PATCH v4] selftests/mm: ksm-functional-tests: fix partial write handling Vineet Agarwal
2026-05-04 10:02 ` Mike Rapoport
@ 2026-05-04 12:58 ` David Hildenbrand (Arm)
1 sibling, 0 replies; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-05-04 12:58 UTC (permalink / raw)
To: Vineet Agarwal, shuah, akpm
Cc: linux-kselftest, linux-mm, linux-kernel, ljs, Liam.Howlett,
vbabka, rppt, surenb, mhocko
On 5/4/26 10:13, Vineet Agarwal wrote:
> 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.
>
> Signed-off-by: Vineet Agarwal <agarwal.vineet2006@gmail.com>
>
Thanks!
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-05-04 12:58 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-04 8:13 [PATCH v4] selftests/mm: ksm-functional-tests: fix partial write handling Vineet Agarwal
2026-05-04 10:02 ` Mike Rapoport
2026-05-04 12:58 ` David Hildenbrand (Arm)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox