From patchwork Wed Jul 6 13:16:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12908104 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 03B78C433EF for ; Wed, 6 Jul 2022 13:17:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233710AbiGFNRT (ORCPT ); Wed, 6 Jul 2022 09:17:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233726AbiGFNRR (ORCPT ); Wed, 6 Jul 2022 09:17:17 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E7B1193FA; Wed, 6 Jul 2022 06:17:10 -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 266Cn3Km005422; Wed, 6 Jul 2022 13:16:49 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=jpjPR0SFNnVcMiSptNs4ea7g2HB2jY1qEFhUqkkrYU4=; b=WUqS5Trxw5vaO5SzI+UlkKscFFnY3PJJ0uqAGQSpyTEWl4R2L/PbMTUON5GM6ftfwwTo D7RRA2Dze5ETEhKGVKNiuZFAW3q6SwwkVLe3D8Xq98phfb0tzniMpHim4uXBRuRjIGkV BAq4bylAosBw0AxsRywvSCDebS/UnP6PbF5w7OTZKlBLAxd/qxB27wJlSCxm3SlBCIxS yFv5MLsG/8vhmaVV63XVdkcdDNO8coKLW61h9q/lwM3wEwy/aXWx4hYkNhoHnH2UKl/2 eHsWN5JXVpbyePQ3RcAY2drChyP75gVKI4OEsRxeKl+tXqgYcDi/1gqCKpkPY7RaKFMW XA== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h4ubyswv5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Jul 2022 13:16:49 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 266DBF7D010580; Wed, 6 Jul 2022 13:16:48 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2106.outbound.protection.outlook.com [104.47.55.106]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3h4ud4v17g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Jul 2022 13:16:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=K5wIHJs0hIKvrcxbN545GUTgT9eAZSbd5j54cN2axXr1Gjj/Iq0gD3GCSPuCwuMfuepVeaddVJxN+Mv5vlGZJtc4SbWue/DdjmXUt0wttXIfcuqKvVS9x8ICzqVM04/Tb9Nwqx+ahF7PCCHkJjfy2LsfQzR1gK2FUwGKjTiPoI3cmZ2VlF4c/O8q8ZR/3AZ/IpCZ7p3Zz/ENUeOpN0fFjgUcNcgmJidFf+CsPWHkJtB8+FhY6jRX8ugbseyqCq1GKmNJKT/WJr2Y6GZcwkBFogBe9MojMhYq8w1Eyb/S+6rLTf6UdQqfrx61sG6tFHDDA2wjQjlWPKZIA6gAvsQRww== 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=jpjPR0SFNnVcMiSptNs4ea7g2HB2jY1qEFhUqkkrYU4=; b=MR9KsMoN34NN4VmR8qibJVzmz887mJJt6MfTXAHYMPFiLXwGF7Pr2TrYGo53iSeKPkaXQcPCW8j5wn1BuP1woSYRe63Xqn3ugnh+zSC9HTXR/dQGrOx6QzDBDNjWRpY4TI5b321RYN73mCCnSMl71WykwNkz/N0H8uJ1jDWuTlS0XA5VadAiZGqWjtyA8zqLoO8r4biqrsAE3R/6c6t8azNt01+jsL3kXeOss8vfS33BX57f+9NwzXt8lHX0hSxfgQ6T33gs5kV+nrSF/6R8yFGrKnTHawazzjlSvtmnnsxktlhzq9cVlyWTuHDIebknvNvocP3RM/QOwdj8MrF3dw== 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=jpjPR0SFNnVcMiSptNs4ea7g2HB2jY1qEFhUqkkrYU4=; b=ZNOrOVSZx7neU9JCbQwrkkhdKgHxd3CdoJW+ST7uvNsdB22VQmI8+W8ZwU9OpHUgVOBUZc34e3uBOnmw40d1SJ+8zxZOHQbLlUiuYFpdrNfZERUZr4vuakMvVIDxXtHgKzXNMagPa/JBmXOroAGYxkITuBjHIDvtBvUQUWoLdrY= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by BN6PR1001MB2163.namprd10.prod.outlook.com (2603:10b6:405:2e::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.14; Wed, 6 Jul 2022 13:16:45 +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.022; Wed, 6 Jul 2022 13:16:40 +0000 From: Alan Maguire To: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net Cc: kafai@fb.com, songliubraving@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 v4 bpf-next 1/2] bpf: add a ksym BPF iterator Date: Wed, 6 Jul 2022 14:16:30 +0100 Message-Id: <1657113391-5624-2-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1657113391-5624-1-git-send-email-alan.maguire@oracle.com> References: <1657113391-5624-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AS4P190CA0027.EURP190.PROD.OUTLOOK.COM (2603:10a6:20b:5d0::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: e67d25c5-f93a-4a71-2736-08da5f51c324 X-MS-TrafficTypeDiagnostic: BN6PR1001MB2163:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mvhn+CHfFSUWNVjlW/TZX/MXJ7CMxHF3oqQHgnbs+/MIaiQK7/K6fQ4aKKrcTzRBTODjv+PSkLqS/1jx4nXfaFvLLB4gLupipOtOxuoseuRebRp1cZjLMwJoMIByJCtKWyUlGvOUC21qgZ4KNw7ymzBDBXu8WCawNZuwER1WNRLDQjJuHJMYYjrb7ZsRIyNaXtGzxtcHTzw/G/f0dZWRIvocln8E7mGzyDNsQ8rEKjygNCHf7y8OHXHWZLz3/7NQfVrBldhqyQFnd3bM+1bK8CK3tYLZHiSEEN7uP2c5UQ0pOLFTZwLRy2Ik1bUEN+Atl01FK4JJQUVdiKF7E/YdmVV0tRcMOUr5qBg2fRbYMUjt4Qc3tA09uUxqHAwAwayfo29Rf/6mZnLigP7YbczD3kpagR6TOxDTZnyGEWcUL8hff1Mm+WH9oIsEnjtJOFFpKs9iD3Xi+UhCfxj78x0vNktVuGDmmzHgnyG3Hvpq5OECSBs3NfUHbQe65WmOnsR7bh2wsy1FMY3Zr7Uwhl8Sx1/mBjOeepLifpdJERy16zAMhAvs3fqfhe63oF27+M07nwUfIjFxwPrTV/R+HknyOgrKp+Lq238rqbaNUNKY2DC18+MJZOzgpUem2ZgTnTER0/N3qxaRe5TseM2mp9q99eWfrhaE5nTg/5EGFeVC1OlULLA4VCpIFsI5KQkqeAV8ytzimuMtipBQIr/GgXdwPfMmjlBVgbjHD/NkQLuSbKMcRlKKMF8PIyExFopSLC1EJH8hNrgktZHJTK9fw0iy3oKp35vER9jcCqKhJn7cq6z3n9/TL9eVODmvzR/jW3lE+9BJaNySdCIjb35GAbTAtQ== 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)(136003)(376002)(366004)(39860400002)(396003)(2906002)(38100700002)(6512007)(26005)(6666004)(2616005)(41300700001)(6506007)(38350700002)(86362001)(186003)(8936002)(83380400001)(52116002)(5660300002)(8676002)(36756003)(44832011)(316002)(4326008)(66946007)(66476007)(7416002)(66556008)(6486002)(966005)(478600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: m2lwe6DX/Yr6QpfAm/k3IhB/HOFZ/tHf/Wx82ou/KnbTyQeWd3+drUHx16+Pe23GaTuM27YEkmP0ceWI9+Z47Pu8kHA41g2E+lMKJjkfz1fdhlKguVKIxbRMf1b6RFdIVFETpIDoakr9DzzGS4o9V4PCcOBNuFUyzQvs+6a6r2e03HzrqvQ0LpqbC8I2ziIpUkGYZWVV+SrhYm2rThHtdZBRIfLBd6mA1t92ByPvwKE+gQcUqG+YgXgxZNnwyCwbjvNNXa/htOV6Q8c9JcpdHshrwtvQqU1ped88mqbisypCQt//VFKsacfy9cUTor3c6x6WbQ2DeTLcWTXaAgvIIFGCBcLzDRUBrAXFx7nRvx2WCdaFgCFA0vDLLDfwc+7DStxzXrNeyBW3i1l7lJLTSheu1/l4zx52zpFeBx+j5swfNxh8Rr//O6mc2720DMifMt5YIPKj34wu6cRZlPoxI/KNdnddMcDwhkr3we51RqSHoIktZsp83up+gv9ovvgKYzqeMhlmVbyEouVVenU0AR7887EhdWr2WGSManWWSjNdDMCnlHb5Nwaje1NudGqKEATLfLsAgWo//MOHHOOU9Q2btd2ANA7ZqSvLp/5//dvbDZgWt1jhdHR45RTNS38jRHWKMlMmid6sWwjxqZAq1rMee72UFwIc+TpItKBLsjkYIdpacr9jHp6GTm27vhwiREG+B69/mLNHERKmgIG2W22OwLj+KrLltLL1Kl6gUQ/quI6EgzO4KlLRbltuBtKuWop/UMlu5G9DE3soUBEKz/ksi65uTcUOgrHjP80w6wQ7YS6C7EoIOlwfnFYwQTYSFNLL0uW9ERFxMdEthRXtvynBNrlvgZtE2FvcyEu6iboIusb81dNd8kMOI1h/dKjSvfSSafJa+e7pxV7xY+LSLKeawFAXYXybKu5hS5yHhaUhVOejOD8mmgtteoWYXVTUFJtU+I3n0CCOKzJm/rXhPQjUm5sQJHDyPgREcvvXQF4S3b/Cd9tXRgmbC2XlNDuTIpzCfzWuErTjxf9FOubPtQiJdAm3z0z1+Sq5XaM2ij1fPnTEHBxb93WekvGG1gACtAQZAlwkKDGIpoCcpiwdDXX+4Mg9FE61rGIwhs2iceYApwogWIbU5Mlexvx8UYGYWruczHb1jGCrh/oXK78RAZwxJ4yR7BYRz2U526BA4hFdG+vL5gtKbMrPJHG6O4zPC1Ex4YR+bjxPiXMdeFonq0SDsH8tgXy+gnTrpjBTPhbaRDadxg8+KkpFw4Fb3o33v9ucKVis4JXFVvyrmtW+sqpEvsa21YoHCZa2gM7b+aszxztuw0e+8dTMPlXYmNs+L/eK1EcAX+75/Mg4DtGIt9xiNH4n08D49V7s4MkfiAeBJph/aja+u2gfOPfP9OTB7tymgwnvQEq+dApVrWoEnShjkGiUAjlf7EXiEPFiUqW6zElvEvGMg4EW3hx47uRdpxhy/Y7Fu+LtI7dcUVxF8ZU/wnLUjKyAQIIQv1Y/lL20KDxd8sGxfWFnKtH4SqHeebAFtpiWTdJhymujCRTOt5aM+M1i4Os4+qGgdtEP+Z7XN7c++vXE5tN5R6Zfy2nz X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e67d25c5-f93a-4a71-2736-08da5f51c324 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jul 2022 13:16:39.9449 (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: SL/9YyYR9Nk9GzcAgCyyDqx3i3/4cCikOTvXp2Blju5th8+jPzOP4bfkKMN/NJwaZa6TkHNTSxPnbTW0QZMipQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR1001MB2163 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-06_08:2022-06-28,2022-07-06 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 spamscore=0 mlxlogscore=999 phishscore=0 malwarescore=0 mlxscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2207060052 X-Proofpoint-ORIG-GUID: lypIi4NF8axv0xL3_olXuL9e-IfwiKLT X-Proofpoint-GUID: lypIi4NF8axv0xL3_olXuL9e-IfwiKLT 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 | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index fbdf8d3..5748020 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,100 @@ 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) + +static int __init bpf_ksym_iter_register(void) +{ + 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; +} + +late_initcall(bpf_ksym_iter_register); + +#endif /* CONFIG_BPF_SYSCALL */ + static inline int kallsyms_for_perf(void) { #ifdef CONFIG_PERF_EVENTS From patchwork Wed Jul 6 13:16:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12908105 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 10C79C433EF for ; Wed, 6 Jul 2022 13:17:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233759AbiGFNRm (ORCPT ); Wed, 6 Jul 2022 09:17:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233870AbiGFNRf (ORCPT ); Wed, 6 Jul 2022 09:17:35 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0226ADF0A; Wed, 6 Jul 2022 06:17:31 -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 266Cmx1J017970; Wed, 6 Jul 2022 13:16:50 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=IiQIJN69PD5IJ10FmgQqijE0q3+yD1jjO2zKNLsotHo=; b=l+xyy4PGWb0Tok0k1M3md5ZQxaK5WRXLL57KilREbpHVRAgXoKfyNQMFvLKeU6MzmHfR uBu6IfiJMMKgqRIm6Gv1uYOqPAs2AdjHX+T23pJsDOshEIQG5icwZ4d9amZnJu2HCZ4O DWJoU/w5hZtBgTZcLfG17lvu5XzLwk6uF2WOWWyyzFqVk55oV3M35vkZnalTesD0zl6D UFJUH2za9wNpdAxRt2Cxnfhp2z8akhp2c22HDKk/s+RGpo9w9pHTpR4N1goFjvny3Icx N3b26D5n6vI7YdEM1VLMsJQCdtCvb0EzQzQp/roEpJfnZkci7OE9OwWyZAFZLBluoEXl ng== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3h4uby1xrj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Jul 2022 13:16:49 +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 266DB1Ks023774; Wed, 6 Jul 2022 13:16:48 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 3h4ud0tj2y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Jul 2022 13:16:48 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Yg7XaO4+BTntMsJnm/CQr2N/BSpOsu7gy1QdVLxXtVatjIb5kQ1VxdkUAvlQCWexxQhQk2Wv8a4bnm5K8ryA3nMK9mCCjHdRDEO2ABKJXPqQpHOn+0WVBSoSV11mWK/4/v1ojOZRD59zWz2g6MZAHqVrn45BVcFUE6sP81n5XlSdm+V3ZUGp4jJKoAh+sgSRJpi0rB2V4IlpbwDolb2OWt4T5grrSBOlzzxUY6O22C053nHVELFCvzTD15MH03iXaSHOh50AKkvJ0qRKxbaDeqmWnmz3U/iLNRDSllJPJ37nvtWMzhVhv1Y+6O03H9BlRJ/fMijux+dr7rciF5TTCw== 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=IiQIJN69PD5IJ10FmgQqijE0q3+yD1jjO2zKNLsotHo=; b=H2zGFKO5q7l2NWK6FMC86mDpQCQEMMyYAeHsspA5RDHY0XTvCMcL1VuRJG9KXhw3GYAcvBgsKXStzUA5w7lG28bcoG351RzMX17KmRQEHrjaWg5FZ7MfkWGlw7v/QukRCW1HMhLrTzenybG0DJfOtAYpZOp4hK3S6JOrYyi7YCAotQHPYZ5at8Ul7HPvwdIA0RmumPyMavnKqJW243Dqc/3ZV0NDj+ylrl0Lx09onLRfV6k2adQpRakJkbF5d5//9gCkZdBZOxmqS+ThhA/myjfB2GrXjeZQBHXfzP1uaC6q3QlwxadM9RJVNs0OzY2wZv+FrTxfN+iQ8oLoHi1XGQ== 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=IiQIJN69PD5IJ10FmgQqijE0q3+yD1jjO2zKNLsotHo=; b=njOZ4Z9tsoTgSRBMu4/+rxcDOC2DhpWw7W/agB49yXWHrEdFYyrVhHWhsYwYXRv39X7HBbXiMnLNwfhstHQN0SM2HNvGYyThOuIgoBoL7eMu360rrtZHDWBphjTmokpmdRYKQZtLCUK3ceTbQXuHaBqicBWXBooIvM7HiBUl4yw= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by MWHPR10MB1421.namprd10.prod.outlook.com (2603:10b6:300:24::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.17; Wed, 6 Jul 2022 13:16:46 +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.022; Wed, 6 Jul 2022 13:16:46 +0000 From: Alan Maguire To: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net Cc: kafai@fb.com, songliubraving@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 v4 bpf-next 2/2] selftests/bpf: add a ksym iter subtest Date: Wed, 6 Jul 2022 14:16:31 +0100 Message-Id: <1657113391-5624-3-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1657113391-5624-1-git-send-email-alan.maguire@oracle.com> References: <1657113391-5624-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AS4P190CA0027.EURP190.PROD.OUTLOOK.COM (2603:10a6:20b:5d0::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: 80450b46-6080-4f6a-2dc1-08da5f51c4c3 X-MS-TrafficTypeDiagnostic: MWHPR10MB1421:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lOvnjRXedUo9FMwSvjyWi9GAhxs4gg4CrEk0uByCeBd/zOoxMbG/fiSMpPKDSKpkMXutmjmFJtDYzerf6u1H3QSQ1fWjsgwI4TtkpqO9m3iM9hCgKhs+S0laS2+wLUKv7kllEorPIkC2qNznx8HYGZL4S8xyAx8/0eWW/JHih/8+0IK37yIzn4lNPTk2ZTJZUR9GG27GzHw9+bvMwHq7TFcGadKPSasa3rO1oGVs1hJkDV5V27N0uq2XuEqZW1KHkTt6HrYwcrJx1pFtgEDLlRsSQc/e0nGCYISpnkoyfllsfgzVGFiambCnaU2/DPmZZcfPLUujdCep/6JmyCbQ/dERGWQ+Z5S4MOGnIP28lqTZogV+OO66Q41WdAryQXvw4ZLB27YLMpKTIn2D+g/UKc5SibGoGjzkfqEf1U3BJmjNCfG+Jidpf7wbumSu9xNInjSn9CwyjABxc9YqK5EnZ2TlxZofzb40riYqs+Uvqt1JVosoPmVNyz1ErPJXb2Nyxvt6TMhTd1bEVfW8cnB/zBoXGwC+y+nUkhiCNR4gEY/k65xbOdkUGTvzMqKxPPqLV7/01lqLnrO5Lify2xIkZQf/M6sl8xyMg9VSD5sKBMXE5PLubLzzqfxN1uQmaol7Kya72NHHKOPXffK90EIRecU/dKpOVosKW/vEI3jHok6Q470gAXmB1vvO9xYA7u3vFvrZ/rYRhhIwf2TGf/ABiazZNrK0bqyfRlyuveS4rLGYnqrUONFngL97TAb8xy4KEoPgHFHzxu3Zm+QO/5x+Y+jL0WkOeJbZ+E45+2AyhyFyOSWEhv1EV76+cM6AKMhC 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)(366004)(376002)(346002)(396003)(136003)(39860400002)(316002)(83380400001)(36756003)(6512007)(26005)(186003)(6506007)(86362001)(5660300002)(7416002)(44832011)(2906002)(38350700002)(66556008)(66476007)(38100700002)(66946007)(8676002)(4326008)(6666004)(478600001)(6486002)(41300700001)(52116002)(2616005)(8936002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: uEynSxJ11LO+4L6isQXZo8Ff58uC2liugSBhqVUafwm6Q2SYR/xqyVZtcvY62yFVEl5REzItir80RNVPTEY51GTwRXXq7Hzr9n2KoZY28HfBxivUAgybPK0bH29NeAdG7+ITWDsZkHmF0eTFOr/ConJMH96KF7dA8XryMQ+VBJGYm30Fwjn4v1gp0bJMF5Tw7rjTjH2tst9cD0rjDx4ZLfVTpSVv8qD8DhHfJPmWo+8/Q6gODAPRPH/H+kDMBr5ysX0ReSwceO+w0OqhRDw2iCbhULC5Nme8jMUkb6Uirr9mlASILTj/6pvABGYKrRI3/25ajqVkss8tbI1BOhuJewWlzk0hOlTxjZniS+DG6g3Cwcg28IOG1ytAufVwiZHSWHRaT9iy5N2340lUfF6E1jrGwQRVALLdJHO3fBKx4m+pLfmt5TlPbNkFsX22KMUEUj9RTp5Nl8dDK1QY10Hj3hMFHdGowMjc9z5s6ZKfw1uYSs3XRKm7rXkyVuOtTy/07IlrOBBP2069JIxv0gx5GQcUjTEKHctxLYTpw5JxxGmXfoW24FUq4HmdWcoSBlxxDdH0k/H3+J7ZjpX9gmawUvXjX0t9vdmuFyfUVh/Q0bDOXigErWm6vqO6n7XudIgKw6xojvU6vgclGNKGwckRkdAqL7qIqwt9vl7hdhjE6kNv9qGPrAidosiuhPzlAdHAGaXmG/FnYKXC5d1TST72uTGvnfDnSbXU1auoAQklHDGlMQGLIn/YjiDgHxF9UQvecVs8GxdfL3hJ7QDtrPsXtGVzlavGbHU9jMHVAX/uCU6bK1yZvPObPyCPi9KLVwwWdpUHPo/gtuOOY4NMhBKTQudDY2tpwF2NX5t8QsJtS+tx17/F64wnQjjJE6OY6JQMdsc1MWNV/l7WdmAY1VuNM7kqDS41LfTpoA509Hfu+L3sTqQwXOyoq4eBpB6HJ4zonIhoDTDCJ3iV5oE1zMMkH8T0cb8lxxg2ozF92CptiqZ3/iGYeJxFk22NDHXJyUpBwHGBx59ACx3/CY6kxuW804vMzJWWYJSI7ZXlxPtwPgaTi7kiAhTYRW/m/cBAFbIwj4M92vr49mPhKX8hs7EZFLR6bBQ4sTnae+YB4jGieffVcwxAgcAp84ETlvRh0ts9x9dZgrg5AsM2l7uSsX67iA9xCzWlBpcMgOXQh48GoOLBdgMD+H/I+cE7UOIu+GAehjNLcGMa6w3V+RjdT3PNlta5kPzmBo1swREPrBuSJGwDzSb+KGa/dxBVh9QjJxBppgl7X+Sqq05YsArcdxdaC2tVqV2JwrLCLLe8bcryU+CWXVE3BkKrK72Ul9bDj+sjtxUGxQiQv0Hn0oZa/0mBg8p/VVzJE63YV75wW7SC3H2hO+3bQZ2qqMoS3N9yUUgcgjYHLfRGgPvVomdPly/F9y5kXmSvaK9X8LNh2p30LLPT6mPhZEECgZQhLJOj0B53KDE6RyNhP8E1vFtAxFgqXqeaFjvTl+e63Xmi/9oVZ05aJJ7ieMR8j4NTilSnuJw10AhLq/cr1bZr29c4A+pQx+B96S8ClkyClKJS4hQisQ/1hJltEjrMdr0P7HE6ESmu X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 80450b46-6080-4f6a-2dc1-08da5f51c4c3 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jul 2022 13:16:42.6805 (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: L7JI0+2hEDsROrOYyLE1hQVjKiKMn2U3X44YzJVFntS0cvc0wWO2SImFpDAZfWPSpZQo9gND2yTLCahh1M8DfQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR10MB1421 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.883 definitions=2022-07-06_08:2022-06-28,2022-07-06 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 adultscore=0 spamscore=0 phishscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2207060052 X-Proofpoint-ORIG-GUID: EFxXgvfKh5Q0b61qvNmaU5GjebcWWUTq X-Proofpoint-GUID: EFxXgvfKh5Q0b61qvNmaU5GjebcWWUTq 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; +}