From: Qiliang Yuan <odys.yuan@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>, Xu Xin <xu.xin@linux.dev>,
Chengming Zhou <chengming.zhou@linux.dev>,
Stefan Roesch <shr@devkernel.io>,
Lorenzo Stoakes <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>, Shuah Khan <shuah@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Randy Dunlap <rdunlap@infradead.org>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org,
Qiliang Yuan <odys.yuan@gmail.com>
Subject: [PATCH v4 3/5] selftests/mm: fail KSM helpers on short reads and writes
Date: Sun, 08 Nov 2026 20:22:52 -0500 [thread overview]
Message-ID: <20261108-bug-mm-ksm-smart-scan-align-skips-v4-3-86be8fe77e7a@gmail.com> (raw)
In-Reply-To: <20261108-bug-mm-ksm-smart-scan-align-skips-v4-0-86be8fe77e7a@gmail.com>
The KSM helpers in vm_util read and write KSM's sysfs and procfs files
with a single pread() or write(), and return -errno when the call does
not transfer what they asked for.
A read of zero bytes or a short write leaves errno at 0, so the helpers
then return 0, which their callers take as a value of 0 or as a write
that took effect. A short write of run makes ksm_start() wait for scans
of a ksmd that never started, a short read of full_scans makes it wait
for scans that never seem to complete, and short reads of
ksm_merging_pages or ksm_stat and a short write of use_zero_pages make
cases fail that should have been skipped.
Return -errno only when the call fails, and -EIO when it transfers less
than asked, so that callers skip or fail as they do on other errors.
Forcing each short read or write with an LD_PRELOAD shim on
ksm_functional_tests:
short read or write of before after
run cases fail, then hangs all skipped
full_scans hangs in the first case all skipped
ksm_merging_pages 9 of 11 cases fail all skipped
ksm_stat zero page case fails zero page case skipped
use_zero_pages zero page case fails zero page case skipped
Signed-off-by: Qiliang Yuan <odys.yuan@gmail.com>
---
The checks come from 93fb70aa5904 ("selftests/vm: add KSM unmerge
tests"), 3d0745e59c84 ("selftest: add a testcase of ksm zero pages")
and 42096aa24b82 ("selftest/mm: ksm_functional_tests: test in
mmap_and_merge_range() if anything got merged"), and b27f292de6b1
("selftests/mm: put general ksm operation into vm_util") turned the
use_zero_pages check into one of them. There are no Fixes tags, as
only short reads and writes forced with the shim hit them, and the
helpers are in vm_util only since v6.18.
---
tools/testing/selftests/mm/vm_util.c | 22 +++++++++++++++++-----
1 file changed, 17 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 515031b8283da..caea045fa7d46 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -920,6 +920,8 @@ long ksm_get_self_zero_pages(void)
close(proc_self_ksm_stat_fd);
if (read_size < 0)
return -errno;
+ if (!read_size)
+ return -EIO;
buf[read_size] = 0;
@@ -944,8 +946,10 @@ long ksm_get_self_merging_pages(void)
ret = pread(proc_self_ksm_merging_pages_fd, buf, sizeof(buf) - 1, 0);
close(proc_self_ksm_merging_pages_fd);
- if (ret <= 0)
+ if (ret < 0)
return -errno;
+ if (!ret)
+ return -EIO;
buf[ret] = 0;
return strtol(buf, NULL, 10);
@@ -963,8 +967,10 @@ long ksm_get_full_scans(void)
ret = pread(ksm_full_scans_fd, buf, sizeof(buf) - 1, 0);
close(ksm_full_scans_fd);
- if (ret <= 0)
+ if (ret < 0)
return -errno;
+ if (!ret)
+ return -EIO;
buf[ret] = 0;
return strtol(buf, NULL, 10);
@@ -981,7 +987,9 @@ int ksm_use_zero_pages(void)
ret = write(ksm_use_zero_pages_fd, "1", 1);
close(ksm_use_zero_pages_fd);
- return ret == 1 ? 0 : -errno;
+ if (ret < 0)
+ return -errno;
+ return ret == 1 ? 0 : -EIO;
}
int ksm_start(void)
@@ -1002,8 +1010,10 @@ int ksm_start(void)
}
ret = write(ksm_fd, "1", 1);
close(ksm_fd);
- if (ret != 1)
+ if (ret < 0)
return -errno;
+ if (ret != 1)
+ return -EIO;
do {
end_scans = ksm_get_full_scans();
if (end_scans < 0)
@@ -1024,7 +1034,9 @@ int ksm_stop(void)
ret = write(ksm_fd, "2", 1);
close(ksm_fd);
- return ret == 1 ? 0 : -errno;
+ if (ret < 0)
+ return -errno;
+ return ret == 1 ? 0 : -EIO;
}
int get_hardware_corrupted_size(unsigned long *val)
--
2.43.0
next prev parent reply other threads:[~2026-11-09 1:23 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-09 1:22 [PATCH v4 0/5] mm/ksm: keep smart scan from losing duplicate pages Qiliang Yuan
2026-11-09 1:22 ` [PATCH v4 1/5] mm/ksm: align smart scan skips with the scan number Qiliang Yuan
2026-11-09 1:22 ` [PATCH v4 2/5] docs/mm: ksm: describe how smart scan picks the scans to skip Qiliang Yuan
2026-11-09 2:12 ` Xu Xin (vivo)
2026-11-09 1:22 ` Qiliang Yuan [this message]
2026-11-09 2:29 ` [PATCH v4 3/5] selftests/mm: fail KSM helpers on short reads and writes Xu Xin (vivo)
2026-11-09 1:22 ` [PATCH v4 4/5] selftests/mm: move waiting for KSM full scans into a helper Qiliang Yuan
2026-11-09 2:32 ` Xu Xin (vivo)
2026-11-09 1:22 ` [PATCH v4 5/5] selftests/mm: cover merging KSM pages aged by smart scan Qiliang Yuan
2026-11-09 2:34 ` Xu Xin (vivo)
2026-11-09 12:32 ` [PATCH v4 0/5] mm/ksm: keep smart scan from losing duplicate pages David Hildenbrand
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=20261108-bug-mm-ksm-smart-scan-align-skips-v4-3-86be8fe77e7a@gmail.com \
--to=odys.yuan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=chengming.zhou@linux.dev \
--cc=corbet@lwn.net \
--cc=david@kernel.org \
--cc=liam@infradead.org \
--cc=linux-doc@vger.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=rdunlap@infradead.org \
--cc=rppt@kernel.org \
--cc=shr@devkernel.io \
--cc=shuah@kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
--cc=xu.xin@linux.dev \
/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
all inboxes | Powered by JetHome®