Skip to content

Commit 47e6943

Browse files
Martin KaFai LauAlexei Starovoitov
authored andcommitted
selftests/bpf: Test gen_prologue and gen_epilogue
This test adds a new struct_ops "bpf_testmod_st_ops" in bpf_testmod. The ops of the bpf_testmod_st_ops is triggered by new kfunc calls "bpf_kfunc_st_ops_test_*logue". These new kfunc calls are primarily used by the SEC("syscall") program. The test triggering sequence is like: SEC("syscall") syscall_prologue(struct st_ops_args *args) bpf_kfunc_st_op_test_prologue(args) st_ops->test_prologue(args) .gen_prologue adds 1000 to args->a .gen_epilogue adds 10000 to args->a .gen_epilogue will also set the r0 to 2 * args->a. The .gen_prologue and .gen_epilogue of the bpf_testmod_st_ops will test the prog->aux->attach_func_name to decide if it needs to generate codes. The main programs of the pro_epilogue.c will call a new kfunc bpf_kfunc_st_ops_inc10 which does "args->a += 10". It will also call a subprog() which does "args->a += 1". This patch uses the test_loader infra to check the __xlated instructions patched after gen_prologue and/or gen_epilogue. The __xlated check is based on Eduard's example (Thanks!) in v1. args->a is returned by the struct_ops prog (either the main prog or the epilogue). Thus, the __retval of the SEC("syscall") prog is checked. For example, when triggering the ops in the 'SEC("struct_ops/test_epilogue") int test_epilogue' The expected args->a is +1 (subprog call) + 10 (kfunc call) + 10000 (.gen_epilogue) = 10011. The expected return value is 2 * 10011 (.gen_epilogue). Suggested-by: Eduard Zingerman <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent a0dbf6d commit 47e6943

File tree

5 files changed

+371
-0
lines changed

5 files changed

+371
-0
lines changed

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

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/in.h>
1818
#include <linux/in6.h>
1919
#include <linux/un.h>
20+
#include <linux/filter.h>
2021
#include <net/sock.h>
2122
#include <linux/namei.h>
2223
#include "bpf_testmod.h"
@@ -945,6 +946,51 @@ __bpf_kfunc int bpf_kfunc_call_kernel_getpeername(struct addr_args *args)
945946
return err;
946947
}
947948

949+
static DEFINE_MUTEX(st_ops_mutex);
950+
static struct bpf_testmod_st_ops *st_ops;
951+
952+
__bpf_kfunc int bpf_kfunc_st_ops_test_prologue(struct st_ops_args *args)
953+
{
954+
int ret = -1;
955+
956+
mutex_lock(&st_ops_mutex);
957+
if (st_ops && st_ops->test_prologue)
958+
ret = st_ops->test_prologue(args);
959+
mutex_unlock(&st_ops_mutex);
960+
961+
return ret;
962+
}
963+
964+
__bpf_kfunc int bpf_kfunc_st_ops_test_epilogue(struct st_ops_args *args)
965+
{
966+
int ret = -1;
967+
968+
mutex_lock(&st_ops_mutex);
969+
if (st_ops && st_ops->test_epilogue)
970+
ret = st_ops->test_epilogue(args);
971+
mutex_unlock(&st_ops_mutex);
972+
973+
return ret;
974+
}
975+
976+
__bpf_kfunc int bpf_kfunc_st_ops_test_pro_epilogue(struct st_ops_args *args)
977+
{
978+
int ret = -1;
979+
980+
mutex_lock(&st_ops_mutex);
981+
if (st_ops && st_ops->test_pro_epilogue)
982+
ret = st_ops->test_pro_epilogue(args);
983+
mutex_unlock(&st_ops_mutex);
984+
985+
return ret;
986+
}
987+
988+
__bpf_kfunc int bpf_kfunc_st_ops_inc10(struct st_ops_args *args)
989+
{
990+
args->a += 10;
991+
return args->a;
992+
}
993+
948994
BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids)
949995
BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc)
950996
BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
@@ -981,6 +1027,10 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_sendmsg, KF_SLEEPABLE)
9811027
BTF_ID_FLAGS(func, bpf_kfunc_call_sock_sendmsg, KF_SLEEPABLE)
9821028
BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_getsockname, KF_SLEEPABLE)
9831029
BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_getpeername, KF_SLEEPABLE)
1030+
BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_prologue, KF_TRUSTED_ARGS | KF_SLEEPABLE)
1031+
BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_epilogue, KF_TRUSTED_ARGS | KF_SLEEPABLE)
1032+
BTF_ID_FLAGS(func, bpf_kfunc_st_ops_test_pro_epilogue, KF_TRUSTED_ARGS | KF_SLEEPABLE)
1033+
BTF_ID_FLAGS(func, bpf_kfunc_st_ops_inc10, KF_TRUSTED_ARGS)
9841034
BTF_KFUNCS_END(bpf_testmod_check_kfunc_ids)
9851035

