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/sysmsg.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 sysmsg *sysmsg, const struct __semctl_args *uap)
342 struct thread *td = curthread;
343 struct prison *pr = td->td_proc->p_ucred->cr_prison;
344 int semid = uap->semid;
345 int semnum = uap->semnum;
347 union semun *arg = uap->arg;
348 union semun real_arg;
349 struct ucred *cred = td->td_ucred;
351 struct semid_ds sbuf;
352 struct semid_pool *semaptr;
356 kprintf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
359 if (pr && !PRISON_CAP_ISSET(pr->pr_caps, PRISON_CAP_SYS_SYSVIPC))
362 semid = IPCID_TO_IX(semid);
363 if (semid < 0 || semid >= seminfo.semmni) {
366 semaptr = &sema[semid];
367 lockmgr(&semaptr->lk, LK_EXCLUSIVE);
369 if ((semaptr->ds.sem_perm.mode & SEM_ALLOC) == 0 ||
370 semaptr->ds.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
371 lockmgr(&semaptr->lk, LK_RELEASE);
380 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_M);
383 semaptr->ds.sem_perm.cuid = cred->cr_uid;
384 semaptr->ds.sem_perm.uid = cred->cr_uid;
387 * NOTE: Nobody will be waiting on the semaphores since
388 * we have an exclusive lock on semaptr->lk).
390 lockmgr(&sema_lk, LK_EXCLUSIVE);
391 semtot -= semaptr->ds.sem_nsems;
392 kfree(semaptr->ds.sem_base, M_SEM);
393 semaptr->ds.sem_base = NULL;
394 semaptr->ds.sem_perm.mode = 0; /* clears SEM_ALLOC */
395 lockmgr(&sema_lk, LK_RELEASE);
397 semundo_clear(semid, -1);
401 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_M);
404 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
406 if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
407 sizeof(sbuf))) != 0) {
410 semaptr->ds.sem_perm.uid = sbuf.sem_perm.uid;
411 semaptr->ds.sem_perm.gid = sbuf.sem_perm.gid;
412 semaptr->ds.sem_perm.mode =
413 (semaptr->ds.sem_perm.mode & ~0777) |
414 (sbuf.sem_perm.mode & 0777);
415 semaptr->ds.sem_ctime = time_second;
419 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
422 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
424 eval = copyout(&semaptr->ds, real_arg.buf,
425 sizeof(struct semid_ds));
429 * For this command we assume semid is an array index
430 * rather than an IPC id. However, the conversion is
431 * just a mask so just validate that the passed-in semid
432 * matches the masked semid.
434 if (uap->semid != semid) {
438 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
441 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
443 eval = copyout(&semaptr->ds, real_arg.buf,
444 sizeof(struct semid_ds));
445 rval = IXSEQ_TO_IPCID(semid, semaptr->ds.sem_perm);
449 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
452 if (semnum < 0 || semnum >= semaptr->ds.sem_nsems) {
456 rval = semaptr->ds.sem_base[semnum].semncnt;
460 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
463 if (semnum < 0 || semnum >= semaptr->ds.sem_nsems) {
467 rval = semaptr->ds.sem_base[semnum].sempid;
471 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
474 if (semnum < 0 || semnum >= semaptr->ds.sem_nsems) {
478 rval = semaptr->ds.sem_base[semnum].semval;
482 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
485 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
487 for (i = 0; i < semaptr->ds.sem_nsems; i++) {
488 eval = copyout(&semaptr->ds.sem_base[i].semval,
490 sizeof(real_arg.array[0]));
497 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_R);
500 if (semnum < 0 || semnum >= semaptr->ds.sem_nsems) {
504 rval = semaptr->ds.sem_base[semnum].semzcnt;
508 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_W);
511 if (semnum < 0 || semnum >= semaptr->ds.sem_nsems) {
515 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
519 * Because we hold semaptr->lk exclusively we can safely
520 * modify any semptr content without acquiring its token.
522 semptr = &semaptr->ds.sem_base[semnum];
523 semptr->semval = real_arg.val;
524 semundo_clear(semid, semnum);
525 if (semptr->semzcnt || semptr->semncnt)
530 eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_W);
533 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
536 * Because we hold semaptr->lk exclusively we can safely
537 * modify any semptr content without acquiring its token.
539 for (i = 0; i < semaptr->ds.sem_nsems; i++) {
540 semptr = &semaptr->ds.sem_base[i];
541 eval = copyin(&real_arg.array[i],
542 (caddr_t)&semptr->semval,
543 sizeof(real_arg.array[0]));
544 if (semptr->semzcnt || semptr->semncnt)
549 semundo_clear(semid, -1);
556 lockmgr(&semaptr->lk, LK_RELEASE);
559 sysmsg->sysmsg_result = rval;
567 sys_semget(struct sysmsg *sysmsg, const struct semget_args *uap)
569 struct thread *td = curthread;
570 struct prison *pr = td->td_proc->p_ucred->cr_prison;
573 int nsems = uap->nsems;
574 int semflg = uap->semflg;
575 struct ucred *cred = td->td_ucred;
578 kprintf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
581 if (pr && !PRISON_CAP_ISSET(pr->pr_caps, PRISON_CAP_SYS_SYSVIPC))
586 if (key != IPC_PRIVATE) {
587 for (semid = 0; semid < seminfo.semmni; semid++) {
588 if ((sema[semid].ds.sem_perm.mode & SEM_ALLOC) == 0 ||
589 sema[semid].ds.sem_perm.key != key) {
592 lockmgr(&sema[semid].lk, LK_EXCLUSIVE);
593 if ((sema[semid].ds.sem_perm.mode & SEM_ALLOC) == 0 ||
594 sema[semid].ds.sem_perm.key != key) {
595 lockmgr(&sema[semid].lk, LK_RELEASE);
600 if (semid < seminfo.semmni) {
601 /* sema[semid].lk still locked from above */
603 kprintf("found public key\n");
605 if ((eval = ipcperm(td->td_proc,
606 &sema[semid].ds.sem_perm,
608 lockmgr(&sema[semid].lk, LK_RELEASE);
611 if (nsems > 0 && sema[semid].ds.sem_nsems < nsems) {
613 kprintf("too small\n");
616 lockmgr(&sema[semid].lk, LK_RELEASE);
619 if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
621 kprintf("not exclusive\n");
624 lockmgr(&sema[semid].lk, LK_RELEASE);
631 lockmgr(&sema[semid].lk, LK_RELEASE);
637 kprintf("need to allocate the semid_ds\n");
639 if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
640 if (nsems <= 0 || nsems > seminfo.semmsl) {
642 kprintf("nsems out of range (0<%d<=%d)\n",
643 nsems, seminfo.semmsl);
650 * SEM_ALLOC flag cannot be set unless sema_lk is locked.
651 * semtot field also protected by sema_lk.
653 lockmgr(&sema_lk, LK_EXCLUSIVE);
654 if (nsems > seminfo.semmns - semtot) {
656 kprintf("not enough semaphores left "
657 "(need %d, got %d)\n",
658 nsems, seminfo.semmns - semtot);
661 lockmgr(&sema_lk, LK_RELEASE);
664 for (semid = 0; semid < seminfo.semmni; semid++) {
665 if ((sema[semid].ds.sem_perm.mode & SEM_ALLOC) == 0)
668 if (semid == seminfo.semmni) {
670 kprintf("no more semid_ds's available\n");
673 lockmgr(&sema_lk, LK_RELEASE);
677 kprintf("semid %d is available\n", semid);
679 lockmgr(&sema[semid].lk, LK_EXCLUSIVE);
680 sema[semid].ds.sem_perm.key = key;
681 sema[semid].ds.sem_perm.cuid = cred->cr_uid;
682 sema[semid].ds.sem_perm.uid = cred->cr_uid;
683 sema[semid].ds.sem_perm.cgid = cred->cr_gid;
684 sema[semid].ds.sem_perm.gid = cred->cr_gid;
685 sema[semid].ds.sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
686 sema[semid].ds.sem_perm.seq =
687 (sema[semid].ds.sem_perm.seq + 1) & 0x7fff;
688 sema[semid].ds.sem_nsems = nsems;
689 sema[semid].ds.sem_otime = 0;
690 sema[semid].ds.sem_ctime = time_second;
691 sema[semid].ds.sem_base = kmalloc(sizeof(struct sem) * nsems,
692 M_SEM, M_WAITOK|M_ZERO);
695 lockmgr(&sema[semid].lk, LK_RELEASE);
696 lockmgr(&sema_lk, LK_RELEASE);
698 kprintf("sembase = 0x%x, next = 0x%x\n",
699 sema[semid].ds.sem_base, &sem[semtot]);
704 kprintf("didn't find it and wasn't asked to create it\n");
711 sysmsg->sysmsg_result =
712 IXSEQ_TO_IPCID(semid, sema[semid].ds.sem_perm);
721 sys_semop(struct sysmsg *sysmsg, const struct semop_args *uap)
723 struct thread *td = curthread;
724 struct prison *pr = td->td_proc->p_ucred->cr_prison;
725 int semid = uap->semid;
726 u_int nsops = uap->nsops;
727 struct sembuf sops[MAX_SOPS];
728 struct semid_pool *semaptr;
729 struct sembuf *sopptr;
736 kprintf("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops);
738 if (pr && !PRISON_CAP_ISSET(pr->pr_caps, PRISON_CAP_SYS_SYSVIPC))
741 semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
743 if (semid < 0 || semid >= seminfo.semmni) {
748 wakeup_start_delayed();
749 semaptr = &sema[semid];
750 lockmgr(&semaptr->lk, LK_SHARED);
752 if ((semaptr->ds.sem_perm.mode & SEM_ALLOC) == 0) {
756 if (semaptr->ds.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
761 if ((eval = ipcperm(td->td_proc, &semaptr->ds.sem_perm, IPC_W))) {
763 kprintf("eval = %d from ipaccess\n", eval);
768 if (nsops > MAX_SOPS) {
770 kprintf("too many sops (max=%d, nsops=%u)\n", MAX_SOPS, nsops);
776 if ((eval = copyin(uap->sops, &sops, nsops * sizeof(sops[0]))) != 0) {
778 kprintf("eval = %d from copyin(%08x, %08x, %u)\n", eval,
779 uap->sops, &sops, nsops * sizeof(sops[0]));
785 * Loop trying to satisfy the vector of requests.
786 * If we reach a point where we must wait, any requests already
787 * performed are rolled back and we go to sleep until some other
788 * process wakes us up. At this point, we start all over again.
790 * This ensures that from the perspective of other tasks, a set
791 * of requests is atomic (never partially satisfied).
800 for (i = 0; i < nsops; i++) {
803 if (sopptr->sem_num >= semaptr->ds.sem_nsems) {
808 semptr = &semaptr->ds.sem_base[sopptr->sem_num];
809 lwkt_getpooltoken(semptr);
812 kprintf("semop: semaptr=%x, sem_base=%x, semptr=%x, "
813 "sem[%d]=%d : op=%d, flag=%s\n",
814 semaptr, semaptr->ds.sem_base, semptr,
815 sopptr->sem_num, semptr->semval, sopptr->sem_op,
816 (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
819 if (sopptr->sem_op < 0) {
820 if (semptr->semval + sopptr->sem_op < 0) {
822 kprintf("semop: can't do it now\n");
826 semptr->semval += sopptr->sem_op;
827 if (semptr->semval == 0 &&
828 semptr->semzcnt > 0) {
832 if (sopptr->sem_flg & SEM_UNDO)
834 } else if (sopptr->sem_op == 0) {
835 if (semptr->semval > 0) {
837 kprintf("semop: not zero now\n");
842 semptr->semval += sopptr->sem_op;
843 if (sopptr->sem_flg & SEM_UNDO)
845 if (semptr->semncnt > 0)
848 lwkt_relpooltoken(semptr);
852 * Did we get through the entire vector?
858 * No, protect the semaphore request which also flags that
859 * a wakeup is needed, then release semptr since we know
860 * another process is likely going to need to access it
863 if (sopptr->sem_op == 0)
867 tsleep_interlock(semptr, PCATCH);
868 lwkt_relpooltoken(semptr);
871 * Rollback the semaphores we had acquired.
874 kprintf("semop: rollback 0 through %d\n", i-1);
876 for (j = 0; j < i; j++) {
877 xsemptr = &semaptr->ds.sem_base[sops[j].sem_num];
878 lwkt_getpooltoken(xsemptr);
879 xsemptr->semval -= sops[j].sem_op;
880 if (xsemptr->semval == 0 && xsemptr->semzcnt > 0)
882 if (xsemptr->semval <= 0 && xsemptr->semncnt > 0)
884 lwkt_relpooltoken(xsemptr);
888 * If the request that we couldn't satisfy has the
889 * NOWAIT flag set then return with EAGAIN.
891 if (sopptr->sem_flg & IPC_NOWAIT) {
897 * Release semaptr->lk while sleeping, allowing other
898 * semops (like SETVAL, SETALL, etc), which require an
899 * exclusive lock and might wake us up.
901 * Reload and recheck the validity of semaptr on return.
902 * Note that semptr itself might have changed too, but
903 * we've already interlocked for semptr and that is what
904 * will be woken up if it wakes up the tsleep on a MP
907 * gen protects against destroy/re-create races where the
911 kprintf("semop: good night!\n");
914 lockmgr(&semaptr->lk, LK_RELEASE);
915 eval = tsleep(semptr, PCATCH | PINTERLOCKED, "semwait", hz);
916 lockmgr(&semaptr->lk, LK_SHARED);
918 kprintf("semop: good morning (eval=%d)!\n", eval);
921 /* return code is checked below, after sem[nz]cnt-- */
924 * Make sure that the semaphore still exists
926 if (semaptr->gen != gen ||
927 (semaptr->ds.sem_perm.mode & SEM_ALLOC) == 0 ||
928 semaptr->ds.sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
934 * The semaphore is still alive. Readjust the count of
937 semptr = &semaptr->ds.sem_base[sopptr->sem_num];
938 lwkt_getpooltoken(semptr);
939 if (sopptr->sem_op == 0)
943 lwkt_relpooltoken(semptr);
946 * Is it really morning, or was our sleep interrupted?
947 * (Delayed check of tsleep() return code because we
948 * need to decrement sem[nz]cnt either way.)
955 kprintf("semop: good morning!\n");
962 * Process any SEM_UNDO requests.
965 for (i = 0; i < nsops; i++) {
967 * We only need to deal with SEM_UNDO's for non-zero
972 if ((sops[i].sem_flg & SEM_UNDO) == 0)
974 adjval = sops[i].sem_op;
977 eval = semundo_adjust(td->td_proc, semid,
978 sops[i].sem_num, -adjval);
983 * Oh-Oh! We ran out of either sem_undo's or undo's.
984 * Rollback the adjustments to this point and then
985 * rollback the semaphore ups and down so we can return
986 * with an error with all structures restored. We
987 * rollback the undo's in the exact reverse order that
988 * we applied them. This guarantees that we won't run
989 * out of space as we roll things back out.
991 for (j = i - 1; j >= 0; j--) {
992 if ((sops[j].sem_flg & SEM_UNDO) == 0)
994 adjval = sops[j].sem_op;
997 if (semundo_adjust(td->td_proc, semid,
998 sops[j].sem_num, adjval) != 0)
999 panic("semop - can't undo undos");
1002 for (j = 0; j < nsops; j++) {
1003 xsemptr = &semaptr->ds.sem_base[
1005 lwkt_getpooltoken(xsemptr);
1006 xsemptr->semval -= sops[j].sem_op;
1007 if (xsemptr->semval == 0 &&
1008 xsemptr->semzcnt > 0)
1010 if (xsemptr->semval <= 0 &&
1011 xsemptr->semncnt > 0)
1013 lwkt_relpooltoken(xsemptr);
1017 kprintf("eval = %d from semundo_adjust\n", eval);
1020 } /* loop through the sops */
1021 } /* if (do_undos) */
1023 /* We're definitely done - set the sempid's */
1024 for (i = 0; i < nsops; i++) {
1026 semptr = &semaptr->ds.sem_base[sopptr->sem_num];
1027 lwkt_getpooltoken(semptr);
1028 semptr->sempid = td->td_proc->p_pid;
1029 lwkt_relpooltoken(semptr);
1032 /* Do a wakeup if any semaphore was up'd. */
1034 kprintf("semop: done\n");
1036 sysmsg->sysmsg_result = 0;
1039 lockmgr(&semaptr->lk, LK_RELEASE);
1040 wakeup_end_delayed();
1046 * Go through the undo structures for this process and apply the adjustments to
1049 * (p->p_token is held by the caller)
1052 semexit(struct proc *p)
1054 struct sem_undo *suptr;
1058 * We're getting a global token, don't do it if we couldn't
1059 * possibly have any semaphores.
1061 if ((p->p_flags & P_SYSVSEM) == 0)
1063 suptr = p->p_sem_undo;
1064 KKASSERT(suptr != NULL);
1067 * Disconnect suptr from the process and increment un_refs to
1068 * prevent anyone else from being able to destroy the structure.
1069 * Do not remove it from the linked list until after we are through
1070 * scanning it as other semaphore calls might still effect it.
1072 lwkt_gettoken(&semu_token);
1073 p->p_sem_undo = NULL;
1074 p->p_flags &= ~P_SYSVSEM;
1075 suptr->un_proc = NULL;
1077 lwkt_reltoken(&semu_token);
1079 while (suptr->un_cnt) {
1080 struct semid_pool *semaptr;
1087 * These values are stable because we hold p->p_token.
1088 * However, they can get ripped out from under us when
1089 * we block or obtain other tokens so we have to re-check.
1091 ix = suptr->un_cnt - 1;
1092 semid = suptr->un_ent[ix].un_id;
1093 semnum = suptr->un_ent[ix].un_num;
1094 adjval = suptr->un_ent[ix].un_adjval;
1096 semaptr = &sema[semid];
1099 * Recheck after locking, then execute the undo
1100 * operation. semptr remains valid due to the
1103 lockmgr(&semaptr->lk, LK_SHARED);
1104 semptr = &semaptr->ds.sem_base[semnum];
1105 lwkt_getpooltoken(semptr);
1107 if (ix == suptr->un_cnt - 1 &&
1108 semid == suptr->un_ent[ix].un_id &&
1109 semnum == suptr->un_ent[ix].un_num &&
1110 adjval == suptr->un_ent[ix].un_adjval) {
1112 * Only do assertions when we aren't in a SMP race.
1114 if ((semaptr->ds.sem_perm.mode & SEM_ALLOC) == 0)
1115 panic("semexit - semid not allocated");
1116 if (semnum >= semaptr->ds.sem_nsems)
1117 panic("semexit - semnum out of range");
1121 if (semptr->semval < -adjval)
1124 semptr->semval += adjval;
1126 semptr->semval += adjval;
1130 lwkt_relpooltoken(semptr);
1131 lockmgr(&semaptr->lk, LK_RELEASE);
1135 * Final cleanup, remove from the list and deallocate on the
1138 lwkt_gettoken(&semu_token);
1139 if (--suptr->un_refs == 0) {
1140 TAILQ_REMOVE(&semu_list, suptr, un_entry);
1141 KKASSERT(suptr->un_cnt == 0);
1142 kfree(suptr, M_SEM);
1144 lwkt_reltoken(&semu_token);