From patchwork Mon Feb 3 20:17:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Arcangeli X-Patchwork-Id: 11363391 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CC4721398 for ; Mon, 3 Feb 2020 20:18:03 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7E6E82084E for ; Mon, 3 Feb 2020 20:18:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="agWsnF1z" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7E6E82084E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 7DA056B0007; Mon, 3 Feb 2020 15:18:02 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7630A6B0008; Mon, 3 Feb 2020 15:18:02 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 69F9A6B000A; Mon, 3 Feb 2020 15:18:02 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0108.hostedemail.com [216.40.44.108]) by kanga.kvack.org (Postfix) with ESMTP id 50EAA6B0007 for ; Mon, 3 Feb 2020 15:18:02 -0500 (EST) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id ED2E8181AEF10 for ; Mon, 3 Feb 2020 20:18:01 +0000 (UTC) X-FDA: 76449927162.05.rule59_bcab30e99847 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,aarcange@redhat.com,:will@kernel.org:catalin.marinas@arm.com:jcm@jonmasters.org:aquini@redhat.com:msalter@redhat.com:linux-kernel@vger.kernel.org::linux-arm-kernel@lists.infradead.org,RULES_HIT:30026:30034:30051:30054:30070:30080:30083,0,RBL:205.139.110.61:@redhat.com:.lbl8.mailshell.net-62.18.0.100 66.10.201.10,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:30,LUA_SUMMARY:none X-HE-Tag: rule59_bcab30e99847 X-Filterd-Recvd-Size: 11316 Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) by imf36.hostedemail.com (Postfix) with ESMTP for ; Mon, 3 Feb 2020 20:18:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580761080; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rnGScHbTlJ76sEJ4ZUeqFR3hYk6wSCyUU5+x/f3HazU=; b=agWsnF1zTNT134uj2vX8aypuI0jI1uZAyCATKnYnBnTLDWpIKwgJfiABp8ZF6a10MgFRaX dvRqZtRJq4c91u60vilmyJ7kxRPfwY68J1lKWA9/1NgpeATRZI8fTXJoJMkAON5i/4aelL yfM4Gwcsqp0OGOBItrJJSmcxIEYy+K8= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-46-8YXHLavUPaS-JWZqlrwJfw-1; Mon, 03 Feb 2020 15:17:50 -0500 X-MC-Unique: 8YXHLavUPaS-JWZqlrwJfw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C42AC1005514; Mon, 3 Feb 2020 20:17:48 +0000 (UTC) Received: from mail (ovpn-120-67.rdu2.redhat.com [10.10.120.67]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0D6E210018FF; Mon, 3 Feb 2020 20:17:46 +0000 (UTC) From: Andrea Arcangeli To: Will Deacon , Catalin Marinas , Jon Masters , Rafael Aquini , Mark Salter Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH 2/2] arm64: tlb: skip tlbi broadcast for single threaded TLB flushes Date: Mon, 3 Feb 2020 15:17:45 -0500 Message-Id: <20200203201745.29986-3-aarcange@redhat.com> In-Reply-To: <20200203201745.29986-1-aarcange@redhat.com> References: <20200203201745.29986-1-aarcange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: With multiple NUMA nodes and multiple sockets, the tlbi broadcast shall be delivered through the interconnects in turn increasing the interconnect traffic and the latency of the tlbi broadcast instruction. Even within a single NUMA node the latency of the tlbi broadcast instruction increases almost linearly with the number of CPUs trying to send tlbi broadcasts at the same time. When the process is single threaded however we can achieve full SMP scalability by skipping the tlbi broadcasting. Other arches already deploy this optimization. After the local TLB flush this however means the ASID context goes out of sync in all CPUs except the local one. This can be tracked in the mm_cpumask(mm): if the bit is set it means the asid context is stale for that CPU. This results in an extra local ASID TLB flush only if a single threaded process is migrated to a different CPU and only after a TLB flush. No extra local TLB flush is needed for the common case of single threaded processes context scheduling within the same CPU and for multithreaded processes. Skipping the tlbi instruction broadcasting is already implemented in local_flush_tlb_all(), this patch only extends it to flush_tlb_mm(), flush_tlb_range() and flush_tlb_page() too. Here's the result of 32 CPUs (ARMv8 Ampere) running mprotect at the same time from 32 single threaded processes before the patch: Performance counter stats for './loop' (3 runs): 0 dummy 2.121353 +- 0.000387 seconds time elapsed ( +- 0.02% ) and with the patch applied: Performance counter stats for './loop' (3 runs): 0 dummy 0.1197750 +- 0.0000827 seconds time elapsed ( +- 0.07% ) Signed-off-by: Andrea Arcangeli --- arch/arm64/include/asm/efi.h | 2 +- arch/arm64/include/asm/mmu.h | 3 +- arch/arm64/include/asm/mmu_context.h | 10 +-- arch/arm64/include/asm/tlbflush.h | 91 +++++++++++++++++++++++++++- arch/arm64/mm/context.c | 13 +++- 5 files changed, 109 insertions(+), 10 deletions(-) diff --git a/arch/arm64/include/asm/efi.h b/arch/arm64/include/asm/efi.h index 44531a69d32b..5d9a1433d918 100644 --- a/arch/arm64/include/asm/efi.h +++ b/arch/arm64/include/asm/efi.h @@ -131,7 +131,7 @@ static inline void efifb_setup_from_dmi(struct screen_info *si, const char *opt) static inline void efi_set_pgd(struct mm_struct *mm) { - __switch_mm(mm); + __switch_mm(mm, smp_processor_id()); if (system_uses_ttbr0_pan()) { if (mm != current->active_mm) { diff --git a/arch/arm64/include/asm/mmu.h b/arch/arm64/include/asm/mmu.h index e4d862420bb4..1f84289d3e44 100644 --- a/arch/arm64/include/asm/mmu.h +++ b/arch/arm64/include/asm/mmu.h @@ -27,7 +27,8 @@ typedef struct { * ASID change and therefore doesn't need to reload the counter using * atomic64_read. */ -#define ASID(mm) ((mm)->context.id.counter & 0xffff) +#define __ASID(asid) ((asid) & 0xffff) +#define ASID(mm) __ASID((mm)->context.id.counter) extern bool arm64_use_ng_mappings; diff --git a/arch/arm64/include/asm/mmu_context.h b/arch/arm64/include/asm/mmu_context.h index 3827ff4040a3..5ec264297968 100644 --- a/arch/arm64/include/asm/mmu_context.h +++ b/arch/arm64/include/asm/mmu_context.h @@ -210,10 +210,8 @@ enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) update_saved_ttbr0(tsk, &init_mm); } -static inline void __switch_mm(struct mm_struct *next) +static inline void __switch_mm(struct mm_struct *next, unsigned int cpu) { - unsigned int cpu = smp_processor_id(); - /* * init_mm.pgd does not contain any user mappings and it is always * active for kernel addresses in TTBR1. Just set the reserved TTBR0. @@ -230,8 +228,12 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, struct task_struct *tsk) { + unsigned int cpu = smp_processor_id(); + if (prev != next) - __switch_mm(next); + __switch_mm(next, cpu); + else if (cpumask_test_and_clear_cpu(cpu, mm_cpumask(next))) + local_flush_tlb_asid(atomic64_read(&next->context.id)); /* * Update the saved TTBR0_EL1 of the scheduled-in task as the previous diff --git a/arch/arm64/include/asm/tlbflush.h b/arch/arm64/include/asm/tlbflush.h index bc3949064725..283f97af3fc5 100644 --- a/arch/arm64/include/asm/tlbflush.h +++ b/arch/arm64/include/asm/tlbflush.h @@ -136,6 +136,15 @@ static inline void local_flush_tlb_all(void) isb(); } +static inline void local_flush_tlb_asid(unsigned long asid) +{ + asid = __TLBI_VADDR(0, __ASID(asid)); + dsb(nshst); + __tlbi(aside1, asid); + __tlbi_user(aside1, asid); + dsb(nsh); +} + static inline void flush_tlb_all(void) { dsb(ishst); @@ -148,6 +157,27 @@ static inline void flush_tlb_mm(struct mm_struct *mm) { unsigned long asid = __TLBI_VADDR(0, ASID(mm)); + /* avoid TLB-i broadcast to remote NUMA nodes if it's a local flush */ + if (current->mm == mm && atomic_read(&mm->mm_users) <= 1) { + int cpu = get_cpu(); + + cpumask_setall(mm_cpumask(mm)); + cpumask_clear_cpu(cpu, mm_cpumask(mm)); + + smp_mb(); + + if (atomic_read(&mm->mm_users) <= 1) { + dsb(nshst); + __tlbi(aside1, asid); + __tlbi_user(aside1, asid); + dsb(nsh); + + put_cpu(); + return; + } + put_cpu(); + } + dsb(ishst); __tlbi(aside1is, asid); __tlbi_user(aside1is, asid); @@ -167,7 +197,33 @@ static inline void flush_tlb_page_nosync(struct vm_area_struct *vma, static inline void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr) { - flush_tlb_page_nosync(vma, uaddr); + struct mm_struct *mm = vma->vm_mm; + unsigned long addr = __TLBI_VADDR(uaddr, ASID(mm)); + + /* avoid TLB-i broadcast to remote NUMA nodes if it's a local flush */ + if (current->mm == mm && atomic_read(&mm->mm_users) <= 1) { + int cpu = get_cpu(); + + cpumask_setall(mm_cpumask(mm)); + cpumask_clear_cpu(cpu, mm_cpumask(mm)); + + smp_mb(); + + if (atomic_read(&mm->mm_users) <= 1) { + dsb(nshst); + __tlbi(vale1, addr); + __tlbi_user(vale1, addr); + dsb(nsh); + + put_cpu(); + return; + } + put_cpu(); + } + + dsb(ishst); + __tlbi(vale1is, addr); + __tlbi_user(vale1is, addr); dsb(ish); } @@ -181,14 +237,15 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end, unsigned long stride, bool last_level) { - unsigned long asid = ASID(vma->vm_mm); + struct mm_struct *mm = vma->vm_mm; + unsigned long asid = ASID(mm); unsigned long addr; start = round_down(start, stride); end = round_up(end, stride); if ((end - start) >= (MAX_TLBI_OPS * stride)) { - flush_tlb_mm(vma->vm_mm); + flush_tlb_mm(mm); return; } @@ -198,6 +255,34 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, start = __TLBI_VADDR(start, asid); end = __TLBI_VADDR(end, asid); + /* avoid TLB-i broadcast to remote NUMA nodes if it's a local flush */ + if (current->mm == mm && atomic_read(&mm->mm_users) <= 1) { + int cpu = get_cpu(); + + cpumask_setall(mm_cpumask(mm)); + cpumask_clear_cpu(cpu, mm_cpumask(mm)); + + smp_mb(); + + if (atomic_read(&mm->mm_users) <= 1) { + dsb(nshst); + for (addr = start; addr < end; addr += stride) { + if (last_level) { + __tlbi(vale1, addr); + __tlbi_user(vale1, addr); + } else { + __tlbi(vae1, addr); + __tlbi_user(vae1, addr); + } + } + dsb(nsh); + + put_cpu(); + return; + } + put_cpu(); + } + dsb(ishst); for (addr = start; addr < end; addr += stride) { if (last_level) { diff --git a/arch/arm64/mm/context.c b/arch/arm64/mm/context.c index 8ef73e89d514..b44459d64dff 100644 --- a/arch/arm64/mm/context.c +++ b/arch/arm64/mm/context.c @@ -198,6 +198,7 @@ void check_and_switch_context(struct mm_struct *mm, unsigned int cpu) { unsigned long flags; u64 asid, old_active_asid; + int need_flush = 0; if (system_supports_cnp()) cpu_set_reserved_ttbr0(); @@ -233,7 +234,8 @@ void check_and_switch_context(struct mm_struct *mm, unsigned int cpu) atomic64_set(&mm->context.id, asid); } - if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending)) + need_flush = cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending); + if (need_flush) local_flush_tlb_all(); atomic64_set(&per_cpu(active_asids, cpu), asid); @@ -241,6 +243,15 @@ void check_and_switch_context(struct mm_struct *mm, unsigned int cpu) switch_mm_fastpath: + /* + * Enforce CPU ordering between the mmget() in use_mm() and + * the below cpumask_test_and_clear_cpu(). + */ + smp_mb(); + if (cpumask_test_and_clear_cpu(cpu, mm_cpumask(mm)) && + likely(!need_flush)) + local_flush_tlb_asid(asid); + arm64_apply_bp_hardening(); /*