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=1796381095; b=Tr79Ax8b+Vxk00Sn2+rZ2KE2SIN1Q8EXALi5gMZh22P0YnclRT5mzZW6XIOmzH1okS7T Dl0XKd9THNZtKq1os/h70o1m3SXkLRuanpPByXgf5388ZepAdBql3GfIxxL9uiliK6oA8 fdfmYknLeeMQPybbASJ+CJcBeYGLBL3VhfFF/fNvtDTnnbcw369cIytYpUXb0fycIsnUB 1k2Nc4fBCYXUnHahsXm7xj4sDabYUhGrBif02djHsBeXQCBpNHlbt2zg+UuLyMoQRuBUy ADBfwZaAZc9KwCtJdXWsm67g5hK2nk67lk5sEFlDnXp77Dv2Yl9/51nxSUf91SMFGeQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381095; 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=9KPH9qBrcWv48LZS2aGWbNH7XCKIAmhZ1ikNfuxsjSk=; b=BSm18EjtZnY3v+wX04d0Q15aMfhpzin/DfWVMosvZBS8IYb4rJ9T1cq15m4kL2e1Dvwr ZLrtA3vsQ0g0qmb+spPiZSnalaCHk5VWERMrKv29Hs6FHcbjF4wnFM9Naz0eKdysto6VW gfLwFSH8RFiwVM4/4I8+VB2iCZKYrqwLcU5cUufII35WHucssCXsdO0eTK/Y0Lo7eGcHR kHZLnWo7shdJEjoCWK0YOwwK7qJOvbfFJkIy1RZbMlyPswJwswmjYFlAvjngx/IwsP4jP cUX3bKRXhaIPX2HeGtl7c7FzTkK93P6KvOI0n8WOazOpvO9TqCDWGgSELiAcGOXap9A== 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=nUh3swxB; 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 2C965606A32C; Fri, 4 Dec 2026 10:44:54 +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=nUh3swxB Received: by smtp.kernel.org (Postfix) with ESMTPSA id 540A71F000C2; Fri, 4 Dec 2026 10:44:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381094; bh=9KPH9qBrcWv48LZS2aGWbNH7XCKIAmhZ1ikNfuxsjSk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=nUh3swxB7sWBtSCtY1lYpE+EEElRCHrQXK4lObu5MGaT3ejcior3TVxHO/6E6jz2g YZwPi3RgpzF1VFdzjaS0woU8Opji2jBzK4hh//q2NdPNDS4pV+yVHF3EK8vlnTP3tl NO6naAAVK/2LxBwMSWZ+W2ZlT7FljuOJsbBn408o= 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)" Subject: [PATCH 7.3 556/742] mm/mmu_gather: flush pending references early before the folio limit Date: Fri, 4 Dec 2026 19:07:00 +0900 Message-ID: <20261204100649.918876417@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: 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 7.3-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: Greg Kroah-Hartman --- 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(-) --- 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. --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2111,6 +2111,7 @@ vm_fault_t finish_fault(struct vm_fault /* 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); --- a/mm/memory.c +++ b/mm/memory.c @@ -2113,8 +2113,8 @@ retry: 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. */ --- a/mm/mmu_gather.c +++ b/mm/mmu_gather.c @@ -179,10 +179,7 @@ static bool __tlb_remove_folio_pages_siz #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_siz 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.