From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB70A13C83D for ; Wed, 14 Aug 2024 08:45:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723625107; cv=none; b=FfmQ8E2pvue6R3qWx3jGyz6PScgucSBWJYPO9HRG4f40aoPNkOhlUh39rkvzSeQSleZWflWo6yRDYvJfbSg/KKWjGreEISUaDdDND3qObs/SWFAPESkVDbfNiXtBZPnrr/jPlaXWRkF4x1w/i6UQBHBNNTkUmu/R7uCvLMkrbvg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723625107; c=relaxed/simple; bh=QrDGC86x1xaRXuKpPMO6Uk2eDrZ9ud7JG3yamK/yxnk=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=QLyKEMGM8z26XVskMKm/iq3I80YMnN4xYSGVyaeCXk0hHsdYFJTx25QX+Eq19PmPH0hoLCGU5lIic3UbKMkkZvWd3gIjQVqSPMsYpUPJXOtKF7vu8swwI/dxaFT+PfIY6/xu59ViK//OvYx0KYO4BF07fk4D7NCRl2jKE/NZMAY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=SV7WyUtB; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="SV7WyUtB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723625104; 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: in-reply-to:in-reply-to:references:references; bh=7hwkKCoOBN+b1OvqT/QSoUQZv/ejIIMA/dhfZb9tdx0=; b=SV7WyUtBTe3+/EELOkra8d0WysJxe91HQBctREEgqzVhOVY/YnnUJfnZAofVUxNKMmJjhm JeyQgZazoEpciYf3Uve9BsNG2ZICJmHnP9YKNnZhFgwHC+njtYwuSf8I0kHImSXbZbhRMU PfJQtuE/yr3UC4pAgQAmaOnBTo4bxR0= Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-592-qXt-fFVePo-uREgsWERxjg-1; Wed, 14 Aug 2024 04:45:03 -0400 X-MC-Unique: qXt-fFVePo-uREgsWERxjg-1 Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-532fbabc468so275991e87.0 for ; Wed, 14 Aug 2024 01:45:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723625102; x=1724229902; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7hwkKCoOBN+b1OvqT/QSoUQZv/ejIIMA/dhfZb9tdx0=; b=L3Q82uDECrG9Gh1QBClxmMugTbEGzV4zl2dsrXeppdMYIzUaPPvC6GVgSBYZOXMboh i+QJIsrVSGcAjSnZ54H+SfatKF0W3htO+FtskO3TEgRbKUyrRCj+0JisC74mckqrLiFj iPtm0ymUz6u1LEL/PgrSpYxEDCNlli3iEkhWI0jDNVDABI//zMsgzY4zYocOk/pWE05W 52OIho5ZW3c1trUXNAQX8js6UDNM6dpXZ9VeyDjPnmlT3mUqGZf6b4+J01A6AnlDQG+T 3905Z44FL+ckSHmiupTH9d5tVCfzCSOIvFkHE0iQ6fa4AX8JtCowxm5Gz73dZp5ouwcl 8rdQ== X-Forwarded-Encrypted: i=1; AJvYcCXXs+6wqCiR3pGWU4mXAKsm/u/+FZLtzvOY5rQ8Z0u3gr5efv5GBGiykdBCM2eB5nqK/SyQfNy1Lota7+Ml/oj6ImItbyTTvZB5fycW X-Gm-Message-State: AOJu0Yz2414zwkqWulGUhuzN/3be1H+N/I/mll9QNS/2KKtk97A3qFqQ rRxYJwpLstOGxTE3K+f+fPo/ixHQyXAhCp+gjT2yBeRa2LzPgVOXIZLdGPItcXM3UQe+OYh/1Te eOwyxVKX6t5LZtSBSpsUawnzI2oA1nGfNBhnUL1+UXGriK7hvHOuRlkmd5+X/lA== X-Received: by 2002:a05:6512:1388:b0:52e:7542:f471 with SMTP id 2adb3069b0e04-532eda83bd3mr1535834e87.29.1723625101819; Wed, 14 Aug 2024 01:45:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IExvv2JH1bujn3OdzYrvDxJ3jmVSbA8zfxLsfLv8SXbL6vxyV/RWT5TFXZWT1E65o1HmeWJUQ== X-Received: by 2002:a05:6512:1388:b0:52e:7542:f471 with SMTP id 2adb3069b0e04-532eda83bd3mr1535805e87.29.1723625101246; Wed, 14 Aug 2024 01:45:01 -0700 (PDT) Received: from fedora (g2.ign.cz. [91.219.240.8]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36e4ebd2bb4sm12394742f8f.91.2024.08.14.01.45.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 01:45:00 -0700 (PDT) From: Vitaly Kuznetsov To: Mirsad Todorovac Cc: kvm@vger.kernel.org, Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org, Sean Christopherson Subject: Re: [BUG] arch/x86/kvm/vmx/vmx_onhyperv.h:109:36: error: dereference of NULL =?utf-8?B?4oCYMOKAmQ==?= In-Reply-To: References: <207a5c75-b6ad-4bfb-b436-07d4a3353003@gmail.com> <87a5i05nqj.fsf@redhat.com> Date: Wed, 14 Aug 2024 10:44:59 +0200 Message-ID: <87plqbfq7o.fsf@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Mirsad Todorovac writes: > On 7/29/24 15:31, Vitaly Kuznetsov wrote: >> Mirsad Todorovac writes: >> >>> On 7/19/24 20:53, Sean Christopherson wrote: >>>> On Fri, Jul 19, 2024, Mirsad Todorovac wrote: >>>>> Hi, all! >>>>> >>>>> Here is another potential NULL pointer dereference in kvm subsystem of linux >>>>> stable vanilla 6.10, as GCC 12.3.0 complains. >>>>> >>>>> (Please don't throw stuff at me, I think this is the last one for today :-) >>>>> >>>>> arch/x86/include/asm/mshyperv.h >>>>> ------------------------------- >>>>> 242 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) >>>>> 243 { >>>>> 244 if (!hv_vp_assist_page) >>>>> 245 return NULL; >>>>> 246 >>>>> 247 return hv_vp_assist_page[cpu]; >>>>> 248 } >>>>> >>>>> arch/x86/kvm/vmx/vmx_onhyperv.h >>>>> ------------------------------- >>>>> 102 static inline void evmcs_load(u64 phys_addr) >>>>> 103 { >>>>> 104 struct hv_vp_assist_page *vp_ap = >>>>> 105 hv_get_vp_assist_page(smp_processor_id()); >>>>> 106 >>>>> 107 if (current_evmcs->hv_enlightenments_control.nested_flush_hypercall) >>>>> 108 vp_ap->nested_control.features.directhypercall = 1; >>>>> 109 vp_ap->current_nested_vmcs = phys_addr; >>>>> 110 vp_ap->enlighten_vmentry = 1; >>>>> 111 } >>>>> >> >> ... >> >>> >>> GCC 12.3.0 appears unaware of this fact that evmcs_load() cannot be called with hv_vp_assist_page() == NULL. >>> >>> This, for example, silences the warning and also hardens the code against the "impossible" situations: >>> >>> -------------------><------------------------------------------------------------------ >>> diff --git a/arch/x86/kvm/vmx/vmx_onhyperv.h b/arch/x86/kvm/vmx/vmx_onhyperv.h >>> index eb48153bfd73..8b0e3ffa7fc1 100644 >>> --- a/arch/x86/kvm/vmx/vmx_onhyperv.h >>> +++ b/arch/x86/kvm/vmx/vmx_onhyperv.h >>> @@ -104,6 +104,11 @@ static inline void evmcs_load(u64 phys_addr) >>> struct hv_vp_assist_page *vp_ap = >>> hv_get_vp_assist_page(smp_processor_id()); >>> >>> + if (!vp_ap) { >>> + pr_warn("BUG: hy_get_vp_assist_page(%d) returned NULL.\n", smp_processor_id()); >>> + return; >>> + } >>> + >>> if (current_evmcs->hv_enlightenments_control.nested_flush_hypercall) >>> vp_ap->nested_control.features.directhypercall = 1; >>> vp_ap->current_nested_vmcs = phys_addr; >> >> As Sean said, this does not seem to be possible today but I uderstand >> why the compiler is not able to infer this. If we were to fix this, I'd >> suggest we do something like "BUG_ON(!vp_ap)" (with a comment why) >> instead of the suggested patch: > > That sounds awesome, but I really dare not poke into KVM stuff at my level. :-/ > What I meant is something along these lines (untested): diff --git a/arch/x86/kvm/vmx/vmx_onhyperv.h b/arch/x86/kvm/vmx/vmx_onhyperv.h index eb48153bfd73..e2d8c67d0cad 100644 --- a/arch/x86/kvm/vmx/vmx_onhyperv.h +++ b/arch/x86/kvm/vmx/vmx_onhyperv.h @@ -104,6 +104,14 @@ static inline void evmcs_load(u64 phys_addr) struct hv_vp_assist_page *vp_ap = hv_get_vp_assist_page(smp_processor_id()); + /* + * When enabling eVMCS, KVM verifies that every CPU has a valid hv_vp_assist_page() + * and aborts enabling the feature otherwise. CPU onlining path is also checked in + * vmx_hardware_enable(). With this, it is impossible to reach here with vp_ap == NULL + * but compilers may still complain. + */ + BUG_ON(!vp_ap); + if (current_evmcs->hv_enlightenments_control.nested_flush_hypercall) vp_ap->nested_control.features.directhypercall = 1; vp_ap->current_nested_vmcs = phys_addr; the BUG_ON() will silence compiler warning as well as become a sentinel for future code changes. -- Vitaly