From patchwork Tue Jul 12 12:31:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12914899 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2AC64C43334 for ; Tue, 12 Jul 2022 12:32:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232752AbiGLMc3 (ORCPT ); Tue, 12 Jul 2022 08:32:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229829AbiGLMc2 (ORCPT ); Tue, 12 Jul 2022 08:32:28 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F73132D8B; Tue, 12 Jul 2022 05:32:27 -0700 (PDT) Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 26CCDdL2007831; Tue, 12 Jul 2022 12:32:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2021-07-09; bh=iZMK9bktdwDJd00q6Om8llTMLBx5AoG2S8LRR0BTstI=; b=N/w10pL5CB1mEiCXb8FGUSxlRCUexnVXjfVIrmlE4seAm2sP+gxdSSK3v2+PrjrcvJT2 0ShY6RfcIyHkvnxnuguIYujnu1JxboWNNldfO1NCxJFwGTfCzivcysWQh1/gcHnl2j7f uIVUE+iZuw8htcPZ9kWck9AaVj4A2dAcNaQCDIyrXEpJ8LhMa8YtYF3cDnThCy0sIIXV xB5JixOF+AgiepgoeZntimvV98+TYd9dAYVzPmA9kSX3dK2je0UwSS3qNFfYDlWpyDxS j/lOdNbXLJ86C/NRHg9dY1xiZfSpj9KdPZTYaoa1+CMWgDA+KJ2DdE0090Fry/yyVPm+ Vw== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h71sc6n2s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 12 Jul 2022 12:32:01 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 26CCLb5H039161; Tue, 12 Jul 2022 12:32:00 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2177.outbound.protection.outlook.com [104.47.57.177]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3h7043buk2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 12 Jul 2022 12:31:59 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NT4w1jS85X3g84sdDjDIFfWaV0y2gCEizR5bIeBQcD/3KJtFcBQSsg4AbJPzNDZZS96cuG7+37NurS7YYwvWEdvYun6Esm7JnG6S44ZMVHasX31rMmma2+hggB1cDMAPh7dwJMQPy5Pv8VMKYuHhFeAHOooX2cOxlqtyHeZ5hiVxazMs59pJQF9EGpu7jrQcaul79IlKACTLyjoaeyeEmzHWzFsa8f/khbJgPOZWUA42llD3DA/ixsGkiayYF4CtNL/3PwxU3EOJEbT6u1vLMv8l7yi5x6aPBIUMeq/xuiTT0S+Jqxt/xsDEy7u8tozNjR+PdXfCrjO9GaKS+EtK6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=iZMK9bktdwDJd00q6Om8llTMLBx5AoG2S8LRR0BTstI=; b=ZCI97w7T+Q92Ltarm0Vyt+cWvZ1aIKusYLVXl9/FdrkbC3bsMVMSrfwcSxu0RtUYUjDl5xlRzKuT2HEMkxY4esEjaO2OnwEqJsVwjQLOmtpHnQoC2u1uUn1/Ih/HqtwZpbS4XLoWeg2rVkPAj84q0ylphOwPW8Fok4xpwkLw8TiV1gbckv/cn+jabd8TlP2C7xuu48KFv2wC2MRLBnUuHetfP4WetQ1dT08mi+boMnHdEkTXX99hO5lIbphS0Kv9bTY9eaCXCtJgUHzrbeFac16tjObo3xLXOFPEigXXtofk5MQeJT/ZlGeWpTzK+/icUs3OeMXjLGrQF/3jhpQXkg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=iZMK9bktdwDJd00q6Om8llTMLBx5AoG2S8LRR0BTstI=; b=RgtvQB9T8TT4eEslwgTOv/j1IVsJE1Mi0+1jpnSHZgj4NM0KVBNc7sJWgBqieuyHMludCUbNfYD1TleyzAT4yOM8Y5ceBxAENcULtlzp9k0tB5rCeFC07G3XEk5yeT5CsysuKXz50u3YxHs/NedhkGsPNIvLJWfytfnqjvUCtjQ= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by SA2PR10MB4426.namprd10.prod.outlook.com (2603:10b6:806:117::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5417.23; Tue, 12 Jul 2022 12:31:57 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::b1c7:933:e8c2:f84f]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::b1c7:933:e8c2:f84f%7]) with mapi id 15.20.5417.026; Tue, 12 Jul 2022 12:31:57 +0000 From: Alan Maguire To: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net Cc: kafai@fb.com, songliubraving@fb.com, haoluo@google.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, jolsa@kernel.org, mhiramat@kernel.org, akpm@linux-foundation.org, void@manifault.com, swboyd@chromium.org, ndesaulniers@google.com, 9erthalion6@gmail.com, kennyyu@fb.com, geliang.tang@suse.com, kuniyu@amazon.co.jp, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 bpf-next 1/2] bpf: add a ksym BPF iterator Date: Tue, 12 Jul 2022 13:31:44 +0100 Message-Id: <1657629105-7812-2-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1657629105-7812-1-git-send-email-alan.maguire@oracle.com> References: <1657629105-7812-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM4PR0902CA0006.eurprd09.prod.outlook.com (2603:10a6:200:9b::16) To BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e5b80d83-d31f-473c-ab2a-08da640282bb X-MS-TrafficTypeDiagnostic: SA2PR10MB4426:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sCnHSX4rHsWroV2oOKh2kgqpCjis7TeJRul+3K3UhVccwJLRGS8oCnm6tjP7ajdLCbBdPc9h8r7PcTItyaP4avznwW3+/sh1MkrrMCFpleoTsiUwUYUDe8vvJluSkyZN+AWPxkoE/JFdBjytJ36lcQGP8NUEz6XkFAD7sNPBSul4qK0LGZt8UQ8ptFl57jdBIk7B3EBui+H40lvMfUC8IrsZmHSMDkEYlx0WOUWC1Me1/EPEwVbjakWc+AwuUkdASQNNTDhxYxMCGaFptHcSAEjPS0DPQ0SRcurzIgoelEhmWnXAIkYANjk2O0aPfJ3eWGEEuzv7y9MzmpOgHIWDwCQNO+Ycjh6s2uEmm1ldFHt2GgleCvg1PRXp8siWNXhwYpxpQx0QSlXuCEgy+VXZAnjSquPyNRQA/kXpVNi2sriHVasWyICROecV6LJfCZ+uluvBX6WenApEEKq/XxngOE47pFh+2nUt9tBcCgvTEKKBbDEDFgD1xlpiHJn5nRvc/diyyPMcyZ2pojQNsFcNspYq31Gih9093JNzVZxX4Z+lf97EBerb2eJwA6ZJpqFUt11eQWi2EL5c1Y3ScJwfuam9WlcupPXBtdfUw3Rfhyf/NAheHJiMzU02MvEBY5GD2iLNSm596c+Nh25WwTRzDyXajUQjpSdru7IibktzBbJ04EJy0cFC/XvQgD7ByDIf3eVpU3ULDphV/RFL0kgfsO8Z6QNjl5HDzGqNKEonFIYZk9iF7Y4zfjF9lfZeO907mmTUi78nPrIQgB0MzpOCgrYjQjWrHhKS59clqPVs6Z7PvIjtwSVghsWzMhhC1YSyEm94xtYlM1tmcAukM8kyxw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BLAPR10MB5267.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(136003)(376002)(346002)(366004)(39860400002)(396003)(6512007)(26005)(6506007)(52116002)(186003)(2616005)(41300700001)(6666004)(8676002)(478600001)(4326008)(83380400001)(38100700002)(66556008)(66476007)(38350700002)(2906002)(966005)(8936002)(36756003)(7416002)(5660300002)(44832011)(86362001)(316002)(6486002)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HMW3+q4WfgiTwby6TqBgIziLEW6bDPmUuni1EMLkeBUuJoOYC/AMoBvFKmUd5sTBuD+jM3yTPy+czxJL0UpFzARZWhfvhI5NLOntD1g/aC5D/Pv7LXay3JaOg/46IYEl984YwUUy6NO5pzmsJwg6CHgKprKB8Et6JMyZR1UFDybuEwV07vbX4MueXgiRmDKJnNv26W4jOAWBQ8v6pG1pr5UmvEVRinJSAfqACsJ/p8z7trzTPDbMtr3SC9ELoSQlSMNwtJ8KqsQUq7deKX/Gt2pMCCHTimmrkHRl5bU2wg2997TM5wuA5LJ8xkIQ6zGlg9i3Ru3/BPWRKuuhRlxtIZwPGBGNZkmRXOG0R6gFdhzBXf8wGBtl4sVjrcGJlP+evTOEAl8jGR/a+M0OFWybO2h7Qvf/ih00gKtuG8krXbuNYwlfke/os3KCsNoJFYUGG4eR9cKLOZ9Wk2AuoBeQrpp/l0cNzvC1VA7fezkMNBh0KrDHmgTyOWwWwAQb7v4OPBiBwm3+nO9dVd7lFPzi3q1RukUKTrJuxfyutHDux+M+zcQQlNk4if2mfcmLvb7g47lXuO3IKMTkwSAs0umIQY/3m1twVYxcnK4ulr5SHyDaF3Z2ot9GLFIPWNSp/hGsT0ZCLPjoqzcuSNLrd9QFwGxQG/QKdsg+WvFZUm0XCQelDXcjfm7OyPUgfI4EQ0sTx5eQjhkm+lEd6uFwGh22m6ZuHE3+/3TKpM5fw3Kn+ZzNz0z99oWoE+RWWYa3QK6XlyLv3dmKtYkl59PxHJYdOw4dW2KFB0savE1cabWGyQumA/9DwgWx0GWjc+CYCMZw4uz/oLBeBLMHPQ7FRLsY6Q8dketTBh1ShvZ2Qx8bBvPs7VoBMmFxKVoC8pZD4XCteATPth8+D6ngmHdGEEjyN1plBDU4bi3lsVJSFdhviQvijvgxJTyiywoJUx8CNu/ikyWuKTRoK+5hrenRUIeVKtsU8KLw8CmURERBnD/bP6kPzYQqy1YuxuFQ/13m1Aw4e2pYuty2TSlRC+iVg5k83E3acq5TXfyFA2bmdCxpM6k1zQkO3lhpD2FwlO4xV4RdQgslhAzabEtyJHLxxPk+zO3GhxaWCiqOOrx9mhpHPr4PiP8mzhJDRgOMEaJhERayh17+eyAy5bYxJgKXlaj79pe3lYUbhzYHg5MDf/4GnPv6vyv8K4MDHoHrr+AreEOcBADVOpquPsUvUe0HYLLCQoiZD6ZDEY/50v6xuoO4PWRLovnyOcuuO6Q7YAtDmOuKXzQzTbpWfrKNTHVfJU3DMcwWK7R7hXTm/V7cfVd3+8eRHVWc/CcG0sb4A5u4i5NZ3YNvxB8rIrVya9Of3wZX2ZdhYo/Aq9jCMqgOfEd0jrLk0tQGlWFU57vTDUlnJS5woucpRujch5/j8gM1GBDHn5NozuFhsIMHL0biFtdkeZXeGfaz31W1+GUkBiFVe4nMB1sbBRKVbaCVV/NmqB6FibuZcPNeslAfHl62XrIB2IfTO8q53W6ldDw/5Jft5RW4m5tUy3p3+Er+0EETZaBI/ejYnGuVPA0u+Qv+K1H7intkbZQ0se8QO3sec8XpWiyt/evfw5niGfR72FuaGkssWQ== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e5b80d83-d31f-473c-ab2a-08da640282bb X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jul 2022 12:31:57.6457 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: alqysBbTNYdU+C3FfvC4UiJdxk/X4aLy9nh8wpOJXKtAqYDrA/Kvx9npzst/Jn1scdHHRx093a34S33DivfoTA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4426 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-12_08:2022-07-12,2022-07-12 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 suspectscore=0 mlxlogscore=999 mlxscore=0 spamscore=0 malwarescore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2207120048 X-Proofpoint-GUID: Y5F0OVlOm8tw2JYRRR_Z5wsjYuP-A89C X-Proofpoint-ORIG-GUID: Y5F0OVlOm8tw2JYRRR_Z5wsjYuP-A89C Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net add a "ksym" iterator which provides access to a "struct kallsym_iter" for each symbol. Intent is to support more flexible symbol parsing as discussed in [1]. [1] https://lore.kernel.org/all/YjRPZj6Z8vuLeEZo@krava/ Suggested-by: Alexei Starovoitov Signed-off-by: Alan Maguire Acked-by: Yonghong Song --- kernel/kallsyms.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index fbdf8d3..79a8583 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -30,6 +30,7 @@ #include #include #include +#include /* * These will be re-linked against their real values @@ -799,6 +800,96 @@ static int s_show(struct seq_file *m, void *p) .show = s_show }; +#ifdef CONFIG_BPF_SYSCALL + +struct bpf_iter__ksym { + __bpf_md_ptr(struct bpf_iter_meta *, meta); + __bpf_md_ptr(struct kallsym_iter *, ksym); +}; + +static int ksym_prog_seq_show(struct seq_file *m, bool in_stop) +{ + struct bpf_iter__ksym ctx; + struct bpf_iter_meta meta; + struct bpf_prog *prog; + + meta.seq = m; + prog = bpf_iter_get_info(&meta, in_stop); + if (!prog) + return 0; + + ctx.meta = &meta; + ctx.ksym = m ? m->private : NULL; + return bpf_iter_run_prog(prog, &ctx); +} + +static int bpf_iter_ksym_seq_show(struct seq_file *m, void *p) +{ + return ksym_prog_seq_show(m, false); +} + +static void bpf_iter_ksym_seq_stop(struct seq_file *m, void *p) +{ + if (!p) + (void) ksym_prog_seq_show(m, true); + else + s_stop(m, p); +} + +static const struct seq_operations bpf_iter_ksym_ops = { + .start = s_start, + .next = s_next, + .stop = bpf_iter_ksym_seq_stop, + .show = bpf_iter_ksym_seq_show, +}; + +static int bpf_iter_ksym_init(void *priv_data, struct bpf_iter_aux_info *aux) +{ + struct kallsym_iter *iter = priv_data; + + reset_iter(iter, 0); + + /* cache here as in kallsyms_open() case; use current process + * credentials to tell BPF iterators if values should be shown. + */ + iter->show_value = kallsyms_show_value(current_cred()); + + return 0; +} + +DEFINE_BPF_ITER_FUNC(ksym, struct bpf_iter_meta *meta, struct kallsym_iter *ksym) + +static const struct bpf_iter_seq_info ksym_iter_seq_info = { + .seq_ops = &bpf_iter_ksym_ops, + .init_seq_private = bpf_iter_ksym_init, + .fini_seq_private = NULL, + .seq_priv_size = sizeof(struct kallsym_iter), +}; + +static struct bpf_iter_reg ksym_iter_reg_info = { + .target = "ksym", + .feature = BPF_ITER_RESCHED, + .ctx_arg_info_size = 1, + .ctx_arg_info = { + { offsetof(struct bpf_iter__ksym, ksym), + PTR_TO_BTF_ID_OR_NULL }, + }, + .seq_info = &ksym_iter_seq_info, +}; + +BTF_ID_LIST(btf_ksym_iter_id) +BTF_ID(struct, kallsym_iter) + +static int __init bpf_ksym_iter_register(void) +{ + ksym_iter_reg_info.ctx_arg_info[0].btf_id = *btf_ksym_iter_id; + return bpf_iter_reg_target(&ksym_iter_reg_info); +} + +late_initcall(bpf_ksym_iter_register); + +#endif /* CONFIG_BPF_SYSCALL */ + static inline int kallsyms_for_perf(void) { #ifdef CONFIG_PERF_EVENTS From patchwork Tue Jul 12 12:31:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12914900 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 600A7CCA481 for ; Tue, 12 Jul 2022 12:32:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229810AbiGLMcb (ORCPT ); Tue, 12 Jul 2022 08:32:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232819AbiGLMc3 (ORCPT ); Tue, 12 Jul 2022 08:32:29 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3E7D32079; Tue, 12 Jul 2022 05:32:28 -0700 (PDT) Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 26CCDnOd009255; Tue, 12 Jul 2022 12:32:05 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2021-07-09; bh=YnYcffDk6ML4k3uLtmHf8a/FiXkNsaaPpPh8Gwy25g4=; b=c8vMXEx5MLry9R4OWVa/yIVw9mjU6VwNAKaiBF0Jtm90d0Jmjzg05Nyu+2hdImlVLqde XdGcUjYwP4RCsCJy1427TDljBt5++zQUXSbEVN8uqL55y4cBhKTNG7NNr9RIh6Wcs0JB 1qLQJaN6J37elrTwfXPkELlgvJyOwTkRAmC5S65fcrmS911rOH5p++V/W2CxkBhatUeA yCOqN4s0hr17/USyQCaxe9kX3GJHNQEx6MlkzV4ksffICAqwQrBDnLyB5sDLSviQUK1Y x2dOIM4i8jFqTLyAuYzbuu4/vzSoW24tr/b0deMmwWlLpNXLQ1fIYstMHXHb57rjD70u bQ== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h71sgpb39-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 12 Jul 2022 12:32:04 +0000 Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 26CCLJSs032296; Tue, 12 Jul 2022 12:32:03 GMT Received: from nam02-dm3-obe.outbound.protection.outlook.com (mail-dm3nam02lp2042.outbound.protection.outlook.com [104.47.56.42]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3h7043nxj0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 12 Jul 2022 12:32:03 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KBTMcpDemY6bTtubZ7fre4mQ/+zpf9mR6eHCdORHrzcFClasR0rfsHV5xBEZAbiVyXoZPyA9Dz3W15xXk3ciD5XSYHh2Sx2fEp0G87ImfjYEr4CnbIe3RbckHtkcyjZDHUFWB12fvCC8S2oq6N/dOYyy78mdMevFPhHd4P+kmFZidWBb9dppZ7Jh2cFZZ/qeKTy9OjBndGzfLdtac8fECIVOqmFYSYxiFzAAt5Ii+U5oj5SQuLrDsFg/7Spewb9dhO5QF0qd/WIVAJC4GLY3NGMRGpol+sMYh8JWqjqsBb45ANwQ/rU+nV4PN2ebS3hbdDtj4zIk+nkv9H4wYm5jxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=YnYcffDk6ML4k3uLtmHf8a/FiXkNsaaPpPh8Gwy25g4=; b=DRDS/LHWfVLDy/u6icTSOYpYT0aW58JFKh2XbZsefiPzZ3Q4wekng98SUQ9Rjm+i6nbK0zQQ1PkoFsUkJ8a7hj978bddRahlWftVAm6iqO2a6KAUqUqQI7qgB0NNN7erQqeSEp2swKev6eUCzxMXepgo/ymaiE7t/4X7qLmdZCErUrxkkkLaFqP3VRD6cFoxNbhMFPh0tPUmkeblFwarAeVvNDc48GuxeG0Xk+OSxU0RppiKWCprlXA3ALVUu75hHxIGpv9GrqjvxhuSxAa48gHAZTm+rZLQyQ3Vr7DsiB5HY6/J6BhJfhiNWt09bkXR04H8cNRgGGFf7B4DSW724A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=YnYcffDk6ML4k3uLtmHf8a/FiXkNsaaPpPh8Gwy25g4=; b=Q08/w0Id6bRonyxzfaFwJaFCT6j+F6Yc5nlBvrwvRjrzWHwupvCiOvFhfn8Aa8lVc/Hkjeh7ZZZKAjcjVim63Vzx5g2zy4R2yqdlEVaHlIgpDptoTsGJ7IyYjfHdt4IrdTyLo8uRE2ZKXUGXFFXJmz5uPV0eWBxWfkciV/Ys6A4= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by MN2PR10MB3293.namprd10.prod.outlook.com (2603:10b6:208:12b::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5417.16; Tue, 12 Jul 2022 12:32:00 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::b1c7:933:e8c2:f84f]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::b1c7:933:e8c2:f84f%7]) with mapi id 15.20.5417.026; Tue, 12 Jul 2022 12:32:00 +0000 From: Alan Maguire To: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net Cc: kafai@fb.com, songliubraving@fb.com, haoluo@google.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, jolsa@kernel.org, mhiramat@kernel.org, akpm@linux-foundation.org, void@manifault.com, swboyd@chromium.org, ndesaulniers@google.com, 9erthalion6@gmail.com, kennyyu@fb.com, geliang.tang@suse.com, kuniyu@amazon.co.jp, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 bpf-next 2/2] selftests/bpf: add a ksym iter subtest Date: Tue, 12 Jul 2022 13:31:45 +0100 Message-Id: <1657629105-7812-3-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1657629105-7812-1-git-send-email-alan.maguire@oracle.com> References: <1657629105-7812-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM4PR0902CA0006.eurprd09.prod.outlook.com (2603:10a6:200:9b::16) To BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 87f717a1-7ef4-42d3-2a65-08da6402849b X-MS-TrafficTypeDiagnostic: MN2PR10MB3293:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qnahrTxg+crZjB8MmvHqlUeNQdmOGfVYfYhgtMW3QMeru8YZ92KtHgRVP1udLPVCUn+5EizzLKF8076XKSFyS1nEDUvcwvo4/GpSVtKSPPbevgnZlf9x4CJ+b6olQiL/ldekrO2JnLHTjipgb55C7yxD4toRnnUDjbuPrWDDI3vPbyzO03m4lBtA9+gjAfoC9r4DbjsnqVrdbKTNTOOWciM/aoBEFXdCpx60wJreTJ64LtYjg9DxIKPvJu/0EqwixB/t39PPbg1bs74liRGOtGkj/Y5f6DPCyVGSIY2OaGvL9FMQ/sFdnVDTete+jGZLX2NvVUWwe7dW7CCY9TnNhtHpteFZMEG+JwnyH0x5E3avGzDTg6/lT5SmYNJTjjAJozI48Cph6kgoSE4/5XnoewFO0rWnGHUI5/HC23vWq7d6B6nBQAZ3sD3nfRNUOhxDadj+tjb+1cvrb46FL8bjJvN/wKzoBjJrvJMUSOgYkaYzgQG2PosfV8sDZsrFe0StGnZ4HUh+SHzR/9DqqCjuNkxT9DF9S8rQ1d6sESj8n0Q84ozv5yiZmJxv2yFZKkbOOQky6MVXrKNE9DAAfX6Xz2fbJJaG9AWexW6yvBWNTiLrrbBjYhtEUUUrtgRvFgG5RNQv7SV+wolo+/TeglwqvtknZd0lo29fn8i61b2wKln2iGE79q2HGkAzephJ+Yzle97PdfTMZdQtMSCY74Yf2wMdGY4Cf3iEb5rXES0OeF20oo4SQgGy+akTfsOc+HvIh5BZH6UhtWmdUL9YkvSwbvoGG1PhKQSgeSr6ixcD31wYIiAV0ibfRu6sl+nE6sTD X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BLAPR10MB5267.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(396003)(346002)(376002)(366004)(39860400002)(136003)(8936002)(66556008)(186003)(66476007)(8676002)(4326008)(5660300002)(38100700002)(66946007)(86362001)(7416002)(6506007)(2616005)(6666004)(41300700001)(316002)(6486002)(26005)(2906002)(6512007)(478600001)(38350700002)(52116002)(83380400001)(36756003)(44832011);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Uu9UwW1KKxQGdG/rUsEkwvQK2HARAvGS/IYaGIIhnks0tWAlBXSZ4B4/vhfAnuRASeqEZWM6lGnZxPdlzq+Uj3cbOVXsBG3XCEAFYlL6HGZJ5bvIJks5ymU30ZGqNg+AWGWKemNXJHyDh4L9tzfLHZTEMrqz9v2Nmoj3ZqdWWXn5IJIPfjUT2Y1pqShm9HbwRaYQjHgoHEcDd0LGuIWOc0SHLzEWgGp0AuUpVKlPTI2YQ9A2VFNpXVNFJfIaXICqTRwNAoTz+sm9qbSf5xHdDEyhtK7dc0EiBNPN8BkVVSGgzI42UROkXFhj80YxtjhLxTKsGvSr9nBXQfq+lI0yYY03+BUg/8trg69mD9mEhLvGUtHqsLkic+fAj3cIdR+HiAFJSQFAIADDw2RcpdRLdPE/vmk9XWLjsHGupappMHjh45nzh440kwaGlsFuRvaS7lS5ylBBs971dos3xQD3r6Jw+rdpC4b/6nECvx4CgwJnDddEQzvbZvnKNWVMEeoVUc3levMMqlHZsGToJUMPA6bUKR1Q6tlRfgv+0cu2KYIPXYUx8tdXBikEYTfE8Rp8IsRaulgfh/+sl2rKxpv6kITwcYe/4bc2zk3tcNc5hlbT6uTbRlWzAw0GXdCeI75yEH1jRiqwrRntPASZL+i8X5Wj4O2WJpfhsSTc4FhTSKm/A0CCMgJbnBhz6kKszDESm9nxZoBARjjj8to/0fdnioWQzjZWMRbeBZJX9Ucj98otZW41lyf+Z5iMuCBJ+vWkQAeDJVwMUtyqbBFyktWPkFDh6LSTkXDG4vRKIdV0EXQC/HzFnJmpjnEu6+lnyupDHyh5yP/K/vg9cliJNqfPDGqFqygTmcV7GEkXRD97pMC2WFDM7BbT8vLIs5hT09E3xoXfXGuBTgYa3+HIxwsZFU8rv5SXwFV7QdzT/yJTOIaCP1K+0Neks0ezFu0YcQrE6z0MrTDS2y6HdHNCx4omnFnGP4gqzToPW+ok2s1WHrAlDud7NdeksR5zfifIYci6XuS3/XYi8hFNrcJQE7DmkQFlOjYL86ddZCGil445kRUgN9emwHXiV2cvTT5rupQVWckvzcu5MulM8RwqZsvnHeVhB6EpIP9d+x9F/Kl4vweGAvOQS7TRXuzF7NqFW3ma47MaI66PreDRFSTU3z2csLE+ntAfQDBScDiv2wam5dEoFX5j61h5qkJB5OyYaaP9dBCkmYEfWOLPZcfbQi64KrmhjovNBrEy8b5UQ9Mopig9td5JQnRRXMGXcNDmko3+cq+jwftDRwqlq7I8xB7L8LuPcE6kN+V84+N696cRGMxoCkQe9hmiP2zPk+vs6TXGdFZtCpxwA4KxZcCdC3d5+MxgIv/Wz2APaMRtg0Jg3LwT+R46oWgcSOIluOOCAnj0T5PEcxeROr2fDRsejCbKyicXxSRqxsnmeeBJLDBPRtk2OE9KL6aZSxE3Ac6JCCiwQYo1t6J+Wvie73mKjpOjzXA52UdqwCVkuvjJrc5AJk9Mvz4bPNuXvwhjlFWYcnymmsAJo2h2B7fPZqEdRsnm58hYmaYOf1CCraV/gGZUJG6G6VEKN5XpRWbVQOZx2ArNU0U3HMJq1mihd8STc0tvjg== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 87f717a1-7ef4-42d3-2a65-08da6402849b X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jul 2022 12:32:00.6787 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: oEvBA02LhBAgIL6qofEghHFTEi1a1sxYWiD0jIxF1mP4EW/M2+iwWZcOYBCFBMNwRUaxnJ5ci8wfFTbdigFWTw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR10MB3293 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-12_08:2022-07-12,2022-07-12 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 phishscore=0 mlxscore=0 suspectscore=0 adultscore=0 spamscore=0 mlxlogscore=999 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2207120048 X-Proofpoint-GUID: ezaceH_9DT8x6F3jZ-oLPkWawfcX-DlD X-Proofpoint-ORIG-GUID: ezaceH_9DT8x6F3jZ-oLPkWawfcX-DlD Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net add subtest verifying BPF ksym iter behaviour. The BPF ksym iter program shows an example of dumping a format different to /proc/kallsyms. It adds KIND and MAX_SIZE fields which represent the kind of symbol (core kernel, module, ftrace, bpf, or kprobe) and the maximum size the symbol can be. The latter is calculated from the difference between current symbol value and the next symbol value. The key benefit for this iterator will likely be supporting in-kernel data-gathering rather than dumping symbol details to userspace and parsing the results. Signed-off-by: Alan Maguire Acked-by: Yonghong Song --- tools/testing/selftests/bpf/prog_tests/bpf_iter.c | 16 +++++ tools/testing/selftests/bpf/progs/bpf_iter.h | 7 +++ tools/testing/selftests/bpf/progs/bpf_iter_ksym.c | 74 +++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_ksym.c diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c index 7ff5fa9..a33874b 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c @@ -27,6 +27,7 @@ #include "bpf_iter_test_kern5.skel.h" #include "bpf_iter_test_kern6.skel.h" #include "bpf_iter_bpf_link.skel.h" +#include "bpf_iter_ksym.skel.h" static int duration; @@ -1120,6 +1121,19 @@ static void test_link_iter(void) bpf_iter_bpf_link__destroy(skel); } +static void test_ksym_iter(void) +{ + struct bpf_iter_ksym *skel; + + skel = bpf_iter_ksym__open_and_load(); + if (!ASSERT_OK_PTR(skel, "bpf_iter_ksym__open_and_load")) + return; + + do_dummy_read(skel->progs.dump_ksym); + + bpf_iter_ksym__destroy(skel); +} + #define CMP_BUFFER_SIZE 1024 static char task_vma_output[CMP_BUFFER_SIZE]; static char proc_maps_output[CMP_BUFFER_SIZE]; @@ -1267,4 +1281,6 @@ void test_bpf_iter(void) test_buf_neg_offset(); if (test__start_subtest("link-iter")) test_link_iter(); + if (test__start_subtest("ksym")) + test_ksym_iter(); } diff --git a/tools/testing/selftests/bpf/progs/bpf_iter.h b/tools/testing/selftests/bpf/progs/bpf_iter.h index 97ec8bc..e984660 100644 --- a/tools/testing/selftests/bpf/progs/bpf_iter.h +++ b/tools/testing/selftests/bpf/progs/bpf_iter.h @@ -22,6 +22,7 @@ #define BTF_F_NONAME BTF_F_NONAME___not_used #define BTF_F_PTR_RAW BTF_F_PTR_RAW___not_used #define BTF_F_ZERO BTF_F_ZERO___not_used +#define bpf_iter__ksym bpf_iter__ksym___not_used #include "vmlinux.h" #undef bpf_iter_meta #undef bpf_iter__bpf_map @@ -44,6 +45,7 @@ #undef BTF_F_NONAME #undef BTF_F_PTR_RAW #undef BTF_F_ZERO +#undef bpf_iter__ksym struct bpf_iter_meta { struct seq_file *seq; @@ -151,3 +153,8 @@ enum { BTF_F_PTR_RAW = (1ULL << 2), BTF_F_ZERO = (1ULL << 3), }; + +struct bpf_iter__ksym { + struct bpf_iter_meta *meta; + struct kallsym_iter *ksym; +}; diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_ksym.c b/tools/testing/selftests/bpf/progs/bpf_iter_ksym.c new file mode 100644 index 0000000..285c008 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bpf_iter_ksym.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022, Oracle and/or its affiliates. */ +#include "bpf_iter.h" +#include + +char _license[] SEC("license") = "GPL"; + +unsigned long last_sym_value = 0; + +static inline char tolower(char c) +{ + if (c >= 'A' && c <= 'Z') + c += ('a' - 'A'); + return c; +} + +static inline char toupper(char c) +{ + if (c >= 'a' && c <= 'z') + c -= ('a' - 'A'); + return c; +} + +/* Dump symbols with max size; the latter is calculated by caching symbol N value + * and when iterating on symbol N+1, we can print max size of symbol N via + * address of N+1 - address of N. + */ +SEC("iter/ksym") +int dump_ksym(struct bpf_iter__ksym *ctx) +{ + struct seq_file *seq = ctx->meta->seq; + struct kallsym_iter *iter = ctx->ksym; + __u32 seq_num = ctx->meta->seq_num; + unsigned long value; + char type; + int ret; + + if (!iter) + return 0; + + if (seq_num == 0) { + BPF_SEQ_PRINTF(seq, "ADDR TYPE NAME MODULE_NAME KIND MAX_SIZE\n"); + return 0; + } + if (last_sym_value) + BPF_SEQ_PRINTF(seq, "0x%x\n", iter->value - last_sym_value); + else + BPF_SEQ_PRINTF(seq, "\n"); + + value = iter->show_value ? iter->value : 0; + + last_sym_value = value; + + type = iter->type; + + if (iter->module_name[0]) { + type = iter->exported ? toupper(type) : tolower(type); + BPF_SEQ_PRINTF(seq, "0x%llx %c %s [ %s ] ", + value, type, iter->name, iter->module_name); + } else { + BPF_SEQ_PRINTF(seq, "0x%llx %c %s ", value, type, iter->name); + } + if (!iter->pos_arch_end || iter->pos_arch_end > iter->pos) + BPF_SEQ_PRINTF(seq, "CORE "); + else if (!iter->pos_mod_end || iter->pos_mod_end > iter->pos) + BPF_SEQ_PRINTF(seq, "MOD "); + else if (!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > iter->pos) + BPF_SEQ_PRINTF(seq, "FTRACE_MOD "); + else if (!iter->pos_bpf_end || iter->pos_bpf_end > iter->pos) + BPF_SEQ_PRINTF(seq, "BPF "); + else + BPF_SEQ_PRINTF(seq, "KPROBE "); + return 0; +}