hugetlb_vs_thp_test.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. munmap() length of
  20. * MAP_HUGETLB memory must be hugepage aligned.
  21. */
  22. if (munmap(addr, SIZE)) {
  23. perror("munmap");
  24. return 1;
  25. }
  26. }
  27. p = mmap(addr, SIZE, PROT_READ | PROT_WRITE,
  28. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  29. if (p == MAP_FAILED) {
  30. printf("Mapping failed @ %p\n", addr);
  31. perror("mmap");
  32. return 1;
  33. }
  34. /*
  35. * Either a user or kernel access is sufficient to trigger the bug.
  36. * A kernel access is easier to spot & debug, as it will trigger the
  37. * softlockup or RCU stall detectors, and when the system is kicked
  38. * into xmon we get a backtrace in the kernel.
  39. *
  40. * A good option is:
  41. * getcwd(p, SIZE);
  42. *
  43. * For the purposes of this testcase it's preferable to spin in
  44. * userspace, so the harness can kill us if we get stuck. That way we
  45. * see a test failure rather than a dead system.
  46. */
  47. *p = 0xf;
  48. munmap(addr, SIZE);
  49. return 0;
  50. }
  51. static int test_main(void)
  52. {
  53. int i;
  54. /* 10,000 because it's a "bunch", and completes reasonably quickly */
  55. for (i = 0; i < 10000; i++)
  56. if (test_body())
  57. return 1;
  58. return 0;
  59. }
  60. int main(void)
  61. {
  62. return test_harness(test_main, "hugetlb_vs_thp");
  63. }