blob: 2b2ed56e0fde246cd16c1022b05adb694476ee9f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/sem.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 * Copyright (C) 1995 Eric Schenk, Bruno Haible
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
7 *
8 * SMP-threaded, sysctl's added
Christian Kujau624dffc2006-01-15 02:43:54 +01009 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Enforced range limit on SEM_UNDO
Alan Cox046c6882009-01-05 14:06:29 +000011 * (c) 2001 Red Hat Inc
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Lockless wakeup
13 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -080014 * (c) 2016 Davidlohr Bueso <dave@stgolabs.net>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070015 * Further wakeup optimizations, documentation
16 * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040017 *
18 * support for audit of ipc object properties and permission changes
19 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaeve3893532006-10-02 02:18:22 -070020 *
21 * namespaces support
22 * OpenVZ, SWsoft Inc.
23 * Pavel Emelianov <xemul@openvz.org>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070024 *
25 * Implementation notes: (May 2010)
26 * This file implements System V semaphores.
27 *
28 * User space visible behavior:
29 * - FIFO ordering for semop() operations (just FIFO, not starvation
30 * protection)
31 * - multiple semaphore operations that alter the same semaphore in
32 * one semop() are handled.
33 * - sem_ctime (time of last semctl()) is updated in the IPC_SET, SETVAL and
34 * SETALL calls.
35 * - two Linux specific semctl() commands: SEM_STAT, SEM_INFO.
36 * - undo adjustments at process exit are limited to 0..SEMVMX.
37 * - namespace are supported.
38 * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtine by writing
39 * to /proc/sys/kernel/sem.
40 * - statistics about the usage are reported in /proc/sysvipc/sem.
41 *
42 * Internals:
43 * - scalability:
44 * - all global variables are read-mostly.
45 * - semop() calls and semctl(RMID) are synchronized by RCU.
46 * - most operations do write operations (actually: spin_lock calls) to
47 * the per-semaphore array structure.
48 * Thus: Perfect SMP scaling between independent semaphore arrays.
49 * If multiple semaphores in one array are used, then cache line
50 * trashing on the semaphore array spinlock will limit the scaling.
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -070051 * - semncnt and semzcnt are calculated on demand in count_semcnt()
Manfred Spraulc5cf6352010-05-26 14:43:43 -070052 * - the task that performs a successful semop() scans the list of all
53 * sleeping tasks and completes any pending operations that can be fulfilled.
54 * Semaphores are actively given to waiting tasks (necessary for FIFO).
55 * (see update_queue())
56 * - To improve the scalability, the actual wake-up calls are performed after
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -080057 * dropping all locks. (see wake_up_sem_queue_prepare())
Manfred Spraulc5cf6352010-05-26 14:43:43 -070058 * - All work is done by the waker, the woken up task does not have to do
59 * anything - not even acquiring a lock or dropping a refcount.
60 * - A woken up task may not even touch the semaphore array anymore, it may
61 * have been destroyed already by a semctl(RMID).
Manfred Spraulc5cf6352010-05-26 14:43:43 -070062 * - UNDO values are stored in an array (one per process and per
63 * semaphore array, lazily allocated). For backwards compatibility, multiple
64 * modes for the UNDO variables are supported (per process, per thread)
65 * (see copy_semundo, CLONE_SYSVSEM)
66 * - There are two lists of the pending operations: a per-array list
67 * and per-semaphore list (stored in the array). This allows to achieve FIFO
68 * ordering without always scanning all pending operations.
69 * The worst-case behavior is nevertheless O(N^2) for N wakeups.
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/slab.h>
73#include <linux/spinlock.h>
74#include <linux/init.h>
75#include <linux/proc_fs.h>
76#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/security.h>
78#include <linux/syscalls.h>
79#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080080#include <linux/capability.h>
Mike Waychison19b49462005-09-06 15:17:10 -070081#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070082#include <linux/rwsem.h>
Kirill Korotaeve3893532006-10-02 02:18:22 -070083#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080084#include <linux/ipc_namespace.h>
Ingo Molnar84f001e2017-02-01 16:36:40 +010085#include <linux/sched/wake_q.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080086
Paul McQuade7153e402014-06-06 14:37:37 -070087#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include "util.h"
89
Manfred Spraule57940d2011-11-02 13:38:54 -070090
91/* One queue for each sleeping process in the system. */
92struct sem_queue {
Manfred Spraule57940d2011-11-02 13:38:54 -070093 struct list_head list; /* queue of pending operations */
94 struct task_struct *sleeper; /* this process */
95 struct sem_undo *undo; /* undo structure */
96 int pid; /* process id of requesting process */
97 int status; /* completion status of operation */
98 struct sembuf *sops; /* array of pending operations */
Manfred Sprauled247b72014-06-06 14:37:49 -070099 struct sembuf *blocking; /* the operation that blocked */
Manfred Spraule57940d2011-11-02 13:38:54 -0700100 int nsops; /* number of operations */
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800101 bool alter; /* does *sops alter the array? */
102 bool dupsop; /* sops on more than one sem_num */
Manfred Spraule57940d2011-11-02 13:38:54 -0700103};
104
105/* Each task has a list of undo requests. They are executed automatically
106 * when the process exits.
107 */
108struct sem_undo {
109 struct list_head list_proc; /* per-process list: *
110 * all undos from one process
111 * rcu protected */
112 struct rcu_head rcu; /* rcu struct for sem_undo */
113 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
114 struct list_head list_id; /* per semaphore array list:
115 * all undos for one array */
116 int semid; /* semaphore set identifier */
117 short *semadj; /* array of adjustments */
118 /* one per semaphore */
119};
120
121/* sem_undo_list controls shared access to the list of sem_undo structures
122 * that may be shared among all a CLONE_SYSVSEM task group.
123 */
124struct sem_undo_list {
125 atomic_t refcnt;
126 spinlock_t lock;
127 struct list_head list_proc;
128};
129
130
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800131#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Nadia Derbey1b531f22007-10-18 23:40:55 -0700133#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700135static int newary(struct ipc_namespace *, struct ipc_params *);
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800136static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700138static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#endif
140
141#define SEMMSL_FAST 256 /* 512 bytes on stack */
142#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
143
144/*
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800145 * Switching from the mode suitable for simple ops
146 * to the mode for complex ops is costly. Therefore:
147 * use some hysteresis
148 */
149#define USE_GLOBAL_LOCK_HYSTERESIS 10
150
151/*
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700152 * Locking:
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700153 * a) global sem_lock() for read/write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * sem_undo.id_next,
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700155 * sem_array.complex_count,
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700156 * sem_array.pending{_alter,_const},
157 * sem_array.sem_undo
Paul McQuade46c0a8c2014-06-06 14:37:37 -0700158 *
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700159 * b) global or semaphore sem_lock() for read/write:
Manfred Spraul1a233952017-07-12 14:34:38 -0700160 * sem_array.sems[i].pending_{const,alter}:
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700161 *
162 * c) special:
163 * sem_undo_list.list_proc:
164 * * undo_list->lock for write
165 * * rcu for read
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800166 * use_global_lock:
167 * * global sem_lock() for write
168 * * either local or global sem_lock() for read.
169 *
170 * Memory ordering:
171 * Most ordering is enforced by using spin_lock() and spin_unlock().
172 * The special case is use_global_lock:
173 * Setting it from non-zero to 0 is a RELEASE, this is ensured by
174 * using smp_store_release().
175 * Testing if it is non-zero is an ACQUIRE, this is ensured by using
176 * smp_load_acquire().
177 * Setting it from 0 to non-zero must be ordered with regards to
178 * this smp_load_acquire(), this is guaranteed because the smp_load_acquire()
179 * is inside a spin_lock() and after a write from 0 to non-zero a
180 * spin_lock()+spin_unlock() is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 */
182
Kirill Korotaeve3893532006-10-02 02:18:22 -0700183#define sc_semmsl sem_ctls[0]
184#define sc_semmns sem_ctls[1]
185#define sc_semopm sem_ctls[2]
186#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800188void sem_init_ns(struct ipc_namespace *ns)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700189{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700190 ns->sc_semmsl = SEMMSL;
191 ns->sc_semmns = SEMMNS;
192 ns->sc_semopm = SEMOPM;
193 ns->sc_semmni = SEMMNI;
194 ns->used_sems = 0;
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800195 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700196}
197
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800198#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700199void sem_exit_ns(struct ipc_namespace *ns)
200{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800201 free_ipcs(ns, &sem_ids(ns), freeary);
Serge E. Hallyn7d6feeb2009-12-15 16:47:27 -0800202 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700203}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800204#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Manfred Spraul239521f2014-01-27 17:07:04 -0800206void __init sem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800208 sem_init_ns(&init_ipc_ns);
Mike Waychison19b49462005-09-06 15:17:10 -0700209 ipc_init_proc_interface("sysvipc/sem",
210 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700211 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Manfred Spraulf269f402013-07-08 16:01:24 -0700214/**
215 * unmerge_queues - unmerge queues, if possible.
216 * @sma: semaphore array
217 *
218 * The function unmerges the wait queues if complex_count is 0.
219 * It must be called prior to dropping the global semaphore array lock.
220 */
221static void unmerge_queues(struct sem_array *sma)
222{
223 struct sem_queue *q, *tq;
224
225 /* complex operations still around? */
226 if (sma->complex_count)
227 return;
228 /*
229 * We will switch back to simple mode.
230 * Move all pending operation back into the per-semaphore
231 * queues.
232 */
233 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
234 struct sem *curr;
Manfred Spraul1a233952017-07-12 14:34:38 -0700235 curr = &sma->sems[q->sops[0].sem_num];
Manfred Spraulf269f402013-07-08 16:01:24 -0700236
237 list_add_tail(&q->list, &curr->pending_alter);
238 }
239 INIT_LIST_HEAD(&sma->pending_alter);
240}
241
242/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800243 * merge_queues - merge single semop queues into global queue
Manfred Spraulf269f402013-07-08 16:01:24 -0700244 * @sma: semaphore array
245 *
246 * This function merges all per-semaphore queues into the global queue.
247 * It is necessary to achieve FIFO ordering for the pending single-sop
248 * operations when a multi-semop operation must sleep.
249 * Only the alter operations must be moved, the const operations can stay.
250 */
251static void merge_queues(struct sem_array *sma)
252{
253 int i;
254 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700255 struct sem *sem = &sma->sems[i];
Manfred Spraulf269f402013-07-08 16:01:24 -0700256
257 list_splice_init(&sem->pending_alter, &sma->pending_alter);
258 }
259}
260
Kees Cook1b4654e2017-07-12 14:34:50 -0700261static void __sem_free(struct sem_array *sma)
262{
263 kvfree(sma);
264}
265
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700266static void sem_rcu_free(struct rcu_head *head)
267{
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700268 struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu);
269 struct sem_array *sma = container_of(p, struct sem_array, sem_perm);
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700270
271 security_sem_free(sma);
Kees Cook1b4654e2017-07-12 14:34:50 -0700272 __sem_free(sma);
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700273}
274
Nadia Derbey3e148c72007-10-18 23:40:54 -0700275/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700276 * Enter the mode suitable for non-simple operations:
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700277 * Caller must own sem_perm.lock.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700278 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700279static void complexmode_enter(struct sem_array *sma)
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700280{
281 int i;
282 struct sem *sem;
283
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800284 if (sma->use_global_lock > 0) {
285 /*
286 * We are already in global lock mode.
287 * Nothing to do, just reset the
288 * counter until we return to simple mode.
289 */
290 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul6d07b682013-09-30 13:45:06 -0700291 return;
292 }
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800293 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700294
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700295 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700296 sem = &sma->sems[i];
Manfred Spraul27d7be12017-02-27 14:28:15 -0800297 spin_lock(&sem->lock);
298 spin_unlock(&sem->lock);
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700299 }
300}
301
302/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700303 * Try to leave the mode that disallows simple operations:
304 * Caller must own sem_perm.lock.
305 */
306static void complexmode_tryleave(struct sem_array *sma)
307{
308 if (sma->complex_count) {
309 /* Complex ops are sleeping.
310 * We must stay in complex mode
311 */
312 return;
313 }
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800314 if (sma->use_global_lock == 1) {
315 /*
316 * Immediately after setting use_global_lock to 0,
317 * a simple op can start. Thus: all memory writes
318 * performed by the current operation must be visible
319 * before we set use_global_lock to 0.
320 */
321 smp_store_release(&sma->use_global_lock, 0);
322 } else {
323 sma->use_global_lock--;
324 }
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700325}
326
327#define SEM_GLOBAL_LOCK (-1)
328/*
Rik van Riel6062a8d2013-04-30 19:15:44 -0700329 * If the request contains only one semaphore operation, and there are
330 * no complex transactions pending, lock only the semaphore involved.
331 * Otherwise, lock the entire semaphore array, since we either have
332 * multiple semaphores in our own semops, or we need to look at
333 * semaphores from other pending complex operations.
Rik van Riel6062a8d2013-04-30 19:15:44 -0700334 */
335static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
336 int nsops)
337{
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700338 struct sem *sem;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700339
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700340 if (nsops != 1) {
341 /* Complex operation - acquire a full lock */
342 ipc_lock_object(&sma->sem_perm);
343
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700344 /* Prevent parallel simple ops */
345 complexmode_enter(sma);
346 return SEM_GLOBAL_LOCK;
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700347 }
348
349 /*
350 * Only one semaphore affected - try to optimize locking.
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700351 * Optimized locking is possible if no complex operation
352 * is either enqueued or processed right now.
353 *
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800354 * Both facts are tracked by use_global_mode.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700355 */
Manfred Spraul1a233952017-07-12 14:34:38 -0700356 sem = &sma->sems[sops->sem_num];
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700357
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700358 /*
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800359 * Initial check for use_global_lock. Just an optimization,
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700360 * no locking, no memory barrier.
361 */
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800362 if (!sma->use_global_lock) {
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700363 /*
364 * It appears that no complex operation is around.
365 * Acquire the per-semaphore lock.
366 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700367 spin_lock(&sem->lock);
368
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800369 /* pairs with smp_store_release() */
370 if (!smp_load_acquire(&sma->use_global_lock)) {
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700371 /* fast path successful! */
372 return sops->sem_num;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700373 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700374 spin_unlock(&sem->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700375 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700376
377 /* slow path: acquire the full lock */
378 ipc_lock_object(&sma->sem_perm);
379
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800380 if (sma->use_global_lock == 0) {
381 /*
382 * The use_global_lock mode ended while we waited for
383 * sma->sem_perm.lock. Thus we must switch to locking
384 * with sem->lock.
385 * Unlike in the fast path, there is no need to recheck
386 * sma->use_global_lock after we have acquired sem->lock:
387 * We own sma->sem_perm.lock, thus use_global_lock cannot
388 * change.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700389 */
390 spin_lock(&sem->lock);
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800391
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700392 ipc_unlock_object(&sma->sem_perm);
393 return sops->sem_num;
394 } else {
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800395 /*
396 * Not a false alarm, thus continue to use the global lock
397 * mode. No need for complexmode_enter(), this was done by
398 * the caller that has set use_global_mode to non-zero.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700399 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700400 return SEM_GLOBAL_LOCK;
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700401 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700402}
403
404static inline void sem_unlock(struct sem_array *sma, int locknum)
405{
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700406 if (locknum == SEM_GLOBAL_LOCK) {
Manfred Spraulf269f402013-07-08 16:01:24 -0700407 unmerge_queues(sma);
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700408 complexmode_tryleave(sma);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700409 ipc_unlock_object(&sma->sem_perm);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700410 } else {
Manfred Spraul1a233952017-07-12 14:34:38 -0700411 struct sem *sem = &sma->sems[locknum];
Rik van Riel6062a8d2013-04-30 19:15:44 -0700412 spin_unlock(&sem->lock);
413 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700414}
415
416/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700417 * sem_lock_(check_) routines are called in the paths where the rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -0700418 * is not held.
Linus Torvalds321310c2013-05-04 10:47:57 -0700419 *
420 * The caller holds the RCU read lock.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700421 */
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700422static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
423{
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700424 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&sem_ids(ns), id);
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700425
426 if (IS_ERR(ipcp))
427 return ERR_CAST(ipcp);
428
429 return container_of(ipcp, struct sem_array, sem_perm);
430}
431
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700432static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
433 int id)
434{
435 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
436
437 if (IS_ERR(ipcp))
438 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800439
Nadia Derbey03f02c72007-10-18 23:40:51 -0700440 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700441}
442
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700443static inline void sem_lock_and_putref(struct sem_array *sma)
444{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700445 sem_lock(sma, NULL, -1);
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700446 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700447}
448
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700449static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
450{
451 ipc_rmid(&sem_ids(ns), &s->sem_perm);
452}
453
Kees Cook101ede02017-07-12 14:35:02 -0700454static struct sem_array *sem_alloc(size_t nsems)
455{
456 struct sem_array *sma;
457 size_t size;
458
459 if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
460 return NULL;
461
462 size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
463 sma = kvmalloc(size, GFP_KERNEL);
464 if (unlikely(!sma))
465 return NULL;
466
467 memset(sma, 0, size);
468 atomic_set(&sma->sem_perm.refcount, 1);
469
470 return sma;
471}
472
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700473/**
474 * newary - Create a new semaphore set
475 * @ns: namespace
476 * @params: ptr to the structure that contains key, semflg and nsems
477 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700478 * Called with sem_ids.rwsem held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700479 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700480static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int retval;
483 struct sem_array *sma;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700484 key_t key = params->key;
485 int nsems = params->u.nsems;
486 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800487 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (!nsems)
490 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700491 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -ENOSPC;
493
Kees Cook101ede02017-07-12 14:35:02 -0700494 sma = sem_alloc(nsems);
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800495 if (!sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return -ENOMEM;
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 sma->sem_perm.mode = (semflg & S_IRWXUGO);
499 sma->sem_perm.key = key;
500
501 sma->sem_perm.security = NULL;
502 retval = security_sem_alloc(sma);
503 if (retval) {
Kees Cook1b4654e2017-07-12 14:34:50 -0700504 __sem_free(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return retval;
506 }
507
Rik van Riel6062a8d2013-04-30 19:15:44 -0700508 for (i = 0; i < nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700509 INIT_LIST_HEAD(&sma->sems[i].pending_alter);
510 INIT_LIST_HEAD(&sma->sems[i].pending_const);
511 spin_lock_init(&sma->sems[i].lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700512 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800513
514 sma->complex_count = 0;
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800515 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700516 INIT_LIST_HEAD(&sma->pending_alter);
517 INIT_LIST_HEAD(&sma->pending_const);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700518 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 sma->sem_nsems = nsems;
520 sma->sem_ctime = get_seconds();
Manfred Spraule8577d12014-12-02 15:59:34 -0800521
Manfred Spraul2ec55f82017-07-12 14:35:13 -0700522 retval = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
523 if (retval < 0) {
524 call_rcu(&sma->sem_perm.rcu, sem_rcu_free);
525 return retval;
Manfred Spraule8577d12014-12-02 15:59:34 -0800526 }
527 ns->used_sems += nsems;
528
Rik van Riel6062a8d2013-04-30 19:15:44 -0700529 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -0700530 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700532 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700535
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700536/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700537 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700538 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700539static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700541 struct sem_array *sma;
542
543 sma = container_of(ipcp, struct sem_array, sem_perm);
544 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700545}
546
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700547/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700548 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700549 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700550static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
551 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700552{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700553 struct sem_array *sma;
554
555 sma = container_of(ipcp, struct sem_array, sem_perm);
556 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700557 return -EINVAL;
558
559 return 0;
560}
561
Heiko Carstensd5460c92009-01-14 14:14:27 +0100562SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700563{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700564 struct ipc_namespace *ns;
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700565 static const struct ipc_ops sem_ops = {
566 .getnew = newary,
567 .associate = sem_security,
568 .more_checks = sem_more_checks,
569 };
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700570 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Kirill Korotaeve3893532006-10-02 02:18:22 -0700572 ns = current->nsproxy->ipc_ns;
573
574 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700576
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700577 sem_params.key = key;
578 sem_params.flg = semflg;
579 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700580
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700581 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Petr Mladek78f50092014-01-27 17:07:00 -0800584/**
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800585 * perform_atomic_semop[_slow] - Attempt to perform semaphore
586 * operations on a given array.
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700587 * @sma: semaphore array
Manfred Sprauld198cd62014-06-06 14:37:49 -0700588 * @q: struct sem_queue that describes the operation
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700589 *
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800590 * Caller blocking are as follows, based the value
591 * indicated by the semaphore operation (sem_op):
592 *
593 * (1) >0 never blocks.
594 * (2) 0 (wait-for-zero operation): semval is non-zero.
595 * (3) <0 attempting to decrement semval to a value smaller than zero.
596 *
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700597 * Returns 0 if the operation was possible.
598 * Returns 1 if the operation is impossible, the caller must sleep.
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800599 * Returns <0 for error codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800601static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Manfred Sprauld198cd62014-06-06 14:37:49 -0700603 int result, sem_op, nsops, pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct sembuf *sop;
Manfred Spraul239521f2014-01-27 17:07:04 -0800605 struct sem *curr;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700606 struct sembuf *sops;
607 struct sem_undo *un;
608
609 sops = q->sops;
610 nsops = q->nsops;
611 un = q->undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 for (sop = sops; sop < sops + nsops; sop++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700614 curr = &sma->sems[sop->sem_num];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 sem_op = sop->sem_op;
616 result = curr->semval;
Petr Mladek78f50092014-01-27 17:07:00 -0800617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (!sem_op && result)
619 goto would_block;
620
621 result += sem_op;
622 if (result < 0)
623 goto would_block;
624 if (result > SEMVMX)
625 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (sop->sem_flg & SEM_UNDO) {
628 int undo = un->semadj[sop->sem_num] - sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800629 /* Exceeding the undo range is an error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
631 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800632 un->semadj[sop->sem_num] = undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Petr Mladek78f50092014-01-27 17:07:00 -0800634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 curr->semval = result;
636 }
637
638 sop--;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700639 pid = q->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 while (sop >= sops) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700641 sma->sems[sop->sem_num].sempid = pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 sop--;
643 }
Petr Mladek78f50092014-01-27 17:07:00 -0800644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
646
647out_of_range:
648 result = -ERANGE;
649 goto undo;
650
651would_block:
Manfred Sprauled247b72014-06-06 14:37:49 -0700652 q->blocking = sop;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (sop->sem_flg & IPC_NOWAIT)
655 result = -EAGAIN;
656 else
657 result = 1;
658
659undo:
660 sop--;
661 while (sop >= sops) {
Petr Mladek78f50092014-01-27 17:07:00 -0800662 sem_op = sop->sem_op;
Manfred Spraul1a233952017-07-12 14:34:38 -0700663 sma->sems[sop->sem_num].semval -= sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800664 if (sop->sem_flg & SEM_UNDO)
665 un->semadj[sop->sem_num] += sem_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 sop--;
667 }
668
669 return result;
670}
671
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800672static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
673{
674 int result, sem_op, nsops;
675 struct sembuf *sop;
676 struct sem *curr;
677 struct sembuf *sops;
678 struct sem_undo *un;
679
680 sops = q->sops;
681 nsops = q->nsops;
682 un = q->undo;
683
684 if (unlikely(q->dupsop))
685 return perform_atomic_semop_slow(sma, q);
686
687 /*
688 * We scan the semaphore set twice, first to ensure that the entire
689 * operation can succeed, therefore avoiding any pointless writes
690 * to shared memory and having to undo such changes in order to block
691 * until the operations can go through.
692 */
693 for (sop = sops; sop < sops + nsops; sop++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700694 curr = &sma->sems[sop->sem_num];
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800695 sem_op = sop->sem_op;
696 result = curr->semval;
697
698 if (!sem_op && result)
699 goto would_block; /* wait-for-zero */
700
701 result += sem_op;
702 if (result < 0)
703 goto would_block;
704
705 if (result > SEMVMX)
706 return -ERANGE;
707
708 if (sop->sem_flg & SEM_UNDO) {
709 int undo = un->semadj[sop->sem_num] - sem_op;
710
711 /* Exceeding the undo range is an error. */
712 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
713 return -ERANGE;
714 }
715 }
716
717 for (sop = sops; sop < sops + nsops; sop++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700718 curr = &sma->sems[sop->sem_num];
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800719 sem_op = sop->sem_op;
720 result = curr->semval;
721
722 if (sop->sem_flg & SEM_UNDO) {
723 int undo = un->semadj[sop->sem_num] - sem_op;
724
725 un->semadj[sop->sem_num] = undo;
726 }
727 curr->semval += sem_op;
728 curr->sempid = q->pid;
729 }
730
731 return 0;
732
733would_block:
734 q->blocking = sop;
735 return sop->sem_flg & IPC_NOWAIT ? -EAGAIN : 1;
736}
737
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800738static inline void wake_up_sem_queue_prepare(struct sem_queue *q, int error,
739 struct wake_q_head *wake_q)
Nick Piggind4212092009-12-15 16:47:30 -0800740{
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800741 wake_q_add(wake_q, q->sleeper);
742 /*
743 * Rely on the above implicit barrier, such that we can
744 * ensure that we hold reference to the task before setting
745 * q->status. Otherwise we could race with do_exit if the
746 * task is awoken by an external event before calling
747 * wake_up_process().
748 */
749 WRITE_ONCE(q->status, error);
Nick Piggind4212092009-12-15 16:47:30 -0800750}
751
Manfred Spraulb97e8202009-12-15 16:47:32 -0800752static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
753{
754 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700755 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800756 sma->complex_count--;
757}
758
Manfred Spraulfd5db422010-05-26 14:43:40 -0700759/** check_restart(sma, q)
760 * @sma: semaphore array
761 * @q: the operation that just completed
762 *
763 * update_queue is O(N^2) when it restarts scanning the whole queue of
764 * waiting operations. Therefore this function checks if the restart is
765 * really necessary. It is called after a previously waiting operation
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700766 * modified the array.
767 * Note that wait-for-zero operations are handled without restart.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700768 */
Davidlohr Bueso4663d3e2016-12-14 15:06:40 -0800769static inline int check_restart(struct sem_array *sma, struct sem_queue *q)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700770{
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700771 /* pending complex alter operations are too difficult to analyse */
772 if (!list_empty(&sma->pending_alter))
Manfred Spraulfd5db422010-05-26 14:43:40 -0700773 return 1;
774
775 /* we were a sleeping complex operation. Too difficult */
776 if (q->nsops > 1)
777 return 1;
778
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700779 /* It is impossible that someone waits for the new value:
780 * - complex operations always restart.
781 * - wait-for-zero are handled seperately.
782 * - q is a previously sleeping simple operation that
783 * altered the array. It must be a decrement, because
784 * simple increments never sleep.
785 * - If there are older (higher priority) decrements
786 * in the queue, then they have observed the original
787 * semval value and couldn't proceed. The operation
788 * decremented to value - thus they won't proceed either.
789 */
790 return 0;
791}
Manfred Spraulfd5db422010-05-26 14:43:40 -0700792
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700793/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800794 * wake_const_ops - wake up non-alter tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700795 * @sma: semaphore array.
796 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800797 * @wake_q: lockless wake-queue head.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700798 *
799 * wake_const_ops must be called after a semaphore in a semaphore array
800 * was set to 0. If complex const operations are pending, wake_const_ops must
801 * be called with semnum = -1, as well as with the number of each modified
802 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800803 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700804 * is stored in q->pid.
805 * The function returns 1 if at least one operation was completed successfully.
806 */
807static int wake_const_ops(struct sem_array *sma, int semnum,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800808 struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700809{
Davidlohr Buesof150f022016-12-14 15:06:43 -0800810 struct sem_queue *q, *tmp;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700811 struct list_head *pending_list;
812 int semop_completed = 0;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700813
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700814 if (semnum == -1)
815 pending_list = &sma->pending_const;
816 else
Manfred Spraul1a233952017-07-12 14:34:38 -0700817 pending_list = &sma->sems[semnum].pending_const;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700818
Davidlohr Buesof150f022016-12-14 15:06:43 -0800819 list_for_each_entry_safe(q, tmp, pending_list, list) {
820 int error = perform_atomic_semop(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700821
Davidlohr Buesof150f022016-12-14 15:06:43 -0800822 if (error > 0)
823 continue;
824 /* operation completed, remove from queue & wakeup */
825 unlink_queue(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700826
Davidlohr Buesof150f022016-12-14 15:06:43 -0800827 wake_up_sem_queue_prepare(q, error, wake_q);
828 if (error == 0)
829 semop_completed = 1;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700830 }
Davidlohr Buesof150f022016-12-14 15:06:43 -0800831
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700832 return semop_completed;
833}
834
835/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800836 * do_smart_wakeup_zero - wakeup all wait for zero tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700837 * @sma: semaphore array
838 * @sops: operations that were performed
839 * @nsops: number of operations
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800840 * @wake_q: lockless wake-queue head
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700841 *
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800842 * Checks all required queue for wait-for-zero operations, based
843 * on the actual changes that were performed on the semaphore array.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700844 * The function returns 1 if at least one operation was completed successfully.
845 */
846static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800847 int nsops, struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700848{
849 int i;
850 int semop_completed = 0;
851 int got_zero = 0;
852
853 /* first: the per-semaphore queues, if known */
854 if (sops) {
855 for (i = 0; i < nsops; i++) {
856 int num = sops[i].sem_num;
857
Manfred Spraul1a233952017-07-12 14:34:38 -0700858 if (sma->sems[num].semval == 0) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700859 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800860 semop_completed |= wake_const_ops(sma, num, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700861 }
862 }
863 } else {
864 /*
865 * No sops means modified semaphores not known.
866 * Assume all were changed.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700867 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700868 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700869 if (sma->sems[i].semval == 0) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700870 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800871 semop_completed |= wake_const_ops(sma, i, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700872 }
873 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700874 }
875 /*
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700876 * If one of the modified semaphores got 0,
877 * then check the global queue, too.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700878 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700879 if (got_zero)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800880 semop_completed |= wake_const_ops(sma, -1, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700881
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700882 return semop_completed;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700883}
884
Manfred Spraul636c6be2009-12-15 16:47:33 -0800885
886/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800887 * update_queue - look for tasks that can be completed.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800888 * @sma: semaphore array.
889 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800890 * @wake_q: lockless wake-queue head.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800891 *
892 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700893 * was modified. If multiple semaphores were modified, update_queue must
894 * be called with semnum = -1, as well as with the number of each modified
895 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800896 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700897 * is stored in q->pid.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700898 * The function internally checks if const operations can now succeed.
899 *
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700900 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800902static int update_queue(struct sem_array *sma, int semnum, struct wake_q_head *wake_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Davidlohr Buesof150f022016-12-14 15:06:43 -0800904 struct sem_queue *q, *tmp;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800905 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700906 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800907
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700908 if (semnum == -1)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700909 pending_list = &sma->pending_alter;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700910 else
Manfred Spraul1a233952017-07-12 14:34:38 -0700911 pending_list = &sma->sems[semnum].pending_alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Nick Piggin9cad2002009-12-15 16:47:29 -0800913again:
Davidlohr Buesof150f022016-12-14 15:06:43 -0800914 list_for_each_entry_safe(q, tmp, pending_list, list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700915 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800916
Manfred Sprauld987f8b22009-12-15 16:47:34 -0800917 /* If we are scanning the single sop, per-semaphore list of
918 * one semaphore and that semaphore is 0, then it is not
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700919 * necessary to scan further: simple increments
Manfred Sprauld987f8b22009-12-15 16:47:34 -0800920 * that affect only one entry succeed immediately and cannot
921 * be in the per semaphore pending queue, and decrements
922 * cannot be successful if the value is already 0.
923 */
Manfred Spraul1a233952017-07-12 14:34:38 -0700924 if (semnum != -1 && sma->sems[semnum].semval == 0)
Manfred Sprauld987f8b22009-12-15 16:47:34 -0800925 break;
926
Manfred Sprauld198cd62014-06-06 14:37:49 -0700927 error = perform_atomic_semop(sma, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800930 if (error > 0)
931 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700932
Manfred Spraulb97e8202009-12-15 16:47:32 -0800933 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700934
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700935 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700936 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700937 } else {
938 semop_completed = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800939 do_smart_wakeup_zero(sma, q->sops, q->nsops, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700940 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700941 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700942
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800943 wake_up_sem_queue_prepare(q, error, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700944 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800945 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700947 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700950/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800951 * set_semotime - set sem_otime
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700952 * @sma: semaphore array
953 * @sops: operations that modified the array, may be NULL
954 *
955 * sem_otime is replicated to avoid cache line trashing.
956 * This function sets one instance to the current time.
957 */
958static void set_semotime(struct sem_array *sma, struct sembuf *sops)
959{
960 if (sops == NULL) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700961 sma->sems[0].sem_otime = get_seconds();
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700962 } else {
Manfred Spraul1a233952017-07-12 14:34:38 -0700963 sma->sems[sops[0].sem_num].sem_otime =
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700964 get_seconds();
965 }
966}
967
968/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800969 * do_smart_update - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700970 * @sma: semaphore array
971 * @sops: operations that were performed
972 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700973 * @otime: force setting otime
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800974 * @wake_q: lockless wake-queue head
Manfred Spraulfd5db422010-05-26 14:43:40 -0700975 *
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700976 * do_smart_update() does the required calls to update_queue and wakeup_zero,
977 * based on the actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700978 * Note that the function does not do the actual wake-up: the caller is
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800979 * responsible for calling wake_up_q().
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700980 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700981 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700982static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800983 int otime, struct wake_q_head *wake_q)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700984{
985 int i;
986
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800987 otime |= do_smart_wakeup_zero(sma, sops, nsops, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700988
Manfred Spraulf269f402013-07-08 16:01:24 -0700989 if (!list_empty(&sma->pending_alter)) {
990 /* semaphore array uses the global queue - just process it. */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800991 otime |= update_queue(sma, -1, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -0700992 } else {
993 if (!sops) {
994 /*
995 * No sops, thus the modified semaphores are not
996 * known. Check all.
997 */
998 for (i = 0; i < sma->sem_nsems; i++)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800999 otime |= update_queue(sma, i, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -07001000 } else {
1001 /*
1002 * Check the semaphores that were increased:
1003 * - No complex ops, thus all sleeping ops are
1004 * decrease.
1005 * - if we decreased the value, then any sleeping
1006 * semaphore ops wont be able to run: If the
1007 * previous value was too small, then the new
1008 * value will be too small, too.
1009 */
1010 for (i = 0; i < nsops; i++) {
1011 if (sops[i].sem_op > 0) {
1012 otime |= update_queue(sma,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001013 sops[i].sem_num, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -07001014 }
Manfred Spraulab465df2013-05-26 11:08:52 +02001015 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001016 }
Manfred Spraulfd5db422010-05-26 14:43:40 -07001017 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001018 if (otime)
1019 set_semotime(sma, sops);
Manfred Spraulfd5db422010-05-26 14:43:40 -07001020}
1021
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001022/*
Manfred Spraulb220c572014-06-06 14:37:51 -07001023 * check_qop: Test if a queued operation sleeps on the semaphore semnum
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001024 */
1025static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
1026 bool count_zero)
1027{
Manfred Spraulb220c572014-06-06 14:37:51 -07001028 struct sembuf *sop = q->blocking;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001029
Manfred Spraul9b44ee22014-06-06 14:37:52 -07001030 /*
1031 * Linux always (since 0.99.10) reported a task as sleeping on all
1032 * semaphores. This violates SUS, therefore it was changed to the
1033 * standard compliant behavior.
1034 * Give the administrators a chance to notice that an application
1035 * might misbehave because it relies on the Linux behavior.
1036 */
1037 pr_info_once("semctl(GETNCNT/GETZCNT) is since 3.16 Single Unix Specification compliant.\n"
1038 "The task %s (%d) triggered the difference, watch for misbehavior.\n",
1039 current->comm, task_pid_nr(current));
1040
Manfred Spraulb220c572014-06-06 14:37:51 -07001041 if (sop->sem_num != semnum)
1042 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001043
Manfred Spraulb220c572014-06-06 14:37:51 -07001044 if (count_zero && sop->sem_op == 0)
1045 return 1;
1046 if (!count_zero && sop->sem_op < 0)
1047 return 1;
1048
1049 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/* The following counts are associated to each semaphore:
1053 * semncnt number of tasks waiting on semval being nonzero
1054 * semzcnt number of tasks waiting on semval being zero
Manfred Spraulb220c572014-06-06 14:37:51 -07001055 *
1056 * Per definition, a task waits only on the semaphore of the first semop
1057 * that cannot proceed, even if additional operation would block, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001059static int count_semcnt(struct sem_array *sma, ushort semnum,
1060 bool count_zero)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001062 struct list_head *l;
Manfred Spraul239521f2014-01-27 17:07:04 -08001063 struct sem_queue *q;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001064 int semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001066 semcnt = 0;
1067 /* First: check the simple operations. They are easy to evaluate */
1068 if (count_zero)
Manfred Spraul1a233952017-07-12 14:34:38 -07001069 l = &sma->sems[semnum].pending_const;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001070 else
Manfred Spraul1a233952017-07-12 14:34:38 -07001071 l = &sma->sems[semnum].pending_alter;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001072
1073 list_for_each_entry(q, l, list) {
1074 /* all task on a per-semaphore list sleep on exactly
1075 * that semaphore
1076 */
1077 semcnt++;
Rik van Rielde2657f2013-05-09 16:59:59 -04001078 }
1079
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001080 /* Then: check the complex operations. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001081 list_for_each_entry(q, &sma->pending_alter, list) {
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001082 semcnt += check_qop(sma, semnum, q, count_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001084 if (count_zero) {
1085 list_for_each_entry(q, &sma->pending_const, list) {
1086 semcnt += check_qop(sma, semnum, q, count_zero);
1087 }
Rik van Rielebc2e5e2013-05-09 16:53:28 -04001088 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001089 return semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001092/* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1093 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -07001094 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001096static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Manfred Spraul380af1b2008-07-25 01:48:06 -07001098 struct sem_undo *un, *tu;
1099 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001100 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001101 int i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001102 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Manfred Spraul380af1b2008-07-25 01:48:06 -07001104 /* Free the existing undo structures for this semaphore set. */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001105 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001106 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1107 list_del(&un->list_id);
1108 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001110 list_del_rcu(&un->list_proc);
1111 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001112 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001116 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1117 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001118 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001119 }
1120
1121 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -08001122 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001123 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001125 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07001126 struct sem *sem = &sma->sems[i];
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001127 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1128 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001129 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001130 }
1131 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001132 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001133 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001134 }
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001137 /* Remove the semaphore set from the IDR */
1138 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001139 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001140 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001142 wake_up_q(&wake_q);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001143 ns->used_sems -= sma->sem_nsems;
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001144 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
1147static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1148{
Manfred Spraul239521f2014-01-27 17:07:04 -08001149 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 case IPC_64:
1151 return copy_to_user(buf, in, sizeof(*in));
1152 case IPC_OLD:
1153 {
1154 struct semid_ds out;
1155
Dan Rosenberg982f7c22010-09-30 15:15:31 -07001156 memset(&out, 0, sizeof(out));
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1159
1160 out.sem_otime = in->sem_otime;
1161 out.sem_ctime = in->sem_ctime;
1162 out.sem_nsems = in->sem_nsems;
1163
1164 return copy_to_user(buf, &out, sizeof(out));
1165 }
1166 default:
1167 return -EINVAL;
1168 }
1169}
1170
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001171static time_t get_semotime(struct sem_array *sma)
1172{
1173 int i;
1174 time_t res;
1175
Manfred Spraul1a233952017-07-12 14:34:38 -07001176 res = sma->sems[0].sem_otime;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001177 for (i = 1; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07001178 time_t to = sma->sems[i].sem_otime;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001179
1180 if (to > res)
1181 res = to;
1182 }
1183 return res;
1184}
1185
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001186static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001187 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Amerigo Wange5cc9c72009-12-15 16:47:35 -08001189 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 struct sem_array *sma;
1191
Manfred Spraul239521f2014-01-27 17:07:04 -08001192 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 case IPC_INFO:
1194 case SEM_INFO:
1195 {
1196 struct seminfo seminfo;
1197 int max_id;
1198
1199 err = security_sem_semctl(NULL, cmd);
1200 if (err)
1201 return err;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001202
Manfred Spraul239521f2014-01-27 17:07:04 -08001203 memset(&seminfo, 0, sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -07001204 seminfo.semmni = ns->sc_semmni;
1205 seminfo.semmns = ns->sc_semmns;
1206 seminfo.semmsl = ns->sc_semmsl;
1207 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 seminfo.semvmx = SEMVMX;
1209 seminfo.semmnu = SEMMNU;
1210 seminfo.semmap = SEMMAP;
1211 seminfo.semume = SEMUME;
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001212 down_read(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001214 seminfo.semusz = sem_ids(ns).in_use;
1215 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 } else {
1217 seminfo.semusz = SEMUSZ;
1218 seminfo.semaem = SEMAEM;
1219 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001220 max_id = ipc_get_maxid(&sem_ids(ns));
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001221 up_read(&sem_ids(ns).rwsem);
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001222 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return -EFAULT;
Manfred Spraul239521f2014-01-27 17:07:04 -08001224 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001226 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 case SEM_STAT:
1228 {
1229 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001230 int id = 0;
1231
1232 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Linus Torvalds941b0302013-05-04 11:04:29 -07001234 rcu_read_lock();
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001235 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001236 sma = sem_obtain_object(ns, semid);
1237 if (IS_ERR(sma)) {
1238 err = PTR_ERR(sma);
1239 goto out_unlock;
1240 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001241 id = sma->sem_perm.id;
1242 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001243 sma = sem_obtain_object_check(ns, semid);
1244 if (IS_ERR(sma)) {
1245 err = PTR_ERR(sma);
1246 goto out_unlock;
1247 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001251 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 goto out_unlock;
1253
1254 err = security_sem_semctl(sma, cmd);
1255 if (err)
1256 goto out_unlock;
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001259 tbuf.sem_otime = get_semotime(sma);
1260 tbuf.sem_ctime = sma->sem_ctime;
1261 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001262 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001263 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return -EFAULT;
1265 return id;
1266 }
1267 default:
1268 return -EINVAL;
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001271 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return err;
1273}
1274
Al Viroe1fd1f42013-03-05 15:04:55 -05001275static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1276 unsigned long arg)
1277{
1278 struct sem_undo *un;
1279 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001280 struct sem *curr;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001281 int err, val;
1282 DEFINE_WAKE_Q(wake_q);
1283
Al Viroe1fd1f42013-03-05 15:04:55 -05001284#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1285 /* big-endian 64bit */
1286 val = arg >> 32;
1287#else
1288 /* 32bit or little-endian 64bit */
1289 val = arg;
1290#endif
1291
Rik van Riel6062a8d2013-04-30 19:15:44 -07001292 if (val > SEMVMX || val < 0)
1293 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001294
Rik van Riel6062a8d2013-04-30 19:15:44 -07001295 rcu_read_lock();
1296 sma = sem_obtain_object_check(ns, semid);
1297 if (IS_ERR(sma)) {
1298 rcu_read_unlock();
1299 return PTR_ERR(sma);
1300 }
1301
1302 if (semnum < 0 || semnum >= sma->sem_nsems) {
1303 rcu_read_unlock();
1304 return -EINVAL;
1305 }
1306
1307
1308 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1309 rcu_read_unlock();
1310 return -EACCES;
1311 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001312
1313 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001314 if (err) {
1315 rcu_read_unlock();
1316 return -EACCES;
1317 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001318
Rik van Riel6062a8d2013-04-30 19:15:44 -07001319 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001320
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001321 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001322 sem_unlock(sma, -1);
1323 rcu_read_unlock();
1324 return -EIDRM;
1325 }
1326
Manfred Spraul1a233952017-07-12 14:34:38 -07001327 curr = &sma->sems[semnum];
Al Viroe1fd1f42013-03-05 15:04:55 -05001328
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001329 ipc_assert_locked_object(&sma->sem_perm);
Al Viroe1fd1f42013-03-05 15:04:55 -05001330 list_for_each_entry(un, &sma->list_id, list_id)
1331 un->semadj[semnum] = 0;
1332
1333 curr->semval = val;
1334 curr->sempid = task_tgid_vnr(current);
1335 sma->sem_ctime = get_seconds();
1336 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001337 do_smart_update(sma, NULL, 0, 0, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001338 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001339 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001340 wake_up_q(&wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001341 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001342}
1343
Kirill Korotaeve3893532006-10-02 02:18:22 -07001344static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001345 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001348 struct sem *curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001349 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 ushort fast_sem_io[SEMMSL_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001351 ushort *sem_io = fast_sem_io;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001352 DEFINE_WAKE_Q(wake_q);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001353
1354 rcu_read_lock();
1355 sma = sem_obtain_object_check(ns, semid);
1356 if (IS_ERR(sma)) {
1357 rcu_read_unlock();
1358 return PTR_ERR(sma);
1359 }
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 nsems = sma->sem_nsems;
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 err = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001364 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1365 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 err = security_sem_semctl(sma, cmd);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001368 if (err)
1369 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 err = -EACCES;
1372 switch (cmd) {
1373 case GETALL:
1374 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001375 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 int i;
1377
Al Viroce857222013-05-03 00:30:49 +01001378 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001379 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001380 err = -EIDRM;
1381 goto out_unlock;
1382 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001383 if (nsems > SEMMSL_FAST) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001384 if (!ipc_rcu_getref(&sma->sem_perm)) {
Al Viroce857222013-05-03 00:30:49 +01001385 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001386 goto out_unlock;
Al Viroce857222013-05-03 00:30:49 +01001387 }
1388 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001389 rcu_read_unlock();
Kees Cookf8dbe8d2017-07-12 14:34:47 -07001390 sem_io = kvmalloc_array(nsems, sizeof(ushort),
1391 GFP_KERNEL);
Manfred Spraul239521f2014-01-27 17:07:04 -08001392 if (sem_io == NULL) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001393 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return -ENOMEM;
1395 }
1396
Linus Torvalds4091fd92013-05-04 10:13:40 -07001397 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001398 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001399 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001401 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
Al Viroce857222013-05-03 00:30:49 +01001403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 for (i = 0; i < sma->sem_nsems; i++)
Manfred Spraul1a233952017-07-12 14:34:38 -07001405 sem_io[i] = sma->sems[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001406 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001407 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 err = 0;
Manfred Spraul239521f2014-01-27 17:07:04 -08001409 if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 err = -EFAULT;
1411 goto out_free;
1412 }
1413 case SETALL:
1414 {
1415 int i;
1416 struct sem_undo *un;
1417
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001418 if (!ipc_rcu_getref(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001419 err = -EIDRM;
1420 goto out_rcu_wakeup;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001421 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001422 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Manfred Spraul239521f2014-01-27 17:07:04 -08001424 if (nsems > SEMMSL_FAST) {
Kees Cookf8dbe8d2017-07-12 14:34:47 -07001425 sem_io = kvmalloc_array(nsems, sizeof(ushort),
1426 GFP_KERNEL);
Manfred Spraul239521f2014-01-27 17:07:04 -08001427 if (sem_io == NULL) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001428 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -ENOMEM;
1430 }
1431 }
1432
Manfred Spraul239521f2014-01-27 17:07:04 -08001433 if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001434 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 err = -EFAULT;
1436 goto out_free;
1437 }
1438
1439 for (i = 0; i < nsems; i++) {
1440 if (sem_io[i] > SEMVMX) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001441 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 err = -ERANGE;
1443 goto out_free;
1444 }
1445 }
Linus Torvalds4091fd92013-05-04 10:13:40 -07001446 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001447 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001448 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001450 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
1452
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001453 for (i = 0; i < nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07001454 sma->sems[i].semval = sem_io[i];
1455 sma->sems[i].sempid = task_tgid_vnr(current);
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001456 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001457
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001458 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001459 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 for (i = 0; i < nsems; i++)
1461 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 sma->sem_ctime = get_seconds();
1464 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001465 do_smart_update(sma, NULL, 0, 0, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 err = 0;
1467 goto out_unlock;
1468 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001469 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471 err = -EINVAL;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001472 if (semnum < 0 || semnum >= nsems)
1473 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Rik van Riel6062a8d2013-04-30 19:15:44 -07001475 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001476 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001477 err = -EIDRM;
1478 goto out_unlock;
1479 }
Manfred Spraul1a233952017-07-12 14:34:38 -07001480 curr = &sma->sems[semnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 switch (cmd) {
1483 case GETVAL:
1484 err = curr->semval;
1485 goto out_unlock;
1486 case GETPID:
1487 err = curr->sempid;
1488 goto out_unlock;
1489 case GETNCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001490 err = count_semcnt(sma, semnum, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 goto out_unlock;
1492 case GETZCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001493 err = count_semcnt(sma, semnum, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001498 sem_unlock(sma, -1);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001499out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001500 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001501 wake_up_q(&wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08001503 if (sem_io != fast_sem_io)
Kees Cookf8dbe8d2017-07-12 14:34:47 -07001504 kvfree(sem_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return err;
1506}
1507
Pierre Peiffer016d7132008-04-29 01:00:50 -07001508static inline unsigned long
1509copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Manfred Spraul239521f2014-01-27 17:07:04 -08001511 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001513 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 case IPC_OLD:
1517 {
1518 struct semid_ds tbuf_old;
1519
Manfred Spraul239521f2014-01-27 17:07:04 -08001520 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return -EFAULT;
1522
Pierre Peiffer016d7132008-04-29 01:00:50 -07001523 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1524 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1525 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 return 0;
1528 }
1529 default:
1530 return -EINVAL;
1531 }
1532}
1533
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001534/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001535 * This function handles some semctl commands which require the rwsem
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001536 * to be held in write mode.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001537 * NOTE: no locks must be held, the rwsem is taken inside this function.
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001538 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001539static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001540 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 struct sem_array *sma;
1543 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001544 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 struct kern_ipc_perm *ipcp;
1546
Manfred Spraul239521f2014-01-27 17:07:04 -08001547 if (cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001548 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001552 down_write(&sem_ids(ns).rwsem);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001553 rcu_read_lock();
1554
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001555 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1556 &semid64.sem_perm, 0);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001557 if (IS_ERR(ipcp)) {
1558 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001559 goto out_unlock1;
1560 }
Steve Grubb073115d2006-04-02 17:07:33 -04001561
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001562 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001565 if (err)
1566 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001568 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001570 sem_lock(sma, NULL, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001571 /* freeary unlocks the ipc object and rcu */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001572 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001573 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001575 sem_lock(sma, NULL, -1);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001576 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1577 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001578 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 break;
1581 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 err = -EINVAL;
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001583 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001586out_unlock0:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001587 sem_unlock(sma, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001588out_unlock1:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001589 rcu_read_unlock();
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001590out_up:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001591 up_write(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return err;
1593}
1594
Al Viroe1fd1f42013-03-05 15:04:55 -05001595SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001598 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001599 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 if (semid < 0)
1602 return -EINVAL;
1603
1604 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001605 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Manfred Spraul239521f2014-01-27 17:07:04 -08001607 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 case IPC_INFO:
1609 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001610 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001612 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 case GETALL:
1614 case GETVAL:
1615 case GETPID:
1616 case GETNCNT:
1617 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001619 return semctl_main(ns, semid, semnum, cmd, p);
1620 case SETVAL:
1621 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 case IPC_RMID:
1623 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001624 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 default:
1626 return -EINVAL;
1627 }
1628}
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630/* If the task doesn't already have a undo_list, then allocate one
1631 * here. We guarantee there is only one thread using this undo list,
1632 * and current is THE ONE
1633 *
1634 * If this allocation and assignment succeeds, but later
1635 * portions of this code fail, there is no need to free the sem_undo_list.
1636 * Just let it stay associated with the task, and it'll be freed later
1637 * at exit time.
1638 *
1639 * This can block, so callers must hold no locks.
1640 */
1641static inline int get_undo_list(struct sem_undo_list **undo_listp)
1642{
1643 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 undo_list = current->sysvsem.undo_list;
1646 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001647 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (undo_list == NULL)
1649 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001650 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001652 INIT_LIST_HEAD(&undo_list->list_proc);
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 current->sysvsem.undo_list = undo_list;
1655 }
1656 *undo_listp = undo_list;
1657 return 0;
1658}
1659
Nick Pigginbf17bb72009-12-15 16:47:28 -08001660static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001662 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Nick Pigginbf17bb72009-12-15 16:47:28 -08001664 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1665 if (un->semid == semid)
1666 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001668 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Nick Pigginbf17bb72009-12-15 16:47:28 -08001671static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1672{
1673 struct sem_undo *un;
1674
Manfred Spraul239521f2014-01-27 17:07:04 -08001675 assert_spin_locked(&ulp->lock);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001676
1677 un = __lookup_undo(ulp, semid);
1678 if (un) {
1679 list_del_rcu(&un->list_proc);
1680 list_add_rcu(&un->list_proc, &ulp->list_proc);
1681 }
1682 return un;
1683}
1684
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001685/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001686 * find_alloc_undo - lookup (and if not present create) undo array
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001687 * @ns: namespace
1688 * @semid: semaphore array id
1689 *
1690 * The function looks up (and if not present creates) the undo structure.
1691 * The size of the undo structure depends on the size of the semaphore
1692 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001693 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1694 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001695 */
1696static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 struct sem_array *sma;
1699 struct sem_undo_list *ulp;
1700 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001701 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 error = get_undo_list(&ulp);
1704 if (error)
1705 return ERR_PTR(error);
1706
Manfred Spraul380af1b2008-07-25 01:48:06 -07001707 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001708 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001710 spin_unlock(&ulp->lock);
Manfred Spraul239521f2014-01-27 17:07:04 -08001711 if (likely(un != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 goto out;
1713
1714 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001715 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001716 sma = sem_obtain_object_check(ns, semid);
1717 if (IS_ERR(sma)) {
1718 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001719 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001720 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 nsems = sma->sem_nsems;
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001723 if (!ipc_rcu_getref(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001724 rcu_read_unlock();
1725 un = ERR_PTR(-EIDRM);
1726 goto out;
1727 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001728 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001730 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001731 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (!new) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001733 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return ERR_PTR(-ENOMEM);
1735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Manfred Spraul380af1b2008-07-25 01:48:06 -07001737 /* step 3: Acquire the lock on semaphore array */
Linus Torvalds4091fd92013-05-04 10:13:40 -07001738 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001739 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001740 if (!ipc_valid_object(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001741 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001742 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 kfree(new);
1744 un = ERR_PTR(-EIDRM);
1745 goto out;
1746 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001747 spin_lock(&ulp->lock);
1748
1749 /*
1750 * step 4: check for races: did someone else allocate the undo struct?
1751 */
1752 un = lookup_undo(ulp, semid);
1753 if (un) {
1754 kfree(new);
1755 goto success;
1756 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001757 /* step 5: initialize & link new undo structure */
1758 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001759 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001760 new->semid = semid;
1761 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001762 list_add_rcu(&new->list_proc, &ulp->list_proc);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001763 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001764 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001765 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001766
1767success:
1768 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001769 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770out:
1771 return un;
1772}
1773
Heiko Carstensd5460c92009-01-14 14:14:27 +01001774SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1775 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 int error = -EINVAL;
1778 struct sem_array *sma;
1779 struct sembuf fast_sops[SEMOPM_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001780 struct sembuf *sops = fast_sops, *sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 struct sem_undo *un;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001782 int max, locknum;
1783 bool undos = false, alter = false, dupsop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 struct sem_queue queue;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001785 unsigned long dup = 0, jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001786 struct ipc_namespace *ns;
1787
1788 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 if (nsops < 1 || semid < 0)
1791 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001792 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return -E2BIG;
Manfred Spraul239521f2014-01-27 17:07:04 -08001794 if (nsops > SEMOPM_FAST) {
1795 sops = kmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
1796 if (sops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return -ENOMEM;
1798 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001799
Manfred Spraul239521f2014-01-27 17:07:04 -08001800 if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
1801 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 goto out_free;
1803 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (timeout) {
1806 struct timespec _timeout;
1807 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1808 error = -EFAULT;
1809 goto out_free;
1810 }
1811 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1812 _timeout.tv_nsec >= 1000000000L) {
1813 error = -EINVAL;
1814 goto out_free;
1815 }
1816 jiffies_left = timespec_to_jiffies(&_timeout);
1817 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 max = 0;
1820 for (sop = sops; sop < sops + nsops; sop++) {
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001821 unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (sop->sem_num >= max)
1824 max = sop->sem_num;
1825 if (sop->sem_flg & SEM_UNDO)
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001826 undos = true;
1827 if (dup & mask) {
1828 /*
1829 * There was a previous alter access that appears
1830 * to have accessed the same semaphore, thus use
1831 * the dupsop logic. "appears", because the detection
1832 * can only check % BITS_PER_LONG.
1833 */
1834 dupsop = true;
1835 }
1836 if (sop->sem_op != 0) {
1837 alter = true;
1838 dup |= mask;
1839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001843 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001844 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (IS_ERR(un)) {
1846 error = PTR_ERR(un);
1847 goto out_free;
1848 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001849 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001851 rcu_read_lock();
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001854 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001855 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001856 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001857 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001859 }
1860
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001861 error = -EFBIG;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001862 if (max >= sma->sem_nsems) {
1863 rcu_read_unlock();
1864 goto out_free;
1865 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001866
1867 error = -EACCES;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001868 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
1869 rcu_read_unlock();
1870 goto out_free;
1871 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001872
1873 error = security_sem_semop(sma, sops, nsops, alter);
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001874 if (error) {
1875 rcu_read_unlock();
1876 goto out_free;
1877 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001878
Manfred Spraul6e224f92013-10-16 13:46:45 -07001879 error = -EIDRM;
1880 locknum = sem_lock(sma, sops, nsops);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001881 /*
1882 * We eventually might perform the following check in a lockless
1883 * fashion, considering ipc_valid_object() locking constraints.
1884 * If nsops == 1 and there is no contention for sem_perm.lock, then
1885 * only a per-semaphore lock is held and it's OK to proceed with the
1886 * check below. More details on the fine grained locking scheme
1887 * entangled here and why it's RMID race safe on comments at sem_lock()
1888 */
1889 if (!ipc_valid_object(&sma->sem_perm))
Manfred Spraul6e224f92013-10-16 13:46:45 -07001890 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001892 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001894 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001895 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001896 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001898 if (un && un->semid == -1)
1899 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001900
Manfred Sprauld198cd62014-06-06 14:37:49 -07001901 queue.sops = sops;
1902 queue.nsops = nsops;
1903 queue.undo = un;
1904 queue.pid = task_tgid_vnr(current);
1905 queue.alter = alter;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001906 queue.dupsop = dupsop;
Manfred Sprauld198cd62014-06-06 14:37:49 -07001907
1908 error = perform_atomic_semop(sma, &queue);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001909 if (error == 0) { /* non-blocking succesfull path */
1910 DEFINE_WAKE_Q(wake_q);
1911
1912 /*
1913 * If the operation was successful, then do
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001914 * the required updates.
1915 */
1916 if (alter)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001917 do_smart_update(sma, sops, nsops, 1, &wake_q);
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001918 else
1919 set_semotime(sma, sops);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001920
1921 sem_unlock(sma, locknum);
1922 rcu_read_unlock();
1923 wake_up_q(&wake_q);
1924
1925 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001927 if (error < 0) /* non-blocking error path */
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001928 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001930 /*
1931 * We need to sleep on this operation, so we put the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 * task into the pending queue and go to sleep.
1933 */
Manfred Spraulb97e8202009-12-15 16:47:32 -08001934 if (nsops == 1) {
1935 struct sem *curr;
Manfred Spraul1a233952017-07-12 14:34:38 -07001936 curr = &sma->sems[sops->sem_num];
Manfred Spraulb97e8202009-12-15 16:47:32 -08001937
Manfred Spraulf269f402013-07-08 16:01:24 -07001938 if (alter) {
1939 if (sma->complex_count) {
1940 list_add_tail(&queue.list,
1941 &sma->pending_alter);
1942 } else {
1943
1944 list_add_tail(&queue.list,
1945 &curr->pending_alter);
1946 }
1947 } else {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001948 list_add_tail(&queue.list, &curr->pending_const);
Manfred Spraulf269f402013-07-08 16:01:24 -07001949 }
Manfred Spraulb97e8202009-12-15 16:47:32 -08001950 } else {
Manfred Spraulf269f402013-07-08 16:01:24 -07001951 if (!sma->complex_count)
1952 merge_queues(sma);
1953
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001954 if (alter)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001955 list_add_tail(&queue.list, &sma->pending_alter);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001956 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001957 list_add_tail(&queue.list, &sma->pending_const);
1958
Manfred Spraulb97e8202009-12-15 16:47:32 -08001959 sma->complex_count++;
1960 }
1961
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001962 do {
1963 queue.status = -EINTR;
1964 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001965
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001966 __set_current_state(TASK_INTERRUPTIBLE);
1967 sem_unlock(sma, locknum);
Linus Torvalds321310c2013-05-04 10:47:57 -07001968 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001970 if (timeout)
1971 jiffies_left = schedule_timeout(jiffies_left);
1972 else
1973 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001975 /*
1976 * fastpath: the semop has completed, either successfully or
1977 * not, from the syscall pov, is quite irrelevant to us at this
1978 * point; we're done.
1979 *
1980 * We _do_ care, nonetheless, about being awoken by a signal or
1981 * spuriously. The queue.status is checked again in the
1982 * slowpath (aka after taking sem_lock), such that we can detect
1983 * scenarios where we were awakened externally, during the
1984 * window between wake_q_add() and wake_up_q().
1985 */
1986 error = READ_ONCE(queue.status);
1987 if (error != -EINTR) {
1988 /*
1989 * User space could assume that semop() is a memory
1990 * barrier: Without the mb(), the cpu could
1991 * speculatively read in userspace stale data that was
1992 * overwritten by the previous owner of the semaphore.
1993 */
1994 smp_mb();
1995 goto out_free;
1996 }
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001997
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001998 rcu_read_lock();
Manfred Spraulc626bc42017-01-10 16:57:48 -08001999 locknum = sem_lock(sma, sops, nsops);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002000
Davidlohr Bueso370b2622016-12-14 15:06:49 -08002001 if (!ipc_valid_object(&sma->sem_perm))
2002 goto out_unlock_free;
2003
2004 error = READ_ONCE(queue.status);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002005
2006 /*
2007 * If queue.status != -EINTR we are woken up by another process.
2008 * Leave without unlink_queue(), but with sem_unlock().
2009 */
2010 if (error != -EINTR)
2011 goto out_unlock_free;
2012
2013 /*
2014 * If an interrupt occurred we have to clean up the queue.
2015 */
2016 if (timeout && jiffies_left == 0)
2017 error = -EAGAIN;
2018 } while (error == -EINTR && !signal_pending(current)); /* spurious */
Manfred Spraul0b0577f2011-11-02 13:38:52 -07002019
Manfred Spraulb97e8202009-12-15 16:47:32 -08002020 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022out_unlock_free:
Rik van Riel6062a8d2013-04-30 19:15:44 -07002023 sem_unlock(sma, locknum);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002024 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08002026 if (sops != fast_sops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 kfree(sops);
2028 return error;
2029}
2030
Heiko Carstensd5460c92009-01-14 14:14:27 +01002031SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
2032 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
2034 return sys_semtimedop(semid, tsops, nsops, NULL);
2035}
2036
2037/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
2038 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 */
2040
2041int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
2042{
2043 struct sem_undo_list *undo_list;
2044 int error;
2045
2046 if (clone_flags & CLONE_SYSVSEM) {
2047 error = get_undo_list(&undo_list);
2048 if (error)
2049 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 atomic_inc(&undo_list->refcnt);
2051 tsk->sysvsem.undo_list = undo_list;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07002052 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 tsk->sysvsem.undo_list = NULL;
2054
2055 return 0;
2056}
2057
2058/*
2059 * add semadj values to semaphores, free undo structures.
2060 * undo structures are not freed when semaphore arrays are destroyed
2061 * so some of them may be out of date.
2062 * IMPLEMENTATION NOTE: There is some confusion over whether the
2063 * set of adjustments that needs to be done should be done in an atomic
2064 * manner or not. That is, if we are attempting to decrement the semval
2065 * should we queue up and wait until we can do so legally?
2066 * The original implementation attempted to do this (queue and wait).
2067 * The current implementation does not do so. The POSIX standard
2068 * and SVID should be consulted to determine what behavior is mandated.
2069 */
2070void exit_sem(struct task_struct *tsk)
2071{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002072 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002074 ulp = tsk->sysvsem.undo_list;
2075 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002077 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002079 if (!atomic_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return;
2081
Manfred Spraul380af1b2008-07-25 01:48:06 -07002082 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07002084 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002085 int semid, i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002086 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Nikolay Borisov2a1613a2016-10-11 13:55:05 -07002088 cond_resched();
2089
Manfred Spraul380af1b2008-07-25 01:48:06 -07002090 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02002091 un = list_entry_rcu(ulp->list_proc.next,
2092 struct sem_undo, list_proc);
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002093 if (&un->list_proc == &ulp->list_proc) {
2094 /*
2095 * We must wait for freeary() before freeing this ulp,
2096 * in case we raced with last sem_undo. There is a small
2097 * possibility where we exit while freeary() didn't
2098 * finish unlocking sem_undo_list.
2099 */
2100 spin_unlock_wait(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002101 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002102 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002103 }
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002104 spin_lock(&ulp->lock);
2105 semid = un->semid;
2106 spin_unlock(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002107
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002108 /* exit_sem raced with IPC_RMID, nothing to do */
2109 if (semid == -1) {
2110 rcu_read_unlock();
2111 continue;
2112 }
2113
2114 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002115 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002116 if (IS_ERR(sma)) {
2117 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002118 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Rik van Riel6062a8d2013-04-30 19:15:44 -07002121 sem_lock(sma, NULL, -1);
Manfred Spraul6e224f92013-10-16 13:46:45 -07002122 /* exit_sem raced with IPC_RMID, nothing to do */
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08002123 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07002124 sem_unlock(sma, -1);
2125 rcu_read_unlock();
2126 continue;
2127 }
Nick Pigginbf17bb72009-12-15 16:47:28 -08002128 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002129 if (un == NULL) {
2130 /* exit_sem raced with IPC_RMID+semget() that created
2131 * exactly the same semid. Nothing to do.
2132 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002133 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002134 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002135 continue;
2136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Manfred Spraul380af1b2008-07-25 01:48:06 -07002138 /* remove un from the linked lists */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07002139 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002140 list_del(&un->list_id);
2141
Herton R. Krzesinskia9795582015-08-14 15:35:05 -07002142 /* we are the last process using this ulp, acquiring ulp->lock
2143 * isn't required. Besides that, we are also protected against
2144 * IPC_RMID as we hold sma->sem_perm lock now
2145 */
Manfred Spraul380af1b2008-07-25 01:48:06 -07002146 list_del_rcu(&un->list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002147
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002148 /* perform adjustments registered in un */
2149 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07002150 struct sem *semaphore = &sma->sems[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002151 if (un->semadj[i]) {
2152 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 /*
2154 * Range checks of the new semaphore value,
2155 * not defined by sus:
2156 * - Some unices ignore the undo entirely
2157 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2158 * - some cap the value (e.g. FreeBSD caps
2159 * at 0, but doesn't enforce SEMVMX)
2160 *
2161 * Linux caps the semaphore value, both at 0
2162 * and at SEMVMX.
2163 *
Manfred Spraul239521f2014-01-27 17:07:04 -08002164 * Manfred <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08002166 if (semaphore->semval < 0)
2167 semaphore->semval = 0;
2168 if (semaphore->semval > SEMVMX)
2169 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002170 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002174 do_smart_update(sma, NULL, 0, 1, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002175 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002176 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002177 wake_up_q(&wake_q);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002178
Lai Jiangshan693a8b62011-03-18 12:09:35 +08002179 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002181 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
2184#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07002185static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08002187 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07002188 struct sem_array *sma = it;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002189 time_t sem_otime;
2190
Manfred Sprauld8c63372013-09-30 13:45:07 -07002191 /*
2192 * The proc interface isn't aware of sem_lock(), it calls
2193 * ipc_lock_object() directly (in sysvipc_find_ipc).
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002194 * In order to stay compatible with sem_lock(), we must
2195 * enter / leave complex_mode.
Manfred Sprauld8c63372013-09-30 13:45:07 -07002196 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002197 complexmode_enter(sma);
Manfred Sprauld8c63372013-09-30 13:45:07 -07002198
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002199 sem_otime = get_semotime(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Joe Perches7f032d62015-04-15 16:17:54 -07002201 seq_printf(s,
2202 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
2203 sma->sem_perm.key,
2204 sma->sem_perm.id,
2205 sma->sem_perm.mode,
2206 sma->sem_nsems,
2207 from_kuid_munged(user_ns, sma->sem_perm.uid),
2208 from_kgid_munged(user_ns, sma->sem_perm.gid),
2209 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2210 from_kgid_munged(user_ns, sma->sem_perm.cgid),
2211 sem_otime,
2212 sma->sem_ctime);
2213
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002214 complexmode_tryleave(sma);
2215
Joe Perches7f032d62015-04-15 16:17:54 -07002216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218#endif