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