From patchwork Thu Nov 9 04:59:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 163220 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp220291vqs; Wed, 8 Nov 2023 20:59:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IGjhy5s+NP5yxxZHMNMfdNhBb0a+gbivEXQs/s73wVIPxfMKxEOPVsL2KbhDHsS6a+W/fxz X-Received: by 2002:a9d:77d1:0:b0:6cd:da93:90ce with SMTP id w17-20020a9d77d1000000b006cdda9390cemr3976000otl.19.1699505997049; Wed, 08 Nov 2023 20:59:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699505997; cv=none; d=google.com; s=arc-20160816; b=BPzosnglN0MZEuTbZzmZd4ZjUhmcWhZktbnGj13XthbHkhydd8wXhcglUWJrV44j5I esg7ELh3m8GpJf4nsZhlFg2efchj9SGE6PvhiKTQth5goQ4KNHUDFv6icGkocV8vq2T2 1/xOSbpOKXg3YNvPAG4oJkvmuooFFY/PgygnZET+kg1qJUoYDxjYyMv9vOutF2Pkh+FW 84ijkdvClBztqu8rvMplGxCz6RsupRGf6slXOuc6AUnJJr1c1ysYWwdacrhrPRHoR3CJ 9pBwnrhI7Clpp9os5xSnWNy11WTBCu7nYNt38dNVXcq8Nb7KJL3iIECsC18H9P/1QLWV dwNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=tMkuIFDucDn4K8UkLSIcjaBo7QuWngab7iODf/vA+Gk=; fh=qkutryktk1+eA6li8Kr0a/P8xPDRQ0wHxGCKWFfNW0w=; b=ttJO4Spp0s+XzWlATz9wkh5gKYJndtxFeORYQ/ycpyIRvR4HcU9tEA79rih8fxPlBh blhCLhnbB5lMYo33fbmGigcvNpdaMzn8xrGAvF0+V60euIcDTE6s74BL1E25Y8zaHouw hkNF/F/fHHK64ue4PfIH9O514iGDNVCR0ZuumWSs+2h76yVoIMKNTGY8109BstmCElRt Y9TG1xUfp0IAo+sRHasQ7MyTkUcs8bIl4nVTfjoOu9lSX097h4snv0ckHF0HZr13Vld3 BdLqyNhbm5Cr7faR94ElWw4akQbNC+RTVH/Cbd9BSd0gtgxXCl9PoR/pHEqp4S3qg11+ fAoA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id u188-20020a6279c5000000b006c3415c3214si14206536pfc.48.2023.11.08.20.59.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 20:59:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 0D61C826CB57; Wed, 8 Nov 2023 20:59:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232367AbjKIE72 (ORCPT + 32 others); Wed, 8 Nov 2023 23:59:28 -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+1ltVix9J9TBaXDixgsri+6yGjxfHeA0wW mzdNZbb4/QOobs4UK4uTsyazOAh4fG/tY/FYsKnUY/MKLY/Fe14yeWxa1cnmsenTJHaPd+fO sXucmPGbxWPnQ0uPeScDPd7vu8rmsfWXncfnTXIe7+a/ZQvgi+KySUnNySxLLdK3S+DKWPpm KUvBLZWKGU82MjcwrpPrYuTgkBAwkfi3SKSLkRPM/PnhCDuIzSagLnHjxk9mEFtEwEziYOsf oDgXB7PAAyaJuW9XMIIkhAWCJY4vPswGYrMIqEo8Pf8OLM4rYCox/dlrNoih8hKrNxwAG8QJ NOjP1O1gthBQzdTP+xlBhkoIvGeTWL3zADtEg6TEwRU3WCYw8i5gZFjFKJSZV5abmJljopdR mZdZoZecn7uJERj2y2r/RO9g/HQh+BCjAA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LtfxRnQuFKjU2j0wPae4MTJDxiudJttl+ +9BzJZbijERDLeai4kQAgfMvLV8CAAA= X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 08 Nov 2023 20:59:52 -0800 (PST) X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782061200344805771 X-GMAIL-MSGID: 1782061200344805771 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/tlbflush.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(struct mm_struct *mm) } 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); 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(); } +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 struct tlbflush_unmap_batch tlb_ubc; + struct tlbflush_unmap_batch tlb_ubc_ro; /* 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 */ 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, } #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH + +void fold_ubc_ro(void) +{ + struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; + struct tlbflush_unmap_batch *tlb_ubc_ro = ¤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 = true; + + /* + * Reset tlb_ubc_ro's data. + */ + arch_tlbbatch_clear(&tlb_ubc_ro->arch); + tlb_ubc_ro->flush_required = 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 = ¤t->tlb_ubc; + fold_ubc_ro(); if (!tlb_ubc->flush_required) return; @@ -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 = ¤t->tlb_ubc; + struct tlbflush_unmap_batch *tlb_ubc; int batch; bool writable = pte_dirty(pteval); if (!pte_accessible(mm, pteval)) return; + if (pte_write(pteval) || writable) + tlb_ubc = ¤t->tlb_ubc; + else + tlb_ubc = ¤t->tlb_ubc_ro; + arch_tlbbatch_add_pending(&tlb_ubc->arch, mm, uaddr); tlb_ubc->flush_required = true;