Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp584514rdb; Thu, 1 Feb 2024 18:59:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IH4kSyIcPjTAuhn73+z0uTSS50HByS3nruVlZkY2pNKz8dN4BCP/+toPq3JcsnDhIHx4apZ X-Received: by 2002:a05:6a20:4393:b0:19e:4cd8:cc4b with SMTP id i19-20020a056a20439300b0019e4cd8cc4bmr1302300pzl.2.1706842795606; Thu, 01 Feb 2024 18:59:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706842795; cv=pass; d=google.com; s=arc-20160816; b=sfafDb34BkKUFByeQhaAUx80szc4NXknwKlbe7wj27cfgWRgH9uc5B+4aK5Mtm/rLy 92vQ10sFIVTHhOepXtPk7dtObK9MifRKTbQC1+eIxJNyPStwmJdATKhgZhtGeuMHUKp5 M4s2xzGJcBYbaMR0mIAd7E3lWqkfOV1F1jAc4XsJiZMjUDeyy94/zFcR9mW2CnS9o2pj 63/83oBSQUwb+80rR/veSGVAYBkN2qm+pC0aZAipfsLKQNAHXsqo5fI6Bpy2R5jFlbDU NdmtOrt6mukQKevgspOwGiOE69XpNJ9pg7SxRV41WOOPhLhqVoaLAmkbo+WrqiJLnrZZ 1dtQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=4CEu9+XX9yg0RRxChkIYOsKurRZVTMg42gyQ5nWY6ec=; fh=iGTwmT0mVZAwtFoIwH5rtIrJHI4RUjEqC3mD7Kg1qXw=; b=UyRYW6t5QnPn1KK7lvsfqy14g3wKzn+nPZtAdQdkglqvpxrSus9t6zkQLYXwAZtQoC HIX5/R5h0qwq9fJGj5g5ea8g5+gsPzW63HS6Og7KAWMDZeI3qtiSMDhmYE2MA8CHuBDx TO/bfNO2hxMUTeE6fUdbOOFHsu/xRQ4fT47KzxojaxgtQxmHH2b6BksOfAb2v5Szaisg mm2MnT3PmTAeYE2tZIzlF+cciBR9AuSWIAwHe4rfbmfeoKuB1ShvV90OoIrY8741YCLt HW1+dDgq+6629IGQyYspxr9ah4xaO+uErFKydioCHEnh2OE6PPbim8N+/DgxrTAiJq2x uOpg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aBcZfTxQ; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-49125-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49125-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCX13CbO0ncXIa907a5RSaJIOJMys5lmZiXg461RC2WsJ1mLxd5XJbVIouPTFW8XR3RAvjBqqFK7wwBgBZoxl5wIECMpKLDiHnd0IumCeg== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id y1-20020a17090ad70100b0028ffa434c6asi878948pju.62.2024.02.01.18.59.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 18:59:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-49125-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aBcZfTxQ; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-49125-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49125-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 1594FB259CE for ; Fri, 2 Feb 2024 02:58:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 13059BE79; Fri, 2 Feb 2024 02:57:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="aBcZfTxQ" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 95D5CF500 for ; Fri, 2 Feb 2024 02:57:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706842636; cv=none; b=hLYLsEWtXtsUUnsXk2izLR8wcNxSlQBSeMARCz7vVB/nl3nSvRFvADstmo1utVsGwm5JIMWAsy0IiJ6yr03UVIFK/UdSncSCCeriKrooWPT23GyAo/qgrASFBHsUJIMHk/KM2Q9r8EQ8SQUYuAgY/NjFgQTBsk91tznkvligmkU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706842636; c=relaxed/simple; bh=fldV5vNj7GoybbAQS5yVjYnryWxRYloB2U1r4gz2Wiw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=i/LTmqwcBNNNN2pV7DGMJLDUiF45GqVh5RQSYUPorQKjoWtY1mhJ8gqZb0T+jJkdc4p3MXMaTmkHcNVP7DmdIAHBabeWH8TjboeQXDySmBvbHa0SPquuSuKsAfGYdtFkWpow2oa7+NyzY8gNtZEvNssE/JxgAIIw9fkQ4gMz8d0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=aBcZfTxQ; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706842633; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4CEu9+XX9yg0RRxChkIYOsKurRZVTMg42gyQ5nWY6ec=; b=aBcZfTxQLxquAtfnergALbfbVHGhTkFejtuoeJrFcRNRutWFI+G+yVciGt9Et+MWfvzI0S EtUvrQgYB6Neose7+xOcHHRuHHi2uRWHZfE5++CbXzq5APMI/e5ooJXrEQlmtIX+6peoQ5 z9BSM2gn9bpIW75dkBj4W6PO1HqGe4A= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-221-Oz1OAG9HN0WVeXz3vDaJHg-1; Thu, 01 Feb 2024 21:57:10 -0500 X-MC-Unique: Oz1OAG9HN0WVeXz3vDaJHg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 081EF101A526; Fri, 2 Feb 2024 02:57:10 +0000 (UTC) Received: from virt-mtcollins-01.lab.eng.rdu2.redhat.com (virt-mtcollins-01.lab.eng.rdu2.redhat.com [10.8.1.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id E7FAAC1ED76; Fri, 2 Feb 2024 02:57:09 +0000 (UTC) From: Shaoqin Huang To: Oliver Upton , Marc Zyngier , kvmarm@lists.linux.dev Cc: Eric Auger , Shaoqin Huang , Paolo Bonzini , Shuah Khan , James Morse , Suzuki K Poulose , Zenghui Yu , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v4 4/5] KVM: selftests: aarch64: Introduce pmu_event_filter_test Date: Thu, 1 Feb 2024 21:56:53 -0500 Message-Id: <20240202025659.5065-5-shahuang@redhat.com> In-Reply-To: <20240202025659.5065-1-shahuang@redhat.com> References: <20240202025659.5065-1-shahuang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 Introduce pmu_event_filter_test for arm64 platforms. The test configures PMUv3 for a vCPU, and sets different pmu event filters for the vCPU, and check if the guest can see those events which user allow and can't use those events which use deny. This test refactor the create_vpmu_vm() and make it a wrapper for __create_vpmu_vm(), which allows some extra init code before KVM_ARM_VCPU_PMU_V3_INIT. And this test use the KVM_ARM_VCPU_PMU_V3_FILTER attribute to set the pmu event filter in KVM. And choose to filter two common event branches_retired and instructions_retired, and let the guest to check if it see the right pmceid register. Signed-off-by: Shaoqin Huang --- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/aarch64/pmu_event_filter_test.c | 219 ++++++++++++++++++ .../selftests/kvm/include/aarch64/vpmu.h | 4 + .../testing/selftests/kvm/lib/aarch64/vpmu.c | 14 +- 4 files changed, 236 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/kvm/aarch64/pmu_event_filter_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 709a70b31ca2..733ec86a3385 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -148,6 +148,7 @@ TEST_GEN_PROGS_aarch64 += aarch64/arch_timer TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions TEST_GEN_PROGS_aarch64 += aarch64/hypercalls TEST_GEN_PROGS_aarch64 += aarch64/page_fault_test +TEST_GEN_PROGS_aarch64 += aarch64/pmu_event_filter_test TEST_GEN_PROGS_aarch64 += aarch64/psci_test TEST_GEN_PROGS_aarch64 += aarch64/set_id_regs TEST_GEN_PROGS_aarch64 += aarch64/smccc_filter diff --git a/tools/testing/selftests/kvm/aarch64/pmu_event_filter_test.c b/tools/testing/selftests/kvm/aarch64/pmu_event_filter_test.c new file mode 100644 index 000000000000..d280382f362f --- /dev/null +++ b/tools/testing/selftests/kvm/aarch64/pmu_event_filter_test.c @@ -0,0 +1,219 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * pmu_event_filter_test - Test user limit pmu event for guest. + * + * Copyright (c) 2023 Red Hat, Inc. + * + * This test checks if the guest only see the limited pmu event that userspace + * sets, if the guest can use those events which user allow, and if the guest + * can't use those events which user deny. + * This test runs only when KVM_CAP_ARM_PMU_V3, KVM_ARM_VCPU_PMU_V3_FILTER + * is supported on the host. + */ +#include +#include +#include +#include +#include +#include + +struct pmce{ + uint64_t pmceid0; + uint64_t pmceid1; +} supported_pmce, guest_pmce; + +static struct vpmu_vm *vpmu_vm; + +#define FILTER_NR 10 + +struct test_desc { + const char *name; + struct kvm_pmu_event_filter filter[FILTER_NR]; +}; + +#define __DEFINE_FILTER(base, num, act) \ + ((struct kvm_pmu_event_filter) { \ + .base_event = base, \ + .nevents = num, \ + .action = act, \ + }) + +#define DEFINE_FILTER(base, act) __DEFINE_FILTER(base, 1, act) + +#define EMPTY_FILTER { 0 } + +#define SW_INCR 0x0 +#define INST_RETIRED 0x8 +#define BR_RETIRED 0x21 + +static void guest_code(void) +{ + uint64_t pmceid0 = read_sysreg(pmceid0_el0); + uint64_t pmceid1 = read_sysreg(pmceid1_el0); + + GUEST_ASSERT_EQ(guest_pmce.pmceid0, pmceid0); + GUEST_ASSERT_EQ(guest_pmce.pmceid1, pmceid1); + + GUEST_DONE(); +} + +static void guest_get_pmceid(void) +{ + supported_pmce.pmceid0 = read_sysreg(pmceid0_el0); + supported_pmce.pmceid1 = read_sysreg(pmceid1_el0); + + GUEST_DONE(); +} + +static void pmu_event_filter_init(struct vpmu_vm *vm, void *arg) +{ + struct kvm_device_attr attr = { + .group = KVM_ARM_VCPU_PMU_V3_CTRL, + .attr = KVM_ARM_VCPU_PMU_V3_FILTER, + }; + struct kvm_pmu_event_filter *filter = (struct kvm_pmu_event_filter *)arg; + + while (filter && filter->nevents != 0) { + attr.addr = (uint64_t)filter; + vcpu_ioctl(vm->vcpu, KVM_SET_DEVICE_ATTR, &attr); + filter++; + } +} + +static void create_vpmu_vm_with_filter(void *guest_code, + struct kvm_pmu_event_filter *filter) +{ + vpmu_vm = __create_vpmu_vm(guest_code, pmu_event_filter_init, filter); +} + +static void run_vcpu(struct kvm_vcpu *vcpu) +{ + struct ucall uc; + + while (1) { + vcpu_run(vcpu); + switch (get_ucall(vcpu, &uc)) { + case UCALL_DONE: + return; + default: + TEST_FAIL("Unknown ucall %lu", uc.cmd); + } + } +} + +static void set_pmce(struct pmce *pmce, int action, int event) +{ + int base = 0; + uint64_t *pmceid = NULL; + + if (event >= 0x4000) { + event -= 0x4000; + base = 32; + } + + if (event >= 0 && event <= 0x1F) { + pmceid = &pmce->pmceid0; + } else if (event >= 0x20 && event <= 0x3F) { + event -= 0x20; + pmceid = &pmce->pmceid1; + } else { + return; + } + + event += base; + if (action == KVM_PMU_EVENT_ALLOW) + *pmceid |= BIT(event); + else + *pmceid &= ~BIT(event); +} + +static void prepare_guest_pmce(struct kvm_pmu_event_filter *filter) +{ + struct pmce pmce_mask = { ~0, ~0 }; + bool first_filter = true; + + while (filter && filter->nevents != 0) { + if (first_filter) { + if (filter->action == KVM_PMU_EVENT_ALLOW) + memset(&pmce_mask, 0, sizeof(pmce_mask)); + first_filter = false; + } + + set_pmce(&pmce_mask, filter->action, filter->base_event); + filter++; + } + + guest_pmce.pmceid0 = supported_pmce.pmceid0 & pmce_mask.pmceid0; + guest_pmce.pmceid1 = supported_pmce.pmceid1 & pmce_mask.pmceid1; +} + +static void run_test(struct test_desc *t) +{ + pr_debug("Test: %s\n", t->name); + + create_vpmu_vm_with_filter(guest_code, t->filter); + prepare_guest_pmce(t->filter); + sync_global_to_guest(vpmu_vm->vm, guest_pmce); + + run_vcpu(vpmu_vm->vcpu); + + destroy_vpmu_vm(vpmu_vm); +} + +static struct test_desc tests[] = { + {"without_filter", { EMPTY_FILTER }}, + {"member_allow_filter", + {DEFINE_FILTER(SW_INCR, 0), DEFINE_FILTER(INST_RETIRED, 0), + DEFINE_FILTER(BR_RETIRED, 0), EMPTY_FILTER}}, + {"member_deny_filter", + {DEFINE_FILTER(SW_INCR, 1), DEFINE_FILTER(INST_RETIRED, 1), + DEFINE_FILTER(BR_RETIRED, 1), EMPTY_FILTER}}, + {"not_member_deny_filter", + {DEFINE_FILTER(SW_INCR, 1), EMPTY_FILTER}}, + {"not_member_allow_filter", + {DEFINE_FILTER(SW_INCR, 0), EMPTY_FILTER}}, + { 0 } +}; + +static void for_each_test(void) +{ + struct test_desc *t; + + for (t = &tests[0]; t->name; t++) + run_test(t); +} + +static bool kvm_supports_pmu_event_filter(void) +{ + int r; + + vpmu_vm = create_vpmu_vm(guest_code); + + r = __kvm_has_device_attr(vpmu_vm->vcpu->fd, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_FILTER); + + destroy_vpmu_vm(vpmu_vm); + return !r; +} + +static bool host_pmu_supports_events(void) +{ + vpmu_vm = create_vpmu_vm(guest_get_pmceid); + + memset(&supported_pmce, 0, sizeof(supported_pmce)); + sync_global_to_guest(vpmu_vm->vm, supported_pmce); + run_vcpu(vpmu_vm->vcpu); + sync_global_from_guest(vpmu_vm->vm, supported_pmce); + destroy_vpmu_vm(vpmu_vm); + + return supported_pmce.pmceid0 & (BR_RETIRED | INST_RETIRED); +} + +int main(void) +{ + TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_PMU_V3)); + TEST_REQUIRE(kvm_supports_pmu_event_filter()); + TEST_REQUIRE(host_pmu_supports_events()); + + for_each_test(); +} diff --git a/tools/testing/selftests/kvm/include/aarch64/vpmu.h b/tools/testing/selftests/kvm/include/aarch64/vpmu.h index 92715021f892..ec37dbd0b1c0 100644 --- a/tools/testing/selftests/kvm/include/aarch64/vpmu.h +++ b/tools/testing/selftests/kvm/include/aarch64/vpmu.h @@ -18,6 +18,10 @@ struct vpmu_vm { int gic_fd; }; +struct vpmu_vm *__create_vpmu_vm(void *guest_code, + void (*init_pmu)(struct vpmu_vm *vm, void *arg), + void *arg); + struct vpmu_vm *create_vpmu_vm(void *guest_code); void destroy_vpmu_vm(struct vpmu_vm *vpmu_vm); diff --git a/tools/testing/selftests/kvm/lib/aarch64/vpmu.c b/tools/testing/selftests/kvm/lib/aarch64/vpmu.c index b3de8fdc555e..76ea03d607f1 100644 --- a/tools/testing/selftests/kvm/lib/aarch64/vpmu.c +++ b/tools/testing/selftests/kvm/lib/aarch64/vpmu.c @@ -7,8 +7,9 @@ #include #include -/* Create a VM that has one vCPU with PMUv3 configured. */ -struct vpmu_vm *create_vpmu_vm(void *guest_code) +struct vpmu_vm *__create_vpmu_vm(void *guest_code, + void (*init_pmu)(struct vpmu_vm *vm, void *arg), + void *arg) { struct kvm_vcpu_init init; uint8_t pmuver; @@ -50,12 +51,21 @@ struct vpmu_vm *create_vpmu_vm(void *guest_code) "Unexpected PMUVER (0x%x) on the vCPU with PMUv3", pmuver); /* Initialize vPMU */ + if (init_pmu) + init_pmu(vpmu_vm, arg); + vcpu_ioctl(vpmu_vm->vcpu, KVM_SET_DEVICE_ATTR, &irq_attr); vcpu_ioctl(vpmu_vm->vcpu, KVM_SET_DEVICE_ATTR, &init_attr); return vpmu_vm; } +/* Create a VM that has one vCPU with PMUv3 configured. */ +struct vpmu_vm *create_vpmu_vm(void *guest_code) +{ + return __create_vpmu_vm(guest_code, NULL, NULL); +} + void destroy_vpmu_vm(struct vpmu_vm *vpmu_vm) { close(vpmu_vm->gic_fd); -- 2.40.1