From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796382517; b=2ed2YsDUmo4XSxbQhSg1Snx/AVJREIZ10QQWm5q/OjILqbBhtW95kz+VeY1HQDsr7Qv9 bqgsoy8KB67WFlf8c0q5skRxT0kqPe1biQs7vURWtzxly1Ja4Kea57vmXDHUvPrPcxg/8 /hvFhBqyhSDDtFhIGvduCYeiIcT5My/NYDZE9xZsXVbsI0KkCfXZ7vVuZi6ot3ngqS1Dh 4ZMZLhkSc/EnP2FEmccSAI7HgTfv1lgUEiI11I2xpuAi8CBOvq5I6S5noCTIQVAtnNVKc lNC2JYMJPJENjBc3+AcmVNOzwCbuKmGVxcT8tCO9IVpI0rMhZbCYn/JG6LQBYshfkbg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382517; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=kShwjU9AgwXD+wdciYhAL9w2N9g8bE6jBLJdnH2qq6M=; b=pFf1uUl6g5F8ATIVWHC/PsuXMVYhGbHftlHC2K9zAmHiubBRblEYZHhYyDP9IxGlX9qq 2aacOzlEImrlXlnyFXFF5u8o9MY6aFVOdaY5b2WkcZGy3Ifm+KCgtjmce6zI6p+iSamQE dY4WjI7z2meYW02m878dMHjQ+tKGW4hz2oHRxwfPEFaMRVKb4+iR1ffQCLl4Jppk4oBZf yrttc/O2Yg/FAASvbRA/JTJ0WRgSQ2b3SRlGhZMN21WE7O9+ywY4ynO2OmbEUauMD7Q8T V1rsG/zunv7mAR09HQDJC0V0HJD18bJbmly67hUHj4DHI5+bRRZjpbh7EDIbGwAwVYQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=WOyZQz4H; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42AE2606A322; Fri, 4 Dec 2026 11:08:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=WOyZQz4H Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6AD691F000CE; Fri, 4 Dec 2026 11:08:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382517; bh=kShwjU9AgwXD+wdciYhAL9w2N9g8bE6jBLJdnH2qq6M=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=WOyZQz4HzKt+rsCogHvqgs4qybmwrhak/ly+nq6jpNL/qP861nJdbxFhOhJJgqAt/ QEdfjWRSd1wtmIpLEgoh6Xi+zfn9c00cdxqX39EOjvhm54VuCt9IqFYkwS6FgxFpxM H2NTkFO+199yDekGhyqB7A9AoVxcDbvJOE6HBRco= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Chris Mason , "Lorenzo Stoakes (ARM)" , Lance Yang , "David Hildenbrand (Arm)" Subject: [PATCH 6.18 487/703] mm/memfd: handle GUP pins more reliably when write-sealing Date: Fri, 4 Dec 2026 19:06:26 +0900 Message-ID: <20261204100640.756694767@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@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 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Hildenbrand (Arm) commit d369ba5a46c65c54b94c94713ca1fb4ad6a87ebf upstream. memfd_wait_for_pins() detects GUP references by comparing a folio's actual refcount with its expected refcount. As spelled out by folio_expected_ref_count(), obtaining the expected refcount when there can be concurrent folio (un)mapping is racy. So it is possible that a changing folio mapcount can race with our check for GUP references, and F_SEAL_WRITE might succeed even if there are some, allowing file contents to be modified after sealing. For folios with a reliable pincount, detect FOLL_PIN references using folio_maybe_dma_pinned() before falling back to the refcount comparison. For small folios without that, the 1024 pinning BIAS makes it very hard (although not impossible) to misguide the refcount comparison. This improves FOLL_PIN detection for folios with a pincount. Handling FOLL_GET and small folios requires a more fundamental change. Fortunately, most interfaces that allow for GUP'ing such pages use FOLL_PIN, in particular interfaces using FOLL_LONGTERM. However, we still have some FOLL_GET leftovers ... Fixes: 05f65b5c7090 ("shm: wait for pins to be released when sealing") Cc: stable@vger.kernel.org Cc: Chris Mason Reviewed-by: Lorenzo Stoakes (ARM) Link: https://patch.msgid.link/20261103-fixes_core_2-v1-1-f332954f1df4@kernel.org Reviewed-by: Lance Yang Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Greg Kroah-Hartman --- mm/memfd.c | 7 +++++++ 1 file changed, 7 insertions(+) --- a/mm/memfd.c +++ b/mm/memfd.c @@ -32,6 +32,13 @@ static bool memfd_folio_has_extra_refs(struct folio *folio) { + /* + * Use the precise pincount where available to avoid racy refcount + * comparisons. + */ + if (folio_has_pincount(folio) && folio_maybe_dma_pinned(folio)) + return true; + return folio_ref_count(folio) != folio_expected_ref_count(folio); }