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=1796382812; b=kC6sOwo1ZUHOzG92NRFQKpUdEnBd3za9rgX8pmxdsSfcjOPDyaHFmOZjmXwrBhJtGUFP G6Zlk4LizgGMAC73i8XId3Euk2t3XoH+aiWpxTYZH2Jr4jJfqAJ/RmoCKOFq6iwd+skP4 2d830jwZPT5+z+JMXAJei9Q7KFjq+Vv4U/ZVoG1EGhlx7RJCRqGqFeh/r2hd0Siy1AGuN I7fsAPU4fco2uRXg/kOX0DC2MBOWNYmAyWg/m2kYdQpvFEpCOwwIpnhMefqAne1EDw6pW 0GjQ+y6XOaGHbYHgtAXvfPkGjeQy/y+uFUlE4h4uVML9zHmp1lxRBMzXFo8QXuqZIfg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382812; 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=6zDQexoEgFBTnjeReP5htS8Y0yANohP4RE03Ueetj+Y=; b=XO0Ri0a+O+pFLa8soZtgNayeVmbiTE5ZDQACJnj1JxIgpi4IDXSoiLoRE1oq2LWVW2g2 qEzeCwik6JX1yXTabfRW+tEnkhPlNCelRUQHrhmFPGWU87cEdsMAivkdoCCfdoS3IigwG c/XvVbptvt+vuFag56LzPON20hcUW4HkHNdlu1MjlFQx2Z5omFcwMl/d+9TEKpGTX5dgj j2nebWwqunPxm8YWQf949hb9buaolXsKLI10X/56ebG+ab5VRj8POulP8UqJdWBhMi2vQ hMCLbsqta3XQxF7Jxcf7R8sWIksqIShN5NgXcCrEzuy6HaIrFpgkV6lRG0+iVAkQBXg== 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=I7rHOiVo; 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 EA161606A682; Fri, 4 Dec 2026 11:13:31 +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=I7rHOiVo Received: by smtp.kernel.org (Postfix) with ESMTPSA id 015811F00931; Fri, 4 Dec 2026 11:13:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382811; bh=6zDQexoEgFBTnjeReP5htS8Y0yANohP4RE03Ueetj+Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=I7rHOiVo2Q93erOWcvDU6bJpoNwJ8EuVJbLMqRoGJB3WDgm7q4pJksjbR8R2yscv/ OZqOWPBUYin7TyYw4/eL2fTzlnDQtkTp+1UM+8vgsMAelYcHt5c5cIVyNDV6mOXIVX wMWe1m2TVlFeGHAL3nxG+YyPh2/1EDWOjSsz9460= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "Jose A. Perez de Azpillaga" , Pedro Falcato , "David Hildenbrand (Arm)" , Sasha Levin Subject: [PATCH 6.18 609/703] mm/gup: limit pagecache folio refcount when grabbing references Date: Fri, 4 Dec 2026 19:08:28 +0900 Message-ID: <20261204100643.974599862@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: patches@lists.linux.dev 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 2d9bcd380585abc436f055fbb618b34f98882004 upstream. Let's add __folio_ref_count_over_limit() checks in fast and slow GUP before acquiring additional references or pins. Keep it simple and use the check for all folio types. Also propagate try_grab_folio() errors when grabbing additional references in __get_user_pages() and in memfd_pin_folios(), so hitting the limit returns -ENOMEM instead of -EFAULT and -EINVAL, respectively. Keep the existing invalid-refcount warnings. Fixes: 800d8c63b2e9 ("shmem: add huge pages support") Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS") Fixes: 793917d997df ("mm/readahead: Add large folio readahead") Cc: stable@vger.kernel.org Reviewed-by: Jose A. Perez de Azpillaga Reviewed-by: Pedro Falcato Link: https://patch.msgid.link/20261112-refcount_overflow_stable-v4-5-ad3deef0bd23@kernel.org Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Pedro Falcato Signed-off-by: Sasha Levin --- include/linux/mm.h | 11 +++++------ mm/gup.c | 32 +++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index e089fd3875f8e..d69c8e942eabf 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1490,6 +1490,9 @@ bool folio_ref_count_over_limit_slowpath(const struct folio *folio, * to pagecache folios (!hugetlb), either before acquiring references or * afterwards with rollback if the operation is refused. * + * Also required when pinning folios without a dedicated pincount that use + * GUP_PIN_COUNTING_BIAS. + * * Checking other folios does no harm. * * This check is an approximation; we leave some headroom such that concurrent @@ -2119,12 +2122,8 @@ static inline bool folio_maybe_dma_pinned(struct folio *folio) return atomic_read(&folio->_pincount) > 0; /* - * folio_ref_count() is signed. If that refcount overflows, then - * folio_ref_count() returns a negative value, and callers will avoid - * further incrementing the refcount. - * - * Here, for that overflow case, use the sign bit to count a little - * bit higher via unsigned math, and thus still get an accurate result. + * Use unsigned math so even an unexpected negative refcount is + * treated as possibly pinned. */ return ((unsigned int)folio_ref_count(folio)) >= GUP_PIN_COUNTING_BIAS; diff --git a/mm/gup.c b/mm/gup.c index f74fe4b17d017..72fc72115f3ef 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -74,10 +74,14 @@ static inline void sanity_check_pinned_pages(struct page **pages, static inline struct folio *try_get_folio(struct page *page, int refs) { struct folio *folio; + int ref_count; retry: folio = page_folio(page); - if (WARN_ON_ONCE(folio_ref_count(folio) < 0)) + ref_count = folio_ref_count(folio); + if (WARN_ON_ONCE(ref_count < 0)) + return NULL; + if (unlikely(__folio_ref_count_over_limit(folio, ref_count))) return NULL; if (unlikely(!folio_ref_try_add(folio, refs))) return NULL; @@ -140,7 +144,13 @@ static void gup_put_folio(struct folio *folio, int refs, unsigned int flags) int __must_check try_grab_folio(struct folio *folio, int refs, unsigned int flags) { - if (WARN_ON_ONCE(folio_ref_count(folio) <= 0)) + const int ref_count = folio_ref_count(folio); + + if (WARN_ON_ONCE(ref_count <= 0)) + return -ENOMEM; + + if ((flags & (FOLL_GET | FOLL_PIN)) && + unlikely(__folio_ref_count_over_limit(folio, ref_count))) return -ENOMEM; if (unlikely(!(flags & FOLL_PCI_P2PDMA) && folio_is_pci_p2pdma(folio))) @@ -1482,17 +1492,17 @@ static long __get_user_pages(struct mm_struct *mm, struct folio *folio = page_folio(page); /* - * Since we already hold refcount on the - * large folio, this should never fail. + * Grabbing more references can fail once we + * hit folio refcount limits. */ - if (try_grab_folio(folio, page_increm - 1, - gup_flags)) { + ret = try_grab_folio(folio, page_increm - 1, + gup_flags); + if (ret) { /* - * Release the 1st page ref if the - * folio is problematic, fail hard. + * Release the folio reference we + * already took. */ gup_put_folio(folio, 1, gup_flags); - ret = -EFAULT; goto out; } } @@ -3541,10 +3551,10 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, break; } - if (try_grab_folio(folio, 1, FOLL_PIN)) { + ret = try_grab_folio(folio, 1, FOLL_PIN); + if (ret) { folio_unlock(folio); folio_batch_release(&fbatch); - ret = -EINVAL; goto err; } -- 2.53.0