Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2304611rwd; Fri, 16 Jun 2023 02:01:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6/VsfPWBq0pmbcK5qf3Bmc2zV7ZkNtbUGMntcx4UXLILiKBuD6eoELGf80KtdXHd8hkNv4 X-Received: by 2002:a17:902:c94f:b0:1ae:6003:2de0 with SMTP id i15-20020a170902c94f00b001ae60032de0mr1147034pla.68.1686906111686; Fri, 16 Jun 2023 02:01:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686906111; cv=none; d=google.com; s=arc-20160816; b=pKVQXa298GolD4rvZ+tnCd7gy1BO/Ujo4lJDf8DzhBN3RU9QPa41n62kItya98GogZ 0hIVk9260hqlQx2I3CeiVtpIctguQaboyMGLneDyhGw16Y8qR0E/hVI+hChO4YjKmbdj 8NM6nzmX9Q8unCuwz9tHG0XsngbHmrPyHQyhXyZ/tZ4wP7tKt4qtuhJJQGCoJQ2n5EU0 FZTL5bO9tBEg/fKHf5vgL5PT+EZ4Z9HRkUvH3FiDkqec/QV+zeeVNPLYy+cwN/Trei5M RmtGBzdWNbc0t13g5V6NIYljBvR/0x8wHI+plC7gxCpILSvkPmeg+xmwMOOVSimYLGpG QNkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=aKmBd/hk94KIms9coTfr32sXba2SQWkRq10+sLhytcA=; b=zdIaiSZUyDy5UNFtCJ4JZGYgRtdIxUBUhnuGRtHIYa72XqSKzZXEvR8NaXhyyGdbAh TEj9rDUPGnir86ClKGmdvSPX0Fb9yS+V8p1rqU+PUq8TKrXkBlP9fsXb/2g9Md5eQqL2 oJNiolZDBFRvyTVnusGOVikOTl3xJ655f1iVP/29LiqUmh8eXmyWOC0sTVsEGM+/APhO 8dGAGg1aFlZS9rghInbaCttaEbMf/5uFNDVI1YCsiRUUmVez1ZEK5sKStwDGzQX/ZLxl Uc0ka7lb02lgzhV6oRgdrhuEMk15c/Q+XdQydkWE7FWxuc6uoKb+niwwUfUW1RejLE/f tkkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=JKPxfqh9; 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 s4-20020a170902ea0400b001afe7fcb257si10930310plg.64.2023.06.16.02.01.34; Fri, 16 Jun 2023 02:01:51 -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=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=JKPxfqh9; 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 S231221AbjFPI5M (ORCPT + 99 others); Fri, 16 Jun 2023 04:57:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233756AbjFPI5K (ORCPT ); Fri, 16 Jun 2023 04:57:10 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5EDC10F6 for ; Fri, 16 Jun 2023 01:57:07 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-3111547c8f9so305633f8f.1 for ; Fri, 16 Jun 2023 01:57:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1686905826; x=1689497826; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=aKmBd/hk94KIms9coTfr32sXba2SQWkRq10+sLhytcA=; b=JKPxfqh9BgcQebcWJLie7vNP9FJDT7beqezBv/ZAlaD2Y4L8nk+y+hNKSbU1Td7qz6 HD1Y1MQxIiFqAoiRN3aOIAfp4/+7OLmVQrrr6R4+UuUlvpqK2uQlCHOJv5BWCX3OCRyH BCK5RyLF7hwR6cCrLPJmlZKcHKMmp+34Z+3vtwZHK0HcratK78xZys7EJ8UtTjJisJ/x 19XYdpYcCjdNI25BAUrVhW6P5/di3z0EFupM2PZGuy548Gl8Z1EnkAg5WFh+i6nDcXN1 9U+CjXbAOkOEvuBeF6ekUM3u4XF0VQtKU1W7eyCjvqFK5m9o18TyHWyXAD0HWaiGwPu0 KoFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686905826; x=1689497826; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aKmBd/hk94KIms9coTfr32sXba2SQWkRq10+sLhytcA=; b=hkyWWLlC+f1Dkx1MxLTy/5FtYFZsr8OWBiLeoYCBaTFukhlSP7ratChrDsKWSoUmEa zIfNJ/JXEnHq0br2hPipZWXAzwn8dL579wivZSEBU0s8hlA/MzaHBevRJ5AVXb4WX1cC WYGHhWIdWebRkV3ILUjudZrPBrNwkQJ36St4iUQNeqiLJ0YWBgGxM5j00aQWxSWIbcgP sRSLXTbFvpOTdIrJ859TWYilBZa8pG5niiwvm9ZmvP0TZng418wFknqj4MYP2xl8Yag1 6bSnBPe49MBi7jEQhnuAJGeLWF1FMtszWJfPsmT7fTyWrdet3a9rzirVB0msXATbDx1Y SGOw== X-Gm-Message-State: AC+VfDwEeZEQq5fv/GG67lXmYah65/kJ9ScaqqHohmzGzyU+JTSlF9Js 61kLlBU371lUlddpZtRhelkq/W8yd5Zfmn1B3AQElg== X-Received: by 2002:a5d:4c88:0:b0:309:38af:d300 with SMTP id z8-20020a5d4c88000000b0030938afd300mr914039wrs.33.1686905826176; Fri, 16 Jun 2023 01:57:06 -0700 (PDT) MIME-Version: 1.0 References: <20230512085321.13259-1-alexghiti@rivosinc.com> <20230512085321.13259-6-alexghiti@rivosinc.com> In-Reply-To: From: Alexandre Ghiti Date: Fri, 16 Jun 2023 10:56:55 +0200 Message-ID: Subject: Re: [PATCH v2 05/10] riscv: Prepare for user-space perf event mmap support To: Atish Patra Cc: Jonathan Corbet , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Will Deacon , Rob Herring , Andrew Jones , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Fri, Jun 16, 2023 at 10:28=E2=80=AFAM Atish Patra wrote: > > On Fri, May 12, 2023 at 1:58=E2=80=AFAM Alexandre Ghiti wrote: > > > > Provide all the necessary bits in the generic riscv pmu driver to be > > able to mmap perf events in userspace: the heavy lifting lies in the > > driver backend, namely the legacy and sbi implementations. > > > > Note that arch_perf_update_userpage is almost a copy of arm64 code. > > > > Signed-off-by: Alexandre Ghiti > > --- > > arch/riscv/kernel/Makefile | 2 +- > > arch/riscv/kernel/perf_event.c | 58 ++++++++++++++++++++++++++++++++++ > > drivers/perf/riscv_pmu.c | 41 ++++++++++++++++++++++++ > > include/linux/perf/riscv_pmu.h | 4 +++ > > 4 files changed, 104 insertions(+), 1 deletion(-) > > create mode 100644 arch/riscv/kernel/perf_event.c > > > > diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > > index 4cf303a779ab..0d215fd9860d 100644 > > --- a/arch/riscv/kernel/Makefile > > +++ b/arch/riscv/kernel/Makefile > > @@ -70,7 +70,7 @@ obj-$(CONFIG_DYNAMIC_FTRACE) +=3D mcount-dyn.o > > > > obj-$(CONFIG_TRACE_IRQFLAGS) +=3D trace_irq.o > > > > -obj-$(CONFIG_PERF_EVENTS) +=3D perf_callchain.o > > +obj-$(CONFIG_PERF_EVENTS) +=3D perf_callchain.o perf_event.o > > obj-$(CONFIG_HAVE_PERF_REGS) +=3D perf_regs.o > > obj-$(CONFIG_RISCV_SBI) +=3D sbi.o > > ifeq ($(CONFIG_RISCV_SBI), y) > > diff --git a/arch/riscv/kernel/perf_event.c b/arch/riscv/kernel/perf_ev= ent.c > > new file mode 100644 > > index 000000000000..94174a0fc251 > > --- /dev/null > > +++ b/arch/riscv/kernel/perf_event.c > > @@ -0,0 +1,58 @@ > > +// SPDX-License-Identifier: GPL-2.0-only > > +#include > > + > > +void arch_perf_update_userpage(struct perf_event *event, > > + struct perf_event_mmap_page *userpg, u64= now) > > +{ > > + struct clock_read_data *rd; > > + unsigned int seq; > > + u64 ns; > > + > > + userpg->cap_user_time =3D 0; > > + userpg->cap_user_time_zero =3D 0; > > + userpg->cap_user_time_short =3D 0; > > + userpg->cap_user_rdpmc =3D > > + !!(event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT); > > + > > + userpg->pmc_width =3D 64; > > + > > The counter width is 64 for cycle & instret. Other hpmcounter can have > different width. > This information should retrieved from counter info. Yes, this is done in patch 7 when I adapt the perf SBI backend to allow the user access. > > > + do { > > + rd =3D sched_clock_read_begin(&seq); > > + > > + userpg->time_mult =3D rd->mult; > > + userpg->time_shift =3D rd->shift; > > + userpg->time_zero =3D rd->epoch_ns; > > + userpg->time_cycles =3D rd->epoch_cyc; > > + userpg->time_mask =3D rd->sched_clock_mask; > > + > > + /* > > + * Subtract the cycle base, such that software that > > + * doesn't know about cap_user_time_short still 'works' > > + * assuming no wraps. > > + */ > > + ns =3D mul_u64_u32_shr(rd->epoch_cyc, rd->mult, rd->shi= ft); > > + userpg->time_zero -=3D ns; > > + > > + } while (sched_clock_read_retry(seq)); > > + > > + userpg->time_offset =3D userpg->time_zero - now; > > + > > + /* > > + * time_shift is not expected to be greater than 31 due to > > + * the original published conversion algorithm shifting a > > + * 32-bit value (now specifies a 64-bit value) - refer > > + * perf_event_mmap_page documentation in perf_event.h. > > + */ > > + if (userpg->time_shift =3D=3D 32) { > > + userpg->time_shift =3D 31; > > + userpg->time_mult >>=3D 1; > > + } > > + > > + /* > > + * Internal timekeeping for enabled/running/stopped times > > + * is always computed with the sched_clock. > > + */ > > + userpg->cap_user_time =3D 1; > > + userpg->cap_user_time_zero =3D 1; > > + userpg->cap_user_time_short =3D 1; > > +} > > diff --git a/drivers/perf/riscv_pmu.c b/drivers/perf/riscv_pmu.c > > index ebca5eab9c9b..af69da268246 100644 > > --- a/drivers/perf/riscv_pmu.c > > +++ b/drivers/perf/riscv_pmu.c > > @@ -171,6 +171,8 @@ int riscv_pmu_event_set_period(struct perf_event *e= vent) > > > > local64_set(&hwc->prev_count, (u64)-left); > > > > + perf_event_update_userpage(event); > > + > > return overflow; > > } > > > > @@ -267,6 +269,9 @@ static int riscv_pmu_event_init(struct perf_event *= event) > > hwc->idx =3D -1; > > hwc->event_base =3D mapped_event; > > > > + if (rvpmu->event_init) > > + rvpmu->event_init(event); > > + > > if (!is_sampling_event(event)) { > > /* > > * For non-sampling runs, limit the sample_period to ha= lf > > @@ -283,6 +288,39 @@ static int riscv_pmu_event_init(struct perf_event = *event) > > return 0; > > } > > > > +static int riscv_pmu_event_idx(struct perf_event *event) > > +{ > > + struct riscv_pmu *rvpmu =3D to_riscv_pmu(event->pmu); > > + > > + if (!(event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT)) > > + return 0; > > + > > + if (rvpmu->csr_index) > > + return rvpmu->csr_index(event) + 1; > > + > > + return 0; > > +} > > + > > +static void riscv_pmu_event_mapped(struct perf_event *event, struct mm= _struct *mm) > > +{ > > + struct riscv_pmu *rvpmu =3D to_riscv_pmu(event->pmu); > > + > > + if (rvpmu->event_mapped) { > > + rvpmu->event_mapped(event, mm); > > + perf_event_update_userpage(event); > > + } > > +} > > + > > +static void riscv_pmu_event_unmapped(struct perf_event *event, struct = mm_struct *mm) > > +{ > > + struct riscv_pmu *rvpmu =3D to_riscv_pmu(event->pmu); > > + > > + if (rvpmu->event_unmapped) { > > + rvpmu->event_unmapped(event, mm); > > + perf_event_update_userpage(event); > > + } > > +} > > + > > struct riscv_pmu *riscv_pmu_alloc(void) > > { > > struct riscv_pmu *pmu; > > @@ -307,6 +345,9 @@ struct riscv_pmu *riscv_pmu_alloc(void) > > } > > pmu->pmu =3D (struct pmu) { > > .event_init =3D riscv_pmu_event_init, > > + .event_mapped =3D riscv_pmu_event_mapped, > > + .event_unmapped =3D riscv_pmu_event_unmapped, > > + .event_idx =3D riscv_pmu_event_idx, > > .add =3D riscv_pmu_add, > > .del =3D riscv_pmu_del, > > .start =3D riscv_pmu_start, > > diff --git a/include/linux/perf/riscv_pmu.h b/include/linux/perf/riscv_= pmu.h > > index 9f70d94942e0..1452c8af3b67 100644 > > --- a/include/linux/perf/riscv_pmu.h > > +++ b/include/linux/perf/riscv_pmu.h > > @@ -55,6 +55,10 @@ struct riscv_pmu { > > void (*ctr_start)(struct perf_event *event, u64 init= _val); > > void (*ctr_stop)(struct perf_event *event, unsigned = long flag); > > int (*event_map)(struct perf_event *event, u64 *con= fig); > > + void (*event_init)(struct perf_event *event); > > + void (*event_mapped)(struct perf_event *event, struc= t mm_struct *mm); > > + void (*event_unmapped)(struct perf_event *event, str= uct mm_struct *mm); > > + uint8_t (*csr_index)(struct perf_event *event); > > > > struct cpu_hw_events __percpu *hw_events; > > struct hlist_node node; > > -- > > 2.37.2 > > > > > -- > Regards, > Atish