From nobody Sat Feb 7 12:19:33 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59DF6C001B0 for ; Wed, 19 Jul 2023 09:28:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229819AbjGSJ2s (ORCPT ); Wed, 19 Jul 2023 05:28:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229689AbjGSJ2n (ORCPT ); Wed, 19 Jul 2023 05:28:43 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2C511BFC for ; Wed, 19 Jul 2023 02:28:42 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-3110ab7110aso6864775f8f.3 for ; Wed, 19 Jul 2023 02:28:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1689758921; x=1692350921; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=7yIrbZZR4lCf9GkhcpzxSxXCXtiiG2twcc9m1HrepQM=; b=YEixGh5HQBFgZR9iRMGxVQCaew6ZN4cNCvdwaCgkfJ4NWRHyaXymSonkXqsxYBpeHw eFC+YxzSSrCN7HiNvb14OxUMDE8BJyipOGvbCz2c4iFNE1kVuNRKPGkbtOojEi5nU9O1 10SWJi8kIW2w4bJ9Zm88mldz64l1zZLyPWdqqAtXxawhrlOGm4TgPy9mGIs0JMn/vVqZ 0XaPgHMVc8OlKMG/t1sPKUe0I4eLQbEwCfsL9bBWJziaxlG+QoRVZn2oHrw6UBqHkzYB vqLIcP+F3xKVCbHQd0NfLVf32dNlWI91inCPekKmzovxRwFbW7Y6IVI9/P+MkBPN7cTF VVCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689758921; x=1692350921; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7yIrbZZR4lCf9GkhcpzxSxXCXtiiG2twcc9m1HrepQM=; b=NcSywi+HWU+zgLHWWr3JkcJaMuFdtNbcAWnN0zAMD4usF3Pswo4K1qxCXhH3Ty0CAY IzySY8uYnEOJHElV5Fv+duUXEfMXnqU3F3oCreK0oWZw2x3g9hh4LNr2jHXxeFGIDzRK e79uBGoLMLckvS38G2dObEEPz7hVvvEnB5KtSW7kWcvPUlYqtxUaPJPH3eDXheqeNOXa ycxy3TXo9hYrUAAn371AVa/+F0CsUnBhVPhZ2mIMwr3qRS+eob93IMdpBiWP9eul9OTA MCPJSt56ztiNijJ/SER0MhNFel1REPckk4vFxG+LtqV5s57/aY4n3z8V+AO7MzCAti7v TMtw== X-Gm-Message-State: ABy/qLYC0QfcLEriVG3bBxcKTUJmLuFK7KUiuANRubnSieFam4uyhleT 2RjmDDjmNbRQidbg2UL3zu6mOw== X-Google-Smtp-Source: APBJJlGdrZyXQBJRRhdPij5Uln/4bYjkXgebmKRLaYptoKxPns58oPRvii6FI4Dg7/Guu6ObcLZAbQ== X-Received: by 2002:a5d:4292:0:b0:314:f88:4fea with SMTP id k18-20020a5d4292000000b003140f884feamr15008563wrq.8.1689758921130; Wed, 19 Jul 2023 02:28:41 -0700 (PDT) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id r18-20020adff112000000b0031435c2600esm4857213wro.79.2023.07.19.02.28.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jul 2023 02:28:40 -0700 (PDT) From: Anton Protopopov To: Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Hou Tao , Joe Stringer , Anton Protopopov , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v2 bpf-next 1/4] bpf: consider types listed in reg2btf_ids as trusted Date: Wed, 19 Jul 2023 09:29:49 +0000 Message-Id: <20230719092952.41202-2-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230719092952.41202-1-aspsk@isovalent.com> References: <20230719092952.41202-1-aspsk@isovalent.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The reg2btf_ids array contains a list of types for which we can (and need) to find a corresponding static BTF id. All the types in the list can be considered as trusted for purposes of kfuncs. Signed-off-by: Anton Protopopov --- kernel/bpf/verifier.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 0b9da95331d7..05123feab378 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5413,12 +5413,24 @@ static bool is_flow_key_reg(struct bpf_verifier_env= *env, int regno) return reg->type =3D=3D PTR_TO_FLOW_KEYS; } =20 +static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] =3D { +#ifdef CONFIG_NET + [PTR_TO_SOCKET] =3D &btf_sock_ids[BTF_SOCK_TYPE_SOCK], + [PTR_TO_SOCK_COMMON] =3D &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], + [PTR_TO_TCP_SOCK] =3D &btf_sock_ids[BTF_SOCK_TYPE_TCP], +#endif +}; + static bool is_trusted_reg(const struct bpf_reg_state *reg) { /* A referenced register is always trusted. */ if (reg->ref_obj_id) return true; =20 + /* Types listed in the reg2btf_ids are always trusted */ + if (reg2btf_ids[base_type(reg->type)]) + return true; + /* If a register is not referenced, it is trusted if it has the * MEM_ALLOC or PTR_TRUSTED type modifiers, and no others. Some of the * other type modifiers may be safe, but we elect to take an opt-in @@ -10052,15 +10064,6 @@ static bool __btf_type_is_scalar_struct(struct bpf= _verifier_env *env, return true; } =20 - -static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] =3D { -#ifdef CONFIG_NET - [PTR_TO_SOCKET] =3D &btf_sock_ids[BTF_SOCK_TYPE_SOCK], - [PTR_TO_SOCK_COMMON] =3D &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], - [PTR_TO_TCP_SOCK] =3D &btf_sock_ids[BTF_SOCK_TYPE_TCP], -#endif -}; - enum kfunc_ptr_arg_type { KF_ARG_PTR_TO_CTX, KF_ARG_PTR_TO_ALLOC_BTF_ID, /* Allocated object */ --=20 2.34.1 From nobody Sat Feb 7 12:19:33 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75565C001B0 for ; Wed, 19 Jul 2023 09:28:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229802AbjGSJ2v (ORCPT ); Wed, 19 Jul 2023 05:28:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229761AbjGSJ2q (ORCPT ); Wed, 19 Jul 2023 05:28:46 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD83619A0 for ; Wed, 19 Jul 2023 02:28:43 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-316f9abf204so3965955f8f.1 for ; Wed, 19 Jul 2023 02:28:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1689758922; x=1692350922; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=/ox7+dO2lf7lgMqaaTMVSFAPK5+oYnmrVK+bXEJp/W0=; b=Y2Mpy8bAABv4S46y7WZ90sd+XQqYerSsyNFK/fOu5v+1aFTrKSRh157qWeb2fcZFET 3ujI6G+AWSuXWleaTkj6PW77kImZOuxsWPr7WNqnFQavys+x/jC3mNS/2wvq9oPGpcjL ImbPB9uXkyRaLUm0kcv4RFO36Acn2QJwhaJmqA8PJ+uv0+RTDSkazJVLRUrl23PZ+/02 ecJIYrKOIHzmWw/jlH1nyYB2JcUFO5WCC8ouKj2wH6g0rs+5gwlOnFDOHLnsr5xhP8dH JWHA4RLKDXHv9KafdYTVQB5yC3/jsIRiF6R1i/Ru/iOcG4Jkc1WDhl6Fh44r47q1KG9R 5JIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689758922; x=1692350922; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/ox7+dO2lf7lgMqaaTMVSFAPK5+oYnmrVK+bXEJp/W0=; b=jCsxVEu7LK9RP968aeA0LD2ao29glKxauJO6DxNC4YoZ9f7/2x0uRhnvtS9kH9zTVb GhnL490iH+vnpBtPC+zTnKVDqsydODcrY82SW31GaWNPgPKPMc0UmG670sXRBrQwGenK LB1xgloWONbEqXvXG3SPQXBGk7lI3DVkAbQwwq3ZVBqJm8cFYtFTtIeMDJNDXsPKTMh3 ILrlgzlOTu1i7Id8O8zVwktiBNNDS3IUhKHZDHqxBC+Xo5xhkeN2u+QOP98xS1KTEufv deGxq22IQ5vyb6RFTv4sbbIwyyiFfwZk0WGcD5hCY95Vcs9mUXVloXJqdYi3m9cduTt5 0hDw== X-Gm-Message-State: ABy/qLaAxNmzdKKQ21C+nUz4DpJlk8nqNgIBqenWLHh+Hd8wqiVv9b1S gZgMKQnCiHhpGF8ozaZtEuteWg== X-Google-Smtp-Source: APBJJlFFjbihNqB2C1H/22PKc4G/vqLpNf4xB0qRfHVT9SwRQy1zNL5ufYtH9malvGJfo8NCXO/WNA== X-Received: by 2002:a05:6000:18d:b0:314:4ba7:b024 with SMTP id p13-20020a056000018d00b003144ba7b024mr16716585wrx.9.1689758922063; Wed, 19 Jul 2023 02:28:42 -0700 (PDT) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id r18-20020adff112000000b0031435c2600esm4857213wro.79.2023.07.19.02.28.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jul 2023 02:28:41 -0700 (PDT) From: Anton Protopopov To: Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Hou Tao , Joe Stringer , Anton Protopopov , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v2 bpf-next 2/4] bpf: consider CONST_PTR_TO_MAP as trusted pointer to struct bpf_map Date: Wed, 19 Jul 2023 09:29:50 +0000 Message-Id: <20230719092952.41202-3-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230719092952.41202-1-aspsk@isovalent.com> References: <20230719092952.41202-1-aspsk@isovalent.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add the BTF id of struct bpf_map to the reg2btf_ids array. This makes the values of the CONST_PTR_TO_MAP type to be considered as trusted by kfuncs. This, in turn, allows users to execute trusted kfuncs which accept `struct bpf_map *` arguments from non-tracing programs. While exporting the btf_bpf_map_id variable, save some bytes by defining it as BTF_ID_LIST_GLOBAL_SINGLE (which is u32[1]) and not as BTF_ID_LIST (which is u32[64]). Signed-off-by: Anton Protopopov --- include/linux/btf_ids.h | 1 + kernel/bpf/map_iter.c | 3 +-- kernel/bpf/verifier.c | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/linux/btf_ids.h b/include/linux/btf_ids.h index 00950cc03bff..a3462a9b8e18 100644 --- a/include/linux/btf_ids.h +++ b/include/linux/btf_ids.h @@ -267,5 +267,6 @@ MAX_BTF_TRACING_TYPE, extern u32 btf_tracing_ids[]; extern u32 bpf_cgroup_btf_id[]; extern u32 bpf_local_storage_map_btf_id[]; +extern u32 btf_bpf_map_id[]; =20 #endif diff --git a/kernel/bpf/map_iter.c b/kernel/bpf/map_iter.c index d06d3b7150e5..b67996147895 100644 --- a/kernel/bpf/map_iter.c +++ b/kernel/bpf/map_iter.c @@ -78,8 +78,7 @@ static const struct seq_operations bpf_map_seq_ops =3D { .show =3D bpf_map_seq_show, }; =20 -BTF_ID_LIST(btf_bpf_map_id) -BTF_ID(struct, bpf_map) +BTF_ID_LIST_GLOBAL_SINGLE(btf_bpf_map_id, struct, bpf_map) =20 static const struct bpf_iter_seq_info bpf_map_seq_info =3D { .seq_ops =3D &bpf_map_seq_ops, diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 05123feab378..803b91135ca0 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5419,6 +5419,7 @@ static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] =3D { [PTR_TO_SOCK_COMMON] =3D &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], [PTR_TO_TCP_SOCK] =3D &btf_sock_ids[BTF_SOCK_TYPE_TCP], #endif + [CONST_PTR_TO_MAP] =3D btf_bpf_map_id, }; =20 static bool is_trusted_reg(const struct bpf_reg_state *reg) --=20 2.34.1 From nobody Sat Feb 7 12:19:33 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 916CCC0015E for ; Wed, 19 Jul 2023 09:28:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229949AbjGSJ2y (ORCPT ); Wed, 19 Jul 2023 05:28:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229803AbjGSJ2q (ORCPT ); Wed, 19 Jul 2023 05:28:46 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E691F1BFC for ; Wed, 19 Jul 2023 02:28:44 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-3141fa31c2bso5970191f8f.2 for ; Wed, 19 Jul 2023 02:28:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1689758923; x=1692350923; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=O4zgnqP7ASsB+eUpBj68dhMcY1VzfTyfkf9nUd4K0Yg=; b=SJ3Q0B0A3Ka3GcM2fsrRRu1ABrz3qH5VgmkgNyaET96MSIOytg/OiEvrePd6F8G38R 3hsF8hHq1GaxCVrvDR4njcuc8XDEMSD/QU52mvwbWVqCXXmsKizJdl+1vcFeg5dH3uBE D18W8vJhf5Pzs3smkfJc1RF9ftkSDd5q8XubHReffV4Svz9vwql31AQfXLIFkFFQBuJj bYk5O0F0zscjX6UwNUMlxOWG8C5eRgR5yrEBMczGh7d0S8+2q58dK9PNv18cDwHv9txy 3qf/p9NX9okmwlwyzKiHN7FyxZqgYiG+vQW0XUSSQzLohGIejHdnf/JD5laAtmUHJCGR BpAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689758923; x=1692350923; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O4zgnqP7ASsB+eUpBj68dhMcY1VzfTyfkf9nUd4K0Yg=; b=JlEKxDR/Y0Iv4ixjL8xu7xLcL/mLgGawMdAZB2xkL8OYIMxXM/EN+AGz6tlfAOPH8+ hJk95oMYa3oP2OJLZnMZiGcAtKKCPDWrvwYuwivhGkXajQhH7bU7djmBQ+B5aSPGUI+M 510aC8XAljn+XMBP3tvr/uQy4cHTJ6fr/1qm4Iw88ry4KvWlnSqPKJZ3wbyO3zzYBbVZ KxD003BI8muiMwW6C38OJRC4PbzzYZZgT0l1WmzsR1z79Fi3rY05lsNtwH2Y9Pb6mFbn gPvJpFordqdfnVDwrBpc9W4IUnHpr/lLg7SbvxcPErX5hridVhbFZTU0tkwbf8BK5BMS uXNw== X-Gm-Message-State: ABy/qLYh2Cew2kzW0YPFucO7Tvuf0Ex2xAXH8wFgy9jJpvYXjKf2HW4R 1oUHA/1CThDZHdJrDCZ9yeULhw== X-Google-Smtp-Source: APBJJlGtFawbZBqZERUAV75DJCPsKZRCyTCDL7eU0AnTNqHeGvVdeQGCV9GkZYMhye2U0gW5/Sl8MA== X-Received: by 2002:a5d:4532:0:b0:316:cbb4:4e49 with SMTP id j18-20020a5d4532000000b00316cbb44e49mr3584324wra.1.1689758923173; Wed, 19 Jul 2023 02:28:43 -0700 (PDT) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id r18-20020adff112000000b0031435c2600esm4857213wro.79.2023.07.19.02.28.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jul 2023 02:28:42 -0700 (PDT) From: Anton Protopopov To: Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Hou Tao , Joe Stringer , Anton Protopopov , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v2 bpf-next 3/4] bpf: make an argument const in the bpf_map_sum_elem_count kfunc Date: Wed, 19 Jul 2023 09:29:51 +0000 Message-Id: <20230719092952.41202-4-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230719092952.41202-1-aspsk@isovalent.com> References: <20230719092952.41202-1-aspsk@isovalent.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" We use the map pointer only to read the counter values, no locking involved, so mark the argument as const. Signed-off-by: Anton Protopopov --- kernel/bpf/map_iter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/map_iter.c b/kernel/bpf/map_iter.c index b67996147895..011adb41858e 100644 --- a/kernel/bpf/map_iter.c +++ b/kernel/bpf/map_iter.c @@ -197,7 +197,7 @@ __diag_push(); __diag_ignore_all("-Wmissing-prototypes", "Global functions as their definitions will be in vmlinux BTF"); =20 -__bpf_kfunc s64 bpf_map_sum_elem_count(struct bpf_map *map) +__bpf_kfunc s64 bpf_map_sum_elem_count(const struct bpf_map *map) { s64 *pcount; s64 ret =3D 0; --=20 2.34.1 From nobody Sat Feb 7 12:19:33 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 373B5C0015E for ; Wed, 19 Jul 2023 09:29:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229685AbjGSJ3B (ORCPT ); Wed, 19 Jul 2023 05:29:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229902AbjGSJ2u (ORCPT ); Wed, 19 Jul 2023 05:28:50 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 728241FD3 for ; Wed, 19 Jul 2023 02:28:46 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-4fbc0314a7bso11029031e87.2 for ; Wed, 19 Jul 2023 02:28:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1689758924; x=1692350924; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=TL4iPDnWm7LI7dooTGYVCYljPJYJzbf9kW0UMI/E3E8=; b=RmR0tn2XLxoJOQVfhj6Z1y0iP4vl4SctrEUwL1kVlHU/vpEljcorwTzzRgX4ay8x6J ZxCler5k1qYfD6FdRhctRHWJrBIPj38PG89ytDH1l+GK/juPRE8kbFKAaIJvYhEnX+81 30HIyfPQhmb4VSWcnrPkYKDmvk69GBJc5iD2tQCEvBqIpJOSQdaWDlwlQGrmkiLNazPC 2Bd5pSUZiJkEroDFWFD7oNPe2iJXfklJtZZAxJi52al2Sf6Kt+wJ4C53pbYDuTYFHwb6 ENKVUA0rzunIqRYXUKNUzwvzz8u/Zxja1M6bOI/9MJ46+Hm2LNiCV5O8vSnBxs2Ub3fI jbug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689758924; x=1692350924; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TL4iPDnWm7LI7dooTGYVCYljPJYJzbf9kW0UMI/E3E8=; b=HYf7B1fFR/N7jA2KCmphCSasLAiPiy9Kyn8fuwxw3N05syV+jZc2dbY1v7dxwZPWFW FMV/7dja/E6PzQWCo8zl09uGbksYl9umkZD0bumoqYkp6gpBLRrIfwyIE8OefuPJ5vm5 pPH6sW6oEaGqnXS+jQ8srUUzSx6zFFpj+natOT9g1a8fNc0MovLGP3HpFkMSwkBdpSlG JHeozDZM7T+eziYVYPZGZLV4nY0EyNGQtklOOuTulOCaYHFN3snwzD6WqRRlk8H1FxK4 78InOgSwDu0D4ji3MdlgcHCY4xoxusLAmYhfMbHk+W++S8MOPzIWqGUJx2bCvvbwzjND 6Uvw== X-Gm-Message-State: ABy/qLYdacl31DXX09GTRCFLhnaL41ZJ7ElOm/RCmlPGqCRuSV0fJjEq XgbIunZWOmgc6dRYMCwCxCNl0o057tpjrOGZwMvF6Q== X-Google-Smtp-Source: APBJJlEvJxlIlo047VV+7wzcGwDG8LI2FzpITTuAkDeRT6GN74ABqTYEyn20L8ozGDwoQvHV0y9XxA== X-Received: by 2002:a05:6512:3d1b:b0:4fb:8a92:4fba with SMTP id d27-20020a0565123d1b00b004fb8a924fbamr15073243lfv.25.1689758924233; Wed, 19 Jul 2023 02:28:44 -0700 (PDT) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id r18-20020adff112000000b0031435c2600esm4857213wro.79.2023.07.19.02.28.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jul 2023 02:28:43 -0700 (PDT) From: Anton Protopopov To: Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Hou Tao , Joe Stringer , Anton Protopopov , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v2 bpf-next 4/4] bpf: allow any program to use the bpf_map_sum_elem_count kfunc Date: Wed, 19 Jul 2023 09:29:52 +0000 Message-Id: <20230719092952.41202-5-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230719092952.41202-1-aspsk@isovalent.com> References: <20230719092952.41202-1-aspsk@isovalent.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Register the bpf_map_sum_elem_count func for all programs, and update the map_ptr subtest of the test_progs test to test the new functionality. The usage is allowed as long as the pointer to the map is trusted (when using tracing programs) or is a const pointer to map, as in the following example: struct { __uint(type, BPF_MAP_TYPE_HASH); ... } hash SEC(".maps"); ... static inline int some_bpf_prog(void) { struct bpf_map *map =3D (struct bpf_map *)&hash; __s64 count; count =3D bpf_map_sum_elem_count(map); ... } Signed-off-by: Anton Protopopov --- kernel/bpf/map_iter.c | 2 +- tools/testing/selftests/bpf/progs/map_ptr_kern.c | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/map_iter.c b/kernel/bpf/map_iter.c index 011adb41858e..6fc9dae9edc8 100644 --- a/kernel/bpf/map_iter.c +++ b/kernel/bpf/map_iter.c @@ -226,6 +226,6 @@ static const struct btf_kfunc_id_set bpf_map_iter_kfunc= _set =3D { =20 static int init_subsystem(void) { - return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_map_iter_kfu= nc_set); + return register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &bpf_map_iter_kfun= c_set); } late_initcall(init_subsystem); diff --git a/tools/testing/selftests/bpf/progs/map_ptr_kern.c b/tools/testi= ng/selftests/bpf/progs/map_ptr_kern.c index db388f593d0a..3325da17ec81 100644 --- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c +++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c @@ -103,6 +103,8 @@ struct { __type(value, __u32); } m_hash SEC(".maps"); =20 +__s64 bpf_map_sum_elem_count(struct bpf_map *map) __ksym; + static inline int check_hash(void) { struct bpf_htab *hash =3D (struct bpf_htab *)&m_hash; @@ -115,6 +117,8 @@ static inline int check_hash(void) VERIFY(hash->elem_size =3D=3D 64); =20 VERIFY(hash->count.counter =3D=3D 0); + VERIFY(bpf_map_sum_elem_count(map) =3D=3D 0); + for (i =3D 0; i < HALF_ENTRIES; ++i) { const __u32 key =3D i; const __u32 val =3D 1; @@ -123,6 +127,7 @@ static inline int check_hash(void) return 0; } VERIFY(hash->count.counter =3D=3D HALF_ENTRIES); + VERIFY(bpf_map_sum_elem_count(map) =3D=3D HALF_ENTRIES); =20 return 1; } --=20 2.34.1