From patchwork Tue Mar 15 22:15:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12781890 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 8EF39C433F5 for ; Tue, 15 Mar 2022 22:15:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240840AbiCOWQk (ORCPT ); Tue, 15 Mar 2022 18:16:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345807AbiCOWQi (ORCPT ); Tue, 15 Mar 2022 18:16:38 -0400 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 626925574F for ; Tue, 15 Mar 2022 15:15:26 -0700 (PDT) Received: from pps.filterd (m0148460.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 22FLgP1O014360 for ; Tue, 15 Mar 2022 15:15:25 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=facebook; bh=ytBv8VCeqcCMyIjRtdCdZbRvZm+1dAj4mlFhYms3YrQ=; b=kLQVNxOHCtGuoPAEcN6Q0QPPgiO8PjE2r/2QbP76R15ZiW1+kMEkXBtF/oAkQ/pykzRg IdIfJWu/vD9squklZsWfe9s6jz+eLGspx/P97xw94vz7Ep8xG4oj92X103UKRZ2Lti6F 8DbfajksQZ5II5m1O4D5LKQ7b/ZeILCaTUo= Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2171.outbound.protection.outlook.com [104.47.58.171]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3et8vrbcbq-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 15 Mar 2022 15:15:25 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MeEiCGxbpYbE5vhld74iDpVgmFAxW+5ij523L5wqSk7tj6zrPGhIYjUeh7gOTcnpxpoWDsuGcwcUWW5N+22j7Jyopizk83iKj6j7NRh7ABh3C1UOA+vz7VULUCZMl90aJMKgyV0xYn6ti4d4ybZQ7BSc19fRFtzJUjBrp+Rtuk0DEAByqMRtCdIJlbrwO8ZJg5/oT5OPJiZcGLSiRQyRuJPsT/xijzuR7RoXya0SRFgBwOGfQSr2yQuf3m7qSa49bZAr+jr6y1t02lYWkwfCDcuyJhMGaanpaMO6MZL0kR9lm3QzVrOeZF7jZtDxSo9ldt2bY622v6LUMLy5dY6eqA== 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=ytBv8VCeqcCMyIjRtdCdZbRvZm+1dAj4mlFhYms3YrQ=; b=PH/tN80j8RWwx+O3An32LbncQ1N22k4m53wIVa/KcazrC+zB3Mrgr0LvCmlF1bt00PsLbNQDvmr7XMWkWyi/FybBKGCAXwBvzExxqUhONdv3D+0xPFy4vJMSqOjys8xqFKZvgPLDe79z+ZsKhTzr2fsb0mLXtNmmqn77Sfu179M3XAAW0XRddzex9ZjB5S869Djl6N1vvucPBG800seUU53qPUfItyUr7AEUYzaT13wjaG1qhpii4d98P2XzWJPEySa76grPfLOcdD3k6+Do6TfrMHrdjcnEag51VXpIq54wxYl40Hg5IIduF20CUqrJM0PGeBsInHa0IaGBlmGx/g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass header.d=fb.com; arc=none Received: from SJ0PR15MB5154.namprd15.prod.outlook.com (2603:10b6:a03:423::6) by DM6PR15MB4313.namprd15.prod.outlook.com (2603:10b6:5:1fa::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.13; Tue, 15 Mar 2022 22:15:21 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3%6]) with mapi id 15.20.5081.014; Tue, 15 Mar 2022 22:15:21 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v3 1/5] libbpf: .text routines are subprograms in strict mode Thread-Topic: [PATCH bpf-next v3 1/5] libbpf: .text routines are subprograms in strict mode Thread-Index: AQHYOLoopc2X8fRgPEWiZpLDx/LByA== Date: Tue, 15 Mar 2022 22:15:20 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 89dda0a2-69ef-4536-4c15-08da06d14ba5 x-ms-traffictypediagnostic: DM6PR15MB4313:EE_ x-microsoft-antispam-prvs: x-fb-source: Internal x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: qTsce+b7G8wOsmu0PAedgErNDy6D7zYi5aGd8y1pPfeByMKnMFOykLhmUujfKrq9s9fMULzfTdBLUCwroCkKyiGmujnAOUZk8q11wFNX+Vn6fZ0lose7/XTs40SFUqyMIVtN85t4zpzgQhMXQjv40iFgSzL95DhHOGuKzqKOhwYpQjtHok2uHhSh+MpKdg2ydlM2xVkfYBtBeuLkP8tceuvDdRy9TreNGJak+/+ULaBZ2i66deu/U8ohKlah82XKxFQXOf8UEMjIkOMsklT6nbIGn9lnIoEkXzOqZAjF+1A3QE7zZkHOmnTJuGNmazL2VIB9zkhKUQB+bH5rLuH27cY2Xi2bqcyioszNznzDi3yFa5ZaylUEAAknbCuPQMxc89QNtqErznlEmm+T2cY1jHbaI18OT1Sd3Vp8wjkLcG1HUyAx7xjA3m5AwqF6MK60zHPSaCoH7uiIQTXXDXwbDhx7Wr46brLQnSFN0SZFLLd5efsUgu9RaAeuf3FIj9q3Aa7Ms5gqqeYYWs2xVViyhTUshVi8aBKn1k9oyvfJwx7w+9gidx1wht1YKECs+0Mwewq3N2SebalkVXbqbA7p4Ow0qpsEh+fFEGrcDo7Sy+bKQMWxTUkU6qouOAd/w/85XrjaNoIYsBOo6U7MlgZ4oQRfX7+zfMex+/337rLCyS3OQpJr6Et1gsZ+uVt/vrEjK17wOM4lBNN8M7lw72ImTw== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR15MB5154.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(91956017)(6506007)(6512007)(6486002)(83380400001)(110136005)(86362001)(122000001)(508600001)(8936002)(36756003)(38070700005)(38100700002)(5660300002)(316002)(76116006)(186003)(66556008)(71200400001)(66946007)(66476007)(66446008)(64756008)(8676002)(2906002)(2616005);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?nz0+63bhsvHCTyMEmjfWt2j?= =?iso-8859-1?q?rvEFKLa1APbBgQDqxGD24hlW84NbTo80qa5En8RAxCL40S9ZWEa1rPnHwgce?= =?iso-8859-1?q?qL+X6/vgorUe1p+yynFviL/uL43pDBm3PebnYWlb8Hhxk8KdWmQV7rBQu8Yn?= =?iso-8859-1?q?zBGnOdOgFmsFAVo3mFvCYf2swZL7f97LXq81b0vF30a+XXn9jzqJW/l3jVWm?= =?iso-8859-1?q?3caXofDDO5iCFqgIQcppZSOZjGwHphs3Y+6Ow/o/dwCnf7fy2gbpDSWkBzww?= =?iso-8859-1?q?Sv/sGEde6fgz5tbNObzeMii5fb4kEQB2/2I60f9UAImJUnWYIzUJOCzkXXoN?= =?iso-8859-1?q?QHXtZWXplHoi+zNBMfRgpfnVd3ZqN8VmmATXtgmpW6WvmScKiQLmD+o6aFW7?= =?iso-8859-1?q?T7ellzz/zCKNrKE40zFo5EBTnuEutY23zpmzlZcSew3ETDDDf54UGsndmfWR?= =?iso-8859-1?q?uv7cdwqNbZkbMnP7lzQ1P5WO5PgdPk6Ct8vqWTVagj4NsnzMhs6kfqqjXjWu?= =?iso-8859-1?q?8RLFjo+xGZeYz+3Y9pj5+3bPitP72UU497sGc2/QS5eDISF5A0BqEXSNh7E+?= =?iso-8859-1?q?yEgYtNvgacvfhQrzMNiS9piPg1+E97WJ6NPRvyLXmrKEq2HDLu33mJ4b66Hl?= =?iso-8859-1?q?suhN98Xb+CjDc3ak8T3K4kk1VWpJM1vdVhhoO0Uoco+t+j9DtEiif4bzW/vH?= =?iso-8859-1?q?NbkqkYTuc3sY0lkN1dxPqsSlISVEnaPgm4XAqqf6GqZrET52Uu7UJ0fyrhoY?= =?iso-8859-1?q?0G3vqM7UuUPwrS23RfU2tfWlhCjaI0OQPPhURLVlgk+BESWU/sAUW4gzp/VG?= =?iso-8859-1?q?dNvBrCb4H4gTtZjJbWNy5Xs5J/MxBTcznLx3ubY9el/zchEmxOuxpFUjqK8l?= =?iso-8859-1?q?yggQbSlH8Pmv5o4+hd+gpi4YLsdWWT4A9LA3jS8QKfXn8dqokj4/kUopa5fL?= =?iso-8859-1?q?RoMkmiFTKSn8eKPuQs0JRQAwZpmOX+jcUpCxQje3I74+iR9Io8PvyHan6jFe?= =?iso-8859-1?q?Egq0AsbVe/YsWygtd3kFcVZQuSnTj6in63bLHNEav2WOQBP8egrT6y5CM6C3?= =?iso-8859-1?q?pzl7fkXgoFEbU1yDH224EzGlssAjnKI8CmpZRK6lGS0g/uEHbBIwNwAr8K5f?= =?iso-8859-1?q?P6Hff9VyY+HoN6GUbee8e2Y2pJBYM8IbWt5GsCit+eMwu0TUIt/c2B9o7nfD?= =?iso-8859-1?q?LpNP7XwRnDY+wgBW62Uxz2inRbh8kBcMKHeJRykdN4dL8Ti4/m9ugYlQFg3g?= =?iso-8859-1?q?q4/H4GF1CV+iO0SqCpzw8RkyzPxfxBAYfdkhcciQPXEzuRk3shcNm/3WQ9Qx?= =?iso-8859-1?q?dGUkmmmy5+iaLPgl7O8Y/9wixdr5ccdIiEAD8RJ6IHkgS8PMFBcgqZgvszEi?= =?iso-8859-1?q?rBqH0kyIEbSdB8roKt5JK+m6ZUIPjNiteuqt+upDZ0lw2oWHWInyOH95NMyb?= =?iso-8859-1?q?ROqWY+WR2QsXO9Q326iqxekoN8fUaRNfByGc1KXYE8mNUo9Ji/gOxDjj7eky?= =?iso-8859-1?q?bx3klheY1m1r/wSe4FnQiB+tQ/ZE9Ac/i4SGbyXsDOY4NUspbR5bb20n5PvV?= =?iso-8859-1?q?ajNORDRTgF7ajrRN4PJkVjnubZmsjfvrEWSUNVYyFgtXXbENzhnNNEXK+klF?= =?iso-8859-1?q?ejzwogjzRc4ZAc+1GS0GiT8jMbMZmrs70ERKvCw=3D=3D?= MIME-Version: 1.0 X-OriginatorOrg: fb.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SJ0PR15MB5154.namprd15.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 89dda0a2-69ef-4536-4c15-08da06d14ba5 X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Mar 2022 22:15:20.0296 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: i0tj5l25Kb5ukvp/oTPK0WkycmUc01NOYA7FGGqRYxjDdySb6tGhmTEWKGMqrP9Y X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR15MB4313 X-Proofpoint-ORIG-GUID: JjBuUf9tqZ71POiMs_P2X2uo6RA7nIAd X-Proofpoint-GUID: JjBuUf9tqZ71POiMs_P2X2uo6RA7nIAd X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.850,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-15_11,2022-03-15_01,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Currently, libbpf considers a single routine in .text to be a program. This is particularly confusing when it comes to library objects - a single routine meant to be used as an extern will instead be considered a bpf_program. This patch hides this compatibility behavior behind the pre-existing SEC_NAME strict mode flag. Signed-off-by: Delyan Kratunov --- tools/lib/bpf/libbpf.c | 7 +++++++ tools/lib/bpf/libbpf_legacy.h | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 43161fdd44bb..aa26163e4ca1 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -3832,7 +3832,14 @@ static bool prog_is_subprog(const struct bpf_object *obj, * .text programs are subprograms (even if they are not called from * other programs), because libbpf never explicitly supported mixing * SEC()-designated BPF programs and .text entry-point BPF programs. + * + * In libbpf 1.0 strict mode, we always consider .text + * programs to be subprograms. */ + + if (libbpf_mode & LIBBPF_STRICT_SEC_NAME) + return prog->sec_idx == obj->efile.text_shndx; + return prog->sec_idx == obj->efile.text_shndx && obj->nr_programs > 1; } diff --git a/tools/lib/bpf/libbpf_legacy.h b/tools/lib/bpf/libbpf_legacy.h index a283cf031665..8d2e632aec79 100644 --- a/tools/lib/bpf/libbpf_legacy.h +++ b/tools/lib/bpf/libbpf_legacy.h @@ -53,7 +53,9 @@ enum libbpf_strict_mode { * SEC("xdp") and SEC("perf_event"). * * Note, in this mode the program pin path will be based on the - * function name instead of section name. + * function name instead of section name. Additionally, routines in the + * .text section are always considered sub-programs. (Legacy behavior + * allows for a single routine in .text to be a program.) */ LIBBPF_STRICT_SEC_NAME = 0x04, /* From patchwork Tue Mar 15 22:15:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12781885 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 5DF21C433F5 for ; Tue, 15 Mar 2022 22:15:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245650AbiCOWQc (ORCPT ); Tue, 15 Mar 2022 18:16:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231623AbiCOWQa (ORCPT ); Tue, 15 Mar 2022 18:16:30 -0400 Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 827EA5574F for ; Tue, 15 Mar 2022 15:15:16 -0700 (PDT) Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.16.1.2/8.16.1.2) with ESMTP id 22FLfqgq019624 for ; Tue, 15 Mar 2022 15:15:16 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=facebook; bh=VojZKIOiLxwYSZgUwP4QSusRUqVAZXE9L0TPyOFByJ4=; b=hmzV89OiquBwoKArOfve2540prSlcxDD0VhcoFqSyCVxXfg9AEDg1mobnlIAkAZ8mwKa 9c5xuWE8addKB+bqWUFAQqkJ4pCERe4icEn3Drx4gc0lOallBeY6kp2pJMI6F1iWYI6h 2B9Iq4dv/cG10/elqNW02dL5P+zerUq/muA= Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2175.outbound.protection.outlook.com [104.47.58.175]) by m0001303.ppops.net (PPS) with ESMTPS id 3etj2ueywh-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 15 Mar 2022 15:15:16 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hfFdC6kg720Kd6FzO/dFUDM1OZnhR5ndUXw7DW1ImPKwKpcWCcZJVerPHeaTSjBlgnY7pa2bhE6HE54MoZ0Y59cqbf5o1uKvblmwcteqZHc4NK45q5uMvkMS7zWex/3jm8bJbYgkijLdz5lEQF0GoJhJaspIl+Ue/69nxzRu8QYDCDaoXa9pDCXFIr2PcBPnExHnuO0T9xpOYzrNBSKVPuJEy7TUzoN9r5XWquvSyT8+Qlnkz4u8iMDvsp3nS9KhR7relM4WcrGMWVxEi2U+zdJlHi373UmBthLj0Odr54UYD8QyjozQrCFc3Q2G9/lnuBKBpud7Jam49+bedqgTLQ== 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=VojZKIOiLxwYSZgUwP4QSusRUqVAZXE9L0TPyOFByJ4=; b=gezOB+uFJvcj+g++6O2EyTpONPLLbrDd4GxOI46iCwLFOX/Xg1cL2Wr/wuk/wvhSGepDX5ksqRU1oBebwMDZumvvTfAi2R816nIxOSGmndLIoEYbcdhzUrNDpjaufKSxTyAD3OweGgIUGi85aiU14qD3iIwwulrd7a9An9HvRq2xKx48rKsBafEc+iFP+37blmt4MOLcY7DxeEANqj7w142KmLI7DgAomIqfViuc51FUY8A7fMYeh7+s8RYeJtE3GWen7Xfor3Tvf/xlYHl2mSlFS63D6vcueaHx+40FWtcfG4GA/fHrqv6UT6pxFm8rbXVIq4MHoiPEKVdgenaJlw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass header.d=fb.com; arc=none Received: from SJ0PR15MB5154.namprd15.prod.outlook.com (2603:10b6:a03:423::6) by BYAPR15MB2695.namprd15.prod.outlook.com (2603:10b6:a03:150::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5061.28; Tue, 15 Mar 2022 22:15:12 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3%6]) with mapi id 15.20.5081.014; Tue, 15 Mar 2022 22:15:10 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v3 2/5] libbpf: init btf_{key,value}_type_id on internal map open Thread-Topic: [PATCH bpf-next v3 2/5] libbpf: init btf_{key,value}_type_id on internal map open Thread-Index: AQHYOLoir9tCuOllkkm7a6Yf41ZA6w== Date: Tue, 15 Mar 2022 22:15:10 +0000 Message-ID: <249e598bc7a9b66397465518654e98a11719ce13.1647382072.git.delyank@fb.com> References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 7f6aa1da-9aca-4c97-79e5-08da06d14532 x-ms-traffictypediagnostic: BYAPR15MB2695:EE_ x-microsoft-antispam-prvs: x-fb-source: Internal x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: sRVlwYHbWRyZJRcIIhktTNJphKuxE48jWwEkYkEm2yqZuUyQNPl9WxH/lwdjfLAzyHfRw3yG9kNtDlcXy7GaCP8wt7v70ONW5pD/iqAikz9ya8kG0Z+ofNJeNFuIr8BNMezoHFMaOYfjMPwLRNGcUl2jqVroXcgtd9jHrWUbPnTAHiwoiV71Gb/SwkH4ZpSaK6wZoSSS7+XobAFbzj00nGZH9YnUoPFK4FeqsClJsEv7wf/XEvByMG2Jlpcar2nV5QmAkQW+bzuX2Iod8/eXYRkkWn28gIPhuVTKsMS1W/9WwrKGPnLKJvzeyEUzxKkeJ7DAVlcDQ8ZwCzU4rFJJsVAXZKZRWB7ZsMp9XG9llfnIWHEsCxsFQliVBfmKz5TN3J1+UtYLYt9SUxD47zunxrq7vrj87Me7VvHVVLdwfo1UFLKdHvYZYO6HWuWS5cObbmHvdBukbpprmQqok8bHipdAQb+BJpmWVTRwYqNTsWS9DgJzEArfoZ1rdh6xClBWWpLnF7Nx06quJF0HQRFypnEbuEy1BWHtvSfIXf/uMsWW0HsvuJ8maTCQEhPEwlaTiOM6ofp/RYSL8E7z1TmjuG42iHglJRZSL68HUJfTtolAm6fZgvRzb+fF8nBKhGlr0Ng5iUA+4mSCL1HmsuHqc9zQ9Xr0/tpg6SpkrMznLXV/ipfx5Lhs4/bOYqbBToqhwvxl8x+69XcaAZuL7Peyhg== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR15MB5154.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(66446008)(66946007)(6512007)(6506007)(2616005)(64756008)(38100700002)(8676002)(76116006)(66556008)(91956017)(122000001)(66476007)(2906002)(186003)(83380400001)(316002)(110136005)(6486002)(38070700005)(71200400001)(508600001)(86362001)(36756003)(8936002)(5660300002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?BmH8lkVTmYkmNYC2dp9qPcO?= =?iso-8859-1?q?I+bdeMKnnpWMapRW6KeJiFdFJ8AP58PEVEcs47R5xJ81ttTPh0kHg7nSke69?= =?iso-8859-1?q?K+KL2l4+2SmJ6Wi10OVkakl0R/m+znlTpYhqKnVaEuf8umAGUIJeIwlJLRzc?= =?iso-8859-1?q?EmDAJfrXmolM92NLMC3aeYHD8S8DMVC4CJMPGKJTliDu2l+smaDT/BdpLsHD?= =?iso-8859-1?q?VSM9ExRCHbVBxbmwPkiaugQPdhXrz2ItK04cTQFbk8btZpNLUZR743UUHg/e?= =?iso-8859-1?q?JI6rnMlyjEvxxmc2MQBPMrVBMIHCUnq5dV8qhe8ZqW4TukQu5FL7upBKyxft?= =?iso-8859-1?q?05kFLHJdFd773CMZKOQNeFrM6OPOJHT+aRfMNYnRXeF/ACSsv5nvCUlXl+Jp?= =?iso-8859-1?q?EfWs/845picX08Qd89AT1nRy92u0X0m2VtJ72u1DuQpI8DhC6rgqtlxr/8bS?= =?iso-8859-1?q?NGjPzGQl4gRxwi6vzXM/GLSF8QN/mr7FkzDI8ldboZF6U+fnjizslHWyzGtt?= =?iso-8859-1?q?H/hIjJJaKnSPSQmMT4rnPJ/WrwNcaA1yCy5KyYRJkYz0vbZrkJQPIvG+AuP3?= =?iso-8859-1?q?u1n1XZNr/y9uoJXLynmeRpOvwKxLX9/FBqfFLqInuRr0mpwrNkXkQzCo8vJo?= =?iso-8859-1?q?qDc9AKHXdhfU47y6t2ItAzGAH5tvMsYR7ALZeREQJdm1ZU2d3uaT0Wpq+qaJ?= =?iso-8859-1?q?m/gTFCXsQ/yHrceoFCYaQmvo7v+Wk0iazTB5kgQ/vDQpDe53qnsOdS873mPB?= =?iso-8859-1?q?L9rGTeIIQYw4tAp1xYYZCnXFHWLj7o1dPvG2dqM3S2YuS6m2gKqnJKoo/1tq?= =?iso-8859-1?q?TFckSK0vqeLpSHDj5S6y/V3LakU/1QD/2/LDvD0eOWIutO1a4TjYkprzYpyS?= =?iso-8859-1?q?8l1Dp0xN4pPRnA9OIsCYcNC+U2r/b9dZU3mHjmIsQTVx/qIYaPhW7U49ObVT?= =?iso-8859-1?q?01jvZQ0FRqPR7Sk0lbqHKApPivyQDB/LrsBJ3TW3amB/j+BZsRnsQUBorv6Y?= =?iso-8859-1?q?mHtCQu17bruQBwA5HgXNlokP+U3JeHxzyszAInc4xIIWQvesBcx/aScFNE7g?= =?iso-8859-1?q?9wqXGyDwo9lLaVumap/f6Iy+DM0WUuqe/OVljf1PNWt77aRu2qRXnQmsZVyk?= =?iso-8859-1?q?2MrgFt3Wcduoj2XOXo2NbVR9tq4APDVJ/5yIA49IbDNxj74J4zuRoTRaO6yc?= =?iso-8859-1?q?r0kD8KXIFNIWIZ1GgwfPZni/ddJ/r4+plmF8SHYbDeFcNYl86Bq4SHYqXl0R?= =?iso-8859-1?q?/rt6Mnh2MyeSLYmZ9q/y49pXLeqjaQV7hFj1YFg9rZ0LZ6TusLirjiM17wtq?= =?iso-8859-1?q?B75fhlN6kvVtaoSS+6ueDnNwdHtcpLA5+iygolg2pNhzEjkUwtqXaJ15/lSt?= =?iso-8859-1?q?mdWG3anYG0cDe1UlXU9duRNMSlPQYcHb+6Oo4YSAaUx8fkk7/xIzWDImH8F9?= =?iso-8859-1?q?P032WzZLjX0jdP7wzMi5Ms0l2bMWAx0CZy3Sl/p132vSBZX2M/5SDATxBmFB?= =?iso-8859-1?q?Dl5xAeo21Dqm6mwcoDpPJ+cS248jRB97Wy5gAGIE9Z8C9/Bd0GUE=3D?= MIME-Version: 1.0 X-OriginatorOrg: fb.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SJ0PR15MB5154.namprd15.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7f6aa1da-9aca-4c97-79e5-08da06d14532 X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Mar 2022 22:15:10.3807 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: DP5JXSppg/MxJ8Da28by0/AsbOIjQkur7CGfy6z+lk5SAX+dOdLxZqa7WM4zV7Ht X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR15MB2695 X-Proofpoint-GUID: csqWurzJQhf-zYwNG4m6F0-wRYMkT1ud X-Proofpoint-ORIG-GUID: csqWurzJQhf-zYwNG4m6F0-wRYMkT1ud X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.850,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-15_11,2022-03-15_01,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net For internal and user BTF maps, look up the key and value btf types on open() and not load(), so that `bpf_map_btf_value_type_id` is usable in `bpftool gen`. Signed-off-by: Delyan Kratunov --- tools/lib/bpf/libbpf.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index aa26163e4ca1..e98a8381aad8 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1517,6 +1517,9 @@ static char *internal_map_name(struct bpf_object *obj, const char *real_name) return strdup(map_name); } +static int +bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map); + static int bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type, const char *real_name, int sec_idx, void *data, size_t data_sz) @@ -1564,6 +1567,9 @@ bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type, return err; } + /* failures are fine because of maps like .rodata.str1.1 */ + (void) bpf_map_find_btf_info(obj, map); + if (data) memcpy(map->mmaped, data, data_sz); @@ -2534,6 +2540,10 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj, fill_map_from_def(map->inner_map, &inner_def); } + err = bpf_map_find_btf_info(obj, map); + if (err) + return err; + return 0; } @@ -4873,7 +4883,7 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b if (bpf_map__is_struct_ops(map)) create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; - if (obj->btf && btf__fd(obj->btf) >= 0 && !bpf_map_find_btf_info(obj, map)) { + if (obj->btf && btf__fd(obj->btf) >= 0) { create_attr.btf_fd = btf__fd(obj->btf); create_attr.btf_key_type_id = map->btf_key_type_id; create_attr.btf_value_type_id = map->btf_value_type_id; From patchwork Tue Mar 15 22:15:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12781886 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 F2970C433EF for ; Tue, 15 Mar 2022 22:15:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231623AbiCOWQd (ORCPT ); Tue, 15 Mar 2022 18:16:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240840AbiCOWQc (ORCPT ); Tue, 15 Mar 2022 18:16:32 -0400 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F34D5577B for ; Tue, 15 Mar 2022 15:15:19 -0700 (PDT) Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 22FLfkuD009029 for ; Tue, 15 Mar 2022 15:15:18 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=facebook; bh=/DnVhiE3vtRvuA3Jusro+q582zGMKeebATYn/qcZxSM=; b=RlH09Ds4O8ddEqnUo5pYJCtTzk+gE4piT3oVPFjmx9BT50A/TJYWweseYA/R+B+PiPJX usKfvLsEPO9TRMaixz+d+fndzUeYnd5GWxdXM73qng6Ia+X7i8kqJWnhik3Sqzhv2HM0 N4pU8xT1yrijVh8ar/30RdWZwWpsrS1XRMU= Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2177.outbound.protection.outlook.com [104.47.58.177]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3eu2brghmn-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 15 Mar 2022 15:15:18 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TZFB/d/ryOgIEoRzSsfZpKucy77ikUAh2Etc84BYPRPTncGxSWuNSP2RRAgil7oUnV27d+Sy4NDMrVkdCpmJlk7ltjqZ7UOAWuQzyci5K/gjNa/vx7CD6XSFkm7p4ozegShrYp6moOMUKHl9iSXh0qDTkUXbgYXkctVYYAc2mOwtntP55UldCLpEe7wHZ4FnnfpyflLlO9q0NhZHniigXwoJ5RcFruJW+9qB8R9EbLFMpPLWnkwzbS6g3fy45dTVGbQsUcIdfaZ1zF8M26P95oW70m1safw6QjwoJGeczb2eMlnV6e7xXjUvyVRt+fN/xtckC3+9zHBnZc2c1r18cQ== 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=/DnVhiE3vtRvuA3Jusro+q582zGMKeebATYn/qcZxSM=; b=U60ieu7AIbr2v0pJOwKCjpHjAW5yLzw11bh7qy+OrxaU2mYA3EDMvST2L6aQzHtpM0blUcjQCzAD2Em6dsACbZ8OVCia82qcQnmN/XrSLU1pa0mSw+zhPyd1hYNwc4p7i8hYaXrsh13T0ZEPkNG6VlVipB63TknsQ9hNcQm0/+V2sYKB17dFSuO0u4I/l4BLy+RWBygHmAIWCyKXnydBigrFg/L6OUANlCmO8bgRPOX8bja++VkG/Gz1BW+Rbs+VCl7Z1ZqzPGhl1xRkZNEYSQT/eYWMVbhY+Hu+48lWIJhsClukV87KcLjuOI4sP160d18w73vgSgDFN8XDUOj8TA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass header.d=fb.com; arc=none Received: from SJ0PR15MB5154.namprd15.prod.outlook.com (2603:10b6:a03:423::6) by BYAPR15MB2695.namprd15.prod.outlook.com (2603:10b6:a03:150::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5061.28; Tue, 15 Mar 2022 22:15:13 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3%6]) with mapi id 15.20.5081.014; Tue, 15 Mar 2022 22:15:13 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v3 3/5] libbpf: add subskeleton scaffolding Thread-Topic: [PATCH bpf-next v3 3/5] libbpf: add subskeleton scaffolding Thread-Index: AQHYOLojqAQIwWhdUEimrWTBFQsXiw== Date: Tue, 15 Mar 2022 22:15:12 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 7a47d0c7-e480-4f9b-31bc-08da06d14716 x-ms-traffictypediagnostic: BYAPR15MB2695:EE_ x-microsoft-antispam-prvs: x-fb-source: Internal x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: uPtNibVXDplg1bORNhlrI796YEE79HS+BVs/6igUln4SNl/u6kfOgxvHYbyatOOWHpUo0stwqlUoFe0MyhA9BB4a2BFMEM5OWZKx6JYCTStjThoz6Kh2Sx69G/hSVHHB3Kg1XmYQFBHfAYYPlUHQju8G890mgIiEH2T59AW9usgrfh1DFDka861X+UAkKyawk4pTP2leBGEBG0jxxISKUS4HWAdhh6sqw//jt/nVxOopD3bBKcFLVDSis5ymYeGCRcZUR67pmiOL7dviJ+0V6uaPWgofDEOigqbnjMFQUoHf32x5LhszQpnu/0qNES+DnIUIUGFWOrhuVtNzuzeocIxee/T4ycNuDej6Ix0PkSqJ2bzYEm9ooYqoXdrovl75+zyGVzrEomtrinew9juRMefhdr7Rd5IXemPn00DJwooawnwu/+5VZbKDf81UkBtsebZsozHLjPx27uxCtDAlobO+vn+y76O7b9jqwTY+pyrkovpq0d10rlLhnYfk8by7djDwN3RUK/IXq1Yj7iBx8gtnO9HuhRx4waHbu5mO3sjNcoYF+9OHH0fsp6F5ZBvkYl8KcTZgxUeAc5RIjjaROzocmo6dIv3Hhgzaa6pPz0mDovxIlq7nBG88Z54EQFDIiEMl4oIA53Jrs9Am7ej3U1jcc64wE8KZt60ju/Uw+F48hS4zp4zCFF2LMtPKwaWpx235v/Cmwvj+be5OB0eTIg== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR15MB5154.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(66446008)(66946007)(6512007)(6506007)(2616005)(64756008)(38100700002)(8676002)(76116006)(66556008)(91956017)(122000001)(66476007)(2906002)(186003)(83380400001)(316002)(110136005)(6486002)(38070700005)(71200400001)(508600001)(86362001)(36756003)(8936002)(5660300002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?wj93ecyLo0F0h15varjfxuu?= =?iso-8859-1?q?h2Ls4Ke3ktIi61EkOfZezOwkp4YPmzPL/+GF0FASRX7/M5Q3M7LMTzC5nsUW?= =?iso-8859-1?q?McatuTbDjI7lejsLGK1LdnfdVIJY4kP6VtewgZqFliuYf18Kk8CoN4qWd4n6?= =?iso-8859-1?q?7Y+BVCjCxbwhERg5ZG2PSraS/EpSb85YkJw59czRELrAYoWaaZRMgxN/BlAE?= =?iso-8859-1?q?Lzs0rBqhPJnyZ6glRbBGT/mqkdiQIKq97dEcSs44vXdMVixsMZ1ttqQ/1uUg?= =?iso-8859-1?q?D4hAOMUooSw3OhlI0IyT2VAazqbl46ssrkOHJwAlls14kauWMElbYO4YFtEh?= =?iso-8859-1?q?KsFR7htnMb+COJdiTNUwv8umm3N7VQUbwYj01kB/a5UHmo50qYgatmFWhyow?= =?iso-8859-1?q?Y+EWvuRxI6ifvHFmVFiOlX7oUAR9CN+utnIfErauOxDxAjsp9BCfnhvXUNGr?= =?iso-8859-1?q?eClxbf/xi5HovtpVGGe9iGlZEev8Ns5mUWadBjaULO9oO7JRdnRObyb9g25q?= =?iso-8859-1?q?Sc0ET8+DeBFjrInB5bUSq6P5qMCQhLXLGGlMU9i0JyvaabNJIT3aMsjGqRpN?= =?iso-8859-1?q?rigmYdOv9ftDWJUE+ZZcaaqiemrHsEGCxEOqtGO7Yl31L4vrvkzj/pATqnbd?= =?iso-8859-1?q?5sHLJEacKzeKuYvcDM4t3g+BX1A0+eBAujoJ0AuYmaCaEVmaNvWu0K+PaCqt?= =?iso-8859-1?q?jYwlQEYYoQ4NBYYvnxw2c1z/uaGFSvE7k2DkShfn6cLf8z0He8lU6lxJmyT2?= =?iso-8859-1?q?7rmlxMEYjxbCvlQOTz8wPOJX+okS9tKH5Vv+f3wiuUL9im7ZwZmx0i6ivCQe?= =?iso-8859-1?q?KDg1Hcj77CHkzoB26PnlUd4YW42kkiaCrTUpUqJ3ej+G7RJDUDZAO0wmS0lm?= =?iso-8859-1?q?VOBv/Uj0TZOw+Oqw9vyovN8/+pgIZSbbXPWEJtBko+i4LMiKd8yGHk3VcDaO?= =?iso-8859-1?q?KsL65e/K4F0JpnYg0lzriiRlyzjC+HI7u34qitDSG44sFl1b3AujdiHOc6g4?= =?iso-8859-1?q?q6LXIrz45eNtl7jqVPoZXWGUW5adkIbKcyby/lizeIj5BNOAJPwoPrf93XGx?= =?iso-8859-1?q?T2yBh1P8S/hdUaAIDlnoYMoAFLn+lnDZJT6pG86BaN+CtQ9+QtErfAXad4Af?= =?iso-8859-1?q?lYJKujpdFMRvp6rwp5xvQAWDUk5jQhi7lbucbv9R+Sd5eNtyHU8r6zESrOF6?= =?iso-8859-1?q?ForISA8XRj4Hq98fsGyOgF54VxaAWeHgiUtXHSq7bin2zkt1ZmyRS9Wg7KOm?= =?iso-8859-1?q?xBQREaoabsNbLw0z8/rjbazXKFdZonefKgTLgjc6fu1Oazd8tXggkNTYOail?= =?iso-8859-1?q?InZ0nzt9jD1Lpwh7tgsr8VC/xyAV0m1T6xHSxjCbqcF0FxSP+BX3Xe9LlT3f?= =?iso-8859-1?q?P898SIB4JQYC7mYHLHk4DoCJoPyqf7oEUW9Rk717blnaiBozFSXzwoFnhrV2?= =?iso-8859-1?q?XSfqdX4q5BHEXYBRROpcKR9UJI5qAXrcNXB6kWJE8rnVEBZCuD4qFFnW8+Tb?= =?iso-8859-1?q?2uWYsdpNS7Pv8KhnuZUT5UeDBTTS1k4hp8Wpd+wvbb5fL1UYYlYQ=3D?= MIME-Version: 1.0 X-OriginatorOrg: fb.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SJ0PR15MB5154.namprd15.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7a47d0c7-e480-4f9b-31bc-08da06d14716 X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Mar 2022 22:15:12.1317 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: LvqAgbEvTgV6iio+05pPsjGX+LJljo7hwzBuNdylWfJr1Fo43XZS5uFdTtGcx6at X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR15MB2695 X-Proofpoint-GUID: cTcUTFj4F8oxTnSm1Dwylth2aMRRypYi X-Proofpoint-ORIG-GUID: cTcUTFj4F8oxTnSm1Dwylth2aMRRypYi X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.850,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-15_11,2022-03-15_01,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net In symmetry with bpf_object__open_skeleton(), bpf_object__open_subskeleton() performs the actual walking and linking of maps, progs, and globals described by bpf_*_skeleton objects. Signed-off-by: Delyan Kratunov --- tools/lib/bpf/libbpf.c | 136 +++++++++++++++++++++++++++++++++------ tools/lib/bpf/libbpf.h | 29 +++++++++ tools/lib/bpf/libbpf.map | 2 + 3 files changed, 146 insertions(+), 21 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index e98a8381aad8..dac905171aaf 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -11812,6 +11812,49 @@ int libbpf_num_possible_cpus(void) return tmp_cpus; } +static int populate_skeleton_maps(const struct bpf_object *obj, + struct bpf_map_skeleton *maps, + size_t map_cnt) +{ + int i; + + for (i = 0; i < map_cnt; i++) { + struct bpf_map **map = maps[i].map; + const char *name = maps[i].name; + void **mmaped = maps[i].mmaped; + + *map = bpf_object__find_map_by_name(obj, name); + if (!*map) { + pr_warn("failed to find skeleton map '%s'\n", name); + return -ESRCH; + } + + /* externs shouldn't be pre-setup from user code */ + if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG) + *mmaped = (*map)->mmaped; + } + return 0; +} + +static int populate_skeleton_progs(const struct bpf_object *obj, + struct bpf_prog_skeleton *progs, + size_t prog_cnt) +{ + int i; + + for (i = 0; i < prog_cnt; i++) { + struct bpf_program **prog = progs[i].prog; + const char *name = progs[i].name; + + *prog = bpf_object__find_program_by_name(obj, name); + if (!*prog) { + pr_warn("failed to find skeleton program '%s'\n", name); + return -ESRCH; + } + } + return 0; +} + int bpf_object__open_skeleton(struct bpf_object_skeleton *s, const struct bpf_object_open_opts *opts) { @@ -11819,7 +11862,7 @@ int bpf_object__open_skeleton(struct bpf_object_skeleton *s, .object_name = s->name, ); struct bpf_object *obj; - int i, err; + int err; /* Attempt to preserve opts->object_name, unless overriden by user * explicitly. Overwriting object name for skeletons is discouraged, @@ -11842,37 +11885,88 @@ int bpf_object__open_skeleton(struct bpf_object_skeleton *s, } *s->obj = obj; + err = populate_skeleton_maps(obj, s->maps, s->map_cnt); + if (err) { + pr_warn("failed to populate skeleton maps for '%s': %d\n", s->name, err); + return libbpf_err(err); + } - for (i = 0; i < s->map_cnt; i++) { - struct bpf_map **map = s->maps[i].map; - const char *name = s->maps[i].name; - void **mmaped = s->maps[i].mmaped; + err = populate_skeleton_progs(obj, s->progs, s->prog_cnt); + if (err) { + pr_warn("failed to populate skeleton progs for '%s': %d\n", s->name, err); + return libbpf_err(err); + } - *map = bpf_object__find_map_by_name(obj, name); - if (!*map) { - pr_warn("failed to find skeleton map '%s'\n", name); - return libbpf_err(-ESRCH); - } + return 0; +} - /* externs shouldn't be pre-setup from user code */ - if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG) - *mmaped = (*map)->mmaped; +int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s) +{ + int err, len, var_idx, i; + const char *var_name; + const struct bpf_map *map; + struct btf *btf; + __u32 map_type_id; + const struct btf_type *map_type, *var_type; + const struct bpf_var_skeleton *var_skel; + struct btf_var_secinfo *var; + + if (!s->obj) + return libbpf_err(-EINVAL); + + btf = bpf_object__btf(s->obj); + if (!btf) + return libbpf_err(-errno); + + err = populate_skeleton_maps(s->obj, s->maps, s->map_cnt); + if (err) { + pr_warn("failed to populate subskeleton maps: %d\n", err); + return libbpf_err(err); } - for (i = 0; i < s->prog_cnt; i++) { - struct bpf_program **prog = s->progs[i].prog; - const char *name = s->progs[i].name; + err = populate_skeleton_progs(s->obj, s->progs, s->prog_cnt); + if (err) { + pr_warn("failed to populate subskeleton maps: %d\n", err); + return libbpf_err(err); + } - *prog = bpf_object__find_program_by_name(obj, name); - if (!*prog) { - pr_warn("failed to find skeleton program '%s'\n", name); - return libbpf_err(-ESRCH); + for (var_idx = 0; var_idx < s->var_cnt; var_idx++) { + var_skel = &s->vars[var_idx]; + map = *var_skel->map; + map_type_id = bpf_map__btf_value_type_id(map); + map_type = btf__type_by_id(btf, map_type_id); + + if (!btf_is_datasec(map_type)) { + pr_warn("Type for map '%1$s' is not a datasec: %2$s", + bpf_map__name(map), + __btf_kind_str(btf_kind(map_type))); + return libbpf_err(-EINVAL); } - } + len = btf_vlen(map_type); + var = btf_var_secinfos(map_type); + for (i = 0; i < len; i++, var++) { + var_type = btf__type_by_id(btf, var->type); + var_name = btf__name_by_offset(btf, var_type->name_off); + if (strcmp(var_name, var_skel->name) == 0) { + *var_skel->addr = map->mmaped + var->offset; + break; + } + } + } return 0; } +void bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s) +{ + if (!s) + return; + free(s->maps); + free(s->progs); + free(s->vars); + free(s); +} + int bpf_object__load_skeleton(struct bpf_object_skeleton *s) { int i, err; diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index c1b0c2ef14d8..1bff7647d797 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1289,6 +1289,35 @@ LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s); LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s); LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s); +struct bpf_var_skeleton { + const char *name; + struct bpf_map **map; + void **addr; +}; + +struct bpf_object_subskeleton { + size_t sz; /* size of this struct, for forward/backward compatibility */ + + const struct bpf_object *obj; + + int map_cnt; + int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */ + struct bpf_map_skeleton *maps; + + int prog_cnt; + int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */ + struct bpf_prog_skeleton *progs; + + int var_cnt; + int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */ + struct bpf_var_skeleton *vars; +}; + +LIBBPF_API int +bpf_object__open_subskeleton(struct bpf_object_subskeleton *s); +LIBBPF_API void +bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s); + struct gen_loader_opts { size_t sz; /* size of this struct, for forward/backward compatiblity */ const char *data; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index df1b947792c8..087c77e520e1 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -442,6 +442,8 @@ LIBBPF_0.7.0 { LIBBPF_0.8.0 { global: + bpf_object__destroy_subskeleton; + bpf_object__open_subskeleton; libbpf_register_prog_handler; libbpf_unregister_prog_handler; } LIBBPF_0.7.0; From patchwork Tue Mar 15 22:15:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12781889 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 2254EC433EF for ; Tue, 15 Mar 2022 22:15:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344201AbiCOWQi (ORCPT ); Tue, 15 Mar 2022 18:16:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46020 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345255AbiCOWQh (ORCPT ); Tue, 15 Mar 2022 18:16:37 -0400 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C75835577B for ; Tue, 15 Mar 2022 15:15:23 -0700 (PDT) Received: from pps.filterd (m0109332.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 22FLfkcB020039 for ; Tue, 15 Mar 2022 15:15:23 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=facebook; bh=QFTwaEYO+ibG8uY6y425JEOT7e0XT93C5Ujgt3Rs/8U=; b=oBVcTSUc9IVMEj9Rp+344j9yr44H7iVLI/Alpxf/5UQ9bWiZpZ4ccXvG3l4aQSx3SLZy C67Hx5Y5Lmrzj4tCUXzmaZ+U8+EjoOBa1+7EYcPpdTFsz5jW2QY4ZwX/EQKzEeGT5zFE YgmoHkKzIo5Wu1EQFr4UdsvGrY4lksT92mY= Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2173.outbound.protection.outlook.com [104.47.58.173]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3et7cycbb2-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 15 Mar 2022 15:15:22 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=D5nPitdJJLobbKBXozjXohjYHDhFcq36+HSCu/uoQ9kXiWlFiWlgOXe3mBQgyPO2PJUNK5je/v3VbGSrJF7NcxjYu/vmhzyFFnNxc1ZtRvym87Qka7Hhzqo0Bc3DRYPwOVG/oTViCoJ8a3BEGJwnlwrSYtm6eiuqwZdZkvUF1ZUXTGlzw7uYrxuGFUyFiAcsln1iqK2LPdWLzgVfFOJswQT4GGiAE1jIz3nKQyxoXVOfp73M/DcEQA0CqRrIrFd80xoLYx4nYQep3FTgXBLRXF05u/Lr1DQQQdPCWzyVcG7cVhaNyELpnOeAgLf8IY32hCtvHru53j4H7fGB9daTHQ== 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=QFTwaEYO+ibG8uY6y425JEOT7e0XT93C5Ujgt3Rs/8U=; b=g0vABuV/NPyNG6EsFH9J+4Y+lEiheqfRGClvj3B8FtiV8f1DKtCBNRTxwMQeTWQQAybxEbtPfpT00u58J+NujLCBmaWDQxG6D1q5tPqYL4kg9EZiTtojJf8GtmWbyWBse3Ig5nGPTEPVGKStPeb0g8NaUDz68aEPNwRQK4RJs5Huf7HwnjjnFF6quYu8Mq2c8UBI8vxDBa7bYy5kOU9xDAmNmv8ZoLw4GkcsCBjNZscjQ4DyhQH9NiJ4W6frXcXT4zRgU8RNJGs5CW2UE6xBjJ1+gvdb7CoCfFnaYJC597wnie3DOzpoh826ulmVccAGw20tF3NpMwoq7svLaPvXDg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass header.d=fb.com; arc=none Received: from SJ0PR15MB5154.namprd15.prod.outlook.com (2603:10b6:a03:423::6) by BYAPR15MB2695.namprd15.prod.outlook.com (2603:10b6:a03:150::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5061.28; Tue, 15 Mar 2022 22:15:17 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3%6]) with mapi id 15.20.5081.014; Tue, 15 Mar 2022 22:15:17 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v3 4/5] bpftool: add support for subskeletons Thread-Topic: [PATCH bpf-next v3 4/5] bpftool: add support for subskeletons Thread-Index: AQHYOLol+WztxSpPs0O0PPej6M9nrA== Date: Tue, 15 Mar 2022 22:15:15 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 6e97796b-38de-4223-ffe1-08da06d1497a x-ms-traffictypediagnostic: BYAPR15MB2695:EE_ x-microsoft-antispam-prvs: x-fb-source: Internal x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: kiN0M9C8Djuz4RVB1DgDTcSOPM9tpSs0KXaaQj6JAwBnanjEAidei4iYcTnwYdz7YECmUkAyi2coZcxKs6ppy43Ege4ccXuFv9xudtvE5B8WFSb0r03OjPBUmsLGF4NR6Z0ElKf1XcCvTjAJ3UG0/Oeky8dXIGAPspMCYarIrZ0aRtc6GOPkYsTjMb4d8+I86F+chTUf2w3vNFX0WHxdIbMLmZuxjQSFTSJB5t1KbW0FKhuvpZoFtLKeW7XY4pLVqJrSj03XmnHmaTbsWCt6rLKwh9M/5ETbcs3GIESJdaQm+d1SxNXhX1PEn3TBeOT06YSrxyObJd+Ri3Q/UHmD/MD754d1uzxRouESMXbX0ALxCAlrkoEeZtAlgJ1OjZ9Gd0Qa/3hjTjyapGBbwk3wHfhWJA9yHDfAUEat18vsqBGvvpCtS8jcHddljeRVF4pYCNc8RyQFOYd4VG0uUApgGW5HdsE9D/QIu3MOPnWr1zuDLU4WVs1gbUyjE9sxEt+3kEupY2EEXe5HfL7alwNj7VPSP56NciJwRvgCqSwMQgDYhlJw1g8TnyEXwlvJIW/AoMdP24zS9oJHUuDDyQZUSk4L+ZNNUNJg1mPeVaPUUXAd8VI0w/0T9hjdjH0rUw3H9M8B1MPbX4z3MlxO0njoL/4XgyKVULnTxrwW2C8vdjCBD5wSRQASKOXkkYmEakQ42oyhr7QA87+d7zuwSywqMA== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR15MB5154.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(66446008)(66946007)(6512007)(6506007)(2616005)(64756008)(38100700002)(8676002)(76116006)(66556008)(91956017)(122000001)(66476007)(2906002)(186003)(83380400001)(316002)(110136005)(6486002)(38070700005)(71200400001)(508600001)(30864003)(86362001)(36756003)(8936002)(5660300002)(579004)(559001);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?Sc7P2p88kzMB8452V9EaXxg?= =?iso-8859-1?q?G4np9+5JvBzxdhaKEtkW6XiFGTXNhoHvOenkZImDCTEAWYjzLJsSf0JbHDlm?= =?iso-8859-1?q?7en8UQKisToyLyfMzFZLla9cTl8kayk87Iw/lz7P6xUVoGfsisS2Advxyf+9?= =?iso-8859-1?q?feTgLiXvxrpV9rTkZj1wNOUB9wCx2v2/yF2or8HRjnbPDoZaDT6Tlu+YV7zZ?= =?iso-8859-1?q?/AHmrIvBJPVbcIB2vsLpYD/vcoh7sSfvf2o9X1UPA3gN883D93P/qOQ3Pz4v?= =?iso-8859-1?q?6bG9wHZYovAncCzZffNgQl61w9DpjqLtvorHDt3YKRTnrB7lJPg34O9ETwbN?= =?iso-8859-1?q?T4F6S7Qh4MUGIw7TpsR3lGnZPwPirqHbtVwQVfIHhRYZQFFz2ASEq9uG0ZZr?= =?iso-8859-1?q?JOX7Kj6HWMSjve41f04n3pUjx9Bclgyf7QyUypi5mdqAJpVlujBI0qNn5+Sf?= =?iso-8859-1?q?q9fWklw1GCUVL6orMHQfppQ8jvFJ/pNPtGGuAcyvj54Ubt5TSeF0vjiJrOU2?= =?iso-8859-1?q?rBXkEwX8sGKJoosMAyqJR1I+FSlgpBg+n0O6U13bqc6PFnLsSkikoVm7I4wD?= =?iso-8859-1?q?JAshyd6sNi+hhtQCyMhm0llVY/PCvL1YwavBwkCcjVHIHFMJ4nGkKnKYG8MM?= =?iso-8859-1?q?KIaCXBHXAKcv3aqp8Jp4zmxOghFjJVwUPqLjN8ITEgEUuzLkOzpNFM1xadvM?= =?iso-8859-1?q?edxwZ18GTS9Gl0UzwRtn7k16lPzu3X1Qe409vIXx91rY8L3VwAmaE0SfYWyb?= =?iso-8859-1?q?QhaqqCWtcL4gHrwuyi5bWTtJ+uqlFOe9F6+KtUy0rTw+CJijjQfW9Trd6QsQ?= =?iso-8859-1?q?SdBBgU7LoDbhCqlUphEkFKMqqLceFrzXYUE2AVYtkGnHcM6/V0iGPwFkxX0D?= =?iso-8859-1?q?BN8+cZyXvG2GkPLb+tVXIxpnS1ZtRgjuh7615hd7MiTvoOB06bH8dqE3gPWG?= =?iso-8859-1?q?zl3UXlu47oLjQdqSqpzGkqIPg/kX9kimVCXTANgwBFT2ZV6OO/JHOK2JtTBC?= =?iso-8859-1?q?IYQfgAx8bXqhmIZgfb5xDjfROc1c30LsiakNVF59K4yzd4jzHKNOrCClquNx?= =?iso-8859-1?q?iXaBUS+X+yN/tlBHnDvRcKnIUV7AJZ3mudAzbr9Pir65VyaE8gdUnro8iAPd?= =?iso-8859-1?q?/N1wEv0M/IUFq9+DxIitRg0wRm+ki4r8wcmrLuB2N1AY56YglMzS8NSGopB4?= =?iso-8859-1?q?tlfzHonLL1JAgKiflHl9VaYz8PSKd0DGlabmQaUt0LmVUa4ArBLwOqBrtJJp?= =?iso-8859-1?q?u21xVnU2FnQuhPdyUh8IiCULIOLC3wqesQ+CtGa5GHmA+ElXM+lSVMIPew3F?= =?iso-8859-1?q?pKoa1CuR9wEF9ZkV/TTgA7h50lAxsaptwk1Afcbw709Leq622fXz9hsV4QID?= =?iso-8859-1?q?41TaZ/reAGAcKoiH7UjpDzTWBOKTVH4duaoYDxfkMq2kR19bAlgko1agVrKv?= =?iso-8859-1?q?JNNdQ02gQ1319uzc58rbu6EOWXxkWeVfevg8bpk8pPsrl5WiQmYyC/8j6/aj?= =?iso-8859-1?q?iTrzeJ3W1gl3s3pnpiI2I3ZlI8j5BntpHVqTf/jyMH9kPhbqHMfQ=3D?= MIME-Version: 1.0 X-OriginatorOrg: fb.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SJ0PR15MB5154.namprd15.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6e97796b-38de-4223-ffe1-08da06d1497a X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Mar 2022 22:15:15.8159 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: HZ6sa1Lb5yzAk6JzEttFf+n3ojMlU7D1/xkupe3DblBsgBFVYeGZwAiMLG5ASulu X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR15MB2695 X-Proofpoint-GUID: JDr653u41897qKCwzTw65Qm-7d8AQv5N X-Proofpoint-ORIG-GUID: JDr653u41897qKCwzTw65Qm-7d8AQv5N X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.850,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-15_11,2022-03-15_01,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Subskeletons are headers which require an already loaded program to operate. For example, when a BPF library is linked into a larger BPF object file, the library userspace needs a way to access its own global variables without requiring knowledge about the larger program at build time. As a result, subskeletons require a loaded bpf_object to open(). Further, they find their own symbols in the larger program by walking BTF type data at run time. At this time, programs, maps, and globals are supported through non-owning pointers. Signed-off-by: Delyan Kratunov --- .../bpf/bpftool/Documentation/bpftool-gen.rst | 25 + tools/bpf/bpftool/bash-completion/bpftool | 14 +- tools/bpf/bpftool/gen.c | 595 +++++++++++++++--- 3 files changed, 549 insertions(+), 85 deletions(-) diff --git a/tools/bpf/bpftool/Documentation/bpftool-gen.rst b/tools/bpf/bpftool/Documentation/bpftool-gen.rst index 18d646b571ec..68454ef28f58 100644 --- a/tools/bpf/bpftool/Documentation/bpftool-gen.rst +++ b/tools/bpf/bpftool/Documentation/bpftool-gen.rst @@ -25,6 +25,7 @@ GEN COMMANDS | **bpftool** **gen object** *OUTPUT_FILE* *INPUT_FILE* [*INPUT_FILE*...] | **bpftool** **gen skeleton** *FILE* [**name** *OBJECT_NAME*] +| **bpftool** **gen subskeleton** *FILE* [**name** *OBJECT_NAME*] | **bpftool** **gen min_core_btf** *INPUT* *OUTPUT* *OBJECT* [*OBJECT*...] | **bpftool** **gen help** @@ -150,6 +151,30 @@ DESCRIPTION (non-read-only) data from userspace, with same simplicity as for BPF side. + **bpftool gen subskeleton** *FILE* + Generate BPF subskeleton C header file for a given *FILE*. + + Subskeletons are similar to skeletons, except they do not own + the corresponding maps, programs, or global variables. They + require that the object file used to generate them is already + loaded into a *bpf_object* by some other means. + + This functionality is useful when a library is included into a + larger BPF program. A subskeleton for the library would have + access to all objects and globals defined in it, without + having to know about the larger program. + + Consequently, there are only two functions defined + for subskeletons: + + - **example__open(bpf_object\*)** + Instantiates a subskeleton from an already opened (but not + necessarily loaded) **bpf_object**. + + - **example__destroy()** + Frees the storage for the subskeleton but *does not* unload + any BPF programs or maps. + **bpftool** **gen min_core_btf** *INPUT* *OUTPUT* *OBJECT* [*OBJECT*...] Generate a minimum BTF file as *OUTPUT*, derived from a given *INPUT* BTF file, containing all needed BTF types so one, or diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool index 958e1fd71b5c..5df8d72c5179 100644 --- a/tools/bpf/bpftool/bash-completion/bpftool +++ b/tools/bpf/bpftool/bash-completion/bpftool @@ -1003,13 +1003,25 @@ _bpftool() ;; esac ;; + subskeleton) + case $prev in + $command) + _filedir + return 0 + ;; + *) + _bpftool_once_attr 'name' + return 0 + ;; + esac + ;; min_core_btf) _filedir return 0 ;; *) [[ $prev == $object ]] && \ - COMPREPLY=( $( compgen -W 'object skeleton help min_core_btf' -- "$cur" ) ) + COMPREPLY=( $( compgen -W 'object skeleton subskeleton help min_core_btf' -- "$cur" ) ) ;; esac ;; diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index 145734b4fe41..309fbc33e89f 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -64,11 +64,11 @@ static void get_obj_name(char *name, const char *file) sanitize_identifier(name); } -static void get_header_guard(char *guard, const char *obj_name) +static void get_header_guard(char *guard, const char *obj_name, const char *suffix) { int i; - sprintf(guard, "__%s_SKEL_H__", obj_name); + sprintf(guard, "__%s_%s__", obj_name, suffix); for (i = 0; guard[i]; i++) guard[i] = toupper(guard[i]); } @@ -231,6 +231,17 @@ static const struct btf_type *find_type_for_map(struct btf *btf, const char *map return NULL; } +static bool bpf_map__is_internal_mmappable(const struct bpf_map *map, char *buf, size_t sz) +{ + if (!bpf_map__is_internal(map) || !(bpf_map__map_flags(map) & BPF_F_MMAPABLE)) + return false; + + if (!get_map_ident(map, buf, sz)) + return false; + + return true; +} + static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) { struct btf *btf = bpf_object__btf(obj); @@ -247,12 +258,7 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) bpf_object__for_each_map(map, obj) { /* only generate definitions for memory-mapped internal maps */ - if (!bpf_map__is_internal(map)) - continue; - if (!(bpf_map__map_flags(map) & BPF_F_MMAPABLE)) - continue; - - if (!get_map_ident(map, map_ident, sizeof(map_ident))) + if (!bpf_map__is_internal_mmappable(map, map_ident, sizeof(map_ident))) continue; sec = find_type_for_map(btf, map_ident); @@ -280,6 +286,96 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) return err; } +static bool btf_is_ptr_to_func_proto(const struct btf *btf, + const struct btf_type *v) +{ + return btf_is_ptr(v) && btf_is_func_proto(btf__type_by_id(btf, v->type)); +} + +static int codegen_subskel_datasecs(struct bpf_object *obj, const char *obj_name) +{ + struct btf *btf = bpf_object__btf(obj); + struct btf_dump *d; + struct bpf_map *map; + const struct btf_type *sec, *var; + const struct btf_var_secinfo *sec_var; + int i, err = 0, vlen; + char map_ident[256], sec_ident[256]; + bool strip_mods = false, needs_typeof = false; + const char *sec_name, *var_name; + __u32 var_type_id; + + d = btf_dump__new(btf, codegen_btf_dump_printf, NULL, NULL); + if (!d) + return -errno; + + bpf_object__for_each_map(map, obj) { + /* only generate definitions for memory-mapped internal maps */ + if (!bpf_map__is_internal_mmappable(map, map_ident, sizeof(map_ident))) + continue; + + sec = find_type_for_map(btf, map_ident); + if (!sec) + continue; + + sec_name = btf__name_by_offset(btf, sec->name_off); + if (!get_datasec_ident(sec_name, sec_ident, sizeof(sec_ident))) + continue; + + strip_mods = strcmp(sec_name, ".kconfig") != 0; + printf(" struct %s__%s {\n", obj_name, sec_ident); + + sec_var = btf_var_secinfos(sec); + vlen = btf_vlen(sec); + for (i = 0; i < vlen; i++, sec_var++) { + DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts, + .indent_level = 2, + .strip_mods = strip_mods, + /* we'll print the name separately */ + .field_name = "", + ); + + var = btf__type_by_id(btf, sec_var->type); + var_name = btf__name_by_offset(btf, var->name_off); + var_type_id = var->type; + + /* static variables are not exposed through BPF skeleton */ + if (btf_var(var)->linkage == BTF_VAR_STATIC) + continue; + + /* The datasec member has KIND_VAR but we want the + * underlying type of the variable (e.g. KIND_INT). + */ + var = skip_mods_and_typedefs(btf, var->type, NULL); + + printf("\t\t"); + /* Func and array members require special handling. + * Instead of producing `typename *var`, they produce + * `typeof(typename) *var`. This allows us to keep a + * similar syntax where the identifier is just prefixed + * by *, allowing us to ignore C declaration minutae. + */ + needs_typeof = btf_is_array(var) || btf_is_ptr_to_func_proto(btf, var); + if (needs_typeof) + printf("typeof("); + + err = btf_dump__emit_type_decl(d, var_type_id, &opts); + if (err) + goto out; + + if (needs_typeof) + printf(")"); + + printf(" *%s;\n", var_name); + } + printf(" } %s;\n", sec_ident); + } + +out: + btf_dump__free(d); + return err; +} + static void codegen(const char *template, ...) { const char *src, *end; @@ -389,11 +485,7 @@ static void codegen_asserts(struct bpf_object *obj, const char *obj_name) ", obj_name); bpf_object__for_each_map(map, obj) { - if (!bpf_map__is_internal(map)) - continue; - if (!(bpf_map__map_flags(map) & BPF_F_MMAPABLE)) - continue; - if (!get_map_ident(map, map_ident, sizeof(map_ident))) + if (!bpf_map__is_internal_mmappable(map, map_ident, sizeof(map_ident))) continue; sec = find_type_for_map(btf, map_ident); @@ -608,11 +700,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h const void *mmap_data = NULL; size_t mmap_size = 0; - if (!get_map_ident(map, ident, sizeof(ident))) - continue; - - if (!bpf_map__is_internal(map) || - !(bpf_map__map_flags(map) & BPF_F_MMAPABLE)) + if (!bpf_map__is_internal_mmappable(map, ident, sizeof(ident))) continue; codegen("\ @@ -671,11 +759,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h bpf_object__for_each_map(map, obj) { const char *mmap_flags; - if (!get_map_ident(map, ident, sizeof(ident))) - continue; - - if (!bpf_map__is_internal(map) || - !(bpf_map__map_flags(map) & BPF_F_MMAPABLE)) + if (!bpf_map__is_internal_mmappable(map, ident, sizeof(ident))) continue; if (bpf_map__map_flags(map) & BPF_F_RDONLY_PROG) @@ -727,10 +811,96 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h return err; } +static void +codegen_maps_skeleton(struct bpf_object *obj, size_t map_cnt, bool mmaped) +{ + struct bpf_map *map; + char ident[256]; + size_t i; + + if (!map_cnt) + return; + + codegen("\ + \n\ + \n\ + /* maps */ \n\ + s->map_cnt = %zu; \n\ + s->map_skel_sz = sizeof(*s->maps); \n\ + s->maps = (struct bpf_map_skeleton *)calloc(s->map_cnt, s->map_skel_sz);\n\ + if (!s->maps) \n\ + goto err; \n\ + ", + map_cnt + ); + i = 0; + bpf_object__for_each_map(map, obj) { + if (!get_map_ident(map, ident, sizeof(ident))) + continue; + + codegen("\ + \n\ + \n\ + s->maps[%zu].name = \"%s\"; \n\ + s->maps[%zu].map = &obj->maps.%s; \n\ + ", + i, bpf_map__name(map), i, ident); + /* memory-mapped internal maps */ + if (mmaped && bpf_map__is_internal(map) && + (bpf_map__map_flags(map) & BPF_F_MMAPABLE)) { + printf("\ts->maps[%zu].mmaped = (void **)&obj->%s;\n", + i, ident); + } + i++; + } +} + +static void +codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_links) +{ + struct bpf_program *prog; + int i; + + if (!prog_cnt) + return; + + codegen("\ + \n\ + \n\ + /* programs */ \n\ + s->prog_cnt = %zu; \n\ + s->prog_skel_sz = sizeof(*s->progs); \n\ + s->progs = (struct bpf_prog_skeleton *)calloc(s->prog_cnt, s->prog_skel_sz);\n\ + if (!s->progs) \n\ + goto err; \n\ + ", + prog_cnt + ); + i = 0; + bpf_object__for_each_program(prog, obj) { + codegen("\ + \n\ + \n\ + s->progs[%1$zu].name = \"%2$s\"; \n\ + s->progs[%1$zu].prog = &obj->progs.%2$s;\n\ + ", + i, bpf_program__name(prog)); + + if (populate_links) { + codegen("\ + \n\ + s->progs[%1$zu].link = &obj->links.%2$s;\n\ + ", + i, bpf_program__name(prog)); + } + i++; + } +} + static int do_skeleton(int argc, char **argv) { char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")]; - size_t i, map_cnt = 0, prog_cnt = 0, file_sz, mmap_sz; + size_t map_cnt = 0, prog_cnt = 0, file_sz, mmap_sz; DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts); char obj_name[MAX_OBJ_NAME_LEN] = "", *obj_data; struct bpf_object *obj = NULL; @@ -821,7 +991,7 @@ static int do_skeleton(int argc, char **argv) prog_cnt++; } - get_header_guard(header_guard, obj_name); + get_header_guard(header_guard, obj_name, "SKEL_H"); if (use_loader) { codegen("\ \n\ @@ -1024,66 +1194,10 @@ static int do_skeleton(int argc, char **argv) ", obj_name ); - if (map_cnt) { - codegen("\ - \n\ - \n\ - /* maps */ \n\ - s->map_cnt = %zu; \n\ - s->map_skel_sz = sizeof(*s->maps); \n\ - s->maps = (struct bpf_map_skeleton *)calloc(s->map_cnt, s->map_skel_sz);\n\ - if (!s->maps) \n\ - goto err; \n\ - ", - map_cnt - ); - i = 0; - bpf_object__for_each_map(map, obj) { - if (!get_map_ident(map, ident, sizeof(ident))) - continue; - codegen("\ - \n\ - \n\ - s->maps[%zu].name = \"%s\"; \n\ - s->maps[%zu].map = &obj->maps.%s; \n\ - ", - i, bpf_map__name(map), i, ident); - /* memory-mapped internal maps */ - if (bpf_map__is_internal(map) && - (bpf_map__map_flags(map) & BPF_F_MMAPABLE)) { - printf("\ts->maps[%zu].mmaped = (void **)&obj->%s;\n", - i, ident); - } - i++; - } - } - if (prog_cnt) { - codegen("\ - \n\ - \n\ - /* programs */ \n\ - s->prog_cnt = %zu; \n\ - s->prog_skel_sz = sizeof(*s->progs); \n\ - s->progs = (struct bpf_prog_skeleton *)calloc(s->prog_cnt, s->prog_skel_sz);\n\ - if (!s->progs) \n\ - goto err; \n\ - ", - prog_cnt - ); - i = 0; - bpf_object__for_each_program(prog, obj) { - codegen("\ - \n\ - \n\ - s->progs[%1$zu].name = \"%2$s\"; \n\ - s->progs[%1$zu].prog = &obj->progs.%2$s;\n\ - s->progs[%1$zu].link = &obj->links.%2$s;\n\ - ", - i, bpf_program__name(prog)); - i++; - } - } + codegen_maps_skeleton(obj, map_cnt, true /*mmaped*/); + codegen_progs_skeleton(obj, prog_cnt, true /*populate_links*/); + codegen("\ \n\ \n\ @@ -1141,6 +1255,317 @@ static int do_skeleton(int argc, char **argv) return err; } +/* Subskeletons are like skeletons, except they don't own the bpf_object, + * associated maps, links, etc. Instead, they know about the existence of + * variables, maps, programs and are able to find their locations + * _at runtime_ from an already loaded bpf_object. + * + * This allows for library-like BPF objects to have userspace counterparts + * with access to their own items without having to know anything about the + * final BPF object that the library was linked into. + */ +static int do_subskeleton(int argc, char **argv) +{ + char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SUBSKEL_H__")]; + size_t i, len, file_sz, map_cnt = 0, prog_cnt = 0, mmap_sz, var_cnt = 0, var_idx = 0; + DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts); + char obj_name[MAX_OBJ_NAME_LEN] = "", *obj_data; + struct bpf_object *obj = NULL; + const char *file, *var_name; + char ident[256], var_ident[256]; + int fd, err = -1, map_type_id; + const struct bpf_map *map; + struct bpf_program *prog; + struct btf *btf; + const struct btf_type *map_type, *var_type; + const struct btf_var_secinfo *var; + struct stat st; + + if (!REQ_ARGS(1)) { + usage(); + return -1; + } + file = GET_ARG(); + + while (argc) { + if (!REQ_ARGS(2)) + return -1; + + if (is_prefix(*argv, "name")) { + NEXT_ARG(); + + if (obj_name[0] != '\0') { + p_err("object name already specified"); + return -1; + } + + strncpy(obj_name, *argv, MAX_OBJ_NAME_LEN - 1); + obj_name[MAX_OBJ_NAME_LEN - 1] = '\0'; + } else { + p_err("unknown arg %s", *argv); + return -1; + } + + NEXT_ARG(); + } + + if (argc) { + p_err("extra unknown arguments"); + return -1; + } + + if (use_loader) { + p_err("cannot use loader for subskeletons"); + return -1; + } + + if (stat(file, &st)) { + p_err("failed to stat() %s: %s", file, strerror(errno)); + return -1; + } + file_sz = st.st_size; + mmap_sz = roundup(file_sz, sysconf(_SC_PAGE_SIZE)); + fd = open(file, O_RDONLY); + if (fd < 0) { + p_err("failed to open() %s: %s", file, strerror(errno)); + return -1; + } + obj_data = mmap(NULL, mmap_sz, PROT_READ, MAP_PRIVATE, fd, 0); + if (obj_data == MAP_FAILED) { + obj_data = NULL; + p_err("failed to mmap() %s: %s", file, strerror(errno)); + goto out; + } + if (obj_name[0] == '\0') + get_obj_name(obj_name, file); + + /* The empty object name allows us to use bpf_map__name and produce + * ELF section names out of it. (".data" instead of "obj.data") + */ + opts.object_name = ""; + obj = bpf_object__open_mem(obj_data, file_sz, &opts); + if (!obj) { + char err_buf[256]; + + libbpf_strerror(errno, err_buf, sizeof(err_buf)); + p_err("failed to open BPF object file: %s", err_buf); + obj = NULL; + goto out; + } + + btf = bpf_object__btf(obj); + if (!btf) { + err = -1; + p_err("need btf type information for %s", obj_name); + goto out; + } + + bpf_object__for_each_program(prog, obj) { + prog_cnt++; + } + + /* First, count how many variables we have to find. + * We need this in advance so the subskel can allocate the right + * amount of storage. + */ + bpf_object__for_each_map(map, obj) { + if (!get_map_ident(map, ident, sizeof(ident))) + continue; + + /* Also count all maps that have a name */ + map_cnt++; + + if (!bpf_map__is_internal(map)) + continue; + if (!(bpf_map__map_flags(map) & BPF_F_MMAPABLE)) + continue; + + map_type_id = bpf_map__btf_value_type_id(map); + if (map_type_id < 0) { + err = map_type_id; + goto out; + } + map_type = btf__type_by_id(btf, map_type_id); + + var = btf_var_secinfos(map_type); + len = btf_vlen(map_type); + for (i = 0; i < len; i++, var++) { + var_type = btf__type_by_id(btf, var->type); + + if (btf_var(var_type)->linkage == BTF_VAR_STATIC) + continue; + + var_cnt++; + } + } + + get_header_guard(header_guard, obj_name, "SUBSKEL_H"); + codegen("\ + \n\ + /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ \n\ + \n\ + /* THIS FILE IS AUTOGENERATED! */ \n\ + #ifndef %2$s \n\ + #define %2$s \n\ + \n\ + #include \n\ + #include \n\ + #include \n\ + \n\ + struct %1$s { \n\ + struct bpf_object *obj; \n\ + struct bpf_object_subskeleton *subskel; \n\ + ", obj_name, header_guard); + + if (map_cnt) { + printf("\tstruct {\n"); + bpf_object__for_each_map(map, obj) { + if (!get_map_ident(map, ident, sizeof(ident))) + continue; + printf("\t\tstruct bpf_map *%s;\n", ident); + } + printf("\t} maps;\n"); + } + + if (prog_cnt) { + printf("\tstruct {\n"); + bpf_object__for_each_program(prog, obj) { + printf("\t\tstruct bpf_program *%s;\n", + bpf_program__name(prog)); + } + printf("\t} progs;\n"); + } + + err = codegen_subskel_datasecs(obj, obj_name); + if (err) + goto out; + + /* emit code that will allocate enough storage for all symbols */ + codegen("\ + \n\ + \n\ + #ifdef __cplusplus \n\ + static inline struct %1$s *open(const struct bpf_object *src);\n\ + static inline void destroy(struct %1$s *skel);\n\ + #endif /* __cplusplus */ \n\ + }; \n\ + \n\ + static inline void \n\ + %1$s__destroy(struct %1$s *skel) \n\ + { \n\ + if (!skel) \n\ + return; \n\ + if (skel->subskel) \n\ + bpf_object__destroy_subskeleton(skel->subskel);\n\ + free(skel); \n\ + } \n\ + \n\ + static inline struct %1$s * \n\ + %1$s__open(const struct bpf_object *src) \n\ + { \n\ + struct %1$s *obj; \n\ + struct bpf_object_subskeleton *s; \n\ + int err; \n\ + \n\ + obj = (struct %1$s *)calloc(1, sizeof(*obj)); \n\ + if (!obj) { \n\ + errno = ENOMEM; \n\ + goto err; \n\ + } \n\ + s = (struct bpf_object_subskeleton *)calloc(1, sizeof(*s));\n\ + if (!s) { \n\ + errno = ENOMEM; \n\ + goto err; \n\ + } \n\ + s->sz = sizeof(*s); \n\ + s->obj = src; \n\ + s->var_skel_sz = sizeof(*s->vars); \n\ + obj->subskel = s; \n\ + \n\ + /* vars */ \n\ + s->var_cnt = %2$d; \n\ + s->vars = (struct bpf_var_skeleton *)calloc(%2$d, sizeof(*s->vars));\n\ + if (!s->vars) { \n\ + errno = ENOMEM; \n\ + goto err; \n\ + } \n\ + \n\ + ", + obj_name, var_cnt + ); + + /* walk through each symbol and emit the runtime representation */ + bpf_object__for_each_map(map, obj) { + if (!bpf_map__is_internal_mmappable(map, ident, sizeof(ident))) + continue; + + map_type_id = bpf_map__btf_value_type_id(map); + if (map_type_id < 0) + /* skip over internal maps with no type*/ + continue; + + map_type = btf__type_by_id(btf, map_type_id); + var = btf_var_secinfos(map_type); + len = btf_vlen(map_type); + for (i = 0; i < len; i++, var++) { + var_type = btf__type_by_id(btf, var->type); + var_name = btf__name_by_offset(btf, var_type->name_off); + + if (btf_var(var_type)->linkage == BTF_VAR_STATIC) + continue; + + var_ident[0] = '\0'; + strncat(var_ident, var_name, sizeof(var_ident) - 1); + sanitize_identifier(var_ident); + + /* Note that we use the dot prefix in .data as the + * field access operator i.e. maps%s becomes maps.data + */ + codegen("\ + \n\ + s->vars[%4$d].name = \"%1$s\"; \n\ + s->vars[%4$d].map = &obj->maps.%3$s; \n\ + s->vars[%4$d].addr = (void**) &obj->%2$s.%1$s;\n\ + ", var_ident, ident, ident, var_idx); + + var_idx++; + } + } + + codegen_maps_skeleton(obj, map_cnt, false /*mmaped*/); + codegen_progs_skeleton(obj, prog_cnt, false /*links*/); + + codegen("\ + \n\ + \n\ + err = bpf_object__open_subskeleton(s); \n\ + if (err) \n\ + goto err; \n\ + \n\ + return obj; \n\ + err: \n\ + %1$s__destroy(obj); \n\ + errno = -err; \n\ + return NULL; \n\ + } \n\ + \n\ + #ifdef __cplusplus \n\ + struct %1$s *%1$s::open(const struct bpf_object *src) { return %1$s__open(src); }\n\ + void %1$s::destroy(struct %1$s *skel) { %1$s__destroy(skel); }\n\ + #endif /* __cplusplus */ \n\ + \n\ + #endif /* %2$s */ \n\ + ", + obj_name, header_guard); + err = 0; +out: + bpf_object__close(obj); + if (obj_data) + munmap(obj_data, mmap_sz); + close(fd); + return err; +} + static int do_object(int argc, char **argv) { struct bpf_linker *linker; @@ -1192,6 +1617,7 @@ static int do_help(int argc, char **argv) fprintf(stderr, "Usage: %1$s %2$s object OUTPUT_FILE INPUT_FILE [INPUT_FILE...]\n" " %1$s %2$s skeleton FILE [name OBJECT_NAME]\n" + " %1$s %2$s subskeleton FILE [name OBJECT_NAME]\n" " %1$s %2$s min_core_btf INPUT OUTPUT OBJECT [OBJECT...]\n" " %1$s %2$s help\n" "\n" @@ -1788,6 +2214,7 @@ static int do_min_core_btf(int argc, char **argv) static const struct cmd cmds[] = { { "object", do_object }, { "skeleton", do_skeleton }, + { "subskeleton", do_subskeleton }, { "min_core_btf", do_min_core_btf}, { "help", do_help }, { 0 } From patchwork Tue Mar 15 22:15:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12781887 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 89408C433F5 for ; Tue, 15 Mar 2022 22:15:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343614AbiCOWQe (ORCPT ); Tue, 15 Mar 2022 18:16:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240840AbiCOWQe (ORCPT ); Tue, 15 Mar 2022 18:16:34 -0400 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 982D55574F for ; Tue, 15 Mar 2022 15:15:20 -0700 (PDT) Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 22FLfkuF009029 for ; Tue, 15 Mar 2022 15:15:19 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=facebook; bh=bpSyBZYfDZJO+j/9/M5sfXR5hWvYTgnecGBCL87Dlec=; b=QNpm4cVwFpvHAZ6wcvKMNhg3LGa/buMEcB+ENpz+hdBYoN+HNdocnz1aXKkE7+ZhDehr UyAmt880aiZxeUytWqqX4wz1YI4sWm7M65iB1RIyOcOsqO3hc9upsurwnA231eAXFj3h /QOuhcEk2Mr9BGuWhNoy9b7QEhbNEhoK6DY= Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2177.outbound.protection.outlook.com [104.47.58.177]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3eu2brghmn-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 15 Mar 2022 15:15:19 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Di8EKFoDimDEanA56oSSsSbDHPJRMqjVX1StwAW7KeJ8gu4ltJ46Ia65XUS9hBI4gXUVpLyhKQOrlUW2EEOI/KOap4Meb5V/XNGh1f9l2YEwrJzOXrzdlcRH5Ri+JXhQE+HXxFvl/zo58+GemLTtXSooFMLR6Xi6EgwQsbUVIPYH8TqeldSLYJCOQvhMa429aFJ2ZLPqAh7eBhYfVdFivXl+V9WkakuXRwCVj4Lhez91FTHUPrAg9gNRDSZJlSydPZ1XjigBHYVYIREug3Az+yGSSuResb/ApZv21esvYNNmWQseCJZYYEK7ss/SZc9d1K/0Qo9Myui7WGEkGkYXFQ== 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=bpSyBZYfDZJO+j/9/M5sfXR5hWvYTgnecGBCL87Dlec=; b=QVGAKvIvKSW9YLh8tASeNoXYNd0R2+B+mIpOaqIm3rMnS6uPypwWdYiUOVk/QIuNwuF7+b2exfZgIWSa2RvqxWgsdXYhNQO0MuDqnqCwYl3t23kie+YtHQ1dAPZfjOgxT8m23yZvbz4ltrhQI9X1L5krfPZGBkdY7XnrvGDTT8CQTAbFTrG+4jEJtAomL/MwoFQ0JJ7JpD15tPidNB0cha9sRjIUAJojEFKazkF8YqpbDbgBkvRnJTIAJFJJdYI5ORhz7g2ElXTFNHj5Lqx/yGRi+jehQjhjHsPphFeQ3ka2dTfhrb8MtGIQsQXikHA4Qj7Srj9+9/BdrSAquZDTsw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass header.d=fb.com; arc=none Received: from SJ0PR15MB5154.namprd15.prod.outlook.com (2603:10b6:a03:423::6) by BYAPR15MB2695.namprd15.prod.outlook.com (2603:10b6:a03:150::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5061.28; Tue, 15 Mar 2022 22:15:14 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3%6]) with mapi id 15.20.5081.014; Tue, 15 Mar 2022 22:15:14 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v3 5/5] selftests/bpf: test subskeleton functionality Thread-Topic: [PATCH bpf-next v3 5/5] selftests/bpf: test subskeleton functionality Thread-Index: AQHYOLokidjpb1++V0aDAKFsjqM1GA== Date: Tue, 15 Mar 2022 22:15:13 +0000 Message-ID: <9666648cd9f680756c53dd4286cf34e627c7c73c.1647382072.git.delyank@fb.com> References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: dedf8aad-dc33-425b-02fb-08da06d147c9 x-ms-traffictypediagnostic: BYAPR15MB2695:EE_ x-microsoft-antispam-prvs: x-fb-source: Internal x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: vwEV19A6drL78wPTWpWeZm0s4gHINRUTFBvlXRuKmvZPnsc1W7zKl+/xbdqwk9keyzyApyOU1HbmBSAXStBtoutwhOf6tpvf0caFOkqy74dsmtftOayseypHzxXmGAG3msg3goD6cjW75ZH3b17u9KwLsL+VVJHUEXkfLOBkMeST31RH0Vm/DUNcvK5UU7DvAc95F+17Xv5WQvEI1j2fvR+kr55QICzQ65hU9/G3IZbaBYGQu6u+DOzrX9r1whRP7NwHhKhPsdSlDBY0Hw3HAqjwVQlEKT3/hPJY15e2Ug2FG80zgdXXRmISQ/ejiY9tUNNrMAGPAaOw413v9fsWuBTjYkPa/Kbmu2KIouXblvWF0LEIjdiz6cGHbX6vjaATK3Aaur2CeyjximQ7EQTy7CwXUHnE67Pqn5a4Keq+AUEnrfdU5j4unXswKLJShfskocPI+A5MvDL9c1Z5qUdjaKWU9YM4TxvsUbXJ1RGrEsboz9y4ATmD+oYoPLGuao2Zv244u4JwcS0wldA3gYgu/XRs6ulEETRk8R5KQtqhSF0NvLn3KmHswNYLN2+X4YapyzAlZZJSzeP1q8CqkNE+AkAOL2Y9W8xl8UexfKUhg/9K8wMv8UL/2hcQmqtwDMJSoFR543fPBvyJUN3NlKD3GUhtjFeo5xhrrETRjWOjPLDNO/2GqMbhjbAnZ8iT5JBh9nd1d+wmCFgQxqp2caV2jA== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR15MB5154.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(66446008)(66946007)(6512007)(6506007)(2616005)(64756008)(38100700002)(8676002)(76116006)(66556008)(122000001)(66476007)(2906002)(186003)(83380400001)(316002)(110136005)(6486002)(38070700005)(71200400001)(508600001)(86362001)(36756003)(8936002)(5660300002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?ht/IdzkRE624N6Dmf+l8j/v?= =?iso-8859-1?q?IOl2A3RKIXKuDeHc1c10snw/zYN/i8IhtXbmw5isU/qhgeauvcIphwoHVtKT?= =?iso-8859-1?q?F+de+rvUQC/RahYmg//TTsxA2Vee46lLI+QpJorBmvjIFwHV/NC08jb/vVp1?= =?iso-8859-1?q?SjULOZv9kF0dFPxmL/0X9tu5xggEZFixj+ZC++xYt07QjfgLuuyoPUb39nm6?= =?iso-8859-1?q?qYEAOKqb/SACPnn4egpDCe01NZmpeBthza+ltP2o5W3gW+IZiJHBJRq2BpZy?= =?iso-8859-1?q?j3xEd+x/4Y/l130I8Psk79uBPMZAWkZ1R4aZDizxfE0v6V5qkQyUq1K2APsw?= =?iso-8859-1?q?f0DwQwdcFwJ3v4+gIR/eViF7/n8m+BuLzMbViDR1/lcILSAlNhhgnX++Tofz?= =?iso-8859-1?q?52C1KHqPsO/NGWPfelhcpZENfVGvAB8EY62V9kWob7vrAKn/gO5CSXfLTc5E?= =?iso-8859-1?q?gbZaVJrjHh9yuSArVdd0jl0ZroeX6bebaL+q4O8+qlqAX/r8OIsiOSroDcoV?= =?iso-8859-1?q?l57nQl6j7XC80R4b93+ojxym/skzQyQTfOssHT871nm/QAKdtsufdtEQVSGA?= =?iso-8859-1?q?L7DbJ5n9RnSIzDy3Mp5opq2QzNAAvdtjFnubm8aDrw4sje1bv0WfrTYgo2QJ?= =?iso-8859-1?q?nMc+T6OziIRAwYEgDVX4OUaXDCFOAAz7d7Adh/YD1RgDcGrPAlMeKC4mUH5i?= =?iso-8859-1?q?3UE6X59tK4ZH0aP5TBxGQlC+H3lkybyAz5e6mrXEYvu8QpB7hA7WrjwpwS8K?= =?iso-8859-1?q?2M7GJ6hi6XMc5x/meXceiUfq8qWR7FKPy5zF6crCPjyFxLFnZjZPmR2zVTzI?= =?iso-8859-1?q?wevwxKSxnATxgZ9+EbMOPZOk7eGvZMtX9rIL3eNpnl5zsJ+qXmkqm9LW33rj?= =?iso-8859-1?q?QLYk0FfPfB1AHfYnSks6HREJEUOzKNVUzlId2xm5lQk2TM8gg/4BOoTQnZf0?= =?iso-8859-1?q?Z/OIimPdBySq1Zt2jT/A1s2rIshAXY4FMaWvHskpSyGoE0HMBhk17r9nKaCO?= =?iso-8859-1?q?sZQxG8WeXbU1vrR4oG/UZ9j7s/s62YMWdO7ZAPsRIOMQzKjmhlD8dcOH+EVj?= =?iso-8859-1?q?Q2Wts9u0GVVde/DloCosvKGRousqo3Ou04GvscyZ/IBz5/yzdyjDVJ3CzSOb?= =?iso-8859-1?q?0L1/J3Ri/55L9wKtmNzsRLgfF7BXStRo4/zrCMJiZAmWgz+1m/5cWmZJ+iZ6?= =?iso-8859-1?q?f5jt375+DBonMRfevV/IYj3VYqATUY+A9mmUj+ToCOU16UmZyrMqmemMaNEd?= =?iso-8859-1?q?smGxLBXQYkYiBNBYf33wxWEYIapu2N/73HhjSt/zmnskzuramyfHlwf5TZs4?= =?iso-8859-1?q?4YaeafRbpAwGDspGb3WubikM85sdPSQQ4Rpo/9jNggq/rUW/hXsyLO2fissd?= =?iso-8859-1?q?L3Z89gqcICz88qNq3js8ZN1PtDSpq+Hue7zY16hrYex2T4N7rPEYMsP80A2i?= =?iso-8859-1?q?6oh5JsxWPJD8O4Dp+ZHL0bvW+erF094ld0eWZQaePaESequYG940dHNM6HGr?= =?iso-8859-1?q?OdaLSLskfHX8jD/EdUimqNV4F18Ns3dTxh3dks57RIbLndVojQaE=3D?= MIME-Version: 1.0 X-OriginatorOrg: fb.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SJ0PR15MB5154.namprd15.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: dedf8aad-dc33-425b-02fb-08da06d147c9 X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Mar 2022 22:15:13.8320 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: A6Sk4hOf5/GjIudVLdkAVzlGH0k1GPS7gwbdeduygBn/KxVQAzOxpqnWaReaecAF X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR15MB2695 X-Proofpoint-GUID: lTDkx6GB6iHjYs-FkjxRgYSaKTBF_sne X-Proofpoint-ORIG-GUID: lTDkx6GB6iHjYs-FkjxRgYSaKTBF_sne X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.850,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-15_11,2022-03-15_01,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net This patch changes the selftests/bpf Makefile to also generate a subskel.h for every skel.h it would have normally generated. Separately, it also introduces a new subskeleton test which tests library objects, externs, weak symbols, kconfigs, and user maps. Signed-off-by: Delyan Kratunov --- tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 12 ++- .../selftests/bpf/prog_tests/subskeleton.c | 78 +++++++++++++++++++ .../selftests/bpf/progs/test_subskeleton.c | 28 +++++++ .../bpf/progs/test_subskeleton_lib.c | 61 +++++++++++++++ .../bpf/progs/test_subskeleton_lib2.c | 16 ++++ 6 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/subskeleton.c create mode 100644 tools/testing/selftests/bpf/progs/test_subskeleton.c create mode 100644 tools/testing/selftests/bpf/progs/test_subskeleton_lib.c create mode 100644 tools/testing/selftests/bpf/progs/test_subskeleton_lib2.c diff --git a/tools/testing/selftests/bpf/.gitignore b/tools/testing/selftests/bpf/.gitignore index a7eead8820a0..595565eb68c0 100644 --- a/tools/testing/selftests/bpf/.gitignore +++ b/tools/testing/selftests/bpf/.gitignore @@ -31,6 +31,7 @@ test_tcp_check_syncookie_user test_sysctl xdping test_cpp +*.subskel.h *.skel.h *.lskel.h /no_alu32 diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index fe12b4f5fe20..8949ace58c33 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -326,7 +326,13 @@ endef SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ - linked_vars.skel.h linked_maps.skel.h + linked_vars.skel.h linked_maps.skel.h \ + test_subskeleton.skel.h test_subskeleton_lib.skel.h + +# In the subskeleton case, we want the test_subskeleton_lib.subskel.h file +# but that's created as a side-effect of the skel.h generation. +test_subskeleton.skel.h-deps := test_subskeleton_lib2.o test_subskeleton_lib.o test_subskeleton.o +test_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.o test_subskeleton_lib.o LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c \ @@ -404,6 +410,7 @@ $(TRUNNER_BPF_SKELS): %.skel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT) $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o) $(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o) $(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$@ + $(Q)$$(BPFTOOL) gen subskeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$(@:.skel.h=.subskel.h) $(TRUNNER_BPF_LSKELS): %.lskel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT) $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@) @@ -421,6 +428,7 @@ $(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT) $(Q)diff $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o) $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@) $(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@ + $(Q)$$(BPFTOOL) gen subskeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$(@:.skel.h=.subskel.h) endif # ensure we set up tests.h header generation rule just once @@ -557,6 +565,6 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \ EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \ prog_tests/tests.h map_tests/tests.h verifier/tests.h \ feature bpftool \ - $(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h no_alu32 bpf_gcc bpf_testmod.ko) + $(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h *.subskel.h no_alu32 bpf_gcc bpf_testmod.ko) .PHONY: docs docs-clean diff --git a/tools/testing/selftests/bpf/prog_tests/subskeleton.c b/tools/testing/selftests/bpf/prog_tests/subskeleton.c new file mode 100644 index 000000000000..9c31b7004f9c --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/subskeleton.c @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) Meta Platforms, Inc. and affiliates. */ + +#include +#include "test_subskeleton.skel.h" +#include "test_subskeleton_lib.subskel.h" + +static void subskeleton_lib_setup(struct bpf_object *obj) +{ + struct test_subskeleton_lib *lib = test_subskeleton_lib__open(obj); + + if (!ASSERT_OK_PTR(lib, "open subskeleton")) + return; + + *lib->rodata.var1 = 1; + *lib->data.var2 = 2; + lib->bss.var3->var3_1 = 3; + lib->bss.var3->var3_2 = 4; + + test_subskeleton_lib__destroy(lib); +} + +static int subskeleton_lib_subresult(struct bpf_object *obj) +{ + struct test_subskeleton_lib *lib = test_subskeleton_lib__open(obj); + int result; + + if (!ASSERT_OK_PTR(lib, "open subskeleton")) + return -EINVAL; + + result = *lib->bss.libout1; + ASSERT_EQ(result, 1 + 2 + 3 + 4 + 5 + 6, "lib subresult"); + + ASSERT_OK_PTR(lib->progs.lib_perf_handler, "lib_perf_handler"); + ASSERT_STREQ(bpf_program__name(lib->progs.lib_perf_handler), + "lib_perf_handler", "program name"); + + ASSERT_OK_PTR(lib->maps.map1, "map1"); + ASSERT_STREQ(bpf_map__name(lib->maps.map1), "map1", "map name"); + + ASSERT_EQ(*lib->data.var5, 5, "__weak var5"); + ASSERT_EQ(*lib->data.var6, 6, "extern var6"); + ASSERT_TRUE(*lib->kconfig.CONFIG_BPF_SYSCALL, "CONFIG_BPF_SYSCALL"); + + test_subskeleton_lib__destroy(lib); + return result; +} + +void test_subskeleton(void) +{ + int err, result; + struct test_subskeleton *skel; + + skel = test_subskeleton__open(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + skel->rodata->rovar1 = 10; + skel->rodata->var1 = 1; + subskeleton_lib_setup(skel->obj); + + err = test_subskeleton__load(skel); + if (!ASSERT_OK(err, "skel_load")) + goto cleanup; + + err = test_subskeleton__attach(skel); + if (!ASSERT_OK(err, "skel_attach")) + goto cleanup; + + /* trigger tracepoint */ + usleep(1); + + result = subskeleton_lib_subresult(skel->obj) * 10; + ASSERT_EQ(skel->bss->out1, result, "unexpected calculation"); + +cleanup: + test_subskeleton__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_subskeleton.c b/tools/testing/selftests/bpf/progs/test_subskeleton.c new file mode 100644 index 000000000000..006417974372 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_subskeleton.c @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include + +/* volatile to force a read, compiler may assume 0 otherwise */ +const volatile int rovar1; +int out1; + +/* Override weak symbol in test_subskeleton_lib */ +int var5 = 5; + +extern volatile bool CONFIG_BPF_SYSCALL __kconfig; + +extern int lib_routine(void); + +SEC("raw_tp/sys_enter") +int handler1(const void *ctx) +{ + (void) CONFIG_BPF_SYSCALL; + + out1 = lib_routine() * rovar1; + return 0; +} + +char LICENSE[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/progs/test_subskeleton_lib.c b/tools/testing/selftests/bpf/progs/test_subskeleton_lib.c new file mode 100644 index 000000000000..ecfafe812c36 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_subskeleton_lib.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include + +/* volatile to force a read */ +const volatile int var1; +volatile int var2 = 1; +struct { + int var3_1; + __s64 var3_2; +} var3; +int libout1; + +extern volatile bool CONFIG_BPF_SYSCALL __kconfig; + +int var4[4]; + +__weak int var5 SEC(".data"); + +/* Fully contained within library extern-and-definition */ +extern int var6; + +int var7 SEC(".data.custom"); + +int (*fn_ptr)(void); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, __u32); + __type(value, __u32); + __uint(max_entries, 16); +} map1 SEC(".maps"); + +extern struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, __u32); + __type(value, __u32); + __uint(max_entries, 16); +} map2 SEC(".maps"); + +int lib_routine(void) +{ + __u32 key = 1, value = 2; + + (void) CONFIG_BPF_SYSCALL; + bpf_map_update_elem(&map2, &key, &value, BPF_ANY); + + libout1 = var1 + var2 + var3.var3_1 + var3.var3_2 + var5 + var6; + return libout1; +} + +SEC("perf_event") +int lib_perf_handler(struct pt_regs *ctx) +{ + return 0; +} + +char LICENSE[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/progs/test_subskeleton_lib2.c b/tools/testing/selftests/bpf/progs/test_subskeleton_lib2.c new file mode 100644 index 000000000000..80238486b7ce --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_subskeleton_lib2.c @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) Meta Platforms, Inc. and affiliates. */ + +#include +#include + +int var6 = 6; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, __u32); + __type(value, __u32); + __uint(max_entries, 16); +} map2 SEC(".maps"); + +char LICENSE[] SEC("license") = "GPL";