From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4BEF6C433EF for ; Thu, 14 Oct 2021 16:54:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 326E560F59 for ; Thu, 14 Oct 2021 16:54:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232753AbhJNQ4o (ORCPT ); Thu, 14 Oct 2021 12:56:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232641AbhJNQ4k (ORCPT ); Thu, 14 Oct 2021 12:56:40 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C340C061753 for ; Thu, 14 Oct 2021 09:54:35 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id x130so5977856pfd.6 for ; Thu, 14 Oct 2021 09:54:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=DxjJWbKincMlvSXC/FY3Ala2ok6WlFlcTqLiBRO6M/w=; b=TmOfNDnpZ594weM/2UNFzn+MY2mLVQtNlWPXx1nvRjNLRuk3e4qJSZ+hf870+QB2jR agy34dupqT+XlQu0aOc5FFWJkU7NrAo35nTYXfcP1hVKEEJPOCK6m9qlcrztza4Rdx9V gTLuI0kzy0ZIf+ljd82tD/Qq0iMg4ZFRRqQzVCdh5zG/QjM/PkK/tP64E2fGXdFNVRk+ QPtSuqkBVFKKaUwSqa9cVDsu6SzCzg7Rg6SaCHVzSB+CPCVTtpPilbwGB4rBpGwdc3Ib qKHpgWBPd6ro+VOtbcgwcyhyeTWuKZV0bpMxJ0Q/ixHV2LGaM2/3erKqDn/DF70aoeoB EWmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=DxjJWbKincMlvSXC/FY3Ala2ok6WlFlcTqLiBRO6M/w=; b=f2q1gIm/Q+qQHtHRgjJNwrwv9gw6fyrrR3YvjLh6Riu92Htnxd4ttwmsl7wauS/6+l TERPY2C9K/ULrxmx8xvmF28OeQmSyIOyUGjmLTGMfHeTRlgVSS5s1VsOYgLRBxB4VIgu zEph8FR+eDq/2m5zMr0/Glo15wiJU7P7lZi2UQLvhqd3qSRsJlp6o1Mi3ptCxoG42oma +O2KvypgrUzaoHuCOh0iz2ZHo/pURwLW1S+3JjdAt0HnEUW0XULwEQUImkgLdKORRVRu dMEvq45qN2c1s8rfsgFtPRQFQIFaC5S+MycOR3t4iZG12qVVMPD2rfNygK52sL2gdQsA 2bkg== X-Gm-Message-State: AOAM532N1iUFwYZTS6gTp9XjX6C48zdqLb9SXA6PraWrAhLUDLAtan3G UlFP2M6+Sbyjt1aGvnX7OYV2fA== X-Google-Smtp-Source: ABdhPJxMtGllP8NiZbEk0on6Wf4jCwBWbZKO6nNiOAtImB19UfktQycjNgLkgeaQaEGg3IeQfOfgiA== X-Received: by 2002:a63:d205:: with SMTP id a5mr5010873pgg.30.1634230474404; Thu, 14 Oct 2021 09:54:34 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id m186sm3131409pfb.165.2021.10.14.09.54.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 09:54:33 -0700 (PDT) Date: Thu, 14 Oct 2021 16:54:30 +0000 From: Sean Christopherson To: Jim Mattson Cc: Vipin Sharma , pbonzini@redhat.com, dmatlack@google.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] KVM: VMX: Add a wrapper for reading INVPCID/INVEPT/INVVPID type Message-ID: References: <20211011194615.2955791-1-vipinsh@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 11, 2021, Jim Mattson wrote: > On Mon, Oct 11, 2021 at 1:23 PM Sean Christopherson wrote: > > > > On Mon, Oct 11, 2021, Vipin Sharma wrote: > > > - if (type > 3) { > > > + if (type > INVPCID_TYPE_MAX) { > > > > Hrm, I don't love this because it's not auto-updating in the unlikely chance that > > a new type is added. I definitely don't like open coding '3' either. What about > > going with a verbose option of > > > > if (type != INVPCID_TYPE_INDIV_ADDR && > > type != INVPCID_TYPE_SINGLE_CTXT && > > type != INVPCID_TYPE_ALL_INCL_GLOBAL && > > type != INVPCID_TYPE_ALL_NON_GLOBAL) { > > kvm_inject_gp(vcpu, 0); > > return 1; > > } > > Better, perhaps, to introduce a new function, valid_invpcid_type(), > and squirrel away the ugliness there? Oh, yeah, definitely. I missed that SVM's invpcid_interception() has the same open-coded check. Alternatively, could we handle the invalid type in the main switch statement? I don't see anything in the SDM or APM that architecturally _requires_ the type be checked before reading the INVPCID descriptor. Hardware may operate that way, but that's uArch specific behavior unless there's explicit documentation. diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 89077160d463..c8aade2e2a20 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3119,11 +3119,6 @@ static int invpcid_interception(struct kvm_vcpu *vcpu) type = svm->vmcb->control.exit_info_2; gva = svm->vmcb->control.exit_info_1; - if (type > 3) { - kvm_inject_gp(vcpu, 0); - return 1; - } - return kvm_handle_invpcid(vcpu, type, gva); } diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 1c8b2b6e7ed9..ad2e794d4cb2 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5504,11 +5504,6 @@ static int handle_invpcid(struct kvm_vcpu *vcpu) vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf); - if (type > 3) { - kvm_inject_gp(vcpu, 0); - return 1; - } - /* According to the Intel instruction reference, the memory operand * is read even if it isn't needed (e.g., for type==all) */ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d9273f536f9d..a3657db6daf9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12382,7 +12382,8 @@ int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva) return kvm_skip_emulated_instruction(vcpu); default: - BUG(); /* We have already checked above that type <= 3 */ + kvm_inject_gp(vcpu, 0); + return 1; } } EXPORT_SYMBOL_GPL(kvm_handle_invpcid);