Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp1170102imw; Wed, 13 Jul 2022 15:07:23 -0700 (PDT) X-Google-Smtp-Source: AGRyM1shCXfBgPOI5ZfnT7IuRZvMqyByiJnq8KnMr51IkybKnJp8cffeN4cLB9tUwWPddA7fmI8A X-Received: by 2002:a17:903:44b:b0:16b:ec58:3727 with SMTP id iw11-20020a170903044b00b0016bec583727mr4941688plb.171.1657750042896; Wed, 13 Jul 2022 15:07:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657750042; cv=none; d=google.com; s=arc-20160816; b=SKSn8blCWagC/9E2UaO9SQACHnAfLqVpmXrJxEz1vuzWEd80EhUOekuad69N0jskZ7 Xl4LqAl6GIL+9itu53MxQlu0HD24sdfcS2xg/LLVi8p8EGsF/88U9/0giOAPYl8+ChRJ VoDvjZDYxqctLaAvGCjL7bwUiz0Waf07HKDkQz6Vwaf3MF4VIAXQWqTb3CO5A3TXvnPg WodRa+2Z1yGjai2hQdolSkE0Gd5DKLg3tkYAKOoz+FTv27KGAxF0oSE+LEsG6s4P2cf/ w9+zO7qfa3LgWyyJy8VaucBXMBgj24yNds95QRokXqAAWD+re5L4pNzgxp1BMkFQyUoF 0ecw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=f+NhKyzppDv/2IvxpOEHkasVtdb6OH3y3PQ0MVNgHPs=; b=dPciAH6WVihkqt0kplPalAM5ihzCU9hpdZED44sNx0hl6mESZG8fNyhH3/QwT+02YB V6+lnN02ZdlP+oN8AddvWqUWjZ9J7Ai3W5+F3jwxBUBeqWQSNRmJrMzgI62BBrJ9dZHS pW7k7kPnWxoBPGYV4GmDy+GhCdcyq6rPF4jmofHpbpu1sGJQIrLW91XN+lpOTkLhH8Vo CCjIZtV3oakty4gDuByGRnLMvJ31cC8BJ5v/91p8c333hLkzN+kW1SO/j8d7OGnLWXmV mBz9684oEcMUVCKts7qlndSAboS98AK+a0Y1WvhZqMfTDphPov1tfmhwS4aA4lVtcHxH XhPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=o0uaAhII; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r184-20020a632bc1000000b00414ec29c59esi18505661pgr.426.2022.07.13.15.06.50; Wed, 13 Jul 2022 15:07:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=o0uaAhII; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236862AbiGMVnD (ORCPT + 99 others); Wed, 13 Jul 2022 17:43:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231860AbiGMVnB (ORCPT ); Wed, 13 Jul 2022 17:43:01 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B0C133E15; Wed, 13 Jul 2022 14:43:01 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id e16so120801pfm.11; Wed, 13 Jul 2022 14:43:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=f+NhKyzppDv/2IvxpOEHkasVtdb6OH3y3PQ0MVNgHPs=; b=o0uaAhIIC6tyC6F2t0kp91snxL/aTvAzHKrsGKgA8zyNxnYKajXsAPMWy6VrZgMJJS Rscx0NsOWvkF8BM//TFwdfrGZZQyrZsVPLDoSLnfk+lCzht1IqNgCB6DgbORabzMYx5q PlLSshUGgK8y+ApDX5Am5LgYg9n99byIRXvrL7tUeKqLU3SuAB/up4iORhFR+4JRNnv0 SPTQV6Z8KKN0Yxu3sSWxWOCkwOEBEODvn/a0S7SNXC9jB3at9o+GBMlbqvqGbt7utssb Ny4tLEBFI8wCZd0Tr9WidLYwjBP19Cg44jBnTBLYDSiuuzXSSIbwOMjargW2kY5ASbUD NceQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=f+NhKyzppDv/2IvxpOEHkasVtdb6OH3y3PQ0MVNgHPs=; b=NQiSlDQNEE20pNjNZdRYoPDA36M07fXu33qdhuwgvPYJmup23swxe/0mXvt2ef0wKJ BT00o6nYmsU+ypFYBlaKwHYYR/YbHoAM30045tKbPd8x0b6/7Gu0nzMbE1ax+lTr9RQg D27K6Ow0IAcMATWTejBco/X529intZT9kEBx3XJAl95wNt80+3rUAmm/3B/XejgNohMk 0UhIhvE85VsDKttCPQuAsBEvAY3bzNB5KV8YpYLGD7u85egKeYdN2XVpemjqAC4huyGX rsIL1iw1kGYWHpD36B8Db5EaQ//fb17VyHXu4nmJxFtjjxoMfNRZqG2qEOquySNSHu1/ vhnQ== X-Gm-Message-State: AJIora9TYe6jESqHaD00vjkrfGR0ol8ZB2WHSMOqQssGMhLzxzoA+YYa rBF58iFAyRZ3iwZW24cHgA== X-Received: by 2002:a63:1220:0:b0:411:f661:f819 with SMTP id h32-20020a631220000000b00411f661f819mr4643273pgl.250.1657748580523; Wed, 13 Jul 2022 14:43:00 -0700 (PDT) Received: from jevburton3.c.googlers.com.com (223.103.125.34.bc.googleusercontent.com. [34.125.103.223]) by smtp.gmail.com with ESMTPSA id e29-20020aa7981d000000b0052ab54a4711sm1194pfl.150.2022.07.13.14.42.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Jul 2022 14:43:00 -0700 (PDT) From: Joe Burton To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Joe Burton Subject: [PATCH bpf-next] libbpf: Add bpf_map__set_name() Date: Wed, 13 Jul 2022 21:42:46 +0000 Message-Id: <20220713214246.2545204-1-jevburton.kernel@gmail.com> X-Mailer: git-send-email 2.37.0.144.g8ac04bfd2-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joe Burton Add the capability to set a `struct bpf_map` name. bpf_map__reuse_fd(struct bpf_map *map, int fd) does the following: 1. get the bpf_map_info of the passed-in fd 2. strdup the name from the bpf_map_info 3. assign that name to the map 4. and some other stuff While `map.name` may initially be arbitrarily long, this operation truncates it after 15 characters. We have some infrastructure that uses bpf_map__reuse_fd() to preserve maps across upgrades. Some of our users have long map names, and are seeing their maps 'disappear' after an upgrade, due to the name truncation. By invoking `bpf_map__set_name()` after `bpf_map__reuse_fd()`, we can trivially work around the issue. Signed-off-by: Joe Burton --- tools/lib/bpf/libbpf.c | 22 ++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 72548798126b..725baf508e6f 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9089,6 +9089,28 @@ const char *bpf_map__name(const struct bpf_map *map) return map->name; } +int bpf_map__set_name(struct bpf_map *map, const char *name) +{ + char *new_name; + + if (!map) + return libbpf_err(-EINVAL); + + new_name = strdup(name); + if (!new_name) + return libbpf_err(-ENOMEM); + + if (map_uses_real_name(map)) { + free(map->real_name); + map->real_name = new_name; + } else { + free(map->name); + map->name = new_name; + } + + return 0; +} + 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 e4d5353f757b..e898c4cb514a 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -731,8 +731,9 @@ LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map); */ LIBBPF_API int bpf_map__fd(const struct bpf_map *map); LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd); -/* get map name */ +/* get/set map name */ LIBBPF_API const char *bpf_map__name(const struct bpf_map *map); +LIBBPF_API int bpf_map__set_name(struct bpf_map *map, const char *name); /* 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); -- 2.37.0.144.g8ac04bfd2-goog