mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3 0/3] selftests/mm: skip tests when FOLL_FORCE writes are denied
@ 2026-11-30 17:39 Nikolai Dokutovich
  2026-11-30 17:39 ` [PATCH v3 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-11-30 17:39 UTC (permalink / raw)
  To: David Hildenbrand, Andrew Morton
  Cc: Shuah Khan, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Michal Hocko, linux-mm, linux-kselftest, linux-kernel,
	Nikolai Dokutovich

A write through /proc/self/mem into a mapping that is not writable is
performed by the kernel as a forced write.  Since commit
41e8149c8892 ("proc: add config & param to block forcing mem writes")
such a write is refused unless the writing task is tracing the target
task, the kernel was built with CONFIG_PROC_MEM_ALWAYS_FORCE, or it was
booted with proc_mem.force_override=always.  Commit
599bbba5a36f ("proc: make PROC_MEM_FORCE_PTRACE the Kconfig default") made
the refusal the default.

Commit a3907a3169d0 ("selftests/mm: specify requirement for
PROC_MEM_ALWAYS_FORCE=y") set CONFIG_PROC_MEM_ALWAYS_FORCE=y in mm/config.
That fragment is merged into the kernel configuration only when it is
requested, so a test binary run against a distribution kernel is not
covered, and mkdirty and the ksm PROT_NONE test then report failures
instead of skipping:

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

The first patch adds a probe to vm_util that reports whether such a write
is permitted.  The permission belongs to the kernel and the calling task,
not to the mapping, so the probe takes no protection argument: it maps one
read-only page and writes a byte through /proc/self/mem.  A refused forced
write copies zero bytes and mem_rw() reports that as -EIO, but any other
zero-byte remote access reports the same, so -EIO can indicate a refusal
without proving one.  Only the two decisive results are cached; anything
else is inconclusive, returns allowed and is not cached, so the test below
still runs and can fail.

This is deliberately not a check of errno on the tests' own write: that
was raised in the April discussion of the mm/config fix, and checking the
result of the operation under test cannot tell a policy refusal from a
failure of the operation itself.  The probe isolates the question, and
the test then either runs unchanged or is skipped at its entry.

Tested with this series on x86_64 against a kernel built from this base
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 (2 probe skips, 1 no uffd)
    ksm_functional_tests pass:8 skip:2 (test_prot_none, 1 no uffd)

  boot with proc_mem.force_override=always
    mkdirty              pass:5 skip:1 (no uffd)
    ksm_functional_tests pass:9 skip:1 (no uffd)

When the probe causes a skip, it is reported as "FOLL_FORCE write probe
returned EIO", and no other test changes its result.

Changes in v3:
 - Reduce the probe to bool proc_mem_force_write_allowed(void), taking no
   protection argument and caching the answer internally, as David
   Hildenbrand asked.
 - Call it at the entry of each affected test instead of caching the result
   in mkdirty's main().


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          | 47 +++++++++++++++++++
 tools/testing/selftests/mm/vm_util.h          | 17 +++++++
 4 files changed, 79 insertions(+)

-- 
2.43.0


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

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

A write through /proc/self/mem into a mapping that is not writable is
performed by the kernel as a forced write.  Since commit
41e8149c8892 ("proc: add config & param to block forcing mem writes")
such a write is refused with -EIO unless the writing task is tracing the
target task, the kernel was built with
CONFIG_PROC_MEM_ALWAYS_FORCE, or it was booted with
proc_mem.force_override=always.  Commit 599bbba5a36f ("proc: make
PROC_MEM_FORCE_PTRACE the Kconfig default") made the refusal the default,
so it is the common case on distribution kernels.

Commit a3907a3169d0 ("selftests/mm: specify requirement for
PROC_MEM_ALWAYS_FORCE=y") set CONFIG_PROC_MEM_ALWAYS_FORCE=y in mm/config
for dedicated selftest builds.  That fragment is merged into the kernel
configuration only when it is requested, so it does not cover a test
binary run against a distribution kernel.

Add a helper that reports whether such a write is permitted.  The
permission is a property of the kernel and the calling task, not of the
mapping, so the helper probes a read-only mapping, caches a decisive
answer and takes no protection argument.  A refused forced write
transfers zero bytes and mem_rw() reports it as -EIO; any other
zero-byte remote access reports the same, so -EIO can indicate a refusal
but does not prove one.  Only the two decisive results are cached; an
inconclusive probe returns true and is not cached, leaving a real fault
to fail the test.

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

diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 515031b8283d..5ccf1e2e7ec9 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -1119,3 +1119,50 @@ 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();
+	ssize_t ret;
+	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 true;
+
+	fd = open("/proc/self/mem", O_RDWR);
+	if (fd < 0) {
+		munmap(mem, pagesize);
+		return true;
+	}
+
+	if (lseek(fd, (off_t)(uintptr_t)mem, SEEK_SET) == (off_t)-1) {
+		close(fd);
+		munmap(mem, pagesize);
+		return true;
+	}
+
+	ret = write(fd, &data, 1);
+	if (ret < 0)
+		ret = -errno;
+	close(fd);
+	munmap(mem, pagesize);
+
+	/*
+	 * A refused forced write transfers zero bytes and mem_rw() reports
+	 * that as -EIO, but any other zero-byte remote access reports the
+	 * same.  Cache only the two decisive results and leave anything else
+	 * to the test.
+	 */
+	if (ret != 1 && ret != -EIO)
+		return true;
+
+	allowed = (ret == 1);
+	return allowed;
+}
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index b5d59729d432..0a1af711e0f3 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -68,6 +68,23 @@ 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.  The kernel performs such a write as a forced
+ * (FOLL_FORCE) write.  The answer depends on the kernel configuration,
+ * the boot parameters and whether the writing task traces the target,
+ * not on the mapping, so a decisive result is cached after one probe.
+ * This holds for the self probe only: another target or a policy that
+ * could change during the run would not be safe to cache.
+ *
+ * A successful write returns true; -EIO returns false, although -EIO can
+ * also mean another zero-byte remote access and so indicates a refusal
+ * without proving one.  Any other result is inconclusive: the probe
+ * returns true and caches nothing, so the test runs and reports its own
+ * failure instead of skipping.
+ */
+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	[flat|nested] 4+ messages in thread

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

mkdirty writes through /proc/self/mem into read-only PTEs and expects the
kernel to force the write.  Under the default CONFIG_PROC_MEM_FORCE_PTRACE
policy that is only allowed when the writing task is tracing the target
task, which is not the case here, so the setup write fails and the test
reports a failure instead of skipping:

    not ok 1 write() failed

Check the permission at the entry of the two ptrace write tests and skip
them when it is denied.  A kernel built with
CONFIG_PROC_MEM_ALWAYS_FORCE or booted with proc_mem.force_override=always
still runs them for real, so a genuine failure is still reported.

Commit a3907a3169d0 ("selftests/mm: specify requirement for
PROC_MEM_ALWAYS_FORCE=y") asks for CONFIG_PROC_MEM_ALWAYS_FORCE=y in
mm/config, which applies once that fragment is merged into the kernel
configuration; this skip additionally keeps an installed binary from
failing on a kernel that was not built that way.

The remaining subtests do not depend on the forced write 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..333d450d4ff3 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 probe returned EIO\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 probe returned EIO\n");
+		return;
+	}
+
 	mem = mmap_thp_range(PROT_READ, &mmap_mem, &mmap_size);
 	if (mem == MAP_FAILED)
 		return;
-- 
2.43.0


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

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

test_prot_none() stores a unique value in every page of a PROT_NONE mapping
through /proc/self/mem, which is a forced write.  With the default
CONFIG_PROC_MEM_FORCE_PTRACE the kernel denies it, so the test reports

    not ok 5 ptrace write failed

instead of skipping.  Check the permission at the start of the test and
skip when it is denied.

Commit a3907a3169d0 ("selftests/mm: specify requirement for
PROC_MEM_ALWAYS_FORCE=y") asks for CONFIG_PROC_MEM_ALWAYS_FORCE=y in
mm/config, which applies once that fragment is merged into the kernel
configuration; this skip additionally keeps an installed binary from
failing on a kernel that was not built that way.

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..ffb25b250d2f 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 probe returned EIO\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	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-30 17:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 17:39 [PATCH v3 0/3] selftests/mm: skip tests when FOLL_FORCE writes are denied Nikolai Dokutovich
2026-11-30 17:39 ` [PATCH v3 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem Nikolai Dokutovich
2026-11-30 17:39 ` [PATCH v3 2/3] selftests/mm: skip the mkdirty ptrace write tests without FOLL_FORCE Nikolai Dokutovich
2026-11-30 17:39 ` [PATCH v3 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

all inboxes | Powered by JetHome®