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>
82 #include <linux/vs_base.h>
84 #include <asm/uaccess.h>
88 #define sem_lock(id) ((struct sem_array*)ipc_lock(&sem_ids,id))
89 #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
90 #define sem_rmid(id) ((struct sem_array*)ipc_rmid(&sem_ids,id))
91 #define sem_checkid(sma, semid) \
92 ipc_checkid(&sem_ids,&sma->sem_perm,semid)
93 #define sem_buildid(id, seq) \
94 ipc_buildid(&sem_ids, id, seq)
95 static struct ipc_ids sem_ids;
97 static int newary (key_t, int, int);
98 static void freeary (struct sem_array *sma, int id);
100 static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
103 #define SEMMSL_FAST 256 /* 512 bytes on stack */
104 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
107 * linked list protection:
109 * sem_array.sem_pending{,last},
110 * sem_array.sem_undo: sem_lock() for read/write
111 * sem_undo.proc_next: only "current" is allowed to read/write that field.
115 int sem_ctls[4] = {SEMMSL, SEMMNS, SEMOPM, SEMMNI};
116 #define sc_semmsl (sem_ctls[0])
117 #define sc_semmns (sem_ctls[1])
118 #define sc_semopm (sem_ctls[2])
119 #define sc_semmni (sem_ctls[3])
121 static int used_sems;
123 void __init sem_init (void)
126 ipc_init_ids(&sem_ids,sc_semmni);
127 ipc_init_proc_interface("sysvipc/sem",
128 " key semid perms nsems uid gid cuid cgid otime ctime\n",
130 sysvipc_sem_proc_show);
134 * Lockless wakeup algorithm:
135 * Without the check/retry algorithm a lockless wakeup is possible:
136 * - queue.status is initialized to -EINTR before blocking.
137 * - wakeup is performed by
138 * * unlinking the queue entry from sma->sem_pending
139 * * setting queue.status to IN_WAKEUP
140 * This is the notification for the blocked thread that a
141 * result value is imminent.
142 * * call wake_up_process
143 * * set queue.status to the final value.
144 * - the previously blocked thread checks queue.status:
145 * * if it's IN_WAKEUP, then it must wait until the value changes
146 * * if it's not -EINTR, then the operation was completed by
147 * update_queue. semtimedop can return queue.status without
148 * performing any operation on the sem array.
149 * * otherwise it must acquire the spinlock and check what's up.
151 * The two-stage algorithm is necessary to protect against the following
153 * - if queue.status is set after wake_up_process, then the woken up idle
154 * thread could race forward and try (and fail) to acquire sma->lock
155 * before update_queue had a chance to set queue.status
156 * - if queue.status is written before wake_up_process and if the
157 * blocked process is woken up by a signal between writing
158 * queue.status and the wake_up_process, then the woken up
159 * process could return from semtimedop and die by calling
160 * sys_exit before wake_up_process is called. Then wake_up_process
161 * will oops, because the task structure is already invalid.
162 * (yes, this happened on s390 with sysv msg).
167 static int newary (key_t key, int nsems, int semflg)
171 struct sem_array *sma;
176 if (used_sems + nsems > sc_semmns)
179 size = sizeof (*sma) + nsems * sizeof (struct sem);
180 sma = ipc_rcu_alloc(size);
184 memset (sma, 0, size);
186 sma->sem_perm.mode = (semflg & S_IRWXUGO);
187 sma->sem_perm.key = key;
188 sma->sem_perm.xid = vx_current_xid();
190 sma->sem_perm.security = NULL;
191 retval = security_sem_alloc(sma);
197 id = ipc_addid(&sem_ids, &sma->sem_perm, sc_semmni);
199 security_sem_free(sma);
205 sma->sem_id = sem_buildid(id, sma->sem_perm.seq);
206 sma->sem_base = (struct sem *) &sma[1];
207 /* sma->sem_pending = NULL; */
208 sma->sem_pending_last = &sma->sem_pending;
209 /* sma->undo = NULL; */
210 sma->sem_nsems = nsems;
211 sma->sem_ctime = get_seconds();
217 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
219 int id, err = -EINVAL;
220 struct sem_array *sma;
222 if (nsems < 0 || nsems > sc_semmsl)
224 mutex_lock(&sem_ids.mutex);
226 if (key == IPC_PRIVATE) {
227 err = newary(key, nsems, semflg);
228 } else if ((id = ipc_findkey(&sem_ids, key)) == -1) { /* key not used */
229 if (!(semflg & IPC_CREAT))
232 err = newary(key, nsems, semflg);
233 } else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
238 if (nsems > sma->sem_nsems)
240 else if (ipcperms(&sma->sem_perm, semflg))
243 int semid = sem_buildid(id, sma->sem_perm.seq);
244 err = security_sem_associate(sma, semflg);
251 mutex_unlock(&sem_ids.mutex);
255 /* Manage the doubly linked list sma->sem_pending as a FIFO:
256 * insert new queue elements at the tail sma->sem_pending_last.
258 static inline void append_to_queue (struct sem_array * sma,
259 struct sem_queue * q)
261 *(q->prev = sma->sem_pending_last) = q;
262 *(sma->sem_pending_last = &q->next) = NULL;
265 static inline void prepend_to_queue (struct sem_array * sma,
266 struct sem_queue * q)
268 q->next = sma->sem_pending;
269 *(q->prev = &sma->sem_pending) = q;
271 q->next->prev = &q->next;
272 else /* sma->sem_pending_last == &sma->sem_pending */
273 sma->sem_pending_last = &q->next;
276 static inline void remove_from_queue (struct sem_array * sma,
277 struct sem_queue * q)
279 *(q->prev) = q->next;
281 q->next->prev = q->prev;
282 else /* sma->sem_pending_last == &q->next */
283 sma->sem_pending_last = q->prev;
284 q->prev = NULL; /* mark as removed */
288 * Determine whether a sequence of semaphore operations would succeed
289 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
292 static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
293 int nsops, struct sem_undo *un, int pid)
299 for (sop = sops; sop < sops + nsops; sop++) {
300 curr = sma->sem_base + sop->sem_num;
301 sem_op = sop->sem_op;
302 result = curr->semval;
304 if (!sem_op && result)
312 if (sop->sem_flg & SEM_UNDO) {
313 int undo = un->semadj[sop->sem_num] - sem_op;
315 * Exceeding the undo range is an error.
317 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
320 curr->semval = result;
324 while (sop >= sops) {
325 sma->sem_base[sop->sem_num].sempid = pid;
326 if (sop->sem_flg & SEM_UNDO)
327 un->semadj[sop->sem_num] -= sop->sem_op;
331 sma->sem_otime = get_seconds();
339 if (sop->sem_flg & IPC_NOWAIT)
346 while (sop >= sops) {
347 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
354 /* Go through the pending queue for the indicated semaphore
355 * looking for tasks that can be completed.
357 static void update_queue (struct sem_array * sma)
360 struct sem_queue * q;
362 q = sma->sem_pending;
364 error = try_atomic_semop(sma, q->sops, q->nsops,
367 /* Does q->sleeper still need to sleep? */
370 remove_from_queue(sma,q);
371 q->status = IN_WAKEUP;
373 * Continue scanning. The next operation
374 * that must be checked depends on the type of the
375 * completed operation:
376 * - if the operation modified the array, then
377 * restart from the head of the queue and
378 * check for threads that might be waiting
379 * for semaphore values to become 0.
380 * - if the operation didn't modify the array,
381 * then just continue.
384 n = sma->sem_pending;
387 wake_up_process(q->sleeper);
388 /* hands-off: q will disappear immediately after
400 /* The following counts are associated to each semaphore:
401 * semncnt number of tasks waiting on semval being nonzero
402 * semzcnt number of tasks waiting on semval being zero
403 * This model assumes that a task waits on exactly one semaphore.
404 * Since semaphore operations are to be performed atomically, tasks actually
405 * wait on a whole sequence of semaphores simultaneously.
406 * The counts we return here are a rough approximation, but still
407 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
409 static int count_semncnt (struct sem_array * sma, ushort semnum)
412 struct sem_queue * q;
415 for (q = sma->sem_pending; q; q = q->next) {
416 struct sembuf * sops = q->sops;
417 int nsops = q->nsops;
419 for (i = 0; i < nsops; i++)
420 if (sops[i].sem_num == semnum
421 && (sops[i].sem_op < 0)
422 && !(sops[i].sem_flg & IPC_NOWAIT))
427 static int count_semzcnt (struct sem_array * sma, ushort semnum)
430 struct sem_queue * q;
433 for (q = sma->sem_pending; q; q = q->next) {
434 struct sembuf * sops = q->sops;
435 int nsops = q->nsops;
437 for (i = 0; i < nsops; i++)
438 if (sops[i].sem_num == semnum
439 && (sops[i].sem_op == 0)
440 && !(sops[i].sem_flg & IPC_NOWAIT))
446 /* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
447 * the spinlock for this semaphore set hold. sem_ids.mutex remains locked
450 static void freeary (struct sem_array *sma, int id)
456 /* Invalidate the existing undo structures for this semaphore set.
457 * (They will be freed without any further action in exit_sem()
458 * or during the next semop.)
460 for (un = sma->undo; un; un = un->id_next)
463 /* Wake up all pending processes and let them fail with EIDRM. */
464 q = sma->sem_pending;
467 /* lazy remove_from_queue: we are killing the whole queue */
470 q->status = IN_WAKEUP;
471 wake_up_process(q->sleeper); /* doesn't sleep */
473 q->status = -EIDRM; /* hands-off q */
477 /* Remove the semaphore set from the ID array*/
481 used_sems -= sma->sem_nsems;
482 size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
483 security_sem_free(sma);
487 static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
491 return copy_to_user(buf, in, sizeof(*in));
496 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
498 out.sem_otime = in->sem_otime;
499 out.sem_ctime = in->sem_ctime;
500 out.sem_nsems = in->sem_nsems;
502 return copy_to_user(buf, &out, sizeof(out));
509 static int semctl_nolock(int semid, int semnum, int cmd, int version, union semun arg)
512 struct sem_array *sma;
518 struct seminfo seminfo;
521 err = security_sem_semctl(NULL, cmd);
525 memset(&seminfo,0,sizeof(seminfo));
526 seminfo.semmni = sc_semmni;
527 seminfo.semmns = sc_semmns;
528 seminfo.semmsl = sc_semmsl;
529 seminfo.semopm = sc_semopm;
530 seminfo.semvmx = SEMVMX;
531 seminfo.semmnu = SEMMNU;
532 seminfo.semmap = SEMMAP;
533 seminfo.semume = SEMUME;
534 mutex_lock(&sem_ids.mutex);
535 if (cmd == SEM_INFO) {
536 seminfo.semusz = sem_ids.in_use;
537 seminfo.semaem = used_sems;
539 seminfo.semusz = SEMUSZ;
540 seminfo.semaem = SEMAEM;
542 max_id = sem_ids.max_id;
543 mutex_unlock(&sem_ids.mutex);
544 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
546 return (max_id < 0) ? 0: max_id;
550 struct semid64_ds tbuf;
553 if(semid >= sem_ids.entries->size)
556 memset(&tbuf,0,sizeof(tbuf));
558 sma = sem_lock(semid);
563 if (ipcperms (&sma->sem_perm, S_IRUGO))
566 err = security_sem_semctl(sma, cmd);
570 id = sem_buildid(semid, sma->sem_perm.seq);
572 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
573 tbuf.sem_otime = sma->sem_otime;
574 tbuf.sem_ctime = sma->sem_ctime;
575 tbuf.sem_nsems = sma->sem_nsems;
577 if (copy_semid_to_user (arg.buf, &tbuf, version))
590 static int semctl_main(int semid, int semnum, int cmd, int version, union semun arg)
592 struct sem_array *sma;
595 ushort fast_sem_io[SEMMSL_FAST];
596 ushort* sem_io = fast_sem_io;
599 sma = sem_lock(semid);
603 nsems = sma->sem_nsems;
606 if (sem_checkid(sma,semid))
610 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
613 err = security_sem_semctl(sma, cmd);
621 ushort __user *array = arg.array;
624 if(nsems > SEMMSL_FAST) {
628 sem_io = ipc_alloc(sizeof(ushort)*nsems);
630 ipc_lock_by_ptr(&sma->sem_perm);
636 ipc_lock_by_ptr(&sma->sem_perm);
638 if (sma->sem_perm.deleted) {
645 for (i = 0; i < sma->sem_nsems; i++)
646 sem_io[i] = sma->sem_base[i].semval;
649 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
661 if(nsems > SEMMSL_FAST) {
662 sem_io = ipc_alloc(sizeof(ushort)*nsems);
664 ipc_lock_by_ptr(&sma->sem_perm);
671 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
672 ipc_lock_by_ptr(&sma->sem_perm);
679 for (i = 0; i < nsems; i++) {
680 if (sem_io[i] > SEMVMX) {
681 ipc_lock_by_ptr(&sma->sem_perm);
688 ipc_lock_by_ptr(&sma->sem_perm);
690 if (sma->sem_perm.deleted) {
696 for (i = 0; i < nsems; i++)
697 sma->sem_base[i].semval = sem_io[i];
698 for (un = sma->undo; un; un = un->id_next)
699 for (i = 0; i < nsems; i++)
701 sma->sem_ctime = get_seconds();
702 /* maybe some queued-up processes were waiting for this */
709 struct semid64_ds tbuf;
710 memset(&tbuf,0,sizeof(tbuf));
711 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
712 tbuf.sem_otime = sma->sem_otime;
713 tbuf.sem_ctime = sma->sem_ctime;
714 tbuf.sem_nsems = sma->sem_nsems;
716 if (copy_semid_to_user (arg.buf, &tbuf, version))
720 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
723 if(semnum < 0 || semnum >= nsems)
726 curr = &sma->sem_base[semnum];
736 err = count_semncnt(sma,semnum);
739 err = count_semzcnt(sma,semnum);
746 if (val > SEMVMX || val < 0)
749 for (un = sma->undo; un; un = un->id_next)
750 un->semadj[semnum] = 0;
752 curr->sempid = current->tgid;
753 sma->sem_ctime = get_seconds();
754 /* maybe some queued-up processes were waiting for this */
763 if(sem_io != fast_sem_io)
764 ipc_free(sem_io, sizeof(ushort)*nsems);
774 static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
779 struct semid64_ds tbuf;
781 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
784 out->uid = tbuf.sem_perm.uid;
785 out->gid = tbuf.sem_perm.gid;
786 out->mode = tbuf.sem_perm.mode;
792 struct semid_ds tbuf_old;
794 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
797 out->uid = tbuf_old.sem_perm.uid;
798 out->gid = tbuf_old.sem_perm.gid;
799 out->mode = tbuf_old.sem_perm.mode;
808 static int semctl_down(int semid, int semnum, int cmd, int version, union semun arg)
810 struct sem_array *sma;
812 struct sem_setbuf setbuf;
813 struct kern_ipc_perm *ipcp;
816 if(copy_semid_from_user (&setbuf, arg.buf, version))
819 sma = sem_lock(semid);
823 if (sem_checkid(sma,semid)) {
827 ipcp = &sma->sem_perm;
829 err = audit_ipc_obj(ipcp);
833 if (current->euid != ipcp->cuid &&
834 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
839 err = security_sem_semctl(sma, cmd);
849 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode, ipcp);
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 if (!vx_check(sma->sem_perm.xid, VX_IDENT))
1353 return seq_printf(s,
1354 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",