Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp3950479pxb; Mon, 30 Aug 2021 14:42:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxlatLBcWIPQQ6CHKUmfNB+wfp35yxLDDJXODfvlmXkOG4q8GTtKkI5LcjYa7vQ6y7NYK4j X-Received: by 2002:a50:99cc:: with SMTP id n12mr26530593edb.53.1630359772404; Mon, 30 Aug 2021 14:42:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630359772; cv=none; d=google.com; s=arc-20160816; b=hFu/gMm/iR08evGETmX940Wm+srmY0ZppNTQfKOeSz10VJGHAlraXO5ftzJsR/EhdG hwCQvNOutvOsxH89iBuw5nr3I/NZL/aPbWKC3VGvEVFxIKIAAauCxoCEI/jJJPgBDuUr W/47FbQ8iiXQ/ZKTpeHLjGqLsvNvhWcB539iwdhtO05vBC0qVezkFHOQukAxaVXEsEwn vxDMjEUoljTafnZBVrG3e2aIzl2XxGMu1ZDD+rybbvfEP0diFNbQ3ffoJYOnsYO4Dg9q +dmfzTmg6T2ZBot7OaTpKafGGey5ApubE/z7wpwBiOpAN3mdc6O0tdEvtWU10jGD+y/o I4yQ== 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=GQkjT68Y1G0cFcBBOBufW5uO3v6jWnEaZhZVXddLF0Q=; b=tRvwqFjWAEamHOjJYnWOBWFEy/fG+kJVwUb55itxIigj3ofvs8gLK63HX7PetTB0Xe G8MIEDGzldRSXUMbytpPe9I2RUeVS6aZY4GOiOUtULEwTIfkYvY1DYMBxT41k08CgEHO G17JIdLgHHeD3AkL60NHoQTCV6j+yFafa5ZlxhVfCccrhAAfyJ1i4D+wiIA6tfq06z3k vnMuIh9ksa9ozhGN8Q8epD6ac12SLaxj7Fln9VFSQ6ELGveUnSS/pvt+0Ny+ShhNp8Ao GuwUMzLpyl00p8K3DiT8cq5ll+5IhpuSk7Ixe431DuAp3FJsSMh8tP2g6FRU+ht5aehQ 5chA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=PGj5ps+z; 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 k2si11187415edh.529.2021.08.30.14.42.28; Mon, 30 Aug 2021 14:42:52 -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=PGj5ps+z; 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 S238423AbhH3VmJ (ORCPT + 99 others); Mon, 30 Aug 2021 17:42:09 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:44594 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237612AbhH3VmI (ORCPT ); Mon, 30 Aug 2021 17:42:08 -0400 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 17ULcYAa014094 for ; Mon, 30 Aug 2021 14:41: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=GQkjT68Y1G0cFcBBOBufW5uO3v6jWnEaZhZVXddLF0Q=; b=PGj5ps+zKOHRiEsgkrVfJEZF6xWFWfPDBUyFn+sneBwVyzfR7O80uvkq4deTlv1vvsmg XEi8MlakBXr+byFwHt5O7bvky4VhBVzzisggJd6Xop31isMxFt26EaUYAQsLV249a4Si vMU6fWeUncu5bQZOhK3VDV5L3e1U+iToU2U= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 3artudvxwa-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 30 Aug 2021 14:41:13 -0700 Received: from intmgw002.06.ash9.facebook.com (2620:10d:c085:108::4) by mail.thefacebook.com (2620:10d:c085:11d::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Mon, 30 Aug 2021 14:41:11 -0700 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id DD81FF146804; Mon, 30 Aug 2021 14:41:09 -0700 (PDT) From: Song Liu To: , CC: , , , , , Song Liu Subject: [PATCH v3 bpf-next 0/3] bpf: introduce bpf_get_branch_snapshot Date: Mon, 30 Aug 2021 14:41:03 -0700 Message-ID: <20210830214106.4142056-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: GZrA2XXFDO1nEK_GmqpRjU__EQL2UJq_ X-Proofpoint-ORIG-GUID: GZrA2XXFDO1nEK_GmqpRjU__EQL2UJq_ 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-30_06:2021-08-30,2021-08-30 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 suspectscore=0 mlxscore=0 spamscore=0 mlxlogscore=999 adultscore=0 phishscore=0 malwarescore=0 clxscore=1015 lowpriorityscore=0 bulkscore=0 impostorscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2107140000 definitions=main-2108300136 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Changes v2 =3D> v3: 1. Fix the use of static_call. (Peter) 2. Limit the use to perfmon version >=3D 2. (Peter) 3. Modify intel_pmu_snapshot_branch_stack() to use intel_pmu_disable_all and intel_pmu_enable_all(). Changes v1 =3D> v2: 1. Rename the helper as bpf_get_branch_snapshot; 2. Fix/simplify the use of static_call; 3. Instead of percpu variables, let intel_pmu_snapshot_branch_stack output branch records to an output argument of type perf_branch_snapshot. 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_snapshot selftests/bpf: add test for bpf_get_branch_snapshot arch/x86/events/intel/core.c | 24 +++- include/linux/bpf.h | 2 + include/linux/filter.h | 3 +- include/linux/perf_event.h | 24 ++++ include/uapi/linux/bpf.h | 16 +++ kernel/bpf/trampoline.c | 13 +++ kernel/bpf/verifier.c | 12 ++ kernel/events/core.c | 3 + kernel/trace/bpf_trace.c | 43 +++++++ net/bpf/test_run.c | 15 ++- tools/include/uapi/linux/bpf.h | 16 +++ .../bpf/prog_tests/get_branch_snapshot.c | 106 ++++++++++++++++++ .../selftests/bpf/progs/get_branch_snapshot.c | 41 +++++++ tools/testing/selftests/bpf/trace_helpers.c | 30 +++++ tools/testing/selftests/bpf/trace_helpers.h | 5 + 15 files changed, 349 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/get_branch_snaps= hot.c create mode 100644 tools/testing/selftests/bpf/progs/get_branch_snapshot.c -- 2.30.2