9861036
static int bpf_testmod_ops_init(struct btf *btf)
@@ -1100,6 +1150,144 @@ struct bpf_struct_ops bpf_testmod_ops2 = {
11001150
.owner = THIS_MODULE,
11011151
};
11021152

1153+
static int bpf_test_mod_st_ops__test_prologue(struct st_ops_args *args)
1154+
{
1155+
return 0;
1156+
}
1157+
1158+
static int bpf_test_mod_st_ops__test_epilogue(struct st_ops_args *args)
1159+
{
1160+
return 0;
1161+
}
1162+
1163+
static int bpf_test_mod_st_ops__test_pro_epilogue(struct st_ops_args *args)
1164+
{
1165+
return 0;
1166+
}
1167+
1168+
static int st_ops_gen_prologue(struct bpf_insn *insn_buf, bool direct_write,
1169+
const struct bpf_prog *prog)
1170+
{
1171+
struct bpf_insn *insn = insn_buf;
1172+
1173+
if (strcmp(prog->aux->attach_func_name, "test_prologue") &&
1174+
strcmp(prog->aux->attach_func_name, "test_pro_epilogue"))
1175+
return 0;
1176+
1177+
/* r6 = r1[0]; // r6 will be "struct st_ops *args". r1 is "u64 *ctx".
1178+
* r7 = r6->a;
1179+
* r7 += 1000;
1180+
* r6->a = r7;
1181+
*/
1182+
*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0);
1183+
*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_6, offsetof(struct st_ops_args, a));
1184+
*insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, 1000);
1185+
*insn++ = BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_7, offsetof(struct st_ops_args, a));
1186+
*insn++ = prog->insnsi[0];
1187+
1188+
return insn - insn_buf;
1189+
}
1190+
1191+
static int st_ops_gen_epilogue(struct bpf_insn *insn_buf, const struct bpf_prog *prog,
1192+
s16 ctx_stack_off)
1193+
{
1194+
struct bpf_insn *insn = insn_buf;
1195+
1196+
if (strcmp(prog->aux->attach_func_name, "test_epilogue") &&
1197+
strcmp(prog->aux->attach_func_name, "test_pro_epilogue"))
1198+
return 0;
1199+
1200+
/* r1 = stack[ctx_stack_off]; // r1 will be "u64 *ctx"
1201+
* r1 = r1[0]; // r1 will be "struct st_ops *args"
1202+
* r6 = r1->a;
1203+
* r6 += 10000;
1204+
* r1->a = r6;
1205+
* r0 = r6;
1206+
* r0 *= 2;
1207+
* BPF_EXIT;
1208+
*/
1209+
*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_FP, ctx_stack_off);
1210+
*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
1211+
*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, offsetof(struct st_ops_args, a));
1212+
*insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, 10000);
1213+
*insn++ = BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, offsetof(struct st_ops_args, a));
1214+
*insn++ = BPF_MOV64_REG(BPF_REG_0, BPF_REG_6);
1215+
*insn++ = BPF_ALU64_IMM(BPF_MUL, BPF_REG_0, 2);
1216+
*insn++ = BPF_EXIT_INSN();
1217+
1218+
return insn - insn_buf;
1219+
}
1220+
1221+
static int st_ops_btf_struct_access(struct bpf_verifier_log *log,
1222+
const struct bpf_reg_state *reg,
1223+
int off, int size)
1224+
{
1225+
if (off < 0 || off + size > sizeof(struct st_ops_args))
1226+
return -EACCES;
1227+
return 0;
1228+
}
1229+
1230+
static const struct bpf_verifier_ops st_ops_verifier_ops = {
1231+
.is_valid_access = bpf_testmod_ops_is_valid_access,
1232+
.btf_struct_access = st_ops_btf_struct_access,
1233+
.gen_prologue = st_ops_gen_prologue,
1234+
.gen_epilogue = st_ops_gen_epilogue,
1235+
.get_func_proto = bpf_base_func_proto,
1236+
};
1237+
1238+
static struct bpf_testmod_st_ops st_ops_cfi_stubs = {
1239+
.test_prologue = bpf_test_mod_st_ops__test_prologue,
1240+
.test_epilogue = bpf_test_mod_st_ops__test_epilogue,
1241+
.test_pro_epilogue = bpf_test_mod_st_ops__test_pro_epilogue,
1242+
};
1243+
1244+
static int st_ops_reg(void *kdata, struct bpf_link *link)
1245+
{
1246+
int err = 0;
1247+
1248+
mutex_lock(&st_ops_mutex);
1249+
if (st_ops) {
1250+
pr_err("st_ops has already been registered\n");
1251+
err = -EEXIST;
1252+
goto unlock;
1253+
}
1254+
st_ops = kdata;
1255+
1256+
unlock:
1257+
mutex_unlock(&st_ops_mutex);
1258+
return err;
1259+
}
1260+
1261+
static void st_ops_unreg(void *kdata, struct bpf_link *link)
1262+
{
1263+
mutex_lock(&st_ops_mutex);
1264+
st_ops = NULL;
1265+
mutex_unlock(&st_ops_mutex);
1266+
}
1267+
1268+
static int st_ops_init(struct btf *btf)
1269+
{
1270+
return 0;
1271+
}
1272+
1273+
static int st_ops_init_member(const struct btf_type *t,
1274+
const struct btf_member *member,
1275+
void *kdata, const void *udata)
1276+
{
1277+
return 0;
1278+
}
1279+
1280+
static struct bpf_struct_ops testmod_st_ops = {
1281+
.verifier_ops = &st_ops_verifier_ops,
1282+
.init = st_ops_init,
1283+
.init_member = st_ops_init_member,
1284+
.reg = st_ops_reg,
1285+
.unreg = st_ops_unreg,
1286+
.cfi_stubs = &st_ops_cfi_stubs,
1287+
.name = "bpf_testmod_st_ops",
1288+
.owner = THIS_MODULE,
1289+
};
1290+
11031291
extern int bpf_fentry_test1(int a);
11041292

