Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Nikolai Dokutovich <ndokutovich@gmail.com>,
	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
Subject: Re: [PATCH v3 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem
Date: Tue, 1 Dec 2026 17:22:04 +0100	[thread overview]
Message-ID: <b9714a42-e690-440b-8eba-474c6b8dcfbf@kernel.org> (raw)
In-Reply-To: <20261130173950.41003-2-ndokutovich@gmail.com>

On 11/30/26 18:39, Nikolai Dokutovich wrote:
> 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.
> +	 */

Under which circumstances would we get that. IOW, why do we even care?

> +	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.

Please reduce that to a bare minimum. No need to write a book for something
simple ;)

-- 
Cheers,

David

  parent reply	other threads:[~2026-12-01 16:22 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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-12-01  7:04   ` xu.xin
2026-12-01 16:22   ` David Hildenbrand (Arm) [this message]
2026-11-30 17:39 ` [PATCH v3 2/3] selftests/mm: skip the mkdirty ptrace write tests without FOLL_FORCE Nikolai Dokutovich
2026-12-01  7:06   ` xu.xin
2026-12-01 16:23   ` David Hildenbrand (Arm)
2026-11-30 17:39 ` [PATCH v3 3/3] selftests/mm: skip the ksm PROT_NONE test " Nikolai Dokutovich
2026-12-01  7:07   ` xu.xin
2026-12-01  7:15   ` xu.xin
2026-12-01 16:23     ` 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=b9714a42-e690-440b-8eba-474c6b8dcfbf@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.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=ndokutovich@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox