Received: by 2002:a05:6358:111d:b0:dc:6189:e246 with SMTP id f29csp2274608rwi; Tue, 1 Nov 2022 06:03:42 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4NIwqZXGVn+E+vuY+3JSHFYo8tKxufaWzBf27fRaWGxVuhJm0HKyRWlpK657oJ/c8xuL/z X-Received: by 2002:a05:6402:1348:b0:461:c056:bf65 with SMTP id y8-20020a056402134800b00461c056bf65mr19454917edw.414.1667307821892; Tue, 01 Nov 2022 06:03:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667307821; cv=none; d=google.com; s=arc-20160816; b=B4cuzUooI10bHQOdgJfSi402SjrDXjW/Kfjo1RFkWqATgMgSN/8HeEHOhPk4vVCSvY WagKX9FaSb7KZTXNQETrgpVFQiZ81fpG5FJIvGE+FLC9wP4WPtDVddpv79UxM4nUJvIM GHHG2XUPpr44qvjlLHOSQA4YPyTCBwDpo0vbPLYej58LW/EXhUSNcKMa5WI98F46razK NBGXp+stRM3mWEhYXbEzfvmI+RX9UWvqZoaCbhICYBF7QlF1jXTuYKBg0LqegqbKLGcj PJsszH6NLAN8pdxOM0cehPZm1nEVa4kC12FixOSHO0FmB0U8ZoSC0RlHx30IIjFDLxCv BLNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=I3ZWUelOBGuajwMCGSZuHahRmnca9IMPS5lnk5/9ETI=; b=kyWvleL6w44GHOkCSK/TIAh1RHQpRu5TBhd/KAc+4eXygFwi825F5dVoXbqDXAdfTr /KuVVryqwjg9ivkL6fKeaH28DA43FBjDbOqCGWW/67I5eROhNkbYy3Y8nkSmda2CcECS 1QSa+/t2zsVka4QagLnxgyrPuP5uNgqNrTPqCz/u8MmHRiHtAr7eizKofM+qHgp8CrPC w7AU3fhlw+yUe1hwzG/dfD4NbduYr7U8XxdNdZ14K/gMJweW0BviaHpVt7UiBDQ+D2eV 3Mgm5pd8H0EyyKLA+0XrBczEIegANF8bEHc33cN/JG3eCQJkhpA/xU5Rd8pwMe6LYLXc LlJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=SH22qPUX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hv12-20020a17090760cc00b00782b24c1ed8si13456543ejc.368.2022.11.01.06.03.16; Tue, 01 Nov 2022 06:03:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=SH22qPUX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230405AbiKAMv3 (ORCPT + 97 others); Tue, 1 Nov 2022 08:51:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229468AbiKAMvY (ORCPT ); Tue, 1 Nov 2022 08:51:24 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CDB31B1FC for ; Tue, 1 Nov 2022 05:51:23 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id x2so21627752edd.2 for ; Tue, 01 Nov 2022 05:51:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; 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=I3ZWUelOBGuajwMCGSZuHahRmnca9IMPS5lnk5/9ETI=; b=SH22qPUXsEn3p/Floa1ZUMCLaXYV8bLk5ULHiQsQqH465E1SVAmIu6Gj850XUtWV9A D3vcOJJpkBKDfQObFbUbvPzsspuQeDi8iVxdxbPuOD5L9lqdeXzBxpoBhQrusiuhFHEW X5gb+sDNXCfylI/G4HA9XvQ5zBoOQLjxCMPiCtygwfXo80GSGpNvwLPh/Z+T/8bh3Zuk SwXqo9RR/EoPDC2jCc+HLc5z3HKMEmC/z195V79dBXKBEvrvXGJaudJx8wGWvzn2YMCf SHKwY8wXPJECzov7T2eLVkWbEI2/y+28jeSZJQxu8o71oB3W65T8TXYTCq5r5pvnAkJ1 KxsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=I3ZWUelOBGuajwMCGSZuHahRmnca9IMPS5lnk5/9ETI=; b=ZWHUf1uX7q+F38X7eJ+BdAA3vZbdseoV2t4B6cSLZHbNuia+LZ83++cHbEIvAQ39Qc EFjQbWSal+Akl/D9Y8HKLHzZcwlwm6p/Rw3A9ThpghmXUSV3DGRok+ijsEzQyrFFJtx+ 60ecIbA9D5WfRB4EFtV/ZQbguvemtQkcx0V/SbWdhUnFbScBlUEiU2ExZiqNrsxsdEkF biNehMl1EdoVJ8ygbBcWZEi5G98LYYZJ3Q5FZ9czaYIAUn1eg5fyd3MGdjc9I9tSBb8b EwvR1E667W6Xp53C8UlY4B/xe1PTm+GQhe4MpDZWEfaEIF2hiZDq+edAvVRqis52Xue7 lBIA== X-Gm-Message-State: ACrzQf38Vqy51/FLE2DtIZ2rZHbrkxaF75AB7W9yhOzUaJMl0YFDKPhb JVXFXFPCfUL/nkq1l345awL9LomfpbXFlw== X-Received: by 2002:a05:6402:158d:b0:463:2343:b980 with SMTP id c13-20020a056402158d00b004632343b980mr14233232edv.150.1667307081713; Tue, 01 Nov 2022 05:51:21 -0700 (PDT) Received: from localhost (cst2-173-61.cust.vodafone.cz. [31.30.173.61]) by smtp.gmail.com with ESMTPSA id e22-20020a17090658d600b007ad0b97e6e9sm4112927ejs.162.2022.11.01.05.51.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 05:51:21 -0700 (PDT) Date: Tue, 1 Nov 2022 13:51:20 +0100 From: Andrew Jones To: Atish Patra Cc: linux-kernel@vger.kernel.org, Albert Ou , Anup Patel , Atish Patra , Guo Ren , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paul Walmsley , Will Deacon Subject: Re: [RFC 4/9] RISC-V: KVM: Improve privilege mode filtering for perf Message-ID: <20221101125120.lkd3n3yybihjhzyk@kamzik> References: <20220718170205.2972215-1-atishp@rivosinc.com> <20220718170205.2972215-5-atishp@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220718170205.2972215-5-atishp@rivosinc.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 18, 2022 at 10:02:00AM -0700, Atish Patra wrote: > Currently, the host driver doesn't have any method to identify if the > requested perf event is from kvm or bare metal. As KVM runs in HS > mode, there are no separate hypervisor privilege mode to distinguish > between the attributes for guest/host. > > Improve the privilege mode filtering by using the event specific > config1 field. > > Signed-off-by: Atish Patra > --- > drivers/perf/riscv_pmu_sbi.c | 27 ++++++++++++++++++++++----- > include/linux/perf/riscv_pmu.h | 2 ++ > 2 files changed, 24 insertions(+), 5 deletions(-) > > diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c > index 5d0eef3ef136..34f9fcc221a8 100644 > --- a/drivers/perf/riscv_pmu_sbi.c > +++ b/drivers/perf/riscv_pmu_sbi.c > @@ -275,6 +275,27 @@ int riscv_pmu_sbi_hpmc_width(void) > } > EXPORT_SYMBOL(riscv_pmu_sbi_hpmc_width); > > +static unsigned long pmu_sbi_get_filter_flags(struct perf_event *event) > +{ > + unsigned long cflags = 0; > + bool guest_events = false; > + > + if (event->attr.config1 & RISCV_KVM_PMU_CONFIG1_GUEST_EVENTS) > + guest_events = true; > + if (event->attr.exclude_kernel) > + cflags |= guest_events ? SBI_PMU_CFG_FLAG_SET_VSINH : SBI_PMU_CFG_FLAG_SET_SINH; > + if (event->attr.exclude_user) > + cflags |= guest_events ? SBI_PMU_CFG_FLAG_SET_VUINH : SBI_PMU_CFG_FLAG_SET_UINH; > + if (guest_events && event->attr.exclude_hv) > + cflags |= SBI_PMU_CFG_FLAG_SET_SINH; > + if (event->attr.exclude_host) > + cflags |= SBI_PMU_CFG_FLAG_SET_UINH | SBI_PMU_CFG_FLAG_SET_SINH; > + if (event->attr.exclude_guest) > + cflags |= SBI_PMU_CFG_FLAG_SET_VSINH | SBI_PMU_CFG_FLAG_SET_VUINH; > + > + return cflags; > +} > + > static int pmu_sbi_ctr_get_idx(struct perf_event *event) > { > struct hw_perf_event *hwc = &event->hw; > @@ -286,11 +307,7 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event) > uint64_t cmask = GENMASK_ULL(rvpmu->num_counters - 1, 0); > unsigned long cflags = 0; > > - if (event->attr.exclude_kernel) > - cflags |= SBI_PMU_CFG_FLAG_SET_SINH; > - if (event->attr.exclude_user) > - cflags |= SBI_PMU_CFG_FLAG_SET_UINH; > - > + cflags = pmu_sbi_get_filter_flags(event); > /* retrieve the available counter index */ > #if defined(CONFIG_32BIT) > ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase, cmask, > diff --git a/include/linux/perf/riscv_pmu.h b/include/linux/perf/riscv_pmu.h > index 6fee211c27b5..825b95253bc5 100644 > --- a/include/linux/perf/riscv_pmu.h > +++ b/include/linux/perf/riscv_pmu.h > @@ -26,6 +26,8 @@ > > #define RISCV_PMU_STOP_FLAG_RESET 1 > > +#define RISCV_KVM_PMU_CONFIG1_GUEST_EVENTS 0x1 > + > struct cpu_hw_events { > /* currently enabled events */ > int n_events; > -- > 2.25.1 > Reviewed-by: Andrew Jones