Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp2143704rwp; Fri, 14 Jul 2023 01:10:14 -0700 (PDT) X-Google-Smtp-Source: APBJJlFieAw4MXQ7LVrddVVgi2N1CZCPvAApSjUGjR3szVaWQT9OifXOnij6v6xLJXasosVomo3d X-Received: by 2002:a17:906:5308:b0:978:8e8c:1bcb with SMTP id h8-20020a170906530800b009788e8c1bcbmr3413061ejo.43.1689322214187; Fri, 14 Jul 2023 01:10:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689322214; cv=none; d=google.com; s=arc-20160816; b=R6WGYiTeO40o1QP+p8eahZfdiRGbYFwoLEXy7REgpFB5fO+M+4W/I3nziZkYVZcji8 4m8QKib//URM7xk9NkW2SzfcCTfJ2tBbEyDaMXbPBDpDNNnBSed3yr7z8ogKBJXbokdQ kadzV5trQQuiYs2jGQdUcn+f0IsqyAwWKI6xrnoydvzxEhTfw0wtwn0fsxDBtk8CCtbi vJ2FP+1itlbB6uA24B0lSZ/NwD5tDiY8lPo6Ndt2gXKUp/Qx0PzIveGDW3JwMpP+BLqB UVqjL2IgcbEm75Z+YlTgnve/GEIJAmhWFyNDsa277DPL7ApT6ZCivJPuYgtYd04D5Q/Y 2uog== 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=+W0mJqRd+TFAhVjrDLKGgb/GgAz4XBWoxvyD6sXJRhU=; fh=y2M5v1gYeu0HYmN0hofCIqVy6M5fV8gJXE/fccYEzwc=; b=VJX+hPBvqWeWTnsdP4gqgidHgjkVNLsZvvGbFN3W5Fxknpezh0UGzlok9pU4ef8m7B UcW++81NTxvyiTqFu/n4VMkI7BnHCDYSqE03FfqlyLMOwW1k1jlHCAS1rOXwUgoK3QIy OPD6bN6klAwZJVtgd9YLT2kfISh3AW/bhJZ30vDkOKpFyM4E8ITvMwk8iKq3LFwcDnBv BrV0u8RPY3w/uJhqz9UBmdpAdcxwV+eIEMxetrci+ovDJfr7QwbMw6lwHaTPeH7DsFNk BL0NxS3mOST5ZVHqm4eyKwPOoQK1K+kXemywah+YoD4mBtBJaZnyUrTPkDcCv1sLETTL e36A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b=uWqFhJqe; 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 ov4-20020a170906fc0400b0098e1bbc2cffsi9637590ejb.958.2023.07.14.01.09.48; Fri, 14 Jul 2023 01:10:14 -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=@atishpatra.org header.s=google header.b=uWqFhJqe; 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 S235511AbjGNIFa (ORCPT + 99 others); Fri, 14 Jul 2023 04:05:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235119AbjGNIFG (ORCPT ); Fri, 14 Jul 2023 04:05:06 -0400 Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [IPv6:2a00:1450:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 460B135BB for ; Fri, 14 Jul 2023 01:03:13 -0700 (PDT) Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2b6f0508f54so24210401fa.3 for ; Fri, 14 Jul 2023 01:03:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=atishpatra.org; s=google; t=1689321791; x=1691913791; 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=+W0mJqRd+TFAhVjrDLKGgb/GgAz4XBWoxvyD6sXJRhU=; b=uWqFhJqej+8tCoJCvz6HmT/L+2vSKE4vBiTfKGeXqU+oB1LvGzXG2XmndUCr9xAejP wGMR7wYA9IPir6vFan3N2HsFKBeAkkj94j80jLuLaPdimmTCM54Nkv6G4OjiyMjC7ujZ pVaPrrNVbtxLjBFhzK+0CW/kscR0Z0b23xHqg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689321791; x=1691913791; 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=+W0mJqRd+TFAhVjrDLKGgb/GgAz4XBWoxvyD6sXJRhU=; b=a13EVEClZCluHNo5kCdO3vt+j1kyHJcOp8zcTc14Tg0kpO/bfcYoiR1/KEPtM3PrJr B9C3Qk7U6a5yJPqAsHUj4zUaqhF8mcr/80Uog+kFT85T9MKPChXK/mz4mNSH207J8TjO SxDPEWfLijn3CtKwWKqulvYrm8ICSodijW/46FkKl5FgsQgIIk+WoEaMq8xR+NnHKCKs LrKQf3YNvh1v4d5wb7d1yzQXSTVMy5BM0jMtoGRNDPYB474I5I9FcD8VWKi1ajRZP7IJ eYvzUVHAfy2FJzrtojLF7JxdefGVFOdvQElS03DmqN75RzLlbE/A1SaWArQ/sEBhosdE himA== X-Gm-Message-State: ABy/qLYtYeflZeAEmUxF/MFy2jBeu5GsUGU1SQdbBPzGq8KcnVbjxJe7 SHljQvlLXN+k9XyMqCboNrLTSvbyP1WLUNwei8Zs X-Received: by 2002:a2e:900d:0:b0:2b6:f85a:20b0 with SMTP id h13-20020a2e900d000000b002b6f85a20b0mr3041414ljg.16.1689321791509; Fri, 14 Jul 2023 01:03:11 -0700 (PDT) MIME-Version: 1.0 References: <20230703124647.215952-1-alexghiti@rivosinc.com> <20230703124647.215952-6-alexghiti@rivosinc.com> In-Reply-To: <20230703124647.215952-6-alexghiti@rivosinc.com> From: Atish Patra Date: Fri, 14 Jul 2023 01:03:00 -0700 Message-ID: Subject: Re: [PATCH v4 05/10] riscv: Prepare for user-space perf event mmap support To: Alexandre Ghiti 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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 3, 2023 at 5:51=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 > Reviewed-by: Andrew Jones > --- > drivers/perf/riscv_pmu.c | 105 +++++++++++++++++++++++++++++++++ > include/linux/perf/riscv_pmu.h | 4 ++ > 2 files changed, 109 insertions(+) > > diff --git a/drivers/perf/riscv_pmu.c b/drivers/perf/riscv_pmu.c > index ebca5eab9c9b..432ad2e80ce3 100644 > --- a/drivers/perf/riscv_pmu.c > +++ b/drivers/perf/riscv_pmu.c > @@ -14,9 +14,73 @@ > #include > #include > #include > +#include > > #include > > +static bool riscv_perf_user_access(struct perf_event *event) > +{ > + return ((event->attr.type =3D=3D PERF_TYPE_HARDWARE) || > + (event->attr.type =3D=3D PERF_TYPE_HW_CACHE) || > + (event->attr.type =3D=3D PERF_TYPE_RAW)) && > + !!(event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT); > +} > + > +void arch_perf_update_userpage(struct perf_event *event, > + struct perf_event_mmap_page *userpg, u64 n= ow) > +{ > + 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 riscv_perf_user_access(event); > + > + userpg->pmc_width =3D 64; > + > + 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->shift= ); > + 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; > +} > + > static unsigned long csr_read_num(int csr_num) > { > #define switchcase_csr_read(__csr_num, __val) {\ > @@ -171,6 +235,8 @@ int riscv_pmu_event_set_period(struct perf_event *eve= nt) > > local64_set(&hwc->prev_count, (u64)-left); > > + perf_event_update_userpage(event); > + > return overflow; > } > > @@ -267,6 +333,9 @@ static int riscv_pmu_event_init(struct perf_event *ev= ent) > 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 half > @@ -283,6 +352,39 @@ static int riscv_pmu_event_init(struct perf_event *e= vent) > 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_s= truct *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 +409,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_pm= u.h > index 5deeea0be7cb..43282e22ebe1 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_v= al); > void (*ctr_stop)(struct perf_event *event, unsigned lo= ng flag); > int (*event_map)(struct perf_event *event, u64 *confi= g); > + void (*event_init)(struct perf_event *event); > + void (*event_mapped)(struct perf_event *event, struct = mm_struct *mm); > + void (*event_unmapped)(struct perf_event *event, struc= t mm_struct *mm); > + uint8_t (*csr_index)(struct perf_event *event); > > struct cpu_hw_events __percpu *hw_events; > struct hlist_node node; > -- > 2.39.2 > Reviewed-by: Atish Patra --=20 Regards, Atish