[PATCH v5 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem
From: Nikolai Dokutovich
Date: Tue Dec 01 2026 - 14:17:01 EST
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@xxxxxxxxx>
---
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