~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/progs/test_get_stack_rawtp.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 
  3 #include <linux/bpf.h>
  4 #include <bpf/bpf_helpers.h>
  5 
  6 /* Permit pretty deep stack traces */
  7 #define MAX_STACK_RAWTP 100
  8 struct stack_trace_t {
  9         int pid;
 10         int kern_stack_size;
 11         int user_stack_size;
 12         int user_stack_buildid_size;
 13         __u64 kern_stack[MAX_STACK_RAWTP];
 14         __u64 user_stack[MAX_STACK_RAWTP];
 15         struct bpf_stack_build_id user_stack_buildid[MAX_STACK_RAWTP];
 16 };
 17 
 18 struct {
 19         __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
 20         __uint(max_entries, 2);
 21         __uint(key_size, sizeof(int));
 22         __uint(value_size, sizeof(__u32));
 23 } perfmap SEC(".maps");
 24 
 25 struct {
 26         __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
 27         __uint(max_entries, 1);
 28         __type(key, __u32);
 29         __type(value, struct stack_trace_t);
 30 } stackdata_map SEC(".maps");
 31 
 32 /* Allocate per-cpu space twice the needed. For the code below
 33  *   usize = bpf_get_stack(ctx, raw_data, max_len, BPF_F_USER_STACK);
 34  *   if (usize < 0)
 35  *     return 0;
 36  *   ksize = bpf_get_stack(ctx, raw_data + usize, max_len - usize, 0);
 37  *
 38  * If we have value_size = MAX_STACK_RAWTP * sizeof(__u64),
 39  * verifier will complain that access "raw_data + usize"
 40  * with size "max_len - usize" may be out of bound.
 41  * The maximum "raw_data + usize" is "raw_data + max_len"
 42  * and the maximum "max_len - usize" is "max_len", verifier
 43  * concludes that the maximum buffer access range is
 44  * "raw_data[0...max_len * 2 - 1]" and hence reject the program.
 45  *
 46  * Doubling the to-be-used max buffer size can fix this verifier
 47  * issue and avoid complicated C programming massaging.
 48  * This is an acceptable workaround since there is one entry here.
 49  */
 50 struct {
 51         __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
 52         __uint(max_entries, 1);
 53         __type(key, __u32);
 54         __type(value, __u64[2 * MAX_STACK_RAWTP]);
 55 } rawdata_map SEC(".maps");
 56 
 57 SEC("raw_tracepoint/sys_enter")
 58 int bpf_prog1(void *ctx)
 59 {
 60         int max_len, max_buildid_len, total_size;
 61         struct stack_trace_t *data;
 62         long usize, ksize;
 63         void *raw_data;
 64         __u32 key = 0;
 65 
 66         data = bpf_map_lookup_elem(&stackdata_map, &key);
 67         if (!data)
 68                 return 0;
 69 
 70         max_len = MAX_STACK_RAWTP * sizeof(__u64);
 71         max_buildid_len = MAX_STACK_RAWTP * sizeof(struct bpf_stack_build_id);
 72         data->pid = bpf_get_current_pid_tgid();
 73         data->kern_stack_size = bpf_get_stack(ctx, data->kern_stack,
 74                                               max_len, 0);
 75         data->user_stack_size = bpf_get_stack(ctx, data->user_stack, max_len,
 76                                             BPF_F_USER_STACK);
 77         data->user_stack_buildid_size = bpf_get_stack(
 78                 ctx, data->user_stack_buildid, max_buildid_len,
 79                 BPF_F_USER_STACK | BPF_F_USER_BUILD_ID);
 80         bpf_perf_event_output(ctx, &perfmap, 0, data, sizeof(*data));
 81 
 82         /* write both kernel and user stacks to the same buffer */
 83         raw_data = bpf_map_lookup_elem(&rawdata_map, &key);
 84         if (!raw_data)
 85                 return 0;
 86 
 87         usize = bpf_get_stack(ctx, raw_data, max_len, BPF_F_USER_STACK);
 88         if (usize < 0)
 89                 return 0;
 90 
 91         ksize = bpf_get_stack(ctx, raw_data + usize, max_len - usize, 0);
 92         if (ksize < 0)
 93                 return 0;
 94 
 95         total_size = usize + ksize;
 96         if (total_size > 0 && total_size <= max_len)
 97                 bpf_perf_event_output(ctx, &perfmap, 0, raw_data, total_size);
 98 
 99         return 0;
100 }
101 
102 char _license[] SEC("license") = "GPL";
103 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php