Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1378746imm; Thu, 6 Sep 2018 22:21:44 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbHDAkxq0wq8XXnECCli3vD0PKJ8TKtPUug/jTQ5C+3cVd7KSRXy9ka3NmFd4yN8TL+VG6Q X-Received: by 2002:a17:902:988a:: with SMTP id s10-v6mr6047421plp.200.1536297704157; Thu, 06 Sep 2018 22:21:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536297704; cv=none; d=google.com; s=arc-20160816; b=YOD03j2gZ2Pn49rEZ9iCRnCekz16p+A5wDtOI3J+t7tR0Ja/HQwE9p1hXhB+vjGlVj ozdwt03oJ77vO80cffA9MHY04w9LGrHAiNxUVzs7bgKnuHVDU7T6PxCCYs/YdjwvLu1R 8YUN7gQFi/0wS1DbGyI/aI0zxPFug9t2yClvTYg2Lmd1PR/xMBZVispIU7VSaV/PGbww ZuqiDb4L1vkEiwjGFm4Spf1uE5T3A6bD6O2k+qwngdIDjqzWAY/4zDtMGYtmOdGtSXCl LpPo2O+tFfvoqJmpWhQJht6dhUMQSXEK80fqECfa0MVZ1ex4udDFbmpc+Ut/7iQbKEw+ yTJQ== 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:in-reply-to :references:subject:cc:to:mime-version:user-agent:from:date :message-id; bh=TUpKHIMUC9e8UwcUj5+/wC4DT5B7ey/UXNZrUvW1Dyk=; b=juJZTcLVGGsCVd3e7sUOQ9EHV1Ig6+DiZrbB711kc4+gHBSQZ46IImO5Ooq4ZFtpPM r632JlMiQ6AQvauJnR2sYqPJLZL17KirmmthaoZyL4Y4BYuDo20fjeLLvft9EW0RSyzf 2CNS7SarTGZoItatCVemi+YXZAXUCj30AmmWIp1MygmRMNR0S4N+IxkjjO3/BMvecu3X HThltBoqB8XGOEgazKA/RG31XVERpm2Ag/gAjeVDufmFds4fDsdlQBOXxHEtABFrcE3u d32ivHhlLM62tN+h1wwPxd0szzsQV91lP2k/NUg/WBjRIWNLTLnm1NuKd5uK5ewZ5Nth uEPg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id s125-v6si7450679pfb.335.2018.09.06.22.21.28; Thu, 06 Sep 2018 22:21:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1727282AbeIGJ7g (ORCPT + 99 others); Fri, 7 Sep 2018 05:59:36 -0400 Received: from mga05.intel.com ([192.55.52.43]:53790 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725831AbeIGJ7f (ORCPT ); Fri, 7 Sep 2018 05:59:35 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Sep 2018 22:20:23 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,340,1531810800"; d="scan'208";a="89711020" Received: from unknown (HELO [10.239.13.3]) ([10.239.13.3]) by orsmga002.jf.intel.com with ESMTP; 06 Sep 2018 22:19:55 -0700 Message-ID: <5B920B96.6080803@intel.com> Date: Fri, 07 Sep 2018 13:24:38 +0800 From: Wei Wang User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: Andi Kleen CC: "linux-kernel@vger.kernel.org" , "kvm@vger.kernel.org" , "pbonzini@redhat.com" , "Liang, Kan" , "peterz@infradead.org" , "mingo@redhat.com" , "rkrcmar@redhat.com" , "Xu, Like" Subject: Re: [PATCH v2 6/8] perf/x86/intel/lbr: guest requesting KVM for lbr stack save/restore References: <1536233456-12173-1-git-send-email-wei.w.wang@intel.com> <1536233456-12173-7-git-send-email-wei.w.wang@intel.com> <20180907032707.GN27886@tassilo.jf.intel.com> In-Reply-To: <20180907032707.GN27886@tassilo.jf.intel.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 09/07/2018 11:27 AM, Andi Kleen wrote: > On Thu, Sep 06, 2018 at 07:30:54PM +0800, Wei Wang wrote: >> This patch adds an interface to enable a guest to request KVM to save >> and restore the lbr stack on vCPU context switching. >> >> KVM couldn't capture the info about whether the guest is actively using >> the lbr feature via the lbr enable bit in the debugctl MSR, because that >> control bit is frequently enabled and disabled by the guest, and in some >> csaes, it is disabled even when the guest is actively using the lbr >> feature. For example, perf_pmu_sched_task in the guest disables the bit >> before reading out the lbr stack. In this case, the bit is disabled though >> the guest is still using the lbr feature. >> >> So, a KVM-specific MSR, MSR_KVM_PV_LBR_CTRL, is used by the guest at a >> proper place to tell KVM if the LBR is actively in use or not. Basically, >> the lbr user callstack mode needs the lbr stack to be saved/restored on a >> context switching, so we set the ACTIVE bit of MSR_KVM_PV_LBR_CTRL only >> when the user callstack mode is used. The KVM hypervisor will add the lbr >> stack save/restore support on vCPU switching after the ACTIVE bit is set. > PV is difficult because it requires changing all the users. It needs changes of the guest driver, but remains transparent to guest user applications (e.g. the perf tool). Btw, we tested it, and it works in guest as good as on the native linux. This was thought of as the hardest part of this work. Let me just clarify it a little bit: The fundamental function we want to achieve is #1 when the vCPU is actively using the LBR feature, save/restore the lbr stack when the vCPU is scheduled out/in; #2 when the vCPU is NOT actively using the LBR feature, DON'T save/restore the lbr stack when the vCPU is scheduled out/in; The key problem we need to solve is: how does the host know if the guest is actively using the lbr feature or not? > > Maybe a better approach would be a lazy restore of the LBRs: > > Don't restore the LBRs on context switch, but set the LBR MSRs to intercept. > Then on the first access restore the LBRs and allow direct access to the > MSRs again. > > Also when the LBRs haven't been set to direct access the state doesn't > need to be saved. This could achieve the above #1, but how would it solve #2 above? That is, after the guest uses the lbr feature for a while, the lbr stack has been passed through, then the guest doesn't use lbr any more, but the vCPU will still save/restore on switching? (Host cannot know that the guest is not using lbr by the debugctl[0], the commit log above has some explanations about this) Best, Wei