Skip to content

Commit 13d637f

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 or 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. A kernel helper bpf_prog_get_assoc_struct_ops() can be used to retrieve the pointer. 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. Each associated programs except struct_ops programs of the map will take a refcount on the map to pin it so that prog->aux->st_ops_assoc, if set, is always valid. However, it is not guaranteed whether the map members are fully updated nor is it attached or not. For example, a BPF program can be associated with a struct_ops map before map_update. The struct_ops implementer will be responsible for maintaining and checking the state of the associated struct_ops map before accessing it. Signed-off-by: Amery Hung <[email protected]>
1 parent 13cd013 commit 13d637f

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

include/linux/bpf.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,8 @@ struct bpf_prog_aux {
17171717
struct rcu_head rcu;
17181718
};
17191719
struct bpf_stream stream[2];
1720+
struct mutex st_ops_assoc_mutex;
1721+
void *st_ops_assoc;
17201722
};
17211723

17221724
struct bpf_prog {
@@ -2017,6 +2019,9 @@ static inline void bpf_module_put(const void *data, struct module *owner)
20172019
module_put(owner);
20182020
}
20192021
int bpf_struct_ops_link_create(union bpf_attr *attr);
2022+
int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map);
2023+
void bpf_prog_disassoc_struct_ops(struct bpf_prog *prog);
2024+
void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux);
20202025
u32 bpf_struct_ops_id(const void *kdata);
20212026

20222027
#ifdef CONFIG_NET
@@ -2064,6 +2069,17 @@ static inline int bpf_struct_ops_link_create(union bpf_attr *attr)
20642069
{
20652070
return -EOPNOTSUPP;
20662071
}
2072+
static inline int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map)
2073+
{
2074+
return -EOPNOTSUPP;
2075+
}
2076+
static inline void bpf_prog_disassoc_struct_ops(struct bpf_prog *prog)
2077+
{
2078+
}
2079+
static inline void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux)
2080+
{
2081+
return NULL;
2082+
}
20672083
static inline void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
20682084
{
20692085
}

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

@@ -1890,6 +1901,12 @@ union bpf_attr {
18901901
__u32 prog_fd;
18911902
} prog_stream_read;
18921903

1904+
struct {
1905+
__u32 map_fd;
1906+
__u32 prog_fd;
1907+
__u32 flags;
1908+
} prog_assoc_struct_ops;
1909+
18931910
} __attribute__((aligned(8)));
18941911

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

kernel/bpf/bpf_struct_ops.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)
528528
for (i = 0; i < st_map->funcs_cnt; i++) {
529529
if (!st_map->links[i])
530530
break;
531+
bpf_prog_disassoc_struct_ops(st_map->links[i]->prog);
531532
bpf_link_put(st_map->links[i]);
532533
st_map->links[i] = NULL;
533534
}
@@ -801,6 +802,9 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
801802
goto reset_unlock;
802803
}
803804

805+
/* If the program is reused, prog->aux->st_ops_assoc will be poisoned */
806+
bpf_prog_assoc_struct_ops(prog, &st_map->map);
807+
804808
link = kzalloc(sizeof(*link), GFP_USER);
805809
if (!link) {
806810
bpf_prog_put(prog);
@@ -1394,6 +1398,46 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
13941398
return err;
13951399
}
13961400

