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=1796221298; b=JnHlJdQnGC0Ut+9CGIL0hEG/flqUOjPTKkJsKW49lDKLVQtxahboZ8Nz+vrWcw3MU37c KrI3Qoq5KJfvyjwAESsMgJ06Yd+sDQsFQhKBBTtjEEO5ApjXcSVVAGmvMoz/PqC9cDJAB PHhDi61OzsvzKmiP6RMKAKLa4coxWDLdsF7CXE4MryPWJLEefw2nyuRaJbTbMDKdiRx2D /P4FYblceJsWOf7pk1GjhkYRwBdpyR0u/5wn/CMJ6gRRGjkILy+1cPFNXN6IUsZCARRQL 48Aq9GYT0UGE85n+UCLZENh3XdVM0jjDhU/mYdnSn7ln78h5Ew7Vls86+v3BiGL2rqg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796221298; h=Received:DKIM-Signature:Subject:To:Cc:From:Date:Message-ID: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=dB9Vqs7HY+kkMBHzmH3eSv+eu0jHuBZIAifOzOQafrA=; b=rH5ecInZM8yDHPNvTjjt5wCdMP7Yv9Q7odvOZ7CaQGVyoeYrsWrgcLR2sElJGD5rLIsw K56Y+qkBpLwk962U4E/xB0Utw+aQMlhJ33jfsF8U97g/K9dZx9LZ9tXok27degQbJNSPU H29l+i32CRnMj9P+mW1GK2gOyx/xezMmXIEMrqOzMw6xSGrEVV+hG4R+8OPmBUMNLDEff Fi56rL60Js5WL2zy91lYvOlvSqLPZ+vwQemZdHe1E3KwLhvo17P0NOeac85G1U8fBrKkH u6Q5z+9edfB38HpHVqYuqz80sHPebwxZkKFqeBA4ILO3EeuesyKjVSDlBUshvAsQcRA== 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=uVuJzeI7; 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 99683606A31A for ; Wed, 2 Dec 2026 14:21:38 +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=uVuJzeI7 Received: by smtp.kernel.org (Postfix) with ESMTPSA id EADC71F00897; Wed, 2 Dec 2026 14:21:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796221298; bh=dB9Vqs7HY+kkMBHzmH3eSv+eu0jHuBZIAifOzOQafrA=; h=Subject:To:Cc:From:Date; b=uVuJzeI7gePp8/x+ljCOYJbQJghgnQgp8pnQhbEBBLi+u3PWquDCWhrdcSLaJrdtj 4d7k90+oryIIu/Rn3K+5e1inuHc4oMvyZdP3fEpnEEOg+RqPYa6kmjTL6KMFHomeeR qY4FvoEOzpEdLfXjPhw7OdY7fCPcDpnQzE7JO5pc= Subject: FAILED: patch "[PATCH] mm/mmu_gather: flush pending references early before the" failed to apply to 6.18-stable tree To: david@kernel.org,azpijr@gmail.com,lance.yang@linux.dev,mason@kernel.org,pfalcato@suse.de Cc: From: Date: Wed, 02 Dec 2026 14:37:53 +0100 Message-ID: <2026120253-refurnish-attic-c785@gregkh> 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-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit The patch below does not apply to the 6.18-stable tree. If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to . To reproduce the conflict and resubmit, you may use the following commands: git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y git checkout FETCH_HEAD git cherry-pick -x 317bd7d28b248fd4d4e14614d7c30b17a36ef4d7 # git commit -s git send-email --to '' --in-reply-to '2026120253-refurnish-attic-c785@gregkh' --subject-prefix 'PATCH 6.18.y' 'HEAD^..' Possible dependencies: thanks, greg k-h ------------------ original commit in Linus's tree ------------------ >From 317bd7d28b248fd4d4e14614d7c30b17a36ef4d7 Mon Sep 17 00:00:00 2001 From: "David Hildenbrand (Arm)" Date: Thu, 12 Nov 2026 23:53:22 +0100 Subject: [PATCH] mm/mmu_gather: flush pending references early before the folio limit 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) diff --git a/include/asm-generic/tlb.h b/include/asm-generic/tlb.h index 3f4a180b48f3..4bba02fd5d54 100644 --- a/include/asm-generic/tlb.h +++ b/include/asm-generic/tlb.h @@ -75,8 +75,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 95d5e9a13e51..12a090d8d07f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2252,6 +2252,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 115b994cabfd..7f019afc35ca 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2076,8 +2076,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 9f353f0e2ef4..e98397ac577c 100644 --- a/mm/mmu_gather.c +++ b/mm/mmu_gather.c @@ -179,10 +179,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 { @@ -190,6 +187,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.