From patchwork Fri May 12 23:31:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 93419 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp5456908vqo; Fri, 12 May 2023 16:36:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4XtqylG1i+NOMHFF4vM5VgPZwvxWo+yjqvEQEksPP+1NZ2UBiiTsodQgeq4UdW8ibpyPoY X-Received: by 2002:a17:903:41d2:b0:1ab:28b4:6d5 with SMTP id u18-20020a17090341d200b001ab28b406d5mr41453311ple.5.1683934571368; Fri, 12 May 2023 16:36:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683934571; cv=none; d=google.com; s=arc-20160816; b=0djEz6RUMOA95AdN+zXvZul31aqpdmdvoJeve/ayevtiAJFaRxJFfgV+8y0r+B61T/ 3FUvPFVx4ov2zjUDRODyP3r/poVgD7h4DHJDo0szqtfLEYQtludYH8TbvQtHNUqp1Q9f K+sJVdKQvuYzc6wjEOK8YtuaIZDPVJ5eZS4dtGuzJd1FcDnDstQVAsrkz8TfW/egfWNt dEnshVXe2pb1LZYpLq5TimA51UaL8AxTC7z66O7Csx0e6G5vYyOjG3JEQ4AHAE/nOz0v XPBfkVToiqaOj7J6XB5vdM9yP8kCGRguKYUH54+/3gRfEAXL9supdodH9IgZNGfB0h+/ yapw== 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:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=NwZBPWdtO/ryX4s5SybYGIvceJOpqL00ruN6lX2awnM=; b=bjkoDEwcliwmxb5Hh4qORcYGthnKY/SWtgYyPpJkbDt5fT33qO7ILfhj6Iu3oudZJ+ QiH6deeOQ1OQOK+mNgnvuG00qj4tyFd+ixMeMr0T3RzN8MlHGgCo1IMHOYQxRNVsdJHV o2M/aCkf5PEW2XeJjHiAmFPWwsGg+xWj0QlLFRKi/YdDFFRrEytpa9TIbdtJaAozHTJK PnGq4kj0aE1NvTC00SWcnn+xNpLrZCsDY9LOcE0TQ3xltUT/gDF13hfRNwk1pZoGjDZz RSXu3BHPIThz92WgfyZZiczm/7uFR6jkzujlN00fHDjVuPvjjiY/eTkB4JBZZBddQwsh SbcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=I5cbSAqj; 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 c2-20020a170902aa4200b001a6f91439d8si9834696plr.393.2023.05.12.16.35.56; Fri, 12 May 2023 16:36:11 -0700 (PDT) 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=20221208 header.b=I5cbSAqj; 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 S240792AbjELXbl (ORCPT + 99 others); Fri, 12 May 2023 19:31:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240777AbjELXbd (ORCPT ); Fri, 12 May 2023 19:31:33 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FCD030CB for ; Fri, 12 May 2023 16:31:32 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-25017576284so5354436a91.3 for ; Fri, 12 May 2023 16:31:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683934291; x=1686526291; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=NwZBPWdtO/ryX4s5SybYGIvceJOpqL00ruN6lX2awnM=; b=I5cbSAqjIEnw5/qA+SDzjgh/7wgSqDk/YzpaFGJ7XoAzj+2/l7pFdtD5ThRZCIUl/W j89Z/QLK01RU1+FF7Q1NSVNIXc9+WeqCYfcXKkLBsNlH7pN+KD952mpxIOfZNnNaPPAx kJEIeLUQuqO6MlSJGSX/4+kTSk5nsmrcqpyV0TIyrG1f4BE5jjQbGlKPuKOWPgS3c+Zg CkGfru99WPin3KXQTN2x4uQPR85Bw8T6bo9L8TC2sJXkrUmbzS1Tr9K5UkpAw8NlpIfE nUcO4fytBd/+p7f42PwcenWjH6Vj0RsnJlWnQA86zrYZPv69LDVCZYbHCIddmIm0YfdS 6reg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683934291; x=1686526291; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NwZBPWdtO/ryX4s5SybYGIvceJOpqL00ruN6lX2awnM=; b=aWG0m3GkCqMTEkMAvPKAkClnZCBue4VhW/XTzxUeCXDwaE6/U4tpMSR/ns9jKdrKNw tyWBl6w7e99486kvWqPHb5VsErzr+LLTO84w9BwE5G6OG39E8nwr7XGsiMvuC2L9S4mJ F+clge+uHDCrQYwJnEpj8b7ONuzjw9OM1oy5D7T1Cvbl8roBt7+FR8AQwGWczPO05r7z 1u5Sk0NAuVjkL6NDwIzZptIo8k9U173tfoBO+lwEEYMrR1Lo768aALAr0b6HUvLkaSau 0Zmm3EXwZLiw2VhIj/Jy79tm8vzjZlMBXQUNwc3yELnHhl12dBe2k0XtWRyBytFGkkEn ZGLA== X-Gm-Message-State: AC+VfDwdR+QR65HWG88UpdZy5V7bJSg/CUwBPdkqh1liUZlGlMsHChFs nwD3wR2q2cE0/t18JVu5koALO9Kzhok= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:895:b0:250:a6c1:b843 with SMTP id bj21-20020a17090b089500b00250a6c1b843mr4708527pjb.9.1683934291769; Fri, 12 May 2023 16:31:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 12 May 2023 16:31:26 -0700 In-Reply-To: <20230512233127.804012-1-seanjc@google.com> Mime-Version: 1.0 References: <20230512233127.804012-1-seanjc@google.com> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog Message-ID: <20230512233127.804012-2-seanjc@google.com> Subject: [PATCH v2 1/2] KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , kvmarm@lists.linux.dev, Huacai Chen , Aleksandar Markovic , Anup Patel , Atish Patra , kvm-riscv@lists.infradead.org, Sean Christopherson 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,T_SCC_BODY_TEXT_LINE,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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765733377196592279?= X-GMAIL-MSGID: =?utf-8?q?1765733377196592279?= Use syscore_ops.shutdown to disable hardware virtualization during a reboot instead of using the dedicated reboot_notifier so that KVM disables virtualization _after_ system_state has been updated. This will allow fixing a race in KVM's handling of a forced reboot where KVM can end up enabling hardware virtualization between kernel_restart_prepare() and machine_restart(). Rename KVM's hook to match the syscore op to avoid any possible confusion from wiring up a "reboot" helper to a "shutdown" hook (neither "shutdown nor "reboot" is completely accurate as the hook handles both). Opportunistically rewrite kvm_shutdown()'s comment to make it less VMX specific, and to explain why kvm_rebooting exists. Cc: Marc Zyngier Cc: Oliver Upton Cc: James Morse Cc: Suzuki K Poulose Cc: Zenghui Yu Cc: kvmarm@lists.linux.dev Cc: Huacai Chen Cc: Aleksandar Markovic Cc: Anup Patel Cc: Atish Patra Cc: kvm-riscv@lists.infradead.org Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d1abb331ea68..e771b6a013c9 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5211,26 +5211,24 @@ static int hardware_enable_all(void) return r; } -static int kvm_reboot(struct notifier_block *notifier, unsigned long val, - void *v) +static void kvm_shutdown(void) { /* - * Some (well, at least mine) BIOSes hang on reboot if - * in vmx root mode. - * - * And Intel TXT required VMX off for all cpu when system shutdown. + * Disable hardware virtualization and set kvm_rebooting to indicate + * that KVM has asynchronously disabled hardware virtualization, i.e. + * that relevant errors and exceptions aren't entirely unexpected. + * Some flavors of hardware virtualization need to be disabled before + * transferring control to firmware (to perform shutdown/reboot), e.g. + * on x86, virtualization can block INIT interrupts, which are used by + * firmware to pull APs back under firmware control. Note, this path + * is used for both shutdown and reboot scenarios, i.e. neither name is + * 100% comprehensive. */ pr_info("kvm: exiting hardware virtualization\n"); kvm_rebooting = true; on_each_cpu(hardware_disable_nolock, NULL, 1); - return NOTIFY_OK; } -static struct notifier_block kvm_reboot_notifier = { - .notifier_call = kvm_reboot, - .priority = 0, -}; - static int kvm_suspend(void) { /* @@ -5261,6 +5259,7 @@ static void kvm_resume(void) static struct syscore_ops kvm_syscore_ops = { .suspend = kvm_suspend, .resume = kvm_resume, + .shutdown = kvm_shutdown, }; #else /* CONFIG_KVM_GENERIC_HARDWARE_ENABLING */ static int hardware_enable_all(void) @@ -5965,7 +5964,6 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) if (r) return r; - register_reboot_notifier(&kvm_reboot_notifier); register_syscore_ops(&kvm_syscore_ops); #endif @@ -6037,7 +6035,6 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) err_vcpu_cache: #ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING unregister_syscore_ops(&kvm_syscore_ops); - unregister_reboot_notifier(&kvm_reboot_notifier); cpuhp_remove_state_nocalls(CPUHP_AP_KVM_ONLINE); #endif return r; @@ -6063,7 +6060,6 @@ void kvm_exit(void) kvm_async_pf_deinit(); #ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING unregister_syscore_ops(&kvm_syscore_ops); - unregister_reboot_notifier(&kvm_reboot_notifier); cpuhp_remove_state_nocalls(CPUHP_AP_KVM_ONLINE); #endif kvm_irqfd_exit();