Skip to content

Commit c680e7e

Browse files
ameryhungKernel Patches Daemon
authored andcommitted
bpf: Allow getting bpf_map from struct_ops kdata
To allow struct_ops implementors to access the bpf_map during reg() and unreg(), return bpf_map from bpf_struct_ops_get() instead and let callers check the error. Additionally, expose bpf_struct_ops_get() and bpf_struct_ops_put() so that kernel modules can use them. Signed-off-by: Amery Hung <[email protected]>
1 parent dbe33e3 commit c680e7e

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

include/linux/bpf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,7 +1948,7 @@ struct bpf_struct_ops_common_value {
19481948
__register_bpf_struct_ops(st_ops); \
19491949
})
19501950
#define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
1951-
bool bpf_struct_ops_get(const void *kdata);
1951+
struct bpf_map *bpf_struct_ops_get(const void *kdata);
19521952
void bpf_struct_ops_put(const void *kdata);
19531953
int bpf_struct_ops_supported(const struct bpf_struct_ops *st_ops, u32 moff);
19541954
int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
@@ -1963,7 +1963,7 @@ void bpf_struct_ops_image_free(void *image);
19631963
static inline bool bpf_try_module_get(const void *data, struct module *owner)
19641964
{
19651965
if (owner == BPF_MODULE_OWNER)
1966-
return bpf_struct_ops_get(data);
1966+
return !IS_ERR(bpf_struct_ops_get(data));
19671967
else
19681968
return try_module_get(owner);
19691969
}

kernel/bpf/bpf_struct_ops.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ const struct bpf_map_ops bpf_struct_ops_map_ops = {
11501150
/* "const void *" because some subsystem is
11511151
* passing a const (e.g. const struct tcp_congestion_ops *)
11521152
*/
1153-
bool bpf_struct_ops_get(const void *kdata)
1153+
struct bpf_map *bpf_struct_ops_get(const void *kdata)
11541154
{
11551155
struct bpf_struct_ops_value *kvalue;
11561156
struct bpf_struct_ops_map *st_map;
@@ -1159,9 +1159,9 @@ bool bpf_struct_ops_get(const void *kdata)
11591159
kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
11601160
st_map = container_of(kvalue, struct bpf_struct_ops_map, kvalue);
11611161

1162-
map = __bpf_map_inc_not_zero(&st_map->map, false);
1163-
return !IS_ERR(map);
1162+
return __bpf_map_inc_not_zero(&st_map->map, false);
11641163
}
1164+
EXPORT_SYMBOL_GPL(bpf_struct_ops_get);
11651165

11661166
void bpf_struct_ops_put(const void *kdata)
11671167
{
@@ -1173,6 +1173,7 @@ void bpf_struct_ops_put(const void *kdata)
11731173

11741174
bpf_map_put(&st_map->map);
11751175
}
1176+
EXPORT_SYMBOL_GPL(bpf_struct_ops_put);
11761177

11771178
static bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)
11781179
{

0 commit comments

Comments
 (0)