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] selftests/mm: ksm-functional-tests: fix partial writes
Date: Sat,  2 May 2026 06:27:02 +0530	[thread overview]
Message-ID: <20260502005702.593878-1-agarwal.vineet2006@gmail.com> (raw)

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.

Signed-off-by: Vineet Agarwal <agarwal.vineet2006@gmail.com>
---
 tools/testing/selftests/mm/ksm_functional_tests.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
index 8d874c4754f3..9a8c852acd68 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;
+	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;
-- 
2.54.0


             reply	other threads:[~2026-05-02  0:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-02  0:57 Vineet Agarwal [this message]
2026-05-03  6:00 ` [PATCH v2] selftests/mm: ksm-functional-tests: fix partial write handling Vineet Agarwal
2026-05-03  8:48   ` 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=20260502005702.593878-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