Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <errno.h>
0003 #include <inttypes.h>
0004 /* For the CPU_* macros */
0005 #include <pthread.h>
0006 
0007 #include <sys/types.h>
0008 #include <sys/stat.h>
0009 #include <fcntl.h>
0010 #include <api/fs/fs.h>
0011 #include <linux/err.h>
0012 #include <linux/string.h>
0013 #include <api/fs/tracing_path.h>
0014 #include "evsel.h"
0015 #include "tests.h"
0016 #include "thread_map.h"
0017 #include <perf/cpumap.h>
0018 #include "debug.h"
0019 #include "stat.h"
0020 #include "util/counts.h"
0021 
0022 static int test__openat_syscall_event_on_all_cpus(struct test_suite *test __maybe_unused,
0023                           int subtest __maybe_unused)
0024 {
0025     int err = TEST_FAIL, fd, idx;
0026     struct perf_cpu cpu;
0027     struct perf_cpu_map *cpus;
0028     struct evsel *evsel;
0029     unsigned int nr_openat_calls = 111, i;
0030     cpu_set_t cpu_set;
0031     struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
0032     char sbuf[STRERR_BUFSIZE];
0033     char errbuf[BUFSIZ];
0034 
0035     if (threads == NULL) {
0036         pr_debug("thread_map__new\n");
0037         return -1;
0038     }
0039 
0040     cpus = perf_cpu_map__new(NULL);
0041     if (cpus == NULL) {
0042         pr_debug("perf_cpu_map__new\n");
0043         goto out_thread_map_delete;
0044     }
0045 
0046     CPU_ZERO(&cpu_set);
0047 
0048     evsel = evsel__newtp("syscalls", "sys_enter_openat");
0049     if (IS_ERR(evsel)) {
0050         tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
0051         pr_debug("%s\n", errbuf);
0052         err = TEST_SKIP;
0053         goto out_cpu_map_delete;
0054     }
0055 
0056     if (evsel__open(evsel, cpus, threads) < 0) {
0057         pr_debug("failed to open counter: %s, "
0058              "tweak /proc/sys/kernel/perf_event_paranoid?\n",
0059              str_error_r(errno, sbuf, sizeof(sbuf)));
0060         err = TEST_SKIP;
0061         goto out_evsel_delete;
0062     }
0063 
0064     perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
0065         unsigned int ncalls = nr_openat_calls + idx;
0066         /*
0067          * XXX eventually lift this restriction in a way that
0068          * keeps perf building on older glibc installations
0069          * without CPU_ALLOC. 1024 cpus in 2010 still seems
0070          * a reasonable upper limit tho :-)
0071          */
0072         if (cpu.cpu >= CPU_SETSIZE) {
0073             pr_debug("Ignoring CPU %d\n", cpu.cpu);
0074             continue;
0075         }
0076 
0077         CPU_SET(cpu.cpu, &cpu_set);
0078         if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
0079             pr_debug("sched_setaffinity() failed on CPU %d: %s ",
0080                  cpu.cpu,
0081                  str_error_r(errno, sbuf, sizeof(sbuf)));
0082             goto out_close_fd;
0083         }
0084         for (i = 0; i < ncalls; ++i) {
0085             fd = openat(0, "/etc/passwd", O_RDONLY);
0086             close(fd);
0087         }
0088         CPU_CLR(cpu.cpu, &cpu_set);
0089     }
0090 
0091     evsel->core.cpus = perf_cpu_map__get(cpus);
0092 
0093     err = TEST_OK;
0094 
0095     perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
0096         unsigned int expected;
0097 
0098         if (cpu.cpu >= CPU_SETSIZE)
0099             continue;
0100 
0101         if (evsel__read_on_cpu(evsel, idx, 0) < 0) {
0102             pr_debug("evsel__read_on_cpu\n");
0103             err = TEST_FAIL;
0104             break;
0105         }
0106 
0107         expected = nr_openat_calls + idx;
0108         if (perf_counts(evsel->counts, idx, 0)->val != expected) {
0109             pr_debug("evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
0110                  expected, cpu.cpu, perf_counts(evsel->counts, idx, 0)->val);
0111             err = TEST_FAIL;
0112         }
0113     }
0114 
0115     evsel__free_counts(evsel);
0116 out_close_fd:
0117     perf_evsel__close_fd(&evsel->core);
0118 out_evsel_delete:
0119     evsel__delete(evsel);
0120 out_cpu_map_delete:
0121     perf_cpu_map__put(cpus);
0122 out_thread_map_delete:
0123     perf_thread_map__put(threads);
0124     return err;
0125 }
0126 
0127 
0128 static struct test_case tests__openat_syscall_event_on_all_cpus[] = {
0129     TEST_CASE_REASON("Detect openat syscall event on all cpus",
0130              openat_syscall_event_on_all_cpus,
0131              "permissions"),
0132     {   .name = NULL, }
0133 };
0134 
0135 struct test_suite suite__openat_syscall_event_on_all_cpus = {
0136     .desc = "Detect openat syscall event on all cpus",
0137     .test_cases = tests__openat_syscall_event_on_all_cpus,
0138 };