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>
70 * Pavel Emelianov <xemul@openvz.org>
73 #include <linux/slab.h>
74 #include <linux/spinlock.h>
75 #include <linux/init.h>
76 #include <linux/proc_fs.h>
77 #include <linux/time.h>
78 #include <linux/smp_lock.h>
79 #include <linux/security.h>
80 #include <linux/syscalls.h>
81 #include <linux/audit.h>
82 #include <linux/capability.h>
83 #include <linux/seq_file.h>
84 #include <linux/mutex.h>
85 #include <linux/nsproxy.h>
86 #include <linux/vs_base.h>
87 #include <linux/vs_limit.h>
89 #include <asm/uaccess.h>
92 #define sem_ids(ns) (*((ns)->ids[IPC_SEM_IDS]))
94 #define sem_lock(ns, id) ((struct sem_array*)ipc_lock(&sem_ids(ns), id))
95 #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
96 #define sem_rmid(ns, id) ((struct sem_array*)ipc_rmid(&sem_ids(ns), id))
97 #define sem_checkid(ns, sma, semid) \
98 ipc_checkid(&sem_ids(ns),&sma->sem_perm,semid)
99 #define sem_buildid(ns, id, seq) \
100 ipc_buildid(&sem_ids(ns), id, seq)
102 static struct ipc_ids init_sem_ids;
104 static int newary(struct ipc_namespace *, key_t, int, int);
105 static void freeary(struct ipc_namespace *ns, struct sem_array *sma, int id);
106 #ifdef CONFIG_PROC_FS
107 static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
110 #define SEMMSL_FAST 256 /* 512 bytes on stack */
111 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
114 * linked list protection:
116 * sem_array.sem_pending{,last},
117 * sem_array.sem_undo: sem_lock() for read/write
118 * sem_undo.proc_next: only "current" is allowed to read/write that field.
122 #define sc_semmsl sem_ctls[0]
123 #define sc_semmns sem_ctls[1]
124 #define sc_semopm sem_ctls[2]
125 #define sc_semmni sem_ctls[3]
127 static void __ipc_init __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
129 ns->ids[IPC_SEM_IDS] = ids;
130 ns->sc_semmsl = SEMMSL;
131 ns->sc_semmns = SEMMNS;
132 ns->sc_semopm = SEMOPM;
133 ns->sc_semmni = SEMMNI;
135 ipc_init_ids(ids, ns->sc_semmni);
139 int sem_init_ns(struct ipc_namespace *ns)
143 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
147 __sem_init_ns(ns, ids);
151 void sem_exit_ns(struct ipc_namespace *ns)
154 struct sem_array *sma;
156 mutex_lock(&sem_ids(ns).mutex);
157 for (i = 0; i <= sem_ids(ns).max_id; i++) {
158 sma = sem_lock(ns, i);
164 mutex_unlock(&sem_ids(ns).mutex);
166 ipc_fini_ids(ns->ids[IPC_SEM_IDS]);
167 kfree(ns->ids[IPC_SEM_IDS]);
168 ns->ids[IPC_SEM_IDS] = NULL;
172 void __init sem_init (void)
174 __sem_init_ns(&init_ipc_ns, &init_sem_ids);
175 ipc_init_proc_interface("sysvipc/sem",
176 " key semid perms nsems uid gid cuid cgid otime ctime\n",
177 IPC_SEM_IDS, sysvipc_sem_proc_show);
181 * Lockless wakeup algorithm:
182 * Without the check/retry algorithm a lockless wakeup is possible:
183 * - queue.status is initialized to -EINTR before blocking.
184 * - wakeup is performed by
185 * * unlinking the queue entry from sma->sem_pending
186 * * setting queue.status to IN_WAKEUP
187 * This is the notification for the blocked thread that a
188 * result value is imminent.
189 * * call wake_up_process
190 * * set queue.status to the final value.
191 * - the previously blocked thread checks queue.status:
192 * * if it's IN_WAKEUP, then it must wait until the value changes
193 * * if it's not -EINTR, then the operation was completed by
194 * update_queue. semtimedop can return queue.status without
195 * performing any operation on the sem array.
196 * * otherwise it must acquire the spinlock and check what's up.
198 * The two-stage algorithm is necessary to protect against the following
200 * - if queue.status is set after wake_up_process, then the woken up idle
201 * thread could race forward and try (and fail) to acquire sma->lock
202 * before update_queue had a chance to set queue.status
203 * - if queue.status is written before wake_up_process and if the
204 * blocked process is woken up by a signal between writing
205 * queue.status and the wake_up_process, then the woken up
206 * process could return from semtimedop and die by calling
207 * sys_exit before wake_up_process is called. Then wake_up_process
208 * will oops, because the task structure is already invalid.
209 * (yes, this happened on s390 with sysv msg).
214 static int newary (struct ipc_namespace *ns, key_t key, int nsems, int semflg)
218 struct sem_array *sma;
223 if (ns->used_sems + nsems > ns->sc_semmns)
226 size = sizeof (*sma) + nsems * sizeof (struct sem);
227 sma = ipc_rcu_alloc(size);
231 memset (sma, 0, size);
233 sma->sem_perm.mode = (semflg & S_IRWXUGO);
234 sma->sem_perm.key = key;
235 sma->sem_perm.xid = vx_current_xid();
237 sma->sem_perm.security = NULL;
238 retval = security_sem_alloc(sma);
244 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
246 security_sem_free(sma);
250 ns->used_sems += nsems;
251 /* FIXME: obsoleted? */
253 vx_nsems_add(sma, nsems);
255 sma->sem_id = sem_buildid(ns, id, sma->sem_perm.seq);
256 sma->sem_base = (struct sem *) &sma[1];
257 /* sma->sem_pending = NULL; */
258 sma->sem_pending_last = &sma->sem_pending;
259 /* sma->undo = NULL; */
260 sma->sem_nsems = nsems;
261 sma->sem_ctime = get_seconds();
267 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
269 int id, err = -EINVAL;
270 struct sem_array *sma;
271 struct ipc_namespace *ns;
273 ns = current->nsproxy->ipc_ns;
275 if (nsems < 0 || nsems > ns->sc_semmsl)
277 mutex_lock(&sem_ids(ns).mutex);
279 if (key == IPC_PRIVATE) {
280 err = newary(ns, key, nsems, semflg);
281 } else if ((id = ipc_findkey(&sem_ids(ns), key)) == -1) { /* key not used */
282 if (!(semflg & IPC_CREAT))
285 err = newary(ns, key, nsems, semflg);
286 } else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
289 sma = sem_lock(ns, id);
291 if (nsems > sma->sem_nsems)
293 else if (ipcperms(&sma->sem_perm, semflg))
296 int semid = sem_buildid(ns, id, sma->sem_perm.seq);
297 err = security_sem_associate(sma, semflg);
304 mutex_unlock(&sem_ids(ns).mutex);
308 /* Manage the doubly linked list sma->sem_pending as a FIFO:
309 * insert new queue elements at the tail sma->sem_pending_last.
311 static inline void append_to_queue (struct sem_array * sma,
312 struct sem_queue * q)
314 *(q->prev = sma->sem_pending_last) = q;
315 *(sma->sem_pending_last = &q->next) = NULL;
318 static inline void prepend_to_queue (struct sem_array * sma,
319 struct sem_queue * q)
321 q->next = sma->sem_pending;
322 *(q->prev = &sma->sem_pending) = q;
324 q->next->prev = &q->next;
325 else /* sma->sem_pending_last == &sma->sem_pending */
326 sma->sem_pending_last = &q->next;
329 static inline void remove_from_queue (struct sem_array * sma,
330 struct sem_queue * q)
332 *(q->prev) = q->next;
334 q->next->prev = q->prev;
335 else /* sma->sem_pending_last == &q->next */
336 sma->sem_pending_last = q->prev;
337 q->prev = NULL; /* mark as removed */
341 * Determine whether a sequence of semaphore operations would succeed
342 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
345 static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
346 int nsops, struct sem_undo *un, int pid)
352 for (sop = sops; sop < sops + nsops; sop++) {
353 curr = sma->sem_base + sop->sem_num;
354 sem_op = sop->sem_op;
355 result = curr->semval;
357 if (!sem_op && result)
365 if (sop->sem_flg & SEM_UNDO) {
366 int undo = un->semadj[sop->sem_num] - sem_op;
368 * Exceeding the undo range is an error.
370 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
373 curr->semval = result;
377 while (sop >= sops) {
378 sma->sem_base[sop->sem_num].sempid = pid;
379 if (sop->sem_flg & SEM_UNDO)
380 un->semadj[sop->sem_num] -= sop->sem_op;
384 sma->sem_otime = get_seconds();
392 if (sop->sem_flg & IPC_NOWAIT)
399 while (sop >= sops) {
400 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
407 /* Go through the pending queue for the indicated semaphore
408 * looking for tasks that can be completed.
410 static void update_queue (struct sem_array * sma)
413 struct sem_queue * q;
415 q = sma->sem_pending;
417 error = try_atomic_semop(sma, q->sops, q->nsops,
420 /* Does q->sleeper still need to sleep? */
423 remove_from_queue(sma,q);
424 q->status = IN_WAKEUP;
426 * Continue scanning. The next operation
427 * that must be checked depends on the type of the
428 * completed operation:
429 * - if the operation modified the array, then
430 * restart from the head of the queue and
431 * check for threads that might be waiting
432 * for semaphore values to become 0.
433 * - if the operation didn't modify the array,
434 * then just continue.
437 n = sma->sem_pending;
440 wake_up_process(q->sleeper);
441 /* hands-off: q will disappear immediately after
453 /* The following counts are associated to each semaphore:
454 * semncnt number of tasks waiting on semval being nonzero
455 * semzcnt number of tasks waiting on semval being zero
456 * This model assumes that a task waits on exactly one semaphore.
457 * Since semaphore operations are to be performed atomically, tasks actually
458 * wait on a whole sequence of semaphores simultaneously.
459 * The counts we return here are a rough approximation, but still
460 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
462 static int count_semncnt (struct sem_array * sma, ushort semnum)
465 struct sem_queue * q;
468 for (q = sma->sem_pending; q; q = q->next) {
469 struct sembuf * sops = q->sops;
470 int nsops = q->nsops;
472 for (i = 0; i < nsops; i++)
473 if (sops[i].sem_num == semnum
474 && (sops[i].sem_op < 0)
475 && !(sops[i].sem_flg & IPC_NOWAIT))
480 static int count_semzcnt (struct sem_array * sma, ushort semnum)
483 struct sem_queue * q;
486 for (q = sma->sem_pending; q; q = q->next) {
487 struct sembuf * sops = q->sops;
488 int nsops = q->nsops;
490 for (i = 0; i < nsops; i++)
491 if (sops[i].sem_num == semnum
492 && (sops[i].sem_op == 0)
493 && !(sops[i].sem_flg & IPC_NOWAIT))
499 /* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
500 * the spinlock for this semaphore set hold. sem_ids.mutex remains locked
503 static void freeary (struct ipc_namespace *ns, struct sem_array *sma, int id)
509 /* Invalidate the existing undo structures for this semaphore set.
510 * (They will be freed without any further action in exit_sem()
511 * or during the next semop.)
513 for (un = sma->undo; un; un = un->id_next)
516 /* Wake up all pending processes and let them fail with EIDRM. */
517 q = sma->sem_pending;
520 /* lazy remove_from_queue: we are killing the whole queue */
523 q->status = IN_WAKEUP;
524 wake_up_process(q->sleeper); /* doesn't sleep */
526 q->status = -EIDRM; /* hands-off q */
530 /* Remove the semaphore set from the ID array*/
531 sma = sem_rmid(ns, id);
534 ns->used_sems -= sma->sem_nsems;
535 /* FIXME: obsoleted? */
536 vx_nsems_sub(sma, sma->sem_nsems);
538 size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
539 security_sem_free(sma);
543 static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
547 return copy_to_user(buf, in, sizeof(*in));
552 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
554 out.sem_otime = in->sem_otime;
555 out.sem_ctime = in->sem_ctime;
556 out.sem_nsems = in->sem_nsems;
558 return copy_to_user(buf, &out, sizeof(out));
565 static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
566 int cmd, int version, union semun arg)
569 struct sem_array *sma;
575 struct seminfo seminfo;
578 err = security_sem_semctl(NULL, cmd);
582 memset(&seminfo,0,sizeof(seminfo));
583 seminfo.semmni = ns->sc_semmni;
584 seminfo.semmns = ns->sc_semmns;
585 seminfo.semmsl = ns->sc_semmsl;
586 seminfo.semopm = ns->sc_semopm;
587 seminfo.semvmx = SEMVMX;
588 seminfo.semmnu = SEMMNU;
589 seminfo.semmap = SEMMAP;
590 seminfo.semume = SEMUME;
591 mutex_lock(&sem_ids(ns).mutex);
592 if (cmd == SEM_INFO) {
593 seminfo.semusz = sem_ids(ns).in_use;
594 seminfo.semaem = ns->used_sems;
596 seminfo.semusz = SEMUSZ;
597 seminfo.semaem = SEMAEM;
599 max_id = sem_ids(ns).max_id;
600 mutex_unlock(&sem_ids(ns).mutex);
601 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
603 return (max_id < 0) ? 0: max_id;
607 struct semid64_ds tbuf;
610 if(semid >= sem_ids(ns).entries->size)
613 memset(&tbuf,0,sizeof(tbuf));
615 sma = sem_lock(ns, semid);
620 if (ipcperms (&sma->sem_perm, S_IRUGO))
623 err = security_sem_semctl(sma, cmd);
627 id = sem_buildid(ns, semid, sma->sem_perm.seq);
629 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
630 tbuf.sem_otime = sma->sem_otime;
631 tbuf.sem_ctime = sma->sem_ctime;
632 tbuf.sem_nsems = sma->sem_nsems;
634 if (copy_semid_to_user (arg.buf, &tbuf, version))
647 static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
648 int cmd, int version, union semun arg)
650 struct sem_array *sma;
653 ushort fast_sem_io[SEMMSL_FAST];
654 ushort* sem_io = fast_sem_io;
657 sma = sem_lock(ns, semid);
661 nsems = sma->sem_nsems;
664 if (sem_checkid(ns,sma,semid))
668 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
671 err = security_sem_semctl(sma, cmd);
679 ushort __user *array = arg.array;
682 if(nsems > SEMMSL_FAST) {
686 sem_io = ipc_alloc(sizeof(ushort)*nsems);
688 ipc_lock_by_ptr(&sma->sem_perm);
694 ipc_lock_by_ptr(&sma->sem_perm);
696 if (sma->sem_perm.deleted) {
703 for (i = 0; i < sma->sem_nsems; i++)
704 sem_io[i] = sma->sem_base[i].semval;
707 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
719 if(nsems > SEMMSL_FAST) {
720 sem_io = ipc_alloc(sizeof(ushort)*nsems);
722 ipc_lock_by_ptr(&sma->sem_perm);
729 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
730 ipc_lock_by_ptr(&sma->sem_perm);
737 for (i = 0; i < nsems; i++) {
738 if (sem_io[i] > SEMVMX) {
739 ipc_lock_by_ptr(&sma->sem_perm);
746 ipc_lock_by_ptr(&sma->sem_perm);
748 if (sma->sem_perm.deleted) {
754 for (i = 0; i < nsems; i++)
755 sma->sem_base[i].semval = sem_io[i];
756 for (un = sma->undo; un; un = un->id_next)
757 for (i = 0; i < nsems; i++)
759 sma->sem_ctime = get_seconds();
760 /* maybe some queued-up processes were waiting for this */
767 struct semid64_ds tbuf;
768 memset(&tbuf,0,sizeof(tbuf));
769 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
770 tbuf.sem_otime = sma->sem_otime;
771 tbuf.sem_ctime = sma->sem_ctime;
772 tbuf.sem_nsems = sma->sem_nsems;
774 if (copy_semid_to_user (arg.buf, &tbuf, version))
778 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
781 if(semnum < 0 || semnum >= nsems)
784 curr = &sma->sem_base[semnum];
794 err = count_semncnt(sma,semnum);
797 err = count_semzcnt(sma,semnum);
804 if (val > SEMVMX || val < 0)
807 for (un = sma->undo; un; un = un->id_next)
808 un->semadj[semnum] = 0;
810 curr->sempid = current->tgid;
811 sma->sem_ctime = get_seconds();
812 /* maybe some queued-up processes were waiting for this */
821 if(sem_io != fast_sem_io)
822 ipc_free(sem_io, sizeof(ushort)*nsems);
832 static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
837 struct semid64_ds tbuf;
839 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
842 out->uid = tbuf.sem_perm.uid;
843 out->gid = tbuf.sem_perm.gid;
844 out->mode = tbuf.sem_perm.mode;
850 struct semid_ds tbuf_old;
852 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
855 out->uid = tbuf_old.sem_perm.uid;
856 out->gid = tbuf_old.sem_perm.gid;
857 out->mode = tbuf_old.sem_perm.mode;
866 static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
867 int cmd, int version, union semun arg)
869 struct sem_array *sma;
871 struct sem_setbuf setbuf;
872 struct kern_ipc_perm *ipcp;
875 if(copy_semid_from_user (&setbuf, arg.buf, version))
878 sma = sem_lock(ns, semid);
882 if (sem_checkid(ns,sma,semid)) {
886 ipcp = &sma->sem_perm;
888 err = audit_ipc_obj(ipcp);
892 if (cmd == IPC_SET) {
893 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
897 if (current->euid != ipcp->cuid &&
898 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
903 err = security_sem_semctl(sma, cmd);
909 freeary(ns, sma, semid);
913 ipcp->uid = setbuf.uid;
914 ipcp->gid = setbuf.gid;
915 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
916 | (setbuf.mode & S_IRWXUGO);
917 sma->sem_ctime = get_seconds();
933 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
937 struct ipc_namespace *ns;
942 version = ipc_parse_version(&cmd);
943 ns = current->nsproxy->ipc_ns;
949 err = semctl_nolock(ns,semid,semnum,cmd,version,arg);
959 err = semctl_main(ns,semid,semnum,cmd,version,arg);
963 mutex_lock(&sem_ids(ns).mutex);
964 err = semctl_down(ns,semid,semnum,cmd,version,arg);
965 mutex_unlock(&sem_ids(ns).mutex);
972 static inline void lock_semundo(void)
974 struct sem_undo_list *undo_list;
976 undo_list = current->sysvsem.undo_list;
978 spin_lock(&undo_list->lock);
981 /* This code has an interaction with copy_semundo().
982 * Consider; two tasks are sharing the undo_list. task1
983 * acquires the undo_list lock in lock_semundo(). If task2 now
984 * exits before task1 releases the lock (by calling
985 * unlock_semundo()), then task1 will never call spin_unlock().
986 * This leave the sem_undo_list in a locked state. If task1 now creats task3
987 * and once again shares the sem_undo_list, the sem_undo_list will still be
988 * locked, and future SEM_UNDO operations will deadlock. This case is
989 * dealt with in copy_semundo() by having it reinitialize the spin lock when
990 * the refcnt goes from 1 to 2.
992 static inline void unlock_semundo(void)
994 struct sem_undo_list *undo_list;
996 undo_list = current->sysvsem.undo_list;
998 spin_unlock(&undo_list->lock);
1002 /* If the task doesn't already have a undo_list, then allocate one
1003 * here. We guarantee there is only one thread using this undo list,
1004 * and current is THE ONE
1006 * If this allocation and assignment succeeds, but later
1007 * portions of this code fail, there is no need to free the sem_undo_list.
1008 * Just let it stay associated with the task, and it'll be freed later
1011 * This can block, so callers must hold no locks.
1013 static inline int get_undo_list(struct sem_undo_list **undo_listp)
1015 struct sem_undo_list *undo_list;
1017 undo_list = current->sysvsem.undo_list;
1019 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
1020 if (undo_list == NULL)
1022 spin_lock_init(&undo_list->lock);
1023 atomic_set(&undo_list->refcnt, 1);
1024 current->sysvsem.undo_list = undo_list;
1026 *undo_listp = undo_list;
1030 static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1032 struct sem_undo **last, *un;
1034 last = &ulp->proc_list;
1037 if(un->semid==semid)
1040 *last=un->proc_next;
1043 last=&un->proc_next;
1050 static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
1052 struct sem_array *sma;
1053 struct sem_undo_list *ulp;
1054 struct sem_undo *un, *new;
1058 error = get_undo_list(&ulp);
1060 return ERR_PTR(error);
1063 un = lookup_undo(ulp, semid);
1065 if (likely(un!=NULL))
1068 /* no undo structure around - allocate one. */
1069 sma = sem_lock(ns, semid);
1070 un = ERR_PTR(-EINVAL);
1073 un = ERR_PTR(-EIDRM);
1074 if (sem_checkid(ns,sma,semid)) {
1078 nsems = sma->sem_nsems;
1079 ipc_rcu_getref(sma);
1082 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1084 ipc_lock_by_ptr(&sma->sem_perm);
1085 ipc_rcu_putref(sma);
1087 return ERR_PTR(-ENOMEM);
1089 new->semadj = (short *) &new[1];
1093 un = lookup_undo(ulp, semid);
1097 ipc_lock_by_ptr(&sma->sem_perm);
1098 ipc_rcu_putref(sma);
1102 ipc_lock_by_ptr(&sma->sem_perm);
1103 ipc_rcu_putref(sma);
1104 if (sma->sem_perm.deleted) {
1108 un = ERR_PTR(-EIDRM);
1111 new->proc_next = ulp->proc_list;
1112 ulp->proc_list = new;
1113 new->id_next = sma->undo;
1122 asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1123 unsigned nsops, const struct timespec __user *timeout)
1125 int error = -EINVAL;
1126 struct sem_array *sma;
1127 struct sembuf fast_sops[SEMOPM_FAST];
1128 struct sembuf* sops = fast_sops, *sop;
1129 struct sem_undo *un;
1130 int undos = 0, alter = 0, max;
1131 struct sem_queue queue;
1132 unsigned long jiffies_left = 0;
1133 struct ipc_namespace *ns;
1135 ns = current->nsproxy->ipc_ns;
1137 if (nsops < 1 || semid < 0)
1139 if (nsops > ns->sc_semopm)
1141 if(nsops > SEMOPM_FAST) {
1142 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1146 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1151 struct timespec _timeout;
1152 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1156 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1157 _timeout.tv_nsec >= 1000000000L) {
1161 jiffies_left = timespec_to_jiffies(&_timeout);
1164 for (sop = sops; sop < sops + nsops; sop++) {
1165 if (sop->sem_num >= max)
1167 if (sop->sem_flg & SEM_UNDO)
1169 if (sop->sem_op != 0)
1175 un = find_undo(ns, semid);
1177 error = PTR_ERR(un);
1183 sma = sem_lock(ns, semid);
1188 if (sem_checkid(ns,sma,semid))
1189 goto out_unlock_free;
1191 * semid identifies are not unique - find_undo may have
1192 * allocated an undo structure, it was invalidated by an RMID
1193 * and now a new array with received the same id. Check and retry.
1195 if (un && un->semid == -1) {
1200 if (max >= sma->sem_nsems)
1201 goto out_unlock_free;
1204 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1205 goto out_unlock_free;
1207 error = security_sem_semop(sma, sops, nsops, alter);
1209 goto out_unlock_free;
1211 error = try_atomic_semop (sma, sops, nsops, un, current->tgid);
1213 if (alter && error == 0)
1215 goto out_unlock_free;
1218 /* We need to sleep on this operation, so we put the current
1219 * task into the pending queue and go to sleep.
1224 queue.nsops = nsops;
1226 queue.pid = current->tgid;
1228 queue.alter = alter;
1230 append_to_queue(sma ,&queue);
1232 prepend_to_queue(sma ,&queue);
1234 queue.status = -EINTR;
1235 queue.sleeper = current;
1236 current->state = TASK_INTERRUPTIBLE;
1240 jiffies_left = schedule_timeout(jiffies_left);
1244 error = queue.status;
1245 while(unlikely(error == IN_WAKEUP)) {
1247 error = queue.status;
1250 if (error != -EINTR) {
1251 /* fast path: update_queue already obtained all requested
1256 sma = sem_lock(ns, semid);
1258 BUG_ON(queue.prev != NULL);
1264 * If queue.status != -EINTR we are woken up by another process
1266 error = queue.status;
1267 if (error != -EINTR) {
1268 goto out_unlock_free;
1272 * If an interrupt occurred we have to clean up the queue
1274 if (timeout && jiffies_left == 0)
1276 remove_from_queue(sma,&queue);
1277 goto out_unlock_free;
1282 if(sops != fast_sops)
1287 asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1289 return sys_semtimedop(semid, tsops, nsops, NULL);
1292 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1293 * parent and child tasks.
1295 * See the notes above unlock_semundo() regarding the spin_lock_init()
1296 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1297 * because of the reasoning in the comment above unlock_semundo.
1300 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1302 struct sem_undo_list *undo_list;
1305 if (clone_flags & CLONE_SYSVSEM) {
1306 error = get_undo_list(&undo_list);
1309 atomic_inc(&undo_list->refcnt);
1310 tsk->sysvsem.undo_list = undo_list;
1312 tsk->sysvsem.undo_list = NULL;
1318 * add semadj values to semaphores, free undo structures.
1319 * undo structures are not freed when semaphore arrays are destroyed
1320 * so some of them may be out of date.
1321 * IMPLEMENTATION NOTE: There is some confusion over whether the
1322 * set of adjustments that needs to be done should be done in an atomic
1323 * manner or not. That is, if we are attempting to decrement the semval
1324 * should we queue up and wait until we can do so legally?
1325 * The original implementation attempted to do this (queue and wait).
1326 * The current implementation does not do so. The POSIX standard
1327 * and SVID should be consulted to determine what behavior is mandated.
1329 void exit_sem(struct task_struct *tsk)
1331 struct sem_undo_list *undo_list;
1332 struct sem_undo *u, **up;
1333 struct ipc_namespace *ns;
1335 undo_list = tsk->sysvsem.undo_list;
1339 if (!atomic_dec_and_test(&undo_list->refcnt))
1342 ns = tsk->nsproxy->ipc_ns;
1343 /* There's no need to hold the semundo list lock, as current
1344 * is the last task exiting for this undo list.
1346 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1347 struct sem_array *sma;
1349 struct sem_undo *un, **unp;
1356 sma = sem_lock(ns, semid);
1363 BUG_ON(sem_checkid(ns,sma,u->semid));
1365 /* remove u from the sma->undo list */
1366 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1370 printk ("exit_sem undo list error id=%d\n", u->semid);
1374 /* perform adjustments registered in u */
1375 nsems = sma->sem_nsems;
1376 for (i = 0; i < nsems; i++) {
1377 struct sem * semaphore = &sma->sem_base[i];
1379 semaphore->semval += u->semadj[i];
1381 * Range checks of the new semaphore value,
1382 * not defined by sus:
1383 * - Some unices ignore the undo entirely
1384 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1385 * - some cap the value (e.g. FreeBSD caps
1386 * at 0, but doesn't enforce SEMVMX)
1388 * Linux caps the semaphore value, both at 0
1391 * Manfred <manfred@colorfullife.com>
1393 if (semaphore->semval < 0)
1394 semaphore->semval = 0;
1395 if (semaphore->semval > SEMVMX)
1396 semaphore->semval = SEMVMX;
1397 semaphore->sempid = current->tgid;
1400 sma->sem_otime = get_seconds();
1401 /* maybe some queued-up processes were waiting for this */
1409 #ifdef CONFIG_PROC_FS
1410 static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
1412 struct sem_array *sma = it;
1414 return seq_printf(s,
1415 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",