From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796142370; b=2s+jpOTUbkwJ4l8dXmZpFdNvf6Vv+N8raslyerbxQcW+fXJgwaYq9O0Ph1juALIFSJxQ MSc1Z3IVyxBjNaQ651MGxl/IFIDwodrRxKGC7q/z75+KLXfhgW8Z/QW8pOFMgjSkr819p CFCD6ROKw/UQ8c8XG109h2ARIrxtXnVDnlJO7ovnAIk0nSwLEqojpBmq/nDi5cPTG8AoE sPS66wzI0adKruW+JDQXkChB1FAAuoJUwt1EGVdmI/yxcK9ql2B0sdyKfgx+S28b017Fp 6CwTs6tyDL9JnHP1BxWDu0i+EheuVuPpyATi5EZdHX8u4n/MGfNWnIbXysHQFXdCo2A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796142370; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=WQ7gSuFsJ5BGEV9QS1KAUDxDZhXWbdjDhzVwsnwRLAw=; b=ukMzTGo+VgIBhmqcsN5UYAE6FoRtgGI82k8W4D1HyBeV3NlAtsyJ1RkBB1mNN4uF9fDL fzIrrLg/3KRodJrebxliUtSH9h2+hiHleDM/Y7EAM/vEVOWLHOw3dcKBUPqz3vsJQNX2T SnnOjOf3SM+2jgFWOovie/Wp28Y7xGT6NvNvUtKdA1gpfzsUnPv5GqnDZ4niaUCGB4QNf QD2raKF/7GrQuBhDwn3o3P/xCK+YbUmopMFartcAFGwSmjezNgu8na8dI2o7y+AsXAD9C B1JOGqrsJ1zUHi1uO3B8nN/6Z1YyqJv+Hb8Thu6hyDCiNKXP26jOLx05LA6dA1UzjxQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="Kr4jatO/"; arc=none smtp.remote-ip=74.125.224.41 Received: from mail-yx1-f41.google.com (mail-yx1-f41.google.com [74.125.224.41]) (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 D7011606A68A for ; Tue, 1 Dec 2026 16:26:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D7011606A68A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Kr4jatO/ Received: by mail-yx1-f41.google.com with SMTP id 956f58d0204a3-67990a45e02so12437198d50.3 for ; Tue, 01 Dec 2026 08:26:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796142367; x=1796747167; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=WQ7gSuFsJ5BGEV9QS1KAUDxDZhXWbdjDhzVwsnwRLAw=; b=Kr4jatO/04tafo0jUXvnNekPWvg5t6H9k6BDkT9R+18JoVUV2Mbc7Vmcw0rQJmZMX6 xcpUCL9GO/epOUzLZV7P2ITh1qI854/THvIUto5JCjvQA912zB28IzN3l8RWdGNPnrBL V0WJkmHA+dq/YNbrRc3QvBjf0avMpNkSf8PGSRv8l560/JUKb7Yn/2fCB7qLsk4fndEB 2gH1MZ1C0pCQbKFWutdugrw6ay5eFUKwH/svMXVlhjQgGMPhERJk8J/ErjBzwix3X4o3 wmdOWugP08bn7yBUqmHq/1QnwqXLRTFxEI3R09XNS1LXhiSXQERWga2CXQ/DJNl/xo6c cwIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796142367; x=1796747167; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=WQ7gSuFsJ5BGEV9QS1KAUDxDZhXWbdjDhzVwsnwRLAw=; b=c6c1XFTFgg+Dh7qc70I9goFyLEoaXKpoX1lsIBlBJkSu6VBd2u9TekFuNe3MpQZdLF uDC1IbZ2IfeVJzg4SKHj7ZRERyyguP8UrufrRbLZiTF2v+JLYCKH2kQdlwacN6thib9N E5wnvEkLi/0jVOl6EnF3/R86Q0LXg+AZ0KH4a5zRDFbr6byefQNfbkEAnaR1SjteiagF NsiJyfxlyzmDnxmwwAydAh6fN3fymABRobo3ArLCstGlE8uJd+MQvijCYIaEjEKJM5pk sjJOdS/yP93XwSKC0NFMCH/rODnuBe7mYif6CgpGlOz/3sqRpkjecSQv+w85WoNVA7yA 5wug== X-Forwarded-Encrypted: i=1; AL+hErHKciKER2d2uoXPSvsOREoDFKluXkMe/L2Rm7X2NAhSKkue2lQCC2qMwPzgErDW4FXa9eJilxcQ4p0DQ/Y=@vger.kernel.org X-Gm-Message-State: APuU4Z+M1PeVVJ0rAY9SvlwryrD3eAInQM8jUtui5f1hylqzZmpfwv2I K2q8UiGVkpDkmWSuE+lb5CAnN8K+g4PzVE4f6YuQZneXgJHLZmPRfYl/ X-Gm-Gg: AZFdTZcHkxCIYMgnFD44QJ1MYgYsPxBL2slsBc7Uts73mblRDm+BMdJJ9z0uRTqfBIp UBvlrBDHqUAnw+s8ixIqYMEXLigUt0qn9D73MSdaNy0/0bwkg1pb98H1XIcR5E25/JHK0Vifcr7 FK/b8E8MnUL0pCI5EInHBlHnupDz/IfczELEJo2ryRRTQahX9vIbaq/GCCNJkt5KYtTIXy9geJX LrVkjsTRoTLsjTYTcW+EJZmejGZcPOnXT2l8MZARlxHGBSwle+Nakr1Zj5AlOigbi7WVKMuagKp UGaHheVXrEob5qkJFtAeiiroRpeh0grloySwAeJq9rw26i+J2COFa7DqMjBgxnwtYlIazocL/pI RB/dFTpTw4GyGt0kna6tQuL9L8WYVzMGH36bT/QZLzx/bMQJjXzP9sQxSnVyBLIUNW3GMbHwKTg iJ1GloVRR/8HMNrtvvmqr7QBsNktB2yKvXEobJ4Ith7p7/1cDeRI7Op9u1jzRYQrfv32hist+iP LeqaflV88e5J9m1LY2a8W4wXOX50A3nuxmCs7V0+ufEJlSRgQY9u86G3N3AyxmXBNl4zj3XKrgq 8N1uDCH46pP7IM5Fe9Ahv472P1S5U6J8Q0U05BKwM+0leRtIgIkFb5LOv3mg94i97SrKOBIP4M9 2mkOabA== X-Received: by 2002:a05:690e:4803:b0:67d:c29e:944e with SMTP id 956f58d0204a3-67df441f41bmr11187440d50.48.1796142366842; Tue, 01 Dec 2026 08:26:06 -0800 (PST) Received: from localhost ([138.197.109.108]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-921366d853esm320155196d6.27.2026.12.01.08.26.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 08:26:06 -0800 (PST) From: Nikolai Dokutovich To: David Hildenbrand , Andrew Morton Cc: Shuah Khan , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Xu Xin , linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Nikolai Dokutovich Subject: [PATCH v4 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem Date: Tue, 1 Dec 2026 19:25:58 +0300 Message-ID: <20261201162600.42818-2-ndokutovich@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261201162600.42818-1-ndokutovich@gmail.com> References: <20261201162600.42818-1-ndokutovich@gmail.com> X-Mailing-List: linux-kernel@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(+) 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