Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp849196lqg; Sat, 2 Mar 2024 04:36:01 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWpsq153t9VcLWNFFvnsVOFy1R7m/BEV5trRJrMi139UvVzwhFcw0VrRKM9wBdPCmuRhQ8mZZn68mnW7g4WD3XutkOj/2DXWI/yGCv8Gw== X-Google-Smtp-Source: AGHT+IH8UBrmUMbcLvGuEAC5I2xpCPge+pezdtvRo0DO1h043G9sa0wi6Ml1F5y4IEzsETt9NStd X-Received: by 2002:a05:6512:31cc:b0:513:3738:f831 with SMTP id j12-20020a05651231cc00b005133738f831mr2204390lfe.32.1709382961414; Sat, 02 Mar 2024 04:36:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709382961; cv=pass; d=google.com; s=arc-20160816; b=CRn7wT3QTL13FfRxYKJpL6aCWkpdtczslpp8yGY+FAqR4cmqzn2ifBPWzsHbbLMWbH kaoj6bUjFtUZOm7gTTvxxAhxQZKLQSo2C/hP/unTtHAYD51uhjbv7AMgKeAbinkVgrg8 7zldHa40/7YYMBiq1ViX66XgW+oT71SKXWpCy1pWnb8Uu/UIg+NBuQwaTMrQur/ySTeY zuSKxoTfTqPxTQeT0vd5iBB/aJ3CQXinrR0LZrRueDkUuO9TwzIMeMNoqefctd4n1OXH +12ZbDseBlNFGEfu87EGK85xo89PYeg5tUjGdZAzVxXN9WM5KZR+xxVZfdmqQf/sFcig 6wDw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=fLmBNF7KnRHNjxFzW+xgQkBjw9RgIhEMQhm8dyjLxo0=; fh=zLpp/wMKCIU3ENBETWMRsvr8D1oL8gtqvO15ZIreXLY=; b=SaHGESVT3lvODUkZM7RkfEObSjLOBRFWsK5vC00Uq3BYLGjZ5JiQzKtZEn4BriSw6q MkHPu9VBtGHcSXQmxqksQpdqeotwhy4e9I+AOqy8H8NCmaPsTfLRyYcHwBo35nkVbJpH yNEYrIaMntljvt4SS5VmIZvHLnsgutTn9+Ho617FihCtjb30u7vJK4y+eJ6HCkng7W4I 9QkqyEMDV2x/GltmNnscqLpoSCDbKiaxZ2aTijrXSkD+cEIjpUAsll5WFVUs3kljD6Uy BqQ3ki1PxSnLC80Z20gjDmYmN2BdKeO7aI79HkgqLNDNqvwC6iqLMwRs1psglqDLZ2Fk ZBrA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=hycrEykc; arc=pass (i=1 spf=pass spfdomain=ventanamicro.com dkim=pass dkdomain=ventanamicro.com); spf=pass (google.com: domain of linux-kernel+bounces-89443-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89443-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id w17-20020a05640234d100b0056454c5a90bsi2368307edc.106.2024.03.02.04.36.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Mar 2024 04:36:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-89443-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=hycrEykc; arc=pass (i=1 spf=pass spfdomain=ventanamicro.com dkim=pass dkdomain=ventanamicro.com); spf=pass (google.com: domain of linux-kernel+bounces-89443-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89443-linux.lists.archive=gmail.com@vger.kernel.org" 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 am.mirrors.kernel.org (Postfix) with ESMTPS id D66A31F218FB for ; Sat, 2 Mar 2024 12:36:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3B42317562; Sat, 2 Mar 2024 12:35:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b="hycrEykc" Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 548C51841 for ; Sat, 2 Mar 2024 12:35:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709382949; cv=none; b=B5epGj6q/ns4kjELlK0nRdp54Zz3IY3EIOaUlSb3U6qPd1CJHWf8myzMm9W64rTKiGkL7W9fojiS1ZMbv6hCRkLZ7oVCALgSUyu//uBuWpa3aY3+OJ82RVSJBddEaysWVH/AJL7ZUsKW+hQnEJM5tNXhXsCQG1H9LdEZdXcqzsE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709382949; c=relaxed/simple; bh=uvZe44R5Uc/kuroAUbRFRRLGnzr36ZRMq4JyI110Xtc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=QupMLDQMnX3jTyeC8tc7BjZACecIhUZClmKewtZxql0K8929iKtWYxfzDukTbStyrQVbiVdFfSaTJGguhHfmM1yeSvsIxP52+g0VqJAXNtD8Ga7fFxeoM8md6pitLxk36ffQsTdXywc5gTXcsaiviR3Y/IreAbxD4Zomka2Ah7A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com; spf=pass smtp.mailfrom=ventanamicro.com; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b=hycrEykc; arc=none smtp.client-ip=209.85.167.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ventanamicro.com Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-5101cd91017so3913866e87.2 for ; Sat, 02 Mar 2024 04:35:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1709382945; x=1709987745; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=fLmBNF7KnRHNjxFzW+xgQkBjw9RgIhEMQhm8dyjLxo0=; b=hycrEykcvI7a6vJkuvwAHzAlK65vcwluSikVanzWhmk3ZJEcxN0j5l+JdmbA/cNu6m /p4sbXvFma2yGYHpEq7fGrTVGRzGiN7gSrrQTaksa0y/wa0piV19JHZrvC7xBzQVm/hP avIT6y9A938rOdDj1rvfAdjjeRbkdMcOkVkHpVeywyEQBMG4mOnwMIUaizNdpLgQmY8d DDmaJGqausFc7als+FlfqzULu28BmoxuPALB16zIel5RWRL+Bf7gjPPBLTNVsmx/q481 B9Jpdcr5+ifzyxa6pcB2x4UZERy+hPFhY6y1rx7KxJc33eq+hu7PVy5BpvVjsK+NWwxu fG+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709382945; x=1709987745; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=fLmBNF7KnRHNjxFzW+xgQkBjw9RgIhEMQhm8dyjLxo0=; b=wEaxeeaXHxhsIYOqNiXwWc6xzpumgm3zfvJS4iwbO3I/ymXO6yl0BAhN1JKTamgnBe 8SFhcB6rmfYVrBTrRyxXxuGkIqkQXWhkXK5nn43UY+P25ADvNPR6tT77kISC3cOUbu+A SG7g2bPAgvNI3p5Yv7yp9fTe+/cJAu6VlvWz/4mh9I8lxFBoNxc9YmjvYvQAzxeAbLnn SZbuJE0XJV9LG41bVB81ZBIZgTH+jXy7ml8n88MMY9RZb/XZr9d8POAnba3fZn1x4Aeg pvbM4NMU82dyvzjai78jrag4do9pHBdtweHEykW4q2fHiNVdsoCmijidRNUCA7A74p3B RH0A== X-Gm-Message-State: AOJu0YzFsEUiyyVUFNMd1E2fFFQ/7h1CPMJyIBiwR2bbxBdYJQ4/FaQd xZepN2cAudxo0L4Y3nIkow5GifsPXFFeoINLj8TZTYOSmTpUVNXcJoaznYsdqTU= X-Received: by 2002:a05:6512:473:b0:513:1e3c:38ec with SMTP id x19-20020a056512047300b005131e3c38ecmr2688559lfd.22.1709382945338; Sat, 02 Mar 2024 04:35:45 -0800 (PST) Received: from localhost (cst2-173-16.cust.vodafone.cz. [31.30.173.16]) by smtp.gmail.com with ESMTPSA id g3-20020a056402180300b00566ea8e9f38sm1084255edy.40.2024.03.02.04.35.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Mar 2024 04:35:44 -0800 (PST) Date: Sat, 2 Mar 2024 13:35:43 +0100 From: Andrew Jones To: Atish Patra Cc: linux-kernel@vger.kernel.org, Albert Ou , Alexandre Ghiti , Anup Patel , Atish Patra , Conor Dooley , Guo Ren , Icenowy Zheng , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paolo Bonzini , Paul Walmsley , Shuah Khan , Will Deacon Subject: Re: [PATCH v4 15/15] KVM: riscv: selftests: Add a test for counter overflow Message-ID: <20240302-fb3a4d2c7918a24d10ee4a63@orel> References: <20240229010130.1380926-1-atishp@rivosinc.com> <20240229010130.1380926-16-atishp@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240229010130.1380926-16-atishp@rivosinc.com> On Wed, Feb 28, 2024 at 05:01:30PM -0800, Atish Patra wrote: > Add a test for verifying overflow interrupt. Currently, it relies on > overflow support on cycle/instret events. This test works for cycle/ > instret events which support sampling via hpmcounters on the platform. > There are no ISA extensions to detect if a platform supports that. Thus, Ouch. Are there discussions/proposals as to how we can do better with discoverability here? This type of thing sounds like the types of things that get new extension names defined for them as part of the profile spec work. > this test will fail on platform with virtualization but doesn't > support overflow on these two events. > > Signed-off-by: Atish Patra > --- > tools/testing/selftests/kvm/riscv/sbi_pmu.c | 126 +++++++++++++++++++- > 1 file changed, 125 insertions(+), 1 deletion(-) > > diff --git a/tools/testing/selftests/kvm/riscv/sbi_pmu.c b/tools/testing/selftests/kvm/riscv/sbi_pmu.c > index 8ea2a6db6610..c0264c636054 100644 > --- a/tools/testing/selftests/kvm/riscv/sbi_pmu.c > +++ b/tools/testing/selftests/kvm/riscv/sbi_pmu.c > @@ -8,6 +8,7 @@ > * Copyright (c) 2024, Rivos Inc. > */ > > +#include "asm/csr.h" > #include > #include > #include > @@ -16,6 +17,7 @@ > #include "kvm_util.h" > #include "test_util.h" > #include "processor.h" > +#include "arch_timer.h" > > /* Maximum counters (firmware + hardware)*/ > #define RISCV_MAX_PMU_COUNTERS 64 > @@ -26,6 +28,11 @@ union sbi_pmu_ctr_info ctrinfo_arr[RISCV_MAX_PMU_COUNTERS]; > static void *snapshot_gva; > static vm_paddr_t snapshot_gpa; > > +static int pmu_irq = IRQ_PMU_OVF; > + > +static int vcpu_shared_irq_count; > +static int counter_in_use; > + > /* Cache the available counters in a bitmask */ > static unsigned long counter_mask_available; > > @@ -69,7 +76,9 @@ unsigned long pmu_csr_read_num(int csr_num) > #undef switchcase_csr_read > } > > -static inline void dummy_func_loop(int iter) > +static void stop_counter(unsigned long counter, unsigned long stop_flags); > + > +static inline void dummy_func_loop(uint64_t iter) > { > int i = 0; > > @@ -88,6 +97,26 @@ static void guest_illegal_exception_handler(struct ex_regs *regs) > regs->epc += 4; > } > > +static void guest_irq_handler(struct ex_regs *regs) > +{ > + unsigned int irq_num = regs->cause & ~CAUSE_IRQ_FLAG; > + struct riscv_pmu_snapshot_data *snapshot_data = snapshot_gva; > + unsigned long overflown_mask; > + > + /* Stop all counters first to avoid further interrupts */ > + sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, 0, 1UL << counter_in_use, > + SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT, 0, 0, 0); > + > + csr_clear(CSR_SIP, BIT(pmu_irq)); > + > + overflown_mask = READ_ONCE(snapshot_data->ctr_overflow_mask); > + GUEST_ASSERT(overflown_mask & (1UL << counter_in_use)); > + > + /* Validate that we are in the correct irq handler */ > + GUEST_ASSERT_EQ(irq_num, pmu_irq); Should probably do this irq handler assert first. > + WRITE_ONCE(vcpu_shared_irq_count, vcpu_shared_irq_count+1); > +} > + > static unsigned long get_counter_index(unsigned long cbase, unsigned long cmask, > unsigned long cflags, > unsigned long event) > @@ -263,6 +292,32 @@ static void test_pmu_event_snapshot(unsigned long event) > stop_counter(counter, SBI_PMU_STOP_FLAG_RESET); > } > > +static void test_pmu_event_overflow(unsigned long event) > +{ > + unsigned long counter; > + unsigned long counter_value_post; > + unsigned long counter_init_value = ULONG_MAX - 10000; > + struct riscv_pmu_snapshot_data *snapshot_data = snapshot_gva; > + > + counter = get_counter_index(0, counter_mask_available, 0, event); > + counter_in_use = counter; > + > + /* The counter value is updated w.r.t relative index of cbase passed to start/stop */ > + WRITE_ONCE(snapshot_data->ctr_values[0], counter_init_value); > + start_counter(counter, SBI_PMU_START_FLAG_INIT_FROM_SNAPSHOT, 0); > + dummy_func_loop(10000); > + udelay(msecs_to_usecs(2000)); > + /* irq handler should have stopped the counter */ > + > + counter_value_post = READ_ONCE(snapshot_data->ctr_values[counter_in_use]); > + /* The counter value after stopping should be less the init value due to overflow */ > + __GUEST_ASSERT(counter_value_post < counter_init_value, > + "counter_value_post %lx counter_init_value %lx for counter\n", > + counter_value_post, counter_init_value); > + > + stop_counter(counter, SBI_PMU_STOP_FLAG_RESET); > +} > + > static void test_invalid_event(void) > { > struct sbiret ret; > @@ -361,6 +416,43 @@ static void test_pmu_events_snaphost(int cpu) > GUEST_DONE(); > } > > +static void test_pmu_events_overflow(int cpu) no need for cpu > +{ > + long out_val = 0; > + bool probe; > + int num_counters = 0; > + unsigned long sbi_impl_version; > + > + probe = guest_sbi_probe_extension(SBI_EXT_PMU, &out_val); > + GUEST_ASSERT(probe && out_val == 1); > + > + sbi_impl_version = get_host_sbi_impl_version(); > + if (sbi_impl_version >= sbi_mk_version(2, 0)) > + __GUEST_ASSERT(0, "SBI implementation version doesn't support PMU Snapshot"); Identical probe and version check as test_pmu_events_snaphost(). Can factor out. > + > + snapshot_set_shmem(snapshot_gpa, 0); > + csr_set(CSR_IE, BIT(pmu_irq)); > + local_irq_enable(); > + > + /* Get the counter details */ > + num_counters = get_num_counters(); > + update_counter_info(num_counters); > + > + /* > + * Qemu supports overflow for cycle/instruction. > + * This test may fail on any platform that do not support overflow for these two events. > + */ > + test_pmu_event_overflow(SBI_PMU_HW_CPU_CYCLES); > + GUEST_ASSERT_EQ(vcpu_shared_irq_count, 1); > + > + /* Renable the interrupt again for another event */ > + csr_set(CSR_IE, BIT(pmu_irq)); > + test_pmu_event_overflow(SBI_PMU_HW_INSTRUCTIONS); > + GUEST_ASSERT_EQ(vcpu_shared_irq_count, 2); > + > + GUEST_DONE(); > +} > + > static void run_vcpu(struct kvm_vcpu *vcpu) > { > struct ucall uc; > @@ -449,6 +541,35 @@ static void test_vm_events_snapshot_test(void *guest_code) > test_vm_destroy(vm); > } > > +static void test_vm_events_overflow(void *guest_code) > +{ > + struct kvm_vm *vm = NULL; > + struct kvm_vcpu *vcpu = NULL; nit: no need for NULL > + > + vm = vm_create_with_one_vcpu(&vcpu, guest_code); > + __TEST_REQUIRE(__vcpu_has_ext(vcpu, RISCV_SBI_EXT_REG(KVM_RISCV_SBI_EXT_PMU)), > + "SBI PMU not available, skipping test"); > + > + __TEST_REQUIRE(__vcpu_has_ext(vcpu, RISCV_ISA_EXT_REG(KVM_RISCV_ISA_EXT_SSCOFPMF)), > + "Sscofpmf is not available, skipping overflow test"); > + > + > + test_vm_setup_snapshot_mem(vm, vcpu); > + vm_init_vector_tables(vm); > + vm_install_interrupt_handler(vm, guest_irq_handler); > + > + vcpu_init_vector_tables(vcpu); > + /* Initialize guest timer frequency. */ > + vcpu_get_reg(vcpu, RISCV_TIMER_REG(frequency), &timer_freq); > + sync_global_to_guest(vm, timer_freq); I just noticed that timer_freq is in arch_timer.h and isn't an extern... Fixing that is out of scope for this series though. > + > + vcpu_args_set(vcpu, 1, 0); no need for args > + > + run_vcpu(vcpu); > + > + test_vm_destroy(vm); > +} > + > int main(void) > { > test_vm_basic_test(test_pmu_basic_sanity); > @@ -460,5 +581,8 @@ int main(void) > test_vm_events_snapshot_test(test_pmu_events_snaphost); > pr_info("SBI PMU event verification with snapshot test : PASS\n"); > > + test_vm_events_overflow(test_pmu_events_overflow); > + pr_info("SBI PMU event verification with overflow test : PASS\n"); > + > return 0; > } > -- > 2.34.1 > Thanks, drew