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/security.h>
79 #include <linux/syscalls.h>
80 #include <linux/audit.h>
81 #include <linux/capability.h>
82 #include <linux/seq_file.h>
83 #include <linux/mutex.h>
84 #include <linux/nsproxy.h>
86 #include <asm/uaccess.h>
89 #define sem_ids(ns) (*((ns)->ids[IPC_SEM_IDS]))
91 #define sem_lock(ns, id) ((struct sem_array*)ipc_lock(&sem_ids(ns), id))
92 #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
93 #define sem_checkid(ns, sma, semid) \
94 ipc_checkid(&sem_ids(ns),&sma->sem_perm,semid)
95 #define sem_buildid(ns, id, seq) \
96 ipc_buildid(&sem_ids(ns), id, seq)
98 static struct ipc_ids init_sem_ids;
100 static int newary(struct ipc_namespace *, key_t, int, int);
101 static void freeary(struct ipc_namespace *, struct sem_array *);
102 #ifdef CONFIG_PROC_FS
103 static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
106 #define SEMMSL_FAST 256 /* 512 bytes on stack */
107 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
110 * linked list protection:
112 * sem_array.sem_pending{,last},
113 * sem_array.sem_undo: sem_lock() for read/write
114 * sem_undo.proc_next: only "current" is allowed to read/write that field.
118 #define sc_semmsl sem_ctls[0]
119 #define sc_semmns sem_ctls[1]
120 #define sc_semopm sem_ctls[2]
121 #define sc_semmni sem_ctls[3]
123 static void __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
125 ns->ids[IPC_SEM_IDS] = ids;
126 ns->sc_semmsl = SEMMSL;
127 ns->sc_semmns = SEMMNS;
128 ns->sc_semopm = SEMOPM;
129 ns->sc_semmni = SEMMNI;
134 int sem_init_ns(struct ipc_namespace *ns)
138 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
142 __sem_init_ns(ns, ids);
146 void sem_exit_ns(struct ipc_namespace *ns)
148 struct sem_array *sma;
152 mutex_lock(&sem_ids(ns).mutex);
154 in_use = sem_ids(ns).in_use;
156 for (total = 0, next_id = 0; total < in_use; next_id++) {
157 sma = idr_find(&sem_ids(ns).ipcs_idr, next_id);
160 ipc_lock_by_ptr(&sma->sem_perm);
164 mutex_unlock(&sem_ids(ns).mutex);
166 kfree(ns->ids[IPC_SEM_IDS]);
167 ns->ids[IPC_SEM_IDS] = NULL;
170 void __init sem_init (void)
172 __sem_init_ns(&init_ipc_ns, &init_sem_ids);
173 ipc_init_proc_interface("sysvipc/sem",
174 " key semid perms nsems uid gid cuid cgid otime ctime\n",
175 IPC_SEM_IDS, sysvipc_sem_proc_show);
178 static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
180 ipc_rmid(&sem_ids(ns), &s->sem_perm);
184 * Lockless wakeup algorithm:
185 * Without the check/retry algorithm a lockless wakeup is possible:
186 * - queue.status is initialized to -EINTR before blocking.
187 * - wakeup is performed by
188 * * unlinking the queue entry from sma->sem_pending
189 * * setting queue.status to IN_WAKEUP
190 * This is the notification for the blocked thread that a
191 * result value is imminent.
192 * * call wake_up_process
193 * * set queue.status to the final value.
194 * - the previously blocked thread checks queue.status:
195 * * if it's IN_WAKEUP, then it must wait until the value changes
196 * * if it's not -EINTR, then the operation was completed by
197 * update_queue. semtimedop can return queue.status without
198 * performing any operation on the sem array.
199 * * otherwise it must acquire the spinlock and check what's up.
201 * The two-stage algorithm is necessary to protect against the following
203 * - if queue.status is set after wake_up_process, then the woken up idle
204 * thread could race forward and try (and fail) to acquire sma->lock
205 * before update_queue had a chance to set queue.status
206 * - if queue.status is written before wake_up_process and if the
207 * blocked process is woken up by a signal between writing
208 * queue.status and the wake_up_process, then the woken up
209 * process could return from semtimedop and die by calling
210 * sys_exit before wake_up_process is called. Then wake_up_process
211 * will oops, because the task structure is already invalid.
212 * (yes, this happened on s390 with sysv msg).
217 static int newary (struct ipc_namespace *ns, key_t key, int nsems, int semflg)
221 struct sem_array *sma;
226 if (ns->used_sems + nsems > ns->sc_semmns)
229 size = sizeof (*sma) + nsems * sizeof (struct sem);
230 sma = ipc_rcu_alloc(size);
234 memset (sma, 0, size);
236 sma->sem_perm.mode = (semflg & S_IRWXUGO);
237 sma->sem_perm.key = key;
239 sma->sem_perm.security = NULL;
240 retval = security_sem_alloc(sma);
246 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
248 security_sem_free(sma);
252 ns->used_sems += nsems;
254 sma->sem_perm.id = sem_buildid(ns, id, sma->sem_perm.seq);
255 sma->sem_base = (struct sem *) &sma[1];
256 /* sma->sem_pending = NULL; */
257 sma->sem_pending_last = &sma->sem_pending;
258 /* sma->undo = NULL; */
259 sma->sem_nsems = nsems;
260 sma->sem_ctime = get_seconds();
263 return sma->sem_perm.id;
266 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
269 struct sem_array *sma;
270 struct ipc_namespace *ns;
272 ns = current->nsproxy->ipc_ns;
274 if (nsems < 0 || nsems > ns->sc_semmsl)
277 err = idr_pre_get(&sem_ids(ns).ipcs_idr, GFP_KERNEL);
279 if (key == IPC_PRIVATE) {
283 mutex_lock(&sem_ids(ns).mutex);
284 err = newary(ns, key, nsems, semflg);
285 mutex_unlock(&sem_ids(ns).mutex);
288 mutex_lock(&sem_ids(ns).mutex);
289 sma = (struct sem_array *) ipc_findkey(&sem_ids(ns), key);
292 if (!(semflg & IPC_CREAT))
297 err = newary(ns, key, nsems, semflg);
299 /* sma has been locked by ipc_findkey() */
301 if (semflg & IPC_CREAT && semflg & IPC_EXCL)
304 if (nsems > sma->sem_nsems)
306 else if (ipcperms(&sma->sem_perm, semflg))
309 err = security_sem_associate(sma,
312 err = sma->sem_perm.id;
317 mutex_unlock(&sem_ids(ns).mutex);
323 /* Manage the doubly linked list sma->sem_pending as a FIFO:
324 * insert new queue elements at the tail sma->sem_pending_last.
326 static inline void append_to_queue (struct sem_array * sma,
327 struct sem_queue * q)
329 *(q->prev = sma->sem_pending_last) = q;
330 *(sma->sem_pending_last = &q->next) = NULL;
333 static inline void prepend_to_queue (struct sem_array * sma,
334 struct sem_queue * q)
336 q->next = sma->sem_pending;
337 *(q->prev = &sma->sem_pending) = q;
339 q->next->prev = &q->next;
340 else /* sma->sem_pending_last == &sma->sem_pending */
341 sma->sem_pending_last = &q->next;
344 static inline void remove_from_queue (struct sem_array * sma,
345 struct sem_queue * q)
347 *(q->prev) = q->next;
349 q->next->prev = q->prev;
350 else /* sma->sem_pending_last == &q->next */
351 sma->sem_pending_last = q->prev;
352 q->prev = NULL; /* mark as removed */
356 * Determine whether a sequence of semaphore operations would succeed
357 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
360 static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
361 int nsops, struct sem_undo *un, int pid)
367 for (sop = sops; sop < sops + nsops; sop++) {
368 curr = sma->sem_base + sop->sem_num;
369 sem_op = sop->sem_op;
370 result = curr->semval;
372 if (!sem_op && result)
380 if (sop->sem_flg & SEM_UNDO) {
381 int undo = un->semadj[sop->sem_num] - sem_op;
383 * Exceeding the undo range is an error.
385 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
388 curr->semval = result;
392 while (sop >= sops) {
393 sma->sem_base[sop->sem_num].sempid = pid;
394 if (sop->sem_flg & SEM_UNDO)
395 un->semadj[sop->sem_num] -= sop->sem_op;
399 sma->sem_otime = get_seconds();
407 if (sop->sem_flg & IPC_NOWAIT)
414 while (sop >= sops) {
415 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
422 /* Go through the pending queue for the indicated semaphore
423 * looking for tasks that can be completed.
425 static void update_queue (struct sem_array * sma)
428 struct sem_queue * q;
430 q = sma->sem_pending;
432 error = try_atomic_semop(sma, q->sops, q->nsops,
435 /* Does q->sleeper still need to sleep? */
438 remove_from_queue(sma,q);
439 q->status = IN_WAKEUP;
441 * Continue scanning. The next operation
442 * that must be checked depends on the type of the
443 * completed operation:
444 * - if the operation modified the array, then
445 * restart from the head of the queue and
446 * check for threads that might be waiting
447 * for semaphore values to become 0.
448 * - if the operation didn't modify the array,
449 * then just continue.
452 n = sma->sem_pending;
455 wake_up_process(q->sleeper);
456 /* hands-off: q will disappear immediately after
468 /* The following counts are associated to each semaphore:
469 * semncnt number of tasks waiting on semval being nonzero
470 * semzcnt number of tasks waiting on semval being zero
471 * This model assumes that a task waits on exactly one semaphore.
472 * Since semaphore operations are to be performed atomically, tasks actually
473 * wait on a whole sequence of semaphores simultaneously.
474 * The counts we return here are a rough approximation, but still
475 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
477 static int count_semncnt (struct sem_array * sma, ushort semnum)
480 struct sem_queue * q;
483 for (q = sma->sem_pending; q; q = q->next) {
484 struct sembuf * sops = q->sops;
485 int nsops = q->nsops;
487 for (i = 0; i < nsops; i++)
488 if (sops[i].sem_num == semnum
489 && (sops[i].sem_op < 0)
490 && !(sops[i].sem_flg & IPC_NOWAIT))
495 static int count_semzcnt (struct sem_array * sma, ushort semnum)
498 struct sem_queue * q;
501 for (q = sma->sem_pending; q; q = q->next) {
502 struct sembuf * sops = q->sops;
503 int nsops = q->nsops;
505 for (i = 0; i < nsops; i++)
506 if (sops[i].sem_num == semnum
507 && (sops[i].sem_op == 0)
508 && !(sops[i].sem_flg & IPC_NOWAIT))
514 /* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
515 * the spinlock for this semaphore set hold. sem_ids.mutex remains locked
518 static void freeary(struct ipc_namespace *ns, struct sem_array *sma)
523 /* Invalidate the existing undo structures for this semaphore set.
524 * (They will be freed without any further action in exit_sem()
525 * or during the next semop.)
527 for (un = sma->undo; un; un = un->id_next)
530 /* Wake up all pending processes and let them fail with EIDRM. */
531 q = sma->sem_pending;
534 /* lazy remove_from_queue: we are killing the whole queue */
537 q->status = IN_WAKEUP;
538 wake_up_process(q->sleeper); /* doesn't sleep */
540 q->status = -EIDRM; /* hands-off q */
544 /* Remove the semaphore set from the IDR */
548 ns->used_sems -= sma->sem_nsems;
549 security_sem_free(sma);
553 static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
557 return copy_to_user(buf, in, sizeof(*in));
562 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
564 out.sem_otime = in->sem_otime;
565 out.sem_ctime = in->sem_ctime;
566 out.sem_nsems = in->sem_nsems;
568 return copy_to_user(buf, &out, sizeof(out));
575 static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
576 int cmd, int version, union semun arg)
579 struct sem_array *sma;
585 struct seminfo seminfo;
588 err = security_sem_semctl(NULL, cmd);
592 memset(&seminfo,0,sizeof(seminfo));
593 seminfo.semmni = ns->sc_semmni;
594 seminfo.semmns = ns->sc_semmns;
595 seminfo.semmsl = ns->sc_semmsl;
596 seminfo.semopm = ns->sc_semopm;
597 seminfo.semvmx = SEMVMX;
598 seminfo.semmnu = SEMMNU;
599 seminfo.semmap = SEMMAP;
600 seminfo.semume = SEMUME;
601 mutex_lock(&sem_ids(ns).mutex);
602 if (cmd == SEM_INFO) {
603 seminfo.semusz = sem_ids(ns).in_use;
604 seminfo.semaem = ns->used_sems;
606 seminfo.semusz = SEMUSZ;
607 seminfo.semaem = SEMAEM;
609 max_id = ipc_get_maxid(&sem_ids(ns));
610 mutex_unlock(&sem_ids(ns).mutex);
611 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
613 return (max_id < 0) ? 0: max_id;
617 struct semid64_ds tbuf;
620 memset(&tbuf,0,sizeof(tbuf));
622 sma = sem_lock(ns, semid);
627 if (ipcperms (&sma->sem_perm, S_IRUGO))
630 err = security_sem_semctl(sma, cmd);
634 id = sma->sem_perm.id;
636 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
637 tbuf.sem_otime = sma->sem_otime;
638 tbuf.sem_ctime = sma->sem_ctime;
639 tbuf.sem_nsems = sma->sem_nsems;
641 if (copy_semid_to_user (arg.buf, &tbuf, version))
654 static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
655 int cmd, int version, union semun arg)
657 struct sem_array *sma;
660 ushort fast_sem_io[SEMMSL_FAST];
661 ushort* sem_io = fast_sem_io;
664 sma = sem_lock(ns, semid);
668 nsems = sma->sem_nsems;
671 if (sem_checkid(ns,sma,semid))
675 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
678 err = security_sem_semctl(sma, cmd);
686 ushort __user *array = arg.array;
689 if(nsems > SEMMSL_FAST) {
693 sem_io = ipc_alloc(sizeof(ushort)*nsems);
695 ipc_lock_by_ptr(&sma->sem_perm);
701 ipc_lock_by_ptr(&sma->sem_perm);
703 if (sma->sem_perm.deleted) {
710 for (i = 0; i < sma->sem_nsems; i++)
711 sem_io[i] = sma->sem_base[i].semval;
714 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
726 if(nsems > SEMMSL_FAST) {
727 sem_io = ipc_alloc(sizeof(ushort)*nsems);
729 ipc_lock_by_ptr(&sma->sem_perm);
736 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
737 ipc_lock_by_ptr(&sma->sem_perm);
744 for (i = 0; i < nsems; i++) {
745 if (sem_io[i] > SEMVMX) {
746 ipc_lock_by_ptr(&sma->sem_perm);
753 ipc_lock_by_ptr(&sma->sem_perm);
755 if (sma->sem_perm.deleted) {
761 for (i = 0; i < nsems; i++)
762 sma->sem_base[i].semval = sem_io[i];
763 for (un = sma->undo; un; un = un->id_next)
764 for (i = 0; i < nsems; i++)
766 sma->sem_ctime = get_seconds();
767 /* maybe some queued-up processes were waiting for this */
774 struct semid64_ds tbuf;
775 memset(&tbuf,0,sizeof(tbuf));
776 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
777 tbuf.sem_otime = sma->sem_otime;
778 tbuf.sem_ctime = sma->sem_ctime;
779 tbuf.sem_nsems = sma->sem_nsems;
781 if (copy_semid_to_user (arg.buf, &tbuf, version))
785 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
788 if(semnum < 0 || semnum >= nsems)
791 curr = &sma->sem_base[semnum];
801 err = count_semncnt(sma,semnum);
804 err = count_semzcnt(sma,semnum);
811 if (val > SEMVMX || val < 0)
814 for (un = sma->undo; un; un = un->id_next)
815 un->semadj[semnum] = 0;
817 curr->sempid = task_tgid_vnr(current);
818 sma->sem_ctime = get_seconds();
819 /* maybe some queued-up processes were waiting for this */
828 if(sem_io != fast_sem_io)
829 ipc_free(sem_io, sizeof(ushort)*nsems);
839 static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
844 struct semid64_ds tbuf;
846 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
849 out->uid = tbuf.sem_perm.uid;
850 out->gid = tbuf.sem_perm.gid;
851 out->mode = tbuf.sem_perm.mode;
857 struct semid_ds tbuf_old;
859 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
862 out->uid = tbuf_old.sem_perm.uid;
863 out->gid = tbuf_old.sem_perm.gid;
864 out->mode = tbuf_old.sem_perm.mode;
873 static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
874 int cmd, int version, union semun arg)
876 struct sem_array *sma;
878 struct sem_setbuf uninitialized_var(setbuf);
879 struct kern_ipc_perm *ipcp;
882 if(copy_semid_from_user (&setbuf, arg.buf, version))
885 sma = sem_lock(ns, semid);
889 if (sem_checkid(ns,sma,semid)) {
893 ipcp = &sma->sem_perm;
895 err = audit_ipc_obj(ipcp);
899 if (cmd == IPC_SET) {
900 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
904 if (current->euid != ipcp->cuid &&
905 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
910 err = security_sem_semctl(sma, cmd);
920 ipcp->uid = setbuf.uid;
921 ipcp->gid = setbuf.gid;
922 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
923 | (setbuf.mode & S_IRWXUGO);
924 sma->sem_ctime = get_seconds();
940 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
944 struct ipc_namespace *ns;
949 version = ipc_parse_version(&cmd);
950 ns = current->nsproxy->ipc_ns;
956 err = semctl_nolock(ns,semid,semnum,cmd,version,arg);
966 err = semctl_main(ns,semid,semnum,cmd,version,arg);
970 mutex_lock(&sem_ids(ns).mutex);
971 err = semctl_down(ns,semid,semnum,cmd,version,arg);
972 mutex_unlock(&sem_ids(ns).mutex);
979 static inline void lock_semundo(void)
981 struct sem_undo_list *undo_list;
983 undo_list = current->sysvsem.undo_list;
985 spin_lock(&undo_list->lock);
988 /* This code has an interaction with copy_semundo().
989 * Consider; two tasks are sharing the undo_list. task1
990 * acquires the undo_list lock in lock_semundo(). If task2 now
991 * exits before task1 releases the lock (by calling
992 * unlock_semundo()), then task1 will never call spin_unlock().
993 * This leave the sem_undo_list in a locked state. If task1 now creats task3
994 * and once again shares the sem_undo_list, the sem_undo_list will still be
995 * locked, and future SEM_UNDO operations will deadlock. This case is
996 * dealt with in copy_semundo() by having it reinitialize the spin lock when
997 * the refcnt goes from 1 to 2.
999 static inline void unlock_semundo(void)
1001 struct sem_undo_list *undo_list;
1003 undo_list = current->sysvsem.undo_list;
1005 spin_unlock(&undo_list->lock);
1009 /* If the task doesn't already have a undo_list, then allocate one
1010 * here. We guarantee there is only one thread using this undo list,
1011 * and current is THE ONE
1013 * If this allocation and assignment succeeds, but later
1014 * portions of this code fail, there is no need to free the sem_undo_list.
1015 * Just let it stay associated with the task, and it'll be freed later
1018 * This can block, so callers must hold no locks.
1020 static inline int get_undo_list(struct sem_undo_list **undo_listp)
1022 struct sem_undo_list *undo_list;
1024 undo_list = current->sysvsem.undo_list;
1026 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
1027 if (undo_list == NULL)
1029 spin_lock_init(&undo_list->lock);
1030 atomic_set(&undo_list->refcnt, 1);
1031 current->sysvsem.undo_list = undo_list;
1033 *undo_listp = undo_list;
1037 static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1039 struct sem_undo **last, *un;
1041 last = &ulp->proc_list;
1044 if(un->semid==semid)
1047 *last=un->proc_next;
1050 last=&un->proc_next;
1057 static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
1059 struct sem_array *sma;
1060 struct sem_undo_list *ulp;
1061 struct sem_undo *un, *new;
1065 error = get_undo_list(&ulp);
1067 return ERR_PTR(error);
1070 un = lookup_undo(ulp, semid);
1072 if (likely(un!=NULL))
1075 /* no undo structure around - allocate one. */
1076 sma = sem_lock(ns, semid);
1077 un = ERR_PTR(-EINVAL);
1080 un = ERR_PTR(-EIDRM);
1081 if (sem_checkid(ns,sma,semid)) {
1085 nsems = sma->sem_nsems;
1086 ipc_rcu_getref(sma);
1089 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1091 ipc_lock_by_ptr(&sma->sem_perm);
1092 ipc_rcu_putref(sma);
1094 return ERR_PTR(-ENOMEM);
1096 new->semadj = (short *) &new[1];
1100 un = lookup_undo(ulp, semid);
1104 ipc_lock_by_ptr(&sma->sem_perm);
1105 ipc_rcu_putref(sma);
1109 ipc_lock_by_ptr(&sma->sem_perm);
1110 ipc_rcu_putref(sma);
1111 if (sma->sem_perm.deleted) {
1115 un = ERR_PTR(-EIDRM);
1118 new->proc_next = ulp->proc_list;
1119 ulp->proc_list = new;
1120 new->id_next = sma->undo;
1129 asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1130 unsigned nsops, const struct timespec __user *timeout)
1132 int error = -EINVAL;
1133 struct sem_array *sma;
1134 struct sembuf fast_sops[SEMOPM_FAST];
1135 struct sembuf* sops = fast_sops, *sop;
1136 struct sem_undo *un;
1137 int undos = 0, alter = 0, max;
1138 struct sem_queue queue;
1139 unsigned long jiffies_left = 0;
1140 struct ipc_namespace *ns;
1142 ns = current->nsproxy->ipc_ns;
1144 if (nsops < 1 || semid < 0)
1146 if (nsops > ns->sc_semopm)
1148 if(nsops > SEMOPM_FAST) {
1149 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1153 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1158 struct timespec _timeout;
1159 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1163 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1164 _timeout.tv_nsec >= 1000000000L) {
1168 jiffies_left = timespec_to_jiffies(&_timeout);
1171 for (sop = sops; sop < sops + nsops; sop++) {
1172 if (sop->sem_num >= max)
1174 if (sop->sem_flg & SEM_UNDO)
1176 if (sop->sem_op != 0)
1182 un = find_undo(ns, semid);
1184 error = PTR_ERR(un);
1190 sma = sem_lock(ns, semid);
1195 if (sem_checkid(ns,sma,semid))
1196 goto out_unlock_free;
1198 * semid identifies are not unique - find_undo may have
1199 * allocated an undo structure, it was invalidated by an RMID
1200 * and now a new array with received the same id. Check and retry.
1202 if (un && un->semid == -1) {
1207 if (max >= sma->sem_nsems)
1208 goto out_unlock_free;
1211 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1212 goto out_unlock_free;
1214 error = security_sem_semop(sma, sops, nsops, alter);
1216 goto out_unlock_free;
1218 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
1220 if (alter && error == 0)
1222 goto out_unlock_free;
1225 /* We need to sleep on this operation, so we put the current
1226 * task into the pending queue and go to sleep.
1231 queue.nsops = nsops;
1233 queue.pid = task_tgid_vnr(current);
1235 queue.alter = alter;
1237 append_to_queue(sma ,&queue);
1239 prepend_to_queue(sma ,&queue);
1241 queue.status = -EINTR;
1242 queue.sleeper = current;
1243 current->state = TASK_INTERRUPTIBLE;
1247 jiffies_left = schedule_timeout(jiffies_left);
1251 error = queue.status;
1252 while(unlikely(error == IN_WAKEUP)) {
1254 error = queue.status;
1257 if (error != -EINTR) {
1258 /* fast path: update_queue already obtained all requested
1263 sma = sem_lock(ns, semid);
1265 BUG_ON(queue.prev != NULL);
1271 * If queue.status != -EINTR we are woken up by another process
1273 error = queue.status;
1274 if (error != -EINTR) {
1275 goto out_unlock_free;
1279 * If an interrupt occurred we have to clean up the queue
1281 if (timeout && jiffies_left == 0)
1283 remove_from_queue(sma,&queue);
1284 goto out_unlock_free;
1289 if(sops != fast_sops)
1294 asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1296 return sys_semtimedop(semid, tsops, nsops, NULL);
1299 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1300 * parent and child tasks.
1302 * See the notes above unlock_semundo() regarding the spin_lock_init()
1303 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1304 * because of the reasoning in the comment above unlock_semundo.
1307 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1309 struct sem_undo_list *undo_list;
1312 if (clone_flags & CLONE_SYSVSEM) {
1313 error = get_undo_list(&undo_list);
1316 atomic_inc(&undo_list->refcnt);
1317 tsk->sysvsem.undo_list = undo_list;
1319 tsk->sysvsem.undo_list = NULL;
1325 * add semadj values to semaphores, free undo structures.
1326 * undo structures are not freed when semaphore arrays are destroyed
1327 * so some of them may be out of date.
1328 * IMPLEMENTATION NOTE: There is some confusion over whether the
1329 * set of adjustments that needs to be done should be done in an atomic
1330 * manner or not. That is, if we are attempting to decrement the semval
1331 * should we queue up and wait until we can do so legally?
1332 * The original implementation attempted to do this (queue and wait).
1333 * The current implementation does not do so. The POSIX standard
1334 * and SVID should be consulted to determine what behavior is mandated.
1336 void exit_sem(struct task_struct *tsk)
1338 struct sem_undo_list *undo_list;
1339 struct sem_undo *u, **up;
1340 struct ipc_namespace *ns;
1342 undo_list = tsk->sysvsem.undo_list;
1346 if (!atomic_dec_and_test(&undo_list->refcnt))
1349 ns = tsk->nsproxy->ipc_ns;
1350 /* There's no need to hold the semundo list lock, as current
1351 * is the last task exiting for this undo list.
1353 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1354 struct sem_array *sma;
1356 struct sem_undo *un, **unp;
1363 sma = sem_lock(ns, semid);
1370 BUG_ON(sem_checkid(ns,sma,u->semid));
1372 /* remove u from the sma->undo list */
1373 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1377 printk ("exit_sem undo list error id=%d\n", u->semid);
1381 /* perform adjustments registered in u */
1382 nsems = sma->sem_nsems;
1383 for (i = 0; i < nsems; i++) {
1384 struct sem * semaphore = &sma->sem_base[i];
1386 semaphore->semval += u->semadj[i];
1388 * Range checks of the new semaphore value,
1389 * not defined by sus:
1390 * - Some unices ignore the undo entirely
1391 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1392 * - some cap the value (e.g. FreeBSD caps
1393 * at 0, but doesn't enforce SEMVMX)
1395 * Linux caps the semaphore value, both at 0
1398 * Manfred <manfred@colorfullife.com>
1400 if (semaphore->semval < 0)
1401 semaphore->semval = 0;
1402 if (semaphore->semval > SEMVMX)
1403 semaphore->semval = SEMVMX;
1404 semaphore->sempid = task_tgid_vnr(current);
1407 sma->sem_otime = get_seconds();
1408 /* maybe some queued-up processes were waiting for this */
1416 #ifdef CONFIG_PROC_FS
1417 static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
1419 struct sem_array *sma = it;
1421 return seq_printf(s,
1422 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",