1401+
int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map)
1402+
{
1403+
struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
1404+
void *kdata = &st_map->kvalue.data;
1405+
int ret = 0;
1406+
1407+
mutex_lock(&prog->aux->st_ops_assoc_mutex);
1408+
1409+
if (prog->aux->st_ops_assoc && prog->aux->st_ops_assoc != kdata) {
1410+
if (prog->type == BPF_PROG_TYPE_STRUCT_OPS)
1411+
WRITE_ONCE(prog->aux->st_ops_assoc, BPF_PTR_POISON);
1412+
1413+
ret = -EBUSY;
1414+
goto out;
1415+
}
1416+
1417+
WRITE_ONCE(prog->aux->st_ops_assoc, kdata);
1418+
out:
1419+
mutex_unlock(&prog->aux->st_ops_assoc_mutex);
1420+
return ret;
1421+
}
1422+
1423+
void bpf_prog_disassoc_struct_ops(struct bpf_prog *prog)
1424+
{
1425+
mutex_lock(&prog->aux->st_ops_assoc_mutex);
1426+
WRITE_ONCE(prog->aux->st_ops_assoc, NULL);
1427+
mutex_unlock(&prog->aux->st_ops_assoc_mutex);
1428+
}
1429+
1430+
void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux)
1431+
{
1432+
void *st_ops_assoc = READ_ONCE(aux->st_ops_assoc);
1433+
1434+
if (!st_ops_assoc || st_ops_assoc == BPF_PTR_POISON)
1435+
return NULL;
1436+
1437+
return st_ops_assoc;
1438+
}
1439+
EXPORT_SYMBOL_GPL(bpf_prog_get_assoc_struct_ops);
1440+
13971441
void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
13981442
{
13991443
struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;

kernel/bpf/core.c

Lines changed: 6 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
}
@@ -2875,6 +2877,10 @@ static void bpf_prog_free_deferred(struct work_struct *work)
28752877
#endif
28762878
bpf_free_used_maps(aux);
28772879
bpf_free_used_btfs(aux);
2880+
if (aux->st_ops_assoc) {
2881+
bpf_struct_ops_put(aux->st_ops_assoc);
2882+
bpf_prog_disassoc_struct_ops(aux->prog);
2883+
}
28782884
if (bpf_prog_is_dev_bound(aux))
28792885
bpf_prog_dev_bound_destroy(aux->prog);
28802886
#ifdef CONFIG_PERF_EVENTS

kernel/bpf/syscall.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6092,6 +6092,53 @@ static int prog_stream_read(union bpf_attr *attr)
60926092
return ret;
60936093
}
60946094

6095+
#define BPF_PROG_ASSOC_STRUCT_OPS_LAST_FIELD prog_assoc_struct_ops.prog_fd
6096+
6097+
static int prog_assoc_struct_ops(union bpf_attr *attr)
6098+
{
6099+
struct bpf_prog *prog;
6100+
struct bpf_map *map;
6101+
int ret;
6102+
6103+
if (CHECK_ATTR(BPF_PROG_ASSOC_STRUCT_OPS))
6104+
return -EINVAL;
6105+
6106+
if (attr->prog_assoc_struct_ops.flags)
6107+
return -EINVAL;
6108+
6109+
prog = bpf_prog_get(attr->prog_assoc_struct_ops.prog_fd);
6110+
if (IS_ERR(prog))
6111+
return PTR_ERR(prog);
6112+
6113+
if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
6114+
ret = -EINVAL;
6115+
goto put_prog;
6116+
}
6117+
6118+
map = bpf_map_get(attr->prog_assoc_struct_ops.map_fd);
6119+
if (IS_ERR(map)) {
6120+
ret = PTR_ERR(map);
6121+
goto put_prog;
6122+
}
6123+
6124+
if (map->map_type != BPF_MAP_TYPE_STRUCT_OPS) {
6125+
ret = -EINVAL;
6126+
goto put_map;
6127+
}
6128+
6129+
ret = bpf_prog_assoc_struct_ops(prog, map);
6130+
if (ret)
6131+
goto put_map;
6132+
6133+
bpf_prog_put(prog);
6134+
return 0;
6135+
put_map:
6136+
bpf_map_put(map);
6137+
put_prog:
6138+
bpf_prog_put(prog);
6139+
return ret;
6140+
}
6141+
60956142
static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)
60966143
{
60976144
union bpf_attr attr;
@@ -6231,6 +6278,9 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)
62316278
case BPF_PROG_STREAM_READ_BY_FD:
62326279
err = prog_stream_read(&attr);
62336280
break;
6281+
case BPF_PROG_ASSOC_STRUCT_OPS:
6282+
err = prog_assoc_struct_ops(&attr);
6283+
break;
62346284
default:
62356285
err = -EINVAL;
62366286
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

@@ -1890,6 +1901,12 @@ union bpf_attr {
18901901
__u32 prog_fd;
18911902
} prog_stream_read;
18921903

1904+
struct {
1905+
__u32 map_fd;
1906+
__u32 prog_fd;
1907+
__u32 flags;
1908+
} prog_assoc_struct_ops;
1909+
18931910
} __attribute__((aligned(8)));
18941911

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

0 commit comments

Comments
 (0)