Debian Codegraph @

tests/shmoverride_unlinked.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #include <sys/types.h>
19 #include <sys/shm.h>
20 #include <sys/types.h>
21 #include <sys/mman.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <hugetlbfs.h>
31 #include "hugetests.h"
32
33 /*
34 * Test Scenario:
35 *
36 * libhugetlbfs_shmoverride can be used to force shmget() to use the
37 * SHM_HUGETLB flag. This test ensures that the flag is correctly used
38 * based on the value of the environment variable. The assumption is
39 * made that the library is being preloaded.
40 */
41
42 extern int errno;
43
44 /* Global test configuration */
45 #define DYNAMIC_SYSCTL "/proc/sys/vm/nr_overcommit_hugepages"
46 static long saved_nr_hugepages = -1;
47 static long hpage_size, bpage_size;
48 static long oc_pool = -1;
49
50 /* Required pool size for test */
51 #define POOL_SIZE 4
52
53 /* State arrays for our mmaps */
54 #define NR_SLOTS 1
55 #define SL_TEST 0
56 static int map_id[NR_SLOTS];
57 static char *map_addr[NR_SLOTS];
58 static size_t map_size[NR_SLOTS];
59
60 /* Only ia64 requires this */
61 #ifdef __ia64__
62 #define ADDR (void *)(0x8000000000000000UL)
63 #define SHMAT_FLAGS (SHM_RND)
64 #else
65 #define ADDR (void *)(0x0UL)
66 #define SHMAT_FLAGS (0)
67 #endif
68
69 void _shmmap(int s, int hpages, int bpages, int line)
70 {
71 map_size[s] = hpages * hpage_size + bpages * bpage_size;
72 map_id[s] = shmget(IPC_PRIVATE, map_size[s], IPC_CREAT | SHM_R | SHM_W);
73 if (map_id[s] < 0)
74 FAIL("shmget failed size %zd from line %d: %s",
75 map_size[s], line, strerror(errno));
76
77 map_addr[s] = shmat(map_id[s], ADDR, SHMAT_FLAGS);
78 if (map_addr[s] == (char *)-1)
79 FAIL("shmmat failed from line %d: %s", line, strerror(errno));
80 }
81 #define shmmap(s, h, b) _shmmap(s, h, b, __LINE__)
82
83 void _shmunmap(int s, int line)
84 {
85 if (shmdt((const void *)map_addr[s]) != 0) {
86 FAIL("shmdt failed from line %d: %s", line, strerror(errno));
87 return;
88 }
89
90 if (shmctl(map_id[s], IPC_RMID, NULL) == -1)
91 FAIL("shmctl failed from line %d: %s", line, strerror(errno));
92
93 map_id[s] = -1;
94 map_addr[s] = NULL;
95 map_size[s] = 0;
96 }
97 #define shmunmap(s) _shmunmap(s, __LINE__)
98
99 /*
100 * This test wants to manipulate the hugetlb pool without necessarily linking
101 * to libhugetlbfs so the helpers for doing this may not be available -- hence
102 * the duplicated versions below.
103 *
104 * NOTE: We use /proc/sys/vm/nr_hugepages and /proc/meminfo for writing and
105 * reading pool counters because shared memory will always use the system
106 * default huge page size regardless of any libhugetlbfs settings.
107 */
108 #define MEMINFO_SIZE 2048
109 long local_read_meminfo(const char *tag)
110 {
111 int fd;
112 char buf[MEMINFO_SIZE];
113 int len, readerr;
114 char *p, *q;
115 long val;
116
117 fd = open("/proc/meminfo", O_RDONLY);
118 if (fd < 0)
119 FAIL("Couldn't open /proc/meminfo: %s\n", strerror(errno));
120
121 len = read(fd, buf, sizeof(buf));
122 readerr = errno;
123 close(fd);
124 if (len < 0)
125 FAIL("Error reading /proc/meminfo: %s\n", strerror(readerr));
126
127 if (len == sizeof(buf))
128 FAIL("/proc/meminfo is too large\n");
129 buf[len] = '\0';
130
131 p = strstr(buf, tag);
132 if (!p)
133 FAIL("Tag %s not found in /proc/meminfo\n", tag);
134 p += strlen(tag);
135
136 val = strtol(p, &q, 0);
137 if (!isspace(*q))
138 FAIL("Couldn't parse /proc/meminfo\n");
139
140 return val;
141 }
142
143 void setup_hugetlb_pool(unsigned long count)
144 {
145 FILE *fd;
146 unsigned long poolsize;
147 count += local_read_meminfo("HugePages_Rsvd:");
148 fd = fopen("/proc/sys/vm/nr_hugepages", "w");
149 if (!fd)
150 CONFIG("Cannot open nr_hugepages for writing\n");
151 fprintf(fd, "%lu", count);
152 fclose(fd);
153
154 /* Confirm the resize worked */
155 poolsize = local_read_meminfo("HugePages_Total:");
156 if (poolsize != count)
157 FAIL("Failed to resize pool to %lu pages. Got %lu instead\n",
158 count, poolsize);
159 }
160
161 void local_check_free_huge_pages(int needed_pages)
162 {
163 int free = local_read_meminfo("HugePages_Free:");
164 if (free < needed_pages)
165 CONFIG("Must have at least %i free hugepages", needed_pages);
166 }
167
168 void run_test(char *desc, int hpages, int bpages, int pool_nr, int expect_diff)
169 {
170 long resv_before, resv_after;
171 verbose_printf("%s...\n", desc);
172 setup_hugetlb_pool(pool_nr);
173
174 /* untouched, shared mmap */
175 resv_before = local_read_meminfo("HugePages_Rsvd:");
176 shmmap(SL_TEST, hpages, bpages);
177 resv_after = local_read_meminfo("HugePages_Rsvd:");
178 memset(map_addr[SL_TEST], 0, map_size[SL_TEST]);
179 shmunmap(SL_TEST);
180
181 if (resv_after - resv_before != expect_diff)
182 FAIL("%s: Reserve page count did not adjust by %d page. "
183 "Expected %li reserved pages but got %li pages",
184 desc, expect_diff,
185 resv_before + expect_diff, resv_after);
186 }
187
188 void cleanup(void)
189 {
190 int i;
191
192 /* Clean up any allocated shmids */
193 for (i = 0; i < NR_SLOTS; i++)
194 if (map_id[i] > 0)
195 shmctl(map_id[i], IPC_RMID, NULL);
196
197 /* Restore the pool size. */
198 if (saved_nr_hugepages >= 0)
199 setup_hugetlb_pool(saved_nr_hugepages);
200
201 if (oc_pool > 0)
202 restore_overcommit_pages(hpage_size, oc_pool);
203 }
204
205 int main(int argc, char **argv)
206 {
207 char *env;
208
209 test_init(argc, argv);
210 check_must_be_root();
211 local_check_free_huge_pages(POOL_SIZE);
212 saved_nr_hugepages = local_read_meminfo("HugePages_Total:");
213
214 /*
215 * We cannot call check_hugepagesize because we are not linked to
216 * libhugetlbfs. This is a bit hacky but we are depending on earlier
217 * tests failing to catch when this wouldn't work
218 */
219 hpage_size = local_read_meminfo("Hugepagesize:") * 1024;
220 bpage_size = getpagesize();
221 oc_pool = read_nr_overcommit(hpage_size);
222 if (oc_pool > 0)
223 set_nr_overcommit_hugepages(hpage_size, 0);
224
225 env = getenv("HUGETLB_SHM");
226
227 /* Now that all env parsing is in one location and is only done once
228 * during library init, we cannot modify the value of HGUETLB_SHM
229 * in the middle of the test, instead run the tests that fit with
230 * the current value of HUGETLB_SHM
231 */
232 if (env && strcasecmp(env, "yes") == 0) {
233 /* Run the test with large pages */
234 run_test("override-requested-aligned", 1, 0, POOL_SIZE, 1);
235
236 /* Run the test with large pages but with an unaligned size */
237 run_test("override-requested-unaligned", 1, 1, POOL_SIZE, 2);
238
239 /* Run the test with no pool but requested large pages */
240 setup_hugetlb_pool(0);
241 run_test("override-requested-aligned-nopool", 1, 0, 0, 0);
242 } else {
243 /* Run the test with small pages */
244 run_test("override-not-requested-aligned", 1, 0, POOL_SIZE, 0);
245 }
246
247 PASS();
248 }