Skip to content

Commit 62db312

Browse files
ameryhungKernel Patches Daemon
authored andcommitted
bpf: Support associating BPF program with struct_ops
Add a new BPF command BPF_PROG_ASSOC_STRUCT_OPS to allow associating a BPF program with a struct_ops map. This command takes a file descriptor of a struct_ops map and a BPF program and set prog->aux->st_ops_assoc to the kdata of the struct_ops map. The command does not accept a struct_ops program nor a non-struct_ops map. Programs of a struct_ops map is automatically associated with the map during map update. If a program is shared between two struct_ops maps, prog->aux->st_ops_assoc will be poisoned to indicate that the associated struct_ops is ambiguous. The pointer, once poisoned, cannot be reset since we have lost track of associated struct_ops. For other program types, the associated struct_ops map, once set, cannot be changed later. This restriction may be lifted in the future if there is a use case. A kernel helper bpf_prog_get_assoc_struct_ops() can be used to retrieve the associated struct_ops pointer. The returned pointer, if not NULL, is guaranteed to be valid and point to a fully updated struct_ops struct. For struct_ops program reused in multiple struct_ops map, the return will be NULL. prog->aux->st_ops_assoc is protected by bumping the refcount for non-struct_ops programs and RCU for struct_ops programs. Since it would be inefficient to track programs associated with a struct_ops map, every non-struct_ops program will bump the refcount of the map to make sure st_ops_assoc stays valid. For a struct_ops program, it is protected by RCU as map_free will wait for an RCU grace period before disassociating the program with the map. The helper must be called in BPF program context or RCU read-side critical section. struct_ops implementers should note that the struct_ops returned may or may not be attached. The struct_ops implementer will be responsible for tracking and checking the state of the associated struct_ops map if the use case requires an attached struct_ops. Signed-off-by: Amery Hung <[email protected]>
1 parent a5d30cb commit 62db312

File tree

6 files changed

+191
-0
lines changed

6 files changed

+191
-0
lines changed

include/linux/bpf.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,8 @@ struct bpf_prog_aux {
17271727
struct rcu_head rcu;
17281728
};
17291729
struct bpf_stream stream[2];
1730+
struct mutex st_ops_assoc_mutex;
1731+
struct bpf_map __rcu *st_ops_assoc;
17301732
};
17311733

17321734
struct bpf_prog {
@@ -2027,6 +2029,9 @@ static inline void bpf_module_put(const void *data, struct module *owner)
20272029
module_put(owner);
20282030
}
20292031
int bpf_struct_ops_link_create(union bpf_attr *attr);
2032+
int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map);
2033+
void bpf_prog_disassoc_struct_ops(struct bpf_prog *prog);
2034+
void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux);
20302035
u32 bpf_struct_ops_id(const void *kdata);
20312036

20322037
#ifdef CONFIG_NET
@@ -2074,6 +2079,17 @@ static inline int bpf_struct_ops_link_create(union bpf_attr *attr)
20742079
{
20752080
return -EOPNOTSUPP;
20762081
}
2082+
static inline int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map)
2083+
{
2084+
return -EOPNOTSUPP;
2085+
}
2086+
static inline void bpf_prog_disassoc_struct_ops(struct bpf_prog *prog)
2087+
{
2088+
}
2089+
static inline void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux)
2090+
{
2091+
return NULL;
2092+
}
20772093
static inline void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
20782094
{
20792095
}

include/uapi/linux/bpf.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,16 @@ union bpf_iter_link_info {
918918
* Number of bytes read from the stream on success, or -1 if an
919919
* error occurred (in which case, *errno* is set appropriately).
920920
*
921+
* BPF_PROG_ASSOC_STRUCT_OPS
922+
* Description
923+
* Associate a BPF program with a struct_ops map. The struct_ops
924+
* map is identified by *map_fd* and the BPF program is
925+
* identified by *prog_fd*.
926+
*
927+
* Return
928+
* 0 on success or -1 if an error occurred (in which case,
929+
* *errno* is set appropriately).
930+
*
921931
* NOTES
922932
* eBPF objects (maps and programs) can be shared between processes.
923933
*
@@ -974,6 +984,7 @@ enum bpf_cmd {
974984
BPF_PROG_BIND_MAP,
975985
BPF_TOKEN_CREATE,
976986
BPF_PROG_STREAM_READ_BY_FD,
987+
BPF_PROG_ASSOC_STRUCT_OPS,
977988
__MAX_BPF_CMD,
978989
};
979990

@@ -1894,6 +1905,12 @@ union bpf_attr {
18941905
__u32 prog_fd;
18951906
} prog_stream_read;
18961907

1908+
struct {
1909+
__u32 map_fd;
1910+
__u32 prog_fd;
1911+
__u32 flags;
1912+
} prog_assoc_struct_ops;
1913+
18971914
} __attribute__((aligned(8)));
18981915

