From patchwork Sun Jul 10 22:09:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12912703 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 19F61C43334 for ; Sun, 10 Jul 2022 22:10:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229591AbiGJWKl (ORCPT ); Sun, 10 Jul 2022 18:10:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbiGJWKj (ORCPT ); Sun, 10 Jul 2022 18:10:39 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE19D9FF7; Sun, 10 Jul 2022 15:10:36 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 26AFVbru006469; Sun, 10 Jul 2022 22:10:08 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=s9X2xy2+jAXWPYSXKu9v6yOU+zjX6sJYQf+H+ZbO23h+2iUJzx+pKY3UAyC1qYSCY1jN OPJCiqEFkRuC4cByfGxbNp+zJsapQDw3Lcs6oLFTZHVpTCcEevOzpRxjeHhfOd6PErGL Q6SAAoi7t7YKHxw5m0m9sk2SizDas+LcUN8MAmTGHn/cmRwuXrHkG9B4q09qR0gKPZbE 24TwEBqgjZCj2RDr+0jeqLcrvFByXBprSXGj132DoFabfAQTWr53WliEtFlc0pQjRB8W ay8TDCIta1tMRDoKLfj5MdKfFZ7VpnsQUGCMyEIwLuSJFdpFjU11eXLZ59+/iQP+jYAQ Zg== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h71xr9w7j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Jul 2022 22:10:08 +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 26AM5CBt007949; Sun, 10 Jul 2022 22:10:07 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2169.outbound.protection.outlook.com [104.47.58.169]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3h7041x8t0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Jul 2022 22:10:07 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oQr2CHMTghKz+O9dS5/kW0WC0Mcm7smYylVSW18Q8nN1P2eSU7OIBK02OFEbRKZH1MCziVJ7bpiupPPti/OlqOsh7udJK2TnTIH2F8lsDnysymZfXfcQeWI21ftYu5GfwrPMJxmcemy8XarQmWlUuu2GLXANLHvxTKsmhgFOj4iQlsSvBsImaLRjLtAgQh5drMYWUxllpauF/5MWwb9hnEUVIncAmD1kk67XOO3UV8Yn9Os3b5KRDeDYhfH5yeKzpR2Ksmgpk9g31CreJxB8I/IhNd1El1PNdA9ZYde/hEsSDT4N7wwkMp+fxCXWsOAF+Qo3PRmm1s3f1bLt3VFXcw== 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=Jgq/wH9Nkk1z387qLRMZONU8/aXIlRlKZpTRU0Cuvp6jJln5mFF5b/71GhYb262nLfSiPRJTmgZw06FpZ7Wpmd2/UyZPKYq49lk2IZ7sQ11Evavtw/tv/i+RWcBIBrCfy8thZbt9+iiq2JKyBu0ZksjZ+r8Lthys71b9Gy/qc0ASERQLe8rBpZdC2HGPR5mupq/vZKuC4GogTxGXSw6yrq1TZiPSBQT9QnDlTpBaxKWzZ8YgQFn/2QlX6UQDroMfX88+HheQmjUQ738GOtvC2YWjEw2QBipc9XRdRFvb8s6oc3QlUUXCvdFMWwYY89FeCLJzYrHWQiIfz5swq1MIog== 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=Z4GpU/YXyEEv/QZ1Nvw/SzETlLNf/xkz1++f7qQc8vYpYz6kKqn1WHJoam/mYtGoWyOY4LmJNLntz+2zJqoI7rAAJ5YP7q7SVQKtFyIOqenFLQvgnKS6NdJmleWzEry6u4kS2zsRMcC01iKRjUIghX3oUCQr0Y8BVHEuRTz3e94= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by BN0PR10MB5207.namprd10.prod.outlook.com (2603:10b6:408:12c::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5417.16; Sun, 10 Jul 2022 22:10:06 +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; Sun, 10 Jul 2022 22:10:06 +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 v5 bpf-next 1/2] bpf: add a ksym BPF iterator Date: Sun, 10 Jul 2022 23:09:57 +0100 Message-Id: <1657490998-31468-2-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1657490998-31468-1-git-send-email-alan.maguire@oracle.com> References: <1657490998-31468-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: DU2PR04CA0355.eurprd04.prod.outlook.com (2603:10a6:10:2b4::34) 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: df608e25-9c49-45a1-8d20-08da62c0f1d5 X-MS-TrafficTypeDiagnostic: BN0PR10MB5207:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5fHebmAnGMZWjZM+GnACa8JZlUaYTtOP/f8IHHPSp1dRWmmzahLKDp5OT4NFfZptTKMkgmKz5cjm29lRi+aeOBBtPzFCUaqEBWQfFdQ27e0zUD/rnf4YEG4HANC5qvwjIf2dVw7koNbe2FbWq7HfJVBDx4DKyG4bh/djY6IPzAJ3zXaIGfyfd+BmdJGBDp0On4Wm/s2yROMMFUICzXDe0kuFzlzXMwLJ/q6O6gYBOlm2x7uDMqQ9r/g7bgbhICoCaLLnN3Z0b0P2a49F1vX1fTCOmZuReFxoSwRAv4AfXVx6N1KRf44ZdLPgJEa1vn0w1bC3izV5U0EGvyU/NQSJBHCYZBV96GllQJSMh1IS/PiU/GlKGQGK7Ut24powNCnoiHhqefABuJWf0QVxfMHlgDQAg1PxVsSuzoDzLRpArdxHvp+d5wrL0MPWazcegG674IOgw2B9CeNg33mcBL0f5zDbgMB/tsfwy0QW+hSFimJhe1xP3SdIm13YLAWykofCPzEPHXLQkPCAGj16rN04aMzzO8qKsBzpHfjVMNCQ+l6kqV0R6bCInycAHDtKb/ls1x2DXnP8CZuKMP7zZfM2ruHBFxQB1ETL5RAguVq0ior2cw2XTZ9mM8njQHRHcexqTaN7KeCQF9B7mmeOiZy+ljj4xwC9YytIKRwbQjQ/q34N9gTCMi01Qu2oekRfiLeXa3/OdJkRPy40JLc1q7giAmapHM05QrVLss+ii1c9PcFkCAhr2DdodqKV1HmAxXVPittgYLPoO/XdNe0ph5K79liG+hPRroRueuXgiWZmhW4hmX2HO9e7tHPnbrM/uaAJfcVSeN4zZUbsFASoEwAQKQ== 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)(376002)(39860400002)(346002)(136003)(366004)(396003)(478600001)(6486002)(966005)(44832011)(83380400001)(6666004)(36756003)(4326008)(41300700001)(66946007)(2906002)(8676002)(5660300002)(7416002)(8936002)(316002)(6506007)(2616005)(66556008)(66476007)(6512007)(186003)(26005)(52116002)(86362001)(38350700002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7OYGa0QSyQIM9lzeY9OWlFc3vCo93d6ZIsV7E4a2nu8zDoZnsUCjWQiTBq5gUGOYRgC68XHhxrulOPbM9zmMga80jI5oVBb25qUxyVyvvOW4l8BegrB3caQawluwSClUGiH0VbQmbDoWNZGJYT5+7AKTZq2KTyqb/mS64/8a3LxTsfooKCbXSPPKmcOxFiLFU9PpiFK0dnlUBzo40gGGBO3Giit1e9elH+whycrdDRGCcBQP0Akw61ICvOCdD5ZdES/GIWYBUG+QHNmlySYL8oF3AKdECZNNeG7il+zW6rcMM+d9JuqPfAvOqwDHibCAnfd4FVp2i8lpLA/CWVWNAsZMHE4NQPH+VTt9rVf1AkIrqzGAhcstwnisSAKlNZEOUmqPxhNgN8odSHr0GuuJBphNU0ZjpSsLf5vvhfV++GinN7Y26F479yd9irI9xC8cFjSBFA0SxXghxKIplTDZ2oZCdoR32T+W1/Ctfw9qADwEVBZSaFuushoIJjDtakG/2dSAMPfT21loYogmyMmoQYaX52mRKdBgyilwFODTf/HiSiF2V4VIBf0W2cOmB5+jr61J61/O/Gg0imvRmKBWl5be79I51siIN9ifi1PbyEEXouL+1RMU+eVdwCrddTentlwuZYeDdeRdvZ2JjICiEzes3MoJI9x/1+kx5ouQatKxrwb87qWViphsWJzdAxHMAdEuvIC1exO/vn9pHutjBNgkS+MX+ycGJRzrLF/mXR7qMGoKAfO82xn3J1CYmHOAFZqIj6cSWj2ED7dzoheIg7K0OkETEhlwurByIJk+DwGDdTKNwiNVxM4GJjel3HGV055IOdhRdzOXEki2fOsWrD+xC5ngvKfJBl1UvfklqsNMAuNo/eMDlf599I3F2zdZpTx2aPkDxtpZPk6Mz1A1rZlcOmnzKH7e0cKTKwGzh6cqHgAR0V13orJMuziGCZJxrM7JTSkxN7rbBLtCdUtlwFKIp7lmXTP8RT4B0CvV7sydDBeVIyi/xDpTTHvIOCqp9b+GU5nYm3nC8nfnaGe++05teoCHxtHHUhRtmf38zaMx8jsnn0moSM4PkjwN0HBnwnrq5D+dleHm62ohlI+3e/WWlRTUKwulipW9Y4ftHIj0sK5wLQw26iMueRvCeZeAh1GlcB7Jsh7nvtgXgta4yV+XMl9vadzwLtdYAMrByp9ObYHsHm83g8sSASIY6iX9jlQkuEia5KR6NHY+s1kKIHd88NX4VZnQPmPKvsNwGqE1sK5+M8eomju92Xx643bdtTREGfg4nlPTtomv7KXz2Ijfu/v7HajAKzW/p4CyLuqL6/gpTzaiW/me8ns4rOUJcZiTz4Hxu4jySun0OpLtbJzCAB8JVoPSOmdJ6KpUt1Zu0ByVAcHUcPvbCfadxDGpQzgNZ/ucD+8PKiH3knJyb0yqMtsPg1NOgv3Gi7UTWCclzuKUYGZu3ElK/Yx4NkO21rpon+R3W3Nt06XpBBsLaTWIeUiqRHeS1tt2Zi1rkbWtgtxKFASzpO1Y38OFsQgwIVGesZtMKpe0gQqQgDap21ePCZP2Z4dpmOPGZL7NIu13HnygHBd4b7a+o3P1B/yW X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: df608e25-9c49-45a1-8d20-08da62c0f1d5 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2022 22:10:05.9406 (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: 1ek246sBb6dFJzKZQcKcYmr/+5w23vGKV6jtKXlvfS3eLLULI+8Gfo9QuANuUHa5xXnJXBc6rCYhF/4vJc/mXQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN0PR10MB5207 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-10_18:2022-07-08,2022-07-10 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-2207100102 X-Proofpoint-GUID: -2w1wpQCYnHeZniZeZKsXxCSis1u3mD- X-Proofpoint-ORIG-GUID: -2w1wpQCYnHeZniZeZKsXxCSis1u3mD- 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 Sun Jul 10 22:09:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12912704 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 9B5F5CCA481 for ; Sun, 10 Jul 2022 22:10:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229624AbiGJWKn (ORCPT ); Sun, 10 Jul 2022 18:10:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229510AbiGJWKl (ORCPT ); Sun, 10 Jul 2022 18:10:41 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 144089FF7; Sun, 10 Jul 2022 15:10:41 -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 26AEbH1E013818; Sun, 10 Jul 2022 22:10:11 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=mdDJHa1Jke1cSpgJlPn8SVJFYbF5UAmR4s2XTPjmMqg=; b=SLpHMucAAmcbbSWwli8Gj2ahV5/JJEbguEMXK8qGR8qcCor4DAo/rBEcGVfcGbkwbYTi W4xYPpo/hfdMe+VTsPAHrIqRi8GmHqRoQbwbqEOfs2F4ztofo2yJrKucOl2afWaOCfDO vCoqZW19grCSzm7AiVFCz4em+GEhoriwlr/wIsCIUSRUYjemsUZ6ZehU8jtgLazTKE/J fCotIibSzGJ6FFPDg5HOtrrAzLK5qlPkfc5A40lft9KtyAaUqYLVczV7qdpZrgj0DWio cgl1NqGwCNxLsibvaBC1OB1a3GjuKukYrxkUeehm+Uz/f3TjIp4dbkWQqIiy+yIAy0eM uQ== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h71sghwtp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Jul 2022 22:10:11 +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 26AM5TCf003457; Sun, 10 Jul 2022 22:10:10 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2169.outbound.protection.outlook.com [104.47.58.169]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3h7041jpku-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 10 Jul 2022 22:10:10 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BReFCB/3W39po/EsryTIGnekE9xzlBGz1CbITM7YjHPHgkHFyZZrwuhyBjFKaifNYvioQXd0K3uTQ/6KZjhoHyS7RRT2FZw7pzxC3MpvWaIWyZuFN74LR57DZXgvxio5UUPT1AR9UTPFGtu13qzP0WT1md27yUF6qwOCk7s3R6OmAcSpBqlWhWiv8pf4fkFc0HBw+VSQoBIsnf0Ib3Lv5DXwbS6TYEBkrzEZs2D/o7KUYTIAnxSoJ4YQaTdXcIctN27HpIS5ln2rMKSwRPFzJ1mA0ayku9XxSDWLewPTjmUwJmEUucyrFoGNkvOjuaDMUpughjnBdDRdc6z14QGxOg== 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=mdDJHa1Jke1cSpgJlPn8SVJFYbF5UAmR4s2XTPjmMqg=; b=PC3EDV3IkWPni1Fq5WACBGq4yh+0csHV0XIDFgqMT2MbuSplQJSFMxHnaTH+Yyy2NGTWCjhaHsZXhXtzsaAVmGczr+FgJG+EriPUpvdZYDT3vt9GIZpRUDMJg/x2PfC8OAhF0htQmnvdoFzOGjOAEjxpD1UNuec+ikYe0BHcfkiMle0Z2og5KMk5t+sEllJL4GhS7fpXuaAPijy4VChlBElfQ33iKkQv89Bvh278A4DxZsQcitS4ihCUzkEcNSJx+zKxWJ+iUruMAFdgmdAnxNzHE0+YNdE/i42NuwjzCWcMkZw6MYjEbQlhGjvN0C92Pu7/mfqosaYlzO2DVJ2KcQ== 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=mdDJHa1Jke1cSpgJlPn8SVJFYbF5UAmR4s2XTPjmMqg=; b=nTaEJM6jXeM3Mh6cdslKHQNHvo8RFHhsuc/PYm598neDMsqP+wjFdSLx/qpmGvwG+JUgFpLQaKYtyFSJ33OYnW+e+iU3hR7/0zuey2GO9xfqKqoPNs6Dsdk4kYTUEDxxZh7S0VPEhOADzzWSW/oyzZyeABlkBlEorYeJZouqZzQ= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by BN0PR10MB5207.namprd10.prod.outlook.com (2603:10b6:408:12c::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5417.16; Sun, 10 Jul 2022 22:10:08 +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; Sun, 10 Jul 2022 22:10:08 +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 v5 bpf-next 2/2] selftests/bpf: add a ksym iter subtest Date: Sun, 10 Jul 2022 23:09:58 +0100 Message-Id: <1657490998-31468-3-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1657490998-31468-1-git-send-email-alan.maguire@oracle.com> References: <1657490998-31468-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: DU2PR04CA0355.eurprd04.prod.outlook.com (2603:10a6:10:2b4::34) 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: 2918a020-4e9f-4080-7f71-08da62c0f33b X-MS-TrafficTypeDiagnostic: BN0PR10MB5207:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mZZptx/OX9DgD9zP6RYG+dudUCNb6wZxRiqGB1Jr2TO8Zn4KJT63WWR1WTljPbeq0E9zDgE9cxsj39CdxbLaD27+aU4WdKnyLA+0NB/dTfoj3d7J8Qk3NV1958Hb0d1ace3HKYo87iyVf+AdxhqAw7VzSt4PboMzHFjEhQ0MYeagfxOBLovocN5En7nYWAs7V/aTqX/7+KM3qsZVLt2bE3+IMg+Ne/+I37Z0pFY8TgOgPtD7FKpjxIfHkofVdomVRwZZbA5VsEzI9uR+3dlh6XbSxg8eNi1yNNPN4guFD1HYDTzXzR33MEEhIni9fYQFQowJNtvjzEoxjn87w/frZMBaz1CZDF6tBxo7pt3A7XTZoDZBe3u1yEF+3JX2kh4/lrfjKGPsWiMQbBdvKF9pKOCvz185yeSZdMyBDqa1/SoVFCAym4LQ6Ijibe8mYttboIVXIPDgdF/0KHuulgcCWs7D6576JJLBiiA5BQvYXDc5gT1OvVyMVZzn72+S6NukklYxTWLHXnzaSD2yYXVNhyBqzVCe8AOFp/tVcVnG5IruSdl5MHoRknR3ZsBuztrNrW1/gb/c/JMEGoGjdWhtbBB08L2rAbWTAnshMsvLhFY1BBP0lJKw1ATbCx+lp6amPUB6bjFg/hT+O5Wtvb2tYveFzJtafHXwIthk/u6+Y1lsZcqTZD7a54FefEPq7OqoihFGSE0obLyMunontte2MNkTQ9DOxGowwKzRp5UDlX9PvenLE5f0IzZxwPezb1JLM9dTv3L9ps/NWpTTU7TTuVdhNcUuvNqIw5H9g6S4Wb3XmsTtqzrj0jaWKWLZwfxx 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)(376002)(39860400002)(346002)(136003)(366004)(396003)(478600001)(6486002)(44832011)(83380400001)(6666004)(36756003)(4326008)(41300700001)(66946007)(2906002)(8676002)(5660300002)(7416002)(8936002)(316002)(6506007)(2616005)(66556008)(66476007)(6512007)(186003)(26005)(52116002)(86362001)(38350700002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TAwnXJBrtqh0mzDnaWh5b8TyBdBfdh0slzckPFqs09rg6XAgTCPjnlfBEgLubgmXaGe3We7yMWQVBacx4jrV21Y0Nak//SRzScOTI7wZvmOpY5vN55DvIw7iMM2F1bKspfGTuNP22oq4RY2M6uLjkegHz6lCvIrfOHbXwPAZpZIMEWtSA5mlcZJ5T1KZ0d8A58rWsF3JtVTxufdjE4l4cUvKool4YQWR4OHVosxWAIf+c0thV/UBaa6MezikuIItDAow9TRspua0Hk+fKR3jqVFjLD+v1/1HBf1CS22eV8geymZN11WGmyQKBRmAOZ1oS0gpwcjWGo8m9IC5sn05GgS9mpzfrY+p13/hJCf42jx5IAs9A8PHZ7xk6Ts+sH7lPGQpf65Zz8okiuGX24Gaj+XX0haVUcPv4wB74SgSDaAxuUw7cWnWj3KIpDtTeM4+ePfhGkE9vb9w8z8On1uKw5IFhsBzqIUGWy9XLnjYcqeR2kbz4yCGszMfarTm+af2zlsREQ7rXx/y8m/QRJaACMQYIezfYm5Hxn1A2Dyzjfigkb1lZmUwJ6wvV+kyIlwRIs7HVukVoObjxaIy038wGXTY5dd5jk9/Nw9/LODchBY8oMPMjLDg+yct3udLLqCCcc2EwGDcIl7plV6ALk52kJzcAzefFpEKRAXwNFJsbTyumvPXeKETO62jLuoskzJdl+5j6iVWnUTJtOqSFVturpVtdbT/+4Nm6bjSrLDeBHfhbXfeEiGDaQTmmU5sis/naqrRQkvQAkctkH1oV8ge+2kwx+5PcVUdQ5MfDJzYXYEZoYu1fpNUqVDr5+uyZ++bQzZJaMlqxeUeX0gIyWqMmn5Dvumqotp9fxZqiR13GTxvGAeWHgzC5EDBjjNiDZ+/KouOaJuunUVbbSzZrNwQHo95w227MF0BuvhthWrr7wEg+SMzs7YSH2efxrCZY2guU09iFIP6cxQ+Wy7x4qM6wxIyMkud3fNTG2S+5WR/I8ELT/ErsHb85IcTIkjVf8ImvCjCNTwWMchNjp1+//bItysJlJTFsXmfGCy+qJh8ck7XNVAKVdqBTPOrfhoHao/hIZyCcsnTSabKtAP1b7TmzeWlCzgnR6AZ/YM5BzhS64V8HbrRtUxRVe8D/x//T4WI2mos6S10AJUCgDhjFjJ0tlJdtOE5wIhqpjyqrsmcVLzoX7x5F9mKf7b3FXu5ZhBrB3DLsuAvqK1ott9XBJohQz18Z+Ni9CLKfPfpcQckM2HhK3GyVMdoNDyX80x4Nz1OQpRNO999hn6+3CgxMWWWGkGU+Bb1jW89Pu0KF18FMiyYqeet64X2keeO6cUpP7pe6PeXoTh9vUJxf4kbcE0grTu6D2bNb0Jb5bux5j0dkhvpdTNSYrMPKbxDdmdeelb5VxKIxy0c0hh8qqVnw8rE4kijkQdvLdUeZAJ22q6n/f0hgf3QEoa0YHWyEcmuZgTQVys1gYZRQ2qj9PKYXNyJLyKXNNi/xwBdhru66MMrqQV+149yYv2RzLo7C316XVTQMWBySBQPdz9U9IsWTnE9SPRnyVZHtLPcfj8niRP0EbZE7TblPlhXGN3UHh/4c80m X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2918a020-4e9f-4080-7f71-08da62c0f33b X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2022 22:10:08.3007 (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: zzBYrfX6qb0ff9vTvtH67M5E6K9mYRvj1G0/fbiz34/5+7rEpH83gkVcGecKtMCXZOn7Ea+5wKE/FoZCstf9Zw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN0PR10MB5207 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-10_18:2022-07-08,2022-07-10 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-2207100102 X-Proofpoint-GUID: q1BmFoZE6iXT7r_X0hDBSBX6KhMuGkQP X-Proofpoint-ORIG-GUID: q1BmFoZE6iXT7r_X0hDBSBX6KhMuGkQP 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 | 32 ++++++++++ tools/testing/selftests/bpf/progs/bpf_iter_ksym.c | 74 +++++++++++++++++++++++ 3 files changed, 122 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..4b23a08 100644 --- a/tools/testing/selftests/bpf/progs/bpf_iter.h +++ b/tools/testing/selftests/bpf/progs/bpf_iter.h @@ -22,6 +22,8 @@ #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 +#define kallsym_iter kallsym_iter___not_used #include "vmlinux.h" #undef bpf_iter_meta #undef bpf_iter__bpf_map @@ -44,6 +46,8 @@ #undef BTF_F_NONAME #undef BTF_F_PTR_RAW #undef BTF_F_ZERO +#undef bpf_iter__ksym +#undef kallsym_iter struct bpf_iter_meta { struct seq_file *seq; @@ -151,3 +155,31 @@ enum { BTF_F_PTR_RAW = (1ULL << 2), BTF_F_ZERO = (1ULL << 3), }; + +#ifndef KSYM_NAME_LEN +#define KSYM_NAME_LEN 128 +#endif + +#ifndef MODULE_NAME_LEN +#define MODULE_NAME_LEN (64 - sizeof(unsigned long)) +#endif + +struct kallsym_iter { + loff_t pos; + loff_t pos_arch_end; + loff_t pos_mod_end; + loff_t pos_ftrace_mod_end; + loff_t pos_bpf_end; + unsigned long value; + unsigned int nameoff; /* If iterating in core kernel symbols. */ + char type; + char name[KSYM_NAME_LEN]; + char module_name[MODULE_NAME_LEN]; + int exported; + int show_value; +}; + +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; +}