All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nikolai Dokutovich <ndokutovich@gmail.com>
To: David Hildenbrand <david@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: Shuah Khan <shuah@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	Mike Rapoport <rppt@kernel.org>, Michal Hocko <mhocko@suse.com>,
	linux-mm@kvack.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Nikolai Dokutovich <ndokutovich@gmail.com>
Subject: [PATCH 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem
Date: Sun, 29 Nov 2026 07:00:39 +0300	[thread overview]
Message-ID: <20261129040041.37139-2-ndokutovich@gmail.com> (raw)
In-Reply-To: <20261129040041.37139-1-ndokutovich@gmail.com>

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.  A refused
forced write transfers zero bytes and mem_rw() reports it as -EIO; any
other zero-byte remote access reports the same.  The helper therefore
treats -EIO as a refusal, but keeps every other outcome separate, so a
caller skips only on that error and a real fault in the test below still
fails.

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

diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 515031b8283d..f80188f500a2 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -1119,3 +1119,45 @@ void shm_limits_prepare(unsigned long length)
 		shmall = val;
 	}
 }
+
+enum proc_mem_force_write proc_mem_force_write_probe(int prot)
+{
+	const size_t pagesize = getpagesize();
+	ssize_t ret;
+	char data = 1;
+	char *mem;
+	int fd;
+
+	mem = mmap(NULL, pagesize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (mem == MAP_FAILED)
+		return PROC_MEM_FORCE_WRITE_UNKNOWN;
+
+	fd = open("/proc/self/mem", O_RDWR);
+	if (fd < 0) {
+		munmap(mem, pagesize);
+		return PROC_MEM_FORCE_WRITE_UNKNOWN;
+	}
+
+	if (lseek(fd, (off_t)(uintptr_t)mem, SEEK_SET) == (off_t)-1) {
+		close(fd);
+		munmap(mem, pagesize);
+		return PROC_MEM_FORCE_WRITE_UNKNOWN;
+	}
+
+	ret = write(fd, &data, 1);
+	if (ret < 0)
+		ret = -errno;
+	close(fd);
+	munmap(mem, pagesize);
+
+	if (ret == 1)
+		return PROC_MEM_FORCE_WRITE_ALLOWED;
+	/*
+	 * A denied forced write transfers zero bytes and mem_rw() reports
+	 * that as -EIO.  So does any other zero-byte remote access, hence
+	 * the refusal is only recognized, not proven.
+	 */
+	if (ret == -EIO)
+		return PROC_MEM_FORCE_WRITE_DENIED;
+	return PROC_MEM_FORCE_WRITE_UNKNOWN;
+}
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index b5d59729d432..5b05763945da 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -68,6 +68,22 @@ static inline void force_read_pages(char *addr, unsigned int nr_pages,
 
 bool detect_huge_zeropage(void);
 
+/*
+ * Result of probing a write through /proc/self/mem into a mapping that is
+ * not writable, which the kernel performs as a forced (FOLL_FORCE) write.
+ *
+ * proc_mem_force_write_probe() takes the protection of the mapping to try;
+ * it must not include PROT_WRITE, or the write succeeds without going
+ * through the forced path.
+ */
+enum proc_mem_force_write {
+	PROC_MEM_FORCE_WRITE_ALLOWED,
+	PROC_MEM_FORCE_WRITE_DENIED,
+	PROC_MEM_FORCE_WRITE_UNKNOWN,
+};
+
+enum proc_mem_force_write proc_mem_force_write_probe(int prot);
+
 /*
  * Plan 9 FS has bugs (at least on QEMU) where certain operations fail with
  * ENOENT on unlinked files. See
-- 
2.43.0


  reply	other threads:[~2026-11-29  4:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29  4:00 [PATCH 0/3] selftests/mm: skip tests when FOLL_FORCE writes are denied Nikolai Dokutovich
2026-11-29  4:00 ` Nikolai Dokutovich [this message]
2026-11-29  4:00 ` [PATCH 2/3] selftests/mm: skip the mkdirty ptrace write tests without FOLL_FORCE Nikolai Dokutovich
2026-11-29  4:00 ` [PATCH 3/3] selftests/mm: skip the ksm PROT_NONE test " Nikolai Dokutovich
2026-11-29  4:04 ` [PATCH 0/3] selftests/mm: skip tests when FOLL_FORCE writes are denied Nikolai Dokutovich
  -- strict thread matches above, loose matches on Subject: below --
2026-11-29  4:00 Nikolai Dokutovich
2026-11-29  4:00 ` [PATCH 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem Nikolai Dokutovich
2026-11-30 11:16   ` David Hildenbrand (Arm)
2026-11-30 11:17   ` David Hildenbrand (Arm)

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=20261129040041.37139-2-ndokutovich@gmail.com \
    --to=ndokutovich@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.