Skip to content

Commit 911c035

Browse files
author
Martin KaFai Lau
committed
Merge branch 'allow-struct_ops-to-create-map-id-to'
Amery Hung says: ==================== This patchset allows struct_ops implementors to get map id from kdata in reg(), unreg() and update() so that they create an id to struct_ops instance mapping. This in turn allows struct_ops kfuncs to refer to the calling instance without passing a pointer to the struct_ops. The selftest provides an end-to-end example. Some struct_ops users extend themselves with other bpf programs, which also need to call struct_ops kfuncs. For example, scx_layered uses syscall bpf programs as a scx_layered specific control plane and uses tracing programs to get additional information for scheduling [0]. The kfuncs may need to refer to the struct_ops instance and perform jobs accordingly. To allow calling struct_ops kfuncs referring to specific instances from different program types and context (e.g., struct_ops, tracing, async callbacks), the traditional way is to pass the struct_ops pointer to kfuncs. This patchset provides an alternative way, through a combination of bpf map id and global variable. First, a struct_ops implementor will use the map id of the struct_ops map as the id of an instance. Then, it needs to maintain an id to instance mapping: inserting a new mapping during reg() and removing it during unreg(). The map id can be acquired by calling bpf_struct_ops_id(). v1 -> v2 Add bpf_struct_ops_id() instead of using bpf_struct_ops_get() [0] https://github.com/sched-ext/scx/blob/main/scheds/rust/scx_layered/src/bpf/main.bpf.c ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
2 parents f3af62b + ba7000f commit 911c035

File tree

8 files changed

+325
-0
lines changed

8 files changed

+325
-0
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,7 @@ static inline void bpf_module_put(const void *data, struct module *owner)
19851985
module_put(owner);
19861986
}
19871987
int bpf_struct_ops_link_create(union bpf_attr *attr);
1988+
u32 bpf_struct_ops_id(const void *kdata);
19881989

19891990
#ifdef CONFIG_NET
19901991
/* Define it here to avoid the use of forward declaration */

kernel/bpf/bpf_struct_ops.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,18 @@ void bpf_struct_ops_put(const void *kdata)
11741174
bpf_map_put(&st_map->map);
11751175
}
11761176

