write_to_hugetlbfs.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This program reserves and uses hugetlb memory, supporting a bunch of
  4. * scenarios needed by the charged_reserved_hugetlb.sh test.
  5. */
  6. #include <err.h>
  7. #include <errno.h>
  8. #include <signal.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <sys/types.h>
  15. #include <sys/shm.h>
  16. #include <sys/stat.h>
  17. #include <sys/mman.h>
  18. /* Global definitions. */
  19. enum method {
  20. HUGETLBFS,
  21. MMAP_MAP_HUGETLB,
  22. SHM,
  23. MAX_METHOD
  24. };
  25. /* Global variables. */
  26. static const char *self;
  27. static int *shmaddr;
  28. static int shmid;
  29. /*
  30. * Show usage and exit.
  31. */
  32. static void exit_usage(void)
  33. {
  34. printf("Usage: %s -p <path to hugetlbfs file> -s <size to map> "
  35. "[-m <0=hugetlbfs | 1=mmap(MAP_HUGETLB)>] [-l] [-r] "
  36. "[-o] [-w] [-n]\n",
  37. self);
  38. exit(EXIT_FAILURE);
  39. }
  40. void sig_handler(int signo)
  41. {
  42. printf("Received %d.\n", signo);
  43. if (signo == SIGINT) {
  44. if (shmaddr) {
  45. printf("Deleting the memory\n");
  46. if (shmdt((const void *)shmaddr) != 0) {
  47. perror("Detach failure");
  48. shmctl(shmid, IPC_RMID, NULL);
  49. exit(4);
  50. }
  51. shmctl(shmid, IPC_RMID, NULL);
  52. printf("Done deleting the memory\n");
  53. }
  54. }
  55. exit(2);
  56. }
  57. int main(int argc, char **argv)
  58. {
  59. int fd = 0;
  60. int key = 0;
  61. int *ptr = NULL;
  62. int c = 0;
  63. int size = 0;
  64. char path[256] = "";
  65. enum method method = MAX_METHOD;
  66. int want_sleep = 0, private = 0;
  67. int populate = 0;
  68. int write = 0;
  69. int reserve = 1;
  70. if (signal(SIGINT, sig_handler) == SIG_ERR)
  71. err(1, "\ncan't catch SIGINT\n");
  72. /* Parse command-line arguments. */
  73. setvbuf(stdout, NULL, _IONBF, 0);
  74. self = argv[0];
  75. while ((c = getopt(argc, argv, "s:p:m:owlrn")) != -1) {
  76. switch (c) {
  77. case 's':
  78. size = atoi(optarg);
  79. break;
  80. case 'p':
  81. strncpy(path, optarg, sizeof(path));
  82. break;
  83. case 'm':
  84. if (atoi(optarg) >= MAX_METHOD) {
  85. errno = EINVAL;
  86. perror("Invalid -m.");
  87. exit_usage();
  88. }
  89. method = atoi(optarg);
  90. break;
  91. case 'o':
  92. populate = 1;
  93. break;
  94. case 'w':
  95. write = 1;
  96. break;
  97. case 'l':
  98. want_sleep = 1;
  99. break;
  100. case 'r':
  101. private
  102. = 1;
  103. break;
  104. case 'n':
  105. reserve = 0;
  106. break;
  107. default:
  108. errno = EINVAL;
  109. perror("Invalid arg");
  110. exit_usage();
  111. }
  112. }
  113. if (strncmp(path, "", sizeof(path)) != 0) {
  114. printf("Writing to this path: %s\n", path);
  115. } else {
  116. errno = EINVAL;
  117. perror("path not found");
  118. exit_usage();
  119. }
  120. if (size != 0) {
  121. printf("Writing this size: %d\n", size);
  122. } else {
  123. errno = EINVAL;
  124. perror("size not found");
  125. exit_usage();
  126. }
  127. if (!populate)
  128. printf("Not populating.\n");
  129. else
  130. printf("Populating.\n");
  131. if (!write)
  132. printf("Not writing to memory.\n");
  133. if (method == MAX_METHOD) {
  134. errno = EINVAL;
  135. perror("-m Invalid");
  136. exit_usage();
  137. } else
  138. printf("Using method=%d\n", method);
  139. if (!private)
  140. printf("Shared mapping.\n");
  141. else
  142. printf("Private mapping.\n");
  143. if (!reserve)
  144. printf("NO_RESERVE mapping.\n");
  145. else
  146. printf("RESERVE mapping.\n");
  147. switch (method) {
  148. case HUGETLBFS:
  149. printf("Allocating using HUGETLBFS.\n");
  150. fd = open(path, O_CREAT | O_RDWR, 0777);
  151. if (fd == -1)
  152. err(1, "Failed to open file.");
  153. ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
  154. (private ? MAP_PRIVATE : MAP_SHARED) |
  155. (populate ? MAP_POPULATE : 0) |
  156. (reserve ? 0 : MAP_NORESERVE),
  157. fd, 0);
  158. if (ptr == MAP_FAILED) {
  159. close(fd);
  160. err(1, "Error mapping the file");
  161. }
  162. break;
  163. case MMAP_MAP_HUGETLB:
  164. printf("Allocating using MAP_HUGETLB.\n");
  165. ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
  166. (private ? (MAP_PRIVATE | MAP_ANONYMOUS) :
  167. MAP_SHARED) |
  168. MAP_HUGETLB | (populate ? MAP_POPULATE : 0) |
  169. (reserve ? 0 : MAP_NORESERVE),
  170. -1, 0);
  171. if (ptr == MAP_FAILED)
  172. err(1, "mmap");
  173. printf("Returned address is %p\n", ptr);
  174. break;
  175. case SHM:
  176. printf("Allocating using SHM.\n");
  177. shmid = shmget(key, size,
  178. SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
  179. if (shmid < 0) {
  180. shmid = shmget(++key, size,
  181. SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
  182. if (shmid < 0)
  183. err(1, "shmget");
  184. }
  185. printf("shmid: 0x%x, shmget key:%d\n", shmid, key);
  186. ptr = shmat(shmid, NULL, 0);
  187. if (ptr == (int *)-1) {
  188. perror("Shared memory attach failure");
  189. shmctl(shmid, IPC_RMID, NULL);
  190. exit(2);
  191. }
  192. shmaddr = ptr;
  193. printf("shmaddr: %p\n", shmaddr);
  194. break;
  195. default:
  196. errno = EINVAL;
  197. err(1, "Invalid method.");
  198. }
  199. if (write) {
  200. printf("Writing to memory.\n");
  201. memset(ptr, 1, size);
  202. }
  203. if (want_sleep) {
  204. /* Signal to caller that we're done. */
  205. printf("DONE\n");
  206. /* Hold memory until external kill signal is delivered. */
  207. while (1)
  208. sleep(100);
  209. }
  210. if (method == HUGETLBFS)
  211. close(fd);
  212. return 0;
  213. }