sysv_shm.c revision 1.119 1 /* $NetBSD: sysv_shm.c,v 1.119 2011/05/13 22:22:55 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Mindaugas Rasiukevicius.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1994 Adam Glass and Charles M. Hannum. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by Adam Glass and Charles M.
47 * Hannum.
48 * 4. The names of the authors may not be used to endorse or promote products
49 * derived from this software without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.119 2011/05/13 22:22:55 rmind Exp $");
65
66 #define SYSVSHM
67
68 #include <sys/param.h>
69 #include <sys/kernel.h>
70 #include <sys/kmem.h>
71 #include <sys/shm.h>
72 #include <sys/mutex.h>
73 #include <sys/mman.h>
74 #include <sys/stat.h>
75 #include <sys/sysctl.h>
76 #include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
77 #include <sys/syscallargs.h>
78 #include <sys/queue.h>
79 #include <sys/kauth.h>
80
81 #include <uvm/uvm_extern.h>
82 #include <uvm/uvm_object.h>
83
84 struct shmmap_entry {
85 SLIST_ENTRY(shmmap_entry) next;
86 vaddr_t va;
87 int shmid;
88 };
89
90 int shm_nused __cacheline_aligned;
91 struct shmid_ds * shmsegs __read_mostly;
92
93 static kmutex_t shm_lock __cacheline_aligned;
94 static kcondvar_t * shm_cv __cacheline_aligned;
95 static int shm_last_free __cacheline_aligned;
96 static size_t shm_committed __cacheline_aligned;
97 static int shm_use_phys __read_mostly;
98
99 static kcondvar_t shm_realloc_cv;
100 static bool shm_realloc_state;
101 static u_int shm_realloc_disable;
102
103 struct shmmap_state {
104 unsigned int nitems;
105 unsigned int nrefs;
106 SLIST_HEAD(, shmmap_entry) entries;
107 };
108
109 #ifdef SHMDEBUG
110 #define SHMPRINTF(a) printf a
111 #else
112 #define SHMPRINTF(a)
113 #endif
114
115 static int shmrealloc(int);
116
117 /*
118 * Find the shared memory segment by the identifier.
119 * => must be called with shm_lock held;
120 */
121 static struct shmid_ds *
122 shm_find_segment_by_shmid(int shmid)
123 {
124 int segnum;
125 struct shmid_ds *shmseg;
126
127 KASSERT(mutex_owned(&shm_lock));
128
129 segnum = IPCID_TO_IX(shmid);
130 if (segnum < 0 || segnum >= shminfo.shmmni)
131 return NULL;
132 shmseg = &shmsegs[segnum];
133 if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0)
134 return NULL;
135 if ((shmseg->shm_perm.mode &
136 (SHMSEG_REMOVED|SHMSEG_RMLINGER)) == SHMSEG_REMOVED)
137 return NULL;
138 if (shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid))
139 return NULL;
140
141 return shmseg;
142 }
143
144 /*
145 * Free memory segment.
146 * => must be called with shm_lock held;
147 */
148 static void
149 shm_free_segment(int segnum)
150 {
151 struct shmid_ds *shmseg;
152 size_t size;
153 bool wanted;
154
155 KASSERT(mutex_owned(&shm_lock));
156
157 shmseg = &shmsegs[segnum];
158 SHMPRINTF(("shm freeing key 0x%lx seq 0x%x\n",
159 shmseg->shm_perm._key, shmseg->shm_perm._seq));
160
161 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
162 wanted = (shmseg->shm_perm.mode & SHMSEG_WANTED);
163
164 shmseg->_shm_internal = NULL;
165 shm_committed -= btoc(size);
166 shm_nused--;
167 shmseg->shm_perm.mode = SHMSEG_FREE;
168 shm_last_free = segnum;
169 if (wanted == true)
170 cv_broadcast(&shm_cv[segnum]);
171 }
172
173 /*
174 * Delete entry from the shm map.
175 * => must be called with shm_lock held;
176 */
177 static struct uvm_object *
178 shm_delete_mapping(struct shmmap_state *shmmap_s,
179 struct shmmap_entry *shmmap_se)
180 {
181 struct uvm_object *uobj = NULL;
182 struct shmid_ds *shmseg;
183 int segnum;
184
185 KASSERT(mutex_owned(&shm_lock));
186
187 segnum = IPCID_TO_IX(shmmap_se->shmid);
188 shmseg = &shmsegs[segnum];
189 SLIST_REMOVE(&shmmap_s->entries, shmmap_se, shmmap_entry, next);
190 shmmap_s->nitems--;
191 shmseg->shm_dtime = time_second;
192 if ((--shmseg->shm_nattch <= 0) &&
193 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
194 uobj = shmseg->_shm_internal;
195 shm_free_segment(segnum);
196 }
197
198 return uobj;
199 }
200
201 /*
202 * Get a non-shared shm map for that vmspace. Note, that memory
203 * allocation might be performed with lock held.
204 */
205 static struct shmmap_state *
206 shmmap_getprivate(struct proc *p)
207 {
208 struct shmmap_state *oshmmap_s, *shmmap_s;
209 struct shmmap_entry *oshmmap_se, *shmmap_se;
210
211 KASSERT(mutex_owned(&shm_lock));
212
213 /* 1. A shm map with refcnt = 1, used by ourselves, thus return */
214 oshmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
215 if (oshmmap_s && oshmmap_s->nrefs == 1)
216 return oshmmap_s;
217
218 /* 2. No shm map preset - create a fresh one */
219 shmmap_s = kmem_zalloc(sizeof(struct shmmap_state), KM_SLEEP);
220 shmmap_s->nrefs = 1;
221 SLIST_INIT(&shmmap_s->entries);
222 p->p_vmspace->vm_shm = (void *)shmmap_s;
223
224 if (oshmmap_s == NULL)
225 return shmmap_s;
226
227 SHMPRINTF(("shmmap_getprivate: vm %p split (%d entries), was used by %d\n",
228 p->p_vmspace, oshmmap_s->nitems, oshmmap_s->nrefs));
229
230 /* 3. A shared shm map, copy to a fresh one and adjust refcounts */
231 SLIST_FOREACH(oshmmap_se, &oshmmap_s->entries, next) {
232 shmmap_se = kmem_alloc(sizeof(struct shmmap_entry), KM_SLEEP);
233 shmmap_se->va = oshmmap_se->va;
234 shmmap_se->shmid = oshmmap_se->shmid;
235 SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
236 }
237 shmmap_s->nitems = oshmmap_s->nitems;
238 oshmmap_s->nrefs--;
239
240 return shmmap_s;
241 }
242
243 /*
244 * Lock/unlock the memory.
245 * => must be called with shm_lock held;
246 * => called from one place, thus, inline;
247 */
248 static inline int
249 shm_memlock(struct lwp *l, struct shmid_ds *shmseg, int shmid, int cmd)
250 {
251 struct proc *p = l->l_proc;
252 struct shmmap_entry *shmmap_se;
253 struct shmmap_state *shmmap_s;
254 size_t size;
255 int error;
256
257 KASSERT(mutex_owned(&shm_lock));
258 shmmap_s = shmmap_getprivate(p);
259
260 /* Find our shared memory address by shmid */
261 SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next) {
262 if (shmmap_se->shmid != shmid)
263 continue;
264
265 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
266
267 if (cmd == SHM_LOCK &&
268 (shmseg->shm_perm.mode & SHMSEG_WIRED) == 0) {
269 /* Wire the object and map, then tag it */
270 error = uobj_wirepages(shmseg->_shm_internal, 0, size);
271 if (error)
272 return EIO;
273 error = uvm_map_pageable(&p->p_vmspace->vm_map,
274 shmmap_se->va, shmmap_se->va + size, false, 0);
275 if (error) {
276 uobj_unwirepages(shmseg->_shm_internal, 0, size);
277 if (error == EFAULT)
278 error = ENOMEM;
279 return error;
280 }
281 shmseg->shm_perm.mode |= SHMSEG_WIRED;
282
283 } else if (cmd == SHM_UNLOCK &&
284 (shmseg->shm_perm.mode & SHMSEG_WIRED) != 0) {
285 /* Unwire the object and map, then untag it */
286 uobj_unwirepages(shmseg->_shm_internal, 0, size);
287 error = uvm_map_pageable(&p->p_vmspace->vm_map,
288 shmmap_se->va, shmmap_se->va + size, true, 0);
289 if (error)
290 return EIO;
291 shmseg->shm_perm.mode &= ~SHMSEG_WIRED;
292 }
293 }
294
295 return 0;
296 }
297
298 /*
299 * Unmap shared memory.
300 */
301 int
302 sys_shmdt(struct lwp *l, const struct sys_shmdt_args *uap, register_t *retval)
303 {
304 /* {
305 syscallarg(const void *) shmaddr;
306 } */
307 struct proc *p = l->l_proc;
308 struct shmmap_state *shmmap_s1, *shmmap_s;
309 struct shmmap_entry *shmmap_se;
310 struct uvm_object *uobj;
311 struct shmid_ds *shmseg;
312 size_t size;
313
314 mutex_enter(&shm_lock);
315 /* In case of reallocation, we will wait for completion */
316 while (__predict_false(shm_realloc_state))
317 cv_wait(&shm_realloc_cv, &shm_lock);
318
319 shmmap_s1 = (struct shmmap_state *)p->p_vmspace->vm_shm;
320 if (shmmap_s1 == NULL) {
321 mutex_exit(&shm_lock);
322 return EINVAL;
323 }
324
325 /* Find the map entry */
326 SLIST_FOREACH(shmmap_se, &shmmap_s1->entries, next)
327 if (shmmap_se->va == (vaddr_t)SCARG(uap, shmaddr))
328 break;
329 if (shmmap_se == NULL) {
330 mutex_exit(&shm_lock);
331 return EINVAL;
332 }
333
334 shmmap_s = shmmap_getprivate(p);
335 if (shmmap_s != shmmap_s1) {
336 /* Map has been copied, lookup entry in new map */
337 SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
338 if (shmmap_se->va == (vaddr_t)SCARG(uap, shmaddr))
339 break;
340 if (shmmap_se == NULL) {
341 mutex_exit(&shm_lock);
342 return EINVAL;
343 }
344 }
345
346 SHMPRINTF(("shmdt: vm %p: remove %d @%lx\n",
347 p->p_vmspace, shmmap_se->shmid, shmmap_se->va));
348
349 /* Delete the entry from shm map */
350 uobj = shm_delete_mapping(shmmap_s, shmmap_se);
351 shmseg = &shmsegs[IPCID_TO_IX(shmmap_se->shmid)];
352 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
353 mutex_exit(&shm_lock);
354
355 uvm_deallocate(&p->p_vmspace->vm_map, shmmap_se->va, size);
356 if (uobj != NULL) {
357 uao_detach(uobj);
358 }
359 kmem_free(shmmap_se, sizeof(struct shmmap_entry));
360
361 return 0;
362 }
363
364 /*
365 * Map shared memory.
366 */
367 int
368 sys_shmat(struct lwp *l, const struct sys_shmat_args *uap, register_t *retval)
369 {
370 /* {
371 syscallarg(int) shmid;
372 syscallarg(const void *) shmaddr;
373 syscallarg(int) shmflg;
374 } */
375 int error, flags = 0;
376 struct proc *p = l->l_proc;
377 kauth_cred_t cred = l->l_cred;
378 struct shmid_ds *shmseg;
379 struct shmmap_state *shmmap_s;
380 struct shmmap_entry *shmmap_se;
381 struct uvm_object *uobj;
382 struct vmspace *vm;
383 vaddr_t attach_va;
384 vm_prot_t prot;
385 vsize_t size;
386
387 /* Allocate a new map entry and set it */
388 shmmap_se = kmem_alloc(sizeof(struct shmmap_entry), KM_SLEEP);
389 shmmap_se->shmid = SCARG(uap, shmid);
390
391 mutex_enter(&shm_lock);
392 /* In case of reallocation, we will wait for completion */
393 while (__predict_false(shm_realloc_state))
394 cv_wait(&shm_realloc_cv, &shm_lock);
395
396 shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
397 if (shmseg == NULL) {
398 error = EINVAL;
399 goto err;
400 }
401 error = ipcperm(cred, &shmseg->shm_perm,
402 (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
403 if (error)
404 goto err;
405
406 vm = p->p_vmspace;
407 shmmap_s = (struct shmmap_state *)vm->vm_shm;
408 if (shmmap_s && shmmap_s->nitems >= shminfo.shmseg) {
409 error = EMFILE;
410 goto err;
411 }
412
413 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
414 prot = VM_PROT_READ;
415 if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
416 prot |= VM_PROT_WRITE;
417 if (SCARG(uap, shmaddr)) {
418 flags |= UVM_FLAG_FIXED;
419 if (SCARG(uap, shmflg) & SHM_RND)
420 attach_va =
421 (vaddr_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
422 else if (((vaddr_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
423 attach_va = (vaddr_t)SCARG(uap, shmaddr);
424 else {
425 error = EINVAL;
426 goto err;
427 }
428 } else {
429 /* This is just a hint to uvm_map() about where to put it. */
430 attach_va = p->p_emul->e_vm_default_addr(p,
431 (vaddr_t)vm->vm_daddr, size);
432 }
433
434 /*
435 * Create a map entry, add it to the list and increase the counters.
436 * The lock will be dropped before the mapping, disable reallocation.
437 */
438 shmmap_s = shmmap_getprivate(p);
439 SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
440 shmmap_s->nitems++;
441 shmseg->shm_lpid = p->p_pid;
442 shmseg->shm_nattch++;
443 shm_realloc_disable++;
444 mutex_exit(&shm_lock);
445
446 /*
447 * Add a reference to the memory object, map it to the
448 * address space, and lock the memory, if needed.
449 */
450 uobj = shmseg->_shm_internal;
451 uao_reference(uobj);
452 error = uvm_map(&vm->vm_map, &attach_va, size, uobj, 0, 0,
453 UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, flags));
454 if (error)
455 goto err_detach;
456 if (shm_use_phys || (shmseg->shm_perm.mode & SHMSEG_WIRED)) {
457 error = uvm_map_pageable(&vm->vm_map, attach_va,
458 attach_va + size, false, 0);
459 if (error) {
460 if (error == EFAULT)
461 error = ENOMEM;
462 uvm_deallocate(&vm->vm_map, attach_va, size);
463 goto err_detach;
464 }
465 }
466
467 /* Set the new address, and update the time */
468 mutex_enter(&shm_lock);
469 shmmap_se->va = attach_va;
470 shmseg->shm_atime = time_second;
471 shm_realloc_disable--;
472 retval[0] = attach_va;
473 SHMPRINTF(("shmat: vm %p: add %d @%lx\n",
474 p->p_vmspace, shmmap_se->shmid, attach_va));
475 err:
476 cv_broadcast(&shm_realloc_cv);
477 mutex_exit(&shm_lock);
478 if (error && shmmap_se) {
479 kmem_free(shmmap_se, sizeof(struct shmmap_entry));
480 }
481 return error;
482
483 err_detach:
484 uao_detach(uobj);
485 mutex_enter(&shm_lock);
486 uobj = shm_delete_mapping(shmmap_s, shmmap_se);
487 shm_realloc_disable--;
488 cv_broadcast(&shm_realloc_cv);
489 mutex_exit(&shm_lock);
490 if (uobj != NULL) {
491 uao_detach(uobj);
492 }
493 kmem_free(shmmap_se, sizeof(struct shmmap_entry));
494 return error;
495 }
496
497 /*
498 * Shared memory control operations.
499 */
500 int
501 sys___shmctl50(struct lwp *l, const struct sys___shmctl50_args *uap,
502 register_t *retval)
503 {
504 /* {
505 syscallarg(int) shmid;
506 syscallarg(int) cmd;
507 syscallarg(struct shmid_ds *) buf;
508 } */
509 struct shmid_ds shmbuf;
510 int cmd, error;
511
512 cmd = SCARG(uap, cmd);
513 if (cmd == IPC_SET) {
514 error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
515 if (error)
516 return error;
517 }
518
519 error = shmctl1(l, SCARG(uap, shmid), cmd,
520 (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
521
522 if (error == 0 && cmd == IPC_STAT)
523 error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
524
525 return error;
526 }
527
528 int
529 shmctl1(struct lwp *l, int shmid, int cmd, struct shmid_ds *shmbuf)
530 {
531 struct uvm_object *uobj = NULL;
532 kauth_cred_t cred = l->l_cred;
533 struct shmid_ds *shmseg;
534 int error = 0;
535
536 mutex_enter(&shm_lock);
537 /* In case of reallocation, we will wait for completion */
538 while (__predict_false(shm_realloc_state))
539 cv_wait(&shm_realloc_cv, &shm_lock);
540
541 shmseg = shm_find_segment_by_shmid(shmid);
542 if (shmseg == NULL) {
543 mutex_exit(&shm_lock);
544 return EINVAL;
545 }
546
547 switch (cmd) {
548 case IPC_STAT:
549 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
550 break;
551 memcpy(shmbuf, shmseg, sizeof(struct shmid_ds));
552 break;
553 case IPC_SET:
554 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
555 break;
556 shmseg->shm_perm.uid = shmbuf->shm_perm.uid;
557 shmseg->shm_perm.gid = shmbuf->shm_perm.gid;
558 shmseg->shm_perm.mode =
559 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
560 (shmbuf->shm_perm.mode & ACCESSPERMS);
561 shmseg->shm_ctime = time_second;
562 break;
563 case IPC_RMID:
564 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
565 break;
566 shmseg->shm_perm._key = IPC_PRIVATE;
567 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
568 if (shmseg->shm_nattch <= 0) {
569 uobj = shmseg->_shm_internal;
570 shm_free_segment(IPCID_TO_IX(shmid));
571 }
572 break;
573 case SHM_LOCK:
574 case SHM_UNLOCK:
575 if ((error = kauth_authorize_generic(cred,
576 KAUTH_GENERIC_ISSUSER, NULL)) != 0)
577 break;
578 error = shm_memlock(l, shmseg, shmid, cmd);
579 break;
580 default:
581 error = EINVAL;
582 }
583
584 mutex_exit(&shm_lock);
585 if (uobj != NULL)
586 uao_detach(uobj);
587 return error;
588 }
589
590 /*
591 * Try to take an already existing segment.
592 * => must be called with shm_lock held;
593 * => called from one place, thus, inline;
594 */
595 static inline int
596 shmget_existing(struct lwp *l, const struct sys_shmget_args *uap, int mode,
597 register_t *retval)
598 {
599 struct shmid_ds *shmseg;
600 kauth_cred_t cred = l->l_cred;
601 int segnum, error;
602 again:
603 KASSERT(mutex_owned(&shm_lock));
604
605 /* Find segment by key */
606 for (segnum = 0; segnum < shminfo.shmmni; segnum++)
607 if ((shmsegs[segnum].shm_perm.mode & SHMSEG_ALLOCATED) &&
608 shmsegs[segnum].shm_perm._key == SCARG(uap, key))
609 break;
610 if (segnum == shminfo.shmmni) {
611 /* Not found */
612 return -1;
613 }
614
615 shmseg = &shmsegs[segnum];
616 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
617 /*
618 * This segment is in the process of being allocated. Wait
619 * until it's done, and look the key up again (in case the
620 * allocation failed or it was freed).
621 */
622 shmseg->shm_perm.mode |= SHMSEG_WANTED;
623 error = cv_wait_sig(&shm_cv[segnum], &shm_lock);
624 if (error)
625 return error;
626 goto again;
627 }
628
629 /*
630 * First check the flags, to generate a useful error when a
631 * segment already exists.
632 */
633 if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
634 (IPC_CREAT | IPC_EXCL))
635 return EEXIST;
636
637 /* Check the permission and segment size. */
638 error = ipcperm(cred, &shmseg->shm_perm, mode);
639 if (error)
640 return error;
641 if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
642 return EINVAL;
643
644 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
645 return 0;
646 }
647
648 int
649 sys_shmget(struct lwp *l, const struct sys_shmget_args *uap, register_t *retval)
650 {
651 /* {
652 syscallarg(key_t) key;
653 syscallarg(size_t) size;
654 syscallarg(int) shmflg;
655 } */
656 struct shmid_ds *shmseg;
657 kauth_cred_t cred = l->l_cred;
658 key_t key = SCARG(uap, key);
659 size_t size;
660 int error, mode, segnum;
661 bool lockmem;
662
663 mode = SCARG(uap, shmflg) & ACCESSPERMS;
664 if (SCARG(uap, shmflg) & _SHM_RMLINGER)
665 mode |= SHMSEG_RMLINGER;
666
667 SHMPRINTF(("shmget: key 0x%lx size 0x%zx shmflg 0x%x mode 0x%x\n",
668 SCARG(uap, key), SCARG(uap, size), SCARG(uap, shmflg), mode));
669
670 mutex_enter(&shm_lock);
671 /* In case of reallocation, we will wait for completion */
672 while (__predict_false(shm_realloc_state))
673 cv_wait(&shm_realloc_cv, &shm_lock);
674
675 if (key != IPC_PRIVATE) {
676 error = shmget_existing(l, uap, mode, retval);
677 if (error != -1) {
678 mutex_exit(&shm_lock);
679 return error;
680 }
681 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0) {
682 mutex_exit(&shm_lock);
683 return ENOENT;
684 }
685 }
686 error = 0;
687
688 /*
689 * Check the for the limits.
690 */
691 size = SCARG(uap, size);
692 if (size < shminfo.shmmin || size > shminfo.shmmax) {
693 mutex_exit(&shm_lock);
694 return EINVAL;
695 }
696 if (shm_nused >= shminfo.shmmni) {
697 mutex_exit(&shm_lock);
698 return ENOSPC;
699 }
700 size = (size + PGOFSET) & ~PGOFSET;
701 if (shm_committed + btoc(size) > shminfo.shmall) {
702 mutex_exit(&shm_lock);
703 return ENOMEM;
704 }
705
706 /* Find the first available segment */
707 if (shm_last_free < 0) {
708 for (segnum = 0; segnum < shminfo.shmmni; segnum++)
709 if (shmsegs[segnum].shm_perm.mode & SHMSEG_FREE)
710 break;
711 KASSERT(segnum < shminfo.shmmni);
712 } else {
713 segnum = shm_last_free;
714 shm_last_free = -1;
715 }
716
717 /*
718 * Initialize the segment.
719 * We will drop the lock while allocating the memory, thus mark the
720 * segment present, but removed, that no other thread could take it.
721 * Also, disable reallocation, while lock is dropped.
722 */
723 shmseg = &shmsegs[segnum];
724 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
725 shm_committed += btoc(size);
726 shm_nused++;
727 lockmem = shm_use_phys;
728 shm_realloc_disable++;
729 mutex_exit(&shm_lock);
730
731 /* Allocate the memory object and lock it if needed */
732 shmseg->_shm_internal = uao_create(size, 0);
733 if (lockmem) {
734 /* Wire the pages and tag it */
735 error = uobj_wirepages(shmseg->_shm_internal, 0, size);
736 if (error) {
737 uao_detach(shmseg->_shm_internal);
738 mutex_enter(&shm_lock);
739 shm_free_segment(segnum);
740 shm_realloc_disable--;
741 mutex_exit(&shm_lock);
742 return error;
743 }
744 }
745
746 /*
747 * Please note, while segment is marked, there are no need to hold the
748 * lock, while setting it (except shm_perm.mode).
749 */
750 shmseg->shm_perm._key = SCARG(uap, key);
751 shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
752 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
753
754 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = kauth_cred_geteuid(cred);
755 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = kauth_cred_getegid(cred);
756 shmseg->shm_segsz = SCARG(uap, size);
757 shmseg->shm_cpid = l->l_proc->p_pid;
758 shmseg->shm_lpid = shmseg->shm_nattch = 0;
759 shmseg->shm_atime = shmseg->shm_dtime = 0;
760 shmseg->shm_ctime = time_second;
761
762 /*
763 * Segment is initialized.
764 * Enter the lock, mark as allocated, and notify waiters (if any).
765 * Also, unmark the state of reallocation.
766 */
767 mutex_enter(&shm_lock);
768 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
769 (mode & (ACCESSPERMS | SHMSEG_RMLINGER)) |
770 SHMSEG_ALLOCATED | (lockmem ? SHMSEG_WIRED : 0);
771 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
772 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
773 cv_broadcast(&shm_cv[segnum]);
774 }
775 shm_realloc_disable--;
776 cv_broadcast(&shm_realloc_cv);
777 mutex_exit(&shm_lock);
778
779 return error;
780 }
781
782 void
783 shmfork(struct vmspace *vm1, struct vmspace *vm2)
784 {
785 struct shmmap_state *shmmap_s;
786 struct shmmap_entry *shmmap_se;
787
788 SHMPRINTF(("shmfork %p->%p\n", vm1, vm2));
789 mutex_enter(&shm_lock);
790 vm2->vm_shm = vm1->vm_shm;
791 if (vm1->vm_shm) {
792 shmmap_s = (struct shmmap_state *)vm1->vm_shm;
793 SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
794 shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch++;
795 shmmap_s->nrefs++;
796 }
797 mutex_exit(&shm_lock);
798 }
799
800 void
801 shmexit(struct vmspace *vm)
802 {
803 struct shmmap_state *shmmap_s;
804 struct shmmap_entry *shmmap_se;
805
806 mutex_enter(&shm_lock);
807 shmmap_s = (struct shmmap_state *)vm->vm_shm;
808 if (shmmap_s == NULL) {
809 mutex_exit(&shm_lock);
810 return;
811 }
812 vm->vm_shm = NULL;
813
814 if (--shmmap_s->nrefs > 0) {
815 SHMPRINTF(("shmexit: vm %p drop ref (%d entries), refs = %d\n",
816 vm, shmmap_s->nitems, shmmap_s->nrefs));
817 SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next) {
818 shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch--;
819 }
820 mutex_exit(&shm_lock);
821 return;
822 }
823
824 SHMPRINTF(("shmexit: vm %p cleanup (%d entries)\n", vm, shmmap_s->nitems));
825 if (shmmap_s->nitems == 0) {
826 mutex_exit(&shm_lock);
827 kmem_free(shmmap_s, sizeof(struct shmmap_state));
828 return;
829 }
830
831 /*
832 * Delete the entry from shm map.
833 */
834 for (;;) {
835 struct shmid_ds *shmseg;
836 struct uvm_object *uobj;
837 size_t sz;
838
839 shmmap_se = SLIST_FIRST(&shmmap_s->entries);
840 KASSERT(shmmap_se != NULL);
841
842 shmseg = &shmsegs[IPCID_TO_IX(shmmap_se->shmid)];
843 sz = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
844 /* shm_delete_mapping() removes from the list. */
845 uobj = shm_delete_mapping(shmmap_s, shmmap_se);
846 mutex_exit(&shm_lock);
847
848 uvm_deallocate(&vm->vm_map, shmmap_se->va, sz);
849 if (uobj != NULL) {
850 uao_detach(uobj);
851 }
852 kmem_free(shmmap_se, sizeof(struct shmmap_entry));
853
854 if (SLIST_EMPTY(&shmmap_s->entries)) {
855 break;
856 }
857 mutex_enter(&shm_lock);
858 KASSERT(!SLIST_EMPTY(&shmmap_s->entries));
859 }
860 kmem_free(shmmap_s, sizeof(struct shmmap_state));
861 }
862
863 static int
864 shmrealloc(int newshmni)
865 {
866 vaddr_t v;
867 struct shmid_ds *oldshmsegs, *newshmsegs;
868 kcondvar_t *newshm_cv, *oldshm_cv;
869 size_t sz;
870 int i, lsegid, oldshmni;
871
872 if (newshmni < 1)
873 return EINVAL;
874
875 /* Allocate new memory area */
876 sz = ALIGN(newshmni * sizeof(struct shmid_ds)) +
877 ALIGN(newshmni * sizeof(kcondvar_t));
878 v = uvm_km_alloc(kernel_map, round_page(sz), 0,
879 UVM_KMF_WIRED|UVM_KMF_ZERO);
880 if (v == 0)
881 return ENOMEM;
882
883 mutex_enter(&shm_lock);
884 while (shm_realloc_state || shm_realloc_disable)
885 cv_wait(&shm_realloc_cv, &shm_lock);
886
887 /*
888 * Get the number of last segment. Fail we are trying to
889 * reallocate less memory than we use.
890 */
891 lsegid = 0;
892 for (i = 0; i < shminfo.shmmni; i++)
893 if ((shmsegs[i].shm_perm.mode & SHMSEG_FREE) == 0)
894 lsegid = i;
895 if (lsegid >= newshmni) {
896 mutex_exit(&shm_lock);
897 uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
898 return EBUSY;
899 }
900 shm_realloc_state = true;
901
902 newshmsegs = (void *)v;
903 newshm_cv = (void *)((uintptr_t)newshmsegs +
904 ALIGN(newshmni * sizeof(struct shmid_ds)));
905
906 /* Copy all memory to the new area */
907 for (i = 0; i < shm_nused; i++)
908 (void)memcpy(&newshmsegs[i], &shmsegs[i],
909 sizeof(newshmsegs[0]));
910
911 /* Mark as free all new segments, if there is any */
912 for (; i < newshmni; i++) {
913 cv_init(&newshm_cv[i], "shmwait");
914 newshmsegs[i].shm_perm.mode = SHMSEG_FREE;
915 newshmsegs[i].shm_perm._seq = 0;
916 }
917
918 oldshmsegs = shmsegs;
919 oldshmni = shminfo.shmmni;
920 shminfo.shmmni = newshmni;
921 shmsegs = newshmsegs;
922 shm_cv = newshm_cv;
923
924 /* Reallocation completed - notify all waiters, if any */
925 shm_realloc_state = false;
926 cv_broadcast(&shm_realloc_cv);
927 mutex_exit(&shm_lock);
928
929 /* Release now unused resources. */
930 oldshm_cv = (void *)((uintptr_t)oldshmsegs +
931 ALIGN(oldshmni * sizeof(struct shmid_ds)));
932 for (i = 0; i < oldshmni; i++)
933 cv_destroy(&oldshm_cv[i]);
934
935 sz = ALIGN(oldshmni * sizeof(struct shmid_ds)) +
936 ALIGN(oldshmni * sizeof(kcondvar_t));
937 uvm_km_free(kernel_map, (vaddr_t)oldshmsegs, sz, UVM_KMF_WIRED);
938
939 return 0;
940 }
941
942 void
943 shminit(void)
944 {
945 vaddr_t v;
946 size_t sz;
947 int i;
948
949 mutex_init(&shm_lock, MUTEX_DEFAULT, IPL_NONE);
950 cv_init(&shm_realloc_cv, "shmrealc");
951
952 /* Allocate the wired memory for our structures */
953 sz = ALIGN(shminfo.shmmni * sizeof(struct shmid_ds)) +
954 ALIGN(shminfo.shmmni * sizeof(kcondvar_t));
955 v = uvm_km_alloc(kernel_map, round_page(sz), 0,
956 UVM_KMF_WIRED|UVM_KMF_ZERO);
957 if (v == 0)
958 panic("sysv_shm: cannot allocate memory");
959 shmsegs = (void *)v;
960 shm_cv = (void *)((uintptr_t)shmsegs +
961 ALIGN(shminfo.shmmni * sizeof(struct shmid_ds)));
962
963 if (shminfo.shmmax == 0)
964 shminfo.shmmax = max(physmem / 4, 1024) * PAGE_SIZE;
965 else
966 shminfo.shmmax *= PAGE_SIZE;
967 shminfo.shmall = shminfo.shmmax / PAGE_SIZE;
968
969 for (i = 0; i < shminfo.shmmni; i++) {
970 cv_init(&shm_cv[i], "shmwait");
971 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
972 shmsegs[i].shm_perm._seq = 0;
973 }
974 shm_last_free = 0;
975 shm_nused = 0;
976 shm_committed = 0;
977 shm_realloc_disable = 0;
978 shm_realloc_state = false;
979 }
980
981 static int
982 sysctl_ipc_shmmni(SYSCTLFN_ARGS)
983 {
984 int newsize, error;
985 struct sysctlnode node;
986 node = *rnode;
987 node.sysctl_data = &newsize;
988
989 newsize = shminfo.shmmni;
990 error = sysctl_lookup(SYSCTLFN_CALL(&node));
991 if (error || newp == NULL)
992 return error;
993
994 sysctl_unlock();
995 error = shmrealloc(newsize);
996 sysctl_relock();
997 return error;
998 }
999
1000 static int
1001 sysctl_ipc_shmmaxpgs(SYSCTLFN_ARGS)
1002 {
1003 uint32_t newsize;
1004 int error;
1005 struct sysctlnode node;
1006 node = *rnode;
1007 node.sysctl_data = &newsize;
1008
1009 newsize = shminfo.shmall;
1010 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1011 if (error || newp == NULL)
1012 return error;
1013
1014 if (newsize < 1)
1015 return EINVAL;
1016
1017 shminfo.shmall = newsize;
1018 shminfo.shmmax = (uint64_t)shminfo.shmall * PAGE_SIZE;
1019
1020 return 0;
1021 }
1022
1023 static int
1024 sysctl_ipc_shmmax(SYSCTLFN_ARGS)
1025 {
1026 uint64_t newsize;
1027 int error;
1028 struct sysctlnode node;
1029 node = *rnode;
1030 node.sysctl_data = &newsize;
1031
1032 newsize = shminfo.shmmax;
1033 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1034 if (error || newp == NULL)
1035 return error;
1036
1037 if (newsize < PAGE_SIZE)
1038 return EINVAL;
1039
1040 shminfo.shmmax = round_page(newsize);
1041 shminfo.shmall = shminfo.shmmax >> PAGE_SHIFT;
1042
1043 return 0;
1044 }
1045
1046 SYSCTL_SETUP(sysctl_ipc_shm_setup, "sysctl kern.ipc subtree setup")
1047 {
1048
1049 sysctl_createv(clog, 0, NULL, NULL,
1050 CTLFLAG_PERMANENT,
1051 CTLTYPE_NODE, "kern", NULL,
1052 NULL, 0, NULL, 0,
1053 CTL_KERN, CTL_EOL);
1054 sysctl_createv(clog, 0, NULL, NULL,
1055 CTLFLAG_PERMANENT,
1056 CTLTYPE_NODE, "ipc",
1057 SYSCTL_DESCR("SysV IPC options"),
1058 NULL, 0, NULL, 0,
1059 CTL_KERN, KERN_SYSVIPC, CTL_EOL);
1060 sysctl_createv(clog, 0, NULL, NULL,
1061 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1062 CTLTYPE_QUAD, "shmmax",
1063 SYSCTL_DESCR("Max shared memory segment size in bytes"),
1064 sysctl_ipc_shmmax, 0, &shminfo.shmmax, 0,
1065 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMAX, CTL_EOL);
1066 sysctl_createv(clog, 0, NULL, NULL,
1067 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1068 CTLTYPE_INT, "shmmni",
1069 SYSCTL_DESCR("Max number of shared memory identifiers"),
1070 sysctl_ipc_shmmni, 0, &shminfo.shmmni, 0,
1071 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMNI, CTL_EOL);
1072 sysctl_createv(clog, 0, NULL, NULL,
1073 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1074 CTLTYPE_INT, "shmseg",
1075 SYSCTL_DESCR("Max shared memory segments per process"),
1076 NULL, 0, &shminfo.shmseg, 0,
1077 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMSEG, CTL_EOL);
1078 sysctl_createv(clog, 0, NULL, NULL,
1079 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1080 CTLTYPE_INT, "shmmaxpgs",
1081 SYSCTL_DESCR("Max amount of shared memory in pages"),
1082 sysctl_ipc_shmmaxpgs, 0, &shminfo.shmall, 0,
1083 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMAXPGS, CTL_EOL);
1084 sysctl_createv(clog, 0, NULL, NULL,
1085 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1086 CTLTYPE_INT, "shm_use_phys",
1087 SYSCTL_DESCR("Enable/disable locking of shared memory in "
1088 "physical memory"), NULL, 0, &shm_use_phys, 0,
1089 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMUSEPHYS, CTL_EOL);
1090 }
1091