Message ID | 20230512085321.13259-7-alexghiti@rivosinc.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4955313vqo; Fri, 12 May 2023 02:10:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5c8+J7mvqdZlJXN8D0Bp3IC4u1z2Js/rgZUPCeIyqVhCX6LGO4c6MxTDKEevonQb04LabS X-Received: by 2002:a17:90a:8004:b0:24e:5245:6383 with SMTP id b4-20020a17090a800400b0024e52456383mr23810807pjn.23.1683882652022; Fri, 12 May 2023 02:10:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683882652; cv=none; d=google.com; s=arc-20160816; b=my+qIu5x81c4ug+l3VwICyLLg7JiUP+ONeCt1EWHSVvYGOlLryYMcGGqguVNsbhUQC LLF6BumVOSZjZ2YzdBYwWa35iwE/lc/36rrLHf8hwtK5kVTqLnvumDoBwbtz/Aa2xcCQ iYHIRceFfcnvPCLyKHjqSJtxvSIbJROVV2U6qDsllirM01fRcwQlLujUIYMsJk4rEvqp poW4k64Dnh1S1c+kx36E78EDe2jjCRaHplVpWlI2l8ajGfdTB/5OStnQPtpghmOSk+dE fJ950B7YhvWR6U0Xc4CMLPF7eom9ciWfPqvf9VWO6UkSBy/FN+yLkjIPSfnGr/GgLqXk n+hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=n0tm6iJdFnSKQ00W/5tPio7/nVBToic+wNQP1K4Mncg=; b=DKoDuB0gRrB5wrY850jnQDToIvG7f6CZkGrhwaTwP5iJad391eIVC0NQgu6YUcX0E0 Xndi7bYT7+Rxe08r0PtaZAXl8/Rl2CBUl/bfvBbUL+mZ3MHXOs6qABvHkPk1FODJcf/5 RWEqyDLYxY6uS9SxCiAm3FD0hwzqSSxm56whZhADkwb3vSokPhTij+y/IeCV0X70RUy3 Bz0WRQ+rYjn6c3ZrpRM6IzbuGT1XZBbgE5Drp/RZ4t9f5QFxc8gHYUdh3cGpgooV2tlA D2eOraCXvj3k5U6/gupTfiw1/OVJ7XnMWAIqHeP/6t7lJBh3khov6+rKZ1FrDojVc4+u VO/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=EGtRl3Xr; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mg24-20020a17090b371800b0024b3c34ca20si21943857pjb.55.2023.05.12.02.10.39; Fri, 12 May 2023 02:10:52 -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=EGtRl3Xr; 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 S240244AbjELJAF (ORCPT <rfc822;peekingduck44@gmail.com> + 99 others); Fri, 12 May 2023 05:00:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239962AbjELJAD (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 12 May 2023 05:00:03 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BD482120 for <linux-kernel@vger.kernel.org>; Fri, 12 May 2023 02:00:01 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-3f315712406so313269475e9.0 for <linux-kernel@vger.kernel.org>; Fri, 12 May 2023 02:00:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1683882000; x=1686474000; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=n0tm6iJdFnSKQ00W/5tPio7/nVBToic+wNQP1K4Mncg=; b=EGtRl3XrbxwPlKsaGMLrPknplRMIUf/2v48QERTJWbAD5fZ6GwtIvHIuGTaOG1VkJx kr78QJ1OuWm946zWh0NfAb1XA92r4iYXhlDqOTRD8e5naPGzqvesBFjl8ANthMtqZvyn rCW4QZEIHMW6oTNtVjIOzkDkZTY7gjMVsfoSf10DVc344cFbkshpKLK11/lZioz5cA0w rGI1kujd9CYkfXoc+o4IfNWBEV1VgQo3P7YkIkELNm8PRjZSW13dPvrOX2qyhMv6rzDy c0REunbjflDrF+oa/zPx9fvZWY/Jza2dK2akiNcUCpgBJ6AqW0SVayj0kqJRmerbop3q LKeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683882000; x=1686474000; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=n0tm6iJdFnSKQ00W/5tPio7/nVBToic+wNQP1K4Mncg=; b=gCO+mqQ+dBLEAMOhiqriJyqipp5ic4A7VTjkyiBWfFS+MqXMqcvWWemDkWoUN/B6WS 3In+ldmRG7Vhal96o11fIvrreKIQ6ipgpY2HKptb8toCuCjbVnwM1uhMEIvTdec1tte0 af+w2JfqQgRaxGw3vAXxyhOIe7mIeWmA5yk+Mn/kzwz0ZZ8xtlmtmbbMWtN3YlNr+r2w rNFe8SV9exIOvgIA149U+OLcfaZg9DVsitPTIHta1L9wmEfto5JGfXr37O1sgnKAye0/ uw9zWHmGZ33vhhrX4+G5ezP9ztU+PGEZuP+yKJkkg5HtikK9RanzHN/fKRB9YV0T2vcX 49dg== X-Gm-Message-State: AC+VfDz5PivoXHJ64ToRpz8DFTuCTVydmZSA/hh5OoRatKBmvd+e95ox dBMhTAbqtf+v8KwpxbqOuxZeOg== X-Received: by 2002:adf:ff8c:0:b0:2f8:33bd:a170 with SMTP id j12-20020adfff8c000000b002f833bda170mr21283755wrr.32.1683882000138; Fri, 12 May 2023 02:00:00 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id g8-20020adff3c8000000b003078354f774sm21229104wrp.36.2023.05.12.01.59.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 May 2023 01:59:59 -0700 (PDT) From: Alexandre Ghiti <alexghiti@rivosinc.com> To: Jonathan Corbet <corbet@lwn.net>, Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@redhat.com>, Arnaldo Carvalho de Melo <acme@kernel.org>, Mark Rutland <mark.rutland@arm.com>, Alexander Shishkin <alexander.shishkin@linux.intel.com>, Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>, Ian Rogers <irogers@google.com>, Paul Walmsley <paul.walmsley@sifive.com>, Palmer Dabbelt <palmer@dabbelt.com>, Albert Ou <aou@eecs.berkeley.edu>, Atish Patra <atishp@atishpatra.org>, Anup Patel <anup@brainfault.org>, Will Deacon <will@kernel.org>, Rob Herring <robh@kernel.org>, Andrew Jones <ajones@ventanamicro.com>, 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 Cc: Alexandre Ghiti <alexghiti@rivosinc.com> Subject: [PATCH v2 06/10] drivers: perf: Implement perf event mmap support in the legacy backend Date: Fri, 12 May 2023 10:53:17 +0200 Message-Id: <20230512085321.13259-7-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230512085321.13259-1-alexghiti@rivosinc.com> References: <20230512085321.13259-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765678935787135694?= X-GMAIL-MSGID: =?utf-8?q?1765678935787135694?= |
Series |
riscv: Allow userspace to directly access perf counters
|
|
Commit Message
Alexandre Ghiti
May 12, 2023, 8:53 a.m. UTC
Implement the needed callbacks in the legacy driver so that we can
directly access the counters through perf in userspace.
Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
---
drivers/perf/riscv_pmu_legacy.c | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
Comments
On Fri, May 12, 2023 at 10:53:17AM +0200, Alexandre Ghiti wrote: > Implement the needed callbacks in the legacy driver so that we can > directly access the counters through perf in userspace. > > Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com> > --- > drivers/perf/riscv_pmu_legacy.c | 28 ++++++++++++++++++++++++++++ > 1 file changed, 28 insertions(+) > > diff --git a/drivers/perf/riscv_pmu_legacy.c b/drivers/perf/riscv_pmu_legacy.c > index ffe09d857366..f0f5bd856f66 100644 > --- a/drivers/perf/riscv_pmu_legacy.c > +++ b/drivers/perf/riscv_pmu_legacy.c > @@ -74,6 +74,31 @@ static void pmu_legacy_ctr_start(struct perf_event *event, u64 ival) > local64_set(&hwc->prev_count, initial_val); > } > > +static uint8_t pmu_legacy_csr_index(struct perf_event *event) > +{ > + return event->hw.idx; > +} > + > +static void pmu_legacy_event_mapped(struct perf_event *event, struct mm_struct *mm) > +{ > + /* In legacy mode, the first 3 CSRs are available. */ Shouldn't this be /* In legacy mode, the first and third CSR are available. */ ? > + if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES && > + event->attr.config != PERF_COUNT_HW_INSTRUCTIONS) > + return; > + > + event->hw.flags |= PERF_EVENT_FLAG_USER_READ_CNT; > +} > + > +static void pmu_legacy_event_unmapped(struct perf_event *event, struct mm_struct *mm) > +{ > + /* In legacy mode, the first 3 CSRs are available. */ same comment > + if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES && > + event->attr.config != PERF_COUNT_HW_INSTRUCTIONS) > + return; > + > + event->hw.flags &= ~PERF_EVENT_FLAG_USER_READ_CNT; > +} > + > /* > * This is just a simple implementation to allow legacy implementations > * compatible with new RISC-V PMU driver framework. > @@ -94,6 +119,9 @@ static void pmu_legacy_init(struct riscv_pmu *pmu) > pmu->ctr_get_width = NULL; > pmu->ctr_clear_idx = NULL; > pmu->ctr_read = pmu_legacy_read_ctr; > + pmu->event_mapped = pmu_legacy_event_mapped; > + pmu->event_unmapped = pmu_legacy_event_unmapped; > + pmu->csr_index = pmu_legacy_csr_index; > > perf_pmu_register(&pmu->pmu, RISCV_PMU_LEGACY_PDEV_NAME, PERF_TYPE_RAW); > } > -- > 2.37.2 > Otherwise, Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
On 31/05/2023 16:27, Andrew Jones wrote: > On Fri, May 12, 2023 at 10:53:17AM +0200, Alexandre Ghiti wrote: >> Implement the needed callbacks in the legacy driver so that we can >> directly access the counters through perf in userspace. >> >> Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com> >> --- >> drivers/perf/riscv_pmu_legacy.c | 28 ++++++++++++++++++++++++++++ >> 1 file changed, 28 insertions(+) >> >> diff --git a/drivers/perf/riscv_pmu_legacy.c b/drivers/perf/riscv_pmu_legacy.c >> index ffe09d857366..f0f5bd856f66 100644 >> --- a/drivers/perf/riscv_pmu_legacy.c >> +++ b/drivers/perf/riscv_pmu_legacy.c >> @@ -74,6 +74,31 @@ static void pmu_legacy_ctr_start(struct perf_event *event, u64 ival) >> local64_set(&hwc->prev_count, initial_val); >> } >> >> +static uint8_t pmu_legacy_csr_index(struct perf_event *event) >> +{ >> + return event->hw.idx; >> +} >> + >> +static void pmu_legacy_event_mapped(struct perf_event *event, struct mm_struct *mm) >> +{ >> + /* In legacy mode, the first 3 CSRs are available. */ > Shouldn't this be > > /* In legacy mode, the first and third CSR are available. */ > > ? Yes, I guess this comment is not right in this context, so I'll remove the comment entirely as it does bring much. >> + if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES && >> + event->attr.config != PERF_COUNT_HW_INSTRUCTIONS) >> + return; >> + >> + event->hw.flags |= PERF_EVENT_FLAG_USER_READ_CNT; >> +} >> + >> +static void pmu_legacy_event_unmapped(struct perf_event *event, struct mm_struct *mm) >> +{ >> + /* In legacy mode, the first 3 CSRs are available. */ > same comment > >> + if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES && >> + event->attr.config != PERF_COUNT_HW_INSTRUCTIONS) >> + return; >> + >> + event->hw.flags &= ~PERF_EVENT_FLAG_USER_READ_CNT; >> +} >> + >> /* >> * This is just a simple implementation to allow legacy implementations >> * compatible with new RISC-V PMU driver framework. >> @@ -94,6 +119,9 @@ static void pmu_legacy_init(struct riscv_pmu *pmu) >> pmu->ctr_get_width = NULL; >> pmu->ctr_clear_idx = NULL; >> pmu->ctr_read = pmu_legacy_read_ctr; >> + pmu->event_mapped = pmu_legacy_event_mapped; >> + pmu->event_unmapped = pmu_legacy_event_unmapped; >> + pmu->csr_index = pmu_legacy_csr_index; >> >> perf_pmu_register(&pmu->pmu, RISCV_PMU_LEGACY_PDEV_NAME, PERF_TYPE_RAW); >> } >> -- >> 2.37.2 >> > Otherwise, > > Reviewed-by: Andrew Jones <ajones@ventanamicro.com> Thanks!
diff --git a/drivers/perf/riscv_pmu_legacy.c b/drivers/perf/riscv_pmu_legacy.c index ffe09d857366..f0f5bd856f66 100644 --- a/drivers/perf/riscv_pmu_legacy.c +++ b/drivers/perf/riscv_pmu_legacy.c @@ -74,6 +74,31 @@ static void pmu_legacy_ctr_start(struct perf_event *event, u64 ival) local64_set(&hwc->prev_count, initial_val); } +static uint8_t pmu_legacy_csr_index(struct perf_event *event) +{ + return event->hw.idx; +} + +static void pmu_legacy_event_mapped(struct perf_event *event, struct mm_struct *mm) +{ + /* In legacy mode, the first 3 CSRs are available. */ + if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES && + event->attr.config != PERF_COUNT_HW_INSTRUCTIONS) + return; + + event->hw.flags |= PERF_EVENT_FLAG_USER_READ_CNT; +} + +static void pmu_legacy_event_unmapped(struct perf_event *event, struct mm_struct *mm) +{ + /* In legacy mode, the first 3 CSRs are available. */ + if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES && + event->attr.config != PERF_COUNT_HW_INSTRUCTIONS) + return; + + event->hw.flags &= ~PERF_EVENT_FLAG_USER_READ_CNT; +} + /* * This is just a simple implementation to allow legacy implementations * compatible with new RISC-V PMU driver framework. @@ -94,6 +119,9 @@ static void pmu_legacy_init(struct riscv_pmu *pmu) pmu->ctr_get_width = NULL; pmu->ctr_clear_idx = NULL; pmu->ctr_read = pmu_legacy_read_ctr; + pmu->event_mapped = pmu_legacy_event_mapped; + pmu->event_unmapped = pmu_legacy_event_unmapped; + pmu->csr_index = pmu_legacy_csr_index; perf_pmu_register(&pmu->pmu, RISCV_PMU_LEGACY_PDEV_NAME, PERF_TYPE_RAW); }