Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp193095imw; Wed, 13 Jul 2022 22:55:31 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v/b7yRFHYbuSd/D9g5snEjr0TN9ufCx1DMGki1XytGf1WscPG5/e2vTJ5osiJIgBHaKqx3 X-Received: by 2002:a63:eb50:0:b0:40d:c602:98b0 with SMTP id b16-20020a63eb50000000b0040dc60298b0mr6107348pgk.81.1657778131588; Wed, 13 Jul 2022 22:55:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657778131; cv=none; d=google.com; s=arc-20160816; b=JBY+28kPHFH126g8g50ONFSdJVcBAYH16Z3/P4HhhNFe64fmlzgURVs9lIT676Ltwp AXN+OWn6vbAhAhhMWbzAmB2NBozK6wFYCbp1S0eZS0xmPbMbDHhPoB309mXYdvbci6Vy Dn/T28MMunFZrvaBtztFvkXYtt18meYstkF1/v73agqD+N7oUj/pH4ND69cJcox7A9qz IZNw5eZ1dVLsOh6ekX0MBUQsmGJwPIfutW8m0MeC22kAk9k2S3fU49bXlXc1mnowZR3Y 5lraXaXDPna2jQdsx0AHyMueRF3UhIVI3SebxVpfvkEqKEmvJyUmEax70kaTldtwbnnS vl0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=8jR4w3foBIfwhXpMfecmUsdb0NEz+AG5RBtyTM8Ba6I=; b=sEZMw5C6BQ6J27PBCDyKNY5Z1D0/VGAiOHcK+/iWV2YXLMZwG0V3wFdC0IwMz0bJUD uihG1gJY1dFHrb1NGVrJzEqT8kWGhwm7ZFgx1m3PZtERLULVetN91vq1uS+Msv9YPTQR Ucr8RBg7P46yNHgAnq9qFEJmPJeVi2mbD9XlutH3mZ84mcAcRWpyxd5ovjiyOPdBopQ+ bUF2GBZwyezbZFAuwfJAPXbMapfsy+/8bysDP9EwGlrW3L7DqO4T9DINEOH273aCKEgl 1lGaZhZPSxW+S5jgAy3u05jpwnl0q0elvIyVtbM3ha+XkO92uP+4AK1UAp3Wa95FTIXL Ovpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="lJw33/Rg"; 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 1-20020a630401000000b0040d20139d09si771174pge.243.2022.07.13.22.55.19; Wed, 13 Jul 2022 22:55:31 -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="lJw33/Rg"; 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 S233796AbiGNFYQ (ORCPT + 99 others); Thu, 14 Jul 2022 01:24:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231377AbiGNFYO (ORCPT ); Thu, 14 Jul 2022 01:24:14 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76A81B863; Wed, 13 Jul 2022 22:24:13 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id y4so991598edc.4; Wed, 13 Jul 2022 22:24:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=8jR4w3foBIfwhXpMfecmUsdb0NEz+AG5RBtyTM8Ba6I=; b=lJw33/RgWTAnQsn6ke/Dq0bb5lWGWWtszw1wVY596NJuORD+vgks4YHDnJCTjpgyNi yX69SFQO5O8NVq55A2pR+Lh2bAIu+YOz4HV1fOMLCOc4Ua7MCYFk2zy/cM9FbOMLiLmN bpz25dmpG23dbvNeay8kNVQ8UyHWZGE95nTeRN8bA8xXjswy+2+bGU/VvFaf1boiIC2e hmRgyoWAyz0LME83euDf2o3z78m2YGQ7//i37TsCUcbP9ymQYT0F1JyEjUmAiwqUXGaa 43v1IJjp+LgEx7X/5fOhv4k8CIvIv3n47M5bw6BdOqNJXHZ5TnTrhZrXPDhzDbF99ye/ e61w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=8jR4w3foBIfwhXpMfecmUsdb0NEz+AG5RBtyTM8Ba6I=; b=XejWOOUGju5Epx42K2uQbOYvdbZjli80sJvvnzRSJG7vL1Ea5LY/2wVim0/9uCE8RU owkUm8R9h2ls+FsulevFROL9Nz3YEnW3Vio8UnIEU/lD6csZC1he2F9GTHQcq1rb2vfe vBaDwvaqAKu8Y4QFUE0RH4I9RYFqyV8fi8P4hsO7hByiJaPXdaDL2i+MC25+yQbHtkzh W+6aGuLxJAjH4guDIkTP+eJFAYxcAx3bqul1OgnqI81cHCJ4IrY1XhfmUx8WpUPPvuaL iZhluBWQavuLFNIzuuAl8wACAil99JELOCCnBpCmfVBdKJTGzzB5vmdoqghJfBhSniD0 k/sA== X-Gm-Message-State: AJIora9rsifm/43k1jl6rg4cLjZPuPaWsHv47+2BtCkI9EVpc1EneEQM 8125+iLgiIK5aPc2de0+nWlN8jSGsI2AePdiTic= X-Received: by 2002:a05:6402:1c01:b0:43a:f714:bcbe with SMTP id ck1-20020a0564021c0100b0043af714bcbemr10167036edb.14.1657776251991; Wed, 13 Jul 2022 22:24:11 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Andrii Nakryiko Date: Wed, 13 Jul 2022 22:24:00 -0700 Message-ID: Subject: Re: [PATCH v2] libbpf: fix the name of a reused map To: Anquan Wu Cc: Jiri Olsa , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , bpf , open list Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 On Tue, Jul 12, 2022 at 10:59 PM Anquan Wu wrote: > > On Tue, 2022-07-12 at 10:31 +0200, Jiri Olsa wrote: > > On Tue, Jul 12, 2022 at 11:15:40AM +0800, Anquan Wu wrote: > > > BPF map name is limited to BPF_OBJ_NAME_LEN. > > > A map name is defined as being longer than BPF_OBJ_NAME_LEN, > > > it will be truncated to BPF_OBJ_NAME_LEN when a userspace program > > > calls libbpf to create the map. A pinned map also generates a path > > > in the /sys. If the previous program wanted to reuse the map=EF=BC=8C > > > it can not get bpf_map by name, because the name of the map is only > > > partially the same as the name which get from pinned path. > > > > > > The syscall information below show that map name > > > "process_pinned_map" > > > is truncated to "process_pinned_". > > > > > > bpf(BPF_OBJ_GET, {pathname=3D"/sys/fs/bpf/process_pinned_map", > > > bpf_fd=3D0, file_flags=3D0}, 144) =3D -1 ENOENT (No such file or > > > directory) > > > > > > bpf(BPF_MAP_CREATE, {map_type=3DBPF_MAP_TYPE_HASH, key_size=3D4, > > > value_size=3D4,max_entries=3D1024, map_flags=3D0, inner_map_fd=3D= 0, > > > map_name=3D"process_pinned_",map_ifindex=3D0, btf_fd=3D3, > > > btf_key_type_id=3D6, > > > btf_value_type_id=3D10,btf_vmlinux_value_type_id=3D0}, 72) =3D 4 > > > > > > This patch check that if the name of pinned map are the same as the > > > actual name for the first (BPF_OBJ_NAME_LEN - 1), > > > bpf map still uses the name which is included in bpf object. > > > > > > Signed-off-by: Anquan Wu > > > --- > > > > > > v2: compare against zero explicitly > > > > > > v1: > > > https://lore.kernel.org/linux-kernel/OSZP286MB1725A2361FA2EE8432C4D5F= 4B8879@OSZP286MB1725.JPNP286.PROD.OUTLOOK.COM/ > > > --- > > > tools/lib/bpf/libbpf.c | 8 +++++++- > > > 1 file changed, 7 insertions(+), 1 deletion(-) > > > > > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > > > index e89cc9c885b3..7b4d3604dfb4 100644 > > > --- a/tools/lib/bpf/libbpf.c > > > +++ b/tools/lib/bpf/libbpf.c > > > @@ -4328,6 +4328,7 @@ int bpf_map__reuse_fd(struct bpf_map *map, > > > int > > > fd) > > > { > > > struct bpf_map_info info =3D {}; > > > __u32 len =3D sizeof(info); > > > + __u32 name_len; > > > int new_fd, err; > > > char *new_name; > > > > > > @@ -4337,7 +4338,12 @@ int bpf_map__reuse_fd(struct bpf_map *map, > > > int > > > fd) > > > if (err) > > > return libbpf_err(err); > > > > > > - new_name =3D strdup(info.name); > > > + name_len =3D strlen(info.name); > > > + if (name_len =3D=3D BPF_OBJ_NAME_LEN - 1 && strncmp(map->name= , > > > info.name, name_len) =3D=3D 0) > > > > so what if the map->name is different after 'name_len' ? > > > > jirka > > > > If A map name is defined as being longer than name_len (name_len is > "BPF_OBJ_NAME_LEN - 1" in this context), a program will fail to get a > reused bpf_map by bpf_object__find_map_by_name(). > > fromhttps://github.com/libbpf/libbpf/blob/master/src/libbpf.c#L9295, > pos->name in bpf_object__find_map_by_name() is from new_name > in > bpf_map_reuse_fd(). It can not find map by the name which is defined > in bpf object. > > I wrote some code to verify this problem and test the solution > mentioned above. > Link: https://github.com/leiqi96/libbpf-fix > It would be great to have something like this as a selftest, please send a follow up patch adding a test under selftests/bpf for map reuse. See prog_tests/pinning.c, this might belong there. To also answer Jiri's question. This is not an ideal solution, but it improves the current situation. And while potentially it's not 100% correct (because only checks first 15 characters), user normally would use bpf_map__reuse_fd() on well-known and presumably correct map, so chance of misuse here are pretty minimal. So I added Fixes: 26736eb9a483 ("tools: libbpf: allow map reuse") and applied to bpf-next, thanks. > Anquan > > > > > + new_name =3D strdup(map->name); > > > + else > > > + new_name =3D strdup(info.name); > > > + > > > if (!new_name) > > > return libbpf_err(-errno); > > > > > > -- > > > 2.32.0 > > > > > >