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=1796381092; b=lf2kf2x2xfuAuN8vtp4+bqwudNf5DfvrjKZZl+k17zk+9tZRxATq52+ij6Oq8LGvuh3F /CeG0offHpwlUGN7a6S8NbDcrV6TglmjSgJrU01PThnNIA/y8FTF3KpkarRyKbXQGoobf YlImCxKP9QtZL4Ol5U6xayVaxlt48q87pewloARk4DupjMOE6jwGIt7b9pIPTOaP77qeI MBWc6ro1FY0hTIG5peyEgcgwIhzVeHHHdCZ7hX/b2iJGu8J7DgUTrgwK9lIT3qknhKZhn Y0hb8ruZ6byV/MK/YBnhsyEAr5PnHafXq0h5mxg0NlMa0FDm/F6nDc4SeSesvov4Ttg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381092; 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=0uVhC1fqCjI/fTe/M1TQlLm7Baz6MRy0A18Ghd9xtlM=; b=1x9b6LUfnUgcT9RNeQdDL9eA7pSd7M8dhMPcPpTjVnnVZfN54WOKm9NCKrE1V04BFs3t yI8fLsusIc8adQwUprDMOp1sH+ntsufW4O8Uc+wadqkuvNcFLJpA7A47/XEFY6o20hKPB fFUG8Sw2VVxOZEqvd/l4ErsP8X+1MV0TaL3AcF6CNLx7TsLQtEAySl3/9X8FkUI0VWmCi hRL8YpLDZ/IY9jnqhenYOsPN4lZqeQG+v17mlWDRup/lzvmsFkPxF7nTjQtm/FzuVX3+P SfRrjVZJbtWcyEuIP2X2kIxkdNWezIVxeF1ahkC481gyXvT8K3d7y++EWTiDtiFbZCw== 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=mFxM+Rds; 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 8AF87606A322; Fri, 4 Dec 2026 10:44:52 +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=mFxM+Rds Received: by smtp.kernel.org (Postfix) with ESMTPSA id EBA461F000CE; Fri, 4 Dec 2026 10:44:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381092; bh=0uVhC1fqCjI/fTe/M1TQlLm7Baz6MRy0A18Ghd9xtlM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=mFxM+RdsXRXVQYEFUh33E/uJ2t1mA1ZayIgyxG9ub7xp4gsdTUlkBSchVNO8f/qE4 QeThJ+jcdYIYNNjwj2i5LwR18ztv8Vf9M8tAkFN/3QnOzgJtQykiQkSDucUm+DgbNE y4NoZ5Fpd/50kahottDsPs6ZR6hbwSQncFIQbpT0= 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)" Subject: [PATCH 7.3 555/742] mm/gup: limit pagecache folio refcount when grabbing references Date: Fri, 4 Dec 2026 19:06:59 +0900 Message-ID: <20261204100649.894392289@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@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 7.3-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: Greg Kroah-Hartman --- include/linux/mm.h | 11 +++++------ mm/gup.c | 32 +++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 17 deletions(-) --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2124,6 +2124,9 @@ bool folio_ref_count_over_limit_slowpath * 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 @@ -2834,12 +2837,8 @@ static inline bool folio_maybe_dma_pinne 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; --- a/mm/gup.c +++ b/mm/gup.c @@ -74,10 +74,14 @@ static inline void sanity_check_pinned_p 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 * 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 @@ next_page: 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; } } @@ -3524,10 +3534,10 @@ long memfd_pin_folios(struct file *memfd 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; }