From patchwork Fri Oct 20 21:40:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Raghavendra Rao Ananta X-Patchwork-Id: 13431219 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 701FCC0032E for ; Fri, 20 Oct 2023 21:41:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=0jXgqOpwkbvJnhq7DKC0j68SKRFjPccM6FYBOcfpMVw=; b=wpAckxuxyZ3ol6nUbZHvdgRdl7 S5Se3RuM0p7VKbM+knEWIg1D+LkrZkywWQhY+aTMvq3BxE8DTHHiyxm39vvJR9z5P91253MwL0WbD E/kendEAojSZeIqzOfNAwG8ZhTKXpH2ekb3Ho3GISHXN5an61DpFNvgVZIKVVqjYr4CMqQWs4Tb6q LLDgzXm8K7q5QqUzLX5U5wRmbllQQl9AxZ5G3i+jWSajCeO2jEO0BqwTz1KNhLZHTaDy0blIWS1X+ fdaslnOSntU9HQTnJSmKa9Gq1wzsWjKcNoVbRlzXrOiX020700TNFUVJYchdoEn+gpELtlSFSyxGG 4eot72Pg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qtxF4-0034L0-16; Fri, 20 Oct 2023 21:41:14 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qtxEq-0034Ff-21 for linux-arm-kernel@lists.infradead.org; Fri, 20 Oct 2023 21:41:06 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a818c1d2c7so19058177b3.0 for ; Fri, 20 Oct 2023 14:40:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697838058; x=1698442858; darn=lists.infradead.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=fRZJl825jTe4bNk4e/h0EcnYD7OOEsJ4orW3IfNwdD4=; b=RFcu3yZdhvmu/4OKGqvtM+7KqfEBn7nFK0V4bCzg7VEJP2qEArvQL8u/O7jl/HvG/C UhCnArdHuPqb6ORsrasVEusuwKp9LpMKWAOWu8sfIfe3+K7/yy0/pKfw3RgYsbtfrrl1 zHYdhjqo49tke96341saxwYYOM0kmE2IaPzesqBsX0i5gUmNcAHuMI96x9GHwIbz/+ze /V6NymFMWp4OmqG0+Asttq9s/G1g30CPoXqXZ26OxKZv+nxnxgaTOaQ81UuuMzfYq9Or ZJgd4hAGgwpt9jefrQ+KGdcVcCaBAEh4nrMTsGpY203WQJlXYFD7aUxgKdtK112u/g9b UMAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697838058; x=1698442858; 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=fRZJl825jTe4bNk4e/h0EcnYD7OOEsJ4orW3IfNwdD4=; b=kowwyqAiX3nH/38oSm311iUcpxVK2hYTA5BpAzJiUmf0KMrD7R1YJiF6H5E+g+f/IZ Qv8l6CVooRRS6kQSaEugyHrUPsNqoV3js1dS5pLSZFS1htUA44GhZHNWlwq4cGnaCY03 iFoMEtMxRGSO0lIeiwS3ITWk5dpriacRZn/MD+lw8iI19CU51YBWcGcZp0/9TI0ejISh DjFAko0EBEirEUqZg041c8wYF2U3Hq5+/DFlfx6wN8R6QGx7lhXJD9OX0oejBn0Fd8A8 FxboZ+x7Vk7A5DTpfVgLoO02FjUh1YA1UFvoHDniqTd8b+V/DYDi6wg8k9/QvLh4Zkz5 jnmg== X-Gm-Message-State: AOJu0Yym7kD36XEtjN5dWDnOjEq3Mf7MGSexEk/4dF0xwJ+LznFDBi70 L4tQIK/lP89TrfRtUvWJa/fOhWbBTVCW X-Google-Smtp-Source: AGHT+IHe1u7IUea+IlhDVSmNzeHVDsOIIsOYR3rNgYKN/Y9aeMFrZpa/CBGN7KTEhg1GR5frubeMf8zmtcdy X-Received: from rananta-linux.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:20a1]) (user=rananta job=sendgmr) by 2002:a0d:dd10:0:b0:577:619e:d3c9 with SMTP id g16-20020a0ddd10000000b00577619ed3c9mr76387ywe.10.1697838058021; Fri, 20 Oct 2023 14:40:58 -0700 (PDT) Date: Fri, 20 Oct 2023 21:40:41 +0000 In-Reply-To: <20231020214053.2144305-1-rananta@google.com> Mime-Version: 1.0 References: <20231020214053.2144305-1-rananta@google.com> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog Message-ID: <20231020214053.2144305-2-rananta@google.com> Subject: [PATCH v8 01/13] KVM: arm64: PMU: Introduce helpers to set the guest's PMU From: Raghavendra Rao Ananta To: Oliver Upton , Marc Zyngier Cc: Alexandru Elisei , James Morse , Suzuki K Poulose , Paolo Bonzini , Zenghui Yu , Shaoqin Huang , Jing Zhang , Reiji Watanabe , Colton Lewis , Raghavendra Rao Anata , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Eric Auger X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231020_144100_661529_4E112CB3 X-CRM114-Status: GOOD ( 16.38 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Reiji Watanabe Introduce new helper functions to set the guest's PMU (kvm->arch.arm_pmu) either to a default probed instance or to a caller requested one, and use it when the guest's PMU needs to be set. These helpers will make it easier for the following patches to modify the relevant code. No functional change intended. Signed-off-by: Reiji Watanabe Signed-off-by: Raghavendra Rao Ananta Reviewed-by: Eric Auger Reviewed-by: Sebastian Ott --- arch/arm64/kvm/pmu-emul.c | 50 +++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 3afb281ed8d2c..eb5dcb12dafe9 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -874,6 +874,36 @@ static bool pmu_irq_is_valid(struct kvm *kvm, int irq) return true; } +static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu) +{ + lockdep_assert_held(&kvm->arch.config_lock); + + kvm->arch.arm_pmu = arm_pmu; +} + +/** + * kvm_arm_set_default_pmu - No PMU set, get the default one. + * @kvm: The kvm pointer + * + * The observant among you will notice that the supported_cpus + * mask does not get updated for the default PMU even though it + * is quite possible the selected instance supports only a + * subset of cores in the system. This is intentional, and + * upholds the preexisting behavior on heterogeneous systems + * where vCPUs can be scheduled on any core but the guest + * counters could stop working. + */ +static int kvm_arm_set_default_pmu(struct kvm *kvm) +{ + struct arm_pmu *arm_pmu = kvm_pmu_probe_armpmu(); + + if (!arm_pmu) + return -ENODEV; + + kvm_arm_set_pmu(kvm, arm_pmu); + return 0; +} + static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id) { struct kvm *kvm = vcpu->kvm; @@ -893,7 +923,7 @@ static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id) break; } - kvm->arch.arm_pmu = arm_pmu; + kvm_arm_set_pmu(kvm, arm_pmu); cpumask_copy(kvm->arch.supported_cpus, &arm_pmu->supported_cpus); ret = 0; break; @@ -917,20 +947,10 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) return -EBUSY; if (!kvm->arch.arm_pmu) { - /* - * No PMU set, get the default one. - * - * The observant among you will notice that the supported_cpus - * mask does not get updated for the default PMU even though it - * is quite possible the selected instance supports only a - * subset of cores in the system. This is intentional, and - * upholds the preexisting behavior on heterogeneous systems - * where vCPUs can be scheduled on any core but the guest - * counters could stop working. - */ - kvm->arch.arm_pmu = kvm_pmu_probe_armpmu(); - if (!kvm->arch.arm_pmu) - return -ENODEV; + int ret = kvm_arm_set_default_pmu(kvm); + + if (ret) + return ret; } switch (attr->attr) {