1 // SPDX-License-Identifier: GPL-2.0
3 * This program reserves and uses hugetlb memory, supporting a bunch of
4 * scenarios needed by the charged_reserved_hugetlb.sh test.
15 #include <sys/types.h>
20 /* Global definitions. */
29 /* Global variables. */
30 static const char *self;
35 * Show usage and exit.
37 static void exit_usage(void)
39 printf("Usage: %s -p <path to hugetlbfs file> -s <size to map> "
40 "[-m <0=hugetlbfs | 1=mmap(MAP_HUGETLB)>] [-l] [-r] "
46 void sig_handler(int signo)
48 printf("Received %d.\n", signo);
49 if (signo == SIGINT) {
50 printf("Deleting the memory\n");
51 if (shmdt((const void *)shmaddr) != 0) {
52 perror("Detach failure");
53 shmctl(shmid, IPC_RMID, NULL);
57 shmctl(shmid, IPC_RMID, NULL);
58 printf("Done deleting the memory\n");
63 int main(int argc, char **argv)
71 enum method method = MAX_METHOD;
72 int want_sleep = 0, private = 0;
77 if (signal(SIGINT, sig_handler) == SIG_ERR)
78 err(1, "\ncan't catch SIGINT\n");
80 /* Parse command-line arguments. */
81 setvbuf(stdout, NULL, _IONBF, 0);
84 while ((c = getopt(argc, argv, "s:p:m:owlrn")) != -1) {
90 strncpy(path, optarg, sizeof(path));
93 if (atoi(optarg) >= MAX_METHOD) {
95 perror("Invalid -m.");
98 method = atoi(optarg);
118 perror("Invalid arg");
123 if (strncmp(path, "", sizeof(path)) != 0) {
124 printf("Writing to this path: %s\n", path);
127 perror("path not found");
132 printf("Writing this size: %d\n", size);
135 perror("size not found");
140 printf("Not populating.\n");
142 printf("Populating.\n");
145 printf("Not writing to memory.\n");
147 if (method == MAX_METHOD) {
149 perror("-m Invalid");
152 printf("Using method=%d\n", method);
155 printf("Shared mapping.\n");
157 printf("Private mapping.\n");
160 printf("NO_RESERVE mapping.\n");
162 printf("RESERVE mapping.\n");
166 printf("Allocating using HUGETLBFS.\n");
167 fd = open(path, O_CREAT | O_RDWR, 0777);
169 err(1, "Failed to open file.");
171 ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
172 (private ? MAP_PRIVATE : MAP_SHARED) |
173 (populate ? MAP_POPULATE : 0) |
174 (reserve ? 0 : MAP_NORESERVE),
177 if (ptr == MAP_FAILED) {
179 err(1, "Error mapping the file");
182 case MMAP_MAP_HUGETLB:
183 printf("Allocating using MAP_HUGETLB.\n");
184 ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
185 (private ? (MAP_PRIVATE | MAP_ANONYMOUS) :
187 MAP_HUGETLB | (populate ? MAP_POPULATE : 0) |
188 (reserve ? 0 : MAP_NORESERVE),
191 if (ptr == MAP_FAILED)
194 printf("Returned address is %p\n", ptr);
197 printf("Allocating using SHM.\n");
198 shmid = shmget(key, size,
199 SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
201 shmid = shmget(++key, size,
202 SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
206 printf("shmid: 0x%x, shmget key:%d\n", shmid, key);
208 ptr = shmat(shmid, NULL, 0);
209 if (ptr == (int *)-1) {
210 perror("Shared memory attach failure");
211 shmctl(shmid, IPC_RMID, NULL);
214 printf("shmaddr: %p\n", ptr);
219 err(1, "Invalid method.");
223 printf("Writing to memory.\n");
224 memset(ptr, 1, size);
228 /* Signal to caller that we're done. */
231 /* Hold memory until external kill signal is delivered. */
236 if (method == HUGETLBFS)