1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * User Events Perf Events Test Program
4  *
5  * Copyright (c) 2021 Beau Belgrave <beaub@linux.microsoft.com>
6  */
7 
8 #include <errno.h>
9 #include <linux/user_events.h>
10 #include <linux/perf_event.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <fcntl.h>
14 #include <sys/ioctl.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <asm/unistd.h>
18 
19 #include "../kselftest_harness.h"
20 
21 const char *data_file = "/sys/kernel/tracing/user_events_data";
22 const char *id_file = "/sys/kernel/tracing/events/user_events/__test_event/id";
23 const char *fmt_file = "/sys/kernel/tracing/events/user_events/__test_event/format";
24 
25 struct event {
26 	__u32 index;
27 	__u32 field1;
28 	__u32 field2;
29 };
30 
31 static long perf_event_open(struct perf_event_attr *pe, pid_t pid,
32 			    int cpu, int group_fd, unsigned long flags)
33 {
34 	return syscall(__NR_perf_event_open, pe, pid, cpu, group_fd, flags);
35 }
36 
37 static int get_id(void)
38 {
39 	FILE *fp = fopen(id_file, "r");
40 	int ret, id = 0;
41 
42 	if (!fp)
43 		return -1;
44 
45 	ret = fscanf(fp, "%d", &id);
46 	fclose(fp);
47 
48 	if (ret != 1)
49 		return -1;
50 
51 	return id;
52 }
53 
54 static int get_offset(void)
55 {
56 	FILE *fp = fopen(fmt_file, "r");
57 	int ret, c, last = 0, offset = 0;
58 
59 	if (!fp)
60 		return -1;
61 
62 	/* Read until empty line */
63 	while (true) {
64 		c = getc(fp);
65 
66 		if (c == EOF)
67 			break;
68 
69 		if (last == '\n' && c == '\n')
70 			break;
71 
72 		last = c;
73 	}
74 
75 	ret = fscanf(fp, "\tfield:u32 field1;\toffset:%d;", &offset);
76 	fclose(fp);
77 
78 	if (ret != 1)
79 		return -1;
80 
81 	return offset;
82 }
83 
84 FIXTURE(user) {
85 	int data_fd;
86 	int check;
87 };
88 
89 FIXTURE_SETUP(user) {
90 	self->data_fd = open(data_file, O_RDWR);
91 	ASSERT_NE(-1, self->data_fd);
92 }
93 
94 FIXTURE_TEARDOWN(user) {
95 	close(self->data_fd);
96 }
97 
98 TEST_F(user, perf_write) {
99 	struct perf_event_attr pe = {0};
100 	struct user_reg reg = {0};
101 	struct event event;
102 	struct perf_event_mmap_page *perf_page;
103 	int page_size = sysconf(_SC_PAGESIZE);
104 	int id, fd, offset;
105 	__u32 *val;
106 
107 	reg.size = sizeof(reg);
108 	reg.name_args = (__u64)"__test_event u32 field1; u32 field2";
109 	reg.enable_bit = 31;
110 	reg.enable_addr = (__u64)&self->check;
111 	reg.enable_size = sizeof(self->check);
112 
113 	/* Register should work */
114 	ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, &reg));
115 	ASSERT_EQ(0, reg.write_index);
116 	ASSERT_EQ(0, self->check);
117 
118 	/* Id should be there */
119 	id = get_id();
120 	ASSERT_NE(-1, id);
121 	offset = get_offset();
122 	ASSERT_NE(-1, offset);
123 
124 	pe.type = PERF_TYPE_TRACEPOINT;
125 	pe.size = sizeof(pe);
126 	pe.config = id;
127 	pe.sample_type = PERF_SAMPLE_RAW;
128 	pe.sample_period = 1;
129 	pe.wakeup_events = 1;
130 
131 	/* Tracepoint attach should work */
132 	fd = perf_event_open(&pe, 0, -1, -1, 0);
133 	ASSERT_NE(-1, fd);
134 
135 	perf_page = mmap(NULL, page_size * 2, PROT_READ, MAP_SHARED, fd, 0);
136 	ASSERT_NE(MAP_FAILED, perf_page);
137 
138 	/* Status should be updated */
139 	ASSERT_EQ(1 << reg.enable_bit, self->check);
140 
141 	event.index = reg.write_index;
142 	event.field1 = 0xc001;
143 	event.field2 = 0xc01a;
144 
145 	/* Ensure write shows up at correct offset */
146 	ASSERT_NE(-1, write(self->data_fd, &event, sizeof(event)));
147 	val = (void *)(((char *)perf_page) + perf_page->data_offset);
148 	ASSERT_EQ(PERF_RECORD_SAMPLE, *val);
149 	/* Skip over header and size, move to offset */
150 	val += 3;
151 	val = (void *)((char *)val) + offset;
152 	/* Ensure correct */
153 	ASSERT_EQ(event.field1, *val++);
154 	ASSERT_EQ(event.field2, *val++);
155 
156 	munmap(perf_page, page_size * 2);
157 	close(fd);
158 
159 	/* Status should be updated */
160 	ASSERT_EQ(0, self->check);
161 }
162 
163 int main(int argc, char **argv)
164 {
165 	return test_harness_run(argc, argv);
166 }
167