migration.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <strings.h>
  8. #include <pthread.h>
  9. #include <numa.h>
  10. #include <numaif.h>
  11. #include <sys/mman.h>
  12. #include <sys/prctl.h>
  13. #include <sys/types.h>
  14. #include <signal.h>
  15. #include <time.h>
  16. #define TWOMEG (2<<20)
  17. #define RUNTIME (20)
  18. #define MAX_RETRIES 100
  19. #define ALIGN(x, a) (((x) + (a - 1)) & (~((a) - 1)))
  20. FIXTURE(migration)
  21. {
  22. pthread_t *threads;
  23. pid_t *pids;
  24. int nthreads;
  25. int n1;
  26. int n2;
  27. };
  28. FIXTURE_SETUP(migration)
  29. {
  30. int n;
  31. ASSERT_EQ(numa_available(), 0);
  32. self->nthreads = numa_num_task_cpus() - 1;
  33. self->n1 = -1;
  34. self->n2 = -1;
  35. for (n = 0; n < numa_max_possible_node(); n++)
  36. if (numa_bitmask_isbitset(numa_all_nodes_ptr, n)) {
  37. if (self->n1 == -1) {
  38. self->n1 = n;
  39. } else {
  40. self->n2 = n;
  41. break;
  42. }
  43. }
  44. self->threads = malloc(self->nthreads * sizeof(*self->threads));
  45. ASSERT_NE(self->threads, NULL);
  46. self->pids = malloc(self->nthreads * sizeof(*self->pids));
  47. ASSERT_NE(self->pids, NULL);
  48. };
  49. FIXTURE_TEARDOWN(migration)
  50. {
  51. free(self->threads);
  52. free(self->pids);
  53. }
  54. int migrate(uint64_t *ptr, int n1, int n2)
  55. {
  56. int ret, tmp;
  57. int status = 0;
  58. struct timespec ts1, ts2;
  59. int failures = 0;
  60. if (clock_gettime(CLOCK_MONOTONIC, &ts1))
  61. return -1;
  62. while (1) {
  63. if (clock_gettime(CLOCK_MONOTONIC, &ts2))
  64. return -1;
  65. if (ts2.tv_sec - ts1.tv_sec >= RUNTIME)
  66. return 0;
  67. ret = move_pages(0, 1, (void **) &ptr, &n2, &status,
  68. MPOL_MF_MOVE_ALL);
  69. if (ret) {
  70. if (ret > 0) {
  71. /* Migration is best effort; try again */
  72. if (++failures < MAX_RETRIES)
  73. continue;
  74. printf("Didn't migrate %d pages\n", ret);
  75. }
  76. else
  77. perror("Couldn't migrate pages");
  78. return -2;
  79. }
  80. failures = 0;
  81. tmp = n2;
  82. n2 = n1;
  83. n1 = tmp;
  84. }
  85. return 0;
  86. }
  87. void *access_mem(void *ptr)
  88. {
  89. volatile uint64_t y = 0;
  90. volatile uint64_t *x = ptr;
  91. while (1) {
  92. pthread_testcancel();
  93. y += *x;
  94. /* Prevent the compiler from optimizing out the writes to y: */
  95. asm volatile("" : "+r" (y));
  96. }
  97. return NULL;
  98. }
  99. /*
  100. * Basic migration entry testing. One thread will move pages back and forth
  101. * between nodes whilst other threads try and access them triggering the
  102. * migration entry wait paths in the kernel.
  103. */
  104. TEST_F_TIMEOUT(migration, private_anon, 2*RUNTIME)
  105. {
  106. uint64_t *ptr;
  107. int i;
  108. if (self->nthreads < 2 || self->n1 < 0 || self->n2 < 0)
  109. SKIP(return, "Not enough threads or NUMA nodes available");
  110. ptr = mmap(NULL, TWOMEG, PROT_READ | PROT_WRITE,
  111. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  112. ASSERT_NE(ptr, MAP_FAILED);
  113. memset(ptr, 0xde, TWOMEG);
  114. for (i = 0; i < self->nthreads - 1; i++)
  115. if (pthread_create(&self->threads[i], NULL, access_mem, ptr))
  116. perror("Couldn't create thread");
  117. ASSERT_EQ(migrate(ptr, self->n1, self->n2), 0);
  118. for (i = 0; i < self->nthreads - 1; i++)
  119. ASSERT_EQ(pthread_cancel(self->threads[i]), 0);
  120. }
  121. /*
  122. * Same as the previous test but with shared memory.
  123. */
  124. TEST_F_TIMEOUT(migration, shared_anon, 2*RUNTIME)
  125. {
  126. pid_t pid;
  127. uint64_t *ptr;
  128. int i;
  129. if (self->nthreads < 2 || self->n1 < 0 || self->n2 < 0)
  130. SKIP(return, "Not enough threads or NUMA nodes available");
  131. ptr = mmap(NULL, TWOMEG, PROT_READ | PROT_WRITE,
  132. MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  133. ASSERT_NE(ptr, MAP_FAILED);
  134. memset(ptr, 0xde, TWOMEG);
  135. for (i = 0; i < self->nthreads - 1; i++) {
  136. pid = fork();
  137. if (!pid) {
  138. prctl(PR_SET_PDEATHSIG, SIGHUP);
  139. /* Parent may have died before prctl so check now. */
  140. if (getppid() == 1)
  141. kill(getpid(), SIGHUP);
  142. access_mem(ptr);
  143. } else {
  144. self->pids[i] = pid;
  145. }
  146. }
  147. ASSERT_EQ(migrate(ptr, self->n1, self->n2), 0);
  148. for (i = 0; i < self->nthreads - 1; i++)
  149. ASSERT_EQ(kill(self->pids[i], SIGTERM), 0);
  150. }
  151. /*
  152. * Tests the pmd migration entry paths.
  153. */
  154. TEST_F_TIMEOUT(migration, private_anon_thp, 2*RUNTIME)
  155. {
  156. uint64_t *ptr;
  157. int i;
  158. if (self->nthreads < 2 || self->n1 < 0 || self->n2 < 0)
  159. SKIP(return, "Not enough threads or NUMA nodes available");
  160. ptr = mmap(NULL, 2*TWOMEG, PROT_READ | PROT_WRITE,
  161. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  162. ASSERT_NE(ptr, MAP_FAILED);
  163. ptr = (uint64_t *) ALIGN((uintptr_t) ptr, TWOMEG);
  164. ASSERT_EQ(madvise(ptr, TWOMEG, MADV_HUGEPAGE), 0);
  165. memset(ptr, 0xde, TWOMEG);
  166. for (i = 0; i < self->nthreads - 1; i++)
  167. if (pthread_create(&self->threads[i], NULL, access_mem, ptr))
  168. perror("Couldn't create thread");
  169. ASSERT_EQ(migrate(ptr, self->n1, self->n2), 0);
  170. for (i = 0; i < self->nthreads - 1; i++)
  171. ASSERT_EQ(pthread_cancel(self->threads[i]), 0);
  172. }
  173. TEST_HARNESS_MAIN