1 // SPDX-License-Identifier: GPL-2.0
3 * User Events ABI Test Program
5 * Copyright (c) 2022 Beau Belgrave <beaub@linux.microsoft.com>
12 #include <linux/user_events.h>
16 #include <sys/ioctl.h>
19 #include <asm/unistd.h>
21 #include "../kselftest_harness.h"
22 #include "user_events_selftests.h"
24 const char *data_file = "/sys/kernel/tracing/user_events_data";
25 const char *enable_file = "/sys/kernel/tracing/events/user_events/__abi_event/enable";
27 static int change_event(bool enable)
29 int fd = open(enable_file, O_RDWR);
36 ret = write(fd, "1", 1);
38 ret = write(fd, "0", 1);
50 static int reg_enable(void *enable, int size, int bit)
52 struct user_reg reg = {0};
53 int fd = open(data_file, O_RDWR);
59 reg.size = sizeof(reg);
60 reg.name_args = (__u64)"__abi_event";
62 reg.enable_addr = (__u64)enable;
63 reg.enable_size = size;
65 ret = ioctl(fd, DIAG_IOCSREG, ®);
72 static int reg_disable(void *enable, int bit)
74 struct user_unreg reg = {0};
75 int fd = open(data_file, O_RDWR);
81 reg.size = sizeof(reg);
82 reg.disable_bit = bit;
83 reg.disable_addr = (__u64)enable;
85 ret = ioctl(fd, DIAG_IOCSUNREG, ®);
99 USER_EVENT_FIXTURE_SETUP(return, self->umount);
103 self->check_long = 0;
106 FIXTURE_TEARDOWN(user) {
107 USER_EVENT_FIXTURE_TEARDOWN(self->umount);
110 TEST_F(user, enablement) {
111 /* Changes should be reflected immediately */
112 ASSERT_EQ(0, self->check);
113 ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0));
114 ASSERT_EQ(0, change_event(true));
115 ASSERT_EQ(1, self->check);
116 ASSERT_EQ(0, change_event(false));
117 ASSERT_EQ(0, self->check);
119 /* Ensure kernel clears bit after disable */
120 ASSERT_EQ(0, change_event(true));
121 ASSERT_EQ(1, self->check);
122 ASSERT_EQ(0, reg_disable(&self->check, 0));
123 ASSERT_EQ(0, self->check);
125 /* Ensure doesn't change after unreg */
126 ASSERT_EQ(0, change_event(true));
127 ASSERT_EQ(0, self->check);
128 ASSERT_EQ(0, change_event(false));
131 TEST_F(user, bit_sizes) {
132 /* Allow 0-31 bits for 32-bit */
133 ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0));
134 ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 31));
135 ASSERT_NE(0, reg_enable(&self->check, sizeof(int), 32));
136 ASSERT_EQ(0, reg_disable(&self->check, 0));
137 ASSERT_EQ(0, reg_disable(&self->check, 31));
139 #if BITS_PER_LONG == 8
140 /* Allow 0-64 bits for 64-bit */
141 ASSERT_EQ(0, reg_enable(&self->check_long, sizeof(long), 63));
142 ASSERT_NE(0, reg_enable(&self->check_long, sizeof(long), 64));
143 ASSERT_EQ(0, reg_disable(&self->check_long, 63));
146 /* Disallowed sizes (everything beside 4 and 8) */
147 ASSERT_NE(0, reg_enable(&self->check, 1, 0));
148 ASSERT_NE(0, reg_enable(&self->check, 2, 0));
149 ASSERT_NE(0, reg_enable(&self->check, 3, 0));
150 ASSERT_NE(0, reg_enable(&self->check, 5, 0));
151 ASSERT_NE(0, reg_enable(&self->check, 6, 0));
152 ASSERT_NE(0, reg_enable(&self->check, 7, 0));
153 ASSERT_NE(0, reg_enable(&self->check, 9, 0));
154 ASSERT_NE(0, reg_enable(&self->check, 128, 0));
157 TEST_F(user, forks) {
160 /* Ensure COW pages get updated after fork */
161 ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0));
162 ASSERT_EQ(0, self->check);
168 /* Up to 1 sec for enablement */
169 for (i = 0; i < 10; ++i) {
179 /* Allow generous time for COW, then enable */
181 ASSERT_EQ(0, change_event(true));
183 ASSERT_NE(-1, wait(&i));
184 ASSERT_EQ(0, WEXITSTATUS(i));
186 /* Ensure child doesn't disable parent */
188 exit(reg_disable(&self->check, 0));
190 ASSERT_NE(-1, wait(&i));
191 ASSERT_EQ(0, WEXITSTATUS(i));
192 ASSERT_EQ(1, self->check);
193 ASSERT_EQ(0, change_event(false));
194 ASSERT_EQ(0, self->check);
197 /* Waits up to 1 sec for enablement */
198 static int clone_check(void *check)
202 for (i = 0; i < 10; ++i) {
212 TEST_F(user, clones) {
213 int i, stack_size = 4096;
214 void *stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
215 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
218 ASSERT_NE(MAP_FAILED, stack);
219 ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0));
220 ASSERT_EQ(0, self->check);
222 /* Shared VM should see enablements */
223 ASSERT_NE(-1, clone(&clone_check, stack + stack_size,
224 CLONE_VM | SIGCHLD, &self->check));
226 ASSERT_EQ(0, change_event(true));
227 ASSERT_NE(-1, wait(&i));
228 ASSERT_EQ(0, WEXITSTATUS(i));
229 munmap(stack, stack_size);
230 ASSERT_EQ(0, change_event(false));
233 int main(int argc, char **argv)
235 return test_harness_run(argc, argv);