1 /* $FreeBSD: src/sys/kern/sysv_sem.c,v 1.69 2004/03/17 09:37:13 cperciva Exp $ */
4 * Implementation of SVID semaphores
6 * Author: Daniel Boulet
8 * This software is provided ``AS IS'' without any warranties of any kind.
11 #include "opt_sysvipc.h"
13 #include <sys/param.h>
14 #include <sys/systm.h>
15 #include <sys/sysproto.h>
16 #include <sys/kernel.h>
19 #include <sys/sysent.h>
20 #include <sys/sysctl.h>
21 #include <sys/malloc.h>
23 #include <sys/thread.h>
25 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
27 static void seminit (void *);
29 static struct sem_undo *semu_alloc (struct proc *p);
30 static int semundo_adjust (struct proc *p, int semid, int semnum, int adjval);
31 static void semundo_clear (int semid, int semnum);
33 static struct lwkt_token semu_token = LWKT_TOKEN_INITIALIZER(semu_token);
34 static int semtot = 0;
35 static struct semid_pool *sema; /* semaphore id pool */
36 static TAILQ_HEAD(, sem_undo) semu_list = TAILQ_HEAD_INITIALIZER(semu_list);
37 static struct lock sema_lk;
40 u_short semval; /* semaphore value */
41 pid_t sempid; /* pid of last operation */
42 u_short semncnt; /* # awaiting semval > cval */
43 u_short semzcnt; /* # awaiting semval = 0 */
47 * Undo structure (one per process)
50 TAILQ_ENTRY(sem_undo) un_entry; /* linked list for semundo_clear() */
51 struct proc *un_proc; /* owner of this structure */
52 int un_refs; /* prevent unlink/kfree */
53 short un_cnt; /* # of active entries */
56 short un_adjval; /* adjust on exit values */
57 short un_num; /* semaphore # */
58 int un_id; /* semid */
59 } un_ent[1]; /* undo entries */
63 * Configuration parameters
66 #define SEMMNI 1024 /* # of semaphore identifiers */
69 #define SEMMNS 32767 /* # of semaphores in system */
72 #define SEMUME 25 /* max # of undo entries per process */
75 #define SEMMNU 1024 /* # of undo structures in system */
79 /* shouldn't need tuning */
81 #define SEMMAP 128 /* # of entries in semaphore map */
84 #define SEMMSL SEMMNS /* max # of semaphores per id */
87 #define SEMOPM 100 /* max # of operations per semop call */
90 #define SEMVMX 32767 /* semaphore maximum value */
91 #define SEMAEM 16384 /* adjust on exit max value */
94 * Due to the way semaphore memory is allocated, we have to ensure that
95 * SEMUSZ is properly aligned.
98 #define SEM_ALIGN(bytes) roundup2(bytes, sizeof(long))
100 /* actual size of an undo structure */
101 #define SEMUSZ(nent) SEM_ALIGN(offsetof(struct sem_undo, un_ent[nent]))
104 * semaphore info struct
106 struct seminfo seminfo = {
107 SEMMAP, /* # of entries in semaphore map */
108 SEMMNI, /* # of semaphore identifiers */
109 SEMMNS, /* # of semaphores in system */
110 SEMMNU, /* # of undo structures in system */
111 SEMMSL, /* max # of semaphores per id */
112 SEMOPM, /* max # of operations per semop call */
113 SEMUME, /* max # of undo entries per process */
114 SEMUSZ(SEMUME), /* size in bytes of undo structure */
115 SEMVMX, /* semaphore maximum value */
116 SEMAEM /* adjust on exit max value */
119 TUNABLE_INT("kern.ipc.semmap", &seminfo.semmap);
120 TUNABLE_INT("kern.ipc.semmni", &seminfo.semmni);
121 TUNABLE_INT("kern.ipc.semmns", &seminfo.semmns);
122 TUNABLE_INT("kern.ipc.semmnu", &seminfo.semmnu);
123 TUNABLE_INT("kern.ipc.semmsl", &seminfo.semmsl);
124 TUNABLE_INT("kern.ipc.semopm", &seminfo.semopm);
125 TUNABLE_INT("kern.ipc.semume", &seminfo.semume);
126 TUNABLE_INT("kern.ipc.semusz", &seminfo.semusz);
127 TUNABLE_INT("kern.ipc.semvmx", &seminfo.semvmx);
128 TUNABLE_INT("kern.ipc.semaem", &seminfo.semaem);
130 SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0,
131 "Number of entries in semaphore map");
132 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RD, &seminfo.semmni, 0,
133 "Number of semaphore identifiers");
134 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RD, &seminfo.semmns, 0,
135 "Total number of semaphores");
136 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RD, &seminfo.semmnu, 0,
137 "Total number of undo structures");
138 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0,
139 "Max number of semaphores per id");
140 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RD, &seminfo.semopm, 0,
141 "Max number of operations per semop call");
142 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RD, &seminfo.semume, 0,
143 "Max number of undo entries per process");
144 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RD, &seminfo.semusz, 0,
145 "Size in bytes of undo structure");
146 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0,
147 "Semaphore maximum value");
148 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0,
149 "Adjust on exit max value");
152 RO seminfo.semmap /* SEMMAP unused */
155 RO seminfo.semmnu /* undo entries per system */
157 RO seminfo.semopm /* SEMOPM unused */
159 RO seminfo.semusz /* param - derived from SEMUME for per-proc sizeof */
160 RO seminfo.semvmx /* SEMVMX unused - user param */
161 RO seminfo.semaem /* SEMAEM unused - user param */
169 sema = kmalloc(sizeof(struct semid_pool) * seminfo.semmni,
170 M_SEM, M_WAITOK | M_ZERO);
172 lockinit(&sema_lk, "semglb", 0, 0);
173 for (i = 0; i < seminfo.semmni; i++) {
174 struct semid_pool *semaptr = &sema[i];
176 lockinit(&semaptr->lk, "semary", 0, 0);
177 semaptr->ds.sem_base = NULL;
178 semaptr->ds.sem_perm.mode = 0;
181 SYSINIT(sysv_sem, SI_SUB_SYSV_SEM, SI_ORDER_FIRST, seminit, NULL);
184 * Allocate a new sem_undo structure for a process
185 * (returns ptr to structure or NULL if no more room)
187 static struct sem_undo *
188 semu_alloc(struct proc *p)
190 struct sem_undo *semu;
193 * Allocate the semu structure and associate it with the process,
196 while ((semu = p->p_sem_undo) == NULL) {
197 semu = kmalloc(SEMUSZ(seminfo.semume), M_SEM,
199 lwkt_gettoken(&semu_token);
200 lwkt_gettoken(&p->p_token);
201 if (p->p_sem_undo == NULL) {
202 p->p_sem_undo = semu;
203 p->p_flags |= P_SYSVSEM;
205 TAILQ_INSERT_TAIL(&semu_list, semu, un_entry);
209 lwkt_reltoken(&p->p_token);
210 lwkt_reltoken(&semu_token);
216 * Adjust a particular entry for a particular proc
219 semundo_adjust(struct proc *p, int semid, int semnum, int adjval)
221 struct sem_undo *suptr;
227 * Look for and remember the sem_undo if the caller doesn't
230 suptr = semu_alloc(p);
231 lwkt_gettoken(&p->p_token);
234 * Look for the requested entry and adjust it (delete if adjval becomes
237 sunptr = &suptr->un_ent[0];
238 for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
239 if (sunptr->un_id != semid || sunptr->un_num != semnum)
242 sunptr->un_adjval = 0;
244 sunptr->un_adjval += adjval;
245 if (sunptr->un_adjval == 0) {
247 if (i < suptr->un_cnt)
248 suptr->un_ent[i] = suptr->un_ent[suptr->un_cnt];
253 /* Didn't find the right entry - create it */
256 if (suptr->un_cnt != seminfo.semume) {
257 sunptr = &suptr->un_ent[suptr->un_cnt];
259 sunptr->un_adjval = adjval;
260 sunptr->un_id = semid;
261 sunptr->un_num = semnum;
266 lwkt_reltoken(&p->p_token);
272 * This is rather expensive
275 semundo_clear(int semid, int semnum)
278 struct sem_undo *suptr;
279 struct sem_undo *sunext;
283 lwkt_gettoken(&semu_token);
284 sunext = TAILQ_FIRST(&semu_list);
285 while ((suptr = sunext) != NULL) {
286 if ((p = suptr->un_proc) == NULL) {
287 sunext = TAILQ_NEXT(suptr, un_entry);
292 lwkt_gettoken(&p->p_token);
294 sunptr = &suptr->un_ent[0];
297 while (i < suptr->un_cnt) {
298 if (sunptr->un_id == semid) {
299 if (semnum == -1 || sunptr->un_num == semnum) {
301 if (i < suptr->un_cnt) {
303 suptr->un_ent[suptr->un_cnt];
306 * or sunptr after copydown.
318 lwkt_reltoken(&p->p_token);
322 * Handle deletion races
324 sunext = TAILQ_NEXT(suptr, un_entry);
325 if (--suptr->un_refs == 0 && suptr->un_proc == NULL) {
326 KKASSERT(suptr->un_cnt == 0);
327 TAILQ_REMOVE(&semu_list, suptr, un_entry);
331 lwkt_reltoken(&semu_token);
335 * Note that the user-mode half of this passes a union, not a pointer
340 sys___semctl(struct __semctl_args *uap)
342 struct thread *td = curthread;
343 int semid = uap->semid;
344 int semnum = uap->semnum;
346 union semun *arg = uap->arg;
347 union semun real_arg;
348 struct ucred *cred = td->td_ucred;
350 struct semid_ds sbuf;
351 struct semid_pool *semaptr;
355 kprintf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
358 if (!jail_sysvipc_allowed && cred->cr_prison != NULL)
361 semid = IPCID_TO_IX(semid);
362 if (semid < 0 || semid >= seminfo.semmni) {
365 semaptr = &sema[semid];
366 lockmgr(&semaptr->lk, LK_EXCLUSIVE);
368 if ((semaptr->ds.sem_perm.mode & SEM_ALLOC) == 0 ||
369 semaptr->ds.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
370 lockmgr(&semaptr->lk, LK_RELEASE);
379 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_M);
382 semaptr->ds.sem_perm.cuid = cred->cr_uid;
383 semaptr->ds.sem_perm.uid = cred->cr_uid;
386 * NOTE: Nobody will be waiting on the semaphores since
387 * we have an exclusive lock on semaptr->lk).
389 lockmgr(&sema_lk, LK_EXCLUSIVE);
390 semtot -= semaptr->ds.sem_nsems;
391 kfree(semaptr->ds.sem_base, M_SEM);
392 semaptr->ds.sem_base = NULL;
393 semaptr->ds.sem_perm.mode = 0; /* clears SEM_ALLOC */
394 lockmgr(&sema_lk, LK_RELEASE);
396 semundo_clear(semid, -1);
400 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_M);
403 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
405 if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
406 sizeof(sbuf))) != 0) {
409 semaptr->ds.sem_perm.uid = sbuf.sem_perm.uid;
410 semaptr->ds.sem_perm.gid = sbuf.sem_perm.gid;
411 semaptr->ds.sem_perm.mode =
412 (semaptr->ds.sem_perm.mode & ~0777) |
413 (sbuf.sem_perm.mode & 0777);
414 semaptr->ds.sem_ctime = time_second;
418 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
421 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
423 eval = copyout(&semaptr->ds, real_arg.buf,
424 sizeof(struct semid_ds));
428 * For this command we assume semid is an array index
429 * rather than an IPC id. However, the conversion is
430 * just a mask so just validate that the passed-in semid
431 * matches the masked semid.
433 if (uap->semid != semid) {
437 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
440 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
442 eval = copyout(&semaptr->ds, real_arg.buf,
443 sizeof(struct semid_ds));
444 rval = IXSEQ_TO_IPCID(semid, semaptr->ds.sem_perm);
448 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
451 if (semnum < 0 || semnum >= semaptr->ds.sem_nsems) {
455 rval = semaptr->ds.sem_base[semnum].semncnt;
459 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
462 if (semnum < 0 || semnum >= semaptr->ds.sem_nsems) {
466 rval = semaptr->ds.sem_base[semnum].sempid;
470 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
473 if (semnum < 0 || semnum >= semaptr->ds.sem_nsems) {
477 rval = semaptr->ds.sem_base[semnum].semval;
481 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
484 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
486 for (i = 0; i < semaptr->ds.sem_nsems; i++) {
487 eval = copyout(&semaptr->ds.sem_base[i].semval,
489 sizeof(real_arg.array[0]));
496 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
499 if (semnum < 0 || semnum >= semaptr->ds.sem_nsems) {
503 rval = semaptr->ds.sem_base[semnum].semzcnt;
507 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_W);
510 if (semnum < 0 || semnum >= semaptr->ds.sem_nsems) {
514 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
518 * Because we hold semaptr->lk exclusively we can safely
519 * modify any semptr content without acquiring its token.
521 semptr = &semaptr->ds.sem_base[semnum];
522 semptr->semval = real_arg.val;
523 semundo_clear(semid, semnum);
524 if (semptr->semzcnt || semptr->semncnt)
529 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_W);
532 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
535 * Because we hold semaptr->lk exclusively we can safely
536 * modify any semptr content without acquiring its token.
538 for (i = 0; i < semaptr->ds.sem_nsems; i++) {
539 semptr = &semaptr->ds.sem_base[i];
540 eval = copyin(&real_arg.array[i],
541 (caddr_t)&semptr->semval,
542 sizeof(real_arg.array[0]));
543 if (semptr->semzcnt || semptr->semncnt)
548 semundo_clear(semid, -1);
555 lockmgr(&semaptr->lk, LK_RELEASE);
558 uap->sysmsg_result = rval;
566 sys_semget(struct semget_args *uap)
568 struct thread *td = curthread;
571 int nsems = uap->nsems;
572 int semflg = uap->semflg;
573 struct ucred *cred = td->td_ucred;
576 kprintf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
579 if (!jail_sysvipc_allowed && cred->cr_prison != NULL)
584 if (key != IPC_PRIVATE) {
585 for (semid = 0; semid < seminfo.semmni; semid++) {
586 if ((sema[semid].ds.sem_perm.mode & SEM_ALLOC) == 0 ||
587 sema[semid].ds.sem_perm.key != key) {
590 lockmgr(&sema[semid].lk, LK_EXCLUSIVE);
591 if ((sema[semid].ds.sem_perm.mode & SEM_ALLOC) == 0 ||
592 sema[semid].ds.sem_perm.key != key) {
593 lockmgr(&sema[semid].lk, LK_RELEASE);
598 if (semid < seminfo.semmni) {
599 /* sema[semid].lk still locked from above */
601 kprintf("found public key\n");
603 if ((eval = ipcperm(td->td_proc,
604 &sema[semid].ds.sem_perm,
606 lockmgr(&sema[semid].lk, LK_RELEASE);
609 if (nsems > 0 && sema[semid].ds.sem_nsems < nsems) {
611 kprintf("too small\n");
614 lockmgr(&sema[semid].lk, LK_RELEASE);
617 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
619 kprintf("not exclusive\n");
622 lockmgr(&sema[semid].lk, LK_RELEASE);
629 lockmgr(&sema[semid].lk, LK_RELEASE);
635 kprintf("need to allocate the semid_ds\n");
637 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
638 if (nsems <= 0 || nsems > seminfo.semmsl) {
640 kprintf("nsems out of range (0<%d<=%d)\n",
641 nsems, seminfo.semmsl);
648 * SEM_ALLOC flag cannot be set unless sema_lk is locked.
649 * semtot field also protected by sema_lk.
651 lockmgr(&sema_lk, LK_EXCLUSIVE);
652 if (nsems > seminfo.semmns - semtot) {
654 kprintf("not enough semaphores left "
655 "(need %d, got %d)\n",
656 nsems, seminfo.semmns - semtot);
659 lockmgr(&sema_lk, LK_RELEASE);
662 for (semid = 0; semid < seminfo.semmni; semid++) {
663 if ((sema[semid].ds.sem_perm.mode & SEM_ALLOC) == 0)
666 if (semid == seminfo.semmni) {
668 kprintf("no more semid_ds's available\n");
671 lockmgr(&sema_lk, LK_RELEASE);
675 kprintf("semid %d is available\n", semid);
677 lockmgr(&sema[semid].lk, LK_EXCLUSIVE);
678 sema[semid].ds.sem_perm.key = key;
679 sema[semid].ds.sem_perm.cuid = cred->cr_uid;
680 sema[semid].ds.sem_perm.uid = cred->cr_uid;
681 sema[semid].ds.sem_perm.cgid = cred->cr_gid;
682 sema[semid].ds.sem_perm.gid = cred->cr_gid;
683 sema[semid].ds.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
684 sema[semid].ds.sem_perm.seq =
685 (sema[semid].ds.sem_perm.seq + 1) & 0x7fff;
686 sema[semid].ds.sem_nsems = nsems;
687 sema[semid].ds.sem_otime = 0;
688 sema[semid].ds.sem_ctime = time_second;
689 sema[semid].ds.sem_base = kmalloc(sizeof(struct sem) * nsems,
690 M_SEM, M_WAITOK|M_ZERO);
693 lockmgr(&sema[semid].lk, LK_RELEASE);
694 lockmgr(&sema_lk, LK_RELEASE);
696 kprintf("sembase = 0x%x, next = 0x%x\n",
697 sema[semid].ds.sem_base, &sem[semtot]);
702 kprintf("didn't find it and wasn't asked to create it\n");
710 IXSEQ_TO_IPCID(semid, sema[semid].ds.sem_perm);
719 sys_semop(struct semop_args *uap)
721 struct thread *td = curthread;
722 int semid = uap->semid;
723 u_int nsops = uap->nsops;
724 struct sembuf sops[MAX_SOPS];
725 struct semid_pool *semaptr;
726 struct sembuf *sopptr;
733 kprintf("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops);
735 if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
738 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
740 if (semid < 0 || semid >= seminfo.semmni) {
745 wakeup_start_delayed();
746 semaptr = &sema[semid];
747 lockmgr(&semaptr->lk, LK_SHARED);
749 if ((semaptr->ds.sem_perm.mode & SEM_ALLOC) == 0) {
753 if (semaptr->ds.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
758 if ((eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_W))) {
760 kprintf("eval = %d from ipaccess\n", eval);
765 if (nsops > MAX_SOPS) {
767 kprintf("too many sops (max=%d, nsops=%u)\n", MAX_SOPS, nsops);
773 if ((eval = copyin(uap->sops, &sops, nsops * sizeof(sops[0]))) != 0) {
775 kprintf("eval = %d from copyin(%08x, %08x, %u)\n", eval,
776 uap->sops, &sops, nsops * sizeof(sops[0]));
782 * Loop trying to satisfy the vector of requests.
783 * If we reach a point where we must wait, any requests already
784 * performed are rolled back and we go to sleep until some other
785 * process wakes us up. At this point, we start all over again.
787 * This ensures that from the perspective of other tasks, a set
788 * of requests is atomic (never partially satisfied).
797 for (i = 0; i < nsops; i++) {
800 if (sopptr->sem_num >= semaptr->ds.sem_nsems) {
805 semptr = &semaptr->ds.sem_base[sopptr->sem_num];
806 lwkt_getpooltoken(semptr);
809 kprintf("semop: semaptr=%x, sem_base=%x, semptr=%x, "
810 "sem[%d]=%d : op=%d, flag=%s\n",
811 semaptr, semaptr->ds.sem_base, semptr,
812 sopptr->sem_num, semptr->semval, sopptr->sem_op,
813 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
816 if (sopptr->sem_op < 0) {
817 if (semptr->semval + sopptr->sem_op < 0) {
819 kprintf("semop: can't do it now\n");
823 semptr->semval += sopptr->sem_op;
824 if (semptr->semval == 0 &&
825 semptr->semzcnt > 0) {
829 if (sopptr->sem_flg & SEM_UNDO)
831 } else if (sopptr->sem_op == 0) {
832 if (semptr->semval > 0) {
834 kprintf("semop: not zero now\n");
839 semptr->semval += sopptr->sem_op;
840 if (sopptr->sem_flg & SEM_UNDO)
842 if (semptr->semncnt > 0)
845 lwkt_relpooltoken(semptr);
849 * Did we get through the entire vector?
855 * No, protect the semaphore request which also flags that
856 * a wakeup is needed, then release semptr since we know
857 * another process is likely going to need to access it
860 if (sopptr->sem_op == 0)
864 tsleep_interlock(semptr, PCATCH);
865 lwkt_relpooltoken(semptr);
868 * Rollback the semaphores we had acquired.
871 kprintf("semop: rollback 0 through %d\n", i-1);
873 for (j = 0; j < i; j++) {
874 xsemptr = &semaptr->ds.sem_base[sops[j].sem_num];
875 lwkt_getpooltoken(xsemptr);
876 xsemptr->semval -= sops[j].sem_op;
877 if (xsemptr->semval == 0 && xsemptr->semzcnt > 0)
879 if (xsemptr->semval <= 0 && xsemptr->semncnt > 0)
881 lwkt_relpooltoken(xsemptr);
885 * If the request that we couldn't satisfy has the
886 * NOWAIT flag set then return with EAGAIN.
888 if (sopptr->sem_flg & IPC_NOWAIT) {
894 * Release semaptr->lk while sleeping, allowing other
895 * semops (like SETVAL, SETALL, etc), which require an
896 * exclusive lock and might wake us up.
898 * Reload and recheck the validity of semaptr on return.
899 * Note that semptr itself might have changed too, but
900 * we've already interlocked for semptr and that is what
901 * will be woken up if it wakes up the tsleep on a MP
904 * gen protects against destroy/re-create races where the
908 kprintf("semop: good night!\n");
911 lockmgr(&semaptr->lk, LK_RELEASE);
912 eval = tsleep(semptr, PCATCH | PINTERLOCKED, "semwait", hz);
913 lockmgr(&semaptr->lk, LK_SHARED);
915 kprintf("semop: good morning (eval=%d)!\n", eval);
918 /* return code is checked below, after sem[nz]cnt-- */
921 * Make sure that the semaphore still exists
923 if (semaptr->gen != gen ||
924 (semaptr->ds.sem_perm.mode & SEM_ALLOC) == 0 ||
925 semaptr->ds.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
931 * The semaphore is still alive. Readjust the count of
934 semptr = &semaptr->ds.sem_base[sopptr->sem_num];
935 lwkt_getpooltoken(semptr);
936 if (sopptr->sem_op == 0)
940 lwkt_relpooltoken(semptr);
943 * Is it really morning, or was our sleep interrupted?
944 * (Delayed check of tsleep() return code because we
945 * need to decrement sem[nz]cnt either way.)
952 kprintf("semop: good morning!\n");
959 * Process any SEM_UNDO requests.
962 for (i = 0; i < nsops; i++) {
964 * We only need to deal with SEM_UNDO's for non-zero
969 if ((sops[i].sem_flg & SEM_UNDO) == 0)
971 adjval = sops[i].sem_op;
974 eval = semundo_adjust(td->td_proc, semid,
975 sops[i].sem_num, -adjval);
980 * Oh-Oh! We ran out of either sem_undo's or undo's.
981 * Rollback the adjustments to this point and then
982 * rollback the semaphore ups and down so we can return
983 * with an error with all structures restored. We
984 * rollback the undo's in the exact reverse order that
985 * we applied them. This guarantees that we won't run
986 * out of space as we roll things back out.
988 for (j = i - 1; j >= 0; j--) {
989 if ((sops[j].sem_flg & SEM_UNDO) == 0)
991 adjval = sops[j].sem_op;
994 if (semundo_adjust(td->td_proc, semid,
995 sops[j].sem_num, adjval) != 0)
996 panic("semop - can't undo undos");
999 for (j = 0; j < nsops; j++) {
1000 xsemptr = &semaptr->ds.sem_base[
1002 lwkt_getpooltoken(xsemptr);
1003 xsemptr->semval -= sops[j].sem_op;
1004 if (xsemptr->semval == 0 &&
1005 xsemptr->semzcnt > 0)
1007 if (xsemptr->semval <= 0 &&
1008 xsemptr->semncnt > 0)
1010 lwkt_relpooltoken(xsemptr);
1014 kprintf("eval = %d from semundo_adjust\n", eval);
1017 } /* loop through the sops */
1018 } /* if (do_undos) */
1020 /* We're definitely done - set the sempid's */
1021 for (i = 0; i < nsops; i++) {
1023 semptr = &semaptr->ds.sem_base[sopptr->sem_num];
1024 lwkt_getpooltoken(semptr);
1025 semptr->sempid = td->td_proc->p_pid;
1026 lwkt_relpooltoken(semptr);
1029 /* Do a wakeup if any semaphore was up'd. */
1031 kprintf("semop: done\n");
1033 uap->sysmsg_result = 0;
1036 lockmgr(&semaptr->lk, LK_RELEASE);
1037 wakeup_end_delayed();
1043 * Go through the undo structures for this process and apply the adjustments to
1046 * (p->p_token is held by the caller)
1049 semexit(struct proc *p)
1051 struct sem_undo *suptr;
1055 * We're getting a global token, don't do it if we couldn't
1056 * possibly have any semaphores.
1058 if ((p->p_flags & P_SYSVSEM) == 0)
1060 suptr = p->p_sem_undo;
1061 KKASSERT(suptr != NULL);
1064 * Disconnect suptr from the process and increment un_refs to
1065 * prevent anyone else from being able to destroy the structure.
1066 * Do not remove it from the linked list until after we are through
1067 * scanning it as other semaphore calls might still effect it.
1069 lwkt_gettoken(&semu_token);
1070 p->p_sem_undo = NULL;
1071 p->p_flags &= ~P_SYSVSEM;
1072 suptr->un_proc = NULL;
1074 lwkt_reltoken(&semu_token);
1076 while (suptr->un_cnt) {
1077 struct semid_pool *semaptr;
1084 * These values are stable because we hold p->p_token.
1085 * However, they can get ripped out from under us when
1086 * we block or obtain other tokens so we have to re-check.
1088 ix = suptr->un_cnt - 1;
1089 semid = suptr->un_ent[ix].un_id;
1090 semnum = suptr->un_ent[ix].un_num;
1091 adjval = suptr->un_ent[ix].un_adjval;
1093 semaptr = &sema[semid];
1096 * Recheck after locking, then execute the undo
1097 * operation. semptr remains valid due to the
1100 lockmgr(&semaptr->lk, LK_SHARED);
1101 semptr = &semaptr->ds.sem_base[semnum];
1102 lwkt_getpooltoken(semptr);
1104 if (ix == suptr->un_cnt - 1 &&
1105 semid == suptr->un_ent[ix].un_id &&
1106 semnum == suptr->un_ent[ix].un_num &&
1107 adjval == suptr->un_ent[ix].un_adjval) {
1109 * Only do assertions when we aren't in a SMP race.
1111 if ((semaptr->ds.sem_perm.mode & SEM_ALLOC) == 0)
1112 panic("semexit - semid not allocated");
1113 if (semnum >= semaptr->ds.sem_nsems)
1114 panic("semexit - semnum out of range");
1118 if (semptr->semval < -adjval)
1121 semptr->semval += adjval;
1123 semptr->semval += adjval;
1127 lwkt_relpooltoken(semptr);
1128 lockmgr(&semaptr->lk, LK_RELEASE);
1132 * Final cleanup, remove from the list and deallocate on the
1135 lwkt_gettoken(&semu_token);
1136 if (--suptr->un_refs == 0) {
1137 TAILQ_REMOVE(&semu_list, suptr, un_entry);
1138 KKASSERT(suptr->un_cnt == 0);
1139 kfree(suptr, M_SEM);
1141 lwkt_reltoken(&semu_token);