From patchwork Wed Mar 16 23:37:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12783299 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 4F805C433F5 for ; Wed, 16 Mar 2022 23:37:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241623AbiCPXim (ORCPT ); Wed, 16 Mar 2022 19:38:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237131AbiCPXim (ORCPT ); Wed, 16 Mar 2022 19:38:42 -0400 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C61F15A3B for ; Wed, 16 Mar 2022 16:37:27 -0700 (PDT) Received: from pps.filterd (m0109333.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 22GHCfHr029189 for ; Wed, 16 Mar 2022 16:37:27 -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=Pqu/MEAH1ixxtIzLUti8c4mcK/i+RDDkPIVgurjQb9E=; b=m53HU7tuGI8kkUG0qwJwu+x8awbCvonKg/I7c3LlJ1sA/cHQ/DIc/aiYlb2qAGRjwiwv Hu2k7uEC9zXymhQB+0ofh6q6k5EAJjX/02y3p5FsLPzTF9wCKWg2gyD1cOCWtKQm2At2 FTHZeJfQ0horRRy0PRWKQz7310+QSEX7xt8= Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2106.outbound.protection.outlook.com [104.47.70.106]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3et9d0m35c-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 16 Mar 2022 16:37:27 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cWpgO4VepgUCV4XGryYomHtD3+PaqulDilsSascli0iYvPPFquGHmASYwGt624XZqOB58QVBS8QGPaF/kNFF9P72C2bqVyXilpWFcpruR14F8DufD4OWrLgB9qZ4N/3mmQvi0c+vbsJct2bPBuC7nPPDlc9cDBu5gOdpXQLNnzRiAarXQtSwalagBgteftOsKf71vkQXHLOius3CVPlMIy6eJQQxdNATiHArfKpD0cRJVHJBVmY/UL8iZyqQXtt+nj9NwSRH/R+aQJlDJ0E8sKgeNHqI7Tvj4sdJkihuHB5EA1hJJANOPasCc2gkZnjPbJBXosE+rYIKaHWEqpdhwg== 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=Pqu/MEAH1ixxtIzLUti8c4mcK/i+RDDkPIVgurjQb9E=; b=hpRrJFSR1cWLF+81fz+sONZ8zV7x/0Aq1bAk6uYhCds0jkLmCPLqvi/3syjxWps1NQYEbNk9GWOGDEfQe4lJKjzFV+kLjWsKxgOiGezbbjrTdrR0jO36JOPMpnKmPTehdJWQJa7bN6oZSgi3k1wESM9zmfD1reOP72tIB7wNYuhE7Vk5KAWIhVQLzfS7w9h4hFl1SgxzfJGLTguaNHCh+7zhxa/vA0xz903bqI0zL0vKK63+kJswdGhsjUnLPhn9CpnUigX6mCoq6FEYIiiADfMMd+CsMNK7K73TfLYJztKPDuYDeLu9mI3Vx8WQ2ZXE6XL3NDCdUbtAFzLkuptE4A== 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 DM5PR1501MB2182.namprd15.prod.outlook.com (2603:10b6:4:a4::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5081.15; Wed, 16 Mar 2022 23:37:24 +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; Wed, 16 Mar 2022 23:37:24 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v4 1/5] libbpf: .text routines are subprograms in strict mode Thread-Topic: [PATCH bpf-next v4 1/5] libbpf: .text routines are subprograms in strict mode Thread-Index: AQHYOY7JGutuEwujg0eGCtq27xBI8Q== Date: Wed, 16 Mar 2022 23:37:24 +0000 Message-ID: <018de8d0d67c04bf436055270d35d394ba393505.1647473511.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: b842a929-f05c-4924-8085-08da07a5ec7f x-ms-traffictypediagnostic: DM5PR1501MB2182: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: VgRF73t9BKNaxtxJ24K+WRoPlNYZoBdK1Q5uGhaWQQDxHQQiCV+JNCJj8IXq4vgov11MWarLDJlPHz83nlXjK8JCBCS2LfWesder5XKHRlcEvNLUab1AVXl+F3rsr3bQycRTMzP5uH8/f9cvmduZQlIUighbIqifICoCTCUkSjiQ5LU/wpr1cfvRDZLyXji0lu7TRmo1HG6HNUqLo7/tsvgQ/TUuHV/oTAljwJbfjkma29KYQ0S3FXeDQ02wJIbkpkQOeD41EKqt5Id0yYSDIsV0hqz3wB/X7IlZ6K5IgBOaN1VPN7tidw71HI29nLTnKjqpA9zbra0ahMBPiRlgp7FcalqaFiiaxdKOZSg8rairBHi4NShgYkum/EZpTyEvJal2IdH6QDXqSnkP4ztAw1bH9wtii12HbCMwV/EKVplsGSM1qrWfWut3zLf/HvnD/G79WPxzyuRwBmo7HJGA9T/M4eBUmX941+ZpUlqxekOj+8evJ5d+tHcjm9sTkNG6bNesChJ9ZxvqZuhlwNFZSYdCumhIOdttDaoZv8WHTOnjGxsXOc+9bGRJksnEdqKSE0eDtjL6dy/vcba/NXJ1oiRbbylPFJgdrnpBEzTXBWxDaDqZUXJ6QYFS2MqmA8XAfvrAfUSa6C+8VIwOSJNLJ1tfa0Nzs8Vo9sh45P8gZxluGxBxUxw3U82rvX5OQ4omaRlJBc0YrsxpBw3YfQoZVA== 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)(8936002)(2616005)(5660300002)(110136005)(316002)(38070700005)(6486002)(71200400001)(508600001)(64756008)(8676002)(6512007)(122000001)(6506007)(66476007)(66446008)(86362001)(76116006)(66946007)(66556008)(91956017)(38100700002)(36756003)(186003)(2906002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?JfLgIeTuP6J0BoF4KVlnXp6?= =?iso-8859-1?q?1CzAwLU6OfMkZ0GSlcOIREjWroGOHwXClVzL7LPW33OOonM+1FBXfjqLpwxP?= =?iso-8859-1?q?echUQ43Vx4LOcHWAC3zTt/pWeECY6XuF/unFaprrvpMcPwqkVh7GDL0tOYhr?= =?iso-8859-1?q?yqjs9vQrC2LZ4LOXOEZ286uoqQrpuBXW0/XXJzu0lgP/wijGrhMqWr+tqm29?= =?iso-8859-1?q?c7BJn0iawkbeBqOMUms/WKgeDjFv9wJ9NcUyRGhebhKuxxrly2vjrgGRLbzO?= =?iso-8859-1?q?ENl69CAC4JnLHyYyNFMnJtPOlDk9UbWFW49HLMILjJ0o1ZWYy10cg5FrAgt4?= =?iso-8859-1?q?A3Zc49W1AUABdxclQ9itYBDep7UTxcdkNx3kNWa02y62CqBOeLEd3J+tjFzJ?= =?iso-8859-1?q?l6iu9yeKj4RRNGQWqOoQlfXYgflna0dfeRV/TA3WIhRlT5EIT8GM+ahqU/Ex?= =?iso-8859-1?q?V7sxr7XBlyk8tcpQtBJw1ZE0SRRER4oUQZKLPnYcHO55QAg8yGswWHawBoW7?= =?iso-8859-1?q?b0r5WLS4mHgTndHj/WpHiVYStU8yIiDzOB+JBOT0Qxk695nKttj6qrEpxpc4?= =?iso-8859-1?q?mSDoRTsxB/dc2+nGGo26XXTvBAuZ0iOZsR4WGrp4WahXZhQKbeUE3W38qj8/?= =?iso-8859-1?q?Iw9telNBdC+Y/H5IuO1cLeldoKeSqZrpVdYnQ/PxF9LKCUD4+kSmPnEwzCS2?= =?iso-8859-1?q?RDD1pYqG9RJJUNEfCKSXSD7a6PY2To1AJFG9Ri+8QfFpMpFAg+pLjYmT0Ctp?= =?iso-8859-1?q?yZJZ8Q+43jjjuKaEhzRBCaeihHBCO5cYwcZtLdGTW/Xm8qtUy656k+v4qdL0?= =?iso-8859-1?q?rN8cm/rMkaRwoxOprBmgBHn9aZXkTG7zYVuW975qqrooAJgdSYsKZ237ZQNk?= =?iso-8859-1?q?iWHNUq7BCwFk4g+4xct4lq70isweisSMoFBUpoc7cD89iO5xRadRYXvdIaQc?= =?iso-8859-1?q?y6gGSqU5qe9J9Hoq7i2URFp3ArfynMZIjTDR+Aj9AwXUvjmOlP0JZJ1kU+SF?= =?iso-8859-1?q?7aJmI0ep+gX1yN8DMUewRDPSKA5Mf6I+394VTKODXXNOSRT0Ww/hbP/IpBqr?= =?iso-8859-1?q?0iYY9vB5xrgtaiXv5K+6ChyrFuk4RnnE0z7duR/YCW/pByrE7RZoDoGmlf2x?= =?iso-8859-1?q?L0koZkS69/ULvZ5dUEdiKz27+i/hDCYNdloqT2NbMI2SupdbIy/E7rpCi3yp?= =?iso-8859-1?q?1NB5nN4Oc2/ukUiAO8l/SiQe8spTfv7IXZtZnSCbjzizC1xzM8ihtJHrFCAC?= =?iso-8859-1?q?XvUCUn1zRtJTpB/9Du+0ecc039RwlIdEKRew7TRJIS7vglOAodS7FR1bxhQ5?= =?iso-8859-1?q?2tujMoE00leQfZ16WrJXfeg6XogIUkrf9DR2KbysIpwCM7nIkQ/7L4m6a5LQ?= =?iso-8859-1?q?E9AXP7pe2sRg4UA34NlRa40JW+imVa9xx4FJiTbASfDUxiK9pef7b6wOjr3b?= =?iso-8859-1?q?mK1IcUuXDTqXU3pNo5ZWPjFweWcIRM5HA2XtUh0HnQ820I6k+d1gw1LC0Cyn?= =?iso-8859-1?q?xchBINzUmT5lrw+pXjvE229fiUL1aQw=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: b842a929-f05c-4924-8085-08da07a5ec7f X-MS-Exchange-CrossTenant-originalarrivaltime: 16 Mar 2022 23:37:24.4185 (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: WO2BxUmG5LL3DI6hGDW3iU/cg5+ZOBmNxEvs7nb+3/YnobNVyXKoWawx5vii3weN X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1501MB2182 X-Proofpoint-ORIG-GUID: Q5_5jMSK-Gwr595lIBX_wB9UtptoKEsY X-Proofpoint-GUID: Q5_5jMSK-Gwr595lIBX_wB9UtptoKEsY 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-16_09,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, 11 insertions(+) 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..d7bcbd01f66f 100644 --- a/tools/lib/bpf/libbpf_legacy.h +++ b/tools/lib/bpf/libbpf_legacy.h @@ -54,6 +54,10 @@ enum libbpf_strict_mode { * * Note, in this mode the program pin path will be based on the * 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 Wed Mar 16 23:37:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12783301 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 ED6E3C433EF for ; Wed, 16 Mar 2022 23:37:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244861AbiCPXiu (ORCPT ); Wed, 16 Mar 2022 19:38:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242323AbiCPXit (ORCPT ); Wed, 16 Mar 2022 19:38:49 -0400 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B204167DD for ; Wed, 16 Mar 2022 16:37:33 -0700 (PDT) Received: from pps.filterd (m0109334.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 22GHCdMN028583 for ; Wed, 16 Mar 2022 16:37:33 -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=6HESL43UVRRfdV82QuAKeFQCqniHjuebegPgDSqkqOI=; b=O5fREvpAB3kMoMQz1jEE+7qUM1EoIUPadvwnU55rbIuqyMF4oP6m8M0e95nAoptjCEjP w0zDeOIdF74YLScGWN/l5q3QKsKupesBPoX432iUytdKv03Tw0c1OPtd2bbizG17Nr28 PndH3htbtRnKnzEytk4z8vG5PpsgWPrSjSw= Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2100.outbound.protection.outlook.com [104.47.70.100]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3eucf464d8-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 16 Mar 2022 16:37:32 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nADK4fU6ciL1TRfoF+XIHtXAXa68wSang8lfH4at82f/bCaKfwlg7JBePZVxZEyr0gMk1fdMtzCGBEpuFeaXVSUq+iPma3/HsVfySDKyRUg3lvmAUqKexw+t4Z0A9vbJWCPDXDs8tG/YdwnSqIaylUjBTrozJSN09+Q073WpdHQlzoccJwclJjItvDWJTEeZyIxFYeiVGrzXSQB+hu3Ysr4f3ea0V3usQKNazmI7y2XvV1cSPe3z+CXV3HgnrUIOLUpqsWhd56IxpxYDMP08akZObkCz8JDhqCyc0gihY7d0/eN98emp6A0XUvQ9WVVinTud8p+8CVMoJnXVqjZ42g== 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=6HESL43UVRRfdV82QuAKeFQCqniHjuebegPgDSqkqOI=; b=mTiaiGYlirDHNdPgBnCP/3hZbEDnwQ6tGhR/8ZAfYayZe9GH5wsqIFW0FY66ho8iSlca5RZ0gZXmWnwYLdq+nmUexxgWJ8eFf8LFrdB6AwN9sBoZlzxikPFys/g4mN43HErMvfvpZIL0xoqRy3ZXYn5GW6suVtmfuSebqaodLisjwx/sYxhQ93xiuGCm4SqZU/7oQueBZ/VIu/fy6IQkMAbZastx/KxH9HMy5vTfH3GP0APMnmchsdqu8CwnASRooJR3cH8eXDsNuEywWjQByW7zgAqu1wyptHTw5vBXl83jjabejJ38eciumuJ9QjQRp9hMwHO5nF9UR/Yzb5QYew== 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 DM5PR1501MB2182.namprd15.prod.outlook.com (2603:10b6:4:a4::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5081.15; Wed, 16 Mar 2022 23:37:30 +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; Wed, 16 Mar 2022 23:37:30 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v4 2/5] libbpf: init btf_{key,value}_type_id on internal map open Thread-Topic: [PATCH bpf-next v4 2/5] libbpf: init btf_{key,value}_type_id on internal map open Thread-Index: AQHYOY7NRLAiqF+13k+lQwuPcMDUPA== Date: Wed, 16 Mar 2022 23:37:30 +0000 Message-ID: <78dbe4e457b4a05e098fc6c8f50014b680c86e4e.1647473511.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: 98ffe85c-d023-4f80-4542-08da07a5efed x-ms-traffictypediagnostic: DM5PR1501MB2182: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: mufS2b2W5pXuhq4bM3qYub6NpGFYGVtQLcIDV3dGOw0bA/KcZx4eU+T8vGyde5QFGk5BCJymMK8v+Yf741lowo+1Q8RcSdJaSJV8EI7nSaJ5Ut9YC+5IsqwXTcb3rJjnvIoMqhbMqGkMxIejlaESzDTWlWWVNE7EwSEKNA4JtvJcNf9BaIqupuaXHfpTAZnOnZ5JfOg+3k64DPEhUX11BsPU9JI3yiivZsexjrpI6QIax0sJ6x97nB5K8aM2uHDqblu06+aIlCSdInG1dKdUxTHPtCERomFia6xfYyRVTB6a4txdoTLos7yem2L/y+CNoSaEG39w28nwWqhGXTbzXYeUb3Vb1X7bWRVWk+NA/O6ttBWNP8RcKHMb4Z+FQuSi2MHHHrWRaO1ddq9v6ka5ioWcgsT0N/W+MaXjFdJ5VdTxjmc+fsFXU6sm4+dY75usHzbGBepOO8w+YbBnhqo2hYM4MSqrT7qbKiHVOXf7S5lxBkbiiGkCa4rt6fX+xedq3EQlRY+nGCYVLdBrjPnmVGW534hQT4I9o0xv7Mv67d6Ws0U06B/5eiGQiKADUEG1xCyTOfjOLDfuwXCPPmpeXZUbtIVGuajb7GKY1+YYc6HVRnXqH5ZkFv3jtvFdNq/p3n/ltzVulyOPSQegojzqzu2qPfxRAweM4uhBfcBKSAk1v+QSayqB/JxlZ1elmHtkEImkMHFHNwcewA52UBwxWw== 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)(8936002)(2616005)(5660300002)(110136005)(316002)(38070700005)(6486002)(71200400001)(508600001)(64756008)(8676002)(6512007)(122000001)(6506007)(66476007)(66446008)(86362001)(76116006)(66946007)(66556008)(91956017)(38100700002)(36756003)(186003)(83380400001)(2906002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?9Qb1HFwpqf2xwhrKdHSZnTo?= =?iso-8859-1?q?GDvDeIcAZ7bodQEKAZEIGqpOJtUY42q6Pqzjg3Z/8HGxOsd1+AMSzHtCZ8Uu?= =?iso-8859-1?q?/3CJhHcEhSPkVKZkWAxk4kuKOMqpqzSOR6zl59uFRJJgs75SAsHC6zufAqDV?= =?iso-8859-1?q?vyHCv2rU5A/l+1y2jDHABOqYNCPT3i8QEGJSYmT6wtLC1jZipEkeD++J7Rm5?= =?iso-8859-1?q?O8ivjHf908OjcgEGKJdHM3Mb1EUHDLMMmMxpgnX1Vw5lKPI1DbVP3ZJ27pew?= =?iso-8859-1?q?M1QT/wXMGQ56YBRT0u2uLYlldeJsS2IsVxdhIyCSYSAOAVIBAGF1riELunbr?= =?iso-8859-1?q?n0CuDqiknrwJNb7mcufQqEBI/VbP8Rz+OsVHqaO6HY4iYIZ3RqWemzUl2DdC?= =?iso-8859-1?q?kl2wa/pjlncvPy+R4O9mDM+xEDwCa1OOjh3ogDuTCaeAiEEx3n+VuKDl0WS6?= =?iso-8859-1?q?BsTx8H3rmB9qfH1LUSX7812r1EHjPMqlV9nwlFXUQ52pw7sWQrcXEZIIIvND?= =?iso-8859-1?q?n0kcVMTZWrUUvLddO8zPpJaHQkmAUPxhshWJlJwA38286i2XM/ULwwR2VanO?= =?iso-8859-1?q?ALBEeXFZIlh4sewp0UpExdO/Ge39HHD7DX8eMLiw8fjwbY5B4bR6P3xkxsyl?= =?iso-8859-1?q?ZgwEK/y1LaqfZXQJOc6JDuTzwTFvrJrvHHvJT/gSRqPZLtXh9yOTXro6GDIJ?= =?iso-8859-1?q?g1n4gu0QzmHXwDrHzxBUtjnmQXBt9A0kS9NzuACkGu+pwZzYOO36KsDbM/m+?= =?iso-8859-1?q?DEyZjRvruA+um22XDV4yq2umL/mn7rx3fnuwAbqAIhUswwAUvG1k4K+LclO+?= =?iso-8859-1?q?EcN579SrsLtF2XJk4X7VO7Lwx2YAo/Ds4EwTccaVTh+l99nBIjN/oGHIbOau?= =?iso-8859-1?q?OBCDljncHPO9Ocbe4G4LpByajxAXms+SmPOTG6qpxa7B1djgMsHc3XHLla9U?= =?iso-8859-1?q?geiTFDXj6SHUWn9btiP1ocE523aRA8jRGIR2fecy4aAnRFYRSYy15Hz4L4UZ?= =?iso-8859-1?q?VO46DjYpk8Y5k1mdRUBihop5xj8z2ASwYqlbmLS3tZFVy6mda25/MwdMy8ry?= =?iso-8859-1?q?Rv2+4t//2m5BbYvaS4ErNrqbwvjrtzffHJ92ZLuP7i3Qx70udylu4twWVhmF?= =?iso-8859-1?q?e/ixCLBrEURRxX5OKSuCrwb3WkTGpos7BWQ/8DSARh/ouboi2pqd0C967DPy?= =?iso-8859-1?q?AFl4y0Sol1ORunvBoKb34d2hbXwmOIZntgg1MOYoXv1tqJH6c2lX3EkDrNtk?= =?iso-8859-1?q?O6zJ0dKYDsXqnk4Dsc4d6Z6OQ3ycXoG7fBk7i0hdBOuVnwp7LzJgKwMTxDn0?= =?iso-8859-1?q?SxeN0Zq9UYz6IsncDPrinQZQaj8Bs54ok0odIoajprFLKxjFsPp9k6rMf91a?= =?iso-8859-1?q?usPlFjkmhAaVHHuJSbmNezgNctEiICbCLlKCH8iNEzdzkXFxcwUPgGyLs/Kw?= =?iso-8859-1?q?5HdB/Jzc5uQXW37ijUvwYGssZb9Ofb34tn7bjiHZL5y9jiFB7IEb1d0Cp4KP?= =?iso-8859-1?q?QK/YBn2xVNRG8QxiRf7t47PHCC8KFGTQpl8chOZH/F2APU9/FmTA=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: 98ffe85c-d023-4f80-4542-08da07a5efed X-MS-Exchange-CrossTenant-originalarrivaltime: 16 Mar 2022 23:37:30.1555 (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: eR4uU89xZW2QjR6JETnk4pt3OMb4tHNG70nbr6c8TPRcNi73FAMB8a+P+oF9iXfV X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1501MB2182 X-Proofpoint-ORIG-GUID: OzJOpmI5bt9gfp4Cg5KCVBy1vSXl62HL X-Proofpoint-GUID: OzJOpmI5bt9gfp4Cg5KCVBy1vSXl62HL 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-16_09,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 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 | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index aa26163e4ca1..5ff680750dcf 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); @@ -2046,6 +2052,9 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict) } memcpy(&map->def, def, sizeof(struct bpf_map_def)); } + + /* btf info may not exist but fill it in if it does exist */ + (void) bpf_map_find_btf_info(obj, map); } return 0; } @@ -2534,6 +2543,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 +4886,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 Wed Mar 16 23:37:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12783304 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 4C7ACC433FE for ; Wed, 16 Mar 2022 23:37:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245373AbiCPXix (ORCPT ); Wed, 16 Mar 2022 19:38:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242323AbiCPXiv (ORCPT ); Wed, 16 Mar 2022 19:38:51 -0400 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89D3815A3B for ; Wed, 16 Mar 2022 16:37:36 -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 22GHCing016635 for ; Wed, 16 Mar 2022 16:37:35 -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=xzoVU7G7IGg02eXOnMEZ9PBVMXI9tA0oRtdHkXpgMfc=; b=rMvUqlqyEnUXJ0sQZ/GlLRe+CB4QJ1lUMjVa4ZQV4rEV63KVEgcX5P9Fcj/Ivr5zA/zI gjiOZa3x+1NrLYLUCOPithxJ4bck+vssEI9p2kLuKf700208/Via227VBI7zPtvYqg3E /cSsKRe2gSdHZxJCMSmIlz9SggnTIjkac7k= Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2103.outbound.protection.outlook.com [104.47.70.103]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3eue4bnkqc-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 16 Mar 2022 16:37:35 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XYbC7e41bjZs9wdp2rR78Zpn2ObO5CJJPGvaI19/0o/yFtlHx9qCjb1yAWfVRnpudGOpDon6p/o/1NBld2YgSk1m2ZqxJQxkr6t/7hxQnopR611bZJ0i/U5unS53OrEqflE8bo2nlRzZNmQ4xavzFA4TAw2YJD8QyWOah6zExiatj8wwEjePw+5TJtfqclwOqIH/HDVbEDi63idUFGkmVwLR1e0KOK7WCyziUWdjjhKuhj23nUuufe/uYXQXXLeiORNgYqFIgukNXNUeFDJ4yOievOgnz7qASXLkgedB6pag6sUsEZjrcPctk3FdWiM2uLmCvkFwgL6e/ajbsbyjjw== 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=xzoVU7G7IGg02eXOnMEZ9PBVMXI9tA0oRtdHkXpgMfc=; b=beP3leLUB+EcoU0jY5eiBhztI1ZRh3qM+ITqgNNki3c0UwCOVDYMKUaqvjfQymkL5/kxfw6BlB2T8XjRiPIAWcbtEv2huIMEeMAtTfHSb678KUf0uNW13OC479QkzGvv8CfdHE75Z+BqnJLTX1i/6ehPd70sz89WV2FsHF6CnoMQsVF5QxYKQedWAaMBouXr4P/Wddw/loWjwSY80weOO+GIEBQQRXmsYD6Dlgo5HQ8zd1YVMM62hH2ChDtNQJfNx3e32/G10H4w3zgKvLNXoUWc9D91JPfhIzRuymlNAqMf5HWFqW1J7cgwHqi+PtEiD7OvKHaKlCsL5FEHoNBasw== 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 DM5PR1501MB2182.namprd15.prod.outlook.com (2603:10b6:4:a4::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5081.15; Wed, 16 Mar 2022 23:37:33 +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; Wed, 16 Mar 2022 23:37:33 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v4 3/5] libbpf: add subskeleton scaffolding Thread-Topic: [PATCH bpf-next v4 3/5] libbpf: add subskeleton scaffolding Thread-Index: AQHYOY7Pp/IiWQAJVky8ckDrRBsvpA== Date: Wed, 16 Mar 2022 23:37:33 +0000 Message-ID: <6942a46fbe20e7ebf970affcca307ba616985b15.1647473511.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: 0f5c78b1-3d9d-40b9-bc56-08da07a5f1da x-ms-traffictypediagnostic: DM5PR1501MB2182: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: LrH9EE9omNjxSqNicU5lYtDW6FK+Ka/m4/OScxeqy/kjNbPYvbEcmKTgEX7xGlqU7Jf1PU3btLtK+aMgQsG9rrtiAOd8wgGmXnfc7b+19ZCdp2AKs6yUCG6o7Yka3EhQMP3tUQk+55e5DLwqepWUbStUPMxBYbwkFvh84fNDmdMF0vgtNWIT/SQ9VbHQ0ymmQ/AJD/Jau4MSXlgXH7zeMMf1eT3bYvy0daJzsLFo0C2d9KZdsrHaPe9WJhYcalC/d+hPuGtBSoHsby84SwY8UHJCtYtF5vMQWsf5jPmZlCDNJfXLnX2jJt9SGZdB528QkBqHPMQDcyoR7yoqpZ8fyjW+x6AlTS45QObHMeyaWVv7ky/cKbUpSlina5StO/C4RNmmCH28/q+8+R+3g3Fdw0K3XXIlAODH+HQxJBh+aNLgFBqqUM8y16u230818KkUGstIgTzdC1br8DLy99XK9w6QSxFrCCUg1v5YQlQphSZAEG7C7DAM8xtfqrW37s52ZDDXUzKlCZyDSE0z7/bcuxd4QaH5RXuCoxd1ZqQxFyh1u7YunWbO7bKUzyYJAyHD0W3NflWA/2w1rHTKVLCX9HBjT4wSBn9CgC+N32/hnMnvAUiGH85stg9khC+gAifrlC/7jXeZSKgUVUdD06HasB4pGQ5vemXeOna/mExVT7nU/TmegQaHJQXkLpmf8e37U3GKyv4PxYASzwKdm+gwdg== 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)(8936002)(2616005)(5660300002)(110136005)(316002)(38070700005)(6486002)(71200400001)(508600001)(64756008)(8676002)(6512007)(122000001)(6506007)(66476007)(66446008)(86362001)(76116006)(66946007)(66556008)(91956017)(38100700002)(36756003)(186003)(83380400001)(2906002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?Isn+GcvCwYbGG4EzEJawoIj?= =?iso-8859-1?q?IOosMezYAgzeVeIySeWFiDKHPEG6HGCIpzlNu5Ow3e8j+rSQkys69jzTonWi?= =?iso-8859-1?q?b+MDEepo0PxrW0ouXQSMV6jkvPQ2bMPhxbReQycwbjjCGPD59QUNCMI0/1JK?= =?iso-8859-1?q?KZWCfzLMRwMpsRCb6oxK8H/kZKlJrEcFvseLU99f3iE/oq8mbLbgdJeFVKpo?= =?iso-8859-1?q?TACsjC1Th5acBRDcWL3GsSjUR8W2EooqBwk2P1R1DkIJquAWwooUWI8MNajc?= =?iso-8859-1?q?FV8OVyOm7vMYVOIOgjMkQBL8/TKllpNYONj3dSddwPfYl4VEIalF4GTVn7Qv?= =?iso-8859-1?q?CAnQvxktQhIICzqAW2MFCtkm5YdSSS4E6EZ25+L+wNbI49oK96zErZepvJEr?= =?iso-8859-1?q?UC1ruI7l7s5bf2fTWqXGu+RMe+XQG/pzZxtqQIcIGCmHEWlF3CnM3hnV4A9f?= =?iso-8859-1?q?zf23jLa0Vm8kMVy90guO8BPvvZoASIdWqn/h5jnoBA3r5wnA2ovQjN1FcIWE?= =?iso-8859-1?q?7Qac4DMKQKOwIdcVj+IOKmxQmUrKGi1nzqEhz1QOqInOD0l07fNnegSlYe1x?= =?iso-8859-1?q?t9JASLgtoyRciqxXd0TxUHmBvO0Kqb/btswRtL65P8VtRr1o3yczzq62dbpp?= =?iso-8859-1?q?jwWQgaxoQQpL40viCpvO0jwE+spzvobJyB4q4jquqcjJP6Npu3MbSOk+ljr/?= =?iso-8859-1?q?bpQfyn6X4sAT4VkOvPVSwC7wHCQPpy3LnAYXE0pr2EIzxEJsjTNSrvnXXDPQ?= =?iso-8859-1?q?JhFkvg+8i0Q/ugsrRqMSiaEOsCtgFln72J6RXZeO9QC3Rd1pYi3csw9xZvBt?= =?iso-8859-1?q?MqUGqQRXjDk04iGDF96DD2csPCpjrGV0bodrsc/FUNwO7aXR7YdsQQB9Yu4f?= =?iso-8859-1?q?mFxT0F0ACo5t4GtI/qqemNjoIFogsGG34aRtQ9B4m+XtU/c6DbQmwexNFAnG?= =?iso-8859-1?q?qPLrw98R+h1NwxsWUBd+V8A89gJgcvUVRBBvHXP2vE5pj3SdlC76CEVM0xwd?= =?iso-8859-1?q?K4N6u7Yo2jW00OSpmZI9GSCLasnqmPhEVW4AUCkzh3qjT2Mb+ZHh1VgwcH4Z?= =?iso-8859-1?q?U5b+BVue5IUtmQVGuw1t0O1WHyIB52tiIwD4LnmWcJ05INpfcwNwoELU2S29?= =?iso-8859-1?q?+10N8bUlygTXA/MdzArJ5Ol6OZHzyihLeO2hPHK+j5aGPTwO/V37g7y5J4QC?= =?iso-8859-1?q?cJqy7SsNwt2iFru3QjiYQeKG4V7ZkAkph33VwFUOYN7Vn/Gqm/SESndc6k0N?= =?iso-8859-1?q?r06tfyT2gfH2V4i4MQzd/5koVshh6j8wC0uT2OqXZnv9IqpjzX3NYECFNpJ4?= =?iso-8859-1?q?oW6210YjPsJgv8onJfYjFjRptNbx2kNjSEYoMMf6AD1zPOW2T9e840hJ5Qkk?= =?iso-8859-1?q?67Y8XrAIW09vnLdcG5dzeca1lotM9VELqRDZcrC7SQkcKfKy6jGjCxJAl3o8?= =?iso-8859-1?q?aq0AncETFOw0/aXdurbj2sYRbhcQhhiSaJ/cflvOYh2Zb5ZVwBWEpHDvCY7y?= =?iso-8859-1?q?tYopvJFyelplQBDJQ3mo4JS54liDnZZiHLI68SDNK0r27d36KdOE=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: 0f5c78b1-3d9d-40b9-bc56-08da07a5f1da X-MS-Exchange-CrossTenant-originalarrivaltime: 16 Mar 2022 23:37:33.4207 (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: ultrXc2bo9e3meKIXC+b81YlaYqWt7R1k0B7VTSsFYfR1k1gKRdc5UecyvdsFiv1 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1501MB2182 X-Proofpoint-GUID: lBoiVCMGoKZh_YDDm_CTwNCTqpHhoUTs X-Proofpoint-ORIG-GUID: lBoiVCMGoKZh_YDDm_CTwNCTqpHhoUTs 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-16_09,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 | 139 +++++++++++++++++++++++++++++++++------ tools/lib/bpf/libbpf.h | 29 ++++++++ tools/lib/bpf/libbpf.map | 2 + 3 files changed, 149 insertions(+), 21 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 5ff680750dcf..d678b0d1d8dd 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -11815,6 +11815,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) { @@ -11822,7 +11865,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, @@ -11845,37 +11888,91 @@ 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) { + pr_warn("subskeletons require BTF at runtime (object %s)\n", + bpf_object__name(s->obj)); + return libbpf_err(-errno); } - 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_maps(s->obj, s->maps, s->map_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); - } + 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); } + 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 Wed Mar 16 23:37:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12783302 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 2DA93C433EF for ; Wed, 16 Mar 2022 23:37:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242047AbiCPXiu (ORCPT ); Wed, 16 Mar 2022 19:38:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237131AbiCPXit (ORCPT ); Wed, 16 Mar 2022 19:38:49 -0400 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F72F15A3B for ; Wed, 16 Mar 2022 16:37:32 -0700 (PDT) Received: from pps.filterd (m0109334.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 22GHCdML028583 for ; Wed, 16 Mar 2022 16:37:32 -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=5fO4svkCr3PwRb8jSJ+pvIL+NbYbOh266RPYEgeqZNA=; b=eU0l/Cm1Hgm+HXFaYtr5VdK8yn8JCxPG4Ci2i5xHv4/G9Y3amKNGcsqPgCa5RtohrbdI D7xh4jU8lcDveEdUa83pAvA9KFiZbXjzqb+tC99k8t4+wMlzAcQTMkOrNWG3FCHRfVaj h+dgO/7RUbqfgpRDWP1j5jXWL6ygd66x+gw= Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2100.outbound.protection.outlook.com [104.47.70.100]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3eucf464d8-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 16 Mar 2022 16:37:31 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cD5azFLb5G0T/Y+yf3eD1fPbbm/hnPndUhXrrwakS6RSrpgdqglN5RrAC0DIjGvv1/j0flIz1Fsg/3XaQ6xnRVZ1tV/yOW56q/6mxatUh6WXUHuHzZUmSdOQjUIHWd5+NeY8h+KowEeCsHNHRtSPt2U09Kzf5PJPbSWJi2uggDoXnq46fMlxC/4I7XdVctOLTnuYhOBQoxpGLpodfqFfmL0qZjgdaxff6EdAvBcvL5LDWF8bBtnw5Yn8KntWU+io4iR5KtMCpw/Ng8UayTSJdMp1JKtOzs/AxHE0OTFeH25Dm5WTG/qO81UXk+7z86IIlLOMrEadyf3ACCghI5z6hA== 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=5fO4svkCr3PwRb8jSJ+pvIL+NbYbOh266RPYEgeqZNA=; b=YDHM6QKO0fznQn4+trSI9Xg5UyKNFAaPP9j0osaw//lluiOoPzdBRyurMruGcnex1nlCucEnA/ON3M77hWdIJOqE560cNE+kRUERZ4rRbNh482rDtheKKsplLhciNVKxKCaxQY/S49UXto9uE3C8ibdLjYkPeLsPqt53n1wC6k9IGqslNe3XU0TweWq8nofk25jWhFL11Hy8Pa3QT6bjHgCHYXev5TGlJGTbPYDCHWCGIu9ukcb1+/AP4/g5DsVAl17/TNkTVU/rLDcSo92QKGvu+mV88UqPbTeODYR+rG3b7aO9PV8SO+Tx0VtgPZUpq+5wiQWOFeTLI9h9B8+r8w== 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 DM5PR1501MB2182.namprd15.prod.outlook.com (2603:10b6:4:a4::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5081.15; Wed, 16 Mar 2022 23:37:28 +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; Wed, 16 Mar 2022 23:37:28 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v4 4/5] bpftool: add support for subskeletons Thread-Topic: [PATCH bpf-next v4 4/5] bpftool: add support for subskeletons Thread-Index: AQHYOY7MxVWv4HADZU+Uy5daEh1FbA== Date: Wed, 16 Mar 2022 23:37:28 +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: 9a9df5c4-a05f-4ffc-f955-08da07a5eebb x-ms-traffictypediagnostic: DM5PR1501MB2182: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: qHcxI1HsyA9w3TIsb4CPVvUpZdmlDMt8jpIohBIctGvzrOcSRvkWqNb0GyEukEWCxKf5BfNsTzTH10iv9m/MM26DxOnXakheYmSrGSgOuz4w/qpRGT7p+hNJyLpzM+v5CC+vsxvRBYfq8LLnv1OpfIUwlbgHo6F9Vunpm+G88ZkgCcP3lg7wf0uRu1G2a79I1gGO2cjK+s9e2UZbdEZeh0xUP7zfo1YJDuNxMbEp9QYFitFvGKGsBpJtunKyqUt2X/O9BVmMtbZITbSER4CSzFFjMwn3CO158M9qqYbe1bPvbAwHZLON47DCkio5XafUmVYbhzKKgR6Z4GlgCMLg5w+Dmvj/8QdFOOzvl8cHod35YzS3x7h7ohsMEfCaBFoWFhgIN03lBJjfnF4vfJNP37ePg8Utsde3CxGnUxfazBHnMlbUQCd9FuO/6RqZjPLjVgeHAQbIN3xxYoc6CnRSIHvWaT9jVmL9FOzC4fWBF6L8GqtSJmiTvnAvKvc+kpB9h2ZaFbZrepeV9QxXoRB33131vMK2SWb/RHTxXmTaziNYbBnlCnquOc0yAGr76qLS9PYrNFtoNo4rKNtWRS4D1hWcuYjUA5N1GGJCOjeXA+V8euP62xqV1tnDzbBcMdLEXcDPxGWhi6LLzRru5rBjrq5q6WNkdjNd0F1h95APVlZ+UPOg0DYSw9jEo87JfDIyHz4c/qHCu8EMiDz4/aKWJw== 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)(8936002)(2616005)(5660300002)(110136005)(316002)(38070700005)(6486002)(71200400001)(508600001)(64756008)(8676002)(6512007)(122000001)(6506007)(66476007)(66446008)(86362001)(76116006)(66946007)(66556008)(91956017)(38100700002)(36756003)(186003)(83380400001)(2906002)(30864003)(559001)(579004);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?5FfrFubDMKBJKJkzxPsRQdt?= =?iso-8859-1?q?GVpLzyJ2/rxr+e8INiVrorkj6szH8Nu0cFeQx49TBnohTarPiV5YqYaaAHpE?= =?iso-8859-1?q?kS5QNtTf7bNUA42YxMIwkmSJInFDwohm5gcR4OWPVXnrvWECSgkYEi3oitQ/?= =?iso-8859-1?q?az8WzrBCg1FdiOFUSW7RIjDCPeOdylrPUxM4qSI+bsAVW6OuTNQPqHx88CRB?= =?iso-8859-1?q?lkitDKsC1J9mAwXp4qLNXB/1kyW5e6S1tPs1lH3h84T7c5h01ERzZZuMm+VX?= =?iso-8859-1?q?2JsnpgFaW7E8MtKeySXfH+Y+XtQ2+wMElsgz/ninfKsJB48ps1Tcm/qVFR1q?= =?iso-8859-1?q?JGXpio0Pv/qUuQNpim/PI2VzMMW9tEYfMLaHJodln0Q2VJyNd54nFoOozHj3?= =?iso-8859-1?q?8XKu9ABt4oeo+XpgMRW2F3pZK+qCACrH8mSzur0Y4N4QMknxW99zyph/5Ji9?= =?iso-8859-1?q?KkcsBXgASj0nenX5hHgsZ8N4zXUCEmF1OOmYtFPTg8wtPWTPg00492xUbKIy?= =?iso-8859-1?q?H453iJgHu5K3dGnIlau+llLfNK8VneZ6tbKfxVS3vk9PYGNR7TEK7Le06lVN?= =?iso-8859-1?q?X64aQRV+aHt6TxxAgIBYLtuZh2wDBpyR9fQYsFvES1XpJXkLxy3QgBoy21SZ?= =?iso-8859-1?q?rF1YYmzUCb9CxkHM0QbpBLlRE9SxdVnR/ydpkLakmR0v+fRVV7x09VW45gup?= =?iso-8859-1?q?iZzMZmxwbx4cGV41wtyTl0HcUqy0FzQX21nGD/ybGPS7Mra5flsQTheVuEfL?= =?iso-8859-1?q?whqC5odWoQenmGqc24yCXCElyTb9aNICQz5RphNNJaDySDv3fhb+fHoMNNkB?= =?iso-8859-1?q?OusKxbEJejBiwlDmIX3IxKUsrNfV6FHRVQbZpOm0+jJXHdzF5yR68nCca4jk?= =?iso-8859-1?q?/GhwseGES1yVmp++e9I7rvRNNQq8hnMGh8Tv9l4wuG1rsKEfMaeP5QXHKl0I?= =?iso-8859-1?q?gsLOqe8JFEU7zzlb1+YMoY+/sKhMJxsqPIGNAlhTDIRo3HSjQa+ONBK4XvFc?= =?iso-8859-1?q?eoxdAgrqsQg9/g7z1tYx+95wUDty3sjQErB61z8MyInUT4hgONX+1qKiVQda?= =?iso-8859-1?q?sPCI2X4Dm6mLqe5v3dQuzcx1vrAVtmr+o340u8ol/5P381mV4+NG8dP+zDfB?= =?iso-8859-1?q?Wq2xNZtOLCWhX2ieV94RraChBTyh621eRir9zCjg3qkwcev86FNf8LLS4h45?= =?iso-8859-1?q?6sKvntnHuGgcYERL8ONK4fNJrZr3pE6QgQ4MpjV9cliw+YhB2mBvNflW8MiS?= =?iso-8859-1?q?A0EdiiU2XU1KuiVUgEkXHXDyJ02DLE1CytKtg9vskouFvCTeBDIDpbotAtd6?= =?iso-8859-1?q?0o7Ah/lfa28MlWgHqu4dTqjaXrVofjpbE1PsKxbY3oVGZKnZi9geK5XhaZL3?= =?iso-8859-1?q?D0hVrigNBi6WfIvzJwh3g6R9KULoVc0G0LawiTn/6SUTnPFiSie8adg8/rW7?= =?iso-8859-1?q?f+enYDi101HX6zkbPxLY+Bq8jp04/4E29LUnyMjyySdLe4500yuN6UoQKVyC?= =?iso-8859-1?q?q30/k438N5xC7sNtMt4MLMe5Bf7lCTT3Q+RHGJQYPgq7jxGpxwkg=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: 9a9df5c4-a05f-4ffc-f955-08da07a5eebb X-MS-Exchange-CrossTenant-originalarrivaltime: 16 Mar 2022 23:37:28.1391 (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: csx7FYPJZ0NvV+rCcxxNgQsAJBdf6ftiz+2qTXbuoD9Fv9/FI1nTcvDefWZoeWTo X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1501MB2182 X-Proofpoint-ORIG-GUID: vBA_h01H3BnWMRi10oY9ow-hLQoA3rR5 X-Proofpoint-GUID: vBA_h01H3BnWMRi10oY9ow-hLQoA3rR5 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-16_09,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 | 588 +++++++++++++++--- 3 files changed, 542 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..42614ac94d46 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 is_internal_mmapable_map(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 (!is_internal_mmapable_map(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 (!is_internal_mmapable_map(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 minutiae. + */ + 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 (!is_internal_mmapable_map(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 (!is_internal_mmapable_map(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 (!is_internal_mmapable_map(map, ident, sizeof(ident))) continue; if (bpf_map__map_flags(map) & BPF_F_RDONLY_PROG) @@ -727,10 +811,95 @@ 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 && is_internal_mmapable_map(map, ident, sizeof(ident))) { + 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 +990,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 +1193,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 +1254,311 @@ 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]; + 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 (!is_internal_mmapable_map(map, ident, sizeof(ident))) + 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 (!is_internal_mmapable_map(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; + + /* 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[%3$d].name = \"%1$s\"; \n\ + s->vars[%3$d].map = &obj->maps.%2$s; \n\ + s->vars[%3$d].addr = (void **) &obj->%2$s.%1$s;\n\ + ", var_name, 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 +1610,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 +2207,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 Wed Mar 16 23:37:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12783303 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 94905C433F5 for ; Wed, 16 Mar 2022 23:37:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237131AbiCPXiv (ORCPT ); Wed, 16 Mar 2022 19:38:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245373AbiCPXiu (ORCPT ); Wed, 16 Mar 2022 19:38:50 -0400 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AFD4167DF for ; Wed, 16 Mar 2022 16:37:34 -0700 (PDT) Received: from pps.filterd (m0109334.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 22GHCdMP028583 for ; Wed, 16 Mar 2022 16:37:34 -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=eo6+Un4OkzARWQh7ZGYXGD+mhuQf8D6IBYNz06CMXvpl8q43ho8ZnLzBCy5bwn71++Bt o4MBp/02nz7Ua/4jfFeUvQXQCcWhMQYWMc8U0wN/KdlbNt/7Fso2XUP6et/vWBvymgH7 cfJVsE8cbsQH14qobl8ducJQsn80FVzg1cQ= Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2100.outbound.protection.outlook.com [104.47.70.100]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3eucf464d8-6 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 16 Mar 2022 16:37:34 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RUnazgl3OT7+da4zBfWbmgbvcOUWiCuBN3qMhYB+7sJ49/Xnw/wnqg3H9QLnhLJTvDTHGa/wu+mXPKJfben3+SAHmiqFZ1u1GtML+R6AAf9KXtFllqwYX+LI0+GUkfnraaGyA4WIb/+n9gofleyiBJpGssTE9XAaL+gYDZjBuURxd7p6Wy5ek1R4PXpddXPQyTrSqko+nyx0pp7CSX6tenZM2g9XyeieqqACG1pp8WYGoAiiwPvMOO8s7Kk0MzryFGCBVvQqfNm08GxVyXD/cha2yfp7mlQrWJPgsGyL08yNoSmZaXBiBd1oMA3q4s8+eJMvKaLr4N+8dtYjH3TZDw== 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=OBxisdKXxsmwTN8A6leuqIj/eYVJ9B7lYd+el4CQriFVGjJZbOfgdwMwL06+nlUQOl+ctbSkS+1QqM5xwb+lYm+kKYagJlkaLk7ksPI20tF1oX/D4tYq9ckIKw5J33pntaFoiej7JxB9QV7tUVAmM/t8dkcLvFiXC8EW2yI95I4/PgyFr3MQnLvOYM7lKpOmDGr/enRfoO2BJwGAw3D5DhaOWsv+X4YDpARJVBUAnGzTG4qwr6SGvr5IHCqAPc72B/9hx4s6fBnN3khjw+dQefXxb8k3wq1QN2McPKoytNINJpzaeRtJZ/XmzIUsY+/M1gXSyr5giTNc/O6sqbFuxg== 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 DM5PR1501MB2182.namprd15.prod.outlook.com (2603:10b6:4:a4::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5081.15; Wed, 16 Mar 2022 23:37:31 +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; Wed, 16 Mar 2022 23:37:31 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v4 5/5] selftests/bpf: test subskeleton functionality Thread-Topic: [PATCH bpf-next v4 5/5] selftests/bpf: test subskeleton functionality Thread-Index: AQHYOY7OL0rfKFu3gUWToAwEtc5y+Q== Date: Wed, 16 Mar 2022 23:37:31 +0000 Message-ID: <1bd24956940bbbfe169bb34f7f87b11df52ef011.1647473511.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: f4b8148a-fbe9-48be-bf66-08da07a5f0ee x-ms-traffictypediagnostic: DM5PR1501MB2182: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: DfrBqey20bAxBNd/+RNRmJet0DzBCAyP5lUwd2a3lKNXZaR26y1RpwnXuJDdVPwLEbJ6vXCt4X3bJGro1fCQt5mEhYR3akCa/LQutXIg++8mbwsQJ4nSOnUV7mbIFPq4s8W5Ifwoa0RFnvqK61XIs3SgxH/w9bDRM4nxMac2LUXM2nKJBezV7KseXYFFYHhCvL5bhlujA5KBGBBZNrvWbaG9jVlZmVLBrY2HylKY+3NuuiatrEzD2KZCMWmhU5SDwWEvXJZA/Zfwey8kn7QVPpCuXWhDjJDCjGrqFoAoTXh461m3ff+8LT1fze/qgQNKUL7YY7SFox1nexgiSW9V425TTo8ABuY91aOtz+N5qTxzoubYGKC1PhCK5AR4RmBGRxaSp5UsTYEF7aa2QNc2JFcJe2SdXbQPUWR/x4/V+lHXqWw4L3GXPFEXGv0yaQWDsEgG1lCK4YEWDe/lXFrTeIMozVXkJ38+JKhNATQjkQ+oqHdSe6jRotw83oATFKHdAD1WYnmDAesO7r9mRTJXIfxVNaic/KwsYIE7ZTUFSqPQly+PvlPQDtc5ZrCNOxyeJkMffNlQDh58JReV97l4slFtSV17Fzl9wtyQWwB/zs6yYWkthX9dElWcBZQp0K+ckqiwC9rWj9i0vbgft2rODGKqN7l8gqlNS7wxcb/D0vImulzHSFW8wOQu7mWF/oawkYuj+NnZzJT4SoffM6SeDg== 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)(8936002)(2616005)(5660300002)(110136005)(316002)(38070700005)(6486002)(71200400001)(508600001)(64756008)(8676002)(6512007)(122000001)(6506007)(66476007)(66446008)(86362001)(76116006)(66946007)(66556008)(91956017)(38100700002)(36756003)(186003)(83380400001)(2906002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?UHvMa4WhgvqyX05pfrjWGPz?= =?iso-8859-1?q?khwmsNsyWGCOHaoZ25u+OG0aJpAgLwneITMV5xHc5cQ4D0F6UiDRM4n6v3n2?= =?iso-8859-1?q?987xyQGcJTvgoERkB1k5HW4gIghxjvec/AeTUexZ1WeGUMK5nRuRh8KTFaz9?= =?iso-8859-1?q?VOgswPLkZidXf6461ITdxmkJ9ECmCcAdR9YHKWAau6s7EEoJSvzgy4pA03Hj?= =?iso-8859-1?q?VWnrSzDzN0odwzamNy3TjWOig/MFpNxVu7OQPkvXQZ3Wd9DSxpr5V3Ivemul?= =?iso-8859-1?q?C5IFnWAmpb7CxJYlGwj76JwfjTnVGK8NTkQlJJ+Wpd4FQcIjrSI+uVxX55of?= =?iso-8859-1?q?ILF/OW3LMC6OZrDIn+7yBVBSyiY5mEHN+nZ5URTz9+S+xVhfy6wuxBJ3cA7d?= =?iso-8859-1?q?k8kGKPWKy/8sXpX5ZXb4isk8Ies3v9y11wP7uaOX6cxpHII6QRVXFoly4s/G?= =?iso-8859-1?q?QRvMKXqCP8FlpCAbqHKRRbMFdrN5NBe1wcUw2bULRevVvn7Z2XbCWostpP2B?= =?iso-8859-1?q?QFHnX5eb9FEN+O5VxhaNhFpG9kZPGU/70nVjVEalwMyOmryI6VqfMVmfsNxb?= =?iso-8859-1?q?OJgt0sZtAk5u+SNiOf31GPjjOsYO2rtaMHAgGgDFendA4NBQL5Y07BUs2X67?= =?iso-8859-1?q?iUmvZym9JMI1ASJH88n/CDPNSCa5mnOUEY2KxedPkg04NdBF+ixL0xjYIvhe?= =?iso-8859-1?q?Hjo+SDMvWIEJeppl5eG1waibitI5IKijsXCX4QlEkXVEU6i0tBW/3YE7aAOv?= =?iso-8859-1?q?odkymllGEUwqAA3qromsaN1r/fop4DBJZtwhkv9nnSRDuzH4mOOb28Dd7U/u?= =?iso-8859-1?q?XTafa5vxuqom+O38NuUzjyzCh2szsnd3xb0ivJpcunylGxRuEP7sY9lXZ1E5?= =?iso-8859-1?q?nc999IhrKkIvDUq2OsiCJBQc2O/c+C/hDXt70P5O6IJLB0b5AzMeDivqVh6I?= =?iso-8859-1?q?Mx5+iX3QFz6SXxw7UgXCpatS/Qphy3XJUVXckGJ4pWLp7RPYqcmuNQJmGHxr?= =?iso-8859-1?q?u3NjRNcA42rP2iVbiIa/FrDcA4LrQd75C0paAGZv2XWVOyZd28OcqsbMtkSQ?= =?iso-8859-1?q?AXE0WHSzS3ZIEnTl9MAva4WMhlk5Z9qBoBkGu5NIar51kzAmEE1pR8aBytqj?= =?iso-8859-1?q?AbFvQETl3uj1A/v9Kkh7NtlOnncc9MU68P0MhZGjov+TJv9tC5pSFqREVCOS?= =?iso-8859-1?q?g9LMhxiNks1l+mNQGsQ6I6XatGfrAvPdz+subqmp/z5JG0ezmIiUS0FVp/i4?= =?iso-8859-1?q?zh3DmPz7OaZ6yskCXOwoBLTGIvwYf+2uscxZL5ORLfG3Unr3Xm0jd26VU6b5?= =?iso-8859-1?q?tvk7M6AG5DQrNZ3jrHUhlITJecN9YkD4aMluMQjUi1XTsWAadPlxPuGwb2K0?= =?iso-8859-1?q?3VP2dM0uxyXfzi7cHgW3Vs7nTtB/T1htF9FehaMUtX3vpQy1gL94LiaLa/FI?= =?iso-8859-1?q?rFf8MTBMPulBfq7gTbN24QjoOE/ioeiA9hJpyCeRFVr1PVYtTq/Cj3Rvnx54?= =?iso-8859-1?q?6w4S8WxC7kZ9pga1C20yk4wcGg7sDRqomaGGVMMJ5zk9F+kvf5wA=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: f4b8148a-fbe9-48be-bf66-08da07a5f0ee X-MS-Exchange-CrossTenant-originalarrivaltime: 16 Mar 2022 23:37:31.8742 (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: DdxRegNfvrmLvXnwoXiRl7SAnfvd8+uvp/TpI0OK8GSYnsGwtlaJhiDUmt0+XJ+C X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1501MB2182 X-Proofpoint-ORIG-GUID: w5wwgu9kyHg6gCmg5XbCP_5xFMlEhZrv X-Proofpoint-GUID: w5wwgu9kyHg6gCmg5XbCP_5xFMlEhZrv 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-16_09,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";