From patchwork Mon Jul 4 13:55:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12905396 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 C8E6FC433EF for ; Mon, 4 Jul 2022 13:56:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234755AbiGDN4L (ORCPT ); Mon, 4 Jul 2022 09:56:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234607AbiGDNz5 (ORCPT ); Mon, 4 Jul 2022 09:55:57 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BD3BDF1C; Mon, 4 Jul 2022 06:55:51 -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 264D2QMG004721; Mon, 4 Jul 2022 13:55:29 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=AO7OMtcTBdq2gtPFmgxqa06O7ZYfBQO4Z7rrXq4TK4g=; b=OwhWAnXzbV+VkPeRI1+j8XRBZEFS2SUv7dQwLNwrWtQ5qOLu/WVYV2mu5TejwgniKplp G7OgMW05tKbjS3bc90HkJUtB15cAf8/V0esb7hTFPfErE9+AuRKuJib9HyvDwkC6ZaFX OcRT+Ixtu8wLm1IjEbFjEN576aueSe5RzAGwQVz1f/hleU6ch4wHAyenjNQGkK0ibiKP IrsxvBVRIF0AIszIl0fZ9O7xNUBMN43kqclP0Lc4qQJL/sf1f2lljBEH7FMRU2cP5Fqr saaiMvfB0WbCb/0q9wuCcJSrrptBsROtQE68V0PSbe7Z/O+PjmX/T7CR/nAHBOv3pLgH xg== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h2dmsuhau-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 04 Jul 2022 13:55:29 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 264DpLW2021264; Mon, 4 Jul 2022 13:55:28 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2103.outbound.protection.outlook.com [104.47.55.103]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3h2cf7s00q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 04 Jul 2022 13:55:28 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SiYAqXcuw2MUaRWm8T/OOTlmVp8xOFvfmZku0C3nduaukvPfWBTRkr7nXiLLt2AYQCt1v3X77Gd+qufy3wKHf2R7K2MeUDEbnC/e/FXH5yuY++VckG2GZ2Ej8Otp4cKjck35qQSSxUe6vlS7JgBhfqrtasRnzHRtO9sU1S0mx6Iz5YwuE5B4TnLHH96Wb6z4Ez4SLNyxA6g3gcD5oOfDkpfwnQ5lFy3utLa1IfxqnUgLZ+A0GidLqpFViX0mKaFjRktVIGYj7Os2LA+z9CuZHymbuyK+dPhwmSvuXKmwkogWJpw6HCZzxZpmvDAU87ARWNeCDyCwQfIWhzSKqwKmOA== 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=AO7OMtcTBdq2gtPFmgxqa06O7ZYfBQO4Z7rrXq4TK4g=; b=WoN4CQy3g1fBm2b9brkKOPmZoagv6zQyRQkq1QnUa3yZkUAkq7ZZDyyQ7prhRpPNWSGz4Auo9aVzmvORRVJEnIm8Dbk4MImY6z79os6OShEZp/YN20Kgly+52fzTG4h6qANd2EYburXKK/KXgwRDoY+4cgfBuOVRMYAs1sVvRaEE+xJxnJLhWGK28UpkeqcK6hrkPwqa+PJybGtBE4aUR8eUJyPlx7Ybd0V78pgY0tckuKTxNZE9RDiqp8dQgyw8AteoC2+r0PgNAy85ifpKZL5JbXsgG89+MLWgMkyWlJsF5d31d1GbWY7j16He4dOkbchvaxCo61gdN3rr+KwmDg== 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=AO7OMtcTBdq2gtPFmgxqa06O7ZYfBQO4Z7rrXq4TK4g=; b=Sj9njxR7yRZ9U8Q1XSQ5L3IAILgM4AJUX1UzZIQ8TVRI4INaZqDeGHdRhqlifWQVFolA1R7xJbOZ6lRTMGIpv8s4puHxoj0BkvriVtBwYIR+vfwD/Y8qVxG5jUwlz9LthFI2NdnoMIPqzHjA7NUDfoVKSfF2FGLzcWNNg72sfAY= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by DM4PR10MB6086.namprd10.prod.outlook.com (2603:10b6:8:be::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.20; Mon, 4 Jul 2022 13:55:26 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::ec7b:27cb:a958:e05e]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::ec7b:27cb:a958:e05e%7]) with mapi id 15.20.5395.021; Mon, 4 Jul 2022 13:55:26 +0000 From: Alan Maguire To: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net, songliubraving@fb.com Cc: kafai@fb.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 v3 bpf-next 1/2] bpf: add a ksym BPF iterator Date: Mon, 4 Jul 2022 14:55:15 +0100 Message-Id: <1656942916-13491-2-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1656942916-13491-1-git-send-email-alan.maguire@oracle.com> References: <1656942916-13491-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM3PR04CA0137.eurprd04.prod.outlook.com (2603:10a6:207::21) 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: 1c5f5a12-00ed-47c0-c0e8-08da5dc4d8e1 X-MS-TrafficTypeDiagnostic: DM4PR10MB6086:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: x/CCQo16w+/51ZWurrr7lHmzF1pYmRSj5haRSkRnN+K1Mk9+vn7BsQhuGA490EtWKLGSm/md5jrrwifR2x4CPzD1q619RslB8giHu5p3oSL1iyxbpG8veCDaqaOx84+Qy1CGrrx5D98/pYql0dLJhqPxdAQ/w/dX14TXAcDIrEqwJEufREeINSgM7NU7w89/EXFi4nrqtoym2sB/pRu/JqOkP6WXA1JvOQb1UIe+ZgG/2OiDUxUhoL+W9Sfdesd/fwGwcFJGiQ8hv/4BP+Uh3139zKhESbAUDBgpHef50LHqRUbXnuNr+SV8Bvd+TSmQphoN57JLisDOpooYgF3MXHsaN1s/4I1mirPb8b9KW5Z5QYkz3tg6KlAxQ8NSsj3ondltu2OqeiCGqZUkk3lF0Oylz2K2o3ni/Ifbr6KTzxlJSJLC7jmJvqW3NVRh7qPqUdun0LILcYICSk7+dTP8bRsGfF3nfbSDjolJwHzyRMLrQUNeRDY5A6xeglDh4XdwxPLcyx3/tLs1BIUSt/Z7rjVr95DWJZ/20vFzt3PHCzLbO6C80iE0FvmUQMc2XRuQFU3yac3yBJvqxQ/hoY+aE3Dm/BxjMiVBFkxRo+8A2FQHxpBNy2+X4dd9PmHksoB2kgXNAvgzGLYPTLU9iZl8eXZ/rMZmhpqfAQG6kikH0NmnCYefdw1zB6XfBEf6UTudKWKLqgxIb4/R/HgOLhGCcPCbASFwCy0CcWanpYr02JOARS69y6W5IjxFV1k4r7tsjwLa3BPTRUJ7MgxPsz7s3dlb8spsusdqwL/8kMPi9uk1uFcFxyiclK/P4/mWwbuFJFVYi84Qa81UQfPOMHRMhw== 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)(346002)(396003)(376002)(39860400002)(136003)(366004)(4326008)(6506007)(8676002)(316002)(66476007)(66946007)(66556008)(966005)(6666004)(478600001)(41300700001)(6486002)(8936002)(7416002)(44832011)(2906002)(5660300002)(38350700002)(36756003)(86362001)(2616005)(6512007)(186003)(38100700002)(83380400001)(52116002)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: OJhnJnQQtl0yvYrtfJ3HkZ+ljbaq7eKxBTUxY5L3m5yl9gzyHBoZhiLji3agb3sjllXEKB+PtpVlWBsfAuuAzhYf/ZcOps4eWt3qtPO4L4PslrQDPgFduVN3PMss9Rw5z4AE1G/Oc6k04a0ow19HdpziPLRaAappmDXCq2d5xrCjwFA70vszFhS5VcDRh/GHSY7tEAyTb0MWi3NWqIxpzN0ONs+xxv2hwGJODejLhiGIIujPZMlqMOGU4XAKPnsdiiMM3gYFGgL8HdY/rnrnUmKHaKQ3TqyopawWjc/P1KEl6otYL3cz7E7I1Hd5jxxZaJ/pTv6kOQvJdHnm2ZrtQHHvvqqiHfaEqS5e2KKzEPnSEV4X2SjxQD+WfXgCVF41dSdUFdNtkLO2N3o59ZN0ZZ4NbqdqNjF/n5vBzNA3Mu4IBSrC/oOqrfLcoZCMymOgkcdtVgvHb0KsCSczbeRLGP9adJx5+K7LgmwRp1W1WsOdypm1zTYwG3gFee7RqMAu+JoGAygu5NUpV0D16/yNK/WjSmg0RI9SouxnKfJeTvIgBAcs+H8meT/SyvFvhsUiII5OJv2KRZUkDXOjVwaTPL4Wmuej/X5S38ZEg+XNFx+HRZpTiAr4/roJlic7G0ATYyHiqsklIoBM+9pJEekTUzRBDcKcaiSY3wpY11kfmmAvxHNC4AaQ300wLvMtC+woUFRiOGoF/Zmcq5As9Hp3arb204UZ2FzrYbCWooWA5v5gQT1hfi2sjqdIzCjW82jMLXkzCyY9xNT96EyhAgnJ0mP5r/C8e7H+uiWJL+Lk8xNEZI+pxYM2R711OxajVbkjdUxHtQmP3k86bHlHm+a+0qQ9d5klslTxm1r8ufbrBah+RNW7q5VaYKqkzyj9w0e7hHAiTqMiDVIBH+Yic4gVe7Jww8bfraNees4b7iFF/f0JnSkQ8m/+ZM+sSipca6qsBD2rXlabSQ5A4V8EETqMvX1E6RK+MKdCtbjKtcRPgeIraD3VVe+xlHEypI+303v2OtYVSblliVhzBMmm7rd3fgVbuvJTS0Socqz6bfc1x145QslDLXSO+3B6vNYktFmC74cBhANc4yI9zebPQbXf5gzlGGitWgsRykxsUQ3+TTC6LpyKmwFaovSlLYSTekogx1mM5rU0h+HuIW07DqedELZpE/6HrI1jbcf2O4ymazOLNXCFcPj4GaYz5y6bAPVmeN7H2XkEJVC9KE66llak3Y2AP0eGMrFup4pZgm7JlxaaeO1mjh00TU8aRtq9FPnvSR1sK/BlgcxJCjpVZgMDhYFwXlTGqQvV09zbgQDgLZxYAn5jS6mIpBlZZHL3PDCMN1nxoTl6R33+yrtUncjCME1931oH5eKblZ2YgSYYOC4xe1xaw+ikqBaCcSh8BGfo7QR+qGubfPOruODl/9n8RQISn4fJ1coW8/J1NvdGaUB1eIqU3ymeXrbmiFBeKSLDHb1RNksXISX0yk89nf3x/7Gfx7s4XV+YnJEY5BYj+6jjUtNhnGEBDsiLyDnWNQzKF2Z/H+T7SSzUeXirUExxZQRfIBBNnDXl+SC/NhgyphxJXOQoqK60iy0XNg7wuN01 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1c5f5a12-00ed-47c0-c0e8-08da5dc4d8e1 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Jul 2022 13:55:26.3273 (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: 6pFfqug3QIOFEd+erjOptw+CJ+0ZIbZwUTMfuL4jXVzHDywY2yV8yJNJ7udnU2Tv9KYATTOFmv0ccDaFf0Kv8A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR10MB6086 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-04_13:2022-06-28,2022-07-04 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 adultscore=0 suspectscore=0 malwarescore=0 bulkscore=0 mlxlogscore=999 phishscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2207040060 X-Proofpoint-GUID: tTSTQ3DPxlJYr9wvop5g54hR_0kWMUsj X-Proofpoint-ORIG-GUID: tTSTQ3DPxlJYr9wvop5g54hR_0kWMUsj 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 | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index fbdf8d3..9bd37be 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,87 @@ 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", + .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) + +#endif /* CONFIG_BPF_SYSCALL */ + static inline int kallsyms_for_perf(void) { #ifdef CONFIG_PERF_EVENTS @@ -885,6 +967,18 @@ const char *kdb_walk_kallsyms(loff_t *pos) static int __init kallsyms_init(void) { proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops); +#if defined(CONFIG_BPF_SYSCALL) + { + int ret; + + ksym_iter_reg_info.ctx_arg_info[0].btf_id = *btf_ksym_iter_id; + ret = bpf_iter_reg_target(&ksym_iter_reg_info); + if (ret) { + pr_warn("Warning: could not register bpf ksym iterator: %d\n", ret); + return ret; + } + } +#endif return 0; } device_initcall(kallsyms_init); From patchwork Mon Jul 4 13:55:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12905395 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 DFF2BC43334 for ; Mon, 4 Jul 2022 13:56:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234719AbiGDN4K (ORCPT ); Mon, 4 Jul 2022 09:56:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234615AbiGDNz5 (ORCPT ); Mon, 4 Jul 2022 09:55:57 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5026A2720; Mon, 4 Jul 2022 06:55:52 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 264D1ht8001897; Mon, 4 Jul 2022 13:55:32 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=pObFwCVo2FoHciZaUaaRdKkaYDoKG+CZ3gSiSK2FBz0=; b=iJTVxMp+JW0OxhK0GfoZn8mcHYKwtKLgPHBDurjP5A8bFuvik8rN3HSj2aLJwBTwYJOR IAeQr7HvK+1UKtSCYBBCXkLgu1/iHAHWoQDmads477wRuTP1lPws18Ui49RWBCIAptgl 5MQ2OroIQRrTc8PsczJOAFAoJcjWM+tP3b4P60oTvEtGiLWIqGeTh0sZfGswIfgTqF8b 3HWhuaomwF8fs4jX3U1KP35CqrdTUNXokeiBbqm8BRka75IvHE/ibQcOU/FCsvuu90OY 3GtvEWzYbmioKcH8kmZb3S8rq2jRol186RjqIYjNuS4hTE41P7MCKNOkDwjbC3zWUK8T bA== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h2cecbjpb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 04 Jul 2022 13:55:32 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 264DpBDK028675; Mon, 4 Jul 2022 13:55:31 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2103.outbound.protection.outlook.com [104.47.55.103]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3h2cf7qrh6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 04 Jul 2022 13:55:31 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lrL7saNVtMuFMDFgRkTDJtIORntVSVAD4q+NdqedMVFUTRiCb6AbsKgdX8cazb2dB7kDDX69reKMFisIx7b5A+I1O7I10vDsLdmxctoHfISoIHrD7r30ci9lhfOZxAjIDJLPMIYrFYQCPvd9czM/FH+M54Jp1skVir8RgMw91NUibAya0+tfiP13fsnwRGcTH1ZIHjL8Tm+vQMlIltNbxSJnrmzKHNSko/Q2+C5eB0dCvMrU445K38+gTN3w0MDzkAhvzegqKxAl1dYDlGmSrB4sQ++uQn9F6KSeD5vob4+xGohCyvu8ndfBd3t+1dpQPJNYEkCLjzLyFlPUikt5IQ== 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=pObFwCVo2FoHciZaUaaRdKkaYDoKG+CZ3gSiSK2FBz0=; b=GA+9iIWd5ihv9ySVFQAzDojOgVgqbIuL7G+7hhMX4C581xgNpy7AtP14DkaTrBYljzc0gaba5IMtFy7nT7Jiss577IkvKugpfHG1Ii+9lF89A3qfbuLR2ORo5VGUunj6F5IKLFxL2o5kWlTiNdiNj/kpkoRoumeUDYd8Cx4KshMdz+Rx170E5GNqbFp5nY68k8Iuw9iEQWEs8xSw2Au8RSwVxjHgObQNtJ8JoDhE/jR6SfOPd6Lf+WpYJf/NVHtSr/vRJtuiekqBrddN4lPGL7mtrmr6jgPVEvWbSI0Cdj7a38RTr7cJWuwsJGXvIOsTDr+bgs1UtmNgBMXmgMrzcw== 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=pObFwCVo2FoHciZaUaaRdKkaYDoKG+CZ3gSiSK2FBz0=; b=O0uTdXTek5uCwe5Aefu5IprLl9cjk5b2Fc/218RykHQgUN0KnKmy3GhQMyvOokDfmk9FhgiJbiarX7vPhC0JRMr21s8GA4Y45ti2jNxJ9kUoDaPa/4FO6b7Hztm++izV4SeNZwx4lXiFhBC2GqtVB+/AGdOkgiX2cvb+zwv10HE= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by DM4PR10MB6086.namprd10.prod.outlook.com (2603:10b6:8:be::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.20; Mon, 4 Jul 2022 13:55:29 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::ec7b:27cb:a958:e05e]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::ec7b:27cb:a958:e05e%7]) with mapi id 15.20.5395.021; Mon, 4 Jul 2022 13:55:29 +0000 From: Alan Maguire To: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net, songliubraving@fb.com Cc: kafai@fb.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 v3 bpf-next 2/2] selftests/bpf: add a ksym iter subtest Date: Mon, 4 Jul 2022 14:55:16 +0100 Message-Id: <1656942916-13491-3-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1656942916-13491-1-git-send-email-alan.maguire@oracle.com> References: <1656942916-13491-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM3PR04CA0137.eurprd04.prod.outlook.com (2603:10a6:207::21) 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: 282ae8a2-80fc-419e-c522-08da5dc4da93 X-MS-TrafficTypeDiagnostic: DM4PR10MB6086:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qDwABjghfOGT0MSmrnVqSoSJW6tySYSJQi20Kjf4cDcDH/KXOG5fl6xtWVbeTTHnUP9iTZKlTVsu2TfQiW0EJUedh+BgPGEXKovX1iTuLr7U2f/aAQWUnz3biuE5j40F6hLAV5mAqoEK+qF1kNlBSYsZ3uXowsSbzgqdMMMEetywYw1pC3gHscJa092/eUTr8xc/ifnjbmJLw5kEnFvRPiwsKUyZzB12/O7LX1TI5zkB0s2EKAxZAJ193+UYmcyzkS2HEwEPUbPgZu0tCeEcpAyAVQvS9DLX7ZRpp3oK2D/To+LwbnWsb2B7aqiu1BOv6a0I0TFj/AUeCct5el1/Bnq+nQzYmsIhTPc9Lj3XhFuBVJHmrgw2M3LABJHwRPooyDWcjHaVi8VkmKt1H2RmyheaIkA8z4a1Z/PP8jo41W9IYrN5IDzts8AUHe5+0KkU5U4y30X0yviCdUQsFTPwuQjymFNVoWnyUaICoytiyE3WekYzjvO+qzynhBCXpBg/GYS7dsCKDPaU3hMB0WfKNcJyVrArcxxRlILUxK2NZZ81J84pOTkGAZmaXfbGm2dvGSMcTi+czhEV41k5wqcjm7WacJmCyuof0trocAGoRkJ9vCzGSrm2WegtufjKGKOq8i+Quz73o3fRgZ5JkrW6to090M3QOClc2YRYvpZhzF1b0IdRyEzxKmHB4xzVyyrxMTUs361w/1RHnyx5Qir06dkrL48fCgCM9p9mc16ULG39fi6pbzZeLjfAj5IgLREAcqkdgMojcPc02jIKHsMyaYCQc+qJJXS6npYCXf0qxRp+WEoiWk0h5eMzuvt0APmE 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)(346002)(396003)(376002)(39860400002)(136003)(366004)(4326008)(6506007)(8676002)(316002)(66476007)(66946007)(66556008)(6666004)(478600001)(41300700001)(6486002)(8936002)(7416002)(44832011)(2906002)(5660300002)(38350700002)(36756003)(86362001)(2616005)(6512007)(186003)(38100700002)(83380400001)(52116002)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: dBd13r3gYwPYcM+GOVCXY4CKFFJLqD7qytD7La4UUjkPU5HOj21D4mPJ+pTJSd28lnOsaYut4/dR6MZZrwzCVmQkkdGcnV/hOhjxvqYXNpagsTl2qppgStvNbNCMdcN1HqNY80lziGocyRy8WIjCE77vxOwa6rxQz0Qm/xTKTLQmt1wF74pt8N8HcWpYr3smKnqnBGWUOEberqSBZGikj6TcHGAPr4Lpe7CHiJav369JdyD9PmAui7ChATJTgIdYs/Et0XPY4D0SHfqUr+cepumbmaGrJKmidOhQJ1n2wCteifcoKW+45NkTzGFFwr3S9TLqTx4wwkNRUq/CpXLyjrdoBIx6MdeK6gV+RC1FAi4DklC6ocnCnL2+7HQUqCvb3p+330REXuLd5NKkZN3sB5B3ryD7DD9p9pqeIIF3OmCpfKtz5A8d8sNNge8PiDjf9nWv9023LkkL+In70/UlICocGP9VMe4kHMuimBlZnDnykWr6JL6kv5X1H6btpVq0xJh9l60CMOEf3OEUW+k4Ueux5Emb1b2mJz4VwVz1zyJ3t6N+x1V1O22IS6Td47S/EWK230sxtPCXdAHT9Ehv0KQpecmOnO20iTpXW4lLBOTqdWks9y9vqcVO9WtoBYXpEM0/MVpVgG24OGcICG6TG478LpiBen6Yk9SAHkLSglgWO7vWrGYidQLDkHICXhCIVKV+G+w9dobcnrI0jjl9v6QGiKpHnZDBQtGheSAiGW8s5log89S+vhH55I1LidQtCU0OnFSDskaKey0GWcXc/zib+kOfEcX7SqE3mYMWQbBKKPfxXZDpWsnI3T9LVG5xZXNXSGQaYC5kyDjPhoBXlv0VYsEg0b/ejJDXm4rF+BxI+HquE4dzE9Dwlp8seqHJBFCj2pF2e/I+pCBoZUbOUG0AVDathJXs0Ix8QvxFhzp/PvPjm8F3N00fuAipo2BwyLTemf+jOoTesAjh+nAepre4fK6Do+pHrftUklsCGeYdFs56B1udmDac1/pd3i2ZQS0qhbJj5ZJlDnolR3qnDC3Rphj1W7/mfZ70LMZZKBbEG5/2TFhI4oZO8G9MFbmN5f92FfEVcyyUE1ILKgLRLVkM42NPBNCxsr4xsEoqsx9UzogS3J3tc0M+KYBPSjFabhT6L949wuFAKcqAmg8ZNzWswKB82/2JFBs9zMDMoI6OctRZ/ujP+g4CpdSOErxwRo1/7lhEfq69TXpQuT1wjQmcuOSiAb5QOyv6zGOhyZL1OerRi92M232hzXf0U2TcIHD5bQ9h6U7glxlFdZ0FOmrShJOYSbWmtWbDYSuHupCE/vzA5ADF1Mvc3YhnvjXCH83ITGSHL2VnCsRRea50BBcQgQ/CcVYEk7Fyk8kDTlrtU/azy5+3VuVj5GP6nETTJe5C0NyVGVkuCol22ZYjPbPTEJ/wfZpix0Z+ZbetiodsGzY0X6yE81dRFMvdpSP4e68P7Vz1ktL1ugEtP9TtV9Mzp9ryal6ZHKWkQ0jG1tvDnNtAcUVzv3SXwhx+H18FHbrIYXopOaLRnSmosNa/f3F4pxXZb4qzgx1iL0diRvsvhf2YXwwLi/2xr028HpSY X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 282ae8a2-80fc-419e-c522-08da5dc4da93 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Jul 2022 13:55:29.2811 (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: cIYMesSGVGWjRidZtzap71znBpd6p7Zl893FtBnMuYIchrLflsYgoWJ2KNWnC777pDHZQmeU9rNNX/zWdtt9XA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR10MB6086 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-04_13:2022-06-28,2022-07-04 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxlogscore=999 spamscore=0 malwarescore=0 phishscore=0 bulkscore=0 mlxscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2207040060 X-Proofpoint-GUID: 4qPbnEe5dEoXzo25gzi7lklUWxUk0lFS X-Proofpoint-ORIG-GUID: 4qPbnEe5dEoXzo25gzi7lklUWxUk0lFS 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_ksym.c | 74 +++++++++++++++++++++++ 2 files changed, 90 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_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; +}