From patchwork Tue Feb 6 22:04:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13547847 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 92847C4829A for ; Tue, 6 Feb 2024 22:05:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EBC76B009E; Tue, 6 Feb 2024 17:05:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 129366B009F; Tue, 6 Feb 2024 17:05:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EBB956B00A0; Tue, 6 Feb 2024 17:05:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id CFDE66B009E for ; Tue, 6 Feb 2024 17:05:35 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id B1B291A063A for ; Tue, 6 Feb 2024 22:05:35 +0000 (UTC) X-FDA: 81762761430.11.9246646 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf29.hostedemail.com (Postfix) with ESMTP id 0BCE112000C for ; Tue, 6 Feb 2024 22:05:33 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=R6l9dQOD; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707257134; a=rsa-sha256; cv=none; b=poDg3Qe9YWZDGUj9qou8MqGuZnZvFvCFseWEkQF6bK5JCLhB6HiSmvBTXYH6CVTAas9c1j hevX76II+TYyp/bC6YLnCjF2fHx7yrNOUiXClFLjhcY/YBjYt3R3zoRVEtdxtFYvgr6UYP 2eUnH965GDhBiII1nLgMpNg72c/Hdxc= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=R6l9dQOD; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707257134; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wpvr7dgsK6u0sIpxAzSJWUuWXV5TlyIi0UfrrFuy1/I=; b=e1OFBY8eiPCBS4qcvHxsRzh6OjM9QbzZR8oiLeshUoJjj58MlVVmOpcH1gKjPSejBOlssq 4zSVVLCe4liPTgEGju2O5QeaHiLBHO3kMVttxPitVHqf2z91yy0LcWCuslegKM7gfn1ztG KJGmNa/8cGAhL3ceMc+2PqNaHnD/a3E= Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-6e03f7f427aso6219b3a.2 for ; Tue, 06 Feb 2024 14:05:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707257133; x=1707861933; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wpvr7dgsK6u0sIpxAzSJWUuWXV5TlyIi0UfrrFuy1/I=; b=R6l9dQODLMIb0i3w7OxwWiHlh7lrWwgmCqLQ+D70oDzJOyYwo49uvX/saACx2UY3Sc 29w+bbIQ5D0tOePIoppU329Ps6LblA7wElh1c5bFJgtlekgjvT9bJ7ldMPQCUBwp/XTW NbMY5+3azXgBc4Agfzu1+tPmRjKJeMXqu2Sq8F0GLT/ub3uhB9sgp8MKHTwNGNnnYjkX QRQaRxzJFP5Kc9ocWCBzzSg9bmNRxarAPSSZhPPC2FS1lPBSuN9fDgJtc2GFFyvO7S8v jmF/O7LIpwg1A14d6s2aGkiU+8YRIV8Rwmx9GJP9rEoTyyMBD1fxefZD6z46hTUdjXFx STzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707257133; x=1707861933; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wpvr7dgsK6u0sIpxAzSJWUuWXV5TlyIi0UfrrFuy1/I=; b=hSYzf25SANYgTUVeaKc0XzLEzHf7sVFlbXZTiVkXsXom9dzIUzG5tlReF161wAN2KL AzJZEjNAitXGd1ZJjYRlLDp0YIJO1eg1yrvF3jzSfYcW/ZXeVnjq+eEEvjJ8UR+eSxcP jYzAvmslBadgbAeyd7vLrqnPrU4AK/8G+MaxN1JNPX0/ccjSe/ujCAW4y7gBDu762ZfC vLthN3v+JilkkTOcUm2kOS91aAd1yDApLJCyn3OcJdTaFHbRqFk64zOkR/Mcu99qnv07 r5nZsEk62N/xYQ/uFi+Plh4OdgyKCOhNVvBMWeMuaXMl+au0ZFv3RLeIOWSP3K7mMkib nUkA== X-Gm-Message-State: AOJu0Yzt52Fwumw85K7IN9Pa1/VUnsLG/oQ5fQi97gAqfvPOzTaNT3m2 RMEVSwEJv1UErb8c7pTuQicTMngvzrlrhjzznazy9CDDnO1+DAWa51Rgr9El X-Google-Smtp-Source: AGHT+IEY4Ho/vNriwoPjJ4S75OBCY2kHP9G7jpkOFNGGYsmw17IByMEq936vsD27kQ2v+hwLcnpchA== X-Received: by 2002:a05:6a00:c83:b0:6db:d4f8:bb1d with SMTP id a3-20020a056a000c8300b006dbd4f8bb1dmr1301419pfv.2.1707257132982; Tue, 06 Feb 2024 14:05:32 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCU2OGvHfmq89UQzCp1rwME6GQJLViCec64yB9gCcsghB2SWpFWsKo6/ZGOtD8X8cwbT65OVFmOToCbMFo8mIFHt5dOdX6bHawLpFq78B2yQpO2SQ+CAyDi7JP85wFUDSkEALC6cY8Jt2uzOJxzQANZN9nuIkX26PjvSgZmc7qjDGyu1p3St4AYihBbq9XC9KxRjpCsu9huR4K68LTgrecJJNvFuvorikOF9KmU1EEzO123aAdBH3wpLRjJAe2ZyON/Sjy5Gv2v9eyZ3yOVxj/zYKjqBFSWlngGz Received: from localhost.localdomain ([2620:10d:c090:400::4:27bf]) by smtp.gmail.com with ESMTPSA id o1-20020a056a001b4100b006ddc7ed6edfsm2497587pfv.51.2024.02.06.14.05.31 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 06 Feb 2024 14:05:32 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, memxor@gmail.com, eddyz87@gmail.com, tj@kernel.org, brho@google.com, hannes@cmpxchg.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next 12/16] libbpf: Allow specifying 64-bit integers in map BTF. Date: Tue, 6 Feb 2024 14:04:37 -0800 Message-Id: <20240206220441.38311-13-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240206220441.38311-1-alexei.starovoitov@gmail.com> References: <20240206220441.38311-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 0BCE112000C X-Stat-Signature: ei4uaai1ggwxd1duqgogikuof4x7qrrq X-HE-Tag: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+PmqnyM701trDSdVufAloe4L/USbfuFTW5TwQY/n5K1SDA7q9tFbNl6vMy4oWBrxBnXOF+aHrLNZ3gMzaNhZrg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Alexei Starovoitov __uint() macro that is used to specify map attributes like: __uint(type, BPF_MAP_TYPE_ARRAY); __uint(map_flags, BPF_F_MMAPABLE); is limited to 32-bit, since BTF_KIND_ARRAY has u32 "number of elements" field. Introduce __ulong() macro that allows specifying values bigger than 32-bit. In map definition "map_extra" is the only u64 field. Signed-off-by: Alexei Starovoitov --- tools/lib/bpf/bpf_helpers.h | 1 + tools/lib/bpf/libbpf.c | 44 ++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h index 9c777c21da28..fb909fc6866d 100644 --- a/tools/lib/bpf/bpf_helpers.h +++ b/tools/lib/bpf/bpf_helpers.h @@ -13,6 +13,7 @@ #define __uint(name, val) int (*name)[val] #define __type(name, val) typeof(val) *name #define __array(name, val) typeof(val) *name[] +#define __ulong(name, val) enum name##__enum { name##__value = val } name /* * Helper macro to place programs, maps, license in diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index c5ce5946dc6d..a8c89b2315cd 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -2229,6 +2229,39 @@ static bool get_map_field_int(const char *map_name, const struct btf *btf, return true; } +static bool get_map_field_long(const char *map_name, const struct btf *btf, + const struct btf_member *m, __u64 *res) +{ + const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL); + const char *name = btf__name_by_offset(btf, m->name_off); + + if (btf_is_ptr(t)) + return false; + + if (!btf_is_enum(t) && !btf_is_enum64(t)) { + pr_warn("map '%s': attr '%s': expected enum or enum64, got %s.\n", + map_name, name, btf_kind_str(t)); + return false; + } + + if (btf_vlen(t) != 1) { + pr_warn("map '%s': attr '%s': invalid __ulong\n", + map_name, name); + return false; + } + + if (btf_is_enum(t)) { + const struct btf_enum *e = btf_enum(t); + + *res = e->val; + } else { + const struct btf_enum64 *e = btf_enum64(t); + + *res = btf_enum64_value(e); + } + return true; +} + static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name) { int len; @@ -2462,10 +2495,15 @@ int parse_btf_map_def(const char *map_name, struct btf *btf, map_def->pinning = val; map_def->parts |= MAP_DEF_PINNING; } else if (strcmp(name, "map_extra") == 0) { - __u32 map_extra; + __u64 map_extra; - if (!get_map_field_int(map_name, btf, m, &map_extra)) - return -EINVAL; + if (!get_map_field_long(map_name, btf, m, &map_extra)) { + __u32 map_extra_u32; + + if (!get_map_field_int(map_name, btf, m, &map_extra_u32)) + return -EINVAL; + map_extra = map_extra_u32; + } map_def->map_extra = map_extra; map_def->parts |= MAP_DEF_MAP_EXTRA; } else {