Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp1430647rwe; Thu, 1 Sep 2022 19:25:48 -0700 (PDT) X-Google-Smtp-Source: AA6agR4z+CcFgWrYvM2qAd/+yavb5dhVT7QP3G56Fo9PsGEjkCrFP7qGcOD8GUYdZlYo4WVMjqZK X-Received: by 2002:a17:907:1c82:b0:741:7374:cfb5 with SMTP id nb2-20020a1709071c8200b007417374cfb5mr16604026ejc.451.1662085548070; Thu, 01 Sep 2022 19:25:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662085548; cv=none; d=google.com; s=arc-20160816; b=R5hgHc853wRq76KuDCtD75nVilQAP4MeaNuvvtleAxGijO1TIyRMwuh8OKeOttGf05 iykT7sjlzwjva2jeeGHAS8m47shGBSf5oONNdPC4jQiI7M95qd92oN9+IYkQU2tVEADg +JuMYrNU2sfD9A+kDEslp+1omghBQh1eCNE64Uy10IAIPfGDvNHmUK8U2YHoZK0aD0Sw PkhZFNCAOmMKWXQ1RN1z9wNjxOsdSJXNwHp2WTgvUV3Cb7Mkc+B1Rqj3GTP7c0l11tKg 8s3k0c2D25ifECYL/7g52tcDauYFGA+xhzPTt5T98kbJfyOpDC1maDjgdB1m1B1RHJk/ EVfg== 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=iyZKvJFFQ+s4ah8L5BkCOTFUs46UNETKXjGGN0hDEc4=; b=AsIx6MSdFW162iISs//ctAJ7hwcA6EFufy5KtXcii1rnHPi2/QcB0at1ikseMkVDIB elBLTbbhXryx/AXQbTPwyJ3wTspTBEV7HsVC3V54UgaJ7wS8sMB2QCllSvb8jA0Bg7YD /EgE4eAcldh+bJ4UudLA7wxpZLpAOcRQusxoj1+KU34dUlHo2u04j4whJb+k3BZWXAhZ iqUVbP30RbMfp9AWFWuYWIHaD8RbFH2dLOq+UbVngvFUNOqzH7cUIax3On92wiqjae7Y Flga7snYvroE3OW8VqclNynhLBh8/iHwoK/R73fK6I9KjAFuu0EEjpHs7ZrJMi3rqdzY 0YRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Q81GJMHJ; 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 qa11-20020a170907868b00b00730a2437555si751176ejc.796.2022.09.01.19.25.20; Thu, 01 Sep 2022 19:25:48 -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=Q81GJMHJ; 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 S235340AbiIBCTb (ORCPT + 99 others); Thu, 1 Sep 2022 22:19:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234998AbiIBCSS (ORCPT ); Thu, 1 Sep 2022 22:18:18 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 708FFAA4DA; Thu, 1 Sep 2022 19:18:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662085097; x=1693621097; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kST15Iufsk3QvbMIsi5+QuBElZ8TEn8I+xaF11mTEA8=; b=Q81GJMHJez7oIoYEktruuYCJYolGoEo57HIZvnq52y/SwhL5SJh7Z6qL B7jdAP344mMY40Xbk2NRE/vSvFlmY3l4nHgtr8wrCBp/nheafEW1cyDIF YegJI0kd6hx2JkVD9lf0h8YafNHSw0b3AdqIeeOAu73dX/87DdWViv+yz k22zl4f58doEyQijIMIZzx2SeOrlTtRsNJwrwDX8pNUld2e6Fz08zweKs xSBPQ6VZs95rjFyk3UyINMWaToCG8xD2s+JcI2edbfzkEeuf0Mzz7X8EZ SYVc+C8bGfzxn7Sme5iY5c1xp3iHVfrOHtyLXuac7132v9TqY3hrh2H3A w==; X-IronPort-AV: E=McAfee;i="6500,9779,10457"; a="297157853" X-IronPort-AV: E=Sophos;i="5.93,281,1654585200"; d="scan'208";a="297157853" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Sep 2022 19:18:17 -0700 X-IronPort-AV: E=Sophos;i="5.93,281,1654585200"; d="scan'208";a="608835642" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Sep 2022 19:18:16 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v3 13/22] KVM: Add arch hook when VM is added/deleted Date: Thu, 1 Sep 2022 19:17:48 -0700 Message-Id: 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=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 and pass kvm_usage_count with kvm_lock. Move kvm_arch_post_init_vm() under kvm_arch_add_vm(). Later kvm_arch_post_init_vm() is deleted once x86 overrides kvm_arch_add_vm(). Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_arch.c | 12 +++++++++++- virt/kvm/kvm_main.c | 21 +++++++++++++++++---- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index dd2a6d98d4de..f78364e01ca9 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1445,6 +1445,8 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu); bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu); int kvm_arch_post_init_vm(struct kvm *kvm); +int kvm_arch_add_vm(struct kvm *kvm, int usage_count); +int kvm_arch_del_vm(int usage_count); void kvm_arch_pre_destroy_vm(struct kvm *kvm); int kvm_arch_create_vm_debugfs(struct kvm *kvm); diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c index 4748a76bcb03..0eac996f4981 100644 --- a/virt/kvm/kvm_arch.c +++ b/virt/kvm/kvm_arch.c @@ -10,11 +10,21 @@ #include +__weak int kvm_arch_post_init_vm(struct kvm *kvm) +{ + return 0; +} + /* * Called after the VM is otherwise initialized, but just before adding it to * the vm_list. */ -__weak int kvm_arch_post_init_vm(struct kvm *kvm) +__weak int kvm_arch_add_vm(struct kvm *kvm, int usage_count) +{ + return kvm_arch_post_init_vm(kvm); +} + +__weak int kvm_arch_del_vm(int usage_count) { return 0; } diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index e62240fb8474..90e1dcfc9ace 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -145,6 +145,7 @@ static int kvm_no_compat_open(struct inode *inode, struct file *file) static int hardware_enable_all(void); static void hardware_disable_all(void); static void hardware_disable_nolock(void *junk); +static void kvm_del_vm(void); static void kvm_io_bus_destroy(struct kvm_io_bus *bus); @@ -1215,11 +1216,12 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) if (r) goto out_err_no_debugfs; - r = kvm_arch_post_init_vm(kvm); - if (r) - goto out_err; - mutex_lock(&kvm_lock); + r = kvm_arch_add_vm(kvm, kvm_usage_count); + if (r) { + mutex_unlock(&kvm_lock); + goto out_err; + } list_add(&kvm->vm_list, &vm_list); mutex_unlock(&kvm_lock); @@ -1239,6 +1241,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) #endif out_err_no_mmu_notifier: hardware_disable_all(); + kvm_del_vm(); out_err_no_disable: kvm_arch_destroy_vm(kvm); out_err_no_arch_destroy_vm: @@ -1319,6 +1322,7 @@ static void kvm_destroy_vm(struct kvm *kvm) kvm_arch_free_vm(kvm); preempt_notifier_dec(); hardware_disable_all(); + kvm_del_vm(); mmdrop(mm); module_put(kvm_chardev_ops.owner); } @@ -5082,6 +5086,15 @@ static void hardware_disable_all(void) cpus_read_unlock(); } +static void kvm_del_vm(void) +{ + cpus_read_lock(); + mutex_lock(&kvm_lock); + kvm_arch_del_vm(kvm_usage_count); + mutex_unlock(&kvm_lock); + cpus_read_unlock(); +} + static int hardware_enable_all(void) { int r = 0; -- 2.25.1