2 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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.
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.
31 #include "opt_compat.h"
32 #include "opt_sysvipc.h"
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>
41 #include <sys/malloc.h>
44 #include <sys/sysent.h>
47 #include <sys/mplock2.h>
50 #include <vm/vm_param.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>
58 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
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);
63 #define SHMSEG_FREE 0x0200
64 #define SHMSEG_REMOVED 0x0400
65 #define SHMSEG_ALLOCATED 0x0800
66 #define SHMSEG_WANTED 0x1000
68 static int shm_last_free, shm_committed, shmalloced;
70 static struct shmid_ds *shmsegs;
73 /* vm_offset_t kva; */
74 vm_object_t shm_object;
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 *);
102 struct shminfo shminfo = {
110 static int shm_use_phys = 1;
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);
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");
132 shm_find_segment_by_key(key_t key)
136 for (i = 0; i < shmalloced; i++) {
137 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
138 shmsegs[i].shm_perm.key == key)
144 static struct shmid_ds *
145 shm_find_segment_by_shmid(int shmid)
148 struct shmid_ds *shmseg;
150 segnum = IPCID_TO_IX(shmid);
151 if (segnum < 0 || segnum >= shmalloced)
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)) {
163 shm_deallocate_segment(struct shmid_ds *shmseg)
165 struct shm_handle *shm_handle;
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);
175 shmseg->shm_perm.mode = SHMSEG_FREE;
179 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
181 struct shmid_ds *shmseg;
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)
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;
205 sys_shmdt(struct shmdt_args *uap)
207 struct thread *td = curthread;
208 struct proc *p = td->td_proc;
209 struct shmmap_state *shmmap_s;
213 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
217 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
218 if (shmmap_s == NULL) {
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)
227 if (i == shminfo.shmseg)
230 error = shm_delete_mapping(p->p_vmspace, shmmap_s);
240 sys_shmat(struct shmat_args *uap)
242 struct thread *td = curthread;
243 struct proc *p = td->td_proc;
246 struct shmid_ds *shmseg;
247 struct shmmap_state *shmmap_s = NULL;
248 struct shm_handle *shm_handle;
249 vm_offset_t attach_va;
255 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
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);
270 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
272 shmseg = shm_find_segment_by_shmid(uap->shmid);
273 if (shmseg == NULL) {
277 error = ipcperm(p, &shmseg->shm_perm,
278 (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
281 for (i = 0; i < shminfo.shmseg; i++) {
282 if (shmmap_s->shmid == -1)
286 if (i >= shminfo.shmseg) {
290 size = round_page(shmseg->shm_segsz);
291 #ifdef VM_PROT_READ_IS_EXEC
292 prot = VM_PROT_READ | VM_PROT_EXECUTE;
296 if ((uap->shmflg & SHM_RDONLY) == 0)
297 prot |= VM_PROT_WRITE;
298 flags = MAP_ANON | MAP_SHARED;
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;
311 * This is just a hint to vm_map_find() about where to put it.
313 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr +
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.
322 if ((flags & MAP_FIXED) == 0 && (size & SEG_MASK) == 0)
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,
335 ((flags & MAP_FIXED) ? 0 : 1),
338 vm_object_drop(shm_handle->shm_object);
339 if (rv != KERN_SUCCESS) {
340 vm_object_deallocate(shm_handle->shm_object);
344 vm_map_inherit(&p->p_vmspace->vm_map,
345 attach_va, attach_va + size, VM_INHERIT_SHARE);
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;
364 sys_shmctl(struct shmctl_args *uap)
366 struct thread *td = curthread;
367 struct proc *p = td->td_proc;
369 struct shmid_ds inbuf;
370 struct shmid_ds *shmseg;
372 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
376 shmseg = shm_find_segment_by_shmid(uap->shmid);
377 if (shmseg == NULL) {
384 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
386 error = copyout(shmseg, uap->buf, sizeof(inbuf));
389 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
391 error = copyin(uap->buf, &inbuf, sizeof(inbuf));
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;
402 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
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);
426 shmget_existing(struct proc *p, struct shmget_args *uap, int mode, int segnum)
428 struct shmid_ds *shmseg;
431 shmseg = &shmsegs[segnum];
432 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
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).
438 shmseg->shm_perm.mode |= SHMSEG_WANTED;
439 error = tsleep((caddr_t)shmseg, PCATCH, "shmget", 0);
444 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
446 error = ipcperm(p, &shmseg->shm_perm, mode);
449 if (uap->size && uap->size > shmseg->shm_segsz)
451 uap->sysmsg_result = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
456 shmget_allocate_segment(struct proc *p, struct shmget_args *uap, int mode)
458 int i, segnum, shmid;
460 struct ucred *cred = p->p_ucred;
461 struct shmid_ds *shmseg;
462 struct shm_handle *shm_handle;
464 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
466 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
468 size = round_page(uap->size);
469 if (shm_committed + btoc(size) > shminfo.shmall)
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)
481 segnum = shm_last_free;
484 shmseg = &shmsegs[segnum];
486 * In case we sleep in malloc(), mark the segment present but deleted
487 * so that noone else tries to create the same key.
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);
496 * We make sure that we have allocated a pager before we need
500 shm_handle->shm_object =
501 phys_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0);
503 shm_handle->shm_object =
504 swap_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0);
506 vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
507 vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
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);
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.
529 * This can hang the kernel for a while so only do it if shm_use_phys
530 * is set to 2 or higher.
532 if (shm_use_phys > 1) {
533 vm_pindex_t pi, pmax;
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 |
546 vm_pager_get_page(shm_handle->shm_object, &m, 1);
551 vm_object_drop(shm_handle->shm_object);
554 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
556 * Somebody else wanted this key while we were asleep. Wake
559 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
560 wakeup((caddr_t)shmseg);
562 uap->sysmsg_result = shmid;
570 sys_shmget(struct shmget_args *uap)
572 struct thread *td = curthread;
573 struct proc *p = td->td_proc;
574 int segnum, mode, error;
576 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
579 mode = uap->shmflg & ACCESSPERMS;
582 if (uap->key != IPC_PRIVATE) {
584 segnum = shm_find_segment_by_key(uap->key);
586 error = shmget_existing(p, uap, mode, segnum);
591 if ((uap->shmflg & IPC_CREAT) == 0) {
596 error = shmget_allocate_segment(p, uap, mode);
603 shmfork(struct proc *p1, struct proc *p2)
605 struct shmmap_state *shmmap_s;
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++;
622 shmexit(struct vmspace *vm)
624 struct shmmap_state *base, *shm;
627 if ((base = (struct shmmap_state *)vm->vm_shm) != NULL) {
630 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
631 if (shm->shmid != -1)
632 shm_delete_mapping(vm, shm);
643 struct shmid_ds *newsegs;
645 if (shmalloced >= shminfo.shmmni)
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;
655 kfree(shmsegs, M_SHM);
657 shmalloced = shminfo.shmmni;
666 * If not overridden by a tunable set the maximum shm to
667 * 2/3 of main memory.
669 if (shminfo.shmall == 0)
670 shminfo.shmall = (size_t)vmstats.v_page_count * 2 / 3;
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;
683 SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL);