1 // SPDX-License-Identifier: GPL-2.0 2 #include <errno.h> 3 #include <inttypes.h> 4 /* For the CPU_* macros */ 5 #include <pthread.h> 6 7 #include <sys/types.h> 8 #include <sys/stat.h> 9 #include <fcntl.h> 10 #include <api/fs/fs.h> 11 #include <linux/err.h> 12 #include <linux/string.h> 13 #include <api/fs/tracing_path.h> 14 #include "evsel.h" 15 #include "tests.h" 16 #include "thread_map.h" 17 #include <perf/cpumap.h> 18 #include "debug.h" 19 #include "stat.h" 20 #include "util/counts.h" 21 22 static int test__openat_syscall_event_on_all_cpus(struct test_suite *test __maybe_unused, 23 int subtest __maybe_unused) 24 { 25 int err = -1, fd, idx; 26 struct perf_cpu cpu; 27 struct perf_cpu_map *cpus; 28 struct evsel *evsel; 29 unsigned int nr_openat_calls = 111, i; 30 cpu_set_t cpu_set; 31 struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX); 32 char sbuf[STRERR_BUFSIZE]; 33 char errbuf[BUFSIZ]; 34 35 if (threads == NULL) { 36 pr_debug("thread_map__new\n"); 37 return -1; 38 } 39 40 cpus = perf_cpu_map__new(NULL); 41 if (cpus == NULL) { 42 pr_debug("perf_cpu_map__new\n"); 43 goto out_thread_map_delete; 44 } 45 46 CPU_ZERO(&cpu_set); 47 48 evsel = evsel__newtp("syscalls", "sys_enter_openat"); 49 if (IS_ERR(evsel)) { 50 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat"); 51 pr_debug("%s\n", errbuf); 52 goto out_cpu_map_delete; 53 } 54 55 if (evsel__open(evsel, cpus, threads) < 0) { 56 pr_debug("failed to open counter: %s, " 57 "tweak /proc/sys/kernel/perf_event_paranoid?\n", 58 str_error_r(errno, sbuf, sizeof(sbuf))); 59 goto out_evsel_delete; 60 } 61 62 perf_cpu_map__for_each_cpu(cpu, idx, cpus) { 63 unsigned int ncalls = nr_openat_calls + idx; 64 /* 65 * XXX eventually lift this restriction in a way that 66 * keeps perf building on older glibc installations 67 * without CPU_ALLOC. 1024 cpus in 2010 still seems 68 * a reasonable upper limit tho :-) 69 */ 70 if (cpu.cpu >= CPU_SETSIZE) { 71 pr_debug("Ignoring CPU %d\n", cpu.cpu); 72 continue; 73 } 74 75 CPU_SET(cpu.cpu, &cpu_set); 76 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) { 77 pr_debug("sched_setaffinity() failed on CPU %d: %s ", 78 cpu.cpu, 79 str_error_r(errno, sbuf, sizeof(sbuf))); 80 goto out_close_fd; 81 } 82 for (i = 0; i < ncalls; ++i) { 83 fd = openat(0, "/etc/passwd", O_RDONLY); 84 close(fd); 85 } 86 CPU_CLR(cpu.cpu, &cpu_set); 87 } 88 89 evsel->core.cpus = perf_cpu_map__get(cpus); 90 91 err = 0; 92 93 perf_cpu_map__for_each_cpu(cpu, idx, cpus) { 94 unsigned int expected; 95 96 if (cpu.cpu >= CPU_SETSIZE) 97 continue; 98 99 if (evsel__read_on_cpu(evsel, idx, 0) < 0) { 100 pr_debug("evsel__read_on_cpu\n"); 101 err = -1; 102 break; 103 } 104 105 expected = nr_openat_calls + idx; 106 if (perf_counts(evsel->counts, idx, 0)->val != expected) { 107 pr_debug("evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n", 108 expected, cpu.cpu, perf_counts(evsel->counts, idx, 0)->val); 109 err = -1; 110 } 111 } 112 113 evsel__free_counts(evsel); 114 out_close_fd: 115 perf_evsel__close_fd(&evsel->core); 116 out_evsel_delete: 117 evsel__delete(evsel); 118 out_cpu_map_delete: 119 perf_cpu_map__put(cpus); 120 out_thread_map_delete: 121 perf_thread_map__put(threads); 122 return err; 123 } 124 125 DEFINE_SUITE("Detect openat syscall event on all cpus", openat_syscall_event_on_all_cpus); 126