aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/stacktrace_map_skip.c
blob: 2eb297df3dd67bb1fdb894a377927a6cf8e339fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: GPL-2.0
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>

#define TEST_STACK_DEPTH	2
#define TEST_MAX_ENTRIES	16384

typedef __u64 stack_trace_t[TEST_STACK_DEPTH];

struct {
	__uint(type, BPF_MAP_TYPE_STACK_TRACE);
	__uint(max_entries, TEST_MAX_ENTRIES);
	__type(key, __u32);
	__type(value, stack_trace_t);
} stackmap SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_HASH);
	__uint(max_entries, TEST_MAX_ENTRIES);
	__type(key, __u32);
	__type(value, __u32);
} stackid_hmap SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, TEST_MAX_ENTRIES);
	__type(key, __u32);
	__type(value, stack_trace_t);
} stack_amap SEC(".maps");

int pid = 0;
int control = 0;
int failed = 0;

SEC("tracepoint/sched/sched_switch")
int oncpu(struct trace_event_raw_sched_switch *ctx)
{
	__u32 max_len = TEST_STACK_DEPTH * sizeof(__u64);
	__u32 key = 0, val = 0;
	__u64 *stack_p;

	if (pid != (bpf_get_current_pid_tgid() >> 32))
		return 0;

	if (control)
		return 0;

	/* it should allow skipping whole buffer size entries */
	key = bpf_get_stackid(ctx, &stackmap, TEST_STACK_DEPTH);
	if ((int)key >= 0) {
		/* The size of stackmap and stack_amap should be the same */
		bpf_map_update_elem(&stackid_hmap, &key, &val, 0);
		stack_p = bpf_map_lookup_elem(&stack_amap, &key);
		if (stack_p) {
			bpf_get_stack(ctx, stack_p, max_len, TEST_STACK_DEPTH);
			/* it wrongly skipped all the entries and filled zero */
			if (stack_p[0] == 0)
				failed = 1;
		}
	} else {
		/* old kernel doesn't support skipping that many entries */
		failed = 2;
	}

	return 0;
}

char _license[] SEC("license") = "GPL";