Message ID | 20221114233441.3895891-1-seanjc@google.com |
---|---|
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2421681wru; Mon, 14 Nov 2022 15:41:20 -0800 (PST) X-Google-Smtp-Source: AA0mqf7wfSF9IDlm/fXnlVyyF2wtJ3tRPTbdLAOI6+U8qfgHz99/oO8hehdUobUbQQ/h55HpT0fG X-Received: by 2002:a17:906:c284:b0:7a1:6786:444f with SMTP id r4-20020a170906c28400b007a16786444fmr11876257ejz.409.1668469280212; Mon, 14 Nov 2022 15:41:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668469280; cv=none; d=google.com; s=arc-20160816; b=0gj31J8xgSecvdjlz1k31MWN4AYRuQ2G4GE3+XeXUL0HFX2BIKqSgi94x1YXmEbzYv GibAih6IhJJlDnmHPrFTUkFe29pjuUslsP+DvXCA2yo8fAnYIQqTIR4MnxW8ZGYVItwL z7J/YDg36ezJt6dQJWHtLZZFq0H6B9mTwT99qRYv/cZpoitiQWb9YdH2Bg+/ylzvgJrH m83E8ePUJj/+wTjdOl2Q9Fyp0PMk0Tc/N2e7DE5KXv9oo2lCD/05M8wT9TiiBapkyyHW yEpGtzYdeIMm/wxobCS+1GGgfLihfKKL3zRXnf9wmpiuy9PSVUjRCifUrQkBlXKCvGTC h51A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :reply-to:dkim-signature; bh=2C+mEdWGRF7/5Wann0NujnMardQrr8Pv4jnCYIOIfiI=; b=Vy3WW96GYTJ5b+9ofJwpbv1JV/bGvwB3JA7+yl3rWRv5zBj0SdIqmwOs7DwFm2kyNZ zWeAtTJ1yuai47tr1tvorykkIP5lEunpxfeQnYGj9aNkdS+J2Ys/+cnW/xUUrBIuLgya miPNDcXfphtpJe/O6qmuLhKS6+PMyyTSjlZ13DEq46TjQQjaLzO1f2Z4DXGs4rro+6Tf CAw7zLpf2lwpjybw7HULHi0dyDM0qOc5RPIpQxBj23JFZ2ZfqGEYBGk9hr9tNJGxHUx4 jnpBbnbkOUpxs+TRw8shY7WViBQje9orkD36HVuJA8ddqMbASmudwMS/oKLR2nY9Od9n HdKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KdNFTJ+k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sh44-20020a1709076eac00b007ae26c753edsi10802716ejc.52.2022.11.14.15.40.56; Mon, 14 Nov 2022 15:41:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KdNFTJ+k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236124AbiKNXeq (ORCPT <rfc822;zwp10758@gmail.com> + 99 others); Mon, 14 Nov 2022 18:34:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230520AbiKNXep (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 14 Nov 2022 18:34:45 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1520AE6B for <linux-kernel@vger.kernel.org>; Mon, 14 Nov 2022 15:34:44 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id m6-20020aa79006000000b0056bc283f477so6777740pfo.19 for <linux-kernel@vger.kernel.org>; Mon, 14 Nov 2022 15:34:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:reply-to:from:to:cc :subject:date:message-id:reply-to; bh=2C+mEdWGRF7/5Wann0NujnMardQrr8Pv4jnCYIOIfiI=; b=KdNFTJ+kuX5LRPdITAEfyeE5gXPE4KELlxqjIkj/2C8ZNoZ5iZfHOVNDOOfbCszRT2 EaHbD6iQ0CU8zAwaA6+iaPNkTOFNfl+7SI4hQ9w3qmWHphbSo8l+UcnrhrfcFxRUufWZ RC8hBKQGlGyoM+QDaYRdUAnHywNf8Z+239VTm1JOIFTx/M3ta+dz7FG9CH6TMiVV8fbQ 0HxyUg2OeZDkIqp4q1UwzLYaKEiLBWCY8GQ7HDVo4w93hiRbYNXdW9SI7bCb/MUzSnOS M0md+d9QDWIxVbQgKli/kpzDulxMjA83byuaklikp6IKAYHFDbU3PJ+0feaaGPCd4Zbt E3bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:reply-to :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2C+mEdWGRF7/5Wann0NujnMardQrr8Pv4jnCYIOIfiI=; b=fCYgZV1O5XUCfyGHOY2cXkO/RNsAsW+/xj6GurMLiapG0iEvv/inbOzNJc9nQFHLrX wkVFO/+SRoAs9vPsXoQCpnE4ugKqelsLpttkxVbamnvPpL6ePmWVvMcPQuIqXBO/ExgI U+s+ZkZh6Xiqd3dvcjDTgRZy2bgOtTPNVdceEIQNEfDbQbkgJDfm8oU06Kwptdeftmbw C0n27dFwExbQMTy1p064v/IJJHzEHedYcB8PrtUOHIfNIx8YeMsVopqlR/1I0wVF5I7T vIw+O6anLJSTAG1nkjSz4VGJvxFROsObDC++pnBQl3/MecJQjgUbuBLyaAcdx0bYgfNP wtqA== X-Gm-Message-State: ANoB5pnmWtx4vYklFH2JEYam5zPAyptIIpKSTPDPbOABfKAgfbxFkfXv PC9LruF5MhjS7GkXBsiQ8Ne4/p4JqvA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1c86:b0:56b:d027:214 with SMTP id y6-20020a056a001c8600b0056bd0270214mr15823140pfw.79.1668468884170; Mon, 14 Nov 2022 15:34:44 -0800 (PST) Reply-To: Sean Christopherson <seanjc@google.com> Date: Mon, 14 Nov 2022 23:34:38 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog Message-ID: <20221114233441.3895891-1-seanjc@google.com> Subject: [PATCH v3 0/3] x86/crash: Fix double NMI shootdown bug From: Sean Christopherson <seanjc@google.com> To: Thomas Gleixner <tglx@linutronix.de>, Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>, Dave Hansen <dave.hansen@linux.intel.com>, x86@kernel.org Cc: "H. Peter Anvin" <hpa@zytor.com>, linux-kernel@vger.kernel.org, "Guilherme G . Piccoli" <gpiccoli@igalia.com>, Vitaly Kuznetsov <vkuznets@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, Tom Lendacky <thomas.lendacky@amd.com>, Sean Christopherson <seanjc@google.com> Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749516843578919360?= X-GMAIL-MSGID: =?utf-8?q?1749516843578919360?= |
Series | x86/crash: Fix double NMI shootdown bug | |
Message
Sean Christopherson
Nov. 14, 2022, 11:34 p.m. UTC
Tom, I Cc'd you this time around because the APM doesn't explicitly state that GIF is set when EFER.SVME is disabled. KVM's nSVM emulation does set GIF in this case, but it's not clear whether or not KVM is making up behavior. If clearing EFER.SVME doesn't set GIF, then patch 1 needs to be modified to try STGI before clearing EFER.SVME, e.g. if a crash is initiated from KVM between CLGI and STGI. Responding CPUs are "safe" because GIF=0 also blocks NMIs, but the initiating CPU might leave GIF=0 when jumping into the new kernel. Fix a double NMI shootdown bug found and debugged by Guilherme, who did all the hard work. NMI shootdown is a one-time thing; the handler leaves NMIs blocked and enters halt. At best, a second (or third...) shootdown is an expensive nop, at worst it can hang the kernel and prevent kexec'ing into a new kernel, e.g. prior to the hardening of register_nmi_handler(), a double shootdown resulted in a double list_add(), which is fatal when running with CONFIG_BUG_ON_DATA_CORRUPTION=y. With the "right" kexec/kdump configuration, emergency_vmx_disable_all() can be reached after kdump_nmi_shootdown_cpus() (currently the only two users of nmi_shootdown_cpus()). To fix, move the disabling of virtualization into crash_nmi_callback(), remove emergency_vmx_disable_all()'s callback, and do a shootdown for emergency_vmx_disable_all() if and only if a shootdown hasn't yet occurred. The only thing emergency_vmx_disable_all() cares about is disabling VMX/SVM (obviously), and since I can't envision a use case for an NMI shootdown that doesn't want to disable virtualization, doing that in the core handler means emergency_vmx_disable_all() only needs to ensure _a_ shootdown occurs, it doesn't care when that shootdown happened or what callback may have run. Patch 2 is a related bug fix found while exploring ideas for patch 1. Patch 3 is a cleanup to try to prevent future "fixed VMX but not SVM" style bugs. v3: - Re-collect Guilherme's Tested-by. - Tweak comment in patch 1 to reference STGI instead of CLGI. - Celebrate this series' half-birthday. v2: - Use a NULL handler and crash_ipi_issued instead of a magic nop handler. [tglx] - Add comments to call out that modifying the existing handler once the NMI is sent may cause explosions. - Add a patch to cleanup cpu_emergency_vmxoff(). - https://lore.kernel.org/all/20220518001647.1291448-1-seanjc@google.com v1: https://lore.kernel.org/all/20220511234332.3654455-1-seanjc@google.com Sean Christopherson (3): x86/crash: Disable virt in core NMI crash handler to avoid double shootdown x86/reboot: Disable virtualization in an emergency if SVM is supported x86/virt: Fold __cpu_emergency_vmxoff() into its sole caller arch/x86/include/asm/reboot.h | 1 + arch/x86/include/asm/virtext.h | 14 +----- arch/x86/kernel/crash.c | 16 +----- arch/x86/kernel/reboot.c | 89 +++++++++++++++++++++++++--------- 4 files changed, 69 insertions(+), 51 deletions(-) base-commit: dacca1e5e75d7c1297f1334cdc10491dcdd1b2b8
Comments
On 14/11/2022 20:34, Sean Christopherson wrote: > [...] > v3: > - Re-collect Guilherme's Tested-by. > - Tweak comment in patch 1 to reference STGI instead of CLGI. > - Celebrate this series' half-birthday. Heheh Thanks a lot for persisting with this Sean, much appreciated! I'm surprised on how long is taking to get these _fixes_ merged in the kernel, hence your effort is very valuable =) Cheers, Guilherme
On Tue, Nov 15, 2022, Andrew Cooper wrote: > On 14/11/2022 23:34, Sean Christopherson wrote: > > Tom, > > > > I Cc'd you this time around because the APM doesn't explicitly state that > > GIF is set when EFER.SVME is disabled. KVM's nSVM emulation does set GIF > > in this case, but it's not clear whether or not KVM is making up behavior. > > If clearing EFER.SVME doesn't set GIF, then patch 1 needs to be modified > > to try STGI before clearing EFER.SVME, e.g. if a crash is initiated from > > KVM between CLGI and STGI. Responding CPUs are "safe" because GIF=0 also > > blocks NMIs, but the initiating CPU might leave GIF=0 when jumping into > > the new kernel. > > GIF exists independently of EFER.SVME. > > It is also gets set by the SKINIT instruction, which is why there is an > asymmetry on the #UD conditions of STGI and CLGI. > > STGI is also intended to be used by the DLME once critical > initialisation is done, hence why it's dependent on EFER.SVME || SKINIT. Gah, stupid APM. The pseudocode states "EFER.SVME || CPUID.SKINIT", but the description and the comment both say that SVM must be enabled to execute SKINIT, which made me hope that disabling SVM would reset everything. This instruction generates a #UD exception if SVM is not enabled. See “Enabling SVM” in AMD64 Architecture Programmer’s Manual Volume 2: System Instructions, order# 24593. ... IF ((EFER.SVME == 0) && !(CPUID 8000_0001.ECX[SKINIT]) || (!PROTECTED_MODE)) EXCEPTION [#UD] // This instruction can only be executed // in protected mode with SVM enabled ^^^^^^^^^^^
On 11/15/22 10:56, Sean Christopherson wrote: > On Tue, Nov 15, 2022, Andrew Cooper wrote: >> On 14/11/2022 23:34, Sean Christopherson wrote: >>> Tom, >>> >>> I Cc'd you this time around because the APM doesn't explicitly state that >>> GIF is set when EFER.SVME is disabled. KVM's nSVM emulation does set GIF >>> in this case, but it's not clear whether or not KVM is making up behavior. >>> If clearing EFER.SVME doesn't set GIF, then patch 1 needs to be modified >>> to try STGI before clearing EFER.SVME, e.g. if a crash is initiated from >>> KVM between CLGI and STGI. Responding CPUs are "safe" because GIF=0 also >>> blocks NMIs, but the initiating CPU might leave GIF=0 when jumping into >>> the new kernel. >> >> GIF exists independently of EFER.SVME. >> >> It is also gets set by the SKINIT instruction, which is why there is an >> asymmetry on the #UD conditions of STGI and CLGI. >> >> STGI is also intended to be used by the DLME once critical >> initialisation is done, hence why it's dependent on EFER.SVME || SKINIT. > > Gah, stupid APM. The pseudocode states "EFER.SVME || CPUID.SKINIT", but the > description and the comment both say that SVM must be enabled to execute SKINIT, > which made me hope that disabling SVM would reset everything. > > This instruction generates a #UD exception if SVM is not enabled. See “Enabling > SVM” in AMD64 Architecture Programmer’s Manual Volume 2: System Instructions, > order# 24593. > > ... > > IF ((EFER.SVME == 0) && !(CPUID 8000_0001.ECX[SKINIT]) || (!PROTECTED_MODE)) > EXCEPTION [#UD] // This instruction can only be executed > // in protected mode with SVM enabled > ^^^^^^^^^^^ I'll see if I can't get someone to update the psuedo-code comment here. But, to your original question, as far as I understand, GIF is not automatically set if SVME is cleared from EFER. Thanks, Tom
On Tue, Nov 15, 2022, Guilherme G. Piccoli wrote: > On 14/11/2022 20:34, Sean Christopherson wrote: > > [...] > > v3: > > - Re-collect Guilherme's Tested-by. > > - Tweak comment in patch 1 to reference STGI instead of CLGI. > > - Celebrate this series' half-birthday. > > Heheh > > Thanks a lot for persisting with this Sean, much appreciated! I'm > surprised on how long is taking to get these _fixes_ merged in the > kernel, hence your effort is very valuable =) Well, to be fair, the fixes aren't perfect. Aside from the GIF thing, patch 2 breaks CONFIG_SMP=n. I think there's another bug lurking too. The emergency reboot path doesn't VMCLEAR VMCSes. AFAIK, Intel doesn't guarantee the VMCS caches are purged on INIT, so if the reboot doesn't actually RESET CPUs, the new kernel could observe memory corruption due to an old VMCS getting written back. Argh, and I missed sysvec_reboot() + smp_stop_nmi_callback() for SVM support. And slightly longer term, this entire mess can be cleaned up. Once KVM's handling of VMX/SVM initialization sucks less[*], all of the disabling logic can be moved into KVM callbacks and the kernel can stop speculatively trying to disable VMX/SVM. I'll send a v4 to fix all of the suspected bugs, and then work on another series to clean up the callbacks, which will have dependencies on both the kvm_init() rework and this series. [*] https://lore.kernel.org/all/20221102231911.3107438-1-seanjc@google.com