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