Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp433301lqh; Thu, 28 Mar 2024 06:34:33 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX7MaGOI5UHIMXUSdXFmcwvT68hH1we2vXkx0EByDrIYdGLIB8NmQPlicg5ULHMw4/Q29rBr8YTwA/obb58xwkWDUphUWMs6I3ipgH5/Q== X-Google-Smtp-Source: AGHT+IHEOfB9Wlsl4Ezltv38tGoAfOMRhL+rU7TWBE8FU5p8T03JXHNKb0DGQxgK5BTr9X+tR/EU X-Received: by 2002:a17:906:cd1f:b0:a4e:702:cdda with SMTP id oz31-20020a170906cd1f00b00a4e0702cddamr1800875ejb.66.1711632873084; Thu, 28 Mar 2024 06:34:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711632873; cv=pass; d=google.com; s=arc-20160816; b=iUVGByo7H9TNsxAAzUVkRpTmUPKh6odAxkLRbcyvMT/rMI/mvAU0MJ/9f+L5KDgICa XphqsOFzPKlXmIo5GvrnfEusxCjplZCVDq2Wi2Vj+Tz1f4fUlsEJ2Hd2L4SRleQy23AP 7dCyUtzBS/Huxr+fCUPca2xnrNqnwy3BeBzONXrg3HJLF/ECQmgCrH9VR0IOm5Xr7QTD KKHGjN0xLfNwzvA9o5LcYbl7z+b4FclUFWHupeCsgU/LYbPzfKYAjwaFt5yO5qGeDE9Y KtLYoliv8EKX5MIKz52GC85a5QF18lZNvdqxALwZStspgNmjkex9fR+eUFGijJra3uLs 0XIA== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=9WoG9Kzt6na6Tw2ZdlXQkoQ9nNhjgdz4kA2Vw5Wdulg=; fh=4AyU/x+val9l9ExwDmfmpeW7R8l/NbvEHNqFh2t6BLo=; b=ME8JWEWosbNxyCZQmkf7AP1XKyspsbWJhR1JG9r6e6QfHIqln8zQFuGHKJog/hWZP5 2hTiG+GaAm5VylF9ksqAnPI5GVVKU5fTxnHnxrcz6SZOPXmgiBEeMWrF9Fx/pWYC/Zpl iwOmSX4zWWUXdnJCIWZAsUZoVKPHpbVq6EAYOxvvoYN/3pVwgFH1pqRJEJTqi3AOLjkv XJkQDIji1jKntj2zkQfy2leZvDNz6K/mhnbpBpD3x13MyamrqQKDYgwYL2zHt8znDeWL IB/pm4YTHNTAXEFpkIRy6Wfynj8/iSXR/t9KshrfxGynQnRV8iRAEmn6Zt5Ql3KIgu7e nLeQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oRSajtaA; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-122957-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122957-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id f21-20020a1709062c5500b00a4729ef35c3si755642ejh.65.2024.03.28.06.34.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 06:34:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122957-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oRSajtaA; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-122957-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122957-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 am.mirrors.kernel.org (Postfix) with ESMTPS id A42591F25C2C for ; Thu, 28 Mar 2024 13:34:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7478F81754; Thu, 28 Mar 2024 13:34:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oRSajtaA" 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 8089D405FF; Thu, 28 Mar 2024 13:34:14 +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=1711632854; cv=none; b=ozOP/i682DLl5YVI43aIXYna/qI5c9XC/F3GXWRvDFDsGLTZD0jz6NhDz3BFUQjS+HjiiF5EOblRHX13zoin9zIKF84vAKjQvO9+FDEQTjTnVaCZaBbloBIJcBDN22SssSeAS2zfJsDMi1K2SJssd+yByNFo2yKbdzSwrMXdDBY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711632854; c=relaxed/simple; bh=MTl+MqWhM6jFgml8aciVqT+UCwUjsKjEyPmh+mIzli8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=SdRZRUKyMQSKpR4Jc/8UVXKI+hPZsBUpd/SZfOaFxrn2sx5bksJBfTPGDNFVWx73soHJ9LHlyt9t5pwr2KGvWqQH5Aq2TRRQt3jqXHO7bXqUWLVfbyTu82BpTNS2tr27VVtXjzxNcHxNeNP0NI9x4Gz2Oh76aySS6AST7/xwUO4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oRSajtaA; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4713C43390; Thu, 28 Mar 2024 13:34:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711632854; bh=MTl+MqWhM6jFgml8aciVqT+UCwUjsKjEyPmh+mIzli8=; h=From:To:Cc:Subject:Date:From; b=oRSajtaAg2292U7sKWoJVPr5I6l9Ou3Ab4TTn3OJQP2jHoXgjivlAv6NRgY1OmdlX 6YwdaUicc5iopAXC/FejF8GMKHJw1qljOavV/tCZFDKdgOR9TG/gDpRylqYPDvvhBJ 0CSdyU6ek/wcS1dwtLUVwmqks8sM9cPv8XVHw4vpvsAQh5YB3BGilHHluXVNKe9i3y 3+iyc/KLSqI5fyTvUtjuJwMDwT5Num+iIHq8HpXaB2TU71bg8CcMhlBRj9XVYCTYVp BC9C//xuNHqCzb0IYeh1A9bU4eZLxPbUa7l6XQJmmu6AtCTJ01ChDGmdZxVlYbyT23 SUJX4L5be6hsQ== 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 Subject: [PATCH v2] perf/x86/amd: support capturing LBR from software events Date: Thu, 28 Mar 2024 06:33:59 -0700 Message-ID: <20240328133359.731818-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit [0] 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 ([1]) 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 [0] for Intel CPUs. Given LBR can be set up to capture any indirect jumps, it's critical to minimize indirect jumps on the way to requesting LBR from BPF program, so we split amd_pmu_lbr_disable_all() into a wrapper with some generic conditions vs always-inlined __amd_pmu_lbr_disable() called directly from BPF subsystem (through perf_snapshot_branch_stack static call). Now that it's possible to capture LBR on AMD CPU from BPF at arbitrary point, there is no reason to artificially limit this feature to sampling events. So corresponding check is removed. AFAIU, there is no correctness implications of doing this (and it was possible to bypass this check by just setting perf_event's sample_period to 1 anyways, so it doesn't guard all that much). This was tested on AMD Bergamo CPU and worked well when utilized from the aforementioned retsnoop tool. [0] https://lore.kernel.org/bpf/20210910183352.3151445-2-songliubraving@fb.com/ [1] https://github.com/anakryiko/retsnoop Signed-off-by: Andrii Nakryiko --- arch/x86/events/amd/core.c | 29 ++++++++++++++++++++++++++++- arch/x86/events/amd/lbr.c | 11 +---------- arch/x86/events/perf_event.h | 11 +++++++++++ 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c index aec16e581f5b..88f6d0701342 100644 --- a/arch/x86/events/amd/core.c +++ b/arch/x86/events/amd/core.c @@ -618,7 +618,7 @@ static void amd_pmu_cpu_dead(int cpu) } } -static inline void amd_pmu_set_global_ctl(u64 ctl) +static __always_inline void amd_pmu_set_global_ctl(u64 ctl) { wrmsrl(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, ctl); } @@ -878,6 +878,29 @@ static int amd_pmu_handle_irq(struct pt_regs *regs) return amd_pmu_adjust_nmi_window(handled); } +static int amd_pmu_v2_snapshot_branch_stack(struct perf_branch_entry *entries, unsigned int cnt) +{ + struct cpu_hw_events *cpuc; + unsigned long flags; + + /* must not have branches... */ + local_irq_save(flags); + amd_pmu_core_disable_all(); + __amd_pmu_lbr_disable(); + /* ... until here */ + + cpuc = this_cpu_ptr(&cpu_hw_events); + + amd_pmu_lbr_read(); + cnt = min_t(unsigned int, 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 +1437,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. diff --git a/arch/x86/events/amd/lbr.c b/arch/x86/events/amd/lbr.c index 4a1e600314d5..75920f895d67 100644 --- a/arch/x86/events/amd/lbr.c +++ b/arch/x86/events/amd/lbr.c @@ -310,10 +310,6 @@ int amd_pmu_lbr_hw_config(struct perf_event *event) { int ret = 0; - /* LBR is not recommended in counting mode */ - if (!is_sampling_event(event)) - return -EINVAL; - ret = amd_pmu_lbr_setup_filter(event); if (!ret) event->attach_state |= PERF_ATTACH_SCHED_CB; @@ -412,16 +408,11 @@ void amd_pmu_lbr_enable_all(void) void amd_pmu_lbr_disable_all(void) { struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); - u64 dbg_ctl, dbg_extn_cfg; if (!cpuc->lbr_users || !x86_pmu.lbr_nr) return; - rdmsrl(MSR_AMD_DBG_EXTN_CFG, dbg_extn_cfg); - rdmsrl(MSR_IA32_DEBUGCTLMSR, dbg_ctl); - - wrmsrl(MSR_AMD_DBG_EXTN_CFG, dbg_extn_cfg & ~DBG_EXTN_CFG_LBRV2EN); - wrmsrl(MSR_IA32_DEBUGCTLMSR, dbg_ctl & ~DEBUGCTLMSR_FREEZE_LBRS_ON_PMI); + __amd_pmu_lbr_disable(); } __init int amd_pmu_lbr_init(void) diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index fb56518356ec..4dddf0a7e81e 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -1329,6 +1329,17 @@ void amd_pmu_lbr_enable_all(void); void amd_pmu_lbr_disable_all(void); int amd_pmu_lbr_hw_config(struct perf_event *event); +static __always_inline void __amd_pmu_lbr_disable(void) +{ + u64 dbg_ctl, dbg_extn_cfg; + + rdmsrl(MSR_AMD_DBG_EXTN_CFG, dbg_extn_cfg); + rdmsrl(MSR_IA32_DEBUGCTLMSR, dbg_ctl); + + wrmsrl(MSR_AMD_DBG_EXTN_CFG, dbg_extn_cfg & ~DBG_EXTN_CFG_LBRV2EN); + wrmsrl(MSR_IA32_DEBUGCTLMSR, dbg_ctl & ~DEBUGCTLMSR_FREEZE_LBRS_ON_PMI); +} + #ifdef CONFIG_PERF_EVENTS_AMD_BRS #define AMD_FAM19H_BRS_EVENT 0xc4 /* RETIRED_TAKEN_BRANCH_INSTRUCTIONS */ -- 2.43.0