1177+
u32 bpf_struct_ops_id(const void *kdata)
1178+
{
1179+
struct bpf_struct_ops_value *kvalue;
1180+
struct bpf_struct_ops_map *st_map;
1181+
1182+
kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
1183+
st_map = container_of(kvalue, struct bpf_struct_ops_map, kvalue);
1184+
1185+
return st_map->map.id;
1186+
}
1187+
EXPORT_SYMBOL_GPL(bpf_struct_ops_id);
1188+
11771189
static bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)
11781190
{
11791191
struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
#include "struct_ops_id_ops_mapping1.skel.h"
5+
#include "struct_ops_id_ops_mapping2.skel.h"
6+
7+
static void test_st_ops_id_ops_mapping(void)
8+
{
9+
struct struct_ops_id_ops_mapping1 *skel1 = NULL;
10+
struct struct_ops_id_ops_mapping2 *skel2 = NULL;
11+
struct bpf_map_info info = {};
12+
__u32 len = sizeof(info);
13+
int err, pid, prog1_fd, prog2_fd;
14+
15+
skel1 = struct_ops_id_ops_mapping1__open_and_load();
16+
if (!ASSERT_OK_PTR(skel1, "struct_ops_id_ops_mapping1__open"))
17+
goto out;
18+
19+
skel2 = struct_ops_id_ops_mapping2__open_and_load();
20+
if (!ASSERT_OK_PTR(skel2, "struct_ops_id_ops_mapping2__open"))
21+
goto out;
22+
23+
err = bpf_map_get_info_by_fd(bpf_map__fd(skel1->maps.st_ops_map),
24+
&info, &len);
25+
if (!ASSERT_OK(err, "bpf_map_get_info_by_fd"))
26+
goto out;
27+
28+
skel1->bss->st_ops_id = info.id;
29+
30+
err = bpf_map_get_info_by_fd(bpf_map__fd(skel2->maps.st_ops_map),
31+
&info, &len);
32+
if (!ASSERT_OK(err, "bpf_map_get_info_by_fd"))
33+
goto out;
34+
35+
skel2->bss->st_ops_id = info.id;
36+
37+
err = struct_ops_id_ops_mapping1__attach(skel1);
38+
if (!ASSERT_OK(err, "struct_ops_id_ops_mapping1__attach"))
39+
goto out;
40+
41+
err = struct_ops_id_ops_mapping2__attach(skel2);
42+
if (!ASSERT_OK(err, "struct_ops_id_ops_mapping2__attach"))
43+
goto out;
44+
45+
/* run tracing prog that calls .test_1 and checks return */
46+
pid = getpid();
47+
skel1->bss->test_pid = pid;
48+
skel2->bss->test_pid = pid;
49+
sys_gettid();
50+
skel1->bss->test_pid = 0;
51+
skel2->bss->test_pid = 0;
52+
53+
/* run syscall_prog that calls .test_1 and checks return */
54+
prog1_fd = bpf_program__fd(skel1->progs.syscall_prog);
55+
err = bpf_prog_test_run_opts(prog1_fd, NULL);
56+
ASSERT_OK(err, "bpf_prog_test_run_opts");
57+
58+
prog2_fd = bpf_program__fd(skel2->progs.syscall_prog);
59+
err = bpf_prog_test_run_opts(prog2_fd, NULL);
60+
ASSERT_OK(err, "bpf_prog_test_run_opts");
61+
62+
ASSERT_EQ(skel1->bss->test_err, 0, "skel1->bss->test_err");
63+
ASSERT_EQ(skel2->bss->test_err, 0, "skel2->bss->test_err");
64+
65+
out:
66+
struct_ops_id_ops_mapping1__destroy(skel1);
67+
struct_ops_id_ops_mapping2__destroy(skel2);
68+
}
69+
70+
void test_struct_ops_id_ops_mapping(void)
71+
{
72+
if (test__start_subtest("st_ops_id_ops_mapping"))
73+
test_st_ops_id_ops_mapping();
74+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_tracing.h>
5+
#include "bpf_misc.h"
6+
#include "../test_kmods/bpf_testmod.h"
7+
#include "../test_kmods/bpf_testmod_kfunc.h"
8+
9+
char _license[] SEC("license") = "GPL";
10+
11+
#define bpf_kfunc_multi_st_ops_test_1(args) bpf_kfunc_multi_st_ops_test_1(args, st_ops_id)
12+
int st_ops_id;
13+
14+
int test_pid;
15+
int test_err;
16+
17+
#define MAP1_MAGIC 1234
18+
19+
SEC("struct_ops")
20+
int BPF_PROG(test_1, struct st_ops_args *args)
21+
{
22+
return MAP1_MAGIC;
23+
}
24+
25+
SEC("tp_btf/sys_enter")
26+
int BPF_PROG(sys_enter, struct pt_regs *regs, long id)
27+
{
28+
struct st_ops_args args = {};
29+
struct task_struct *task;
30+
int ret;
31+
32+
task = bpf_get_current_task_btf();
33+
if (!test_pid || task->pid != test_pid)
34+
return 0;
35+
36+
ret = bpf_kfunc_multi_st_ops_test_1(&args);
37+
if (ret != MAP1_MAGIC)
38+
test_err++;
39+
40+
return 0;
41+
}
42+
43+
SEC("syscall")
44+
int syscall_prog(void *ctx)
45+
{
46+
struct st_ops_args args = {};
47+
int ret;
48+
49+
ret = bpf_kfunc_multi_st_ops_test_1(&args);
50+
if (ret != MAP1_MAGIC)
51+
test_err++;
52+
53+
return 0;
54+
}
55+
56+
SEC(".struct_ops.link")
57+
struct bpf_testmod_multi_st_ops st_ops_map = {
58+
.test_1 = (void *)test_1,
59+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_tracing.h>
5+
#include "bpf_misc.h"
6+
#include "../test_kmods/bpf_testmod.h"
7+
#include "../test_kmods/bpf_testmod_kfunc.h"
8+
9+
char _license[] SEC("license") = "GPL";
10+
11+
#define bpf_kfunc_multi_st_ops_test_1(args) bpf_kfunc_multi_st_ops_test_1(args, st_ops_id)
12+
int st_ops_id;
13+
14+
int test_pid;
15+
int test_err;
16+
17+
#define MAP2_MAGIC 4567
18+
19+
SEC("struct_ops")
20+
int BPF_PROG(test_1, struct st_ops_args *args)
21+
{
22+
return MAP2_MAGIC;
23+
}
24+
25+
SEC("tp_btf/sys_enter")
26+
int BPF_PROG(sys_enter, struct pt_regs *regs, long id)
27+
{
28+
struct st_ops_args args = {};
29+
struct task_struct *task;
30+
int ret;
31+
32+
task = bpf_get_current_task_btf();
33+
if (!test_pid || task->pid != test_pid)
34+
return 0;
35+
36+
ret = bpf_kfunc_multi_st_ops_test_1(&args);
37+
if (ret != MAP2_MAGIC)
38+
test_err++;
39+
40+
return 0;
41+
}
42+
43+
SEC("syscall")
44+
int syscall_prog(void *ctx)
45+
{
46+
struct st_ops_args args = {};
47+
int ret;
48+
49+
ret = bpf_kfunc_multi_st_ops_test_1(&args);
50+
if (ret != MAP2_MAGIC)
51+
test_err++;
52+
53+
return 0;
54+
}
55+
56+
SEC(".struct_ops.link")
57+
struct bpf_testmod_multi_st_ops st_ops_map = {
58+
.test_1 = (void *)test_1,
59+
};

tools/testing/selftests/bpf/test_kmods/bpf_testmod.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,8 @@ __bpf_kfunc int bpf_kfunc_st_ops_inc10(struct st_ops_args *args)
10571057
return args->a;
10581058
}
10591059

1060+
__bpf_kfunc int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id);
1061+
10601062
BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids)
10611063
BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc)
10621064
BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
@@ -1097,6 +1099,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_prologue, KF_TRUSTED_ARGS | KF_SLEEPABL
10971099
BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_epilogue, KF_TRUSTED_ARGS | KF_SLEEPABLE)
10981100
BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_pro_epilogue, KF_TRUSTED_ARGS | KF_SLEEPABLE)
10991101
BTF_ID_FLAGS(func, bpf_kfunc_st_ops_inc10, KF_TRUSTED_ARGS)
1102+
BTF_ID_FLAGS(func, bpf_kfunc_multi_st_ops_test_1, KF_TRUSTED_ARGS)
11001103
BTF_KFUNCS_END(bpf_testmod_check_kfunc_ids)
11011104

