Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3124977rwb; Wed, 30 Nov 2022 15:58:35 -0800 (PST) X-Google-Smtp-Source: AA0mqf4ujhKdVvnW+3OhVi1s5tgOU0cT1z5q0ZbafaTim7ztc0W/xEXhfd/yQNQlQwvUF7PIG+ZE X-Received: by 2002:a17:906:18e9:b0:78d:48ac:9044 with SMTP id e9-20020a17090618e900b0078d48ac9044mr42027751ejf.613.1669852715735; Wed, 30 Nov 2022 15:58:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669852715; cv=none; d=google.com; s=arc-20160816; b=ac2CjtpBTjCcHZK0hSySRzgu8p1rPNQbXpTYlFSAu4cUbQx061i9nypoEn68Bm4aLk me28CVlszZzdO2PwDQSiEoUwAqq6MVUQqSESM7jth39LQi2Izbmm+ezQzKvJmpUbHdmi D/2wpQ/LvmvNIwTfOhy6ZNUS4qYgqk23QjWupywzFuFPGhUT6UWlembk3B8us8MwItaa sdFSSswMkyMX/3SfPOV0zfoqNGHSMVFUzHklXqGPmh05lkSo3+hDxFbjNBPH/RRkiebN 3IPrHqrfNDGD9h8gfODrceH9Al+bb4foOMVNX1Tb/2jcfawYD9rJBzhOrzFp15NqNgCJ cS0Q== 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=/jaxWTFPxmj21+X8Hbtmuwlu7GhNr4CXW1v8XgKCn2o=; b=rvJabKv8R/moJ/wTzMg53lIqSH1g8QBGtY2ZXfaiLB7cts8mDj0fVaYiLwajKTagIv aNPB0rDZRj7RbQ9ZsSTFGtJYAqim5eQRVysyWWE96dNW6sNgfVyL7BZNk+lCDbSgEQda ayHao7ewKqF/iWRFycvSC8RCKIxND7kqMfGb/qFs/M29501MdLKuXo5eSlymvQI82ZtL pxVCRBbZXT3EbUt/7O+TVPQBvjqNtqe9Qp/3h6GJSL0F/LkapdtVSzeOsmeEHxiEahqX zJRidU/KAfcN3V9nAIyxN7ewIoFW55Uwms8DeK7A0F7rZl3EubZ6owPM4SaLlw713eRj KdNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=jQgxp+u6; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cw28-20020a056402229c00b0046b5aff3c95si2188564edb.311.2022.11.30.15.58.16; Wed, 30 Nov 2022 15:58:35 -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=jQgxp+u6; 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 S230216AbiK3XQW (ORCPT + 83 others); Wed, 30 Nov 2022 18:16:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230124AbiK3XPm (ORCPT ); Wed, 30 Nov 2022 18:15:42 -0500 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18898A1A3A for ; Wed, 30 Nov 2022 15:11:24 -0800 (PST) Received: by mail-pg1-x549.google.com with SMTP id q63-20020a632a42000000b0045724b1dfb9so103757pgq.3 for ; Wed, 30 Nov 2022 15:11:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=/jaxWTFPxmj21+X8Hbtmuwlu7GhNr4CXW1v8XgKCn2o=; b=jQgxp+u6SHc5NMy81OYePF88zDxKuqUxtOVxluvPTQI3MEuiQG2uqtD9RQG6VmOJ8F kk/Pbv5hy6CTAGXGAAE4oRsWW1h1FNSdzY+gt8AUeNr14OcCE+UIvjb6UQ7TpiTMFIF0 izlNrD0kV5SMk0eBnY4nR4B5h0mn3eoylpGXZ25XCA4WeenN75ctGGp49AtZXHGvUS+T qZhLro5p9fp3cB2diacdo0hy8LHABjsSmVzRqwSHAFnoRTmJG580sa0oMGIlwLbpLirz qqtAn0DlcrzmZfTLixcwk1Q4OO5vUHO9P7L17dYxokTmYYn2YF9q+ofLTjNmrrQIy8bI XJdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=/jaxWTFPxmj21+X8Hbtmuwlu7GhNr4CXW1v8XgKCn2o=; b=bY6t3/iRz5U3vqLbkJX8xi6SHhoX4UvtviJ4jsTWfk6ORhZNkUQnwaK+rHBtX51JJ5 DZ9OlrjbUCGgydL2e6I78akKiItXslVOnbZD3eNwPefJiiBs6jIK/xwjzVCYDTtRnIBq zLt+hxMrkq8IWZ29QmdUQGGq8UfUoLlDOdYN/5HEjBaKLmzCGZafjMTzipu77BRIwFqu QIh1VSBIwWWvhY8LCpKmg4HbllGQGrF6k41FeLw+4KKtyzIKayIJMngoDlXV7oiocvNN /vVm81QvNZ0rVcZok6QClVSkHj22ZZFdubK3MQzJshhxMen0OsTbIBFJDGEWDFDlWTLh zEiQ== X-Gm-Message-State: ANoB5plokdyfG3keeU2i2iAxEmjfjqp2pumGMUlTZhO6axXCMnBdaqHV bLYAeBqgMGPuDunwGtej/cT0P/3pVfA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:6904:b0:185:3f7a:50cc with SMTP id j4-20020a170902690400b001853f7a50ccmr43659852plk.99.1669849850498; Wed, 30 Nov 2022 15:10:50 -0800 (PST) Reply-To: Sean Christopherson Date: Wed, 30 Nov 2022 23:09:26 +0000 In-Reply-To: <20221130230934.1014142-1-seanjc@google.com> Mime-Version: 1.0 References: <20221130230934.1014142-1-seanjc@google.com> X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog Message-ID: <20221130230934.1014142-43-seanjc@google.com> Subject: [PATCH v2 42/50] KVM: Disable CPU hotplug during hardware enabling/disabling From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Matthew Rosato , Eric Farman , Sean Christopherson , Vitaly Kuznetsov , David Woodhouse , Paul Durrant Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Oliver Upton , Atish Patra , David Hildenbrand , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, Yuan Yao , Cornelia Huck , Isaku Yamahata , "=?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?=" , Fabiano Rosas , Michael Ellerman , Kai Huang , Chao Gao , Thomas Gleixner 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=unavailable 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 From: Chao Gao Disable CPU hotplug when enabling/disabling hardware to prevent the corner case where if the following sequence occurs: 1. A hotplugged CPU marks itself online in cpu_online_mask 2. The hotplugged CPU enables interrupt before invoking KVM's ONLINE callback 3 hardware_{en,dis}able_all() is invoked on another CPU the hotplugged CPU will be included in on_each_cpu() and thus get sent through hardware_{en,dis}able_nolock() before kvm_online_cpu() is called. start_secondary { ... set_cpu_online(smp_processor_id(), true); <- 1 ... local_irq_enable(); <- 2 ... cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); <- 3 } KVM currently fudges around this race by keeping track of which CPUs have done hardware enabling (see commit 1b6c016818a5 "KVM: Keep track of which cpus have virtualization enabled"), but that's an inefficient, convoluted, and hacky solution. Signed-off-by: Chao Gao [sean: split to separate patch, write changelog] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 11 ++++++++++- virt/kvm/kvm_main.c | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index dad30097f0c3..d2ad383da998 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9281,7 +9281,16 @@ static inline void kvm_ops_update(struct kvm_x86_init_ops *ops) static int kvm_x86_check_processor_compatibility(void) { - struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); + int cpu = smp_processor_id(); + struct cpuinfo_x86 *c = &cpu_data(cpu); + + /* + * Compatibility checks are done when loading KVM and when enabling + * hardware, e.g. during CPU hotplug, to ensure all online CPUs are + * compatible, i.e. KVM should never perform a compatibility check on + * an offline CPU. + */ + WARN_ON(!cpu_online(cpu)); if (__cr4_reserved_bits(cpu_has, c) != __cr4_reserved_bits(cpu_has, &boot_cpu_data)) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index f26ea779710a..d985b24c423b 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5098,15 +5098,26 @@ static void hardware_disable_all_nolock(void) static void hardware_disable_all(void) { + cpus_read_lock(); raw_spin_lock(&kvm_count_lock); hardware_disable_all_nolock(); raw_spin_unlock(&kvm_count_lock); + cpus_read_unlock(); } static int hardware_enable_all(void) { int r = 0; + /* + * When onlining a CPU, cpu_online_mask is set before kvm_online_cpu() + * is called, and so on_each_cpu() between them includes the CPU that + * is being onlined. As a result, hardware_enable_nolock() may get + * invoked before kvm_online_cpu(), which also enables hardware if the + * usage count is non-zero. Disable CPU hotplug to avoid attempting to + * enable hardware multiple times. + */ + cpus_read_lock(); raw_spin_lock(&kvm_count_lock); kvm_usage_count++; @@ -5121,6 +5132,7 @@ static int hardware_enable_all(void) } raw_spin_unlock(&kvm_count_lock); + cpus_read_unlock(); return r; } -- 2.38.1.584.g0f3c55d4c2-goog