1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Google */
3 
4 #include "vmlinux.h"
5 
6 #include <bpf/bpf_helpers.h>
7 
8 extern const int bpf_prog_active __ksym; /* int type global var. */
9 
10 SEC("raw_tp/sys_enter")
handler1(const void * ctx)11 int handler1(const void *ctx)
12 {
13 	int *active;
14 	__u32 cpu;
15 
16 	cpu = bpf_get_smp_processor_id();
17 	active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);
18 	if (active) {
19 		/* Kernel memory obtained from bpf_{per,this}_cpu_ptr
20 		 * is read-only, should _not_ pass verification.
21 		 */
22 		/* WRITE_ONCE */
23 		*(volatile int *)active = -1;
24 	}
25 
26 	return 0;
27 }
28 
write_active(int * p)29 __noinline int write_active(int *p)
30 {
31 	return p ? (*p = 42) : 0;
32 }
33 
34 SEC("raw_tp/sys_enter")
handler2(const void * ctx)35 int handler2(const void *ctx)
36 {
37 	int *active;
38 
39 	active = bpf_this_cpu_ptr(&bpf_prog_active);
40 	write_active(active);
41 	return 0;
42 }
43 
44 char _license[] SEC("license") = "GPL";
45