11021105
static int bpf_testmod_ops_init(struct btf *btf)
@@ -1528,6 +1531,114 @@ static struct bpf_struct_ops testmod_st_ops = {
15281531
.owner = THIS_MODULE,
15291532
};
15301533

1534+
struct hlist_head multi_st_ops_list;
1535+
static DEFINE_SPINLOCK(multi_st_ops_lock);
1536+
1537+
static int multi_st_ops_init(struct btf *btf)
1538+
{
1539+
spin_lock_init(&multi_st_ops_lock);
1540+
INIT_HLIST_HEAD(&multi_st_ops_list);
1541+
1542+
return 0;
1543+
}
1544+
1545+
static int multi_st_ops_init_member(const struct btf_type *t,
1546+
const struct btf_member *member,
1547+
void *kdata, const void *udata)
1548+
{
1549+
return 0;
1550+
}
1551+
1552+
static struct bpf_testmod_multi_st_ops *multi_st_ops_find_nolock(u32 id)
1553+
{
1554+
struct bpf_testmod_multi_st_ops *st_ops;
1555+
1556+
hlist_for_each_entry(st_ops, &multi_st_ops_list, node) {
1557+
if (st_ops->id == id)
1558+
return st_ops;
1559+
}
1560+
1561+
return NULL;
1562+
}
1563+
1564+
int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id)
1565+
{
1566+
struct bpf_testmod_multi_st_ops *st_ops;
1567+
unsigned long flags;
1568+
int ret = -1;
1569+
1570+
spin_lock_irqsave(&multi_st_ops_lock, flags);
1571+
st_ops = multi_st_ops_find_nolock(id);
1572+
if (st_ops)
1573+
ret = st_ops->test_1(args);
1574+
spin_unlock_irqrestore(&multi_st_ops_lock, flags);
1575+
1576+
return ret;
1577+
}
1578+
1579+
static int multi_st_ops_reg(void *kdata, struct bpf_link *link)
1580+
{
1581+
struct bpf_testmod_multi_st_ops *st_ops =
1582+
(struct bpf_testmod_multi_st_ops *)kdata;
1583+
unsigned long flags;
1584+
int err = 0;
1585+
u32 id;
1586+
1587+
if (!st_ops->test_1)
1588+
return -EINVAL;
1589+
1590+
id = bpf_struct_ops_id(kdata);
1591+
1592+
spin_lock_irqsave(&multi_st_ops_lock, flags);
1593+
if (multi_st_ops_find_nolock(id)) {
1594+
pr_err("multi_st_ops(id:%d) has already been registered\n", id);
1595+
err = -EEXIST;
1596+
goto unlock;
1597+
}
1598+
1599+
st_ops->id = id;
1600+
hlist_add_head(&st_ops->node, &multi_st_ops_list);
1601+
unlock:
1602+
spin_unlock_irqrestore(&multi_st_ops_lock, flags);
1603+
1604+
return err;
1605+
}
1606+
1607+
static void multi_st_ops_unreg(void *kdata, struct bpf_link *link)
1608+
{
1609+
struct bpf_testmod_multi_st_ops *st_ops;
1610+
unsigned long flags;
1611+
u32 id;
1612+
1613+
id = bpf_struct_ops_id(kdata);
1614+
1615+
spin_lock_irqsave(&multi_st_ops_lock, flags);
1616+
st_ops = multi_st_ops_find_nolock(id);
1617+
if (st_ops)
1618+
hlist_del(&st_ops->node);
1619+
spin_unlock_irqrestore(&multi_st_ops_lock, flags);
1620+
}
1621+
1622+
static int bpf_testmod_multi_st_ops__test_1(struct st_ops_args *args)
1623+
{
1624+
return 0;
1625+
}
1626+
1627+
static struct bpf_testmod_multi_st_ops multi_st_ops_cfi_stubs = {
1628+
.test_1 = bpf_testmod_multi_st_ops__test_1,
1629+
};
1630+
1631+
struct bpf_struct_ops testmod_multi_st_ops = {
1632+
.verifier_ops = &bpf_testmod_verifier_ops,
1633+
.init = multi_st_ops_init,
1634+
.init_member = multi_st_ops_init_member,
1635+
.reg = multi_st_ops_reg,
1636+
.unreg = multi_st_ops_unreg,
1637+
.cfi_stubs = &multi_st_ops_cfi_stubs,
1638+
.name = "bpf_testmod_multi_st_ops",
1639+
.owner = THIS_MODULE,
1640+
};
1641+
15311642
extern int bpf_fentry_test1(int a);
15321643

