1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 1992 Krishna Balasubramanian
5 * Copyright (C) 1995 Eric Schenk, Bruno Haible
7 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
9 * SMP-threaded, sysctl's added
10 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
11 * Enforced range limit on SEM_UNDO
12 * (c) 2001 Red Hat Inc
14 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
15 * (c) 2016 Davidlohr Bueso <dave@stgolabs.net>
16 * Further wakeup optimizations, documentation
17 * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
19 * support for audit of ipc object properties and permission changes
20 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
24 * Pavel Emelianov <xemul@openvz.org>
26 * Implementation notes: (May 2010)
27 * This file implements System V semaphores.
29 * User space visible behavior:
30 * - FIFO ordering for semop() operations (just FIFO, not starvation
32 * - multiple semaphore operations that alter the same semaphore in
33 * one semop() are handled.
34 * - sem_ctime (time of last semctl()) is updated in the IPC_SET, SETVAL and
36 * - two Linux specific semctl() commands: SEM_STAT, SEM_INFO.
37 * - undo adjustments at process exit are limited to 0..SEMVMX.
38 * - namespace are supported.
39 * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtine by writing
40 * to /proc/sys/kernel/sem.
41 * - statistics about the usage are reported in /proc/sysvipc/sem.
45 * - all global variables are read-mostly.
46 * - semop() calls and semctl(RMID) are synchronized by RCU.
47 * - most operations do write operations (actually: spin_lock calls) to
48 * the per-semaphore array structure.
49 * Thus: Perfect SMP scaling between independent semaphore arrays.
50 * If multiple semaphores in one array are used, then cache line
51 * trashing on the semaphore array spinlock will limit the scaling.
52 * - semncnt and semzcnt are calculated on demand in count_semcnt()
53 * - the task that performs a successful semop() scans the list of all
54 * sleeping tasks and completes any pending operations that can be fulfilled.
55 * Semaphores are actively given to waiting tasks (necessary for FIFO).
56 * (see update_queue())
57 * - To improve the scalability, the actual wake-up calls are performed after
58 * dropping all locks. (see wake_up_sem_queue_prepare())
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 * - UNDO values are stored in an array (one per process and per
64 * semaphore array, lazily allocated). For backwards compatibility, multiple
65 * modes for the UNDO variables are supported (per process, per thread)
66 * (see copy_semundo, CLONE_SYSVSEM)
67 * - There are two lists of the pending operations: a per-array list
68 * and per-semaphore list (stored in the array). This allows to achieve FIFO
69 * ordering without always scanning all pending operations.
70 * The worst-case behavior is nevertheless O(N^2) for N wakeups.
73 #include <linux/slab.h>
74 #include <linux/spinlock.h>
75 #include <linux/init.h>
76 #include <linux/proc_fs.h>
77 #include <linux/time.h>
78 #include <linux/security.h>
79 #include <linux/syscalls.h>
80 #include <linux/audit.h>
81 #include <linux/capability.h>
82 #include <linux/seq_file.h>
83 #include <linux/rwsem.h>
84 #include <linux/nsproxy.h>
85 #include <linux/ipc_namespace.h>
86 #include <linux/sched/wake_q.h>
88 #include <linux/uaccess.h>
92 /* One queue for each sleeping process in the system. */
94 struct list_head list; /* queue of pending operations */
95 struct task_struct *sleeper; /* this process */
96 struct sem_undo *undo; /* undo structure */
97 int pid; /* process id of requesting process */
98 int status; /* completion status of operation */
99 struct sembuf *sops; /* array of pending operations */
100 struct sembuf *blocking; /* the operation that blocked */
101 int nsops; /* number of operations */
102 bool alter; /* does *sops alter the array? */
103 bool dupsop; /* sops on more than one sem_num */
106 /* Each task has a list of undo requests. They are executed automatically
107 * when the process exits.
110 struct list_head list_proc; /* per-process list: *
111 * all undos from one process
113 struct rcu_head rcu; /* rcu struct for sem_undo */
114 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
115 struct list_head list_id; /* per semaphore array list:
116 * all undos for one array */
117 int semid; /* semaphore set identifier */
118 short *semadj; /* array of adjustments */
119 /* one per semaphore */
122 /* sem_undo_list controls shared access to the list of sem_undo structures
123 * that may be shared among all a CLONE_SYSVSEM task group.
125 struct sem_undo_list {
128 struct list_head list_proc;
132 #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
134 static int newary(struct ipc_namespace *, struct ipc_params *);
135 static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
136 #ifdef CONFIG_PROC_FS
137 static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
140 #define SEMMSL_FAST 256 /* 512 bytes on stack */
141 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
144 * Switching from the mode suitable for simple ops
145 * to the mode for complex ops is costly. Therefore:
146 * use some hysteresis
148 #define USE_GLOBAL_LOCK_HYSTERESIS 10
152 * a) global sem_lock() for read/write
154 * sem_array.complex_count,
155 * sem_array.pending{_alter,_const},
158 * b) global or semaphore sem_lock() for read/write:
159 * sem_array.sems[i].pending_{const,alter}:
162 * sem_undo_list.list_proc:
163 * * undo_list->lock for write
166 * * global sem_lock() for write
167 * * either local or global sem_lock() for read.
170 * Most ordering is enforced by using spin_lock() and spin_unlock().
171 * The special case is use_global_lock:
172 * Setting it from non-zero to 0 is a RELEASE, this is ensured by
173 * using smp_store_release().
174 * Testing if it is non-zero is an ACQUIRE, this is ensured by using
175 * smp_load_acquire().
176 * Setting it from 0 to non-zero must be ordered with regards to
177 * this smp_load_acquire(), this is guaranteed because the smp_load_acquire()
178 * is inside a spin_lock() and after a write from 0 to non-zero a
179 * spin_lock()+spin_unlock() is done.
182 #define sc_semmsl sem_ctls[0]
183 #define sc_semmns sem_ctls[1]
184 #define sc_semopm sem_ctls[2]
185 #define sc_semmni sem_ctls[3]
187 int sem_init_ns(struct ipc_namespace *ns)
189 ns->sc_semmsl = SEMMSL;
190 ns->sc_semmns = SEMMNS;
191 ns->sc_semopm = SEMOPM;
192 ns->sc_semmni = SEMMNI;
194 return ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
198 void sem_exit_ns(struct ipc_namespace *ns)
200 free_ipcs(ns, &sem_ids(ns), freeary);
201 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
202 rhashtable_destroy(&ns->ids[IPC_SEM_IDS].key_ht);
206 int __init sem_init(void)
208 const int err = sem_init_ns(&init_ipc_ns);
210 ipc_init_proc_interface("sysvipc/sem",
211 " key semid perms nsems uid gid cuid cgid otime ctime\n",
212 IPC_SEM_IDS, sysvipc_sem_proc_show);
217 * unmerge_queues - unmerge queues, if possible.
218 * @sma: semaphore array
220 * The function unmerges the wait queues if complex_count is 0.
221 * It must be called prior to dropping the global semaphore array lock.
223 static void unmerge_queues(struct sem_array *sma)
225 struct sem_queue *q, *tq;
227 /* complex operations still around? */
228 if (sma->complex_count)
231 * We will switch back to simple mode.
232 * Move all pending operation back into the per-semaphore
235 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
237 curr = &sma->sems[q->sops[0].sem_num];
239 list_add_tail(&q->list, &curr->pending_alter);
241 INIT_LIST_HEAD(&sma->pending_alter);
245 * merge_queues - merge single semop queues into global queue
246 * @sma: semaphore array
248 * This function merges all per-semaphore queues into the global queue.
249 * It is necessary to achieve FIFO ordering for the pending single-sop
250 * operations when a multi-semop operation must sleep.
251 * Only the alter operations must be moved, the const operations can stay.
253 static void merge_queues(struct sem_array *sma)
256 for (i = 0; i < sma->sem_nsems; i++) {
257 struct sem *sem = &sma->sems[i];
259 list_splice_init(&sem->pending_alter, &sma->pending_alter);
263 static void sem_rcu_free(struct rcu_head *head)
265 struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu);
266 struct sem_array *sma = container_of(p, struct sem_array, sem_perm);
268 security_sem_free(sma);
273 * Enter the mode suitable for non-simple operations:
274 * Caller must own sem_perm.lock.
276 static void complexmode_enter(struct sem_array *sma)
281 if (sma->use_global_lock > 0) {
283 * We are already in global lock mode.
284 * Nothing to do, just reset the
285 * counter until we return to simple mode.
287 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
290 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
292 for (i = 0; i < sma->sem_nsems; i++) {
294 spin_lock(&sem->lock);
295 spin_unlock(&sem->lock);
300 * Try to leave the mode that disallows simple operations:
301 * Caller must own sem_perm.lock.
303 static void complexmode_tryleave(struct sem_array *sma)
305 if (sma->complex_count) {
306 /* Complex ops are sleeping.
307 * We must stay in complex mode
311 if (sma->use_global_lock == 1) {
313 * Immediately after setting use_global_lock to 0,
314 * a simple op can start. Thus: all memory writes
315 * performed by the current operation must be visible
316 * before we set use_global_lock to 0.
318 smp_store_release(&sma->use_global_lock, 0);
320 sma->use_global_lock--;
324 #define SEM_GLOBAL_LOCK (-1)
326 * If the request contains only one semaphore operation, and there are
327 * no complex transactions pending, lock only the semaphore involved.
328 * Otherwise, lock the entire semaphore array, since we either have
329 * multiple semaphores in our own semops, or we need to look at
330 * semaphores from other pending complex operations.
332 static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
338 /* Complex operation - acquire a full lock */
339 ipc_lock_object(&sma->sem_perm);
341 /* Prevent parallel simple ops */
342 complexmode_enter(sma);
343 return SEM_GLOBAL_LOCK;
347 * Only one semaphore affected - try to optimize locking.
348 * Optimized locking is possible if no complex operation
349 * is either enqueued or processed right now.
351 * Both facts are tracked by use_global_mode.
353 sem = &sma->sems[sops->sem_num];
356 * Initial check for use_global_lock. Just an optimization,
357 * no locking, no memory barrier.
359 if (!sma->use_global_lock) {
361 * It appears that no complex operation is around.
362 * Acquire the per-semaphore lock.
364 spin_lock(&sem->lock);
366 /* pairs with smp_store_release() */
367 if (!smp_load_acquire(&sma->use_global_lock)) {
368 /* fast path successful! */
369 return sops->sem_num;
371 spin_unlock(&sem->lock);
374 /* slow path: acquire the full lock */
375 ipc_lock_object(&sma->sem_perm);
377 if (sma->use_global_lock == 0) {
379 * The use_global_lock mode ended while we waited for
380 * sma->sem_perm.lock. Thus we must switch to locking
382 * Unlike in the fast path, there is no need to recheck
383 * sma->use_global_lock after we have acquired sem->lock:
384 * We own sma->sem_perm.lock, thus use_global_lock cannot
387 spin_lock(&sem->lock);
389 ipc_unlock_object(&sma->sem_perm);
390 return sops->sem_num;
393 * Not a false alarm, thus continue to use the global lock
394 * mode. No need for complexmode_enter(), this was done by
395 * the caller that has set use_global_mode to non-zero.
397 return SEM_GLOBAL_LOCK;
401 static inline void sem_unlock(struct sem_array *sma, int locknum)
403 if (locknum == SEM_GLOBAL_LOCK) {
405 complexmode_tryleave(sma);
406 ipc_unlock_object(&sma->sem_perm);
408 struct sem *sem = &sma->sems[locknum];
409 spin_unlock(&sem->lock);
414 * sem_lock_(check_) routines are called in the paths where the rwsem
417 * The caller holds the RCU read lock.
419 static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
421 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&sem_ids(ns), id);
424 return ERR_CAST(ipcp);
426 return container_of(ipcp, struct sem_array, sem_perm);
429 static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
432 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
435 return ERR_CAST(ipcp);
437 return container_of(ipcp, struct sem_array, sem_perm);
440 static inline void sem_lock_and_putref(struct sem_array *sma)
442 sem_lock(sma, NULL, -1);
443 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
446 static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
448 ipc_rmid(&sem_ids(ns), &s->sem_perm);
451 static struct sem_array *sem_alloc(size_t nsems)
453 struct sem_array *sma;
456 if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
459 size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
460 sma = kvmalloc(size, GFP_KERNEL);
464 memset(sma, 0, size);
470 * newary - Create a new semaphore set
472 * @params: ptr to the structure that contains key, semflg and nsems
474 * Called with sem_ids.rwsem held (as a writer)
476 static int newary(struct ipc_namespace *ns, struct ipc_params *params)
479 struct sem_array *sma;
480 key_t key = params->key;
481 int nsems = params->u.nsems;
482 int semflg = params->flg;
487 if (ns->used_sems + nsems > ns->sc_semmns)
490 sma = sem_alloc(nsems);
494 sma->sem_perm.mode = (semflg & S_IRWXUGO);
495 sma->sem_perm.key = key;
497 sma->sem_perm.security = NULL;
498 retval = security_sem_alloc(sma);
504 for (i = 0; i < nsems; i++) {
505 INIT_LIST_HEAD(&sma->sems[i].pending_alter);
506 INIT_LIST_HEAD(&sma->sems[i].pending_const);
507 spin_lock_init(&sma->sems[i].lock);
510 sma->complex_count = 0;
511 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
512 INIT_LIST_HEAD(&sma->pending_alter);
513 INIT_LIST_HEAD(&sma->pending_const);
514 INIT_LIST_HEAD(&sma->list_id);
515 sma->sem_nsems = nsems;
516 sma->sem_ctime = ktime_get_real_seconds();
518 /* ipc_addid() locks sma upon success. */
519 retval = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
521 call_rcu(&sma->sem_perm.rcu, sem_rcu_free);
524 ns->used_sems += nsems;
529 return sma->sem_perm.id;
534 * Called with sem_ids.rwsem and ipcp locked.
536 static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
538 struct sem_array *sma;
540 sma = container_of(ipcp, struct sem_array, sem_perm);
541 return security_sem_associate(sma, semflg);
545 * Called with sem_ids.rwsem and ipcp locked.
547 static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
548 struct ipc_params *params)
550 struct sem_array *sma;
552 sma = container_of(ipcp, struct sem_array, sem_perm);
553 if (params->u.nsems > sma->sem_nsems)
559 long ksys_semget(key_t key, int nsems, int semflg)
561 struct ipc_namespace *ns;
562 static const struct ipc_ops sem_ops = {
564 .associate = sem_security,
565 .more_checks = sem_more_checks,
567 struct ipc_params sem_params;
569 ns = current->nsproxy->ipc_ns;
571 if (nsems < 0 || nsems > ns->sc_semmsl)
574 sem_params.key = key;
575 sem_params.flg = semflg;
576 sem_params.u.nsems = nsems;
578 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
581 SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
583 return ksys_semget(key, nsems, semflg);
587 * perform_atomic_semop[_slow] - Attempt to perform semaphore
588 * operations on a given array.
589 * @sma: semaphore array
590 * @q: struct sem_queue that describes the operation
592 * Caller blocking are as follows, based the value
593 * indicated by the semaphore operation (sem_op):
595 * (1) >0 never blocks.
596 * (2) 0 (wait-for-zero operation): semval is non-zero.
597 * (3) <0 attempting to decrement semval to a value smaller than zero.
599 * Returns 0 if the operation was possible.
600 * Returns 1 if the operation is impossible, the caller must sleep.
601 * Returns <0 for error codes.
603 static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
605 int result, sem_op, nsops, pid;
615 for (sop = sops; sop < sops + nsops; sop++) {
616 curr = &sma->sems[sop->sem_num];
617 sem_op = sop->sem_op;
618 result = curr->semval;
620 if (!sem_op && result)
629 if (sop->sem_flg & SEM_UNDO) {
630 int undo = un->semadj[sop->sem_num] - sem_op;
631 /* Exceeding the undo range is an error. */
632 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
634 un->semadj[sop->sem_num] = undo;
637 curr->semval = result;
642 while (sop >= sops) {
643 sma->sems[sop->sem_num].sempid = pid;
656 if (sop->sem_flg & IPC_NOWAIT)
663 while (sop >= sops) {
664 sem_op = sop->sem_op;
665 sma->sems[sop->sem_num].semval -= sem_op;
666 if (sop->sem_flg & SEM_UNDO)
667 un->semadj[sop->sem_num] += sem_op;
674 static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
676 int result, sem_op, nsops;
686 if (unlikely(q->dupsop))
687 return perform_atomic_semop_slow(sma, q);
690 * We scan the semaphore set twice, first to ensure that the entire
691 * operation can succeed, therefore avoiding any pointless writes
692 * to shared memory and having to undo such changes in order to block
693 * until the operations can go through.
695 for (sop = sops; sop < sops + nsops; sop++) {
696 curr = &sma->sems[sop->sem_num];
697 sem_op = sop->sem_op;
698 result = curr->semval;
700 if (!sem_op && result)
701 goto would_block; /* wait-for-zero */
710 if (sop->sem_flg & SEM_UNDO) {
711 int undo = un->semadj[sop->sem_num] - sem_op;
713 /* Exceeding the undo range is an error. */
714 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
719 for (sop = sops; sop < sops + nsops; sop++) {
720 curr = &sma->sems[sop->sem_num];
721 sem_op = sop->sem_op;
722 result = curr->semval;
724 if (sop->sem_flg & SEM_UNDO) {
725 int undo = un->semadj[sop->sem_num] - sem_op;
727 un->semadj[sop->sem_num] = undo;
729 curr->semval += sem_op;
730 curr->sempid = q->pid;
737 return sop->sem_flg & IPC_NOWAIT ? -EAGAIN : 1;
740 static inline void wake_up_sem_queue_prepare(struct sem_queue *q, int error,
741 struct wake_q_head *wake_q)
743 wake_q_add(wake_q, q->sleeper);
745 * Rely on the above implicit barrier, such that we can
746 * ensure that we hold reference to the task before setting
747 * q->status. Otherwise we could race with do_exit if the
748 * task is awoken by an external event before calling
751 WRITE_ONCE(q->status, error);
754 static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
758 sma->complex_count--;
761 /** check_restart(sma, q)
762 * @sma: semaphore array
763 * @q: the operation that just completed
765 * update_queue is O(N^2) when it restarts scanning the whole queue of
766 * waiting operations. Therefore this function checks if the restart is
767 * really necessary. It is called after a previously waiting operation
768 * modified the array.
769 * Note that wait-for-zero operations are handled without restart.
771 static inline int check_restart(struct sem_array *sma, struct sem_queue *q)
773 /* pending complex alter operations are too difficult to analyse */
774 if (!list_empty(&sma->pending_alter))
777 /* we were a sleeping complex operation. Too difficult */
781 /* It is impossible that someone waits for the new value:
782 * - complex operations always restart.
783 * - wait-for-zero are handled seperately.
784 * - q is a previously sleeping simple operation that
785 * altered the array. It must be a decrement, because
786 * simple increments never sleep.
787 * - If there are older (higher priority) decrements
788 * in the queue, then they have observed the original
789 * semval value and couldn't proceed. The operation
790 * decremented to value - thus they won't proceed either.
796 * wake_const_ops - wake up non-alter tasks
797 * @sma: semaphore array.
798 * @semnum: semaphore that was modified.
799 * @wake_q: lockless wake-queue head.
801 * wake_const_ops must be called after a semaphore in a semaphore array
802 * was set to 0. If complex const operations are pending, wake_const_ops must
803 * be called with semnum = -1, as well as with the number of each modified
805 * The tasks that must be woken up are added to @wake_q. The return code
806 * is stored in q->pid.
807 * The function returns 1 if at least one operation was completed successfully.
809 static int wake_const_ops(struct sem_array *sma, int semnum,
810 struct wake_q_head *wake_q)
812 struct sem_queue *q, *tmp;
813 struct list_head *pending_list;
814 int semop_completed = 0;
817 pending_list = &sma->pending_const;
819 pending_list = &sma->sems[semnum].pending_const;
821 list_for_each_entry_safe(q, tmp, pending_list, list) {
822 int error = perform_atomic_semop(sma, q);
826 /* operation completed, remove from queue & wakeup */
827 unlink_queue(sma, q);
829 wake_up_sem_queue_prepare(q, error, wake_q);
834 return semop_completed;
838 * do_smart_wakeup_zero - wakeup all wait for zero tasks
839 * @sma: semaphore array
840 * @sops: operations that were performed
841 * @nsops: number of operations
842 * @wake_q: lockless wake-queue head
844 * Checks all required queue for wait-for-zero operations, based
845 * on the actual changes that were performed on the semaphore array.
846 * The function returns 1 if at least one operation was completed successfully.
848 static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
849 int nsops, struct wake_q_head *wake_q)
852 int semop_completed = 0;
855 /* first: the per-semaphore queues, if known */
857 for (i = 0; i < nsops; i++) {
858 int num = sops[i].sem_num;
860 if (sma->sems[num].semval == 0) {
862 semop_completed |= wake_const_ops(sma, num, wake_q);
867 * No sops means modified semaphores not known.
868 * Assume all were changed.
870 for (i = 0; i < sma->sem_nsems; i++) {
871 if (sma->sems[i].semval == 0) {
873 semop_completed |= wake_const_ops(sma, i, wake_q);
878 * If one of the modified semaphores got 0,
879 * then check the global queue, too.
882 semop_completed |= wake_const_ops(sma, -1, wake_q);
884 return semop_completed;
889 * update_queue - look for tasks that can be completed.
890 * @sma: semaphore array.
891 * @semnum: semaphore that was modified.
892 * @wake_q: lockless wake-queue head.
894 * update_queue must be called after a semaphore in a semaphore array
895 * was modified. If multiple semaphores were modified, update_queue must
896 * be called with semnum = -1, as well as with the number of each modified
898 * The tasks that must be woken up are added to @wake_q. The return code
899 * is stored in q->pid.
900 * The function internally checks if const operations can now succeed.
902 * The function return 1 if at least one semop was completed successfully.
904 static int update_queue(struct sem_array *sma, int semnum, struct wake_q_head *wake_q)
906 struct sem_queue *q, *tmp;
907 struct list_head *pending_list;
908 int semop_completed = 0;
911 pending_list = &sma->pending_alter;
913 pending_list = &sma->sems[semnum].pending_alter;
916 list_for_each_entry_safe(q, tmp, pending_list, list) {
919 /* If we are scanning the single sop, per-semaphore list of
920 * one semaphore and that semaphore is 0, then it is not
921 * necessary to scan further: simple increments
922 * that affect only one entry succeed immediately and cannot
923 * be in the per semaphore pending queue, and decrements
924 * cannot be successful if the value is already 0.
926 if (semnum != -1 && sma->sems[semnum].semval == 0)
929 error = perform_atomic_semop(sma, q);
931 /* Does q->sleeper still need to sleep? */
935 unlink_queue(sma, q);
941 do_smart_wakeup_zero(sma, q->sops, q->nsops, wake_q);
942 restart = check_restart(sma, q);
945 wake_up_sem_queue_prepare(q, error, wake_q);
949 return semop_completed;
953 * set_semotime - set sem_otime
954 * @sma: semaphore array
955 * @sops: operations that modified the array, may be NULL
957 * sem_otime is replicated to avoid cache line trashing.
958 * This function sets one instance to the current time.
960 static void set_semotime(struct sem_array *sma, struct sembuf *sops)
963 sma->sems[0].sem_otime = get_seconds();
965 sma->sems[sops[0].sem_num].sem_otime =
971 * do_smart_update - optimized update_queue
972 * @sma: semaphore array
973 * @sops: operations that were performed
974 * @nsops: number of operations
975 * @otime: force setting otime
976 * @wake_q: lockless wake-queue head
978 * do_smart_update() does the required calls to update_queue and wakeup_zero,
979 * based on the actual changes that were performed on the semaphore array.
980 * Note that the function does not do the actual wake-up: the caller is
981 * responsible for calling wake_up_q().
982 * It is safe to perform this call after dropping all locks.
984 static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
985 int otime, struct wake_q_head *wake_q)
989 otime |= do_smart_wakeup_zero(sma, sops, nsops, wake_q);
991 if (!list_empty(&sma->pending_alter)) {
992 /* semaphore array uses the global queue - just process it. */
993 otime |= update_queue(sma, -1, wake_q);
997 * No sops, thus the modified semaphores are not
1000 for (i = 0; i < sma->sem_nsems; i++)
1001 otime |= update_queue(sma, i, wake_q);
1004 * Check the semaphores that were increased:
1005 * - No complex ops, thus all sleeping ops are
1007 * - if we decreased the value, then any sleeping
1008 * semaphore ops wont be able to run: If the
1009 * previous value was too small, then the new
1010 * value will be too small, too.
1012 for (i = 0; i < nsops; i++) {
1013 if (sops[i].sem_op > 0) {
1014 otime |= update_queue(sma,
1015 sops[i].sem_num, wake_q);
1021 set_semotime(sma, sops);
1025 * check_qop: Test if a queued operation sleeps on the semaphore semnum
1027 static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
1030 struct sembuf *sop = q->blocking;
1033 * Linux always (since 0.99.10) reported a task as sleeping on all
1034 * semaphores. This violates SUS, therefore it was changed to the
1035 * standard compliant behavior.
1036 * Give the administrators a chance to notice that an application
1037 * might misbehave because it relies on the Linux behavior.
1039 pr_info_once("semctl(GETNCNT/GETZCNT) is since 3.16 Single Unix Specification compliant.\n"
1040 "The task %s (%d) triggered the difference, watch for misbehavior.\n",
1041 current->comm, task_pid_nr(current));
1043 if (sop->sem_num != semnum)
1046 if (count_zero && sop->sem_op == 0)
1048 if (!count_zero && sop->sem_op < 0)
1054 /* The following counts are associated to each semaphore:
1055 * semncnt number of tasks waiting on semval being nonzero
1056 * semzcnt number of tasks waiting on semval being zero
1058 * Per definition, a task waits only on the semaphore of the first semop
1059 * that cannot proceed, even if additional operation would block, too.
1061 static int count_semcnt(struct sem_array *sma, ushort semnum,
1064 struct list_head *l;
1065 struct sem_queue *q;
1069 /* First: check the simple operations. They are easy to evaluate */
1071 l = &sma->sems[semnum].pending_const;
1073 l = &sma->sems[semnum].pending_alter;
1075 list_for_each_entry(q, l, list) {
1076 /* all task on a per-semaphore list sleep on exactly
1082 /* Then: check the complex operations. */
1083 list_for_each_entry(q, &sma->pending_alter, list) {
1084 semcnt += check_qop(sma, semnum, q, count_zero);
1087 list_for_each_entry(q, &sma->pending_const, list) {
1088 semcnt += check_qop(sma, semnum, q, count_zero);
1094 /* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1095 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
1096 * remains locked on exit.
1098 static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
1100 struct sem_undo *un, *tu;
1101 struct sem_queue *q, *tq;
1102 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
1104 DEFINE_WAKE_Q(wake_q);
1106 /* Free the existing undo structures for this semaphore set. */
1107 ipc_assert_locked_object(&sma->sem_perm);
1108 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1109 list_del(&un->list_id);
1110 spin_lock(&un->ulp->lock);
1112 list_del_rcu(&un->list_proc);
1113 spin_unlock(&un->ulp->lock);
1117 /* Wake up all pending processes and let them fail with EIDRM. */
1118 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1119 unlink_queue(sma, q);
1120 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
1123 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
1124 unlink_queue(sma, q);
1125 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
1127 for (i = 0; i < sma->sem_nsems; i++) {
1128 struct sem *sem = &sma->sems[i];
1129 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1130 unlink_queue(sma, q);
1131 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
1133 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
1134 unlink_queue(sma, q);
1135 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
1139 /* Remove the semaphore set from the IDR */
1141 sem_unlock(sma, -1);
1145 ns->used_sems -= sma->sem_nsems;
1146 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
1149 static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1153 return copy_to_user(buf, in, sizeof(*in));
1156 struct semid_ds out;
1158 memset(&out, 0, sizeof(out));
1160 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1162 out.sem_otime = in->sem_otime;
1163 out.sem_ctime = in->sem_ctime;
1164 out.sem_nsems = in->sem_nsems;
1166 return copy_to_user(buf, &out, sizeof(out));
1173 static time64_t get_semotime(struct sem_array *sma)
1178 res = sma->sems[0].sem_otime;
1179 for (i = 1; i < sma->sem_nsems; i++) {
1180 time64_t to = sma->sems[i].sem_otime;
1188 static int semctl_stat(struct ipc_namespace *ns, int semid,
1189 int cmd, struct semid64_ds *semid64)
1191 struct sem_array *sma;
1195 memset(semid64, 0, sizeof(*semid64));
1198 if (cmd == SEM_STAT) {
1199 sma = sem_obtain_object(ns, semid);
1204 id = sma->sem_perm.id;
1206 sma = sem_obtain_object_check(ns, semid);
1214 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
1217 err = security_sem_semctl(sma, cmd);
1221 ipc_lock_object(&sma->sem_perm);
1223 if (!ipc_valid_object(&sma->sem_perm)) {
1224 ipc_unlock_object(&sma->sem_perm);
1229 kernel_to_ipc64_perm(&sma->sem_perm, &semid64->sem_perm);
1230 semid64->sem_otime = get_semotime(sma);
1231 semid64->sem_ctime = sma->sem_ctime;
1232 semid64->sem_nsems = sma->sem_nsems;
1234 ipc_unlock_object(&sma->sem_perm);
1243 static int semctl_info(struct ipc_namespace *ns, int semid,
1244 int cmd, void __user *p)
1246 struct seminfo seminfo;
1250 err = security_sem_semctl(NULL, cmd);
1254 memset(&seminfo, 0, sizeof(seminfo));
1255 seminfo.semmni = ns->sc_semmni;
1256 seminfo.semmns = ns->sc_semmns;
1257 seminfo.semmsl = ns->sc_semmsl;
1258 seminfo.semopm = ns->sc_semopm;
1259 seminfo.semvmx = SEMVMX;
1260 seminfo.semmnu = SEMMNU;
1261 seminfo.semmap = SEMMAP;
1262 seminfo.semume = SEMUME;
1263 down_read(&sem_ids(ns).rwsem);
1264 if (cmd == SEM_INFO) {
1265 seminfo.semusz = sem_ids(ns).in_use;
1266 seminfo.semaem = ns->used_sems;
1268 seminfo.semusz = SEMUSZ;
1269 seminfo.semaem = SEMAEM;
1271 max_id = ipc_get_maxid(&sem_ids(ns));
1272 up_read(&sem_ids(ns).rwsem);
1273 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
1275 return (max_id < 0) ? 0 : max_id;
1278 static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1281 struct sem_undo *un;
1282 struct sem_array *sma;
1285 DEFINE_WAKE_Q(wake_q);
1287 if (val > SEMVMX || val < 0)
1291 sma = sem_obtain_object_check(ns, semid);
1294 return PTR_ERR(sma);
1297 if (semnum < 0 || semnum >= sma->sem_nsems) {
1303 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1308 err = security_sem_semctl(sma, SETVAL);
1314 sem_lock(sma, NULL, -1);
1316 if (!ipc_valid_object(&sma->sem_perm)) {
1317 sem_unlock(sma, -1);
1322 curr = &sma->sems[semnum];
1324 ipc_assert_locked_object(&sma->sem_perm);
1325 list_for_each_entry(un, &sma->list_id, list_id)
1326 un->semadj[semnum] = 0;
1329 curr->sempid = task_tgid_vnr(current);
1330 sma->sem_ctime = ktime_get_real_seconds();
1331 /* maybe some queued-up processes were waiting for this */
1332 do_smart_update(sma, NULL, 0, 0, &wake_q);
1333 sem_unlock(sma, -1);
1339 static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
1340 int cmd, void __user *p)
1342 struct sem_array *sma;
1345 ushort fast_sem_io[SEMMSL_FAST];
1346 ushort *sem_io = fast_sem_io;
1347 DEFINE_WAKE_Q(wake_q);
1350 sma = sem_obtain_object_check(ns, semid);
1353 return PTR_ERR(sma);
1356 nsems = sma->sem_nsems;
1359 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1360 goto out_rcu_wakeup;
1362 err = security_sem_semctl(sma, cmd);
1364 goto out_rcu_wakeup;
1370 ushort __user *array = p;
1373 sem_lock(sma, NULL, -1);
1374 if (!ipc_valid_object(&sma->sem_perm)) {
1378 if (nsems > SEMMSL_FAST) {
1379 if (!ipc_rcu_getref(&sma->sem_perm)) {
1383 sem_unlock(sma, -1);
1385 sem_io = kvmalloc_array(nsems, sizeof(ushort),
1387 if (sem_io == NULL) {
1388 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
1393 sem_lock_and_putref(sma);
1394 if (!ipc_valid_object(&sma->sem_perm)) {
1399 for (i = 0; i < sma->sem_nsems; i++)
1400 sem_io[i] = sma->sems[i].semval;
1401 sem_unlock(sma, -1);
1404 if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
1411 struct sem_undo *un;
1413 if (!ipc_rcu_getref(&sma->sem_perm)) {
1415 goto out_rcu_wakeup;
1419 if (nsems > SEMMSL_FAST) {
1420 sem_io = kvmalloc_array(nsems, sizeof(ushort),
1422 if (sem_io == NULL) {
1423 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
1428 if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
1429 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
1434 for (i = 0; i < nsems; i++) {
1435 if (sem_io[i] > SEMVMX) {
1436 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
1442 sem_lock_and_putref(sma);
1443 if (!ipc_valid_object(&sma->sem_perm)) {
1448 for (i = 0; i < nsems; i++) {
1449 sma->sems[i].semval = sem_io[i];
1450 sma->sems[i].sempid = task_tgid_vnr(current);
1453 ipc_assert_locked_object(&sma->sem_perm);
1454 list_for_each_entry(un, &sma->list_id, list_id) {
1455 for (i = 0; i < nsems; i++)
1458 sma->sem_ctime = ktime_get_real_seconds();
1459 /* maybe some queued-up processes were waiting for this */
1460 do_smart_update(sma, NULL, 0, 0, &wake_q);
1464 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
1467 if (semnum < 0 || semnum >= nsems)
1468 goto out_rcu_wakeup;
1470 sem_lock(sma, NULL, -1);
1471 if (!ipc_valid_object(&sma->sem_perm)) {
1475 curr = &sma->sems[semnum];
1485 err = count_semcnt(sma, semnum, 0);
1488 err = count_semcnt(sma, semnum, 1);
1493 sem_unlock(sma, -1);
1498 if (sem_io != fast_sem_io)
1503 static inline unsigned long
1504 copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
1508 if (copy_from_user(out, buf, sizeof(*out)))
1513 struct semid_ds tbuf_old;
1515 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1518 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1519 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1520 out->sem_perm.mode = tbuf_old.sem_perm.mode;
1530 * This function handles some semctl commands which require the rwsem
1531 * to be held in write mode.
1532 * NOTE: no locks must be held, the rwsem is taken inside this function.
1534 static int semctl_down(struct ipc_namespace *ns, int semid,
1535 int cmd, struct semid64_ds *semid64)
1537 struct sem_array *sma;
1539 struct kern_ipc_perm *ipcp;
1541 down_write(&sem_ids(ns).rwsem);
1544 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1545 &semid64->sem_perm, 0);
1547 err = PTR_ERR(ipcp);
1551 sma = container_of(ipcp, struct sem_array, sem_perm);
1553 err = security_sem_semctl(sma, cmd);
1559 sem_lock(sma, NULL, -1);
1560 /* freeary unlocks the ipc object and rcu */
1564 sem_lock(sma, NULL, -1);
1565 err = ipc_update_perm(&semid64->sem_perm, ipcp);
1568 sma->sem_ctime = ktime_get_real_seconds();
1576 sem_unlock(sma, -1);
1580 up_write(&sem_ids(ns).rwsem);
1584 long ksys_semctl(int semid, int semnum, int cmd, unsigned long arg)
1587 struct ipc_namespace *ns;
1588 void __user *p = (void __user *)arg;
1589 struct semid64_ds semid64;
1595 version = ipc_parse_version(&cmd);
1596 ns = current->nsproxy->ipc_ns;
1601 return semctl_info(ns, semid, cmd, p);
1604 err = semctl_stat(ns, semid, cmd, &semid64);
1607 if (copy_semid_to_user(p, &semid64, version))
1616 return semctl_main(ns, semid, semnum, cmd, p);
1619 #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1620 /* big-endian 64bit */
1623 /* 32bit or little-endian 64bit */
1626 return semctl_setval(ns, semid, semnum, val);
1629 if (copy_semid_from_user(&semid64, p, version))
1632 return semctl_down(ns, semid, cmd, &semid64);
1638 SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
1640 return ksys_semctl(semid, semnum, cmd, arg);
1643 #ifdef CONFIG_COMPAT
1645 struct compat_semid_ds {
1646 struct compat_ipc_perm sem_perm;
1647 compat_time_t sem_otime;
1648 compat_time_t sem_ctime;
1649 compat_uptr_t sem_base;
1650 compat_uptr_t sem_pending;
1651 compat_uptr_t sem_pending_last;
1653 unsigned short sem_nsems;
1656 static int copy_compat_semid_from_user(struct semid64_ds *out, void __user *buf,
1659 memset(out, 0, sizeof(*out));
1660 if (version == IPC_64) {
1661 struct compat_semid64_ds __user *p = buf;
1662 return get_compat_ipc64_perm(&out->sem_perm, &p->sem_perm);
1664 struct compat_semid_ds __user *p = buf;
1665 return get_compat_ipc_perm(&out->sem_perm, &p->sem_perm);
1669 static int copy_compat_semid_to_user(void __user *buf, struct semid64_ds *in,
1672 if (version == IPC_64) {
1673 struct compat_semid64_ds v;
1674 memset(&v, 0, sizeof(v));
1675 to_compat_ipc64_perm(&v.sem_perm, &in->sem_perm);
1676 v.sem_otime = in->sem_otime;
1677 v.sem_ctime = in->sem_ctime;
1678 v.sem_nsems = in->sem_nsems;
1679 return copy_to_user(buf, &v, sizeof(v));
1681 struct compat_semid_ds v;
1682 memset(&v, 0, sizeof(v));
1683 to_compat_ipc_perm(&v.sem_perm, &in->sem_perm);
1684 v.sem_otime = in->sem_otime;
1685 v.sem_ctime = in->sem_ctime;
1686 v.sem_nsems = in->sem_nsems;
1687 return copy_to_user(buf, &v, sizeof(v));
1691 long compat_ksys_semctl(int semid, int semnum, int cmd, int arg)
1693 void __user *p = compat_ptr(arg);
1694 struct ipc_namespace *ns;
1695 struct semid64_ds semid64;
1696 int version = compat_ipc_parse_version(&cmd);
1699 ns = current->nsproxy->ipc_ns;
1704 switch (cmd & (~IPC_64)) {
1707 return semctl_info(ns, semid, cmd, p);
1710 err = semctl_stat(ns, semid, cmd, &semid64);
1713 if (copy_compat_semid_to_user(p, &semid64, version))
1722 return semctl_main(ns, semid, semnum, cmd, p);
1724 return semctl_setval(ns, semid, semnum, arg);
1726 if (copy_compat_semid_from_user(&semid64, p, version))
1730 return semctl_down(ns, semid, cmd, &semid64);
1736 COMPAT_SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, int, arg)
1738 return compat_ksys_semctl(semid, semnum, cmd, arg);
1742 /* If the task doesn't already have a undo_list, then allocate one
1743 * here. We guarantee there is only one thread using this undo list,
1744 * and current is THE ONE
1746 * If this allocation and assignment succeeds, but later
1747 * portions of this code fail, there is no need to free the sem_undo_list.
1748 * Just let it stay associated with the task, and it'll be freed later
1751 * This can block, so callers must hold no locks.
1753 static inline int get_undo_list(struct sem_undo_list **undo_listp)
1755 struct sem_undo_list *undo_list;
1757 undo_list = current->sysvsem.undo_list;
1759 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
1760 if (undo_list == NULL)
1762 spin_lock_init(&undo_list->lock);
1763 refcount_set(&undo_list->refcnt, 1);
1764 INIT_LIST_HEAD(&undo_list->list_proc);
1766 current->sysvsem.undo_list = undo_list;
1768 *undo_listp = undo_list;
1772 static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
1774 struct sem_undo *un;
1776 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1777 if (un->semid == semid)
1783 static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1785 struct sem_undo *un;
1787 assert_spin_locked(&ulp->lock);
1789 un = __lookup_undo(ulp, semid);
1791 list_del_rcu(&un->list_proc);
1792 list_add_rcu(&un->list_proc, &ulp->list_proc);
1798 * find_alloc_undo - lookup (and if not present create) undo array
1800 * @semid: semaphore array id
1802 * The function looks up (and if not present creates) the undo structure.
1803 * The size of the undo structure depends on the size of the semaphore
1804 * array, thus the alloc path is not that straightforward.
1805 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1806 * performs a rcu_read_lock().
1808 static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
1810 struct sem_array *sma;
1811 struct sem_undo_list *ulp;
1812 struct sem_undo *un, *new;
1815 error = get_undo_list(&ulp);
1817 return ERR_PTR(error);
1820 spin_lock(&ulp->lock);
1821 un = lookup_undo(ulp, semid);
1822 spin_unlock(&ulp->lock);
1823 if (likely(un != NULL))
1826 /* no undo structure around - allocate one. */
1827 /* step 1: figure out the size of the semaphore array */
1828 sma = sem_obtain_object_check(ns, semid);
1831 return ERR_CAST(sma);
1834 nsems = sma->sem_nsems;
1835 if (!ipc_rcu_getref(&sma->sem_perm)) {
1837 un = ERR_PTR(-EIDRM);
1842 /* step 2: allocate new undo structure */
1843 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1845 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
1846 return ERR_PTR(-ENOMEM);
1849 /* step 3: Acquire the lock on semaphore array */
1851 sem_lock_and_putref(sma);
1852 if (!ipc_valid_object(&sma->sem_perm)) {
1853 sem_unlock(sma, -1);
1856 un = ERR_PTR(-EIDRM);
1859 spin_lock(&ulp->lock);
1862 * step 4: check for races: did someone else allocate the undo struct?
1864 un = lookup_undo(ulp, semid);
1869 /* step 5: initialize & link new undo structure */
1870 new->semadj = (short *) &new[1];
1873 assert_spin_locked(&ulp->lock);
1874 list_add_rcu(&new->list_proc, &ulp->list_proc);
1875 ipc_assert_locked_object(&sma->sem_perm);
1876 list_add(&new->list_id, &sma->list_id);
1880 spin_unlock(&ulp->lock);
1881 sem_unlock(sma, -1);
1886 static long do_semtimedop(int semid, struct sembuf __user *tsops,
1887 unsigned nsops, const struct timespec64 *timeout)
1889 int error = -EINVAL;
1890 struct sem_array *sma;
1891 struct sembuf fast_sops[SEMOPM_FAST];
1892 struct sembuf *sops = fast_sops, *sop;
1893 struct sem_undo *un;
1895 bool undos = false, alter = false, dupsop = false;
1896 struct sem_queue queue;
1897 unsigned long dup = 0, jiffies_left = 0;
1898 struct ipc_namespace *ns;
1900 ns = current->nsproxy->ipc_ns;
1902 if (nsops < 1 || semid < 0)
1904 if (nsops > ns->sc_semopm)
1906 if (nsops > SEMOPM_FAST) {
1907 sops = kvmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
1912 if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
1918 if (timeout->tv_sec < 0 || timeout->tv_nsec < 0 ||
1919 timeout->tv_nsec >= 1000000000L) {
1923 jiffies_left = timespec64_to_jiffies(timeout);
1927 for (sop = sops; sop < sops + nsops; sop++) {
1928 unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
1930 if (sop->sem_num >= max)
1932 if (sop->sem_flg & SEM_UNDO)
1936 * There was a previous alter access that appears
1937 * to have accessed the same semaphore, thus use
1938 * the dupsop logic. "appears", because the detection
1939 * can only check % BITS_PER_LONG.
1943 if (sop->sem_op != 0) {
1950 /* On success, find_alloc_undo takes the rcu_read_lock */
1951 un = find_alloc_undo(ns, semid);
1953 error = PTR_ERR(un);
1961 sma = sem_obtain_object_check(ns, semid);
1964 error = PTR_ERR(sma);
1969 if (max >= sma->sem_nsems) {
1975 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
1980 error = security_sem_semop(sma, sops, nsops, alter);
1987 locknum = sem_lock(sma, sops, nsops);
1989 * We eventually might perform the following check in a lockless
1990 * fashion, considering ipc_valid_object() locking constraints.
1991 * If nsops == 1 and there is no contention for sem_perm.lock, then
1992 * only a per-semaphore lock is held and it's OK to proceed with the
1993 * check below. More details on the fine grained locking scheme
1994 * entangled here and why it's RMID race safe on comments at sem_lock()
1996 if (!ipc_valid_object(&sma->sem_perm))
1997 goto out_unlock_free;
1999 * semid identifiers are not unique - find_alloc_undo may have
2000 * allocated an undo structure, it was invalidated by an RMID
2001 * and now a new array with received the same id. Check and fail.
2002 * This case can be detected checking un->semid. The existence of
2003 * "un" itself is guaranteed by rcu.
2005 if (un && un->semid == -1)
2006 goto out_unlock_free;
2009 queue.nsops = nsops;
2011 queue.pid = task_tgid_vnr(current);
2012 queue.alter = alter;
2013 queue.dupsop = dupsop;
2015 error = perform_atomic_semop(sma, &queue);
2016 if (error == 0) { /* non-blocking succesfull path */
2017 DEFINE_WAKE_Q(wake_q);
2020 * If the operation was successful, then do
2021 * the required updates.
2024 do_smart_update(sma, sops, nsops, 1, &wake_q);
2026 set_semotime(sma, sops);
2028 sem_unlock(sma, locknum);
2034 if (error < 0) /* non-blocking error path */
2035 goto out_unlock_free;
2038 * We need to sleep on this operation, so we put the current
2039 * task into the pending queue and go to sleep.
2043 curr = &sma->sems[sops->sem_num];
2046 if (sma->complex_count) {
2047 list_add_tail(&queue.list,
2048 &sma->pending_alter);
2051 list_add_tail(&queue.list,
2052 &curr->pending_alter);
2055 list_add_tail(&queue.list, &curr->pending_const);
2058 if (!sma->complex_count)
2062 list_add_tail(&queue.list, &sma->pending_alter);
2064 list_add_tail(&queue.list, &sma->pending_const);
2066 sma->complex_count++;
2070 queue.status = -EINTR;
2071 queue.sleeper = current;
2073 __set_current_state(TASK_INTERRUPTIBLE);
2074 sem_unlock(sma, locknum);
2078 jiffies_left = schedule_timeout(jiffies_left);
2083 * fastpath: the semop has completed, either successfully or
2084 * not, from the syscall pov, is quite irrelevant to us at this
2085 * point; we're done.
2087 * We _do_ care, nonetheless, about being awoken by a signal or
2088 * spuriously. The queue.status is checked again in the
2089 * slowpath (aka after taking sem_lock), such that we can detect
2090 * scenarios where we were awakened externally, during the
2091 * window between wake_q_add() and wake_up_q().
2093 error = READ_ONCE(queue.status);
2094 if (error != -EINTR) {
2096 * User space could assume that semop() is a memory
2097 * barrier: Without the mb(), the cpu could
2098 * speculatively read in userspace stale data that was
2099 * overwritten by the previous owner of the semaphore.
2106 locknum = sem_lock(sma, sops, nsops);
2108 if (!ipc_valid_object(&sma->sem_perm))
2109 goto out_unlock_free;
2111 error = READ_ONCE(queue.status);
2114 * If queue.status != -EINTR we are woken up by another process.
2115 * Leave without unlink_queue(), but with sem_unlock().
2117 if (error != -EINTR)
2118 goto out_unlock_free;
2121 * If an interrupt occurred we have to clean up the queue.
2123 if (timeout && jiffies_left == 0)
2125 } while (error == -EINTR && !signal_pending(current)); /* spurious */
2127 unlink_queue(sma, &queue);
2130 sem_unlock(sma, locknum);
2133 if (sops != fast_sops)
2138 long ksys_semtimedop(int semid, struct sembuf __user *tsops,
2139 unsigned int nsops, const struct timespec __user *timeout)
2142 struct timespec64 ts;
2143 if (get_timespec64(&ts, timeout))
2145 return do_semtimedop(semid, tsops, nsops, &ts);
2147 return do_semtimedop(semid, tsops, nsops, NULL);
2150 SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
2151 unsigned int, nsops, const struct timespec __user *, timeout)
2153 return ksys_semtimedop(semid, tsops, nsops, timeout);
2156 #ifdef CONFIG_COMPAT
2157 long compat_ksys_semtimedop(int semid, struct sembuf __user *tsems,
2159 const struct compat_timespec __user *timeout)
2162 struct timespec64 ts;
2163 if (compat_get_timespec64(&ts, timeout))
2165 return do_semtimedop(semid, tsems, nsops, &ts);
2167 return do_semtimedop(semid, tsems, nsops, NULL);
2170 COMPAT_SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsems,
2171 unsigned int, nsops,
2172 const struct compat_timespec __user *, timeout)
2174 return compat_ksys_semtimedop(semid, tsems, nsops, timeout);
2178 SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
2181 return do_semtimedop(semid, tsops, nsops, NULL);
2184 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
2185 * parent and child tasks.
2188 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
2190 struct sem_undo_list *undo_list;
2193 if (clone_flags & CLONE_SYSVSEM) {
2194 error = get_undo_list(&undo_list);
2197 refcount_inc(&undo_list->refcnt);
2198 tsk->sysvsem.undo_list = undo_list;
2200 tsk->sysvsem.undo_list = NULL;
2206 * add semadj values to semaphores, free undo structures.
2207 * undo structures are not freed when semaphore arrays are destroyed
2208 * so some of them may be out of date.
2209 * IMPLEMENTATION NOTE: There is some confusion over whether the
2210 * set of adjustments that needs to be done should be done in an atomic
2211 * manner or not. That is, if we are attempting to decrement the semval
2212 * should we queue up and wait until we can do so legally?
2213 * The original implementation attempted to do this (queue and wait).
2214 * The current implementation does not do so. The POSIX standard
2215 * and SVID should be consulted to determine what behavior is mandated.
2217 void exit_sem(struct task_struct *tsk)
2219 struct sem_undo_list *ulp;
2221 ulp = tsk->sysvsem.undo_list;
2224 tsk->sysvsem.undo_list = NULL;
2226 if (!refcount_dec_and_test(&ulp->refcnt))
2230 struct sem_array *sma;
2231 struct sem_undo *un;
2233 DEFINE_WAKE_Q(wake_q);
2238 un = list_entry_rcu(ulp->list_proc.next,
2239 struct sem_undo, list_proc);
2240 if (&un->list_proc == &ulp->list_proc) {
2242 * We must wait for freeary() before freeing this ulp,
2243 * in case we raced with last sem_undo. There is a small
2244 * possibility where we exit while freeary() didn't
2245 * finish unlocking sem_undo_list.
2247 spin_lock(&ulp->lock);
2248 spin_unlock(&ulp->lock);
2252 spin_lock(&ulp->lock);
2254 spin_unlock(&ulp->lock);
2256 /* exit_sem raced with IPC_RMID, nothing to do */
2262 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, semid);
2263 /* exit_sem raced with IPC_RMID, nothing to do */
2269 sem_lock(sma, NULL, -1);
2270 /* exit_sem raced with IPC_RMID, nothing to do */
2271 if (!ipc_valid_object(&sma->sem_perm)) {
2272 sem_unlock(sma, -1);
2276 un = __lookup_undo(ulp, semid);
2278 /* exit_sem raced with IPC_RMID+semget() that created
2279 * exactly the same semid. Nothing to do.
2281 sem_unlock(sma, -1);
2286 /* remove un from the linked lists */
2287 ipc_assert_locked_object(&sma->sem_perm);
2288 list_del(&un->list_id);
2290 /* we are the last process using this ulp, acquiring ulp->lock
2291 * isn't required. Besides that, we are also protected against
2292 * IPC_RMID as we hold sma->sem_perm lock now
2294 list_del_rcu(&un->list_proc);
2296 /* perform adjustments registered in un */
2297 for (i = 0; i < sma->sem_nsems; i++) {
2298 struct sem *semaphore = &sma->sems[i];
2299 if (un->semadj[i]) {
2300 semaphore->semval += un->semadj[i];
2302 * Range checks of the new semaphore value,
2303 * not defined by sus:
2304 * - Some unices ignore the undo entirely
2305 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2306 * - some cap the value (e.g. FreeBSD caps
2307 * at 0, but doesn't enforce SEMVMX)
2309 * Linux caps the semaphore value, both at 0
2312 * Manfred <manfred@colorfullife.com>
2314 if (semaphore->semval < 0)
2315 semaphore->semval = 0;
2316 if (semaphore->semval > SEMVMX)
2317 semaphore->semval = SEMVMX;
2318 semaphore->sempid = task_tgid_vnr(current);
2321 /* maybe some queued-up processes were waiting for this */
2322 do_smart_update(sma, NULL, 0, 1, &wake_q);
2323 sem_unlock(sma, -1);
2332 #ifdef CONFIG_PROC_FS
2333 static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
2335 struct user_namespace *user_ns = seq_user_ns(s);
2336 struct kern_ipc_perm *ipcp = it;
2337 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
2341 * The proc interface isn't aware of sem_lock(), it calls
2342 * ipc_lock_object() directly (in sysvipc_find_ipc).
2343 * In order to stay compatible with sem_lock(), we must
2344 * enter / leave complex_mode.
2346 complexmode_enter(sma);
2348 sem_otime = get_semotime(sma);
2351 "%10d %10d %4o %10u %5u %5u %5u %5u %10llu %10llu\n",
2356 from_kuid_munged(user_ns, sma->sem_perm.uid),
2357 from_kgid_munged(user_ns, sma->sem_perm.gid),
2358 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2359 from_kgid_munged(user_ns, sma->sem_perm.cgid),
2363 complexmode_tryleave(sma);