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=1796358061; b=4V71l9fQwdwMfxg5andB7yzQVIcDUruYHaer4qeLJ5djRFjX4BYPO9S1igKl55iYp/FV rqnET81yl94z5OeLCX29s9bxGTpkFF/x+8yX0ByHm/vZmEUSmxB6fSJN8FxnnXbKsZnth zeNAFRF/9uXkdIi6aWTe3Dd4/7ePubNz1hiNZfKDCtIdI1+6HbMFe8iEYXswUe3UxU+4F BLC5gpGD0UDgA28jEiXtiH7YM9d2oc1DCJ3E40h/x01GVEGRRpYv8HYDlRS84Z8bxl9yG kNeJuGYf+/ntO/73uxebeubhm1om6IaI19G+ds9TtQcHZmFJy7nkc9CYsiaRCc6l0sw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796358061; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=Zc5VHvyHAafx+ij2JU0G0Ks2vLu5xKbzstJXY6N06HI=; b=g31MvAEIm1oDSBflVZ508NrLyUwqwAg+5Z37iVqE646t37D8SpRBH3+OlxpZGACaC3GO JGN0C4HyLGV359Bz8NWhqelOeBzHLhMlE+EwmjFu67wkAW1tYPnLrlf8IbEXUQqnqfF8h hIMmEvqKXqAFhK55qKZZbBJGaNfF54c4JPtqQNhwM576EJl6Ak/EsC/PX89IRoIudWVP6 0NJ8XwitPjvT8mwjvnCGyUcpj9+v89Iyq1JWfKQFcp9QRfRuGhBlJ7fnrJzBiUmqgaiRJ uMit4550HydJ5UDTnmTFfy0k/KSp170BAYAMhc9ho7qf2//s/AjLFL/eteSK4B/EYHQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Hkqc8CrT; 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 98458606A338 for ; Fri, 4 Dec 2026 04:21:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Hkqc8CrT Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F82D1F000C2; Fri, 4 Dec 2026 04:21:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796358061; bh=Zc5VHvyHAafx+ij2JU0G0Ks2vLu5xKbzstJXY6N06HI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Hkqc8CrTu/jqeoBw4IjRjTWCaIgJdb+ZzNSsXfiXG/zYzC4vdDO9ZyFiCYXCcTkbO LZ4DhEgaZQfGty/nLNp32BA6X/2ah8CWTtrZ+y0t26I9WJGpAIFTJ0TyOU1TxE1hU1 OwQ4lcUDqYBdXArkGdOxk+8ahMneM1F8lq1OJN89/DH+eKSz1zGckz9A6hUJGWIAbg /QciQbmXqxu28LS3B3ei2yAZuQf5Dz7Sla8sgmELZe/QwLANYv/aL9gZ9RgkmneZGO 0OD7jup9jGZBPuAAAIRbsZW4x9YZAPjhY/ZyQgkRDnY6iybB4SGH1glENJ3LNauHw7 PZw6ehkBRi2Iw== From: Sasha Levin To: stable@vger.kernel.org Cc: "David Hildenbrand (Arm)" , Chris Mason , "Lorenzo Stoakes (ARM)" , Lance Yang , Sasha Levin Subject: [PATCH 6.12.y 5/5] mm/memfd: handle GUP pins more reliably when write-sealing Date: Thu, 3 Dec 2026 23:20:49 -0500 Message-ID: <20261204041943.2537281-5-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261204041943.2537281-1-sashal@kernel.org> References: <2026120239-shank-trekker-0b72@gregkh> <20261204041943.2537281-1-sashal@kernel.org> 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 From: "David Hildenbrand (Arm)" [ Upstream commit d369ba5a46c65c54b94c94713ca1fb4ad6a87ebf ] 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: Sasha Levin --- mm/memfd.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mm/memfd.c b/mm/memfd.c index 5ca501e505ea7..5878744f2bd7f 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -31,6 +31,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); } -- 2.53.0