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

TOMOYO Linux Cross Reference
Linux/samples/bpf/tracex6.bpf.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 #include "vmlinux.h"
  2 #include <linux/version.h>
  3 #include <bpf/bpf_helpers.h>
  4 #include <bpf/bpf_tracing.h>
  5 #include <bpf/bpf_core_read.h>
  6 
  7 struct {
  8         __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
  9         __uint(key_size, sizeof(int));
 10         __uint(value_size, sizeof(u32));
 11         __uint(max_entries, 64);
 12 } counters SEC(".maps");
 13 
 14 struct {
 15         __uint(type, BPF_MAP_TYPE_HASH);
 16         __type(key, int);
 17         __type(value, u64);
 18         __uint(max_entries, 64);
 19 } values SEC(".maps");
 20 
 21 struct {
 22         __uint(type, BPF_MAP_TYPE_HASH);
 23         __type(key, int);
 24         __type(value, struct bpf_perf_event_value);
 25         __uint(max_entries, 64);
 26 } values2 SEC(".maps");
 27 
 28 SEC("kprobe/htab_map_get_next_key")
 29 int bpf_prog1(struct pt_regs *ctx)
 30 {
 31         u32 key = bpf_get_smp_processor_id();
 32         u64 count, *val;
 33         s64 error;
 34 
 35         count = bpf_perf_event_read(&counters, key);
 36         error = (s64)count;
 37         if (error <= -2 && error >= -22)
 38                 return 0;
 39 
 40         val = bpf_map_lookup_elem(&values, &key);
 41         if (val)
 42                 *val = count;
 43         else
 44                 bpf_map_update_elem(&values, &key, &count, BPF_NOEXIST);
 45 
 46         return 0;
 47 }
 48 
 49 /*
 50  * Since *_map_lookup_elem can't be expected to trigger bpf programs
 51  * due to potential deadlocks (bpf_disable_instrumentation), this bpf
 52  * program will be attached to bpf_map_copy_value (which is called
 53  * from map_lookup_elem) and will only filter the hashtable type.
 54  */
 55 SEC("kprobe/bpf_map_copy_value")
 56 int BPF_KPROBE(bpf_prog2, struct bpf_map *map)
 57 {
 58         u32 key = bpf_get_smp_processor_id();
 59         struct bpf_perf_event_value *val, buf;
 60         enum bpf_map_type type;
 61         int error;
 62 
 63         type = BPF_CORE_READ(map, map_type);
 64         if (type != BPF_MAP_TYPE_HASH)
 65                 return 0;
 66 
 67         error = bpf_perf_event_read_value(&counters, key, &buf, sizeof(buf));
 68         if (error)
 69                 return 0;
 70 
 71         val = bpf_map_lookup_elem(&values2, &key);
 72         if (val)
 73                 *val = buf;
 74         else
 75                 bpf_map_update_elem(&values2, &key, &buf, BPF_NOEXIST);
 76 
 77         return 0;
 78 }
 79 
 80 char _license[] SEC("license") = "GPL";
 81 u32 _version SEC("version") = LINUX_VERSION_CODE;
 82 

~ [ 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