Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp4221054rwe; Tue, 30 Aug 2022 06:42:11 -0700 (PDT) X-Google-Smtp-Source: AA6agR7X0cCKqCB0dh5KvY9wwfwBIXyqRI/f0AHtVLMTOKCbNrmiY9XpfIcBAm3uj1UbOUT9egwQ X-Received: by 2002:a05:6402:5ce:b0:448:d458:baf with SMTP id n14-20020a05640205ce00b00448d4580bafmr2133383edx.388.1661866931543; Tue, 30 Aug 2022 06:42:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661866931; cv=none; d=google.com; s=arc-20160816; b=OCuj0xmjdE9JIIYFEL1ymuYaInSwxni6NllsTKPFWoRD87HEn9S3iqcGXIgovGMDRx DPBjTReK/5P96TgFxqPiuKVJRt/xyeLClDAslWCD+reXmu5ZwOxvUWUEuHGAUSrASjzS x6jg7Tr0kqxVvfdO1x5E+l4kyrEhVLO0kvErAuPkpFsy+TgLic4bjv8k05LcyxdGqwTz fY0iLmF6epHkM1MgqSLzhh9UYMpYFPURJeJJg5eMagPQa9mLSXxRRnmmMhBXJcU6Nu2+ d3JeqZT/QE13dgHNcPldrdN7T5D4d2dlkqvWSAgw9nnjyUj7dmH51ahf8AAFynChBBcy mH3w== 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=chPmOM7qhlrImHiruqIlAyoMO1lUXpPxIodNACRsyik=; b=vg3OIOP3MWUv0vhCeAppK3/LRbNvW8tgm8zKoFcIjXOBfr7lJ21gVdL3h6hUV5ihWF PI9GQonxYhVBkcY/df9LvFC+1VS0QD4LZNpAdrJwvAHfTeiwmoYuECNmF8jhKJhO/JWd VYnKxl48kv6cY2j9ohNRRo4Pp1tVKeh5sKG5IFdBDR5vVtpShlKZsA7wt8O6HTXMAY0a aPZ+uYNybTQZfFq/L/GvmJkEdNMq5Cr1TU0Ehs62Pc/C5oErtt5wVUGxrKZi7gTRyyAA J4PQy0mjiOhEOVLBmwK5iA9nwIIztN6C+jgyBxf+Y2JzsE1hGmxV6BZLkLK4XxRcMfDB T0PA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MIoZct1h; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qb5-20020a1709077e8500b0073832d0ce04si9029042ejc.745.2022.08.30.06.41.45; Tue, 30 Aug 2022 06:42:11 -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=@redhat.com header.s=mimecast20190719 header.b=MIoZct1h; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229873AbiH3Ni2 (ORCPT + 99 others); Tue, 30 Aug 2022 09:38:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230181AbiH3NiG (ORCPT ); Tue, 30 Aug 2022 09:38:06 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19BD0ED01D for ; Tue, 30 Aug 2022 06:37:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661866673; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=chPmOM7qhlrImHiruqIlAyoMO1lUXpPxIodNACRsyik=; b=MIoZct1hLLa0RhQuDAkmUPg0o+HXVEVRb7I8o5sKVVlG1PBOapD1dXZ9cU47FSn3UZMkac J6Wa/BSPFeMQMCQ3wdfe7w/EnwU0llAEz2oI4Y1vK3BnQWvkW+QG7eDG7Bvg96AbqBJ+Uj X4CvVscbTVwzGnVBa/BAd8KvYkv9VU4= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-673-6ItNH4xKPFKIhrgQYBZXKQ-1; Tue, 30 Aug 2022 09:37:50 -0400 X-MC-Unique: 6ItNH4xKPFKIhrgQYBZXKQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9C2A72999B44; Tue, 30 Aug 2022 13:37:49 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.40.194.232]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7D0612166B26; Tue, 30 Aug 2022 13:37:47 +0000 (UTC) From: Vitaly Kuznetsov To: kvm@vger.kernel.org, Paolo Bonzini Cc: Sean Christopherson , Wanpeng Li , Jim Mattson , Michael Kelley , Yuan Yao , Maxim Levitsky , linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 04/33] KVM: x86: Check for existing Hyper-V vCPU in kvm_hv_vcpu_init() Date: Tue, 30 Aug 2022 15:37:08 +0200 Message-Id: <20220830133737.1539624-5-vkuznets@redhat.com> In-Reply-To: <20220830133737.1539624-1-vkuznets@redhat.com> References: <20220830133737.1539624-1-vkuznets@redhat.com> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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: Sean Christopherson When potentially allocating/initializing the Hyper-V vCPU struct, check for an existing instance in kvm_hv_vcpu_init() instead of requiring callers to perform the check. Relying on callers to do the check is risky as it's all too easy for KVM to overwrite vcpu->arch.hyperv and leak memory, and it adds additional burden on callers without much benefit. No functional change intended. Signed-off-by: Sean Christopherson Signed-off-by: Vitaly Kuznetsov --- arch/x86/kvm/hyperv.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 611c349a08bf..8aadd31ed058 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -936,9 +936,12 @@ static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index) static int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) { - struct kvm_vcpu_hv *hv_vcpu; + struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); int i; + if (hv_vcpu) + return 0; + hv_vcpu = kzalloc(sizeof(struct kvm_vcpu_hv), GFP_KERNEL_ACCOUNT); if (!hv_vcpu) return -ENOMEM; @@ -962,11 +965,9 @@ int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages) struct kvm_vcpu_hv_synic *synic; int r; - if (!to_hv_vcpu(vcpu)) { - r = kvm_hv_vcpu_init(vcpu); - if (r) - return r; - } + r = kvm_hv_vcpu_init(vcpu); + if (r) + return r; synic = to_hv_synic(vcpu); @@ -1660,10 +1661,8 @@ int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) if (!host && !vcpu->arch.hyperv_enabled) return 1; - if (!to_hv_vcpu(vcpu)) { - if (kvm_hv_vcpu_init(vcpu)) - return 1; - } + if (kvm_hv_vcpu_init(vcpu)) + return 1; if (kvm_hv_msr_partition_wide(msr)) { int r; @@ -1683,10 +1682,8 @@ int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) if (!host && !vcpu->arch.hyperv_enabled) return 1; - if (!to_hv_vcpu(vcpu)) { - if (kvm_hv_vcpu_init(vcpu)) - return 1; - } + if (kvm_hv_vcpu_init(vcpu)) + return 1; if (kvm_hv_msr_partition_wide(msr)) { int r; @@ -2000,7 +1997,7 @@ void kvm_hv_set_cpuid(struct kvm_vcpu *vcpu) return; } - if (!to_hv_vcpu(vcpu) && kvm_hv_vcpu_init(vcpu)) + if (kvm_hv_vcpu_init(vcpu)) return; hv_vcpu = to_hv_vcpu(vcpu); -- 2.37.2