Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp654447ybt; Wed, 8 Jul 2020 08:32:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwLGqDFgoAMAGgyLuZLKI0Qgpewg543OnVkSiziEwMHq26bR47uIaPC/YqBLSD6/MSgHvFX X-Received: by 2002:a17:906:eb5a:: with SMTP id mc26mr49694873ejb.42.1594222371565; Wed, 08 Jul 2020 08:32:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594222371; cv=none; d=google.com; s=arc-20160816; b=pbX3d7CppQKF4s+7gGk2kw2QUzzO7a4eBOyCXC9k+npnNjqs9ajsZ8tXOyEuXBAc/T BQrv7+0HYgsqdBWnUx0N091gTK0udagE9FvYpFN9nrJnSjtmB2slRVxIBTGCY2M6mkjc tzaO1IAqYykKW1roGCfSq1hHQKhKfmX4/W1WV8Mj0bzI2vWXyT0QLFFtG8YVjxaY6B9L lTdVe8KNhmzMSx95tzj5i3vdGatHnK6yw1BQ3mg7NiBvBDXnD2kjRsJ8KVaqsh6Na47C F7DbaKbGfyR4TwJ5vTENUivclMYfylEaxIIiV2Og/RySpynZ6PDTDa0RkrcnBcpA8vzE L9Dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=59uaosHXgT6qmldDLKNeyh6mr1S59liDZ+jRzuKa0uE=; b=pJXna9tZLgEQEJmInLozGflZV9dP23DSIhboeaLHp/Cat1JO3arAbB6WqN6F1TkhyV HdZZODzgx+bRQoQKhfo5TBREhTb+4Pj5Iqe+b1chboqp8F3j+GJS3/4XhMQXl+RNXNGK p2Jhp6bwgDF4HxbG2SXkcLseTAHl16RblrqOTT7FDmqwABka9cHbHAArammN5w5AVIhZ QA5+MrW0r9OGBZWuRFjDlpit0rmxI3k3tyj65uMRgbEEEKKp2/nzrahLRHcW6jVGk9Yc 4V1GAItA/IqzOiDodLyVds+jSLzKfXvZ7TJ5bPO3no8PTFO9FvoR+wYOXB0TLUeIZQNV APlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PtHjIh2D; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e13si71477ejc.215.2020.07.08.08.32.28; Wed, 08 Jul 2020 08:32:51 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PtHjIh2D; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730183AbgGHPbW (ORCPT + 99 others); Wed, 8 Jul 2020 11:31:22 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:54733 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729022AbgGHPbV (ORCPT ); Wed, 8 Jul 2020 11:31:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594222280; 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=59uaosHXgT6qmldDLKNeyh6mr1S59liDZ+jRzuKa0uE=; b=PtHjIh2D3jFSiXtSTx40jUKhzbo3G/+QQ2b0XUSjprKiUZMDQyWShCiU1pNtIxIg98Dcd5 ZAEc0UQ7Pb9sGWrFoXUWXtAU5hewaP9Q16h9tVseftrDMJTp8taSI2tp6TkE2RiBCDXdrA 4bVuWvHKaWCp3cq4WFxtg7jbO2Yi93U= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-416-QOc7kpD8O4Si-HaCi82yOA-1; Wed, 08 Jul 2020 11:31:18 -0400 X-MC-Unique: QOc7kpD8O4Si-HaCi82yOA-1 Received: by mail-wm1-f71.google.com with SMTP id c81so2109997wmd.2 for ; Wed, 08 Jul 2020 08:31:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=59uaosHXgT6qmldDLKNeyh6mr1S59liDZ+jRzuKa0uE=; b=jJ5r6/ToUEfe5YMOUDJMJ8ZH1zjmVAKEFYfTkeCLwX8LxuYTnbAUk7H0qmOoishAK5 RIkMOzoFldFwZH4ua8ybZLbVveB0Fi4311660GAJRIZywnYZ3O+FNMgvMWCLIr8ug8dh oBKt/YJd3zJ9w3DBRJLVbpDdteQauw1RTcvgheWmc8I4BMN6H2Slk3fRZpTKlnof6qJL wlFAQq4wK8F988ICQnhpRInanteGnlwk3duE576RbNsNcVwlBvqeL7BBWbQIbrhYkJs3 EQh9W/OobDLgbmxi6oU8EmYsbV1UXhFtTV+jy5QYxLOzFptWj2tVoQZItV6A9gISSJxa Vsew== X-Gm-Message-State: AOAM531EMRxCHNL7ejze95pcthv/CK7ZttNM3qlfxNW1jd0YEYQgPt0v I6Uf3qUHlrA8X+YeSE3zn2RpKKUlfblGyvhP8Y0ZpmVSk2CadnYVh81SdYGyNZGxmBZ2zp+5lq9 huqwcBtKz5RFZg2xFr3skIaet X-Received: by 2002:a7b:c5c4:: with SMTP id n4mr9638601wmk.67.1594222276521; Wed, 08 Jul 2020 08:31:16 -0700 (PDT) X-Received: by 2002:a7b:c5c4:: with SMTP id n4mr9638575wmk.67.1594222276235; Wed, 08 Jul 2020 08:31:16 -0700 (PDT) Received: from ?IPv6:2001:b07:6468:f312:9541:9439:cb0f:89c? ([2001:b07:6468:f312:9541:9439:cb0f:89c]) by smtp.gmail.com with ESMTPSA id j15sm426678wrx.69.2020.07.08.08.31.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 08 Jul 2020 08:31:15 -0700 (PDT) Subject: Re: [PATCH] KVM/x86: pmu: Fix #GP condition check for RDPMC emulation To: Sean Christopherson , Like Xu Cc: kvm@vger.kernel.org, Wanpeng Li , Jim Mattson , Vitaly Kuznetsov , Joerg Roedel , linux-kernel@vger.kernel.org References: <20200708074409.39028-1-like.xu@linux.intel.com> <20200708151824.GA22737@linux.intel.com> From: Paolo Bonzini Message-ID: Date: Wed, 8 Jul 2020 17:31:14 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: <20200708151824.GA22737@linux.intel.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 08/07/20 17:18, Sean Christopherson wrote: > On Wed, Jul 08, 2020 at 03:44:09PM +0800, Like Xu wrote: >> in guest protected mode, if the current privilege level >> is not 0 and the pce flag in the cr4 register is cleared, >> we will inject a #gp for rdpmc usage. > > Wrapping at ~58 characters is a bit aggressive. checkpatch enforces 75 > chars, something near that would be prefereable. > >> Signed-off-by: Like Xu >> --- >> arch/x86/kvm/pmu.c | 5 +++++ >> 1 file changed, 5 insertions(+) >> >> diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c >> index b86346903f2e..d080d475c808 100644 >> --- a/arch/x86/kvm/pmu.c >> +++ b/arch/x86/kvm/pmu.c >> @@ -372,6 +372,11 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) >> if (!pmc) >> return 1; >> >> + if ((kvm_x86_ops.get_cpl(vcpu) != 0) && >> + !(kvm_read_cr4(vcpu) & X86_CR4_PCE) && >> + (kvm_read_cr4(vcpu) & X86_CR0_PE)) > > This reads CR4 but checks CR0.PE. > > And maybe put the X86_CR4_PCE check first so that it's the focus of the > statement? I'll squash this to fix it (I'm OOO next week and would like to get kvm/queue sorted out these few days that I've left). diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index d080d475c808..67741d2a0308 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -372,9 +372,9 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) if (!pmc) return 1; - if ((kvm_x86_ops.get_cpl(vcpu) != 0) && - !(kvm_read_cr4(vcpu) & X86_CR4_PCE) && - (kvm_read_cr4(vcpu) & X86_CR0_PE)) + if (!(kvm_read_cr4(vcpu) & X86_CR4_PCE) && + (kvm_x86_ops.get_cpl(vcpu) != 0) && + (kvm_read_cr0(vcpu) & X86_CR0_PE)) return 1; *data = pmc_read_counter(pmc) & mask; The order follows the SDM. I'm tempted to remove the CR0 check altogether, since non-protected-mode always runs at CPL0 AFAIK, but let's keep it close to what the manual says. Paolo