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=1796382809; b=r26YMeLlttuqeMosd/2zaDyEoZMuikRxTnUrQQiC14O8jFoG3r/jBqkqmxk03TXQpoFw +cGdnqcOzfPU76/JS7Y1B6Gm+xrA6fPwjNJFy/29IEFmZ93J2AviK4HcoVF/QJbdkqGcI S1LLGRlMFWZ/D39SDPBpLQsxfU5AENsfcYmVOBXjibIjlDqf7/T0r8209p0laQdQZvew1 tFq5toaE7YTd4Ttcb75DFWLLQqRkht+AFhMzsKIOBDfhEbU8BT+zKt5RUK9tBT9o1rafv Xdiss0xgKSfxmGtkkVDaLp5f0Zcfp4Bu05XWu04TYxnXUpyKihRtprjhlxCxfizuOCg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382809; 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=o40mJ6zk8Ux10o0S21mHViOVi5Sip/Jzy7pvvaobpyU=; b=pdRbI2dLvENzuS2RHveR6mM62dK07VsdJXdvVz5oJmSEdxaux3byN2PsTQLJyI0iYCwu M85fFmATfWfoZgryv3xcTVpVGtRf0uE3t14m1AgYn2fH3/jmm+X/p9qztoKckYLcUwgKK h6qS80iXgr+okP0eRbG/Ne8DbxONGQhAguEe6q0F1K37eIJ/k4l0FM77RatYcw3r9iR4X 6q7rbo0I7X4i4PGvhlnxOYbU7VHC+cVCJBygwG7rIIBRBvNOIbGiU1NElyWI6N9fSkWVT Y7w1hPxZNRvmsPsqkjstW0UBaKyFztnARZc2B+8O0yqG8i8kAmgJY1qzqhd1NLb0OnA== 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=hlX++bzZ; 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 04BF7606A688; Fri, 4 Dec 2026 11:13:29 +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=hlX++bzZ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 42F6E1F000C2; Fri, 4 Dec 2026 11:13:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382808; bh=o40mJ6zk8Ux10o0S21mHViOVi5Sip/Jzy7pvvaobpyU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=hlX++bzZF1F3ZOybdIoUlOX9bKitSfUEH3udMZxKLOx774qrLAx+ZsxvVYaurRxYl CJEppk0h9hwbR1933NMGfchpqnLjFLfkcLZX2MMzimDJiNPEisBsB3SvElQywZiVgE PPNco6EAc/pLaSsDjhwn+IrRLlK99s3HIN7kclMw= 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.18 608/703] mm/mmu_gather: flush pending references early before the folio limit Date: Fri, 4 Dec 2026 19:08:27 +0900 Message-ID: <20261204100643.945804082@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 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) Signed-off-by: Pedro Falcato Signed-off-by: Sasha Levin --- include/asm-generic/tlb.h | 4 ++-- include/linux/mm.h | 1 + mm/memory.c | 4 ++-- mm/mmu_gather.c | 10 ++++++---- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/include/asm-generic/tlb.h b/include/asm-generic/tlb.h index 16dd46d376559..d8553ef897e08 100644 --- a/include/asm-generic/tlb.h +++ b/include/asm-generic/tlb.h @@ -78,8 +78,8 @@ * - __tlb_remove_folio_pages_size() * * __tlb_remove_folio_pages_size() is the basic primitive that queues pages - * for freeing. It will return a boolean indicating if the queue is (now) - * full and a call to tlb_flush_mmu() is required. + * for freeing. It returns 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 7b373294359b5..e089fd3875f8e 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1476,6 +1476,7 @@ vm_fault_t finish_fault(struct vm_fault *vmf); /* 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 0473862703030..9bc3162edccfe 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1915,8 +1915,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 5619ee967f3c7..b01ba6456dae1 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