Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp287658pxa; Fri, 21 Aug 2020 07:19:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyDktBWcG/DPBUR6jjpueKCskbD1PZy5KJwklxt4uhyUUn0Wq11i/ZHsFbS+KdWM/0wPCm5 X-Received: by 2002:a17:906:15cc:: with SMTP id l12mr3229358ejd.7.1598019552297; Fri, 21 Aug 2020 07:19:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598019552; cv=none; d=google.com; s=arc-20160816; b=snFJfujV71xW/jfeQeT4vqMktGKUfJRGjVpJeVtzar5zv7WOL9h+FkkJyuBMEcxcgb ZjUCDBI8E5bj3P0mRWjgrB/I1Jzt6pySLQ1v0MRCMjOvqLA5uTm+SHV3/TG1N9/2cvDc KZ7o7Le+J17vv6wSMrOUsXc+aUFjJBaTVzmEIhsYtGI5CRYyXYIp37u5xhvhmNf7P1mR /a/pMS/CDJugtD/g1PXDf+IJQ3B114pCQx5uYbI0yavQ6YNI6In8Vf6eww1vK2lRvjwM yh21Otdds9oEAk6PzcUIgZqJzQdE73Vo5DSMq9AM0zfvnIDJ7Wd2LbC4FY13AEPgqxeZ uoTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=xxeM5gPH8yu6tWCUWA1p+lgxDN3K/oRI20zeA1Ye/AM=; b=CDg8tm0Rxks0qjCJ6GEdBiUOkOPyF+cQkuvRVACQ0PeQj/X2Vmr8KtuM088xr67nys ccXmf5xmEjryUltM8EOOiCMdji7oG+1Xqm7gpqrB1Ij340Gr4MFHm0jwu22vsyxitGnQ kjLhqKhMo7w1l/PDMEzcKzBuwLrsNQdHYQCstIxP8WbagE9ba8x15khKhCj4PaSHWULr Edy7wSEYw1dDgQeyRCn9u4S5c8SM0UUzLw5aNvvtDgr+bV+bmKrXMPCI5XpU0znthAcx LGF2z8yC3S6BGGzS5ShuhWVzcUnBlBgcTaDck/quOgHDo/jsjtxIcrq20BFrBUvRkeF4 Lh1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="KnVRt/vE"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ci27si1413966ejc.22.2020.08.21.07.18.47; Fri, 21 Aug 2020 07:19:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="KnVRt/vE"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727785AbgHUOPS (ORCPT + 99 others); Fri, 21 Aug 2020 10:15:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726993AbgHUOPR (ORCPT ); Fri, 21 Aug 2020 10:15:17 -0400 Received: from mail-il1-x143.google.com (mail-il1-x143.google.com [IPv6:2607:f8b0:4864:20::143]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0442CC061573; Fri, 21 Aug 2020 07:15:16 -0700 (PDT) Received: by mail-il1-x143.google.com with SMTP id f75so1213794ilh.3; Fri, 21 Aug 2020 07:15:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=xxeM5gPH8yu6tWCUWA1p+lgxDN3K/oRI20zeA1Ye/AM=; b=KnVRt/vE7Gs2dug2IuD45cFSjkQWwTvYY56745j/humEhcsfWutRa14n9MbdjyRi16 mZZLqIG70csF2R8txo+gsW2/2WaMHQOI0THY6qEIBRaf0giypShRsTgqWXbaXfxtrqnS fEEVCqfrZ/eFL5WcG4dHc5CPLIRV5ri6J0xN3vtRXlcr5gCbrMLP27ZHCZG3eLNK4/Eo tWRwVWrVNBPQE7MOkz3fcC6slrgW3Yu47hfDJqiXAO73rhN6BDcYj9Ljg0oomPfxAfSu oKAL1ivvSUW4RikdrSu/1IIt4brxlfJdk7xqim9Pp1+C5z0EuS8Y2JIBQ69idqiCNpy/ GQGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=xxeM5gPH8yu6tWCUWA1p+lgxDN3K/oRI20zeA1Ye/AM=; b=MC+TOoPhFXCXux77w0IKfNKNainSXUYIhOyGOZsdqTAj6vVHTYN0WfiAQlL2cPXNRz ChMITcqg6MTM8F+F7UgYNOJCWKitTt3Fvxpjm204IuGbcrnun9i569OHkJRRheUUpc7h C5DBE19Su5X9IVz56Ywm6Gko2Yhlselwwg6p478vwF0TCs/61/MheWKiDZU3sTkcnb+A 49wKfkd1kEIHhORK/WKmCSRcFEbBpQIHjAQs9gjmnckMS5Meh16ecVgry6SJIy3AjWez 26tyPQ+raYn5ph1tGzdpEsLGvWLZIr8ZGNYJNbmMd5fy7ouOWTGeOdVFktv5637NTDkf Q5dg== X-Gm-Message-State: AOAM532ADP2bz0GGI7NVjeplD7cuGKcWctbgEccKAzSq7Vbcs72V5OKQ J6AGmNy2bWpavG1LedpTWW4Js8878R1p6WCAVA== X-Received: by 2002:a92:4f:: with SMTP id 76mr2608119ila.11.1598019313716; Fri, 21 Aug 2020 07:15:13 -0700 (PDT) MIME-Version: 1.0 References: <20200821105229.18938-1-pbonzini@redhat.com> In-Reply-To: <20200821105229.18938-1-pbonzini@redhat.com> From: Brian Gerst Date: Fri, 21 Aug 2020 10:15:02 -0400 Message-ID: Subject: Re: [PATCH v2] x86/entry/64: Do not use RDPID in paranoid entry to accomodate KVM To: Paolo Bonzini Cc: Linux Kernel Mailing List , kvm list , "the arch/x86 maintainers" , Sean Christopherson , Dave Hansen , Chang Seok Bae , Peter Zijlstra , Sasha Levin , Tom Lendacky , Andy Lutomirski Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Aug 21, 2020 at 6:56 AM Paolo Bonzini wrote: > > From: Sean Christopherson > > Don't use RDPID in the paranoid entry flow, as it can consume a KVM > guest's MSR_TSC_AUX value if an NMI arrives during KVM's run loop. > > In general, the kernel does not need TSC_AUX because it can just use > __this_cpu_read(cpu_number) to read the current processor id. It can > also just block preemption and thread migration at its will, therefore > it has no need for the atomic rdtsc+vgetcpu provided by RDTSCP. For this > reason, as a performance optimization, KVM loads the guest's TSC_AUX when > a CPU first enters its run loop. On AMD's SVM, it doesn't restore the > host's value until the CPU exits the run loop; VMX is even more aggressive > and defers restoring the host's value until the CPU returns to userspace. > > This optimization obviously relies on the kernel not consuming TSC_AUX, > which falls apart if an NMI arrives during the run loop and uses RDPID. > Removing it would be painful, as both SVM and VMX would need to context > switch the MSR on every VM-Enter (for a cost of 2x WRMSR), whereas using > LSL instead RDPID is a minor blip. > > Both SAVE_AND_SET_GSBASE and GET_PERCPU_BASE are only used in paranoid entry, > therefore the patch can just remove the RDPID alternative. > > Fixes: eaad981291ee3 ("x86/entry/64: Introduce the FIND_PERCPU_BASE macro") > Cc: Dave Hansen > Cc: Chang Seok Bae > Cc: Peter Zijlstra > Cc: Sasha Levin > Cc: Paolo Bonzini > Cc: kvm@vger.kernel.org > Reported-by: Tom Lendacky > Debugged-by: Tom Lendacky > Suggested-by: Andy Lutomirski > Suggested-by: Peter Zijlstra > Signed-off-by: Sean Christopherson > Signed-off-by: Paolo Bonzini > --- > arch/x86/entry/calling.h | 10 ++++++---- > 1 file changed, 6 insertions(+), 4 deletions(-) > > diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h > index 98e4d8886f11..ae9b0d4615b3 100644 > --- a/arch/x86/entry/calling.h > +++ b/arch/x86/entry/calling.h > @@ -374,12 +374,14 @@ For 32-bit we have the following conventions - kernel is built with > * Fetch the per-CPU GSBASE value for this processor and put it in @reg. > * We normally use %gs for accessing per-CPU data, but we are setting up > * %gs here and obviously can not use %gs itself to access per-CPU data. > + * > + * Do not use RDPID, because KVM loads guest's TSC_AUX on vm-entry and > + * may not restore the host's value until the CPU returns to userspace. > + * Thus the kernel would consume a guest's TSC_AUX if an NMI arrives > + * while running KVM's run loop. > */ > .macro GET_PERCPU_BASE reg:req > - ALTERNATIVE \ > - "LOAD_CPU_AND_NODE_SEG_LIMIT \reg", \ > - "RDPID \reg", \ > - X86_FEATURE_RDPID > + LOAD_CPU_AND_NODE_SEG_LIMIT \reg > andq $VDSO_CPUNODE_MASK, \reg > movq __per_cpu_offset(, \reg, 8), \reg > .endm LOAD_CPU_AND_NODE_SEG_LIMIT can be merged into this, as its only purpose was to work around using CPP macros in an alternative. -- Brian Gerst