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>
65 * support for audit of ipc object properties and permission changes
66 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
69 #include <linux/slab.h>
70 #include <linux/spinlock.h>
71 #include <linux/init.h>
72 #include <linux/proc_fs.h>
73 #include <linux/time.h>
74 #include <linux/smp_lock.h>
75 #include <linux/security.h>
76 #include <linux/syscalls.h>
77 #include <linux/audit.h>
78 #include <linux/capability.h>
79 #include <linux/seq_file.h>
80 #include <linux/mutex.h>
82 #include <asm/uaccess.h>
86 #define sem_lock(id) ((struct sem_array*)ipc_lock(&sem_ids,id))
87 #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
88 #define sem_rmid(id) ((struct sem_array*)ipc_rmid(&sem_ids,id))
89 #define sem_checkid(sma, semid) \
90 ipc_checkid(&sem_ids,&sma->sem_perm,semid)
91 #define sem_buildid(id, seq) \
92 ipc_buildid(&sem_ids, id, seq)
93 static struct ipc_ids sem_ids;
95 static int newary (key_t, int, int);
96 static void freeary (struct sem_array *sma, int id);
98 static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
101 #define SEMMSL_FAST 256 /* 512 bytes on stack */
102 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
105 * linked list protection:
107 * sem_array.sem_pending{,last},
108 * sem_array.sem_undo: sem_lock() for read/write
109 * sem_undo.proc_next: only "current" is allowed to read/write that field.
113 int sem_ctls[4] = {SEMMSL, SEMMNS, SEMOPM, SEMMNI};
114 #define sc_semmsl (sem_ctls[0])
115 #define sc_semmns (sem_ctls[1])
116 #define sc_semopm (sem_ctls[2])
117 #define sc_semmni (sem_ctls[3])
119 static int used_sems;
121 void __init sem_init (void)
124 ipc_init_ids(&sem_ids,sc_semmni);
125 ipc_init_proc_interface("sysvipc/sem",
126 " key semid perms nsems uid gid cuid cgid otime ctime\n",
128 sysvipc_sem_proc_show);
132 * Lockless wakeup algorithm:
133 * Without the check/retry algorithm a lockless wakeup is possible:
134 * - queue.status is initialized to -EINTR before blocking.
135 * - wakeup is performed by
136 * * unlinking the queue entry from sma->sem_pending
137 * * setting queue.status to IN_WAKEUP
138 * This is the notification for the blocked thread that a
139 * result value is imminent.
140 * * call wake_up_process
141 * * set queue.status to the final value.
142 * - the previously blocked thread checks queue.status:
143 * * if it's IN_WAKEUP, then it must wait until the value changes
144 * * if it's not -EINTR, then the operation was completed by
145 * update_queue. semtimedop can return queue.status without
146 * performing any operation on the sem array.
147 * * otherwise it must acquire the spinlock and check what's up.
149 * The two-stage algorithm is necessary to protect against the following
151 * - if queue.status is set after wake_up_process, then the woken up idle
152 * thread could race forward and try (and fail) to acquire sma->lock
153 * before update_queue had a chance to set queue.status
154 * - if queue.status is written before wake_up_process and if the
155 * blocked process is woken up by a signal between writing
156 * queue.status and the wake_up_process, then the woken up
157 * process could return from semtimedop and die by calling
158 * sys_exit before wake_up_process is called. Then wake_up_process
159 * will oops, because the task structure is already invalid.
160 * (yes, this happened on s390 with sysv msg).
165 static int newary (key_t key, int nsems, int semflg)
169 struct sem_array *sma;
174 if (used_sems + nsems > sc_semmns)
177 size = sizeof (*sma) + nsems * sizeof (struct sem);
178 sma = ipc_rcu_alloc(size);
182 memset (sma, 0, size);
184 sma->sem_perm.mode = (semflg & S_IRWXUGO);
185 sma->sem_perm.key = key;
187 sma->sem_perm.security = NULL;
188 retval = security_sem_alloc(sma);
194 id = ipc_addid(&sem_ids, &sma->sem_perm, sc_semmni);
196 security_sem_free(sma);
202 sma->sem_id = sem_buildid(id, sma->sem_perm.seq);
203 sma->sem_base = (struct sem *) &sma[1];
204 /* sma->sem_pending = NULL; */
205 sma->sem_pending_last = &sma->sem_pending;
206 /* sma->undo = NULL; */
207 sma->sem_nsems = nsems;
208 sma->sem_ctime = get_seconds();
214 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
216 int id, err = -EINVAL;
217 struct sem_array *sma;
219 if (nsems < 0 || nsems > sc_semmsl)
221 mutex_lock(&sem_ids.mutex);
223 if (key == IPC_PRIVATE) {
224 err = newary(key, nsems, semflg);
225 } else if ((id = ipc_findkey(&sem_ids, key)) == -1) { /* key not used */
226 if (!(semflg & IPC_CREAT))
229 err = newary(key, nsems, semflg);
230 } else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
235 if (nsems > sma->sem_nsems)
237 else if (ipcperms(&sma->sem_perm, semflg))
240 int semid = sem_buildid(id, sma->sem_perm.seq);
241 err = security_sem_associate(sma, semflg);
248 mutex_unlock(&sem_ids.mutex);
252 /* Manage the doubly linked list sma->sem_pending as a FIFO:
253 * insert new queue elements at the tail sma->sem_pending_last.
255 static inline void append_to_queue (struct sem_array * sma,
256 struct sem_queue * q)
258 *(q->prev = sma->sem_pending_last) = q;
259 *(sma->sem_pending_last = &q->next) = NULL;
262 static inline void prepend_to_queue (struct sem_array * sma,
263 struct sem_queue * q)
265 q->next = sma->sem_pending;
266 *(q->prev = &sma->sem_pending) = q;
268 q->next->prev = &q->next;
269 else /* sma->sem_pending_last == &sma->sem_pending */
270 sma->sem_pending_last = &q->next;
273 static inline void remove_from_queue (struct sem_array * sma,
274 struct sem_queue * q)
276 *(q->prev) = q->next;
278 q->next->prev = q->prev;
279 else /* sma->sem_pending_last == &q->next */
280 sma->sem_pending_last = q->prev;
281 q->prev = NULL; /* mark as removed */
285 * Determine whether a sequence of semaphore operations would succeed
286 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
289 static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
290 int nsops, struct sem_undo *un, int pid)
296 for (sop = sops; sop < sops + nsops; sop++) {
297 curr = sma->sem_base + sop->sem_num;
298 sem_op = sop->sem_op;
299 result = curr->semval;
301 if (!sem_op && result)
309 if (sop->sem_flg & SEM_UNDO) {
310 int undo = un->semadj[sop->sem_num] - sem_op;
312 * Exceeding the undo range is an error.
314 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
317 curr->semval = result;
321 while (sop >= sops) {
322 sma->sem_base[sop->sem_num].sempid = pid;
323 if (sop->sem_flg & SEM_UNDO)
324 un->semadj[sop->sem_num] -= sop->sem_op;
328 sma->sem_otime = get_seconds();
336 if (sop->sem_flg & IPC_NOWAIT)
343 while (sop >= sops) {
344 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
351 /* Go through the pending queue for the indicated semaphore
352 * looking for tasks that can be completed.
354 static void update_queue (struct sem_array * sma)
357 struct sem_queue * q;
359 q = sma->sem_pending;
361 error = try_atomic_semop(sma, q->sops, q->nsops,
364 /* Does q->sleeper still need to sleep? */
367 remove_from_queue(sma,q);
368 q->status = IN_WAKEUP;
370 * Continue scanning. The next operation
371 * that must be checked depends on the type of the
372 * completed operation:
373 * - if the operation modified the array, then
374 * restart from the head of the queue and
375 * check for threads that might be waiting
376 * for semaphore values to become 0.
377 * - if the operation didn't modify the array,
378 * then just continue.
381 n = sma->sem_pending;
384 wake_up_process(q->sleeper);
385 /* hands-off: q will disappear immediately after
397 /* The following counts are associated to each semaphore:
398 * semncnt number of tasks waiting on semval being nonzero
399 * semzcnt number of tasks waiting on semval being zero
400 * This model assumes that a task waits on exactly one semaphore.
401 * Since semaphore operations are to be performed atomically, tasks actually
402 * wait on a whole sequence of semaphores simultaneously.
403 * The counts we return here are a rough approximation, but still
404 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
406 static int count_semncnt (struct sem_array * sma, ushort semnum)
409 struct sem_queue * q;
412 for (q = sma->sem_pending; q; q = q->next) {
413 struct sembuf * sops = q->sops;
414 int nsops = q->nsops;
416 for (i = 0; i < nsops; i++)
417 if (sops[i].sem_num == semnum
418 && (sops[i].sem_op < 0)
419 && !(sops[i].sem_flg & IPC_NOWAIT))
424 static int count_semzcnt (struct sem_array * sma, ushort semnum)
427 struct sem_queue * q;
430 for (q = sma->sem_pending; q; q = q->next) {
431 struct sembuf * sops = q->sops;
432 int nsops = q->nsops;
434 for (i = 0; i < nsops; i++)
435 if (sops[i].sem_num == semnum
436 && (sops[i].sem_op == 0)
437 && !(sops[i].sem_flg & IPC_NOWAIT))
443 /* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
444 * the spinlock for this semaphore set hold. sem_ids.mutex remains locked
447 static void freeary (struct sem_array *sma, int id)
453 /* Invalidate the existing undo structures for this semaphore set.
454 * (They will be freed without any further action in exit_sem()
455 * or during the next semop.)
457 for (un = sma->undo; un; un = un->id_next)
460 /* Wake up all pending processes and let them fail with EIDRM. */
461 q = sma->sem_pending;
464 /* lazy remove_from_queue: we are killing the whole queue */
467 q->status = IN_WAKEUP;
468 wake_up_process(q->sleeper); /* doesn't sleep */
470 q->status = -EIDRM; /* hands-off q */
474 /* Remove the semaphore set from the ID array*/
478 used_sems -= sma->sem_nsems;
479 size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
480 security_sem_free(sma);
484 static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
488 return copy_to_user(buf, in, sizeof(*in));
493 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
495 out.sem_otime = in->sem_otime;
496 out.sem_ctime = in->sem_ctime;
497 out.sem_nsems = in->sem_nsems;
499 return copy_to_user(buf, &out, sizeof(out));
506 static int semctl_nolock(int semid, int semnum, int cmd, int version, union semun arg)
509 struct sem_array *sma;
515 struct seminfo seminfo;
518 err = security_sem_semctl(NULL, cmd);
522 memset(&seminfo,0,sizeof(seminfo));
523 seminfo.semmni = sc_semmni;
524 seminfo.semmns = sc_semmns;
525 seminfo.semmsl = sc_semmsl;
526 seminfo.semopm = sc_semopm;
527 seminfo.semvmx = SEMVMX;
528 seminfo.semmnu = SEMMNU;
529 seminfo.semmap = SEMMAP;
530 seminfo.semume = SEMUME;
531 mutex_lock(&sem_ids.mutex);
532 if (cmd == SEM_INFO) {
533 seminfo.semusz = sem_ids.in_use;
534 seminfo.semaem = used_sems;
536 seminfo.semusz = SEMUSZ;
537 seminfo.semaem = SEMAEM;
539 max_id = sem_ids.max_id;
540 mutex_unlock(&sem_ids.mutex);
541 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
543 return (max_id < 0) ? 0: max_id;
547 struct semid64_ds tbuf;
550 if(semid >= sem_ids.entries->size)
553 memset(&tbuf,0,sizeof(tbuf));
555 sma = sem_lock(semid);
560 if (ipcperms (&sma->sem_perm, S_IRUGO))
563 err = security_sem_semctl(sma, cmd);
567 id = sem_buildid(semid, sma->sem_perm.seq);
569 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
570 tbuf.sem_otime = sma->sem_otime;
571 tbuf.sem_ctime = sma->sem_ctime;
572 tbuf.sem_nsems = sma->sem_nsems;
574 if (copy_semid_to_user (arg.buf, &tbuf, version))
587 static int semctl_main(int semid, int semnum, int cmd, int version, union semun arg)
589 struct sem_array *sma;
592 ushort fast_sem_io[SEMMSL_FAST];
593 ushort* sem_io = fast_sem_io;
596 sma = sem_lock(semid);
600 nsems = sma->sem_nsems;
603 if (sem_checkid(sma,semid))
607 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
610 err = security_sem_semctl(sma, cmd);
618 ushort __user *array = arg.array;
621 if(nsems > SEMMSL_FAST) {
625 sem_io = ipc_alloc(sizeof(ushort)*nsems);
627 ipc_lock_by_ptr(&sma->sem_perm);
633 ipc_lock_by_ptr(&sma->sem_perm);
635 if (sma->sem_perm.deleted) {
642 for (i = 0; i < sma->sem_nsems; i++)
643 sem_io[i] = sma->sem_base[i].semval;
646 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
658 if(nsems > SEMMSL_FAST) {
659 sem_io = ipc_alloc(sizeof(ushort)*nsems);
661 ipc_lock_by_ptr(&sma->sem_perm);
668 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
669 ipc_lock_by_ptr(&sma->sem_perm);
676 for (i = 0; i < nsems; i++) {
677 if (sem_io[i] > SEMVMX) {
678 ipc_lock_by_ptr(&sma->sem_perm);
685 ipc_lock_by_ptr(&sma->sem_perm);
687 if (sma->sem_perm.deleted) {
693 for (i = 0; i < nsems; i++)
694 sma->sem_base[i].semval = sem_io[i];
695 for (un = sma->undo; un; un = un->id_next)
696 for (i = 0; i < nsems; i++)
698 sma->sem_ctime = get_seconds();
699 /* maybe some queued-up processes were waiting for this */
706 struct semid64_ds tbuf;
707 memset(&tbuf,0,sizeof(tbuf));
708 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
709 tbuf.sem_otime = sma->sem_otime;
710 tbuf.sem_ctime = sma->sem_ctime;
711 tbuf.sem_nsems = sma->sem_nsems;
713 if (copy_semid_to_user (arg.buf, &tbuf, version))
717 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
720 if(semnum < 0 || semnum >= nsems)
723 curr = &sma->sem_base[semnum];
733 err = count_semncnt(sma,semnum);
736 err = count_semzcnt(sma,semnum);
743 if (val > SEMVMX || val < 0)
746 for (un = sma->undo; un; un = un->id_next)
747 un->semadj[semnum] = 0;
749 curr->sempid = current->tgid;
750 sma->sem_ctime = get_seconds();
751 /* maybe some queued-up processes were waiting for this */
760 if(sem_io != fast_sem_io)
761 ipc_free(sem_io, sizeof(ushort)*nsems);
771 static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
776 struct semid64_ds tbuf;
778 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
781 out->uid = tbuf.sem_perm.uid;
782 out->gid = tbuf.sem_perm.gid;
783 out->mode = tbuf.sem_perm.mode;
789 struct semid_ds tbuf_old;
791 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
794 out->uid = tbuf_old.sem_perm.uid;
795 out->gid = tbuf_old.sem_perm.gid;
796 out->mode = tbuf_old.sem_perm.mode;
805 static int semctl_down(int semid, int semnum, int cmd, int version, union semun arg)
807 struct sem_array *sma;
809 struct sem_setbuf setbuf;
810 struct kern_ipc_perm *ipcp;
813 if(copy_semid_from_user (&setbuf, arg.buf, version))
816 sma = sem_lock(semid);
820 if (sem_checkid(sma,semid)) {
824 ipcp = &sma->sem_perm;
826 err = audit_ipc_obj(ipcp);
830 if (cmd == IPC_SET) {
831 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
835 if (current->euid != ipcp->cuid &&
836 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
841 err = security_sem_semctl(sma, cmd);
851 ipcp->uid = setbuf.uid;
852 ipcp->gid = setbuf.gid;
853 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
854 | (setbuf.mode & S_IRWXUGO);
855 sma->sem_ctime = get_seconds();
871 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
879 version = ipc_parse_version(&cmd);
885 err = semctl_nolock(semid,semnum,cmd,version,arg);
895 err = semctl_main(semid,semnum,cmd,version,arg);
899 mutex_lock(&sem_ids.mutex);
900 err = semctl_down(semid,semnum,cmd,version,arg);
901 mutex_unlock(&sem_ids.mutex);
908 static inline void lock_semundo(void)
910 struct sem_undo_list *undo_list;
912 undo_list = current->sysvsem.undo_list;
914 spin_lock(&undo_list->lock);
917 /* This code has an interaction with copy_semundo().
918 * Consider; two tasks are sharing the undo_list. task1
919 * acquires the undo_list lock in lock_semundo(). If task2 now
920 * exits before task1 releases the lock (by calling
921 * unlock_semundo()), then task1 will never call spin_unlock().
922 * This leave the sem_undo_list in a locked state. If task1 now creats task3
923 * and once again shares the sem_undo_list, the sem_undo_list will still be
924 * locked, and future SEM_UNDO operations will deadlock. This case is
925 * dealt with in copy_semundo() by having it reinitialize the spin lock when
926 * the refcnt goes from 1 to 2.
928 static inline void unlock_semundo(void)
930 struct sem_undo_list *undo_list;
932 undo_list = current->sysvsem.undo_list;
934 spin_unlock(&undo_list->lock);
938 /* If the task doesn't already have a undo_list, then allocate one
939 * here. We guarantee there is only one thread using this undo list,
940 * and current is THE ONE
942 * If this allocation and assignment succeeds, but later
943 * portions of this code fail, there is no need to free the sem_undo_list.
944 * Just let it stay associated with the task, and it'll be freed later
947 * This can block, so callers must hold no locks.
949 static inline int get_undo_list(struct sem_undo_list **undo_listp)
951 struct sem_undo_list *undo_list;
954 undo_list = current->sysvsem.undo_list;
956 size = sizeof(struct sem_undo_list);
957 undo_list = (struct sem_undo_list *) kmalloc(size, GFP_KERNEL);
958 if (undo_list == NULL)
960 memset(undo_list, 0, size);
961 spin_lock_init(&undo_list->lock);
962 atomic_set(&undo_list->refcnt, 1);
963 current->sysvsem.undo_list = undo_list;
965 *undo_listp = undo_list;
969 static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
971 struct sem_undo **last, *un;
973 last = &ulp->proc_list;
989 static struct sem_undo *find_undo(int semid)
991 struct sem_array *sma;
992 struct sem_undo_list *ulp;
993 struct sem_undo *un, *new;
997 error = get_undo_list(&ulp);
999 return ERR_PTR(error);
1002 un = lookup_undo(ulp, semid);
1004 if (likely(un!=NULL))
1007 /* no undo structure around - allocate one. */
1008 sma = sem_lock(semid);
1009 un = ERR_PTR(-EINVAL);
1012 un = ERR_PTR(-EIDRM);
1013 if (sem_checkid(sma,semid)) {
1017 nsems = sma->sem_nsems;
1018 ipc_rcu_getref(sma);
1021 new = (struct sem_undo *) kmalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1023 ipc_lock_by_ptr(&sma->sem_perm);
1024 ipc_rcu_putref(sma);
1026 return ERR_PTR(-ENOMEM);
1028 memset(new, 0, sizeof(struct sem_undo) + sizeof(short)*nsems);
1029 new->semadj = (short *) &new[1];
1033 un = lookup_undo(ulp, semid);
1037 ipc_lock_by_ptr(&sma->sem_perm);
1038 ipc_rcu_putref(sma);
1042 ipc_lock_by_ptr(&sma->sem_perm);
1043 ipc_rcu_putref(sma);
1044 if (sma->sem_perm.deleted) {
1048 un = ERR_PTR(-EIDRM);
1051 new->proc_next = ulp->proc_list;
1052 ulp->proc_list = new;
1053 new->id_next = sma->undo;
1062 asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1063 unsigned nsops, const struct timespec __user *timeout)
1065 int error = -EINVAL;
1066 struct sem_array *sma;
1067 struct sembuf fast_sops[SEMOPM_FAST];
1068 struct sembuf* sops = fast_sops, *sop;
1069 struct sem_undo *un;
1070 int undos = 0, alter = 0, max;
1071 struct sem_queue queue;
1072 unsigned long jiffies_left = 0;
1074 if (nsops < 1 || semid < 0)
1076 if (nsops > sc_semopm)
1078 if(nsops > SEMOPM_FAST) {
1079 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1083 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1088 struct timespec _timeout;
1089 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1093 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1094 _timeout.tv_nsec >= 1000000000L) {
1098 jiffies_left = timespec_to_jiffies(&_timeout);
1101 for (sop = sops; sop < sops + nsops; sop++) {
1102 if (sop->sem_num >= max)
1104 if (sop->sem_flg & SEM_UNDO)
1106 if (sop->sem_op != 0)
1112 un = find_undo(semid);
1114 error = PTR_ERR(un);
1120 sma = sem_lock(semid);
1125 if (sem_checkid(sma,semid))
1126 goto out_unlock_free;
1128 * semid identifies are not unique - find_undo may have
1129 * allocated an undo structure, it was invalidated by an RMID
1130 * and now a new array with received the same id. Check and retry.
1132 if (un && un->semid == -1) {
1137 if (max >= sma->sem_nsems)
1138 goto out_unlock_free;
1141 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1142 goto out_unlock_free;
1144 error = security_sem_semop(sma, sops, nsops, alter);
1146 goto out_unlock_free;
1148 error = try_atomic_semop (sma, sops, nsops, un, current->tgid);
1150 if (alter && error == 0)
1152 goto out_unlock_free;
1155 /* We need to sleep on this operation, so we put the current
1156 * task into the pending queue and go to sleep.
1161 queue.nsops = nsops;
1163 queue.pid = current->tgid;
1165 queue.alter = alter;
1167 append_to_queue(sma ,&queue);
1169 prepend_to_queue(sma ,&queue);
1171 queue.status = -EINTR;
1172 queue.sleeper = current;
1173 current->state = TASK_INTERRUPTIBLE;
1177 jiffies_left = schedule_timeout(jiffies_left);
1181 error = queue.status;
1182 while(unlikely(error == IN_WAKEUP)) {
1184 error = queue.status;
1187 if (error != -EINTR) {
1188 /* fast path: update_queue already obtained all requested
1193 sma = sem_lock(semid);
1195 BUG_ON(queue.prev != NULL);
1201 * If queue.status != -EINTR we are woken up by another process
1203 error = queue.status;
1204 if (error != -EINTR) {
1205 goto out_unlock_free;
1209 * If an interrupt occurred we have to clean up the queue
1211 if (timeout && jiffies_left == 0)
1213 remove_from_queue(sma,&queue);
1214 goto out_unlock_free;
1219 if(sops != fast_sops)
1224 asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1226 return sys_semtimedop(semid, tsops, nsops, NULL);
1229 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1230 * parent and child tasks.
1232 * See the notes above unlock_semundo() regarding the spin_lock_init()
1233 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1234 * because of the reasoning in the comment above unlock_semundo.
1237 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1239 struct sem_undo_list *undo_list;
1242 if (clone_flags & CLONE_SYSVSEM) {
1243 error = get_undo_list(&undo_list);
1246 atomic_inc(&undo_list->refcnt);
1247 tsk->sysvsem.undo_list = undo_list;
1249 tsk->sysvsem.undo_list = NULL;
1255 * add semadj values to semaphores, free undo structures.
1256 * undo structures are not freed when semaphore arrays are destroyed
1257 * so some of them may be out of date.
1258 * IMPLEMENTATION NOTE: There is some confusion over whether the
1259 * set of adjustments that needs to be done should be done in an atomic
1260 * manner or not. That is, if we are attempting to decrement the semval
1261 * should we queue up and wait until we can do so legally?
1262 * The original implementation attempted to do this (queue and wait).
1263 * The current implementation does not do so. The POSIX standard
1264 * and SVID should be consulted to determine what behavior is mandated.
1266 void exit_sem(struct task_struct *tsk)
1268 struct sem_undo_list *undo_list;
1269 struct sem_undo *u, **up;
1271 undo_list = tsk->sysvsem.undo_list;
1275 if (!atomic_dec_and_test(&undo_list->refcnt))
1278 /* There's no need to hold the semundo list lock, as current
1279 * is the last task exiting for this undo list.
1281 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1282 struct sem_array *sma;
1284 struct sem_undo *un, **unp;
1291 sma = sem_lock(semid);
1298 BUG_ON(sem_checkid(sma,u->semid));
1300 /* remove u from the sma->undo list */
1301 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1305 printk ("exit_sem undo list error id=%d\n", u->semid);
1309 /* perform adjustments registered in u */
1310 nsems = sma->sem_nsems;
1311 for (i = 0; i < nsems; i++) {
1312 struct sem * semaphore = &sma->sem_base[i];
1314 semaphore->semval += u->semadj[i];
1316 * Range checks of the new semaphore value,
1317 * not defined by sus:
1318 * - Some unices ignore the undo entirely
1319 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1320 * - some cap the value (e.g. FreeBSD caps
1321 * at 0, but doesn't enforce SEMVMX)
1323 * Linux caps the semaphore value, both at 0
1326 * Manfred <manfred@colorfullife.com>
1328 if (semaphore->semval < 0)
1329 semaphore->semval = 0;
1330 if (semaphore->semval > SEMVMX)
1331 semaphore->semval = SEMVMX;
1332 semaphore->sempid = current->tgid;
1335 sma->sem_otime = get_seconds();
1336 /* maybe some queued-up processes were waiting for this */
1344 #ifdef CONFIG_PROC_FS
1345 static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
1347 struct sem_array *sma = it;
1349 return seq_printf(s,
1350 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",