Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
  4 * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
  5 */
  6#define __SANE_USERSPACE_TYPES__
  7
  8#include <stdlib.h>
  9#include <stdio.h>
 10#include <unistd.h>
 11#include <string.h>
 12#include <sys/ioctl.h>
 13#include <time.h>
 14#include <fcntl.h>
 15#include <signal.h>
 16#include <sys/mman.h>
 17#include <linux/compiler.h>
 18#include <linux/hw_breakpoint.h>
 19
 20#include "tests.h"
 21#include "debug.h"
 22#include "event.h"
 23#include "../perf-sys.h"
 24#include "cloexec.h"
 25
 26volatile long the_var;
 
 
 
 
 
 
 
 
 
 
 
 
 
 27
 28static noinline int test_function(void)
 29{
 30	return 0;
 31}
 32
 33static int __event(bool is_x, void *addr, struct perf_event_attr *attr)
 34{
 35	int fd;
 36
 37	memset(attr, 0, sizeof(struct perf_event_attr));
 38	attr->type = PERF_TYPE_BREAKPOINT;
 39	attr->size = sizeof(struct perf_event_attr);
 40
 41	attr->config = 0;
 42	attr->bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
 43	attr->bp_addr = (unsigned long) addr;
 44	attr->bp_len = sizeof(long);
 45
 46	attr->sample_period = 1;
 47	attr->sample_type = PERF_SAMPLE_IP;
 48
 49	attr->exclude_kernel = 1;
 50	attr->exclude_hv = 1;
 51
 52	fd = sys_perf_event_open(attr, -1, 0, -1,
 53				 perf_event_open_cloexec_flag());
 54	if (fd < 0) {
 55		pr_debug("failed opening event %llx\n", attr->config);
 56		return TEST_FAIL;
 57	}
 58
 59	return fd;
 60}
 61
 62static int wp_event(void *addr, struct perf_event_attr *attr)
 63{
 64	return __event(false, addr, attr);
 65}
 66
 67static int bp_event(void *addr, struct perf_event_attr *attr)
 68{
 69	return __event(true, addr, attr);
 70}
 71
 72static int bp_accounting(int wp_cnt, int share)
 73{
 74	struct perf_event_attr attr, attr_mod, attr_new;
 75	int i, fd[wp_cnt], fd_wp, ret;
 76
 77	for (i = 0; i < wp_cnt; i++) {
 78		fd[i] = wp_event((void *)&the_var, &attr);
 79		TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
 80		pr_debug("wp %d created\n", i);
 81	}
 82
 83	attr_mod = attr;
 84	attr_mod.bp_type = HW_BREAKPOINT_X;
 85	attr_mod.bp_addr = (unsigned long) test_function;
 86
 87	ret = ioctl(fd[0], PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr_mod);
 88	TEST_ASSERT_VAL("failed to modify wp\n", ret == 0);
 89
 90	pr_debug("wp 0 modified to bp\n");
 91
 92	if (!share) {
 93		fd_wp = wp_event((void *)&the_var, &attr_new);
 94		TEST_ASSERT_VAL("failed to create max wp\n", fd_wp != -1);
 95		pr_debug("wp max created\n");
 96	}
 97
 98	for (i = 0; i < wp_cnt; i++)
 99		close(fd[i]);
100
101	return 0;
102}
103
104static int detect_cnt(bool is_x)
105{
106	struct perf_event_attr attr;
107	void *addr = is_x ? (void *)test_function : (void *)&the_var;
108	int fd[100], cnt = 0, i;
109
110	while (1) {
111		if (cnt == 100) {
112			pr_debug("way too many debug registers, fix the test\n");
113			return 0;
114		}
115		fd[cnt] = __event(is_x, addr, &attr);
116
117		if (fd[cnt] < 0)
118			break;
119		cnt++;
120	}
121
122	for (i = 0; i < cnt; i++)
123		close(fd[i]);
124
125	return cnt;
126}
127
128static int detect_ioctl(void)
129{
130	struct perf_event_attr attr;
131	int fd, ret = 1;
132
133	fd = wp_event((void *) &the_var, &attr);
134	if (fd > 0) {
135		ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr);
136		close(fd);
137	}
138
139	return ret ? 0 : 1;
140}
141
142static int detect_share(int wp_cnt, int bp_cnt)
143{
144	struct perf_event_attr attr;
145	int i, fd[wp_cnt + bp_cnt], ret;
 
 
 
 
 
 
 
146
147	for (i = 0; i < wp_cnt; i++) {
148		fd[i] = wp_event((void *)&the_var, &attr);
149		TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
 
 
 
150	}
151
152	for (; i < (bp_cnt + wp_cnt); i++) {
153		fd[i] = bp_event((void *)test_function, &attr);
154		if (fd[i] == -1)
155			break;
156	}
157
158	ret = i != (bp_cnt + wp_cnt);
159
 
160	while (i--)
161		close(fd[i]);
162
 
163	return ret;
164}
165
166/*
167 * This test does following:
168 *   - detects the number of watch/break-points,
169 *     skip test if any is missing
170 *   - detects PERF_EVENT_IOC_MODIFY_ATTRIBUTES ioctl,
171 *     skip test if it's missing
172 *   - detects if watchpoints and breakpoints share
173 *     same slots
174 *   - create all possible watchpoints on cpu 0
175 *   - change one of it to breakpoint
176 *   - in case wp and bp do not share slots,
177 *     we create another watchpoint to ensure
178 *     the slot accounting is correct
179 */
180int test__bp_accounting(struct test *test __maybe_unused, int subtest __maybe_unused)
181{
182	int has_ioctl = detect_ioctl();
183	int wp_cnt = detect_cnt(false);
184	int bp_cnt = detect_cnt(true);
185	int share  = detect_share(wp_cnt, bp_cnt);
186
 
 
 
 
 
187	pr_debug("watchpoints count %d, breakpoints count %d, has_ioctl %d, share %d\n",
188		 wp_cnt, bp_cnt, has_ioctl, share);
189
190	if (!wp_cnt || !bp_cnt || !has_ioctl)
191		return TEST_SKIP;
192
193	return bp_accounting(wp_cnt, share);
194}
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
  4 * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
  5 */
  6#define __SANE_USERSPACE_TYPES__
  7
  8#include <stdlib.h>
  9#include <stdio.h>
 10#include <unistd.h>
 11#include <string.h>
 12#include <sys/ioctl.h>
 
 13#include <fcntl.h>
 
 
 
 14#include <linux/hw_breakpoint.h>
 15
 16#include "tests.h"
 17#include "debug.h"
 18#include "event.h"
 19#include "../perf-sys.h"
 20#include "cloexec.h"
 21
 22/*
 23 * PowerPC and S390 do not support creation of instruction breakpoints using the
 24 * perf_event interface.
 25 *
 26 * Just disable the test for these architectures until these issues are
 27 * resolved.
 28 */
 29#if defined(__powerpc__) || defined(__s390x__)
 30#define BP_ACCOUNT_IS_SUPPORTED 0
 31#else
 32#define BP_ACCOUNT_IS_SUPPORTED 1
 33#endif
 34
 35static volatile long the_var;
 36
 37static noinline int test_function(void)
 38{
 39	return 0;
 40}
 41
 42static int __event(bool is_x, void *addr, struct perf_event_attr *attr)
 43{
 44	int fd;
 45
 46	memset(attr, 0, sizeof(struct perf_event_attr));
 47	attr->type = PERF_TYPE_BREAKPOINT;
 48	attr->size = sizeof(struct perf_event_attr);
 49
 50	attr->config = 0;
 51	attr->bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
 52	attr->bp_addr = (unsigned long) addr;
 53	attr->bp_len = sizeof(long);
 54
 55	attr->sample_period = 1;
 56	attr->sample_type = PERF_SAMPLE_IP;
 57
 58	attr->exclude_kernel = 1;
 59	attr->exclude_hv = 1;
 60
 61	fd = sys_perf_event_open(attr, -1, 0, -1,
 62				 perf_event_open_cloexec_flag());
 63	if (fd < 0) {
 64		pr_debug("failed opening event %llx\n", attr->config);
 65		return TEST_FAIL;
 66	}
 67
 68	return fd;
 69}
 70
 71static int wp_event(void *addr, struct perf_event_attr *attr)
 72{
 73	return __event(false, addr, attr);
 74}
 75
 76static int bp_event(void *addr, struct perf_event_attr *attr)
 77{
 78	return __event(true, addr, attr);
 79}
 80
 81static int bp_accounting(int wp_cnt, int share)
 82{
 83	struct perf_event_attr attr, attr_mod, attr_new;
 84	int i, fd[wp_cnt], fd_wp, ret;
 85
 86	for (i = 0; i < wp_cnt; i++) {
 87		fd[i] = wp_event((void *)&the_var, &attr);
 88		TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
 89		pr_debug("wp %d created\n", i);
 90	}
 91
 92	attr_mod = attr;
 93	attr_mod.bp_type = HW_BREAKPOINT_X;
 94	attr_mod.bp_addr = (unsigned long) test_function;
 95
 96	ret = ioctl(fd[0], PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr_mod);
 97	TEST_ASSERT_VAL("failed to modify wp\n", ret == 0);
 98
 99	pr_debug("wp 0 modified to bp\n");
100
101	if (!share) {
102		fd_wp = wp_event((void *)&the_var, &attr_new);
103		TEST_ASSERT_VAL("failed to create max wp\n", fd_wp != -1);
104		pr_debug("wp max created\n");
105	}
106
107	for (i = 0; i < wp_cnt; i++)
108		close(fd[i]);
109
110	return 0;
111}
112
113static int detect_cnt(bool is_x)
114{
115	struct perf_event_attr attr;
116	void *addr = is_x ? (void *)test_function : (void *)&the_var;
117	int fd[100], cnt = 0, i;
118
119	while (1) {
120		if (cnt == 100) {
121			pr_debug("way too many debug registers, fix the test\n");
122			return 0;
123		}
124		fd[cnt] = __event(is_x, addr, &attr);
125
126		if (fd[cnt] < 0)
127			break;
128		cnt++;
129	}
130
131	for (i = 0; i < cnt; i++)
132		close(fd[i]);
133
134	return cnt;
135}
136
137static int detect_ioctl(void)
138{
139	struct perf_event_attr attr;
140	int fd, ret = 1;
141
142	fd = wp_event((void *) &the_var, &attr);
143	if (fd > 0) {
144		ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &attr);
145		close(fd);
146	}
147
148	return ret ? 0 : 1;
149}
150
151static int detect_share(int wp_cnt, int bp_cnt)
152{
153	struct perf_event_attr attr;
154	int i, *fd = NULL, ret = -1;
155
156	if (wp_cnt + bp_cnt == 0)
157		return 0;
158
159	fd = malloc(sizeof(int) * (wp_cnt + bp_cnt));
160	if (!fd)
161		return -1;
162
163	for (i = 0; i < wp_cnt; i++) {
164		fd[i] = wp_event((void *)&the_var, &attr);
165		if (fd[i] == -1) {
166			pr_err("failed to create wp\n");
167			goto out;
168		}
169	}
170
171	for (; i < (bp_cnt + wp_cnt); i++) {
172		fd[i] = bp_event((void *)test_function, &attr);
173		if (fd[i] == -1)
174			break;
175	}
176
177	ret = i != (bp_cnt + wp_cnt);
178
179out:
180	while (i--)
181		close(fd[i]);
182
183	free(fd);
184	return ret;
185}
186
187/*
188 * This test does following:
189 *   - detects the number of watch/break-points,
190 *     skip test if any is missing
191 *   - detects PERF_EVENT_IOC_MODIFY_ATTRIBUTES ioctl,
192 *     skip test if it's missing
193 *   - detects if watchpoints and breakpoints share
194 *     same slots
195 *   - create all possible watchpoints on cpu 0
196 *   - change one of it to breakpoint
197 *   - in case wp and bp do not share slots,
198 *     we create another watchpoint to ensure
199 *     the slot accounting is correct
200 */
201static int test__bp_accounting(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
202{
203	int has_ioctl = detect_ioctl();
204	int wp_cnt = detect_cnt(false);
205	int bp_cnt = detect_cnt(true);
206	int share  = detect_share(wp_cnt, bp_cnt);
207
208	if (!BP_ACCOUNT_IS_SUPPORTED) {
209		pr_debug("Test not supported on this architecture");
210		return TEST_SKIP;
211	}
212
213	pr_debug("watchpoints count %d, breakpoints count %d, has_ioctl %d, share %d\n",
214		 wp_cnt, bp_cnt, has_ioctl, share);
215
216	if (!wp_cnt || !bp_cnt || !has_ioctl)
217		return TEST_SKIP;
218
219	return bp_accounting(wp_cnt, share);
220}
221
222DEFINE_SUITE("Breakpoint accounting", bp_accounting);