From patchwork Mon Mar 21 23:29:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12787908 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 B02D3C433EF for ; Mon, 21 Mar 2022 23:29:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232462AbiCUXaz (ORCPT ); Mon, 21 Mar 2022 19:30:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232662AbiCUXat (ORCPT ); Mon, 21 Mar 2022 19:30:49 -0400 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8355F284E58 for ; Mon, 21 Mar 2022 16:29:22 -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 22LKLZ2e003900 for ; Mon, 21 Mar 2022 16:29:22 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : content-type : content-transfer-encoding : mime-version; s=facebook; bh=r0jhrFjdktlKz/bPY17H5tOncw++MgD94FvqPdBUfWw=; b=dxZbX+yk9hZstZIcB/YlBgJz2/ydD66m8csM307Mhd5F3ifMsrzbDYiRapvOumPHULSB FvfmYmrFmdUC+A+cJu36VbgMcIiHZwjue5xbqgg81JJ3ZU2jwvpcL6Jn6Wi3jJqGZEHC YfCzkqrOOtnV4sHXoHv1w88P8TVSl0V9IlM= Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2168.outbound.protection.outlook.com [104.47.55.168]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3eweg2dhce-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 21 Mar 2022 16:29:21 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QxhGy/LwHDYshxMs6jPenfZX7hUDAwt9JH8rqiqw1KaqnKV6p88KuuhAa5Mi5cE7n2yVb/v+dCKxj2jo9vYQMOUe3RoDqDta4N3u2jy3jAUJmddOGrb95ZRvh/qS8KUWfo5w0CDa0rdX/btzMUMLM4VpKca050LwuCSESVLQ8aqNNOhPvXY89QKulnxcHXiUTg6YpYEy61UHrsPYLvg4wgF3GDY7+2zYvRJrloV4PUSepZY5XkOgd1zwtofzVi1a9VI1kvlj/Lg1MzUueNfO9+xYsVZa4GiNb01ZGAdvsBJUxnKc/VDvEq/BaDUKCy9RsQIP+52agyp9Ox4f64bfrw== 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=r0jhrFjdktlKz/bPY17H5tOncw++MgD94FvqPdBUfWw=; b=Om2sTOwpsXHWEXekJaLTCo9R3SuhpHdEVlbw3/LNgIwzFNDCrRe8sAlhj3lua38MqGSDqG4reQVZDounJJWPuTqPfSiGpx+KuIY0muGqILy6aL0NnYtIuNbIne/kuwkjlRjkOQCmHzePT2NINfDZkyWT8LjmILusNon7XbieQI0zFLPlCEo2b+hSd0MrustjH4J48pQJZYSshFEFKWreC6sbUGBxaaTxXHT6ReDss1IbrsJEGjYtQNALH23/eh8h2fjIjDOCslTuARbenu1Ub8boBclEMW6/pH1pITbMqa5tDpyXtuGX5SbWeTRfU15xOWUrJUqWlcZEww/alNeeAA== 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 CH2PR15MB3655.namprd15.prod.outlook.com (2603:10b6:610:9::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5081.23; Mon, 21 Mar 2022 23:29:18 +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.022; Mon, 21 Mar 2022 23:29:18 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" CC: Yonghong Song Subject: [PATCH bpf-next] bpftool: explicit errno handling in skeletons Thread-Topic: [PATCH bpf-next] bpftool: explicit errno handling in skeletons Thread-Index: AQHYPXt8Hkyzw7LMtUaPSITYjIxEhg== Date: Mon, 21 Mar 2022 23:29:18 +0000 Message-ID: <3b6bfbb770c79ae64d8de26c1c1bd9d53a4b85f8.camel@fb.com> 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: 4e0a244c-c18f-4ef0-0853-08da0b929ef5 x-ms-traffictypediagnostic: CH2PR15MB3655: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: LC6PPGf8iVah/vhJtyDRAAcZOP3+zh9L5J8v1g7TlA8jJ4RXxPAZD10tp+ggzkhjni6B5cYpdFqwbOFcrAP/Q7Rd7qVAYJYEc7WmBcmrPnWoIxchYvEvRjeQe/JbWZRz2x2QrI/ZUE4b4gYKj3ErCdHoz0pl58xlAYzQnl7qxrjBoZ/JN65qaHbkIU7PTrKxwVw5xvYdpPt/rOpwPiIiGMQiq9ZewocDj+35pUuN5RBfP0v8qpH7F295s1P7wDEHqSOau9iCfyU/CC4ZmpJ3YEYbbZ8V99STd47x9m42MVC/NXMW4+XsxEm+bg/aa0UaaKL20FS70TfLnvhWRmHLb26zUs0guO3lC6Cb3mMOT7pOA5+CMRgQsci9x/7HcL/UEES0asyUfzkCC95DJKjtHA9X+KkpFsaPxsZUVVLSPOWCcHhKnBtAZNp6zduCULrv1HJXV8iO7rkN7WUX6pt6unRSU7581nbVasaMsGd66A8yMqij0HNeHCEA6Z6C8jJs/CglNTbzkf54rQjyqD7SqOLuxZ0A7XMqMZ7S/ZiWvHEWqyYkrRQo6XdB4JBIbBPycFhA2LvqNj62ON45cPhMnLMVvP59QyT64c/J4IRLAkz+bkcMziTMqKAGypfDMxVB5eT5nqUu+BnIbaPAO8dBF/lIx2cwdROvc82vfmPBWwEjIZtSorCWKqiq42Q7YSr+N6xOOeU8Mb2dBYJsewYI/A== 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)(38070700005)(71200400001)(6486002)(110136005)(2616005)(186003)(86362001)(83380400001)(508600001)(4326008)(6512007)(6506007)(8936002)(36756003)(8676002)(5660300002)(122000001)(38100700002)(2906002)(66476007)(66946007)(76116006)(91956017)(66556008)(64756008)(66446008);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?9IlA5r8+vMJRbFnqhPv/PAB?= =?iso-8859-1?q?+9Nb638ipOQWKOAtWCpQCunWX2/0bBdkDMPeVEVE/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859-1?q?XbteYgcYFjrGiwpC7jM/7G6bsac7GPCYGkxUK1drGrgmIl4hQkpoCxBTGZvD?= =?iso-8859-1?q?eBgE1Kfw/R2UYTw842+R++t+a/W0qqKSo0THknMcNFCiqR4LpMWQidIhGAA1?= =?iso-8859-1?q?a4kXPagKSNSNxR+RPAFFKiVmm2wF41xC0HyIAdgu6C25vaDRDRWY4K42+gJY?= =?iso-8859-1?q?whafG0S8D3HR0xERGxUhANGVupxFEo9AWjBiF2atf5coPqFmoiWlzi920d2O?= =?iso-8859-1?q?LWMY6SnRRZjVsCGm/UBYShuzGhy2YlcUphBm3wIrAXGzeVHThl3NhMVGNVLk?= =?iso-8859-1?q?QpGMiMiDzTRCbzgeshlm05hhxvPPVqfYENiiwLKT2fXj+1OWEU+bNHOIVOHQ?= =?iso-8859-1?q?YZxZmdZE+jbh7rPbz3SvNbSxP0+a4yYVe2RK1a2EhCrI/+SISnvDvi6Re0e2?= =?iso-8859-1?q?mCJQ0vXGFC8TglM1NdWWysZi3HjBrlE0++q2RfVOOLs8g92UU+/s8rEV7YNm?= =?iso-8859-1?q?qtW50UAPFcp4iPCHDNq1J7s/w6i3s5XqLh/LLZhzEyKyDCtQKR/BxLN3VKlR?= =?iso-8859-1?q?DjQelMpq0JhDex3df5EBAD9PjSqyB41lipgRyT/4U3/VNDNw/CGY=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: 4e0a244c-c18f-4ef0-0853-08da0b929ef5 X-MS-Exchange-CrossTenant-originalarrivaltime: 21 Mar 2022 23:29:18.5398 (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: LviWva65v2DmkBOR7W4xyLKXwluWUaM5ddCxyxYjHryEl7b2pZMnvFRwKfU4NwcO X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR15MB3655 X-Proofpoint-GUID: QeWmOrt8e5s7Gtc083henxPTldoL7nOk X-Proofpoint-ORIG-GUID: QeWmOrt8e5s7Gtc083henxPTldoL7nOk 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-21_10,2022-03-21_01,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Andrii noticed that since f97b8b9bd630 (bpftool: Fix a bug in subskeleton code generation) the subskeleton code allows bpf_object__destroy_subskeleton to overwrite the errno that subskeleton__open would return with. While this is not currently an issue, let's make it future-proof. This patch explicitly tracks err in subskeleton__open and skeleton__create (i.e. calloc failure is explicitly ENOMEM) and ensures that errno is -err on the error return path. The skeleton code had to be changed since maps and progs codegen is shared with subskeletons. Signed-off-by: Delyan Kratunov --- tools/bpf/bpftool/gen.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index 7ba7ff55d2ea..e30f7bd48a2b 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -828,8 +828,10 @@ codegen_maps_skeleton(struct bpf_object *obj, size_t map_cnt, bool mmaped) 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\ + if (!s->maps) { \n\ + err = -ENOMEM; \n\ goto err; \n\ + } \n\ ", map_cnt ); @@ -870,8 +872,10 @@ codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_li 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\ + if (!s->progs) { \n\ + err = -ENOMEM; \n\ goto err; \n\ + } \n\ ", prog_cnt ); @@ -1182,10 +1186,13 @@ static int do_skeleton(int argc, char **argv) %1$s__create_skeleton(struct %1$s *obj) \n\ { \n\ struct bpf_object_skeleton *s; \n\ + int err; \n\ \n\ s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\ - if (!s) \n\ + if (!s) { \n\ + err = -ENOMEM; \n\ goto err; \n\ + } \n\ \n\ s->sz = sizeof(*s); \n\ s->name = \"%1$s\"; \n\ @@ -1206,7 +1213,7 @@ static int do_skeleton(int argc, char **argv) return 0; \n\ err: \n\ bpf_object__destroy_skeleton(s); \n\ - return -ENOMEM; \n\ + return err; \n\ } \n\ \n\ static inline const void *%2$s__elf_bytes(size_t *sz) \n\ @@ -1466,12 +1473,12 @@ static int do_subskeleton(int argc, char **argv) \n\ obj = (struct %1$s *)calloc(1, sizeof(*obj)); \n\ if (!obj) { \n\ - errno = ENOMEM; \n\ + err = -ENOMEM; \n\ goto err; \n\ } \n\ s = (struct bpf_object_subskeleton *)calloc(1, sizeof(*s));\n\ if (!s) { \n\ - errno = ENOMEM; \n\ + err = -ENOMEM; \n\ goto err; \n\ } \n\ s->sz = sizeof(*s); \n\ @@ -1483,7 +1490,7 @@ static int do_subskeleton(int argc, char **argv) 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\ + err = -ENOMEM; \n\ goto err; \n\ } \n\ ", @@ -1538,6 +1545,7 @@ static int do_subskeleton(int argc, char **argv) return obj; \n\ err: \n\ %1$s__destroy(obj); \n\ + errno = -err; \n\ return NULL; \n\ } \n\ \n\