public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Vineet Agarwal <agarwal.vineet2006@gmail.com>
To: shuah@kernel.org, akpm@linux-foundation.org
Cc: linux-kselftest@vger.kernel.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, david@kernel.org, ljs@kernel.org,
	Liam.Howlett@oracle.com, vbabka@kernel.org, rppt@kernel.org,
	surenb@google.com, mhocko@suse.com,
	Vineet Agarwal <agarwal.vineet2006@gmail.com>
Subject: [PATCH v3] selftests/mm: ksm-functional-tests: fix partial write handling
Date: Sun,  3 May 2026 15:38:40 +0530	[thread overview]
Message-ID: <20260503101229.654763-1-agarwal.vineet2006@gmail.com> (raw)

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 <agarwal.vineet2006@gmail.com>

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(-)

diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
index 8d874c4754f3..4abd98be3f59 100644
--- a/tools/testing/selftests/mm/ksm_functional_tests.c
+++ b/tools/testing/selftests/mm/ksm_functional_tests.c
@@ -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;
 }
-- 
2.54.0


             reply	other threads:[~2026-05-03 10:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-03 10:08 Vineet Agarwal [this message]
2026-05-03 13:23 ` [PATCH v3] selftests/mm: ksm-functional-tests: fix partial write handling Andrew Morton
2026-05-04  7:35 ` Mike Rapoport

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260503101229.654763-1-agarwal.vineet2006@gmail.com \
    --to=agarwal.vineet2006@gmail.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox