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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB4A8C433EF for ; Thu, 6 Jan 2022 18:01:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242512AbiAFSBW (ORCPT ); Thu, 6 Jan 2022 13:01:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242493AbiAFSBU (ORCPT ); Thu, 6 Jan 2022 13:01:20 -0500 Received: from mail-oo1-xc36.google.com (mail-oo1-xc36.google.com [IPv6:2607:f8b0:4864:20::c36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98CCDC061245 for ; Thu, 6 Jan 2022 10:01:20 -0800 (PST) Received: by mail-oo1-xc36.google.com with SMTP id w15-20020a4a9d0f000000b002c5cfa80e84so842066ooj.5 for ; Thu, 06 Jan 2022 10:01:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=yd72dqLGjywYZ5iurvaOMSLfaNBW11uoIMA6hBdzT+w=; b=Ix3i8LiyqKdcPsMVObpv10gDeddenlWw1CHrRBunNHwQS5LCbLhaXIl8WaWSMYlGUY dC5EQeiJ/7xdSOF9JcQVBOYNnzBJuLAZWmZfWv5JGSzgXRniyow7HEWeRQu10Txkuanz 17spWXZt+ul01MTwowzgzrkQ6RRcpS5SXUYaUSWmDSj32G4GB8ZD/Zv6+sCnu5bXyPcC vDmOgnHBW3YCC9egG/Str4FgSdAhWcQkPG0mDBPiCJvRDkvoFskfhbNqm+YkRxWqi6B9 pEUj9CcSQwFyufZuyld70+9awo1aSY+frJfP8NL+fV44qXsTcnQH6Dr0payE780n1Ds9 7TcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=yd72dqLGjywYZ5iurvaOMSLfaNBW11uoIMA6hBdzT+w=; b=u7dXkcT7plo34mUGVDzU+qJHiT9drNajEsczHsjvIyezsVymUR2rQd9XY3UMWwZVFq poKJpYqiZ5YnjePFx2/55bNJTwID6pjTkNMLNzMipDIjaDZXRttQHm4aIfjr5mw/5UIq f+Q0nN9IuJbyF076sUf4iCAU//Wyo0Riw8MKJLdSInLuFwohNYunM9pQLFPXlTwROC0v YAATk+hR9CQxkVGoH6bL5Q7p4+IAwjiAFN4Ifs7mq83tG9afx79fKIh7JyiJwHm+fxNE k1EyA5kTgZOri3JFwy26cdVBzIfPCYfac5rFMIHn8tAIWLtQl233n3shG9GGtOGFgA9L 6tfQ== X-Gm-Message-State: AOAM530XGhWQXWONqlUS6B9+IjVCh8CPZHgpD0ETObcO4sNGSO09rv/6 EZZ2g/QhSsqN65fs5bhfSRQbHUQquvh7Fbr+/hj35w== X-Google-Smtp-Source: ABdhPJyruBzm9/8La0+52GslZWVrbis9fZrQAm0L5We+2hwsH+ZB9RbSZIde7gM5agGitMuOTFOyk37AI17MI50O+vg= X-Received: by 2002:a4a:ac0a:: with SMTP id p10mr37659104oon.96.1641492078304; Thu, 06 Jan 2022 10:01:18 -0800 (PST) MIME-Version: 1.0 References: <20211222133428.59977-1-likexu@tencent.com> <22776732-0698-c61b-78d9-70db7f1b907d@gmail.com> <212cea42-e445-d6f2-2730-88ccaa65b2cb@gmail.com> In-Reply-To: <212cea42-e445-d6f2-2730-88ccaa65b2cb@gmail.com> From: Jim Mattson Date: Thu, 6 Jan 2022 10:01:07 -0800 Message-ID: Subject: Re: [PATCH v2] KVM: X86: Emulate APERF/MPERF to report actual vCPU frequency To: Like Xu Cc: Paolo Bonzini , Vitaly Kuznetsov , Joerg Roedel , x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Like Xu , Dongli Cao , Li RongQing , Wanpeng Li , "Thomas Gleixner (kernel-recipes.org)" , "Borislav Petkov (kernel-recipes.org)" , Sean Christopherson Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 5, 2022 at 7:29 PM Like Xu wrote: > > On 6/1/2022 6:51 am, Jim Mattson wrote: > > On Thu, Dec 30, 2021 at 11:48 PM Like Xu wrote: > >> > >> On 31/12/2021 9:29 am, Jim Mattson wrote: > > > >>> At sched-in: > >>> 1. Save host APERF/MPERF values from the MSRs. > >>> 2. Load the "current" guest APERF/MPERF values into the MSRs (if the > >>> vCPU configuration allows for unintercepted reads). > >>> > >>> At sched-out: > >>> 1. Calculate the guest APERF/MPERF deltas for use in step 3. > >>> 2. Save the "current" guest APERF/MPERF values. > >>> 3. "Restore" the host APERF/MPERF values, but add in the deltas from step 1. > >>> > >>> Without any writes to IA32_MPERF, I would expect these MSRs to be > >>> synchronized across all logical processors, and the proposal above > >>> would break that synchronization. > > > > I am learning more about IA32_APERF and IA32_MPERF this year. :-) > > Uh, thanks for your attention. > > > > > My worry above is unfounded. These MSRs only increment in C0, so they > > are not likely to be synchronized. > > > > This also raises another issue with your original fast-path > > implementation: the host MSRs will continue to count while the guest > > is halted. However, the guest MSRs should not count while the guest is > > halted. > > > > The emulation based on guest TSC semantics w/ low precision may work it out. > TBH, I still haven't given up on the idea of a pass-through approach. I believe that pass-through can work for IA32_APERF. It can also work for IA32_MPERF on AMD hosts or when the TSC multiplier is 1 on Intel hosts. However, I also believe that it requires KVM to load the hardware MSRs with the guest's values prior to VM-entry, and to update the hardware MSRs with newly calculated host values before any other consumers on the host may read them.