18991916
/* The description below is an attempt at providing documentation to eBPF

kernel/bpf/bpf_struct_ops.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,17 @@ static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)
533533
}
534534
}
535535

536+
static void bpf_struct_ops_map_dissoc_progs(struct bpf_struct_ops_map *st_map)
537+
{
538+
u32 i;
539+
540+
for (i = 0; i < st_map->funcs_cnt; i++) {
541+
if (!st_map->links[i])
542+
break;
543+
bpf_prog_disassoc_struct_ops(st_map->links[i]->prog);
544+
}
545+
}
546+
536547
static void bpf_struct_ops_map_free_image(struct bpf_struct_ops_map *st_map)
537548
{
538549
int i;
@@ -811,6 +822,12 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
811822
&bpf_struct_ops_link_lops, prog, prog->expected_attach_type);
812823
*plink++ = &link->link;
813824

825+
err = bpf_prog_assoc_struct_ops(prog, &st_map->map);
826+
if (err) {
827+
bpf_prog_put(prog);
828+
goto reset_unlock;
829+
}
830+
814831
ksym = kzalloc(sizeof(*ksym), GFP_USER);
815832
if (!ksym) {
816833
err = -ENOMEM;
@@ -980,6 +997,8 @@ static void bpf_struct_ops_map_free(struct bpf_map *map)
980997
if (btf_is_module(st_map->btf))
981998
module_put(st_map->st_ops_desc->st_ops->owner);
982999

1000+
bpf_struct_ops_map_dissoc_progs(st_map);
1001+
9831002
bpf_struct_ops_map_del_ksyms(st_map);
9841003

9851004
/* The struct_ops's function may switch to another struct_ops.
@@ -1394,6 +1413,79 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
13941413
return err;
13951414
}
13961415

1416+
int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map)
1417+
{
1418+
struct bpf_map *st_ops_assoc;
1419+
1420+
guard(mutex)(&prog->aux->st_ops_assoc_mutex);
1421+
1422+
st_ops_assoc = rcu_dereference_protected(prog->aux->st_ops_assoc,
1423+
lockdep_is_held(&prog->aux->st_ops_assoc_mutex));
1424+
if (st_ops_assoc && st_ops_assoc == map)
1425+
return 0;
1426+
1427+
if (st_ops_assoc) {
1428+
if (prog->type != BPF_PROG_TYPE_STRUCT_OPS)
1429+
return -EBUSY;
1430+
1431+
rcu_assign_pointer(prog->aux->st_ops_assoc, BPF_PTR_POISON);
1432+
} else {
1433+
/*
1434+
* struct_ops map does not track associated non-struct_ops programs.
1435+
* Bump the refcount to make sure st_ops_assoc is always valid.
1436+
*/
1437+
if (prog->type != BPF_PROG_TYPE_STRUCT_OPS)
1438+
bpf_map_inc(map);
1439+
1440+
rcu_assign_pointer(prog->aux->st_ops_assoc, map);
1441+
}
1442+
1443+
return 0;
1444+
}
1445+
1446+
void bpf_prog_disassoc_struct_ops(struct bpf_prog *prog)
1447+
{
1448+
struct bpf_map *st_ops_assoc;
1449+
1450+
guard(mutex)(&prog->aux->st_ops_assoc_mutex);
1451+
1452+
st_ops_assoc = rcu_dereference_protected(prog->aux->st_ops_assoc,
1453+
lockdep_is_held(&prog->aux->st_ops_assoc_mutex));
1454+
if (!st_ops_assoc || st_ops_assoc == BPF_PTR_POISON)
1455+
return;
1456+
1457+
if (prog->type != BPF_PROG_TYPE_STRUCT_OPS)
1458+
bpf_map_put(st_ops_assoc);
1459+
1460+
RCU_INIT_POINTER(prog->aux->st_ops_assoc, NULL);
1461+
}
1462+
1463+
/*
1464+
* Get a reference to the struct_ops struct (i.e., kdata) associated with a
1465+
* program. Should only be called in BPF program context (e.g., in a kfunc).
1466+
*
1467+
* If the returned pointer is not NULL, it must points to a valid and
1468+
* initialized struct_ops. The struct_ops may or may not be attached.
1469+
* Kernel struct_ops implementers are responsible for tracking and checking
1470+
* the state of the struct_ops if the use case requires an attached struct_ops.
1471+
*/
1472+
void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux)
1473+
{
1474+
struct bpf_struct_ops_map *st_map;
1475+
struct bpf_map *st_ops_assoc;
1476+
1477+
st_ops_assoc = rcu_dereference_check(aux->st_ops_assoc, bpf_rcu_lock_held());
1478+
if (!st_ops_assoc || st_ops_assoc == BPF_PTR_POISON)
1479+
return NULL;
1480+
1481+
st_map = (struct bpf_struct_ops_map *)st_ops_assoc;
1482+
if (smp_load_acquire(&st_map->kvalue.common.state) == BPF_STRUCT_OPS_STATE_INIT)
1483+
return NULL;
1484+
1485+
return &st_map->kvalue.data;
1486+
}
1487+
EXPORT_SYMBOL_GPL(bpf_prog_get_assoc_struct_ops);
1488+
13971489
void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
13981490
{
13991491
struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;

kernel/bpf/core.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag
136136
mutex_init(&fp->aux->used_maps_mutex);
137137
mutex_init(&fp->aux->ext_mutex);
138138
mutex_init(&fp->aux->dst_mutex);
139+
mutex_init(&fp->aux->st_ops_assoc_mutex);
139140

140141
#ifdef CONFIG_BPF_SYSCALL
141142
bpf_prog_stream_init(fp);
@@ -286,6 +287,7 @@ void __bpf_prog_free(struct bpf_prog *fp)
286287
if (fp->aux) {
287288
mutex_destroy(&fp->aux->used_maps_mutex);
288289
mutex_destroy(&fp->aux->dst_mutex);
290+
mutex_destroy(&fp->aux->st_ops_assoc_mutex);
289291
kfree(fp->aux->poke_tab);
290292
kfree(fp->aux);
291293
}
@@ -2896,6 +2898,7 @@ static void bpf_prog_free_deferred(struct work_struct *work)
28962898
#endif
28972899
bpf_free_used_maps(aux);
28982900
bpf_free_used_btfs(aux);
2901+
bpf_prog_disassoc_struct_ops(aux->prog);
28992902
if (bpf_prog_is_dev_bound(aux))
29002903
bpf_prog_dev_bound_destroy(aux->prog);
29012904
#ifdef CONFIG_PERF_EVENTS

kernel/bpf/syscall.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6129,6 +6129,49 @@ static int prog_stream_read(union bpf_attr *attr)
61296129
return ret;
61306130
}
61316131

