migration.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * The main purpose of the tests here is to exercise the migration entry code
  4. * paths in the kernel.
  5. */
  6. #include "kselftest_harness.h"
  7. #include "thp_settings.h"
  8. #include <strings.h>
  9. #include <pthread.h>
  10. #include <numa.h>
  11. #include <numaif.h>
  12. #include <sys/mman.h>
  13. #include <sys/prctl.h>
  14. #include <sys/types.h>
  15. #include <signal.h>
  16. #include <time.h>
  17. #include "vm_util.h"
  18. #define TWOMEG (2<<20)
  19. #define RUNTIME (20)
  20. #define MAX_RETRIES 100
  21. #define ALIGN(x, a) (((x) + (a - 1)) & (~((a) - 1)))
  22. FIXTURE(migration)
  23. {
  24. pthread_t *threads;
  25. pid_t *pids;
  26. int nthreads;
  27. int n1;
  28. int n2;
  29. };
  30. FIXTURE_SETUP(migration)
  31. {
  32. int n;
  33. ASSERT_EQ(numa_available(), 0);
  34. self->nthreads = numa_num_task_cpus() - 1;
  35. self->n1 = -1;
  36. self->n2 = -1;
  37. for (n = 0; n < numa_max_possible_node(); n++)
  38. if (numa_bitmask_isbitset(numa_all_nodes_ptr, n)) {
  39. if (self->n1 == -1) {
  40. self->n1 = n;
  41. } else {
  42. self->n2 = n;
  43. break;
  44. }
  45. }
  46. self->threads = malloc(self->nthreads * sizeof(*self->threads));
  47. ASSERT_NE(self->threads, NULL);
  48. self->pids = malloc(self->nthreads * sizeof(*self->pids));
  49. ASSERT_NE(self->pids, NULL);
  50. };
  51. FIXTURE_TEARDOWN(migration)
  52. {
  53. free(self->threads);
  54. free(self->pids);
  55. }
  56. int migrate(uint64_t *ptr, int n1, int n2)
  57. {
  58. int ret, tmp;
  59. int status = 0;
  60. struct timespec ts1, ts2;
  61. int failures = 0;
  62. if (clock_gettime(CLOCK_MONOTONIC, &ts1))
  63. return -1;
  64. while (1) {
  65. if (clock_gettime(CLOCK_MONOTONIC, &ts2))
  66. return -1;
  67. if (ts2.tv_sec - ts1.tv_sec >= RUNTIME)
  68. return 0;
  69. ret = move_pages(0, 1, (void **) &ptr, &n2, &status,
  70. MPOL_MF_MOVE_ALL);
  71. if (ret) {
  72. if (ret > 0) {
  73. /* Migration is best effort; try again */
  74. if (++failures < MAX_RETRIES)
  75. continue;
  76. printf("Didn't migrate %d pages\n", ret);
  77. }
  78. else
  79. perror("Couldn't migrate pages");
  80. return -2;
  81. }
  82. failures = 0;
  83. tmp = n2;
  84. n2 = n1;
  85. n1 = tmp;
  86. }
  87. return 0;
  88. }
  89. void *access_mem(void *ptr)
  90. {
  91. while (1) {
  92. pthread_testcancel();
  93. /* Force a read from the memory pointed to by ptr. This ensures
  94. * the memory access actually happens and prevents the compiler
  95. * from optimizing away this entire loop.
  96. */
  97. FORCE_READ(*(uint64_t *)ptr);
  98. }
  99. return NULL;
  100. }
  101. /*
  102. * Basic migration entry testing. One thread will move pages back and forth
  103. * between nodes whilst other threads try and access them triggering the
  104. * migration entry wait paths in the kernel.
  105. */
  106. TEST_F_TIMEOUT(migration, private_anon, 2*RUNTIME)
  107. {
  108. uint64_t *ptr;
  109. int i;
  110. if (self->nthreads < 2 || self->n1 < 0 || self->n2 < 0)
  111. SKIP(return, "Not enough threads or NUMA nodes available");
  112. ptr = mmap(NULL, TWOMEG, PROT_READ | PROT_WRITE,
  113. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  114. ASSERT_NE(ptr, MAP_FAILED);
  115. memset(ptr, 0xde, TWOMEG);
  116. for (i = 0; i < self->nthreads - 1; i++)
  117. if (pthread_create(&self->threads[i], NULL, access_mem, ptr))
  118. perror("Couldn't create thread");
  119. ASSERT_EQ(migrate(ptr, self->n1, self->n2), 0);
  120. for (i = 0; i < self->nthreads - 1; i++)
  121. ASSERT_EQ(pthread_cancel(self->threads[i]), 0);
  122. }
  123. /*
  124. * Same as the previous test but with shared memory.
  125. */
  126. TEST_F_TIMEOUT(migration, shared_anon, 2*RUNTIME)
  127. {
  128. pid_t pid;
  129. uint64_t *ptr;
  130. int i;
  131. if (self->nthreads < 2 || self->n1 < 0 || self->n2 < 0)
  132. SKIP(return, "Not enough threads or NUMA nodes available");
  133. ptr = mmap(NULL, TWOMEG, PROT_READ | PROT_WRITE,
  134. MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  135. ASSERT_NE(ptr, MAP_FAILED);
  136. memset(ptr, 0xde, TWOMEG);
  137. for (i = 0; i < self->nthreads - 1; i++) {
  138. pid = fork();
  139. if (!pid) {
  140. prctl(PR_SET_PDEATHSIG, SIGHUP);
  141. /* Parent may have died before prctl so check now. */
  142. if (getppid() == 1)
  143. kill(getpid(), SIGHUP);
  144. access_mem(ptr);
  145. } else {
  146. self->pids[i] = pid;
  147. }
  148. }
  149. ASSERT_EQ(migrate(ptr, self->n1, self->n2), 0);
  150. for (i = 0; i < self->nthreads - 1; i++)
  151. ASSERT_EQ(kill(self->pids[i], SIGTERM), 0);
  152. }
  153. /*
  154. * Tests the pmd migration entry paths.
  155. */
  156. TEST_F_TIMEOUT(migration, private_anon_thp, 2*RUNTIME)
  157. {
  158. uint64_t *ptr;
  159. int i;
  160. if (!thp_is_enabled())
  161. SKIP(return, "Transparent Hugepages not available");
  162. if (self->nthreads < 2 || self->n1 < 0 || self->n2 < 0)
  163. SKIP(return, "Not enough threads or NUMA nodes available");
  164. ptr = mmap(NULL, 2*TWOMEG, PROT_READ | PROT_WRITE,
  165. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  166. ASSERT_NE(ptr, MAP_FAILED);
  167. ptr = (uint64_t *) ALIGN((uintptr_t) ptr, TWOMEG);
  168. ASSERT_EQ(madvise(ptr, TWOMEG, MADV_HUGEPAGE), 0);
  169. memset(ptr, 0xde, TWOMEG);
  170. for (i = 0; i < self->nthreads - 1; i++)
  171. if (pthread_create(&self->threads[i], NULL, access_mem, ptr))
  172. perror("Couldn't create thread");
  173. ASSERT_EQ(migrate(ptr, self->n1, self->n2), 0);
  174. for (i = 0; i < self->nthreads - 1; i++)
  175. ASSERT_EQ(pthread_cancel(self->threads[i]), 0);
  176. }
  177. /*
  178. * migration test with shared anon THP page
  179. */
  180. TEST_F_TIMEOUT(migration, shared_anon_thp, 2*RUNTIME)
  181. {
  182. pid_t pid;
  183. uint64_t *ptr;
  184. int i;
  185. if (!thp_is_enabled())
  186. SKIP(return, "Transparent Hugepages not available");
  187. if (self->nthreads < 2 || self->n1 < 0 || self->n2 < 0)
  188. SKIP(return, "Not enough threads or NUMA nodes available");
  189. ptr = mmap(NULL, 2 * TWOMEG, PROT_READ | PROT_WRITE,
  190. MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  191. ASSERT_NE(ptr, MAP_FAILED);
  192. ptr = (uint64_t *) ALIGN((uintptr_t) ptr, TWOMEG);
  193. ASSERT_EQ(madvise(ptr, TWOMEG, MADV_HUGEPAGE), 0);
  194. memset(ptr, 0xde, TWOMEG);
  195. for (i = 0; i < self->nthreads - 1; i++) {
  196. pid = fork();
  197. if (!pid) {
  198. prctl(PR_SET_PDEATHSIG, SIGHUP);
  199. /* Parent may have died before prctl so check now. */
  200. if (getppid() == 1)
  201. kill(getpid(), SIGHUP);
  202. access_mem(ptr);
  203. } else {
  204. self->pids[i] = pid;
  205. }
  206. }
  207. ASSERT_EQ(migrate(ptr, self->n1, self->n2), 0);
  208. for (i = 0; i < self->nthreads - 1; i++)
  209. ASSERT_EQ(kill(self->pids[i], SIGTERM), 0);
  210. }
  211. /*
  212. * migration test with private anon hugetlb page
  213. */
  214. TEST_F_TIMEOUT(migration, private_anon_htlb, 2*RUNTIME)
  215. {
  216. uint64_t *ptr;
  217. int i;
  218. if (self->nthreads < 2 || self->n1 < 0 || self->n2 < 0)
  219. SKIP(return, "Not enough threads or NUMA nodes available");
  220. ptr = mmap(NULL, TWOMEG, PROT_READ | PROT_WRITE,
  221. MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
  222. ASSERT_NE(ptr, MAP_FAILED);
  223. memset(ptr, 0xde, TWOMEG);
  224. for (i = 0; i < self->nthreads - 1; i++)
  225. if (pthread_create(&self->threads[i], NULL, access_mem, ptr))
  226. perror("Couldn't create thread");
  227. ASSERT_EQ(migrate(ptr, self->n1, self->n2), 0);
  228. for (i = 0; i < self->nthreads - 1; i++)
  229. ASSERT_EQ(pthread_cancel(self->threads[i]), 0);
  230. }
  231. /*
  232. * migration test with shared anon hugetlb page
  233. */
  234. TEST_F_TIMEOUT(migration, shared_anon_htlb, 2*RUNTIME)
  235. {
  236. pid_t pid;
  237. uint64_t *ptr;
  238. int i;
  239. if (self->nthreads < 2 || self->n1 < 0 || self->n2 < 0)
  240. SKIP(return, "Not enough threads or NUMA nodes available");
  241. ptr = mmap(NULL, TWOMEG, PROT_READ | PROT_WRITE,
  242. MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
  243. ASSERT_NE(ptr, MAP_FAILED);
  244. memset(ptr, 0xde, TWOMEG);
  245. for (i = 0; i < self->nthreads - 1; i++) {
  246. pid = fork();
  247. if (!pid) {
  248. prctl(PR_SET_PDEATHSIG, SIGHUP);
  249. /* Parent may have died before prctl so check now. */
  250. if (getppid() == 1)
  251. kill(getpid(), SIGHUP);
  252. access_mem(ptr);
  253. } else {
  254. self->pids[i] = pid;
  255. }
  256. }
  257. ASSERT_EQ(migrate(ptr, self->n1, self->n2), 0);
  258. for (i = 0; i < self->nthreads - 1; i++)
  259. ASSERT_EQ(kill(self->pids[i], SIGTERM), 0);
  260. }
  261. TEST_HARNESS_MAIN