blob: 6693daf4fe1124b3a69a345c42f30cf9767a739b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/ipc/sem.c
4 * Copyright (C) 1992 Krishna Balasubramanian
5 * Copyright (C) 1995 Eric Schenk, Bruno Haible
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
8 *
9 * SMP-threaded, sysctl's added
Christian Kujau624dffc2006-01-15 02:43:54 +010010 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Enforced range limit on SEM_UNDO
Alan Cox046c6882009-01-05 14:06:29 +000012 * (c) 2001 Red Hat Inc
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Lockless wakeup
14 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -080015 * (c) 2016 Davidlohr Bueso <dave@stgolabs.net>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070016 * Further wakeup optimizations, documentation
17 * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040018 *
19 * support for audit of ipc object properties and permission changes
20 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaeve3893532006-10-02 02:18:22 -070021 *
22 * namespaces support
23 * OpenVZ, SWsoft Inc.
24 * Pavel Emelianov <xemul@openvz.org>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070025 *
26 * Implementation notes: (May 2010)
27 * This file implements System V semaphores.
28 *
29 * User space visible behavior:
30 * - FIFO ordering for semop() operations (just FIFO, not starvation
31 * protection)
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
35 * SETALL calls.
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.
Bhaskar Chowdhuryb1989a32021-05-06 18:05:48 -070039 * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtime by writing
Manfred Spraulc5cf6352010-05-26 14:43:43 -070040 * to /proc/sys/kernel/sem.
41 * - statistics about the usage are reported in /proc/sysvipc/sem.
42 *
43 * Internals:
44 * - scalability:
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.
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -070052 * - semncnt and semzcnt are calculated on demand in count_semcnt()
Manfred Spraulc5cf6352010-05-26 14:43:43 -070053 * - 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
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -080058 * dropping all locks. (see wake_up_sem_queue_prepare())
Manfred Spraulc5cf6352010-05-26 14:43:43 -070059 * - 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).
Manfred Spraulc5cf6352010-05-26 14:43:43 -070063 * - 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72
Arnd Bergmannb0d17572018-04-13 13:58:23 +020073#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/slab.h>
75#include <linux/spinlock.h>
76#include <linux/init.h>
77#include <linux/proc_fs.h>
78#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/security.h>
80#include <linux/syscalls.h>
81#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080082#include <linux/capability.h>
Mike Waychison19b49462005-09-06 15:17:10 -070083#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070084#include <linux/rwsem.h>
Kirill Korotaeve3893532006-10-02 02:18:22 -070085#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080086#include <linux/ipc_namespace.h>
Ingo Molnar84f001e2017-02-01 16:36:40 +010087#include <linux/sched/wake_q.h>
Davidlohr Buesoec67aaa2018-06-14 15:27:51 -070088#include <linux/nospec.h>
NeilBrown0eb71a92018-06-18 12:52:50 +100089#include <linux/rhashtable.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080090
Paul McQuade7153e402014-06-06 14:37:37 -070091#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include "util.h"
93
Eric W. Biederman1a5c1342018-03-22 21:30:56 -050094/* One semaphore structure for each semaphore in the system. */
95struct sem {
96 int semval; /* current value */
97 /*
98 * PID of the process that last modified the semaphore. For
99 * Linux, specifically these are:
100 * - semop
101 * - semctl, via SETVAL and SETALL.
102 * - at task exit when performing undo adjustments (see exit_sem).
103 */
Eric W. Biederman51d6f262018-03-23 01:11:29 -0500104 struct pid *sempid;
Eric W. Biederman1a5c1342018-03-22 21:30:56 -0500105 spinlock_t lock; /* spinlock for fine-grained semtimedop */
106 struct list_head pending_alter; /* pending single-sop operations */
107 /* that alter the semaphore */
108 struct list_head pending_const; /* pending single-sop operations */
109 /* that do not alter the semaphore*/
Arnd Bergmann2a70b782018-04-12 15:19:44 +0200110 time64_t sem_otime; /* candidate for sem_otime */
Eric W. Biederman1a5c1342018-03-22 21:30:56 -0500111} ____cacheline_aligned_in_smp;
112
113/* One sem_array data structure for each set of semaphores in the system. */
114struct sem_array {
115 struct kern_ipc_perm sem_perm; /* permissions .. see ipc.h */
116 time64_t sem_ctime; /* create/last semctl() time */
117 struct list_head pending_alter; /* pending operations */
118 /* that alter the array */
119 struct list_head pending_const; /* pending complex operations */
120 /* that do not alter semvals */
121 struct list_head list_id; /* undo requests on this array */
122 int sem_nsems; /* no. of semaphores in array */
123 int complex_count; /* pending complex operations */
124 unsigned int use_global_lock;/* >0: global lock required */
125
126 struct sem sems[];
127} __randomize_layout;
Manfred Spraule57940d2011-11-02 13:38:54 -0700128
129/* One queue for each sleeping process in the system. */
130struct sem_queue {
Manfred Spraule57940d2011-11-02 13:38:54 -0700131 struct list_head list; /* queue of pending operations */
132 struct task_struct *sleeper; /* this process */
133 struct sem_undo *undo; /* undo structure */
Eric W. Biederman51d6f262018-03-23 01:11:29 -0500134 struct pid *pid; /* process id of requesting process */
Manfred Spraule57940d2011-11-02 13:38:54 -0700135 int status; /* completion status of operation */
136 struct sembuf *sops; /* array of pending operations */
Manfred Sprauled247b72014-06-06 14:37:49 -0700137 struct sembuf *blocking; /* the operation that blocked */
Manfred Spraule57940d2011-11-02 13:38:54 -0700138 int nsops; /* number of operations */
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800139 bool alter; /* does *sops alter the array? */
140 bool dupsop; /* sops on more than one sem_num */
Manfred Spraule57940d2011-11-02 13:38:54 -0700141};
142
143/* Each task has a list of undo requests. They are executed automatically
144 * when the process exits.
145 */
146struct sem_undo {
147 struct list_head list_proc; /* per-process list: *
148 * all undos from one process
149 * rcu protected */
150 struct rcu_head rcu; /* rcu struct for sem_undo */
151 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
152 struct list_head list_id; /* per semaphore array list:
153 * all undos for one array */
154 int semid; /* semaphore set identifier */
155 short *semadj; /* array of adjustments */
156 /* one per semaphore */
157};
158
159/* sem_undo_list controls shared access to the list of sem_undo structures
160 * that may be shared among all a CLONE_SYSVSEM task group.
161 */
162struct sem_undo_list {
Elena Reshetovaf74370b2017-09-08 16:17:42 -0700163 refcount_t refcnt;
Manfred Spraule57940d2011-11-02 13:38:54 -0700164 spinlock_t lock;
165 struct list_head list_proc;
166};
167
168
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800169#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700171static int newary(struct ipc_namespace *, struct ipc_params *);
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800172static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700174static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#endif
176
177#define SEMMSL_FAST 256 /* 512 bytes on stack */
178#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
179
180/*
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800181 * Switching from the mode suitable for simple ops
182 * to the mode for complex ops is costly. Therefore:
183 * use some hysteresis
184 */
185#define USE_GLOBAL_LOCK_HYSTERESIS 10
186
187/*
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700188 * Locking:
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700189 * a) global sem_lock() for read/write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * sem_undo.id_next,
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700191 * sem_array.complex_count,
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700192 * sem_array.pending{_alter,_const},
193 * sem_array.sem_undo
Paul McQuade46c0a8c2014-06-06 14:37:37 -0700194 *
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700195 * b) global or semaphore sem_lock() for read/write:
Manfred Spraul1a233952017-07-12 14:34:38 -0700196 * sem_array.sems[i].pending_{const,alter}:
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700197 *
198 * c) special:
199 * sem_undo_list.list_proc:
200 * * undo_list->lock for write
201 * * rcu for read
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800202 * use_global_lock:
203 * * global sem_lock() for write
204 * * either local or global sem_lock() for read.
205 *
206 * Memory ordering:
207 * Most ordering is enforced by using spin_lock() and spin_unlock().
Manfred Spraul8116b542020-02-03 17:34:42 -0800208 *
209 * Exceptions:
210 * 1) use_global_lock: (SEM_BARRIER_1)
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800211 * Setting it from non-zero to 0 is a RELEASE, this is ensured by
Manfred Spraul8116b542020-02-03 17:34:42 -0800212 * using smp_store_release(): Immediately after setting it to 0,
213 * a simple op can start.
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800214 * Testing if it is non-zero is an ACQUIRE, this is ensured by using
215 * smp_load_acquire().
216 * Setting it from 0 to non-zero must be ordered with regards to
217 * this smp_load_acquire(), this is guaranteed because the smp_load_acquire()
218 * is inside a spin_lock() and after a write from 0 to non-zero a
219 * spin_lock()+spin_unlock() is done.
Manfred Spraul17d056e2021-06-30 18:57:15 -0700220 * To prevent the compiler/cpu temporarily writing 0 to use_global_lock,
221 * READ_ONCE()/WRITE_ONCE() is used.
Manfred Spraul8116b542020-02-03 17:34:42 -0800222 *
223 * 2) queue.status: (SEM_BARRIER_2)
224 * Initialization is done while holding sem_lock(), so no further barrier is
225 * required.
226 * Setting it to a result code is a RELEASE, this is ensured by both a
227 * smp_store_release() (for case a) and while holding sem_lock()
228 * (for case b).
Bhaskar Chowdhuryb1989a32021-05-06 18:05:48 -0700229 * The ACQUIRE when reading the result code without holding sem_lock() is
Manfred Spraul8116b542020-02-03 17:34:42 -0800230 * achieved by using READ_ONCE() + smp_acquire__after_ctrl_dep().
231 * (case a above).
232 * Reading the result code while holding sem_lock() needs no further barriers,
233 * the locks inside sem_lock() enforce ordering (case b above)
234 *
235 * 3) current->state:
236 * current->state is set to TASK_INTERRUPTIBLE while holding sem_lock().
237 * The wakeup is handled using the wake_q infrastructure. wake_q wakeups may
238 * happen immediately after calling wake_q_add. As wake_q_add_safe() is called
239 * when holding sem_lock(), no further barriers are required.
240 *
241 * See also ipc/mqueue.c for more details on the covered races.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243
Kirill Korotaeve3893532006-10-02 02:18:22 -0700244#define sc_semmsl sem_ctls[0]
245#define sc_semmns sem_ctls[1]
246#define sc_semopm sem_ctls[2]
247#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Davidlohr Buesoeae04d22018-08-21 22:01:56 -0700249void sem_init_ns(struct ipc_namespace *ns)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700250{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700251 ns->sc_semmsl = SEMMSL;
252 ns->sc_semmns = SEMMNS;
253 ns->sc_semopm = SEMOPM;
254 ns->sc_semmni = SEMMNI;
255 ns->used_sems = 0;
Davidlohr Buesoeae04d22018-08-21 22:01:56 -0700256 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700257}
258
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800259#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700260void sem_exit_ns(struct ipc_namespace *ns)
261{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800262 free_ipcs(ns, &sem_ids(ns), freeary);
Serge E. Hallyn7d6feeb2009-12-15 16:47:27 -0800263 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -0700264 rhashtable_destroy(&ns->ids[IPC_SEM_IDS].key_ht);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700265}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Davidlohr Buesoeae04d22018-08-21 22:01:56 -0700268void __init sem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Davidlohr Buesoeae04d22018-08-21 22:01:56 -0700270 sem_init_ns(&init_ipc_ns);
Mike Waychison19b49462005-09-06 15:17:10 -0700271 ipc_init_proc_interface("sysvipc/sem",
272 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700273 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Manfred Spraulf269f402013-07-08 16:01:24 -0700276/**
277 * unmerge_queues - unmerge queues, if possible.
278 * @sma: semaphore array
279 *
280 * The function unmerges the wait queues if complex_count is 0.
281 * It must be called prior to dropping the global semaphore array lock.
282 */
283static void unmerge_queues(struct sem_array *sma)
284{
285 struct sem_queue *q, *tq;
286
287 /* complex operations still around? */
288 if (sma->complex_count)
289 return;
290 /*
291 * We will switch back to simple mode.
292 * Move all pending operation back into the per-semaphore
293 * queues.
294 */
295 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
296 struct sem *curr;
Manfred Spraul1a233952017-07-12 14:34:38 -0700297 curr = &sma->sems[q->sops[0].sem_num];
Manfred Spraulf269f402013-07-08 16:01:24 -0700298
299 list_add_tail(&q->list, &curr->pending_alter);
300 }
301 INIT_LIST_HEAD(&sma->pending_alter);
302}
303
304/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800305 * merge_queues - merge single semop queues into global queue
Manfred Spraulf269f402013-07-08 16:01:24 -0700306 * @sma: semaphore array
307 *
308 * This function merges all per-semaphore queues into the global queue.
309 * It is necessary to achieve FIFO ordering for the pending single-sop
310 * operations when a multi-semop operation must sleep.
311 * Only the alter operations must be moved, the const operations can stay.
312 */
313static void merge_queues(struct sem_array *sma)
314{
315 int i;
316 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700317 struct sem *sem = &sma->sems[i];
Manfred Spraulf269f402013-07-08 16:01:24 -0700318
319 list_splice_init(&sem->pending_alter, &sma->pending_alter);
320 }
321}
322
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700323static void sem_rcu_free(struct rcu_head *head)
324{
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700325 struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu);
326 struct sem_array *sma = container_of(p, struct sem_array, sem_perm);
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700327
Eric W. Biedermanaefad952018-03-22 20:52:43 -0500328 security_sem_free(&sma->sem_perm);
Kees Cooke2029df2017-07-12 14:35:31 -0700329 kvfree(sma);
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700330}
331
Nadia Derbey3e148c72007-10-18 23:40:54 -0700332/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700333 * Enter the mode suitable for non-simple operations:
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700334 * Caller must own sem_perm.lock.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700335 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700336static void complexmode_enter(struct sem_array *sma)
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700337{
338 int i;
339 struct sem *sem;
340
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800341 if (sma->use_global_lock > 0) {
342 /*
343 * We are already in global lock mode.
344 * Nothing to do, just reset the
345 * counter until we return to simple mode.
346 */
Manfred Spraul17d056e2021-06-30 18:57:15 -0700347 WRITE_ONCE(sma->use_global_lock, USE_GLOBAL_LOCK_HYSTERESIS);
Manfred Spraul6d07b682013-09-30 13:45:06 -0700348 return;
349 }
Manfred Spraul17d056e2021-06-30 18:57:15 -0700350 WRITE_ONCE(sma->use_global_lock, USE_GLOBAL_LOCK_HYSTERESIS);
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700351
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700352 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700353 sem = &sma->sems[i];
Manfred Spraul27d7be12017-02-27 14:28:15 -0800354 spin_lock(&sem->lock);
355 spin_unlock(&sem->lock);
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700356 }
357}
358
359/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700360 * Try to leave the mode that disallows simple operations:
361 * Caller must own sem_perm.lock.
362 */
363static void complexmode_tryleave(struct sem_array *sma)
364{
365 if (sma->complex_count) {
366 /* Complex ops are sleeping.
367 * We must stay in complex mode
368 */
369 return;
370 }
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800371 if (sma->use_global_lock == 1) {
Manfred Spraul8116b542020-02-03 17:34:42 -0800372
373 /* See SEM_BARRIER_1 for purpose/pairing */
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800374 smp_store_release(&sma->use_global_lock, 0);
375 } else {
Manfred Spraul17d056e2021-06-30 18:57:15 -0700376 WRITE_ONCE(sma->use_global_lock,
377 sma->use_global_lock-1);
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800378 }
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700379}
380
381#define SEM_GLOBAL_LOCK (-1)
382/*
Rik van Riel6062a8d2013-04-30 19:15:44 -0700383 * If the request contains only one semaphore operation, and there are
384 * no complex transactions pending, lock only the semaphore involved.
385 * Otherwise, lock the entire semaphore array, since we either have
386 * multiple semaphores in our own semops, or we need to look at
387 * semaphores from other pending complex operations.
Rik van Riel6062a8d2013-04-30 19:15:44 -0700388 */
389static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
390 int nsops)
391{
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700392 struct sem *sem;
Davidlohr Buesoec67aaa2018-06-14 15:27:51 -0700393 int idx;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700394
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700395 if (nsops != 1) {
396 /* Complex operation - acquire a full lock */
397 ipc_lock_object(&sma->sem_perm);
398
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700399 /* Prevent parallel simple ops */
400 complexmode_enter(sma);
401 return SEM_GLOBAL_LOCK;
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700402 }
403
404 /*
405 * Only one semaphore affected - try to optimize locking.
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700406 * Optimized locking is possible if no complex operation
407 * is either enqueued or processed right now.
408 *
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800409 * Both facts are tracked by use_global_mode.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700410 */
Davidlohr Buesoec67aaa2018-06-14 15:27:51 -0700411 idx = array_index_nospec(sops->sem_num, sma->sem_nsems);
412 sem = &sma->sems[idx];
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700413
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700414 /*
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800415 * Initial check for use_global_lock. Just an optimization,
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700416 * no locking, no memory barrier.
417 */
Manfred Spraul17d056e2021-06-30 18:57:15 -0700418 if (!READ_ONCE(sma->use_global_lock)) {
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700419 /*
420 * It appears that no complex operation is around.
421 * Acquire the per-semaphore lock.
422 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700423 spin_lock(&sem->lock);
424
Manfred Spraul8116b542020-02-03 17:34:42 -0800425 /* see SEM_BARRIER_1 for purpose/pairing */
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800426 if (!smp_load_acquire(&sma->use_global_lock)) {
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700427 /* fast path successful! */
428 return sops->sem_num;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700429 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700430 spin_unlock(&sem->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700431 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700432
433 /* slow path: acquire the full lock */
434 ipc_lock_object(&sma->sem_perm);
435
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800436 if (sma->use_global_lock == 0) {
437 /*
438 * The use_global_lock mode ended while we waited for
439 * sma->sem_perm.lock. Thus we must switch to locking
440 * with sem->lock.
441 * Unlike in the fast path, there is no need to recheck
442 * sma->use_global_lock after we have acquired sem->lock:
443 * We own sma->sem_perm.lock, thus use_global_lock cannot
444 * change.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700445 */
446 spin_lock(&sem->lock);
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800447
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700448 ipc_unlock_object(&sma->sem_perm);
449 return sops->sem_num;
450 } else {
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800451 /*
452 * Not a false alarm, thus continue to use the global lock
453 * mode. No need for complexmode_enter(), this was done by
454 * the caller that has set use_global_mode to non-zero.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700455 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700456 return SEM_GLOBAL_LOCK;
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700457 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700458}
459
460static inline void sem_unlock(struct sem_array *sma, int locknum)
461{
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700462 if (locknum == SEM_GLOBAL_LOCK) {
Manfred Spraulf269f402013-07-08 16:01:24 -0700463 unmerge_queues(sma);
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700464 complexmode_tryleave(sma);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700465 ipc_unlock_object(&sma->sem_perm);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700466 } else {
Manfred Spraul1a233952017-07-12 14:34:38 -0700467 struct sem *sem = &sma->sems[locknum];
Rik van Riel6062a8d2013-04-30 19:15:44 -0700468 spin_unlock(&sem->lock);
469 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700470}
471
472/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700473 * sem_lock_(check_) routines are called in the paths where the rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -0700474 * is not held.
Linus Torvalds321310c2013-05-04 10:47:57 -0700475 *
476 * The caller holds the RCU read lock.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700477 */
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700478static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
479{
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700480 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&sem_ids(ns), id);
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700481
482 if (IS_ERR(ipcp))
483 return ERR_CAST(ipcp);
484
485 return container_of(ipcp, struct sem_array, sem_perm);
486}
487
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700488static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
489 int id)
490{
491 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
492
493 if (IS_ERR(ipcp))
494 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800495
Nadia Derbey03f02c72007-10-18 23:40:51 -0700496 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700497}
498
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700499static inline void sem_lock_and_putref(struct sem_array *sma)
500{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700501 sem_lock(sma, NULL, -1);
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700502 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700503}
504
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700505static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
506{
507 ipc_rmid(&sem_ids(ns), &s->sem_perm);
508}
509
Kees Cook101ede02017-07-12 14:35:02 -0700510static struct sem_array *sem_alloc(size_t nsems)
511{
512 struct sem_array *sma;
Kees Cook101ede02017-07-12 14:35:02 -0700513
514 if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
515 return NULL;
516
Vasily Averin18319492021-09-02 14:55:31 -0700517 sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL_ACCOUNT);
Kees Cook101ede02017-07-12 14:35:02 -0700518 if (unlikely(!sma))
519 return NULL;
520
Kees Cook101ede02017-07-12 14:35:02 -0700521 return sma;
522}
523
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700524/**
525 * newary - Create a new semaphore set
526 * @ns: namespace
527 * @params: ptr to the structure that contains key, semflg and nsems
528 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700529 * Called with sem_ids.rwsem held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700530 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700531static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int retval;
534 struct sem_array *sma;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700535 key_t key = params->key;
536 int nsems = params->u.nsems;
537 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800538 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if (!nsems)
541 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700542 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -ENOSPC;
544
Kees Cook101ede02017-07-12 14:35:02 -0700545 sma = sem_alloc(nsems);
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800546 if (!sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return -ENOMEM;
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 sma->sem_perm.mode = (semflg & S_IRWXUGO);
550 sma->sem_perm.key = key;
551
552 sma->sem_perm.security = NULL;
Eric W. Biedermanaefad952018-03-22 20:52:43 -0500553 retval = security_sem_alloc(&sma->sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (retval) {
Kees Cooke2029df2017-07-12 14:35:31 -0700555 kvfree(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return retval;
557 }
558
Rik van Riel6062a8d2013-04-30 19:15:44 -0700559 for (i = 0; i < nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700560 INIT_LIST_HEAD(&sma->sems[i].pending_alter);
561 INIT_LIST_HEAD(&sma->sems[i].pending_const);
562 spin_lock_init(&sma->sems[i].lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700563 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800564
565 sma->complex_count = 0;
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800566 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700567 INIT_LIST_HEAD(&sma->pending_alter);
568 INIT_LIST_HEAD(&sma->pending_const);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700569 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 sma->sem_nsems = nsems;
Deepa Dinamanie54d02b2017-08-02 19:51:13 -0700571 sma->sem_ctime = ktime_get_real_seconds();
Manfred Spraule8577d12014-12-02 15:59:34 -0800572
Davidlohr Bueso39c96a12017-11-17 15:31:11 -0800573 /* ipc_addid() locks sma upon success. */
Manfred Spraul2ec55f82017-07-12 14:35:13 -0700574 retval = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
575 if (retval < 0) {
Manfred Spraul39cfffd2018-08-21 22:01:29 -0700576 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Manfred Spraul2ec55f82017-07-12 14:35:13 -0700577 return retval;
Manfred Spraule8577d12014-12-02 15:59:34 -0800578 }
579 ns->used_sems += nsems;
580
Rik van Riel6062a8d2013-04-30 19:15:44 -0700581 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -0700582 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700584 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700587
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700588/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700589 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700590 */
Alexey Dobriyan00898e82020-08-11 18:37:05 -0700591static int sem_more_checks(struct kern_ipc_perm *ipcp, struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700592{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700593 struct sem_array *sma;
594
595 sma = container_of(ipcp, struct sem_array, sem_perm);
596 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700597 return -EINVAL;
598
599 return 0;
600}
601
Dominik Brodowski69894712018-03-20 19:53:58 +0100602long ksys_semget(key_t key, int nsems, int semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700603{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700604 struct ipc_namespace *ns;
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700605 static const struct ipc_ops sem_ops = {
606 .getnew = newary,
Eric W. Biederman50ab44b2018-03-23 23:41:55 -0500607 .associate = security_sem_associate,
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700608 .more_checks = sem_more_checks,
609 };
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700610 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Kirill Korotaeve3893532006-10-02 02:18:22 -0700612 ns = current->nsproxy->ipc_ns;
613
614 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700616
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700617 sem_params.key = key;
618 sem_params.flg = semflg;
619 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700620
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700621 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Dominik Brodowski69894712018-03-20 19:53:58 +0100624SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
625{
626 return ksys_semget(key, nsems, semflg);
627}
628
Petr Mladek78f50092014-01-27 17:07:00 -0800629/**
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800630 * perform_atomic_semop[_slow] - Attempt to perform semaphore
631 * operations on a given array.
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700632 * @sma: semaphore array
Manfred Sprauld198cd62014-06-06 14:37:49 -0700633 * @q: struct sem_queue that describes the operation
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700634 *
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800635 * Caller blocking are as follows, based the value
636 * indicated by the semaphore operation (sem_op):
637 *
638 * (1) >0 never blocks.
639 * (2) 0 (wait-for-zero operation): semval is non-zero.
640 * (3) <0 attempting to decrement semval to a value smaller than zero.
641 *
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700642 * Returns 0 if the operation was possible.
643 * Returns 1 if the operation is impossible, the caller must sleep.
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800644 * Returns <0 for error codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800646static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Eric W. Biederman51d6f262018-03-23 01:11:29 -0500648 int result, sem_op, nsops;
649 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct sembuf *sop;
Manfred Spraul239521f2014-01-27 17:07:04 -0800651 struct sem *curr;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700652 struct sembuf *sops;
653 struct sem_undo *un;
654
655 sops = q->sops;
656 nsops = q->nsops;
657 un = q->undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 for (sop = sops; sop < sops + nsops; sop++) {
Davidlohr Buesoec67aaa2018-06-14 15:27:51 -0700660 int idx = array_index_nospec(sop->sem_num, sma->sem_nsems);
661 curr = &sma->sems[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 sem_op = sop->sem_op;
663 result = curr->semval;
Petr Mladek78f50092014-01-27 17:07:00 -0800664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (!sem_op && result)
666 goto would_block;
667
668 result += sem_op;
669 if (result < 0)
670 goto would_block;
671 if (result > SEMVMX)
672 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (sop->sem_flg & SEM_UNDO) {
675 int undo = un->semadj[sop->sem_num] - sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800676 /* Exceeding the undo range is an error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
678 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800679 un->semadj[sop->sem_num] = undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Petr Mladek78f50092014-01-27 17:07:00 -0800681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 curr->semval = result;
683 }
684
685 sop--;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700686 pid = q->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 while (sop >= sops) {
Eric W. Biederman51d6f262018-03-23 01:11:29 -0500688 ipc_update_pid(&sma->sems[sop->sem_num].sempid, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 sop--;
690 }
Petr Mladek78f50092014-01-27 17:07:00 -0800691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
693
694out_of_range:
695 result = -ERANGE;
696 goto undo;
697
698would_block:
Manfred Sprauled247b72014-06-06 14:37:49 -0700699 q->blocking = sop;
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (sop->sem_flg & IPC_NOWAIT)
702 result = -EAGAIN;
703 else
704 result = 1;
705
706undo:
707 sop--;
708 while (sop >= sops) {
Petr Mladek78f50092014-01-27 17:07:00 -0800709 sem_op = sop->sem_op;
Manfred Spraul1a233952017-07-12 14:34:38 -0700710 sma->sems[sop->sem_num].semval -= sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800711 if (sop->sem_flg & SEM_UNDO)
712 un->semadj[sop->sem_num] += sem_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 sop--;
714 }
715
716 return result;
717}
718
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800719static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
720{
721 int result, sem_op, nsops;
722 struct sembuf *sop;
723 struct sem *curr;
724 struct sembuf *sops;
725 struct sem_undo *un;
726
727 sops = q->sops;
728 nsops = q->nsops;
729 un = q->undo;
730
731 if (unlikely(q->dupsop))
732 return perform_atomic_semop_slow(sma, q);
733
734 /*
735 * We scan the semaphore set twice, first to ensure that the entire
736 * operation can succeed, therefore avoiding any pointless writes
737 * to shared memory and having to undo such changes in order to block
738 * until the operations can go through.
739 */
740 for (sop = sops; sop < sops + nsops; sop++) {
Davidlohr Buesoec67aaa2018-06-14 15:27:51 -0700741 int idx = array_index_nospec(sop->sem_num, sma->sem_nsems);
742
743 curr = &sma->sems[idx];
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800744 sem_op = sop->sem_op;
745 result = curr->semval;
746
747 if (!sem_op && result)
748 goto would_block; /* wait-for-zero */
749
750 result += sem_op;
751 if (result < 0)
752 goto would_block;
753
754 if (result > SEMVMX)
755 return -ERANGE;
756
757 if (sop->sem_flg & SEM_UNDO) {
758 int undo = un->semadj[sop->sem_num] - sem_op;
759
760 /* Exceeding the undo range is an error. */
761 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
762 return -ERANGE;
763 }
764 }
765
766 for (sop = sops; sop < sops + nsops; sop++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700767 curr = &sma->sems[sop->sem_num];
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800768 sem_op = sop->sem_op;
769 result = curr->semval;
770
771 if (sop->sem_flg & SEM_UNDO) {
772 int undo = un->semadj[sop->sem_num] - sem_op;
773
774 un->semadj[sop->sem_num] = undo;
775 }
776 curr->semval += sem_op;
Eric W. Biederman51d6f262018-03-23 01:11:29 -0500777 ipc_update_pid(&curr->sempid, q->pid);
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800778 }
779
780 return 0;
781
782would_block:
783 q->blocking = sop;
784 return sop->sem_flg & IPC_NOWAIT ? -EAGAIN : 1;
785}
786
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800787static inline void wake_up_sem_queue_prepare(struct sem_queue *q, int error,
788 struct wake_q_head *wake_q)
Nick Piggind4212092009-12-15 16:47:30 -0800789{
Varad Gautama11ddb32021-05-22 17:41:49 -0700790 struct task_struct *sleeper;
791
792 sleeper = get_task_struct(q->sleeper);
Manfred Spraul8116b542020-02-03 17:34:42 -0800793
Bhaskar Chowdhury74978352021-05-06 18:06:41 -0700794 /* see SEM_BARRIER_2 for purpose/pairing */
Manfred Spraul8116b542020-02-03 17:34:42 -0800795 smp_store_release(&q->status, error);
796
Varad Gautama11ddb32021-05-22 17:41:49 -0700797 wake_q_add_safe(wake_q, sleeper);
Nick Piggind4212092009-12-15 16:47:30 -0800798}
799
Manfred Spraulb97e8202009-12-15 16:47:32 -0800800static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
801{
802 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700803 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800804 sma->complex_count--;
805}
806
Manfred Spraulfd5db422010-05-26 14:43:40 -0700807/** check_restart(sma, q)
808 * @sma: semaphore array
809 * @q: the operation that just completed
810 *
811 * update_queue is O(N^2) when it restarts scanning the whole queue of
812 * waiting operations. Therefore this function checks if the restart is
813 * really necessary. It is called after a previously waiting operation
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700814 * modified the array.
815 * Note that wait-for-zero operations are handled without restart.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700816 */
Davidlohr Bueso4663d3e2016-12-14 15:06:40 -0800817static inline int check_restart(struct sem_array *sma, struct sem_queue *q)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700818{
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700819 /* pending complex alter operations are too difficult to analyse */
820 if (!list_empty(&sma->pending_alter))
Manfred Spraulfd5db422010-05-26 14:43:40 -0700821 return 1;
822
823 /* we were a sleeping complex operation. Too difficult */
824 if (q->nsops > 1)
825 return 1;
826
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700827 /* It is impossible that someone waits for the new value:
828 * - complex operations always restart.
Bhaskar Chowdhuryb1989a32021-05-06 18:05:48 -0700829 * - wait-for-zero are handled separately.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700830 * - q is a previously sleeping simple operation that
831 * altered the array. It must be a decrement, because
832 * simple increments never sleep.
833 * - If there are older (higher priority) decrements
834 * in the queue, then they have observed the original
835 * semval value and couldn't proceed. The operation
836 * decremented to value - thus they won't proceed either.
837 */
838 return 0;
839}
Manfred Spraulfd5db422010-05-26 14:43:40 -0700840
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700841/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800842 * wake_const_ops - wake up non-alter tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700843 * @sma: semaphore array.
844 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800845 * @wake_q: lockless wake-queue head.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700846 *
847 * wake_const_ops must be called after a semaphore in a semaphore array
848 * was set to 0. If complex const operations are pending, wake_const_ops must
849 * be called with semnum = -1, as well as with the number of each modified
850 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800851 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700852 * is stored in q->pid.
853 * The function returns 1 if at least one operation was completed successfully.
854 */
855static int wake_const_ops(struct sem_array *sma, int semnum,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800856 struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700857{
Davidlohr Buesof150f022016-12-14 15:06:43 -0800858 struct sem_queue *q, *tmp;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700859 struct list_head *pending_list;
860 int semop_completed = 0;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700861
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700862 if (semnum == -1)
863 pending_list = &sma->pending_const;
864 else
Manfred Spraul1a233952017-07-12 14:34:38 -0700865 pending_list = &sma->sems[semnum].pending_const;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700866
Davidlohr Buesof150f022016-12-14 15:06:43 -0800867 list_for_each_entry_safe(q, tmp, pending_list, list) {
868 int error = perform_atomic_semop(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700869
Davidlohr Buesof150f022016-12-14 15:06:43 -0800870 if (error > 0)
871 continue;
872 /* operation completed, remove from queue & wakeup */
873 unlink_queue(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700874
Davidlohr Buesof150f022016-12-14 15:06:43 -0800875 wake_up_sem_queue_prepare(q, error, wake_q);
876 if (error == 0)
877 semop_completed = 1;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700878 }
Davidlohr Buesof150f022016-12-14 15:06:43 -0800879
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700880 return semop_completed;
881}
882
883/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800884 * do_smart_wakeup_zero - wakeup all wait for zero tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700885 * @sma: semaphore array
886 * @sops: operations that were performed
887 * @nsops: number of operations
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800888 * @wake_q: lockless wake-queue head
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700889 *
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800890 * Checks all required queue for wait-for-zero operations, based
891 * on the actual changes that were performed on the semaphore array.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700892 * The function returns 1 if at least one operation was completed successfully.
893 */
894static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800895 int nsops, struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700896{
897 int i;
898 int semop_completed = 0;
899 int got_zero = 0;
900
901 /* first: the per-semaphore queues, if known */
902 if (sops) {
903 for (i = 0; i < nsops; i++) {
904 int num = sops[i].sem_num;
905
Manfred Spraul1a233952017-07-12 14:34:38 -0700906 if (sma->sems[num].semval == 0) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700907 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800908 semop_completed |= wake_const_ops(sma, num, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700909 }
910 }
911 } else {
912 /*
913 * No sops means modified semaphores not known.
914 * Assume all were changed.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700915 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700916 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700917 if (sma->sems[i].semval == 0) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700918 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800919 semop_completed |= wake_const_ops(sma, i, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700920 }
921 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700922 }
923 /*
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700924 * If one of the modified semaphores got 0,
925 * then check the global queue, too.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700926 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700927 if (got_zero)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800928 semop_completed |= wake_const_ops(sma, -1, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700929
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700930 return semop_completed;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700931}
932
Manfred Spraul636c6be2009-12-15 16:47:33 -0800933
934/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800935 * update_queue - look for tasks that can be completed.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800936 * @sma: semaphore array.
937 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800938 * @wake_q: lockless wake-queue head.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800939 *
940 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700941 * was modified. If multiple semaphores were modified, update_queue must
942 * be called with semnum = -1, as well as with the number of each modified
943 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800944 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700945 * is stored in q->pid.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700946 * The function internally checks if const operations can now succeed.
947 *
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700948 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800950static int update_queue(struct sem_array *sma, int semnum, struct wake_q_head *wake_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Davidlohr Buesof150f022016-12-14 15:06:43 -0800952 struct sem_queue *q, *tmp;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800953 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700954 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800955
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700956 if (semnum == -1)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700957 pending_list = &sma->pending_alter;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700958 else
Manfred Spraul1a233952017-07-12 14:34:38 -0700959 pending_list = &sma->sems[semnum].pending_alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Nick Piggin9cad2002009-12-15 16:47:29 -0800961again:
Davidlohr Buesof150f022016-12-14 15:06:43 -0800962 list_for_each_entry_safe(q, tmp, pending_list, list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700963 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800964
Manfred Sprauld987f8b22009-12-15 16:47:34 -0800965 /* If we are scanning the single sop, per-semaphore list of
966 * one semaphore and that semaphore is 0, then it is not
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700967 * necessary to scan further: simple increments
Manfred Sprauld987f8b22009-12-15 16:47:34 -0800968 * that affect only one entry succeed immediately and cannot
969 * be in the per semaphore pending queue, and decrements
970 * cannot be successful if the value is already 0.
971 */
Manfred Spraul1a233952017-07-12 14:34:38 -0700972 if (semnum != -1 && sma->sems[semnum].semval == 0)
Manfred Sprauld987f8b22009-12-15 16:47:34 -0800973 break;
974
Manfred Sprauld198cd62014-06-06 14:37:49 -0700975 error = perform_atomic_semop(sma, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800978 if (error > 0)
979 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700980
Manfred Spraulb97e8202009-12-15 16:47:32 -0800981 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700982
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700983 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700984 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700985 } else {
986 semop_completed = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800987 do_smart_wakeup_zero(sma, q->sops, q->nsops, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700988 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700989 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700990
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800991 wake_up_sem_queue_prepare(q, error, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700992 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800993 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700995 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700998/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800999 * set_semotime - set sem_otime
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001000 * @sma: semaphore array
1001 * @sops: operations that modified the array, may be NULL
1002 *
1003 * sem_otime is replicated to avoid cache line trashing.
1004 * This function sets one instance to the current time.
1005 */
1006static void set_semotime(struct sem_array *sma, struct sembuf *sops)
1007{
1008 if (sops == NULL) {
Arnd Bergmann2a70b782018-04-12 15:19:44 +02001009 sma->sems[0].sem_otime = ktime_get_real_seconds();
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001010 } else {
Manfred Spraul1a233952017-07-12 14:34:38 -07001011 sma->sems[sops[0].sem_num].sem_otime =
Arnd Bergmann2a70b782018-04-12 15:19:44 +02001012 ktime_get_real_seconds();
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001013 }
1014}
1015
1016/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001017 * do_smart_update - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -07001018 * @sma: semaphore array
1019 * @sops: operations that were performed
1020 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001021 * @otime: force setting otime
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001022 * @wake_q: lockless wake-queue head
Manfred Spraulfd5db422010-05-26 14:43:40 -07001023 *
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001024 * do_smart_update() does the required calls to update_queue and wakeup_zero,
1025 * based on the actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001026 * Note that the function does not do the actual wake-up: the caller is
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001027 * responsible for calling wake_up_q().
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001028 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -07001029 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001030static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001031 int otime, struct wake_q_head *wake_q)
Manfred Spraulfd5db422010-05-26 14:43:40 -07001032{
1033 int i;
1034
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001035 otime |= do_smart_wakeup_zero(sma, sops, nsops, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001036
Manfred Spraulf269f402013-07-08 16:01:24 -07001037 if (!list_empty(&sma->pending_alter)) {
1038 /* semaphore array uses the global queue - just process it. */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001039 otime |= update_queue(sma, -1, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -07001040 } else {
1041 if (!sops) {
1042 /*
1043 * No sops, thus the modified semaphores are not
1044 * known. Check all.
1045 */
1046 for (i = 0; i < sma->sem_nsems; i++)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001047 otime |= update_queue(sma, i, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -07001048 } else {
1049 /*
1050 * Check the semaphores that were increased:
1051 * - No complex ops, thus all sleeping ops are
1052 * decrease.
1053 * - if we decreased the value, then any sleeping
Bhaskar Chowdhuryb1989a32021-05-06 18:05:48 -07001054 * semaphore ops won't be able to run: If the
Manfred Spraulf269f402013-07-08 16:01:24 -07001055 * previous value was too small, then the new
1056 * value will be too small, too.
1057 */
1058 for (i = 0; i < nsops; i++) {
1059 if (sops[i].sem_op > 0) {
1060 otime |= update_queue(sma,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001061 sops[i].sem_num, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -07001062 }
Manfred Spraulab465df2013-05-26 11:08:52 +02001063 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001064 }
Manfred Spraulfd5db422010-05-26 14:43:40 -07001065 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001066 if (otime)
1067 set_semotime(sma, sops);
Manfred Spraulfd5db422010-05-26 14:43:40 -07001068}
1069
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001070/*
Manfred Spraulb220c572014-06-06 14:37:51 -07001071 * check_qop: Test if a queued operation sleeps on the semaphore semnum
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001072 */
1073static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
1074 bool count_zero)
1075{
Manfred Spraulb220c572014-06-06 14:37:51 -07001076 struct sembuf *sop = q->blocking;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001077
Manfred Spraul9b44ee22014-06-06 14:37:52 -07001078 /*
1079 * Linux always (since 0.99.10) reported a task as sleeping on all
1080 * semaphores. This violates SUS, therefore it was changed to the
1081 * standard compliant behavior.
1082 * Give the administrators a chance to notice that an application
1083 * might misbehave because it relies on the Linux behavior.
1084 */
1085 pr_info_once("semctl(GETNCNT/GETZCNT) is since 3.16 Single Unix Specification compliant.\n"
1086 "The task %s (%d) triggered the difference, watch for misbehavior.\n",
1087 current->comm, task_pid_nr(current));
1088
Manfred Spraulb220c572014-06-06 14:37:51 -07001089 if (sop->sem_num != semnum)
1090 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001091
Manfred Spraulb220c572014-06-06 14:37:51 -07001092 if (count_zero && sop->sem_op == 0)
1093 return 1;
1094 if (!count_zero && sop->sem_op < 0)
1095 return 1;
1096
1097 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001098}
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100/* The following counts are associated to each semaphore:
1101 * semncnt number of tasks waiting on semval being nonzero
1102 * semzcnt number of tasks waiting on semval being zero
Manfred Spraulb220c572014-06-06 14:37:51 -07001103 *
1104 * Per definition, a task waits only on the semaphore of the first semop
1105 * that cannot proceed, even if additional operation would block, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 */
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001107static int count_semcnt(struct sem_array *sma, ushort semnum,
1108 bool count_zero)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001110 struct list_head *l;
Manfred Spraul239521f2014-01-27 17:07:04 -08001111 struct sem_queue *q;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001112 int semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001114 semcnt = 0;
1115 /* First: check the simple operations. They are easy to evaluate */
1116 if (count_zero)
Manfred Spraul1a233952017-07-12 14:34:38 -07001117 l = &sma->sems[semnum].pending_const;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001118 else
Manfred Spraul1a233952017-07-12 14:34:38 -07001119 l = &sma->sems[semnum].pending_alter;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001120
1121 list_for_each_entry(q, l, list) {
1122 /* all task on a per-semaphore list sleep on exactly
1123 * that semaphore
1124 */
1125 semcnt++;
Rik van Rielde2657f2013-05-09 16:59:59 -04001126 }
1127
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001128 /* Then: check the complex operations. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001129 list_for_each_entry(q, &sma->pending_alter, list) {
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001130 semcnt += check_qop(sma, semnum, q, count_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001132 if (count_zero) {
1133 list_for_each_entry(q, &sma->pending_const, list) {
1134 semcnt += check_qop(sma, semnum, q, count_zero);
1135 }
Rik van Rielebc2e5e2013-05-09 16:53:28 -04001136 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001137 return semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001140/* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1141 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -07001142 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001144static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Manfred Spraul380af1b2008-07-25 01:48:06 -07001146 struct sem_undo *un, *tu;
1147 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001148 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001149 int i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001150 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Manfred Spraul380af1b2008-07-25 01:48:06 -07001152 /* Free the existing undo structures for this semaphore set. */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001153 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001154 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1155 list_del(&un->list_id);
1156 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001158 list_del_rcu(&un->list_proc);
1159 spin_unlock(&un->ulp->lock);
Vasily Averinfc37a3b2021-06-30 18:57:09 -07001160 kvfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001164 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1165 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001166 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001167 }
1168
1169 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -08001170 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001171 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001173 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07001174 struct sem *sem = &sma->sems[i];
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001175 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1176 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001177 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001178 }
1179 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001180 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001181 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001182 }
Eric W. Biederman51d6f262018-03-23 01:11:29 -05001183 ipc_update_pid(&sem->sempid, NULL);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001186 /* Remove the semaphore set from the IDR */
1187 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001188 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001189 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001191 wake_up_q(&wake_q);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001192 ns->used_sems -= sma->sem_nsems;
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001193 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1197{
Manfred Spraul239521f2014-01-27 17:07:04 -08001198 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 case IPC_64:
1200 return copy_to_user(buf, in, sizeof(*in));
1201 case IPC_OLD:
1202 {
1203 struct semid_ds out;
1204
Dan Rosenberg982f7c22010-09-30 15:15:31 -07001205 memset(&out, 0, sizeof(out));
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1208
1209 out.sem_otime = in->sem_otime;
1210 out.sem_ctime = in->sem_ctime;
1211 out.sem_nsems = in->sem_nsems;
1212
1213 return copy_to_user(buf, &out, sizeof(out));
1214 }
1215 default:
1216 return -EINVAL;
1217 }
1218}
1219
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07001220static time64_t get_semotime(struct sem_array *sma)
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001221{
1222 int i;
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07001223 time64_t res;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001224
Manfred Spraul1a233952017-07-12 14:34:38 -07001225 res = sma->sems[0].sem_otime;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001226 for (i = 1; i < sma->sem_nsems; i++) {
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07001227 time64_t to = sma->sems[i].sem_otime;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001228
1229 if (to > res)
1230 res = to;
1231 }
1232 return res;
1233}
1234
Al Viro45a4a642017-07-09 09:11:00 -04001235static int semctl_stat(struct ipc_namespace *ns, int semid,
1236 int cmd, struct semid64_ds *semid64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 struct sem_array *sma;
Arnd Bergmannc2ab9752015-04-28 21:39:50 +02001239 time64_t semotime;
Al Viro45a4a642017-07-09 09:11:00 -04001240 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Al Viro45a4a642017-07-09 09:11:00 -04001242 memset(semid64, 0, sizeof(*semid64));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Al Viro45a4a642017-07-09 09:11:00 -04001244 rcu_read_lock();
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07001245 if (cmd == SEM_STAT || cmd == SEM_STAT_ANY) {
Al Viro45a4a642017-07-09 09:11:00 -04001246 sma = sem_obtain_object(ns, semid);
1247 if (IS_ERR(sma)) {
1248 err = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 goto out_unlock;
Al Viro45a4a642017-07-09 09:11:00 -04001250 }
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07001251 } else { /* IPC_STAT */
Al Viro45a4a642017-07-09 09:11:00 -04001252 sma = sem_obtain_object_check(ns, semid);
1253 if (IS_ERR(sma)) {
1254 err = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 goto out_unlock;
Al Viro45a4a642017-07-09 09:11:00 -04001256 }
1257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07001259 /* see comment for SHM_STAT_ANY */
1260 if (cmd == SEM_STAT_ANY)
1261 audit_ipc_obj(&sma->sem_perm);
1262 else {
1263 err = -EACCES;
1264 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
1265 goto out_unlock;
1266 }
Al Viro45a4a642017-07-09 09:11:00 -04001267
Eric W. Biedermanaefad952018-03-22 20:52:43 -05001268 err = security_sem_semctl(&sma->sem_perm, cmd);
Al Viro45a4a642017-07-09 09:11:00 -04001269 if (err)
1270 goto out_unlock;
1271
Philippe Mikoyan87ad4b02018-02-06 15:40:49 -08001272 ipc_lock_object(&sma->sem_perm);
1273
1274 if (!ipc_valid_object(&sma->sem_perm)) {
1275 ipc_unlock_object(&sma->sem_perm);
1276 err = -EIDRM;
1277 goto out_unlock;
1278 }
1279
Al Viro45a4a642017-07-09 09:11:00 -04001280 kernel_to_ipc64_perm(&sma->sem_perm, &semid64->sem_perm);
Arnd Bergmannc2ab9752015-04-28 21:39:50 +02001281 semotime = get_semotime(sma);
1282 semid64->sem_otime = semotime;
Al Viro45a4a642017-07-09 09:11:00 -04001283 semid64->sem_ctime = sma->sem_ctime;
Arnd Bergmannc2ab9752015-04-28 21:39:50 +02001284#ifndef CONFIG_64BIT
1285 semid64->sem_otime_high = semotime >> 32;
1286 semid64->sem_ctime_high = sma->sem_ctime >> 32;
1287#endif
Al Viro45a4a642017-07-09 09:11:00 -04001288 semid64->sem_nsems = sma->sem_nsems;
Philippe Mikoyan87ad4b02018-02-06 15:40:49 -08001289
Manfred Spraul615c9992018-08-21 22:01:21 -07001290 if (cmd == IPC_STAT) {
1291 /*
1292 * As defined in SUS:
1293 * Return 0 on success
1294 */
1295 err = 0;
1296 } else {
1297 /*
1298 * SEM_STAT and SEM_STAT_ANY (both Linux specific)
1299 * Return the full id, including the sequence number
1300 */
1301 err = sma->sem_perm.id;
1302 }
Philippe Mikoyan87ad4b02018-02-06 15:40:49 -08001303 ipc_unlock_object(&sma->sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001305 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return err;
1307}
1308
Al Viro45a4a642017-07-09 09:11:00 -04001309static int semctl_info(struct ipc_namespace *ns, int semid,
1310 int cmd, void __user *p)
1311{
1312 struct seminfo seminfo;
Manfred Spraul27c331a2018-08-21 22:02:00 -07001313 int max_idx;
Al Viro45a4a642017-07-09 09:11:00 -04001314 int err;
1315
1316 err = security_sem_semctl(NULL, cmd);
1317 if (err)
1318 return err;
1319
1320 memset(&seminfo, 0, sizeof(seminfo));
1321 seminfo.semmni = ns->sc_semmni;
1322 seminfo.semmns = ns->sc_semmns;
1323 seminfo.semmsl = ns->sc_semmsl;
1324 seminfo.semopm = ns->sc_semopm;
1325 seminfo.semvmx = SEMVMX;
1326 seminfo.semmnu = SEMMNU;
1327 seminfo.semmap = SEMMAP;
1328 seminfo.semume = SEMUME;
1329 down_read(&sem_ids(ns).rwsem);
1330 if (cmd == SEM_INFO) {
1331 seminfo.semusz = sem_ids(ns).in_use;
1332 seminfo.semaem = ns->used_sems;
1333 } else {
1334 seminfo.semusz = SEMUSZ;
1335 seminfo.semaem = SEMAEM;
1336 }
Manfred Spraul27c331a2018-08-21 22:02:00 -07001337 max_idx = ipc_get_maxidx(&sem_ids(ns));
Al Viro45a4a642017-07-09 09:11:00 -04001338 up_read(&sem_ids(ns).rwsem);
1339 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
1340 return -EFAULT;
Manfred Spraul27c331a2018-08-21 22:02:00 -07001341 return (max_idx < 0) ? 0 : max_idx;
Al Viro45a4a642017-07-09 09:11:00 -04001342}
1343
Al Viroe1fd1f42013-03-05 15:04:55 -05001344static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
Al Viro45a4a642017-07-09 09:11:00 -04001345 int val)
Al Viroe1fd1f42013-03-05 15:04:55 -05001346{
1347 struct sem_undo *un;
1348 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001349 struct sem *curr;
Al Viro45a4a642017-07-09 09:11:00 -04001350 int err;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001351 DEFINE_WAKE_Q(wake_q);
1352
Rik van Riel6062a8d2013-04-30 19:15:44 -07001353 if (val > SEMVMX || val < 0)
1354 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001355
Rik van Riel6062a8d2013-04-30 19:15:44 -07001356 rcu_read_lock();
1357 sma = sem_obtain_object_check(ns, semid);
1358 if (IS_ERR(sma)) {
1359 rcu_read_unlock();
1360 return PTR_ERR(sma);
1361 }
1362
1363 if (semnum < 0 || semnum >= sma->sem_nsems) {
1364 rcu_read_unlock();
1365 return -EINVAL;
1366 }
1367
1368
1369 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1370 rcu_read_unlock();
1371 return -EACCES;
1372 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001373
Eric W. Biedermanaefad952018-03-22 20:52:43 -05001374 err = security_sem_semctl(&sma->sem_perm, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001375 if (err) {
1376 rcu_read_unlock();
1377 return -EACCES;
1378 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001379
Rik van Riel6062a8d2013-04-30 19:15:44 -07001380 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001381
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001382 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001383 sem_unlock(sma, -1);
1384 rcu_read_unlock();
1385 return -EIDRM;
1386 }
1387
Davidlohr Buesoec67aaa2018-06-14 15:27:51 -07001388 semnum = array_index_nospec(semnum, sma->sem_nsems);
Manfred Spraul1a233952017-07-12 14:34:38 -07001389 curr = &sma->sems[semnum];
Al Viroe1fd1f42013-03-05 15:04:55 -05001390
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001391 ipc_assert_locked_object(&sma->sem_perm);
Al Viroe1fd1f42013-03-05 15:04:55 -05001392 list_for_each_entry(un, &sma->list_id, list_id)
1393 un->semadj[semnum] = 0;
1394
1395 curr->semval = val;
Eric W. Biederman51d6f262018-03-23 01:11:29 -05001396 ipc_update_pid(&curr->sempid, task_tgid(current));
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07001397 sma->sem_ctime = ktime_get_real_seconds();
Al Viroe1fd1f42013-03-05 15:04:55 -05001398 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001399 do_smart_update(sma, NULL, 0, 0, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001400 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001401 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001402 wake_up_q(&wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001403 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001404}
1405
Kirill Korotaeve3893532006-10-02 02:18:22 -07001406static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001407 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001410 struct sem *curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001411 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 ushort fast_sem_io[SEMMSL_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001413 ushort *sem_io = fast_sem_io;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001414 DEFINE_WAKE_Q(wake_q);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001415
1416 rcu_read_lock();
1417 sma = sem_obtain_object_check(ns, semid);
1418 if (IS_ERR(sma)) {
1419 rcu_read_unlock();
1420 return PTR_ERR(sma);
1421 }
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 nsems = sma->sem_nsems;
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 err = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001426 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1427 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Eric W. Biedermanaefad952018-03-22 20:52:43 -05001429 err = security_sem_semctl(&sma->sem_perm, cmd);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001430 if (err)
1431 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 err = -EACCES;
1434 switch (cmd) {
1435 case GETALL:
1436 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001437 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 int i;
1439
Al Viroce857222013-05-03 00:30:49 +01001440 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001441 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001442 err = -EIDRM;
1443 goto out_unlock;
1444 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001445 if (nsems > SEMMSL_FAST) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001446 if (!ipc_rcu_getref(&sma->sem_perm)) {
Al Viroce857222013-05-03 00:30:49 +01001447 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001448 goto out_unlock;
Al Viroce857222013-05-03 00:30:49 +01001449 }
1450 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001451 rcu_read_unlock();
Kees Cookf8dbe8d2017-07-12 14:34:47 -07001452 sem_io = kvmalloc_array(nsems, sizeof(ushort),
1453 GFP_KERNEL);
Manfred Spraul239521f2014-01-27 17:07:04 -08001454 if (sem_io == NULL) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001455 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return -ENOMEM;
1457 }
1458
Linus Torvalds4091fd92013-05-04 10:13:40 -07001459 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001460 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001461 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001463 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Al Viroce857222013-05-03 00:30:49 +01001465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 for (i = 0; i < sma->sem_nsems; i++)
Manfred Spraul1a233952017-07-12 14:34:38 -07001467 sem_io[i] = sma->sems[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001468 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001469 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 err = 0;
Manfred Spraul239521f2014-01-27 17:07:04 -08001471 if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 err = -EFAULT;
1473 goto out_free;
1474 }
1475 case SETALL:
1476 {
1477 int i;
1478 struct sem_undo *un;
1479
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001480 if (!ipc_rcu_getref(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001481 err = -EIDRM;
1482 goto out_rcu_wakeup;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001483 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001484 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Manfred Spraul239521f2014-01-27 17:07:04 -08001486 if (nsems > SEMMSL_FAST) {
Kees Cookf8dbe8d2017-07-12 14:34:47 -07001487 sem_io = kvmalloc_array(nsems, sizeof(ushort),
1488 GFP_KERNEL);
Manfred Spraul239521f2014-01-27 17:07:04 -08001489 if (sem_io == NULL) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001490 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return -ENOMEM;
1492 }
1493 }
1494
Manfred Spraul239521f2014-01-27 17:07:04 -08001495 if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001496 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 err = -EFAULT;
1498 goto out_free;
1499 }
1500
1501 for (i = 0; i < nsems; i++) {
1502 if (sem_io[i] > SEMVMX) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001503 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 err = -ERANGE;
1505 goto out_free;
1506 }
1507 }
Linus Torvalds4091fd92013-05-04 10:13:40 -07001508 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001509 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001510 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001512 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001515 for (i = 0; i < nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07001516 sma->sems[i].semval = sem_io[i];
Eric W. Biederman51d6f262018-03-23 01:11:29 -05001517 ipc_update_pid(&sma->sems[i].sempid, task_tgid(current));
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001518 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001519
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001520 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001521 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 for (i = 0; i < nsems; i++)
1523 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001524 }
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07001525 sma->sem_ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001527 do_smart_update(sma, NULL, 0, 0, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 err = 0;
1529 goto out_unlock;
1530 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001531 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 err = -EINVAL;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001534 if (semnum < 0 || semnum >= nsems)
1535 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Rik van Riel6062a8d2013-04-30 19:15:44 -07001537 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001538 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001539 err = -EIDRM;
1540 goto out_unlock;
1541 }
Davidlohr Buesoec67aaa2018-06-14 15:27:51 -07001542
1543 semnum = array_index_nospec(semnum, nsems);
Manfred Spraul1a233952017-07-12 14:34:38 -07001544 curr = &sma->sems[semnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 switch (cmd) {
1547 case GETVAL:
1548 err = curr->semval;
1549 goto out_unlock;
1550 case GETPID:
Eric W. Biederman51d6f262018-03-23 01:11:29 -05001551 err = pid_vnr(curr->sempid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 goto out_unlock;
1553 case GETNCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001554 err = count_semcnt(sma, semnum, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 goto out_unlock;
1556 case GETZCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001557 err = count_semcnt(sma, semnum, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001562 sem_unlock(sma, -1);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001563out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001564 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001565 wake_up_q(&wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08001567 if (sem_io != fast_sem_io)
Kees Cookf8dbe8d2017-07-12 14:34:47 -07001568 kvfree(sem_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return err;
1570}
1571
Pierre Peiffer016d7132008-04-29 01:00:50 -07001572static inline unsigned long
1573copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Manfred Spraul239521f2014-01-27 17:07:04 -08001575 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001577 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 case IPC_OLD:
1581 {
1582 struct semid_ds tbuf_old;
1583
Manfred Spraul239521f2014-01-27 17:07:04 -08001584 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return -EFAULT;
1586
Pierre Peiffer016d7132008-04-29 01:00:50 -07001587 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1588 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1589 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 return 0;
1592 }
1593 default:
1594 return -EINVAL;
1595 }
1596}
1597
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001598/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001599 * This function handles some semctl commands which require the rwsem
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001600 * to be held in write mode.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001601 * NOTE: no locks must be held, the rwsem is taken inside this function.
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001602 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001603static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viro45a4a642017-07-09 09:11:00 -04001604 int cmd, struct semid64_ds *semid64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 struct sem_array *sma;
1607 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 struct kern_ipc_perm *ipcp;
1609
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001610 down_write(&sem_ids(ns).rwsem);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001611 rcu_read_lock();
1612
Manfred Spraul4241c1a2018-08-21 22:01:34 -07001613 ipcp = ipcctl_obtain_check(ns, &sem_ids(ns), semid, cmd,
Al Viro45a4a642017-07-09 09:11:00 -04001614 &semid64->sem_perm, 0);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001615 if (IS_ERR(ipcp)) {
1616 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001617 goto out_unlock1;
1618 }
Steve Grubb073115d2006-04-02 17:07:33 -04001619
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001620 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Eric W. Biedermanaefad952018-03-22 20:52:43 -05001622 err = security_sem_semctl(&sma->sem_perm, cmd);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001623 if (err)
1624 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001626 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001628 sem_lock(sma, NULL, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001629 /* freeary unlocks the ipc object and rcu */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001630 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001631 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001633 sem_lock(sma, NULL, -1);
Al Viro45a4a642017-07-09 09:11:00 -04001634 err = ipc_update_perm(&semid64->sem_perm, ipcp);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001635 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001636 goto out_unlock0;
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07001637 sma->sem_ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 break;
1639 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 err = -EINVAL;
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001641 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001644out_unlock0:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001645 sem_unlock(sma, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001646out_unlock1:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001647 rcu_read_unlock();
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001648out_up:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001649 up_write(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return err;
1651}
1652
Arnd Bergmann275f2212018-12-31 22:22:40 +01001653static long ksys_semctl(int semid, int semnum, int cmd, unsigned long arg, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Kirill Korotaeve3893532006-10-02 02:18:22 -07001655 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001656 void __user *p = (void __user *)arg;
Al Viro45a4a642017-07-09 09:11:00 -04001657 struct semid64_ds semid64;
1658 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 if (semid < 0)
1661 return -EINVAL;
1662
Kirill Korotaeve3893532006-10-02 02:18:22 -07001663 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Manfred Spraul239521f2014-01-27 17:07:04 -08001665 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 case IPC_INFO:
1667 case SEM_INFO:
Al Viro45a4a642017-07-09 09:11:00 -04001668 return semctl_info(ns, semid, cmd, p);
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001669 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07001671 case SEM_STAT_ANY:
Al Viro45a4a642017-07-09 09:11:00 -04001672 err = semctl_stat(ns, semid, cmd, &semid64);
1673 if (err < 0)
1674 return err;
1675 if (copy_semid_to_user(p, &semid64, version))
1676 err = -EFAULT;
1677 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 case GETALL:
1679 case GETVAL:
1680 case GETPID:
1681 case GETNCNT:
1682 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001684 return semctl_main(ns, semid, semnum, cmd, p);
Al Viro45a4a642017-07-09 09:11:00 -04001685 case SETVAL: {
1686 int val;
1687#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1688 /* big-endian 64bit */
1689 val = arg >> 32;
1690#else
1691 /* 32bit or little-endian 64bit */
1692 val = arg;
1693#endif
1694 return semctl_setval(ns, semid, semnum, val);
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 case IPC_SET:
Al Viro45a4a642017-07-09 09:11:00 -04001697 if (copy_semid_from_user(&semid64, p, version))
1698 return -EFAULT;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001699 fallthrough;
Al Viro45a4a642017-07-09 09:11:00 -04001700 case IPC_RMID:
1701 return semctl_down(ns, semid, cmd, &semid64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 default:
1703 return -EINVAL;
1704 }
1705}
1706
Dominik Brodowskid969c6f2018-03-20 20:00:39 +01001707SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
1708{
Arnd Bergmann275f2212018-12-31 22:22:40 +01001709 return ksys_semctl(semid, semnum, cmd, arg, IPC_64);
Dominik Brodowskid969c6f2018-03-20 20:00:39 +01001710}
1711
Arnd Bergmann275f2212018-12-31 22:22:40 +01001712#ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
1713long ksys_old_semctl(int semid, int semnum, int cmd, unsigned long arg)
1714{
1715 int version = ipc_parse_version(&cmd);
1716
1717 return ksys_semctl(semid, semnum, cmd, arg, version);
1718}
1719
1720SYSCALL_DEFINE4(old_semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
1721{
1722 return ksys_old_semctl(semid, semnum, cmd, arg);
1723}
1724#endif
1725
Al Viroc0ebccb2017-07-09 10:03:23 -04001726#ifdef CONFIG_COMPAT
1727
1728struct compat_semid_ds {
1729 struct compat_ipc_perm sem_perm;
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02001730 old_time32_t sem_otime;
1731 old_time32_t sem_ctime;
Al Viroc0ebccb2017-07-09 10:03:23 -04001732 compat_uptr_t sem_base;
1733 compat_uptr_t sem_pending;
1734 compat_uptr_t sem_pending_last;
1735 compat_uptr_t undo;
1736 unsigned short sem_nsems;
1737};
1738
1739static int copy_compat_semid_from_user(struct semid64_ds *out, void __user *buf,
1740 int version)
1741{
1742 memset(out, 0, sizeof(*out));
1743 if (version == IPC_64) {
Linus Torvalds6aa211e2017-09-25 18:37:28 -07001744 struct compat_semid64_ds __user *p = buf;
Al Viroc0ebccb2017-07-09 10:03:23 -04001745 return get_compat_ipc64_perm(&out->sem_perm, &p->sem_perm);
1746 } else {
Linus Torvalds6aa211e2017-09-25 18:37:28 -07001747 struct compat_semid_ds __user *p = buf;
Al Viroc0ebccb2017-07-09 10:03:23 -04001748 return get_compat_ipc_perm(&out->sem_perm, &p->sem_perm);
1749 }
1750}
1751
1752static int copy_compat_semid_to_user(void __user *buf, struct semid64_ds *in,
1753 int version)
1754{
1755 if (version == IPC_64) {
1756 struct compat_semid64_ds v;
1757 memset(&v, 0, sizeof(v));
1758 to_compat_ipc64_perm(&v.sem_perm, &in->sem_perm);
Arnd Bergmannc2ab9752015-04-28 21:39:50 +02001759 v.sem_otime = lower_32_bits(in->sem_otime);
1760 v.sem_otime_high = upper_32_bits(in->sem_otime);
1761 v.sem_ctime = lower_32_bits(in->sem_ctime);
1762 v.sem_ctime_high = upper_32_bits(in->sem_ctime);
Al Viroc0ebccb2017-07-09 10:03:23 -04001763 v.sem_nsems = in->sem_nsems;
1764 return copy_to_user(buf, &v, sizeof(v));
1765 } else {
1766 struct compat_semid_ds v;
1767 memset(&v, 0, sizeof(v));
1768 to_compat_ipc_perm(&v.sem_perm, &in->sem_perm);
1769 v.sem_otime = in->sem_otime;
1770 v.sem_ctime = in->sem_ctime;
1771 v.sem_nsems = in->sem_nsems;
1772 return copy_to_user(buf, &v, sizeof(v));
1773 }
1774}
1775
Arnd Bergmann275f2212018-12-31 22:22:40 +01001776static long compat_ksys_semctl(int semid, int semnum, int cmd, int arg, int version)
Al Viroc0ebccb2017-07-09 10:03:23 -04001777{
1778 void __user *p = compat_ptr(arg);
1779 struct ipc_namespace *ns;
1780 struct semid64_ds semid64;
Al Viroc0ebccb2017-07-09 10:03:23 -04001781 int err;
1782
1783 ns = current->nsproxy->ipc_ns;
1784
1785 if (semid < 0)
1786 return -EINVAL;
1787
1788 switch (cmd & (~IPC_64)) {
1789 case IPC_INFO:
1790 case SEM_INFO:
1791 return semctl_info(ns, semid, cmd, p);
1792 case IPC_STAT:
1793 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07001794 case SEM_STAT_ANY:
Al Viroc0ebccb2017-07-09 10:03:23 -04001795 err = semctl_stat(ns, semid, cmd, &semid64);
1796 if (err < 0)
1797 return err;
1798 if (copy_compat_semid_to_user(p, &semid64, version))
1799 err = -EFAULT;
1800 return err;
1801 case GETVAL:
1802 case GETPID:
1803 case GETNCNT:
1804 case GETZCNT:
1805 case GETALL:
1806 case SETALL:
1807 return semctl_main(ns, semid, semnum, cmd, p);
1808 case SETVAL:
1809 return semctl_setval(ns, semid, semnum, arg);
1810 case IPC_SET:
1811 if (copy_compat_semid_from_user(&semid64, p, version))
1812 return -EFAULT;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001813 fallthrough;
Al Viroc0ebccb2017-07-09 10:03:23 -04001814 case IPC_RMID:
1815 return semctl_down(ns, semid, cmd, &semid64);
1816 default:
1817 return -EINVAL;
1818 }
1819}
Dominik Brodowskid969c6f2018-03-20 20:00:39 +01001820
1821COMPAT_SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, int, arg)
1822{
Arnd Bergmann275f2212018-12-31 22:22:40 +01001823 return compat_ksys_semctl(semid, semnum, cmd, arg, IPC_64);
Dominik Brodowskid969c6f2018-03-20 20:00:39 +01001824}
Arnd Bergmann275f2212018-12-31 22:22:40 +01001825
1826#ifdef CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION
1827long compat_ksys_old_semctl(int semid, int semnum, int cmd, int arg)
1828{
1829 int version = compat_ipc_parse_version(&cmd);
1830
1831 return compat_ksys_semctl(semid, semnum, cmd, arg, version);
1832}
1833
1834COMPAT_SYSCALL_DEFINE4(old_semctl, int, semid, int, semnum, int, cmd, int, arg)
1835{
1836 return compat_ksys_old_semctl(semid, semnum, cmd, arg);
1837}
1838#endif
Al Viroc0ebccb2017-07-09 10:03:23 -04001839#endif
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841/* If the task doesn't already have a undo_list, then allocate one
1842 * here. We guarantee there is only one thread using this undo list,
1843 * and current is THE ONE
1844 *
1845 * If this allocation and assignment succeeds, but later
1846 * portions of this code fail, there is no need to free the sem_undo_list.
1847 * Just let it stay associated with the task, and it'll be freed later
1848 * at exit time.
1849 *
1850 * This can block, so callers must hold no locks.
1851 */
1852static inline int get_undo_list(struct sem_undo_list **undo_listp)
1853{
1854 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 undo_list = current->sysvsem.undo_list;
1857 if (!undo_list) {
Vasily Averin18319492021-09-02 14:55:31 -07001858 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (undo_list == NULL)
1860 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001861 spin_lock_init(&undo_list->lock);
Elena Reshetovaf74370b2017-09-08 16:17:42 -07001862 refcount_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001863 INIT_LIST_HEAD(&undo_list->list_proc);
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 current->sysvsem.undo_list = undo_list;
1866 }
1867 *undo_listp = undo_list;
1868 return 0;
1869}
1870
Nick Pigginbf17bb72009-12-15 16:47:28 -08001871static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001873 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Joel Fernandes (Google)984035a2019-09-25 16:48:20 -07001875 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc,
1876 spin_is_locked(&ulp->lock)) {
Nick Pigginbf17bb72009-12-15 16:47:28 -08001877 if (un->semid == semid)
1878 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001880 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Nick Pigginbf17bb72009-12-15 16:47:28 -08001883static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1884{
1885 struct sem_undo *un;
1886
Manfred Spraul239521f2014-01-27 17:07:04 -08001887 assert_spin_locked(&ulp->lock);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001888
1889 un = __lookup_undo(ulp, semid);
1890 if (un) {
1891 list_del_rcu(&un->list_proc);
1892 list_add_rcu(&un->list_proc, &ulp->list_proc);
1893 }
1894 return un;
1895}
1896
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001897/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001898 * find_alloc_undo - lookup (and if not present create) undo array
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001899 * @ns: namespace
1900 * @semid: semaphore array id
1901 *
1902 * The function looks up (and if not present creates) the undo structure.
1903 * The size of the undo structure depends on the size of the semaphore
1904 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001905 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1906 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001907 */
1908static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 struct sem_array *sma;
1911 struct sem_undo_list *ulp;
1912 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001913 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 error = get_undo_list(&ulp);
1916 if (error)
1917 return ERR_PTR(error);
1918
Manfred Spraul380af1b2008-07-25 01:48:06 -07001919 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001920 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001922 spin_unlock(&ulp->lock);
Manfred Spraul239521f2014-01-27 17:07:04 -08001923 if (likely(un != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 goto out;
1925
1926 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001927 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001928 sma = sem_obtain_object_check(ns, semid);
1929 if (IS_ERR(sma)) {
1930 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001931 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001932 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 nsems = sma->sem_nsems;
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001935 if (!ipc_rcu_getref(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001936 rcu_read_unlock();
1937 un = ERR_PTR(-EIDRM);
1938 goto out;
1939 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001940 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001942 /* step 2: allocate new undo structure */
Vasily Averinfc37a3b2021-06-30 18:57:09 -07001943 new = kvzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems,
Vasily Averin18319492021-09-02 14:55:31 -07001944 GFP_KERNEL_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (!new) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001946 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return ERR_PTR(-ENOMEM);
1948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Manfred Spraul380af1b2008-07-25 01:48:06 -07001950 /* step 3: Acquire the lock on semaphore array */
Linus Torvalds4091fd92013-05-04 10:13:40 -07001951 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001952 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001953 if (!ipc_valid_object(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001954 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001955 rcu_read_unlock();
Vasily Averinfc37a3b2021-06-30 18:57:09 -07001956 kvfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 un = ERR_PTR(-EIDRM);
1958 goto out;
1959 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001960 spin_lock(&ulp->lock);
1961
1962 /*
1963 * step 4: check for races: did someone else allocate the undo struct?
1964 */
1965 un = lookup_undo(ulp, semid);
1966 if (un) {
Vasily Averinfc37a3b2021-06-30 18:57:09 -07001967 kvfree(new);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001968 goto success;
1969 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001970 /* step 5: initialize & link new undo structure */
1971 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001972 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001973 new->semid = semid;
1974 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001975 list_add_rcu(&new->list_proc, &ulp->list_proc);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001976 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001977 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001978 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001979
1980success:
1981 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001982 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983out:
1984 return un;
1985}
1986
Arnd Bergmannbdec0142021-08-11 08:30:23 +01001987long __do_semtimedop(int semid, struct sembuf *sops,
1988 unsigned nsops, const struct timespec64 *timeout,
1989 struct ipc_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 int error = -EINVAL;
1992 struct sem_array *sma;
Arnd Bergmannbdec0142021-08-11 08:30:23 +01001993 struct sembuf *sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 struct sem_undo *un;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001995 int max, locknum;
1996 bool undos = false, alter = false, dupsop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 struct sem_queue queue;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001998 unsigned long dup = 0, jiffies_left = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 if (nsops < 1 || semid < 0)
2001 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07002002 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return -E2BIG;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08002004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (timeout) {
Al Viro44ee4542017-07-09 10:50:14 -04002006 if (timeout->tv_sec < 0 || timeout->tv_nsec < 0 ||
2007 timeout->tv_nsec >= 1000000000L) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 error = -EINVAL;
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
Deepa Dinamani3ef56dc2017-08-02 19:51:10 -07002011 jiffies_left = timespec64_to_jiffies(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08002013
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 max = 0;
2016 for (sop = sops; sop < sops + nsops; sop++) {
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08002017 unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (sop->sem_num >= max)
2020 max = sop->sem_num;
2021 if (sop->sem_flg & SEM_UNDO)
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08002022 undos = true;
2023 if (dup & mask) {
2024 /*
2025 * There was a previous alter access that appears
2026 * to have accessed the same semaphore, thus use
2027 * the dupsop logic. "appears", because the detection
2028 * can only check % BITS_PER_LONG.
2029 */
2030 dupsop = true;
2031 }
2032 if (sop->sem_op != 0) {
2033 alter = true;
2034 dup |= mask;
2035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07002039 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002040 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (IS_ERR(un)) {
2042 error = PTR_ERR(un);
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002043 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07002045 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002047 rcu_read_lock();
2048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Davidlohr Bueso16df3672013-04-30 19:15:29 -07002050 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07002051 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07002052 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07002053 error = PTR_ERR(sma);
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002054 goto out;
Nadia Derbey023a5352007-10-18 23:40:51 -07002055 }
2056
Davidlohr Bueso16df3672013-04-30 19:15:29 -07002057 error = -EFBIG;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08002058 if (max >= sma->sem_nsems) {
2059 rcu_read_unlock();
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002060 goto out;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08002061 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07002062
2063 error = -EACCES;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08002064 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
2065 rcu_read_unlock();
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002066 goto out;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08002067 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07002068
Eric W. Biedermanaefad952018-03-22 20:52:43 -05002069 error = security_sem_semop(&sma->sem_perm, sops, nsops, alter);
Davidlohr Bueso248e7352016-12-14 15:06:31 -08002070 if (error) {
2071 rcu_read_unlock();
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002072 goto out;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08002073 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07002074
Manfred Spraul6e224f92013-10-16 13:46:45 -07002075 error = -EIDRM;
2076 locknum = sem_lock(sma, sops, nsops);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08002077 /*
2078 * We eventually might perform the following check in a lockless
2079 * fashion, considering ipc_valid_object() locking constraints.
2080 * If nsops == 1 and there is no contention for sem_perm.lock, then
2081 * only a per-semaphore lock is held and it's OK to proceed with the
2082 * check below. More details on the fine grained locking scheme
2083 * entangled here and why it's RMID race safe on comments at sem_lock()
2084 */
2085 if (!ipc_valid_object(&sma->sem_perm))
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002086 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002088 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002090 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002091 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07002092 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002094 if (un && un->semid == -1)
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002095 goto out_unlock;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002096
Manfred Sprauld198cd62014-06-06 14:37:49 -07002097 queue.sops = sops;
2098 queue.nsops = nsops;
2099 queue.undo = un;
Eric W. Biederman51d6f262018-03-23 01:11:29 -05002100 queue.pid = task_tgid(current);
Manfred Sprauld198cd62014-06-06 14:37:49 -07002101 queue.alter = alter;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08002102 queue.dupsop = dupsop;
Manfred Sprauld198cd62014-06-06 14:37:49 -07002103
2104 error = perform_atomic_semop(sma, &queue);
Bhaskar Chowdhuryb1989a32021-05-06 18:05:48 -07002105 if (error == 0) { /* non-blocking successful path */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002106 DEFINE_WAKE_Q(wake_q);
2107
2108 /*
2109 * If the operation was successful, then do
Manfred Spraul0e8c6652013-09-30 13:45:25 -07002110 * the required updates.
2111 */
2112 if (alter)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002113 do_smart_update(sma, sops, nsops, 1, &wake_q);
Manfred Spraul0e8c6652013-09-30 13:45:25 -07002114 else
2115 set_semotime(sma, sops);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002116
2117 sem_unlock(sma, locknum);
2118 rcu_read_unlock();
2119 wake_up_q(&wake_q);
2120
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002121 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002123 if (error < 0) /* non-blocking error path */
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002124 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002126 /*
2127 * We need to sleep on this operation, so we put the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 * task into the pending queue and go to sleep.
2129 */
Manfred Spraulb97e8202009-12-15 16:47:32 -08002130 if (nsops == 1) {
2131 struct sem *curr;
Davidlohr Buesoec67aaa2018-06-14 15:27:51 -07002132 int idx = array_index_nospec(sops->sem_num, sma->sem_nsems);
2133 curr = &sma->sems[idx];
Manfred Spraulb97e8202009-12-15 16:47:32 -08002134
Manfred Spraulf269f402013-07-08 16:01:24 -07002135 if (alter) {
2136 if (sma->complex_count) {
2137 list_add_tail(&queue.list,
2138 &sma->pending_alter);
2139 } else {
2140
2141 list_add_tail(&queue.list,
2142 &curr->pending_alter);
2143 }
2144 } else {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07002145 list_add_tail(&queue.list, &curr->pending_const);
Manfred Spraulf269f402013-07-08 16:01:24 -07002146 }
Manfred Spraulb97e8202009-12-15 16:47:32 -08002147 } else {
Manfred Spraulf269f402013-07-08 16:01:24 -07002148 if (!sma->complex_count)
2149 merge_queues(sma);
2150
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07002151 if (alter)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07002152 list_add_tail(&queue.list, &sma->pending_alter);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07002153 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07002154 list_add_tail(&queue.list, &sma->pending_const);
2155
Manfred Spraulb97e8202009-12-15 16:47:32 -08002156 sma->complex_count++;
2157 }
2158
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002159 do {
Manfred Spraul8116b542020-02-03 17:34:42 -08002160 /* memory ordering ensured by the lock in sem_lock() */
Davidlohr Buesof075faa2018-07-26 16:37:19 -07002161 WRITE_ONCE(queue.status, -EINTR);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002162 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07002163
Manfred Spraul8116b542020-02-03 17:34:42 -08002164 /* memory ordering is ensured by the lock in sem_lock() */
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002165 __set_current_state(TASK_INTERRUPTIBLE);
2166 sem_unlock(sma, locknum);
Linus Torvalds321310c2013-05-04 10:47:57 -07002167 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002169 if (timeout)
2170 jiffies_left = schedule_timeout(jiffies_left);
2171 else
2172 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002174 /*
2175 * fastpath: the semop has completed, either successfully or
2176 * not, from the syscall pov, is quite irrelevant to us at this
2177 * point; we're done.
2178 *
2179 * We _do_ care, nonetheless, about being awoken by a signal or
2180 * spuriously. The queue.status is checked again in the
2181 * slowpath (aka after taking sem_lock), such that we can detect
2182 * scenarios where we were awakened externally, during the
2183 * window between wake_q_add() and wake_up_q().
2184 */
2185 error = READ_ONCE(queue.status);
2186 if (error != -EINTR) {
Manfred Spraul8116b542020-02-03 17:34:42 -08002187 /* see SEM_BARRIER_2 for purpose/pairing */
2188 smp_acquire__after_ctrl_dep();
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002189 goto out;
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002190 }
Manfred Spraul0b0577f2011-11-02 13:38:52 -07002191
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002192 rcu_read_lock();
Manfred Spraulc626bc42017-01-10 16:57:48 -08002193 locknum = sem_lock(sma, sops, nsops);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002194
Davidlohr Bueso370b2622016-12-14 15:06:49 -08002195 if (!ipc_valid_object(&sma->sem_perm))
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002196 goto out_unlock;
Davidlohr Bueso370b2622016-12-14 15:06:49 -08002197
Manfred Spraul8116b542020-02-03 17:34:42 -08002198 /*
2199 * No necessity for any barrier: We are protect by sem_lock()
2200 */
Davidlohr Bueso370b2622016-12-14 15:06:49 -08002201 error = READ_ONCE(queue.status);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002202
2203 /*
2204 * If queue.status != -EINTR we are woken up by another process.
2205 * Leave without unlink_queue(), but with sem_unlock().
2206 */
2207 if (error != -EINTR)
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002208 goto out_unlock;
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002209
2210 /*
2211 * If an interrupt occurred we have to clean up the queue.
2212 */
2213 if (timeout && jiffies_left == 0)
2214 error = -EAGAIN;
2215 } while (error == -EINTR && !signal_pending(current)); /* spurious */
Manfred Spraul0b0577f2011-11-02 13:38:52 -07002216
Manfred Spraulb97e8202009-12-15 16:47:32 -08002217 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002219out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07002220 sem_unlock(sma, locknum);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002221 rcu_read_unlock();
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002222out:
2223 return error;
2224}
2225
2226static long do_semtimedop(int semid, struct sembuf __user *tsops,
2227 unsigned nsops, const struct timespec64 *timeout)
2228{
2229 struct sembuf fast_sops[SEMOPM_FAST];
2230 struct sembuf *sops = fast_sops;
2231 struct ipc_namespace *ns;
2232 int ret;
2233
2234 ns = current->nsproxy->ipc_ns;
2235 if (nsops > ns->sc_semopm)
2236 return -E2BIG;
2237 if (nsops < 1)
2238 return -EINVAL;
2239
2240 if (nsops > SEMOPM_FAST) {
Vasily Averin6a4746b2021-09-11 10:40:08 +03002241 sops = kvmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002242 if (sops == NULL)
2243 return -ENOMEM;
2244 }
2245
2246 if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
2247 ret = -EFAULT;
2248 goto out_free;
2249 }
2250
2251 ret = __do_semtimedop(semid, sops, nsops, timeout, ns);
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08002254 if (sops != fast_sops)
Davidlohr Buesoe4243b82017-09-08 16:17:52 -07002255 kvfree(sops);
Arnd Bergmannbdec0142021-08-11 08:30:23 +01002256
2257 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +01002260long ksys_semtimedop(int semid, struct sembuf __user *tsops,
Arnd Bergmann21fc5382018-04-13 13:58:00 +02002261 unsigned int nsops, const struct __kernel_timespec __user *timeout)
Al Viro44ee4542017-07-09 10:50:14 -04002262{
2263 if (timeout) {
Deepa Dinamani3ef56dc2017-08-02 19:51:10 -07002264 struct timespec64 ts;
2265 if (get_timespec64(&ts, timeout))
Al Viro44ee4542017-07-09 10:50:14 -04002266 return -EFAULT;
2267 return do_semtimedop(semid, tsops, nsops, &ts);
2268 }
2269 return do_semtimedop(semid, tsops, nsops, NULL);
2270}
2271
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +01002272SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
Arnd Bergmann21fc5382018-04-13 13:58:00 +02002273 unsigned int, nsops, const struct __kernel_timespec __user *, timeout)
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +01002274{
2275 return ksys_semtimedop(semid, tsops, nsops, timeout);
2276}
2277
Arnd Bergmannb0d17572018-04-13 13:58:23 +02002278#ifdef CONFIG_COMPAT_32BIT_TIME
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +01002279long compat_ksys_semtimedop(int semid, struct sembuf __user *tsems,
2280 unsigned int nsops,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02002281 const struct old_timespec32 __user *timeout)
Al Viro44ee4542017-07-09 10:50:14 -04002282{
2283 if (timeout) {
Deepa Dinamani3ef56dc2017-08-02 19:51:10 -07002284 struct timespec64 ts;
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02002285 if (get_old_timespec32(&ts, timeout))
Al Viro44ee4542017-07-09 10:50:14 -04002286 return -EFAULT;
2287 return do_semtimedop(semid, tsems, nsops, &ts);
2288 }
2289 return do_semtimedop(semid, tsems, nsops, NULL);
2290}
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +01002291
Arnd Bergmann8dabe722019-01-07 00:33:08 +01002292SYSCALL_DEFINE4(semtimedop_time32, int, semid, struct sembuf __user *, tsems,
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +01002293 unsigned int, nsops,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02002294 const struct old_timespec32 __user *, timeout)
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +01002295{
2296 return compat_ksys_semtimedop(semid, tsems, nsops, timeout);
2297}
Al Viro44ee4542017-07-09 10:50:14 -04002298#endif
2299
Heiko Carstensd5460c92009-01-14 14:14:27 +01002300SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
2301 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
Al Viro44ee4542017-07-09 10:50:14 -04002303 return do_semtimedop(semid, tsops, nsops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
2306/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
2307 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 */
2309
2310int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
2311{
2312 struct sem_undo_list *undo_list;
2313 int error;
2314
2315 if (clone_flags & CLONE_SYSVSEM) {
2316 error = get_undo_list(&undo_list);
2317 if (error)
2318 return error;
Elena Reshetovaf74370b2017-09-08 16:17:42 -07002319 refcount_inc(&undo_list->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 tsk->sysvsem.undo_list = undo_list;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07002321 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 tsk->sysvsem.undo_list = NULL;
2323
2324 return 0;
2325}
2326
2327/*
2328 * add semadj values to semaphores, free undo structures.
2329 * undo structures are not freed when semaphore arrays are destroyed
2330 * so some of them may be out of date.
2331 * IMPLEMENTATION NOTE: There is some confusion over whether the
2332 * set of adjustments that needs to be done should be done in an atomic
2333 * manner or not. That is, if we are attempting to decrement the semval
2334 * should we queue up and wait until we can do so legally?
2335 * The original implementation attempted to do this (queue and wait).
2336 * The current implementation does not do so. The POSIX standard
2337 * and SVID should be consulted to determine what behavior is mandated.
2338 */
2339void exit_sem(struct task_struct *tsk)
2340{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002341 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002343 ulp = tsk->sysvsem.undo_list;
2344 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002346 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Elena Reshetovaf74370b2017-09-08 16:17:42 -07002348 if (!refcount_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return;
2350
Manfred Spraul380af1b2008-07-25 01:48:06 -07002351 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07002353 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002354 int semid, i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002355 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Nikolay Borisov2a1613a2016-10-11 13:55:05 -07002357 cond_resched();
2358
Manfred Spraul380af1b2008-07-25 01:48:06 -07002359 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02002360 un = list_entry_rcu(ulp->list_proc.next,
2361 struct sem_undo, list_proc);
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002362 if (&un->list_proc == &ulp->list_proc) {
2363 /*
2364 * We must wait for freeary() before freeing this ulp,
2365 * in case we raced with last sem_undo. There is a small
2366 * possibility where we exit while freeary() didn't
2367 * finish unlocking sem_undo_list.
2368 */
Paul E. McKenneye0892e02017-06-29 12:59:32 -07002369 spin_lock(&ulp->lock);
2370 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002371 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002372 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002373 }
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002374 spin_lock(&ulp->lock);
2375 semid = un->semid;
2376 spin_unlock(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002377
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002378 /* exit_sem raced with IPC_RMID, nothing to do */
2379 if (semid == -1) {
2380 rcu_read_unlock();
2381 continue;
2382 }
2383
2384 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002385 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002386 if (IS_ERR(sma)) {
2387 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002388 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Rik van Riel6062a8d2013-04-30 19:15:44 -07002391 sem_lock(sma, NULL, -1);
Manfred Spraul6e224f92013-10-16 13:46:45 -07002392 /* exit_sem raced with IPC_RMID, nothing to do */
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08002393 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07002394 sem_unlock(sma, -1);
2395 rcu_read_unlock();
2396 continue;
2397 }
Nick Pigginbf17bb72009-12-15 16:47:28 -08002398 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002399 if (un == NULL) {
2400 /* exit_sem raced with IPC_RMID+semget() that created
2401 * exactly the same semid. Nothing to do.
2402 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002403 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002404 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002405 continue;
2406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Manfred Spraul380af1b2008-07-25 01:48:06 -07002408 /* remove un from the linked lists */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07002409 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002410 list_del(&un->list_id);
2411
Ioanna Alifierakiedf28f42020-02-20 20:04:00 -08002412 spin_lock(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002413 list_del_rcu(&un->list_proc);
Ioanna Alifierakiedf28f42020-02-20 20:04:00 -08002414 spin_unlock(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002415
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002416 /* perform adjustments registered in un */
2417 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07002418 struct sem *semaphore = &sma->sems[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002419 if (un->semadj[i]) {
2420 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 /*
2422 * Range checks of the new semaphore value,
2423 * not defined by sus:
2424 * - Some unices ignore the undo entirely
2425 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2426 * - some cap the value (e.g. FreeBSD caps
2427 * at 0, but doesn't enforce SEMVMX)
2428 *
2429 * Linux caps the semaphore value, both at 0
2430 * and at SEMVMX.
2431 *
Manfred Spraul239521f2014-01-27 17:07:04 -08002432 * Manfred <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08002434 if (semaphore->semval < 0)
2435 semaphore->semval = 0;
2436 if (semaphore->semval > SEMVMX)
2437 semaphore->semval = SEMVMX;
Eric W. Biederman51d6f262018-03-23 01:11:29 -05002438 ipc_update_pid(&semaphore->sempid, task_tgid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002442 do_smart_update(sma, NULL, 0, 1, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002443 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002444 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002445 wake_up_q(&wake_q);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002446
Vasily Averinfc37a3b2021-06-30 18:57:09 -07002447 kvfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002449 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
2452#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07002453static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08002455 struct user_namespace *user_ns = seq_user_ns(s);
Kees Cookade9f912017-08-02 13:32:21 -07002456 struct kern_ipc_perm *ipcp = it;
2457 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07002458 time64_t sem_otime;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002459
Manfred Sprauld8c63372013-09-30 13:45:07 -07002460 /*
2461 * The proc interface isn't aware of sem_lock(), it calls
Manfred Spraul17d056e2021-06-30 18:57:15 -07002462 * ipc_lock_object(), i.e. spin_lock(&sma->sem_perm.lock).
2463 * (in sysvipc_find_ipc)
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002464 * In order to stay compatible with sem_lock(), we must
2465 * enter / leave complex_mode.
Manfred Sprauld8c63372013-09-30 13:45:07 -07002466 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002467 complexmode_enter(sma);
Manfred Sprauld8c63372013-09-30 13:45:07 -07002468
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002469 sem_otime = get_semotime(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Joe Perches7f032d62015-04-15 16:17:54 -07002471 seq_printf(s,
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07002472 "%10d %10d %4o %10u %5u %5u %5u %5u %10llu %10llu\n",
Joe Perches7f032d62015-04-15 16:17:54 -07002473 sma->sem_perm.key,
2474 sma->sem_perm.id,
2475 sma->sem_perm.mode,
2476 sma->sem_nsems,
2477 from_kuid_munged(user_ns, sma->sem_perm.uid),
2478 from_kgid_munged(user_ns, sma->sem_perm.gid),
2479 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2480 from_kgid_munged(user_ns, sma->sem_perm.cgid),
2481 sem_otime,
2482 sma->sem_ctime);
2483
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002484 complexmode_tryleave(sma);
2485
Joe Perches7f032d62015-04-15 16:17:54 -07002486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488#endif