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=1796384151; b=eoOpEKszn2NNdWwij7a3o0U3YC6aWQVcpiYSG0xuNnE/5gj7cKuzKF9QpJ9MkVXG4eFV hQnqS4M7dcRGao+aNL2BdOMEp9qewIHxSxtiRTBdU5KYW88OpezdWlWQb3TuBjwv+IUoL EKks2LoDOKAPyTwe69IdcV6xOGtx8ffBoUhRUCEMtOJi7IMauCCrSalXhbUkX+6sx2nj+ fLKYUCkjueRa4Ew/e89lCiGNfCtPSas6ZhjBVn4JTXj8WWMG0TXr+KfzHkZn6ahasGUqh 3KLRda6VjHAU2Vqo0z9HVj09TZFUumP6ft57gdEuH+Dp+bu1O1i28XuILOwumZcxkzA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796384151; 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=Us4TgF1rb41bRb0j/zEr9DyVL/WXp99Qh6UcAF99pcQ=; b=XqNjYr8Tv2e0T7NsuDGwcxUc+4gjSFLNdgLABM/zRHz2XrmsqT7ngJr+WA5LkcPGOj98 g1b3/Pg5ediLbBZzEL4LcVbSsMOajauslRykr3V+tfnhwA5PgQ7qioLjOgeXhJnN9kmVi 50FvNG/4lWmH/HuQgr+vi2JuHdWyRH3flI2r08VnoubWcTBwWp0HkR5tN8AcFCVyYF6fZ h2n0SCMgITh08xp8/nI3bKs8tJfRPZUDh7ornYDjVBMXy5jDBSsWvKR4+ijqAaf5kbp56 BJoRuW9StEMDlUrKA5JNqJ+l21at6LfUNR2cjf7L94+fWuMm0SnXuvnIwED2VDiNg3A== 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=ZdPsZSFU; 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 A7098606A322; Fri, 4 Dec 2026 11:35:51 +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=ZdPsZSFU Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24C3C1F000C2; Fri, 4 Dec 2026 11:35:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796384151; bh=Us4TgF1rb41bRb0j/zEr9DyVL/WXp99Qh6UcAF99pcQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ZdPsZSFUlfLesoxhOk/QySh4i1vtjmZj41yTweUoCJmWTYaAo1yEAcTm/3sztBJhY qK9InXdlHgW+iJAmsQa5Pj7VbOr27vXgUqBpKnMLueI36w6yZC3T3We22H7EX2MEqa loDSGLaENciH2TE63LXUPsdYiBDUHEi/Ct2r5VGY= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Chris Mason , "Jose A. Perez de Azpillaga" , Lance Yang , Pedro Falcato , "David Hildenbrand (Arm)" , Sasha Levin Subject: [PATCH 6.12 519/555] mm/mmu_gather: flush pending references early before the folio limit Date: Fri, 4 Dec 2026 19:09:22 +0900 Message-ID: <20261204100628.551534000@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@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.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Hildenbrand (Arm) commit 317bd7d28b248fd4d4e14614d7c30b17a36ef4d7 upstream. When a task zaps mappings (e.g., MADV_DONTNEED), it can drop the PT lock and defer dropping the folio references. In the meantime, page faults can refault pagecache folios, creating more references for the same folios. These deferred references can build up over multiple VMAs. Let's just request a flush after enqueueing folios whose refcount reached a certain threshold. Start flushing well before new references are refused, regardless of the caller's privileges. When returning "true" from __tlb_remove_folio_pages_size(), PTE zapping will flush the TLB before dropping the PT lock, but drop the references afterwards. Refaults can still race with that, but flushing early reduces the risk of letting others hit the limit. This is a temporary measure until we have something better. Reported-by: Chris Mason 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: Lance Yang Reviewed-by: Pedro Falcato Link: https://patch.msgid.link/20261112-refcount_overflow_stable-v4-3-ad3deef0bd23@kernel.org Signed-off-by: David Hildenbrand (Arm) [pfalcato: massage comment] Signed-off-by: Pedro Falcato Signed-off-by: Sasha Levin --- include/asm-generic/tlb.h | 6 +++--- include/linux/mm.h | 1 + mm/memory.c | 4 ++-- mm/mmu_gather.c | 10 ++++++---- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/include/asm-generic/tlb.h b/include/asm-generic/tlb.h index c046a09b0b487..b4f690a623056 100644 --- a/include/asm-generic/tlb.h +++ b/include/asm-generic/tlb.h @@ -73,9 +73,9 @@ * - __tlb_remove_folio_pages() * * __tlb_remove_page_size() is the basic primitive that queues a page for - * freeing. __tlb_remove_page() assumes PAGE_SIZE. Both will return a - * boolean indicating if the queue is (now) full and a call to - * tlb_flush_mmu() is required. + * freeing. __tlb_remove_page() assumes PAGE_SIZE. They return true when + * a call to tlb_flush_mmu() is required, either because the queue is full + * or a folio is near its refcount limit. * * tlb_remove_page() and tlb_remove_page_size() imply the call to * tlb_flush_mmu() when required and has no return value. diff --git a/include/linux/mm.h b/include/linux/mm.h index 6341c9433dfb1..e8c676fe84511 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1460,6 +1460,7 @@ static inline bool put_devmap_managed_folio_refs(struct folio *folio, int refs) /* Leave separate reserves for administrators and concurrent reference gains. */ #define FOLIO_REF_COUNT_LIMIT (INT_MAX - (1U << 26)) #define FOLIO_REF_COUNT_ADMIN_LIMIT (INT_MAX - (1U << 25)) +#define FOLIO_REF_COUNT_FLUSH_LIMIT (INT_MAX - (1U << 27)) bool folio_ref_count_over_limit_slowpath(const struct folio *folio, unsigned int ref_count); diff --git a/mm/memory.c b/mm/memory.c index 4840889461064..a6a127c5a8255 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1704,8 +1704,8 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, pte_unmap_unlock(start_pte, ptl); /* - * If we forced a TLB flush (either due to running out of - * batch buffers or because we needed to flush dirty TLB + * If we forced a TLB flush (either to release batched + * references or because we needed to flush dirty TLB * entries before releasing the ptl), free the batched * memory too. Come back again if we didn't do everything. */ diff --git a/mm/mmu_gather.c b/mm/mmu_gather.c index 04469b481b93f..fc705de6a3bb7 100644 --- a/mm/mmu_gather.c +++ b/mm/mmu_gather.c @@ -178,10 +178,7 @@ static bool __tlb_remove_folio_pages_size(struct mmu_gather *tlb, #endif batch = tlb->active; - /* - * Add the page and check if we are full. If so - * force a flush. - */ + /* Add the pages before deciding whether we need to flush. */ if (likely(nr_pages == 1)) { batch->encoded_pages[batch->nr++] = encode_page(page, flags); } else { @@ -189,6 +186,11 @@ static bool __tlb_remove_folio_pages_size(struct mmu_gather *tlb, batch->encoded_pages[batch->nr++] = encode_page(page, flags); batch->encoded_pages[batch->nr++] = encode_nr_pages(nr_pages); } + /* Flush pending references well before new references are refused. */ + if (unlikely((unsigned int)folio_ref_count(page_folio(page)) >= + FOLIO_REF_COUNT_FLUSH_LIMIT)) + return true; + /* * Make sure that we can always add another "page" + "nr_pages", * requiring two entries instead of only a single one. -- 2.53.0