11051293
static int bpf_testmod_init(void)
@@ -1117,8 +1305,10 @@ static int bpf_testmod_init(void)
11171305
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
11181306
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_testmod_kfunc_set);
11191307
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_testmod_kfunc_set);
1308+
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_testmod_kfunc_set);
11201309
ret = ret ?: register_bpf_struct_ops(&bpf_bpf_testmod_ops, bpf_testmod_ops);
11211310
ret = ret ?: register_bpf_struct_ops(&bpf_testmod_ops2, bpf_testmod_ops2);
1311+
ret = ret ?: register_bpf_struct_ops(&testmod_st_ops, bpf_testmod_st_ops);
11221312
ret = ret ?: register_btf_id_dtor_kfuncs(bpf_testmod_dtors,
11231313
ARRAY_SIZE(bpf_testmod_dtors),
11241314
THIS_MODULE);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,15 @@ struct bpf_testmod_ops2 {
9494
int (*test_1)(void);
9595
};
9696

97+
struct st_ops_args {
98+
u64 a;
99+
};
100+
101+
struct bpf_testmod_st_ops {
102+
int (*test_prologue)(struct st_ops_args *args);
103+
int (*test_epilogue)(struct st_ops_args *args);
104+
int (*test_pro_epilogue)(struct st_ops_args *args);
105+
struct module *owner;
106+
};
107+
97108
#endif /* _BPF_TESTMOD_H */

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,10 @@ struct sk_buff *bpf_kfunc_nested_acquire_nonzero_offset_test(struct sk_buff_head
148148
struct sk_buff *bpf_kfunc_nested_acquire_zero_offset_test(struct sock_common *ptr) __ksym;
149149
void bpf_kfunc_nested_release_test(struct sk_buff *ptr) __ksym;
150150

151+
struct st_ops_args;
152+
int bpf_kfunc_st_ops_test_prologue(struct st_ops_args *args) __ksym;
153+
int bpf_kfunc_st_ops_test_epilogue(struct st_ops_args *args) __ksym;
154+
int bpf_kfunc_st_ops_test_pro_epilogue(struct st_ops_args *args) __ksym;
155+
int bpf_kfunc_st_ops_inc10(struct st_ops_args *args) __ksym;
156+
151157
#endif /* _BPF_TESTMOD_KFUNC_H */
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <test_progs.h>
5+
#include "pro_epilogue.skel.h"
6+
7+
void test_pro_epilogue(void)
8+
{
9+
RUN_TESTS(pro_epilogue);
10+
}

0 commit comments

Comments
 (0)