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*/
104 /* One queue for each sleeping process in the system. */
106 struct list_head list; /* queue of pending operations */
107 struct task_struct *sleeper; /* this process */
108 struct sem_undo *undo; /* undo structure */
109 int pid; /* process id of requesting process */
110 int status; /* completion status of operation */
111 struct sembuf *sops; /* array of pending operations */
112 int nsops; /* number of operations */
113 int alter; /* does *sops alter the array? */
116 /* Each task has a list of undo requests. They are executed automatically
117 * when the process exits.
120 struct list_head list_proc; /* per-process list: *
121 * all undos from one process
123 struct rcu_head rcu; /* rcu struct for sem_undo */
124 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
125 struct list_head list_id; /* per semaphore array list:
126 * all undos for one array */
127 int semid; /* semaphore set identifier */
128 short *semadj; /* array of adjustments */
129 /* one per semaphore */
132 /* sem_undo_list controls shared access to the list of sem_undo structures
133 * that may be shared among all a CLONE_SYSVSEM task group.
135 struct sem_undo_list {
138 struct list_head list_proc;
142 #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
144 #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
146 static int newary(struct ipc_namespace *, struct ipc_params *);
147 static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
148 #ifdef CONFIG_PROC_FS
149 static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
152 #define SEMMSL_FAST 256 /* 512 bytes on stack */
153 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
158 * sem_array.complex_count,
159 * sem_array.pending{_alter,_cont},
160 * sem_array.sem_undo: global sem_lock() for read/write
161 * sem_undo.proc_next: only "current" is allowed to read/write that field.
163 * sem_array.sem_base[i].pending_{const,alter}:
164 * global or semaphore sem_lock() for read/write
167 #define sc_semmsl sem_ctls[0]
168 #define sc_semmns sem_ctls[1]
169 #define sc_semopm sem_ctls[2]
170 #define sc_semmni sem_ctls[3]
172 void sem_init_ns(struct ipc_namespace *ns)
174 ns->sc_semmsl = SEMMSL;
175 ns->sc_semmns = SEMMNS;
176 ns->sc_semopm = SEMOPM;
177 ns->sc_semmni = SEMMNI;
179 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
183 void sem_exit_ns(struct ipc_namespace *ns)
185 free_ipcs(ns, &sem_ids(ns), freeary);
186 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
190 void __init sem_init (void)
192 sem_init_ns(&init_ipc_ns);
193 ipc_init_proc_interface("sysvipc/sem",
194 " key semid perms nsems uid gid cuid cgid otime ctime\n",
195 IPC_SEM_IDS, sysvipc_sem_proc_show);
199 * unmerge_queues - unmerge queues, if possible.
200 * @sma: semaphore array
202 * The function unmerges the wait queues if complex_count is 0.
203 * It must be called prior to dropping the global semaphore array lock.
205 static void unmerge_queues(struct sem_array *sma)
207 struct sem_queue *q, *tq;
209 /* complex operations still around? */
210 if (sma->complex_count)
213 * We will switch back to simple mode.
214 * Move all pending operation back into the per-semaphore
217 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
219 curr = &sma->sem_base[q->sops[0].sem_num];
221 list_add_tail(&q->list, &curr->pending_alter);
223 INIT_LIST_HEAD(&sma->pending_alter);
227 * merge_queues - Merge single semop queues into global queue
228 * @sma: semaphore array
230 * This function merges all per-semaphore queues into the global queue.
231 * It is necessary to achieve FIFO ordering for the pending single-sop
232 * operations when a multi-semop operation must sleep.
233 * Only the alter operations must be moved, the const operations can stay.
235 static void merge_queues(struct sem_array *sma)
238 for (i = 0; i < sma->sem_nsems; i++) {
239 struct sem *sem = sma->sem_base + i;
241 list_splice_init(&sem->pending_alter, &sma->pending_alter);
246 * If the request contains only one semaphore operation, and there are
247 * no complex transactions pending, lock only the semaphore involved.
248 * Otherwise, lock the entire semaphore array, since we either have
249 * multiple semaphores in our own semops, or we need to look at
250 * semaphores from other pending complex operations.
252 * Carefully guard against sma->complex_count changing between zero
253 * and non-zero while we are spinning for the lock. The value of
254 * sma->complex_count cannot change while we are holding the lock,
255 * so sem_unlock should be fine.
257 * The global lock path checks that all the local locks have been released,
258 * checking each local lock once. This means that the local lock paths
259 * cannot start their critical sections while the global lock is held.
261 static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
266 if (nsops == 1 && !sma->complex_count) {
267 struct sem *sem = sma->sem_base + sops->sem_num;
269 /* Lock just the semaphore we are interested in. */
270 spin_lock(&sem->lock);
273 * If sma->complex_count was set while we were spinning,
274 * we may need to look at things we did not lock here.
276 if (unlikely(sma->complex_count)) {
277 spin_unlock(&sem->lock);
282 * Another process is holding the global lock on the
283 * sem_array; we cannot enter our critical section,
284 * but have to wait for the global lock to be released.
286 if (unlikely(spin_is_locked(&sma->sem_perm.lock))) {
287 spin_unlock(&sem->lock);
288 spin_unlock_wait(&sma->sem_perm.lock);
292 locknum = sops->sem_num;
296 * Lock the semaphore array, and wait for all of the
297 * individual semaphore locks to go away. The code
298 * above ensures no new single-lock holders will enter
299 * their critical section while the array lock is held.
302 ipc_lock_object(&sma->sem_perm);
303 for (i = 0; i < sma->sem_nsems; i++) {
304 struct sem *sem = sma->sem_base + i;
305 spin_unlock_wait(&sem->lock);
312 static inline void sem_unlock(struct sem_array *sma, int locknum)
316 ipc_unlock_object(&sma->sem_perm);
318 struct sem *sem = sma->sem_base + locknum;
319 spin_unlock(&sem->lock);
324 * sem_lock_(check_) routines are called in the paths where the rw_mutex
327 * The caller holds the RCU read lock.
329 static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns,
330 int id, struct sembuf *sops, int nsops, int *locknum)
332 struct kern_ipc_perm *ipcp;
333 struct sem_array *sma;
335 ipcp = ipc_obtain_object(&sem_ids(ns), id);
337 return ERR_CAST(ipcp);
339 sma = container_of(ipcp, struct sem_array, sem_perm);
340 *locknum = sem_lock(sma, sops, nsops);
342 /* ipc_rmid() may have already freed the ID while sem_lock
343 * was spinning: verify that the structure is still valid
346 return container_of(ipcp, struct sem_array, sem_perm);
348 sem_unlock(sma, *locknum);
349 return ERR_PTR(-EINVAL);
352 static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
354 struct kern_ipc_perm *ipcp = ipc_obtain_object(&sem_ids(ns), id);
357 return ERR_CAST(ipcp);
359 return container_of(ipcp, struct sem_array, sem_perm);
362 static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
365 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
368 return ERR_CAST(ipcp);
370 return container_of(ipcp, struct sem_array, sem_perm);
373 static inline void sem_lock_and_putref(struct sem_array *sma)
375 sem_lock(sma, NULL, -1);
379 static inline void sem_putref(struct sem_array *sma)
384 static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
386 ipc_rmid(&sem_ids(ns), &s->sem_perm);
390 * Lockless wakeup algorithm:
391 * Without the check/retry algorithm a lockless wakeup is possible:
392 * - queue.status is initialized to -EINTR before blocking.
393 * - wakeup is performed by
394 * * unlinking the queue entry from the pending list
395 * * setting queue.status to IN_WAKEUP
396 * This is the notification for the blocked thread that a
397 * result value is imminent.
398 * * call wake_up_process
399 * * set queue.status to the final value.
400 * - the previously blocked thread checks queue.status:
401 * * if it's IN_WAKEUP, then it must wait until the value changes
402 * * if it's not -EINTR, then the operation was completed by
403 * update_queue. semtimedop can return queue.status without
404 * performing any operation on the sem array.
405 * * otherwise it must acquire the spinlock and check what's up.
407 * The two-stage algorithm is necessary to protect against the following
409 * - if queue.status is set after wake_up_process, then the woken up idle
410 * thread could race forward and try (and fail) to acquire sma->lock
411 * before update_queue had a chance to set queue.status
412 * - if queue.status is written before wake_up_process and if the
413 * blocked process is woken up by a signal between writing
414 * queue.status and the wake_up_process, then the woken up
415 * process could return from semtimedop and die by calling
416 * sys_exit before wake_up_process is called. Then wake_up_process
417 * will oops, because the task structure is already invalid.
418 * (yes, this happened on s390 with sysv msg).
424 * newary - Create a new semaphore set
426 * @params: ptr to the structure that contains key, semflg and nsems
428 * Called with sem_ids.rw_mutex held (as a writer)
431 static int newary(struct ipc_namespace *ns, struct ipc_params *params)
435 struct sem_array *sma;
437 key_t key = params->key;
438 int nsems = params->u.nsems;
439 int semflg = params->flg;
444 if (ns->used_sems + nsems > ns->sc_semmns)
447 size = sizeof (*sma) + nsems * sizeof (struct sem);
448 sma = ipc_rcu_alloc(size);
452 memset (sma, 0, size);
454 sma->sem_perm.mode = (semflg & S_IRWXUGO);
455 sma->sem_perm.key = key;
457 sma->sem_perm.security = NULL;
458 retval = security_sem_alloc(sma);
465 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
468 security_sem_free(sma);
472 ns->used_sems += nsems;
474 sma->sem_base = (struct sem *) &sma[1];
476 for (i = 0; i < nsems; i++) {
477 INIT_LIST_HEAD(&sma->sem_base[i].pending_alter);
478 INIT_LIST_HEAD(&sma->sem_base[i].pending_const);
479 spin_lock_init(&sma->sem_base[i].lock);
482 sma->complex_count = 0;
483 INIT_LIST_HEAD(&sma->pending_alter);
484 INIT_LIST_HEAD(&sma->pending_const);
485 INIT_LIST_HEAD(&sma->list_id);
486 sma->sem_nsems = nsems;
487 sma->sem_ctime = get_seconds();
491 return sma->sem_perm.id;
496 * Called with sem_ids.rw_mutex and ipcp locked.
498 static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
500 struct sem_array *sma;
502 sma = container_of(ipcp, struct sem_array, sem_perm);
503 return security_sem_associate(sma, semflg);
507 * Called with sem_ids.rw_mutex and ipcp locked.
509 static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
510 struct ipc_params *params)
512 struct sem_array *sma;
514 sma = container_of(ipcp, struct sem_array, sem_perm);
515 if (params->u.nsems > sma->sem_nsems)
521 SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
523 struct ipc_namespace *ns;
524 struct ipc_ops sem_ops;
525 struct ipc_params sem_params;
527 ns = current->nsproxy->ipc_ns;
529 if (nsems < 0 || nsems > ns->sc_semmsl)
532 sem_ops.getnew = newary;
533 sem_ops.associate = sem_security;
534 sem_ops.more_checks = sem_more_checks;
536 sem_params.key = key;
537 sem_params.flg = semflg;
538 sem_params.u.nsems = nsems;
540 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
543 /** perform_atomic_semop - Perform (if possible) a semaphore operation
544 * @sma: semaphore array
545 * @sops: array with operations that should be checked
546 * @nsems: number of sops
548 * @pid: pid that did the change
550 * Returns 0 if the operation was possible.
551 * Returns 1 if the operation is impossible, the caller must sleep.
552 * Negative values are error codes.
555 static int perform_atomic_semop(struct sem_array *sma, struct sembuf *sops,
556 int nsops, struct sem_undo *un, int pid)
562 for (sop = sops; sop < sops + nsops; sop++) {
563 curr = sma->sem_base + sop->sem_num;
564 sem_op = sop->sem_op;
565 result = curr->semval;
567 if (!sem_op && result)
575 if (sop->sem_flg & SEM_UNDO) {
576 int undo = un->semadj[sop->sem_num] - sem_op;
578 * Exceeding the undo range is an error.
580 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
583 curr->semval = result;
587 while (sop >= sops) {
588 sma->sem_base[sop->sem_num].sempid = pid;
589 if (sop->sem_flg & SEM_UNDO)
590 un->semadj[sop->sem_num] -= sop->sem_op;
601 if (sop->sem_flg & IPC_NOWAIT)
608 while (sop >= sops) {
609 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
616 /** wake_up_sem_queue_prepare(q, error): Prepare wake-up
617 * @q: queue entry that must be signaled
618 * @error: Error value for the signal
620 * Prepare the wake-up of the queue entry q.
622 static void wake_up_sem_queue_prepare(struct list_head *pt,
623 struct sem_queue *q, int error)
625 if (list_empty(pt)) {
627 * Hold preempt off so that we don't get preempted and have the
628 * wakee busy-wait until we're scheduled back on.
632 q->status = IN_WAKEUP;
635 list_add_tail(&q->list, pt);
639 * wake_up_sem_queue_do(pt) - do the actual wake-up
640 * @pt: list of tasks to be woken up
642 * Do the actual wake-up.
643 * The function is called without any locks held, thus the semaphore array
644 * could be destroyed already and the tasks can disappear as soon as the
645 * status is set to the actual return code.
647 static void wake_up_sem_queue_do(struct list_head *pt)
649 struct sem_queue *q, *t;
652 did_something = !list_empty(pt);
653 list_for_each_entry_safe(q, t, pt, list) {
654 wake_up_process(q->sleeper);
655 /* q can disappear immediately after writing q->status. */
663 static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
667 sma->complex_count--;
670 /** check_restart(sma, q)
671 * @sma: semaphore array
672 * @q: the operation that just completed
674 * update_queue is O(N^2) when it restarts scanning the whole queue of
675 * waiting operations. Therefore this function checks if the restart is
676 * really necessary. It is called after a previously waiting operation
677 * modified the array.
678 * Note that wait-for-zero operations are handled without restart.
680 static int check_restart(struct sem_array *sma, struct sem_queue *q)
682 /* pending complex alter operations are too difficult to analyse */
683 if (!list_empty(&sma->pending_alter))
686 /* we were a sleeping complex operation. Too difficult */
690 /* It is impossible that someone waits for the new value:
691 * - complex operations always restart.
692 * - wait-for-zero are handled seperately.
693 * - q is a previously sleeping simple operation that
694 * altered the array. It must be a decrement, because
695 * simple increments never sleep.
696 * - If there are older (higher priority) decrements
697 * in the queue, then they have observed the original
698 * semval value and couldn't proceed. The operation
699 * decremented to value - thus they won't proceed either.
705 * wake_const_ops(sma, semnum, pt) - Wake up non-alter tasks
706 * @sma: semaphore array.
707 * @semnum: semaphore that was modified.
708 * @pt: list head for the tasks that must be woken up.
710 * wake_const_ops must be called after a semaphore in a semaphore array
711 * was set to 0. If complex const operations are pending, wake_const_ops must
712 * be called with semnum = -1, as well as with the number of each modified
714 * The tasks that must be woken up are added to @pt. The return code
715 * is stored in q->pid.
716 * The function returns 1 if at least one operation was completed successfully.
718 static int wake_const_ops(struct sem_array *sma, int semnum,
719 struct list_head *pt)
722 struct list_head *walk;
723 struct list_head *pending_list;
724 int semop_completed = 0;
727 pending_list = &sma->pending_const;
729 pending_list = &sma->sem_base[semnum].pending_const;
731 walk = pending_list->next;
732 while (walk != pending_list) {
735 q = container_of(walk, struct sem_queue, list);
738 error = perform_atomic_semop(sma, q->sops, q->nsops,
742 /* operation completed, remove from queue & wakeup */
744 unlink_queue(sma, q);
746 wake_up_sem_queue_prepare(pt, q, error);
751 return semop_completed;
755 * do_smart_wakeup_zero(sma, sops, nsops, pt) - wakeup all wait for zero tasks
756 * @sma: semaphore array
757 * @sops: operations that were performed
758 * @nsops: number of operations
759 * @pt: list head of the tasks that must be woken up.
761 * do_smart_wakeup_zero() checks all required queue for wait-for-zero
762 * operations, based on the actual changes that were performed on the
764 * The function returns 1 if at least one operation was completed successfully.
766 static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
767 int nsops, struct list_head *pt)
770 int semop_completed = 0;
773 /* first: the per-semaphore queues, if known */
775 for (i = 0; i < nsops; i++) {
776 int num = sops[i].sem_num;
778 if (sma->sem_base[num].semval == 0) {
780 semop_completed |= wake_const_ops(sma, num, pt);
785 * No sops means modified semaphores not known.
786 * Assume all were changed.
788 for (i = 0; i < sma->sem_nsems; i++) {
789 if (sma->sem_base[i].semval == 0)
790 semop_completed |= wake_const_ops(sma, i, pt);
794 * If one of the modified semaphores got 0,
795 * then check the global queue, too.
798 semop_completed |= wake_const_ops(sma, -1, pt);
800 return semop_completed;
805 * update_queue(sma, semnum): Look for tasks that can be completed.
806 * @sma: semaphore array.
807 * @semnum: semaphore that was modified.
808 * @pt: list head for the tasks that must be woken up.
810 * update_queue must be called after a semaphore in a semaphore array
811 * was modified. If multiple semaphores were modified, update_queue must
812 * be called with semnum = -1, as well as with the number of each modified
814 * The tasks that must be woken up are added to @pt. The return code
815 * is stored in q->pid.
816 * The function internally checks if const operations can now succeed.
818 * The function return 1 if at least one semop was completed successfully.
820 static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
823 struct list_head *walk;
824 struct list_head *pending_list;
825 int semop_completed = 0;
828 pending_list = &sma->pending_alter;
830 pending_list = &sma->sem_base[semnum].pending_alter;
833 walk = pending_list->next;
834 while (walk != pending_list) {
837 q = container_of(walk, struct sem_queue, list);
840 /* If we are scanning the single sop, per-semaphore list of
841 * one semaphore and that semaphore is 0, then it is not
842 * necessary to scan further: simple increments
843 * that affect only one entry succeed immediately and cannot
844 * be in the per semaphore pending queue, and decrements
845 * cannot be successful if the value is already 0.
847 if (semnum != -1 && sma->sem_base[semnum].semval == 0)
850 error = perform_atomic_semop(sma, q->sops, q->nsops,
853 /* Does q->sleeper still need to sleep? */
857 unlink_queue(sma, q);
863 do_smart_wakeup_zero(sma, q->sops, q->nsops, pt);
864 restart = check_restart(sma, q);
867 wake_up_sem_queue_prepare(pt, q, error);
871 return semop_completed;
875 * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
876 * @sma: semaphore array
877 * @sops: operations that were performed
878 * @nsops: number of operations
879 * @otime: force setting otime
880 * @pt: list head of the tasks that must be woken up.
882 * do_smart_update() does the required calls to update_queue and wakeup_zero,
883 * based on the actual changes that were performed on the semaphore array.
884 * Note that the function does not do the actual wake-up: the caller is
885 * responsible for calling wake_up_sem_queue_do(@pt).
886 * It is safe to perform this call after dropping all locks.
888 static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
889 int otime, struct list_head *pt)
893 otime |= do_smart_wakeup_zero(sma, sops, nsops, pt);
895 if (!list_empty(&sma->pending_alter)) {
896 /* semaphore array uses the global queue - just process it. */
897 otime |= update_queue(sma, -1, pt);
901 * No sops, thus the modified semaphores are not
904 for (i = 0; i < sma->sem_nsems; i++)
905 otime |= update_queue(sma, i, pt);
908 * Check the semaphores that were increased:
909 * - No complex ops, thus all sleeping ops are
911 * - if we decreased the value, then any sleeping
912 * semaphore ops wont be able to run: If the
913 * previous value was too small, then the new
914 * value will be too small, too.
916 for (i = 0; i < nsops; i++) {
917 if (sops[i].sem_op > 0) {
918 otime |= update_queue(sma,
919 sops[i].sem_num, pt);
925 sma->sem_otime = get_seconds();
929 /* The following counts are associated to each semaphore:
930 * semncnt number of tasks waiting on semval being nonzero
931 * semzcnt number of tasks waiting on semval being zero
932 * This model assumes that a task waits on exactly one semaphore.
933 * Since semaphore operations are to be performed atomically, tasks actually
934 * wait on a whole sequence of semaphores simultaneously.
935 * The counts we return here are a rough approximation, but still
936 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
938 static int count_semncnt (struct sem_array * sma, ushort semnum)
941 struct sem_queue * q;
944 list_for_each_entry(q, &sma->sem_base[semnum].pending_alter, list) {
945 struct sembuf * sops = q->sops;
946 BUG_ON(sops->sem_num != semnum);
947 if ((sops->sem_op < 0) && !(sops->sem_flg & IPC_NOWAIT))
951 list_for_each_entry(q, &sma->pending_alter, list) {
952 struct sembuf * sops = q->sops;
953 int nsops = q->nsops;
955 for (i = 0; i < nsops; i++)
956 if (sops[i].sem_num == semnum
957 && (sops[i].sem_op < 0)
958 && !(sops[i].sem_flg & IPC_NOWAIT))
964 static int count_semzcnt (struct sem_array * sma, ushort semnum)
967 struct sem_queue * q;
970 list_for_each_entry(q, &sma->sem_base[semnum].pending_const, list) {
971 struct sembuf * sops = q->sops;
972 BUG_ON(sops->sem_num != semnum);
973 if ((sops->sem_op == 0) && !(sops->sem_flg & IPC_NOWAIT))
977 list_for_each_entry(q, &sma->pending_const, list) {
978 struct sembuf * sops = q->sops;
979 int nsops = q->nsops;
981 for (i = 0; i < nsops; i++)
982 if (sops[i].sem_num == semnum
983 && (sops[i].sem_op == 0)
984 && !(sops[i].sem_flg & IPC_NOWAIT))
990 /* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
991 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
992 * remains locked on exit.
994 static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
996 struct sem_undo *un, *tu;
997 struct sem_queue *q, *tq;
998 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
999 struct list_head tasks;
1002 /* Free the existing undo structures for this semaphore set. */
1003 ipc_assert_locked_object(&sma->sem_perm);
1004 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1005 list_del(&un->list_id);
1006 spin_lock(&un->ulp->lock);
1008 list_del_rcu(&un->list_proc);
1009 spin_unlock(&un->ulp->lock);
1013 /* Wake up all pending processes and let them fail with EIDRM. */
1014 INIT_LIST_HEAD(&tasks);
1015 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1016 unlink_queue(sma, q);
1017 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1020 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
1021 unlink_queue(sma, q);
1022 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1024 for (i = 0; i < sma->sem_nsems; i++) {
1025 struct sem *sem = sma->sem_base + i;
1026 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1027 unlink_queue(sma, q);
1028 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1030 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
1031 unlink_queue(sma, q);
1032 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1036 /* Remove the semaphore set from the IDR */
1038 sem_unlock(sma, -1);
1041 wake_up_sem_queue_do(&tasks);
1042 ns->used_sems -= sma->sem_nsems;
1043 security_sem_free(sma);
1044 ipc_rcu_putref(sma);
1047 static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1051 return copy_to_user(buf, in, sizeof(*in));
1054 struct semid_ds out;
1056 memset(&out, 0, sizeof(out));
1058 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1060 out.sem_otime = in->sem_otime;
1061 out.sem_ctime = in->sem_ctime;
1062 out.sem_nsems = in->sem_nsems;
1064 return copy_to_user(buf, &out, sizeof(out));
1071 static int semctl_nolock(struct ipc_namespace *ns, int semid,
1072 int cmd, int version, void __user *p)
1075 struct sem_array *sma;
1081 struct seminfo seminfo;
1084 err = security_sem_semctl(NULL, cmd);
1088 memset(&seminfo,0,sizeof(seminfo));
1089 seminfo.semmni = ns->sc_semmni;
1090 seminfo.semmns = ns->sc_semmns;
1091 seminfo.semmsl = ns->sc_semmsl;
1092 seminfo.semopm = ns->sc_semopm;
1093 seminfo.semvmx = SEMVMX;
1094 seminfo.semmnu = SEMMNU;
1095 seminfo.semmap = SEMMAP;
1096 seminfo.semume = SEMUME;
1097 down_read(&sem_ids(ns).rw_mutex);
1098 if (cmd == SEM_INFO) {
1099 seminfo.semusz = sem_ids(ns).in_use;
1100 seminfo.semaem = ns->used_sems;
1102 seminfo.semusz = SEMUSZ;
1103 seminfo.semaem = SEMAEM;
1105 max_id = ipc_get_maxid(&sem_ids(ns));
1106 up_read(&sem_ids(ns).rw_mutex);
1107 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
1109 return (max_id < 0) ? 0: max_id;
1114 struct semid64_ds tbuf;
1117 memset(&tbuf, 0, sizeof(tbuf));
1120 if (cmd == SEM_STAT) {
1121 sma = sem_obtain_object(ns, semid);
1126 id = sma->sem_perm.id;
1128 sma = sem_obtain_object_check(ns, semid);
1136 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
1139 err = security_sem_semctl(sma, cmd);
1143 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
1144 tbuf.sem_otime = sma->sem_otime;
1145 tbuf.sem_ctime = sma->sem_ctime;
1146 tbuf.sem_nsems = sma->sem_nsems;
1148 if (copy_semid_to_user(p, &tbuf, version))
1160 static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1163 struct sem_undo *un;
1164 struct sem_array *sma;
1167 struct list_head tasks;
1169 #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1170 /* big-endian 64bit */
1173 /* 32bit or little-endian 64bit */
1177 if (val > SEMVMX || val < 0)
1180 INIT_LIST_HEAD(&tasks);
1183 sma = sem_obtain_object_check(ns, semid);
1186 return PTR_ERR(sma);
1189 if (semnum < 0 || semnum >= sma->sem_nsems) {
1195 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1200 err = security_sem_semctl(sma, SETVAL);
1206 sem_lock(sma, NULL, -1);
1208 curr = &sma->sem_base[semnum];
1210 ipc_assert_locked_object(&sma->sem_perm);
1211 list_for_each_entry(un, &sma->list_id, list_id)
1212 un->semadj[semnum] = 0;
1215 curr->sempid = task_tgid_vnr(current);
1216 sma->sem_ctime = get_seconds();
1217 /* maybe some queued-up processes were waiting for this */
1218 do_smart_update(sma, NULL, 0, 0, &tasks);
1219 sem_unlock(sma, -1);
1221 wake_up_sem_queue_do(&tasks);
1225 static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
1226 int cmd, void __user *p)
1228 struct sem_array *sma;
1231 ushort fast_sem_io[SEMMSL_FAST];
1232 ushort* sem_io = fast_sem_io;
1233 struct list_head tasks;
1235 INIT_LIST_HEAD(&tasks);
1238 sma = sem_obtain_object_check(ns, semid);
1241 return PTR_ERR(sma);
1244 nsems = sma->sem_nsems;
1247 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1248 goto out_rcu_wakeup;
1250 err = security_sem_semctl(sma, cmd);
1252 goto out_rcu_wakeup;
1258 ushort __user *array = p;
1261 sem_lock(sma, NULL, -1);
1262 if(nsems > SEMMSL_FAST) {
1263 if (!ipc_rcu_getref(sma)) {
1264 sem_unlock(sma, -1);
1269 sem_unlock(sma, -1);
1271 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1272 if(sem_io == NULL) {
1278 sem_lock_and_putref(sma);
1279 if (sma->sem_perm.deleted) {
1280 sem_unlock(sma, -1);
1286 for (i = 0; i < sma->sem_nsems; i++)
1287 sem_io[i] = sma->sem_base[i].semval;
1288 sem_unlock(sma, -1);
1291 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
1298 struct sem_undo *un;
1300 if (!ipc_rcu_getref(sma)) {
1306 if(nsems > SEMMSL_FAST) {
1307 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1308 if(sem_io == NULL) {
1314 if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
1320 for (i = 0; i < nsems; i++) {
1321 if (sem_io[i] > SEMVMX) {
1328 sem_lock_and_putref(sma);
1329 if (sma->sem_perm.deleted) {
1330 sem_unlock(sma, -1);
1336 for (i = 0; i < nsems; i++)
1337 sma->sem_base[i].semval = sem_io[i];
1339 ipc_assert_locked_object(&sma->sem_perm);
1340 list_for_each_entry(un, &sma->list_id, list_id) {
1341 for (i = 0; i < nsems; i++)
1344 sma->sem_ctime = get_seconds();
1345 /* maybe some queued-up processes were waiting for this */
1346 do_smart_update(sma, NULL, 0, 0, &tasks);
1350 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
1353 if (semnum < 0 || semnum >= nsems)
1354 goto out_rcu_wakeup;
1356 sem_lock(sma, NULL, -1);
1357 curr = &sma->sem_base[semnum];
1367 err = count_semncnt(sma,semnum);
1370 err = count_semzcnt(sma,semnum);
1375 sem_unlock(sma, -1);
1378 wake_up_sem_queue_do(&tasks);
1380 if(sem_io != fast_sem_io)
1381 ipc_free(sem_io, sizeof(ushort)*nsems);
1385 static inline unsigned long
1386 copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
1390 if (copy_from_user(out, buf, sizeof(*out)))
1395 struct semid_ds tbuf_old;
1397 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1400 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1401 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1402 out->sem_perm.mode = tbuf_old.sem_perm.mode;
1412 * This function handles some semctl commands which require the rw_mutex
1413 * to be held in write mode.
1414 * NOTE: no locks must be held, the rw_mutex is taken inside this function.
1416 static int semctl_down(struct ipc_namespace *ns, int semid,
1417 int cmd, int version, void __user *p)
1419 struct sem_array *sma;
1421 struct semid64_ds semid64;
1422 struct kern_ipc_perm *ipcp;
1424 if(cmd == IPC_SET) {
1425 if (copy_semid_from_user(&semid64, p, version))
1429 down_write(&sem_ids(ns).rw_mutex);
1432 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1433 &semid64.sem_perm, 0);
1435 err = PTR_ERR(ipcp);
1439 sma = container_of(ipcp, struct sem_array, sem_perm);
1441 err = security_sem_semctl(sma, cmd);
1447 sem_lock(sma, NULL, -1);
1448 /* freeary unlocks the ipc object and rcu */
1452 sem_lock(sma, NULL, -1);
1453 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1456 sma->sem_ctime = get_seconds();
1464 sem_unlock(sma, -1);
1468 up_write(&sem_ids(ns).rw_mutex);
1472 SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
1475 struct ipc_namespace *ns;
1476 void __user *p = (void __user *)arg;
1481 version = ipc_parse_version(&cmd);
1482 ns = current->nsproxy->ipc_ns;
1489 return semctl_nolock(ns, semid, cmd, version, p);
1496 return semctl_main(ns, semid, semnum, cmd, p);
1498 return semctl_setval(ns, semid, semnum, arg);
1501 return semctl_down(ns, semid, cmd, version, p);
1507 /* If the task doesn't already have a undo_list, then allocate one
1508 * here. We guarantee there is only one thread using this undo list,
1509 * and current is THE ONE
1511 * If this allocation and assignment succeeds, but later
1512 * portions of this code fail, there is no need to free the sem_undo_list.
1513 * Just let it stay associated with the task, and it'll be freed later
1516 * This can block, so callers must hold no locks.
1518 static inline int get_undo_list(struct sem_undo_list **undo_listp)
1520 struct sem_undo_list *undo_list;
1522 undo_list = current->sysvsem.undo_list;
1524 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
1525 if (undo_list == NULL)
1527 spin_lock_init(&undo_list->lock);
1528 atomic_set(&undo_list->refcnt, 1);
1529 INIT_LIST_HEAD(&undo_list->list_proc);
1531 current->sysvsem.undo_list = undo_list;
1533 *undo_listp = undo_list;
1537 static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
1539 struct sem_undo *un;
1541 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1542 if (un->semid == semid)
1548 static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1550 struct sem_undo *un;
1552 assert_spin_locked(&ulp->lock);
1554 un = __lookup_undo(ulp, semid);
1556 list_del_rcu(&un->list_proc);
1557 list_add_rcu(&un->list_proc, &ulp->list_proc);
1563 * find_alloc_undo - Lookup (and if not present create) undo array
1565 * @semid: semaphore array id
1567 * The function looks up (and if not present creates) the undo structure.
1568 * The size of the undo structure depends on the size of the semaphore
1569 * array, thus the alloc path is not that straightforward.
1570 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1571 * performs a rcu_read_lock().
1573 static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
1575 struct sem_array *sma;
1576 struct sem_undo_list *ulp;
1577 struct sem_undo *un, *new;
1580 error = get_undo_list(&ulp);
1582 return ERR_PTR(error);
1585 spin_lock(&ulp->lock);
1586 un = lookup_undo(ulp, semid);
1587 spin_unlock(&ulp->lock);
1588 if (likely(un!=NULL))
1591 /* no undo structure around - allocate one. */
1592 /* step 1: figure out the size of the semaphore array */
1593 sma = sem_obtain_object_check(ns, semid);
1596 return ERR_CAST(sma);
1599 nsems = sma->sem_nsems;
1600 if (!ipc_rcu_getref(sma)) {
1602 un = ERR_PTR(-EIDRM);
1607 /* step 2: allocate new undo structure */
1608 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1611 return ERR_PTR(-ENOMEM);
1614 /* step 3: Acquire the lock on semaphore array */
1616 sem_lock_and_putref(sma);
1617 if (sma->sem_perm.deleted) {
1618 sem_unlock(sma, -1);
1621 un = ERR_PTR(-EIDRM);
1624 spin_lock(&ulp->lock);
1627 * step 4: check for races: did someone else allocate the undo struct?
1629 un = lookup_undo(ulp, semid);
1634 /* step 5: initialize & link new undo structure */
1635 new->semadj = (short *) &new[1];
1638 assert_spin_locked(&ulp->lock);
1639 list_add_rcu(&new->list_proc, &ulp->list_proc);
1640 ipc_assert_locked_object(&sma->sem_perm);
1641 list_add(&new->list_id, &sma->list_id);
1645 spin_unlock(&ulp->lock);
1646 sem_unlock(sma, -1);
1653 * get_queue_result - Retrieve the result code from sem_queue
1654 * @q: Pointer to queue structure
1656 * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
1657 * q->status, then we must loop until the value is replaced with the final
1658 * value: This may happen if a task is woken up by an unrelated event (e.g.
1659 * signal) and in parallel the task is woken up by another task because it got
1660 * the requested semaphores.
1662 * The function can be called with or without holding the semaphore spinlock.
1664 static int get_queue_result(struct sem_queue *q)
1669 while (unlikely(error == IN_WAKEUP)) {
1677 SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1678 unsigned, nsops, const struct timespec __user *, timeout)
1680 int error = -EINVAL;
1681 struct sem_array *sma;
1682 struct sembuf fast_sops[SEMOPM_FAST];
1683 struct sembuf* sops = fast_sops, *sop;
1684 struct sem_undo *un;
1685 int undos = 0, alter = 0, max, locknum;
1686 struct sem_queue queue;
1687 unsigned long jiffies_left = 0;
1688 struct ipc_namespace *ns;
1689 struct list_head tasks;
1691 ns = current->nsproxy->ipc_ns;
1693 if (nsops < 1 || semid < 0)
1695 if (nsops > ns->sc_semopm)
1697 if(nsops > SEMOPM_FAST) {
1698 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1702 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1707 struct timespec _timeout;
1708 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1712 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1713 _timeout.tv_nsec >= 1000000000L) {
1717 jiffies_left = timespec_to_jiffies(&_timeout);
1720 for (sop = sops; sop < sops + nsops; sop++) {
1721 if (sop->sem_num >= max)
1723 if (sop->sem_flg & SEM_UNDO)
1725 if (sop->sem_op != 0)
1729 INIT_LIST_HEAD(&tasks);
1732 /* On success, find_alloc_undo takes the rcu_read_lock */
1733 un = find_alloc_undo(ns, semid);
1735 error = PTR_ERR(un);
1743 sma = sem_obtain_object_check(ns, semid);
1746 error = PTR_ERR(sma);
1751 if (max >= sma->sem_nsems)
1752 goto out_rcu_wakeup;
1755 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1756 goto out_rcu_wakeup;
1758 error = security_sem_semop(sma, sops, nsops, alter);
1760 goto out_rcu_wakeup;
1763 * semid identifiers are not unique - find_alloc_undo may have
1764 * allocated an undo structure, it was invalidated by an RMID
1765 * and now a new array with received the same id. Check and fail.
1766 * This case can be detected checking un->semid. The existence of
1767 * "un" itself is guaranteed by rcu.
1770 locknum = sem_lock(sma, sops, nsops);
1771 if (un && un->semid == -1)
1772 goto out_unlock_free;
1774 error = perform_atomic_semop(sma, sops, nsops, un,
1775 task_tgid_vnr(current));
1777 if (alter && error == 0)
1778 do_smart_update(sma, sops, nsops, 1, &tasks);
1780 goto out_unlock_free;
1783 /* We need to sleep on this operation, so we put the current
1784 * task into the pending queue and go to sleep.
1788 queue.nsops = nsops;
1790 queue.pid = task_tgid_vnr(current);
1791 queue.alter = alter;
1795 curr = &sma->sem_base[sops->sem_num];
1798 if (sma->complex_count) {
1799 list_add_tail(&queue.list,
1800 &sma->pending_alter);
1803 list_add_tail(&queue.list,
1804 &curr->pending_alter);
1807 list_add_tail(&queue.list, &curr->pending_const);
1810 if (!sma->complex_count)
1814 list_add_tail(&queue.list, &sma->pending_alter);
1816 list_add_tail(&queue.list, &sma->pending_const);
1818 sma->complex_count++;
1821 queue.status = -EINTR;
1822 queue.sleeper = current;
1825 current->state = TASK_INTERRUPTIBLE;
1826 sem_unlock(sma, locknum);
1830 jiffies_left = schedule_timeout(jiffies_left);
1834 error = get_queue_result(&queue);
1836 if (error != -EINTR) {
1837 /* fast path: update_queue already obtained all requested
1839 * Perform a smp_mb(): User space could assume that semop()
1840 * is a memory barrier: Without the mb(), the cpu could
1841 * speculatively read in user space stale data that was
1842 * overwritten by the previous owner of the semaphore.
1850 sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
1853 * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
1855 error = get_queue_result(&queue);
1858 * Array removed? If yes, leave without sem_unlock().
1867 * If queue.status != -EINTR we are woken up by another process.
1868 * Leave without unlink_queue(), but with sem_unlock().
1871 if (error != -EINTR) {
1872 goto out_unlock_free;
1876 * If an interrupt occurred we have to clean up the queue
1878 if (timeout && jiffies_left == 0)
1882 * If the wakeup was spurious, just retry
1884 if (error == -EINTR && !signal_pending(current))
1887 unlink_queue(sma, &queue);
1890 sem_unlock(sma, locknum);
1893 wake_up_sem_queue_do(&tasks);
1895 if(sops != fast_sops)
1900 SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
1903 return sys_semtimedop(semid, tsops, nsops, NULL);
1906 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1907 * parent and child tasks.
1910 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1912 struct sem_undo_list *undo_list;
1915 if (clone_flags & CLONE_SYSVSEM) {
1916 error = get_undo_list(&undo_list);
1919 atomic_inc(&undo_list->refcnt);
1920 tsk->sysvsem.undo_list = undo_list;
1922 tsk->sysvsem.undo_list = NULL;
1928 * add semadj values to semaphores, free undo structures.
1929 * undo structures are not freed when semaphore arrays are destroyed
1930 * so some of them may be out of date.
1931 * IMPLEMENTATION NOTE: There is some confusion over whether the
1932 * set of adjustments that needs to be done should be done in an atomic
1933 * manner or not. That is, if we are attempting to decrement the semval
1934 * should we queue up and wait until we can do so legally?
1935 * The original implementation attempted to do this (queue and wait).
1936 * The current implementation does not do so. The POSIX standard
1937 * and SVID should be consulted to determine what behavior is mandated.
1939 void exit_sem(struct task_struct *tsk)
1941 struct sem_undo_list *ulp;
1943 ulp = tsk->sysvsem.undo_list;
1946 tsk->sysvsem.undo_list = NULL;
1948 if (!atomic_dec_and_test(&ulp->refcnt))
1952 struct sem_array *sma;
1953 struct sem_undo *un;
1954 struct list_head tasks;
1958 un = list_entry_rcu(ulp->list_proc.next,
1959 struct sem_undo, list_proc);
1960 if (&un->list_proc == &ulp->list_proc)
1970 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, un->semid);
1971 /* exit_sem raced with IPC_RMID, nothing to do */
1977 sem_lock(sma, NULL, -1);
1978 un = __lookup_undo(ulp, semid);
1980 /* exit_sem raced with IPC_RMID+semget() that created
1981 * exactly the same semid. Nothing to do.
1983 sem_unlock(sma, -1);
1988 /* remove un from the linked lists */
1989 ipc_assert_locked_object(&sma->sem_perm);
1990 list_del(&un->list_id);
1992 spin_lock(&ulp->lock);
1993 list_del_rcu(&un->list_proc);
1994 spin_unlock(&ulp->lock);
1996 /* perform adjustments registered in un */
1997 for (i = 0; i < sma->sem_nsems; i++) {
1998 struct sem * semaphore = &sma->sem_base[i];
1999 if (un->semadj[i]) {
2000 semaphore->semval += un->semadj[i];
2002 * Range checks of the new semaphore value,
2003 * not defined by sus:
2004 * - Some unices ignore the undo entirely
2005 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2006 * - some cap the value (e.g. FreeBSD caps
2007 * at 0, but doesn't enforce SEMVMX)
2009 * Linux caps the semaphore value, both at 0
2012 * Manfred <manfred@colorfullife.com>
2014 if (semaphore->semval < 0)
2015 semaphore->semval = 0;
2016 if (semaphore->semval > SEMVMX)
2017 semaphore->semval = SEMVMX;
2018 semaphore->sempid = task_tgid_vnr(current);
2021 /* maybe some queued-up processes were waiting for this */
2022 INIT_LIST_HEAD(&tasks);
2023 do_smart_update(sma, NULL, 0, 1, &tasks);
2024 sem_unlock(sma, -1);
2026 wake_up_sem_queue_do(&tasks);
2033 #ifdef CONFIG_PROC_FS
2034 static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
2036 struct user_namespace *user_ns = seq_user_ns(s);
2037 struct sem_array *sma = it;
2039 return seq_printf(s,
2040 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
2045 from_kuid_munged(user_ns, sma->sem_perm.uid),
2046 from_kgid_munged(user_ns, sma->sem_perm.gid),
2047 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2048 from_kgid_munged(user_ns, sma->sem_perm.cgid),