t_mincore.c revision 1.6
1/* $NetBSD: t_mincore.c,v 1.6 2012/06/05 08:44:21 martin Exp $ */ 2 3/*- 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jukka Ruohonen. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32/*- 33 * Copyright (c) 1999 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 38 * NASA Ames Research Center. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 59 * POSSIBILITY OF SUCH DAMAGE. 60 */ 61#include <sys/cdefs.h> 62__RCSID("$NetBSD: t_mincore.c,v 1.6 2012/06/05 08:44:21 martin Exp $"); 63 64#include <sys/mman.h> 65#include <sys/shm.h> 66 67#include <atf-c.h> 68#include <errno.h> 69#include <fcntl.h> 70#include <kvm.h> 71#include <stdio.h> 72#include <stdlib.h> 73#include <string.h> 74#include <unistd.h> 75#include <sys/resource.h> 76#include <sys/sysctl.h> 77 78static long page = 0; 79static const char path[] = "mincore"; 80static size_t check_residency(void *, size_t); 81 82static size_t 83check_residency(void *addr, size_t npgs) 84{ 85 size_t i, resident; 86 char *vec; 87 88 vec = malloc(npgs); 89 90 ATF_REQUIRE(vec != NULL); 91 ATF_REQUIRE(mincore(addr, npgs * page, vec) == 0); 92 93 for (i = resident = 0; i < npgs; i++) { 94 95 if (vec[i] != 0) 96 resident++; 97 98 (void)fprintf(stderr, "page 0x%p is %sresident\n", 99 (char *)addr + (i * page), vec[i] ? "" : "not "); 100 } 101 102 free(vec); 103 104 return resident; 105} 106 107/* 108 * Get an estimate of the current VM size 109 */ 110static size_t 111vm_cur_pages(void) 112{ 113 size_t res = 0; 114 kvm_t *kvm; 115 struct kinfo_proc2 *pi; 116 int cnt; 117 118 kvm = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, getprogname()); 119 if (kvm == NULL) 120 return 0; 121 pi = kvm_getproc2(kvm, KERN_PROC_PID, getpid(), sizeof(*pi), &cnt); 122 if (pi && cnt >= 1) 123 res = pi[0].p_vm_vsize; 124 kvm_close(kvm); 125 126 return res; 127} 128 129ATF_TC(mincore_err); 130ATF_TC_HEAD(mincore_err, tc) 131{ 132 atf_tc_set_md_var(tc, "descr", "Test errors from mincore(2)"); 133} 134 135ATF_TC_BODY(mincore_err, tc) 136{ 137 char *map, *vec; 138 139 map = mmap(NULL, page, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); 140 vec = malloc(page); 141 142 ATF_REQUIRE(vec != NULL); 143 ATF_REQUIRE(map != MAP_FAILED); 144 145 errno = 0; 146 ATF_REQUIRE_ERRNO(EINVAL, mincore(map, 0, vec) == -1); 147 148 errno = 0; 149 ATF_REQUIRE_ERRNO(ENOMEM, mincore(0, page, vec) == -1); 150 151 errno = 0; 152 ATF_REQUIRE_ERRNO(EFAULT, mincore(map, page, (void *)-1) == -1); 153 154 free(vec); 155 ATF_REQUIRE(munmap(map, page) == 0); 156} 157 158ATF_TC_WITH_CLEANUP(mincore_resid); 159ATF_TC_HEAD(mincore_resid, tc) 160{ 161 atf_tc_set_md_var(tc, "descr", "Test page residency with mincore(2)"); 162} 163 164ATF_TC_BODY(mincore_resid, tc) 165{ 166 void *addr, *addr2, *addr3, *buf; 167 size_t npgs = 0; 168 struct stat st; 169 int fd, rv; 170 struct rlimit rlim; 171 size_t needed_pages, limit_pages; 172 173 ATF_REQUIRE(getrlimit(RLIMIT_MEMLOCK, &rlim) == 0); 174 limit_pages = rlim.rlim_cur / page; 175 /* 176 * We can not exactly predict the number of pages resulting from 177 * the test and the mlockall() call below. 178 * Get a safe upper bound instead... 179 */ 180 needed_pages = vm_cur_pages(); 181 /* we certainly will gow by 128 pages */ 182 needed_pages += 128; 183 /* add a bit of safety room */ 184 needed_pages += 12; 185 186 if (needed_pages >= limit_pages) 187 atf_tc_skip("too low limits on locked memory (may need %zu " 188 "pages, limit is %zu pages)", needed_pages, limit_pages); 189 190 (void)memset(&st, 0, sizeof(struct stat)); 191 192 fd = open(path, O_RDWR | O_CREAT, 0700); 193 buf = malloc(page * 5); 194 195 ATF_REQUIRE(fd >= 0); 196 ATF_REQUIRE(buf != NULL); 197 198 rv = write(fd, buf, page * 5); 199 ATF_REQUIRE(rv >= 0); 200 201 ATF_REQUIRE(fd >= 0); 202 ATF_REQUIRE(fstat(fd, &st) == 0); 203 204 addr = mmap(NULL, (size_t)st.st_size, PROT_READ, 205 MAP_FILE | MAP_SHARED, fd, (off_t) 0); 206 207 ATF_REQUIRE(addr != MAP_FAILED); 208 209 (void)close(fd); 210 211 npgs = st.st_size / page; 212 213 if (st.st_size % page != 0) 214 npgs++; 215 216 (void)check_residency(addr, npgs); 217 218 ATF_REQUIRE(mlock(addr, npgs * page) == 0); 219 ATF_REQUIRE(munmap(addr, st.st_size) == 0); 220 221 npgs = 128; 222 223 addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE, 224 MAP_ANON | MAP_PRIVATE | MAP_WIRED, -1, (off_t)0); 225 226 if (addr == MAP_FAILED) 227 return; 228 229 ATF_REQUIRE(check_residency(addr, npgs) == npgs); 230 ATF_REQUIRE(munmap(addr, npgs * page) == 0); 231 232 npgs = 128; 233 234 addr = mmap(NULL, npgs * page, PROT_READ | PROT_WRITE, 235 MAP_ANON | MAP_PRIVATE, -1, (off_t)0); 236 237 if (addr == MAP_FAILED) 238 return; 239 240 /* 241 * Check that the in-core pages match the locked pages. 242 */ 243 ATF_REQUIRE(check_residency(addr, npgs) == 0); 244 245 errno = 0; 246 247 if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0 && errno != ENOMEM) 248 atf_tc_fail("mlockall(2) failed"); 249 250 ATF_REQUIRE(check_residency(addr, npgs) == npgs); 251 252 addr2 = mmap(NULL, npgs * page, PROT_READ, MAP_ANON, -1, (off_t)0); 253 addr3 = mmap(NULL, npgs * page, PROT_NONE, MAP_ANON, -1, (off_t)0); 254 255 if (addr2 == MAP_FAILED || addr3 == MAP_FAILED) 256 return; 257 258 ATF_REQUIRE(check_residency(addr2, npgs) == npgs); 259 ATF_REQUIRE(check_residency(addr3, npgs) == 0); 260 ATF_REQUIRE(mprotect(addr3, npgs * page, PROT_READ) == 0); 261 ATF_REQUIRE(check_residency(addr, npgs) == npgs); 262 ATF_REQUIRE(check_residency(addr2, npgs) == npgs); 263 264 (void)munlockall(); 265 266 ATF_REQUIRE(madvise(addr2, npgs * page, MADV_FREE) == 0); 267 ATF_REQUIRE(check_residency(addr2, npgs) == 0); 268 269 (void)memset(addr, 0, npgs * page); 270 271 ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0); 272 ATF_REQUIRE(check_residency(addr, npgs) == 0); 273 274 (void)munmap(addr, npgs * page); 275 (void)munmap(addr2, npgs * page); 276 (void)munmap(addr3, npgs * page); 277 (void)unlink(path); 278} 279 280ATF_TC_CLEANUP(mincore_resid, tc) 281{ 282 (void)unlink(path); 283} 284 285ATF_TC(mincore_shmseg); 286ATF_TC_HEAD(mincore_shmseg, tc) 287{ 288 atf_tc_set_md_var(tc, "descr", "residency of shared memory"); 289} 290 291ATF_TC_BODY(mincore_shmseg, tc) 292{ 293 size_t npgs = 128; 294 void *addr = NULL; 295 int shmid; 296 297 shmid = shmget(IPC_PRIVATE, npgs * page, 298 IPC_CREAT | S_IRUSR | S_IWUSR); 299 300 ATF_REQUIRE(shmid != -1); 301 302 addr = shmat(shmid, NULL, 0); 303 304 ATF_REQUIRE(addr != NULL); 305 ATF_REQUIRE(check_residency(addr, npgs) == 0); 306 307 (void)memset(addr, 0xff, npgs * page); 308 309 ATF_REQUIRE(check_residency(addr, npgs) == npgs); 310 ATF_REQUIRE(madvise(addr, npgs * page, MADV_FREE) == 0); 311 312 /* 313 * NOTE! Even though we have MADV_FREE'd the range, 314 * there is another reference (the kernel's) to the 315 * object which owns the pages. In this case, the 316 * kernel does not simply free the pages, as haphazardly 317 * freeing pages when there are still references to 318 * an object can cause data corruption (say, the other 319 * referencer doesn't expect the pages to be freed, 320 * and is surprised by the subsequent ZFOD). 321 * 322 * Because of this, we simply report the number of 323 * pages still resident, for information only. 324 */ 325 npgs = check_residency(addr, npgs); 326 327 (void)fprintf(stderr, "%zu pages still resident\n", npgs); 328 329 ATF_REQUIRE(shmdt(addr) == 0); 330 ATF_REQUIRE(shmctl(shmid, IPC_RMID, NULL) == 0); 331} 332 333ATF_TP_ADD_TCS(tp) 334{ 335 336 page = sysconf(_SC_PAGESIZE); 337 ATF_REQUIRE(page >= 0); 338 339 ATF_TP_ADD_TC(tp, mincore_err); 340 ATF_TP_ADD_TC(tp, mincore_resid); 341 ATF_TP_ADD_TC(tp, mincore_shmseg); 342 343 return atf_no_error(); 344} 345