Message ID | 20221202105856.434886-1-vkuznets@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp765170wrr; Fri, 2 Dec 2022 03:02:04 -0800 (PST) X-Google-Smtp-Source: AA0mqf4MEPsQ+lVpB8lRfAvQhm0NGIc+04UWgddXpgZvNZVNU+ICLOCGSsBo4RfiE5iIIkxPb0Dk X-Received: by 2002:a05:6402:4504:b0:463:71ef:b9ce with SMTP id ez4-20020a056402450400b0046371efb9cemr54069139edb.75.1669978924761; Fri, 02 Dec 2022 03:02:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669978924; cv=none; d=google.com; s=arc-20160816; b=RPgE0AauL60D82hqYXxrJHSsvSQag4ooo/+ifA8lPO3tFwqegPjCJyI+w5quQQ0MTk /FvA27xEtmO6nl6kwoeitAi90+EHF5q+N/CAxelxs4tkesGOFSGIhw5xo2RN8rwjgQ8F m6rOyxXLh7p5Umkp8qAd8jE7Ov4yeMLrV7db2fyILDtJDc68HpiYWUOfeedMxXMmVmg2 hI+0v7EYEsmi10q7couYbM5eYD5Ud2MOK0Ls5OEbpvm6Pkbx9Dwzf+6xk3KsRRMc7wYL 6a3SgnJdzcHKabrcU8lCHb0STn4kEwcojsAIBnHqx2JaeE1+Sq1aITQBJDUWPMRrLhOe THmQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=hJSM4gVvibFGELZBTl77hUPT9yZlZqQ7ZzM552mEWO0=; b=tkEoLoTv+lbg+5yYgpdf4Va5pXflmd1Vr0mRJ4jVVmpw3JqIRNLORig1tYBdDjMj7o Pv9bjtswMrLeFw/YWwZtVYOxeFmLXiv1agpX2qk4uom9kZ7WZiF0dgRp8lqtkc99KvFn 5ESaYS1DuTjsi4IF3xKuoHoMvD0BNiUPQLbq89oNIX76pQMReNg269EJlSEPYP0YbZGp 9/O3VbGZ0NYGjd2fFE2nP8d+IUjo0fmnSsex69qYLvKBXK6ld7iiEIHfvtxvBip1tAhu hgtLMPK59IWTzyGSuHLn35lZtB+Nnl6gpzaNDx4NKYrXdXiDMEZNtZOkbPP+6TSR4nkD S5BQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RbszEByY; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f21-20020aa7d855000000b00467ad3f4426si5819560eds.312.2022.12.02.03.01.38; Fri, 02 Dec 2022 03:02:04 -0800 (PST) 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=RbszEByY; 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 S232743AbiLBLAE (ORCPT <rfc822;lhua1029@gmail.com> + 99 others); Fri, 2 Dec 2022 06:00:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230094AbiLBLAC (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 2 Dec 2022 06:00:02 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9CCA7BC0C for <linux-kernel@vger.kernel.org>; Fri, 2 Dec 2022 02:59:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669978749; 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; bh=hJSM4gVvibFGELZBTl77hUPT9yZlZqQ7ZzM552mEWO0=; b=RbszEByY14J0HX5nzoTwIpNxOM1DfzSZwWJ1XozWyvtabDXQKZt0Z8iXvX1l9FT+2ytgxF 1sEvOBbC3a5pPioGLvv6EwLB5EFad6IKFNJhHi6DcyrVei81H1NjjGhIhR6Oz1VawWBCea 5//rj6XFVNHqO5aS9pJRZzOuWoZQpbA= 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-418-Q0X0gSc_PSy_bl-R3uZVhA-1; Fri, 02 Dec 2022 05:59:01 -0500 X-MC-Unique: Q0X0gSc_PSy_bl-R3uZVhA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7F4E729324B7; Fri, 2 Dec 2022 10:59:01 +0000 (UTC) Received: from ovpn-192-33.brq.redhat.com (ovpn-192-33.brq.redhat.com [10.40.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 89580C16922; Fri, 2 Dec 2022 10:58:57 +0000 (UTC) From: Vitaly Kuznetsov <vkuznets@redhat.com> To: kvm@vger.kernel.org, Paolo Bonzini <pbonzini@redhat.com>, Sean Christopherson <seanjc@google.com> Cc: Wanpeng Li <wanpengli@tencent.com>, Jim Mattson <jmattson@google.com>, Maxim Levitsky <mlevitsk@redhat.com>, coverity-bot <keescook@chromium.org>, linux-kernel@vger.kernel.org Subject: [PATCH] KVM: x86: hyper-v: Fix 'using uninitialized value' Coverity warning Date: Fri, 2 Dec 2022 11:58:56 +0100 Message-Id: <20221202105856.434886-1-vkuznets@redhat.com> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751099821089724820?= X-GMAIL-MSGID: =?utf-8?q?1751099821089724820?= |
Series |
KVM: x86: hyper-v: Fix 'using uninitialized value' Coverity warning
|
|
Commit Message
Vitaly Kuznetsov
Dec. 2, 2022, 10:58 a.m. UTC
In kvm_hv_flush_tlb(), 'data_offset' and 'consumed_xmm_halves' variables
are used in a mutually exclusive way: in 'hc->fast' we count in 'XMM
halves' and increase 'data_offset' otherwise. Coverity discovered, that in
one case both variables are incremented unconditionally. This doesn't seem
to cause any issues as the only user of 'data_offset'/'consumed_xmm_halves'
data is kvm_hv_get_tlb_flush_entries() -> kvm_hv_get_hc_data() which also
takes into account 'hc->fast' but is still worth fixing.
While on it, drop the unneeded 'consumed_xmm_halves' initializer. In
'hc->fast' case the variable is always initialized and is not used
otherwise, 'data_offset' is not being initialized either.
Reported-by: coverity-bot <keescook+coverity-bot@chromium.org>
Addresses-Coverity-ID: 1527764 ("Uninitialized variables")
Fixes: 260970862c88 ("KVM: x86: hyper-v: Handle HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST{,EX} calls gently")
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
arch/x86/kvm/hyperv.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
Comments
On Fri, Dec 02, 2022, Vitaly Kuznetsov wrote: > In kvm_hv_flush_tlb(), 'data_offset' and 'consumed_xmm_halves' variables > are used in a mutually exclusive way: in 'hc->fast' we count in 'XMM > halves' and increase 'data_offset' otherwise. Coverity discovered, that in > one case both variables are incremented unconditionally. This doesn't seem > to cause any issues as the only user of 'data_offset'/'consumed_xmm_halves' > data is kvm_hv_get_tlb_flush_entries() -> kvm_hv_get_hc_data() which also > takes into account 'hc->fast' but is still worth fixing. If those calls aren't inlined, then 32-bit Hyper-V will be "consuming" uninitialized data when pushing parameters onto the stack. It won't cause real problems, but checkers might complain. What about shoving this metadata into "struct kvm_hv_hcall" as a union? That'd help convey that the two are mutually exclusive, would provide a place to document said exclusion, and would yield a nice cleanup too by eliminating multiple params from various functions.
Sean Christopherson <seanjc@google.com> writes: > On Fri, Dec 02, 2022, Vitaly Kuznetsov wrote: >> In kvm_hv_flush_tlb(), 'data_offset' and 'consumed_xmm_halves' variables >> are used in a mutually exclusive way: in 'hc->fast' we count in 'XMM >> halves' and increase 'data_offset' otherwise. Coverity discovered, that in >> one case both variables are incremented unconditionally. This doesn't seem >> to cause any issues as the only user of 'data_offset'/'consumed_xmm_halves' >> data is kvm_hv_get_tlb_flush_entries() -> kvm_hv_get_hc_data() which also >> takes into account 'hc->fast' but is still worth fixing. > > If those calls aren't inlined, then 32-bit Hyper-V will be "consuming" uninitialized > data when pushing parameters onto the stack. It won't cause real problems, but > checkers might complain. > > What about shoving this metadata into "struct kvm_hv_hcall" as a union? That'd > help convey that the two are mutually exclusive, would provide a place to document > said exclusion, and would yield a nice cleanup too by eliminating multiple params > from various functions. "struct kvm_hv_hcall" used to hold raw data from the guest and 'consumed_xmm_halves'/ 'data_offset' are rather our implementation details, how we consume these data. I don't see why we can't re-purpose it a little bit to hold both, let me try that in v2.
diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 2c7f2a26421e..dee4961ad8ff 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -1926,7 +1926,7 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc) struct kvm_vcpu *v; unsigned long i; bool all_cpus; - int consumed_xmm_halves = 0; + int consumed_xmm_halves; gpa_t data_offset; /* @@ -2021,8 +2021,10 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc) * consumed_xmm_halves to make sure TLB flush entries are read * from the correct offset. */ - data_offset += hc->var_cnt * sizeof(sparse_banks[0]); - consumed_xmm_halves += hc->var_cnt; + if (hc->fast) + consumed_xmm_halves += hc->var_cnt; + else + data_offset += hc->var_cnt * sizeof(sparse_banks[0]); } if (hc->code == HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE ||