From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.170 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796060398; b=uNnpIf8dYzT38tYIdUi0PstKCiblsftyI2W+RC1M8SjUi3+O687+AMRDxc39CA3RoyLa 1COABIyBOZsrXiVyZJJM4JjvaBjoKKkdRplSWSTDlQgvd3wxYybVLrYvfATbcjHWH9Dy3 iLb3JsaM1kUIBaQ8x4zM7eY7u7PNA9uJMSuTs+mruHjshMu0RV9THM1fzYz7rySkHPbxN 256WB8tQ7kSGAmGZbAl95SmWeGzL3E3YWQU/hnu9MOUrGaYdYOhcTBLbakK//H8XtlvCU lUh1Ip1EEg7wexUlnwbzVAUaQ+IcD0WrlwsQQENYi18dpsXvo9QKRn4RF8sTHOefXXA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796060398; 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=ag4YEoM0OKb8I7Bj+yXeiyLDmCaglqrTTpBWPK0WQEh8lLTI2W5E1mnasH6raz5CiDAe b/qeaJ1q87LdcEzFeaCCd5gECaXCRkDzei+pIZpiz49yoFg0BU5X8lnrFaGoyhoGbAe1H xTsaPpFW8p2iL2L6AueZsnmPmr22B+ULZ2utc36/cSCgjWx//0sSuJKBQjonzXgtnPW1j OjENQYPjPK1T83EqqZ01CIeUyCUxgNdToXwJBX2MnsCc/8+hZGg9qRBHfU5MZuWtZdtif 3a9wlW4IHnzqmHWZ1aH7v5bGKRdVf723Op3XockLRLMYc1DXtLcNApAha5IAqu6nl+g== 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.170 Received: from mail-qk1-f170.google.com (mail-qk1-f170.google.com [209.85.222.170]) (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 12A9B606A32C for ; Mon, 30 Nov 2026 17:39:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 12A9B606A32C 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-f170.google.com with SMTP id af79cd13be357-9496473b511so1276722685a.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=U/51sTxKFP+QD6Z7oRjhW0Q2/BY0frm+jBL64+wWIO9pknbzuzoBF4WK2O+G5de+/6 56zxz8mYi9SWtTrJqGRPdTwGwqK2Om3cnMUEStm6mdXozs+mQI/qdUOAyYcHSVsAOEbb uqF6rEmdDXItitfn8Vn6dJf+aFaXzUQllMrj7yjbOBLrrXwmfLX488F5eZqep6VwIEmY wOmn+XKJV3uScOYNQXUcVFB97nsfGztbbFN2r78hw72A8tOReYsUlHq8l7Qh8qFt4fXK yECtZPkMCfHYYzZP68KpE5H0bK0v/z8ljKqzqbn5m6iZ/1BDbWYTyFwU5fQ2XOfQUPis N7pA== X-Forwarded-Encrypted: i=1; AL+hErGj5BB5WJZV5FUo+9+CZ8ZviWn8vhhMm3ANET7H/LlZ3mxQUjFgkCdTtt9qtDzMWMc0H992bmEViLPzmc4gTVs=@vger.kernel.org X-Gm-Message-State: APuU4Z/rEJXynRlcNadw6u4oIlvlKpq118M2RGZCb6OKGzYF1HMmTO/v PnNqzUJ15THBHSFaYeNPj81xI07wXnzkyHXNREcmDHKmiWkDNbKcJlht X-Gm-Gg: AZFdTZcL2ukU95h5IjMj3tO399bSf4JT30db5KU0izRdE/zOl3JBZSdM0iDn4mPFmNf X/gTY0D0lt9D0FPuR99hUHjPlTsr3nUGwxUJSKQxFaYJnmgDzhpBuTV33CMe5hosmiZtZdfm5EJ qWYCELpNa/FTtFTjSNkMC9W6hTUajNTYtsxHJVFRXBFI0BzeAY6Qm08FXF1lD8M1h6ifKw+hWy5 1Yy3fG23I8sBWSUUJn7RpBI2La65Tmimb3ltzf6fQvgQwyXYJsSeL+EnV+TkOZr+X4YphixoZeK mj0lsL5ctB6RlKGsib9/GqMxtHOl/YL5Xvvl8a6hdMYCcbYe6RK5VcJPiqE8LFoENOhVQfMqDO3 9iEKPxkS9ZkIfgYARjAJ+uJyicssiYPWXEuJirc00zLmRp0ukIQAgwd/Aj36r1x/8KDKXmqh5tU w1KC3I8sg1ZWAoibwwhmDMkEMr6daS+gnyqjHHVotL5fgTJesaYJXIXYmbiiLFW8DVkcuO0ViOB DpM9fU0dTcZ8xkEjcAcsqE7vDnqS0BBNbo6l+k6qWtCZn6JqIOuzprv9KpNJ/G7NBcvheCq2yLI SBH38UH/MMdbgES8Kvq5FPsRSwQzeoSuUWGSJgtu1/S4z07GeglaQASgo67NfrZLGm8J86m6m59 kkzfU 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-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(+) 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