bp_account.c 4.9 KB

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