xref: /linux/tools/testing/selftests/bpf/progs/bpf_iter_bpf_map.c (revision 3503d56cc7233ced602e38a4c13caa64f00ab2aa)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 /* "undefine" structs in vmlinux.h, because we "override" them below */
4 #define bpf_iter_meta bpf_iter_meta___not_used
5 #define bpf_iter__bpf_map bpf_iter__bpf_map___not_used
6 #include "vmlinux.h"
7 #undef bpf_iter_meta
8 #undef bpf_iter__bpf_map
9 #include <bpf/bpf_helpers.h>
10 #include <bpf/bpf_tracing.h>
11 
12 char _license[] SEC("license") = "GPL";
13 
14 struct bpf_iter_meta {
15 	struct seq_file *seq;
16 	__u64 session_id;
17 	__u64 seq_num;
18 } __attribute__((preserve_access_index));
19 
20 struct bpf_iter__bpf_map {
21 	struct bpf_iter_meta *meta;
22 	struct bpf_map *map;
23 } __attribute__((preserve_access_index));
24 
25 SEC("iter/bpf_map")
26 int dump_bpf_map(struct bpf_iter__bpf_map *ctx)
27 {
28 	struct seq_file *seq = ctx->meta->seq;
29 	__u64 seq_num = ctx->meta->seq_num;
30 	struct bpf_map *map = ctx->map;
31 
32 	if (map == (void *)0) {
33 		BPF_SEQ_PRINTF(seq, "      %%%%%% END %%%%%%\n");
34 		return 0;
35 	}
36 
37 	if (seq_num == 0)
38 		BPF_SEQ_PRINTF(seq, "      id   refcnt  usercnt  locked_vm\n");
39 
40 	BPF_SEQ_PRINTF(seq, "%8u %8ld %8ld %10lu\n", map->id, map->refcnt.counter,
41 		       map->usercnt.counter,
42 		       map->memory.user->locked_vm.counter);
43 	return 0;
44 }
45