6132+
#define BPF_PROG_ASSOC_STRUCT_OPS_LAST_FIELD prog_assoc_struct_ops.prog_fd
6133+
6134+
static int prog_assoc_struct_ops(union bpf_attr *attr)
6135+
{
6136+
struct bpf_prog *prog;
6137+
struct bpf_map *map;
6138+
int ret;
6139+
6140+
if (CHECK_ATTR(BPF_PROG_ASSOC_STRUCT_OPS))
6141+
return -EINVAL;
6142+
6143+
if (attr->prog_assoc_struct_ops.flags)
6144+
return -EINVAL;
6145+
6146+
prog = bpf_prog_get(attr->prog_assoc_struct_ops.prog_fd);
6147+
if (IS_ERR(prog))
6148+
return PTR_ERR(prog);
6149+
6150+
if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
6151+
ret = -EINVAL;
6152+
goto put_prog;
6153+
}
6154+
6155+
map = bpf_map_get(attr->prog_assoc_struct_ops.map_fd);
6156+
if (IS_ERR(map)) {
6157+
ret = PTR_ERR(map);
6158+
goto put_prog;
6159+
}
6160+
6161+
if (map->map_type != BPF_MAP_TYPE_STRUCT_OPS) {
6162+
ret = -EINVAL;
6163+
goto put_map;
6164+
}
6165+
6166+
ret = bpf_prog_assoc_struct_ops(prog, map);
6167+
6168+
put_map:
6169+
bpf_map_put(map);
6170+
put_prog:
6171+
bpf_prog_put(prog);
6172+
return ret;
6173+
}
6174+
61326175
static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)
61336176
{
61346177
union bpf_attr attr;
@@ -6268,6 +6311,9 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)
62686311
case BPF_PROG_STREAM_READ_BY_FD:
62696312
err = prog_stream_read(&attr);
62706313
break;
6314+
case BPF_PROG_ASSOC_STRUCT_OPS:
6315+
err = prog_assoc_struct_ops(&attr);
6316+
break;
62716317
default:
62726318
err = -EINVAL;
62736319
break;

tools/include/uapi/linux/bpf.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,16 @@ union bpf_iter_link_info {
918918
* Number of bytes read from the stream on success, or -1 if an
919919
* error occurred (in which case, *errno* is set appropriately).
920920
*
921+
* BPF_PROG_ASSOC_STRUCT_OPS
922+
* Description
923+
* Associate a BPF program with a struct_ops map. The struct_ops
924+
* map is identified by *map_fd* and the BPF program is
925+
* identified by *prog_fd*.
926+
*
927+
* Return
928+
* 0 on success or -1 if an error occurred (in which case,
929+
* *errno* is set appropriately).
930+
*
921931
* NOTES
922932
* eBPF objects (maps and programs) can be shared between processes.
923933
*
@@ -974,6 +984,7 @@ enum bpf_cmd {
974984
BPF_PROG_BIND_MAP,
975985
BPF_TOKEN_CREATE,
976986
BPF_PROG_STREAM_READ_BY_FD,
987+
BPF_PROG_ASSOC_STRUCT_OPS,
977988
__MAX_BPF_CMD,
978989
};
979990

@@ -1894,6 +1905,12 @@ union bpf_attr {
18941905
__u32 prog_fd;
18951906
} prog_stream_read;
18961907

1908+
struct {
1909+
__u32 map_fd;
1910+
__u32 prog_fd;
1911+
__u32 flags;
1912+
} prog_assoc_struct_ops;
1913+
18971914
} __attribute__((aligned(8)));
18981915

18991916
/* The description below is an attempt at providing documentation to eBPF

0 commit comments

Comments
 (0)