Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 0/3] selftests/mm: skip tests when FOLL_FORCE writes are not available
@ 2026-12-01 19:16 Nikolai Dokutovich
  2026-12-01 19:16 ` [PATCH v5 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem Nikolai Dokutovich
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Nikolai Dokutovich @ 2026-12-01 19:16 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton
  Cc: Shuah Khan, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Michal Hocko, Xu Xin, linux-mm, linux-kselftest, linux-kernel,
	Nikolai Dokutovich

A write through /proc/self/mem into a mapping that is not writable needs
FOLL_FORCE, which CONFIG_PROC_MEM_FORCE_PTRACE only grants to a caller that
traces the target.  mkdirty and the ksm PROT_NONE test then fail instead of
skipping on a distribution kernel:

    not ok 1 write() failed
    not ok 5 ptrace write failed

Add a probe that performs the same write on a scratch mapping, and skip the
affected tests when it does not succeed.  mm/config already asks for
CONFIG_PROC_MEM_ALWAYS_FORCE=y for harness runs; this covers binaries run
on kernels built without it.

Tested on x86_64 with a kernel whose .config has
CONFIG_PROC_MEM_FORCE_PTRACE=y, CONFIG_CHECKPOINT_RESTORE=n,
CONFIG_USERFAULTFD=n, CONFIG_KSM=y:

  default boot                        mkdirty pass:3 skip:3
                                      ksm     pass:8 skip:2
  proc_mem.force_override=always      mkdirty pass:5 skip:1
                                      ksm     pass:9 skip:1

Changes in v5 (relative to v3; v4 was retracted):
 - Drop the inconclusive branch; the probe returns a plain bool.
 - Shorten the comment and the commit messages.
 - Skip with "FOLL_FORCE write not available".


Nikolai Dokutovich (3):
  selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem
  selftests/mm: skip the mkdirty ptrace write tests without FOLL_FORCE
  selftests/mm: skip the ksm PROT_NONE test without FOLL_FORCE

 .../selftests/mm/ksm_functional_tests.c       |  5 ++++
 tools/testing/selftests/mm/mkdirty.c          | 10 +++++++
 tools/testing/selftests/mm/vm_util.c          | 29 +++++++++++++++++++
 tools/testing/selftests/mm/vm_util.h          |  8 +++++
 4 files changed, 52 insertions(+)

-- 
2.43.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v5 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem
  2026-12-01 19:16 [PATCH v5 0/3] selftests/mm: skip tests when FOLL_FORCE writes are not available Nikolai Dokutovich
@ 2026-12-01 19:16 ` Nikolai Dokutovich
  2026-12-01 19:16 ` [PATCH v5 2/3] selftests/mm: skip the mkdirty ptrace write tests without FOLL_FORCE Nikolai Dokutovich
  2026-12-01 19:16 ` [PATCH v5 3/3] selftests/mm: skip the ksm PROT_NONE test " Nikolai Dokutovich
  2 siblings, 0 replies; 4+ messages in thread
From: Nikolai Dokutovich @ 2026-12-01 19:16 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton
  Cc: Shuah Khan, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Michal Hocko, Xu Xin, linux-mm, linux-kselftest, linux-kernel,
	Nikolai Dokutovich

mkdirty and the ksm PROT_NONE test write through /proc/self/mem into a
mapping that is not writable.  With CONFIG_PROC_MEM_FORCE_PTRACE such a
write is only allowed for a caller that traces the target, so both tests
fail instead of skipping on a distribution kernel.

Add a helper that performs that write on a scratch mapping and caches the
result.

Signed-off-by: Nikolai Dokutovich <ndokutovich@gmail.com>
---
 tools/testing/selftests/mm/vm_util.c | 29 ++++++++++++++++++++++++++++
 tools/testing/selftests/mm/vm_util.h |  8 ++++++++
 2 files changed, 37 insertions(+)

diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 515031b8283d..c933e6c575a4 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -1119,3 +1119,32 @@ void shm_limits_prepare(unsigned long length)
 		shmall = val;
 	}
 }
