Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp965289pxv; Fri, 9 Jul 2021 13:37:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy8EHYn+vvy1hN3q3fOrJsoBFgTn7vPdZofsmWTBl7YtKgogMypJYWCkskSwAXZEoEL0PAS X-Received: by 2002:a17:906:76cf:: with SMTP id q15mr15353430ejn.69.1625863057114; Fri, 09 Jul 2021 13:37:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625863057; cv=none; d=google.com; s=arc-20160816; b=g7O4yg0PcOgUhx3AOEdYIoN0+Z9gInZImRjuiu/QOA2AWS2pifSNPP4aaFwsrCM7No AqAalV8Hi0rSO00ngOEMWXFm7GuKrZftv9+L0XAHA6n7CmRTc/AlVmyZtuUqUrWAIuoM NDRU2P2NaOquBI8R8tVZtRtV90nMGy+UqUTx9Cs7XZ3okw/J7cui4kNC9cBswkGtwaXV Udkqa7PxXMht7wKnlk8v8fsQo12xOL9t8mgZ0EGLh4wN9UEGHG7RPOErrEOAXKJeZfgt PLGB4/zBgenIXHbEJtxSkbFOaqvqMuNhq91dMxgdZuz3Vi68qnfVwxNAsMYgsWsUMmfa nb7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=kHHhyfCEkW/Vi1HiAXRAzBDCaqb5jnujVXbqXt6B/VI=; b=VeuORqra0M88I9f9hGqGlzS9+Y/xBixHghJq8FvN2OZnCfc2esIDT4df8RzWSOTwm3 xCF3PVE3hO+brdhzX6+W12I/hk1sJPz+lmwG5SylR8WnLH/q/KVYrKE6OtCgIp42XlqO BN5SJRSo9TQcjezmlpeeJAVtAF0CTnCYbQSkcNaT5AeJo4XAo3F3xEnYM+tvHR2VMMAk FB/VLwDj61V0SZMoernTGnmSiwIQT6OYnU8sUKA+4BNOyOCEB0equl/ZolBLeksGnUC6 8jHmpiB2wks3+g4jDhqcqXtJPJO4p+dzmLOFTZr45kyHAtR0cFh0etOi5nYJEiIcmQo1 TPlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=QBAo5MrA; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t4si4618342eju.418.2021.07.09.13.37.09; Fri, 09 Jul 2021 13:37:37 -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=@google.com header.s=20161025 header.b=QBAo5MrA; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229931AbhGIUic (ORCPT + 99 others); Fri, 9 Jul 2021 16:38:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229506AbhGIUic (ORCPT ); Fri, 9 Jul 2021 16:38:32 -0400 Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3CEFC0613DD for ; Fri, 9 Jul 2021 13:35:46 -0700 (PDT) Received: by mail-oi1-x235.google.com with SMTP id u66so7920502oif.13 for ; Fri, 09 Jul 2021 13:35:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kHHhyfCEkW/Vi1HiAXRAzBDCaqb5jnujVXbqXt6B/VI=; b=QBAo5MrAzeb+QjdnVM+fmnW1p7a8EJaQumd0VGQeb8KCkDetUWM1EvEaTgEfhpIZVQ PuD4ss+l6YpCGpcPMojzvU60kJB5+bEHC4suvelLBwX2x40IE0ABrAcDmCNMFy16hcYQ knCM5AWV0Op7FTMN9qaT97LNFHcTnKOLrFJpqL26hENd0UDXTL19Y1jbSho/Cq38SpPZ b4O92u7O0Ee4iqGnUfoeJkwYdM3HZlFIVyVQVlROigLaYcalSa1Ymze3DHdQo3tVvw9W cvgU7RWXFXl5dPtv4SFB94N5RaSx7GRLGsbScr4Z95gzvQr52CrixacaGDujxJerv1Uq xPCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kHHhyfCEkW/Vi1HiAXRAzBDCaqb5jnujVXbqXt6B/VI=; b=WbtPpuGYdOPHtNAkqEjK/7tlqXe4BAE6mjkFxc2yAUUkHAudPhabu5lZ6QE8NzVaeQ ry90wTP9iKRnkn6EAufxG++3etNpVCia49kIUUKYRABzTtQMuNF5xB4y1mrpjeRShePb 6fkWk2sOwzZwvTkQK9S2ztYxQJBEbA5wMXpuO/vyOdlAF+7ghXEVUQYYh8OC/xWT5aR4 FtfJbFQ1nz9PYAWPIMfypAIQ8qJF9RxurBn1c5SWiskm6whj+aOc1+UTCIJAyeFP08vS XLU89BkDnJ/wKHRWq0KDg5X5htRI+ahmshlXcBX+zUeTDlRB6N5H0q+HD0wSGJYIClcw DsLQ== X-Gm-Message-State: AOAM533cB6EAyXuvKaPD6qsoLBwWoS+Q196CiArllDL7NLf2a2lSzoQh VHYgkqhc7z1E3TYzox34VlEdf0deelkG1BdP/dJdVg== X-Received: by 2002:a05:6808:355:: with SMTP id j21mr618879oie.13.1625862945597; Fri, 09 Jul 2021 13:35:45 -0700 (PDT) MIME-Version: 1.0 References: <1625825111-6604-1-git-send-email-weijiang.yang@intel.com> <1625825111-6604-5-git-send-email-weijiang.yang@intel.com> In-Reply-To: <1625825111-6604-5-git-send-email-weijiang.yang@intel.com> From: Jim Mattson Date: Fri, 9 Jul 2021 13:35:34 -0700 Message-ID: Subject: Re: [PATCH v5 04/13] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_DEPTH for guest Arch LBR To: Yang Weijiang Cc: pbonzini@redhat.com, seanjc@google.com, vkuznets@redhat.com, wei.w.wang@intel.com, like.xu.linux@gmail.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Like Xu Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 9, 2021 at 2:51 AM Yang Weijiang wrote: > > From: Like Xu > > The number of Arch LBR entries available is determined by the value > in host MSR_ARCH_LBR_DEPTH.DEPTH. The supported LBR depth values are > enumerated in CPUID.(EAX=01CH, ECX=0):EAX[7:0]. For each bit "n" set > in this field, the MSR_ARCH_LBR_DEPTH.DEPTH value of "8*(n+1)" is > supported. > > On a guest write to MSR_ARCH_LBR_DEPTH, all LBR entries are reset to 0. > KVM emulates the reset behavior by introducing lbr_desc->arch_lbr_reset. > KVM writes guest requested value to the native ARCH_LBR_DEPTH MSR > (this is safe because the two values will be the same) when the Arch LBR > records MSRs are pass-through to the guest. > > Signed-off-by: Like Xu > Signed-off-by: Yang Weijiang > --- > @@ -393,6 +417,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) > { > struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); > struct kvm_pmc *pmc; > + struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); > u32 msr = msr_info->index; > u64 data = msr_info->data; > > @@ -427,6 +452,12 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) > return 0; > } > break; > + case MSR_ARCH_LBR_DEPTH: > + if (!arch_lbr_depth_is_valid(vcpu, data)) > + return 1; Does this imply that, when restoring a vCPU, KVM_SET_CPUID2 must be called before KVM_SET_MSRS, so that arch_lbr_depth_is_valid() knows what to do? Is this documented anywhere? > + lbr_desc->records.nr = data; > + lbr_desc->arch_lbr_reset = true; Doesn't this make it impossible to restore vCPU state, since the LBRs will be reset on the next VM-entry? At the very least, you probably shouldn't set arch_lbr_reset when the MSR write is host-initiated. However, there is another problem: arch_lbr_reset isn't serialized anywhere. If you fix the host-initiated issue, then you still have a problem if the last guest instruction prior to suspending the vCPU was a write to IA32_LBR_DEPTH. If there is no subsequent VM-entry prior to saving the vCPU state, then the LBRs will be saved/restored as part of the guest XSAVE state, and they will not get cleared on resuming the vCPU. > + return 0; > default: > if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) || > (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) { > @@ -566,6 +597,7 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu) > lbr_desc->records.nr = 0; > lbr_desc->event = NULL; > lbr_desc->msr_passthrough = false; > + lbr_desc->arch_lbr_reset = false; I'm not sure this is entirely correct. If the last guest instruction prior to a warm reset was a write to IA32_LBR_DEPTH, then the LBRs should be cleared (and arch_lbr_reset will be true). However, if you clear that flag here, the LBRs will never get cleared. > } >