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