Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1548054rda; Mon, 23 Oct 2023 16:41:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH2Q8d9FRRTA386ltFFqIV4G+94PJiHvwKqx+2tUGyDzGxn8X4Z1stEr5Uy03WuQu1P0Mn/ X-Received: by 2002:a05:6a20:4297:b0:17b:8016:6f79 with SMTP id o23-20020a056a20429700b0017b80166f79mr1179858pzj.3.1698104500260; Mon, 23 Oct 2023 16:41:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698104500; cv=none; d=google.com; s=arc-20160816; b=oGqKmyxgh3ncIaWl5FAMZsUGprLCLLDkAhV8fYSiTfbXjOCLfjiAG0tQTwIieAWGdZ AJa4H6ijkTfMsVW8UYj/+gp2/U99xrYyLTTcYa222KSjGmnKSFCzRbS8Usg3vHRk6JO+ oDo+kStvSPp40dbYmxCYDSfu8j26lQzo1eBpFNy73/fVYTwqSEU2viUO8g51zLqUSLa9 pPQB2nVPiFD/Ed7OrNJdEkJ/Zih/91sUeT9YRD+itGpQ3C9Gt5vvm7A7O3r8Un1zHHwP 9i4MGNWUSAUX9MOjWpudn1ER//uS3IWOatcXRSuYp50TK55w8IY35a6liBx2Z3b3pBkX BeRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=Jf1tQhWhiUHsUcOyz6b9syOvbswpkBFGF9h9iOKMWy8=; fh=t7gFcWLRyAqmyldGbr6d3KgIXGxOYU/i61iCoIU6S88=; b=YQ7U0v0YG1R0xCI3/J2k9lJA1v07F1ohMtLUNcyuUj7reG1TxKmdC2Py4wvqsWsUGz lQdlWEeBxp4jyxAawzC7WdFfrs+ITL3wQJKDCqalduuDXZsbccjydivvmzPAt//qUBNc Kz0JZwoPw1NPuqWH0VLE7JSp5P/XtS4hT6ZIT/+tddH5HTEHNI2STKnUebpmI76hB2CD gMKdEK9eUx6rXCF3Z9J7HMcumPGF2cWB9To54XTAYE4dH0G4ukV0SfFAUmflUMXUebt6 dabNXS7jf5xYjF6zm4eTOZBPO6h+QR4Bid0rhR8AqsXSPkYZvGkcEuJ7SRu+Mr0dUNBN x0RQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="P966TVZ/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id x13-20020aa79a4d000000b006b54b82dac0si7131324pfj.298.2023.10.23.16.41.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 16:41:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="P966TVZ/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 5B90980565B8; Mon, 23 Oct 2023 16:40:45 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231784AbjJWXkX (ORCPT + 99 others); Mon, 23 Oct 2023 19:40:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231433AbjJWXkN (ORCPT ); Mon, 23 Oct 2023 19:40:13 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDE9FCC for ; Mon, 23 Oct 2023 16:40:07 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a7af69a4baso52648357b3.0 for ; Mon, 23 Oct 2023 16:40:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698104407; x=1698709207; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Jf1tQhWhiUHsUcOyz6b9syOvbswpkBFGF9h9iOKMWy8=; b=P966TVZ/R0NaGEdVlwjG21WF+qcIHmwPICnTwvETRjc65V9UfTjzkBucCMZ/TAdpuv Mm6jK1vCno8DkckRhP0rPcjhbt4s5H3SY31sOW56OmfpvP45MzKyDi+k/VQNzmV60oKK aW8zjLBc/QWjeAv3I9n7yUGY0fhF6bcs5/UX6r/fgZxzJOgtvtHCJM2zJ8URnfJ0Zzfu AQMn3Noipq7tnQUrz7t1JwaXajWX1wkwF0wTSjmOUPt8efOj72siZ5DaS5HHZs3kq9I1 N1AyWHiXr0BDfQI2rfzOdedItpkYlZU1I3bk4UxJJDu3FnT9pGNj0e942rwYFh79fPK2 ZczQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698104407; x=1698709207; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Jf1tQhWhiUHsUcOyz6b9syOvbswpkBFGF9h9iOKMWy8=; b=rjWXvSxiH+8YQPki0iVpr7X3n3hsOwIQzva/8fSbEpgcHN1TI8TIPmsovebot//iy7 jEVjHa1+DfoY1wY5NWOUBkCghrVVWtgXB0hBDjdn5lyB7PnaAvUBbexviq/3HS5OS6uz c+OVF42DuNXe13FrfUS1dWMaZLSnpesm7kGDlM3ClEU8m0ukPuKrGLGQj1dEJSdt+mXi HidlCHvifLj8z2NgIXfcIwesg8EZ+ijkf6PjYZZn9iAIf7Rqcvo+mTTqzfDl/JQlreYG SDEnCTxksbe/Kf5lsviRSJ/gwOuDwkpsjULx+2btxE0T9e/XND25C/5mY4yzG8+Ytc0Q ZtYg== X-Gm-Message-State: AOJu0YwC6JNpXkj4OVld6HCoabCQ0ONzh6gHCDQDCf3yr0BueYfbJfGq hC4R79y+HdO+hL9G7Ryg+IVtjvZfdKE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:e245:0:b0:576:af04:3495 with SMTP id l66-20020a0de245000000b00576af043495mr245928ywe.9.1698104407226; Mon, 23 Oct 2023 16:40:07 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 23 Oct 2023 16:39:56 -0700 In-Reply-To: <20231023234000.2499267-1-seanjc@google.com> Mime-Version: 1.0 References: <20231023234000.2499267-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Message-ID: <20231023234000.2499267-3-seanjc@google.com> Subject: [PATCH 2/6] KVM: x86/pmu: Reset the PMU, i.e. stop counters, before refreshing From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Mingwei Zhang , Roman Kagan , Jim Mattson , Dapeng Mi , Like Xu Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 23 Oct 2023 16:40:45 -0700 (PDT) Stop all counters and release all perf events before refreshing the vPMU, i.e. before reconfiguring the vPMU to respond to changes in the vCPU model. Clear need_cleanup in kvm_pmu_reset() as well so that KVM doesn't prematurely stop counters, e.g. if KVM enters the guest and enables counters before the vCPU is scheduled out. Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/pmu.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 027e9c3c2b93..dc8e8e907cfb 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -657,25 +657,14 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) return 0; } -/* refresh PMU settings. This function generally is called when underlying - * settings are changed (such as changes of PMU CPUID by guest VMs), which - * should rarely happen. - */ -void kvm_pmu_refresh(struct kvm_vcpu *vcpu) -{ - if (KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm)) - return; - - bitmap_zero(vcpu_to_pmu(vcpu)->all_valid_pmc_idx, X86_PMC_IDX_MAX); - static_call(kvm_x86_pmu_refresh)(vcpu); -} - void kvm_pmu_reset(struct kvm_vcpu *vcpu) { struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); struct kvm_pmc *pmc; int i; + pmu->need_cleanup = false; + bitmap_zero(pmu->reprogram_pmi, X86_PMC_IDX_MAX); for_each_set_bit(i, pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX) { @@ -695,6 +684,26 @@ void kvm_pmu_reset(struct kvm_vcpu *vcpu) static_call_cond(kvm_x86_pmu_reset)(vcpu); } + +/* + * Refresh the PMU configuration for the vCPU, e.g. if userspace changes CPUID + * and/or PERF_CAPABILITIES. + */ +void kvm_pmu_refresh(struct kvm_vcpu *vcpu) +{ + if (KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm)) + return; + + /* + * Stop/release all existing counters/events before realizing the new + * vPMU model. + */ + kvm_pmu_reset(vcpu); + + bitmap_zero(vcpu_to_pmu(vcpu)->all_valid_pmc_idx, X86_PMC_IDX_MAX); + static_call(kvm_x86_pmu_refresh)(vcpu); +} + void kvm_pmu_init(struct kvm_vcpu *vcpu) { struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); -- 2.42.0.758.gaed0368e0e-goog