Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2839592pxb; Tue, 24 Aug 2021 08:46:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz1YdMNXGI9xLv+RwxIsK/8+A/sWMY7iIHTTKWmvyR0IV6jb9PQWJYcHlPk+AyuEkL1wpxf X-Received: by 2002:a05:6402:cb7:: with SMTP id cn23mr42743174edb.82.1629819979778; Tue, 24 Aug 2021 08:46:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629819979; cv=none; d=google.com; s=arc-20160816; b=NDk16/NeSUsFDzt36wVo4dNOAzTXxLQDod2cm1p5EzU5HVdbzahzXdEgYEKAlZg95j CM5YR7Fvii3hJIbKMQKJyC0B6kGESPG3p3p9tcobfOOSsrh68WwKsp6lBdI1qEuhnsKU PaZiQyyf2spKEsv3VhU1HHLhXsZPeDuc4cvSjfN8l2vP9z0aHwY3uda6iWwc2Yjg2XpD wCDqZX86Z3idGU0SS5vVKlkZSS6b6QZyrrZKkedcN985xK40H3HyDDvBEgJvK96bWlO+ HzlDyWQZJX/tnGlxQHos1+6DA5BDrZb8SWf1nks1alb3+yom919wr/y7uwsAlq0/Wyrf 099g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=OzJlsqJtWqJiQzdLSZy4bbVoZpj5rGuCz3i7BcyBk9c=; b=Xd4ndKv7rs6mWlM0Y12lzf5CNhu+JXIfLn7QOwCb/Ey13TVzg67EQQoKjLkavDiLGL Lmn9HZlz19fNTD/OdGQVR0YY5OZ+JV5dKRpYihrZj2iAiUp0aRLZOZ+X5ir/nOR5IbdQ jnmKDLR8KC1EYC0aXs/e1ktTYbQTtKBPbmZNl6BryZPf6KozhZl0Fvjsp0OIk+TB+tW9 n5qSeQvo6ytR7uTx0qjp3q9fHq9VPBYzpEbpXmLzmh35fQUbji5d0Ge8mD7UvFk+YpBw nTK9dxyFjTPssp9g1Uwn9KCROQHb/IHKakOJWNyF9qhmzyRKxMvp7VJ1JV0ItfZVZxpL 6rPg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n25si1278410ejl.672.2021.08.24.08.45.54; Tue, 24 Aug 2021 08:46:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238530AbhHXPoA (ORCPT + 99 others); Tue, 24 Aug 2021 11:44:00 -0400 Received: from mga14.intel.com ([192.55.52.115]:63076 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238432AbhHXPnt (ORCPT ); Tue, 24 Aug 2021 11:43:49 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10086"; a="217061467" X-IronPort-AV: E=Sophos;i="5.84,347,1620716400"; d="scan'208";a="217061467" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Aug 2021 08:42:48 -0700 X-IronPort-AV: E=Sophos;i="5.84,347,1620716400"; d="scan'208";a="526670292" Received: from xiaoyaol-mobl.ccr.corp.intel.com (HELO [10.249.172.211]) ([10.249.172.211]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Aug 2021 08:42:46 -0700 Subject: Re: [PATCH 2/5] KVM: VMX: Use cached vmx->pt_desc.addr_range To: Sean Christopherson Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org References: <20210824110743.531127-1-xiaoyao.li@intel.com> <20210824110743.531127-3-xiaoyao.li@intel.com> From: Xiaoyao Li Message-ID: Date: Tue, 24 Aug 2021 23:42:44 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 8/24/2021 11:24 PM, Sean Christopherson wrote: > On Tue, Aug 24, 2021, Xiaoyao Li wrote: >> The number of guest's valid PT ADDR MSRs is cached in > > Can you do s/cached/precomputed in the shortlog and changelog? Explanation below. OK. >> vmx->pt_desc.addr_range. Use it instead of calculating it again. >> >> Signed-off-by: Xiaoyao Li >> --- >> arch/x86/kvm/vmx/vmx.c | 3 +-- >> 1 file changed, 1 insertion(+), 2 deletions(-) >> >> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c >> index e0a9460e4dab..7ed96c460661 100644 >> --- a/arch/x86/kvm/vmx/vmx.c >> +++ b/arch/x86/kvm/vmx/vmx.c >> @@ -2202,8 +2202,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) >> if (!pt_can_write_msr(vmx)) >> return 1; >> index = msr_info->index - MSR_IA32_RTIT_ADDR0_A; >> - if (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps, >> - PT_CAP_num_address_ranges)) >> + if (index >= 2 * vmx->pt_desc.addr_range) > > Ugh, "validate" is a lie, a better name would be intel_pt_get_cap() or so. There > is no validation, the helper is simply extracting the requested cap from the > passed in array of capabilities. > > That matters in this case because the number of address ranges exposed to the > guest is not bounded by the number of address ranges present in hardware, i.e. > it's not "validated". And that matters because KVM uses vmx->pt_desc.addr_range > to pass through the ADDRn_{A,B} MSRs when tracing enabled. In other words, > userspace can expose MSRs to the guest that do not exist. That's why I provided patch 5. > The bug shouldn't be a security issue, so long as Intel CPUs are bug free and > aren't doing silly things with MSR indexes. The number of possible address ranges > is encoded in three bits, thus the theoretical max is 8 ranges. So userspace can't > get access to arbitrary MSRs, just ADDR0_A -> ADDR7_B. > > And since KVM would be modifying the "validated" value, it's more than just a > cache, hence the request to use "precomputed". > > Finally, vmx_get_msr() should use the precomputed value as well. Argh, I missed it. > P.S. If you want to introduce a bit of churn, s/addr_range/nr_addr_ranges would > be a welcome change as well. In a separate patch? >> return 1; >> if (is_noncanonical_address(data, vcpu)) >> return 1; >> -- >> 2.27.0 >>