abi_test.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * User Events ABI Test Program
  4. *
  5. * Copyright (c) 2022 Beau Belgrave <beaub@linux.microsoft.com>
  6. */
  7. #define _GNU_SOURCE
  8. #include <sched.h>
  9. #include <errno.h>
  10. #include <linux/user_events.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 <glob.h>
  18. #include <string.h>
  19. #include <asm/unistd.h>
  20. #include "kselftest_harness.h"
  21. #include "user_events_selftests.h"
  22. const char *data_file = "/sys/kernel/tracing/user_events_data";
  23. const char *enable_file = "/sys/kernel/tracing/events/user_events/__abi_event/enable";
  24. const char *multi_dir_glob = "/sys/kernel/tracing/events/user_events_multi/__abi_event.*";
  25. static int wait_for_delete(char *dir)
  26. {
  27. struct stat buf;
  28. int i;
  29. for (i = 0; i < 10000; ++i) {
  30. if (stat(dir, &buf) == -1 && errno == ENOENT)
  31. return 0;
  32. usleep(1000);
  33. }
  34. return -1;
  35. }
  36. static int find_multi_event_dir(char *unique_field, char *out_dir, int dir_len)
  37. {
  38. char path[256];
  39. glob_t buf;
  40. int i, ret;
  41. ret = glob(multi_dir_glob, GLOB_ONLYDIR, NULL, &buf);
  42. if (ret)
  43. return -1;
  44. ret = -1;
  45. for (i = 0; i < buf.gl_pathc; ++i) {
  46. FILE *fp;
  47. snprintf(path, sizeof(path), "%s/format", buf.gl_pathv[i]);
  48. fp = fopen(path, "r");
  49. if (!fp)
  50. continue;
  51. while (fgets(path, sizeof(path), fp) != NULL) {
  52. if (strstr(path, unique_field)) {
  53. fclose(fp);
  54. /* strscpy is not available, use snprintf */
  55. snprintf(out_dir, dir_len, "%s", buf.gl_pathv[i]);
  56. ret = 0;
  57. goto out;
  58. }
  59. }
  60. fclose(fp);
  61. }
  62. out:
  63. globfree(&buf);
  64. return ret;
  65. }
  66. static bool event_exists(void)
  67. {
  68. int fd = open(enable_file, O_RDWR);
  69. if (fd < 0)
  70. return false;
  71. close(fd);
  72. return true;
  73. }
  74. static int change_event(bool enable)
  75. {
  76. int fd = open(enable_file, O_RDWR);
  77. int ret;
  78. if (fd < 0)
  79. return -1;
  80. if (enable)
  81. ret = write(fd, "1", 1);
  82. else
  83. ret = write(fd, "0", 1);
  84. close(fd);
  85. if (ret == 1)
  86. ret = 0;
  87. else
  88. ret = -1;
  89. return ret;
  90. }
  91. static int event_delete(void)
  92. {
  93. int fd = open(data_file, O_RDWR);
  94. int ret;
  95. if (fd < 0)
  96. return -1;
  97. ret = ioctl(fd, DIAG_IOCSDEL, "__abi_event");
  98. close(fd);
  99. return ret;
  100. }
  101. static int reg_enable_multi(void *enable, int size, int bit, int flags,
  102. char *args)
  103. {
  104. struct user_reg reg = {0};
  105. char full_args[512] = {0};
  106. int fd = open(data_file, O_RDWR);
  107. int len;
  108. int ret;
  109. if (fd < 0)
  110. return -1;
  111. len = snprintf(full_args, sizeof(full_args), "__abi_event %s", args);
  112. if (len > sizeof(full_args)) {
  113. ret = -E2BIG;
  114. goto out;
  115. }
  116. reg.size = sizeof(reg);
  117. reg.name_args = (__u64)full_args;
  118. reg.flags = USER_EVENT_REG_MULTI_FORMAT | flags;
  119. reg.enable_bit = bit;
  120. reg.enable_addr = (__u64)enable;
  121. reg.enable_size = size;
  122. ret = ioctl(fd, DIAG_IOCSREG, &reg);
  123. out:
  124. close(fd);
  125. return ret;
  126. }
  127. static int reg_enable_flags(void *enable, int size, int bit, int flags)
  128. {
  129. struct user_reg reg = {0};
  130. int fd = open(data_file, O_RDWR);
  131. int ret;
  132. if (fd < 0)
  133. return -1;
  134. reg.size = sizeof(reg);
  135. reg.name_args = (__u64)"__abi_event";
  136. reg.flags = flags;
  137. reg.enable_bit = bit;
  138. reg.enable_addr = (__u64)enable;
  139. reg.enable_size = size;
  140. ret = ioctl(fd, DIAG_IOCSREG, &reg);
  141. close(fd);
  142. return ret;
  143. }
  144. static int reg_enable(void *enable, int size, int bit)
  145. {
  146. return reg_enable_flags(enable, size, bit, 0);
  147. }
  148. static int reg_disable(void *enable, int bit)
  149. {
  150. struct user_unreg reg = {0};
  151. int fd = open(data_file, O_RDWR);
  152. int ret;
  153. if (fd < 0)
  154. return -1;
  155. reg.size = sizeof(reg);
  156. reg.disable_bit = bit;
  157. reg.disable_addr = (__u64)enable;
  158. ret = ioctl(fd, DIAG_IOCSUNREG, &reg);
  159. close(fd);
  160. return ret;
  161. }
  162. FIXTURE(user) {
  163. int check;
  164. long check_long;
  165. bool umount;
  166. };
  167. FIXTURE_SETUP(user) {
  168. USER_EVENT_FIXTURE_SETUP(return, self->umount);
  169. change_event(false);
  170. self->check = 0;
  171. self->check_long = 0;
  172. }
  173. FIXTURE_TEARDOWN(user) {
  174. USER_EVENT_FIXTURE_TEARDOWN(self->umount);
  175. }
  176. TEST_F(user, enablement) {
  177. /* Changes should be reflected immediately */
  178. ASSERT_EQ(0, self->check);
  179. ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0));
  180. ASSERT_EQ(0, change_event(true));
  181. ASSERT_EQ(1, self->check);
  182. ASSERT_EQ(0, change_event(false));
  183. ASSERT_EQ(0, self->check);
  184. /* Ensure kernel clears bit after disable */
  185. ASSERT_EQ(0, change_event(true));
  186. ASSERT_EQ(1, self->check);
  187. ASSERT_EQ(0, reg_disable(&self->check, 0));
  188. ASSERT_EQ(0, self->check);
  189. /* Ensure doesn't change after unreg */
  190. ASSERT_EQ(0, change_event(true));
  191. ASSERT_EQ(0, self->check);
  192. ASSERT_EQ(0, change_event(false));
  193. }
  194. TEST_F(user, flags) {
  195. /* USER_EVENT_REG_PERSIST is allowed */
  196. ASSERT_EQ(0, reg_enable_flags(&self->check, sizeof(int), 0,
  197. USER_EVENT_REG_PERSIST));
  198. ASSERT_EQ(0, reg_disable(&self->check, 0));
  199. /* Ensure it exists after close and disable */
  200. ASSERT_TRUE(event_exists());
  201. /* Ensure we can delete it */
  202. ASSERT_EQ(0, event_delete());
  203. /* USER_EVENT_REG_MAX or above is not allowed */
  204. ASSERT_EQ(-1, reg_enable_flags(&self->check, sizeof(int), 0,
  205. USER_EVENT_REG_MAX));
  206. /* Ensure it does not exist after invalid flags */
  207. ASSERT_FALSE(event_exists());
  208. }
  209. TEST_F(user, bit_sizes) {
  210. /* Allow 0-31 bits for 32-bit */
  211. ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0));
  212. ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 31));
  213. ASSERT_NE(0, reg_enable(&self->check, sizeof(int), 32));
  214. ASSERT_EQ(0, reg_disable(&self->check, 0));
  215. ASSERT_EQ(0, reg_disable(&self->check, 31));
  216. #if BITS_PER_LONG == 8
  217. /* Allow 0-64 bits for 64-bit */
  218. ASSERT_EQ(0, reg_enable(&self->check_long, sizeof(long), 63));
  219. ASSERT_NE(0, reg_enable(&self->check_long, sizeof(long), 64));
  220. ASSERT_EQ(0, reg_disable(&self->check_long, 63));
  221. #endif
  222. /* Disallowed sizes (everything beside 4 and 8) */
  223. ASSERT_NE(0, reg_enable(&self->check, 1, 0));
  224. ASSERT_NE(0, reg_enable(&self->check, 2, 0));
  225. ASSERT_NE(0, reg_enable(&self->check, 3, 0));
  226. ASSERT_NE(0, reg_enable(&self->check, 5, 0));
  227. ASSERT_NE(0, reg_enable(&self->check, 6, 0));
  228. ASSERT_NE(0, reg_enable(&self->check, 7, 0));
  229. ASSERT_NE(0, reg_enable(&self->check, 9, 0));
  230. ASSERT_NE(0, reg_enable(&self->check, 128, 0));
  231. }
  232. TEST_F(user, multi_format) {
  233. char first_dir[256];
  234. char second_dir[256];
  235. struct stat buf;
  236. /* Multiple formats for the same name should work */
  237. ASSERT_EQ(0, reg_enable_multi(&self->check, sizeof(int), 0,
  238. 0, "u32 multi_first"));
  239. ASSERT_EQ(0, reg_enable_multi(&self->check, sizeof(int), 1,
  240. 0, "u64 multi_second"));
  241. /* Same name with same format should also work */
  242. ASSERT_EQ(0, reg_enable_multi(&self->check, sizeof(int), 2,
  243. 0, "u64 multi_second"));
  244. ASSERT_EQ(0, find_multi_event_dir("multi_first",
  245. first_dir, sizeof(first_dir)));
  246. ASSERT_EQ(0, find_multi_event_dir("multi_second",
  247. second_dir, sizeof(second_dir)));
  248. /* Should not be found in the same dir */
  249. ASSERT_NE(0, strcmp(first_dir, second_dir));
  250. /* First dir should still exist */
  251. ASSERT_EQ(0, stat(first_dir, &buf));
  252. /* Disabling first register should remove first dir */
  253. ASSERT_EQ(0, reg_disable(&self->check, 0));
  254. ASSERT_EQ(0, wait_for_delete(first_dir));
  255. /* Second dir should still exist */
  256. ASSERT_EQ(0, stat(second_dir, &buf));
  257. /* Disabling second register should remove second dir */
  258. ASSERT_EQ(0, reg_disable(&self->check, 1));
  259. /* Ensure bit 1 and 2 are tied together, should not delete yet */
  260. ASSERT_EQ(0, stat(second_dir, &buf));
  261. ASSERT_EQ(0, reg_disable(&self->check, 2));
  262. ASSERT_EQ(0, wait_for_delete(second_dir));
  263. }
  264. TEST_F(user, forks) {
  265. int i;
  266. /* Ensure COW pages get updated after fork */
  267. ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0));
  268. ASSERT_EQ(0, self->check);
  269. if (fork() == 0) {
  270. /* Force COW */
  271. self->check = 0;
  272. /* Up to 1 sec for enablement */
  273. for (i = 0; i < 10; ++i) {
  274. usleep(100000);
  275. if (self->check)
  276. exit(0);
  277. }
  278. exit(1);
  279. }
  280. /* Allow generous time for COW, then enable */
  281. usleep(100000);
  282. ASSERT_EQ(0, change_event(true));
  283. ASSERT_NE(-1, wait(&i));
  284. ASSERT_EQ(0, WEXITSTATUS(i));
  285. /* Ensure child doesn't disable parent */
  286. if (fork() == 0)
  287. exit(reg_disable(&self->check, 0));
  288. ASSERT_NE(-1, wait(&i));
  289. ASSERT_EQ(0, WEXITSTATUS(i));
  290. ASSERT_EQ(1, self->check);
  291. ASSERT_EQ(0, change_event(false));
  292. ASSERT_EQ(0, self->check);
  293. }
  294. /* Waits up to 1 sec for enablement */
  295. static int clone_check(void *check)
  296. {
  297. int i;
  298. for (i = 0; i < 10; ++i) {
  299. usleep(100000);
  300. if (*(int *)check)
  301. return 0;
  302. }
  303. return 1;
  304. }
  305. TEST_F(user, clones) {
  306. int i, stack_size = 4096;
  307. void *stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
  308. MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
  309. -1, 0);
  310. ASSERT_NE(MAP_FAILED, stack);
  311. ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0));
  312. ASSERT_EQ(0, self->check);
  313. /* Shared VM should see enablements */
  314. ASSERT_NE(-1, clone(&clone_check, stack + stack_size,
  315. CLONE_VM | SIGCHLD, &self->check));
  316. ASSERT_EQ(0, change_event(true));
  317. ASSERT_NE(-1, wait(&i));
  318. ASSERT_EQ(0, WEXITSTATUS(i));
  319. munmap(stack, stack_size);
  320. ASSERT_EQ(0, change_event(false));
  321. }
  322. int main(int argc, char **argv)
  323. {
  324. return test_harness_run(argc, argv);
  325. }