From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796060399; b=wuNzKpsUIA5VN26CpRej2nDl0ZnmEz83BzAAEyr0+WeyT8+FrrlWo5r9toTQ60ugkTgC cW9rLRhdj+gYe4dXlamTZS2HnbKPTUDiJlMMSlLSHlGpNojsq+O23P0mNusFFGM1zs6Ia uy+OcRMBQRzLw3el7avrWKDZcmgyZcXFYEE6yDs02HCTnAwQxLqKDbvl20zjbJIPdCqiA yjRd8rX71+O/2WeQy2/yVCDpgHT+dTLZwYjdZawoFOA2XdO94PvzysB9Ofj9rL46zmx5n yr2J3EQq7lohoZGBBs9ZcFuHkmWnj/sTWtDNnrTe9XPXNeHxTVZ2dBYrmxSkoGyfu6A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796060399; 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=o500PWszbFileM0DS2u2g8yfcu3UXwVF873QECpNJgAQC6lioZ45S1fE3SWftxcg/uM6 uVisxZvl0R+aFpmjQtBXeeLtZZKVUu9lI/w85sDpbZhrVKOecRdkZuDxZe0JoIx8ql/h8 JX9L4xlnyBFhmbecJwczXUqnvQPiGPau1W0V0+Dqcvkz3u1KkHIMCMoekWdtAlIdVku/E dXmtuxuVFY1CspL9lFznV4smFSLzHgOYhAYHkYrM2BilbPYiaxbvDRRRuYi5+T/p5g+If S5oepNQU7eXbohyyth4PLun2wfH8IDW3O8usQh4Dc4ePhklryAF0olWltDFW/Mam0ig== 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=dc3MPhnw; arc=none smtp.remote-ip=209.85.222.179 Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) (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 14904606A32D for ; Mon, 30 Nov 2026 17:39:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 14904606A32D 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=dc3MPhnw Received: by mail-qk1-f179.google.com with SMTP id af79cd13be357-93a40a9a01aso1332291685a.0 for ; Mon, 30 Nov 2026 09:39:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796060397; x=1796665197; 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=dc3MPhnwMJc1B64gLZIaxpOyV4Snhcxxejbuapu9j8KT+jTbEyShPW0jdKOWUXekeg KHPlV2cnEbCWB/c/ZeSrBN4ZTSTShmoJKMiu5BQEIaYFyVgVB3RWIc4KSYswi9ANRPrc ndguZLhjkQbgCS1dkVAFQawNsmPpdbz53axJpyeVozS3qYj55jnEHx4OzuoZnicWWSTW sAmGCwKmFiC7Ilhd6Wp59owX+oMjdWXmSdE3ofYoV5aaNbvXDWU2XNdB2Q66mDQTUbk1 qwUKvUkxcytnomqqQX6QKCWSX+Ov57khr8ufFvO8NSuyf0KbFsj30w8wtNP8COAweYRh lVhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796060397; x=1796665197; 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=rU9BzO/m1pEqFJ/amNRAJLgbpJ0AiBSl7i34WTClgiJg6f0DT5I5psN8IpCYW9ddhi n9LttP2SvWGLtUVfXL/wwTfgnN2XNj3hnJxXUehbwE+kr80BEcfajeeUVlJbeOcBiAP0 irhohX0xMDR7b6BSUNfgth7TFjrj+Tot7UEGz1Taq/VH6vS29HTDHgrL6lcz2nr+GNmR PUWtbyqCRLu7dlOO7XGaQ14vC/X2+eWbjW/U/I/6iotv3P8JUctZXg8U5naQwdRpMfgr cL1r+2T4jb9Y46gXpjp903Wi7UJ8kIUzVfxuy/wt+ZUNcVsS7fK/woA2ABOKsduw0a2L 5BKw== X-Forwarded-Encrypted: i=1; AL+hErEdfaBYAI7vDBeBZpsAwV/Sp+ahztaCz0ge4OhC+W/LrmWyrtQkWYsojwghNyhI63WGMFdYcMLKRok7jTg=@vger.kernel.org X-Gm-Message-State: APuU4Z/vLJ1PmyQlKeYALlfVGe8RXZxIvtXhWkDq4RARdaBVLd5KI1Mi Q434c3Oj2Y4rmFMqxbdRS4bxO0lhXCqnlgqvY0SsOxO2+nj+rL1r8dKg X-Gm-Gg: AZFdTZe2C2wV25Ys4rkbOM9zYcz+LNcdzKK8oDh7j5fvLLmNmX4OtRjeVbr+0jzFNha hOn51SwFNj98LX85GsEzXHFC53vExHduc/euHvq2E+91ICL0SzEyqXz31j/F5iGrc/hmYMuX2mt WJGnyaXhOYEJEA7dV5m7w07wU74n8RB5Zlnwkk2hB1c3BZJz6uiwGiX6pfnL25SV30eR8TVjh+q S3+cjGR/unZolLTT1tGgXvueSPvv6k5xmI+Yi62UXY/mcdUZSB6tbv65QFwWyd5r/kuwMTulYPG tAqOKOhBr42OOPRz+bvkgaRZIQggNwTwxsJfGNVVysJLboOp84VaY1yBdfJXSPmcY4lAPnOOBnR VZcTrEJIyVqol6MikGzShRHtyF1a9Im7dYNwGXXnNJVtlLCbpxEmLmYkRDvrAsglo6IKImYLMOs njuHH6A7kb48fb5QI0xtOXBMcBQxJOeMqvwgj6otb9KHsyHuo1oRy011eRDoHhnjeD/Aj4tJdGX qtyj/O3uWZHe5HuWfwTxZ5RwD3Lya1OCq4lPbRBzQtiNQ6p9YVEj/+oQhJK33xX9buWBhfydBga OBYdD+PfcNje0Ngtv6rIYbEKyrG36YKsbTx3z1s05U3kTuvQUCKau8VKkbzvj94UnBuMpF/w5ce SfLLQ X-Received: by 2002:a05:620a:4142:b0:93e:c173:f690 with SMTP id af79cd13be357-949bab12903mr5616294585a.36.1796060396896; Mon, 30 Nov 2026 09:39:56 -0800 (PST) Received: from localhost ([138.197.109.108]) by smtp.gmail.com with ESMTPSA id af79cd13be357-949bb3f2de5sm2581341885a.29.2026.11.30.09.39.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 09:39:56 -0800 (PST) From: Nikolai Dokutovich To: David Hildenbrand , Andrew Morton Cc: Shuah Khan , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Nikolai Dokutovich Subject: [PATCH v3 1/3] selftests/mm: add a probe for FOLL_FORCE writes through /proc/self/mem Date: Mon, 30 Nov 2026 20:39:48 +0300 Message-ID: <20261130173950.41003-2-ndokutovich@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261130173950.41003-1-ndokutovich@gmail.com> References: <20261130173950.41003-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