From patchwork Tue Oct 6 02:13:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 1377182 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=OWc3/QOk; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4C51Jf2N8dz9sSG for ; Tue, 6 Oct 2020 13:15:14 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726398AbgJFCPM (ORCPT ); Mon, 5 Oct 2020 22:15:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725870AbgJFCPL (ORCPT ); Mon, 5 Oct 2020 22:15:11 -0400 Received: from mail-pl1-x642.google.com (mail-pl1-x642.google.com [IPv6:2607:f8b0:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8694DC0613CE; Mon, 5 Oct 2020 19:15:10 -0700 (PDT) Received: by mail-pl1-x642.google.com with SMTP id h2so458486pll.11; Mon, 05 Oct 2020 19:15:10 -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=hWqIIaC1uC7NQxhIyCinlsHBDl2A45Ctu7K6Zjp49H0=; b=OWc3/QOkkYktSNZ2pRDUDfPE6Ri9ODmkc/uR6Jp2a/hRlIBwDuBiG7HnqAjvHvEZeN NwIxADhInRTNpYCsuJKfMci9YD47HRI4oq7knjkQbyOJXjMS4YXBUF9nmODjKnZq+xVd rYiAG5UfNPo7p0dfcPIXhSzOkeA6Rczgq+MXNFUfp3J9d+mGBRnSD0gkzjIYf4XRPoh1 B9msu0GZmopCeIEK6ihc4EbSDnh/g4L9pVTnDWu+VKB/GEUMiDZwIUwfTnZqVDfGhYeF VGDJklx4eo3Qnzi/wi2OcG4I4dLISIfCslbswC7lokGJowjKtHkES+HaPqr0C6zPLNtr tjmw== 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=hWqIIaC1uC7NQxhIyCinlsHBDl2A45Ctu7K6Zjp49H0=; b=ErXXZpl8QWPbZncyYTrSGuE7mYQhRvec3+UpnFbi/mk0tzIsX75iSsOHDFxtDvY5N2 G9CNCnUhb0p57rAEP1FtspqlrANI5xyErhm6cagDHDjK7HCSb4pici7WrHVOfEV4WtdF 4XLmuKPI+wOA7TmRSbYseJKwqVA+nI/75AinYMXPBKhWq5hARp1y/mxa7n7b2+zaL7Dx b1COeJIroBa3jyCsUMw/geDyWpEnq5BvEStJ+MhCjj22tVV4cVqBM58IhRUm156xkkmJ WbokWyH6tfx0q4LYOif1MwIXyOuVFxWYLp35gqfDn3gDWW4jgkXdXWcTVzfI/wK52O1R gn+A== X-Gm-Message-State: AOAM533ocViHumihFPoKaISyeSqUOt5+rfbdDu9RIjktaW5JgAMajz5B obb/bz1zDsC9mr/28Fu3aafyr36TqhUU2g== X-Google-Smtp-Source: ABdhPJz8D133SuKeC3YTXaO86NM+xqpsvM03Y4KoZ9zqjgBeoBVALkqEDrMWFCKPxdD9zA01SRrGZQ== X-Received: by 2002:a17:90a:634b:: with SMTP id v11mr1552479pjs.108.1601950509897; Mon, 05 Oct 2020 19:15:09 -0700 (PDT) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id k9sm1301594pfc.96.2020.10.05.19.15.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Oct 2020 19:15:09 -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?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= , Hangbin Liu , Andrii Nakryiko Subject: [PATCHv3 bpf 1/3] libbpf: close map fd if init map slots failed Date: Tue, 6 Oct 2020 10:13:43 +0800 Message-Id: <20201006021345.3817033-2-liuhangbin@gmail.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201006021345.3817033-1-liuhangbin@gmail.com> References: <20201003085505.3388332-1-liuhangbin@gmail.com> <20201006021345.3817033-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 Acked-by: Andrii Nakryiko Signed-off-by: Hangbin Liu --- 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 Tue Oct 6 02:13:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 1377183 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=FHD1cRk8; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4C51Jj2bnJz9sSG for ; Tue, 6 Oct 2020 13:15:17 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726603AbgJFCPP (ORCPT ); Mon, 5 Oct 2020 22:15:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725870AbgJFCPO (ORCPT ); Mon, 5 Oct 2020 22:15:14 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 680C5C0613CE; Mon, 5 Oct 2020 19:15:14 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id j33so411852pgj.5; Mon, 05 Oct 2020 19:15:14 -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=F6uCDwHT15MZwocQuEWQL17rLGGSpUMgqsQxnevkueU=; b=FHD1cRk8WIrgJ7kDJOju7ENsyyax/WM/f6YFP2Dlo/5F/B+p8Nz6n8jifETv2Ar6AU gupBn1DxQDgIapkXq05fdGYrwTS8lyNcL7iRlPVU4HqjW4xUNx1cNtuLUs6jmZ737uI2 Jr31Np+zRq9In1VZsXKP1Lx1KzAAhZnCaNmXGxQmKsO53mBZCHhQjKkVVP9tP8c8g+5P zsRoWMP+prKGfyXW76V0YN72RWwj63HLDyrm6s16X60yyR7sTdrE/aGOmQulKJrvfWag 7IoqvW8aOL5Wl65wtVPYj3i38l+SuP5DoEkf/i75Isjio2w0m3pQDNZSM5yYifh6WfHJ dMwQ== 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=F6uCDwHT15MZwocQuEWQL17rLGGSpUMgqsQxnevkueU=; b=E6PV+8akRY5gJh2I/5vgNpdZIkQXAwlV2OjEiFlv+3jY1Dr79TQXaieQOhSY8RWiOy cKudQ7U+fsU89j7IRmEipjMcROEp/wPlTCWNujq/GG3ZfkF9dN/prhUDxh+Cu5+1sg5/ UcUTUjDgW0cvisJD2pxhb3yHWnAALWxZaT3z0yyDZ0SGVkIyTt10Gef2VzR5VEZrilKO 82BaHhWrEtB25G+qSCIPPiurqY7mo02z7RZYKS1ACUhXoOVWAt/ww40ghr0l2lV6Dwoa FDlPkWBsF4TelB+PcGmiYpbl+g9o6zBtQQuQyNu9iaS978W1ZtalJJaSBhLa6aETN5ms iJ9w== X-Gm-Message-State: AOAM533LVYUKRKs/0frztuNbP1pTYalyhgDxcBW9TpDqv6kcg5aiCEYX NvT/P5v7p9TUFIgGxTQFPNAs5qw8tZpX2Q== X-Google-Smtp-Source: ABdhPJxDmuKQEZqYhtFjfk3d2M0UhP6vvLOmUAkGI3imSZPfP5ASqahm+5nPrWRsjcjYEFf3qGx8bA== X-Received: by 2002:a63:4c1d:: with SMTP id z29mr2112670pga.203.1601950513759; Mon, 05 Oct 2020 19:15:13 -0700 (PDT) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id k9sm1301594pfc.96.2020.10.05.19.15.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Oct 2020 19:15:13 -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?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= , Hangbin Liu , Andrii Nakryiko Subject: [PATCHv3 bpf 2/3] libbpf: check if pin_path was set even map fd exist Date: Tue, 6 Oct 2020 10:13:44 +0800 Message-Id: <20201006021345.3817033-3-liuhangbin@gmail.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201006021345.3817033-1-liuhangbin@gmail.com> References: <20201003085505.3388332-1-liuhangbin@gmail.com> <20201006021345.3817033-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. Suggested-by: Andrii Nakryiko Acked-by: Andrii Nakryiko Signed-off-by: Hangbin Liu --- v3: no update v2: keep if condition with existing order --- 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 Tue Oct 6 02:13:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hangbin Liu X-Patchwork-Id: 1377184 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=dQwPIdQ2; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4C51Jn6Dtcz9sSG for ; Tue, 6 Oct 2020 13:15:21 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726667AbgJFCPU (ORCPT ); Mon, 5 Oct 2020 22:15:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725870AbgJFCPT (ORCPT ); Mon, 5 Oct 2020 22:15:19 -0400 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2787C0613CE; Mon, 5 Oct 2020 19:15:17 -0700 (PDT) Received: by mail-pf1-x442.google.com with SMTP id k8so8138036pfk.2; Mon, 05 Oct 2020 19:15:17 -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=fjDPfGjNC204PQtu2+64xYwVRdnbGQEEQskQTfu5T04=; b=dQwPIdQ2ndtgszpYBf4IoUyUQV1zhLG7bZxgH37BSA53S6lpk7nhvHP0Z2+U43xlil q9cSO2MqlBtb8szTXRqhU50TGNA4mq5Ck5XsIgZDq/6ohUYuJs25NItdKScQngNzRm6l 51ZgCVa6lags3NtlMNfFO3jCR/K/oYMubCOb2SfkaoU+RyCzYVXTtdm2XAqvPr0Xyr/b tysLcBYG+KOz20MjhOCw+9UNCJN0qwsUTMhEhgyq0/E/3tlTFEKUnDPiATlFSMHggMYW IAafc6P6FZuaJJOWg6hcYi4Ae3OgN4A58H7yac6hYs7JygHfIqJ2E+1NSJXUKQxc7qkz VgcQ== 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=fjDPfGjNC204PQtu2+64xYwVRdnbGQEEQskQTfu5T04=; b=e1Hl2heKmxDsAx9n1iG8O6Wxv0Rf/ghSKrigqD8dkWSiZtRBeorbXghiE4q50U/6NI xfBUmGeIc8jBCQ4uk0l26y3254Lq9HLrkd6N500pXJrYC4fn8ccqv1P4Ssi80m0MqqBe nfFaLEBgHDbeBz7vw5TlIRWOYef6N2wlB9cRzNL3guoq4pk9bEGpDTnnhtvvuYFsOzXd /8x/Ch4U8KxUtdus7IQ0DHicF3dLHINA7lWibBHqdt8DAZC9Y0kmA9Cv4jVikWWlQOpF n2FMd9/KVj0HNYenugve5q1efxRp6PfICt/y5YEVGT8X3cFVJYyPClCm+3v1FWS5Tchp FO6g== X-Gm-Message-State: AOAM532ttXr0RnU5pN1bi9GtEq3Fr5HPqL8pZiyEYd5jBHKpGDoP4u3N Vki/rizM4Khi6EiSIP26SxJ42VwYbKS3dQ== X-Google-Smtp-Source: ABdhPJwtehk5Fo+7nkr28NhgDns9FsdtnsaxgShGdv4+W7g6dZPauIIrtxN1dnehj4C2hErGqoVxww== X-Received: by 2002:a63:784:: with SMTP id 126mr2102981pgh.428.1601950517404; Mon, 05 Oct 2020 19:15:17 -0700 (PDT) Received: from localhost.localdomain.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id k9sm1301594pfc.96.2020.10.05.19.15.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Oct 2020 19:15:16 -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?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= , Hangbin Liu Subject: [PATCHv3 bpf 3/3] selftest/bpf: test pinning map with reused map fd Date: Tue, 6 Oct 2020 10:13:45 +0800 Message-Id: <20201006021345.3817033-4-liuhangbin@gmail.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201006021345.3817033-1-liuhangbin@gmail.com> References: <20201003085505.3388332-1-liuhangbin@gmail.com> <20201006021345.3817033-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 Acked-by: Andrii Nakryiko --- v3: use CHECK() for bpf_object__open_file() and close map fd on error v2: no update --- .../selftests/bpf/prog_tests/pinning.c | 49 ++++++++++++++++++- 1 file changed, 48 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..fcf54b3a1dd0 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,53 @@ 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); + err = libbpf_get_error(obj); + if (CHECK(err, "default open", "err %d errno %d\n", err, errno)) { + 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 close_map_fd; + + err = bpf_map__reuse_fd(map, map_fd); + if (CHECK(err, "reuse pinmap fd", "err %d errno %d\n", err, errno)) + goto close_map_fd; + + err = bpf_map__set_pin_path(map, custpinpath); + if (CHECK(err, "set pin path", "err %d errno %d\n", err, errno)) + goto close_map_fd; + + err = bpf_object__load(obj); + if (CHECK(err, "custom load", "err %d errno %d\n", err, errno)) + goto close_map_fd; + + /* 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 close_map_fd; + +close_map_fd: + close(map_fd); out: unlink(pinpath); unlink(nopinpath);