1 // SPDX-License-Identifier: GPL-2.0 2 #include <vmlinux.h> 3 #include <bpf/bpf_helpers.h> 4 #include "bpf_misc.h" 5 6 /* Used for testing map name. */ 7 int loong SEC(".percpu.looooooooong"); 8 int data3 SEC(".data.percpu"); 9 int data2 SEC(".percpu.data"); 10 11 /* Used for testing bpf_map__set_value_size(). */ 12 int arr[1] SEC(".percpu.arr"); 13 int arr_sum; 14 15 int run; 16 int cpu_id SEC(".percpu"); 17 int data SEC(".percpu") = -1; 18 int nums[7] SEC(".percpu"); 19 bool set SEC(".percpu") = false; 20 struct { 21 char set; 22 int i; 23 int nums[7]; 24 } struct_data SEC(".percpu") = { 25 .set = 0, 26 .i = -1, 27 }; 28 29 SEC("raw_tp/task_rename") 30 __auxiliary 31 int update_percpu_data(void *ctx) 32 { 33 struct_data.nums[6] = 0xc0de; 34 struct_data.set = 1; 35 struct_data.i = 1; 36 nums[6] = 0xc0de; 37 data = 1; 38 run++; 39 set = true; 40 cpu_id = bpf_get_smp_processor_id(); 41 arr_sum += arr[0]; 42 return 0; 43 } 44 45 static const char fmt[] SEC(".percpu.fmt") = "data %d\n"; 46 47 SEC("?kprobe") 48 __failure __msg("R{{[0-9]+}} points to percpu_array map which cannot be used as const string") 49 int verifier_strncmp(void *ctx) 50 { 51 return bpf_strncmp("test", 5, fmt); 52 } 53 54 SEC("?kprobe") 55 __failure __msg("R{{[0-9]+}} points to percpu_array map which cannot be used as const string") 56 int verifier_snprintf(void *ctx) 57 { 58 u64 args[] = { data }; 59 char buf[128]; 60 int len; 61 62 len = bpf_snprintf(buf, sizeof(buf), fmt, args, sizeof(args)); 63 if (len > 0) 64 bpf_printk("snprintf: %s\n", buf); 65 return 0; 66 } 67 68 volatile const __u32 num_cpus = 0; 69 volatile const int num_off; 70 volatile const int elem_sz; 71 __u32 sum = 0; 72 bool run_iter = false; 73 74 SEC("iter/bpf_map_elem") 75 __auxiliary 76 int dump_percpu_data(struct bpf_iter__bpf_map_elem *ctx) 77 { 78 void *pptr = ctx->value; 79 int i; 80 81 if (!pptr) 82 return 0; 83 84 run_iter = true; 85 86 for (i = 0; i < num_cpus; i++) { 87 sum += *(int *) (pptr + num_off); 88 pptr += elem_sz; 89 } 90 return 0; 91 } 92 93 char _license[] SEC("license") = "GPL"; 94