]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
bpf: Allow struct_ops to get map id by kdata
authorAmery Hung <ameryhung@gmail.com>
Wed, 6 Aug 2025 16:25:38 +0000 (09:25 -0700)
committerMartin KaFai Lau <martin.lau@kernel.org>
Wed, 6 Aug 2025 20:39:58 +0000 (13:39 -0700)
Add bpf_struct_ops_id() to enable struct_ops implementors to use
struct_ops map id as the unique id of a struct_ops in their subsystem.
A subsystem that wishes to create a mapping between id and struct_ops
instance pointer can update the mapping accordingly during
bpf_struct_ops::reg(), unreg(), and update().

Signed-off-by: Amery Hung <ameryhung@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://patch.msgid.link/20250806162540.681679-2-ameryhung@gmail.com
include/linux/bpf.h
kernel/bpf/bpf_struct_ops.c

index cc700925b802fe0bb88844cc36120a9136788eaf..e7ee089e8a31a5c534242d4150ac152a4d23e33a 100644 (file)
@@ -1985,6 +1985,7 @@ static inline void bpf_module_put(const void *data, struct module *owner)
                module_put(owner);
 }
 int bpf_struct_ops_link_create(union bpf_attr *attr);
+u32 bpf_struct_ops_id(const void *kdata);
 
 #ifdef CONFIG_NET
 /* Define it here to avoid the use of forward declaration */
index 687a3e9c76f527b18b414b1c3e9065834433e6c5..a41e6730edcf33323f5f0831cc520196f9992bc9 100644 (file)
@@ -1174,6 +1174,18 @@ void bpf_struct_ops_put(const void *kdata)
        bpf_map_put(&st_map->map);
 }
 
+u32 bpf_struct_ops_id(const void *kdata)
+{
+       struct bpf_struct_ops_value *kvalue;
+       struct bpf_struct_ops_map *st_map;
+
+       kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
+       st_map = container_of(kvalue, struct bpf_struct_ops_map, kvalue);
+
+       return st_map->map.id;
+}
+EXPORT_SYMBOL_GPL(bpf_struct_ops_id);
+
 static bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)
 {
        struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;