1
0

sud_benchmark.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020 Collabora Ltd.
  4. *
  5. * Benchmark and test syscall user dispatch
  6. */
  7. #define _GNU_SOURCE
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <signal.h>
  12. #include <errno.h>
  13. #include <time.h>
  14. #include <sys/time.h>
  15. #include <unistd.h>
  16. #include <sys/sysinfo.h>
  17. #include <sys/prctl.h>
  18. #include <sys/syscall.h>
  19. #ifndef PR_SET_SYSCALL_USER_DISPATCH
  20. # define PR_SET_SYSCALL_USER_DISPATCH 59
  21. # define PR_SYS_DISPATCH_OFF 0
  22. # define PR_SYS_DISPATCH_ON 1
  23. # define SYSCALL_DISPATCH_FILTER_ALLOW 0
  24. # define SYSCALL_DISPATCH_FILTER_BLOCK 1
  25. #endif
  26. #ifdef __NR_syscalls
  27. # define MAGIC_SYSCALL_1 (__NR_syscalls + 1) /* Bad Linux syscall number */
  28. #else
  29. # define MAGIC_SYSCALL_1 (0xff00) /* Bad Linux syscall number */
  30. #endif
  31. /*
  32. * To test returning from a sigsys with selector blocked, the test
  33. * requires some per-architecture support (i.e. knowledge about the
  34. * signal trampoline address). On i386, we know it is on the vdso, and
  35. * a small trampoline is open-coded for x86_64. Other architectures
  36. * that have a trampoline in the vdso will support TEST_BLOCKED_RETURN
  37. * out of the box, but don't enable them until they support syscall user
  38. * dispatch.
  39. */
  40. #if defined(__x86_64__) || defined(__i386__)
  41. #define TEST_BLOCKED_RETURN
  42. #endif
  43. #ifdef __x86_64__
  44. void* (syscall_dispatcher_start)(void);
  45. void* (syscall_dispatcher_end)(void);
  46. #else
  47. unsigned long syscall_dispatcher_start = 0;
  48. unsigned long syscall_dispatcher_end = 0;
  49. #endif
  50. unsigned long trapped_call_count = 0;
  51. unsigned long native_call_count = 0;
  52. char selector;
  53. #define SYSCALL_BLOCK (selector = SYSCALL_DISPATCH_FILTER_BLOCK)
  54. #define SYSCALL_UNBLOCK (selector = SYSCALL_DISPATCH_FILTER_ALLOW)
  55. #define CALIBRATION_STEP 100000
  56. #define CALIBRATE_TO_SECS 5
  57. int factor;
  58. static double one_sysinfo_step(void)
  59. {
  60. struct timespec t1, t2;
  61. int i;
  62. struct sysinfo info;
  63. clock_gettime(CLOCK_MONOTONIC, &t1);
  64. for (i = 0; i < CALIBRATION_STEP; i++)
  65. sysinfo(&info);
  66. clock_gettime(CLOCK_MONOTONIC, &t2);
  67. return (t2.tv_sec - t1.tv_sec) + 1.0e-9 * (t2.tv_nsec - t1.tv_nsec);
  68. }
  69. static void calibrate_set(void)
  70. {
  71. double elapsed = 0;
  72. printf("Calibrating test set to last ~%d seconds...\n", CALIBRATE_TO_SECS);
  73. while (elapsed < 1) {
  74. elapsed += one_sysinfo_step();
  75. factor += CALIBRATE_TO_SECS;
  76. }
  77. printf("test iterations = %d\n", CALIBRATION_STEP * factor);
  78. }
  79. static double perf_syscall(void)
  80. {
  81. unsigned int i;
  82. double partial = 0;
  83. for (i = 0; i < factor; ++i)
  84. partial += one_sysinfo_step()/(CALIBRATION_STEP*factor);
  85. return partial;
  86. }
  87. static void handle_sigsys(int sig, siginfo_t *info, void *ucontext)
  88. {
  89. char buf[1024];
  90. int len;
  91. SYSCALL_UNBLOCK;
  92. /* printf and friends are not signal-safe. */
  93. len = snprintf(buf, 1024, "Caught sys_%x\n", info->si_syscall);
  94. write(1, buf, len);
  95. if (info->si_syscall == MAGIC_SYSCALL_1)
  96. trapped_call_count++;
  97. else
  98. native_call_count++;
  99. #ifdef TEST_BLOCKED_RETURN
  100. SYSCALL_BLOCK;
  101. #endif
  102. #ifdef __x86_64__
  103. __asm__ volatile("movq $0xf, %rax");
  104. __asm__ volatile("leaveq");
  105. __asm__ volatile("add $0x8, %rsp");
  106. __asm__ volatile("syscall_dispatcher_start:");
  107. __asm__ volatile("syscall");
  108. __asm__ volatile("nop"); /* Landing pad within dispatcher area */
  109. __asm__ volatile("syscall_dispatcher_end:");
  110. #endif
  111. }
  112. int main(void)
  113. {
  114. struct sigaction act;
  115. double time1, time2;
  116. int ret;
  117. sigset_t mask;
  118. memset(&act, 0, sizeof(act));
  119. sigemptyset(&mask);
  120. act.sa_sigaction = handle_sigsys;
  121. act.sa_flags = SA_SIGINFO;
  122. act.sa_mask = mask;
  123. calibrate_set();
  124. time1 = perf_syscall();
  125. printf("Avg syscall time %.0lfns.\n", time1 * 1.0e9);
  126. ret = sigaction(SIGSYS, &act, NULL);
  127. if (ret) {
  128. perror("Error sigaction:");
  129. exit(-1);
  130. }
  131. fprintf(stderr, "Enabling syscall trapping.\n");
  132. if (prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON,
  133. syscall_dispatcher_start,
  134. (syscall_dispatcher_end - syscall_dispatcher_start + 1),
  135. &selector)) {
  136. perror("prctl failed\n");
  137. exit(-1);
  138. }
  139. SYSCALL_BLOCK;
  140. syscall(MAGIC_SYSCALL_1);
  141. #ifdef TEST_BLOCKED_RETURN
  142. if (selector == SYSCALL_DISPATCH_FILTER_ALLOW) {
  143. fprintf(stderr, "Failed to return with selector blocked.\n");
  144. exit(-1);
  145. }
  146. #endif
  147. SYSCALL_UNBLOCK;
  148. if (!trapped_call_count) {
  149. fprintf(stderr, "syscall trapping does not work.\n");
  150. exit(-1);
  151. }
  152. time2 = perf_syscall();
  153. if (native_call_count) {
  154. perror("syscall trapping intercepted more syscalls than expected\n");
  155. exit(-1);
  156. }
  157. printf("trapped_call_count %lu, native_call_count %lu.\n",
  158. trapped_call_count, native_call_count);
  159. printf("Avg syscall time %.0lfns.\n", time2 * 1.0e9);
  160. printf("Interception overhead: %.1lf%% (+%.0lfns).\n",
  161. 100.0 * (time2 / time1 - 1.0), 1.0e9 * (time2 - time1));
  162. return 0;
  163. }