Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | 06933e3 | 2013-03-10 19:41:11 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Originally done by Vince Weaver <vincent.weaver@maine.edu> for |
| 4 | * perf_event_tests (git://github.com/deater/perf_event_tests) |
| 5 | */ |
| 6 | |
Sukadev Bhattiprolu | b3539d2 | 2013-04-26 10:17:56 -0700 | [diff] [blame] | 7 | /* |
| 8 | * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select |
| 9 | * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu. |
| 10 | */ |
| 11 | #define __SANE_USERSPACE_TYPES__ |
| 12 | |
Jiri Olsa | 06933e3 | 2013-03-10 19:41:11 +0100 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | #include <stdio.h> |
| 15 | #include <unistd.h> |
| 16 | #include <string.h> |
| 17 | #include <sys/ioctl.h> |
| 18 | #include <time.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <signal.h> |
| 21 | #include <sys/mman.h> |
| 22 | #include <linux/compiler.h> |
| 23 | #include <linux/hw_breakpoint.h> |
| 24 | |
| 25 | #include "tests.h" |
| 26 | #include "debug.h" |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 27 | #include "event.h" |
Arnaldo Carvalho de Melo | 91854f9 | 2019-08-29 14:59:50 -0300 | [diff] [blame] | 28 | #include "../perf-sys.h" |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 29 | #include "cloexec.h" |
Jiri Olsa | 06933e3 | 2013-03-10 19:41:11 +0100 | [diff] [blame] | 30 | |
| 31 | static int overflows; |
| 32 | |
Arnaldo Carvalho de Melo | 9dd4ca4 | 2017-06-16 11:39:15 -0300 | [diff] [blame] | 33 | static noinline int test_function(void) |
Jiri Olsa | 06933e3 | 2013-03-10 19:41:11 +0100 | [diff] [blame] | 34 | { |
| 35 | return time(NULL); |
| 36 | } |
| 37 | |
| 38 | static void sig_handler(int signum __maybe_unused, |
| 39 | siginfo_t *oh __maybe_unused, |
| 40 | void *uc __maybe_unused) |
| 41 | { |
| 42 | overflows++; |
| 43 | } |
| 44 | |
| 45 | static long long bp_count(int fd) |
| 46 | { |
| 47 | long long count; |
| 48 | int ret; |
| 49 | |
| 50 | ret = read(fd, &count, sizeof(long long)); |
| 51 | if (ret != sizeof(long long)) { |
| 52 | pr_debug("failed to read: %d\n", ret); |
| 53 | return TEST_FAIL; |
| 54 | } |
| 55 | |
| 56 | return count; |
| 57 | } |
| 58 | |
| 59 | #define EXECUTIONS 10000 |
| 60 | #define THRESHOLD 100 |
| 61 | |
Ian Rogers | 33f44bf | 2021-11-03 23:41:51 -0700 | [diff] [blame] | 62 | static int test__bp_signal_overflow(struct test_suite *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | 06933e3 | 2013-03-10 19:41:11 +0100 | [diff] [blame] | 63 | { |
| 64 | struct perf_event_attr pe; |
| 65 | struct sigaction sa; |
| 66 | long long count; |
| 67 | int fd, i, fails = 0; |
| 68 | |
Ian Rogers | 4935e2c | 2021-11-03 23:42:05 -0700 | [diff] [blame] | 69 | if (!BP_SIGNAL_IS_SUPPORTED) { |
| 70 | pr_debug("Test not supported on this architecture"); |
| 71 | return TEST_SKIP; |
| 72 | } |
| 73 | |
Jiri Olsa | 06933e3 | 2013-03-10 19:41:11 +0100 | [diff] [blame] | 74 | /* setup SIGIO signal handler */ |
| 75 | memset(&sa, 0, sizeof(struct sigaction)); |
| 76 | sa.sa_sigaction = (void *) sig_handler; |
| 77 | sa.sa_flags = SA_SIGINFO; |
| 78 | |
| 79 | if (sigaction(SIGIO, &sa, NULL) < 0) { |
| 80 | pr_debug("failed setting up signal handler\n"); |
| 81 | return TEST_FAIL; |
| 82 | } |
| 83 | |
| 84 | memset(&pe, 0, sizeof(struct perf_event_attr)); |
| 85 | pe.type = PERF_TYPE_BREAKPOINT; |
| 86 | pe.size = sizeof(struct perf_event_attr); |
| 87 | |
| 88 | pe.config = 0; |
| 89 | pe.bp_type = HW_BREAKPOINT_X; |
| 90 | pe.bp_addr = (unsigned long) test_function; |
| 91 | pe.bp_len = sizeof(long); |
| 92 | |
| 93 | pe.sample_period = THRESHOLD; |
| 94 | pe.sample_type = PERF_SAMPLE_IP; |
| 95 | pe.wakeup_events = 1; |
| 96 | |
| 97 | pe.disabled = 1; |
| 98 | pe.exclude_kernel = 1; |
| 99 | pe.exclude_hv = 1; |
| 100 | |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 101 | fd = sys_perf_event_open(&pe, 0, -1, -1, |
| 102 | perf_event_open_cloexec_flag()); |
Jiri Olsa | 06933e3 | 2013-03-10 19:41:11 +0100 | [diff] [blame] | 103 | if (fd < 0) { |
| 104 | pr_debug("failed opening event %llx\n", pe.config); |
| 105 | return TEST_FAIL; |
| 106 | } |
| 107 | |
| 108 | fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC); |
| 109 | fcntl(fd, F_SETSIG, SIGIO); |
| 110 | fcntl(fd, F_SETOWN, getpid()); |
| 111 | |
| 112 | ioctl(fd, PERF_EVENT_IOC_RESET, 0); |
| 113 | ioctl(fd, PERF_EVENT_IOC_ENABLE, 0); |
| 114 | |
| 115 | for (i = 0; i < EXECUTIONS; i++) |
| 116 | test_function(); |
| 117 | |
| 118 | ioctl(fd, PERF_EVENT_IOC_DISABLE, 0); |
| 119 | |
| 120 | count = bp_count(fd); |
| 121 | |
| 122 | close(fd); |
| 123 | |
| 124 | pr_debug("count %lld, overflow %d\n", |
| 125 | count, overflows); |
| 126 | |
| 127 | if (count != EXECUTIONS) { |
| 128 | pr_debug("\tWrong number of executions %lld != %d\n", |
| 129 | count, EXECUTIONS); |
| 130 | fails++; |
| 131 | } |
| 132 | |
| 133 | if (overflows != EXECUTIONS / THRESHOLD) { |
| 134 | pr_debug("\tWrong number of overflows %d != %d\n", |
| 135 | overflows, EXECUTIONS / THRESHOLD); |
| 136 | fails++; |
| 137 | } |
| 138 | |
| 139 | return fails ? TEST_FAIL : TEST_OK; |
| 140 | } |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame] | 141 | |
Ian Rogers | 4935e2c | 2021-11-03 23:42:05 -0700 | [diff] [blame] | 142 | DEFINE_SUITE("Breakpoint overflow sampling", bp_signal_overflow); |