Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/user_events: Test multi-format events

User_events now has multi-format events which allow for the same
register name, but with different formats. When this occurs, different
tracepoints are created with unique names.

Add a new test that ensures the same name can be used for two different
formats. Ensure they are isolated from each other and that name and arg
matching still works if yet another register comes in with the same
format as one of the two.

Link: https://lore.kernel.org/linux-trace-kernel/20240222001807.1463-4-beaub@linux.microsoft.com

Signed-off-by: Beau Belgrave <beaub@linux.microsoft.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

authored by

Beau Belgrave and committed by
Steven Rostedt (Google)
bcb7bdcc 64805e40

+134
+134
tools/testing/selftests/user_events/abi_test.c
··· 16 16 #include <sys/ioctl.h> 17 17 #include <sys/stat.h> 18 18 #include <unistd.h> 19 + #include <glob.h> 20 + #include <string.h> 19 21 #include <asm/unistd.h> 20 22 21 23 #include "../kselftest_harness.h" ··· 25 23 26 24 const char *data_file = "/sys/kernel/tracing/user_events_data"; 27 25 const char *enable_file = "/sys/kernel/tracing/events/user_events/__abi_event/enable"; 26 + const char *multi_dir_glob = "/sys/kernel/tracing/events/user_events_multi/__abi_event.*"; 27 + 28 + static int wait_for_delete(char *dir) 29 + { 30 + struct stat buf; 31 + int i; 32 + 33 + for (i = 0; i < 10000; ++i) { 34 + if (stat(dir, &buf) == -1 && errno == ENOENT) 35 + return 0; 36 + 37 + usleep(1000); 38 + } 39 + 40 + return -1; 41 + } 42 + 43 + static int find_multi_event_dir(char *unique_field, char *out_dir, int dir_len) 44 + { 45 + char path[256]; 46 + glob_t buf; 47 + int i, ret; 48 + 49 + ret = glob(multi_dir_glob, GLOB_ONLYDIR, NULL, &buf); 50 + 51 + if (ret) 52 + return -1; 53 + 54 + ret = -1; 55 + 56 + for (i = 0; i < buf.gl_pathc; ++i) { 57 + FILE *fp; 58 + 59 + snprintf(path, sizeof(path), "%s/format", buf.gl_pathv[i]); 60 + fp = fopen(path, "r"); 61 + 62 + if (!fp) 63 + continue; 64 + 65 + while (fgets(path, sizeof(path), fp) != NULL) { 66 + if (strstr(path, unique_field)) { 67 + fclose(fp); 68 + /* strscpy is not available, use snprintf */ 69 + snprintf(out_dir, dir_len, "%s", buf.gl_pathv[i]); 70 + ret = 0; 71 + goto out; 72 + } 73 + } 74 + 75 + fclose(fp); 76 + } 77 + out: 78 + globfree(&buf); 79 + 80 + return ret; 81 + } 28 82 29 83 static bool event_exists(void) 30 84 { ··· 127 69 128 70 ret = ioctl(fd, DIAG_IOCSDEL, "__abi_event"); 129 71 72 + close(fd); 73 + 74 + return ret; 75 + } 76 + 77 + static int reg_enable_multi(void *enable, int size, int bit, int flags, 78 + char *args) 79 + { 80 + struct user_reg reg = {0}; 81 + char full_args[512] = {0}; 82 + int fd = open(data_file, O_RDWR); 83 + int len; 84 + int ret; 85 + 86 + if (fd < 0) 87 + return -1; 88 + 89 + len = snprintf(full_args, sizeof(full_args), "__abi_event %s", args); 90 + 91 + if (len > sizeof(full_args)) { 92 + ret = -E2BIG; 93 + goto out; 94 + } 95 + 96 + reg.size = sizeof(reg); 97 + reg.name_args = (__u64)full_args; 98 + reg.flags = USER_EVENT_REG_MULTI_FORMAT | flags; 99 + reg.enable_bit = bit; 100 + reg.enable_addr = (__u64)enable; 101 + reg.enable_size = size; 102 + 103 + ret = ioctl(fd, DIAG_IOCSREG, &reg); 104 + out: 130 105 close(fd); 131 106 132 107 return ret; ··· 296 205 ASSERT_NE(0, reg_enable(&self->check, 7, 0)); 297 206 ASSERT_NE(0, reg_enable(&self->check, 9, 0)); 298 207 ASSERT_NE(0, reg_enable(&self->check, 128, 0)); 208 + } 209 + 210 + TEST_F(user, multi_format) { 211 + char first_dir[256]; 212 + char second_dir[256]; 213 + struct stat buf; 214 + 215 + /* Multiple formats for the same name should work */ 216 + ASSERT_EQ(0, reg_enable_multi(&self->check, sizeof(int), 0, 217 + 0, "u32 multi_first")); 218 + 219 + ASSERT_EQ(0, reg_enable_multi(&self->check, sizeof(int), 1, 220 + 0, "u64 multi_second")); 221 + 222 + /* Same name with same format should also work */ 223 + ASSERT_EQ(0, reg_enable_multi(&self->check, sizeof(int), 2, 224 + 0, "u64 multi_second")); 225 + 226 + ASSERT_EQ(0, find_multi_event_dir("multi_first", 227 + first_dir, sizeof(first_dir))); 228 + 229 + ASSERT_EQ(0, find_multi_event_dir("multi_second", 230 + second_dir, sizeof(second_dir))); 231 + 232 + /* Should not be found in the same dir */ 233 + ASSERT_NE(0, strcmp(first_dir, second_dir)); 234 + 235 + /* First dir should still exist */ 236 + ASSERT_EQ(0, stat(first_dir, &buf)); 237 + 238 + /* Disabling first register should remove first dir */ 239 + ASSERT_EQ(0, reg_disable(&self->check, 0)); 240 + ASSERT_EQ(0, wait_for_delete(first_dir)); 241 + 242 + /* Second dir should still exist */ 243 + ASSERT_EQ(0, stat(second_dir, &buf)); 244 + 245 + /* Disabling second register should remove second dir */ 246 + ASSERT_EQ(0, reg_disable(&self->check, 1)); 247 + /* Ensure bit 1 and 2 are tied together, should not delete yet */ 248 + ASSERT_EQ(0, stat(second_dir, &buf)); 249 + ASSERT_EQ(0, reg_disable(&self->check, 2)); 250 + ASSERT_EQ(0, wait_for_delete(second_dir)); 299 251 } 300 252 301 253 TEST_F(user, forks) {