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
) {
51 printf("Deleting the memory\n");
52 if (shmdt((const void *)shmaddr
) != 0) {
53 perror("Detach failure");
54 shmctl(shmid
, IPC_RMID
, NULL
);
58 shmctl(shmid
, IPC_RMID
, NULL
);
59 printf("Done deleting the memory\n");
65 int main(int argc
, char **argv
)
73 enum method method
= MAX_METHOD
;
74 int want_sleep
= 0, private = 0;
79 if (signal(SIGINT
, sig_handler
) == SIG_ERR
)
80 err(1, "\ncan't catch SIGINT\n");
82 /* Parse command-line arguments. */
83 setvbuf(stdout
, NULL
, _IONBF
, 0);
86 while ((c
= getopt(argc
, argv
, "s:p:m:owlrn")) != -1) {
92 strncpy(path
, optarg
, sizeof(path
) - 1);
95 if (atoi(optarg
) >= MAX_METHOD
) {
97 perror("Invalid -m.");
100 method
= atoi(optarg
);
120 perror("Invalid arg");
125 if (strncmp(path
, "", sizeof(path
)) != 0) {
126 printf("Writing to this path: %s\n", path
);
129 perror("path not found");
134 printf("Writing this size: %d\n", size
);
137 perror("size not found");
142 printf("Not populating.\n");
144 printf("Populating.\n");
147 printf("Not writing to memory.\n");
149 if (method
== MAX_METHOD
) {
151 perror("-m Invalid");
154 printf("Using method=%d\n", method
);
157 printf("Shared mapping.\n");
159 printf("Private mapping.\n");
162 printf("NO_RESERVE mapping.\n");
164 printf("RESERVE mapping.\n");
168 printf("Allocating using HUGETLBFS.\n");
169 fd
= open(path
, O_CREAT
| O_RDWR
, 0777);
171 err(1, "Failed to open file.");
173 ptr
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
174 (private ? MAP_PRIVATE
: MAP_SHARED
) |
175 (populate
? MAP_POPULATE
: 0) |
176 (reserve
? 0 : MAP_NORESERVE
),
179 if (ptr
== MAP_FAILED
) {
181 err(1, "Error mapping the file");
184 case MMAP_MAP_HUGETLB
:
185 printf("Allocating using MAP_HUGETLB.\n");
186 ptr
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
187 (private ? (MAP_PRIVATE
| MAP_ANONYMOUS
) :
189 MAP_HUGETLB
| (populate
? MAP_POPULATE
: 0) |
190 (reserve
? 0 : MAP_NORESERVE
),
193 if (ptr
== MAP_FAILED
)
196 printf("Returned address is %p\n", ptr
);
199 printf("Allocating using SHM.\n");
200 shmid
= shmget(key
, size
,
201 SHM_HUGETLB
| IPC_CREAT
| SHM_R
| SHM_W
);
203 shmid
= shmget(++key
, size
,
204 SHM_HUGETLB
| IPC_CREAT
| SHM_R
| SHM_W
);
208 printf("shmid: 0x%x, shmget key:%d\n", shmid
, key
);
210 ptr
= shmat(shmid
, NULL
, 0);
211 if (ptr
== (int *)-1) {
212 perror("Shared memory attach failure");
213 shmctl(shmid
, IPC_RMID
, NULL
);
217 printf("shmaddr: %p\n", shmaddr
);
222 err(1, "Invalid method.");
226 printf("Writing to memory.\n");
227 memset(ptr
, 1, size
);
231 /* Signal to caller that we're done. */
234 /* Hold memory until external kill signal is delivered. */
239 if (method
== HUGETLBFS
)