3 * Copyright (C) 1992 Krishna Balasubramanian
4 * Copyright (C) 1995 Eric Schenk, Bruno Haible
6 * IMPLEMENTATION NOTES ON CODE REWRITE (Eric Schenk, January 1995):
7 * This code underwent a massive rewrite in order to solve some problems
8 * with the original code. In particular the original code failed to
9 * wake up processes that were waiting for semval to go to 0 if the
10 * value went to 0 and was then incremented rapidly enough. In solving
11 * this problem I have also modified the implementation so that it
12 * processes pending operations in a FIFO manner, thus give a guarantee
13 * that processes waiting for a lock on the semaphore won't starve
14 * unless another locking process fails to unlock.
15 * In addition the following two changes in behavior have been introduced:
16 * - The original implementation of semop returned the value
17 * last semaphore element examined on success. This does not
18 * match the manual page specifications, and effectively
19 * allows the user to read the semaphore even if they do not
20 * have read permissions. The implementation now returns 0
21 * on success as stated in the manual page.
22 * - There is some confusion over whether the set of undo adjustments
23 * to be performed at exit should be done in an atomic manner.
24 * That is, if we are attempting to decrement the semval should we queue
25 * up and wait until we can do so legally?
26 * The original implementation attempted to do this.
27 * The current implementation does not do so. This is because I don't
28 * think it is the right thing (TM) to do, and because I couldn't
29 * see a clean way to get the old behavior with the new design.
30 * The POSIX standard and SVID should be consulted to determine
31 * what behavior is mandated.
33 * Further notes on refinement (Christoph Rohland, December 1998):
34 * - The POSIX standard says, that the undo adjustments simply should
35 * redo. So the current implementation is o.K.
36 * - The previous code had two flaws:
37 * 1) It actively gave the semaphore to the next waiting process
38 * sleeping on the semaphore. Since this process did not have the
39 * cpu this led to many unnecessary context switches and bad
40 * performance. Now we only check which process should be able to
41 * get the semaphore and if this process wants to reduce some
42 * semaphore value we simply wake it up without doing the
43 * operation. So it has to try to get it later. Thus e.g. the
44 * running process may reacquire the semaphore during the current
45 * time slice. If it only waits for zero or increases the semaphore,
46 * we do the operation in advance and wake it up.
47 * 2) It did not wake up all zero waiting processes. We try to do
48 * better but only get the semops right which only wait for zero or
49 * increase. If there are decrement operations in the operations
50 * array we do the same as before.
52 * With the incarnation of O(1) scheduler, it becomes unnecessary to perform
53 * check/retry algorithm for waking up blocked processes as the new scheduler
54 * is better at handling thread switch than the old one.
56 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
58 * SMP-threaded, sysctl's added
59 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
60 * Enforced range limit on SEM_UNDO
61 * (c) 2001 Red Hat Inc <alan@redhat.com>
63 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
66 #include <linux/config.h>
67 #include <linux/slab.h>
68 #include <linux/spinlock.h>
69 #include <linux/init.h>
70 #include <linux/proc_fs.h>
71 #include <linux/time.h>
72 #include <linux/smp_lock.h>
73 #include <linux/security.h>
74 #include <linux/syscalls.h>
75 #include <linux/audit.h>
76 #include <linux/capability.h>
77 #include <linux/seq_file.h>
78 #include <asm/uaccess.h>
82 #define sem_lock(id) ((struct sem_array*)ipc_lock(&sem_ids,id))
83 #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
84 #define sem_rmid(id) ((struct sem_array*)ipc_rmid(&sem_ids,id))
85 #define sem_checkid(sma, semid) \
86 ipc_checkid(&sem_ids,&sma->sem_perm,semid)
87 #define sem_buildid(id, seq) \
88 ipc_buildid(&sem_ids, id, seq)
89 static struct ipc_ids sem_ids;
91 static int newary (key_t, int, int);
92 static void freeary (struct sem_array *sma, int id);
94 static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
97 #define SEMMSL_FAST 256 /* 512 bytes on stack */
98 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
101 * linked list protection:
103 * sem_array.sem_pending{,last},
104 * sem_array.sem_undo: sem_lock() for read/write
105 * sem_undo.proc_next: only "current" is allowed to read/write that field.
109 int sem_ctls[4] = {SEMMSL, SEMMNS, SEMOPM, SEMMNI};
110 #define sc_semmsl (sem_ctls[0])
111 #define sc_semmns (sem_ctls[1])
112 #define sc_semopm (sem_ctls[2])
113 #define sc_semmni (sem_ctls[3])
115 static int used_sems;
117 void __init sem_init (void)
120 ipc_init_ids(&sem_ids,sc_semmni);
121 ipc_init_proc_interface("sysvipc/sem",
122 " key semid perms nsems uid gid cuid cgid otime ctime\n",
124 sysvipc_sem_proc_show);
128 * Lockless wakeup algorithm:
129 * Without the check/retry algorithm a lockless wakeup is possible:
130 * - queue.status is initialized to -EINTR before blocking.
131 * - wakeup is performed by
132 * * unlinking the queue entry from sma->sem_pending
133 * * setting queue.status to IN_WAKEUP
134 * This is the notification for the blocked thread that a
135 * result value is imminent.
136 * * call wake_up_process
137 * * set queue.status to the final value.
138 * - the previously blocked thread checks queue.status:
139 * * if it's IN_WAKEUP, then it must wait until the value changes
140 * * if it's not -EINTR, then the operation was completed by
141 * update_queue. semtimedop can return queue.status without
142 * performing any operation on the semaphore array.
143 * * otherwise it must acquire the spinlock and check what's up.
145 * The two-stage algorithm is necessary to protect against the following
147 * - if queue.status is set after wake_up_process, then the woken up idle
148 * thread could race forward and try (and fail) to acquire sma->lock
149 * before update_queue had a chance to set queue.status
150 * - if queue.status is written before wake_up_process and if the
151 * blocked process is woken up by a signal between writing
152 * queue.status and the wake_up_process, then the woken up
153 * process could return from semtimedop and die by calling
154 * sys_exit before wake_up_process is called. Then wake_up_process
155 * will oops, because the task structure is already invalid.
156 * (yes, this happened on s390 with sysv msg).
161 static int newary (key_t key, int nsems, int semflg)
165 struct sem_array *sma;
170 if (used_sems + nsems > sc_semmns)
173 size = sizeof (*sma) + nsems * sizeof (struct sem);
174 sma = ipc_rcu_alloc(size);
178 memset (sma, 0, size);
180 sma->sem_perm.mode = (semflg & S_IRWXUGO);
181 sma->sem_perm.key = key;
182 sma->sem_perm.xid = vx_current_xid();
184 sma->sem_perm.security = NULL;
185 retval = security_sem_alloc(sma);
191 id = ipc_addid(&sem_ids, &sma->sem_perm, sc_semmni);
193 security_sem_free(sma);
199 sma->sem_id = sem_buildid(id, sma->sem_perm.seq);
200 sma->sem_base = (struct sem *) &sma[1];
201 /* sma->sem_pending = NULL; */
202 sma->sem_pending_last = &sma->sem_pending;
203 /* sma->undo = NULL; */
204 sma->sem_nsems = nsems;
205 sma->sem_ctime = get_seconds();
211 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
213 int id, err = -EINVAL;
214 struct sem_array *sma;
216 if (nsems < 0 || nsems > sc_semmsl)
220 if (key == IPC_PRIVATE) {
221 err = newary(key, nsems, semflg);
222 } else if ((id = ipc_findkey(&sem_ids, key)) == -1) { /* key not used */
223 if (!(semflg & IPC_CREAT))
226 err = newary(key, nsems, semflg);
227 } else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
233 if (nsems > sma->sem_nsems)
235 else if (ipcperms(&sma->sem_perm, semflg))
238 int semid = sem_buildid(id, sma->sem_perm.seq);
239 err = security_sem_associate(sma, semflg);
250 /* Manage the doubly linked list sma->sem_pending as a FIFO:
251 * insert new queue elements at the tail sma->sem_pending_last.
253 static inline void append_to_queue (struct sem_array * sma,
254 struct sem_queue * q)
256 *(q->prev = sma->sem_pending_last) = q;
257 *(sma->sem_pending_last = &q->next) = NULL;
260 static inline void prepend_to_queue (struct sem_array * sma,
261 struct sem_queue * q)
263 q->next = sma->sem_pending;
264 *(q->prev = &sma->sem_pending) = q;
266 q->next->prev = &q->next;
267 else /* sma->sem_pending_last == &sma->sem_pending */
268 sma->sem_pending_last = &q->next;
271 static inline void remove_from_queue (struct sem_array * sma,
272 struct sem_queue * q)
274 *(q->prev) = q->next;
276 q->next->prev = q->prev;
277 else /* sma->sem_pending_last == &q->next */
278 sma->sem_pending_last = q->prev;
279 q->prev = NULL; /* mark as removed */
283 * Determine whether a sequence of semaphore operations would succeed
284 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
287 static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
288 int nsops, struct sem_undo *un, int pid)
294 for (sop = sops; sop < sops + nsops; sop++) {
295 curr = sma->sem_base + sop->sem_num;
296 sem_op = sop->sem_op;
297 result = curr->semval;
299 if (!sem_op && result)
307 if (sop->sem_flg & SEM_UNDO) {
308 int undo = un->semadj[sop->sem_num] - sem_op;
310 * Exceeding the undo range is an error.
312 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
315 curr->semval = result;
319 while (sop >= sops) {
320 sma->sem_base[sop->sem_num].sempid = pid;
321 if (sop->sem_flg & SEM_UNDO)
322 un->semadj[sop->sem_num] -= sop->sem_op;
326 sma->sem_otime = get_seconds();
334 if (sop->sem_flg & IPC_NOWAIT)
341 while (sop >= sops) {
342 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
349 /* Go through the pending queue for the indicated semaphore
350 * looking for tasks that can be completed.
352 static void update_queue (struct sem_array * sma)
355 struct sem_queue * q;
357 q = sma->sem_pending;
359 error = try_atomic_semop(sma, q->sops, q->nsops,
362 /* Does q->sleeper still need to sleep? */
365 remove_from_queue(sma,q);
366 q->status = IN_WAKEUP;
368 * Continue scanning. The next operation
369 * that must be checked depends on the type of the
370 * completed operation:
371 * - if the operation modified the array, then
372 * restart from the head of the queue and
373 * check for threads that might be waiting
374 * for semaphore values to become 0.
375 * - if the operation didn't modify the array,
376 * then just continue.
379 n = sma->sem_pending;
382 wake_up_process(q->sleeper);
383 /* hands-off: q will disappear immediately after
395 /* The following counts are associated to each semaphore:
396 * semncnt number of tasks waiting on semval being nonzero
397 * semzcnt number of tasks waiting on semval being zero
398 * This model assumes that a task waits on exactly one semaphore.
399 * Since semaphore operations are to be performed atomically, tasks actually
400 * wait on a whole sequence of semaphores simultaneously.
401 * The counts we return here are a rough approximation, but still
402 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
404 static int count_semncnt (struct sem_array * sma, ushort semnum)
407 struct sem_queue * q;
410 for (q = sma->sem_pending; q; q = q->next) {
411 struct sembuf * sops = q->sops;
412 int nsops = q->nsops;
414 for (i = 0; i < nsops; i++)
415 if (sops[i].sem_num == semnum
416 && (sops[i].sem_op < 0)
417 && !(sops[i].sem_flg & IPC_NOWAIT))
422 static int count_semzcnt (struct sem_array * sma, ushort semnum)
425 struct sem_queue * q;
428 for (q = sma->sem_pending; q; q = q->next) {
429 struct sembuf * sops = q->sops;
430 int nsops = q->nsops;
432 for (i = 0; i < nsops; i++)
433 if (sops[i].sem_num == semnum
434 && (sops[i].sem_op == 0)
435 && !(sops[i].sem_flg & IPC_NOWAIT))
441 /* Free a semaphore set. freeary() is called with sem_ids.sem down and
442 * the spinlock for this semaphore set hold. sem_ids.sem remains locked
445 static void freeary (struct sem_array *sma, int id)
451 /* Invalidate the existing undo structures for this semaphore set.
452 * (They will be freed without any further action in exit_sem()
453 * or during the next semop.)
455 for (un = sma->undo; un; un = un->id_next)
458 /* Wake up all pending processes and let them fail with EIDRM. */
459 q = sma->sem_pending;
462 /* lazy remove_from_queue: we are killing the whole queue */
465 q->status = IN_WAKEUP;
466 wake_up_process(q->sleeper); /* doesn't sleep */
468 q->status = -EIDRM; /* hands-off q */
472 /* Remove the semaphore set from the ID array*/
476 used_sems -= sma->sem_nsems;
477 size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
478 security_sem_free(sma);
482 static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
486 return copy_to_user(buf, in, sizeof(*in));
491 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
493 out.sem_otime = in->sem_otime;
494 out.sem_ctime = in->sem_ctime;
495 out.sem_nsems = in->sem_nsems;
497 return copy_to_user(buf, &out, sizeof(out));
504 static int semctl_nolock(int semid, int semnum, int cmd, int version, union semun arg)
507 struct sem_array *sma;
513 struct seminfo seminfo;
516 err = security_sem_semctl(NULL, cmd);
520 memset(&seminfo,0,sizeof(seminfo));
521 seminfo.semmni = sc_semmni;
522 seminfo.semmns = sc_semmns;
523 seminfo.semmsl = sc_semmsl;
524 seminfo.semopm = sc_semopm;
525 seminfo.semvmx = SEMVMX;
526 seminfo.semmnu = SEMMNU;
527 seminfo.semmap = SEMMAP;
528 seminfo.semume = SEMUME;
530 if (cmd == SEM_INFO) {
531 seminfo.semusz = sem_ids.in_use;
532 seminfo.semaem = used_sems;
534 seminfo.semusz = SEMUSZ;
535 seminfo.semaem = SEMAEM;
537 max_id = sem_ids.max_id;
539 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
541 return (max_id < 0) ? 0: max_id;
545 struct semid64_ds tbuf;
548 if(semid >= sem_ids.entries->size)
551 memset(&tbuf,0,sizeof(tbuf));
553 sma = sem_lock(semid);
558 if (ipcperms (&sma->sem_perm, S_IRUGO))
561 err = security_sem_semctl(sma, cmd);
565 id = sem_buildid(semid, sma->sem_perm.seq);
567 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
568 tbuf.sem_otime = sma->sem_otime;
569 tbuf.sem_ctime = sma->sem_ctime;
570 tbuf.sem_nsems = sma->sem_nsems;
572 if (copy_semid_to_user (arg.buf, &tbuf, version))
585 static int semctl_main(int semid, int semnum, int cmd, int version, union semun arg)
587 struct sem_array *sma;
590 ushort fast_sem_io[SEMMSL_FAST];
591 ushort* sem_io = fast_sem_io;
594 sma = sem_lock(semid);
598 nsems = sma->sem_nsems;
601 if (sem_checkid(sma,semid))
605 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
608 err = security_sem_semctl(sma, cmd);
616 ushort __user *array = arg.array;
619 if(nsems > SEMMSL_FAST) {
623 sem_io = ipc_alloc(sizeof(ushort)*nsems);
625 ipc_lock_by_ptr(&sma->sem_perm);
631 ipc_lock_by_ptr(&sma->sem_perm);
633 if (sma->sem_perm.deleted) {
640 for (i = 0; i < sma->sem_nsems; i++)
641 sem_io[i] = sma->sem_base[i].semval;
644 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
656 if(nsems > SEMMSL_FAST) {
657 sem_io = ipc_alloc(sizeof(ushort)*nsems);
659 ipc_lock_by_ptr(&sma->sem_perm);
666 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
667 ipc_lock_by_ptr(&sma->sem_perm);
674 for (i = 0; i < nsems; i++) {
675 if (sem_io[i] > SEMVMX) {
676 ipc_lock_by_ptr(&sma->sem_perm);
683 ipc_lock_by_ptr(&sma->sem_perm);
685 if (sma->sem_perm.deleted) {
691 for (i = 0; i < nsems; i++)
692 sma->sem_base[i].semval = sem_io[i];
693 for (un = sma->undo; un; un = un->id_next)
694 for (i = 0; i < nsems; i++)
696 sma->sem_ctime = get_seconds();
697 /* maybe some queued-up processes were waiting for this */
704 struct semid64_ds tbuf;
705 memset(&tbuf,0,sizeof(tbuf));
706 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
707 tbuf.sem_otime = sma->sem_otime;
708 tbuf.sem_ctime = sma->sem_ctime;
709 tbuf.sem_nsems = sma->sem_nsems;
711 if (copy_semid_to_user (arg.buf, &tbuf, version))
715 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
718 if(semnum < 0 || semnum >= nsems)
721 curr = &sma->sem_base[semnum];
731 err = count_semncnt(sma,semnum);
734 err = count_semzcnt(sma,semnum);
741 if (val > SEMVMX || val < 0)
744 for (un = sma->undo; un; un = un->id_next)
745 un->semadj[semnum] = 0;
747 curr->sempid = current->tgid;
748 sma->sem_ctime = get_seconds();
749 /* maybe some queued-up processes were waiting for this */
758 if(sem_io != fast_sem_io)
759 ipc_free(sem_io, sizeof(ushort)*nsems);
769 static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
774 struct semid64_ds tbuf;
776 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
779 out->uid = tbuf.sem_perm.uid;
780 out->gid = tbuf.sem_perm.gid;
781 out->mode = tbuf.sem_perm.mode;
787 struct semid_ds tbuf_old;
789 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
792 out->uid = tbuf_old.sem_perm.uid;
793 out->gid = tbuf_old.sem_perm.gid;
794 out->mode = tbuf_old.sem_perm.mode;
803 static int semctl_down(int semid, int semnum, int cmd, int version, union semun arg)
805 struct sem_array *sma;
807 struct sem_setbuf setbuf;
808 struct kern_ipc_perm *ipcp;
811 if(copy_semid_from_user (&setbuf, arg.buf, version))
813 if ((err = audit_ipc_perms(0, setbuf.uid, setbuf.gid, setbuf.mode)))
816 sma = sem_lock(semid);
820 if (sem_checkid(sma,semid)) {
824 ipcp = &sma->sem_perm;
826 if (current->euid != ipcp->cuid &&
827 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
832 err = security_sem_semctl(sma, cmd);
842 ipcp->uid = setbuf.uid;
843 ipcp->gid = setbuf.gid;
844 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
845 | (setbuf.mode & S_IRWXUGO);
846 sma->sem_ctime = get_seconds();
862 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
870 version = ipc_parse_version(&cmd);
876 err = semctl_nolock(semid,semnum,cmd,version,arg);
886 err = semctl_main(semid,semnum,cmd,version,arg);
891 err = semctl_down(semid,semnum,cmd,version,arg);
899 static inline void lock_semundo(void)
901 struct sem_undo_list *undo_list;
903 undo_list = current->sysvsem.undo_list;
905 spin_lock(&undo_list->lock);
908 /* This code has an interaction with copy_semundo().
909 * Consider; two tasks are sharing the undo_list. task1
910 * acquires the undo_list lock in lock_semundo(). If task2 now
911 * exits before task1 releases the lock (by calling
912 * unlock_semundo()), then task1 will never call spin_unlock().
913 * This leave the sem_undo_list in a locked state. If task1 now creats task3
914 * and once again shares the sem_undo_list, the sem_undo_list will still be
915 * locked, and future SEM_UNDO operations will deadlock. This case is
916 * dealt with in copy_semundo() by having it reinitialize the spin lock when
917 * the refcnt goes from 1 to 2.
919 static inline void unlock_semundo(void)
921 struct sem_undo_list *undo_list;
923 undo_list = current->sysvsem.undo_list;
925 spin_unlock(&undo_list->lock);
929 /* If the task doesn't already have a undo_list, then allocate one
930 * here. We guarantee there is only one thread using this undo list,
931 * and current is THE ONE
933 * If this allocation and assignment succeeds, but later
934 * portions of this code fail, there is no need to free the sem_undo_list.
935 * Just let it stay associated with the task, and it'll be freed later
938 * This can block, so callers must hold no locks.
940 static inline int get_undo_list(struct sem_undo_list **undo_listp)
942 struct sem_undo_list *undo_list;
945 undo_list = current->sysvsem.undo_list;
947 size = sizeof(struct sem_undo_list);
948 undo_list = (struct sem_undo_list *) kmalloc(size, GFP_KERNEL);
949 if (undo_list == NULL)
951 memset(undo_list, 0, size);
952 spin_lock_init(&undo_list->lock);
953 atomic_set(&undo_list->refcnt, 1);
954 current->sysvsem.undo_list = undo_list;
956 *undo_listp = undo_list;
960 static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
962 struct sem_undo **last, *un;
964 last = &ulp->proc_list;
980 static struct sem_undo *find_undo(int semid)
982 struct sem_array *sma;
983 struct sem_undo_list *ulp;
984 struct sem_undo *un, *new;
988 error = get_undo_list(&ulp);
990 return ERR_PTR(error);
993 un = lookup_undo(ulp, semid);
995 if (likely(un!=NULL))
998 /* no undo structure around - allocate one. */
999 sma = sem_lock(semid);
1000 un = ERR_PTR(-EINVAL);
1003 un = ERR_PTR(-EIDRM);
1004 if (sem_checkid(sma,semid)) {
1008 nsems = sma->sem_nsems;
1009 ipc_rcu_getref(sma);
1012 new = (struct sem_undo *) kmalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1014 ipc_lock_by_ptr(&sma->sem_perm);
1015 ipc_rcu_putref(sma);
1017 return ERR_PTR(-ENOMEM);
1019 memset(new, 0, sizeof(struct sem_undo) + sizeof(short)*nsems);
1020 new->semadj = (short *) &new[1];
1024 un = lookup_undo(ulp, semid);
1028 ipc_lock_by_ptr(&sma->sem_perm);
1029 ipc_rcu_putref(sma);
1033 ipc_lock_by_ptr(&sma->sem_perm);
1034 ipc_rcu_putref(sma);
1035 if (sma->sem_perm.deleted) {
1039 un = ERR_PTR(-EIDRM);
1042 new->proc_next = ulp->proc_list;
1043 ulp->proc_list = new;
1044 new->id_next = sma->undo;
1053 asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1054 unsigned nsops, const struct timespec __user *timeout)
1056 int error = -EINVAL;
1057 struct sem_array *sma;
1058 struct sembuf fast_sops[SEMOPM_FAST];
1059 struct sembuf* sops = fast_sops, *sop;
1060 struct sem_undo *un;
1061 int undos = 0, alter = 0, max;
1062 struct sem_queue queue;
1063 unsigned long jiffies_left = 0;
1065 if (nsops < 1 || semid < 0)
1067 if (nsops > sc_semopm)
1069 if(nsops > SEMOPM_FAST) {
1070 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1074 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1079 struct timespec _timeout;
1080 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1084 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1085 _timeout.tv_nsec >= 1000000000L) {
1089 jiffies_left = timespec_to_jiffies(&_timeout);
1092 for (sop = sops; sop < sops + nsops; sop++) {
1093 if (sop->sem_num >= max)
1095 if (sop->sem_flg & SEM_UNDO)
1097 if (sop->sem_op != 0)
1103 un = find_undo(semid);
1105 error = PTR_ERR(un);
1111 sma = sem_lock(semid);
1116 if (sem_checkid(sma,semid))
1117 goto out_unlock_free;
1119 * semid identifies are not unique - find_undo may have
1120 * allocated an undo structure, it was invalidated by an RMID
1121 * and now a new array with received the same id. Check and retry.
1123 if (un && un->semid == -1) {
1128 if (max >= sma->sem_nsems)
1129 goto out_unlock_free;
1132 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1133 goto out_unlock_free;
1135 error = security_sem_semop(sma, sops, nsops, alter);
1137 goto out_unlock_free;
1139 error = try_atomic_semop (sma, sops, nsops, un, current->tgid);
1141 if (alter && error == 0)
1143 goto out_unlock_free;
1146 /* We need to sleep on this operation, so we put the current
1147 * task into the pending queue and go to sleep.
1152 queue.nsops = nsops;
1154 queue.pid = current->tgid;
1156 queue.alter = alter;
1158 append_to_queue(sma ,&queue);
1160 prepend_to_queue(sma ,&queue);
1162 queue.status = -EINTR;
1163 queue.sleeper = current;
1164 current->state = TASK_INTERRUPTIBLE;
1168 jiffies_left = schedule_timeout(jiffies_left);
1172 error = queue.status;
1173 while(unlikely(error == IN_WAKEUP)) {
1175 error = queue.status;
1178 if (error != -EINTR) {
1179 /* fast path: update_queue already obtained all requested
1184 sma = sem_lock(semid);
1186 if(queue.prev != NULL)
1193 * If queue.status != -EINTR we are woken up by another process
1195 error = queue.status;
1196 if (error != -EINTR) {
1197 goto out_unlock_free;
1201 * If an interrupt occurred we have to clean up the queue
1203 if (timeout && jiffies_left == 0)
1205 remove_from_queue(sma,&queue);
1206 goto out_unlock_free;
1211 if(sops != fast_sops)
1216 asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1218 return sys_semtimedop(semid, tsops, nsops, NULL);
1221 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1222 * parent and child tasks.
1224 * See the notes above unlock_semundo() regarding the spin_lock_init()
1225 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1226 * because of the reasoning in the comment above unlock_semundo.
1229 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1231 struct sem_undo_list *undo_list;
1234 if (clone_flags & CLONE_SYSVSEM) {
1235 error = get_undo_list(&undo_list);
1238 atomic_inc(&undo_list->refcnt);
1239 tsk->sysvsem.undo_list = undo_list;
1241 tsk->sysvsem.undo_list = NULL;
1247 * add semadj values to semaphores, free undo structures.
1248 * undo structures are not freed when semaphore arrays are destroyed
1249 * so some of them may be out of date.
1250 * IMPLEMENTATION NOTE: There is some confusion over whether the
1251 * set of adjustments that needs to be done should be done in an atomic
1252 * manner or not. That is, if we are attempting to decrement the semval
1253 * should we queue up and wait until we can do so legally?
1254 * The original implementation attempted to do this (queue and wait).
1255 * The current implementation does not do so. The POSIX standard
1256 * and SVID should be consulted to determine what behavior is mandated.
1258 void exit_sem(struct task_struct *tsk)
1260 struct sem_undo_list *undo_list;
1261 struct sem_undo *u, **up;
1263 undo_list = tsk->sysvsem.undo_list;
1267 if (!atomic_dec_and_test(&undo_list->refcnt))
1270 /* There's no need to hold the semundo list lock, as current
1271 * is the last task exiting for this undo list.
1273 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1274 struct sem_array *sma;
1276 struct sem_undo *un, **unp;
1283 sma = sem_lock(semid);
1290 BUG_ON(sem_checkid(sma,u->semid));
1292 /* remove u from the sma->undo list */
1293 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1297 printk ("exit_sem undo list error id=%d\n", u->semid);
1301 /* perform adjustments registered in u */
1302 nsems = sma->sem_nsems;
1303 for (i = 0; i < nsems; i++) {
1304 struct sem * sem = &sma->sem_base[i];
1306 sem->semval += u->semadj[i];
1308 * Range checks of the new semaphore value,
1309 * not defined by sus:
1310 * - Some unices ignore the undo entirely
1311 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1312 * - some cap the value (e.g. FreeBSD caps
1313 * at 0, but doesn't enforce SEMVMX)
1315 * Linux caps the semaphore value, both at 0
1318 * Manfred <manfred@colorfullife.com>
1320 if (sem->semval < 0)
1322 if (sem->semval > SEMVMX)
1323 sem->semval = SEMVMX;
1324 sem->sempid = current->tgid;
1327 sma->sem_otime = get_seconds();
1328 /* maybe some queued-up processes were waiting for this */
1336 #ifdef CONFIG_PROC_FS
1337 static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
1339 struct sem_array *sma = it;
1341 if (!vx_check(sma->sem_perm.xid, VX_IDENT))
1344 return seq_printf(s,
1345 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",