+
+bool proc_mem_force_write_allowed(void)
+{
+	static int allowed = -1;
+	const size_t pagesize = getpagesize();
+	char data = 1;
+	char *mem;
+	int fd;
+
+	if (allowed >= 0)
+		return allowed;
+
+	mem = mmap(NULL, pagesize, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (mem == MAP_FAILED)
+		return false;
+
+	fd = open("/proc/self/mem", O_RDWR);
+	if (fd < 0) {
+		munmap(mem, pagesize);
+		return false;
+	}
+
+	allowed = lseek(fd, (off_t)(uintptr_t)mem, SEEK_SET) != (off_t)-1 &&
+		  write(fd, &data, 1) == 1;
+
+	close(fd);
+	munmap(mem, pagesize);
+	return allowed;
+}
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index b5d59729d432..52acf1a6b725 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -68,6 +68,14 @@ static inline void force_read_pages(char *addr, unsigned int nr_pages,
 
 bool detect_huge_zeropage(void);
 
+/*
+ * Whether a write through /proc/self/mem into a mapping that is not writable
+ * is permitted.  Probed once and cached; the permission depends on the kernel
+ * configuration and on whether the caller traces the target, not on the
+ * mapping.
+ */
+bool proc_mem_force_write_allowed(void);
+
 /*
  * Plan 9 FS has bugs (at least on QEMU) where certain operations fail with
  * ENOENT on unlinked files. See
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v5 2/3] selftests/mm: skip the mkdirty ptrace write tests without FOLL_FORCE
  2026-12-01 19:16 [PATCH v5 0/3] selftests/mm: skip tests when FOLL_FORCE writes are not available Nikolai Dokutovich
  2026-12-01 19:16 ` [PATCH v5 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem Nikolai Dokutovich
@ 2026-12-01 19:16 ` Nikolai Dokutovich
  2026-12-01 19:16 ` [PATCH v5 3/3] selftests/mm: skip the ksm PROT_NONE test " Nikolai Dokutovich
  2 siblings, 0 replies; 4+ messages in thread
From: Nikolai Dokutovich @ 2026-12-01 19:16 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton
  Cc: Shuah Khan, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Michal Hocko, Xu Xin, linux-mm, linux-kselftest, linux-kernel,
	Nikolai Dokutovich

Both ptrace write tests fail when the kernel does not allow forced writes:

    not ok 1 write() failed

Check the permission at their entry and skip instead.  The other subtests
do not depend on it and keep running.

Signed-off-by: Nikolai Dokutovich <ndokutovich@gmail.com>
---
 tools/testing/selftests/mm/mkdirty.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/tools/testing/selftests/mm/mkdirty.c b/tools/testing/selftests/mm/mkdirty.c
index 68dd447a5454..f6f50016cf5c 100644
--- a/tools/testing/selftests/mm/mkdirty.c
+++ b/tools/testing/selftests/mm/mkdirty.c
@@ -91,6 +91,11 @@ static void test_ptrace_write(void)
 
 	ksft_print_msg("[INFO] PTRACE write access\n");
 
+	if (!proc_mem_force_write_allowed()) {
+		ksft_test_result_skip("FOLL_FORCE write not available\n");
+		return;
+	}
+
 	mem = mmap(NULL, pagesize, PROT_READ, MAP_PRIVATE|MAP_ANON, -1, 0);
 	if (mem == MAP_FAILED) {
 		ksft_test_result_fail("mmap() failed\n");
@@ -128,6 +133,11 @@ static void test_ptrace_write_thp(void)
 
 	ksft_print_msg("[INFO] PTRACE write access to THP\n");
 
+	if (!proc_mem_force_write_allowed()) {
+		ksft_test_result_skip("FOLL_FORCE write not available\n");
+		return;
+	}
+
 	mem = mmap_thp_range(PROT_READ, &mmap_mem, &mmap_size);
 	if (mem == MAP_FAILED)
 		return;
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v5 3/3] selftests/mm: skip the ksm PROT_NONE test without FOLL_FORCE
  2026-12-01 19:16 [PATCH v5 0/3] selftests/mm: skip tests when FOLL_FORCE writes are not available Nikolai Dokutovich
  2026-12-01 19:16 ` [PATCH v5 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem Nikolai Dokutovich
  2026-12-01 19:16 ` [PATCH v5 2/3] selftests/mm: skip the mkdirty ptrace write tests without FOLL_FORCE Nikolai Dokutovich
@ 2026-12-01 19:16 ` Nikolai Dokutovich
  2 siblings, 0 replies; 4+ messages in thread
From: Nikolai Dokutovich @ 2026-12-01 19:16 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton
  Cc: Shuah Khan, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Michal Hocko, Xu Xin, linux-mm, linux-kselftest, linux-kernel,
	Nikolai Dokutovich

test_prot_none() fails when the kernel does not allow forced writes:

    not ok 5 ptrace write failed

Check the permission at the start of the test and skip instead.

Signed-off-by: Nikolai Dokutovich <ndokutovich@gmail.com>
---
 tools/testing/selftests/mm/ksm_functional_tests.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
index 31c06c72203f..e21037c9bf24 100644
--- a/tools/testing/selftests/mm/ksm_functional_tests.c
+++ b/tools/testing/selftests/mm/ksm_functional_tests.c
@@ -629,6 +629,11 @@ static void test_prot_none(void)
 
 	ksft_print_msg("[RUN] %s\n", __func__);
 
+	if (!proc_mem_force_write_allowed()) {
+		ksft_test_result_skip("FOLL_FORCE write not available\n");
+		return;
+	}
+
 	map = mmap_and_merge_range(0x11, size, PROT_NONE, KSM_MERGE_MADVISE);
 	if (map == MAP_FAILED)
 		goto unmap;
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-01 19:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 19:16 [PATCH v5 0/3] selftests/mm: skip tests when FOLL_FORCE writes are not available Nikolai Dokutovich
2026-12-01 19:16 ` [PATCH v5 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem Nikolai Dokutovich
2026-12-01 19:16 ` [PATCH v5 2/3] selftests/mm: skip the mkdirty ptrace write tests without FOLL_FORCE Nikolai Dokutovich
2026-12-01 19:16 ` [PATCH v5 3/3] selftests/mm: skip the ksm PROT_NONE test " Nikolai Dokutovich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox