3 * Copyright (C) 1992 Krishna Balasubramanian
4 * Copyright (C) 1995 Eric Schenk, Bruno Haible
6 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
8 * SMP-threaded, sysctl's added
9 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
10 * Enforced range limit on SEM_UNDO
11 * (c) 2001 Red Hat Inc
13 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
14 * Further wakeup optimizations, documentation
15 * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
22 * Pavel Emelianov <xemul@openvz.org>
24 * Implementation notes: (May 2010)
25 * This file implements System V semaphores.
27 * User space visible behavior:
28 * - FIFO ordering for semop() operations (just FIFO, not starvation
30 * - multiple semaphore operations that alter the same semaphore in
31 * one semop() are handled.
32 * - sem_ctime (time of last semctl()) is updated in the IPC_SET, SETVAL and
34 * - two Linux specific semctl() commands: SEM_STAT, SEM_INFO.
35 * - undo adjustments at process exit are limited to 0..SEMVMX.
36 * - namespace are supported.
37 * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtine by writing
38 * to /proc/sys/kernel/sem.
39 * - statistics about the usage are reported in /proc/sysvipc/sem.
43 * - all global variables are read-mostly.
44 * - semop() calls and semctl(RMID) are synchronized by RCU.
45 * - most operations do write operations (actually: spin_lock calls) to
46 * the per-semaphore array structure.
47 * Thus: Perfect SMP scaling between independent semaphore arrays.
48 * If multiple semaphores in one array are used, then cache line
49 * trashing on the semaphore array spinlock will limit the scaling.
50 * - semncnt and semzcnt are calculated on demand in count_semncnt() and
52 * - the task that performs a successful semop() scans the list of all
53 * sleeping tasks and completes any pending operations that can be fulfilled.
54 * Semaphores are actively given to waiting tasks (necessary for FIFO).
55 * (see update_queue())
56 * - To improve the scalability, the actual wake-up calls are performed after
57 * dropping all locks. (see wake_up_sem_queue_prepare(),
58 * wake_up_sem_queue_do())
59 * - All work is done by the waker, the woken up task does not have to do
60 * anything - not even acquiring a lock or dropping a refcount.
61 * - A woken up task may not even touch the semaphore array anymore, it may
62 * have been destroyed already by a semctl(RMID).
63 * - The synchronizations between wake-ups due to a timeout/signal and a
64 * wake-up due to a completed semaphore operation is achieved by using an
65 * intermediate state (IN_WAKEUP).
66 * - UNDO values are stored in an array (one per process and per
67 * semaphore array, lazily allocated). For backwards compatibility, multiple
68 * modes for the UNDO variables are supported (per process, per thread)
69 * (see copy_semundo, CLONE_SYSVSEM)
70 * - There are two lists of the pending operations: a per-array list
71 * and per-semaphore list (stored in the array). This allows to achieve FIFO
72 * ordering without always scanning all pending operations.
73 * The worst-case behavior is nevertheless O(N^2) for N wakeups.
76 #include <linux/slab.h>
77 #include <linux/spinlock.h>
78 #include <linux/init.h>
79 #include <linux/proc_fs.h>
80 #include <linux/time.h>
81 #include <linux/security.h>
82 #include <linux/syscalls.h>
83 #include <linux/audit.h>
84 #include <linux/capability.h>
85 #include <linux/seq_file.h>
86 #include <linux/rwsem.h>
87 #include <linux/nsproxy.h>
88 #include <linux/ipc_namespace.h>
90 #include <asm/uaccess.h>
93 /* One semaphore structure for each semaphore in the system. */
95 int semval; /* current value */
96 int sempid; /* pid of last operation */
97 spinlock_t lock; /* spinlock for fine-grained semtimedop */
98 struct list_head pending_alter; /* pending single-sop operations */
99 /* that alter the semaphore */
100 struct list_head pending_const; /* pending single-sop operations */
101 /* that do not alter the semaphore*/
102 time_t sem_otime; /* candidate for sem_otime */
103 } ____cacheline_aligned_in_smp;
105 /* One queue for each sleeping process in the system. */
107 struct list_head list; /* queue of pending operations */
108 struct task_struct *sleeper; /* this process */
109 struct sem_undo *undo; /* undo structure */
110 int pid; /* process id of requesting process */
111 int status; /* completion status of operation */
112 struct sembuf *sops; /* array of pending operations */
113 int nsops; /* number of operations */
114 int alter; /* does *sops alter the array? */
117 /* Each task has a list of undo requests. They are executed automatically
118 * when the process exits.
121 struct list_head list_proc; /* per-process list: *
122 * all undos from one process
124 struct rcu_head rcu; /* rcu struct for sem_undo */
125 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
126 struct list_head list_id; /* per semaphore array list:
127 * all undos for one array */
128 int semid; /* semaphore set identifier */
129 short *semadj; /* array of adjustments */
130 /* one per semaphore */
133 /* sem_undo_list controls shared access to the list of sem_undo structures
134 * that may be shared among all a CLONE_SYSVSEM task group.
136 struct sem_undo_list {
139 struct list_head list_proc;
143 #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
145 #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
147 static int newary(struct ipc_namespace *, struct ipc_params *);
148 static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
149 #ifdef CONFIG_PROC_FS
150 static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
153 #define SEMMSL_FAST 256 /* 512 bytes on stack */
154 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
159 * sem_array.complex_count,
160 * sem_array.pending{_alter,_cont},
161 * sem_array.sem_undo: global sem_lock() for read/write
162 * sem_undo.proc_next: only "current" is allowed to read/write that field.
164 * sem_array.sem_base[i].pending_{const,alter}:
165 * global or semaphore sem_lock() for read/write
168 #define sc_semmsl sem_ctls[0]
169 #define sc_semmns sem_ctls[1]
170 #define sc_semopm sem_ctls[2]
171 #define sc_semmni sem_ctls[3]
173 void sem_init_ns(struct ipc_namespace *ns)
175 ns->sc_semmsl = SEMMSL;
176 ns->sc_semmns = SEMMNS;
177 ns->sc_semopm = SEMOPM;
178 ns->sc_semmni = SEMMNI;
180 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
184 void sem_exit_ns(struct ipc_namespace *ns)
186 free_ipcs(ns, &sem_ids(ns), freeary);
187 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
191 void __init sem_init (void)
193 sem_init_ns(&init_ipc_ns);
194 ipc_init_proc_interface("sysvipc/sem",
195 " key semid perms nsems uid gid cuid cgid otime ctime\n",
196 IPC_SEM_IDS, sysvipc_sem_proc_show);
200 * unmerge_queues - unmerge queues, if possible.
201 * @sma: semaphore array
203 * The function unmerges the wait queues if complex_count is 0.
204 * It must be called prior to dropping the global semaphore array lock.
206 static void unmerge_queues(struct sem_array *sma)
208 struct sem_queue *q, *tq;
210 /* complex operations still around? */
211 if (sma->complex_count)
214 * We will switch back to simple mode.
215 * Move all pending operation back into the per-semaphore
218 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
220 curr = &sma->sem_base[q->sops[0].sem_num];
222 list_add_tail(&q->list, &curr->pending_alter);
224 INIT_LIST_HEAD(&sma->pending_alter);
228 * merge_queues - Merge single semop queues into global queue
229 * @sma: semaphore array
231 * This function merges all per-semaphore queues into the global queue.
232 * It is necessary to achieve FIFO ordering for the pending single-sop
233 * operations when a multi-semop operation must sleep.
234 * Only the alter operations must be moved, the const operations can stay.
236 static void merge_queues(struct sem_array *sma)
239 for (i = 0; i < sma->sem_nsems; i++) {
240 struct sem *sem = sma->sem_base + i;
242 list_splice_init(&sem->pending_alter, &sma->pending_alter);
246 static void sem_rcu_free(struct rcu_head *head)
248 struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
249 struct sem_array *sma = ipc_rcu_to_struct(p);
251 security_sem_free(sma);
256 * Wait until all currently ongoing simple ops have completed.
257 * Caller must own sem_perm.lock.
258 * New simple ops cannot start, because simple ops first check
259 * that sem_perm.lock is free.
261 static void sem_wait_array(struct sem_array *sma)
266 for (i = 0; i < sma->sem_nsems; i++) {
267 sem = sma->sem_base + i;
268 spin_unlock_wait(&sem->lock);
273 * If the request contains only one semaphore operation, and there are
274 * no complex transactions pending, lock only the semaphore involved.
275 * Otherwise, lock the entire semaphore array, since we either have
276 * multiple semaphores in our own semops, or we need to look at
277 * semaphores from other pending complex operations.
279 static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
285 /* Complex operation - acquire a full lock */
286 ipc_lock_object(&sma->sem_perm);
288 /* And wait until all simple ops that are processed
289 * right now have dropped their locks.
296 * Only one semaphore affected - try to optimize locking.
298 * - optimized locking is possible if no complex operation
299 * is either enqueued or processed right now.
300 * - The test for enqueued complex ops is simple:
301 * sma->complex_count != 0
302 * - Testing for complex ops that are processed right now is
303 * a bit more difficult. Complex ops acquire the full lock
304 * and first wait that the running simple ops have completed.
306 * Thus: If we own a simple lock and the global lock is free
307 * and complex_count is now 0, then it will stay 0 and
308 * thus just locking sem->lock is sufficient.
310 sem = sma->sem_base + sops->sem_num;
312 if (sma->complex_count == 0) {
314 * It appears that no complex operation is around.
315 * Acquire the per-semaphore lock.
317 spin_lock(&sem->lock);
319 /* Then check that the global lock is free */
320 if (!spin_is_locked(&sma->sem_perm.lock)) {
321 /* spin_is_locked() is not a memory barrier */
324 /* Now repeat the test of complex_count:
325 * It can't change anymore until we drop sem->lock.
326 * Thus: if is now 0, then it will stay 0.
328 if (sma->complex_count == 0) {
329 /* fast path successful! */
330 return sops->sem_num;
333 spin_unlock(&sem->lock);
336 /* slow path: acquire the full lock */
337 ipc_lock_object(&sma->sem_perm);
339 if (sma->complex_count == 0) {
341 * There is no complex operation, thus we can switch
342 * back to the fast path.
344 spin_lock(&sem->lock);
345 ipc_unlock_object(&sma->sem_perm);
346 return sops->sem_num;
348 /* Not a false alarm, thus complete the sequence for a
356 static inline void sem_unlock(struct sem_array *sma, int locknum)
360 ipc_unlock_object(&sma->sem_perm);
362 struct sem *sem = sma->sem_base + locknum;
363 spin_unlock(&sem->lock);
368 * sem_lock_(check_) routines are called in the paths where the rwsem
371 * The caller holds the RCU read lock.
373 static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns,
374 int id, struct sembuf *sops, int nsops, int *locknum)
376 struct kern_ipc_perm *ipcp;
377 struct sem_array *sma;
379 ipcp = ipc_obtain_object(&sem_ids(ns), id);
381 return ERR_CAST(ipcp);
383 sma = container_of(ipcp, struct sem_array, sem_perm);
384 *locknum = sem_lock(sma, sops, nsops);
386 /* ipc_rmid() may have already freed the ID while sem_lock
387 * was spinning: verify that the structure is still valid
390 return container_of(ipcp, struct sem_array, sem_perm);
392 sem_unlock(sma, *locknum);
393 return ERR_PTR(-EINVAL);
396 static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
398 struct kern_ipc_perm *ipcp = ipc_obtain_object(&sem_ids(ns), id);
401 return ERR_CAST(ipcp);
403 return container_of(ipcp, struct sem_array, sem_perm);
406 static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
409 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
412 return ERR_CAST(ipcp);
414 return container_of(ipcp, struct sem_array, sem_perm);
417 static inline void sem_lock_and_putref(struct sem_array *sma)
419 sem_lock(sma, NULL, -1);
420 ipc_rcu_putref(sma, ipc_rcu_free);
423 static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
425 ipc_rmid(&sem_ids(ns), &s->sem_perm);
429 * Lockless wakeup algorithm:
430 * Without the check/retry algorithm a lockless wakeup is possible:
431 * - queue.status is initialized to -EINTR before blocking.
432 * - wakeup is performed by
433 * * unlinking the queue entry from the pending list
434 * * setting queue.status to IN_WAKEUP
435 * This is the notification for the blocked thread that a
436 * result value is imminent.
437 * * call wake_up_process
438 * * set queue.status to the final value.
439 * - the previously blocked thread checks queue.status:
440 * * if it's IN_WAKEUP, then it must wait until the value changes
441 * * if it's not -EINTR, then the operation was completed by
442 * update_queue. semtimedop can return queue.status without
443 * performing any operation on the sem array.
444 * * otherwise it must acquire the spinlock and check what's up.
446 * The two-stage algorithm is necessary to protect against the following
448 * - if queue.status is set after wake_up_process, then the woken up idle
449 * thread could race forward and try (and fail) to acquire sma->lock
450 * before update_queue had a chance to set queue.status
451 * - if queue.status is written before wake_up_process and if the
452 * blocked process is woken up by a signal between writing
453 * queue.status and the wake_up_process, then the woken up
454 * process could return from semtimedop and die by calling
455 * sys_exit before wake_up_process is called. Then wake_up_process
456 * will oops, because the task structure is already invalid.
457 * (yes, this happened on s390 with sysv msg).
463 * newary - Create a new semaphore set
465 * @params: ptr to the structure that contains key, semflg and nsems
467 * Called with sem_ids.rwsem held (as a writer)
470 static int newary(struct ipc_namespace *ns, struct ipc_params *params)
474 struct sem_array *sma;
476 key_t key = params->key;
477 int nsems = params->u.nsems;
478 int semflg = params->flg;
483 if (ns->used_sems + nsems > ns->sc_semmns)
486 size = sizeof (*sma) + nsems * sizeof (struct sem);
487 sma = ipc_rcu_alloc(size);
491 memset (sma, 0, size);
493 sma->sem_perm.mode = (semflg & S_IRWXUGO);
494 sma->sem_perm.key = key;
496 sma->sem_perm.security = NULL;
497 retval = security_sem_alloc(sma);
499 ipc_rcu_putref(sma, ipc_rcu_free);
503 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
505 ipc_rcu_putref(sma, sem_rcu_free);
508 ns->used_sems += nsems;
510 sma->sem_base = (struct sem *) &sma[1];
512 for (i = 0; i < nsems; i++) {
513 INIT_LIST_HEAD(&sma->sem_base[i].pending_alter);
514 INIT_LIST_HEAD(&sma->sem_base[i].pending_const);
515 spin_lock_init(&sma->sem_base[i].lock);
518 sma->complex_count = 0;
519 INIT_LIST_HEAD(&sma->pending_alter);
520 INIT_LIST_HEAD(&sma->pending_const);
521 INIT_LIST_HEAD(&sma->list_id);
522 sma->sem_nsems = nsems;
523 sma->sem_ctime = get_seconds();
527 return sma->sem_perm.id;
532 * Called with sem_ids.rwsem and ipcp locked.
534 static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
536 struct sem_array *sma;
538 sma = container_of(ipcp, struct sem_array, sem_perm);
539 return security_sem_associate(sma, semflg);
543 * Called with sem_ids.rwsem and ipcp locked.
545 static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
546 struct ipc_params *params)
548 struct sem_array *sma;
550 sma = container_of(ipcp, struct sem_array, sem_perm);
551 if (params->u.nsems > sma->sem_nsems)
557 SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
559 struct ipc_namespace *ns;
560 struct ipc_ops sem_ops;
561 struct ipc_params sem_params;
563 ns = current->nsproxy->ipc_ns;
565 if (nsems < 0 || nsems > ns->sc_semmsl)
568 sem_ops.getnew = newary;
569 sem_ops.associate = sem_security;
570 sem_ops.more_checks = sem_more_checks;
572 sem_params.key = key;
573 sem_params.flg = semflg;
574 sem_params.u.nsems = nsems;
576 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
579 /** perform_atomic_semop - Perform (if possible) a semaphore operation
580 * @sma: semaphore array
581 * @sops: array with operations that should be checked
582 * @nsems: number of sops
584 * @pid: pid that did the change
586 * Returns 0 if the operation was possible.
587 * Returns 1 if the operation is impossible, the caller must sleep.
588 * Negative values are error codes.
591 static int perform_atomic_semop(struct sem_array *sma, struct sembuf *sops,
592 int nsops, struct sem_undo *un, int pid)
598 for (sop = sops; sop < sops + nsops; sop++) {
599 curr = sma->sem_base + sop->sem_num;
600 sem_op = sop->sem_op;
601 result = curr->semval;
603 if (!sem_op && result)
611 if (sop->sem_flg & SEM_UNDO) {
612 int undo = un->semadj[sop->sem_num] - sem_op;
614 * Exceeding the undo range is an error.
616 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
619 curr->semval = result;
623 while (sop >= sops) {
624 sma->sem_base[sop->sem_num].sempid = pid;
625 if (sop->sem_flg & SEM_UNDO)
626 un->semadj[sop->sem_num] -= sop->sem_op;
637 if (sop->sem_flg & IPC_NOWAIT)
644 while (sop >= sops) {
645 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
652 /** wake_up_sem_queue_prepare(q, error): Prepare wake-up
653 * @q: queue entry that must be signaled
654 * @error: Error value for the signal
656 * Prepare the wake-up of the queue entry q.
658 static void wake_up_sem_queue_prepare(struct list_head *pt,
659 struct sem_queue *q, int error)
661 if (list_empty(pt)) {
663 * Hold preempt off so that we don't get preempted and have the
664 * wakee busy-wait until we're scheduled back on.
668 q->status = IN_WAKEUP;
671 list_add_tail(&q->list, pt);
675 * wake_up_sem_queue_do(pt) - do the actual wake-up
676 * @pt: list of tasks to be woken up
678 * Do the actual wake-up.
679 * The function is called without any locks held, thus the semaphore array
680 * could be destroyed already and the tasks can disappear as soon as the
681 * status is set to the actual return code.
683 static void wake_up_sem_queue_do(struct list_head *pt)
685 struct sem_queue *q, *t;
688 did_something = !list_empty(pt);
689 list_for_each_entry_safe(q, t, pt, list) {
690 wake_up_process(q->sleeper);
691 /* q can disappear immediately after writing q->status. */
699 static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
703 sma->complex_count--;
706 /** check_restart(sma, q)
707 * @sma: semaphore array
708 * @q: the operation that just completed
710 * update_queue is O(N^2) when it restarts scanning the whole queue of
711 * waiting operations. Therefore this function checks if the restart is
712 * really necessary. It is called after a previously waiting operation
713 * modified the array.
714 * Note that wait-for-zero operations are handled without restart.
716 static int check_restart(struct sem_array *sma, struct sem_queue *q)
718 /* pending complex alter operations are too difficult to analyse */
719 if (!list_empty(&sma->pending_alter))
722 /* we were a sleeping complex operation. Too difficult */
726 /* It is impossible that someone waits for the new value:
727 * - complex operations always restart.
728 * - wait-for-zero are handled seperately.
729 * - q is a previously sleeping simple operation that
730 * altered the array. It must be a decrement, because
731 * simple increments never sleep.
732 * - If there are older (higher priority) decrements
733 * in the queue, then they have observed the original
734 * semval value and couldn't proceed. The operation
735 * decremented to value - thus they won't proceed either.
741 * wake_const_ops(sma, semnum, pt) - Wake up non-alter tasks
742 * @sma: semaphore array.
743 * @semnum: semaphore that was modified.
744 * @pt: list head for the tasks that must be woken up.
746 * wake_const_ops must be called after a semaphore in a semaphore array
747 * was set to 0. If complex const operations are pending, wake_const_ops must
748 * be called with semnum = -1, as well as with the number of each modified
750 * The tasks that must be woken up are added to @pt. The return code
751 * is stored in q->pid.
752 * The function returns 1 if at least one operation was completed successfully.
754 static int wake_const_ops(struct sem_array *sma, int semnum,
755 struct list_head *pt)
758 struct list_head *walk;
759 struct list_head *pending_list;
760 int semop_completed = 0;
763 pending_list = &sma->pending_const;
765 pending_list = &sma->sem_base[semnum].pending_const;
767 walk = pending_list->next;
768 while (walk != pending_list) {
771 q = container_of(walk, struct sem_queue, list);
774 error = perform_atomic_semop(sma, q->sops, q->nsops,
778 /* operation completed, remove from queue & wakeup */
780 unlink_queue(sma, q);
782 wake_up_sem_queue_prepare(pt, q, error);
787 return semop_completed;
791 * do_smart_wakeup_zero(sma, sops, nsops, pt) - wakeup all wait for zero tasks
792 * @sma: semaphore array
793 * @sops: operations that were performed
794 * @nsops: number of operations
795 * @pt: list head of the tasks that must be woken up.
797 * do_smart_wakeup_zero() checks all required queue for wait-for-zero
798 * operations, based on the actual changes that were performed on the
800 * The function returns 1 if at least one operation was completed successfully.
802 static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
803 int nsops, struct list_head *pt)
806 int semop_completed = 0;
809 /* first: the per-semaphore queues, if known */
811 for (i = 0; i < nsops; i++) {
812 int num = sops[i].sem_num;
814 if (sma->sem_base[num].semval == 0) {
816 semop_completed |= wake_const_ops(sma, num, pt);
821 * No sops means modified semaphores not known.
822 * Assume all were changed.
824 for (i = 0; i < sma->sem_nsems; i++) {
825 if (sma->sem_base[i].semval == 0) {
827 semop_completed |= wake_const_ops(sma, i, pt);
832 * If one of the modified semaphores got 0,
833 * then check the global queue, too.
836 semop_completed |= wake_const_ops(sma, -1, pt);
838 return semop_completed;
843 * update_queue(sma, semnum): Look for tasks that can be completed.
844 * @sma: semaphore array.
845 * @semnum: semaphore that was modified.
846 * @pt: list head for the tasks that must be woken up.
848 * update_queue must be called after a semaphore in a semaphore array
849 * was modified. If multiple semaphores were modified, update_queue must
850 * be called with semnum = -1, as well as with the number of each modified
852 * The tasks that must be woken up are added to @pt. The return code
853 * is stored in q->pid.
854 * The function internally checks if const operations can now succeed.
856 * The function return 1 if at least one semop was completed successfully.
858 static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
861 struct list_head *walk;
862 struct list_head *pending_list;
863 int semop_completed = 0;
866 pending_list = &sma->pending_alter;
868 pending_list = &sma->sem_base[semnum].pending_alter;
871 walk = pending_list->next;
872 while (walk != pending_list) {
875 q = container_of(walk, struct sem_queue, list);
878 /* If we are scanning the single sop, per-semaphore list of
879 * one semaphore and that semaphore is 0, then it is not
880 * necessary to scan further: simple increments
881 * that affect only one entry succeed immediately and cannot
882 * be in the per semaphore pending queue, and decrements
883 * cannot be successful if the value is already 0.
885 if (semnum != -1 && sma->sem_base[semnum].semval == 0)
888 error = perform_atomic_semop(sma, q->sops, q->nsops,
891 /* Does q->sleeper still need to sleep? */
895 unlink_queue(sma, q);
901 do_smart_wakeup_zero(sma, q->sops, q->nsops, pt);
902 restart = check_restart(sma, q);
905 wake_up_sem_queue_prepare(pt, q, error);
909 return semop_completed;
913 * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
914 * @sma: semaphore array
915 * @sops: operations that were performed
916 * @nsops: number of operations
917 * @otime: force setting otime
918 * @pt: list head of the tasks that must be woken up.
920 * do_smart_update() does the required calls to update_queue and wakeup_zero,
921 * based on the actual changes that were performed on the semaphore array.
922 * Note that the function does not do the actual wake-up: the caller is
923 * responsible for calling wake_up_sem_queue_do(@pt).
924 * It is safe to perform this call after dropping all locks.
926 static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
927 int otime, struct list_head *pt)
931 otime |= do_smart_wakeup_zero(sma, sops, nsops, pt);
933 if (!list_empty(&sma->pending_alter)) {
934 /* semaphore array uses the global queue - just process it. */
935 otime |= update_queue(sma, -1, pt);
939 * No sops, thus the modified semaphores are not
942 for (i = 0; i < sma->sem_nsems; i++)
943 otime |= update_queue(sma, i, pt);
946 * Check the semaphores that were increased:
947 * - No complex ops, thus all sleeping ops are
949 * - if we decreased the value, then any sleeping
950 * semaphore ops wont be able to run: If the
951 * previous value was too small, then the new
952 * value will be too small, too.
954 for (i = 0; i < nsops; i++) {
955 if (sops[i].sem_op > 0) {
956 otime |= update_queue(sma,
957 sops[i].sem_num, pt);
964 sma->sem_base[0].sem_otime = get_seconds();
966 sma->sem_base[sops[0].sem_num].sem_otime =
973 /* The following counts are associated to each semaphore:
974 * semncnt number of tasks waiting on semval being nonzero
975 * semzcnt number of tasks waiting on semval being zero
976 * This model assumes that a task waits on exactly one semaphore.
977 * Since semaphore operations are to be performed atomically, tasks actually
978 * wait on a whole sequence of semaphores simultaneously.
979 * The counts we return here are a rough approximation, but still
980 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
982 static int count_semncnt (struct sem_array * sma, ushort semnum)
985 struct sem_queue * q;
988 list_for_each_entry(q, &sma->sem_base[semnum].pending_alter, list) {
989 struct sembuf * sops = q->sops;
990 BUG_ON(sops->sem_num != semnum);
991 if ((sops->sem_op < 0) && !(sops->sem_flg & IPC_NOWAIT))
995 list_for_each_entry(q, &sma->pending_alter, list) {
996 struct sembuf * sops = q->sops;
997 int nsops = q->nsops;
999 for (i = 0; i < nsops; i++)
1000 if (sops[i].sem_num == semnum
1001 && (sops[i].sem_op < 0)
1002 && !(sops[i].sem_flg & IPC_NOWAIT))
1008 static int count_semzcnt (struct sem_array * sma, ushort semnum)
1011 struct sem_queue * q;
1014 list_for_each_entry(q, &sma->sem_base[semnum].pending_const, list) {
1015 struct sembuf * sops = q->sops;
1016 BUG_ON(sops->sem_num != semnum);
1017 if ((sops->sem_op == 0) && !(sops->sem_flg & IPC_NOWAIT))
1021 list_for_each_entry(q, &sma->pending_const, list) {
1022 struct sembuf * sops = q->sops;
1023 int nsops = q->nsops;
1025 for (i = 0; i < nsops; i++)
1026 if (sops[i].sem_num == semnum
1027 && (sops[i].sem_op == 0)
1028 && !(sops[i].sem_flg & IPC_NOWAIT))
1034 /* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1035 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
1036 * remains locked on exit.
1038 static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
1040 struct sem_undo *un, *tu;
1041 struct sem_queue *q, *tq;
1042 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
1043 struct list_head tasks;
1046 /* Free the existing undo structures for this semaphore set. */
1047 ipc_assert_locked_object(&sma->sem_perm);
1048 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1049 list_del(&un->list_id);
1050 spin_lock(&un->ulp->lock);
1052 list_del_rcu(&un->list_proc);
1053 spin_unlock(&un->ulp->lock);
1057 /* Wake up all pending processes and let them fail with EIDRM. */
1058 INIT_LIST_HEAD(&tasks);
1059 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1060 unlink_queue(sma, q);
1061 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1064 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
1065 unlink_queue(sma, q);
1066 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1068 for (i = 0; i < sma->sem_nsems; i++) {
1069 struct sem *sem = sma->sem_base + i;
1070 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1071 unlink_queue(sma, q);
1072 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1074 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
1075 unlink_queue(sma, q);
1076 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1080 /* Remove the semaphore set from the IDR */
1082 sem_unlock(sma, -1);
1085 wake_up_sem_queue_do(&tasks);
1086 ns->used_sems -= sma->sem_nsems;
1087 ipc_rcu_putref(sma, sem_rcu_free);
1090 static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1094 return copy_to_user(buf, in, sizeof(*in));
1097 struct semid_ds out;
1099 memset(&out, 0, sizeof(out));
1101 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1103 out.sem_otime = in->sem_otime;
1104 out.sem_ctime = in->sem_ctime;
1105 out.sem_nsems = in->sem_nsems;
1107 return copy_to_user(buf, &out, sizeof(out));
1114 static time_t get_semotime(struct sem_array *sma)
1119 res = sma->sem_base[0].sem_otime;
1120 for (i = 1; i < sma->sem_nsems; i++) {
1121 time_t to = sma->sem_base[i].sem_otime;
1129 static int semctl_nolock(struct ipc_namespace *ns, int semid,
1130 int cmd, int version, void __user *p)
1133 struct sem_array *sma;
1139 struct seminfo seminfo;
1142 err = security_sem_semctl(NULL, cmd);
1146 memset(&seminfo,0,sizeof(seminfo));
1147 seminfo.semmni = ns->sc_semmni;
1148 seminfo.semmns = ns->sc_semmns;
1149 seminfo.semmsl = ns->sc_semmsl;
1150 seminfo.semopm = ns->sc_semopm;
1151 seminfo.semvmx = SEMVMX;
1152 seminfo.semmnu = SEMMNU;
1153 seminfo.semmap = SEMMAP;
1154 seminfo.semume = SEMUME;
1155 down_read(&sem_ids(ns).rwsem);
1156 if (cmd == SEM_INFO) {
1157 seminfo.semusz = sem_ids(ns).in_use;
1158 seminfo.semaem = ns->used_sems;
1160 seminfo.semusz = SEMUSZ;
1161 seminfo.semaem = SEMAEM;
1163 max_id = ipc_get_maxid(&sem_ids(ns));
1164 up_read(&sem_ids(ns).rwsem);
1165 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
1167 return (max_id < 0) ? 0: max_id;
1172 struct semid64_ds tbuf;
1175 memset(&tbuf, 0, sizeof(tbuf));
1178 if (cmd == SEM_STAT) {
1179 sma = sem_obtain_object(ns, semid);
1184 id = sma->sem_perm.id;
1186 sma = sem_obtain_object_check(ns, semid);
1194 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
1197 err = security_sem_semctl(sma, cmd);
1201 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
1202 tbuf.sem_otime = get_semotime(sma);
1203 tbuf.sem_ctime = sma->sem_ctime;
1204 tbuf.sem_nsems = sma->sem_nsems;
1206 if (copy_semid_to_user(p, &tbuf, version))
1218 static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1221 struct sem_undo *un;
1222 struct sem_array *sma;
1225 struct list_head tasks;
1227 #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1228 /* big-endian 64bit */
1231 /* 32bit or little-endian 64bit */
1235 if (val > SEMVMX || val < 0)
1238 INIT_LIST_HEAD(&tasks);
1241 sma = sem_obtain_object_check(ns, semid);
1244 return PTR_ERR(sma);
1247 if (semnum < 0 || semnum >= sma->sem_nsems) {
1253 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1258 err = security_sem_semctl(sma, SETVAL);
1264 sem_lock(sma, NULL, -1);
1266 curr = &sma->sem_base[semnum];
1268 ipc_assert_locked_object(&sma->sem_perm);
1269 list_for_each_entry(un, &sma->list_id, list_id)
1270 un->semadj[semnum] = 0;
1273 curr->sempid = task_tgid_vnr(current);
1274 sma->sem_ctime = get_seconds();
1275 /* maybe some queued-up processes were waiting for this */
1276 do_smart_update(sma, NULL, 0, 0, &tasks);
1277 sem_unlock(sma, -1);
1279 wake_up_sem_queue_do(&tasks);
1283 static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
1284 int cmd, void __user *p)
1286 struct sem_array *sma;
1289 ushort fast_sem_io[SEMMSL_FAST];
1290 ushort* sem_io = fast_sem_io;
1291 struct list_head tasks;
1293 INIT_LIST_HEAD(&tasks);
1296 sma = sem_obtain_object_check(ns, semid);
1299 return PTR_ERR(sma);
1302 nsems = sma->sem_nsems;
1305 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1306 goto out_rcu_wakeup;
1308 err = security_sem_semctl(sma, cmd);
1310 goto out_rcu_wakeup;
1316 ushort __user *array = p;
1319 sem_lock(sma, NULL, -1);
1320 if(nsems > SEMMSL_FAST) {
1321 if (!ipc_rcu_getref(sma)) {
1322 sem_unlock(sma, -1);
1327 sem_unlock(sma, -1);
1329 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1330 if(sem_io == NULL) {
1331 ipc_rcu_putref(sma, ipc_rcu_free);
1336 sem_lock_and_putref(sma);
1337 if (sma->sem_perm.deleted) {
1338 sem_unlock(sma, -1);
1344 for (i = 0; i < sma->sem_nsems; i++)
1345 sem_io[i] = sma->sem_base[i].semval;
1346 sem_unlock(sma, -1);
1349 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
1356 struct sem_undo *un;
1358 if (!ipc_rcu_getref(sma)) {
1364 if(nsems > SEMMSL_FAST) {
1365 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1366 if(sem_io == NULL) {
1367 ipc_rcu_putref(sma, ipc_rcu_free);
1372 if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
1373 ipc_rcu_putref(sma, ipc_rcu_free);
1378 for (i = 0; i < nsems; i++) {
1379 if (sem_io[i] > SEMVMX) {
1380 ipc_rcu_putref(sma, ipc_rcu_free);
1386 sem_lock_and_putref(sma);
1387 if (sma->sem_perm.deleted) {
1388 sem_unlock(sma, -1);
1394 for (i = 0; i < nsems; i++)
1395 sma->sem_base[i].semval = sem_io[i];
1397 ipc_assert_locked_object(&sma->sem_perm);
1398 list_for_each_entry(un, &sma->list_id, list_id) {
1399 for (i = 0; i < nsems; i++)
1402 sma->sem_ctime = get_seconds();
1403 /* maybe some queued-up processes were waiting for this */
1404 do_smart_update(sma, NULL, 0, 0, &tasks);
1408 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
1411 if (semnum < 0 || semnum >= nsems)
1412 goto out_rcu_wakeup;
1414 sem_lock(sma, NULL, -1);
1415 curr = &sma->sem_base[semnum];
1425 err = count_semncnt(sma,semnum);
1428 err = count_semzcnt(sma,semnum);
1433 sem_unlock(sma, -1);
1436 wake_up_sem_queue_do(&tasks);
1438 if(sem_io != fast_sem_io)
1439 ipc_free(sem_io, sizeof(ushort)*nsems);
1443 static inline unsigned long
1444 copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
1448 if (copy_from_user(out, buf, sizeof(*out)))
1453 struct semid_ds tbuf_old;
1455 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1458 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1459 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1460 out->sem_perm.mode = tbuf_old.sem_perm.mode;
1470 * This function handles some semctl commands which require the rwsem
1471 * to be held in write mode.
1472 * NOTE: no locks must be held, the rwsem is taken inside this function.
1474 static int semctl_down(struct ipc_namespace *ns, int semid,
1475 int cmd, int version, void __user *p)
1477 struct sem_array *sma;
1479 struct semid64_ds semid64;
1480 struct kern_ipc_perm *ipcp;
1482 if(cmd == IPC_SET) {
1483 if (copy_semid_from_user(&semid64, p, version))
1487 down_write(&sem_ids(ns).rwsem);
1490 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1491 &semid64.sem_perm, 0);
1493 err = PTR_ERR(ipcp);
1497 sma = container_of(ipcp, struct sem_array, sem_perm);
1499 err = security_sem_semctl(sma, cmd);
1505 sem_lock(sma, NULL, -1);
1506 /* freeary unlocks the ipc object and rcu */
1510 sem_lock(sma, NULL, -1);
1511 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1514 sma->sem_ctime = get_seconds();
1522 sem_unlock(sma, -1);
1526 up_write(&sem_ids(ns).rwsem);
1530 SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
1533 struct ipc_namespace *ns;
1534 void __user *p = (void __user *)arg;
1539 version = ipc_parse_version(&cmd);
1540 ns = current->nsproxy->ipc_ns;
1547 return semctl_nolock(ns, semid, cmd, version, p);
1554 return semctl_main(ns, semid, semnum, cmd, p);
1556 return semctl_setval(ns, semid, semnum, arg);
1559 return semctl_down(ns, semid, cmd, version, p);
1565 /* If the task doesn't already have a undo_list, then allocate one
1566 * here. We guarantee there is only one thread using this undo list,
1567 * and current is THE ONE
1569 * If this allocation and assignment succeeds, but later
1570 * portions of this code fail, there is no need to free the sem_undo_list.
1571 * Just let it stay associated with the task, and it'll be freed later
1574 * This can block, so callers must hold no locks.
1576 static inline int get_undo_list(struct sem_undo_list **undo_listp)
1578 struct sem_undo_list *undo_list;
1580 undo_list = current->sysvsem.undo_list;
1582 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
1583 if (undo_list == NULL)
1585 spin_lock_init(&undo_list->lock);
1586 atomic_set(&undo_list->refcnt, 1);
1587 INIT_LIST_HEAD(&undo_list->list_proc);
1589 current->sysvsem.undo_list = undo_list;
1591 *undo_listp = undo_list;
1595 static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
1597 struct sem_undo *un;
1599 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1600 if (un->semid == semid)
1606 static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1608 struct sem_undo *un;
1610 assert_spin_locked(&ulp->lock);
1612 un = __lookup_undo(ulp, semid);
1614 list_del_rcu(&un->list_proc);
1615 list_add_rcu(&un->list_proc, &ulp->list_proc);
1621 * find_alloc_undo - Lookup (and if not present create) undo array
1623 * @semid: semaphore array id
1625 * The function looks up (and if not present creates) the undo structure.
1626 * The size of the undo structure depends on the size of the semaphore
1627 * array, thus the alloc path is not that straightforward.
1628 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1629 * performs a rcu_read_lock().
1631 static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
1633 struct sem_array *sma;
1634 struct sem_undo_list *ulp;
1635 struct sem_undo *un, *new;
1638 error = get_undo_list(&ulp);
1640 return ERR_PTR(error);
1643 spin_lock(&ulp->lock);
1644 un = lookup_undo(ulp, semid);
1645 spin_unlock(&ulp->lock);
1646 if (likely(un!=NULL))
1649 /* no undo structure around - allocate one. */
1650 /* step 1: figure out the size of the semaphore array */
1651 sma = sem_obtain_object_check(ns, semid);
1654 return ERR_CAST(sma);
1657 nsems = sma->sem_nsems;
1658 if (!ipc_rcu_getref(sma)) {
1660 un = ERR_PTR(-EIDRM);
1665 /* step 2: allocate new undo structure */
1666 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1668 ipc_rcu_putref(sma, ipc_rcu_free);
1669 return ERR_PTR(-ENOMEM);
1672 /* step 3: Acquire the lock on semaphore array */
1674 sem_lock_and_putref(sma);
1675 if (sma->sem_perm.deleted) {
1676 sem_unlock(sma, -1);
1679 un = ERR_PTR(-EIDRM);
1682 spin_lock(&ulp->lock);
1685 * step 4: check for races: did someone else allocate the undo struct?
1687 un = lookup_undo(ulp, semid);
1692 /* step 5: initialize & link new undo structure */
1693 new->semadj = (short *) &new[1];
1696 assert_spin_locked(&ulp->lock);
1697 list_add_rcu(&new->list_proc, &ulp->list_proc);
1698 ipc_assert_locked_object(&sma->sem_perm);
1699 list_add(&new->list_id, &sma->list_id);
1703 spin_unlock(&ulp->lock);
1704 sem_unlock(sma, -1);
1711 * get_queue_result - Retrieve the result code from sem_queue
1712 * @q: Pointer to queue structure
1714 * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
1715 * q->status, then we must loop until the value is replaced with the final
1716 * value: This may happen if a task is woken up by an unrelated event (e.g.
1717 * signal) and in parallel the task is woken up by another task because it got
1718 * the requested semaphores.
1720 * The function can be called with or without holding the semaphore spinlock.
1722 static int get_queue_result(struct sem_queue *q)
1727 while (unlikely(error == IN_WAKEUP)) {
1735 SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1736 unsigned, nsops, const struct timespec __user *, timeout)
1738 int error = -EINVAL;
1739 struct sem_array *sma;
1740 struct sembuf fast_sops[SEMOPM_FAST];
1741 struct sembuf* sops = fast_sops, *sop;
1742 struct sem_undo *un;
1743 int undos = 0, alter = 0, max, locknum;
1744 struct sem_queue queue;
1745 unsigned long jiffies_left = 0;
1746 struct ipc_namespace *ns;
1747 struct list_head tasks;
1749 ns = current->nsproxy->ipc_ns;
1751 if (nsops < 1 || semid < 0)
1753 if (nsops > ns->sc_semopm)
1755 if(nsops > SEMOPM_FAST) {
1756 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1760 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1765 struct timespec _timeout;
1766 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1770 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1771 _timeout.tv_nsec >= 1000000000L) {
1775 jiffies_left = timespec_to_jiffies(&_timeout);
1778 for (sop = sops; sop < sops + nsops; sop++) {
1779 if (sop->sem_num >= max)
1781 if (sop->sem_flg & SEM_UNDO)
1783 if (sop->sem_op != 0)
1787 INIT_LIST_HEAD(&tasks);
1790 /* On success, find_alloc_undo takes the rcu_read_lock */
1791 un = find_alloc_undo(ns, semid);
1793 error = PTR_ERR(un);
1801 sma = sem_obtain_object_check(ns, semid);
1804 error = PTR_ERR(sma);
1809 if (max >= sma->sem_nsems)
1810 goto out_rcu_wakeup;
1813 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1814 goto out_rcu_wakeup;
1816 error = security_sem_semop(sma, sops, nsops, alter);
1818 goto out_rcu_wakeup;
1821 * semid identifiers are not unique - find_alloc_undo may have
1822 * allocated an undo structure, it was invalidated by an RMID
1823 * and now a new array with received the same id. Check and fail.
1824 * This case can be detected checking un->semid. The existence of
1825 * "un" itself is guaranteed by rcu.
1828 locknum = sem_lock(sma, sops, nsops);
1829 if (un && un->semid == -1)
1830 goto out_unlock_free;
1832 error = perform_atomic_semop(sma, sops, nsops, un,
1833 task_tgid_vnr(current));
1835 if (alter && error == 0)
1836 do_smart_update(sma, sops, nsops, 1, &tasks);
1838 goto out_unlock_free;
1841 /* We need to sleep on this operation, so we put the current
1842 * task into the pending queue and go to sleep.
1846 queue.nsops = nsops;
1848 queue.pid = task_tgid_vnr(current);
1849 queue.alter = alter;
1853 curr = &sma->sem_base[sops->sem_num];
1856 if (sma->complex_count) {
1857 list_add_tail(&queue.list,
1858 &sma->pending_alter);
1861 list_add_tail(&queue.list,
1862 &curr->pending_alter);
1865 list_add_tail(&queue.list, &curr->pending_const);
1868 if (!sma->complex_count)
1872 list_add_tail(&queue.list, &sma->pending_alter);
1874 list_add_tail(&queue.list, &sma->pending_const);
1876 sma->complex_count++;
1879 queue.status = -EINTR;
1880 queue.sleeper = current;
1883 current->state = TASK_INTERRUPTIBLE;
1884 sem_unlock(sma, locknum);
1888 jiffies_left = schedule_timeout(jiffies_left);
1892 error = get_queue_result(&queue);
1894 if (error != -EINTR) {
1895 /* fast path: update_queue already obtained all requested
1897 * Perform a smp_mb(): User space could assume that semop()
1898 * is a memory barrier: Without the mb(), the cpu could
1899 * speculatively read in user space stale data that was
1900 * overwritten by the previous owner of the semaphore.
1908 sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
1911 * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
1913 error = get_queue_result(&queue);
1916 * Array removed? If yes, leave without sem_unlock().
1925 * If queue.status != -EINTR we are woken up by another process.
1926 * Leave without unlink_queue(), but with sem_unlock().
1929 if (error != -EINTR) {
1930 goto out_unlock_free;
1934 * If an interrupt occurred we have to clean up the queue
1936 if (timeout && jiffies_left == 0)
1940 * If the wakeup was spurious, just retry
1942 if (error == -EINTR && !signal_pending(current))
1945 unlink_queue(sma, &queue);
1948 sem_unlock(sma, locknum);
1951 wake_up_sem_queue_do(&tasks);
1953 if(sops != fast_sops)
1958 SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
1961 return sys_semtimedop(semid, tsops, nsops, NULL);
1964 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1965 * parent and child tasks.
1968 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1970 struct sem_undo_list *undo_list;
1973 if (clone_flags & CLONE_SYSVSEM) {
1974 error = get_undo_list(&undo_list);
1977 atomic_inc(&undo_list->refcnt);
1978 tsk->sysvsem.undo_list = undo_list;
1980 tsk->sysvsem.undo_list = NULL;
1986 * add semadj values to semaphores, free undo structures.
1987 * undo structures are not freed when semaphore arrays are destroyed
1988 * so some of them may be out of date.
1989 * IMPLEMENTATION NOTE: There is some confusion over whether the
1990 * set of adjustments that needs to be done should be done in an atomic
1991 * manner or not. That is, if we are attempting to decrement the semval
1992 * should we queue up and wait until we can do so legally?
1993 * The original implementation attempted to do this (queue and wait).
1994 * The current implementation does not do so. The POSIX standard
1995 * and SVID should be consulted to determine what behavior is mandated.
1997 void exit_sem(struct task_struct *tsk)
1999 struct sem_undo_list *ulp;
2001 ulp = tsk->sysvsem.undo_list;
2004 tsk->sysvsem.undo_list = NULL;
2006 if (!atomic_dec_and_test(&ulp->refcnt))
2010 struct sem_array *sma;
2011 struct sem_undo *un;
2012 struct list_head tasks;
2016 un = list_entry_rcu(ulp->list_proc.next,
2017 struct sem_undo, list_proc);
2018 if (&un->list_proc == &ulp->list_proc)
2028 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, un->semid);
2029 /* exit_sem raced with IPC_RMID, nothing to do */
2035 sem_lock(sma, NULL, -1);
2036 un = __lookup_undo(ulp, semid);
2038 /* exit_sem raced with IPC_RMID+semget() that created
2039 * exactly the same semid. Nothing to do.
2041 sem_unlock(sma, -1);
2046 /* remove un from the linked lists */
2047 ipc_assert_locked_object(&sma->sem_perm);
2048 list_del(&un->list_id);
2050 spin_lock(&ulp->lock);
2051 list_del_rcu(&un->list_proc);
2052 spin_unlock(&ulp->lock);
2054 /* perform adjustments registered in un */
2055 for (i = 0; i < sma->sem_nsems; i++) {
2056 struct sem * semaphore = &sma->sem_base[i];
2057 if (un->semadj[i]) {
2058 semaphore->semval += un->semadj[i];
2060 * Range checks of the new semaphore value,
2061 * not defined by sus:
2062 * - Some unices ignore the undo entirely
2063 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2064 * - some cap the value (e.g. FreeBSD caps
2065 * at 0, but doesn't enforce SEMVMX)
2067 * Linux caps the semaphore value, both at 0
2070 * Manfred <manfred@colorfullife.com>
2072 if (semaphore->semval < 0)
2073 semaphore->semval = 0;
2074 if (semaphore->semval > SEMVMX)
2075 semaphore->semval = SEMVMX;
2076 semaphore->sempid = task_tgid_vnr(current);
2079 /* maybe some queued-up processes were waiting for this */
2080 INIT_LIST_HEAD(&tasks);
2081 do_smart_update(sma, NULL, 0, 1, &tasks);
2082 sem_unlock(sma, -1);
2084 wake_up_sem_queue_do(&tasks);
2091 #ifdef CONFIG_PROC_FS
2092 static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
2094 struct user_namespace *user_ns = seq_user_ns(s);
2095 struct sem_array *sma = it;
2098 sem_otime = get_semotime(sma);
2100 return seq_printf(s,
2101 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
2106 from_kuid_munged(user_ns, sma->sem_perm.uid),
2107 from_kgid_munged(user_ns, sma->sem_perm.gid),
2108 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2109 from_kgid_munged(user_ns, sma->sem_perm.cgid),