From patchwork Tue Sep 26 23:39:58 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: 13399716 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 5FC65E7F144 for ; Tue, 26 Sep 2023 23:40:48 +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=y8VdtI4f0PiqKl0qyVf5+oOFSAsHoGZRsNQhHhF4IAI=; b=BH7Kl6SQml3GJ1608Q2FfH4IcD O9CxZ/K94BCG0yIZeOY/tqJXWmj9TfcCBVjKK1A8HsyXK45iM/bTv8QaaQN5vgne0Gn6t8XyCK+eX wTUnuTDF5l1c6wtQQ1xaRElonuG08b6YBUri47J43sVkVLHKByXCIvzF6/QFENUlgGv0MDa1v0qzM tFWZF1HEVr/Y9MEVFiVci6K14AZx2sNyOgCjB4zLfYodbMoMJffhLBFKC1TPujHgVIyu0TJqeXIFT 0+0mEqKe0rhyZ9w6NGdfsM+JTMdVBUdoPqGQetZkN4mjyrVge/XIhNR1WjDg4WTRov7D4KCOH8Ywe EfedKGIw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qlHf9-00HBeE-0f; Tue, 26 Sep 2023 23:40:19 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qlHf6-00HBcQ-20 for linux-arm-kernel@lists.infradead.org; Tue, 26 Sep 2023 23:40:18 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d85fc108f0eso14610684276.2 for ; Tue, 26 Sep 2023 16:40:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695771612; x=1696376412; 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=5wBvgde8NqXXedlUolMxmuLFm0wZFB1ciTVf7h91dh0=; b=07hC1N5wx1hHTTj4ROtpRpZJis8uHqf79zRb4C5PkP6AhPiPV6Q7BjCs25qE/i9/wm hiNXgEX3FPs1c4Q6n5tIjqlwqAxnqZsuBxAG777O272CwD1q4h9RyhJ7KoOv7ivQJV/O cWXPEJMQFyUdTFblRY4nqlECL1DDewMTbBvCwexpY2Q+AKxVAZyCeVNC6ZkSeLcixzK/ wE1mQVFzhiSgOQyngl5Vn+JorPYa0OeS6JzJ7WH+j7D9jroW7XBTAt2xOfXogvsULE/T lRHvOAHoHC+CSEXxcIzxp956TQxaT7397BwBRXv2whW8i8Kt4I1UHjB747JDLU0L3+/7 qbKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695771612; x=1696376412; 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=5wBvgde8NqXXedlUolMxmuLFm0wZFB1ciTVf7h91dh0=; b=rcgthRq0oJvdkYRcMtE0kUP0GOkFnJvHzqMLmWyoF6A0j2SCjgjaTN0IZzhsKNku3x bYbYks7j20r1NBCFeLrv1Ykg5jIfoBPHaQ+xFeO2LdNsQVt2Zv9acQGvdJaCfU708+2n WYQWj/z8D7pkhZZi2AsfTXtG++pVxWK91ZYn+OpSacfVCaDw18tCvxFIwdUiPsf8UMuL iAZF4uqkrF5HxQOjxcBsFkDG282gT7Fddu7vnXqqzwn2ngtNFIhPByG0y4xuoeJbeMrn Fkz1yuvBoeJkElnr9yGNzFqUP8QBU32sr7RPqcj9/7ulxzkCTsRObjzIKbwbsgIWwW+Y LJPg== X-Gm-Message-State: AOJu0Yx8s8pmV1+EAd28sdW20cRrEQDeb0MlgFiD6fQn1Tahtpnb8cuJ 9jXlqMOFRSY0jSuDWU7Dh6sWU4c5u01l X-Google-Smtp-Source: AGHT+IGQ/Bhiz9+l+ftp30n3Q3sAQclow99O2nr45tC++G2Qcf6D0tuF4DRExCh4Bp6rFs3g73lmp3qip+2k X-Received: from rananta-linux.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:20a1]) (user=rananta job=sendgmr) by 2002:a05:6902:1682:b0:d81:78ec:c403 with SMTP id bx2-20020a056902168200b00d8178ecc403mr4790ybb.12.1695771612735; Tue, 26 Sep 2023 16:40:12 -0700 (PDT) Date: Tue, 26 Sep 2023 23:39:58 +0000 In-Reply-To: <20230926234008.2348607-1-rananta@google.com> Mime-Version: 1.0 References: <20230926234008.2348607-1-rananta@google.com> X-Mailer: git-send-email 2.42.0.582.g8ccd20d70d-goog Message-ID: <20230926234008.2348607-2-rananta@google.com> Subject: [PATCH v6 01/11] 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230926_164016_657768_E96D7C7E X-CRM114-Status: GOOD ( 16.77 ) 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 --- 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 6b066e04dc5df..fb9817bdfeb57 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) {