From patchwork Mon Aug 21 20:35:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= X-Patchwork-Id: 9913759 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2E1BA603F9 for ; Mon, 21 Aug 2017 20:47:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 14E811FE8D for ; Mon, 21 Aug 2017 20:47:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 06D07201B1; Mon, 21 Aug 2017 20:47:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HK_RANDOM_FROM, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B7A161FE8D for ; Mon, 21 Aug 2017 20:47:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=SJYqYVQnow5kq+x1gOWFXv/Lbd65KnbZiMRt8K9/Ii8=; b=HbsFnexjtZXjgS 6d7aldPZSl8cpljAHzhqgreUugpbxpG1Ci9UIGm4GbFP++Cdy7UMKdpSkispx/S/bn4kmPfpFwv7J 3vukDnVnWz/xFdw6RJOkNzu1nRm/aZKzXOpnat55ZmkSiRrmoPQvRSo3igcRfwBnRyCidLbyGSSJh u9vPRfDikeh2RtUpDeqzQma6um3/BF5HF/pRGneYTHsjikxwN5TC1T3oQl6TiYaQENO+H7n+kJkDL OLpcGnm9Eni3Xt/apJ5dpdFGX0jjBw1X6VIDLWuBsgmEl0vPyPwY2H4V5ZL1ieRv9Ngf9YGDsZtwl Nc0E/8LV3cpIM3lexsfQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1djtbb-0000v6-RF; Mon, 21 Aug 2017 20:47:27 +0000 Received: from mx1.redhat.com ([209.132.183.28]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1djtbI-00006J-Pg for linux-arm-kernel@lists.infradead.org; Mon, 21 Aug 2017 20:47:14 +0000 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 62D027EA8F; Mon, 21 Aug 2017 20:38:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 62D027EA8F Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=rkrcmar@redhat.com Received: from flask (unknown [10.43.2.80]) by smtp.corp.redhat.com (Postfix) with SMTP id 93D2517CD7; Mon, 21 Aug 2017 20:38:33 +0000 (UTC) Received: by flask (sSMTP sendmail emulation); Mon, 21 Aug 2017 22:38:32 +0200 From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mips@linux-mips.org, kvm-ppc@vger.kernel.org, linux-s390@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH RFC v3 7/9] KVM: add kvm_free_vcpus and kvm_arch_free_vcpus Date: Mon, 21 Aug 2017 22:35:28 +0200 Message-Id: <20170821203530.9266-8-rkrcmar@redhat.com> In-Reply-To: <20170821203530.9266-1-rkrcmar@redhat.com> References: <20170821203530.9266-1-rkrcmar@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 21 Aug 2017 20:38:39 +0000 (UTC) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170821_134709_243181_EE7A4FD0 X-CRM114-Status: GOOD ( 17.36 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Christoffer Dall , James Hogan , David Hildenbrand , Marc Zyngier , Cornelia Huck , Paul Mackerras , Christian Borntraeger , Paolo Bonzini , Alexander Graf Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Generalize clearing of kvm->vcpus. This should not be needed at all as all accesses to VCPUs in the destruction path are bugs, but maybe helps to catch them. The call path crosses arch/common code way too much, so extra untangling patch is welcome. Doing the clearing later seems be ok. I don't see a reason for the locking the mutex, so there definitely is a room for improvements. Suggested-by: Cornelia Huck Signed-off-by: Radim Krčmář --- arch/mips/kvm/mips.c | 13 ++----------- arch/powerpc/kvm/powerpc.c | 17 +++++++++-------- arch/s390/kvm/kvm-s390.c | 9 +-------- arch/x86/kvm/x86.c | 9 +-------- include/linux/kvm_host.h | 2 ++ virt/kvm/arm/arm.c | 17 +++++++++-------- virt/kvm/kvm_main.c | 17 +++++++++++++++++ 7 files changed, 41 insertions(+), 43 deletions(-) diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c index bce2a6431430..770c40b9df37 100644 --- a/arch/mips/kvm/mips.c +++ b/arch/mips/kvm/mips.c @@ -160,7 +160,7 @@ int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu) return 0; } -void kvm_mips_free_vcpus(struct kvm *kvm) +void kvm_arch_free_vcpus(struct kvm *kvm) { unsigned int i; struct kvm_vcpu *vcpu; @@ -168,15 +168,6 @@ void kvm_mips_free_vcpus(struct kvm *kvm) kvm_for_each_vcpu(i, vcpu, kvm) { kvm_arch_vcpu_free(vcpu); } - - mutex_lock(&kvm->lock); - - for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) - kvm->vcpus[i] = NULL; - - atomic_set(&kvm->online_vcpus, 0); - - mutex_unlock(&kvm->lock); } static void kvm_mips_free_gpa_pt(struct kvm *kvm) @@ -188,7 +179,7 @@ static void kvm_mips_free_gpa_pt(struct kvm *kvm) void kvm_arch_destroy_vm(struct kvm *kvm) { - kvm_mips_free_vcpus(kvm); + kvm_free_vcpus(kvm); kvm_mips_free_gpa_pt(kvm); } diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index 3480faaf1ef8..1c563545473c 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -456,11 +456,17 @@ int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu) return 0; } -void kvm_arch_destroy_vm(struct kvm *kvm) +void kvm_arch_free_vcpus(struct kvm *kvm) { - unsigned int i; + int i; struct kvm_vcpu *vcpu; + kvm_for_each_vcpu(i, vcpu, kvm) + kvm_arch_vcpu_free(vcpu); +} + +void kvm_arch_destroy_vm(struct kvm *kvm) +{ #ifdef CONFIG_KVM_XICS /* * We call kick_all_cpus_sync() to ensure that all @@ -471,14 +477,9 @@ void kvm_arch_destroy_vm(struct kvm *kvm) kick_all_cpus_sync(); #endif - kvm_for_each_vcpu(i, vcpu, kvm) - kvm_arch_vcpu_free(vcpu); + kvm_free_vcpus(kvm); mutex_lock(&kvm->lock); - for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) - kvm->vcpus[i] = NULL; - - atomic_set(&kvm->online_vcpus, 0); kvmppc_core_destroy_vm(kvm); diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c index 1534778a3c66..bb6278d45a25 100644 --- a/arch/s390/kvm/kvm-s390.c +++ b/arch/s390/kvm/kvm-s390.c @@ -1941,20 +1941,13 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) kmem_cache_free(kvm_vcpu_cache, vcpu); } -static void kvm_free_vcpus(struct kvm *kvm) +void kvm_arch_free_vcpus(struct kvm *kvm) { unsigned int i; struct kvm_vcpu *vcpu; kvm_for_each_vcpu(i, vcpu, kvm) kvm_arch_vcpu_destroy(vcpu); - - mutex_lock(&kvm->lock); - for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) - kvm->vcpus[i] = NULL; - - atomic_set(&kvm->online_vcpus, 0); - mutex_unlock(&kvm->lock); } void kvm_arch_destroy_vm(struct kvm *kvm) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e10eda86bc7b..d021746f1fdf 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8077,7 +8077,7 @@ static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) vcpu_put(vcpu); } -static void kvm_free_vcpus(struct kvm *kvm) +void kvm_arch_free_vcpus(struct kvm *kvm) { unsigned int i; struct kvm_vcpu *vcpu; @@ -8091,13 +8091,6 @@ static void kvm_free_vcpus(struct kvm *kvm) } kvm_for_each_vcpu(i, vcpu, kvm) kvm_arch_vcpu_free(vcpu); - - mutex_lock(&kvm->lock); - for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) - kvm->vcpus[i] = NULL; - - atomic_set(&kvm->online_vcpus, 0); - mutex_unlock(&kvm->lock); } void kvm_arch_sync_events(struct kvm *kvm) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index cfb3c0efdd51..a8b9aa563834 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -874,6 +874,8 @@ static inline bool kvm_arch_intc_initialized(struct kvm *kvm) int kvm_arch_init_vm(struct kvm *kvm, unsigned long type); void kvm_arch_destroy_vm(struct kvm *kvm); void kvm_arch_sync_events(struct kvm *kvm); +void kvm_free_vcpus(struct kvm *kvm); +void kvm_arch_free_vcpus(struct kvm *kvm); int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu); void kvm_vcpu_kick(struct kvm_vcpu *vcpu); diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c index b9f68e4add71..d63aa1107fdb 100644 --- a/virt/kvm/arm/arm.c +++ b/virt/kvm/arm/arm.c @@ -166,6 +166,14 @@ int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) return VM_FAULT_SIGBUS; } +void kvm_arch_free_vcpus(struct kvm *kvm) +{ + int i; + struct kvm_vcpu *vcpu; + + for_each_online_vcpu(i, vcpu, kvm) + kvm_arch_vcpu_free(vcpu); +} /** * kvm_arch_destroy_vm - destroy the VM data structure @@ -173,17 +181,10 @@ int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) */ void kvm_arch_destroy_vm(struct kvm *kvm) { - int i; - free_percpu(kvm->arch.last_vcpu_ran); kvm->arch.last_vcpu_ran = NULL; - for (i = 0; i < KVM_MAX_VCPUS; ++i) { - if (kvm->vcpus[i]) { - kvm_arch_vcpu_free(kvm->vcpus[i]); - kvm->vcpus[i] = NULL; - } - } + kvm_free_vcpus(kvm); kvm_vgic_destroy(kvm); } diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 33a15e176927..0d2d8b0c785c 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -750,6 +750,23 @@ static void kvm_destroy_devices(struct kvm *kvm) } } +void kvm_free_vcpus(struct kvm *kvm) +{ + int i; + + kvm_arch_free_vcpus(kvm); + + mutex_lock(&kvm->lock); + + i = atomic_read(&kvm->online_vcpus); + atomic_set(&kvm->online_vcpus, 0); + + while (i--) + kvm->vcpus[i] = NULL; + + mutex_unlock(&kvm->lock); +} + static void kvm_destroy_vm(struct kvm *kvm) { int i;