Received: by 2002:ab2:7407:0:b0:1f4:b336:87c4 with SMTP id e7csp74795lqn; Thu, 11 Apr 2024 14:27:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUHkSOupctXvLbpxD0eEMHKZzcIQqhMlYrUvsdAQe0nUZZStgOjBqFF9V2aidKq9FK2G8yjKMHX5UU9Y3+WNfo8AX6Xtts0enYGt4OKkg== X-Google-Smtp-Source: AGHT+IEU9wr2+lPX1y4eXKQHLI0oWcSDNssABd+BErFw3EM/qMppNzDM78hPrcrH5+ShUjpLHCUn X-Received: by 2002:a05:6870:200b:b0:229:f31d:8c3 with SMTP id o11-20020a056870200b00b00229f31d08c3mr825948oab.50.1712870820516; Thu, 11 Apr 2024 14:27:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712870819; cv=pass; d=google.com; s=arc-20160816; b=rhi/EkV/3cmnZ48EuM1+uU5xnUG+hLXjSzGDAFPJgPJKEQ/VPtGIQFLijwOPBotJv7 LAcykzzO4iyaR/re57v0y8e+szSq9k3+R6PJn2KlaKFfOoe6yo4RFBf9bjikPHtHWAlX gml4dxxVlJWB3lC1bN/qqmjYBRIyfvPf8SPtRwHXdb+pWboMbYtjOnsLyPuk5Cwtwp0w T/K/kGzLTDBV0Y0os22CB/etRz3+vUdEiBPAcVvuHNs+5V6rnO1VJcin2mOoW4Uxz/7s KDfcb5LDeFdFdorrRIE0LTOjc37gQERljVlz3Dxt/73xPCxjxK089l9jmiM8BuRzpBHK TW1w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=WdgVI9xUvckPONDhFdSvuL71fgWc1Ta050v5yu5aktg=; fh=0/u7qZPXMuPvjU5oEDXjh1V9r5Dqlm7U+zlK6PtgbHw=; b=fAhE4CvVdLfuhCyBDfCoxXbK/S3DEXWJn8PlvyLi2MCADLmmsemD01ChXHHE3iZNmo tWocz6R87RY6XRgpKxIczGtXbIkNn9Qf2e6+JtCZJVex8EfCtXln76QAut4y6OWEJEgI dv6YGWh88PuFfhKMtXCtxXqmw7NvmLm48uBjqg6MbqS1Z5+9bo5PN4Bp4moHDWg6kc0T Hyl6ZZ15PEVaPMDUOwhL5KKq1DVIdD1d+d7GOU/k9SjpRk/LazAdYbtSKIMjI+ii55ED 9fEv/XlUH6nxsqUd+x/ajN6n6hotzUZNhVkBmwWfiRCzeLvmRgUL63cUe4i5Ca5XUKZR zEPw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=uoU5d5i4; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-141627-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141627-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id y23-20020aa78057000000b006eaf0665127si1998711pfm.306.2024.04.11.14.26.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 14:26:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-141627-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=uoU5d5i4; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-141627-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141627-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 0DD6A282536 for ; Thu, 11 Apr 2024 21:26:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4EC2C3A8FF; Thu, 11 Apr 2024 21:26:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uoU5d5i4" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 456233716F for ; Thu, 11 Apr 2024 21:26:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712870784; cv=none; b=LSItINM55COw/3Cm2maOPJhCiS1SYmtStCfJaY9G7s/prF7o0nRQC5iPjQ+UVgLhOun0wBpO7zcFOsYZuRMnZqZ/ug80oXGwiJisGRBtAv2vuIv9VdFr2IcYfT/ZvWrtrE1xZOGobdHMTkny4zRz+jKVLp6mYN/fojXcJ80nlrM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712870784; c=relaxed/simple; bh=wd5HLscLEDiELJ0ZS76nEkXx2Pqd+QH6QZ5/zGGUrh4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jRJFoHJX6GAcatorx1g1a4CiGz6C/8X0ZZHFQ6bJafO6Zd/sA/6/xkbRNFsPu5HVUD9OT2tAYsuN4pFqfaLbQAGHx/xBh0rkkq0g7m5ktXsKtgTtpiGOR0/+lZ1Lod+Ti1eZc06kn+QvIx/mUJznqdhvM3klnx0wPDzmo8sO+Jk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uoU5d5i4; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-615372710c4so3746027b3.1 for ; Thu, 11 Apr 2024 14:26:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712870782; x=1713475582; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WdgVI9xUvckPONDhFdSvuL71fgWc1Ta050v5yu5aktg=; b=uoU5d5i4dY6iLPoOw4Oao6TjLJvaGIWfMSy9DDt+HsNDtevOF1LQ4FpcavbaFTeuAq MrkVWNW/ljMXPlpsJsjydtSmwabSBoRGhAqfNFG4BV0bInSiGtKOzcxEU1z0Pn2613sU j3ebtJ8YWfxtZpZVfSwJF2HJnBbrXT8yx49k/MRTyCVKiE/BpI3KseTYclLXjl0liSd3 QwiEBuQx6qF6BZIvzNTT2Bus+8LZk53uABsJv10BX6W8es/z/eFfwWh1q20ctRjZxyTg l8XHOSCQ48ltNROIIcn6eI38W95j4tv/UbPidLWDjRi2sL702aUuiCxgkfucXu3Xxayr pECA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712870782; x=1713475582; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WdgVI9xUvckPONDhFdSvuL71fgWc1Ta050v5yu5aktg=; b=lrn8ocG/u8/qWdH5HjEEvBxoCUKin+ETDFHUoXMzC9V+Nzuee0KyUrIxUwCaJ5+HyN ks5XWSJhWyBCtJy5XVstfrkScbDrrsoQaRakZffy6eFk9DnTucly2pDm+Wt2UvpYhTQ7 Yv35ztmV43cDyVVIjwtcqzlE3EC86mk03wqaTMAhcSSKdZEfOhYwuFJz7lgd3dadUGNl ugUydqX3n6JeEoxXCw8r5zwrxOgfhNMyKtNhZp4IaGmrbUG8hVAA/y6Z6nbTFvweZPsu A1ST5SHoIu2mbUJKGv+Jd1AGeP/HblQ8st7/P+tBNS8D2x09qjRdgrDLXuhZv19jbFO/ RUXA== X-Forwarded-Encrypted: i=1; AJvYcCUYcB9JuPMFyhxJAnetJcGAUZt1hPHWs6KHP4O7PQ0t725622al6RGUqxUnbVDoJyvcmFy5l2170YJJHZdRhD+GNokwGfhSs95Fpurf X-Gm-Message-State: AOJu0YyyY69aAkXKiz87MrQAYd+HFJDXrcbFAG1+Dxmi12GnZBUbw1Hh FO8PTvlRNrlYimVV0hPHaBkEPcvNSF/rQK+FvC/OXqSzOpCdB6XwQv0VHVXYaMDdec0W09HUAHN ggw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:ca07:0:b0:615:27b6:7624 with SMTP id m7-20020a0dca07000000b0061527b67624mr155166ywd.6.1712870782385; Thu, 11 Apr 2024 14:26:22 -0700 (PDT) Date: Thu, 11 Apr 2024 14:26:20 -0700 In-Reply-To: <20240126085444.324918-24-xiong.y.zhang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240126085444.324918-1-xiong.y.zhang@linux.intel.com> <20240126085444.324918-24-xiong.y.zhang@linux.intel.com> Message-ID: Subject: Re: [RFC PATCH 23/41] KVM: x86/pmu: Implement the save/restore of PMU state for Intel CPU From: Sean Christopherson To: Xiong Zhang Cc: pbonzini@redhat.com, peterz@infradead.org, mizhang@google.com, kan.liang@intel.com, zhenyuw@linux.intel.com, dapeng1.mi@linux.intel.com, jmattson@google.com, kvm@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, zhiyuan.lv@intel.com, eranian@google.com, irogers@google.com, samantha.alt@intel.com, like.xu.linux@gmail.com, chao.gao@intel.com Content-Type: text/plain; charset="us-ascii" On Fri, Jan 26, 2024, Xiong Zhang wrote: > static void intel_save_pmu_context(struct kvm_vcpu *vcpu) > { > + struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); > + struct kvm_pmc *pmc; > + u32 i; > + > + if (pmu->version != 2) { > + pr_warn("only PerfMon v2 is supported for passthrough PMU"); > + return; > + } > + > + /* Global ctrl register is already saved at VM-exit. */ > + rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, pmu->global_status); > + /* Clear hardware MSR_CORE_PERF_GLOBAL_STATUS MSR, if non-zero. */ > + if (pmu->global_status) > + wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, pmu->global_status); > + > + for (i = 0; i < pmu->nr_arch_gp_counters; i++) { > + pmc = &pmu->gp_counters[i]; > + rdpmcl(i, pmc->counter); > + rdmsrl(i + MSR_ARCH_PERFMON_EVENTSEL0, pmc->eventsel); > + /* > + * Clear hardware PERFMON_EVENTSELx and its counter to avoid > + * leakage and also avoid this guest GP counter get accidentally > + * enabled during host running when host enable global ctrl. > + */ > + if (pmc->eventsel) > + wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, 0); > + if (pmc->counter) > + wrmsrl(MSR_IA32_PMC0 + i, 0); > + } > + > + rdmsrl(MSR_CORE_PERF_FIXED_CTR_CTRL, pmu->fixed_ctr_ctrl); > + /* > + * Clear hardware FIXED_CTR_CTRL MSR to avoid information leakage and > + * also avoid these guest fixed counters get accidentially enabled > + * during host running when host enable global ctrl. > + */ > + if (pmu->fixed_ctr_ctrl) > + wrmsrl(MSR_CORE_PERF_FIXED_CTR_CTRL, 0); > + for (i = 0; i < pmu->nr_arch_fixed_counters; i++) { > + pmc = &pmu->fixed_counters[i]; > + rdpmcl(INTEL_PMC_FIXED_RDPMC_BASE | i, pmc->counter); > + if (pmc->counter) > + wrmsrl(MSR_CORE_PERF_FIXED_CTR0 + i, 0); > + } For the next RFC, please make that it includes AMD support. Mostly because I'm pretty all of this code can be in common x86. The fixed counters are ugly, but pmu->nr_arch_fixed_counters is guaranteed to '0' on AMD, so it's _just_ ugly, i.e. not functionally problematic.