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, 0, 333 &attach_va, 334 size, align, 335 ((flags & MAP_FIXED) ? 0 : 1), 336 VM_MAPTYPE_NORMAL, 337 prot, prot, 338 0); 339 vm_object_drop(shm_handle->shm_object); 340 if (rv != KERN_SUCCESS) { 341 vm_object_deallocate(shm_handle->shm_object); 342 error = ENOMEM; 343 goto done; 344 } 345 vm_map_inherit(&p->p_vmspace->vm_map, 346 attach_va, attach_va + size, VM_INHERIT_SHARE); 347 348 KKASSERT(shmmap_s->shmid == -1); 349 shmmap_s->va = attach_va; 350 shmmap_s->shmid = uap->shmid; 351 shmseg->shm_lpid = p->p_pid; 352 shmseg->shm_atime = time_second; 353 shmseg->shm_nattch++; 354 uap->sysmsg_resultp = (void *)attach_va; 355 error = 0; 356 done: 357 rel_mplock(); 358 return error; 359 } 360 361 /* 362 * MPALMOSTSAFE 363 */ 364 int 365 sys_shmctl(struct shmctl_args *uap) 366 { 367 struct thread *td = curthread; 368 struct proc *p = td->td_proc; 369 int error; 370 struct shmid_ds inbuf; 371 struct shmid_ds *shmseg; 372 373 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) 374 return (ENOSYS); 375 376 get_mplock(); 377 shmseg = shm_find_segment_by_shmid(uap->shmid); 378 if (shmseg == NULL) { 379 error = EINVAL; 380 goto done; 381 } 382 383 switch (uap->cmd) { 384 case IPC_STAT: 385 error = ipcperm(p, &shmseg->shm_perm, IPC_R); 386 if (error == 0) 387 error = copyout(shmseg, uap->buf, sizeof(inbuf)); 388 break; 389 case IPC_SET: 390 error = ipcperm(p, &shmseg->shm_perm, IPC_M); 391 if (error == 0) 392 error = copyin(uap->buf, &inbuf, sizeof(inbuf)); 393 if (error == 0) { 394 shmseg->shm_perm.uid = inbuf.shm_perm.uid; 395 shmseg->shm_perm.gid = inbuf.shm_perm.gid; 396 shmseg->shm_perm.mode = 397 (shmseg->shm_perm.mode & ~ACCESSPERMS) | 398 (inbuf.shm_perm.mode & ACCESSPERMS); 399 shmseg->shm_ctime = time_second; 400 } 401 break; 402 case IPC_RMID: 403 error = ipcperm(p, &shmseg->shm_perm, IPC_M); 404 if (error == 0) { 405 shmseg->shm_perm.key = IPC_PRIVATE; 406 shmseg->shm_perm.mode |= SHMSEG_REMOVED; 407 if (shmseg->shm_nattch <= 0) { 408 shm_deallocate_segment(shmseg); 409 shm_last_free = IPCID_TO_IX(uap->shmid); 410 } 411 } 412 break; 413 #if 0 414 case SHM_LOCK: 415 case SHM_UNLOCK: 416 #endif 417 default: 418 error = EINVAL; 419 break; 420 } 421 done: 422 rel_mplock(); 423 return error; 424 } 425 426 static int 427 shmget_existing(struct proc *p, struct shmget_args *uap, int mode, int segnum) 428 { 429 struct shmid_ds *shmseg; 430 int error; 431 432 shmseg = &shmsegs[segnum]; 433 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) { 434 /* 435 * This segment is in the process of being allocated. Wait 436 * until it's done, and look the key up again (in case the 437 * allocation failed or it was freed). 438 */ 439 shmseg->shm_perm.mode |= SHMSEG_WANTED; 440 error = tsleep((caddr_t)shmseg, PCATCH, "shmget", 0); 441 if (error) 442 return error; 443 return EAGAIN; 444 } 445 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL)) 446 return EEXIST; 447 error = ipcperm(p, &shmseg->shm_perm, mode); 448 if (error) 449 return error; 450 if (uap->size && uap->size > shmseg->shm_segsz) 451 return EINVAL; 452 uap->sysmsg_result = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm); 453 return 0; 454 } 455 456 static int 457 shmget_allocate_segment(struct proc *p, struct shmget_args *uap, int mode) 458 { 459 int i, segnum, shmid; 460 size_t size; 461 struct ucred *cred = p->p_ucred; 462 struct shmid_ds *shmseg; 463 struct shm_handle *shm_handle; 464 465 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax) 466 return EINVAL; 467 if (shm_nused >= shminfo.shmmni) /* any shmids left? */ 468 return ENOSPC; 469 size = round_page(uap->size); 470 if (shm_committed + btoc(size) > shminfo.shmall) 471 return ENOMEM; 472 if (shm_last_free < 0) { 473 shmrealloc(); /* maybe expand the shmsegs[] array */ 474 for (i = 0; i < shmalloced; i++) { 475 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE) 476 break; 477 } 478 if (i == shmalloced) 479 return ENOSPC; 480 segnum = i; 481 } else { 482 segnum = shm_last_free; 483 shm_last_free = -1; 484 } 485 shmseg = &shmsegs[segnum]; 486 /* 487 * In case we sleep in malloc(), mark the segment present but deleted 488 * so that noone else tries to create the same key. 489 */ 490 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED; 491 shmseg->shm_perm.key = uap->key; 492 shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff; 493 shm_handle = kmalloc(sizeof(struct shm_handle), M_SHM, M_WAITOK); 494 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm); 495 496 /* 497 * We make sure that we have allocated a pager before we need 498 * to. 499 */ 500 if (shm_use_phys) { 501 shm_handle->shm_object = 502 phys_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0); 503 } else { 504 shm_handle->shm_object = 505 swap_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0); 506 } 507 vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING); 508 vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT); 509 510 shmseg->shm_internal = shm_handle; 511 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid; 512 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid; 513 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) | 514 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED; 515 shmseg->shm_segsz = uap->size; 516 shmseg->shm_cpid = p->p_pid; 517 shmseg->shm_lpid = shmseg->shm_nattch = 0; 518 shmseg->shm_atime = shmseg->shm_dtime = 0; 519 shmseg->shm_ctime = time_second; 520 shm_committed += btoc(size); 521 shm_nused++; 522 523 /* 524 * If a physical mapping is desired and we have a ton of free pages 525 * we pre-allocate the pages here in order to avoid on-the-fly 526 * allocation later. This has a big effect on database warm-up 527 * times since DFly supports concurrent page faults coming from the 528 * same VM object for pages which already exist. 529 * 530 * This can hang the kernel for a while so only do it if shm_use_phys 531 * is set to 2 or higher. 532 */ 533 if (shm_use_phys > 1) { 534 vm_pindex_t pi, pmax; 535 vm_page_t m; 536 537 pmax = round_page(shmseg->shm_segsz) >> PAGE_SHIFT; 538 vm_object_hold(shm_handle->shm_object); 539 if (pmax > vmstats.v_free_count) 540 pmax = vmstats.v_free_count; 541 for (pi = 0; pi < pmax; ++pi) { 542 m = vm_page_grab(shm_handle->shm_object, pi, 543 VM_ALLOC_SYSTEM | VM_ALLOC_NULL_OK | 544 VM_ALLOC_ZERO); 545 if (m == NULL) 546 break; 547 vm_pager_get_page(shm_handle->shm_object, &m, 1); 548 vm_page_activate(m); 549 vm_page_wakeup(m); 550 lwkt_yield(); 551 } 552 vm_object_drop(shm_handle->shm_object); 553 } 554 555 if (shmseg->shm_perm.mode & SHMSEG_WANTED) { 556 /* 557 * Somebody else wanted this key while we were asleep. Wake 558 * them up now. 559 */ 560 shmseg->shm_perm.mode &= ~SHMSEG_WANTED; 561 wakeup((caddr_t)shmseg); 562 } 563 uap->sysmsg_result = shmid; 564 return 0; 565 } 566 567 /* 568 * MPALMOSTSAFE 569 */ 570 int 571 sys_shmget(struct shmget_args *uap) 572 { 573 struct thread *td = curthread; 574 struct proc *p = td->td_proc; 575 int segnum, mode, error; 576 577 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL) 578 return (ENOSYS); 579 580 mode = uap->shmflg & ACCESSPERMS; 581 get_mplock(); 582 583 if (uap->key != IPC_PRIVATE) { 584 again: 585 segnum = shm_find_segment_by_key(uap->key); 586 if (segnum >= 0) { 587 error = shmget_existing(p, uap, mode, segnum); 588 if (error == EAGAIN) 589 goto again; 590 goto done; 591 } 592 if ((uap->shmflg & IPC_CREAT) == 0) { 593 error = ENOENT; 594 goto done; 595 } 596 } 597 error = shmget_allocate_segment(p, uap, mode); 598 done: 599 rel_mplock(); 600 return (error); 601 } 602 603 void 604 shmfork(struct proc *p1, struct proc *p2) 605 { 606 struct shmmap_state *shmmap_s; 607 size_t size; 608 int i; 609 610 get_mplock(); 611 size = shminfo.shmseg * sizeof(struct shmmap_state); 612 shmmap_s = kmalloc(size, M_SHM, M_WAITOK); 613 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size); 614 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s; 615 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) { 616 if (shmmap_s->shmid != -1) 617 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++; 618 } 619 rel_mplock(); 620 } 621 622 void 623 shmexit(struct vmspace *vm) 624 { 625 struct shmmap_state *base, *shm; 626 int i; 627 628 if ((base = (struct shmmap_state *)vm->vm_shm) != NULL) { 629 vm->vm_shm = NULL; 630 get_mplock(); 631 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) { 632 if (shm->shmid != -1) 633 shm_delete_mapping(vm, shm); 634 } 635 kfree(base, M_SHM); 636 rel_mplock(); 637 } 638 } 639 640 static void 641 shmrealloc(void) 642 { 643 int i; 644 struct shmid_ds *newsegs; 645 646 if (shmalloced >= shminfo.shmmni) 647 return; 648 649 newsegs = kmalloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK); 650 for (i = 0; i < shmalloced; i++) 651 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0])); 652 for (; i < shminfo.shmmni; i++) { 653 shmsegs[i].shm_perm.mode = SHMSEG_FREE; 654 shmsegs[i].shm_perm.seq = 0; 655 } 656 kfree(shmsegs, M_SHM); 657 shmsegs = newsegs; 658 shmalloced = shminfo.shmmni; 659 } 660 661 static void 662 shminit(void *dummy) 663 { 664 int i; 665 666 /* 667 * If not overridden by a tunable set the maximum shm to 668 * 2/3 of main memory. 669 */ 670 if (shminfo.shmall == 0) 671 shminfo.shmall = (size_t)vmstats.v_page_count * 2 / 3; 672 673 shminfo.shmmax = shminfo.shmall * PAGE_SIZE; 674 shmalloced = shminfo.shmmni; 675 shmsegs = kmalloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK); 676 for (i = 0; i < shmalloced; i++) { 677 shmsegs[i].shm_perm.mode = SHMSEG_FREE; 678 shmsegs[i].shm_perm.seq = 0; 679 } 680 shm_last_free = 0; 681 shm_nused = 0; 682 shm_committed = 0; 683 } 684 SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL); 685