From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.208 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796108669; b=xqIqLYHGKWMuIX1rn2eKhrAROJYN23ji9xb2CQ207AQK5IenO0A7At1dxzRzqGXXPRqI vN48ECpQZvmL3Ff+Hsvcwlbh1u0p256zzYwRj7JFmzpIQrwaOBafC5tgTx/zG3ieP1Q9z DWSlAMf0/mfXPzbsJiH9fXKpP0qQZvsLGA78R70NYlntNIgqfAz4XgmoAwbf78Lh1e0i5 pbefW2n2NB5IpIsufyDr4xdccD1oUVzUe28KnIRJvHnxM3VoPqkNlRuQFYVqd002gvvhG Eop5pBjakW6aGA8rnpbHcAmDxEMW0TSGyaAnRvHYRAHiBbQ3m4khNx79d7jfa/OqVUA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796108669; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-Id: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=8CwY2DpXKzAPFUkFoCyLlEVi7w2pVmbGuVPBvk6BLng=; b=MX1t5mXRdVh8YgPuPKI/b/Pxqz444zks/danQGdfyi6r4M1CXr5RERCUkoq561BBuQWm tHMNBiTJ+mPlFqzQucjmbI4FaWfembaqtO1uWuFtJVSwEv7OtOvycfFDFX/p2avbPsq09 Akl+PNIb72jmHmwSZn74XI7Cu5c3t8RshERuVAdaWYUqNNWbwLjmRtGP6ynqyxvRj0GUB h6I/uLmGw33f0RYIAZGnuNzS2lldFPfvakLRFM/p2c9QdReKB5eUTDTwUCDKvBV0kTer3 EyrUWqQ63l/HIsqQTZACdCTOjn4oQZUWoyvBsFL40eqsiWTUnacjiZa5I05oCf5swMQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=Zhd+weiF; arc=none smtp.remote-ip=91.218.175.208 Received: from mta0.migadu.com (out-208.mta0.migadu.com [91.218.175.208]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3874A606A320 for ; Tue, 1 Dec 2026 07:04:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3874A606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=Zhd+weiF X-Envelope-To: linux-kselftest@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=kj5Bo+uAZvFJZgLAtTuwHsT5pieEf5u27mu9hS4uBXY=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796108665; v=1; x=1796713465; b=Zhd+weiFijyoGuFjskMQ3qwyggU2bLTcL733WprMhNLfdmEwtlkPF1M861hs805f77n8xEUc 5YZJghP3rK9QbxjPqv9hIuUiHPBc4LipCjgI2TgJJlUjpsikn6bHgVwjQMnXuNk2YkhIWq4p64X mMbwH6NOyf69g2K3aa4gYcmo= X-Envelope-To: linux-kselftest@vger.kernel.org Received: by mta10.migadu.com with ESMTPS id e537ee8a2394bf38; Tue, 01 Dec 2026 07:04:25 +0000 X-Mizu-Trace-ID: e537ee8a2394bf38 X-Migadu-Flow: FLOW_OUT From: xu.xin@linux.dev To: ndokutovich@gmail.com Cc: akpm@linux-foundation.org, david@kernel.org, liam@infradead.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, mhocko@suse.com, rppt@kernel.org, shuah@kernel.org, vbabka@kernel.org, Xu Xin Subject: Re: [PATCH v3 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem Date: Tue, 1 Dec 2026 15:04:05 +0800 Message-Id: <20261201070405.516855-1-xu.xin@linux.dev> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261130173950.41003-2-ndokutovich@gmail.com> References: <20261130173950.41003-2-ndokutovich@gmail.com> X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit > 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 > --- > tools/testing/selftests/mm/vm_util.c | 47 ++++++++++++++++++++++++++++ > tools/testing/selftests/mm/vm_util.h | 17 ++++++++++ > 2 files changed, 64 insertions(+) LGTM. Reviewed-by: Xu Xin (vivo) > > 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