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