hugetlb_vs_thp_test.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdio.h>
  2. #include <sys/mman.h>
  3. #include <unistd.h>
  4. #include "utils.h"
  5. /* This must match the huge page & THP size */
  6. #define SIZE (16 * 1024 * 1024)
  7. static int test_body(void)
  8. {
  9. void *addr;
  10. char *p;
  11. addr = (void *)0xa0000000;
  12. p = mmap(addr, SIZE, PROT_READ | PROT_WRITE,
  13. MAP_HUGETLB | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  14. if (p != MAP_FAILED) {
  15. /*
  16. * Typically the mmap will fail because no huge pages are
  17. * allocated on the system. But if there are huge pages
  18. * allocated the mmap will succeed. That's fine too, we just
  19. * munmap here before continuing.
  20. */
  21. munmap(addr, SIZE);
  22. }
  23. p = mmap(addr, SIZE, PROT_READ | PROT_WRITE,
  24. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  25. if (p == MAP_FAILED) {
  26. printf("Mapping failed @ %p\n", addr);
  27. perror("mmap");
  28. return 1;
  29. }
  30. /*
  31. * Either a user or kernel access is sufficient to trigger the bug.
  32. * A kernel access is easier to spot & debug, as it will trigger the
  33. * softlockup or RCU stall detectors, and when the system is kicked
  34. * into xmon we get a backtrace in the kernel.
  35. *
  36. * A good option is:
  37. * getcwd(p, SIZE);
  38. *
  39. * For the purposes of this testcase it's preferable to spin in
  40. * userspace, so the harness can kill us if we get stuck. That way we
  41. * see a test failure rather than a dead system.
  42. */
  43. *p = 0xf;
  44. munmap(addr, SIZE);
  45. return 0;
  46. }
  47. static int test_main(void)
  48. {
  49. int i;
  50. /* 10,000 because it's a "bunch", and completes reasonably quickly */
  51. for (i = 0; i < 10000; i++)
  52. if (test_body())
  53. return 1;
  54. return 0;
  55. }
  56. int main(void)
  57. {
  58. return test_harness(test_main, "hugetlb_vs_thp");
  59. }