Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp782352lqz; Sat, 30 Mar 2024 21:20:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXunG9wHG0CCH/8cv6ktmDs7UTdh3kMqu87+qvogQhWcLrB48Mcsc5Ndh5oBsRl5YeoQHibO2PuwLDJcPmw6OyJ9DyOO88Xd+Wv5q/sbw== X-Google-Smtp-Source: AGHT+IGqrtkP8vmIBUq+KzubSsRRMCTilRJ2H18sxso8XICc8FB+UqPw7tRAJObeoFzpXlEFI9pa X-Received: by 2002:a05:6e02:220e:b0:368:96f0:6f0f with SMTP id j14-20020a056e02220e00b0036896f06f0fmr7302499ilf.27.1711858800290; Sat, 30 Mar 2024 21:20:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711858800; cv=pass; d=google.com; s=arc-20160816; b=D19xAes4h54qdUx5dR8ZjFBS1e5BFxIBne+VISmx5/+yidfBawfg5AISWI+WQePCjb hfTFpiLCo/cuNE88r2iTA30//pIXczGAlyA6i/O0mH3G3LkcZtPgNJpx+pCkm+XMAccI S+RCCjwDis9NhxRyRn0+ePxhwXfEqOMpWpAtKoC+OnDcc0gSIEWagZCQIErOHSqtJxnM I6jP/VLtcdkyGLuyQrIVOEg21GqV1a9dbCFhTyFA4T0KZ8h0+0/oiUi1AF81Ja8jS7x0 d4WDDnTW7wP6EhrX7B3IEfGU+ElTBh1OPCZ2KaFWwbpWjo/7A0Vnf4E9PyH2HCId0p/P zlvQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=1orIZKmLyIwssxPt6lTijAU7rzS9jJY0ET5qPXFy7CI=; fh=fp2HDdlH6q5edGh4HXhNtG/K0put97Oj8J18fF96usE=; b=Bf42qVLqW1R5Qd3jXmXhdRJeDN6qFylO+Tp1L2FDYkueYfGzZnvSOIOoc2okGWV4P2 P9903cnLPe6eH00T1dZJ9djO3EiNFe2ROy6/hBpD32yfFjROY9W3TcOhbJMRjEC82g4y S4WWXIvN07DWubBrnHCDFi1xVgaJCyjvnwBy+YMXPKF3hrQwXNTy53JBkPokA/pkRXi7 Pk32W/Q1brpEwCOB4o2Zcg6gSU5BXdFgZkAyryxXPXr0mT9pfj486M226ms6WS6V1ULJ u4uZeaVshvcSaSVR06Fx6hg2XoWsb4/n7meBkyxF1KO0/1TuNtFCVhbe0mHv2qZHWT2D DeRA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=P16rkTZ2; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-125955-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125955-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id r28-20020a635d1c000000b005cfda2f8f08si6691948pgb.452.2024.03.30.21.19.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Mar 2024 21:20:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-125955-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=P16rkTZ2; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-125955-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125955-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 30189B2190B for ; Sun, 31 Mar 2024 04:19:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 58EEFB666; Sun, 31 Mar 2024 04:18:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="P16rkTZ2" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D80CB660; Sun, 31 Mar 2024 04:18:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711858727; cv=none; b=dvkhbQAvyv77adWYIuNAR5Sc/SQ0UsO+w2xMCI8ufqaF1QWN4qbP2tB8XJ1WUJXZfhnUTu99O6SDKYwquqqY5RBKTz5sF4ypzbiSoJ6ApXiXyMmhi8esR3Ae9NQQhgdI8tbROh5BgolBheDvG/c+4T74Bs4+ni1FfiDymJwkNt8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711858727; c=relaxed/simple; bh=CIe5yz2IxljzMvNVJnEoy0imMXCwroO7kFYa/Os7/jc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=auBlE4xvH+0H8zsEj+g2F8Xl9ofRHnyrtoQNiqtIq22IjqoXG0JIBagsm4qGewhiDHe5U5dVcXFjiUE6ELLvTMXf94DGgIAMKSMEJ1kzfZqOORa+EkpD92ZHEQF/1f7LpiAc2gWdTphVtfG/CFWQnoyToYb6N6cdG4+FSRit7Jc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=P16rkTZ2; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D5145C433C7; Sun, 31 Mar 2024 04:18:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711858727; bh=CIe5yz2IxljzMvNVJnEoy0imMXCwroO7kFYa/Os7/jc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P16rkTZ2Mne5K7Dx32E/+4stuEGOpdhIvaMl7dCwJU/EfL7W3cgksvqE4Yo2P4+L5 ZTv1MakGto0TIcMHJUfSMtEZ7jsVTTJkcrv/FnBDp0XqHLaRispjK0+WHTOHqPVy0s NLb943KQT1Gp1sOuZv+P0d/xrBWg/kc3pZFOQUqqHQVBcrxHB+kwehLxB0PPyEke/s TCeNO1nbHF13xtQTrkr5Yy1zVRZ6OVP8APp2qm+X4YmiXq9JG1aW8fFOAnEZl8MrBc EfgdDXJwetAjPnVs5xakpOD4HEdGkYs/q0YiE5WTNW4lN0EoS7DC/D81XPo6WpvEbr Qj+75HLH6KAkg== From: Andrii Nakryiko To: x86@kernel.org, peterz@infradead.org, mingo@redhat.com, tglx@linutronix.de Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, jolsa@kernel.org, song@kernel.org, kernel-team@meta.com, Andrii Nakryiko , Sandipan Das Subject: [PATCH v4 3/4] perf/x86/amd: support capturing LBR from software events Date: Sat, 30 Mar 2024 21:18:29 -0700 Message-ID: <20240331041830.2806741-4-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240331041830.2806741-1-andrii@kernel.org> References: <20240331041830.2806741-1-andrii@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Upstream commit c22ac2a3d4bd ("perf: Enable branch record for software events") added ability to capture LBR (Last Branch Records) on Intel CPUs from inside BPF program at pretty much any arbitrary point. This is extremely useful capability that allows to figure out otherwise hard to debug problems, because LBR is now available based on some application-defined conditions, not just hardware-supported events. retsnoop ([0]) is one such tool that takes a huge advantage of this functionality and has proved to be an extremely useful tool in practice. Now, AMD Zen4 CPUs got support for similar LBR functionality, but necessary wiring inside the kernel is not yet setup. This patch seeks to rectify this and follows a similar approach to the original patch for Intel CPUs. We implement an AMD-specific callback set to be called through perf_snapshot_branch_stack static call. Previous preparatory patches ensured that amd_pmu_core_disable_all() and __amd_pmu_lbr_disable() will be completely inlined and will have no branches, so LBR snapshot contamination will be minimized. This was tested on AMD Bergamo CPU and worked well when utilized from the aforementioned retsnoop tool. [0] https://github.com/anakryiko/retsnoop Reviewed-by: Sandipan Das Signed-off-by: Andrii Nakryiko --- arch/x86/events/amd/core.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c index c5bcbc87d057..ed53ce091664 100644 --- a/arch/x86/events/amd/core.c +++ b/arch/x86/events/amd/core.c @@ -878,6 +878,37 @@ static int amd_pmu_handle_irq(struct pt_regs *regs) return amd_pmu_adjust_nmi_window(handled); } +/* + * AMD-specific callback invoked through perf_snapshot_branch_stack static + * call, defined in include/linux/perf_event.h. See its definition for API + * details. It's up to caller to provide enough space in *entries* to fit all + * LBR records, otherwise returned result will be truncated to *cnt* entries. + */ +static int amd_pmu_v2_snapshot_branch_stack(struct perf_branch_entry *entries, unsigned int cnt) +{ + struct cpu_hw_events *cpuc; + unsigned long flags; + + /* + * The sequence of steps to freeze LBR should be completely inlined + * and contain no branches to minimize contamination of LBR snapshot + */ + local_irq_save(flags); + amd_pmu_core_disable_all(); + __amd_pmu_lbr_disable(); + + cpuc = this_cpu_ptr(&cpu_hw_events); + + amd_pmu_lbr_read(); + cnt = min(cnt, x86_pmu.lbr_nr); + memcpy(entries, cpuc->lbr_entries, sizeof(struct perf_branch_entry) * cnt); + + amd_pmu_v2_enable_all(0); + local_irq_restore(flags); + + return cnt; +} + static int amd_pmu_v2_handle_irq(struct pt_regs *regs) { struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); @@ -1414,6 +1445,10 @@ static int __init amd_core_pmu_init(void) static_call_update(amd_pmu_branch_reset, amd_pmu_lbr_reset); static_call_update(amd_pmu_branch_add, amd_pmu_lbr_add); static_call_update(amd_pmu_branch_del, amd_pmu_lbr_del); + + /* Only support branch_stack snapshot on perfmon v2 */ + if (x86_pmu.handle_irq == amd_pmu_v2_handle_irq) + static_call_update(perf_snapshot_branch_stack, amd_pmu_v2_snapshot_branch_stack); } else if (!amd_brs_init()) { /* * BRS requires special event constraints and flushing on ctxsw. -- 2.43.0