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