1 /* 2 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. All advertising materials mentioning features or use of this software 13 * must display the following acknowledgement: 14 * This product includes software developed by Adam Glass and Charles 15 * Hannum. 16 * 4. The names of the authors may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "opt_compat.h" 32 #include "opt_sysvipc.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/sysproto.h> 37 #include <sys/kernel.h> 38 #include <sys/sysctl.h> 39 #include <sys/shm.h> 40 #include <sys/proc.h> 41 #include <sys/malloc.h> 42 #include <sys/mman.h> 43 #include <sys/stat.h> 44 #include <sys/sysent.h> 45 #include <sys/jail.h> 46 47 #include <sys/mplock2.h> 48 49 #include <vm/vm.h> 50 #include <vm/vm_param.h> 51 #include <sys/lock.h> 52 #include <vm/pmap.h> 53 #include <vm/vm_object.h> 54 #include <vm/vm_map.h> 55 #include <vm/vm_page.h> 56 #include <vm/vm_pager.h> 57 58 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments"); 59 60 static int shmget_allocate_segment (struct proc *p, struct shmget_args *uap, int mode); 61 static int shmget_existing (struct proc *p, struct shmget_args *uap, int mode, int segnum); 62 63 #define SHMSEG_FREE 0x0200 64 #define SHMSEG_REMOVED 0x0400 65 #define SHMSEG_ALLOCATED 0x0800 66 #define SHMSEG_WANTED 0x1000 67 68 static int shm_last_free, shm_committed, shmalloced; 69 int shm_nused; 70 static struct shmid_ds *shmsegs; 71 72 struct shm_handle { 73 /* vm_offset_t kva; */ 74 vm_object_t shm_object; 75 }; 76 77 struct shmmap_state { 78 vm_offset_t va; 79 int shmid; 80 }; 81 82 static void shm_deallocate_segment (struct shmid_ds *); 83 static int shm_find_segment_by_key (key_t); 84 static struct shmid_ds *shm_find_segment_by_shmid (int); 85 static int shm_delete_mapping (struct vmspace *vm, struct shmmap_state *); 86 static void shmrealloc (void); 87 static void shminit (void *); 88 89 /* 90 * Tuneable values 91 */ 92 #ifndef SHMMIN 93 #define SHMMIN 1 94 #endif 95 #ifndef SHMMNI 96 #define SHMMNI 512 97 #endif 98 #ifndef SHMSEG 99 #define SHMSEG 1024 100 #endif 101 102 struct shminfo shminfo = { 103 0, 104 SHMMIN, 105 SHMMNI, 106 SHMSEG, 107 0 108 }; 109 110 static int shm_use_phys = 1; 111 112 TUNABLE_LONG("kern.ipc.shmmin", &shminfo.shmmin); 113 TUNABLE_LONG("kern.ipc.shmmni", &shminfo.shmmni); 114 TUNABLE_LONG("kern.ipc.shmseg", &shminfo.shmseg); 115 TUNABLE_LONG("kern.ipc.shmmaxpgs", &shminfo.shmall); 116 TUNABLE_INT("kern.ipc.shm_use_phys", &shm_use_phys); 117 118 SYSCTL_LONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0, 119 "Max shared memory segment size"); 120 SYSCTL_LONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0, 121 "Min shared memory segment size"); 122 SYSCTL_LONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RD, &shminfo.shmmni, 0, 123 "Max number of shared memory identifiers"); 124 SYSCTL_LONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RW, &shminfo.shmseg, 0, 125 "Max shared memory segments per process"); 126 SYSCTL_LONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0, 127 "Max pages of shared memory"); 128 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW, &shm_use_phys, 0, 129 "Use phys pager allocation instead of swap pager allocation"); 130 131 static int 132 shm_find_segment_by_key(key_t key) 133 { 134 int i; 135 136 for (i = 0; i < shmalloced; i++) { 137 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) && 138 shmsegs[i].shm_perm.key == key) 139 return i; 140 } 141 return -1; 142 } 143 144 static struct shmid_ds * 145 shm_find_segment_by_shmid(int shmid) 146 { 147 int segnum; 148 struct shmid_ds *shmseg; 149 150 segnum = IPCID_TO_IX(shmid); 151 if (segnum < 0 || segnum >= shmalloced) 152 return NULL; 153 shmseg = &shmsegs[segnum]; 154 if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED)) 155 != SHMSEG_ALLOCATED || 156 shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid)) { 157 return NULL; 158 } 159 return shmseg; 160 } 161 162 static void 163 shm_deallocate_segment(struct shmid_ds *shmseg) 164 { 165 struct shm_handle *shm_handle; 166 size_t size; 167 168 shm_handle = shmseg->shm_internal; 169 vm_object_deallocate(shm_handle->shm_object); 170 kfree((caddr_t)shm_handle, M_SHM); 171 shmseg->shm_internal = NULL; 172 size = round_page(shmseg->shm_segsz); 173 shm_committed -= btoc(size); 174 shm_nused--; 175 shmseg->shm_perm.mode = SHMSEG_FREE; 176 } 177 178 static int 179 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s) 180 { 181 struct shmid_ds *shmseg; 182 int segnum, result; 183 size_t size; 184 185 segnum = IPCID_TO_IX(shmmap_s->shmid); 186 shmseg = &shmsegs[segnum]; 187 size = round_page(shmseg->shm_segsz); 188 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size); 189 if (result != KERN_SUCCESS) 190 return EINVAL; 191 shmmap_s->shmid = -1; 192 shmseg->shm_dtime = time_second; 193 if ((--shmseg->shm_nattch <= 0) && 194 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) { 195 shm_deallocate_segment(shmseg); 196 shm_last_free = segnum; 197 } 198 return 0; 199 } 200 201 /* 202 * MPALMOSTSAFE 203 */ 204 int 205 sys_shmdt(struct shmdt_args *uap) 206 { 207 struct thread *td = curthread; 208 struct proc *p = td->td_proc; 209 struct shmmap_state *shmmap_s; 210 long i; 211 int error; 212 213 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) 214 return (ENOSYS); 215 216 get_mplock(); 217 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm; 218 if (shmmap_s == NULL) { 219 error = EINVAL; 220 goto done; 221 } 222 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) { 223 if (shmmap_s->shmid != -1 && 224 shmmap_s->va == (vm_offset_t)uap->shmaddr) 225 break; 226 } 227 if (i == shminfo.shmseg) 228 error = EINVAL; 229 else 230 error = shm_delete_mapping(p->p_vmspace, shmmap_s); 231 done: 232 rel_mplock(); 233 return (error); 234 } 235 236 /* 237 * MPALMOSTSAFE 238 */ 239 int 240 sys_shmat(struct shmat_args *uap) 241 { 242 struct thread *td = curthread; 243 struct proc *p = td->td_proc; 244 int error, flags; 245 long i; 246 struct shmid_ds *shmseg; 247 struct shmmap_state *shmmap_s = NULL; 248 struct shm_handle *shm_handle; 249 vm_offset_t attach_va; 250 vm_prot_t prot; 251 vm_size_t size; 252 vm_size_t align; 253 int rv; 254 255 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) 256 return (ENOSYS); 257 258 get_mplock(); 259 again: 260 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm; 261 if (shmmap_s == NULL) { 262 size = shminfo.shmseg * sizeof(struct shmmap_state); 263 shmmap_s = kmalloc(size, M_SHM, M_WAITOK); 264 for (i = 0; i < shminfo.shmseg; i++) 265 shmmap_s[i].shmid = -1; 266 if (p->p_vmspace->vm_shm != NULL) { 267 kfree(shmmap_s, M_SHM); 268 goto again; 269 } 270 p->p_vmspace->vm_shm = (caddr_t)shmmap_s; 271 } 272 shmseg = shm_find_segment_by_shmid(uap->shmid); 273 if (shmseg == NULL) { 274 error = EINVAL; 275 goto done; 276 } 277 error = ipcperm(p, &shmseg->shm_perm, 278 (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W); 279 if (error) 280 goto done; 281 for (i = 0; i < shminfo.shmseg; i++) { 282 if (shmmap_s->shmid == -1) 283 break; 284 shmmap_s++; 285 } 286 if (i >= shminfo.shmseg) { 287 error = EMFILE; 288 goto done; 289 } 290 size = round_page(shmseg->shm_segsz); 291 #ifdef VM_PROT_READ_IS_EXEC 292 prot = VM_PROT_READ | VM_PROT_EXECUTE; 293 #else 294 prot = VM_PROT_READ; 295 #endif 296 if ((uap->shmflg & SHM_RDONLY) == 0) 297 prot |= VM_PROT_WRITE; 298 flags = MAP_ANON | MAP_SHARED; 299 if (uap->shmaddr) { 300 flags |= MAP_FIXED; 301 if (uap->shmflg & SHM_RND) { 302 attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1); 303 } else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0) { 304 attach_va = (vm_offset_t)uap->shmaddr; 305 } else { 306 error = EINVAL; 307 goto done; 308 } 309 } else { 310 /* 311 * This is just a hint to vm_map_find() about where to put it. 312 */ 313 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr + 314 maxtsiz + maxdsiz); 315 } 316 317 /* 318 * Handle alignment. For large memory maps it is possible 319 * that the MMU can optimize the page table so align anything 320 * that is a multiple of SEG_SIZE to SEG_SIZE. 321 */ 322 if ((flags & MAP_FIXED) == 0 && (size & SEG_MASK) == 0) 323 align = SEG_SIZE; 324 else 325 align = PAGE_SIZE; 326 327 shm_handle = shmseg->shm_internal; 328 vm_object_hold(shm_handle->shm_object); 329 vm_object_chain_wait(shm_handle->shm_object, 0); 330 vm_object_reference_locked(shm_handle->shm_object); 331 rv = vm_map_find(&p->p_vmspace->vm_map, 332 shm_handle->shm_object, NULL, 333 0, &attach_va, size, 334 align, 335 ((flags & MAP_FIXED) ? 0 : 1), 336 VM_MAPTYPE_NORMAL, 337 prot, prot, 0); 338 vm_object_drop(shm_handle->shm_object); 339 if (rv != KERN_SUCCESS) { 340 vm_object_deallocate(shm_handle->shm_object); 341 error = ENOMEM; 342 goto done; 343 } 344 vm_map_inherit(&p->p_vmspace->vm_map, 345 attach_va, attach_va + size, VM_INHERIT_SHARE); 346 347 KKASSERT(shmmap_s->shmid == -1); 348 shmmap_s->va = attach_va; 349 shmmap_s->shmid = uap->shmid; 350 shmseg->shm_lpid = p->p_pid; 351 shmseg->shm_atime = time_second; 352 shmseg->shm_nattch++; 353 uap->sysmsg_resultp = (void *)attach_va; 354 error = 0; 355 done: 356 rel_mplock(); 357 return error; 358 } 359 360 /* 361 * MPALMOSTSAFE 362 */ 363 int 364 sys_shmctl(struct shmctl_args *uap) 365 { 366 struct thread *td = curthread; 367 struct proc *p = td->td_proc; 368 int error; 369 struct shmid_ds inbuf; 370 struct shmid_ds *shmseg; 371 372 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) 373 return (ENOSYS); 374 375 get_mplock(); 376 shmseg = shm_find_segment_by_shmid(uap->shmid); 377 if (shmseg == NULL) { 378 error = EINVAL; 379 goto done; 380 } 381 382 switch (uap->cmd) { 383 case IPC_STAT: 384 error = ipcperm(p, &shmseg->shm_perm, IPC_R); 385 if (error == 0) 386 error = copyout(shmseg, uap->buf, sizeof(inbuf)); 387 break; 388 case IPC_SET: 389 error = ipcperm(p, &shmseg->shm_perm, IPC_M); 390 if (error == 0) 391 error = copyin(uap->buf, &inbuf, sizeof(inbuf)); 392 if (error == 0) { 393 shmseg->shm_perm.uid = inbuf.shm_perm.uid; 394 shmseg->shm_perm.gid = inbuf.shm_perm.gid; 395 shmseg->shm_perm.mode = 396 (shmseg->shm_perm.mode & ~ACCESSPERMS) | 397 (inbuf.shm_perm.mode & ACCESSPERMS); 398 shmseg->shm_ctime = time_second; 399 } 400 break; 401 case IPC_RMID: 402 error = ipcperm(p, &shmseg->shm_perm, IPC_M); 403 if (error == 0) { 404 shmseg->shm_perm.key = IPC_PRIVATE; 405 shmseg->shm_perm.mode |= SHMSEG_REMOVED; 406 if (shmseg->shm_nattch <= 0) { 407 shm_deallocate_segment(shmseg); 408 shm_last_free = IPCID_TO_IX(uap->shmid); 409 } 410 } 411 break; 412 #if 0 413 case SHM_LOCK: 414 case SHM_UNLOCK: 415 #endif 416 default: 417 error = EINVAL; 418 break; 419 } 420 done: 421 rel_mplock(); 422 return error; 423 } 424 425 static int 426 shmget_existing(struct proc *p, struct shmget_args *uap, int mode, int segnum) 427 { 428 struct shmid_ds *shmseg; 429 int error; 430 431 shmseg = &shmsegs[segnum]; 432 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) { 433 /* 434 * This segment is in the process of being allocated. Wait 435 * until it's done, and look the key up again (in case the 436 * allocation failed or it was freed). 437 */ 438 shmseg->shm_perm.mode |= SHMSEG_WANTED; 439 error = tsleep((caddr_t)shmseg, PCATCH, "shmget", 0); 440 if (error) 441 return error; 442 return EAGAIN; 443 } 444 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL)) 445 return EEXIST; 446 error = ipcperm(p, &shmseg->shm_perm, mode); 447 if (error) 448 return error; 449 if (uap->size && uap->size > shmseg->shm_segsz) 450 return EINVAL; 451 uap->sysmsg_result = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm); 452 return 0; 453 } 454 455 static int 456 shmget_allocate_segment(struct proc *p, struct shmget_args *uap, int mode) 457 { 458 int i, segnum, shmid; 459 size_t size; 460 struct ucred *cred = p->p_ucred; 461 struct shmid_ds *shmseg; 462 struct shm_handle *shm_handle; 463 464 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax) 465 return EINVAL; 466 if (shm_nused >= shminfo.shmmni) /* any shmids left? */ 467 return ENOSPC; 468 size = round_page(uap->size); 469 if (shm_committed + btoc(size) > shminfo.shmall) 470 return ENOMEM; 471 if (shm_last_free < 0) { 472 shmrealloc(); /* maybe expand the shmsegs[] array */ 473 for (i = 0; i < shmalloced; i++) { 474 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE) 475 break; 476 } 477 if (i == shmalloced) 478 return ENOSPC; 479 segnum = i; 480 } else { 481 segnum = shm_last_free; 482 shm_last_free = -1; 483 } 484 shmseg = &shmsegs[segnum]; 485 /* 486 * In case we sleep in malloc(), mark the segment present but deleted 487 * so that noone else tries to create the same key. 488 */ 489 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED; 490 shmseg->shm_perm.key = uap->key; 491 shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff; 492 shm_handle = kmalloc(sizeof(struct shm_handle), M_SHM, M_WAITOK); 493 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm); 494 495 /* 496 * We make sure that we have allocated a pager before we need 497 * to. 498 */ 499 if (shm_use_phys) { 500 shm_handle->shm_object = 501 phys_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0); 502 } else { 503 shm_handle->shm_object = 504 swap_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0); 505 } 506 vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING); 507 vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT); 508 509 shmseg->shm_internal = shm_handle; 510 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid; 511 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid; 512 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) | 513 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED; 514 shmseg->shm_segsz = uap->size; 515 shmseg->shm_cpid = p->p_pid; 516 shmseg->shm_lpid = shmseg->shm_nattch = 0; 517 shmseg->shm_atime = shmseg->shm_dtime = 0; 518 shmseg->shm_ctime = time_second; 519 shm_committed += btoc(size); 520 shm_nused++; 521 522 /* 523 * If a physical mapping is desired and we have a ton of free pages 524 * we pre-allocate the pages here in order to avoid on-the-fly 525 * allocation later. This has a big effect on database warm-up 526 * times since DFly supports concurrent page faults coming from the 527 * same VM object for pages which already exist. 528 * 529 * This can hang the kernel for a while so only do it if shm_use_phys 530 * is set to 2 or higher. 531 */ 532 if (shm_use_phys > 1) { 533 vm_pindex_t pi, pmax; 534 vm_page_t m; 535 536 pmax = round_page(shmseg->shm_segsz) >> PAGE_SHIFT; 537 vm_object_hold(shm_handle->shm_object); 538 if (pmax > vmstats.v_free_count) 539 pmax = vmstats.v_free_count; 540 for (pi = 0; pi < pmax; ++pi) { 541 m = vm_page_grab(shm_handle->shm_object, pi, 542 VM_ALLOC_SYSTEM | VM_ALLOC_NULL_OK | 543 VM_ALLOC_ZERO); 544 if (m == NULL) 545 break; 546 vm_pager_get_page(shm_handle->shm_object, &m, 1); 547 vm_page_activate(m); 548 vm_page_wakeup(m); 549 lwkt_yield(); 550 } 551 vm_object_drop(shm_handle->shm_object); 552 } 553 554 if (shmseg->shm_perm.mode & SHMSEG_WANTED) { 555 /* 556 * Somebody else wanted this key while we were asleep. Wake 557 * them up now. 558 */ 559 shmseg->shm_perm.mode &= ~SHMSEG_WANTED; 560 wakeup((caddr_t)shmseg); 561 } 562 uap->sysmsg_result = shmid; 563 return 0; 564 } 565 566 /* 567 * MPALMOSTSAFE 568 */ 569 int 570 sys_shmget(struct shmget_args *uap) 571 { 572 struct thread *td = curthread; 573 struct proc *p = td->td_proc; 574 int segnum, mode, error; 575 576 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) 577 return (ENOSYS); 578 579 mode = uap->shmflg & ACCESSPERMS; 580 get_mplock(); 581 582 if (uap->key != IPC_PRIVATE) { 583 again: 584 segnum = shm_find_segment_by_key(uap->key); 585 if (segnum >= 0) { 586 error = shmget_existing(p, uap, mode, segnum); 587 if (error == EAGAIN) 588 goto again; 589 goto done; 590 } 591 if ((uap->shmflg & IPC_CREAT) == 0) { 592 error = ENOENT; 593 goto done; 594 } 595 } 596 error = shmget_allocate_segment(p, uap, mode); 597 done: 598 rel_mplock(); 599 return (error); 600 } 601 602 void 603 shmfork(struct proc *p1, struct proc *p2) 604 { 605 struct shmmap_state *shmmap_s; 606 size_t size; 607 int i; 608 609 get_mplock(); 610 size = shminfo.shmseg * sizeof(struct shmmap_state); 611 shmmap_s = kmalloc(size, M_SHM, M_WAITOK); 612 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size); 613 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s; 614 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) { 615 if (shmmap_s->shmid != -1) 616 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++; 617 } 618 rel_mplock(); 619 } 620 621 void 622 shmexit(struct vmspace *vm) 623 { 624 struct shmmap_state *base, *shm; 625 int i; 626 627 if ((base = (struct shmmap_state *)vm->vm_shm) != NULL) { 628 vm->vm_shm = NULL; 629 get_mplock(); 630 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) { 631 if (shm->shmid != -1) 632 shm_delete_mapping(vm, shm); 633 } 634 kfree(base, M_SHM); 635 rel_mplock(); 636 } 637 } 638 639 static void 640 shmrealloc(void) 641 { 642 int i; 643 struct shmid_ds *newsegs; 644 645 if (shmalloced >= shminfo.shmmni) 646 return; 647 648 newsegs = kmalloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK); 649 for (i = 0; i < shmalloced; i++) 650 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0])); 651 for (; i < shminfo.shmmni; i++) { 652 shmsegs[i].shm_perm.mode = SHMSEG_FREE; 653 shmsegs[i].shm_perm.seq = 0; 654 } 655 kfree(shmsegs, M_SHM); 656 shmsegs = newsegs; 657 shmalloced = shminfo.shmmni; 658 } 659 660 static void 661 shminit(void *dummy) 662 { 663 int i; 664 665 /* 666 * If not overridden by a tunable set the maximum shm to 667 * 2/3 of main memory. 668 */ 669 if (shminfo.shmall == 0) 670 shminfo.shmall = (size_t)vmstats.v_page_count * 2 / 3; 671 672 shminfo.shmmax = shminfo.shmall * PAGE_SIZE; 673 shmalloced = shminfo.shmmni; 674 shmsegs = kmalloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK); 675 for (i = 0; i < shmalloced; i++) { 676 shmsegs[i].shm_perm.mode = SHMSEG_FREE; 677 shmsegs[i].shm_perm.seq = 0; 678 } 679 shm_last_free = 0; 680 shm_nused = 0; 681 shm_committed = 0; 682 } 683 SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL); 684