sysv_shm.c revision 1.107.2.2 1 /* $NetBSD: sysv_shm.c,v 1.107.2.2 2008/05/14 01:35:14 wrstuden 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.107.2.2 2008/05/14 01:35:14 wrstuden 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/pool.h>
80 #include <sys/kauth.h>
81
82 #include <uvm/uvm_extern.h>
83 #include <uvm/uvm_object.h>
84
85 int shm_nused;
86 struct shmid_ds *shmsegs;
87
88 struct shmmap_entry {
89 SLIST_ENTRY(shmmap_entry) next;
90 vaddr_t va;
91 int shmid;
92 };
93
94 static kmutex_t shm_lock;
95 static kcondvar_t * shm_cv;
96 static struct pool shmmap_entry_pool;
97 static int shm_last_free, shm_use_phys;
98 static size_t shm_committed;
99
100 static kcondvar_t shm_realloc_cv;
101 static bool shm_realloc_state;
102 static u_int shm_realloc_disable;
103
104 struct shmmap_state {
105 unsigned int nitems;
106 unsigned int nrefs;
107 SLIST_HEAD(, shmmap_entry) entries;
108 };
109
110 #ifdef SHMDEBUG
111 #define SHMPRINTF(a) printf a
112 #else
113 #define SHMPRINTF(a)
114 #endif
115
116 static int shmrealloc(int);
117
118 /*
119 * Find the shared memory segment by the identifier.
120 * => must be called with shm_lock held;
121 */
122 static struct shmid_ds *
123 shm_find_segment_by_shmid(int shmid)
124 {
125 int segnum;
126 struct shmid_ds *shmseg;
127
128 KASSERT(mutex_owned(&shm_lock));
129
130 segnum = IPCID_TO_IX(shmid);
131 if (segnum < 0 || segnum >= shminfo.shmmni)
132 return NULL;
133 shmseg = &shmsegs[segnum];
134 if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0)
135 return NULL;
136 if ((shmseg->shm_perm.mode &
137 (SHMSEG_REMOVED|SHMSEG_RMLINGER)) == SHMSEG_REMOVED)
138 return NULL;
139 if (shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid))
140 return NULL;
141
142 return shmseg;
143 }
144
145 /*
146 * Free memory segment.
147 * => must be called with shm_lock held;
148 */
149 static void
150 shm_free_segment(int segnum)
151 {
152 struct shmid_ds *shmseg;
153 size_t size;
154 bool wanted;
155
156 KASSERT(mutex_owned(&shm_lock));
157
158 shmseg = &shmsegs[segnum];
159 SHMPRINTF(("shm freeing key 0x%lx seq 0x%x\n",
160 shmseg->shm_perm._key, shmseg->shm_perm._seq));
161
162 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
163 wanted = (shmseg->shm_perm.mode & SHMSEG_WANTED);
164
165 shmseg->_shm_internal = NULL;
166 shm_committed -= btoc(size);
167 shm_nused--;
168 shmseg->shm_perm.mode = SHMSEG_FREE;
169 shm_last_free = segnum;
170 if (wanted == true)
171 cv_broadcast(&shm_cv[segnum]);
172 }
173
174 /*
175 * Delete entry from the shm map.
176 * => must be called with shm_lock held;
177 */
178 static struct uvm_object *
179 shm_delete_mapping(struct shmmap_state *shmmap_s,
180 struct shmmap_entry *shmmap_se)
181 {
182 struct uvm_object *uobj = NULL;
183 struct shmid_ds *shmseg;
184 int segnum;
185
186 KASSERT(mutex_owned(&shm_lock));
187
188 segnum = IPCID_TO_IX(shmmap_se->shmid);
189 shmseg = &shmsegs[segnum];
190 SLIST_REMOVE(&shmmap_s->entries, shmmap_se, shmmap_entry, next);
191 shmmap_s->nitems--;
192 shmseg->shm_dtime = time_second;
193 if ((--shmseg->shm_nattch <= 0) &&
194 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
195 uobj = shmseg->_shm_internal;
196 shm_free_segment(segnum);
197 }
198
199 return uobj;
200 }
201
202 /*
203 * Get a non-shared shm map for that vmspace. Note, that memory
204 * allocation might be performed with lock held.
205 */
206 static struct shmmap_state *
207 shmmap_getprivate(struct proc *p)
208 {
209 struct shmmap_state *oshmmap_s, *shmmap_s;
210 struct shmmap_entry *oshmmap_se, *shmmap_se;
211
212 KASSERT(mutex_owned(&shm_lock));
213
214 /* 1. A shm map with refcnt = 1, used by ourselves, thus return */
215 oshmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
216 if (oshmmap_s && oshmmap_s->nrefs == 1)
217 return oshmmap_s;
218
219 /* 2. No shm map preset - create a fresh one */
220 shmmap_s = kmem_zalloc(sizeof(struct shmmap_state), KM_SLEEP);
221 shmmap_s->nrefs = 1;
222 SLIST_INIT(&shmmap_s->entries);
223 p->p_vmspace->vm_shm = (void *)shmmap_s;
224
225 if (oshmmap_s == NULL)
226 return shmmap_s;
227
228 SHMPRINTF(("shmmap_getprivate: vm %p split (%d entries), was used by %d\n",
229 p->p_vmspace, oshmmap_s->nitems, oshmmap_s->nrefs));
230
231 /* 3. A shared shm map, copy to a fresh one and adjust refcounts */
232 SLIST_FOREACH(oshmmap_se, &oshmmap_s->entries, next) {
233 shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
234 shmmap_se->va = oshmmap_se->va;
235 shmmap_se->shmid = oshmmap_se->shmid;
236 SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
237 }
238 shmmap_s->nitems = oshmmap_s->nitems;
239 oshmmap_s->nrefs--;
240
241 return shmmap_s;
242 }
243
244 /*
245 * Lock/unlock the memory.
246 * => must be called with shm_lock held;
247 * => called from one place, thus, inline;
248 */
249 static inline int
250 shm_memlock(struct lwp *l, struct shmid_ds *shmseg, int shmid, int cmd)
251 {
252 struct proc *p = l->l_proc;
253 struct shmmap_entry *shmmap_se;
254 struct shmmap_state *shmmap_s;
255 size_t size;
256 int error;
257
258 KASSERT(mutex_owned(&shm_lock));
259 shmmap_s = shmmap_getprivate(p);
260
261 /* Find our shared memory address by shmid */
262 SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next) {
263 if (shmmap_se->shmid != shmid)
264 continue;
265
266 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
267
268 if (cmd == SHM_LOCK &&
269 (shmseg->shm_perm.mode & SHMSEG_WIRED) == 0) {
270 /* Wire the object and map, then tag it */
271 error = uobj_wirepages(shmseg->_shm_internal, 0,
272 round_page(shmseg->shm_segsz));
273 if (error)
274 return EIO;
275 error = uvm_map_pageable(&p->p_vmspace->vm_map,
276 shmmap_se->va, shmmap_se->va + size, false, 0);
277 if (error) {
278 uobj_unwirepages(shmseg->_shm_internal, 0,
279 round_page(shmseg->shm_segsz));
280 if (error == EFAULT)
281 error = ENOMEM;
282 return error;
283 }
284 shmseg->shm_perm.mode |= SHMSEG_WIRED;
285
286 } else if (cmd == SHM_UNLOCK &&
287 (shmseg->shm_perm.mode & SHMSEG_WIRED) != 0) {
288 /* Unwire the object and map, then untag it */
289 uobj_unwirepages(shmseg->_shm_internal, 0,
290 round_page(shmseg->shm_segsz));
291 error = uvm_map_pageable(&p->p_vmspace->vm_map,
292 shmmap_se->va, shmmap_se->va + size, true, 0);
293 if (error)
294 return EIO;
295 shmseg->shm_perm.mode &= ~SHMSEG_WIRED;
296 }
297 }
298
299 return 0;
300 }
301
302 /*
303 * Unmap shared memory.
304 */
305 int
306 sys_shmdt(struct lwp *l, const struct sys_shmdt_args *uap, register_t *retval)
307 {
308 /* {
309 syscallarg(const void *) shmaddr;
310 } */
311 struct proc *p = l->l_proc;
312 struct shmmap_state *shmmap_s1, *shmmap_s;
313 struct shmmap_entry *shmmap_se;
314 struct uvm_object *uobj;
315 struct shmid_ds *shmseg;
316 size_t size;
317
318 mutex_enter(&shm_lock);
319 /* In case of reallocation, we will wait for completion */
320 while (__predict_false(shm_realloc_state))
321 cv_wait(&shm_realloc_cv, &shm_lock);
322
323 shmmap_s1 = (struct shmmap_state *)p->p_vmspace->vm_shm;
324 if (shmmap_s1 == NULL) {
325 mutex_exit(&shm_lock);
326 return EINVAL;
327 }
328
329 /* Find the map entry */
330 SLIST_FOREACH(shmmap_se, &shmmap_s1->entries, next)
331 if (shmmap_se->va == (vaddr_t)SCARG(uap, shmaddr))
332 break;
333 if (shmmap_se == NULL) {
334 mutex_exit(&shm_lock);
335 return EINVAL;
336 }
337
338 shmmap_s = shmmap_getprivate(p);
339 if (shmmap_s != shmmap_s1) {
340 /* Map has been copied, lookup entry in new map */
341 SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
342 if (shmmap_se->va == (vaddr_t)SCARG(uap, shmaddr))
343 break;
344 if (shmmap_se == NULL) {
345 mutex_exit(&shm_lock);
346 return EINVAL;
347 }
348 }
349
350 SHMPRINTF(("shmdt: vm %p: remove %d @%lx\n",
351 p->p_vmspace, shmmap_se->shmid, shmmap_se->va));
352
353 /* Delete the entry from shm map */
354 uobj = shm_delete_mapping(shmmap_s, shmmap_se);
355 shmseg = &shmsegs[IPCID_TO_IX(shmmap_se->shmid)];
356 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
357 mutex_exit(&shm_lock);
358
359 uvm_deallocate(&p->p_vmspace->vm_map, shmmap_se->va, size);
360 if (uobj != NULL)
361 uao_detach(uobj);
362 pool_put(&shmmap_entry_pool, shmmap_se);
363
364 return 0;
365 }
366
367 /*
368 * Map shared memory.
369 */
370 int
371 sys_shmat(struct lwp *l, const struct sys_shmat_args *uap, register_t *retval)
372 {
373 /* {
374 syscallarg(int) shmid;
375 syscallarg(const void *) shmaddr;
376 syscallarg(int) shmflg;
377 } */
378 int error, flags = 0;
379 struct proc *p = l->l_proc;
380 kauth_cred_t cred = l->l_cred;
381 struct shmid_ds *shmseg;
382 struct shmmap_state *shmmap_s;
383 struct shmmap_entry *shmmap_se;
384 struct uvm_object *uobj;
385 struct vmspace *vm;
386 vaddr_t attach_va;
387 vm_prot_t prot;
388 vsize_t size;
389
390 /* Allocate a new map entry and set it */
391 shmmap_se = pool_get(&shmmap_entry_pool, PR_WAITOK);
392
393 mutex_enter(&shm_lock);
394 /* In case of reallocation, we will wait for completion */
395 while (__predict_false(shm_realloc_state))
396 cv_wait(&shm_realloc_cv, &shm_lock);
397
398 shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
399 if (shmseg == NULL) {
400 error = EINVAL;
401 goto err;
402 }
403 error = ipcperm(cred, &shmseg->shm_perm,
404 (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
405 if (error)
406 goto err;
407
408 vm = p->p_vmspace;
409 shmmap_s = (struct shmmap_state *)vm->vm_shm;
410 if (shmmap_s && shmmap_s->nitems >= shminfo.shmseg) {
411 error = EMFILE;
412 goto err;
413 }
414
415 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
416 prot = VM_PROT_READ;
417 if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
418 prot |= VM_PROT_WRITE;
419 if (SCARG(uap, shmaddr)) {
420 flags |= UVM_FLAG_FIXED;
421 if (SCARG(uap, shmflg) & SHM_RND)
422 attach_va =
423 (vaddr_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
424 else if (((vaddr_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
425 attach_va = (vaddr_t)SCARG(uap, shmaddr);
426 else {
427 error = EINVAL;
428 goto err;
429 }
430 } else {
431 /* This is just a hint to uvm_mmap() about where to put it. */
432 attach_va = p->p_emul->e_vm_default_addr(p,
433 (vaddr_t)vm->vm_daddr, size);
434 }
435
436 /*
437 * Create a map entry, add it to the list and increase the counters.
438 * The lock will be dropped before the mapping, disable reallocation.
439 */
440 shmmap_s = shmmap_getprivate(p);
441 SLIST_INSERT_HEAD(&shmmap_s->entries, shmmap_se, next);
442 shmmap_s->nitems++;
443 shmseg->shm_lpid = p->p_pid;
444 shmseg->shm_nattch++;
445 shm_realloc_disable++;
446 mutex_exit(&shm_lock);
447
448 /*
449 * Add a reference to the memory object, map it to the
450 * address space, and lock the memory, if needed.
451 */
452 uobj = shmseg->_shm_internal;
453 uao_reference(uobj);
454 error = uvm_map(&vm->vm_map, &attach_va, size, uobj, 0, 0,
455 UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, flags));
456 if (error)
457 goto err_detach;
458 if (shm_use_phys || (shmseg->shm_perm.mode & SHMSEG_WIRED)) {
459 error = uvm_map_pageable(&vm->vm_map, attach_va,
460 attach_va + size, false, 0);
461 if (error) {
462 if (error == EFAULT)
463 error = ENOMEM;
464 uvm_deallocate(&vm->vm_map, attach_va, size);
465 goto err_detach;
466 }
467 }
468
469 /* Set the new address, and update the time */
470 mutex_enter(&shm_lock);
471 shmmap_se->va = attach_va;
472 shmmap_se->shmid = SCARG(uap, shmid);
473 shmseg->shm_atime = time_second;
474 shm_realloc_disable--;
475 retval[0] = attach_va;
476 SHMPRINTF(("shmat: vm %p: add %d @%lx\n",
477 p->p_vmspace, shmmap_se->shmid, attach_va));
478 err:
479 cv_broadcast(&shm_realloc_cv);
480 mutex_exit(&shm_lock);
481 if (error && shmmap_se)
482 pool_put(&shmmap_entry_pool, shmmap_se);
483 return error;
484
485 err_detach:
486 uao_detach(uobj);
487 mutex_enter(&shm_lock);
488 uobj = shm_delete_mapping(shmmap_s, shmmap_se);
489 shm_realloc_disable--;
490 cv_broadcast(&shm_realloc_cv);
491 mutex_exit(&shm_lock);
492 if (uobj != NULL)
493 uao_detach(uobj);
494 pool_put(&shmmap_entry_pool, shmmap_se);
495 return error;
496 }
497
498 /*
499 * Shared memory control operations.
500 */
501 int
502 sys___shmctl13(struct lwp *l, const struct sys___shmctl13_args *uap, 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 /* Check the permission, segment size and appropriate flag */
630 error = ipcperm(cred, &shmseg->shm_perm, mode);
631 if (error)
632 return error;
633 if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
634 return EINVAL;
635 if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
636 (IPC_CREAT | IPC_EXCL))
637 return EEXIST;
638
639 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
640 return 0;
641 }
642
643 int
644 sys_shmget(struct lwp *l, const struct sys_shmget_args *uap, register_t *retval)
645 {
646 /* {
647 syscallarg(key_t) key;
648 syscallarg(size_t) size;
649 syscallarg(int) shmflg;
650 } */
651 struct shmid_ds *shmseg;
652 kauth_cred_t cred = l->l_cred;
653 key_t key = SCARG(uap, key);
654 size_t size;
655 int error, mode, segnum;
656 bool lockmem;
657
658 mode = SCARG(uap, shmflg) & ACCESSPERMS;
659 if (SCARG(uap, shmflg) & _SHM_RMLINGER)
660 mode |= SHMSEG_RMLINGER;
661
662 SHMPRINTF(("shmget: key 0x%lx size 0x%x shmflg 0x%x mode 0x%x\n",
663 SCARG(uap, key), SCARG(uap, size), SCARG(uap, shmflg), mode));
664
665 mutex_enter(&shm_lock);
666 /* In case of reallocation, we will wait for completion */
667 while (__predict_false(shm_realloc_state))
668 cv_wait(&shm_realloc_cv, &shm_lock);
669
670 if (key != IPC_PRIVATE) {
671 error = shmget_existing(l, uap, mode, retval);
672 if (error != -1) {
673 mutex_exit(&shm_lock);
674 return error;
675 }
676 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0) {
677 mutex_exit(&shm_lock);
678 return ENOENT;
679 }
680 }
681 error = 0;
682
683 /*
684 * Check the for the limits.
685 */
686 size = SCARG(uap, size);
687 if (size < shminfo.shmmin || size > shminfo.shmmax) {
688 mutex_exit(&shm_lock);
689 return EINVAL;
690 }
691 if (shm_nused >= shminfo.shmmni) {
692 mutex_exit(&shm_lock);
693 return ENOSPC;
694 }
695 size = (size + PGOFSET) & ~PGOFSET;
696 if (shm_committed + btoc(size) > shminfo.shmall) {
697 mutex_exit(&shm_lock);
698 return ENOMEM;
699 }
700
701 /* Find the first available segment */
702 if (shm_last_free < 0) {
703 for (segnum = 0; segnum < shminfo.shmmni; segnum++)
704 if (shmsegs[segnum].shm_perm.mode & SHMSEG_FREE)
705 break;
706 KASSERT(segnum < shminfo.shmmni);
707 } else {
708 segnum = shm_last_free;
709 shm_last_free = -1;
710 }
711
712 /*
713 * Initialize the segment.
714 * We will drop the lock while allocating the memory, thus mark the
715 * segment present, but removed, that no other thread could take it.
716 * Also, disable reallocation, while lock is dropped.
717 */
718 shmseg = &shmsegs[segnum];
719 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
720 shm_committed += btoc(size);
721 shm_nused++;
722 lockmem = shm_use_phys;
723 shm_realloc_disable++;
724 mutex_exit(&shm_lock);
725
726 /* Allocate the memory object and lock it if needed */
727 shmseg->_shm_internal = uao_create(size, 0);
728 if (lockmem) {
729 /* Wire the pages and tag it */
730 error = uobj_wirepages(shmseg->_shm_internal, 0,
731 round_page(shmseg->shm_segsz));
732 if (error) {
733 mutex_enter(&shm_lock);
734 shm_free_segment(segnum);
735 shm_realloc_disable--;
736 mutex_exit(&shm_lock);
737 return error;
738 }
739 }
740
741 /*
742 * Please note, while segment is marked, there are no need to hold the
743 * lock, while setting it (except shm_perm.mode).
744 */
745 shmseg->shm_perm._key = SCARG(uap, key);
746 shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
747 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
748
749 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = kauth_cred_geteuid(cred);
750 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = kauth_cred_getegid(cred);
751 shmseg->shm_segsz = SCARG(uap, size);
752 shmseg->shm_cpid = l->l_proc->p_pid;
753 shmseg->shm_lpid = shmseg->shm_nattch = 0;
754 shmseg->shm_atime = shmseg->shm_dtime = 0;
755 shmseg->shm_ctime = time_second;
756
757 /*
758 * Segment is initialized.
759 * Enter the lock, mark as allocated, and notify waiters (if any).
760 * Also, unmark the state of reallocation.
761 */
762 mutex_enter(&shm_lock);
763 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
764 (mode & (ACCESSPERMS | SHMSEG_RMLINGER)) |
765 SHMSEG_ALLOCATED | (lockmem ? SHMSEG_WIRED : 0);
766 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
767 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
768 cv_broadcast(&shm_cv[segnum]);
769 }
770 shm_realloc_disable--;
771 cv_broadcast(&shm_realloc_cv);
772 mutex_exit(&shm_lock);
773
774 return error;
775 }
776
777 void
778 shmfork(struct vmspace *vm1, struct vmspace *vm2)
779 {
780 struct shmmap_state *shmmap_s;
781 struct shmmap_entry *shmmap_se;
782
783 SHMPRINTF(("shmfork %p->%p\n", vm1, vm2));
784 mutex_enter(&shm_lock);
785 vm2->vm_shm = vm1->vm_shm;
786 if (vm1->vm_shm) {
787 shmmap_s = (struct shmmap_state *)vm1->vm_shm;
788 SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
789 shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch++;
790 shmmap_s->nrefs++;
791 }
792 mutex_exit(&shm_lock);
793 }
794
795 void
796 shmexit(struct vmspace *vm)
797 {
798 struct shmmap_state *shmmap_s;
799 struct shmmap_entry *shmmap_se;
800 struct uvm_object **uobj;
801 size_t *size;
802 u_int i, n;
803
804 SLIST_HEAD(, shmmap_entry) tmp_entries;
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
813 vm->vm_shm = NULL;
814
815 if (--shmmap_s->nrefs > 0) {
816 SHMPRINTF(("shmexit: vm %p drop ref (%d entries), refs = %d\n",
817 vm, shmmap_s->nitems, shmmap_s->nrefs));
818 SLIST_FOREACH(shmmap_se, &shmmap_s->entries, next)
819 shmsegs[IPCID_TO_IX(shmmap_se->shmid)].shm_nattch--;
820 mutex_exit(&shm_lock);
821 return;
822 }
823
824 KASSERT(shmmap_s->nrefs == 0);
825 n = shmmap_s->nitems;
826 SHMPRINTF(("shmexit: vm %p cleanup (%d entries)\n", vm, n));
827 mutex_exit(&shm_lock);
828 if (n == 0) {
829 kmem_free(shmmap_s, sizeof(struct shmmap_state));
830 return;
831 }
832
833 /* Allocate the arrays */
834 SLIST_INIT(&tmp_entries);
835 uobj = kmem_zalloc(n * sizeof(void *), KM_SLEEP);
836 size = kmem_zalloc(n * sizeof(size_t), KM_SLEEP);
837
838 /* Delete the entry from shm map */
839 i = 0;
840 mutex_enter(&shm_lock);
841 while (!SLIST_EMPTY(&shmmap_s->entries)) {
842 struct shmid_ds *shmseg;
843
844 shmmap_se = SLIST_FIRST(&shmmap_s->entries);
845 shmseg = &shmsegs[IPCID_TO_IX(shmmap_se->shmid)];
846 size[i] = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
847 uobj[i] = shm_delete_mapping(shmmap_s, shmmap_se);
848 SLIST_INSERT_HEAD(&tmp_entries, shmmap_se, next);
849 i++;
850 }
851 mutex_exit(&shm_lock);
852
853 /* Unmap all segments, free the entries */
854 i = 0;
855 while (!SLIST_EMPTY(&tmp_entries)) {
856 KASSERT(i < n);
857 shmmap_se = SLIST_FIRST(&tmp_entries);
858 SLIST_REMOVE(&tmp_entries, shmmap_se, shmmap_entry, next);
859 uvm_deallocate(&vm->vm_map, shmmap_se->va, size[i]);
860 if (uobj[i] != NULL)
861 uao_detach(uobj[i]);
862 pool_put(&shmmap_entry_pool, shmmap_se);
863 i++;
864 }
865
866 kmem_free(uobj, n * sizeof(void *));
867 kmem_free(size, n * sizeof(size_t));
868 kmem_free(shmmap_s, sizeof(struct shmmap_state));
869 }
870
871 static int
872 shmrealloc(int newshmni)
873 {
874 vaddr_t v;
875 struct shmid_ds *oldshmsegs, *newshmsegs;
876 kcondvar_t *newshm_cv;
877 size_t sz;
878 int i, lsegid;
879
880 if (newshmni < 1)
881 return EINVAL;
882
883 /* Allocate new memory area */
884 sz = ALIGN(newshmni * sizeof(struct shmid_ds)) +
885 ALIGN(newshmni * sizeof(kcondvar_t));
886 v = uvm_km_alloc(kernel_map, round_page(sz), 0,
887 UVM_KMF_WIRED|UVM_KMF_ZERO);
888 if (v == 0)
889 return ENOMEM;
890
891 mutex_enter(&shm_lock);
892 while (shm_realloc_state || shm_realloc_disable)
893 cv_wait(&shm_realloc_cv, &shm_lock);
894
895 /*
896 * Get the number of last segment. Fail we are trying to
897 * reallocate less memory than we use.
898 */
899 lsegid = 0;
900 for (i = 0; i < shminfo.shmmni; i++)
901 if ((shmsegs[i].shm_perm.mode & SHMSEG_FREE) == 0)
902 lsegid = i;
903 if (lsegid >= newshmni) {
904 mutex_exit(&shm_lock);
905 uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
906 return EBUSY;
907 }
908 shm_realloc_state = true;
909
910 newshmsegs = (void *)v;
911 newshm_cv = (void *)(ALIGN(newshmsegs) +
912 newshmni * sizeof(struct shmid_ds));
913
914 /* Copy all memory to the new area */
915 for (i = 0; i < shm_nused; i++)
916 (void)memcpy(&newshmsegs[i], &shmsegs[i],
917 sizeof(newshmsegs[0]));
918
919 /* Mark as free all new segments, if there is any */
920 for (; i < newshmni; i++) {
921 cv_init(&newshm_cv[i], "shmwait");
922 newshmsegs[i].shm_perm.mode = SHMSEG_FREE;
923 newshmsegs[i].shm_perm._seq = 0;
924 }
925
926 oldshmsegs = shmsegs;
927 sz = ALIGN(shminfo.shmmni * sizeof(struct shmid_ds)) +
928 ALIGN(shminfo.shmmni * sizeof(kcondvar_t));
929
930 shminfo.shmmni = newshmni;
931 shmsegs = newshmsegs;
932 shm_cv = newshm_cv;
933
934 /* Reallocation completed - notify all waiters, if any */
935 shm_realloc_state = false;
936 cv_broadcast(&shm_realloc_cv);
937 mutex_exit(&shm_lock);
938
939 uvm_km_free(kernel_map, (vaddr_t)oldshmsegs, sz, UVM_KMF_WIRED);
940 return 0;
941 }
942
943 void
944 shminit(void)
945 {
946 vaddr_t v;
947 size_t sz;
948 int i;
949
950 mutex_init(&shm_lock, MUTEX_DEFAULT, IPL_NONE);
951 pool_init(&shmmap_entry_pool, sizeof(struct shmmap_entry), 0, 0, 0,
952 "shmmp", &pool_allocator_nointr, IPL_NONE);
953 cv_init(&shm_realloc_cv, "shmrealc");
954
955 /* Allocate the wired memory for our structures */
956 sz = ALIGN(shminfo.shmmni * sizeof(struct shmid_ds)) +
957 ALIGN(shminfo.shmmni * sizeof(kcondvar_t));
958 v = uvm_km_alloc(kernel_map, round_page(sz), 0,
959 UVM_KMF_WIRED|UVM_KMF_ZERO);
960 if (v == 0)
961 panic("sysv_shm: cannot allocate memory");
962 shmsegs = (void *)v;
963 shm_cv = (void *)(ALIGN(shmsegs) +
964 shminfo.shmmni * sizeof(struct shmid_ds));
965
966 shminfo.shmmax *= PAGE_SIZE;
967
968 for (i = 0; i < shminfo.shmmni; i++) {
969 cv_init(&shm_cv[i], "shmwait");
970 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
971 shmsegs[i].shm_perm._seq = 0;
972 }
973 shm_last_free = 0;
974 shm_nused = 0;
975 shm_committed = 0;
976 shm_realloc_disable = 0;
977 shm_realloc_state = false;
978 }
979
980 static int
981 sysctl_ipc_shmmni(SYSCTLFN_ARGS)
982 {
983 int newsize, error;
984 struct sysctlnode node;
985 node = *rnode;
986 node.sysctl_data = &newsize;
987
988 newsize = shminfo.shmmni;
989 error = sysctl_lookup(SYSCTLFN_CALL(&node));
990 if (error || newp == NULL)
991 return error;
992
993 sysctl_unlock();
994 error = shmrealloc(newsize);
995 sysctl_relock();
996 return error;
997 }
998
999 static int
1000 sysctl_ipc_shmmaxpgs(SYSCTLFN_ARGS)
1001 {
1002 int newsize, error;
1003 struct sysctlnode node;
1004 node = *rnode;
1005 node.sysctl_data = &newsize;
1006
1007 newsize = shminfo.shmall;
1008 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1009 if (error || newp == NULL)
1010 return error;
1011
1012 if (newsize < 1)
1013 return EINVAL;
1014
1015 shminfo.shmall = newsize;
1016 shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
1017
1018 return 0;
1019 }
1020
1021 SYSCTL_SETUP(sysctl_ipc_shm_setup, "sysctl kern.ipc subtree setup")
1022 {
1023
1024 sysctl_createv(clog, 0, NULL, NULL,
1025 CTLFLAG_PERMANENT,
1026 CTLTYPE_NODE, "kern", NULL,
1027 NULL, 0, NULL, 0,
1028 CTL_KERN, CTL_EOL);
1029 sysctl_createv(clog, 0, NULL, NULL,
1030 CTLFLAG_PERMANENT,
1031 CTLTYPE_NODE, "ipc",
1032 SYSCTL_DESCR("SysV IPC options"),
1033 NULL, 0, NULL, 0,
1034 CTL_KERN, KERN_SYSVIPC, CTL_EOL);
1035 sysctl_createv(clog, 0, NULL, NULL,
1036 CTLFLAG_PERMANENT | CTLFLAG_READONLY,
1037 CTLTYPE_INT, "shmmax",
1038 SYSCTL_DESCR("Max shared memory segment size in bytes"),
1039 NULL, 0, &shminfo.shmmax, 0,
1040 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMAX, CTL_EOL);
1041 sysctl_createv(clog, 0, NULL, NULL,
1042 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1043 CTLTYPE_INT, "shmmni",
1044 SYSCTL_DESCR("Max number of shared memory identifiers"),
1045 sysctl_ipc_shmmni, 0, &shminfo.shmmni, 0,
1046 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMNI, CTL_EOL);
1047 sysctl_createv(clog, 0, NULL, NULL,
1048 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1049 CTLTYPE_INT, "shmseg",
1050 SYSCTL_DESCR("Max shared memory segments per process"),
1051 NULL, 0, &shminfo.shmseg, 0,
1052 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMSEG, CTL_EOL);
1053 sysctl_createv(clog, 0, NULL, NULL,
1054 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1055 CTLTYPE_INT, "shmmaxpgs",
1056 SYSCTL_DESCR("Max amount of shared memory in pages"),
1057 sysctl_ipc_shmmaxpgs, 0, &shminfo.shmall, 0,
1058 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMMAXPGS, CTL_EOL);
1059 sysctl_createv(clog, 0, NULL, NULL,
1060 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1061 CTLTYPE_INT, "shm_use_phys",
1062 SYSCTL_DESCR("Enable/disable locking of shared memory in "
1063 "physical memory"), NULL, 0, &shm_use_phys, 0,
1064 CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHMUSEPHYS, CTL_EOL);
1065 }
1066