1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2010-2017 Red Hat, Inc.
4 *
5 * hugetlbfs allows to overcommit hugepages and there are tunables in
6 * sysfs and procfs. The test here want to ensure it is possible to
7 * overcommit by either mmap or shared memory. Also ensure those
8 * reservation can be read/write, and several statistics work correctly.
9 *
10 * First, it resets nr_hugepages and nr_overcommit_hugepages. Then, set
11 * both to a specify value - N, and allocate N + %50 x N hugepages.
12 * Finally, it reads and writes every page. There are command options to
13 * choose either to manage hugepages from sysfs or procfs, and reserve
14 * them by mmap or shmget.
15 */
16
17 #include <string.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include "hugetlb.h"
21 #include "tst_safe_sysv_ipc.h"
22 #include "tst_test.h"
23
24 #define PROTECTION (PROT_READ | PROT_WRITE)
25 #define PATH_MEMINFO "/proc/meminfo"
26
27 static char path_sys_sz[BUFSIZ];
28 static char path_sys_sz_over[BUFSIZ];
29 static char path_sys_sz_free[BUFSIZ];
30 static char path_sys_sz_resv[BUFSIZ];
31 static char path_sys_sz_surp[BUFSIZ];
32 static char path_sys_sz_huge[BUFSIZ];
33
34 #define PATH_PROC_VM "/proc/sys/vm/"
35 #define PATH_PROC_OVER PATH_PROC_VM "nr_overcommit_hugepages"
36 #define PATH_PROC_HUGE PATH_PROC_VM "nr_hugepages"
37 #define PATH_SHMMAX "/proc/sys/kernel/shmmax"
38
39 /* Only ia64 requires this */
40 #ifdef __ia64__
41 #define ADDR (void *)(0x8000000000000000UL)
42 #define FLAGS (MAP_SHARED | MAP_FIXED)
43 #define SHMAT_FLAGS (SHM_RND)
44 #else
45 #define ADDR (void *)(0x0UL)
46 #define FLAGS (MAP_SHARED)
47 #define SHMAT_FLAGS (0)
48 #endif
49
50 #ifndef SHM_HUGETLB
51 #define SHM_HUGETLB 04000
52 #endif
53
54 #define NR_HPAGES 2
55 #define MOUNT_DIR "hugemmap05"
56 #define TEST_FILE MOUNT_DIR "/file"
57
58 static unsigned long long shmmax;
59 static char *path, *pathover;
60 static int key = -1, shmid = -1, fd = -1;
61 static int mounted, restore_shmmax, restore_overcomm_hgpgs;
62 static long hugepagesize, nr_overcommit_hugepages;
63 static long size = NR_HPAGES, length = (NR_HPAGES + NR_HPAGES/2) * 2;
64
65 char *opt_sysfs;
66 char *opt_alloc;
67 char *opt_shmid;
68
69 static void check_wr_bytes(void *addr);
70 static int checkproc(long act_val, char *string, long exp_val);
71 static int checksys(char *path, char *pattern, long exp_val);
72 static void init_sys_sz_paths(void);
73
test_overcommit(void)74 static void test_overcommit(void)
75 {
76 void *addr = NULL, *shmaddr = NULL;
77
78 if (opt_shmid) {
79 shmid = SAFE_SHMGET(key, (length / 2 * hugepagesize),
80 SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
81 } else {
82 fd = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR, 0755);
83 addr = SAFE_MMAP(ADDR, (length / 2 * hugepagesize),
84 PROTECTION, FLAGS, fd, 0);
85 }
86
87 if (opt_sysfs) {
88 tst_res(TINFO, "check sysfs before allocation.");
89 if (checksys(path_sys_sz_huge, "HugePages_Total", length / 2))
90 return;
91 if (checksys(path_sys_sz_free, "HugePages_Free", length / 2))
92 return;
93 if (checksys(path_sys_sz_surp, "HugePages_Surp",
94 length / 2 - size))
95 return;
96 if (checksys(path_sys_sz_resv, "HugePages_Rsvd", length / 2))
97 return;
98 } else {
99 tst_res(TINFO, "check /proc/meminfo before allocation.");
100 if (checkproc(SAFE_READ_MEMINFO("HugePages_Total:"),
101 "HugePages_Total", length / 2))
102 return;
103 if (checkproc(SAFE_READ_MEMINFO("HugePages_Free:"),
104 "HugePages_Free", length / 2))
105 return;
106 if (checkproc(SAFE_READ_MEMINFO("HugePages_Surp:"),
107 "HugePages_Surp", length / 2 - size))
108 return;
109 if (checkproc(SAFE_READ_MEMINFO("HugePages_Rsvd:"),
110 "HugePages_Rsvd", length / 2))
111 return;
112 }
113
114 if (opt_shmid) {
115 tst_res(TINFO, "shmid: 0x%x", shmid);
116 shmaddr = SAFE_SHMAT(shmid, ADDR, SHMAT_FLAGS);
117 check_wr_bytes(shmaddr);
118 } else {
119 check_wr_bytes(addr);
120 }
121
122 if (opt_sysfs) {
123 tst_res(TINFO, "check sysfs.");
124 if (checksys(path_sys_sz_huge, "HugePages_Total", length / 2))
125 return;
126 if (checksys(path_sys_sz_free, "HugePages_Free", 0))
127 return;
128 if (checksys(path_sys_sz_surp, "HugePages_Surp",
129 length / 2 - size))
130 return;
131 if (checksys(path_sys_sz_resv, "HugePages_Rsvd", 0))
132 return;
133 } else {
134 tst_res(TINFO, "check /proc/meminfo.");
135 if (checkproc(SAFE_READ_MEMINFO("HugePages_Total:"),
136 "HugePages_Total", length / 2))
137 return;
138 if (checkproc(SAFE_READ_MEMINFO("HugePages_Free:"),
139 "HugePages_Free", 0))
140 return;
141 if (checkproc(SAFE_READ_MEMINFO("HugePages_Surp:"),
142 "HugePages_Surp", length / 2 - size))
143 return;
144 if (checkproc(SAFE_READ_MEMINFO("HugePages_Rsvd:"),
145 "HugePages_Rsvd", 0))
146 return;
147 }
148
149 if (opt_shmid) {
150 SAFE_SHMDT(shmaddr);
151 SAFE_SHMCTL(shmid, IPC_RMID, NULL);
152 } else {
153 SAFE_MUNMAP(addr, (length / 2 * hugepagesize));
154 SAFE_CLOSE(fd);
155 SAFE_UNLINK(TEST_FILE);
156 }
157
158 tst_res(TPASS, "hugepages overcommit test pass");
159 }
160
cleanup(void)161 static void cleanup(void)
162 {
163 if (opt_shmid && shmid != -1)
164 SAFE_SHMCTL(shmid, IPC_RMID, NULL);
165
166 if (!opt_shmid && fd != -1) {
167 SAFE_CLOSE(fd);
168 SAFE_UNLINK(TEST_FILE);
169 }
170
171 if (mounted)
172 tst_umount(MOUNT_DIR);
173
174 if (restore_shmmax)
175 SAFE_FILE_PRINTF(PATH_SHMMAX, "%llu", shmmax);
176
177 if (restore_overcomm_hgpgs) {
178 tst_res(TINFO, "restore nr_overcommit_hugepages to %ld.",
179 nr_overcommit_hugepages);
180 SAFE_FILE_PRINTF(pathover, "%ld", nr_overcommit_hugepages);
181 }
182 }
183
setup(void)184 static void setup(void)
185 {
186 unsigned long hpages;
187
188 if (tst_hugepages != NR_HPAGES)
189 tst_brk(TCONF, "Not enough hugepages for testing!");
190
191 hugepagesize = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
192 init_sys_sz_paths();
193
194 if (opt_sysfs) {
195 path = path_sys_sz_huge;
196 pathover = path_sys_sz_over;
197 } else {
198 path = PATH_PROC_HUGE;
199 pathover = PATH_PROC_OVER;
200 }
201
202 if (opt_alloc) {
203 size = atoi(opt_alloc);
204 length = (size + size * 0.5) * 2;
205 }
206
207 if (opt_shmid) {
208 SAFE_FILE_SCANF(PATH_SHMMAX, "%llu", &shmmax);
209 if (shmmax < (unsigned long long)(length / 2 * hugepagesize)) {
210 restore_shmmax = 1;
211 SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld",
212 (length / 2 * hugepagesize));
213 }
214 }
215
216 /* Reset. */
217 SAFE_FILE_PRINTF(path, "%ld", size);
218 SAFE_FILE_SCANF(path, "%lu", &hpages);
219 if (hpages != size)
220 tst_brk(TCONF, "Not enough hugepages for testing!");
221
222 if (access(pathover, F_OK)) {
223 tst_brk(TCONF, "file %s does not exist in the system",
224 pathover);
225 }
226
227 SAFE_FILE_SCANF(pathover, "%ld", &nr_overcommit_hugepages);
228 tst_res(TINFO, "original nr_overcommit_hugepages is %ld",
229 nr_overcommit_hugepages);
230
231 /* Reset. */
232 SAFE_FILE_PRINTF(pathover, "%ld", size);
233 restore_overcomm_hgpgs = 1;
234
235 SAFE_MKDIR(MOUNT_DIR, 0700);
236 SAFE_MOUNT(NULL, MOUNT_DIR, "hugetlbfs", 0, NULL);
237 mounted = 1;
238
239 if (opt_shmid) {
240 /* Use /proc/meminfo to generate an IPC key. */
241 key = ftok(PATH_MEMINFO, strlen(PATH_MEMINFO));
242 if (key == -1)
243 tst_brk(TBROK | TERRNO, "ftok");
244 }
245 }
246
check_wr_bytes(void * addr)247 static void check_wr_bytes(void *addr)
248 {
249 long i;
250
251 memset((char *)addr, '\a', (length / 2 * hugepagesize));
252
253 tst_res(TINFO, "First hex is %x", *((unsigned int *)addr));
254 for (i = 0; i < (length / 2 * hugepagesize); i++) {
255 if (((char *)addr)[i] != '\a') {
256 tst_res(TFAIL, "mismatch at %ld", i);
257 break;
258 }
259 }
260 }
261
checksys(char * path,char * string,long exp_val)262 static int checksys(char *path, char *string, long exp_val)
263 {
264 long act_val;
265
266 SAFE_FILE_SCANF(path, "%ld", &act_val);
267 tst_res(TINFO, "%s is %ld.", string, act_val);
268 if (act_val != exp_val) {
269 tst_res(TFAIL, "%s is not %ld but %ld.", string, exp_val,
270 act_val);
271 return 1;
272 }
273 return 0;
274 }
275
checkproc(long act_val,char * pattern,long exp_val)276 static int checkproc(long act_val, char *pattern, long exp_val)
277 {
278 tst_res(TINFO, "%s is %ld.", pattern, act_val);
279 if (act_val != exp_val) {
280 tst_res(TFAIL, "%s is not %ld but %ld.",
281 pattern, exp_val, act_val);
282 return 1;
283 }
284 return 0;
285 }
286
init_sys_sz_paths(void)287 static void init_sys_sz_paths(void)
288 {
289 sprintf(path_sys_sz, "/sys/kernel/mm/hugepages/hugepages-%ldkB",
290 hugepagesize / 1024);
291 sprintf(path_sys_sz_over, "%s/nr_overcommit_hugepages", path_sys_sz);
292 sprintf(path_sys_sz_free, "%s/free_hugepages", path_sys_sz);
293 sprintf(path_sys_sz_resv, "%s/resv_hugepages", path_sys_sz);
294 sprintf(path_sys_sz_surp, "%s/surplus_hugepages", path_sys_sz);
295 sprintf(path_sys_sz_huge, "%s/nr_hugepages", path_sys_sz);
296 }
297
298 static struct tst_test test = {
299 .needs_root = 1,
300 .needs_tmpdir = 1,
301 .options = (struct tst_option[]) {
302 {"s", &opt_sysfs, "Setup hugepages from sysfs"},
303 {"m", &opt_shmid, "Reserve hugepages by shmget"},
304 {"a:", &opt_alloc, "Number of overcommint hugepages"},
305 {}
306 },
307 .setup = setup,
308 .cleanup = cleanup,
309 .test_all = test_overcommit,
310 .request_hugepages = NR_HPAGES,
311 };
312