Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1844960pxb; Wed, 9 Feb 2022 05:50:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJy+t3snZjAmvktwh1KDJDicP8rPszrIf67+AvBAYreIXytMVJEd7syjy/MbYhx4UJqDdrI6 X-Received: by 2002:a05:6a00:1691:: with SMTP id k17mr2380962pfc.54.1644414605433; Wed, 09 Feb 2022 05:50:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644414605; cv=none; d=google.com; s=arc-20160816; b=fEkpgRJgxtXbXWVHg3QLg0C5tOKeXyiMOfBwip4MlAjVvwG5VpzjW9AkjyrG4Y3hu3 iCzxzzjshhTTQW5h0D8Hu57mYRhuwRtMc3pLNrubnmIBdwIogqzg6jZZUMPCPPX9Z1Oc hSZMRLHmiq3B9oqYd2iu0qEDwbij0NjFJvio0eoej41+tJl6sUFpGPrcw/0b2oZIYuQo cqL4oio+5mtEaysjy49QK23nBQbevboZIJAuw1EKOgWtZ5k1iqrU65+MTUEORXXjvQk/ HTbxInnLp0pnXvcdLuYiI5WfXhvwM6d2fkr0+zSzpQduYzTQSK+srALrKTioz1AjL2zN 4RcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=KkbshxdCVHNifz3CHY4N06lHJSQF5TTZJr75+MV14nI=; b=wK6lsEN/6o8TkmAn88lFoCjRjKuVVwhTbbODaEKnRiB4wpHFFdOOkr/jl0UYRaHsWJ l4jA/TLsQs1qaDQfZtPHSSciphSvx4raGNxqI0Ert/O07iMANwrxNXmApdIIZ2x6/Fpr Pu7KuQZ4JjLrKSWm4mjPym3BqKjjb65kU+35oaFrGQw72S+6+cui/BQS5tPvAS6R++kv vKJ8jB6Buu6kfZZBWuJewrMMkxv3kKKEepg8VnU7lV+qxqIDNTCXUjMZEdWOtkbgCakh 1KGc/4xCNAPOcMrclM0KC+v76KMsIES0WqLUScHqO+7LNeA4MQrBsbdox8RJqGFm1fXt WDow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=frLs+1gv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id v17si16869277pfu.27.2022.02.09.05.50.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 05:50:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=frLs+1gv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 603B0E159E9D; Wed, 9 Feb 2022 02:30:15 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231623AbiBIHnD (ORCPT + 99 others); Wed, 9 Feb 2022 02:43:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239356AbiBIHm2 (ORCPT ); Wed, 9 Feb 2022 02:42:28 -0500 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9ACCDC05CB90; Tue, 8 Feb 2022 23:42:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644392551; x=1675928551; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/DKEaaHy2+BNSJa66JF603f8foYnTYwUDIcXvB3wl7g=; b=frLs+1gvHw9HyVf2UAKu6JB6h3OtO2KmlwR+Ezu4W8COdGU8ZG9aID++ aGNrRvnsZl8bPWO72dGZVhg7xP0tfUDhW7+rviFmGj84HglI+ckDHtFet XdYqwqdsxchgGKIVHYBDr0mMTIMP5XEvoQpq4a286aTamRq9iwPqjAmtG c2rRP0M/LAwbonCSuc/f/Kx1JyZdJsJA5Y/9YZwGeVedlG8npcIVR4jOx JN5LlGMXIljGbeP066kCyZS0p1u4rMlIZKQ5cJCKIvKLF+JRtL2UZlXab ePGREZ64F+tSSDSiwsngEMIkkHKmmVdfCIiK2IPWqO4bFjbpyQZxnMCeo A==; X-IronPort-AV: E=McAfee;i="6200,9189,10252"; a="248907365" X-IronPort-AV: E=Sophos;i="5.88,355,1635231600"; d="scan'208";a="248907365" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2022 23:42:02 -0800 X-IronPort-AV: E=Sophos;i="5.88,355,1635231600"; d="scan'208";a="540984776" Received: from hyperv-sh4.sh.intel.com ([10.239.48.22]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2022 23:41:58 -0800 From: Chao Gao To: kvm@vger.kernel.org, seanjc@google.com, pbonzini@redhat.com, kevin.tian@intel.com, tglx@linutronix.de Cc: Chao Gao , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: [PATCH v3 5/5] KVM: Do compatibility checks on hotplugged CPUs Date: Wed, 9 Feb 2022 15:41:06 +0800 Message-Id: <20220209074109.453116-6-chao.gao@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220209074109.453116-1-chao.gao@intel.com> References: <20220209074109.453116-1-chao.gao@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 At init time, KVM does compatibility checks to ensure that all online CPUs support hardware virtualization and a common set of features. But KVM uses hotplugged CPUs without such compatibility checks. On Intel CPUs, this leads to #GP if the hotplugged CPU doesn't support VMX or vmentry failure if the hotplugged CPU doesn't meet minimal feature requirements. Do compatibility checks when onlining a CPU and abort the online process if the hotplugged CPU is incompatible with online CPUs. CPU hotplug is disabled during hardware_enable_all() to prevent the corner case as shown below. A hotplugged CPU marks itself online in cpu_online_mask (1) and enables interrupt (2) before invoking callbacks registered in ONLINE section (3). So, if hardware_enable_all() is invoked on another CPU right after (2), then on_each_cpu() in hardware_enable_all() invokes hardware_enable_nolock() on the hotplugged CPU before kvm_online_cpu() is called. This makes the CPU escape from compatibility checks, which is risky. start_secondary { ... set_cpu_online(smp_processor_id(), true); <- 1 ... local_irq_enable(); <- 2 ... cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); <- 3 } Keep compatibility checks at KVM init time. It can help to find incompatibility issues earlier and refuse to load arch KVM module (e.g., kvm-intel). Loosen the WARN_ON in kvm_arch_check_processor_compat so that it can be invoked from KVM's CPU hotplug callback (i.e., kvm_online_cpu). Opportunistically, add a pr_err() for setup_vmcs_config() path in vmx_check_processor_compatibility() so that each possible error path has its own error message. Convert printk(KERN_ERR ... to pr_err to please checkpatch.pl Signed-off-by: Chao Gao --- arch/x86/kvm/vmx/vmx.c | 10 ++++++---- arch/x86/kvm/x86.c | 11 +++++++++-- virt/kvm/kvm_main.c | 18 +++++++++++++++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index fc94d7139f69..efde9faca02a 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7120,20 +7120,22 @@ static int vmx_check_processor_compatibility(void) { struct vmcs_config vmcs_conf; struct vmx_capability vmx_cap; + int cpu = smp_processor_id(); if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) || !this_cpu_has(X86_FEATURE_VMX)) { - pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id()); + pr_err("kvm: VMX is disabled on CPU %d\n", cpu); return -EIO; } - if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0) + if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0) { + pr_err("kvm: failed to setup vmcs config on CPU %d\n", cpu); return -EIO; + } if (nested) nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept); if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { - printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", - smp_processor_id()); + pr_err("kvm: CPU %d feature inconsistency!\n", cpu); return -EIO; } return 0; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e9777ffc50c2..219df62115d2 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11550,9 +11550,16 @@ void kvm_arch_hardware_unsetup(void) int kvm_arch_check_processor_compat(void) { - struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); + int cpu = smp_processor_id(); + struct cpuinfo_x86 *c = &cpu_data(cpu); - WARN_ON(!irqs_disabled()); + /* + * Compatibility checks are done when loading KVM or in KVM's CPU + * hotplug callback. It ensures all online CPUs are compatible to run + * vCPUs. For other cases, compatibility checks are unnecessary or + * even problematic. Try to detect improper usages here. + */ + WARN_ON(!irqs_disabled() && cpu_active(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 f60724736cb1..96df789ecd4d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4855,7 +4855,11 @@ static void hardware_enable_nolock(void *caller_name) static int kvm_online_cpu(unsigned int cpu) { - int ret = 0; + int ret; + + ret = kvm_arch_check_processor_compat(); + if (ret) + return ret; raw_spin_lock(&kvm_count_lock); /* @@ -4915,6 +4919,17 @@ static int hardware_enable_all(void) { int r = 0; + /* + * During onlining a CPU, cpu_online_mask is set before kvm_online_cpu() + * is called. on_each_cpu() between them includes the CPU. As a result, + * hardware_enable_nolock() may get invoked before kvm_online_cpu(). + * This would enable hardware virtualization on that cpu without + * compatibility checks, which can potentially crash system or break + * running VMs. + * + * Disable CPU hotplug to prevent this case from happening. + */ + cpus_read_lock(); raw_spin_lock(&kvm_count_lock); kvm_usage_count++; @@ -4929,6 +4944,7 @@ static int hardware_enable_all(void) } raw_spin_unlock(&kvm_count_lock); + cpus_read_unlock(); return r; } -- 2.25.1