Received: by 2002:a05:6358:111d:b0:dc:6189:e246 with SMTP id f29csp3564181rwi; Tue, 1 Nov 2022 23:57:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM460nssx9ZZweRtvTqQxd3EWyoKIt42Cv+f3SO4tlpJc+Pfb2MYBr2WShVn9QC2NTklBqVu X-Received: by 2002:a17:902:e902:b0:186:9c03:5f27 with SMTP id k2-20020a170902e90200b001869c035f27mr23197169pld.16.1667372223621; Tue, 01 Nov 2022 23:57:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667372223; cv=none; d=google.com; s=arc-20160816; b=MQDCtkDinzpydaHak+/idnvT5JtHfXhpT/2WTzcOpofuaBy19tjos8ysrXPP7c9YDM wjLrIt1AqCDA3/CXEOgytSyV0IUL9v8ohrWVVfehWg/ec8Qeyz15kNkhawWpzADF6kFe Lf7VaRGLQnv9gYqdSHHX3i5B6QCK50f9RU17MAd2bmwEvWqzE0QsMvee6F+Tu7aKi9jJ RGyXvG6/fxwCrzO1W6g/AIjeWimxOWv7aax/7SSEAgJf60sKATBRsllAQRgoAhMIlg0V SUHcKOcVNA+opFKItoLi7nIYg66lBL14RWfI+bG0kDuNNKaaIcQkpqurp9U2KGyNbQJ0 VUCg== 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=d2JL24J5OJkG2iBz3+yJWwWocp3Yq1/s/7q6KUxHEB0=; b=v/bPRse22hR1jHo21llx+RD6WdYx2vrl8fdp1JH3lRiPPk3q0iEs5KrosajOxBoX5x CY17OKn0LRhzrtNwkHajGTJPedhNcp9CbnCamU/QSCT30w2100cPv0Epns3g5ZgegqAU gz45MfBsUyEkpn+qyYR3kogAdputp2XKwIMnIoE4cJoTtbV91VHIjXTxP1cvDx779iUZ 8dEAwkNdMrbjOtFjdEMDbWATzUKXnIgIYNG9vGYz+aKAeC2JnzGdXgzhtrnzFoyi4aVP SqT9PwcVi+BJfngzrIx1P8q1nLERGPizkiKwiv26ye9npEUFK0aa3lLIGERRl3NMW/20 5Q4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EMK7UqWR; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u6-20020a63d346000000b00439fb921f8fsi14807413pgi.460.2022.11.01.23.56.51; Tue, 01 Nov 2022 23:57:03 -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=@intel.com header.s=Intel header.b=EMK7UqWR; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230182AbiKBGYd (ORCPT + 96 others); Wed, 2 Nov 2022 02:24:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230013AbiKBGYX (ORCPT ); Wed, 2 Nov 2022 02:24:23 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 602EB26109; Tue, 1 Nov 2022 23:24:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667370263; x=1698906263; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0CO9zqn5QJQYlgV3XsYpGa3hDmGaoRGtQlVPOneiyEU=; b=EMK7UqWRLUMlJcDHWL1iOIwEoJE8MBwjpf9d1Tu9twfJz9MrPLS+F4vn Bs+isjbLzs870MU8zdquEfMmEPtJW+00DAbvP39lLZn/Ijj3MwG4gNCwX oUJ0GWV0XYfGKGWdmXDYEkV5ZU6S4Rj7Zf8V5sayW/Z7gVi2OgL6IZMQ5 ceB9mjgmENJvE9ULx60SYXObdjY+oQTpgc9MZYTo+g10Gsm+kv8xA0rxc kvBEnO/D20Lrs/H6fwN0Yes2L1hdHZ/2tSA5lFLXh5FYJ2Kbah1v/WRJJ qm7EuHnGfyjnmXcK+Xl9amcE5ZYK+4nNFW9oZb6LVs4WyxIVDMdeBwcBh Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10518"; a="296765013" X-IronPort-AV: E=Sophos;i="5.95,232,1661842800"; d="scan'208";a="296765013" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Nov 2022 23:24:22 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10518"; a="703146406" X-IronPort-AV: E=Sophos;i="5.95,232,1661842800"; d="scan'208";a="703146406" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Nov 2022 23:24:21 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com Subject: [PATCH 3/4] KVM: kvm_main.c: Remove a global variable, hardware_enable_failed Date: Tue, 1 Nov 2022 23:24:10 -0700 Message-Id: <0836ee2f6c40091907092fc07fd50e64a499e6d1.1667369456.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE 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 From: Isaku Yamahata A global variable hardware_enable_failed in kvm_main.c is used only by hardware_enable_all() and hardware_enable_nolock(). It doesn't have to be a global variable. Make it function local. Signed-off-by: Isaku Yamahata --- virt/kvm/kvm_main.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 1a21b21ba326..ac24fef2c818 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -105,7 +105,6 @@ LIST_HEAD(vm_list); static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE; static int kvm_usage_count; -static atomic_t hardware_enable_failed; static struct kmem_cache *kvm_vcpu_cache; @@ -5007,30 +5006,36 @@ static struct miscdevice kvm_dev = { &kvm_chardev_ops, }; -static void hardware_enable_nolock(void *junk) +static int __hardware_enable_nolock(void) { int cpu = raw_smp_processor_id(); int r; - if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) - return; - - cpumask_set_cpu(cpu, &cpus_hardware_enabled); + if (cpumask_test_and_set_cpu(cpu, &cpus_hardware_enabled)) + return 0; r = kvm_arch_hardware_enable(); if (r) { cpumask_clear_cpu(cpu, &cpus_hardware_enabled); - atomic_inc(&hardware_enable_failed); pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); } + return r; +} + +static void hardware_enable_nolock(void *arg) +{ + atomic_t *failed = arg; + + if (__hardware_enable_nolock()) + atomic_inc(failed); } static int kvm_starting_cpu(unsigned int cpu) { raw_spin_lock(&kvm_count_lock); if (kvm_usage_count) - hardware_enable_nolock(NULL); + (void)__hardware_enable_nolock(); raw_spin_unlock(&kvm_count_lock); return 0; } @@ -5072,16 +5077,16 @@ static void hardware_disable_all(void) static int hardware_enable_all(void) { + atomic_t failed = ATOMIC_INIT(0); int r = 0; raw_spin_lock(&kvm_count_lock); kvm_usage_count++; if (kvm_usage_count == 1) { - atomic_set(&hardware_enable_failed, 0); - on_each_cpu(hardware_enable_nolock, NULL, 1); + on_each_cpu(hardware_enable_nolock, &failed, 1); - if (atomic_read(&hardware_enable_failed)) { + if (atomic_read(&failed)) { hardware_disable_all_nolock(); r = -EBUSY; } @@ -5702,7 +5707,7 @@ static void kvm_resume(void) { if (kvm_usage_count) { lockdep_assert_not_held(&kvm_count_lock); - hardware_enable_nolock(NULL); + (void)__hardware_enable_nolock(); } } -- 2.25.1