Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2463804pxb; Mon, 23 Aug 2021 23:03:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzgdIEL/lMgBglN8A9tOQHckpAbU6sYWaiVxXsovaERPJmq4uDWJLF+qtXMN02JO53jPaeX X-Received: by 2002:a92:c264:: with SMTP id h4mr6828504ild.26.1629784989028; Mon, 23 Aug 2021 23:03:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629784989; cv=none; d=google.com; s=arc-20160816; b=Rmqook55KHuba7NEasB3BUZsz1oyIXe9fHITylt/z/hYHEsRhAtpISnCI7Yjya5/zy zarMCItdeEpxFO4cr8sQHgHcCDpmLbEK9MHIZ+2+h7XgBfeGjNaoHiY3yQ+qnSwVXn1b SUggrACZI4fKdiCPZLpmI1oFE8+mmOAAVhCtb8mPmMtOEdrIr+YjiXQNMXveVNW2gHmI dXieJprBP0Tm0w6qAFG56FsfGhZGsy2g+3sBwzO7rBHKPC22we29tn7b4eVqOEwDpDcJ xPms4hxkJahSSCxk28muqG74iFXq+YQDeb8sEo9f9m6yf/64yJPEuOCAHEZ9nkaelvCb v0DQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :message-id:date:subject:cc:to:from:dkim-signature; bh=lPa7PCjv5m0QXRnNv3eW6sd2bXRA8WEwGpz+psv/Mp8=; b=BsQ3KmejQqYM4SQG07tQw+m1seQooI6bn0xuVa82BxeIW3DtjhI7BfGjILbzfSjK9h 7L2CKxFyLmKWo8nq7+MbkHpAgIL7kpHngD+nBDpdoQxApX5N76Eqvw9BKRLZUe8IQisg 29cwXpSJZjUZDGD05lME/A8bRijPnZ4c9LDD1dokQfx25Se0jUZc0h9SvsNYEm2d++Vc JrDu67dNE8ZljXGOz+eJXiIfowhoLuA+9V/iXtEJMCRVXKp/jJgZ8GtirAiRs0qsIKlf OZK6yAMLmLLwGMjoJbMQm/Ix+7OSxf/2ivUQVoh1omj/WiY66zDIawrZBbgFQsn4j7LB I5Bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=BMyXrx3z; 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=REJECT sp=REJECT dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e101si17915617jai.50.2021.08.23.23.02.58; Mon, 23 Aug 2021 23:03:09 -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=@fb.com header.s=facebook header.b=BMyXrx3z; 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=REJECT sp=REJECT dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234541AbhHXGC7 (ORCPT + 99 others); Tue, 24 Aug 2021 02:02:59 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:63848 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234523AbhHXGC5 (ORCPT ); Tue, 24 Aug 2021 02:02:57 -0400 Received: from pps.filterd (m0109333.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 17O5tuQb003141 for ; Mon, 23 Aug 2021 23:02:13 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : content-type : content-transfer-encoding : mime-version; s=facebook; bh=lPa7PCjv5m0QXRnNv3eW6sd2bXRA8WEwGpz+psv/Mp8=; b=BMyXrx3z1nm8AhXf74cfXejMFaHOvCajb2is9qogqXRnPZKB5Xpq2HzC7S+tt4y4+LLM IXSNxncar4ETOijGe+SHCoA3k5Tcl4R89btHfP8zYLRLGHS5v6yhV48BjEqW0Q1jZh/y h0i7+21XycdcuezjMJNWQVn3IbEzIc62mpw= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 3amfqt3ugs-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 23 Aug 2021 23:02:13 -0700 Received: from intmgw001.05.ash7.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.14; Mon, 23 Aug 2021 23:02:12 -0700 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 3CF02DDDF76D; Mon, 23 Aug 2021 23:02:06 -0700 (PDT) From: Song Liu To: , CC: , , , , Song Liu Subject: [PATCH bpf-next 0/3] bpf: introduce bpf_get_branch_trace Date: Mon, 23 Aug 2021 23:01:54 -0700 Message-ID: <20210824060157.3889139-1-songliubraving@fb.com> X-Mailer: git-send-email 2.30.2 X-FB-Internal: Safe Content-Type: text/plain X-FB-Source: Intern X-Proofpoint-GUID: hlKnTqKclOhAK8TH16tbHQwhT8rQBVju X-Proofpoint-ORIG-GUID: hlKnTqKclOhAK8TH16tbHQwhT8rQBVju Content-Transfer-Encoding: quoted-printable X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391,18.0.790 definitions=2021-08-24_01:2021-08-23,2021-08-23 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 priorityscore=1501 lowpriorityscore=0 malwarescore=0 adultscore=0 phishscore=0 mlxlogscore=848 mlxscore=0 spamscore=0 impostorscore=0 suspectscore=0 clxscore=1015 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2107140000 definitions=main-2108240039 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Branch stack can be very useful in understanding software events. For example, when a long function, e.g. sys_perf_event_open, returns an errno, it is not obvious why the function failed. Branch stack could provide very helpful information in this type of scenarios. This set adds support to read branch stack with a new BPF helper bpf_get_branch_trace(). Currently, this is only supported in Intel systems. It is also possible to support the same feaure for PowerPC. The hardware that records the branch stace is not stopped automatically on software events. Therefore, it is necessary to stop it in software soon. Otherwise, the hardware buffers/registers will be flushed. One of the key design consideration in this set is to minimize the number of branch record entries between the event triggers and the hardware recorder is stopped. Based on this goal, current design is different from the discussions in original RFC [1]: 1) Static call is used when supported, to save function pointer dereference; 2) intel_pmu_lbr_disable_all is used instead of perf_pmu_disable(), because the latter uses about 10 entries before stopping LBR. With current code, on Intel CPU, LBR is stopped after 6 branch entries after fexit triggers: ID: 0 from intel_pmu_lbr_disable_all.part.10+37 to intel_pmu_lbr_disable_al= l.part.10+72 ID: 1 from intel_pmu_lbr_disable_all.part.10+33 to intel_pmu_lbr_disable_al= l.part.10+37 ID: 2 from intel_pmu_snapshot_branch_stack+46 to intel_pmu_lbr_disable_all.= part.10+0 ID: 3 from __bpf_prog_enter+38 to intel_pmu_snapshot_branch_stack+0 ID: 4 from __bpf_prog_enter+8 to __bpf_prog_enter+38 ID: 5 from __brk_limit+477020214 to __bpf_prog_enter+0 ID: 6 from bpf_fexit_loop_test1+22 to __brk_limit+477020195 ID: 7 from bpf_fexit_loop_test1+20 to bpf_fexit_loop_test1+13 ID: 8 from bpf_fexit_loop_test1+20 to bpf_fexit_loop_test1+13 ... [1] https://lore.kernel.org/bpf/20210818012937.2522409-1-songliubraving@fb.= com/ Song Liu (3): perf: enable branch record for software events bpf: introduce helper bpf_get_branch_trace selftests/bpf: add test for bpf_get_branch_trace arch/x86/events/intel/core.c | 5 +- arch/x86/events/intel/lbr.c | 12 ++ arch/x86/events/perf_event.h | 2 + include/linux/filter.h | 3 +- include/linux/perf_event.h | 33 ++++++ include/uapi/linux/bpf.h | 16 +++ kernel/bpf/trampoline.c | 15 +++ kernel/bpf/verifier.c | 7 ++ kernel/events/core.c | 28 +++++ kernel/trace/bpf_trace.c | 30 +++++ net/bpf/test_run.c | 15 ++- tools/include/uapi/linux/bpf.h | 16 +++ .../bpf/prog_tests/get_branch_trace.c | 106 ++++++++++++++++++ .../selftests/bpf/progs/get_branch_trace.c | 41 +++++++ tools/testing/selftests/bpf/trace_helpers.c | 30 +++++ tools/testing/selftests/bpf/trace_helpers.h | 5 + 16 files changed, 361 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/get_branch_trace= .c create mode 100644 tools/testing/selftests/bpf/progs/get_branch_trace.c -- 2.30.2