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_sysvipc.h"
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>
40 #include <sys/malloc.h>
43 #include <sys/sysent.h>
46 #include <sys/mplock2.h>
49 #include <vm/vm_param.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_pager.h>
57 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
59 static int shmget_allocate_segment (struct proc *p, struct shmget_args *uap, int mode);
60 static int shmget_existing (struct proc *p, struct shmget_args *uap, int mode, int segnum);
62 #define SHMSEG_FREE 0x0200
63 #define SHMSEG_REMOVED 0x0400
64 #define SHMSEG_ALLOCATED 0x0800
65 #define SHMSEG_WANTED 0x1000
67 static int shm_last_free, shm_committed, shmalloced;
69 static struct shmid_ds *shmsegs;
72 /* vm_offset_t kva; */
73 vm_object_t shm_object;
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 *);
101 struct shminfo shminfo = {
109 static int shm_allow_removed;
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");
130 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RW,
131 &shm_allow_removed, 0,
132 "Enable/Disable attachment to attached segments marked for removal");
135 shm_find_segment_by_key(key_t key)
139 for (i = 0; i < shmalloced; i++) {
140 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
141 shmsegs[i].shm_perm.key == key)
147 static struct shmid_ds *
148 shm_find_segment_by_shmid(int shmid)
151 struct shmid_ds *shmseg;
153 segnum = IPCID_TO_IX(shmid);
154 if (segnum < 0 || segnum >= shmalloced)
156 shmseg = &shmsegs[segnum];
157 if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
158 (!shm_allow_removed &&
159 (shmseg->shm_perm.mode & SHMSEG_REMOVED) != 0) ||
160 shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid)) {
167 shm_deallocate_segment(struct shmid_ds *shmseg)
169 struct shm_handle *shm_handle;
172 shm_handle = shmseg->shm_internal;
173 vm_object_deallocate(shm_handle->shm_object);
174 kfree((caddr_t)shm_handle, M_SHM);
175 shmseg->shm_internal = NULL;
176 size = round_page(shmseg->shm_segsz);
177 shm_committed -= btoc(size);
179 shmseg->shm_perm.mode = SHMSEG_FREE;
183 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
185 struct shmid_ds *shmseg;
189 segnum = IPCID_TO_IX(shmmap_s->shmid);
190 shmseg = &shmsegs[segnum];
191 size = round_page(shmseg->shm_segsz);
192 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
193 if (result != KERN_SUCCESS)
195 shmmap_s->shmid = -1;
196 shmseg->shm_dtime = time_second;
197 if ((--shmseg->shm_nattch <= 0) &&
198 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
199 shm_deallocate_segment(shmseg);
200 shm_last_free = segnum;
209 sys_shmdt(struct shmdt_args *uap)
211 struct thread *td = curthread;
212 struct proc *p = td->td_proc;
213 struct shmmap_state *shmmap_s;
217 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
221 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
222 if (shmmap_s == NULL) {
226 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
227 if (shmmap_s->shmid != -1 &&
228 shmmap_s->va == (vm_offset_t)uap->shmaddr)
231 if (i == shminfo.shmseg)
234 error = shm_delete_mapping(p->p_vmspace, shmmap_s);
244 sys_shmat(struct shmat_args *uap)
246 struct thread *td = curthread;
247 struct proc *p = td->td_proc;
250 struct shmid_ds *shmseg;
251 struct shmmap_state *shmmap_s = NULL;
252 struct shm_handle *shm_handle;
253 vm_offset_t attach_va;
259 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
264 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
265 if (shmmap_s == NULL) {
266 size = shminfo.shmseg * sizeof(struct shmmap_state);
267 shmmap_s = kmalloc(size, M_SHM, M_WAITOK);
268 for (i = 0; i < shminfo.shmseg; i++)
269 shmmap_s[i].shmid = -1;
270 if (p->p_vmspace->vm_shm != NULL) {
271 kfree(shmmap_s, M_SHM);
274 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
276 shmseg = shm_find_segment_by_shmid(uap->shmid);
277 if (shmseg == NULL) {
281 error = ipcperm(p, &shmseg->shm_perm,
282 (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
285 for (i = 0; i < shminfo.shmseg; i++) {
286 if (shmmap_s->shmid == -1)
290 if (i >= shminfo.shmseg) {
294 size = round_page(shmseg->shm_segsz);
295 #ifdef VM_PROT_READ_IS_EXEC
296 prot = VM_PROT_READ | VM_PROT_EXECUTE;
300 if ((uap->shmflg & SHM_RDONLY) == 0)
301 prot |= VM_PROT_WRITE;
302 flags = MAP_ANON | MAP_SHARED;
305 if (uap->shmflg & SHM_RND) {
306 attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
307 } else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0) {
308 attach_va = (vm_offset_t)uap->shmaddr;
315 * This is just a hint to vm_map_find() about where to put it.
317 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr +
322 * Handle alignment. For large memory maps it is possible
323 * that the MMU can optimize the page table so align anything
324 * that is a multiple of SEG_SIZE to SEG_SIZE.
326 if ((flags & MAP_FIXED) == 0 && (size & SEG_MASK) == 0)
331 shm_handle = shmseg->shm_internal;
332 vm_object_hold(shm_handle->shm_object);
333 vm_object_chain_wait(shm_handle->shm_object, 0);
334 vm_object_reference_locked(shm_handle->shm_object);
335 rv = vm_map_find(&p->p_vmspace->vm_map,
336 shm_handle->shm_object, NULL,
339 ((flags & MAP_FIXED) ? 0 : 1),
342 vm_object_drop(shm_handle->shm_object);
343 if (rv != KERN_SUCCESS) {
344 vm_object_deallocate(shm_handle->shm_object);
348 vm_map_inherit(&p->p_vmspace->vm_map,
349 attach_va, attach_va + size, VM_INHERIT_SHARE);
351 KKASSERT(shmmap_s->shmid == -1);
352 shmmap_s->va = attach_va;
353 shmmap_s->shmid = uap->shmid;
354 shmseg->shm_lpid = p->p_pid;
355 shmseg->shm_atime = time_second;
356 shmseg->shm_nattch++;
357 uap->sysmsg_resultp = (void *)attach_va;
368 sys_shmctl(struct shmctl_args *uap)
370 struct thread *td = curthread;
371 struct proc *p = td->td_proc;
373 struct shmid_ds inbuf;
374 struct shmid_ds *shmseg;
376 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
380 shmseg = shm_find_segment_by_shmid(uap->shmid);
381 if (shmseg == NULL) {
388 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
390 error = copyout(shmseg, uap->buf, sizeof(inbuf));
393 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
395 error = copyin(uap->buf, &inbuf, sizeof(inbuf));
397 shmseg->shm_perm.uid = inbuf.shm_perm.uid;
398 shmseg->shm_perm.gid = inbuf.shm_perm.gid;
399 shmseg->shm_perm.mode =
400 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
401 (inbuf.shm_perm.mode & ACCESSPERMS);
402 shmseg->shm_ctime = time_second;
406 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
408 shmseg->shm_perm.key = IPC_PRIVATE;
409 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
410 if (shmseg->shm_nattch <= 0) {
411 shm_deallocate_segment(shmseg);
412 shm_last_free = IPCID_TO_IX(uap->shmid);
430 shmget_existing(struct proc *p, struct shmget_args *uap, int mode, int segnum)
432 struct shmid_ds *shmseg;
435 shmseg = &shmsegs[segnum];
436 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
438 * This segment is in the process of being allocated. Wait
439 * until it's done, and look the key up again (in case the
440 * allocation failed or it was freed).
442 shmseg->shm_perm.mode |= SHMSEG_WANTED;
443 error = tsleep((caddr_t)shmseg, PCATCH, "shmget", 0);
448 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
450 error = ipcperm(p, &shmseg->shm_perm, mode);
453 if (uap->size && uap->size > shmseg->shm_segsz)
455 uap->sysmsg_result = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
460 shmget_allocate_segment(struct proc *p, struct shmget_args *uap, int mode)
462 int i, segnum, shmid;
464 struct ucred *cred = p->p_ucred;
465 struct shmid_ds *shmseg;
466 struct shm_handle *shm_handle;
468 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
470 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
472 size = round_page(uap->size);
473 if (shm_committed + btoc(size) > shminfo.shmall)
475 if (shm_last_free < 0) {
476 shmrealloc(); /* maybe expand the shmsegs[] array */
477 for (i = 0; i < shmalloced; i++) {
478 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
485 segnum = shm_last_free;
488 shmseg = &shmsegs[segnum];
490 * In case we sleep in malloc(), mark the segment present but deleted
491 * so that noone else tries to create the same key.
493 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
494 shmseg->shm_perm.key = uap->key;
495 shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
496 shm_handle = kmalloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
497 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
500 * We make sure that we have allocated a pager before we need
504 shm_handle->shm_object =
505 phys_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0);
507 shm_handle->shm_object =
508 swap_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0);
510 vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
511 vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
513 shmseg->shm_internal = shm_handle;
514 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
515 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
516 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
517 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
518 shmseg->shm_segsz = uap->size;
519 shmseg->shm_cpid = p->p_pid;
520 shmseg->shm_lpid = shmseg->shm_nattch = 0;
521 shmseg->shm_atime = shmseg->shm_dtime = 0;
522 shmseg->shm_ctime = time_second;
523 shm_committed += btoc(size);
527 * If a physical mapping is desired and we have a ton of free pages
528 * we pre-allocate the pages here in order to avoid on-the-fly
529 * allocation later. This has a big effect on database warm-up
530 * times since DFly supports concurrent page faults coming from the
531 * same VM object for pages which already exist.
533 * This can hang the kernel for a while so only do it if shm_use_phys
534 * is set to 2 or higher.
536 if (shm_use_phys > 1) {
537 vm_pindex_t pi, pmax;
540 pmax = round_page(shmseg->shm_segsz) >> PAGE_SHIFT;
541 vm_object_hold(shm_handle->shm_object);
542 if (pmax > vmstats.v_free_count)
543 pmax = vmstats.v_free_count;
544 for (pi = 0; pi < pmax; ++pi) {
545 m = vm_page_grab(shm_handle->shm_object, pi,
546 VM_ALLOC_SYSTEM | VM_ALLOC_NULL_OK |
550 vm_pager_get_page(shm_handle->shm_object, &m, 1);
555 vm_object_drop(shm_handle->shm_object);
558 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
560 * Somebody else wanted this key while we were asleep. Wake
563 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
564 wakeup((caddr_t)shmseg);
566 uap->sysmsg_result = shmid;
574 sys_shmget(struct shmget_args *uap)
576 struct thread *td = curthread;
577 struct proc *p = td->td_proc;
578 int segnum, mode, error;
580 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
583 mode = uap->shmflg & ACCESSPERMS;
586 if (uap->key != IPC_PRIVATE) {
588 segnum = shm_find_segment_by_key(uap->key);
590 error = shmget_existing(p, uap, mode, segnum);
595 if ((uap->shmflg & IPC_CREAT) == 0) {
600 error = shmget_allocate_segment(p, uap, mode);
607 shmfork(struct proc *p1, struct proc *p2)
609 struct shmmap_state *shmmap_s;
614 size = shminfo.shmseg * sizeof(struct shmmap_state);
615 shmmap_s = kmalloc(size, M_SHM, M_WAITOK);
616 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
617 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
618 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
619 if (shmmap_s->shmid != -1)
620 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
626 shmexit(struct vmspace *vm)
628 struct shmmap_state *base, *shm;
631 if ((base = (struct shmmap_state *)vm->vm_shm) != NULL) {
634 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
635 if (shm->shmid != -1)
636 shm_delete_mapping(vm, shm);
647 struct shmid_ds *newsegs;
649 if (shmalloced >= shminfo.shmmni)
652 newsegs = kmalloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
653 for (i = 0; i < shmalloced; i++)
654 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
655 for (; i < shminfo.shmmni; i++) {
656 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
657 shmsegs[i].shm_perm.seq = 0;
659 kfree(shmsegs, M_SHM);
661 shmalloced = shminfo.shmmni;
670 * If not overridden by a tunable set the maximum shm to
671 * 2/3 of main memory.
673 if (shminfo.shmall == 0)
674 shminfo.shmall = (size_t)vmstats.v_page_count * 2 / 3;
676 shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
677 shmalloced = shminfo.shmmni;
678 shmsegs = kmalloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
679 for (i = 0; i < shmalloced; i++) {
680 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
681 shmsegs[i].shm_perm.seq = 0;
687 SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL);