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,16 @@ 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_ref_kfunc __kptr_percpu *ptr;
629+ * struct prog_test_ref_kfunc __kptr_user *ptr;
630+ * }
624631 */
625- static const char btf_str_sec [] = "\0bpf_spin_lock\0val\0cnt\0l\0bpf_timer\0timer\0t" ;
632+ static const char btf_str_sec [] = "\0bpf_spin_lock\0val\0cnt\0l\0bpf_timer\0timer\0t"
633+ "\0btf_ptr\0prog_test_ref_kfunc\0ptr\0kptr\0kptr_ref"
634+ "\0kptr_percpu\0kptr_user" ;
626635static __u32 btf_raw_types [] = {
627636 /* int */
628637 BTF_TYPE_INT_ENC (0 , BTF_INT_SIGNED , 0 , 32 , 4 ), /* [1] */
@@ -638,6 +647,26 @@ static __u32 btf_raw_types[] = {
638647 /* struct timer */ /* [5] */
639648 BTF_TYPE_ENC (35 , BTF_INFO_ENC (BTF_KIND_STRUCT , 0 , 1 ), 16 ),
640649 BTF_MEMBER_ENC (41 , 4 , 0 ), /* struct bpf_timer t; */
650+ /* struct prog_test_ref_kfunc */ /* [6] */
651+ BTF_STRUCT_ENC (51 , 0 , 0 ),
652+ /* type tag "kptr" */
653+ BTF_TYPE_TAG_ENC (75 , 6 ), /* [7] */
654+ /* type tag "kptr_ref" */
655+ BTF_TYPE_TAG_ENC (80 , 6 ), /* [8] */
656+ /* type tag "kptr_percpu" */
657+ BTF_TYPE_TAG_ENC (89 , 6 ), /* [9] */
658+ /* type tag "kptr_user" */
659+ BTF_TYPE_TAG_ENC (101 , 6 ), /* [10] */
660+ BTF_PTR_ENC (7 ), /* [11] */
661+ BTF_PTR_ENC (8 ), /* [12] */
662+ BTF_PTR_ENC (9 ), /* [13] */
663+ BTF_PTR_ENC (10 ), /* [14] */
664+ /* struct btf_ptr */ /* [15] */
665+ BTF_STRUCT_ENC (43 , 4 , 32 ),
666+ BTF_MEMBER_ENC (71 , 11 , 0 ), /* struct prog_test_ref_kfunc __kptr *ptr; */
667+ BTF_MEMBER_ENC (71 , 12 , 64 ), /* struct prog_test_ref_kfunc __kptr_ref *ptr; */
668+ BTF_MEMBER_ENC (71 , 13 , 128 ), /* struct prog_test_ref_kfunc __kptr_percpu *ptr; */
669+ BTF_MEMBER_ENC (71 , 14 , 192 ), /* struct prog_test_ref_kfunc __kptr_user *ptr; */
641670};
642671
643672static int load_btf (void )
@@ -727,6 +756,25 @@ static int create_map_timer(void)
727756 return fd ;
728757}
729758
759+ static int create_map_kptr (void )
760+ {
761+ LIBBPF_OPTS (bpf_map_create_opts , opts ,
762+ .btf_key_type_id = 1 ,
763+ .btf_value_type_id = 15 ,
764+ );
765+ int fd , btf_fd ;
766+
767+ btf_fd = load_btf ();
768+ if (btf_fd < 0 )
769+ return -1 ;
770+
771+ opts .btf_fd = btf_fd ;
772+ fd = bpf_map_create (BPF_MAP_TYPE_ARRAY , "test_map" , 4 , 32 , 1 , & opts );
773+ if (fd < 0 )
774+ printf ("Failed to create map with btf_id pointer\n" );
775+ return fd ;
776+ }
777+
730778static char bpf_vlog [UINT_MAX >> 8 ];
731779
732780static void do_test_fixup (struct bpf_test * test , enum bpf_prog_type prog_type ,
@@ -754,6 +802,7 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
754802 int * fixup_map_reuseport_array = test -> fixup_map_reuseport_array ;
755803 int * fixup_map_ringbuf = test -> fixup_map_ringbuf ;
756804 int * fixup_map_timer = test -> fixup_map_timer ;
805+ int * fixup_map_kptr = test -> fixup_map_kptr ;
757806 struct kfunc_btf_id_pair * fixup_kfunc_btf_id = test -> fixup_kfunc_btf_id ;
758807
759808 if (test -> fill_helper ) {
@@ -947,6 +996,13 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
947996 fixup_map_timer ++ ;
948997 } while (* fixup_map_timer );
949998 }
999+ if (* fixup_map_kptr ) {
1000+ map_fds [22 ] = create_map_kptr ();
1001+ do {
1002+ prog [* fixup_map_kptr ].imm = map_fds [22 ];
1003+ fixup_map_kptr ++ ;
1004+ } while (* fixup_map_kptr );
1005+ }
9501006
9511007 /* Patch in kfunc BTF IDs */
9521008 if (fixup_kfunc_btf_id -> kfunc ) {
0 commit comments