Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp5908597rwe; Tue, 18 Apr 2023 13:31:08 -0700 (PDT) X-Google-Smtp-Source: AKy350aYGpBweVhT03LN0yeDdRK9/MLZbCwckEq9IVg5w1B1pM0B3ipRBUM++whWSbBmt0TebXk2 X-Received: by 2002:a05:6a20:918c:b0:ec:6e2d:5a32 with SMTP id v12-20020a056a20918c00b000ec6e2d5a32mr805939pzd.62.1681849867741; Tue, 18 Apr 2023 13:31:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681849867; cv=none; d=google.com; s=arc-20160816; b=SkSZ2Wpt4gv4z1j565TWOAHWtyijfcRVj10H6/t8NikBCejTRCeUZCTzFJc6v1lBak /kMhn0hZWLX4uBYpCDE4VWd9uRvXVICwhOzerDR8ur2vEXfcxz+aplZd/mRM5lj/ZIm2 xf92fxwSzFqEKl/9ioWDAjKUX0SIIRa4h5A4Kj4LiWMovQgRbNkehwYbe6nD6RxgeAN0 m4IM8DOGuPo02cvL8e6fMvjx+XjNs8ohdgwk+PwgXPybXjVdkBS8J7naKb2+jfCetq4R VjQoT4tJRtWAgfyTf003Po/aOCzfz+9dfXZdCqdrvt3g+S9OCTvovjm52JcIjjDLaYUI tpRA== 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=758KJFnubLFSsZj7NLSbl2wQ+MQOeQGEXb82zXsrHA8=; b=a0Phs4xb0V9Q9VwUhhaVAi5jAEZPJ9shO3iK686Ziqn0oCa4t0grR2gkrnVa06maGn evhmw8EoRwDhNuzMHZWDY7KNssnzmAUPpHQEeCAl0mmeSsmx2AM1gtk0PNzNq/xjjYPf BBBbKpJHYlxFnZol7XHfm1ytOLhSkzfO+//DoVhtZtvvk/Aaml/uXFmNIobZe9cnBrN0 v2YjtrY/u6ReUJmlUu3pfuyxs7TpSeiW4Ce2Ry0UEZICY243fhENbLsmLKPhQu3JpQQK xkKqSzoi5fJNTzA53Nv6IhozLsSLrgoOtg9WXSBMJgB2QWAYF6W2bEnLjI2ESuEphLmD g78g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b=JKWc07Hs; 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 23-20020a631857000000b0050bc150235dsi14265590pgy.104.2023.04.18.13.30.55; Tue, 18 Apr 2023 13:31:07 -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=JKWc07Hs; 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 S231262AbjDRUaf (ORCPT + 99 others); Tue, 18 Apr 2023 16:30:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230202AbjDRUad (ORCPT ); Tue, 18 Apr 2023 16:30:33 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57771A275 for ; Tue, 18 Apr 2023 13:30:31 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-517bfdf55c3so1158707a12.2 for ; Tue, 18 Apr 2023 13:30:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=atishpatra.org; s=google; t=1681849831; x=1684441831; 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=758KJFnubLFSsZj7NLSbl2wQ+MQOeQGEXb82zXsrHA8=; b=JKWc07Hsa1AIwKVKIMzXI4sw331w8kX1TDKqtQB//KSPOSUQN+tk39Ofo87lgk87cs A++4AkL4VO7Zo172B6EmKiVr7FrbMePdElPSloCsouI2E/r/+XOY/fDC60aCptlCrI9z Pj+V02e4O7mTkISl5wP8rj3Y1EWU2fzkx9GVY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681849831; x=1684441831; 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=758KJFnubLFSsZj7NLSbl2wQ+MQOeQGEXb82zXsrHA8=; b=PERDbCODNC9FPzQF0J/apfVXmyx5YMFjumlXgnbiyDK4EfwIzm2PODFFeFyoh0Wzbn LyUxdYHXHx/C8ryhT3BA3snduiJ32bctDYNEbAFgXcBlon5Z7UgujDUZFSj9/j3jfhXq RaartYQCHd7vhgga+IwYAJtZUwWLuvvkOyElckHo5LbxUC0T2yVO0abso9d9tkxfY64a XezSexBM0IveEuiDbf6e7pGLal9OAiObseXi0oipDw1RzyDzjcYy+DWC5oGUq4d+82oP gnSWOPsCWBJXKdxFNTphrOYOUiKCa6ajAYEqB78uk6w+wZ5hdyEoLDz/b0TDr0gutNa4 7pEQ== X-Gm-Message-State: AAQBX9evMGTlNmSBweJ4t9cRA9V58KwcsKrCS53RUDNdSVcLMWnCOB0R i+wBO8q/rVxRJ2PudbqYKqYn1puwGz8eIRuhE78G X-Received: by 2002:a17:90a:4706:b0:23d:1121:f211 with SMTP id h6-20020a17090a470600b0023d1121f211mr293761pjg.5.1681849830738; Tue, 18 Apr 2023 13:30:30 -0700 (PDT) MIME-Version: 1.0 References: <20230413161725.195417-1-alexghiti@rivosinc.com> In-Reply-To: From: Atish Patra Date: Wed, 19 Apr 2023 02:00:18 +0530 Message-ID: Subject: Re: [PATCH 0/4] riscv: Allow userspace to directly access perf counters To: Ian Rogers Cc: David Laight , Alexandre Ghiti , Jonathan Corbet , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Will Deacon , Rob Herring , "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" , paranlee 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_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 Tue, Apr 18, 2023 at 11:46=E2=80=AFPM Ian Rogers wr= ote: > > On Tue, Apr 18, 2023 at 9:43=E2=80=AFAM Atish Patra wrote: > > > > On Fri, Apr 14, 2023 at 2:40=E2=80=AFAM David Laight wrote: > > > > > > From: Atish Patra > > > > Sent: 13 April 2023 20:18 > > > > > > > > On Thu, Apr 13, 2023 at 9:47=E2=80=AFPM Alexandre Ghiti wrote: > > > > > > > > > > riscv used to allow direct access to cycle/time/instret counters, > > > > > bypassing the perf framework, this patchset intends to allow the = user to > > > > > mmap any counter when accessed through perf. But we can't break t= he > > > > > existing behaviour so we introduce a sysctl perf_user_access like= arm64 > > > > > does, which defaults to the legacy mode described above. > > > > > > > > > > > > > It would be good provide additional direction for user space packag= es: > > > > > > > > The legacy behavior is supported for now in order to avoid breaking > > > > existing software. > > > > However, reading counters directly without perf interaction may > > > > provide incorrect values which > > > > the userspace software must avoid. We are hoping that the user spac= e > > > > packages which > > > > read the cycle/instret directly, will move to the proper interface > > > > eventually if they actually need it. > > > > Most of the users are supposed to read "time" instead of "cycle" if > > > > they intend to read timestamps. > > > > > > If you are trying to measure the performance of short code > > > fragments then you need pretty much raw access directly to > > > the cycle/clock count register. > > > > > > I've done this on x86 to compare the actual cycle times > > > of different implementations of the IP checksum loop > > > (and compare them to the theoretical limit). > > > The perf framework just added far too much latency, > > > only directly reading the cpu registers gave anything > > > like reliable (and consistent) answers. > > > > > > > This series allows direct access to the counters once configured > > through the perf. > > Earlier the cycle/instret counters are directly exposed to the > > userspace without kernel/perf frameworking knowing > > when/which user space application is reading it. That has security impl= ications. > > > > With this series applied, the user space application just needs to > > configure the event (cycle/instret) through perf syscall. > > Once configured, the userspace application can find out the counter > > information from the mmap & directly > > read the counter. There is no latency while reading the counters. > > > > This mechanism allows stop/clear the counters when the requesting task > > is not running. It also takes care of context switching > > which may result in invalid values as you mentioned below. This is > > nothing new and all other arch (x86, ARM64) allow user space > > counter read through the same mechanism. > > > > Here is the relevant upstream discussion: > > https://lore.kernel.org/lkml/Y7wLa7I2hlz3rKw%2F@hirez.programming.kicks= -ass.net/T/ > > > > ARM64: > > https://docs.kernel.org/arm64/perf.html?highlight=3Dperf_user_access#pe= rf-userspace-pmu-hardware-counter-access > > > > example usage in x86: > > https://github.com/andikleen/pmu-tools/blob/master/jevents/rdpmc.c > > The canonical implementation of this should be: > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/t= ools/lib/perf/mmap.c#n400 Thanks for sharing the libperf implementation. > which is updated in these patches but the tests are not: > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/t= ools/perf/tests/mmap-basic.c#n287 > Which appears to be an oversight. The tests display some differences Yes. It's an oversight. We should make sure that perf mmap tests pass for RISC-V as well. > between x86 and aarch64 that have assumed userspace hardware counter > access, and everything else that it is assumed don't. > > Thanks, > Ian > > > > Clearly process switches (especially cpu migrations) cause > > > problems, but they are obviously invalid values and can > > > be ignored. > > > > > > So while a lot of uses may be 'happy' with the values the > > > perf framework gives, sometimes you do need to directly > > > read the relevant registers. > > > > > > David > > > > > > - > > > Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes,= MK1 1PT, UK > > > Registration No: 1397386 (Wales) > > > > > > > > -- > > Regards, > > Atish -- Regards, Atish