From patchwork Sun Sep 5 10:09:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hengqi Chen X-Patchwork-Id: 12476061 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 X-Spam-Level: X-Spam-Status: No, score=-20.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,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 45AE2C433EF for ; Sun, 5 Sep 2021 10:09:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2280860FE6 for ; Sun, 5 Sep 2021 10:09:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237640AbhIEKKV (ORCPT ); Sun, 5 Sep 2021 06:10:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237550AbhIEKKV (ORCPT ); Sun, 5 Sep 2021 06:10:21 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7793C061575 for ; Sun, 5 Sep 2021 03:09:18 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id u1so2184134plq.5 for ; Sun, 05 Sep 2021 03:09:18 -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=UhmPc9CscbDpYTWunzyrAHEC/4hOr33nyYt4zFFItCE=; b=D5CBdGdIEgzkKXk0Rlg1QTYknkIPhjfuXqQgR6nnL3aHOdE+znC+QTCamZqWb4d3CV MnhlkHKOqH+TJbcimuhhRbQdwyKOAWJ2Fsqs5v9P0eK4eqiPduH2yGAd+AgP1+/a7RS8 7hUnWsKb0X9AqF8qsFZnGoUJWa1xSJMZBDX40HWXxnmgEHFdLMnk5bbl0sGW+h2F+EJ+ 6QuHPr6Ua6xX1uSc4qLaed4QyEiJWJAyibOdFMxkoF8gPhE34e5iR/+muKvi7tqVLUcZ Kn7uwZQagSzqNXw8YfdLXOT9nJCyRxBjcUaosckf6quwmWzbbcIoTFhpVRGhef0lPqCs /eEw== 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:mime-version :content-transfer-encoding; bh=UhmPc9CscbDpYTWunzyrAHEC/4hOr33nyYt4zFFItCE=; b=UmxC5GxMcrQlhZ9jronbulyT5HkPZRE8W+c5iUrOf7h+UT3wuHZAYnbQwU9yWGGsfn TMsE/tVaxgb+WiTBXyHNeq8Oo5Tm+X9yFVZD0j3eUY7MkZBEPaeGHSRr/b34+WayIp5z nplw9vDJejTUF37VIeqngBI9+msRNsLGS2lfx4A766loZSOAPqFOWbp/8fW/bbx18hes ZlloTJ5Lr6XTc8XzYy4PXSBzfwFRIAq0palo37D8cYMBuiMNn0zI5ty7OpLYutYBPwV7 Gyryen+nJxmELSsM9xZozUu8BxdizZ0UTzn65ORtcZmgWGQ6CGrAAUjkpyr23nuZkGDR 9Rvw== X-Gm-Message-State: AOAM532fyK2cnjsSPApdJ3RkvlXHNOdgRaa1lpQe7mc1Ak+K/9c/ag3N T/CvHvbQZ5GpZ/+Lx9t0jCnJj6W90YE= X-Google-Smtp-Source: ABdhPJx+07dfKEXUT31BQNHcspS74kX/YTo18ubfVf/R31rD1RmSByDG//lPjl9ICGSddn4qDypOwQ== X-Received: by 2002:a17:902:74c3:b0:132:45eb:6e9c with SMTP id f3-20020a17090274c300b0013245eb6e9cmr6252286plt.72.1630836558089; Sun, 05 Sep 2021 03:09:18 -0700 (PDT) Received: from localhost.localdomain ([119.28.83.143]) by smtp.gmail.com with ESMTPSA id o6sm4443960pjk.4.2021.09.05.03.09.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Sep 2021 03:09:17 -0700 (PDT) From: Hengqi Chen To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, yhs@fb.com, john.fastabend@gmail.com, hengqi.chen@gmail.com Subject: [PATCH bpf-next 1/2] libbpf: Support uniform BTF-defined key/value specification across all BPF maps Date: Sun, 5 Sep 2021 18:09:13 +0800 Message-Id: <20210905100914.33007-1-hengqi.chen@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A bunch of BPF maps do not support specifying types for key and value. This is non-uniform and inconvenient[0]. Currently, libbpf uses a retry logic which removes BTF type IDs when BPF map creation failed. Instead of retrying, this commit recognizes those specialized map and removes BTF type IDs when creating BPF map. [0] Closes: https://github.com/libbpf/libbpf/issues/355 Signed-off-by: Hengqi Chen --- tools/lib/bpf/libbpf.c | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 88d8825fc6f6..7068c4d07337 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -4613,6 +4613,26 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b create_attr.inner_map_fd = map->inner_map_fd; } + if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || + def->type == BPF_MAP_TYPE_STACK_TRACE || + def->type == BPF_MAP_TYPE_CGROUP_ARRAY || + def->type == BPF_MAP_TYPE_ARRAY_OF_MAPS || + def->type == BPF_MAP_TYPE_HASH_OF_MAPS || + def->type == BPF_MAP_TYPE_DEVMAP || + def->type == BPF_MAP_TYPE_SOCKMAP || + def->type == BPF_MAP_TYPE_CPUMAP || + def->type == BPF_MAP_TYPE_XSKMAP || + def->type == BPF_MAP_TYPE_SOCKHASH || + def->type == BPF_MAP_TYPE_QUEUE || + def->type == BPF_MAP_TYPE_STACK || + def->type == BPF_MAP_TYPE_DEVMAP_HASH) { + create_attr.btf_fd = 0; + create_attr.btf_key_type_id = 0; + create_attr.btf_value_type_id = 0; + map->btf_key_type_id = 0; + map->btf_value_type_id = 0; + } + if (obj->gen_loader) { bpf_gen__map_create(obj->gen_loader, &create_attr, is_inner ? -1 : map - obj->maps); /* Pretend to have valid FD to pass various fd >= 0 checks. @@ -4622,21 +4642,6 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b } else { map->fd = bpf_create_map_xattr(&create_attr); } - if (map->fd < 0 && (create_attr.btf_key_type_id || - create_attr.btf_value_type_id)) { - char *cp, errmsg[STRERR_BUFSIZE]; - - err = -errno; - cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg)); - pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n", - map->name, cp, err); - create_attr.btf_fd = 0; - create_attr.btf_key_type_id = 0; - create_attr.btf_value_type_id = 0; - map->btf_key_type_id = 0; - map->btf_value_type_id = 0; - map->fd = bpf_create_map_xattr(&create_attr); - } err = map->fd < 0 ? -errno : 0; From patchwork Sun Sep 5 10:09:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hengqi Chen X-Patchwork-Id: 12476063 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 X-Spam-Level: X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, 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 AEE15C433FE for ; Sun, 5 Sep 2021 10:09:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8F76160F21 for ; Sun, 5 Sep 2021 10:09:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237662AbhIEKKX (ORCPT ); Sun, 5 Sep 2021 06:10:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237550AbhIEKKX (ORCPT ); Sun, 5 Sep 2021 06:10:23 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D47EC061575 for ; Sun, 5 Sep 2021 03:09:20 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id j1so2456353pjv.3 for ; Sun, 05 Sep 2021 03:09:20 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=dLiniIKer+yqpCrftVKNNMJg4bC6zOjvixu+pjUgVRY=; b=EdAiVQ1TDzZOqHrzcM67/InnLz/S1JDOK/0KQRy7kqqbSveVlqNR7uGvfwSSD9GvP7 YJJ2ICbfkxY3tQbG9xJyNzzG8Pym7p6Lq8eDlEyWqTHRkntYNYatcYNRGb8qsogcdnxg J6cNgaFBSIUcLKV7C+spuv1hnDXhnlzh04I+5NzgE0houqJePFkBsfrEuIG6IDh5brKH BdGb2COR8/Ge61qiaPeLj8Yzx8w6g+2a4z7acSO7o4qLj1pzuuwjHtfau5GqSWr4KOvU pZ9u7jtZ+YGObAu5bwBEqQSQuMfFEJ+QaZNaGyl5uDRH6i/0JsNwmM9/FuZPNtv/JRCk ii4w== 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=dLiniIKer+yqpCrftVKNNMJg4bC6zOjvixu+pjUgVRY=; b=f17rqCeqSr4URrRz9Jjc/YCcuVUu0l7/paMHbgl8QZ7s5kc/GkUCji/ByODjO6K7Xr vsW4jwMOsX9VG1APDFzr66z/pyYpnoqeAQrX3Gw6C/yegZCD5/G/h5rNNJNQo95eaPXP t+n1gqjJoLdJw82nQTYKPUWMXYPMQLJZWHp7ILW0JDpUeJuTeC+JggYdjQnE493nIQdX hCgOMKExeWAx6295LS25/L7lZFLtO8IPAENaWz4oXF05we6nCLdlt/d3Zc96g5uRKycC enOLBWVFFg9ut5uLJrbonWsADWzo7g1JW0f+hv+nuoRPw59bQ4ojWu9l9/EIzLcGRYnK PWhg== X-Gm-Message-State: AOAM53265L5S+zgA+xJGin8abscOD4tQN2PUTuoa5qdY0zWa38q0wN18 aIgZOvfdR5/vfNOjICRWvpH5PmU6RHQ= X-Google-Smtp-Source: ABdhPJx9c/Cz/x5xNgnILpeCRr8IgVr8kETEROF7lL9LtrRM9gHQQf6Mwnp3XvHSZZ6ka8UYax4zkQ== X-Received: by 2002:a17:90a:f285:: with SMTP id fs5mr8318848pjb.148.1630836559757; Sun, 05 Sep 2021 03:09:19 -0700 (PDT) Received: from localhost.localdomain ([119.28.83.143]) by smtp.gmail.com with ESMTPSA id o6sm4443960pjk.4.2021.09.05.03.09.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Sep 2021 03:09:19 -0700 (PDT) From: Hengqi Chen To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, yhs@fb.com, john.fastabend@gmail.com, hengqi.chen@gmail.com Subject: [PATCH bpf-next 2/2] selftests/bpf: Test BPF map creation using BTF-defined key/value Date: Sun, 5 Sep 2021 18:09:14 +0800 Message-Id: <20210905100914.33007-2-hengqi.chen@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210905100914.33007-1-hengqi.chen@gmail.com> References: <20210905100914.33007-1-hengqi.chen@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Test BPF map creation using BTF-defined key/value. The test defines some specialized maps by specifying BTF types for key/value and checks those maps are correctly initialized and loaded. Signed-off-by: Hengqi Chen --- .../selftests/bpf/prog_tests/map_create.c | 87 ++++++++++++++ .../selftests/bpf/progs/test_map_create.c | 110 ++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/map_create.c create mode 100644 tools/testing/selftests/bpf/progs/test_map_create.c diff --git a/tools/testing/selftests/bpf/prog_tests/map_create.c b/tools/testing/selftests/bpf/prog_tests/map_create.c new file mode 100644 index 000000000000..6ca32d0dffd2 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/map_create.c @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2021 Hengqi Chen */ + +#include +#include "test_map_create.skel.h" + +void test_map_create(void) +{ + struct test_map_create *skel; + int err, fd; + + skel = test_map_create__open(); + if (!ASSERT_OK_PTR(skel, "test_map_create__open failed")) + return; + + err = test_map_create__load(skel); + if (!ASSERT_OK(err, "test_map_create__load failed")) + goto cleanup; + + fd = bpf_map__fd(skel->maps.map1); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + + fd = bpf_map__fd(skel->maps.map2); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + + fd = bpf_map__fd(skel->maps.map3); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + + fd = bpf_map__fd(skel->maps.map4); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + + fd = bpf_map__fd(skel->maps.map5); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + + fd = bpf_map__fd(skel->maps.map6); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + + fd = bpf_map__fd(skel->maps.map7); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + + fd = bpf_map__fd(skel->maps.map8); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + + fd = bpf_map__fd(skel->maps.map9); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + + fd = bpf_map__fd(skel->maps.map10); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + + fd = bpf_map__fd(skel->maps.map11); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + + fd = bpf_map__fd(skel->maps.map12); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + + fd = bpf_map__fd(skel->maps.map13); + if (!ASSERT_GT(fd, 0, "bpf_map__fd failed")) + goto cleanup; + close(fd); + +cleanup: + test_map_create__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_map_create.c b/tools/testing/selftests/bpf/progs/test_map_create.c new file mode 100644 index 000000000000..2e9950e56b0f --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_map_create.c @@ -0,0 +1,110 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2021 Hengqi Chen */ + +#include "vmlinux.h" +#include + +#define MAX_ENTRIES 8 + +struct { + __uint(type,BPF_MAP_TYPE_PERF_EVENT_ARRAY); + __uint(max_entries, MAX_ENTRIES); + __type(key, int); + __type(value, int); +} map1 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_STACK_TRACE); + __uint(max_entries, MAX_ENTRIES); + __type(key, int); + __type(value, __u64); +} map2 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY); + __uint(max_entries, MAX_ENTRIES); + __type(key, int); + __type(value, int); +} map3 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS); + __uint(max_entries, MAX_ENTRIES); + __type(key, int); + __type(value, int); + __array(values, struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u32); + }); +} map4 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS); + __uint(max_entries, MAX_ENTRIES); + __type(key, int); + __type(value, int); + __array(values, struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u32); + }); +} map5 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP); + __uint(max_entries, MAX_ENTRIES); + __type(key, int); + __type(value, int); +} map6 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_SOCKMAP); + __uint(max_entries, MAX_ENTRIES); + __type(key, int); + __type(value, int); +} map7 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_CPUMAP); + __uint(max_entries, MAX_ENTRIES); + __type(key, int); + __type(value, int); +} map8 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_XSKMAP); + __uint(max_entries, MAX_ENTRIES); + __type(key, int); + __type(value, int); +} map9 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_SOCKHASH); + __uint(max_entries, MAX_ENTRIES); + __type(key, int); + __type(value, int); +} map10 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_QUEUE); + __uint(max_entries, MAX_ENTRIES); + __type(value, int); +} map11 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_STACK); + __uint(max_entries, MAX_ENTRIES); + __type(value, int); +} map12 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP_HASH); + __uint(max_entries, MAX_ENTRIES); + __type(key, int); + __type(value, int); +} map13 SEC(".maps"); + +char _license[] SEC("license") = "GPL";