From nobody Mon Apr 27 23:53:31 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 AC5AFC43334 for ; Thu, 9 Jun 2022 05:24:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238322AbiFIFYN (ORCPT ); Thu, 9 Jun 2022 01:24:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238273AbiFIFYH (ORCPT ); Thu, 9 Jun 2022 01:24:07 -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 CA67225E9D for ; Wed, 8 Jun 2022 22:24:04 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 10-20020a250d0a000000b0065ca0e7be90so19362378ybn.17 for ; Wed, 08 Jun 2022 22:24:04 -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=m+GH7afRGBYGWqJBmM4w4QGt8MCZhwn+CP1UOE+F21RUMbBBtczrX4dpaH1Th5zjis 5S6qp7GTToPhKiuLySTPJzaCFI1KR6/8dXZe13wiB6LmZ/xe0yf8PvdBPr+Ok+0x2GdZ F9KeQSU1oKIhH6z3bkKeT54UJNjFiHmG15KjOj1sU8IbSC7CS2pxTYRshhKAblVtZfuH jhC7NKiOrQp6DtxkRsa8jz/FlBvkRtC8dtfKzI4z4YUZ/Tz8davC212ATw4ccRhYXd2o aNBeSMqX1B6ac+B4AKSv+Ftl549whZaXk6ea5N+/HveYjTol1iYFewxASPZxNJt0iEuT oF9w== 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=bRAw+xrHubUrGqIiKQtGDDOwakc+WcWMgCeirimfRpe0bkH6rXpg4FHOZBXZdjcCFh HVKf37RL4CeqqEIy6RHu1ferySjShFjZ+W46/wfYBeSBztcO4P/7XHHiIeziji6vnoNX qsGKKq7/8E6qd/6WVh6L4S3qUq4Ruebnp6VocBocVzc+rbA3InCnUcxt4HlH/UO2Uxq4 6vSOCP4cWfaNv9Rc3WjR4UUJr4MyTnFT51Zp7Y+kTQuwVO9cPxURXaYhWUcnm4iHYawd VnFAZkJ6hxcUuTeQ4srA9z7jyuj7PfSqU7G2UtocOzy2MDrVhZ6VlHjZKn9pDJLKqLdi 1j1Q== X-Gm-Message-State: AOAM531Oo1OnSjwdqM4BKvLiIjsWSrDGqqVyMAHG08crCx7QoUtUFyF/ 7dpKsY3lXHviTFzeBINPotNfXLBmy3xH X-Google-Smtp-Source: ABdhPJzR1whIf7j55WQjVp8vOL2cQbpNCuyhPsZMw7XC5LhktLRlst3wiJkrl8JtpqpEzCumqhY37TGcvUF+ X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:ae20:7578:fb21:976b]) (user=irogers job=sendgmr) by 2002:a81:1414:0:b0:30c:1c83:7702 with SMTP id 20-20020a811414000000b0030c1c837702mr41227650ywu.474.1654752244006; Wed, 08 Jun 2022 22:24:04 -0700 (PDT) Date: Wed, 8 Jun 2022 22:23:52 -0700 In-Reply-To: <20220609052355.1300162-1-irogers@google.com> Message-Id: <20220609052355.1300162-2-irogers@google.com> Mime-Version: 1.0 References: <20220609052355.1300162-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH v2 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 Reviewed-By: Kajol Jain --- 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 Apr 27 23:53:31 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 DB835C433EF for ; Thu, 9 Jun 2022 05:24:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238273AbiFIFYS (ORCPT ); Thu, 9 Jun 2022 01:24:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238277AbiFIFYK (ORCPT ); Thu, 9 Jun 2022 01:24:10 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 418FDE005 for ; Wed, 8 Jun 2022 22:24:07 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2fb7cb07885so193781547b3.23 for ; Wed, 08 Jun 2022 22:24:07 -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=c+zLR0wVXL9kWNiEJ57kdQibv8Zr0VL9IAwXHVwAVZyRLzSx4FHmS08R8X92ZplbmI JwWF11N1zwiHTbW9SmZ4vb/QH4ZKShWHaH/YgSkPyTt2dpuEuLZxGAC4HzpAJIlK8g1F vzBlyDuOohjGG5ZK/cNkxV6wGysCTSaUjckk9Wmp8VXiGLRSNOZYW1/AXTWBn6yZGFc4 2Rib8pTuoWepYbhadEw+NZZlEayCVyc+nwqJ/flEGluYAL7WbBTYW1f+86at6Cy47Q5n nyKHKvQLgkVDkXo+19wYo+iKLuU7doOb4y0GRrkQCrmqdPP/G9OITHbvlijbbos7QKwR xNSg== 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=DSINLy9J6yY/xHJrNxmlAjJGgBmMnADozQmH7jG+Rxjsiy5N66Uhgn18wfs6Tm+969 mEz7kXbNxkmC23lQwDmOnoH/akOyV5bjlYksk5GOpAu59mhMr8nq1x1V535+0YsxoVd5 SMLbwLSSTFMYnJt6aQBD98YzPEziqE00Y0ROAdkCmpy0Xar3man2YbyBUKyV/6NrhXGe xBkM8rzDGjC8WEul7rze4tVDV9ZJeG3h4DBHNruWpt0BFKFaj7C1UOWkKLVwKjgA1tr6 NwZtKhIfbkpPECBCTrlwhgloDlUfAdJPiBgMAxMKnJ2N+yyAdTNQbZ4j65IRtkHxH9+Z oS8w== X-Gm-Message-State: AOAM531AmNRoVf35WTeVix+I54K2RaicaYsZGFAWwrueqbI0HFyxuBS6 u7vAH362nPByCzpdwVE+A3qxGVCppznu X-Google-Smtp-Source: ABdhPJyNkg3gb8kG/ZewVhB0Qss0toz5dRxqodcpwXpCLk+fIrqos5bELAzypdRYZvj9VrwV05oVuaVHTLzK X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:ae20:7578:fb21:976b]) (user=irogers job=sendgmr) by 2002:a25:6583:0:b0:660:3a01:84f7 with SMTP id z125-20020a256583000000b006603a0184f7mr35986524ybb.130.1654752246414; Wed, 08 Jun 2022 22:24:06 -0700 (PDT) Date: Wed, 8 Jun 2022 22:23:53 -0700 In-Reply-To: <20220609052355.1300162-1-irogers@google.com> Message-Id: <20220609052355.1300162-3-irogers@google.com> Mime-Version: 1.0 References: <20220609052355.1300162-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH v2 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 Apr 27 23:53:31 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 9BF7BC433EF for ; Thu, 9 Jun 2022 05:24:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237842AbiFIFYZ (ORCPT ); Thu, 9 Jun 2022 01:24:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238298AbiFIFYL (ORCPT ); Thu, 9 Jun 2022 01:24:11 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78B91BC0B for ; Wed, 8 Jun 2022 22:24:09 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-30c99cb3d4dso192333557b3.6 for ; Wed, 08 Jun 2022 22:24:09 -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=b++yLK5vVsGdFBaIM6rGmH/DOg+gORi2ZxhmP1SeobQ=; b=VTJ+0fyg8SyDnE/rh6wYotnT1dJLOLfAWjVueAJnBO7Pg1CCI79URKig6R+fudxzzw lX8XNEAoTwGOGZaErOYtGF05HIJHXyrtM0zM4i9ERlzYXfQywqPQaqO5smKtCouZkn38 kCA89vuLTh+HTvGN1SQMfVLI56/4kOqzBuzD3Smr3tgNJxtU9NL1U5kWiJzPD0ZW/v+e Uy50TJCsxbDvWlZ2D9bdGq8F8D6qK2BTrqBwk17zW0Y6SPa4sEwprc84ElEgv3uacq8v dYUbJlDvi+ChL64kTk0jOfBx2FGoUhIAp8QH7uFPE/b34DHSbGb2OfijVRynEmjj0Me4 tVmg== 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=b++yLK5vVsGdFBaIM6rGmH/DOg+gORi2ZxhmP1SeobQ=; b=JvIMxKrtXz/B/3iQkCxa3ImfqVggGjGLeJXYmd5RH7j2yrnXVzD5XE9G0CF57cAwln +K2E5dCZ7q0OVy0kmxYuzulezR5mlqF3IeGhSex5lfwHRyijrwn5qZ8n0Vh1C3bDvNjb Bik5wQ2NiPqqlpjuf22AWYacv/wVptjh8AA8DQywi0Rl6MV5Uns/jI8GabP6XkuRgET2 OR1VyMqagBuJyOwo/3FHJxujlCZwi/NOj9vMrfzfppU/EPQ9AxqAf6c/aCQS7zBsTm2n Mcd+SMqF5OrIKDrKmrsGPKOyI+jQ/EPKQVkbt9pLpIXFMivqdcgfJvWovZYJFyEohrH1 a5Fg== X-Gm-Message-State: AOAM532QnP8A6i1lvZTZlql+qOP4q2nVaWo1fMWfVTLNtMPjgLzRHuPo MMXHoloAx3G+qyplsy6zafVDx/eK6MpE X-Google-Smtp-Source: ABdhPJx6QBJolbnRjdss2Gnt4IA9EsCqsIcmy/TaARcq2OcOjXwoJn8PWpLwgIhp7NBh24TcE27EfhhuH2hW X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:ae20:7578:fb21:976b]) (user=irogers job=sendgmr) by 2002:a5b:c08:0:b0:664:2461:fb40 with SMTP id f8-20020a5b0c08000000b006642461fb40mr3447551ybq.215.1654752248716; Wed, 08 Jun 2022 22:24:08 -0700 (PDT) Date: Wed, 8 Jun 2022 22:23:54 -0700 In-Reply-To: <20220609052355.1300162-1-irogers@google.com> Message-Id: <20220609052355.1300162-4-irogers@google.com> Mime-Version: 1.0 References: <20220609052355.1300162-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH v2 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. Acked-by: Rob Herring Signed-off-by: Ian Rogers --- 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 Apr 27 23:53:31 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 A616EC433EF for ; Thu, 9 Jun 2022 05:24:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234589AbiFIFYb (ORCPT ); Thu, 9 Jun 2022 01:24:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238313AbiFIFYN (ORCPT ); Thu, 9 Jun 2022 01:24:13 -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 F23383BFB0 for ; Wed, 8 Jun 2022 22:24:11 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e5-20020a255005000000b0065cb3669fe9so19451357ybb.0 for ; Wed, 08 Jun 2022 22:24:11 -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=lb9q5Yv/NRSmWQ/bYz01a/1C1DKx+kQvVtIfEuSv0m8=; b=qaF4ArVQ5Ijgk1R5sedAyMQtN/PHxBZPjddPKT5shPniD6r9GsaYHQBs41nCGkkNuL SHeGn0BJokgDCtz1HiabL50GLDN9M66oSLJ0Cptw0pKhtcRozR9pcq5NlwCaipB6PQI1 BN3aPtREHhon32R+AKAjbVOY6MyuHU2QlgePTzJa43GC3ZOxSSF5w7soyhEImLmWxlKz 4vgCgVz7oVxq9kWTqcZKuaiSB9E22ifRjJjq/ES84kRBpS47/mCKRN1f+F8kKz+ef/r2 H5/m76I5fY5vbtPh82w0aCnWJ2wfsWWIoSzIa+do4QPri0HuWJK4ky021voryleXLPTP xEFQ== 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=lb9q5Yv/NRSmWQ/bYz01a/1C1DKx+kQvVtIfEuSv0m8=; b=ZSGeDqYoPoqESQgCJn+PnsqyLZaYObiduhyTUeISnsib/p05K7cS5jSJPMOHtnlKf1 ujALF422VirE0NmXDet8id375bM3jubAAqCdwGbNJDStRGgOBWpvZVviCiNg1V775BNt TTqE8ZARwmnltTngv2BkloNJj0cdIc2K3u52BzAolwV66btKPEGNzr+bqprUQpUHDGzO XittliH+wBBk4e2LeM5Dx2Y2g8u05LDaONnwKj9MVWRZ0unmDBbmwuMU9lOtJkzLzsYp IqjpyDJmR2Xwo5hOpiRxndA+Fh9Qr5dUFyM3HW9A04kJu2Un3zb5uACBxzXuw8MzzX+6 ZeFA== X-Gm-Message-State: AOAM532Osr2am1ifETPZ/coL/G5GYyu7eb8kHSion+h6IbXmzVs87Cnx LUvncrywG3DCVFPEMqA6Spuy+IUuWcsA X-Google-Smtp-Source: ABdhPJyAqMlhCuxzMnoqvzbXCEIxUYUC+UEZoPwaqzgyUw3HKZqt3ckccIlBiudFoF39cskbid5mabc+fva+ X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:ae20:7578:fb21:976b]) (user=irogers job=sendgmr) by 2002:a0d:f443:0:b0:300:4795:fc0f with SMTP id d64-20020a0df443000000b003004795fc0fmr40154662ywf.354.1654752251112; Wed, 08 Jun 2022 22:24:11 -0700 (PDT) Date: Wed, 8 Jun 2022 22:23:55 -0700 In-Reply-To: <20220609052355.1300162-1-irogers@google.com> Message-Id: <20220609052355.1300162-5-irogers@google.com> Mime-Version: 1.0 References: <20220609052355.1300162-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH v2 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 Reviewed-By: Kajol Jain --- tools/perf/tests/mmap-basic.c | 127 +++++++++++++++++++++++++++++++++- 1 file changed, 126 insertions(+), 1 deletion(-) diff --git a/tools/perf/tests/mmap-basic.c b/tools/perf/tests/mmap-basic.c index 30bbe144648a..dfb6173b2a82 100644 --- a/tools/perf/tests/mmap-basic.c +++ b/tools/perf/tests/mmap-basic.c @@ -170,14 +170,139 @@ 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)); + 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