From nobody Thu May 2 01:11:40 2024 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 43C5DC04A95 for ; Wed, 28 Sep 2022 18:41:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233707AbiI1SlB (ORCPT ); Wed, 28 Sep 2022 14:41:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231652AbiI1Sky (ORCPT ); Wed, 28 Sep 2022 14:40:54 -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 5EF9872B42 for ; Wed, 28 Sep 2022 11:40:53 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-35076702654so127688977b3.17 for ; Wed, 28 Sep 2022 11:40:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date; bh=3935bCieyFmPbWCFJjhpDhrqHj3m5qA7eEIiW2Mb31M=; b=YqI1WESsX1JN1SX3EDdN3t0+z5lQnLgO9roDEXa88xtna+McONc24ekfKMWcVHWJ5k 8Y3BBVp6ZbMUC36mkMgaACQm8lPSjK3jEBA5eV+NVzee9UrbJYCZLEW7gEyLiMhq+KKD k+OU2BaH5ZeeLJITqT2dD5Wv5mv+bMGOuRAoDMdTbV8pghU+FM8NRdRbnkp3WLs5sYKp Ap+lhwb0ObAUkLzOP3wjhV0hUSSNaU8ft7dQrcxSlvMfC64iyuBsYOafnO6xyjZ+YK/y iKEzhpb5PWnlchhpVYALvW4zSxN4DpUTWFhMXg3In4DaE3tBOcC9926kfa/krO/KW+bh mn/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=3935bCieyFmPbWCFJjhpDhrqHj3m5qA7eEIiW2Mb31M=; b=O/M8n+K4fmJd74998RvPO3rb4ABpOGX00ixn59a6hofVG6XdZjRRPrqaS9OdwvavWw B7Fb71kmGqFOLfE8n9Tl4DKbEZ3yyP8M0aETZXSmltIZbKk5dDdlJafSFgMbzJuh8FhQ llxuyCYOTcmDTrCwZp5NxxqRzXINBNii48JfHhUc0znqk8+2L83A8v8Ryvj3dgwgPWXZ DA4fGlMczWiANhW8BD4MMpd9gpNeb8kPefZzp+uJxQofOFGUpouEiXLBGucca0/pTxJF GMk5IxyS6nnTE8WpSQyb+/LPCE398/c2EM4Wm8duxmJWAOJFpoG6fbqsimcoDZnv6R52 6XBQ== X-Gm-Message-State: ACrzQf3tLmC4x8KZzggtweK1cZ3QSWYOn8wJ6O5yyU24XedU6aPZm71g s6PSf7d61e03sQLIjysqQdYSlQ2IDjGAEs9IZkm1VGVn1QE7fFPMYb6QUpCZ8ATYzx9HhitW8yx xgpnX29mdDbFKbMDLt3SI0mldj8tSFrVUcbo2qg1kuvYfJ9GQO/aN1X4I/WM7j2RMGVxtAOUq X-Google-Smtp-Source: AMsMyM7POtnRycLOa3IZT0STyJLWiXB6VPZUHzYSrRTMaAKl4fQWbR8UYSEFE36mfrgoFLvnsnoHAhekLHl3 X-Received: from uluru3.svl.corp.google.com ([2620:15c:2d4:203:d508:baac:7b11:f912]) (user=eranian job=sendgmr) by 2002:a0d:f201:0:b0:335:4933:6683 with SMTP id b1-20020a0df201000000b0033549336683mr30829921ywf.23.1664390452599; Wed, 28 Sep 2022 11:40:52 -0700 (PDT) Date: Wed, 28 Sep 2022 11:40:42 -0700 In-Reply-To: <20220928184043.408364-1-eranian@google.com> Message-Id: <20220928184043.408364-2-eranian@google.com> Mime-Version: 1.0 References: <20220928184043.408364-1-eranian@google.com> X-Mailer: git-send-email 2.37.3.998.g577e59143f-goog Subject: [PATCH 1/2] perf/x86/utils: fix uninitialized var in get_branch_type() From: Stephane Eranian To: linux-kernel@vger.kernel.org Cc: peterz@infradead.org, sandipan.das@amd.com, ananth.narayan@amd.com, ravi.bangoria@amd.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" offset is passed as a pointer and on certain call path is not set by the function. If the caller does not re-initialize offset between calls, value could be inherited between calls. Prevent this by initializing offset on ea= ch call. This impacts the code in amd_pmu_lbr_filter() which does for(i=3D0; ...) { ret =3D get_branch_type_fused(..., &offset); if (offset) lbr_entries[i].from +=3D offset; } Signed-off-by: Stephane Eranian Reviewed-by: Sandipan Das --- arch/x86/events/utils.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/events/utils.c b/arch/x86/events/utils.c index 5f5617afde79..76b1f8bb0fd5 100644 --- a/arch/x86/events/utils.c +++ b/arch/x86/events/utils.c @@ -94,6 +94,10 @@ static int get_branch_type(unsigned long from, unsigned = long to, int abort, u8 buf[MAX_INSN_SIZE]; int is64 =3D 0; =20 + /* make sure we initialize offset */ + if (offset) + *offset =3D 0; + to_plm =3D kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER; from_plm =3D kernel_ip(from) ? X86_BR_KERNEL : X86_BR_USER; =20 --=20 2.37.3.998.g577e59143f-goog From nobody Thu May 2 01:11:40 2024 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 97602C32771 for ; Wed, 28 Sep 2022 18:41:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233762AbiI1SlH (ORCPT ); Wed, 28 Sep 2022 14:41:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232896AbiI1Sk5 (ORCPT ); Wed, 28 Sep 2022 14:40:57 -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 D9D4A7754C for ; Wed, 28 Sep 2022 11:40:55 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-349f88710b2so129334357b3.20 for ; Wed, 28 Sep 2022 11:40:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date; bh=AKTQvHiwvoKSKsVkI66/u6S6G2oPSkqTUWLlsYVxWU0=; b=rdtDSC292qeg8KFcuHwoId2wBao8CuTmZj9xs7RX4Q3wj8/glfdhitzz6zvo6B1bmH XyWsGaAykVVX5OymAj21IbfDDexT8Elg8Rk76O08sOSSwg+4FgUkHg+DO9kiDQZVJ8yy IWmpxW6Mi8rSIndlM1NBxJwr8+3GK13mxJAsufbgQf0uIG9YPPp+URrFCy6/+lQk+Bf5 zMEpv9eGZKGd4TpXPJ+1dDCcMrlmsP0ym8XAYN/6+Zr1oe1I423zi7GjD1FBkgTgLl/t qy5nacDhDUs7nx5eGGMV+8uRzHvnXoNAuY6KBTCMvXvHzOsjP0NZYECMLl2DFta4fHPs Y8pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=AKTQvHiwvoKSKsVkI66/u6S6G2oPSkqTUWLlsYVxWU0=; b=ARxQZr0OZEjNB1fJRi+uio6gR5Z09eNWHBFFTk6LoQdVgJ8A1iNRgc3X3HPwg2kkCR TCVijc3V80OWw2PceK5cCjYGvT7c5ug2xIEqrrOtbY69Jk7CmdfVdCwVUPwxJSwOfXHG plqyJ+AJ4dGb2+b7S/Oe4875xKISUlMvMrBtLopCYsowj2y3acxK1WrRlpHnmoFL+r96 2BOCoHffDI2/IDzYCgYAwOhJib1os4BC9ZJOSAxLHZ1yRORsX1/WptXb0U+pZs6PjRss zWIYRlJJ97fm20J3ZCAEyJ03OMtJQ8L5nQXZKF9ftHAcyUFbBBr03NMQ6mqVMfcpPuYG vz/w== X-Gm-Message-State: ACrzQf38Kr2kbMDsyvjmDv4gcZMF6olSp+K1KUT/WugEb0lLpNKmsrvI JqqYlvdVucsv+t2O74mf1OATKkEtJHaQyfHdU0DeS6D/FrV3A/ET6YhFkhBnFoFenWJk/DkzsUF m2GUf//BkMKBcKaJnoyZv2PtbnEqrCjAOU4BvXryqs8fgrNloSt1BKJhb0Fo2qwbJ/ORweatQ X-Google-Smtp-Source: AMsMyM5Nj3EJMs43dt6DsRGMQWeth5T6Lyo2kGLI+4iqa/b8L0SczxhxH0bfI2CpY24VU1Y7Stl64s03mFsN X-Received: from uluru3.svl.corp.google.com ([2620:15c:2d4:203:d508:baac:7b11:f912]) (user=eranian job=sendgmr) by 2002:a25:3b03:0:b0:696:5b34:39eb with SMTP id i3-20020a253b03000000b006965b3439ebmr32511362yba.497.1664390455108; Wed, 28 Sep 2022 11:40:55 -0700 (PDT) Date: Wed, 28 Sep 2022 11:40:43 -0700 In-Reply-To: <20220928184043.408364-1-eranian@google.com> Message-Id: <20220928184043.408364-3-eranian@google.com> Mime-Version: 1.0 References: <20220928184043.408364-1-eranian@google.com> X-Mailer: git-send-email 2.37.3.998.g577e59143f-goog Subject: [PATCH 2/2] perf/x86/amd/lbr: adjust LBR regardless of filtering From: Stephane Eranian To: linux-kernel@vger.kernel.org Cc: peterz@infradead.org, sandipan.das@amd.com, ananth.narayan@amd.com, ravi.bangoria@amd.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In case of fused compare and taken branch instructions, the AMD LBR points = to the compare instruction instead of the branch. Users of LBR usually expects the from address to point to a branch instruction. The kernel has code to adjust the from address via get_branch_type_fused(). However this correction is only applied when a branch filter is applied. That means that if no filter is present, the quality of the data is lower. Fix the problem by applying the adjustment regardless of the filter setting, bringing the AMD LBR to the same level as other LBR implementations. Signed-off-by: Stephane Eranian Reviewed-by: Sandipan Das --- arch/x86/events/amd/lbr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/x86/events/amd/lbr.c b/arch/x86/events/amd/lbr.c index 2e1c1573efe7..38a75216c12c 100644 --- a/arch/x86/events/amd/lbr.c +++ b/arch/x86/events/amd/lbr.c @@ -99,12 +99,13 @@ static void amd_pmu_lbr_filter(void) struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); int br_sel =3D cpuc->br_sel, offset, type, i, j; bool compress =3D false; + bool fused_only =3D false; u64 from, to; =20 /* If sampling all branches, there is nothing to filter */ if (((br_sel & X86_BR_ALL) =3D=3D X86_BR_ALL) && ((br_sel & X86_BR_TYPE_SAVE) !=3D X86_BR_TYPE_SAVE)) - return; + fused_only =3D true; =20 for (i =3D 0; i < cpuc->lbr_stack.nr; i++) { from =3D cpuc->lbr_entries[i].from; @@ -116,8 +117,11 @@ static void amd_pmu_lbr_filter(void) * fusion where it points to an instruction preceding the * actual branch */ - if (offset) + if (offset) { cpuc->lbr_entries[i].from +=3D offset; + if (fused_only) + continue; + } =20 /* If type does not correspond, then discard */ if (type =3D=3D X86_BR_NONE || (br_sel & type) !=3D type) { --=20 2.37.3.998.g577e59143f-goog