xref: /openbmc/linux/samples/bpf/spintest.bpf.c (revision 456d5355)
14a0ee788SDaniel T. Lee /* Copyright (c) 2016, Facebook
24a0ee788SDaniel T. Lee  *
34a0ee788SDaniel T. Lee  * This program is free software; you can redistribute it and/or
44a0ee788SDaniel T. Lee  * modify it under the terms of version 2 of the GNU General Public
54a0ee788SDaniel T. Lee  * License as published by the Free Software Foundation.
64a0ee788SDaniel T. Lee  */
74a0ee788SDaniel T. Lee #include "vmlinux.h"
84a0ee788SDaniel T. Lee #include <linux/version.h>
94a0ee788SDaniel T. Lee #include <bpf/bpf_helpers.h>
104a0ee788SDaniel T. Lee #include <bpf/bpf_tracing.h>
114a0ee788SDaniel T. Lee 
124a0ee788SDaniel T. Lee #ifndef PERF_MAX_STACK_DEPTH
134a0ee788SDaniel T. Lee #define PERF_MAX_STACK_DEPTH         127
144a0ee788SDaniel T. Lee #endif
154a0ee788SDaniel T. Lee 
164a0ee788SDaniel T. Lee struct {
174a0ee788SDaniel T. Lee 	__uint(type, BPF_MAP_TYPE_HASH);
184a0ee788SDaniel T. Lee 	__type(key, long);
194a0ee788SDaniel T. Lee 	__type(value, long);
204a0ee788SDaniel T. Lee 	__uint(max_entries, 1024);
214a0ee788SDaniel T. Lee } my_map SEC(".maps");
224a0ee788SDaniel T. Lee struct {
234a0ee788SDaniel T. Lee 	__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
244a0ee788SDaniel T. Lee 	__uint(key_size, sizeof(long));
254a0ee788SDaniel T. Lee 	__uint(value_size, sizeof(long));
264a0ee788SDaniel T. Lee 	__uint(max_entries, 1024);
274a0ee788SDaniel T. Lee } my_map2 SEC(".maps");
284a0ee788SDaniel T. Lee 
294a0ee788SDaniel T. Lee struct {
304a0ee788SDaniel T. Lee 	__uint(type, BPF_MAP_TYPE_STACK_TRACE);
314a0ee788SDaniel T. Lee 	__uint(key_size, sizeof(u32));
324a0ee788SDaniel T. Lee 	__uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64));
334a0ee788SDaniel T. Lee 	__uint(max_entries, 10000);
344a0ee788SDaniel T. Lee } stackmap SEC(".maps");
354a0ee788SDaniel T. Lee 
364a0ee788SDaniel T. Lee #define PROG(foo) \
374a0ee788SDaniel T. Lee int foo(struct pt_regs *ctx) \
384a0ee788SDaniel T. Lee { \
394a0ee788SDaniel T. Lee 	long v = PT_REGS_IP(ctx), *val; \
404a0ee788SDaniel T. Lee \
414a0ee788SDaniel T. Lee 	val = bpf_map_lookup_elem(&my_map, &v); \
424a0ee788SDaniel T. Lee 	bpf_map_update_elem(&my_map, &v, &v, BPF_ANY); \
434a0ee788SDaniel T. Lee 	bpf_map_update_elem(&my_map2, &v, &v, BPF_ANY); \
444a0ee788SDaniel T. Lee 	bpf_map_delete_elem(&my_map2, &v); \
454a0ee788SDaniel T. Lee 	bpf_get_stackid(ctx, &stackmap, BPF_F_REUSE_STACKID); \
464a0ee788SDaniel T. Lee 	return 0; \
474a0ee788SDaniel T. Lee }
484a0ee788SDaniel T. Lee 
494a0ee788SDaniel T. Lee /* add kprobes to all possible *spin* functions */
50*456d5355SDaniel T. Lee SEC("kprobe.multi/spin_*lock*")PROG(spin_lock)
51*456d5355SDaniel T. Lee SEC("kprobe.multi/*_spin_on_owner")PROG(spin_on_owner)
52*456d5355SDaniel T. Lee SEC("kprobe.multi/_raw_spin_*lock*")PROG(raw_spin_lock)
534a0ee788SDaniel T. Lee 
544a0ee788SDaniel T. Lee /* and to inner bpf helpers */
554a0ee788SDaniel T. Lee SEC("kprobe/htab_map_update_elem")PROG(p15)
564a0ee788SDaniel T. Lee SEC("kprobe/__htab_percpu_map_update_elem")PROG(p16)
574a0ee788SDaniel T. Lee SEC("kprobe/htab_map_alloc")PROG(p17)
584a0ee788SDaniel T. Lee 
594a0ee788SDaniel T. Lee char _license[] SEC("license") = "GPL";
604a0ee788SDaniel T. Lee u32 _version SEC("version") = LINUX_VERSION_CODE;
61