Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <vmlinux.h>
0003 #include <bpf/bpf_helpers.h>
0004 
0005 #define TEST_STACK_DEPTH    2
0006 #define TEST_MAX_ENTRIES    16384
0007 
0008 typedef __u64 stack_trace_t[TEST_STACK_DEPTH];
0009 
0010 struct {
0011     __uint(type, BPF_MAP_TYPE_STACK_TRACE);
0012     __uint(max_entries, TEST_MAX_ENTRIES);
0013     __type(key, __u32);
0014     __type(value, stack_trace_t);
0015 } stackmap SEC(".maps");
0016 
0017 struct {
0018     __uint(type, BPF_MAP_TYPE_HASH);
0019     __uint(max_entries, TEST_MAX_ENTRIES);
0020     __type(key, __u32);
0021     __type(value, __u32);
0022 } stackid_hmap SEC(".maps");
0023 
0024 struct {
0025     __uint(type, BPF_MAP_TYPE_ARRAY);
0026     __uint(max_entries, TEST_MAX_ENTRIES);
0027     __type(key, __u32);
0028     __type(value, stack_trace_t);
0029 } stack_amap SEC(".maps");
0030 
0031 int pid = 0;
0032 int control = 0;
0033 int failed = 0;
0034 
0035 SEC("tracepoint/sched/sched_switch")
0036 int oncpu(struct trace_event_raw_sched_switch *ctx)
0037 {
0038     __u32 max_len = TEST_STACK_DEPTH * sizeof(__u64);
0039     __u32 key = 0, val = 0;
0040     __u64 *stack_p;
0041 
0042     if (pid != (bpf_get_current_pid_tgid() >> 32))
0043         return 0;
0044 
0045     if (control)
0046         return 0;
0047 
0048     /* it should allow skipping whole buffer size entries */
0049     key = bpf_get_stackid(ctx, &stackmap, TEST_STACK_DEPTH);
0050     if ((int)key >= 0) {
0051         /* The size of stackmap and stack_amap should be the same */
0052         bpf_map_update_elem(&stackid_hmap, &key, &val, 0);
0053         stack_p = bpf_map_lookup_elem(&stack_amap, &key);
0054         if (stack_p) {
0055             bpf_get_stack(ctx, stack_p, max_len, TEST_STACK_DEPTH);
0056             /* it wrongly skipped all the entries and filled zero */
0057             if (stack_p[0] == 0)
0058                 failed = 1;
0059         }
0060     } else {
0061         /* old kernel doesn't support skipping that many entries */
0062         failed = 2;
0063     }
0064 
0065     return 0;
0066 }
0067 
0068 char _license[] SEC("license") = "GPL";