Skip to content

Commit 3661527

Browse files
kkdwivediKernel Patches Daemon
authored andcommitted
selftests/bpf: Add verifier tests for kptr
Reuse bpf_prog_test functions to test the support for PTR_TO_BTF_ID in BPF map case, including some tests that verify implementation sanity and corner cases. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
1 parent a1a73cd commit 3661527

File tree

3 files changed

+562
-7
lines changed

3 files changed

+562
-7
lines changed

net/bpf/test_run.c

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,12 @@ noinline void bpf_kfunc_call_memb_release(struct prog_test_member *p)
584584
{
585585
}
586586

587+
noinline struct prog_test_ref_kfunc *
588+
bpf_kfunc_call_test_kptr_get(struct prog_test_ref_kfunc **p, int a, int b)
589+
{
590+
return &prog_test_struct;
591+
}
592+
587593
struct prog_test_pass1 {
588594
int x0;
589595
struct {
@@ -669,6 +675,7 @@ BTF_ID(func, bpf_kfunc_call_test3)
669675
BTF_ID(func, bpf_kfunc_call_test_acquire)
670676
BTF_ID(func, bpf_kfunc_call_test_release)
671677
BTF_ID(func, bpf_kfunc_call_memb_release)
678+
BTF_ID(func, bpf_kfunc_call_test_kptr_get)
672679
BTF_ID(func, bpf_kfunc_call_test_pass_ctx)
673680
BTF_ID(func, bpf_kfunc_call_test_pass1)
674681
BTF_ID(func, bpf_kfunc_call_test_pass2)
@@ -682,6 +689,7 @@ BTF_SET_END(test_sk_check_kfunc_ids)
682689

683690
BTF_SET_START(test_sk_acquire_kfunc_ids)
684691
BTF_ID(func, bpf_kfunc_call_test_acquire)
692+
BTF_ID(func, bpf_kfunc_call_test_kptr_get)
685693
BTF_SET_END(test_sk_acquire_kfunc_ids)
686694

687695
BTF_SET_START(test_sk_release_kfunc_ids)
@@ -691,8 +699,13 @@ BTF_SET_END(test_sk_release_kfunc_ids)
691699

692700
BTF_SET_START(test_sk_ret_null_kfunc_ids)
693701
BTF_ID(func, bpf_kfunc_call_test_acquire)
702+
BTF_ID(func, bpf_kfunc_call_test_kptr_get)
694703
BTF_SET_END(test_sk_ret_null_kfunc_ids)
695704

705+
BTF_SET_START(test_sk_kptr_acquire_kfunc_ids)
706+
BTF_ID(func, bpf_kfunc_call_test_kptr_get)
707+
BTF_SET_END(test_sk_kptr_acquire_kfunc_ids)
708+
696709
static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,
697710
u32 size, u32 headroom, u32 tailroom)
698711
{
@@ -1579,14 +1592,36 @@ int bpf_prog_test_run_syscall(struct bpf_prog *prog,
15791592

15801593
static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = {
15811594
.owner = THIS_MODULE,
1582-
.check_set = &test_sk_check_kfunc_ids,
1583-
.acquire_set = &test_sk_acquire_kfunc_ids,
1584-
.release_set = &test_sk_release_kfunc_ids,
1585-
.ret_null_set = &test_sk_ret_null_kfunc_ids,
1595+
.check_set = &test_sk_check_kfunc_ids,
1596+
.acquire_set = &test_sk_acquire_kfunc_ids,
1597+
.release_set = &test_sk_release_kfunc_ids,
1598+
.ret_null_set = &test_sk_ret_null_kfunc_ids,
1599+
.kptr_acquire_set = &test_sk_kptr_acquire_kfunc_ids
15861600
};
15871601

1602+
BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids)
1603+
BTF_ID(struct, prog_test_ref_kfunc)
1604+
BTF_ID(func, bpf_kfunc_call_test_release)
1605+
BTF_ID(struct, prog_test_member)
1606+
BTF_ID(func, bpf_kfunc_call_memb_release)
1607+
15881608
static int __init bpf_prog_test_run_init(void)
15891609
{
1590-
return register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set);
1610+
const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = {
1611+
{
1612+
.btf_id = bpf_prog_test_dtor_kfunc_ids[0],
1613+
.kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1]
1614+
},
1615+
{
1616+
.btf_id = bpf_prog_test_dtor_kfunc_ids[2],
1617+
.kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[3],
1618+
},
1619+
};
1620+
int ret;
1621+
1622+
ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set);
1623+
return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc,
1624+
ARRAY_SIZE(bpf_prog_test_dtor_kfunc),
1625+
THIS_MODULE);
15911626
}
15921627
late_initcall(bpf_prog_test_run_init);

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
#define MAX_INSNS BPF_MAXINSNS
5454
#define MAX_TEST_INSNS 1000000
5555
#define MAX_FIXUPS 8
56-
#define MAX_NR_MAPS 22
56+
#define MAX_NR_MAPS 23
5757
#define MAX_TEST_RUNS 8
5858
#define POINTER_VALUE 0xcafe4all
5959
#define TEST_DATA_LEN 64
@@ -101,6 +101,7 @@ struct bpf_test {
101101
int fixup_map_reuseport_array[MAX_FIXUPS];
102102
int fixup_map_ringbuf[MAX_FIXUPS];
103103
int fixup_map_timer[MAX_FIXUPS];
104+
int fixup_map_kptr[MAX_FIXUPS];
104105
struct kfunc_btf_id_pair fixup_kfunc_btf_id[MAX_FIXUPS];
105106
/* Expected verifier log output for result REJECT or VERBOSE_ACCEPT.
106107
* Can be a tab-separated sequence of expected strings. An empty string
@@ -621,8 +622,15 @@ static int create_cgroup_storage(bool percpu)
621622
* struct timer {
622623
* struct bpf_timer t;
623624
* };
625+
* struct btf_ptr {
626+
* struct prog_test_ref_kfunc __kptr *ptr;
627+
* struct prog_test_ref_kfunc __kptr_ref *ptr;
628+
* struct prog_test_member __kptr_ref *ptr;
629+
* }
624630
*/
625-
static const char btf_str_sec[] = "\0bpf_spin_lock\0val\0cnt\0l\0bpf_timer\0timer\0t";
631+
static const char btf_str_sec[] = "\0bpf_spin_lock\0val\0cnt\0l\0bpf_timer\0timer\0t"
632+
"\0btf_ptr\0prog_test_ref_kfunc\0ptr\0kptr\0kptr_ref"
633+
"\0prog_test_member";
626634
static __u32 btf_raw_types[] = {
627635
/* int */
628636
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
@@ -638,6 +646,22 @@ static __u32 btf_raw_types[] = {
638646
/* struct timer */ /* [5] */
639647
BTF_TYPE_ENC(35, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16),
640648
BTF_MEMBER_ENC(41, 4, 0), /* struct bpf_timer t; */
649+
/* struct prog_test_ref_kfunc */ /* [6] */
650+
BTF_STRUCT_ENC(51, 0, 0),
651+
BTF_STRUCT_ENC(89, 0, 0), /* [7] */
652+
/* type tag "kptr" */
653+
BTF_TYPE_TAG_ENC(75, 6), /* [8] */
654+
/* type tag "kptr_ref" */
655+
BTF_TYPE_TAG_ENC(80, 6), /* [9] */
656+
BTF_TYPE_TAG_ENC(80, 7), /* [10] */
657+
BTF_PTR_ENC(8), /* [11] */
658+
BTF_PTR_ENC(9), /* [12] */
659+
BTF_PTR_ENC(10), /* [13] */
660+
/* struct btf_ptr */ /* [14] */
661+
BTF_STRUCT_ENC(43, 3, 24),
662+
BTF_MEMBER_ENC(71, 11, 0), /* struct prog_test_ref_kfunc __kptr *ptr; */
663+
BTF_MEMBER_ENC(71, 12, 64), /* struct prog_test_ref_kfunc __kptr_ref *ptr; */
664+
BTF_MEMBER_ENC(71, 13, 128), /* struct prog_test_member __kptr_ref *ptr; */
641665
};
642666

643667
static int load_btf(void)
@@ -727,6 +751,25 @@ static int create_map_timer(void)
727751
return fd;
728752
}
729753

754+
static int create_map_kptr(void)
755+
{
756+
LIBBPF_OPTS(bpf_map_create_opts, opts,
757+
.btf_key_type_id = 1,
758+
.btf_value_type_id = 14,
759+
);
760+
int fd, btf_fd;
761+
762+
btf_fd = load_btf();
763+
if (btf_fd < 0)
764+
return -1;
765+
766+
opts.btf_fd = btf_fd;
767+
fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test_map", 4, 24, 1, &opts);
768+
if (fd < 0)
769+
printf("Failed to create map with btf_id pointer\n");
770+
return fd;
771+
}
772+
730773
static char bpf_vlog[UINT_MAX >> 8];
731774

732775
static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
@@ -754,6 +797,7 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
754797
int *fixup_map_reuseport_array = test->fixup_map_reuseport_array;
755798
int *fixup_map_ringbuf = test->fixup_map_ringbuf;
756799
int *fixup_map_timer = test->fixup_map_timer;
800+
int *fixup_map_kptr = test->fixup_map_kptr;
757801
struct kfunc_btf_id_pair *fixup_kfunc_btf_id = test->fixup_kfunc_btf_id;
758802

759803
if (test->fill_helper) {
@@ -947,6 +991,13 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
947991
fixup_map_timer++;
948992
} while (*fixup_map_timer);
949993
}
994+
if (*fixup_map_kptr) {
995+
map_fds[22] = create_map_kptr();
996+
do {
997+
prog[*fixup_map_kptr].imm = map_fds[22];
998+
fixup_map_kptr++;
999+
} while (*fixup_map_kptr);
1000+
}
9501001

9511002
/* Patch in kfunc BTF IDs */
9521003
if (fixup_kfunc_btf_id->kfunc) {

0 commit comments

Comments
 (0)