Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2021 Google */
0003 
0004 #include "vmlinux.h"
0005 
0006 #include <bpf/bpf_helpers.h>
0007 
0008 extern const int bpf_prog_active __ksym; /* int type global var. */
0009 
0010 SEC("raw_tp/sys_enter")
0011 int handler1(const void *ctx)
0012 {
0013     int *active;
0014     __u32 cpu;
0015 
0016     cpu = bpf_get_smp_processor_id();
0017     active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);
0018     if (active) {
0019         /* Kernel memory obtained from bpf_{per,this}_cpu_ptr
0020          * is read-only, should _not_ pass verification.
0021          */
0022         /* WRITE_ONCE */
0023         *(volatile int *)active = -1;
0024     }
0025 
0026     return 0;
0027 }
0028 
0029 __noinline int write_active(int *p)
0030 {
0031     return p ? (*p = 42) : 0;
0032 }
0033 
0034 SEC("raw_tp/sys_enter")
0035 int handler2(const void *ctx)
0036 {
0037     int *active;
0038     __u32 cpu;
0039 
0040     active = bpf_this_cpu_ptr(&bpf_prog_active);
0041     write_active(active);
0042     return 0;
0043 }
0044 
0045 char _license[] SEC("license") = "GPL";