15331644
static int bpf_testmod_init(void)
@@ -1550,6 +1661,7 @@ static int bpf_testmod_init(void)
15501661
ret = ret ?: register_bpf_struct_ops(&bpf_testmod_ops2, bpf_testmod_ops2);
15511662
ret = ret ?: register_bpf_struct_ops(&bpf_testmod_ops3, bpf_testmod_ops3);
15521663
ret = ret ?: register_bpf_struct_ops(&testmod_st_ops, bpf_testmod_st_ops);
1664+
ret = ret ?: register_bpf_struct_ops(&testmod_multi_st_ops, bpf_testmod_multi_st_ops);
15531665
ret = ret ?: register_btf_id_dtor_kfuncs(bpf_testmod_dtors,
15541666
ARRAY_SIZE(bpf_testmod_dtors),
15551667
THIS_MODULE);

tools/testing/selftests/bpf/test_kmods/bpf_testmod.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,10 @@ struct bpf_testmod_st_ops {
116116
struct module *owner;
117117
};
118118

119+
struct bpf_testmod_multi_st_ops {
120+
int (*test_1)(struct st_ops_args *args);
121+
struct hlist_node node;
122+
int id;
123+
};
124+
119125
#endif /* _BPF_TESTMOD_H */

tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,6 @@ void bpf_kfunc_trusted_task_test(struct task_struct *ptr) __ksym;
159159
void bpf_kfunc_trusted_num_test(int *ptr) __ksym;
160160
void bpf_kfunc_rcu_task_test(struct task_struct *ptr) __ksym;
161161

162+
int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id) __ksym;
163+
162164
#endif /* _BPF_TESTMOD_KFUNC_H */

0 commit comments

Comments
 (0)