From nobody Wed Dec 17 21:13:28 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF23FC41535 for ; Thu, 9 Nov 2023 04:59:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232378AbjKIE73 (ORCPT ); Wed, 8 Nov 2023 23:59:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230303AbjKIE7Y (ORCPT ); Wed, 8 Nov 2023 23:59:24 -0500 Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BBD39258E for ; Wed, 8 Nov 2023 20:59:21 -0800 (PST) X-AuditID: a67dfc5b-d6dff70000001748-fc-654c67269bde From: Byungchul Park To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: kernel_team@skhynix.com, akpm@linux-foundation.org, ying.huang@intel.com, namit@vmware.com, xhao@linux.alibaba.com, mgorman@techsingularity.net, hughd@google.com, willy@infradead.org, david@redhat.com, peterz@infradead.org, luto@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com Subject: [v4 1/3] mm/rmap: Recognize read-only TLB entries during batched TLB flush Date: Thu, 9 Nov 2023 13:59:06 +0900 Message-Id: <20231109045908.54996-2-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231109045908.54996-1-byungchul@sk.com> References: <20231109045908.54996-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrCLMWRmVeSWpSXmKPExsXC9ZZnoa5auk+qwcEeQ4s569ewWXze8I/N 4sWGdkaLr+t/MVs8/dTHYnF51xw2i3tr/rNanN+1ltVix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BwSAiYSPz8cYQex2QTUJW7c+MkMYosImEkc bP0DFOfiYBZ4wCQx9+0KRpCEsECwxPHFh9lAbBYBVYmn59+BxXkFTCWmP3vNBjFUXmL1hgNg gziBBv2Zuh3MFgKqmfp5P+MERq4FjAyrGEUy88pyEzNzTPWKszMq8zIr9JLzczcxAsN4We2f iTsYv1x2P8QowMGoxMObMMU7VYg1say4MvcQowQHs5II7wUTn1Qh3pTEyqrUovz4otKc1OJD jNIcLErivF7hqQlCAumJJanZqakFqUUwWSYOTqkGRpsSs+z6tFvLflbdjphjF8GqW+tws7ao 06hU4rRf+N1vJT0u7HcW3bi/8bbas6z927zZWYvlD5gsfJDPw3vvx2YW3QNNlUUrXvj8s240 553ae1I6xLdr06F7l/fVP6iv9PPb33WsW/RQrAVLtfxRnQuFKjU2j0wPae4MTJDxiudJttl+ +9BzJZbijERDLeai4kQAgfMvLV8CAAA= X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Functionally, no change. This is a preparation for migrc mechanism that requires to recognize read-only TLB entries and makes use of them to batch more aggressively. Signed-off-by: Byungchul Park --- arch/x86/include/asm/tlbflush.h | 3 +++ arch/x86/mm/tlb.c | 11 +++++++++++ include/linux/sched.h | 1 + mm/internal.h | 4 ++++ mm/rmap.c | 30 +++++++++++++++++++++++++++++- 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflus= h.h index 25726893c6f4..5c618a8821de 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -292,6 +292,9 @@ static inline void arch_flush_tlb_batched_pending(struc= t mm_struct *mm) } =20 extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch); +extern void arch_tlbbatch_clear(struct arch_tlbflush_unmap_batch *batch); +extern void arch_tlbbatch_fold(struct arch_tlbflush_unmap_batch *bdst, + struct arch_tlbflush_unmap_batch *bsrc); =20 static inline bool pte_flags_need_flush(unsigned long oldflags, unsigned long newflags, diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 453ea95b667d..d3c89a3d91eb 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1274,6 +1274,17 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_= batch *batch) put_cpu(); } =20 +void arch_tlbbatch_clear(struct arch_tlbflush_unmap_batch *batch) +{ + cpumask_clear(&batch->cpumask); +} + +void arch_tlbbatch_fold(struct arch_tlbflush_unmap_batch *bdst, + struct arch_tlbflush_unmap_batch *bsrc) +{ + cpumask_or(&bdst->cpumask, &bdst->cpumask, &bsrc->cpumask); +} + /* * Blindly accessing user memory from NMI context can be dangerous * if we're in the middle of switching the current user task or diff --git a/include/linux/sched.h b/include/linux/sched.h index 77f01ac385f7..8a31527d9ed8 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1324,6 +1324,7 @@ struct task_struct { #endif =20 struct tlbflush_unmap_batch tlb_ubc; + struct tlbflush_unmap_batch tlb_ubc_ro; =20 /* Cache last used pipe for splice(): */ struct pipe_inode_info *splice_pipe; diff --git a/mm/internal.h b/mm/internal.h index 30cf724ddbce..9764b240e259 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -861,6 +861,7 @@ extern struct workqueue_struct *mm_percpu_wq; void try_to_unmap_flush(void); void try_to_unmap_flush_dirty(void); void flush_tlb_batched_pending(struct mm_struct *mm); +void fold_ubc_ro(void); #else static inline void try_to_unmap_flush(void) { @@ -871,6 +872,9 @@ static inline void try_to_unmap_flush_dirty(void) static inline void flush_tlb_batched_pending(struct mm_struct *mm) { } +static inline void fold_ubc_ro(void) +{ +} #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ =20 extern const struct trace_print_flags pageflag_names[]; diff --git a/mm/rmap.c b/mm/rmap.c index 9f795b93cf40..c787ae94b4c6 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -605,6 +605,28 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio= *folio, } =20 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH + +void fold_ubc_ro(void) +{ + struct tlbflush_unmap_batch *tlb_ubc =3D ¤t->tlb_ubc; + struct tlbflush_unmap_batch *tlb_ubc_ro =3D ¤t->tlb_ubc_ro; + + if (!tlb_ubc_ro->flush_required) + return; + + /* + * Fold tlb_ubc_ro's data to tlb_ubc. + */ + arch_tlbbatch_fold(&tlb_ubc->arch, &tlb_ubc_ro->arch); + tlb_ubc->flush_required =3D true; + + /* + * Reset tlb_ubc_ro's data. + */ + arch_tlbbatch_clear(&tlb_ubc_ro->arch); + tlb_ubc_ro->flush_required =3D false; +} + /* * Flush TLB entries for recently unmapped pages from remote CPUs. It is * important if a PTE was dirty when it was unmapped that it's flushed @@ -615,6 +637,7 @@ void try_to_unmap_flush(void) { struct tlbflush_unmap_batch *tlb_ubc =3D ¤t->tlb_ubc; =20 + fold_ubc_ro(); if (!tlb_ubc->flush_required) return; =20 @@ -645,13 +668,18 @@ void try_to_unmap_flush_dirty(void) static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval, unsigned long uaddr) { - struct tlbflush_unmap_batch *tlb_ubc =3D ¤t->tlb_ubc; + struct tlbflush_unmap_batch *tlb_ubc; int batch; bool writable =3D pte_dirty(pteval); =20 if (!pte_accessible(mm, pteval)) return; =20 + if (pte_write(pteval) || writable) + tlb_ubc =3D ¤t->tlb_ubc; + else + tlb_ubc =3D ¤t->tlb_ubc_ro; + arch_tlbbatch_add_pending(&tlb_ubc->arch, mm, uaddr); tlb_ubc->flush_required =3D true; =20 --=20 2.17.1