From nobody Mon Jun 15 10:47:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B846C433EF for ; Wed, 8 Jun 2022 22:44:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235496AbiFHWoQ (ORCPT ); Wed, 8 Jun 2022 18:44:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236552AbiFHWoL (ORCPT ); Wed, 8 Jun 2022 18:44:11 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9A9A115C8F for ; Wed, 8 Jun 2022 15:44:10 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id w36-20020a17090a6ba700b001e876698a01so6732350pjj.5 for ; Wed, 08 Jun 2022 15:44:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=bfkEdtucqgA+wBrOBxrBZd3UEfs8ZNdv2ifdbarRzlY=; b=ekhZ4wBvlPn/dbdAv6DpZoPYJXSrjHhH3vsu1Ns85VYgtvnHjevVS+J3GQ6jXRTSfX 3phJNq0VEY5NXH5SQCmeb3Wq4ya5jZRnyK3jkeGs7V79agvHEAOec6tOLhKA0RW67VzT lnJC1t81mZiCjUex1/HhgnUlDvtebaL8C1CERHhIqNNS5COd4+lb2EudOnc17DiBvm7n kxr5s3eFaqc8gnb76bOXNg+jY/wA6p37zN8dLeK7s6Hnqm4Zuf8hGGepLgpmAFXl5bKm OJXZ6wLTeFcASLUd9lTPgYjpbXPXUJsUR2RAKHoMZt38KoirvOb8O/Sj4+wiMub0gf1U e52Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=bfkEdtucqgA+wBrOBxrBZd3UEfs8ZNdv2ifdbarRzlY=; b=nu+vXCqBjOf1/ytFb9P/u2Hjypt1BCbpNSepr6rh37xqz64cupF3fN/5Aq4hRNaoz7 5wue9QHc07It1phwE+wc8oIxtP5OEr3BltYC8FA0xbcFzLSjaFAMkRvme3mb0FK1JuPh osAekIq7PCupxrAez+vtbO219ke3FIM598Q2AHH+3ujty/DqvV4XnGBh17AwTRkWKrTO D/Nul6er5tPCqOstmimfTWJEobhJljR02/fNBEWYnrmb6tGH+iNpnhdeMcFz4kad+QpF aqGjixUCBbG8HVncA+zBFqLgfPr5XX5n7QU8XtBVxoKjXWUmLmHrmnLEBzeteSnhRoQT OBRA== X-Gm-Message-State: AOAM5336Sknjm/NiLunpOl+EacnLe1AyzjMR+LPbZWcat3tbAx3gXLJk 3pzbMvGm+vfaZEY2Uph+uAvQvdXD7Web X-Google-Smtp-Source: ABdhPJwiZrRqHCYP62YKEXvoVQ53Bbqy96qtS43wIUeLesJ+bttYHn5FJ24TSzmDscTk0ZfS1Ktj1g/cMxU6 X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:de48:5efa:e4a1:2219]) (user=irogers job=sendgmr) by 2002:a05:6a00:1a91:b0:51c:2ef4:fa1c with SMTP id e17-20020a056a001a9100b0051c2ef4fa1cmr13542691pfv.75.1654728250194; Wed, 08 Jun 2022 15:44:10 -0700 (PDT) Date: Wed, 8 Jun 2022 15:43:50 -0700 In-Reply-To: <20220608224353.1176079-1-irogers@google.com> Message-Id: <20220608224353.1176079-2-irogers@google.com> Mime-Version: 1.0 References: <20220608224353.1176079-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH 1/4] libperf evsel: Open shouldn't leak fd on failure From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Kajol Jain , Andi Kleen , Adrian Hunter , Anshuman Khandual , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Rob Herring Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If the perf_event_open fails the fd is opened but the fd is only freed by closing (not by delete). Typically when an open fails you don't call close and so this results in a memory leak. To avoid this, add a close when open fails. Signed-off-by: Ian Rogers --- tools/lib/perf/evsel.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c index c1d58673f6ef..952f3520d5c2 100644 --- a/tools/lib/perf/evsel.c +++ b/tools/lib/perf/evsel.c @@ -149,23 +149,30 @@ int perf_evsel__open(struct perf_evsel *evsel, struct= perf_cpu_map *cpus, int fd, group_fd, *evsel_fd; =20 evsel_fd =3D FD(evsel, idx, thread); - if (evsel_fd =3D=3D NULL) - return -EINVAL; + if (evsel_fd =3D=3D NULL) { + err =3D -EINVAL; + goto out; + } =20 err =3D get_group_fd(evsel, idx, thread, &group_fd); if (err < 0) - return err; + goto out; =20 fd =3D sys_perf_event_open(&evsel->attr, threads->map[thread].pid, cpu, group_fd, 0); =20 - if (fd < 0) - return -errno; + if (fd < 0) { + err =3D -errno; + goto out; + } =20 *evsel_fd =3D fd; } } +out: + if (err) + perf_evsel__close(evsel); =20 return err; } --=20 2.36.1.255.ge46751e96f-goog From nobody Mon Jun 15 10:47:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8CB87C43334 for ; Wed, 8 Jun 2022 22:44:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232734AbiFHWoW (ORCPT ); Wed, 8 Jun 2022 18:44:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236540AbiFHWoO (ORCPT ); Wed, 8 Jun 2022 18:44:14 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B699184877 for ; Wed, 8 Jun 2022 15:44:13 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id n201-20020a2540d2000000b0065cbae85d67so18777360yba.11 for ; Wed, 08 Jun 2022 15:44:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=92Ap8HPAB8/MDXHmk/KascZglACyrXnQYkxdMYEdECc=; b=e99nTxwzmHI8yebHt+bbgofhp8aiUOekQygtiQN9XVjtflMrSYrSxwUnYhMDVL025Z M3h6iW3nbPYbQT3tOE1FIm8plo1TrsMnyVTfADBzqDQntVwHcGKU8+TBm6d5k4x7CEmH v9KbRZ7jVYm/t/tLpV/xBFHFPZurF1Hy8Jdh3Hxv8ZqtO4xp00LCarmiLo+abbE4k0cZ 2txF2ryyWgDGWLnW08thypUFOCfLKLKsaz3PwIzj91vCu8VCJs/DxajOJq/rsgouSHT4 xXEHIq2PyfH2pBaidi1w6y5VI7asiz89GopJYDV9zzYiszeQ91//MJEneWiwUsczB1O5 DWtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=92Ap8HPAB8/MDXHmk/KascZglACyrXnQYkxdMYEdECc=; b=R5FtFwq5RCeWcyQhLvpDofn9poA29A/DVEaOFy2u0R81bT8Hv1rcsOcl3GCXZ8Dnt0 0PCQEtZL6XVXHeqOwk8Vv8rBzceT+aUOzi6ci5zGim1zkfwxt8t3iHASb4q7ny+ZyYrp 9cyNrQ8arcDQWGijV6Ibpp7hSE8NDb6i+UsCCJYv2w3DzhasYylrMN3dbjhdKV1kChVq oAirUWX+WichhhjsYbWQFqvcVqYRuoboM209lwgNswAy3f7ITCxab9iUGfT7zwTT2zNw KyQn0/wXQyV2cUMuz54JV8OyOAhKD9pEZ7rcGITRJfPL0nakZKRogWVk6qTTFUjDiRpJ 9VXQ== X-Gm-Message-State: AOAM533CDm3rdzdZSslq6jLOL49ERqdEurTHgfbOpTtb2j3CpoYs67ws J4jPNV6PbC95bc9XW3wgIwAnMnqIF4Kw X-Google-Smtp-Source: ABdhPJxMTueI/fWmU45+M23F3qCnjJDGJHxta8YxrKZGEvI7f7QC8ZqBbTtRq6zU6Ra68YgqHXiCfr8LgW9Y X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:de48:5efa:e4a1:2219]) (user=irogers job=sendgmr) by 2002:a05:690c:108:b0:2e5:b91a:195b with SMTP id bd8-20020a05690c010800b002e5b91a195bmr41183210ywb.44.1654728252702; Wed, 08 Jun 2022 15:44:12 -0700 (PDT) Date: Wed, 8 Jun 2022 15:43:51 -0700 In-Reply-To: <20220608224353.1176079-1-irogers@google.com> Message-Id: <20220608224353.1176079-3-irogers@google.com> Mime-Version: 1.0 References: <20220608224353.1176079-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH 2/4] perf: Align user space counter reading with code From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Kajol Jain , Andi Kleen , Adrian Hunter , Anshuman Khandual , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Rob Herring Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Align the user space counter reading documentation with the code in perf_mmap__read_self. Previously the documentation was based on the perf rdpmc test, but now general purpose code is provided by libperf. Signed-off-by: Ian Rogers --- include/uapi/linux/perf_event.h | 32 ++++++++++++++++----------- tools/include/uapi/linux/perf_event.h | 32 ++++++++++++++++----------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_even= t.h index d37629dbad72..3b84e0ad0723 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -538,9 +538,13 @@ struct perf_event_mmap_page { * * if (pc->cap_usr_time && enabled !=3D running) { * cyc =3D rdtsc(); - * time_offset =3D pc->time_offset; * time_mult =3D pc->time_mult; * time_shift =3D pc->time_shift; + * time_offset =3D pc->time_offset; + * if (pc->cap_user_time_short) { + * time_cycles =3D pc->time_cycles; + * time_mask =3D pc->time_mask; + * } * } * * index =3D pc->index; @@ -548,6 +552,9 @@ struct perf_event_mmap_page { * if (pc->cap_user_rdpmc && index) { * width =3D pc->pmc_width; * pmc =3D rdpmc(index - 1); + * pmc <<=3D 64 - width; + * pmc >>=3D 64 - width; + * count +=3D pmc; * } * * barrier(); @@ -590,25 +597,24 @@ struct perf_event_mmap_page { * If cap_usr_time the below fields can be used to compute the time * delta since time_enabled (in ns) using rdtsc or similar. * - * u64 quot, rem; - * u64 delta; - * - * quot =3D (cyc >> time_shift); - * rem =3D cyc & (((u64)1 << time_shift) - 1); - * delta =3D time_offset + quot * time_mult + - * ((rem * time_mult) >> time_shift); + * cyc =3D time_cycles + ((cyc - time_cycles) & time_mask); + * delta =3D time_offset + mul_u64_u32_shr(cyc, time_mult, time_shift); * * Where time_offset,time_mult,time_shift and cyc are read in the * seqcount loop described above. This delta can then be added to - * enabled and possible running (if index), improving the scaling: + * enabled and possible running (if index) to improve the scaling. Due + * to event multiplexing, running maybe zero and so care is needed to + * avoid division by zero. * * enabled +=3D delta; - * if (index) + * if (idx) * running +=3D delta; * - * quot =3D count / running; - * rem =3D count % running; - * count =3D quot * enabled + (rem * enabled) / running; + * if (running !=3D 0) { + * quot =3D count / running; + * rem =3D count % running; + * count =3D quot * enabled + (rem * enabled) / running; + * } */ __u16 time_shift; __u32 time_mult; diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/lin= ux/perf_event.h index d37629dbad72..3b84e0ad0723 100644 --- a/tools/include/uapi/linux/perf_event.h +++ b/tools/include/uapi/linux/perf_event.h @@ -538,9 +538,13 @@ struct perf_event_mmap_page { * * if (pc->cap_usr_time && enabled !=3D running) { * cyc =3D rdtsc(); - * time_offset =3D pc->time_offset; * time_mult =3D pc->time_mult; * time_shift =3D pc->time_shift; + * time_offset =3D pc->time_offset; + * if (pc->cap_user_time_short) { + * time_cycles =3D pc->time_cycles; + * time_mask =3D pc->time_mask; + * } * } * * index =3D pc->index; @@ -548,6 +552,9 @@ struct perf_event_mmap_page { * if (pc->cap_user_rdpmc && index) { * width =3D pc->pmc_width; * pmc =3D rdpmc(index - 1); + * pmc <<=3D 64 - width; + * pmc >>=3D 64 - width; + * count +=3D pmc; * } * * barrier(); @@ -590,25 +597,24 @@ struct perf_event_mmap_page { * If cap_usr_time the below fields can be used to compute the time * delta since time_enabled (in ns) using rdtsc or similar. * - * u64 quot, rem; - * u64 delta; - * - * quot =3D (cyc >> time_shift); - * rem =3D cyc & (((u64)1 << time_shift) - 1); - * delta =3D time_offset + quot * time_mult + - * ((rem * time_mult) >> time_shift); + * cyc =3D time_cycles + ((cyc - time_cycles) & time_mask); + * delta =3D time_offset + mul_u64_u32_shr(cyc, time_mult, time_shift); * * Where time_offset,time_mult,time_shift and cyc are read in the * seqcount loop described above. This delta can then be added to - * enabled and possible running (if index), improving the scaling: + * enabled and possible running (if index) to improve the scaling. Due + * to event multiplexing, running maybe zero and so care is needed to + * avoid division by zero. * * enabled +=3D delta; - * if (index) + * if (idx) * running +=3D delta; * - * quot =3D count / running; - * rem =3D count % running; - * count =3D quot * enabled + (rem * enabled) / running; + * if (running !=3D 0) { + * quot =3D count / running; + * rem =3D count % running; + * count =3D quot * enabled + (rem * enabled) / running; + * } */ __u16 time_shift; __u32 time_mult; --=20 2.36.1.255.ge46751e96f-goog From nobody Mon Jun 15 10:47:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3309C433EF for ; Wed, 8 Jun 2022 22:44:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236885AbiFHWoa (ORCPT ); Wed, 8 Jun 2022 18:44:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236795AbiFHWoQ (ORCPT ); Wed, 8 Jun 2022 18:44:16 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4CA124F959 for ; Wed, 8 Jun 2022 15:44:15 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id n8-20020a170902d2c800b001663868e2c2so10616541plc.21 for ; Wed, 08 Jun 2022 15:44:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=oQIyu4cKVU7oWo40WjhPnpMJD4dvWtcQq5pq7XDuQOU=; b=ARir2B30o4f11vJ7UT3Kl+yHxYDlXXGc5vmkBlGwJYi3MnGrxeVWYVR6mhh3V7qKEj TOVYfBeN/c+RxvuqAk7goUoTwGZ/VywDY6s5rg3U8OynlZGsRKbf2nj60Xri4JKL/LtJ iAxsoWVW//HuBtv1W1aEk5DqrkoX1iFZ4HhF7W16LSnqqUFr5S6pLmbDgRN+REy/MLOm m4ukBRPjgPWOaOPoEmdf8Q8IYFM4rQgWu7WRPAgBhZYS6XMb0c0joi1eAXM12sFG5vdZ vmZb0tYlbsYprxgd0dWqGSYgxhsVq22EHGPHSkeb9Tu7FZNMVcmHq5FvyKM+Ywia9Z82 4hpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=oQIyu4cKVU7oWo40WjhPnpMJD4dvWtcQq5pq7XDuQOU=; b=sKseW81IsrIsiqbvMxsdPjaNhXL5IZu6/j75OB9NK1EP1Ll9lCij2WnqXAh0OLZ+uy aivDlA5HGse1ThAP4mdlh13L5bEJ21hMcSlPYb5KAWYBeYZBDPw5aYo4fYN0r3Vp7l59 diW1JrbCIyaV40eJl/i6ZrMl8n09k/f/OINBuKHjLlcnUM5zeKzmADGcwi7L26eWd+Ib Z8MC02ExpZ/JAViH0JfqugsDX9PS00Tlm4g4BZfbNhLnsJHlK68JknOeEquW9bwx2aJI xUsch+deRXTO/FZA5KaiPy0dZ2N4dwTjqnDF0yJTk6F4BX/+Te4k4qOIihqD9UDFYbeh ZldQ== X-Gm-Message-State: AOAM531CTudpb/rCxnPvmEbbBEnnTW+IIhIy2FKVNcHJrayODi/GbsSU pxf7M3kkY/7qea6rPw172gjDxSsl+nZq X-Google-Smtp-Source: ABdhPJzRXDhD76XhB04n4Hh4hY55zSkHem5rzfZtiBOp2wtOwQjlRss8XJvcJmyGmYGXa98ts3iv1HWm43bH X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:de48:5efa:e4a1:2219]) (user=irogers job=sendgmr) by 2002:a05:6a00:2386:b0:51c:3ca7:b185 with SMTP id f6-20020a056a00238600b0051c3ca7b185mr10563849pfc.8.1654728255109; Wed, 08 Jun 2022 15:44:15 -0700 (PDT) Date: Wed, 8 Jun 2022 15:43:52 -0700 In-Reply-To: <20220608224353.1176079-1-irogers@google.com> Message-Id: <20220608224353.1176079-4-irogers@google.com> Mime-Version: 1.0 References: <20220608224353.1176079-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH 3/4] perf test: Remove x86 rdpmc test From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Kajol Jain , Andi Kleen , Adrian Hunter , Anshuman Khandual , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Rob Herring Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This test has been superseded by test_stat_user_read in: tools/lib/perf/tests/test-evsel.c The updated test doesn't divide-by-0 when running time of a counter is 0. It also supports ARM64. Signed-off-by: Ian Rogers Acked-by: Rob Herring --- tools/perf/arch/x86/tests/Build | 1 - tools/perf/arch/x86/tests/arch-tests.c | 2 - tools/perf/arch/x86/tests/rdpmc.c | 182 ------------------------- 3 files changed, 185 deletions(-) delete mode 100644 tools/perf/arch/x86/tests/rdpmc.c diff --git a/tools/perf/arch/x86/tests/Build b/tools/perf/arch/x86/tests/Bu= ild index 28d793390198..70b5bcbc15df 100644 --- a/tools/perf/arch/x86/tests/Build +++ b/tools/perf/arch/x86/tests/Build @@ -2,7 +2,6 @@ perf-$(CONFIG_DWARF_UNWIND) +=3D regs_load.o perf-$(CONFIG_DWARF_UNWIND) +=3D dwarf-unwind.o =20 perf-y +=3D arch-tests.o -perf-y +=3D rdpmc.o perf-y +=3D sample-parsing.o perf-$(CONFIG_AUXTRACE) +=3D insn-x86.o intel-pt-pkt-decoder-test.o perf-$(CONFIG_X86_64) +=3D bp-modify.o diff --git a/tools/perf/arch/x86/tests/arch-tests.c b/tools/perf/arch/x86/t= ests/arch-tests.c index 64fb73d14d2f..04018b8aa85b 100644 --- a/tools/perf/arch/x86/tests/arch-tests.c +++ b/tools/perf/arch/x86/tests/arch-tests.c @@ -3,7 +3,6 @@ #include "tests/tests.h" #include "arch-tests.h" =20 -DEFINE_SUITE("x86 rdpmc", rdpmc); #ifdef HAVE_AUXTRACE_SUPPORT DEFINE_SUITE("x86 instruction decoder - new instructions", insn_x86); DEFINE_SUITE("Intel PT packet decoder", intel_pt_pkt_decoder); @@ -14,7 +13,6 @@ DEFINE_SUITE("x86 bp modify", bp_modify); DEFINE_SUITE("x86 Sample parsing", x86_sample_parsing); =20 struct test_suite *arch_tests[] =3D { - &suite__rdpmc, #ifdef HAVE_DWARF_UNWIND_SUPPORT &suite__dwarf_unwind, #endif diff --git a/tools/perf/arch/x86/tests/rdpmc.c b/tools/perf/arch/x86/tests/= rdpmc.c deleted file mode 100644 index 498413ad9c97..000000000000 --- a/tools/perf/arch/x86/tests/rdpmc.c +++ /dev/null @@ -1,182 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "perf-sys.h" -#include "debug.h" -#include "tests/tests.h" -#include "cloexec.h" -#include "event.h" -#include // page_size -#include "arch-tests.h" - -static u64 rdpmc(unsigned int counter) -{ - unsigned int low, high; - - asm volatile("rdpmc" : "=3Da" (low), "=3Dd" (high) : "c" (counter)); - - return low | ((u64)high) << 32; -} - -static u64 rdtsc(void) -{ - unsigned int low, high; - - asm volatile("rdtsc" : "=3Da" (low), "=3Dd" (high)); - - return low | ((u64)high) << 32; -} - -static u64 mmap_read_self(void *addr) -{ - struct perf_event_mmap_page *pc =3D addr; - u32 seq, idx, time_mult =3D 0, time_shift =3D 0; - u64 count, cyc =3D 0, time_offset =3D 0, enabled, running, delta; - - do { - seq =3D pc->lock; - barrier(); - - enabled =3D pc->time_enabled; - running =3D pc->time_running; - - if (enabled !=3D running) { - cyc =3D rdtsc(); - time_mult =3D pc->time_mult; - time_shift =3D pc->time_shift; - time_offset =3D pc->time_offset; - } - - idx =3D pc->index; - count =3D pc->offset; - if (idx) - count +=3D rdpmc(idx - 1); - - barrier(); - } while (pc->lock !=3D seq); - - if (enabled !=3D running) { - u64 quot, rem; - - quot =3D (cyc >> time_shift); - rem =3D cyc & (((u64)1 << time_shift) - 1); - delta =3D time_offset + quot * time_mult + - ((rem * time_mult) >> time_shift); - - enabled +=3D delta; - if (idx) - running +=3D delta; - - quot =3D count / running; - rem =3D count % running; - count =3D quot * enabled + (rem * enabled) / running; - } - - return count; -} - -/* - * If the RDPMC instruction faults then signal this back to the test paren= t task: - */ -static void segfault_handler(int sig __maybe_unused, - siginfo_t *info __maybe_unused, - void *uc __maybe_unused) -{ - exit(-1); -} - -static int __test__rdpmc(void) -{ - volatile int tmp =3D 0; - u64 i, loops =3D 1000; - int n; - int fd; - void *addr; - struct perf_event_attr attr =3D { - .type =3D PERF_TYPE_HARDWARE, - .config =3D PERF_COUNT_HW_INSTRUCTIONS, - .exclude_kernel =3D 1, - }; - u64 delta_sum =3D 0; - struct sigaction sa; - char sbuf[STRERR_BUFSIZE]; - - sigfillset(&sa.sa_mask); - sa.sa_sigaction =3D segfault_handler; - sa.sa_flags =3D 0; - sigaction(SIGSEGV, &sa, NULL); - - fd =3D sys_perf_event_open(&attr, 0, -1, -1, - perf_event_open_cloexec_flag()); - if (fd < 0) { - pr_err("Error: sys_perf_event_open() syscall returned " - "with %d (%s)\n", fd, - str_error_r(errno, sbuf, sizeof(sbuf))); - return -1; - } - - addr =3D mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0); - if (addr =3D=3D (void *)(-1)) { - pr_err("Error: mmap() syscall returned with (%s)\n", - str_error_r(errno, sbuf, sizeof(sbuf))); - goto out_close; - } - - for (n =3D 0; n < 6; n++) { - u64 stamp, now, delta; - - stamp =3D mmap_read_self(addr); - - for (i =3D 0; i < loops; i++) - tmp++; - - now =3D mmap_read_self(addr); - loops *=3D 10; - - delta =3D now - stamp; - pr_debug("%14d: %14Lu\n", n, (long long)delta); - - delta_sum +=3D delta; - } - - munmap(addr, page_size); - pr_debug(" "); -out_close: - close(fd); - - if (!delta_sum) - return -1; - - return 0; -} - -int test__rdpmc(struct test_suite *test __maybe_unused, int subtest __mayb= e_unused) -{ - int status =3D 0; - int wret =3D 0; - int ret; - int pid; - - pid =3D fork(); - if (pid < 0) - return -1; - - if (!pid) { - ret =3D __test__rdpmc(); - - exit(ret); - } - - wret =3D waitpid(pid, &status, 0); - if (wret < 0 || status) - return -1; - - return 0; -} --=20 2.36.1.255.ge46751e96f-goog From nobody Mon Jun 15 10:47:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5AF00C433EF for ; Wed, 8 Jun 2022 22:44:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236888AbiFHWoe (ORCPT ); Wed, 8 Jun 2022 18:44:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236785AbiFHWoX (ORCPT ); Wed, 8 Jun 2022 18:44:23 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 582FE2504E3 for ; Wed, 8 Jun 2022 15:44:18 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e5-20020a255005000000b0065cb3669fe9so18845167ybb.0 for ; Wed, 08 Jun 2022 15:44:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=2geO0ahuljEn77v5yZYAdJcWJ8DuHGsKu15Ptk4z1CU=; b=l5jFMVhRHYNtdJfhQvNcXTPSLsGUyeeftIx9EEF8LyZJstnVOe7pTpdR1efUT7nFfp +uyIF/k/VlGO6TwK70EhNvnpf8zaLXfQr/hFECUmySP7NxbCaJRR9//siudLs5EmGRiV rTt6LaU0zf0wXSptc/7pM5GVsGZlcpQWfE5tzAjOjJZT+MEsuTuu9n4FxN3grDxNY0BT 8LVxfdG0bQR3i86PbSCegT767zZoqGNc5O0mZuziWI4yXpaf8qBBuc1q35Maf0Yk/zbe J/DYLDq2rLzVQGI5Xj9zICvBSgwoMvRexJLYsllbt4Tpus29PqT7ZR5yomFgxamm6g/u n4vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=2geO0ahuljEn77v5yZYAdJcWJ8DuHGsKu15Ptk4z1CU=; b=TkpzssLpv3+Slt303lQL5f3bWB2OVZnh/p9d2RFRcpi1x82jYLqN43rYJyjbBDUO7f Ko1wxDIpKoEu4zTE1BjCv+0l4PHNXkbX/zGZ2kSQwS2mSgt4I6nq0phBixdDo7ESEo+C j89hLK0oaAYRQYY0xhpILMER4sX107mx/4XgCIBvRQEV9daJ5b6kdPYUpUaG9UHuHpXW 5gh7OWt1dlIbLY8EC8zqGEVxras9YPCvKhmkaCNtLc9t+ZtbIW+A4FOFI09q81GucSEK pQse/TTrbFYx9rdHV8Tw4mjJsSr1RcZ4giS6oLWAbVavq6F0o8X9bzkAEHTPjP3Mfxns /Wzg== X-Gm-Message-State: AOAM530j35M073u6LhnNyJzTmfwZKbRiV0KeBxyZCn5SmAycPQKmHNjp TwNX4lyZl2vzbXX7bL+QVm+4uKgPFYnp X-Google-Smtp-Source: ABdhPJwOyTVT6fRTm9r0F+MK1W7bVH/+DpvqtizK5yosDhrXnnn8esrtNJzT6b/akFohsrP2YKqRQOW6xncH X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:de48:5efa:e4a1:2219]) (user=irogers job=sendgmr) by 2002:a05:6902:102e:b0:65d:2286:a3b5 with SMTP id x14-20020a056902102e00b0065d2286a3b5mr37765177ybt.515.1654728257392; Wed, 08 Jun 2022 15:44:17 -0700 (PDT) Date: Wed, 8 Jun 2022 15:43:53 -0700 In-Reply-To: <20220608224353.1176079-1-irogers@google.com> Message-Id: <20220608224353.1176079-5-irogers@google.com> Mime-Version: 1.0 References: <20220608224353.1176079-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH 4/4] perf test: Add user space counter reading tests From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Kajol Jain , Andi Kleen , Adrian Hunter , Anshuman Khandual , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Rob Herring Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" These tests are based on test_stat_user_read in tools/lib/perf/tests/test-evsel.c. The tests are modified to skip if perf_event_open fails or rdpmc isn't supported. Signed-off-by: Ian Rogers --- tools/perf/tests/mmap-basic.c | 128 +++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/tools/perf/tests/mmap-basic.c b/tools/perf/tests/mmap-basic.c index 30bbe144648a..791eebcc5883 100644 --- a/tools/perf/tests/mmap-basic.c +++ b/tools/perf/tests/mmap-basic.c @@ -170,14 +170,140 @@ static int test__basic_mmap(struct test_suite *test = __maybe_unused, int subtest return err; } =20 +static int test_stat_user_read(int event) +{ + struct perf_counts_values counts =3D { .val =3D 0 }; + struct perf_thread_map *threads; + struct perf_evsel *evsel; + struct perf_event_mmap_page *pc; + struct perf_event_attr attr =3D { + .type =3D PERF_TYPE_HARDWARE, + .config =3D event, +#ifdef __aarch64__ + .config1 =3D 0x2, /* Request user access */ +#endif + }; + int err, i, ret =3D TEST_FAIL; + bool opened =3D false, mapped =3D false; + + threads =3D perf_thread_map__new_dummy(); + TEST_ASSERT_VAL("failed to create threads", threads); + + perf_thread_map__set_pid(threads, 0, 0); + + evsel =3D perf_evsel__new(&attr); + TEST_ASSERT_VAL("failed to create evsel", evsel); + + err =3D perf_evsel__open(evsel, NULL, threads); + if (err) { + pr_err("failed to open evsel: %s\n", strerror(-err)); + if (err =3D=3D -EACCES) + ret =3D TEST_SKIP; + goto out; + } + opened =3D true; + + err =3D perf_evsel__mmap(evsel, 0); + if (err) { + pr_err("failed to mmap evsel: %s\n", strerror(-err)); + goto out; + } + mapped =3D true; + + pc =3D perf_evsel__mmap_base(evsel, 0, 0); + if (!pc) { + pr_err("failed to get mmapped address\n"); + goto out; + } + + if (!pc->cap_user_rdpmc || !pc->index) { + pr_err("userspace counter access not %s\n", + !pc->cap_user_rdpmc ? "supported" : "enabled"); + ret =3D TEST_SKIP; + goto out; + } + if (pc->pmc_width < 32) { + pr_err("userspace counter width not set (%d)\n", pc->pmc_width); + goto out; + } + + perf_evsel__read(evsel, 0, 0, &counts); + if (counts.val =3D=3D 0) { + pr_err("failed to read value for evsel\n"); + goto out; + } + + for (i =3D 0; i < 5; i++) { + volatile int count =3D 0x10000 << i; + __u64 start, end, last =3D 0; + + pr_debug("\tloop =3D %u, ", count); + + perf_evsel__read(evsel, 0, 0, &counts); + start =3D counts.val; + + while (count--) ; + + perf_evsel__read(evsel, 0, 0, &counts); + end =3D counts.val; + + if ((end - start) < last) { + pr_err("invalid counter data: end=3D%llu start=3D%llu last=3D %llu\n", + end, start, last); + goto out; + } + last =3D end - start; + pr_debug("count =3D %llu\n", end - start); + } + ret =3D TEST_OK; + +out: + if (mapped) + perf_evsel__munmap(evsel); + if (opened) + perf_evsel__close(evsel); + perf_evsel__delete(evsel); + + perf_thread_map__put(threads); + return ret; +} + +static int test__mmap_user_read_instr(struct test_suite *test __maybe_unus= ed, + int subtest __maybe_unused) +{ + return test_stat_user_read(PERF_COUNT_HW_INSTRUCTIONS); +} + +static int test__mmap_user_read_cycles(struct test_suite *test __maybe_unu= sed, + int subtest __maybe_unused) +{ + return test_stat_user_read(PERF_COUNT_HW_CPU_CYCLES); +} + static struct test_case tests__basic_mmap[] =3D { TEST_CASE_REASON("Read samples using the mmap interface", basic_mmap, "permissions"), + TEST_CASE_REASON("User space counter reading of instructions", + mmap_user_read_instr, +#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) + "permissions" +#else + "unsupported" +#endif + ), + TEST_CASE_REASON("User space counter reading of cycles", + mmap_user_read_cycles, +#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) + "permissions" +#else + "unsupported" +#endif + ), { .name =3D NULL, } }; =20 struct test_suite suite__basic_mmap =3D { - .desc =3D "Read samples using the mmap interface", + .desc =3D "mmap interface tests", .test_cases =3D tests__basic_mmap, }; --=20 2.36.1.255.ge46751e96f-goog