Received: by 2002:a05:7412:8d09:b0:fa:4c10:6cad with SMTP id bj9csp263228rdb; Mon, 15 Jan 2024 22:03:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IFwR5St3sqZUb4PL/wrJUJZuqkuTaEiCc7KS53rc6RK0AQJqMpI4CahqMHezxtoaGJnECkZ X-Received: by 2002:a17:90a:f0d7:b0:28e:776a:2a7a with SMTP id fa23-20020a17090af0d700b0028e776a2a7amr199599pjb.23.1705385034476; Mon, 15 Jan 2024 22:03:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705385034; cv=none; d=google.com; s=arc-20160816; b=XJLaw+AiOs4TTM5jlXGd5AlDpU2MrVcDe5OpUGR2WA57CCaXvfCd5VKUmdlIR+DOiL CF4BzSrQJjHAh7WqbqDSjXOPdatQiBRsrtTmNQqoSiECQB3Z8Pzbx4jQBgOKCgmiuOHX P4sSt4YY5e8PlQWrOJGdT7fxGtW4YxNNc5ny+UVDhjlt8Sn0gZ88FgRx8Fd9px10XBjE fKWGuCD4A8KQykgI/rfTRXTyLKegF/jVdByQds8G9nktPNzmJo1QWn2dcBaPsY99je7m wBoPLd+Qp8b4InP3PBNDbmTcpi4UdmmcljGUCJhcnjUbFYF9XJB58uGMyF9CxKgb5vhA lBbA== ARC-Message-Signature: i=1; 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=9Qw6FiQjfkuQIwLM/Cd1XVofWUckSVDRH6rB1Jby9Xc=; fh=879xdiqzWURCyCfdxkLuo0iLZdh7U5YG6OMWr7e/hrg=; b=wBjYUjPDAaXJkOP4WMvpyeLV0MshTNwQKP9Bjt7rvR+Sqob6lFnVlbFGvGKQq2aJ8E XAfB+TSrf9Hjjh6aGrza7twDcAVDmVJOm3VjD2DmgVYif+/XdQ7fykXphkiSHBRtGAaA 3Ah2l3VX30TGYTmTH+vRbRSqu+XpbAqdWLwrbouekyWnTNVm3B3KoA7Bp4yrQwptrtSu j3JmVmNhKHbCC5yFl2aOg38M4oUAU/s4EiwrlEVZDTq1T1928aES9KewqQGLoliFYT1a 1+BB/vk8iQZeHjv+j29AHt4Wkchd309HB/zDyjsrb9B9VnwqgZmhRrRhIwmfRw9wHY7a mJuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZgdWIgUW; spf=pass (google.com: domain of linux-kernel+bounces-27031-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-27031-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id w4-20020a17090aad4400b0028cf1218b1fsi6529983pjv.1.2024.01.15.22.03.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jan 2024 22:03:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-27031-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZgdWIgUW; spf=pass (google.com: domain of linux-kernel+bounces-27031-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-27031-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 295D5B2267B for ; Tue, 16 Jan 2024 06:03:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AF25E134AB; Tue, 16 Jan 2024 06:01:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZgdWIgUW" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 147A912B90 for ; Tue, 16 Jan 2024 06:01:46 +0000 (UTC) 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=1705384905; 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=9Qw6FiQjfkuQIwLM/Cd1XVofWUckSVDRH6rB1Jby9Xc=; b=ZgdWIgUWdhvc1+ubJRVcVOHyWzne2tMQHCw+X88XdvcjO9VvSwd1GiYO7vNVqUxFy+GK1y KfQW9WuGNcF/34TEvfw+NfdRqO4DVTpKfKgOT0lwqVCry2r9JMg3PuMHQtSnICWO7yYxO+ QnJC2v3F6g8k88nFo5NgPOqpAnqizC0= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-370-eny1oHpJPiGlyNf6cxFRLg-1; Tue, 16 Jan 2024 01:01:37 -0500 X-MC-Unique: eny1oHpJPiGlyNf6cxFRLg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 1FB9B1C05ABF; Tue, 16 Jan 2024 06:01:37 +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 125332026D6F; Tue, 16 Jan 2024 06:01:37 +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 v3 4/5] KVM: selftests: aarch64: Introduce pmu_event_filter_test Date: Tue, 16 Jan 2024 01:01:22 -0500 Message-Id: <20240116060129.55473-5-shahuang@redhat.com> In-Reply-To: <20240116060129.55473-1-shahuang@redhat.com> References: <20240116060129.55473-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.4 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 b60852c222ac..5f126e1a1dbf 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -155,6 +155,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 644dae3814b5..f103d0824f8a 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