From patchwork Sat Oct 3 08:55:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 267645 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31EB0C4363D for ; Sat, 3 Oct 2020 08:55:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ECCEC206DB for ; Sat, 3 Oct 2020 08:55:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ssi8vCip" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725819AbgJCIzY (ORCPT ); Sat, 3 Oct 2020 04:55:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725782AbgJCIzW (ORCPT ); Sat, 3 Oct 2020 04:55:22 -0400 Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4E87C0613E7; Sat, 3 Oct 2020 01:55:22 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id g9so1999594pgh.8; Sat, 03 Oct 2020 01:55:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3Cq63vVAQgqttSJiTYOC88cJIda331MR96jczDhYXvQ=; b=Ssi8vCipS69InurrBShlPBge2sczbFKLxzrF3yr3PIEyeG8hIwonbj6J1wXkU2hoGj RM9T2Kgjp51+5nql4jDCBT/OQN3e8Bq1cF6LrF/6KUpEncG0uicsUAAPkRPNlyqJNr3E phwmQIFESBOijFk+4awCXJjNVYxRBL0gZf0tCTibJ9PYmODNUCDhiwdYZg0ZYpnJzPSz vNpQcRVL5zAYMsVxOEqtu02BpU4dggSxNXRbbAOMHzE3gn4QgapIbFPD7DuYCjcLoIlr 3qBIVxxkgKfJMrMmuwHM5TlK5l5jlrpaPJluTwgxdEC1mr/7q9DF4DhN2M+1a+XVZO9+ 0a4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=3Cq63vVAQgqttSJiTYOC88cJIda331MR96jczDhYXvQ=; b=G3KphFWaCNQUwAtTL/K1REcswFNYHntY+eDEutG1ExroG9lXlMyAp/pKTVMoSXTALt IF5MOSpu7rLdNbzON+94+U58jO8Ws97PngDurCEADqZSkUCQ7JSYliWWpZtiXbtaZKq/ tjBjE53Q8PMfT2hTK9bEfptdtuAC3tXIEXdfNKj9RCrFYyu4CMyofPEgwlF/vQKPSdGB Wr/crCOjZqfPtc9XNPNOKttwXUhd9D1/NH9NzOtxSrazrttyo28ysemrlY6yH9NlKVAm DH2WvPoZM8AIIq31EmmC8K3I0/c/6zecKCocvnu7+scVq+iDYTiF5eaDc/21XBmEoRHz vIow== X-Gm-Message-State: AOAM530nU1s9IwOswa3BAjXrvoBIAxKtgqRpx4oF9tOxwAJWADRRCmIm RxMAIhC3JzHP0vseflqTmS6d0pcxug1REg== X-Google-Smtp-Source: ABdhPJyMwOFioky4ETcrvGXkDk/ggxiQbPp1COij9F+19ekRrntUvSWH5dOwx8g/yoe4pTeoChpD1A== X-Received: by 2002:a62:1b86:0:b029:13e:d13d:a05b with SMTP id b128-20020a621b860000b029013ed13da05bmr6745167pfb.33.1601715322201; Sat, 03 Oct 2020 01:55:22 -0700 (PDT) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id a15sm4566374pgi.69.2020.10.03.01.55.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Oct 2020 01:55:21 -0700 (PDT) From: Hangbin Liu To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Song Liu , Yonghong Song , Andrii Nakryiko , Andrii Nakryiko , netdev@vger.kernel.org, =?utf-8?b?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vu?= =?utf-8?q?sen?= , Hangbin Liu Subject: [PATCHv2 bpf 1/3] libbpf: close map fd if init map slots failed Date: Sat, 3 Oct 2020 16:55:03 +0800 Message-Id: <20201003085505.3388332-2-liuhangbin@gmail.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201003085505.3388332-1-liuhangbin@gmail.com> References: <20201002075750.1978298-1-liuhangbin@gmail.com> <20201003085505.3388332-1-liuhangbin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Previously we forgot to close the map fd if bpf_map_update_elem() failed during map slot init, which will leak map fd. Let's move map slot initialization to new function init_map_slots() to simplify the code. And close the map fd if init slot failed. Reported-by: Andrii Nakryiko Signed-off-by: Hangbin Liu Acked-by: Andrii Nakryiko --- tools/lib/bpf/libbpf.c | 55 ++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 32dc444224d8..c8b5fe45682d 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -4192,6 +4192,36 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map) return 0; } +static int init_map_slots(struct bpf_map *map) +{ + const struct bpf_map *targ_map; + unsigned int i; + int fd, err; + + for (i = 0; i < map->init_slots_sz; i++) { + if (!map->init_slots[i]) + continue; + + targ_map = map->init_slots[i]; + fd = bpf_map__fd(targ_map); + err = bpf_map_update_elem(map->fd, &i, &fd, 0); + if (err) { + err = -errno; + pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n", + map->name, i, targ_map->name, + fd, err); + return err; + } + pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n", + map->name, i, targ_map->name, fd); + } + + zfree(&map->init_slots); + map->init_slots_sz = 0; + + return 0; +} + static int bpf_object__create_maps(struct bpf_object *obj) { @@ -4234,28 +4264,11 @@ bpf_object__create_maps(struct bpf_object *obj) } if (map->init_slots_sz) { - for (j = 0; j < map->init_slots_sz; j++) { - const struct bpf_map *targ_map; - int fd; - - if (!map->init_slots[j]) - continue; - - targ_map = map->init_slots[j]; - fd = bpf_map__fd(targ_map); - err = bpf_map_update_elem(map->fd, &j, &fd, 0); - if (err) { - err = -errno; - pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n", - map->name, j, targ_map->name, - fd, err); - goto err_out; - } - pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n", - map->name, j, targ_map->name, fd); + err = init_map_slots(map); + if (err < 0) { + zclose(map->fd); + goto err_out; } - zfree(&map->init_slots); - map->init_slots_sz = 0; } if (map->pin_path && !map->pinned) { From patchwork Sat Oct 3 08:55:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 289035 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F1CB5C4363D for ; Sat, 3 Oct 2020 08:55:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BDE89206DB for ; Sat, 3 Oct 2020 08:55:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jihCVLBJ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725830AbgJCIz1 (ORCPT ); Sat, 3 Oct 2020 04:55:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725824AbgJCIz0 (ORCPT ); Sat, 3 Oct 2020 04:55:26 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CB14C0613D0; Sat, 3 Oct 2020 01:55:26 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id k8so3157669pfk.2; Sat, 03 Oct 2020 01:55:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=S3nfrEarbedDU7+cG2A8GUy+tauzCAIB7UfM/mCIPRU=; b=jihCVLBJ0JkeZQ6J3wFulAsQk7dMrt5K/w1OdRYcbqpNAo6mk2sfa+2fcHpYhkCcCx HROdYUzvJieHD11EsmP2FVmA3giANUYWZ6pmlkXn4eLTQPJkmMHLmFu45C6g/SsABFXR UPE+KGeeKJDMAvyv+uAtJk9CmGxZHtrcooSk583fBMUjf4+zOvC5Qv/m+G+VGRGFIE57 23r5UZqu5jpZHfW3PWA7dxVDJ6wi0rURR8ZDhCgeokZlO1V2hcYAGgWWZu89QqvUw+Ea xUeoerhPnaYIo/V9cWHoLzq6hMlT5kcKNH/XAmU7k1ZpYSkkTsCmRL2GUuuU526nVs23 FwbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=S3nfrEarbedDU7+cG2A8GUy+tauzCAIB7UfM/mCIPRU=; b=tNoQm9PP5DRM50Sgsp3qbYlW8MybcsGZjGKPn4w5iRRZ/Phq70Yvi54RuUMebhZUO9 +0AY5ErGq7LRyT2vE5soCFWIuPIiJ75lhNDD9XNO/K9B67DNyeq1ScbRzjTOYYlyDoFp i7+QKl8I3D1SAGn3g1k4e+6Eg+Yh7y6E0PVAHeL0T6+Gwd5PECxsVtBWokQTJRjtxK5m qCI5udVwCYrbWtN+qwn48QhScXF29gG8hEk3hqU5bvtfnHx41rBH0D3rAULKIHlomz3p 4dz6Urm6kBtq1SgmFDZcNfXJi7iiF9Oz7NR4Hj05AvvBx0PO2p12IY4Q8zfdbaOebwRt 4S+g== X-Gm-Message-State: AOAM532g2jXSmBjTsUqHMF+Zo2ZbCWLyRFBdWukP3ocqsKHmWC0nXLg3 c1e1KK70HyHdINAHdEnqXOLhs9GwJ5bM2A== X-Google-Smtp-Source: ABdhPJzl5g7SCdzOiQW0AKBLd54FztTafsaGnDmwHpm4+zy8T/pEFhSPISC70f7HE2KWzniLOcvFTA== X-Received: by 2002:a63:f815:: with SMTP id n21mr5942108pgh.410.1601715325776; Sat, 03 Oct 2020 01:55:25 -0700 (PDT) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id a15sm4566374pgi.69.2020.10.03.01.55.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Oct 2020 01:55:25 -0700 (PDT) From: Hangbin Liu To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Song Liu , Yonghong Song , Andrii Nakryiko , Andrii Nakryiko , netdev@vger.kernel.org, =?utf-8?b?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vu?= =?utf-8?q?sen?= , Hangbin Liu Subject: [PATCHv2 bpf 2/3] libbpf: check if pin_path was set even map fd exist Date: Sat, 3 Oct 2020 16:55:04 +0800 Message-Id: <20201003085505.3388332-3-liuhangbin@gmail.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201003085505.3388332-1-liuhangbin@gmail.com> References: <20201002075750.1978298-1-liuhangbin@gmail.com> <20201003085505.3388332-1-liuhangbin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Say a user reuse map fd after creating a map manually and set the pin_path, then load the object via libbpf. In libbpf bpf_object__create_maps(), bpf_object__reuse_map() will return 0 if there is no pinned map in map->pin_path. Then after checking if map fd exist, we should also check if pin_path was set and do bpf_map__pin() instead of continue the loop. Fix it by creating map if fd not exist and continue checking pin_path after that. v2: keep if condition with existing order Suggested-by: Andrii Nakryiko Signed-off-by: Hangbin Liu Acked-by: Andrii Nakryiko --- tools/lib/bpf/libbpf.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index c8b5fe45682d..1fb3fd733b23 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -4245,29 +4245,28 @@ bpf_object__create_maps(struct bpf_object *obj) if (map->fd >= 0) { pr_debug("map '%s': skipping creation (preset fd=%d)\n", map->name, map->fd); - continue; - } - - err = bpf_object__create_map(obj, map); - if (err) - goto err_out; + } else { + err = bpf_object__create_map(obj, map); + if (err) + goto err_out; - pr_debug("map '%s': created successfully, fd=%d\n", map->name, - map->fd); + pr_debug("map '%s': created successfully, fd=%d\n", + map->name, map->fd); - if (bpf_map__is_internal(map)) { - err = bpf_object__populate_internal_map(obj, map); - if (err < 0) { - zclose(map->fd); - goto err_out; + if (bpf_map__is_internal(map)) { + err = bpf_object__populate_internal_map(obj, map); + if (err < 0) { + zclose(map->fd); + goto err_out; + } } - } - if (map->init_slots_sz) { - err = init_map_slots(map); - if (err < 0) { - zclose(map->fd); - goto err_out; + if (map->init_slots_sz) { + err = init_map_slots(map); + if (err < 0) { + zclose(map->fd); + goto err_out; + } } } From patchwork Sat Oct 3 08:55:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 267644 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 561D5C4727D for ; Sat, 3 Oct 2020 08:55:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 12DC0206DB for ; Sat, 3 Oct 2020 08:55:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JOzOz+1I" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725835AbgJCIze (ORCPT ); Sat, 3 Oct 2020 04:55:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725781AbgJCIza (ORCPT ); Sat, 3 Oct 2020 04:55:30 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 004E1C0613D0; Sat, 3 Oct 2020 01:55:29 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id x2so2427683pjk.0; Sat, 03 Oct 2020 01:55:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nuGVlYXVa1ADtayePQ599HBG7S1v+QC4V52l2CtSHRg=; b=JOzOz+1IBVA+4o8aa42xbdQQGYjAeZ64RH9FdmpHdTH1XvcxXJ5dYx8eZmYxZdpbIw S8Fb2Qg/KNrAeSeSAbAFEH1jCewTP+JudjXpDmV8HZv/tCl1mLkQg+/khQxtcL0qo8hg EjvB0jSKZbayFrmSqf9IZNIZcsjToo8MDPYahU91ovXJXKLddr/Gse8znhQaRBWx7bPN NA78+EQhLG4P8I/huWAY1A+P3nK3SEV6Mw6DErKhwyTj9A3C5z+T56jC8DAeuOX5PncK J18Cm3ijTnlv4+eihrHkYbFc7NtkCBP4ekJ7VKCFuOMbY+CMFFq2Xa5u5DTLeKu9oEF0 2oBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nuGVlYXVa1ADtayePQ599HBG7S1v+QC4V52l2CtSHRg=; b=btPkymV8UUnQu2fU8qSHby1lfVtqiOMyQjU8F8j7AzRrKKuqnwP1Jh9Lg3WJkeMcXu A9UoZLSQuT+1vUzAUBVAGz6XN6tnXNa9tZ4jbne070zOpTEzxwVaSm2Pp/3+nkjTN9CF 1cLvq1nBhRKcRpvI2ELpND4djQwBDyHHdogbUDPgKnkS2XcxdJtm5gftctyZKtNMCRU4 kYciv2ma7A9xvmPuGe0axO/kGJvI7klynl3cMI3NxaPZ/LQhji3D/Yz8dW2iMvSIRarX yIyhnUsDlD/mW4/U4j9F4h5G/E0f2TDdmNJ1lFF7h29XyPQaJiWfI+s+rFtsSbmeBiqv HMAQ== X-Gm-Message-State: AOAM532cnojJRxx74NdmYBRkHXU4zOPm2Uu1YMPrCKqmQnTmG1a3axnr RwG3TA/DXk3GPv9JTNViMHVDL4j0081q/w== X-Google-Smtp-Source: ABdhPJyuKtdm+fyQDBh92MQpMN97ZInscOQN+Q12U44h7Gs+f0llwwDuwP8WdxXyOuNpkNXTGKyRog== X-Received: by 2002:a17:90a:71c3:: with SMTP id m3mr7043719pjs.68.1601715329314; Sat, 03 Oct 2020 01:55:29 -0700 (PDT) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id a15sm4566374pgi.69.2020.10.03.01.55.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Oct 2020 01:55:28 -0700 (PDT) From: Hangbin Liu To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Song Liu , Yonghong Song , Andrii Nakryiko , Andrii Nakryiko , netdev@vger.kernel.org, =?utf-8?b?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vu?= =?utf-8?q?sen?= , Hangbin Liu Subject: [PATCHv2 bpf 3/3] selftest/bpf: test pinning map with reused map fd Date: Sat, 3 Oct 2020 16:55:05 +0800 Message-Id: <20201003085505.3388332-4-liuhangbin@gmail.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201003085505.3388332-1-liuhangbin@gmail.com> References: <20201002075750.1978298-1-liuhangbin@gmail.com> <20201003085505.3388332-1-liuhangbin@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This add a test to make sure that we can still pin maps with reused map fd. Signed-off-by: Hangbin Liu --- .../selftests/bpf/prog_tests/pinning.c | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/pinning.c b/tools/testing/selftests/bpf/prog_tests/pinning.c index 041952524c55..299f99ef92b2 100644 --- a/tools/testing/selftests/bpf/prog_tests/pinning.c +++ b/tools/testing/selftests/bpf/prog_tests/pinning.c @@ -37,7 +37,7 @@ void test_pinning(void) struct stat statbuf = {}; struct bpf_object *obj; struct bpf_map *map; - int err; + int err, map_fd; DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, .pin_root_path = custpath, ); @@ -213,6 +213,50 @@ void test_pinning(void) if (CHECK(err, "stat custpinpath", "err %d errno %d\n", err, errno)) goto out; + /* remove the custom pin path to re-test it with reuse fd below */ + err = unlink(custpinpath); + if (CHECK(err, "unlink custpinpath", "err %d errno %d\n", err, errno)) + goto out; + + err = rmdir(custpath); + if (CHECK(err, "rmdir custpindir", "err %d errno %d\n", err, errno)) + goto out; + + bpf_object__close(obj); + + /* test pinning at custom path with reuse fd */ + obj = bpf_object__open_file(file, NULL); + if (CHECK_FAIL(libbpf_get_error(obj))) { + obj = NULL; + goto out; + } + + map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(__u32), + sizeof(__u64), 1, 0); + if (CHECK(map_fd < 0, "create pinmap manually", "fd %d\n", map_fd)) + goto out; + + map = bpf_object__find_map_by_name(obj, "pinmap"); + if (CHECK(!map, "find map", "NULL map")) + goto out; + + err = bpf_map__reuse_fd(map, map_fd); + if (CHECK(err, "reuse pinmap fd", "err %d errno %d\n", err, errno)) + goto out; + + err = bpf_map__set_pin_path(map, custpinpath); + if (CHECK(err, "set pin path", "err %d errno %d\n", err, errno)) + goto out; + + err = bpf_object__load(obj); + if (CHECK(err, "custom load", "err %d errno %d\n", err, errno)) + goto out; + + /* check that pinmap was pinned at the custom path */ + err = stat(custpinpath, &statbuf); + if (CHECK(err, "stat custpinpath", "err %d errno %d\n", err, errno)) + goto out; + out: unlink(pinpath); unlink(nopinpath);