Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp513857ybi; Wed, 3 Jul 2019 00:16:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqyrEjQxUuP2G8uGJjeoaZ5jojyHGTNiFPVwja/RdXmQV4XyP3KWRc72mqATYcPPi87lp+MD X-Received: by 2002:a17:902:f089:: with SMTP id go9mr40420341plb.81.1562138162361; Wed, 03 Jul 2019 00:16:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562138162; cv=none; d=google.com; s=arc-20160816; b=P/g85dLZTQFCtQmUADTHgoswo2sKpmVepPkbBsHfLogMK3LZ1c/MiGOUuwQKYdrSz+ 4xSHviF1O19aKIWaOBWIsbKHVQCZs33Ok4LR5DGriZZZiMlC9im89g/EXSQ1MaovcoYH HyBbdAXSG8TUXo0sAtuBC6mhrEf3oqYiD0doA+FK7th8yriws21s6ZoI2TTpJ+/d4SWl NS1Vij9QzwLwvwYFx/gGkmKNIWwCKXxhIYu5qDf/G+E/zUphRGKqfWBfpFsyIOi8O3qG Go5laaej2PBVR2eOZLPjV7XHUK+C7VlUYxzM8IwmA8QBDKhkLqz/WDmla/CUfdJ4Te2m hh4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=Vf519Hq+S5g1ICNSISpoe7Si+uS0J77hxLTRKQGpRN8=; b=HSxiYznjjLgXqrDCYC+fjdJNxPe5eTgHNK1C0ssPyUI+swKWsCzn/eKRJAwih260AH YQ8gmHloQ3KkmGUSqjIms54s3ciEd+rGf8099j0yHIZ3lJnvRxN9hkXBfm831bdqjDaH vt4a788bWuEKwexwoEYdCutGsHzSs+e0ElzuKgJJ5Ty3vCQkMMKxlncD9ioAvDIhyE/q emIsmoyCNrdbAH/sg7c1ZS+Hes13YH/OTA6aUwwp2qRdggDGGq6W92j7Wy3l6ZfxJDJD 85zSaFPPkvqIbydi2XRtNO4XJKHVQFsUNmKRAjtu4Xgs5XBGFglk2N5K3D/zEj32LqWP +XdA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=vmware.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 129si1538132pgc.522.2019.07.03.00.15.45; Wed, 03 Jul 2019 00:16:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=vmware.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727375AbfGCHOi (ORCPT + 99 others); Wed, 3 Jul 2019 03:14:38 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:43985 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727210AbfGCHOH (ORCPT ); Wed, 3 Jul 2019 03:14:07 -0400 Received: by mail-pg1-f194.google.com with SMTP id f25so715357pgv.10 for ; Wed, 03 Jul 2019 00:14:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Vf519Hq+S5g1ICNSISpoe7Si+uS0J77hxLTRKQGpRN8=; b=DaVmdDx1uY9cHma0p8iUqM7ULeiM7l7lI6zeWgieq677S0+QXfOAdgh1FU76cE92r6 XN8qWDNeqZ2+kc5IBVQouTA6PY86je+dLB4pMcQQUfYuC0rDhHRJAbVf2Sjj5VRNDZ9/ fLXtVadv98Hd79gS49plxDoDvRCnwV791q6nU3Qc4pO4xpzkeqaXcNlt1/IGWBaL56yW GBBM+ZNzVHJCNVucLRbxP88cYyYdFe1KF/g763hVsSRVz60c2wd5JOsSci2QQh4gRoTa p+UPkS3wVtBCZT+NlN5BcLRejfC4FRzhpQobcIBZIeEIOaHiI8gR52ffbqFxYvaraajT ySLw== X-Gm-Message-State: APjAAAXVDKqbeNQaycC9HUmNSScnqPlkyl31fFC4+k6ynWfYOmXsk5Hl e36lLmdBc9GOkYaYkhtI5wI= X-Received: by 2002:a17:90a:2488:: with SMTP id i8mr10625520pje.123.1562138046862; Wed, 03 Jul 2019 00:14:06 -0700 (PDT) Received: from sc2-haas01-esx0118.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id j21sm1256593pfh.86.2019.07.03.00.14.05 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 03 Jul 2019 00:14:06 -0700 (PDT) From: Nadav Amit To: Andy Lutomirski , Dave Hansen Cc: x86@kernel.org, linux-kernel@vger.kernel.org, Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Nadav Amit , Rik van Riel , Josh Poimboeuf Subject: [PATCH v2 3/9] x86/mm/tlb: Open-code on_each_cpu_cond_mask() for tlb_is_not_lazy() Date: Tue, 2 Jul 2019 16:51:45 -0700 Message-Id: <20190702235151.4377-4-namit@vmware.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190702235151.4377-1-namit@vmware.com> References: <20190702235151.4377-1-namit@vmware.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Open-code on_each_cpu_cond_mask() in native_flush_tlb_others() to optimize the code. Open-coding eliminates the need for the indirect branch that is used to call is_lazy(), and in CPUs that are vulnerable to Spectre v2, it eliminates the retpoline. In addition, it allows to use a preallocated cpumask to compute the CPUs that should be. This would later allow us not to adapt on_each_cpu_cond_mask() to support local and remote functions. Note that calling tlb_is_not_lazy() for every CPU that needs to be flushed, as done in native_flush_tlb_multi() might look ugly, but it is equivalent to what is currently done in on_each_cpu_cond_mask(). Actually, native_flush_tlb_multi() does it more efficiently since it avoids using an indirect branch for the matter. Cc: Peter Zijlstra Cc: Dave Hansen Cc: Rik van Riel Cc: Thomas Gleixner Cc: Andy Lutomirski Cc: Josh Poimboeuf Signed-off-by: Nadav Amit --- arch/x86/mm/tlb.c | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 8a957b58525f..5c9b1607191d 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -658,11 +658,13 @@ static void flush_tlb_func_remote(void *info) flush_tlb_func_common(f, false, TLB_REMOTE_SHOOTDOWN); } -static bool tlb_is_not_lazy(int cpu, void *data) +static bool tlb_is_not_lazy(int cpu) { return !per_cpu(cpu_tlbstate.is_lazy, cpu); } +static DEFINE_PER_CPU(cpumask_t, flush_tlb_mask); + void native_flush_tlb_others(const struct cpumask *cpumask, const struct flush_tlb_info *info) { @@ -706,12 +708,38 @@ void native_flush_tlb_others(const struct cpumask *cpumask, * up on the new contents of what used to be page tables, while * doing a speculative memory access. */ - if (info->freed_tables) + if (info->freed_tables) { smp_call_function_many(cpumask, flush_tlb_func_remote, (void *)info, 1); - else - on_each_cpu_cond_mask(tlb_is_not_lazy, flush_tlb_func_remote, - (void *)info, 1, GFP_ATOMIC, cpumask); + } else { + /* + * Although we could have used on_each_cpu_cond_mask(), + * open-coding it has performance advantages, as it eliminates + * the need for indirect calls or retpolines. In addition, it + * allows to use a designated cpumask for evaluating the + * condition, instead of allocating one. + * + * This code works under the assumption that there are no nested + * TLB flushes, an assumption that is already made in + * flush_tlb_mm_range(). + * + * cond_cpumask is logically a stack-local variable, but it is + * more efficient to have it off the stack and not to allocate + * it on demand. Preemption is disabled and this code is + * non-reentrant. + */ + struct cpumask *cond_cpumask = this_cpu_ptr(&flush_tlb_mask); + int cpu; + + cpumask_clear(cond_cpumask); + + for_each_cpu(cpu, cpumask) { + if (tlb_is_not_lazy(cpu)) + __cpumask_set_cpu(cpu, cond_cpumask); + } + smp_call_function_many(cond_cpumask, flush_tlb_func_remote, + (void *)info, 1); + } } /* @@ -865,7 +893,7 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) if (cpumask_test_cpu(cpu, &batch->cpumask)) { lockdep_assert_irqs_enabled(); local_irq_disable(); - flush_tlb_func_local(&full_flush_tlb_info); + flush_tlb_func_local((void *)&full_flush_tlb_info); local_irq_enable(); } -- 2.17.1