File Index
Files and directories
Sign in
FILE

bp_account.c

File preview with syntax highlighting.

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