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/config.h>
70 #include <linux/slab.h>
71 #include <linux/spinlock.h>
72 #include <linux/init.h>
73 #include <linux/proc_fs.h>
74 #include <linux/time.h>
75 #include <linux/smp_lock.h>
76 #include <linux/security.h>
77 #include <linux/syscalls.h>
78 #include <linux/audit.h>
79 #include <linux/capability.h>
80 #include <linux/seq_file.h>
81 #include <linux/mutex.h>
83 #include <asm/uaccess.h>
87 #define sem_lock(id) ((struct sem_array*)ipc_lock(&sem_ids,id))
88 #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
89 #define sem_rmid(id) ((struct sem_array*)ipc_rmid(&sem_ids,id))
90 #define sem_checkid(sma, semid) \
91 ipc_checkid(&sem_ids,&sma->sem_perm,semid)
92 #define sem_buildid(id, seq) \
93 ipc_buildid(&sem_ids, id, seq)
94 static struct ipc_ids sem_ids;
96 static int newary (key_t, int, int);
97 static void freeary (struct sem_array *sma, int id);
99 static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
102 #define SEMMSL_FAST 256 /* 512 bytes on stack */
103 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
106 * linked list protection:
108 * sem_array.sem_pending{,last},
109 * sem_array.sem_undo: sem_lock() for read/write
110 * sem_undo.proc_next: only "current" is allowed to read/write that field.
114 int sem_ctls[4] = {SEMMSL, SEMMNS, SEMOPM, SEMMNI};
115 #define sc_semmsl (sem_ctls[0])
116 #define sc_semmns (sem_ctls[1])
117 #define sc_semopm (sem_ctls[2])
118 #define sc_semmni (sem_ctls[3])
120 static int used_sems;
122 void __init sem_init (void)
125 ipc_init_ids(&sem_ids,sc_semmni);
126 ipc_init_proc_interface("sysvipc/sem",
127 " key semid perms nsems uid gid cuid cgid otime ctime\n",
129 sysvipc_sem_proc_show);
133 * Lockless wakeup algorithm:
134 * Without the check/retry algorithm a lockless wakeup is possible:
135 * - queue.status is initialized to -EINTR before blocking.
136 * - wakeup is performed by
137 * * unlinking the queue entry from sma->sem_pending
138 * * setting queue.status to IN_WAKEUP
139 * This is the notification for the blocked thread that a
140 * result value is imminent.
141 * * call wake_up_process
142 * * set queue.status to the final value.
143 * - the previously blocked thread checks queue.status:
144 * * if it's IN_WAKEUP, then it must wait until the value changes
145 * * if it's not -EINTR, then the operation was completed by
146 * update_queue. semtimedop can return queue.status without
147 * performing any operation on the sem array.
148 * * otherwise it must acquire the spinlock and check what's up.
150 * The two-stage algorithm is necessary to protect against the following
152 * - if queue.status is set after wake_up_process, then the woken up idle
153 * thread could race forward and try (and fail) to acquire sma->lock
154 * before update_queue had a chance to set queue.status
155 * - if queue.status is written before wake_up_process and if the
156 * blocked process is woken up by a signal between writing
157 * queue.status and the wake_up_process, then the woken up
158 * process could return from semtimedop and die by calling
159 * sys_exit before wake_up_process is called. Then wake_up_process
160 * will oops, because the task structure is already invalid.
161 * (yes, this happened on s390 with sysv msg).
166 static int newary (key_t key, int nsems, int semflg)
170 struct sem_array *sma;
175 if (used_sems + nsems > sc_semmns)
178 size = sizeof (*sma) + nsems * sizeof (struct sem);
179 sma = ipc_rcu_alloc(size);
183 memset (sma, 0, size);
185 sma->sem_perm.mode = (semflg & S_IRWXUGO);
186 sma->sem_perm.key = key;
188 sma->sem_perm.security = NULL;
189 retval = security_sem_alloc(sma);
195 id = ipc_addid(&sem_ids, &sma->sem_perm, sc_semmni);
197 security_sem_free(sma);
203 sma->sem_id = sem_buildid(id, sma->sem_perm.seq);
204 sma->sem_base = (struct sem *) &sma[1];
205 /* sma->sem_pending = NULL; */
206 sma->sem_pending_last = &sma->sem_pending;
207 /* sma->undo = NULL; */
208 sma->sem_nsems = nsems;
209 sma->sem_ctime = get_seconds();
215 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
217 int id, err = -EINVAL;
218 struct sem_array *sma;
220 if (nsems < 0 || nsems > sc_semmsl)
222 mutex_lock(&sem_ids.mutex);
224 if (key == IPC_PRIVATE) {
225 err = newary(key, nsems, semflg);
226 } else if ((id = ipc_findkey(&sem_ids, key)) == -1) { /* key not used */
227 if (!(semflg & IPC_CREAT))
230 err = newary(key, nsems, semflg);
231 } else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
236 if (nsems > sma->sem_nsems)
238 else if (ipcperms(&sma->sem_perm, semflg))
241 int semid = sem_buildid(id, sma->sem_perm.seq);
242 err = security_sem_associate(sma, semflg);
249 mutex_unlock(&sem_ids.mutex);
253 /* Manage the doubly linked list sma->sem_pending as a FIFO:
254 * insert new queue elements at the tail sma->sem_pending_last.
256 static inline void append_to_queue (struct sem_array * sma,
257 struct sem_queue * q)
259 *(q->prev = sma->sem_pending_last) = q;
260 *(sma->sem_pending_last = &q->next) = NULL;
263 static inline void prepend_to_queue (struct sem_array * sma,
264 struct sem_queue * q)
266 q->next = sma->sem_pending;
267 *(q->prev = &sma->sem_pending) = q;
269 q->next->prev = &q->next;
270 else /* sma->sem_pending_last == &sma->sem_pending */
271 sma->sem_pending_last = &q->next;
274 static inline void remove_from_queue (struct sem_array * sma,
275 struct sem_queue * q)
277 *(q->prev) = q->next;
279 q->next->prev = q->prev;
280 else /* sma->sem_pending_last == &q->next */
281 sma->sem_pending_last = q->prev;
282 q->prev = NULL; /* mark as removed */
286 * Determine whether a sequence of semaphore operations would succeed
287 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
290 static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
291 int nsops, struct sem_undo *un, int pid)
297 for (sop = sops; sop < sops + nsops; sop++) {
298 curr = sma->sem_base + sop->sem_num;
299 sem_op = sop->sem_op;
300 result = curr->semval;
302 if (!sem_op && result)
310 if (sop->sem_flg & SEM_UNDO) {
311 int undo = un->semadj[sop->sem_num] - sem_op;
313 * Exceeding the undo range is an error.
315 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
318 curr->semval = result;
322 while (sop >= sops) {
323 sma->sem_base[sop->sem_num].sempid = pid;
324 if (sop->sem_flg & SEM_UNDO)
325 un->semadj[sop->sem_num] -= sop->sem_op;
329 sma->sem_otime = get_seconds();
337 if (sop->sem_flg & IPC_NOWAIT)
344 while (sop >= sops) {
345 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
352 /* Go through the pending queue for the indicated semaphore
353 * looking for tasks that can be completed.
355 static void update_queue (struct sem_array * sma)
358 struct sem_queue * q;
360 q = sma->sem_pending;
362 error = try_atomic_semop(sma, q->sops, q->nsops,
365 /* Does q->sleeper still need to sleep? */
368 remove_from_queue(sma,q);
369 q->status = IN_WAKEUP;
371 * Continue scanning. The next operation
372 * that must be checked depends on the type of the
373 * completed operation:
374 * - if the operation modified the array, then
375 * restart from the head of the queue and
376 * check for threads that might be waiting
377 * for semaphore values to become 0.
378 * - if the operation didn't modify the array,
379 * then just continue.
382 n = sma->sem_pending;
385 wake_up_process(q->sleeper);
386 /* hands-off: q will disappear immediately after
398 /* The following counts are associated to each semaphore:
399 * semncnt number of tasks waiting on semval being nonzero
400 * semzcnt number of tasks waiting on semval being zero
401 * This model assumes that a task waits on exactly one semaphore.
402 * Since semaphore operations are to be performed atomically, tasks actually
403 * wait on a whole sequence of semaphores simultaneously.
404 * The counts we return here are a rough approximation, but still
405 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
407 static int count_semncnt (struct sem_array * sma, ushort semnum)
410 struct sem_queue * q;
413 for (q = sma->sem_pending; q; q = q->next) {
414 struct sembuf * sops = q->sops;
415 int nsops = q->nsops;
417 for (i = 0; i < nsops; i++)
418 if (sops[i].sem_num == semnum
419 && (sops[i].sem_op < 0)
420 && !(sops[i].sem_flg & IPC_NOWAIT))
425 static int count_semzcnt (struct sem_array * sma, ushort semnum)
428 struct sem_queue * q;
431 for (q = sma->sem_pending; q; q = q->next) {
432 struct sembuf * sops = q->sops;
433 int nsops = q->nsops;
435 for (i = 0; i < nsops; i++)
436 if (sops[i].sem_num == semnum
437 && (sops[i].sem_op == 0)
438 && !(sops[i].sem_flg & IPC_NOWAIT))
444 /* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
445 * the spinlock for this semaphore set hold. sem_ids.mutex remains locked
448 static void freeary (struct sem_array *sma, int id)
454 /* Invalidate the existing undo structures for this semaphore set.
455 * (They will be freed without any further action in exit_sem()
456 * or during the next semop.)
458 for (un = sma->undo; un; un = un->id_next)
461 /* Wake up all pending processes and let them fail with EIDRM. */
462 q = sma->sem_pending;
465 /* lazy remove_from_queue: we are killing the whole queue */
468 q->status = IN_WAKEUP;
469 wake_up_process(q->sleeper); /* doesn't sleep */
471 q->status = -EIDRM; /* hands-off q */
475 /* Remove the semaphore set from the ID array*/
479 used_sems -= sma->sem_nsems;
480 size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
481 security_sem_free(sma);
485 static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
489 return copy_to_user(buf, in, sizeof(*in));
494 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
496 out.sem_otime = in->sem_otime;
497 out.sem_ctime = in->sem_ctime;
498 out.sem_nsems = in->sem_nsems;
500 return copy_to_user(buf, &out, sizeof(out));
507 static int semctl_nolock(int semid, int semnum, int cmd, int version, union semun arg)
510 struct sem_array *sma;
516 struct seminfo seminfo;
519 err = security_sem_semctl(NULL, cmd);
523 memset(&seminfo,0,sizeof(seminfo));
524 seminfo.semmni = sc_semmni;
525 seminfo.semmns = sc_semmns;
526 seminfo.semmsl = sc_semmsl;
527 seminfo.semopm = sc_semopm;
528 seminfo.semvmx = SEMVMX;
529 seminfo.semmnu = SEMMNU;
530 seminfo.semmap = SEMMAP;
531 seminfo.semume = SEMUME;
532 mutex_lock(&sem_ids.mutex);
533 if (cmd == SEM_INFO) {
534 seminfo.semusz = sem_ids.in_use;
535 seminfo.semaem = used_sems;
537 seminfo.semusz = SEMUSZ;
538 seminfo.semaem = SEMAEM;
540 max_id = sem_ids.max_id;
541 mutex_unlock(&sem_ids.mutex);
542 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
544 return (max_id < 0) ? 0: max_id;
548 struct semid64_ds tbuf;
551 if(semid >= sem_ids.entries->size)
554 memset(&tbuf,0,sizeof(tbuf));
556 sma = sem_lock(semid);
561 if (ipcperms (&sma->sem_perm, S_IRUGO))
564 err = security_sem_semctl(sma, cmd);
568 id = sem_buildid(semid, sma->sem_perm.seq);
570 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
571 tbuf.sem_otime = sma->sem_otime;
572 tbuf.sem_ctime = sma->sem_ctime;
573 tbuf.sem_nsems = sma->sem_nsems;
575 if (copy_semid_to_user (arg.buf, &tbuf, version))
588 static int semctl_main(int semid, int semnum, int cmd, int version, union semun arg)
590 struct sem_array *sma;
593 ushort fast_sem_io[SEMMSL_FAST];
594 ushort* sem_io = fast_sem_io;
597 sma = sem_lock(semid);
601 nsems = sma->sem_nsems;
604 if (sem_checkid(sma,semid))
608 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
611 err = security_sem_semctl(sma, cmd);
619 ushort __user *array = arg.array;
622 if(nsems > SEMMSL_FAST) {
626 sem_io = ipc_alloc(sizeof(ushort)*nsems);
628 ipc_lock_by_ptr(&sma->sem_perm);
634 ipc_lock_by_ptr(&sma->sem_perm);
636 if (sma->sem_perm.deleted) {
643 for (i = 0; i < sma->sem_nsems; i++)
644 sem_io[i] = sma->sem_base[i].semval;
647 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
659 if(nsems > SEMMSL_FAST) {
660 sem_io = ipc_alloc(sizeof(ushort)*nsems);
662 ipc_lock_by_ptr(&sma->sem_perm);
669 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
670 ipc_lock_by_ptr(&sma->sem_perm);
677 for (i = 0; i < nsems; i++) {
678 if (sem_io[i] > SEMVMX) {
679 ipc_lock_by_ptr(&sma->sem_perm);
686 ipc_lock_by_ptr(&sma->sem_perm);
688 if (sma->sem_perm.deleted) {
694 for (i = 0; i < nsems; i++)
695 sma->sem_base[i].semval = sem_io[i];
696 for (un = sma->undo; un; un = un->id_next)
697 for (i = 0; i < nsems; i++)
699 sma->sem_ctime = get_seconds();
700 /* maybe some queued-up processes were waiting for this */
707 struct semid64_ds tbuf;
708 memset(&tbuf,0,sizeof(tbuf));
709 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
710 tbuf.sem_otime = sma->sem_otime;
711 tbuf.sem_ctime = sma->sem_ctime;
712 tbuf.sem_nsems = sma->sem_nsems;
714 if (copy_semid_to_user (arg.buf, &tbuf, version))
718 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
721 if(semnum < 0 || semnum >= nsems)
724 curr = &sma->sem_base[semnum];
734 err = count_semncnt(sma,semnum);
737 err = count_semzcnt(sma,semnum);
744 if (val > SEMVMX || val < 0)
747 for (un = sma->undo; un; un = un->id_next)
748 un->semadj[semnum] = 0;
750 curr->sempid = current->tgid;
751 sma->sem_ctime = get_seconds();
752 /* maybe some queued-up processes were waiting for this */
761 if(sem_io != fast_sem_io)
762 ipc_free(sem_io, sizeof(ushort)*nsems);
772 static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
777 struct semid64_ds tbuf;
779 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
782 out->uid = tbuf.sem_perm.uid;
783 out->gid = tbuf.sem_perm.gid;
784 out->mode = tbuf.sem_perm.mode;
790 struct semid_ds tbuf_old;
792 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
795 out->uid = tbuf_old.sem_perm.uid;
796 out->gid = tbuf_old.sem_perm.gid;
797 out->mode = tbuf_old.sem_perm.mode;
806 static int semctl_down(int semid, int semnum, int cmd, int version, union semun arg)
808 struct sem_array *sma;
810 struct sem_setbuf setbuf;
811 struct kern_ipc_perm *ipcp;
814 if(copy_semid_from_user (&setbuf, arg.buf, version))
817 sma = sem_lock(semid);
821 if (sem_checkid(sma,semid)) {
825 ipcp = &sma->sem_perm;
827 err = audit_ipc_obj(ipcp);
831 if (cmd == IPC_SET) {
832 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
836 if (current->euid != ipcp->cuid &&
837 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
842 err = security_sem_semctl(sma, cmd);
852 ipcp->uid = setbuf.uid;
853 ipcp->gid = setbuf.gid;
854 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
855 | (setbuf.mode & S_IRWXUGO);
856 sma->sem_ctime = get_seconds();
872 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
880 version = ipc_parse_version(&cmd);
886 err = semctl_nolock(semid,semnum,cmd,version,arg);
896 err = semctl_main(semid,semnum,cmd,version,arg);
900 mutex_lock(&sem_ids.mutex);
901 err = semctl_down(semid,semnum,cmd,version,arg);
902 mutex_unlock(&sem_ids.mutex);
909 static inline void lock_semundo(void)
911 struct sem_undo_list *undo_list;
913 undo_list = current->sysvsem.undo_list;
915 spin_lock(&undo_list->lock);
918 /* This code has an interaction with copy_semundo().
919 * Consider; two tasks are sharing the undo_list. task1
920 * acquires the undo_list lock in lock_semundo(). If task2 now
921 * exits before task1 releases the lock (by calling
922 * unlock_semundo()), then task1 will never call spin_unlock().
923 * This leave the sem_undo_list in a locked state. If task1 now creats task3
924 * and once again shares the sem_undo_list, the sem_undo_list will still be
925 * locked, and future SEM_UNDO operations will deadlock. This case is
926 * dealt with in copy_semundo() by having it reinitialize the spin lock when
927 * the refcnt goes from 1 to 2.
929 static inline void unlock_semundo(void)
931 struct sem_undo_list *undo_list;
933 undo_list = current->sysvsem.undo_list;
935 spin_unlock(&undo_list->lock);
939 /* If the task doesn't already have a undo_list, then allocate one
940 * here. We guarantee there is only one thread using this undo list,
941 * and current is THE ONE
943 * If this allocation and assignment succeeds, but later
944 * portions of this code fail, there is no need to free the sem_undo_list.
945 * Just let it stay associated with the task, and it'll be freed later
948 * This can block, so callers must hold no locks.
950 static inline int get_undo_list(struct sem_undo_list **undo_listp)
952 struct sem_undo_list *undo_list;
955 undo_list = current->sysvsem.undo_list;
957 size = sizeof(struct sem_undo_list);
958 undo_list = (struct sem_undo_list *) kmalloc(size, GFP_KERNEL);
959 if (undo_list == NULL)
961 memset(undo_list, 0, size);
962 spin_lock_init(&undo_list->lock);
963 atomic_set(&undo_list->refcnt, 1);
964 current->sysvsem.undo_list = undo_list;
966 *undo_listp = undo_list;
970 static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
972 struct sem_undo **last, *un;
974 last = &ulp->proc_list;
990 static struct sem_undo *find_undo(int semid)
992 struct sem_array *sma;
993 struct sem_undo_list *ulp;
994 struct sem_undo *un, *new;
998 error = get_undo_list(&ulp);
1000 return ERR_PTR(error);
1003 un = lookup_undo(ulp, semid);
1005 if (likely(un!=NULL))
1008 /* no undo structure around - allocate one. */
1009 sma = sem_lock(semid);
1010 un = ERR_PTR(-EINVAL);
1013 un = ERR_PTR(-EIDRM);
1014 if (sem_checkid(sma,semid)) {
1018 nsems = sma->sem_nsems;
1019 ipc_rcu_getref(sma);
1022 new = (struct sem_undo *) kmalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1024 ipc_lock_by_ptr(&sma->sem_perm);
1025 ipc_rcu_putref(sma);
1027 return ERR_PTR(-ENOMEM);
1029 memset(new, 0, sizeof(struct sem_undo) + sizeof(short)*nsems);
1030 new->semadj = (short *) &new[1];
1034 un = lookup_undo(ulp, semid);
1038 ipc_lock_by_ptr(&sma->sem_perm);
1039 ipc_rcu_putref(sma);
1043 ipc_lock_by_ptr(&sma->sem_perm);
1044 ipc_rcu_putref(sma);
1045 if (sma->sem_perm.deleted) {
1049 un = ERR_PTR(-EIDRM);
1052 new->proc_next = ulp->proc_list;
1053 ulp->proc_list = new;
1054 new->id_next = sma->undo;
1063 asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1064 unsigned nsops, const struct timespec __user *timeout)
1066 int error = -EINVAL;
1067 struct sem_array *sma;
1068 struct sembuf fast_sops[SEMOPM_FAST];
1069 struct sembuf* sops = fast_sops, *sop;
1070 struct sem_undo *un;
1071 int undos = 0, alter = 0, max;
1072 struct sem_queue queue;
1073 unsigned long jiffies_left = 0;
1075 if (nsops < 1 || semid < 0)
1077 if (nsops > sc_semopm)
1079 if(nsops > SEMOPM_FAST) {
1080 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1084 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1089 struct timespec _timeout;
1090 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1094 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1095 _timeout.tv_nsec >= 1000000000L) {
1099 jiffies_left = timespec_to_jiffies(&_timeout);
1102 for (sop = sops; sop < sops + nsops; sop++) {
1103 if (sop->sem_num >= max)
1105 if (sop->sem_flg & SEM_UNDO)
1107 if (sop->sem_op != 0)
1113 un = find_undo(semid);
1115 error = PTR_ERR(un);
1121 sma = sem_lock(semid);
1126 if (sem_checkid(sma,semid))
1127 goto out_unlock_free;
1129 * semid identifies are not unique - find_undo may have
1130 * allocated an undo structure, it was invalidated by an RMID
1131 * and now a new array with received the same id. Check and retry.
1133 if (un && un->semid == -1) {
1138 if (max >= sma->sem_nsems)
1139 goto out_unlock_free;
1142 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1143 goto out_unlock_free;
1145 error = security_sem_semop(sma, sops, nsops, alter);
1147 goto out_unlock_free;
1149 error = try_atomic_semop (sma, sops, nsops, un, current->tgid);
1151 if (alter && error == 0)
1153 goto out_unlock_free;
1156 /* We need to sleep on this operation, so we put the current
1157 * task into the pending queue and go to sleep.
1162 queue.nsops = nsops;
1164 queue.pid = current->tgid;
1166 queue.alter = alter;
1168 append_to_queue(sma ,&queue);
1170 prepend_to_queue(sma ,&queue);
1172 queue.status = -EINTR;
1173 queue.sleeper = current;
1174 current->state = TASK_INTERRUPTIBLE;
1178 jiffies_left = schedule_timeout(jiffies_left);
1182 error = queue.status;
1183 while(unlikely(error == IN_WAKEUP)) {
1185 error = queue.status;
1188 if (error != -EINTR) {
1189 /* fast path: update_queue already obtained all requested
1194 sma = sem_lock(semid);
1196 BUG_ON(queue.prev != NULL);
1202 * If queue.status != -EINTR we are woken up by another process
1204 error = queue.status;
1205 if (error != -EINTR) {
1206 goto out_unlock_free;
1210 * If an interrupt occurred we have to clean up the queue
1212 if (timeout && jiffies_left == 0)
1214 remove_from_queue(sma,&queue);
1215 goto out_unlock_free;
1220 if(sops != fast_sops)
1225 asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1227 return sys_semtimedop(semid, tsops, nsops, NULL);
1230 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1231 * parent and child tasks.
1233 * See the notes above unlock_semundo() regarding the spin_lock_init()
1234 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1235 * because of the reasoning in the comment above unlock_semundo.
1238 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1240 struct sem_undo_list *undo_list;
1243 if (clone_flags & CLONE_SYSVSEM) {
1244 error = get_undo_list(&undo_list);
1247 atomic_inc(&undo_list->refcnt);
1248 tsk->sysvsem.undo_list = undo_list;
1250 tsk->sysvsem.undo_list = NULL;
1256 * add semadj values to semaphores, free undo structures.
1257 * undo structures are not freed when semaphore arrays are destroyed
1258 * so some of them may be out of date.
1259 * IMPLEMENTATION NOTE: There is some confusion over whether the
1260 * set of adjustments that needs to be done should be done in an atomic
1261 * manner or not. That is, if we are attempting to decrement the semval
1262 * should we queue up and wait until we can do so legally?
1263 * The original implementation attempted to do this (queue and wait).
1264 * The current implementation does not do so. The POSIX standard
1265 * and SVID should be consulted to determine what behavior is mandated.
1267 void exit_sem(struct task_struct *tsk)
1269 struct sem_undo_list *undo_list;
1270 struct sem_undo *u, **up;
1272 undo_list = tsk->sysvsem.undo_list;
1276 if (!atomic_dec_and_test(&undo_list->refcnt))
1279 /* There's no need to hold the semundo list lock, as current
1280 * is the last task exiting for this undo list.
1282 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1283 struct sem_array *sma;
1285 struct sem_undo *un, **unp;
1292 sma = sem_lock(semid);
1299 BUG_ON(sem_checkid(sma,u->semid));
1301 /* remove u from the sma->undo list */
1302 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1306 printk ("exit_sem undo list error id=%d\n", u->semid);
1310 /* perform adjustments registered in u */
1311 nsems = sma->sem_nsems;
1312 for (i = 0; i < nsems; i++) {
1313 struct sem * semaphore = &sma->sem_base[i];
1315 semaphore->semval += u->semadj[i];
1317 * Range checks of the new semaphore value,
1318 * not defined by sus:
1319 * - Some unices ignore the undo entirely
1320 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1321 * - some cap the value (e.g. FreeBSD caps
1322 * at 0, but doesn't enforce SEMVMX)
1324 * Linux caps the semaphore value, both at 0
1327 * Manfred <manfred@colorfullife.com>
1329 if (semaphore->semval < 0)
1330 semaphore->semval = 0;
1331 if (semaphore->semval > SEMVMX)
1332 semaphore->semval = SEMVMX;
1333 semaphore->sempid = current->tgid;
1336 sma->sem_otime = get_seconds();
1337 /* maybe some queued-up processes were waiting for this */
1345 #ifdef CONFIG_PROC_FS
1346 static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
1348 struct sem_array *sma = it;
1350 return seq_printf(s,
1351 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",