Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp5777831rwe; Tue, 18 Apr 2023 11:19:20 -0700 (PDT) X-Google-Smtp-Source: AKy350YjSJDD/h55rJVojrfvpT5wJ/69H6n2TXEAl1id3xhkeBBPFuelHSW6ypSJKMKlPa4WzAnP X-Received: by 2002:a05:6a21:32a2:b0:e9:5b0a:deff with SMTP id yt34-20020a056a2132a200b000e95b0adeffmr486431pzb.22.1681841960217; Tue, 18 Apr 2023 11:19:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681841960; cv=none; d=google.com; s=arc-20160816; b=uLaegwJjEeBpSRTGrdEik0/1klIM570yhgbjADCKpy6LlCOTO0qLFa8sS82yxOxgSn ENfKoMZPtU2GeJ0cPUkNZaTpMBwZ3DfgqX+H0W8L36/FlSkV4Qs6i25hoyRl+oGeuZFN Vvdbh4LKvY2V3HG1UeZ4LI1PeUHRNdthkgohqwxhTGhsn43FVCIJ1Sp6uj0gEI/34LSS HV8ev0wm3bhHS7ulszwPxXYSXKUJlLQLPiuc7dTMTmEpGgFu29leZXK4Uw1E96JQP/+A ibyTjaqPGQY8ERju5iAnkREOHySYR1J++2KmuXOjSPlRlGb4TQE52m5tCvmUhD4gx5Rh qltA== 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=Uzk00VVwAMnUgl8nqdf7G+u1lGlONN9yY2/sgJWoENw=; b=TC5At0I481QVaCu7aJjnRORaj9S/ZVHaxpTTEMxq9a3Ryczj5YLwPoKXW+li6c5l6U poaWBMrgWWeU67PjY+Q9Hd8YstFf/Jn+TVdZ47vBdQbj6WIZnrJNiiJNgDnM3qnSk94h tlBSTKwTJSY5QFLndeP/v1RFSy87Jg19iDsRfVsgY6mTrNEdNmY0WThKlFCI2vkOrTbJ zfxXaju1WfPzccj7QUuzc8jQUvS6EhoNn/AMFIzsABt4BmR/rAolx4XKHQQzL6Ro0Wz+ R6l1AIK9gVW7Q0r22GkY4tqctHWZn3M+mIL3BMpI/7aMgjQDZTyLiM5PIy/jkZC+vUwm tzOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=nVANayyN; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w22-20020a631616000000b0051b423d966csi13997340pgl.280.2023.04.18.11.19.06; Tue, 18 Apr 2023 11:19:20 -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=@google.com header.s=20221208 header.b=nVANayyN; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231296AbjDRSQP (ORCPT + 99 others); Tue, 18 Apr 2023 14:16:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229813AbjDRSQO (ORCPT ); Tue, 18 Apr 2023 14:16:14 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EE9C30E0 for ; Tue, 18 Apr 2023 11:16:12 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-3f04275b2bdso66865e9.1 for ; Tue, 18 Apr 2023 11:16:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681841771; x=1684433771; 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=Uzk00VVwAMnUgl8nqdf7G+u1lGlONN9yY2/sgJWoENw=; b=nVANayyNWQEEcixXLqV58hlA44rj6vtMwQer5ALpXD+J7VO5ysQutiY7HHiuVoz8F+ yORfLruneACibPMnxRaVgKWwcMZVcMD9oFIhFI67ivS4lcNI+4TRMk9rtITgyWkXMNNd PlCuhCVu8rfB3QxBCZQCgaDOC1FDvoVj1H5R5FnFIc1PqdCrK/uiOqLTVShiSQ7M170d VEdup8js/MK8j0kVW+RDn+EEyfkMLCIX7k1ylHxCZlM1YJnlk7+R1enDl3Iq+wHyzLDa deRchW644KPY6XyWgOy4AczDdGW3MUgxI3c4nJ7bH6UddtzrMLoK7+huqXWBBoG+hvlf zj4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681841771; x=1684433771; 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=Uzk00VVwAMnUgl8nqdf7G+u1lGlONN9yY2/sgJWoENw=; b=Er6w6ljT5N6clo76HEBlZJn2u6fMH3/mKxoZ936UkR4LBRPZnHaYgK/5+UJ4Ws4Rbw ii0Ljb8qGMPHDlqp8STZFGFlWC3KMXL0IVfldvAn+3EXJNVpNW1fqUERHRi9u/aeTnNZ n9miH/qCWaM99mlTPa9EAw2nFYmg6+3qB19Q1KxpFaX9YJHDFo45BsjUCQeGWsOP85fq RiOoZFd8c6t6/QFBd276NBfmlz35J3Wo+PykXq9+ERZrniqB2nwE88D9kB83w5CW9J01 oCdHFhDbuv00KjuzHOcsSKB4eSxyVANinpPovg7g4Iiy5VN8B7cg5+MT/jy2YdiLc08l /GLw== X-Gm-Message-State: AAQBX9ertWLCPnACm/y+hutkiEEBIuasPKoAWKXJTSWGlYWdvpTOliUj UlZUkquH8i+S4WjUrAPGhAaTfWWtiop9/uZU2aZM9g== X-Received: by 2002:a05:600c:22c8:b0:3f1:758c:dd23 with SMTP id 8-20020a05600c22c800b003f1758cdd23mr10353wmg.7.1681841770657; Tue, 18 Apr 2023 11:16:10 -0700 (PDT) MIME-Version: 1.0 References: <20230413161725.195417-1-alexghiti@rivosinc.com> In-Reply-To: From: Ian Rogers Date: Tue, 18 Apr 2023 11:15:56 -0700 Message-ID: Subject: Re: [PATCH 0/4] riscv: Allow userspace to directly access perf counters To: Atish Patra 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=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 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 us= er to > > > > mmap any counter when accessed through perf. But we can't break the > > > > existing behaviour so we introduce a sysctl perf_user_access like a= rm64 > > > > does, which defaults to the legacy mode described above. > > > > > > > > > > It would be good provide additional direction for user space packages= : > > > > > > 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 space > > > 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 implic= ations. > > 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-a= ss.net/T/ > > ARM64: > https://docs.kernel.org/arm64/perf.html?highlight=3Dperf_user_access#perf= -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/too= ls/lib/perf/mmap.c#n400 which is updated in these patches but the tests are not: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/too= ls/perf/tests/mmap-basic.c#n287 Which appears to be an oversight. The tests display some differences 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, M= K1 1PT, UK > > Registration No: 1397386 (Wales) > > > > -- > Regards, > Atish