From patchwork Wed Mar 2 02:48:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12765415 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 54771C433EF for ; Wed, 2 Mar 2022 02:49:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236789AbiCBCtk (ORCPT ); Tue, 1 Mar 2022 21:49:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232091AbiCBCtj (ORCPT ); Tue, 1 Mar 2022 21:49:39 -0500 Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A87E5AA034 for ; Tue, 1 Mar 2022 18:48:57 -0800 (PST) Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.16.1.2/8.16.1.2) with ESMTP id 2221cmCh017818 for ; Tue, 1 Mar 2022 18:48:57 -0800 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=QTXK9CEGhkKY5dXe8uJ0KKxvAZc4A3LXaGiCZuV+a0s=; b=qbhm8v4RdvN+c9lGcR5q9OGtdZn5wl/D0lGKKPZEJeMeB02cnJQPEa2Eavk5R7eC+b9z zo7xlGb74bxTkxiKZFqixP8yC7vYpLIavzexAcH5P4+dH9ijHEFnmbjpvnfjIEYS+gen vCkh/K0u/iDOTiQxc92f+XgHbbVy7THxGZU= Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2049.outbound.protection.outlook.com [104.47.66.49]) by m0089730.ppops.net (PPS) with ESMTPS id 3ehn5wmt5a-7 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 01 Mar 2022 18:48:56 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZvY/kRqLlll6o2V//sFlmtSUPz75QyAQyXkVJ4txo4mDHGZdU7RiUt5kF/hw5oUrIHQk4VDH+16dwcECUkZEtzsnj8v7q7yuB1zy/agM42+hsD/d7ZyLpsY15ZtLnWUn7+7DkSnaOp6eItx2vIa+vB6QFAeLbnGIsTCzvuXT8UULXGLDiVV93rLSDZryiFKwzVpUokCzE2zbPA/FLBejHVXhHSyUORzO3hpIctzibWtBT2F9vBZZQisGUFYisEXFbt1iZOSEzktsftg5eJiAXBgBmWUig+yAEUA2vDXyQoLEkZ3e9tSCLWp+lwQTH/Luh5IHpPucnfKZM8/6NpYokg== 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=QTXK9CEGhkKY5dXe8uJ0KKxvAZc4A3LXaGiCZuV+a0s=; b=JXASZcGFqWZvq++/hud7OVsLNrDzr+/L6Hq1QyM02eT360x8wFlCXxrwEAUuWBThsqj6mkzDN25vXSLV6V91/IGKjItD4G2012OHX5zfjRdAIeHEzkGsBu1kCZF07tEQqCv3Aoe7fdqiifd+1xg4kYcxxW/W0afp4+pvEJOGfH/vzOBYuZBPMUiMM/ogVhK1q641UbaKHkKc6bFy+GrNzf3SlH7/2YnKemGzCR3O1DzX+gaoj3RTL24ZOIil26Zk4KHIYO3dfWx0VHp5S9c9SyxmTI47iC8BDQ6uIc/Kmfg+B1NSmJ/rclXFhzQj0amqqLRgUbQRizXQYLhKNwSPHw== 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 MW4PR15MB4441.namprd15.prod.outlook.com (2603:10b6:303:102::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5017.26; Wed, 2 Mar 2022 02:48:52 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3%5]) with mapi id 15.20.5017.027; Wed, 2 Mar 2022 02:48:52 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next 1/4] libbpf: expose map elf section name Thread-Topic: [PATCH bpf-next 1/4] libbpf: expose map elf section name Thread-Index: AQHYLeANyepTcOz9fEOe9yiUzCe+vQ== Date: Wed, 2 Mar 2022 02:48:52 +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: 0bb1cb98-be90-4aad-dfc9-08d9fbf72f9c x-ms-traffictypediagnostic: MW4PR15MB4441: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: swtBpgGMhn3x0FI+dDGdipwaRf3Pbv5QJVjj3SLbITp1XiOtnZTjJxW2J4DazOC0sr+EI9K3ASyDPP25Do4+ETaaUixe5hzsqX9I4Xq2nKf1idX6FpdXNpv1Z35BpDvDCIW2/MM87alrWsTLOm1CBzBrFKE1WIGDOa0mJprEiHmFmb3i3UniGyWEWwoIIX4/moXuKJAAcZmfH2M/Ji2ETUzOfLLdNTLDvmIRsmjcgpsnHbJfAEk70iaZiZud7LfkakgS2HbDwLbT2LbfIsgNQwwOp/fwbygCPLRMJwj/M+PLpBGyI3X/JkNaRe22pPetHC3+rKFBhz2jEg4k7J0o5/gdmC1DpuLHB1VRmsrkgFDGXk8jBTQWLHSbVRE8dFnsc4l7cN9gNDPt0WGE/9pEwhZ8XdGQW8OyFVkMXbR2fqhtG3jQTD0D1l8c0x2SPj4gRAwDzKzL5/hoH0WE5f+hvGHK9a28TAhPgpno4hhF1XGVsrOY39lVx+2lqShie0YCrygMMUkQ2xTRqw+Iw5vY1vwCDL4k2SNZi+to3nC13GYbrssiiR6mQPD3XJ8TDEMyrsH1JibFqSRjQX+5y0kfKVpjNoogY3s1Uo+QNVajlefvuj1NFnkgLMRZNQsjGWhn0NnV3Gu8Tot5kB7X2JRHhWD7LAlqUlkDDQ7YaaR9MfdYpCBc7ydv1WjBBRB6otMcepm8IpZLizBbx9p6gSfftw== 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)(316002)(38100700002)(83380400001)(110136005)(122000001)(86362001)(6512007)(6506007)(66476007)(186003)(38070700005)(91956017)(36756003)(2616005)(66946007)(76116006)(66446008)(66556008)(64756008)(2906002)(8936002)(5660300002)(71200400001)(508600001)(8676002)(6486002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?d1TP86rD3PwCp1HaIl0IKnG?= =?iso-8859-1?q?i9Z833NembHKl/TkE/rT8FUo3Msiyty+A5/5NDMZ5YtO9BnsdWXp29mHCOM8?= =?iso-8859-1?q?pGgT1qBZwFKYwXQ23xrYH61pyeC+bXwty7nY4Q5uSzT+hNVAnl8gnR3hpWaU?= =?iso-8859-1?q?mzjEsoDG0kPTcD5iaExiOh/1n40ia4iAVy/zkivHTt2aBBGNg662Vi9xezT7?= =?iso-8859-1?q?GeUBZyZTBMJmbcQrUTphde9xWESH+6X/OBUJGnIh8ov4lpFc3ivwMy0KTPCU?= =?iso-8859-1?q?uCmIEiVB9e9pJ8nyWFBAxqy2WkDJNKs4JTXQDhRhpc57qA4BznKMnxEIbaER?= =?iso-8859-1?q?/BeWtgcEptm6Ua1Lej5Uz/1x62ZN4wqcHqK9D4q/jOh8SzOJrn91Bcr6kq8M?= =?iso-8859-1?q?bJNrk3YbYVKq7S+n3AflyIxNi5+BsxEmIYyMHEC8KzAKJc1SrvIaQPtOwqBI?= =?iso-8859-1?q?FCc7UwIyh9t3fUYtMywAh8TZ/ENRRjif3KT8mMo83OP/LeIQc+qZxqg/k+g6?= =?iso-8859-1?q?PhYI31Suwfb7WwAaFyfQ3Oib04ZVCLaoscUHD/AQeVXSvteCTvdzqFlpu684?= =?iso-8859-1?q?b9US4Pqz6eXBt7nmc/t9KW7inqg5SeI6sddiI3rJGrNG5GBWnJ11jtaUy9Gu?= =?iso-8859-1?q?E21Lo1cmc5YYSQdt7+CE2kJ08OBIV7/scXQkVvvkKEdDvNW9VIm3oRvzLJce?= =?iso-8859-1?q?I/YZnDVPKsDgXUBvwAm5e2PJidWZdRZ4unq5PVclDLfgqAQjYkcJ9YglYIBY?= =?iso-8859-1?q?5Fb9Tp5bT+Mg//NhSk9VG8M1iFO3E2XcJo4kQGkRgmoCoVgwppNk71XIYlRO?= =?iso-8859-1?q?0CVFbkYvPHSXptMR3kaQ/wcZWFEj4aXz69rI1UUNvvGInrMRQz+dj9OWfRKE?= =?iso-8859-1?q?dlLz47NsZiAcT1qnzXuDaKs4jtQPWe2OihR061XPMRzOObpQDn0vPi6N3cd+?= =?iso-8859-1?q?KRT4Qy9DMLOq3gy3WjutqzFEcyLZM0eM6/nYsAcq3+ui8qhMxj7lxUV7OjoP?= =?iso-8859-1?q?REHUufEXttwmS+W6uAfw5aUnHgWEbCvGAnM7ZJWZd+QcAX4uYAEH2pOMGkZ0?= =?iso-8859-1?q?8xcnSLgDrVtgMl+VWiEJiwfgXlLIh0aYyhdbvyG/jLoEaDsabDoGL4gR6dlv?= =?iso-8859-1?q?b8RToKMwY0k2mih+J/qw+CrOzkAMNaBWmiinCkp3g+CvF1yyYNv/wtxXoaxL?= =?iso-8859-1?q?TMIxL5zNFqaogTAomePKgJAE9gvmeBn/qQZW2B+WI/cJbigaH+rGHsP4ywqP?= =?iso-8859-1?q?fGD6sNllfWqVppHaQXgqjrB8GEl0SgiPTaKseo3fsNMqcXyAWgP2POoAn/6f?= =?iso-8859-1?q?sKl/nlhDyhUlkVQJxZt/uW854g8Ms5FkpEzkyDIqSyupp3lxEkiEABUpmFcM?= =?iso-8859-1?q?FKG7dk/f3XG3T+MtNbH9A2CJrvz1+ZlnxA8qxxGQqWdWtAOa3b79MPp6ojzk?= =?iso-8859-1?q?u7t5crMNA9ZbHI0Wg6o1CSY3dTu3IRsNjmXFZoSJeY90bk/9A6s3w6ORbUbq?= =?iso-8859-1?q?lbGP/I1WGDYAdRkUdvojSiHNQg1t7X1gtqketjVwL88CMHAINzIR3OURnkPt?= =?iso-8859-1?q?tNx2pQWrTfa1QbfvA7zLEdWu15ZS84wWp8Cjsz13ltnU8eaW5AUb9fCh4Va8?= =?iso-8859-1?q?J9Da/egR5sd4ND6TvFZS76nlLe4DzZTuNvlqUBA=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: 0bb1cb98-be90-4aad-dfc9-08d9fbf72f9c X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Mar 2022 02:48:52.3002 (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: jv0vAkJtwLW81d9AEHuDVcfrDCRkQbRKlh7jBs2IQBEmlJ7CQJSmAesPX1lI4Pgb X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR15MB4441 X-Proofpoint-GUID: wALwC8apmKufgpZb0Daob1d-t8Av_rKX X-Proofpoint-ORIG-GUID: wALwC8apmKufgpZb0Daob1d-t8Av_rKX X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-02_01,2022-02-26_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=fb_outbound_notspam policy=fb_outbound score=0 clxscore=1015 impostorscore=0 lowpriorityscore=0 priorityscore=1501 mlxscore=0 spamscore=0 suspectscore=0 mlxlogscore=910 phishscore=0 adultscore=0 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2203020010 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net When generating subskeletons, bpftool needs to know the elf section name, as that's the stable identifier that will survive into the final linked bpf_object. This patch adds bpf_map__section_name in symmetry with bpf_program__section_name. Signed-off-by: Delyan Kratunov --- tools/lib/bpf/libbpf.c | 8 ++++++++ tools/lib/bpf/libbpf.h | 2 ++ tools/lib/bpf/libbpf.map | 5 +++++ tools/lib/bpf/libbpf_version.h | 2 +- 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index be6480e260c4..d20ae8f225ee 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9180,6 +9180,14 @@ const char *bpf_map__name(const struct bpf_map *map) return map->name; } +const char *bpf_map__section_name(const struct bpf_map *map) +{ + if (!map) + return NULL; + + return map->real_name; +} + enum bpf_map_type bpf_map__type(const struct bpf_map *map) { return map->def.type; diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index c8d8daad212e..7b66794f1c0a 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -741,6 +741,8 @@ LIBBPF_API LIBBPF_DEPRECATED_SINCE(0, 8, "use appropriate getters or setters ins const struct bpf_map_def *bpf_map__def(const struct bpf_map *map); /* get map name */ LIBBPF_API const char *bpf_map__name(const struct bpf_map *map); +/* get map ELF section name */ +LIBBPF_API const char *bpf_map__section_name(const struct bpf_map *map); /* get/set map type */ LIBBPF_API enum bpf_map_type bpf_map__type(const struct bpf_map *map); LIBBPF_API int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 47e70c9058d9..5c85d297d955 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -439,3 +439,8 @@ LIBBPF_0.7.0 { libbpf_probe_bpf_prog_type; libbpf_set_memlock_rlim_max; } LIBBPF_0.6.0; + +LIBBPF_0.8.0 { + global: + bpf_map__section_name; +} LIBBPF_0.7.0; diff --git a/tools/lib/bpf/libbpf_version.h b/tools/lib/bpf/libbpf_version.h index 0fefefc3500b..61f2039404b6 100644 --- a/tools/lib/bpf/libbpf_version.h +++ b/tools/lib/bpf/libbpf_version.h @@ -4,6 +4,6 @@ #define __LIBBPF_VERSION_H #define LIBBPF_MAJOR_VERSION 0 -#define LIBBPF_MINOR_VERSION 7 +#define LIBBPF_MINOR_VERSION 8 #endif /* __LIBBPF_VERSION_H */ From patchwork Wed Mar 2 02:48:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12765416 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 11BC1C433EF for ; Wed, 2 Mar 2022 02:49:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237103AbiCBCtm (ORCPT ); Tue, 1 Mar 2022 21:49:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232091AbiCBCtl (ORCPT ); Tue, 1 Mar 2022 21:49:41 -0500 Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA619AA022 for ; Tue, 1 Mar 2022 18:48:58 -0800 (PST) Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.16.1.2/8.16.1.2) with ESMTP id 2221cmCj017818 for ; Tue, 1 Mar 2022 18:48:58 -0800 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=TXsdPEdp73jK+ERklfSew823H05ygELazNLNqjLVWb0=; b=b7irk3KhOHYuPCvgu/7V7aZuV8ICpLKs5EAPfHHZnUruGFb4mqRTkAv9XjoK+xhqe2hC f1F5OyKVWo71mQIlvSkRB3WFDeoO/xqXW8ZdXdO72TA4fhNb9Pv08hvSFCyI+XIeFc6R b1exOQI4OeBitOF77DTYVq5FwpEa3t/5qiI= Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2049.outbound.protection.outlook.com [104.47.66.49]) by m0089730.ppops.net (PPS) with ESMTPS id 3ehn5wmt5a-9 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 01 Mar 2022 18:48:57 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XHhWVGLRxiM4DLiHMuQoXp/Q21KcP2/2bVylan1tSI1RjmBWs1QzxyiUbybHLZ8HesDwmaRnBT3vOLhSRsAQt3K3nPEm89qeRvZg3jkiEVZHE2ySF4wm4iTdj9TRQsEbPuxxZyrlEEeWMJXR0rlAGW3xH+KADZSnOV06UvHkEvkkwZunNUYsGE6WlNcENlbiebSzocoUNcXTHgpXdzZBvT5V5XpHOXtBazIFSmEk3XcHb4FUFfR1Eg6QZSmsoYyrRNX4V3HGoV0mwHIUuyZMwpIFgEwofhHKGoYb2hLqSPQ0CLRYMLpiaxYLnyoxg5wp19ivdK7tRdX92n9Ewzuy4g== 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=TXsdPEdp73jK+ERklfSew823H05ygELazNLNqjLVWb0=; b=NM9jKk8PkcTkJ76GFvdUrd6FHiJIXr6WQmhK8KZnN9BQYVrPVocXhPwWoeHdWCpvn89om1GrSNT6Ycn9ZBgKM3sbyd/Sbgf77vifoYVDbzzGmUlWSVPuSE7lQZfFnqUoNbxTk3Fs88f5nNy59tY+X2QJ2cUJvIbJuaIv9X/P2jcw2Y8A+ZV32TsscVqNC9Ckmxkt5GXdtin9OcJtooZncEHa2akted09uqTFNtZosZelBqFIbWh8559PljAco047SVtew9L33XIIqgtZ/3Rco04J417D4nD5d6zulsG/P6j54xUAJ+P4ZAb44fWlVqYTdwavrkdBotZCr7NSr9P2FQ== 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 MW4PR15MB4441.namprd15.prod.outlook.com (2603:10b6:303:102::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5017.26; Wed, 2 Mar 2022 02:48:54 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3%5]) with mapi id 15.20.5017.027; Wed, 2 Mar 2022 02:48:54 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next 2/4] bpftool: add support for subskeletons Thread-Topic: [PATCH bpf-next 2/4] bpftool: add support for subskeletons Thread-Index: AQHYLeAOCjyNx/p0zkqw4nvA3AAv2w== Date: Wed, 2 Mar 2022 02:48:53 +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: 3081ff61-6d0b-4795-64cf-08d9fbf73097 x-ms-traffictypediagnostic: MW4PR15MB4441: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: KRDkcncfk7StxbfhycZLXTrGXGBeyTVhpjd6WJXo3bnvU8DJBOjgkV+W2wdgkn4NWzDstzBisUENThQh6UQ9h1RDStHG6nXw+XSnzZJoZnQI05yjzyoJ5IKada17Y3ptZj/tBhHfi3w1/YEOmivefzqxBq1JgyxxPH1DAQyED/+HQVQrhdbVRWabtcZ6EoXCin5gAOyKFjgS/L4xGUSgz3bLfK5VPN4iIYVNMbb6jvJHkF/36okmAhMULX57JSy1RWjRqYL2COGoRZlBLmpvHJyNLZFI3Itq/W9r5UkNerZlnPn8X2Nn6wmcwzrWAcTGn2XnQbKa6BU5/8APOE9Bl37Bid6WPQ3eTF0gQ1Ywsd/Fd9l8BOPJ+8BMUimab0oD4cQfedQsCDwj9p9QVMFsQl4+BWTTkTSLEM9GCxcxjrkEGCJRIB2GyYDwRfNNliEEJMUf1nnlUNfikWjF1r+Xy67YmcFcSLqJRaSdxVr+fjqgGFQ+2ZjB7yH4tpV067FeUKrZLSJHFkWtKNzbfEsApO4DZbrY7e1a2WjDJ6dsqXtAt6ajOp3pLma7zEJ9OdY2tGO8wWW4GUpvje3872+9ZP15//Ogcatj8QGuQylaD04naY0xjVp47wBxLv9uSja6VRpl+R1tiNSy2UIsxIkF7swKfwA38AJt+WPtQDe/tYxQ7Yrn2H8kBAXsUtb46Vqs27r4mHHgJmC5kHAuXQ7Fvg== 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)(316002)(38100700002)(83380400001)(110136005)(122000001)(86362001)(6512007)(6506007)(66476007)(186003)(38070700005)(91956017)(36756003)(2616005)(66946007)(76116006)(66446008)(66556008)(64756008)(2906002)(8936002)(30864003)(5660300002)(71200400001)(508600001)(8676002)(6486002)(579004);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?w8cBusk8Rso5UP1JbzhQ47M?= =?iso-8859-1?q?/8q9u29ris64x+fHdbGIxxKiHQypUMVN+ujTtWzIYJUq/8J9iYajCD2JlnXh?= =?iso-8859-1?q?u7CYx9G0JPdVXTuZTO02ZqRM51Li8HL3AUSFdANlsCh0OdWtueA/04vuU3h/?= =?iso-8859-1?q?oYr8egNOCdIMj6V2V744spK8DtOVUyhY3IRrEIroVpNH9uGDKzwxkz6bd7mA?= =?iso-8859-1?q?e/mMh9JLr0UzoJ/RI6VZ2wlg0tzBhJdYvxCb6vSLkcyQdsuIqQw2KP2lDwAq?= =?iso-8859-1?q?ax6do5PO1IRVViwy1weLx6AN888AcJbYG8GvNSxCSmFdFKaShppewBlxqysc?= =?iso-8859-1?q?LPWRvqzhVJ9ocbc8a8tliyOTAWeVmGVQMliLOSBCJoZcHL43O3kbYZaoQiXE?= =?iso-8859-1?q?cMM+eGdyTQDPFrK8yDjKYe182zIrgsJf0dqc7a9yfA9rL6Fonsgpn3ZmV13s?= =?iso-8859-1?q?+WliH4PKJldCWknidaAuyRLFfoz4QyMVJS5mG6TuGNg6V57NT0NpY2K0LjSA?= =?iso-8859-1?q?nhH6ZWoJFygwBODmmgNXm3Rkhp3Up+6Ny0PmTRZ71KP9/QCYxoEfzciJQkGd?= =?iso-8859-1?q?hz6j0FZDfd68ccF+2jfa2Fb6PIM+LvRX9lrrYWKxPwRgPFls8I608PqKp+BC?= =?iso-8859-1?q?kqBg49PPuYaIXrTWnRoP5s8p4I52O1czDNvgi28SvbVQ6flyobJSan38Ifc/?= =?iso-8859-1?q?UPiRDpiYxsHuISZRyuTfZaREWZdOeCvg2cOwvzh3shSI+EmuQOPGrGavvv9b?= =?iso-8859-1?q?cJwFNJq9ufM7QAqPl61t+46QOm+ZJ7uhXC+ZP09dGwv6RD0j7TWH59ARhwHe?= =?iso-8859-1?q?6fMQ/kip2ONq6I3hzDv28Znh3uNSszwzjKlK+0g1FpGzrMXfFnDu1XEluv9E?= =?iso-8859-1?q?93L1A0aY6VgHyGe3m7DiE5jVysfyP+a3VzDm1suw3vL+yTgaMgu0C1u7kOZ5?= =?iso-8859-1?q?cF267mnTgkUi+K2ada+OLGgXFih3mJLM7pswHCMttkXJfMEwv4qlnyRcjrrM?= =?iso-8859-1?q?kHpF3SsvrSJP5FRmdLFP2+jL3z4vTM2N5gz+2IRnLo5vVhgKax4JXtRG4uMB?= =?iso-8859-1?q?8SZkz+NXDeyfsq31Wlf64SP4WDV+bLBjlx7beUX972S9RMUvH0MjuD+Npe0u?= =?iso-8859-1?q?t5Fkzi6YKn8gTKvYLe1wQCVHEaOVTJtfIBePbQH+eFUiBepovNasNIcl708b?= =?iso-8859-1?q?ZCG7uASl+fpxp0aJmrX1XegmaBzJBo8vI8qlt7C4NwdKzp7L5IDsDzYJt0EY?= =?iso-8859-1?q?Fq0iKf2dPfKdcwHSsSxL1Qhcb5YCmWEEjG/TvsWLaW1RUKlfQYjR8ZVckv18?= =?iso-8859-1?q?qDdhqZsdXnxQHPe0gIoxL/1ZXu2YzgA0QX33XUQ30SlTfqnhtmVgIE9s3P26?= =?iso-8859-1?q?nb4Ur1XOODK7KhFS4ij9Jb3W+ME2/EZz3nVsTVcsphOMDLK5cOqmjSlBcKGI?= =?iso-8859-1?q?TIBLWjdCmEqJfAli5lSrjA8wQODGazedyWyd+VW0o1O1q+bUR4MLhHkwAuOA?= =?iso-8859-1?q?MYz1sLx920+wqQ5nxBVI+ZLnceIz0AVIiQ23odry4XSl9mjsk8kNqBcwMp+U?= =?iso-8859-1?q?83cmihOP2KKhOLy/hxufKvyPW3+wFRM9ur+eDRkoz4UooND0opyCmfpFY/Mb?= =?iso-8859-1?q?rPjXzkHEul8lGghfKHO6pfSzizsuxz8Hyl/8QQQ=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: 3081ff61-6d0b-4795-64cf-08d9fbf73097 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Mar 2022 02:48:53.9561 (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: JRdILaFzXG7Th2OMIirM25BXSKe6mjoSw7JrL8iB21FmSe7Yi5XNui6eDUDn7CDT X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR15MB4441 X-Proofpoint-GUID: BPorzhRkqrfeH3TR34TTx8-RWnqmrvFH X-Proofpoint-ORIG-GUID: BPorzhRkqrfeH3TR34TTx8-RWnqmrvFH X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-02_01,2022-02-26_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=fb_outbound_notspam policy=fb_outbound score=0 clxscore=1015 impostorscore=0 lowpriorityscore=0 priorityscore=1501 mlxscore=0 spamscore=0 suspectscore=0 mlxlogscore=999 phishscore=0 adultscore=0 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2203020010 X-FB-Internal: deliver 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, only globals are supported, though non-owning references to links, programs, and other objects may be added as the needs arise. Signed-off-by: Delyan Kratunov --- tools/bpf/bpftool/gen.c | 322 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 313 insertions(+), 9 deletions(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index 145734b4fe41..ea292e09c17b 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -125,14 +125,14 @@ static int codegen_datasec_def(struct bpf_object *obj, struct btf *btf, struct btf_dump *d, const struct btf_type *sec, - const char *obj_name) + const char *obj_name, + bool subskel) { const char *sec_name = btf__name_by_offset(btf, sec->name_off); const struct btf_var_secinfo *sec_var = btf_var_secinfos(sec); int i, err, off = 0, pad_cnt = 0, vlen = btf_vlen(sec); char var_ident[256], sec_ident[256]; bool strip_mods = false; - if (!get_datasec_ident(sec_name, sec_ident, sizeof(sec_ident))) return 0; @@ -183,7 +183,7 @@ static int codegen_datasec_def(struct bpf_object *obj, align = 4; align_off = (off + align - 1) / align * align; - if (align_off != need_off) { + if (align_off != need_off && !subskel) { printf("\t\tchar __pad%d[%d];\n", pad_cnt, need_off - off); pad_cnt++; @@ -197,6 +197,15 @@ static int codegen_datasec_def(struct bpf_object *obj, strncat(var_ident, var_name, sizeof(var_ident) - 1); sanitize_identifier(var_ident); + /* to emit a pointer to the type in the map, we need to + * make sure our btf has that pointer type first. + */ + if (subskel) { + var_type_id = btf__add_ptr(btf, var_type_id); + if (var_type_id < 0) + return var_type_id; + } + printf("\t\t"); err = btf_dump__emit_type_decl(d, var_type_id, &opts); if (err) @@ -205,7 +214,10 @@ static int codegen_datasec_def(struct bpf_object *obj, off = sec_var->offset + sec_var->size; } - printf(" } *%s;\n", sec_ident); + if (subskel) + printf(" } %s;\n", sec_ident); + else + printf(" } *%s;\n", sec_ident); return 0; } @@ -231,7 +243,7 @@ static const struct btf_type *find_type_for_map(struct btf *btf, const char *map return NULL; } -static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) +static int codegen_datasecs(struct bpf_object *obj, const char *obj_name, bool subskel) { struct btf *btf = bpf_object__btf(obj); struct btf_dump *d; @@ -240,6 +252,13 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) char map_ident[256]; int err = 0; + /* When generating a subskeleton, we need to emit _pointers_ + * to the types in the maps. Use a new btf object as storage for these + * new types as they're not guaranteed to already exist. + */ + if (subskel) + btf = btf__new_empty_split(btf); + d = btf_dump__new(btf, codegen_btf_dump_printf, NULL, NULL); err = libbpf_get_error(d); if (err) @@ -264,11 +283,11 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) * map. It will still be memory-mapped and its contents * accessible from user-space through BPF skeleton. */ - if (!sec) { + if (!sec && !subskel) { printf(" struct %s__%s {\n", obj_name, map_ident); printf(" } *%s;\n", map_ident); - } else { - err = codegen_datasec_def(obj, btf, d, sec, obj_name); + } else if (sec) { + err = codegen_datasec_def(obj, btf, d, sec, obj_name, subskel); if (err) goto out; } @@ -276,6 +295,8 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) out: + if (subskel) + btf__free(btf); btf_dump__free(d); return err; } @@ -896,7 +917,7 @@ static int do_skeleton(int argc, char **argv) btf = bpf_object__btf(obj); if (btf) { - err = codegen_datasecs(obj, obj_name); + err = codegen_datasecs(obj, obj_name, false); if (err) goto out; } @@ -1141,6 +1162,287 @@ 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 + * a certain number of datasec fields 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 globals 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("__SKEL_H__")]; + size_t i, len, file_sz, mmap_sz, sym_sz = 0, sym_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 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); + opts.object_name = obj_name; + if (verifier_logs) + /* log_level1 + log_level2 + stats, but not stable UAPI */ + opts.kernel_log_level = 1 + 2 + 4; + obj = bpf_object__open_mem(obj_data, file_sz, &opts); + err = libbpf_get_error(obj); + if (err) { + char err_buf[256]; + + libbpf_strerror(err, 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); + err = libbpf_get_error(btf); + if (err) { + err = -1; + p_err("need btf type information for %s", obj_name); + goto out; + } + + /* First, count how many symbols we have to link. */ + 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, ident, sizeof(ident))) + continue; + + map_type_id = btf__find_by_name_kind(btf, bpf_map__section_name(map), BTF_KIND_DATASEC); + if (map_type_id < 0) { + err = map_type_id; + goto out; + } + map_type = btf__type_by_id(btf, map_type_id); + + for (i = 0, var = btf_var_secinfos(map_type), len = btf_vlen(map_type); + i < len; + i++, var++) { + sym_sz++; + } + } + + get_header_guard(header_guard, obj_name); + 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); + + err = codegen_datasecs(obj, obj_name, true); + 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 %1$s::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 *subskel; \n\ + struct bpf_sym_skeleton *syms; \n\ + int err; \n\ + \n\ + obj = (struct %1$s *)calloc(1, sizeof(*obj)); \n\ + if (!obj) { \n\ + errno = ENOMEM; \n\ + return NULL; \n\ + } \n\ + subskel = (struct bpf_object_subskeleton *)calloc(1, sizeof(*subskel));\n\ + if (!subskel) { \n\ + errno = ENOMEM; \n\ + return NULL; \n\ + } \n\ + subskel->sz = sizeof(*subskel); \n\ + subskel->obj = src; \n\ + subskel->sym_skel_sz = sizeof(struct bpf_sym_skeleton); \n\ + subskel->sym_cnt = %2$d; \n\ + obj->subskel = subskel; \n\ + \n\ + syms = (struct bpf_sym_skeleton *)calloc(%2$d, sizeof(*syms));\n\ + if (!syms) { \n\ + free(subskel); \n\ + errno = ENOMEM; \n\ + return NULL; \n\ + } \n\ + subskel->syms = syms; \n\ + \n\ + ", + obj_name, sym_sz + ); + + /* walk through each symbol and emit the runtime representation + */ + 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, ident, sizeof(ident))) + continue; + + map_type_id = btf__find_by_name_kind(btf, bpf_map__section_name(map), BTF_KIND_DATASEC); + if (map_type_id < 0) { + err = map_type_id; + goto out; + } + map_type = btf__type_by_id(btf, map_type_id); + + for (i = 0, var = btf_var_secinfos(map_type), len = btf_vlen(map_type); + i < len; + i++, var++, sym_idx++) { + var_type = btf__type_by_id(btf, var->type); + var_name = btf__name_by_offset(btf, var_type->name_off); + + var_ident[0] = '\0'; + strncat(var_ident, var_name, sizeof(var_ident) - 1); + sanitize_identifier(var_ident); + + codegen("\ + \n\ + syms[%4$d].name = \"%1$s\"; \n\ + syms[%4$d].section = \"%3$s\"; \n\ + syms[%4$d].addr = (void**) &obj->%2$s.%1$s; \n\ + ", var_ident, ident, bpf_map__section_name(map), sym_idx); + } + } + + codegen("\ + \n\ + \n\ + err = bpf_object__open_subskeleton(subskel); \n\ + if (err) { \n\ + %1$s__destroy(obj); \n\ + errno = err; \n\ + return NULL; \n\ + } \n\ + \n\ + return obj; \n\ + } \n\ + ", + obj_name); + + codegen("\ + \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 +1494,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 +2091,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 2 02:48:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12765412 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 E416CC433F5 for ; Wed, 2 Mar 2022 02:48:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233642AbiCBCti (ORCPT ); Tue, 1 Mar 2022 21:49:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232091AbiCBCth (ORCPT ); Tue, 1 Mar 2022 21:49:37 -0500 Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 244A3AA022 for ; Tue, 1 Mar 2022 18:48:54 -0800 (PST) Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.16.1.2/8.16.1.2) with ESMTP id 2221cmCb017818 for ; Tue, 1 Mar 2022 18:48:53 -0800 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=cblz7JdFHO3bxo/XRXNtepK+Ev/JKha2UBcmTxGDITQ=; b=qi0XmovuIpZ17AYzofsYFJ7nnNd3imj85JF18iWdicxWJaloPX3Sm4sR1/UJffpwc4SL UdK406ii6NKRXuFpDNsICe+r79Wj8l3Csbt2G1KkvkHfJFzSOd9ad0ez4hXyOFmrb9v9 eAARybTlGp3Gzzt229hx7+yyR080cj/tOpw= Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2049.outbound.protection.outlook.com [104.47.66.49]) by m0089730.ppops.net (PPS) with ESMTPS id 3ehn5wmt5a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 01 Mar 2022 18:48:53 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NwZxmP39zM5XsRSnri3p8cFsNz8e1+p8XGVDKFHs7HprDfWaz39vFZReaRj94yXkRNJl37QJwbKwy1JuoVry6GtZHhOI3JnFmIkG0fN4BEozZyV1hU5A7xERpT0PY53t4fUvHcMqphIkdHssTOc6ANAi6EJIYc3zvGwuKZjCCoMlGy1spd7Eq7lar3VAY4ofpZ8lINnLLsKAE6KReanKAgtzCJE0mtnKrKEPeZtPk8y74BKhiXAhJpfifSwuwWQvQ1XB/ZanpKyUnBHpvmU1ZVZFgrRVNpuFtrxK+dhQKvobSDVS3OhQ1cWoeTpQXRDcf2AqtdubtwUHY5sqlHJ8xA== 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=cblz7JdFHO3bxo/XRXNtepK+Ev/JKha2UBcmTxGDITQ=; b=hgj+TS3DdcjeXyPR1SQbeIbs9dycMkWo7OvRi1eQasMJH81zcB3Y81302udEhM9J9KJjlTqAqn1FdAJfgKBYoCj0qH/g5IHqW6GTmpyIfOb0ZjwYpB9AHeH6fAtL6q08ERj7+3mAnTpc5/ySK52qrjZZdaBrjWZQFMeWCcPbK0eSNh49x8DMTIo0PbBhff33cIJBwb9/SdnqAttzgTLlwMhByX76GBDfJvvTYaW/ag7103KcTmoGpDbLepgaxKFrB2eatw6DDaq5dIT2u9QoECv1AbSMBop9zMLK9wmQZnLf87QeO+L1yyDFGLvvHPzJ1t4q6DgCxzHqrDJnGQlDRg== 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 MW4PR15MB4441.namprd15.prod.outlook.com (2603:10b6:303:102::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5017.26; Wed, 2 Mar 2022 02:48:47 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3%5]) with mapi id 15.20.5017.027; Wed, 2 Mar 2022 02:48:46 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next 3/4] libbpf: add subskeleton scaffolding Thread-Topic: [PATCH bpf-next 3/4] libbpf: add subskeleton scaffolding Thread-Index: AQHYLeAJ25nB2g/kyUKDt/jLzXCj8Q== Date: Wed, 2 Mar 2022 02:48:46 +0000 Message-ID: <13cba9e1c39e999e7bfb14f1f986b76d13e150b3.1646188795.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: 6a489585-677b-4f10-1cc5-08d9fbf72c55 x-ms-traffictypediagnostic: MW4PR15MB4441: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: 6Gv9K73u8Nb1SoDU2QgOLM/YXBwuqHyMdtTjVR9F5ZX0lYXT/rtLg0+cgjBNG1d9xnuM04OvvjrMcSs0olrr/fCSZEcze2VvhWK6da4QgVlFx616d7nr4feCe5PgVfu9l4Rgi72K3HfYoUjc5JRl8xvtXzr4X9P+xJBoucHLnYnncdHWMvKqH6mU6ynfLUwdkgV852Yd0721eXSL5D2PN7CnbOWj8UdP7t0nse+EVav9XNNj36MNBw3ZNv4j6orQn7Xg5Hlox3jFR4gI2gNgJo47wJkNZUC/B2obEfKD6qX/Yw8uUR4Mr2SJr8x6HvmT6vs5flYdKraXL4q4lOqZZA4LkC7hp1/B/ApZ/TKs/UaS5J7nxV6RvAsQuuUtP2zLxDm81h6XOEpwXbRzpJMrikan1tZPoeKc23Z5BXWhYaqWHl1s34RkdethFQ9lxm4vq1uKgPCOe8e178jdmPqQMyE6CFJ+VIeGMk6DzVvHP3imRpT413mIo6a6jEPBNIX2nnqRaHrsceJZHw+oeoI0Q8/DnBQTzmPoebm2FvyZvMln+H4YIr3uFPL3Dh4xyHvd7RnXJLND5fR95YNcKcfT8GrHdswju+3I4YvyOcynL/ZQ1opG0Ueo5IikH7OPeCI72lmu1kzveXOYyHL5VCSjUk6QRdObQIHn0gIif+5U9S8CoDN03XYkAExX/rBidZVwhVKx8gmqOXmGYrAHY0wdJg== 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)(316002)(38100700002)(110136005)(122000001)(86362001)(6512007)(6506007)(66476007)(186003)(38070700005)(91956017)(36756003)(2616005)(66946007)(76116006)(66446008)(66556008)(64756008)(2906002)(8936002)(5660300002)(71200400001)(508600001)(8676002)(6486002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?gAV7LIGgFaMV2zIVYjw7eK8?= =?iso-8859-1?q?8foa6cmnN1nWsaKjCxy1Rh5BSbU9mhL4aEr/YDJmWa+ywQ7G4Dd581Fg8ftn?= =?iso-8859-1?q?LfqvCSHJ554R6MpL3DCViiScESdFLoOWO7kVI2vgqSmyj+Q5drRPlJfi6oZx?= =?iso-8859-1?q?UQ1U9Uj9fGZ/KcV96al8rDi8wOFFRC/1Pxjc/+EJqikkG8Xbws4yR3vCZer9?= =?iso-8859-1?q?RMU4QnXaZIZt5wI2GN01rDn8zvB5bl9URZsE8GOgQ6GJPbh67pg0wUFEfI41?= =?iso-8859-1?q?vLJAmDFSxxIv0vLokQu9rHnMdTeM5E8lsHfDAyymQwvZZC+WqjM8/iS6WiKc?= =?iso-8859-1?q?Qe3rBcGZltN79GWhvKksMttHP+ebj8NuxFc3Zpf0Kmjw1tBq90FJdFnqUdnB?= =?iso-8859-1?q?f1I4Qxt/DjjQ7UEpXnOnfijBjrZS5DGNIuIGvkUXKXKiVvsLlHMxfzUUYuv0?= =?iso-8859-1?q?cS2jyhfirvdIaQu+VB/oMuyvx1Z3XSi5mhbfnNT4zqd2lyrFCrpCDJN+mFHs?= =?iso-8859-1?q?ncc6dys66TK7S0oynoNFeCsYvKeP3+7e+1HtFjOC66LTbs4RKed6qq+dSyHL?= =?iso-8859-1?q?20ASJpugQ12r7TsgvkoDOuIsKW3cfXqYt3LEnC6yrSOSC0NXe//b2v9i0Gb1?= =?iso-8859-1?q?vcwvAwXCJQiPt3NDmaZWRD/f51PesDaAwZsZvjt1704t9ALsRCGc8v4sNu/n?= =?iso-8859-1?q?Yet8JlUoVKnzvo0bJ1zZs8dCOkueVJZAIhulaWy8MwMYFbuXAof2CYU7EKcf?= =?iso-8859-1?q?6/3lpAMu13vhp7hFxGbgMLpnSE8BH3ikouK/0wPZK7tQeP4RjQcUZo0kaTTF?= =?iso-8859-1?q?le62ETskujaQfAFwC4Qnr1bic8GcLqlpd+8vRFB+BAMV+jFGuzgBtmEONnZq?= =?iso-8859-1?q?wa3U5ICUHKBuF9eGrlRb2sEvS73L4pBIcUgi/DbNw5KQoZzLeZBwNgmbqTi6?= =?iso-8859-1?q?wTfpJHecTxQJI7/smn7mdEYam+QFsdqA54h8re0yjDqtJU+y+IN1aPPZbrQc?= =?iso-8859-1?q?eTocdTIvmQblisbjaW6+UIV8ZP1OEsa+vG1aYn6koIT0v1+hj3WktwhKVFAI?= =?iso-8859-1?q?CfL4SaT+zWk4H1WIPMqDRD8SMqPyKyLdrS3vWgwBBuHVr9A871v55xQqpM1A?= =?iso-8859-1?q?3Myncw9oDNMIxpMnawPJWSOWC2SzKUmSJizexa+Zi2yg+TngL9iM7Nd5fFgj?= =?iso-8859-1?q?1UZVD++6KescNUYar0HjXNBB3s6yizQE9CS4B5I3ccsXEddXrsLHr/Yz9T/Z?= =?iso-8859-1?q?TV4v5SG4mNqhThlJpNeUsJV4o39bdsi9PLghU/fzcLc22Bbg/VJ6bork2Uss?= =?iso-8859-1?q?J4ZTKvhGHRKdNEyexkIe+FWxiibryXJYjcBZIxh7AD+FR9gQ7Mz0ibF26psP?= =?iso-8859-1?q?RWSZR28Kj/CT1TrKkmsc0qOqsrcmIOT/2YMNbpM4P2JmMBBnZcFVVd7jLUt6?= =?iso-8859-1?q?oF4P6/idsD0uF9O0yeNXw+EXCLJFiMdnyvOD7BiqYMZjiKYHe/Z7QY9LRjqD?= =?iso-8859-1?q?TL1XXDyHzPzC4gPqno0YU/truuNlyjt7qD8gKJcuw1UeGtYovPqKfygUV49M?= =?iso-8859-1?q?DCoafcuiAKgPwa+egNV7I4OH245mrXmM2Q4C8pseG8SbOAwUxgR8TD1X1S8G?= =?iso-8859-1?q?EqImEGkTYMsf42i3OO93Dgll0NxrQhd/rU2eeuA=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: 6a489585-677b-4f10-1cc5-08d9fbf72c55 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Mar 2022 02:48:46.7968 (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: EMp+b7Dfh+ilEr6ebkQMDZuSdAEtV7fUuxgGHxCQ6Nd8YPMdAcOL/eKVSFHeOrVM X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR15MB4441 X-Proofpoint-GUID: wNgBfzJ1_VTQZsfNKmQLj8uP3vwuecnB X-Proofpoint-ORIG-GUID: wNgBfzJ1_VTQZsfNKmQLj8uP3vwuecnB X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-02_01,2022-02-26_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=fb_outbound_notspam policy=fb_outbound score=0 clxscore=1015 impostorscore=0 lowpriorityscore=0 priorityscore=1501 mlxscore=0 spamscore=0 suspectscore=0 mlxlogscore=394 phishscore=0 adultscore=0 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2203020010 X-FB-Internal: deliver 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 symbols described by bpf_sym_skeleton objects. Signed-off-by: Delyan Kratunov --- tools/lib/bpf/libbpf.c | 76 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 21 +++++++++++ tools/lib/bpf/libbpf.map | 2 ++ 3 files changed, 99 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index d20ae8f225ee..e6c27f4b9dea 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -11748,6 +11748,82 @@ int bpf_object__open_skeleton(struct bpf_object_skeleton *s, return 0; } +int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s) +{ + int i, len, map_type_id, sym_idx; + const char *var_name; + struct bpf_map *map; + struct btf *btf; + const struct btf_type *map_type, *var_type; + const struct bpf_sym_skeleton *sym; + struct btf_var_secinfo *var; + struct bpf_map *last_map = NULL; + const struct btf_type *last_map_type = NULL; + + if (!s->obj) + return libbpf_err(-EINVAL); + + btf = bpf_object__btf(s->obj); + if (!btf) + return libbpf_err(errno); + + for (sym_idx = 0; sym_idx < s->sym_cnt; sym_idx++) { + sym = &s->syms[sym_idx]; + if (last_map && (strcmp(sym->section, bpf_map__section_name(last_map)) == 0)) { + map = last_map; + map_type = last_map_type; + } else { + map = bpf_object__find_map_by_name(s->obj, sym->section); + if (!map) { + pr_warn("Could not find map for section %1$s, symbol %2$s", + sym->section, s->syms[i].name); + return libbpf_err(-EINVAL); + } + map_type_id = btf__find_by_name_kind(btf, sym->section, BTF_KIND_DATASEC); + if (map_type_id < 0) { + pr_warn("Could not find map type in btf for section %1$s (due to symbol %2$s)", + sym->section, sym->name); + return libbpf_err(-EINVAL); + } + map_type = btf__type_by_id(btf, map_type_id); + } + + /* We have a section and a corresponding type, now find the + * symbol in the loaded map. This is clearly quadratic in the + * number of symbols in the section, but that's easy to optimize + * once the need arises. + */ + + len = btf_vlen(map_type); + for (i = 0, var = btf_var_secinfos(map_type); i < len; i++, var++) { + var_type = btf__type_by_id(btf, var->type); + if (!var_type) { + pr_warn("Could not find var type for item %1$d in section %2$s", + i, sym->section); + return libbpf_err(-EINVAL); + } + var_name = btf__name_by_offset(btf, var_type->name_off); + if (strcmp(var_name, sym->name) == 0) { + *sym->addr = (char *) map->mmaped + var->offset; + break; + } + } + + last_map = map; + last_map_type = map_type; + } + return 0; +} + +void bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s) +{ + if (!s) + return; + if (s->syms) + free(s->syms); + 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 7b66794f1c0a..915d59c31ad5 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1291,6 +1291,27 @@ 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_sym_skeleton { + const char *name; + const char *section; + void **addr; +}; + +struct bpf_object_subskeleton { + size_t sz; /* size of this struct, for forward/backward compatibility */ + + const struct bpf_object *obj; + + int sym_cnt; + int sym_skel_sz; + struct bpf_sym_skeleton *syms; +}; + +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 5c85d297d955..81a1d0259866 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -443,4 +443,6 @@ LIBBPF_0.7.0 { LIBBPF_0.8.0 { global: bpf_map__section_name; + bpf_object__open_subskeleton; + bpf_object__destroy_subskeleton; } LIBBPF_0.7.0; From patchwork Wed Mar 2 02:48:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12765413 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 41971C433EF for ; Wed, 2 Mar 2022 02:48:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235996AbiCBCti (ORCPT ); Tue, 1 Mar 2022 21:49:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235976AbiCBCti (ORCPT ); Tue, 1 Mar 2022 21:49:38 -0500 Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1EE2AA034 for ; Tue, 1 Mar 2022 18:48:55 -0800 (PST) Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.16.1.2/8.16.1.2) with ESMTP id 2221cmCd017818 for ; Tue, 1 Mar 2022 18:48:55 -0800 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=f8lPfnRiag8el5diAMmbcStYfZyYO2j2g16tDVcfMwk=; b=ZIc6VpV2Dj6p9/9W+jbg875dfWWeXE+6J8sH0LAgd4aZlK3nRU0TZi8j+J7HGDowIR0a GQ9BJu5nZc5MrueCSZjrJimflPgabRVlAeTRv5FcmZ9WWWyHKGwalClYi8XFBXcQFbi6 9q3olb+dkyMe/eNdxaIKSwgHho0TCO/UcYE= Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2049.outbound.protection.outlook.com [104.47.66.49]) by m0089730.ppops.net (PPS) with ESMTPS id 3ehn5wmt5a-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 01 Mar 2022 18:48:54 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VUL1KbX0fawymq/8vkcZPkPWVlBFLOX5a0Sn0Bwt93zORxjeRp9UixaVKKo8zppu/10jQQK0Wlk0NDvtAU4MQrRfw2svSiMEkbBE+bQB+TC8Uy+SJ8ecAMkWpCTWSS2GFzJVTRFPfXR/Pvm1/8YfyPtV2oJW9Lhl9pr9H4yiirmHrOQ72nvvJAwg/Ax9jkF8W0z2je82ZNDfoMSkRwCTc5oX6rZcoIc75h7E0LZHw+mSSPhh1xng5CCu4jB9OWJCsVjb4CZcXOCMHpyZWdIUuQiO9DKBIyodVvlPXB4Ht/cFqCkHum7lOeTHdEhUmTsUo0VB2MS6pvpwPUC9vVSu4Q== 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=f8lPfnRiag8el5diAMmbcStYfZyYO2j2g16tDVcfMwk=; b=SZk7naYUqpnjAr0WfrVQUpuGlXfa9z1rlfc99ARGDOtVv4Qe6J52QpCErwcFDWUSCRsiz5tDZvYifPkRr7Etl2+OIMf5bfOAK8uRcJKW+yRVDuf0WCFPDrL9h6QzZS/SpCsaFbRJhocEsT5CAIjV2wZ/Ti0gk/yiUxG/ECtK8oeXu+g7BmPI4eDpPWQ26qCUKMVN14QS9hUFhAvEpJhpJEJJhmwwgh/i1csJ4EkNT3vlr91+gtwJGOPwhiQImuiO0A6Ykq5b67BvJvlU0IhNrxrrwupDQP80xoYDPrZZXOKyptzg/eOiPYvV0zJLatoEeCV1RmPfl9EbxPGIkZnwyA== 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 MW4PR15MB4441.namprd15.prod.outlook.com (2603:10b6:303:102::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5017.26; Wed, 2 Mar 2022 02:48:48 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3%5]) with mapi id 15.20.5017.027; Wed, 2 Mar 2022 02:48:48 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next 4/4] selftests/bpf: test subskeleton functionality Thread-Topic: [PATCH bpf-next 4/4] selftests/bpf: test subskeleton functionality Thread-Index: AQHYLeAKFjdznAZJQ0qoqoIsaLkktQ== Date: Wed, 2 Mar 2022 02:48:48 +0000 Message-ID: <89a850b9c06835b839da76386ee0e4bbeaf5a37b.1646188795.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: 17ec22d4-690c-4c20-4160-08d9fbf72d46 x-ms-traffictypediagnostic: MW4PR15MB4441: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: EzOjkV8pP3KLX9MYF4t5ojV+JpoOvJyo5E7nMzk1zezjIV/UUAL0sThm4UpKRUady2us5cyFKXqLOiQqswtqaGFoYa8bn32MeJ7LYi1hkjPNNx/ije8RPe5H26A/s0BZDSO9pBYkuOuv9CJqsMeGnYgOcQFAWlG9i9CictCSApcK3hWCK/7YKmhYyy0TP74HpTykukwMLLFfTPv9ZaeC7wOLzURFxPqJJWwIkjoAXrxT7kMsJksh8HzYPPBDo1p97Q6PFjrLTWMf62xIjs5q2mqw3/zkvRtE1RvKgqOlCgPP9nKyN143kxYq71VOHYbsTGWVuc6j7ZbA8uU2Ww4GQ1o9+22ysZTCTl5R/8l2fFo3UqUbpFhm9fr2Unf5INYDRQarA7OSkyrgS3zOB1WPnTVC+AALslyUEYt1wmlP5DnBH9Sz0MWZwP3NwSahvXkkKdtza4PFV3ctNfHsI3Yae8H0v7L2yyLaEvc/tdun460d40F/CrtLpffUM5I8nXbk/Pw0NoMlCw7KIVT+MPkQgiIvrG6zKN8qIWbl89FXykf3ssRatQzHRSnor5UAuOLw0pL6RGX9NpfDjEtgjRi9z130b1asdZxJN5kbl0dmiwOnXcUHdeB1sBd9hnUiGiPO9PG309OgCXSyDbudjIUq8FCfoJ9SOpbaA3/+h/aSG3tUEefIrvLJL0kf95RlUGPIwbS0hlY/TXZGiU74j0UrUQ== 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)(316002)(38100700002)(83380400001)(110136005)(122000001)(86362001)(6512007)(6506007)(66476007)(186003)(38070700005)(91956017)(36756003)(2616005)(66946007)(76116006)(66446008)(66556008)(64756008)(2906002)(8936002)(5660300002)(71200400001)(508600001)(8676002)(6486002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?wYC9QTh5gYC1mS0C/jN3Ptj?= =?iso-8859-1?q?bBut0xq8gyzwwbi/E90JiqR0hjvsXYRqAd074O+6QKC3PtTXEn4aqRzIo/Kw?= =?iso-8859-1?q?DxZHIdzfQEVd4arErWx27Cremqob4reY/NkxBhPrioPCqu/76rNU8ZwiK48A?= =?iso-8859-1?q?OynjkZpYvFqLItFSKuftCyHy9TseUCGPl5PVecMWX5e4sRNqpuIR1HpGQtVC?= =?iso-8859-1?q?zmFpYQ0k2zwLtUWyZmLnpGz1pZ26N29tU6UUrXyCOCCQ7HGEz85xG/tOzBEu?= =?iso-8859-1?q?4JZ7cTCAfsuBGHT3fdDWH3dkwXTBJJSj3P/ZZVH7XHDGlzkuSYdZG+DQDIw0?= =?iso-8859-1?q?6M0i5G7Vr1Dhd8tXZ/16a7sRFINMn3DjrfcFUQUL9TV0DPQnKxN0zgxXO3xi?= =?iso-8859-1?q?XvUGyEtt7NJvzeCuPBT96ZgG5hIqvpGAz7LHQ08WXJ3M40D9C/DYFv6UBNwr?= =?iso-8859-1?q?gfC1FMs3R4xnuxIsykIlSfqsWgNAKcH09BEzS2/cW821hXexNZz3d9JC4RhR?= =?iso-8859-1?q?2e7LHIsHTyFUj8nYH5RFNAiA2uPxyi3Gvxze78tMzNCNXN5GAzZSTcWOBpQ2?= =?iso-8859-1?q?n+zrPZ6d5h0JMdD62v2mDlLx/g/sX420ZxYyTyzDdU4G8vjhweEWbrkvI3fE?= =?iso-8859-1?q?7VZduwGpKCWra5E1Isul20b8SME3aYiHYHqC9pEWGQ+XGGTPvkHPCrq2d+gK?= =?iso-8859-1?q?LJloa3h3Plmjiw/WdtRVnr1ZRSEJxHoSMTMjm1eNsgfm5MWQxnei3FW5yBWU?= =?iso-8859-1?q?s9j3RQNpLPDGNZMrRbux863i1tmArZFRejT056/VvytOJKPKLyxh+J1TzGD7?= =?iso-8859-1?q?CzcD9Bfry3UkC0dYH+Ggh/IM5RhF7PsKozAUHnm69ZAUpwk5QV3Y/ykCMqLF?= =?iso-8859-1?q?F+IobCiuR7YtNkRFcFyfLgx4dxJ6/EvptBOtQ2sMRDVQcbYM41DKQYC1i+1u?= =?iso-8859-1?q?S7QsKD+Oqgcm6es3ljWsdrhK5XW3cV1zR7kA183ladHOwA2DQc2rmdA5/zYr?= =?iso-8859-1?q?fXsxx2rQOrHRll/drhILpWq0oj2zgjoVwtFRWtq7Sl/+UrK5biG6VGhWZhYp?= =?iso-8859-1?q?hJNFZ0HE4Q36E4DdBhQzNX3Tax8MbVfo+aZSkLhoTFuzXZF5BMtvxgetJ6vS?= =?iso-8859-1?q?MQLy3ctouPPoWwKlDEscK27IbtE8vdcq1G1UcjA+xRLv1sECFbKCOgB17YlU?= =?iso-8859-1?q?Gh8oWpeNW42oUUfWgkpIiUfFvWiN8R7r86j6IGbu1H0ezfRNP8XhVdzGXocx?= =?iso-8859-1?q?wmTVwx7CFsYulBrsYhmjqNouMTgB05d6u7M6C89xoAc15dQKFizjiZ06JOcW?= =?iso-8859-1?q?/ntwdHx2c7z4G3PrlNBZS+zEDR9FOMp1AhbGLwaUS9jZjm9A7cgFF34V105J?= =?iso-8859-1?q?986AYWnxFNNywBBtc33KVGUfi4xENKWzKppOMazNCXuZncWuZ//tJmbZEZ2J?= =?iso-8859-1?q?navjzS8Xlr51tTVhAXabcg/bkRkE1UZVeM+WSayhy+Hct7YR4ezQZn2FbCMo?= =?iso-8859-1?q?h0HZrGa7Wq/rnu6UNgw3XVm/Jo9Gkc7p1xmMTKJc7mkMxdlxgx+yIQXnkLHd?= =?iso-8859-1?q?ZmAqxCg2PBLE+PaKB1IolvLoqSiDa3kjcmOHav1rMwL/S2D390KmRLVuIWbg?= =?iso-8859-1?q?w2IyaPCn3EV84qb34/CEIY8r1RfQnY8fZ+yqjzg=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: 17ec22d4-690c-4c20-4160-08d9fbf72d46 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Mar 2022 02:48:48.3759 (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: fhfU2FfpMJ3UHlICg8Q6he2OYd2vmLKryuMNrYkMUQOrhJ1Kxh2ovDId1gBPUED2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR15MB4441 X-Proofpoint-GUID: aqKLqf5eAEmbRevizC1jSo2CPX44wvJ9 X-Proofpoint-ORIG-GUID: aqKLqf5eAEmbRevizC1jSo2CPX44wvJ9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-02_01,2022-02-26_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=fb_outbound_notspam policy=fb_outbound score=0 clxscore=1015 impostorscore=0 lowpriorityscore=0 priorityscore=1501 mlxscore=0 spamscore=0 suspectscore=0 mlxlogscore=999 phishscore=0 adultscore=0 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2203020010 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The new Makefile support via SUBSKELS and .skel.h-deps is a mixture of LINKED_SKELS and LSKELS. By definition subskeletons require multiple BPF object files to be linked together. However, generating the subskeleton only requires the library object file and not the final program object file. Signed-off-by: Delyan Kratunov --- tools/testing/selftests/bpf/Makefile | 18 ++++++++- .../selftests/bpf/prog_tests/subskeleton.c | 38 +++++++++++++++++++ .../bpf/prog_tests/subskeleton_lib.c | 29 ++++++++++++++ .../selftests/bpf/progs/test_subskeleton.c | 20 ++++++++++ .../bpf/progs/test_subskeleton_lib.c | 22 +++++++++++ 5 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/subskeleton.c create mode 100644 tools/testing/selftests/bpf/prog_tests/subskeleton_lib.c create mode 100644 tools/testing/selftests/bpf/progs/test_subskeleton.c create mode 100644 tools/testing/selftests/bpf/progs/test_subskeleton_lib.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index fe12b4f5fe20..57da63ba790b 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -326,19 +326,23 @@ 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 + +SUBSKELS := test_subskeleton_lib.skel.h 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 \ map_ptr_kern.c core_kern.c core_kern_overflow.c # Generate both light skeleton and libbpf skeleton for these LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test_subprog.c -SKEL_BLACKLIST += $$(LSKELS) +SKEL_BLACKLIST += $$(LSKELS) $$(SUBSKELS) test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o linked_funcs.skel.h-deps := linked_funcs1.o linked_funcs2.o linked_vars.skel.h-deps := linked_vars1.o linked_vars2.o linked_maps.skel.h-deps := linked_maps1.o linked_maps2.o +test_subskeleton.skel.h-deps := test_subskeleton_lib.o test_subskeleton.o +test_subskeleton_lib.skel.h-deps := test_subskeleton_lib.o LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps))) @@ -363,6 +367,7 @@ TRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h, \ $$(filter-out $(SKEL_BLACKLIST) $(LINKED_BPF_SRCS),\ $$(TRUNNER_BPF_SRCS))) TRUNNER_BPF_LSKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS) $$(LSKELS_EXTRA)) +TRUNNER_BPF_SUBSKELS := $$(addprefix $$(TRUNNER_OUTPUT)/,$(SUBSKELS)) TRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS)) TEST_GEN_FILES += $$(TRUNNER_BPF_OBJS) @@ -405,6 +410,14 @@ $(TRUNNER_BPF_SKELS): %.skel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT) $(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o) $(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$@ +$(TRUNNER_BPF_SUBSKELS): %.skel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT) + $$(call msg,GEN-SUBSKEL,$(TRUNNER_BINARY),$$@) + $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$< + $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o) + $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o) + $(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o) + $(Q)$$(BPFTOOL) gen subskeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.o=)) > $$@ + $(TRUNNER_BPF_LSKELS): %.lskel.h: %.o $(BPFTOOL) | $(TRUNNER_OUTPUT) $$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@) $(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$< @@ -441,6 +454,7 @@ $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \ $(TRUNNER_EXTRA_HDRS) \ $(TRUNNER_BPF_OBJS) \ $(TRUNNER_BPF_SKELS) \ + $(TRUNNER_BPF_SUBSKELS) \ $(TRUNNER_BPF_LSKELS) \ $(TRUNNER_BPF_SKELS_LINKED) \ $$(BPFOBJ) | $(TRUNNER_OUTPUT) 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..651aafc28e7f --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/subskeleton.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 Facebook */ + +#include +#include "test_subskeleton.skel.h" + +extern void subskeleton_lib_setup(struct bpf_object *obj); +extern int subskeleton_lib_subresult(struct bpf_object *obj); + +void test_subskeleton(void) +{ + int duration = 0, err, result; + struct test_subskeleton *skel; + + skel = test_subskeleton__open(); + if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) + return; + + skel->rodata->rovar1 = 10; + + err = test_subskeleton__load(skel); + if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err)) + goto cleanup; + + subskeleton_lib_setup(skel->obj); + + err = test_subskeleton__attach(skel); + if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) + 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/prog_tests/subskeleton_lib.c b/tools/testing/selftests/bpf/prog_tests/subskeleton_lib.c new file mode 100644 index 000000000000..f7f98b3febaf --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/subskeleton_lib.c @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 Facebook */ + +#include +#include + +#include "test_subskeleton_lib.skel.h" + +void subskeleton_lib_setup(struct bpf_object *obj) +{ + struct test_subskeleton_lib *lib = test_subskeleton_lib__open(obj); + + ASSERT_OK_PTR(lib, "open subskeleton"); + + *lib->data.var1 = 1; + *lib->bss.var2 = 2; + lib->bss.var3->var3_1 = 3; + lib->bss.var3->var3_2 = 4; +} + +int subskeleton_lib_subresult(struct bpf_object *obj) +{ + struct test_subskeleton_lib *lib = test_subskeleton_lib__open(obj); + + ASSERT_OK_PTR(lib, "open subskeleton"); + + ASSERT_EQ(*lib->bss.libout1, 1 + 2 + 3 + 4, "lib subresult"); + return *lib->bss.libout1; +} 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..bad3970718cb --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_subskeleton.c @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ + +#include +#include + +const int rovar1; +int out1; + +extern int lib_routine(void); + +SEC("raw_tp/sys_enter") +int handler1(const void *ctx) +{ + out1 = lib_routine() * rovar1; + return 0; +} + +char LICENSE[] SEC("license") = "GPL"; +int VERSION SEC("version") = 1; 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..23c7f24997a7 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_subskeleton_lib.c @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ + +#include +#include + +int var1 = -1; +int var2; +struct { + int var3_1; + __s64 var3_2; +} var3; +int libout1; + +int lib_routine(void) +{ + libout1 = var1 + var2 + var3.var3_1 + var3.var3_2; + return libout1; +} + +char LICENSE[] SEC("license") = "GPL"; +int VERSION SEC("version") = 1;