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