Skip to content

Commit 3f9ca60

Browse files
ameryhungKernel Patches Daemon
authored andcommitted
bpf: Support associating BPF program with struct_ops
Add a new BPF command BPF_STRUCT_OPS_ASSOCIATE_PROG to allow associating a BPF program with a struct_ops. 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, the first one will be the map associated with the program. 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 76be20e commit 3f9ca60

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-0
lines changed

include/linux/bpf.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,8 @@ struct bpf_prog_aux {
17101710
struct rcu_head rcu;
17111711
};
17121712
struct bpf_stream stream[2];
1713+
struct mutex st_ops_assoc_mutex;
1714+
void *st_ops_assoc;
17131715
};
17141716

17151717
struct bpf_prog {
@@ -2010,6 +2012,8 @@ static inline void bpf_module_put(const void *data, struct module *owner)
20102012
module_put(owner);
20112013
}
20122014
int bpf_struct_ops_link_create(union bpf_attr *attr);
2015+
int bpf_struct_ops_assoc_prog(struct bpf_map *map, struct bpf_prog *prog);
2016+
void bpf_struct_ops_disassoc_prog(struct bpf_prog *prog);
20132017
u32 bpf_struct_ops_id(const void *kdata);
20142018

20152019
#ifdef CONFIG_NET
@@ -2057,6 +2061,13 @@ static inline int bpf_struct_ops_link_create(union bpf_attr *attr)
20572061
{
20582062
return -EOPNOTSUPP;
20592063
}
2064+
static inline void bpf_struct_ops_assoc_prog(struct bpf_map *map, struct bpf_prog *prog)
2065+
{
2066+
return -EOPNOTSUPP;
2067+
}
2068+
static inline void bpf_struct_ops_disassoc_prog(struct bpf_prog *prog)
2069+
{
2070+
}
20602071
static inline void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
20612072
{
20622073
}

include/uapi/linux/bpf.h

Lines changed: 16 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_STRUCT_OPS_ASSOCIATE_PROG
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_STRUCT_OPS_ASSOCIATE_PROG,
977988
__MAX_BPF_CMD,
978989
};
979990

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

1904+
struct {
1905+
__u32 map_fd;
1906+
__u32 prog_fd;
1907+
} struct_ops_assoc_prog;
1908+
18931909
} __attribute__((aligned(8)));
18941910

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

kernel/bpf/bpf_struct_ops.c

Lines changed: 32 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_struct_ops_disassoc_prog(st_map->links[i]->prog);
531532
bpf_link_put(st_map->links[i]);
532533
st_map->links[i] = NULL;
533534
}
@@ -801,6 +802,11 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
801802
goto reset_unlock;
802803
}
803804

805+
/* Don't stop a program from being reused. prog->aux->st_ops_assoc
806+
* will point to the first struct_ops kdata.
807+
*/
808+
bpf_struct_ops_assoc_prog(&st_map->map, prog);
809+
804810
link = kzalloc(sizeof(*link), GFP_USER);
805811
if (!link) {
806812
bpf_prog_put(prog);
@@ -1394,6 +1400,32 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
13941400
return err;
13951401
}
13961402

1403+
int bpf_struct_ops_assoc_prog(struct bpf_map *map, struct bpf_prog *prog)
1404+
{
1405+
struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
1406+
void *kdata = &st_map->kvalue.data;
1407+
int ret = 0;
1408+
1409+
mutex_lock(&prog->aux->st_ops_assoc_mutex);
1410+
1411+
if (prog->aux->st_ops_assoc && prog->aux->st_ops_assoc != kdata) {
1412+
ret = -EBUSY;
1413+
goto out;
1414+
}
1415+
1416+
prog->aux->st_ops_assoc = kdata;
1417+
out:
1418+
mutex_unlock(&prog->aux->st_ops_assoc_mutex);
1419+
return ret;
1420+
}
1421+
1422+
void bpf_struct_ops_disassoc_prog(struct bpf_prog *prog)
1423+
{
1424+
mutex_lock(&prog->aux->st_ops_assoc_mutex);
1425+
prog->aux->st_ops_assoc = NULL;
1426+
mutex_unlock(&prog->aux->st_ops_assoc_mutex);
1427+
}
1428+
13971429
void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
13981430
{
13991431
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_struct_ops_disassoc_prog(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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6092,6 +6092,41 @@ static int prog_stream_read(union bpf_attr *attr)
60926092
return ret;
60936093
}
60946094

6095+
#define BPF_STRUCT_OPS_ASSOCIATE_PROG_LAST_FIELD struct_ops_assoc_prog.prog_fd
6096+
6097+
static int struct_ops_assoc_prog(union bpf_attr *attr)
6098+
{
6099+
struct bpf_prog *prog;
6100+
struct bpf_map *map;
6101+
int ret;
6102+
6103+
if (CHECK_ATTR(BPF_STRUCT_OPS_ASSOCIATE_PROG))
6104+
return -EINVAL;
6105+
6106+
prog = bpf_prog_get(attr->struct_ops_assoc_prog.prog_fd);
6107+
if (IS_ERR(prog))
6108+
return PTR_ERR(prog);
6109+
6110+
map = bpf_map_get(attr->struct_ops_assoc_prog.map_fd);
6111+
if (IS_ERR(map)) {
6112+
ret = PTR_ERR(map);
6113+
goto out;
6114+
}
6115+
6116+
if (map->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
6117+
prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
6118+
ret = -EINVAL;
6119+
goto out;
6120+
}
6121+
6122+
ret = bpf_struct_ops_assoc_prog(map, prog);
6123+
out:
6124+
if (ret && !IS_ERR(map))
6125+
bpf_map_put(map);
6126+
bpf_prog_put(prog);
6127+
return ret;
6128+
}
6129+
60956130
static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)
60966131
{
60976132
union bpf_attr attr;
@@ -6231,6 +6266,9 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)
62316266
case BPF_PROG_STREAM_READ_BY_FD:
62326267
err = prog_stream_read(&attr);
62336268
break;
6269+
case BPF_STRUCT_OPS_ASSOCIATE_PROG:
6270+
err = struct_ops_assoc_prog(&attr);
6271+
break;
62346272
default:
62356273
err = -EINVAL;
62366274
break;

tools/include/uapi/linux/bpf.h

Lines changed: 16 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_STRUCT_OPS_ASSOCIATE_PROG
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_STRUCT_OPS_ASSOCIATE_PROG,
977988
__MAX_BPF_CMD,
978989
};
979990

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

1904+
struct {
1905+
__u32 map_fd;
1906+
__u32 prog_fd;
1907+
} struct_ops_assoc_prog;
1908+
18931909
} __attribute__((aligned(8)));
18941910

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

0 commit comments

Comments
 (0)