virtual_address_range.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2017, Anshuman Khandual, IBM Corp.
  4. *
  5. * Works on architectures which support 128TB virtual
  6. * address range and beyond.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <sys/mman.h>
  14. #include <sys/time.h>
  15. #include <fcntl.h>
  16. #include "../kselftest.h"
  17. /*
  18. * Maximum address range mapped with a single mmap()
  19. * call is little bit more than 1GB. Hence 1GB is
  20. * chosen as the single chunk size for address space
  21. * mapping.
  22. */
  23. #define SZ_1GB (1024 * 1024 * 1024UL)
  24. #define SZ_1TB (1024 * 1024 * 1024 * 1024UL)
  25. #define MAP_CHUNK_SIZE SZ_1GB
  26. /*
  27. * Address space till 128TB is mapped without any hint
  28. * and is enabled by default. Address space beyond 128TB
  29. * till 512TB is obtained by passing hint address as the
  30. * first argument into mmap() system call.
  31. *
  32. * The process heap address space is divided into two
  33. * different areas one below 128TB and one above 128TB
  34. * till it reaches 512TB. One with size 128TB and the
  35. * other being 384TB.
  36. *
  37. * On Arm64 the address space is 256TB and support for
  38. * high mappings up to 4PB virtual address space has
  39. * been added.
  40. */
  41. #define NR_CHUNKS_128TB ((128 * SZ_1TB) / MAP_CHUNK_SIZE) /* Number of chunks for 128TB */
  42. #define NR_CHUNKS_256TB (NR_CHUNKS_128TB * 2UL)
  43. #define NR_CHUNKS_384TB (NR_CHUNKS_128TB * 3UL)
  44. #define NR_CHUNKS_3840TB (NR_CHUNKS_128TB * 30UL)
  45. #define ADDR_MARK_128TB (1UL << 47) /* First address beyond 128TB */
  46. #define ADDR_MARK_256TB (1UL << 48) /* First address beyond 256TB */
  47. #ifdef __aarch64__
  48. #define HIGH_ADDR_MARK ADDR_MARK_256TB
  49. #define HIGH_ADDR_SHIFT 49
  50. #define NR_CHUNKS_LOW NR_CHUNKS_256TB
  51. #define NR_CHUNKS_HIGH NR_CHUNKS_3840TB
  52. #else
  53. #define HIGH_ADDR_MARK ADDR_MARK_128TB
  54. #define HIGH_ADDR_SHIFT 48
  55. #define NR_CHUNKS_LOW NR_CHUNKS_128TB
  56. #define NR_CHUNKS_HIGH NR_CHUNKS_384TB
  57. #endif
  58. static char *hind_addr(void)
  59. {
  60. int bits = HIGH_ADDR_SHIFT + rand() % (63 - HIGH_ADDR_SHIFT);
  61. return (char *) (1UL << bits);
  62. }
  63. static void validate_addr(char *ptr, int high_addr)
  64. {
  65. unsigned long addr = (unsigned long) ptr;
  66. if (high_addr && addr < HIGH_ADDR_MARK)
  67. ksft_exit_fail_msg("Bad address %lx\n", addr);
  68. if (addr > HIGH_ADDR_MARK)
  69. ksft_exit_fail_msg("Bad address %lx\n", addr);
  70. }
  71. static int validate_lower_address_hint(void)
  72. {
  73. char *ptr;
  74. ptr = mmap((void *) (1UL << 45), MAP_CHUNK_SIZE, PROT_READ |
  75. PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  76. if (ptr == MAP_FAILED)
  77. return 0;
  78. return 1;
  79. }
  80. static int validate_complete_va_space(void)
  81. {
  82. unsigned long start_addr, end_addr, prev_end_addr;
  83. char line[400];
  84. char prot[6];
  85. FILE *file;
  86. int fd;
  87. fd = open("va_dump", O_CREAT | O_WRONLY, 0600);
  88. unlink("va_dump");
  89. if (fd < 0) {
  90. ksft_test_result_skip("cannot create or open dump file\n");
  91. ksft_finished();
  92. }
  93. file = fopen("/proc/self/maps", "r");
  94. if (file == NULL)
  95. ksft_exit_fail_msg("cannot open /proc/self/maps\n");
  96. prev_end_addr = 0;
  97. while (fgets(line, sizeof(line), file)) {
  98. unsigned long hop;
  99. if (sscanf(line, "%lx-%lx %s[rwxp-]",
  100. &start_addr, &end_addr, prot) != 3)
  101. ksft_exit_fail_msg("cannot parse /proc/self/maps\n");
  102. /* end of userspace mappings; ignore vsyscall mapping */
  103. if (start_addr & (1UL << 63))
  104. return 0;
  105. /* /proc/self/maps must have gaps less than MAP_CHUNK_SIZE */
  106. if (start_addr - prev_end_addr >= MAP_CHUNK_SIZE)
  107. return 1;
  108. prev_end_addr = end_addr;
  109. if (prot[0] != 'r')
  110. continue;
  111. /*
  112. * Confirm whether MAP_CHUNK_SIZE chunk can be found or not.
  113. * If write succeeds, no need to check MAP_CHUNK_SIZE - 1
  114. * addresses after that. If the address was not held by this
  115. * process, write would fail with errno set to EFAULT.
  116. * Anyways, if write returns anything apart from 1, exit the
  117. * program since that would mean a bug in /proc/self/maps.
  118. */
  119. hop = 0;
  120. while (start_addr + hop < end_addr) {
  121. if (write(fd, (void *)(start_addr + hop), 1) != 1)
  122. return 1;
  123. lseek(fd, 0, SEEK_SET);
  124. hop += MAP_CHUNK_SIZE;
  125. }
  126. }
  127. return 0;
  128. }
  129. int main(int argc, char *argv[])
  130. {
  131. char *ptr[NR_CHUNKS_LOW];
  132. char **hptr;
  133. char *hint;
  134. unsigned long i, lchunks, hchunks;
  135. ksft_print_header();
  136. ksft_set_plan(1);
  137. for (i = 0; i < NR_CHUNKS_LOW; i++) {
  138. ptr[i] = mmap(NULL, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
  139. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  140. if (ptr[i] == MAP_FAILED) {
  141. if (validate_lower_address_hint())
  142. ksft_exit_fail_msg("mmap unexpectedly succeeded with hint\n");
  143. break;
  144. }
  145. validate_addr(ptr[i], 0);
  146. }
  147. lchunks = i;
  148. hptr = (char **) calloc(NR_CHUNKS_HIGH, sizeof(char *));
  149. if (hptr == NULL) {
  150. ksft_test_result_skip("Memory constraint not fulfilled\n");
  151. ksft_finished();
  152. }
  153. for (i = 0; i < NR_CHUNKS_HIGH; i++) {
  154. hint = hind_addr();
  155. hptr[i] = mmap(hint, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
  156. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  157. if (hptr[i] == MAP_FAILED)
  158. break;
  159. validate_addr(hptr[i], 1);
  160. }
  161. hchunks = i;
  162. if (validate_complete_va_space()) {
  163. ksft_test_result_fail("BUG in mmap() or /proc/self/maps\n");
  164. ksft_finished();
  165. }
  166. for (i = 0; i < lchunks; i++)
  167. munmap(ptr[i], MAP_CHUNK_SIZE);
  168. for (i = 0; i < hchunks; i++)
  169. munmap(hptr[i], MAP_CHUNK_SIZE);
  170. free(hptr);
  171. ksft_test_result_pass("Test\n");
  172. ksft_finished();
  173. }