Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp94175pxb; Thu, 2 Sep 2021 20:23:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxhWQQG7moo1delVuPuHC23cJL49jTXj5iyjk9Usgg2M1hxo55E31McMbAI57JYAj2vVBIT X-Received: by 2002:a6b:d90b:: with SMTP id r11mr1305026ioc.99.1630639437546; Thu, 02 Sep 2021 20:23:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630639437; cv=none; d=google.com; s=arc-20160816; b=BhxikjkVIWtFDxprRgReAYEeFenkmseABeq9Ssfxk5DCx2+gY8z+mY79O6Bea6GsFL qr57hDcTDkm0wT9BTS6WTAWiVHj9aZcLlhhzM2zjURVgf+xN0yiWojoVaU41223J5HpS L7O7nYvZINwfi1ZRK8d7tBkBGRg6f7nBPqBb8NO6Xre+11D7SOcZ+TwGFx+VeLctkDBr IfAyTSwevFaznmSXE55nN1QGBVpdVXCHgm3iWgh3F8JRSoLYrxMFDxBs6gAO5utBk41l P+MbFLzEBVZ1H9ze4h4PYIf+7oL0yZwvz/AaQfhFH4eSkYpO14qX4JuB7uyRCug1m6AM PuWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=B86ESlgqkajdOaq6Bjknz3MFl+TpH2X5kP28d+2n8So=; b=st7P4F9ZMjNxhkMQyvZBsC6N2IUq5NXeeAFr8CglTzpXcmcwTuF3xp+OjN/YxVFd2r zXFzC5swO3Z+9G6bMwt0V1iNnC/dk+6+Ak7RjLhXGxFSIs/4H7ceIRnXamyU5VI32lMK bb1k+MftwoLmNy4bsOdlGF2oJyZ7ElyvThYCEo5/D+Ll95UhDhwRI4blm2ixK9zYTrbc 3z6ijHSK7ZlgGCtffC0SUlMDRY1+eLPUONdgM0DNvSD0pwG+7wIyNaR/UmpiGWPudAxH q5SxKf/BJqhK2ULwu+rU84MRh3n59Iz5VElUucz3CVf7Bu3pDMOA74MadPTUO/osq4dl 3Qxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=lYvOwRGs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b4si3657713iln.32.2021.09.02.20.23.46; Thu, 02 Sep 2021 20:23:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=lYvOwRGs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347929AbhIBXGw (ORCPT + 99 others); Thu, 2 Sep 2021 19:06:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347912AbhIBXGv (ORCPT ); Thu, 2 Sep 2021 19:06:51 -0400 Received: from mail-yb1-xb35.google.com (mail-yb1-xb35.google.com [IPv6:2607:f8b0:4864:20::b35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF114C061575; Thu, 2 Sep 2021 16:05:52 -0700 (PDT) Received: by mail-yb1-xb35.google.com with SMTP id z18so6844927ybg.8; Thu, 02 Sep 2021 16:05:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=B86ESlgqkajdOaq6Bjknz3MFl+TpH2X5kP28d+2n8So=; b=lYvOwRGs1vjxgzh2WiJKppW7pB8bP6jhUKVmxygCMpozFDZNEyAMVKo/i7mGirozQO 0TJ8iyTjDKmA9W7EpImHZvj43GWarqWpTo52VpHcC4fh/nNzj4RZcGT/Svl7LteP1YfR B96yZ+UmqdzWapvpyoJg9Y8I0e8Xn2hlPvLTOQ5lE0WuuptYXKGU8nGNlydX7QDiIS9X 5xx60/JJf12hww9fvy/pteltfi0A5hcYSIJVjEYTq6kbRGJv+uKZShGdSJuqKTaa/g50 QzV9mhBiWZYQCh+R6sGkHqvPkIRx+Mx1IIKRm1lVz3TVNoCUxwzj/dGHBbFzvqO9nLDU 1Gkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=B86ESlgqkajdOaq6Bjknz3MFl+TpH2X5kP28d+2n8So=; b=qfS8OV1gZSEE1SVDrYUO7ShDLTK7FgRibP+ygbQQC6Od4duFZRvbAzeQ3Js/qr2iuF s2BxsQeY8CASx0DzI/p8+tTOd2UQOrYRs7PRFtpVhBCoUckQipolPIs8IBBgpTZOBE/K YfIzZCRZS64QQRpKbVZCJcYM6if37kYR8wZP19dRajKGLmVHJVZCmz3Csb0SbJsA6uNf 1bXyWJN9sg5zYJH5J/iYDCg/5rWLpcTCUcvPuKO1qeqgaZ8cZZEqYl3tzTfVjstGT+LN jKowy00CSIO3rDu0gEbETxXGGBGoFkjIM/aKsVRA+T93P9TAh4ly45f4RKFofALa48fm xwTw== X-Gm-Message-State: AOAM530JvEn3nM/8+d8lFxrtGaB5qTKmH1asEkSds8jjP+Fjclmu4X2p pyTehB2oh+trJuqsVEs4SeAvt2cK9tIUfp+Itac= X-Received: by 2002:a25:bb13:: with SMTP id z19mr1085813ybg.347.1630623952207; Thu, 02 Sep 2021 16:05:52 -0700 (PDT) MIME-Version: 1.0 References: <20210902165706.2812867-1-songliubraving@fb.com> <20210902165706.2812867-3-songliubraving@fb.com> In-Reply-To: From: Andrii Nakryiko Date: Thu, 2 Sep 2021 16:05:41 -0700 Message-ID: Subject: Re: [PATCH v5 bpf-next 2/3] bpf: introduce helper bpf_get_branch_snapshot To: Song Liu Cc: bpf , open list , Arnaldo Carvalho de Melo , Peter Ziljstra , Ingo Molnar , Kajol Jain , Kernel Team Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 2, 2021 at 4:03 PM Song Liu wrote: > > > > > On Sep 2, 2021, at 3:53 PM, Andrii Nakryiko wrote: > > > > On Thu, Sep 2, 2021 at 9:58 AM Song Liu wrote: > >> > >> Introduce bpf_get_branch_snapshot(), which allows tracing pogram to get > >> branch trace from hardware (e.g. Intel LBR). To use the feature, the > >> user need to create perf_event with proper branch_record filtering > >> on each cpu, and then calls bpf_get_branch_snapshot in the bpf function. > >> On Intel CPUs, VLBR event (raw event 0x1b00) can be use for this. > >> > >> Signed-off-by: Song Liu > >> --- > >> include/uapi/linux/bpf.h | 22 ++++++++++++++++++++++ > >> kernel/bpf/trampoline.c | 3 ++- > >> kernel/trace/bpf_trace.c | 33 +++++++++++++++++++++++++++++++++ > >> tools/include/uapi/linux/bpf.h | 22 ++++++++++++++++++++++ > >> 4 files changed, 79 insertions(+), 1 deletion(-) > >> > >> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h > >> index 791f31dd0abee..c986e6fad5bc0 100644 > >> --- a/include/uapi/linux/bpf.h > >> +++ b/include/uapi/linux/bpf.h > >> @@ -4877,6 +4877,27 @@ union bpf_attr { > >> * Get the struct pt_regs associated with **task**. > >> * Return > >> * A pointer to struct pt_regs. > >> + * > >> + * long bpf_get_branch_snapshot(void *entries, u32 size, u64 flags) > >> + * Description > >> + * Get branch trace from hardware engines like Intel LBR. The > >> + * branch trace is taken soon after the trigger point of the > >> + * BPF program, so it may contain some entries after the > > > > This part is a leftover from previous design, so not relevant anymore? > > Hmm.. This is still relevant, but not very accurate. I guess we should > provide more information, like "For more information about branches before > the trigger point, this should be called early in the BPF program". I read the part about "taken soon after the trigger point of BPF program" as a reference to previous implementation. So maybe let's clarify that because LBR is not frozen, from the time bpf_get_branch_snapshot() is called to when we actually capture LBRs we can waste few records due to internal kernel calls, so the user has to be aware of that. > > Song > > > > > >> + * trigger point. The user need to filter these entries > >> + * accordingly. > >> + * > >> + * The data is stored as struct perf_branch_entry into output > >> + * buffer *entries*. *size* is the size of *entries* in bytes. > >> + * *flags* is reserved for now and must be zero. > >> + * > >> + * Return > >> + * On success, number of bytes written to *buf*. On error, a > >> + * negative value. > >> + * > >> + * **-EINVAL** if arguments invalid or **size** not a multiple > >> + * of **sizeof**\ (**struct perf_branch_entry**\ ). > >> + * > >> + * **-ENOENT** if architecture does not support branch records. > > > > [...] >