]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
bpf: add btf_type_is_i{32,64} helpers
authorAnton Protopopov <a.s.protopopov@gmail.com>
Wed, 25 Jun 2025 15:16:21 +0000 (15:16 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 25 Jun 2025 22:15:49 +0000 (15:15 -0700)
There are places in BPF code which check if a BTF type is an integer
of particular size. This code can be made simpler by using helpers.
Add new btf_type_is_i{32,64} helpers, and simplify code in a few
files. (Suggested by Eduard for a patch which copy-pasted such a
check [1].)

  v1 -> v2:
    * export less generic helpers (Eduard)
    * make subject less generic than in [v1] (Eduard)

[1] https://lore.kernel.org/bpf/7edb47e73baa46705119a23c6bf4af26517a640f.camel@gmail.com/
[v1] https://lore.kernel.org/bpf/20250624193655.733050-1-a.s.protopopov@gmail.com/

Suggested-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20250625151621.1000584-1-a.s.protopopov@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/btf.h
kernel/bpf/arraymap.c
kernel/bpf/bpf_local_storage.c
kernel/bpf/btf.c
kernel/bpf/local_storage.c

index b2983706292f712ca182bd2cbe72534f95c5bf8b..a40beb9cf16040084acb5ba1146c397e6e3bc5a3 100644 (file)
@@ -221,6 +221,8 @@ bool btf_is_vmlinux(const struct btf *btf);
 struct module *btf_try_get_module(const struct btf *btf);
 u32 btf_nr_types(const struct btf *btf);
 struct btf *btf_base_btf(const struct btf *btf);
+bool btf_type_is_i32(const struct btf_type *t);
+bool btf_type_is_i64(const struct btf_type *t);
 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
                           const struct btf_member *m,
                           u32 expected_offset, u32 expected_size);
index eb28c0f219ee468198c31d7ad0561469f12b0aee..3d080916faf97651bfeefb885d5c88f0ea4c2200 100644 (file)
@@ -530,8 +530,6 @@ static int array_map_check_btf(const struct bpf_map *map,
                               const struct btf_type *key_type,
                               const struct btf_type *value_type)
 {
-       u32 int_data;
-
        /* One exception for keyless BTF: .bss/.data/.rodata map */
        if (btf_type_is_void(key_type)) {
                if (map->map_type != BPF_MAP_TYPE_ARRAY ||
@@ -544,14 +542,11 @@ static int array_map_check_btf(const struct bpf_map *map,
                return 0;
        }
 
-       if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
-               return -EINVAL;
-
-       int_data = *(u32 *)(key_type + 1);
-       /* bpf array can only take a u32 key. This check makes sure
+       /*
+        * Bpf array can only take a u32 key. This check makes sure
         * that the btf matches the attr used during map_create.
         */
-       if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))
+       if (!btf_type_is_i32(key_type))
                return -EINVAL;
 
        return 0;
index fa56c30833ff15280fcb270999f743e8ea5b7ed6..b931fbceb54da00f182d15ab343d7a47cb3b162f 100644 (file)
@@ -722,13 +722,7 @@ int bpf_local_storage_map_check_btf(const struct bpf_map *map,
                                    const struct btf_type *key_type,
                                    const struct btf_type *value_type)
 {
-       u32 int_data;
-
-       if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
-               return -EINVAL;
-
-       int_data = *(u32 *)(key_type + 1);
-       if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))
+       if (!btf_type_is_i32(key_type))
                return -EINVAL;
 
        return 0;
index 682acb1ed23421f9336272621ad6989ca01e378f..05fd64a371af1cd993214673d958be8a508c291e 100644 (file)
@@ -858,26 +858,37 @@ const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)
 EXPORT_SYMBOL_GPL(btf_type_by_id);
 
 /*
- * Regular int is not a bit field and it must be either
- * u8/u16/u32/u64 or __int128.
+ * Check that the type @t is a regular int. This means that @t is not
+ * a bit field and it has the same size as either of u8/u16/u32/u64
+ * or __int128. If @expected_size is not zero, then size of @t should
+ * be the same. A caller should already have checked that the type @t
+ * is an integer.
  */
+static bool __btf_type_int_is_regular(const struct btf_type *t, size_t expected_size)
+{
+       u32 int_data = btf_type_int(t);
+       u8 nr_bits = BTF_INT_BITS(int_data);
+       u8 nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
+
+       return BITS_PER_BYTE_MASKED(nr_bits) == 0 &&
+              BTF_INT_OFFSET(int_data) == 0 &&
+              (nr_bytes <= 16 && is_power_of_2(nr_bytes)) &&
+              (expected_size == 0 || nr_bytes == expected_size);
+}
+
 static bool btf_type_int_is_regular(const struct btf_type *t)
 {
-       u8 nr_bits, nr_bytes;
-       u32 int_data;
+       return __btf_type_int_is_regular(t, 0);
+}
 
-       int_data = btf_type_int(t);
-       nr_bits = BTF_INT_BITS(int_data);
-       nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
-       if (BITS_PER_BYTE_MASKED(nr_bits) ||
-           BTF_INT_OFFSET(int_data) ||
-           (nr_bytes != sizeof(u8) && nr_bytes != sizeof(u16) &&
-            nr_bytes != sizeof(u32) && nr_bytes != sizeof(u64) &&
-            nr_bytes != (2 * sizeof(u64)))) {
-               return false;
-       }
+bool btf_type_is_i32(const struct btf_type *t)
+{
+       return btf_type_is_int(t) && __btf_type_int_is_regular(t, 4);
+}
 
-       return true;
+bool btf_type_is_i64(const struct btf_type *t)
+{
+       return btf_type_is_int(t) && __btf_type_int_is_regular(t, 8);
 }
 
 /*
index 3969eb0382afba0abb237d2af88565a5d6bf1120..632d51b05fe983d649087a05e09177c021c16d33 100644 (file)
@@ -394,17 +394,10 @@ static int cgroup_storage_check_btf(const struct bpf_map *map,
                if (!btf_member_is_reg_int(btf, key_type, m, offset, size))
                        return -EINVAL;
        } else {
-               u32 int_data;
-
                /*
                 * Key is expected to be u64, which stores the cgroup_inode_id
                 */
-
-               if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
-                       return -EINVAL;
-
-               int_data = *(u32 *)(key_type + 1);
-               if (BTF_INT_BITS(int_data) != 64 || BTF_INT_OFFSET(int_data))
+               if (!btf_type_is_i64(key_type))
                        return -EINVAL;
        }