From patchwork Thu Jul 20 16:30:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 13320803 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5DAFEB64DA for ; Thu, 20 Jul 2023 16:34:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 731F2280141; Thu, 20 Jul 2023 12:34:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E1F428004C; Thu, 20 Jul 2023 12:34:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53487280141; Thu, 20 Jul 2023 12:34:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4344628004C for ; Thu, 20 Jul 2023 12:34:40 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1B84D401D8 for ; Thu, 20 Jul 2023 16:34:40 +0000 (UTC) X-FDA: 81032538720.21.E1754C1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf13.hostedemail.com (Postfix) with ESMTP id 4488220023 for ; Thu, 20 Jul 2023 16:34:37 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OcU9zw0h; spf=pass (imf13.hostedemail.com: domain of vschneid@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=vschneid@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689870878; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=qlKugqrUieeoCK15s+XaSmSpmpWi+6cYdLHEecxMVFQ=; b=7fUZyN6PFF/QGuaeqOq2bgAf+ZrvF7BvaUr4vYDPy93gChxcIaO2Knw579h86tUDwveqq4 nuo5sHkR7PZKQVZvKVfUpjvf+fbkf32kemXxiKS9p058f+ARGbPMmm29XUbRXASDZuheam sRoB7v3mIsq1+6GPUbdTLt9pk3/8lMw= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OcU9zw0h; spf=pass (imf13.hostedemail.com: domain of vschneid@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=vschneid@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689870878; a=rsa-sha256; cv=none; b=N60fUKVej2HDF1CAkybyJ9tlTgsp3nY3oAEc+y+Pn15m5k1PgHQwiG5ivoOwgooebohALL FD/wQPige0WXcsYC2xSR1fVnJryXctbtYbTwni1fjvgMAjYXnL5dOu9t5nWKsIRix5b/Wu Jrm3oHm6UJgb+6DUq6spvdkG0SzW/b8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689870877; 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=qlKugqrUieeoCK15s+XaSmSpmpWi+6cYdLHEecxMVFQ=; b=OcU9zw0h7zBApjuoQctxqPIx+OV5Yk94dSB3XH1Cc4Mb2vtV1zGd2B6CHa9w0kLD4d5YxZ 09Lw5NcGCCla97nSdJLkcF/SDLGqlXwjev+LmeIdzpo3kddNrFyyAyJzQ4uJfoj6wdSTep xH60arkvKDWjLkvVfuUMjJga9KH9Sbc= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-240-myilGmoaNUKUY_DOur6_gg-1; Thu, 20 Jul 2023 12:34:35 -0400 X-MC-Unique: myilGmoaNUKUY_DOur6_gg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 95D7E3815EEF; Thu, 20 Jul 2023 16:34:32 +0000 (UTC) Received: from vschneid.remote.csb (unknown [10.42.28.48]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 28EAC40C2070; Thu, 20 Jul 2023 16:34:24 +0000 (UTC) From: Valentin Schneider To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, x86@kernel.org, rcu@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Steven Rostedt , Masami Hiramatsu , Jonathan Corbet , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Paolo Bonzini , Wanpeng Li , Vitaly Kuznetsov , Andy Lutomirski , Peter Zijlstra , Frederic Weisbecker , "Paul E. McKenney" , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Andrew Morton , Uladzislau Rezki , Christoph Hellwig , Lorenzo Stoakes , Josh Poimboeuf , Jason Baron , Kees Cook , Sami Tolvanen , Ard Biesheuvel , Nicholas Piggin , Juerg Haefliger , Nicolas Saenz Julienne , "Kirill A. Shutemov" , Nadav Amit , Dan Carpenter , Chuang Wang , Yang Jihong , Petr Mladek , "Jason A. Donenfeld" , Song Liu , Julian Pidancet , Tom Lendacky , Dionna Glaze , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Juri Lelli , Daniel Bristot de Oliveira , Marcelo Tosatti , Yair Podemsky Subject: [RFC PATCH v2 19/20] context_tracking,x86: Add infrastructure to defer kernel TLBI Date: Thu, 20 Jul 2023 17:30:55 +0100 Message-Id: <20230720163056.2564824-20-vschneid@redhat.com> In-Reply-To: <20230720163056.2564824-1-vschneid@redhat.com> References: <20230720163056.2564824-1-vschneid@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Rspamd-Queue-Id: 4488220023 X-Rspam-User: X-Stat-Signature: ry8698oo9wmkutqccaphuz18wd19oett X-Rspamd-Server: rspam01 X-HE-Tag: 1689870877-841647 X-HE-Meta: U2FsdGVkX1+JDD+f6Z6/oEf7AOf4jODjoCHLisIluzFCX915mPKnk3tbwMvgk8oL4Jd343QQ/vWh0rO+qjAdOBdV2MvWRqZYBBiqPyuxkJak78jsAUBuxsLIOFZyjwmS215FCzedjeP2MICACRJJaBjS4dIGicSqVX6dUvWgvKxA72eHvn15DQjl+B4fTHoVHoSXsnpdIUaSxh5n8rTPTeEkjcWnbJQ5Di1CvAjcR2iYYY7ziK3loz+nMtjy4fKW+8z8V5NetcDiDKhbhRpxxuQppVXydG/eHJCMOMZJG4R+FHY35s/wIGPGXZqgBu+qUAIqA+heXzIMPhvPpiN8/ChIhHntS1CSBFc3O5uNRkDH0DvZqQz+vj9dgtoXzxbWUTsLkMXaeO3MIzNwhwq9zxr0aClVmKx/FweWUu40zZs+UoO/mf/HVM3Brz+uceJpyML/P7tKMfn1PVuWTb+TGgI6KiSlhEUvIwHuWuX91Sqy4XWCVZK1H9x8Oj5StEjp8Kd7iZObgyNV6FrCmWc66kIwqGL6FPsu5UyBCsa+VxJkpY7qVwcA/dBWv7Z5T+aAHUss8eoYOA8MZ31us/4lHjkQijq+03DAG/J087s6EMDMJ9gSbm/ZvWleKIdLjeLC8vs6/VAY7zkFWWznuI9ipwJmW2I1a3bpByDiNGPXOgy/84jXu8b5YArv9qLmlt9F2LA2Ul78Js4nPzbIkPnjm5hRk9YTBqlSuRtEAxEwh2/VQ5OqaVaD9XcHzPcBXQ6UoWXhTic+WNm92xaG6NSpGaPfRkgIExHjMW++CfPsdyLqv7KKyXZEf/cnPDlL/XoA9cbk45PVL7P0IthCADorLGCtJKSdAGw1jNRj9slMr4D5d+QanX9gRs0DB5kqtgLyHhqdQ9cKJ019FPsRqAxFFmYGGpDdMW8pBSytyqdfvIRBHglrLFrIdYIRZPvm+plUnR/rPQUKP4zTwiOm4W2 E6U08hjT OHFHRWvccZUN6KZAU2V95Ff+0635nr4DqIkdqIHJrco1IXtMRbInFJUG0FBtcstaglQArc+ZX/GAQI39afyxCY1xT+Wf81qbc7VHGQeGSkrE+99W4orBtUZkgOegDQUXipWr129/NWwyH5GgMQCW6uJxCbkeDAFm6O5BGFdWkFa8IMT1Yvm8ioFxNWs0wehH2+LIqrpLlFWPnPuJiOXoLVP30Ym71+eNxa12tRW3RIBmJG3kYTV7xJp8GWPDPxtGZOiC9IuRtiayYMY7oeVegHL6h9xVfdegGMVyq 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: Kernel TLB invalidation IPIs are a common source of interference on NOHZ_FULL CPUs. Given NOHZ_FULL CPUs executing in userspace are not accessing any kernel addresses, these invalidations do not need to happen immediately, and can be deferred until the next user->kernel transition. Rather than make __flush_tlb_all() noinstr, add a minimal noinstr variant that doesn't try to leverage INVPCID. FIXME: not fully noinstr compliant XXX: same issue as with ins patching, when do we access data that should be invalidated? Signed-off-by: Valentin Schneider --- arch/x86/include/asm/context_tracking_work.h | 4 ++++ arch/x86/include/asm/tlbflush.h | 1 + arch/x86/mm/tlb.c | 17 +++++++++++++++++ include/linux/context_tracking_state.h | 4 ++++ include/linux/context_tracking_work.h | 2 ++ 5 files changed, 28 insertions(+) diff --git a/arch/x86/include/asm/context_tracking_work.h b/arch/x86/include/asm/context_tracking_work.h index 2c66687ce00e2..9d4f021b5a45b 100644 --- a/arch/x86/include/asm/context_tracking_work.h +++ b/arch/x86/include/asm/context_tracking_work.h @@ -3,6 +3,7 @@ #define _ASM_X86_CONTEXT_TRACKING_WORK_H #include +#include static __always_inline void arch_context_tracking_work(int work) { @@ -10,6 +11,9 @@ static __always_inline void arch_context_tracking_work(int work) case CONTEXT_WORK_SYNC: sync_core(); break; + case CONTEXT_WORK_TLBI: + __flush_tlb_all_noinstr(); + break; } } diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 80450e1d5385a..323b971987af7 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -17,6 +17,7 @@ DECLARE_PER_CPU(u64, tlbstate_untag_mask); void __flush_tlb_all(void); +void noinstr __flush_tlb_all_noinstr(void); #define TLB_FLUSH_ALL -1UL #define TLB_GENERATION_INVALID 0 diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 267acf27480af..631df9189ded4 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1237,6 +1237,23 @@ void __flush_tlb_all(void) } EXPORT_SYMBOL_GPL(__flush_tlb_all); +void noinstr __flush_tlb_all_noinstr(void) +{ + /* + * This is for invocation in early entry code that cannot be + * instrumented. A RMW to CR4 works for most cases, but relies on + * being able to flip either of the PGE or PCIDE bits. Flipping CR4.PCID + * would require also resetting CR3.PCID, so just try with CR4.PGE, else + * do the CR3 write. + * + * TODO: paravirt + */ + if (cpu_feature_enabled(X86_FEATURE_PGE)) + __native_tlb_flush_global(this_cpu_read(cpu_tlbstate.cr4)); + else + flush_tlb_local(); +} + void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) { struct flush_tlb_info *info; diff --git a/include/linux/context_tracking_state.h b/include/linux/context_tracking_state.h index 292a0b7c06948..3571c62cbb9cd 100644 --- a/include/linux/context_tracking_state.h +++ b/include/linux/context_tracking_state.h @@ -62,6 +62,10 @@ enum ctx_state { #define RCU_DYNTICKS_END (CT_STATE_SIZE - 1) #define RCU_DYNTICKS_IDX BIT(RCU_DYNTICKS_START) +/* + * When CONFIG_CONTEXT_TRACKING_WORK=n, _END is 1 behind _START, which makes + * the CONTEXT_WORK size computation below 0, which is what we want! + */ #define CONTEXT_WORK_START (CONTEXT_STATE_END + 1) #define CONTEXT_WORK_END (RCU_DYNTICKS_START - 1) diff --git a/include/linux/context_tracking_work.h b/include/linux/context_tracking_work.h index 13fc97b395030..47d5ced39a43a 100644 --- a/include/linux/context_tracking_work.h +++ b/include/linux/context_tracking_work.h @@ -6,11 +6,13 @@ enum { CONTEXT_WORK_SYNC_OFFSET, + CONTEXT_WORK_TLBI_OFFSET, CONTEXT_WORK_MAX_OFFSET }; enum ct_work { CONTEXT_WORK_SYNC = BIT(CONTEXT_WORK_SYNC_OFFSET), + CONTEXT_WORK_TLBI = BIT(CONTEXT_WORK_TLBI_OFFSET), CONTEXT_WORK_MAX = BIT(CONTEXT_WORK_MAX_OFFSET) };