blob: c6c50370504cc9b5a6e9dcd0b50193414caf88a2 [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
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700261static void sem_rcu_free(struct rcu_head *head)
262{
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700263 struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu);
264 struct sem_array *sma = container_of(p, struct sem_array, sem_perm);
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700265
266 security_sem_free(sma);
Kees Cooke2029df2017-07-12 14:35:31 -0700267 kvfree(sma);
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700268}
269
Nadia Derbey3e148c72007-10-18 23:40:54 -0700270/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700271 * Enter the mode suitable for non-simple operations:
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700272 * Caller must own sem_perm.lock.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700273 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700274static void complexmode_enter(struct sem_array *sma)
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700275{
276 int i;
277 struct sem *sem;
278
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800279 if (sma->use_global_lock > 0) {
280 /*
281 * We are already in global lock mode.
282 * Nothing to do, just reset the
283 * counter until we return to simple mode.
284 */
285 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul6d07b682013-09-30 13:45:06 -0700286 return;
287 }
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800288 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700289
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700290 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700291 sem = &sma->sems[i];
Manfred Spraul27d7be12017-02-27 14:28:15 -0800292 spin_lock(&sem->lock);
293 spin_unlock(&sem->lock);
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700294 }
295}
296
297/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700298 * Try to leave the mode that disallows simple operations:
299 * Caller must own sem_perm.lock.
300 */
301static void complexmode_tryleave(struct sem_array *sma)
302{
303 if (sma->complex_count) {
304 /* Complex ops are sleeping.
305 * We must stay in complex mode
306 */
307 return;
308 }
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800309 if (sma->use_global_lock == 1) {
310 /*
311 * Immediately after setting use_global_lock to 0,
312 * a simple op can start. Thus: all memory writes
313 * performed by the current operation must be visible
314 * before we set use_global_lock to 0.
315 */
316 smp_store_release(&sma->use_global_lock, 0);
317 } else {
318 sma->use_global_lock--;
319 }
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700320}
321
322#define SEM_GLOBAL_LOCK (-1)
323/*
Rik van Riel6062a8d2013-04-30 19:15:44 -0700324 * If the request contains only one semaphore operation, and there are
325 * no complex transactions pending, lock only the semaphore involved.
326 * Otherwise, lock the entire semaphore array, since we either have
327 * multiple semaphores in our own semops, or we need to look at
328 * semaphores from other pending complex operations.
Rik van Riel6062a8d2013-04-30 19:15:44 -0700329 */
330static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
331 int nsops)
332{
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700333 struct sem *sem;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700334
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700335 if (nsops != 1) {
336 /* Complex operation - acquire a full lock */
337 ipc_lock_object(&sma->sem_perm);
338
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700339 /* Prevent parallel simple ops */
340 complexmode_enter(sma);
341 return SEM_GLOBAL_LOCK;
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700342 }
343
344 /*
345 * Only one semaphore affected - try to optimize locking.
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700346 * Optimized locking is possible if no complex operation
347 * is either enqueued or processed right now.
348 *
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800349 * Both facts are tracked by use_global_mode.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700350 */
Manfred Spraul1a233952017-07-12 14:34:38 -0700351 sem = &sma->sems[sops->sem_num];
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700352
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700353 /*
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800354 * Initial check for use_global_lock. Just an optimization,
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700355 * no locking, no memory barrier.
356 */
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800357 if (!sma->use_global_lock) {
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700358 /*
359 * It appears that no complex operation is around.
360 * Acquire the per-semaphore lock.
361 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700362 spin_lock(&sem->lock);
363
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800364 /* pairs with smp_store_release() */
365 if (!smp_load_acquire(&sma->use_global_lock)) {
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700366 /* fast path successful! */
367 return sops->sem_num;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700368 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700369 spin_unlock(&sem->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700370 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700371
372 /* slow path: acquire the full lock */
373 ipc_lock_object(&sma->sem_perm);
374
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800375 if (sma->use_global_lock == 0) {
376 /*
377 * The use_global_lock mode ended while we waited for
378 * sma->sem_perm.lock. Thus we must switch to locking
379 * with sem->lock.
380 * Unlike in the fast path, there is no need to recheck
381 * sma->use_global_lock after we have acquired sem->lock:
382 * We own sma->sem_perm.lock, thus use_global_lock cannot
383 * change.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700384 */
385 spin_lock(&sem->lock);
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800386
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700387 ipc_unlock_object(&sma->sem_perm);
388 return sops->sem_num;
389 } else {
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800390 /*
391 * Not a false alarm, thus continue to use the global lock
392 * mode. No need for complexmode_enter(), this was done by
393 * the caller that has set use_global_mode to non-zero.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700394 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700395 return SEM_GLOBAL_LOCK;
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700396 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700397}
398
399static inline void sem_unlock(struct sem_array *sma, int locknum)
400{
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700401 if (locknum == SEM_GLOBAL_LOCK) {
Manfred Spraulf269f402013-07-08 16:01:24 -0700402 unmerge_queues(sma);
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700403 complexmode_tryleave(sma);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700404 ipc_unlock_object(&sma->sem_perm);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700405 } else {
Manfred Spraul1a233952017-07-12 14:34:38 -0700406 struct sem *sem = &sma->sems[locknum];
Rik van Riel6062a8d2013-04-30 19:15:44 -0700407 spin_unlock(&sem->lock);
408 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700409}
410
411/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700412 * sem_lock_(check_) routines are called in the paths where the rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -0700413 * is not held.
Linus Torvalds321310c2013-05-04 10:47:57 -0700414 *
415 * The caller holds the RCU read lock.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700416 */
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700417static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
418{
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700419 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&sem_ids(ns), id);
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700420
421 if (IS_ERR(ipcp))
422 return ERR_CAST(ipcp);
423
424 return container_of(ipcp, struct sem_array, sem_perm);
425}
426
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700427static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
428 int id)
429{
430 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
431
432 if (IS_ERR(ipcp))
433 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800434
Nadia Derbey03f02c72007-10-18 23:40:51 -0700435 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700436}
437
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700438static inline void sem_lock_and_putref(struct sem_array *sma)
439{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700440 sem_lock(sma, NULL, -1);
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700441 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700442}
443
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700444static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
445{
446 ipc_rmid(&sem_ids(ns), &s->sem_perm);
447}
448
Kees Cook101ede02017-07-12 14:35:02 -0700449static struct sem_array *sem_alloc(size_t nsems)
450{
451 struct sem_array *sma;
452 size_t size;
453
454 if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
455 return NULL;
456
457 size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
458 sma = kvmalloc(size, GFP_KERNEL);
459 if (unlikely(!sma))
460 return NULL;
461
462 memset(sma, 0, size);
Kees Cook101ede02017-07-12 14:35:02 -0700463
464 return sma;
465}
466
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700467/**
468 * newary - Create a new semaphore set
469 * @ns: namespace
470 * @params: ptr to the structure that contains key, semflg and nsems
471 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700472 * Called with sem_ids.rwsem held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700473 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700474static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 int retval;
477 struct sem_array *sma;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700478 key_t key = params->key;
479 int nsems = params->u.nsems;
480 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800481 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (!nsems)
484 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700485 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -ENOSPC;
487
Kees Cook101ede02017-07-12 14:35:02 -0700488 sma = sem_alloc(nsems);
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800489 if (!sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -ENOMEM;
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 sma->sem_perm.mode = (semflg & S_IRWXUGO);
493 sma->sem_perm.key = key;
494
495 sma->sem_perm.security = NULL;
496 retval = security_sem_alloc(sma);
497 if (retval) {
Kees Cooke2029df2017-07-12 14:35:31 -0700498 kvfree(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return retval;
500 }
501
Rik van Riel6062a8d2013-04-30 19:15:44 -0700502 for (i = 0; i < nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700503 INIT_LIST_HEAD(&sma->sems[i].pending_alter);
504 INIT_LIST_HEAD(&sma->sems[i].pending_const);
505 spin_lock_init(&sma->sems[i].lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700506 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800507
508 sma->complex_count = 0;
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800509 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700510 INIT_LIST_HEAD(&sma->pending_alter);
511 INIT_LIST_HEAD(&sma->pending_const);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700512 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 sma->sem_nsems = nsems;
514 sma->sem_ctime = get_seconds();
Manfred Spraule8577d12014-12-02 15:59:34 -0800515
Manfred Spraul2ec55f82017-07-12 14:35:13 -0700516 retval = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
517 if (retval < 0) {
518 call_rcu(&sma->sem_perm.rcu, sem_rcu_free);
519 return retval;
Manfred Spraule8577d12014-12-02 15:59:34 -0800520 }
521 ns->used_sems += nsems;
522
Rik van Riel6062a8d2013-04-30 19:15:44 -0700523 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -0700524 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700526 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700529
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700530/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700531 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700532 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700533static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700535 struct sem_array *sma;
536
537 sma = container_of(ipcp, struct sem_array, sem_perm);
538 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700539}
540
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700541/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700542 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700543 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700544static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
545 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700546{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700547 struct sem_array *sma;
548
549 sma = container_of(ipcp, struct sem_array, sem_perm);
550 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700551 return -EINVAL;
552
553 return 0;
554}
555
Heiko Carstensd5460c92009-01-14 14:14:27 +0100556SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700557{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700558 struct ipc_namespace *ns;
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700559 static const struct ipc_ops sem_ops = {
560 .getnew = newary,
561 .associate = sem_security,
562 .more_checks = sem_more_checks,
563 };
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700564 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Kirill Korotaeve3893532006-10-02 02:18:22 -0700566 ns = current->nsproxy->ipc_ns;
567
568 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700570
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700571 sem_params.key = key;
572 sem_params.flg = semflg;
573 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700574
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700575 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Petr Mladek78f50092014-01-27 17:07:00 -0800578/**
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800579 * perform_atomic_semop[_slow] - Attempt to perform semaphore
580 * operations on a given array.
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700581 * @sma: semaphore array
Manfred Sprauld198cd62014-06-06 14:37:49 -0700582 * @q: struct sem_queue that describes the operation
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700583 *
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800584 * Caller blocking are as follows, based the value
585 * indicated by the semaphore operation (sem_op):
586 *
587 * (1) >0 never blocks.
588 * (2) 0 (wait-for-zero operation): semval is non-zero.
589 * (3) <0 attempting to decrement semval to a value smaller than zero.
590 *
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700591 * Returns 0 if the operation was possible.
592 * Returns 1 if the operation is impossible, the caller must sleep.
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800593 * Returns <0 for error codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800595static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Manfred Sprauld198cd62014-06-06 14:37:49 -0700597 int result, sem_op, nsops, pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct sembuf *sop;
Manfred Spraul239521f2014-01-27 17:07:04 -0800599 struct sem *curr;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700600 struct sembuf *sops;
601 struct sem_undo *un;
602
603 sops = q->sops;
604 nsops = q->nsops;
605 un = q->undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 for (sop = sops; sop < sops + nsops; sop++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700608 curr = &sma->sems[sop->sem_num];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 sem_op = sop->sem_op;
610 result = curr->semval;
Petr Mladek78f50092014-01-27 17:07:00 -0800611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!sem_op && result)
613 goto would_block;
614
615 result += sem_op;
616 if (result < 0)
617 goto would_block;
618 if (result > SEMVMX)
619 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (sop->sem_flg & SEM_UNDO) {
622 int undo = un->semadj[sop->sem_num] - sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800623 /* Exceeding the undo range is an error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
625 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800626 un->semadj[sop->sem_num] = undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Petr Mladek78f50092014-01-27 17:07:00 -0800628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 curr->semval = result;
630 }
631
632 sop--;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700633 pid = q->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 while (sop >= sops) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700635 sma->sems[sop->sem_num].sempid = pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 sop--;
637 }
Petr Mladek78f50092014-01-27 17:07:00 -0800638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return 0;
640
641out_of_range:
642 result = -ERANGE;
643 goto undo;
644
645would_block:
Manfred Sprauled247b72014-06-06 14:37:49 -0700646 q->blocking = sop;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (sop->sem_flg & IPC_NOWAIT)
649 result = -EAGAIN;
650 else
651 result = 1;
652
653undo:
654 sop--;
655 while (sop >= sops) {
Petr Mladek78f50092014-01-27 17:07:00 -0800656 sem_op = sop->sem_op;
Manfred Spraul1a233952017-07-12 14:34:38 -0700657 sma->sems[sop->sem_num].semval -= sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800658 if (sop->sem_flg & SEM_UNDO)
659 un->semadj[sop->sem_num] += sem_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 sop--;
661 }
662
663 return result;
664}
665
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800666static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
667{
668 int result, sem_op, nsops;
669 struct sembuf *sop;
670 struct sem *curr;
671 struct sembuf *sops;
672 struct sem_undo *un;
673
674 sops = q->sops;
675 nsops = q->nsops;
676 un = q->undo;
677
678 if (unlikely(q->dupsop))
679 return perform_atomic_semop_slow(sma, q);
680
681 /*
682 * We scan the semaphore set twice, first to ensure that the entire
683 * operation can succeed, therefore avoiding any pointless writes
684 * to shared memory and having to undo such changes in order to block
685 * until the operations can go through.
686 */
687 for (sop = sops; sop < sops + nsops; sop++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700688 curr = &sma->sems[sop->sem_num];
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800689 sem_op = sop->sem_op;
690 result = curr->semval;
691
692 if (!sem_op && result)
693 goto would_block; /* wait-for-zero */
694
695 result += sem_op;
696 if (result < 0)
697 goto would_block;
698
699 if (result > SEMVMX)
700 return -ERANGE;
701
702 if (sop->sem_flg & SEM_UNDO) {
703 int undo = un->semadj[sop->sem_num] - sem_op;
704
705 /* Exceeding the undo range is an error. */
706 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
707 return -ERANGE;
708 }
709 }
710
711 for (sop = sops; sop < sops + nsops; sop++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700712 curr = &sma->sems[sop->sem_num];
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800713 sem_op = sop->sem_op;
714 result = curr->semval;
715
716 if (sop->sem_flg & SEM_UNDO) {
717 int undo = un->semadj[sop->sem_num] - sem_op;
718
719 un->semadj[sop->sem_num] = undo;
720 }
721 curr->semval += sem_op;
722 curr->sempid = q->pid;
723 }
724
725 return 0;
726
727would_block:
728 q->blocking = sop;
729 return sop->sem_flg & IPC_NOWAIT ? -EAGAIN : 1;
730}
731
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800732static inline void wake_up_sem_queue_prepare(struct sem_queue *q, int error,
733 struct wake_q_head *wake_q)
Nick Piggind4212092009-12-15 16:47:30 -0800734{
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800735 wake_q_add(wake_q, q->sleeper);
736 /*
737 * Rely on the above implicit barrier, such that we can
738 * ensure that we hold reference to the task before setting
739 * q->status. Otherwise we could race with do_exit if the
740 * task is awoken by an external event before calling
741 * wake_up_process().
742 */
743 WRITE_ONCE(q->status, error);
Nick Piggind4212092009-12-15 16:47:30 -0800744}
745
Manfred Spraulb97e8202009-12-15 16:47:32 -0800746static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
747{
748 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700749 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800750 sma->complex_count--;
751}
752
Manfred Spraulfd5db422010-05-26 14:43:40 -0700753/** check_restart(sma, q)
754 * @sma: semaphore array
755 * @q: the operation that just completed
756 *
757 * update_queue is O(N^2) when it restarts scanning the whole queue of
758 * waiting operations. Therefore this function checks if the restart is
759 * really necessary. It is called after a previously waiting operation
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700760 * modified the array.
761 * Note that wait-for-zero operations are handled without restart.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700762 */
Davidlohr Bueso4663d3e2016-12-14 15:06:40 -0800763static inline int check_restart(struct sem_array *sma, struct sem_queue *q)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700764{
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700765 /* pending complex alter operations are too difficult to analyse */
766 if (!list_empty(&sma->pending_alter))
Manfred Spraulfd5db422010-05-26 14:43:40 -0700767 return 1;
768
769 /* we were a sleeping complex operation. Too difficult */
770 if (q->nsops > 1)
771 return 1;
772
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700773 /* It is impossible that someone waits for the new value:
774 * - complex operations always restart.
775 * - wait-for-zero are handled seperately.
776 * - q is a previously sleeping simple operation that
777 * altered the array. It must be a decrement, because
778 * simple increments never sleep.
779 * - If there are older (higher priority) decrements
780 * in the queue, then they have observed the original
781 * semval value and couldn't proceed. The operation
782 * decremented to value - thus they won't proceed either.
783 */
784 return 0;
785}
Manfred Spraulfd5db422010-05-26 14:43:40 -0700786
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700787/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800788 * wake_const_ops - wake up non-alter tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700789 * @sma: semaphore array.
790 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800791 * @wake_q: lockless wake-queue head.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700792 *
793 * wake_const_ops must be called after a semaphore in a semaphore array
794 * was set to 0. If complex const operations are pending, wake_const_ops must
795 * be called with semnum = -1, as well as with the number of each modified
796 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800797 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700798 * is stored in q->pid.
799 * The function returns 1 if at least one operation was completed successfully.
800 */
801static int wake_const_ops(struct sem_array *sma, int semnum,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800802 struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700803{
Davidlohr Buesof150f022016-12-14 15:06:43 -0800804 struct sem_queue *q, *tmp;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700805 struct list_head *pending_list;
806 int semop_completed = 0;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700807
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700808 if (semnum == -1)
809 pending_list = &sma->pending_const;
810 else
Manfred Spraul1a233952017-07-12 14:34:38 -0700811 pending_list = &sma->sems[semnum].pending_const;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700812
Davidlohr Buesof150f022016-12-14 15:06:43 -0800813 list_for_each_entry_safe(q, tmp, pending_list, list) {
814 int error = perform_atomic_semop(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700815
Davidlohr Buesof150f022016-12-14 15:06:43 -0800816 if (error > 0)
817 continue;
818 /* operation completed, remove from queue & wakeup */
819 unlink_queue(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700820
Davidlohr Buesof150f022016-12-14 15:06:43 -0800821 wake_up_sem_queue_prepare(q, error, wake_q);
822 if (error == 0)
823 semop_completed = 1;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700824 }
Davidlohr Buesof150f022016-12-14 15:06:43 -0800825
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700826 return semop_completed;
827}
828
829/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800830 * do_smart_wakeup_zero - wakeup all wait for zero tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700831 * @sma: semaphore array
832 * @sops: operations that were performed
833 * @nsops: number of operations
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800834 * @wake_q: lockless wake-queue head
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700835 *
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800836 * Checks all required queue for wait-for-zero operations, based
837 * on the actual changes that were performed on the semaphore array.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700838 * The function returns 1 if at least one operation was completed successfully.
839 */
840static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800841 int nsops, struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700842{
843 int i;
844 int semop_completed = 0;
845 int got_zero = 0;
846
847 /* first: the per-semaphore queues, if known */
848 if (sops) {
849 for (i = 0; i < nsops; i++) {
850 int num = sops[i].sem_num;
851
Manfred Spraul1a233952017-07-12 14:34:38 -0700852 if (sma->sems[num].semval == 0) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700853 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800854 semop_completed |= wake_const_ops(sma, num, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700855 }
856 }
857 } else {
858 /*
859 * No sops means modified semaphores not known.
860 * Assume all were changed.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700861 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700862 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700863 if (sma->sems[i].semval == 0) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700864 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800865 semop_completed |= wake_const_ops(sma, i, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700866 }
867 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700868 }
869 /*
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700870 * If one of the modified semaphores got 0,
871 * then check the global queue, too.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700872 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700873 if (got_zero)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800874 semop_completed |= wake_const_ops(sma, -1, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700875
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700876 return semop_completed;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700877}
878
Manfred Spraul636c6be2009-12-15 16:47:33 -0800879
880/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800881 * update_queue - look for tasks that can be completed.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800882 * @sma: semaphore array.
883 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800884 * @wake_q: lockless wake-queue head.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800885 *
886 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700887 * was modified. If multiple semaphores were modified, update_queue must
888 * be called with semnum = -1, as well as with the number of each modified
889 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800890 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700891 * is stored in q->pid.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700892 * The function internally checks if const operations can now succeed.
893 *
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700894 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800896static int update_queue(struct sem_array *sma, int semnum, struct wake_q_head *wake_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Davidlohr Buesof150f022016-12-14 15:06:43 -0800898 struct sem_queue *q, *tmp;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800899 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700900 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800901
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700902 if (semnum == -1)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700903 pending_list = &sma->pending_alter;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700904 else
Manfred Spraul1a233952017-07-12 14:34:38 -0700905 pending_list = &sma->sems[semnum].pending_alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Nick Piggin9cad2002009-12-15 16:47:29 -0800907again:
Davidlohr Buesof150f022016-12-14 15:06:43 -0800908 list_for_each_entry_safe(q, tmp, pending_list, list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700909 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800910
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800911 /* If we are scanning the single sop, per-semaphore list of
912 * one semaphore and that semaphore is 0, then it is not
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700913 * necessary to scan further: simple increments
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800914 * that affect only one entry succeed immediately and cannot
915 * be in the per semaphore pending queue, and decrements
916 * cannot be successful if the value is already 0.
917 */
Manfred Spraul1a233952017-07-12 14:34:38 -0700918 if (semnum != -1 && sma->sems[semnum].semval == 0)
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800919 break;
920
Manfred Sprauld198cd62014-06-06 14:37:49 -0700921 error = perform_atomic_semop(sma, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800924 if (error > 0)
925 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700926
Manfred Spraulb97e8202009-12-15 16:47:32 -0800927 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700928
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700929 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700930 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700931 } else {
932 semop_completed = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800933 do_smart_wakeup_zero(sma, q->sops, q->nsops, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700934 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700935 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700936
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800937 wake_up_sem_queue_prepare(q, error, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700938 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800939 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700941 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700944/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800945 * set_semotime - set sem_otime
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700946 * @sma: semaphore array
947 * @sops: operations that modified the array, may be NULL
948 *
949 * sem_otime is replicated to avoid cache line trashing.
950 * This function sets one instance to the current time.
951 */
952static void set_semotime(struct sem_array *sma, struct sembuf *sops)
953{
954 if (sops == NULL) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700955 sma->sems[0].sem_otime = get_seconds();
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700956 } else {
Manfred Spraul1a233952017-07-12 14:34:38 -0700957 sma->sems[sops[0].sem_num].sem_otime =
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700958 get_seconds();
959 }
960}
961
962/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800963 * do_smart_update - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700964 * @sma: semaphore array
965 * @sops: operations that were performed
966 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700967 * @otime: force setting otime
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800968 * @wake_q: lockless wake-queue head
Manfred Spraulfd5db422010-05-26 14:43:40 -0700969 *
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700970 * do_smart_update() does the required calls to update_queue and wakeup_zero,
971 * based on the actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700972 * Note that the function does not do the actual wake-up: the caller is
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800973 * responsible for calling wake_up_q().
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700974 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700975 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700976static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800977 int otime, struct wake_q_head *wake_q)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700978{
979 int i;
980
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800981 otime |= do_smart_wakeup_zero(sma, sops, nsops, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700982
Manfred Spraulf269f402013-07-08 16:01:24 -0700983 if (!list_empty(&sma->pending_alter)) {
984 /* semaphore array uses the global queue - just process it. */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800985 otime |= update_queue(sma, -1, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -0700986 } else {
987 if (!sops) {
988 /*
989 * No sops, thus the modified semaphores are not
990 * known. Check all.
991 */
992 for (i = 0; i < sma->sem_nsems; i++)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800993 otime |= update_queue(sma, i, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -0700994 } else {
995 /*
996 * Check the semaphores that were increased:
997 * - No complex ops, thus all sleeping ops are
998 * decrease.
999 * - if we decreased the value, then any sleeping
1000 * semaphore ops wont be able to run: If the
1001 * previous value was too small, then the new
1002 * value will be too small, too.
1003 */
1004 for (i = 0; i < nsops; i++) {
1005 if (sops[i].sem_op > 0) {
1006 otime |= update_queue(sma,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001007 sops[i].sem_num, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -07001008 }
Manfred Spraulab465df2013-05-26 11:08:52 +02001009 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001010 }
Manfred Spraulfd5db422010-05-26 14:43:40 -07001011 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001012 if (otime)
1013 set_semotime(sma, sops);
Manfred Spraulfd5db422010-05-26 14:43:40 -07001014}
1015
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001016/*
Manfred Spraulb220c572014-06-06 14:37:51 -07001017 * check_qop: Test if a queued operation sleeps on the semaphore semnum
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001018 */
1019static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
1020 bool count_zero)
1021{
Manfred Spraulb220c572014-06-06 14:37:51 -07001022 struct sembuf *sop = q->blocking;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001023
Manfred Spraul9b44ee22014-06-06 14:37:52 -07001024 /*
1025 * Linux always (since 0.99.10) reported a task as sleeping on all
1026 * semaphores. This violates SUS, therefore it was changed to the
1027 * standard compliant behavior.
1028 * Give the administrators a chance to notice that an application
1029 * might misbehave because it relies on the Linux behavior.
1030 */
1031 pr_info_once("semctl(GETNCNT/GETZCNT) is since 3.16 Single Unix Specification compliant.\n"
1032 "The task %s (%d) triggered the difference, watch for misbehavior.\n",
1033 current->comm, task_pid_nr(current));
1034
Manfred Spraulb220c572014-06-06 14:37:51 -07001035 if (sop->sem_num != semnum)
1036 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001037
Manfred Spraulb220c572014-06-06 14:37:51 -07001038 if (count_zero && sop->sem_op == 0)
1039 return 1;
1040 if (!count_zero && sop->sem_op < 0)
1041 return 1;
1042
1043 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001044}
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046/* The following counts are associated to each semaphore:
1047 * semncnt number of tasks waiting on semval being nonzero
1048 * semzcnt number of tasks waiting on semval being zero
Manfred Spraulb220c572014-06-06 14:37:51 -07001049 *
1050 * Per definition, a task waits only on the semaphore of the first semop
1051 * that cannot proceed, even if additional operation would block, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 */
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001053static int count_semcnt(struct sem_array *sma, ushort semnum,
1054 bool count_zero)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001056 struct list_head *l;
Manfred Spraul239521f2014-01-27 17:07:04 -08001057 struct sem_queue *q;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001058 int semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001060 semcnt = 0;
1061 /* First: check the simple operations. They are easy to evaluate */
1062 if (count_zero)
Manfred Spraul1a233952017-07-12 14:34:38 -07001063 l = &sma->sems[semnum].pending_const;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001064 else
Manfred Spraul1a233952017-07-12 14:34:38 -07001065 l = &sma->sems[semnum].pending_alter;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001066
1067 list_for_each_entry(q, l, list) {
1068 /* all task on a per-semaphore list sleep on exactly
1069 * that semaphore
1070 */
1071 semcnt++;
Rik van Rielde2657f2013-05-09 16:59:59 -04001072 }
1073
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001074 /* Then: check the complex operations. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001075 list_for_each_entry(q, &sma->pending_alter, list) {
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001076 semcnt += check_qop(sma, semnum, q, count_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001078 if (count_zero) {
1079 list_for_each_entry(q, &sma->pending_const, list) {
1080 semcnt += check_qop(sma, semnum, q, count_zero);
1081 }
Rik van Rielebc2e5e2013-05-09 16:53:28 -04001082 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001083 return semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001086/* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1087 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -07001088 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001090static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Manfred Spraul380af1b2008-07-25 01:48:06 -07001092 struct sem_undo *un, *tu;
1093 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001094 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001095 int i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001096 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Manfred Spraul380af1b2008-07-25 01:48:06 -07001098 /* Free the existing undo structures for this semaphore set. */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001099 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001100 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1101 list_del(&un->list_id);
1102 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001104 list_del_rcu(&un->list_proc);
1105 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001106 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001110 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1111 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001112 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001113 }
1114
1115 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -08001116 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001117 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001119 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07001120 struct sem *sem = &sma->sems[i];
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001121 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1122 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001123 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001124 }
1125 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001126 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001127 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001128 }
1129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001131 /* Remove the semaphore set from the IDR */
1132 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001133 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001134 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001136 wake_up_q(&wake_q);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001137 ns->used_sems -= sma->sem_nsems;
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001138 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1142{
Manfred Spraul239521f2014-01-27 17:07:04 -08001143 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 case IPC_64:
1145 return copy_to_user(buf, in, sizeof(*in));
1146 case IPC_OLD:
1147 {
1148 struct semid_ds out;
1149
Dan Rosenberg982f7c22010-09-30 15:15:31 -07001150 memset(&out, 0, sizeof(out));
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1153
1154 out.sem_otime = in->sem_otime;
1155 out.sem_ctime = in->sem_ctime;
1156 out.sem_nsems = in->sem_nsems;
1157
1158 return copy_to_user(buf, &out, sizeof(out));
1159 }
1160 default:
1161 return -EINVAL;
1162 }
1163}
1164
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001165static time_t get_semotime(struct sem_array *sma)
1166{
1167 int i;
1168 time_t res;
1169
Manfred Spraul1a233952017-07-12 14:34:38 -07001170 res = sma->sems[0].sem_otime;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001171 for (i = 1; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07001172 time_t to = sma->sems[i].sem_otime;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001173
1174 if (to > res)
1175 res = to;
1176 }
1177 return res;
1178}
1179
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001180static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001181 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Amerigo Wange5cc9c72009-12-15 16:47:35 -08001183 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 struct sem_array *sma;
1185
Manfred Spraul239521f2014-01-27 17:07:04 -08001186 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 case IPC_INFO:
1188 case SEM_INFO:
1189 {
1190 struct seminfo seminfo;
1191 int max_id;
1192
1193 err = security_sem_semctl(NULL, cmd);
1194 if (err)
1195 return err;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001196
Manfred Spraul239521f2014-01-27 17:07:04 -08001197 memset(&seminfo, 0, sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -07001198 seminfo.semmni = ns->sc_semmni;
1199 seminfo.semmns = ns->sc_semmns;
1200 seminfo.semmsl = ns->sc_semmsl;
1201 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 seminfo.semvmx = SEMVMX;
1203 seminfo.semmnu = SEMMNU;
1204 seminfo.semmap = SEMMAP;
1205 seminfo.semume = SEMUME;
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001206 down_read(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001208 seminfo.semusz = sem_ids(ns).in_use;
1209 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 } else {
1211 seminfo.semusz = SEMUSZ;
1212 seminfo.semaem = SEMAEM;
1213 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001214 max_id = ipc_get_maxid(&sem_ids(ns));
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001215 up_read(&sem_ids(ns).rwsem);
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001216 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return -EFAULT;
Manfred Spraul239521f2014-01-27 17:07:04 -08001218 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001220 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 case SEM_STAT:
1222 {
1223 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001224 int id = 0;
1225
1226 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Linus Torvalds941b0302013-05-04 11:04:29 -07001228 rcu_read_lock();
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001229 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001230 sma = sem_obtain_object(ns, semid);
1231 if (IS_ERR(sma)) {
1232 err = PTR_ERR(sma);
1233 goto out_unlock;
1234 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001235 id = sma->sem_perm.id;
1236 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001237 sma = sem_obtain_object_check(ns, semid);
1238 if (IS_ERR(sma)) {
1239 err = PTR_ERR(sma);
1240 goto out_unlock;
1241 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001245 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 goto out_unlock;
1247
1248 err = security_sem_semctl(sma, cmd);
1249 if (err)
1250 goto out_unlock;
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001253 tbuf.sem_otime = get_semotime(sma);
1254 tbuf.sem_ctime = sma->sem_ctime;
1255 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001256 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001257 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return -EFAULT;
1259 return id;
1260 }
1261 default:
1262 return -EINVAL;
1263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001265 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return err;
1267}
1268
Al Viroe1fd1f42013-03-05 15:04:55 -05001269static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1270 unsigned long arg)
1271{
1272 struct sem_undo *un;
1273 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001274 struct sem *curr;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001275 int err, val;
1276 DEFINE_WAKE_Q(wake_q);
1277
Al Viroe1fd1f42013-03-05 15:04:55 -05001278#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1279 /* big-endian 64bit */
1280 val = arg >> 32;
1281#else
1282 /* 32bit or little-endian 64bit */
1283 val = arg;
1284#endif
1285
Rik van Riel6062a8d2013-04-30 19:15:44 -07001286 if (val > SEMVMX || val < 0)
1287 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001288
Rik van Riel6062a8d2013-04-30 19:15:44 -07001289 rcu_read_lock();
1290 sma = sem_obtain_object_check(ns, semid);
1291 if (IS_ERR(sma)) {
1292 rcu_read_unlock();
1293 return PTR_ERR(sma);
1294 }
1295
1296 if (semnum < 0 || semnum >= sma->sem_nsems) {
1297 rcu_read_unlock();
1298 return -EINVAL;
1299 }
1300
1301
1302 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1303 rcu_read_unlock();
1304 return -EACCES;
1305 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001306
1307 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001308 if (err) {
1309 rcu_read_unlock();
1310 return -EACCES;
1311 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001312
Rik van Riel6062a8d2013-04-30 19:15:44 -07001313 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001314
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001315 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001316 sem_unlock(sma, -1);
1317 rcu_read_unlock();
1318 return -EIDRM;
1319 }
1320
Manfred Spraul1a233952017-07-12 14:34:38 -07001321 curr = &sma->sems[semnum];
Al Viroe1fd1f42013-03-05 15:04:55 -05001322
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001323 ipc_assert_locked_object(&sma->sem_perm);
Al Viroe1fd1f42013-03-05 15:04:55 -05001324 list_for_each_entry(un, &sma->list_id, list_id)
1325 un->semadj[semnum] = 0;
1326
1327 curr->semval = val;
1328 curr->sempid = task_tgid_vnr(current);
1329 sma->sem_ctime = get_seconds();
1330 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001331 do_smart_update(sma, NULL, 0, 0, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001332 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001333 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001334 wake_up_q(&wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001335 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001336}
1337
Kirill Korotaeve3893532006-10-02 02:18:22 -07001338static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001339 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001342 struct sem *curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001343 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 ushort fast_sem_io[SEMMSL_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001345 ushort *sem_io = fast_sem_io;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001346 DEFINE_WAKE_Q(wake_q);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001347
1348 rcu_read_lock();
1349 sma = sem_obtain_object_check(ns, semid);
1350 if (IS_ERR(sma)) {
1351 rcu_read_unlock();
1352 return PTR_ERR(sma);
1353 }
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 nsems = sma->sem_nsems;
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 err = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001358 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1359 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 err = security_sem_semctl(sma, cmd);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001362 if (err)
1363 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 err = -EACCES;
1366 switch (cmd) {
1367 case GETALL:
1368 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001369 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 int i;
1371
Al Viroce857222013-05-03 00:30:49 +01001372 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001373 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001374 err = -EIDRM;
1375 goto out_unlock;
1376 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001377 if (nsems > SEMMSL_FAST) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001378 if (!ipc_rcu_getref(&sma->sem_perm)) {
Al Viroce857222013-05-03 00:30:49 +01001379 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001380 goto out_unlock;
Al Viroce857222013-05-03 00:30:49 +01001381 }
1382 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001383 rcu_read_unlock();
Kees Cookf8dbe8d2017-07-12 14:34:47 -07001384 sem_io = kvmalloc_array(nsems, sizeof(ushort),
1385 GFP_KERNEL);
Manfred Spraul239521f2014-01-27 17:07:04 -08001386 if (sem_io == NULL) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001387 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return -ENOMEM;
1389 }
1390
Linus Torvalds4091fd942013-05-04 10:13:40 -07001391 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001392 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001393 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001395 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Al Viroce857222013-05-03 00:30:49 +01001397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 for (i = 0; i < sma->sem_nsems; i++)
Manfred Spraul1a233952017-07-12 14:34:38 -07001399 sem_io[i] = sma->sems[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001400 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001401 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 err = 0;
Manfred Spraul239521f2014-01-27 17:07:04 -08001403 if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 err = -EFAULT;
1405 goto out_free;
1406 }
1407 case SETALL:
1408 {
1409 int i;
1410 struct sem_undo *un;
1411
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001412 if (!ipc_rcu_getref(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001413 err = -EIDRM;
1414 goto out_rcu_wakeup;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001415 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001416 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Manfred Spraul239521f2014-01-27 17:07:04 -08001418 if (nsems > SEMMSL_FAST) {
Kees Cookf8dbe8d2017-07-12 14:34:47 -07001419 sem_io = kvmalloc_array(nsems, sizeof(ushort),
1420 GFP_KERNEL);
Manfred Spraul239521f2014-01-27 17:07:04 -08001421 if (sem_io == NULL) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001422 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -ENOMEM;
1424 }
1425 }
1426
Manfred Spraul239521f2014-01-27 17:07:04 -08001427 if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
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 err = -EFAULT;
1430 goto out_free;
1431 }
1432
1433 for (i = 0; i < nsems; i++) {
1434 if (sem_io[i] > SEMVMX) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001435 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 err = -ERANGE;
1437 goto out_free;
1438 }
1439 }
Linus Torvalds4091fd942013-05-04 10:13:40 -07001440 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001441 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001442 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001444 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001447 for (i = 0; i < nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07001448 sma->sems[i].semval = sem_io[i];
1449 sma->sems[i].sempid = task_tgid_vnr(current);
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001450 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001451
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001452 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001453 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 for (i = 0; i < nsems; i++)
1455 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 sma->sem_ctime = get_seconds();
1458 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001459 do_smart_update(sma, NULL, 0, 0, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 err = 0;
1461 goto out_unlock;
1462 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001463 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465 err = -EINVAL;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001466 if (semnum < 0 || semnum >= nsems)
1467 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Rik van Riel6062a8d2013-04-30 19:15:44 -07001469 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001470 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001471 err = -EIDRM;
1472 goto out_unlock;
1473 }
Manfred Spraul1a233952017-07-12 14:34:38 -07001474 curr = &sma->sems[semnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 switch (cmd) {
1477 case GETVAL:
1478 err = curr->semval;
1479 goto out_unlock;
1480 case GETPID:
1481 err = curr->sempid;
1482 goto out_unlock;
1483 case GETNCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001484 err = count_semcnt(sma, semnum, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 goto out_unlock;
1486 case GETZCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001487 err = count_semcnt(sma, semnum, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001492 sem_unlock(sma, -1);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001493out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001494 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001495 wake_up_q(&wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08001497 if (sem_io != fast_sem_io)
Kees Cookf8dbe8d2017-07-12 14:34:47 -07001498 kvfree(sem_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return err;
1500}
1501
Pierre Peiffer016d7132008-04-29 01:00:50 -07001502static inline unsigned long
1503copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Manfred Spraul239521f2014-01-27 17:07:04 -08001505 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001507 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 case IPC_OLD:
1511 {
1512 struct semid_ds tbuf_old;
1513
Manfred Spraul239521f2014-01-27 17:07:04 -08001514 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return -EFAULT;
1516
Pierre Peiffer016d7132008-04-29 01:00:50 -07001517 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1518 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1519 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 return 0;
1522 }
1523 default:
1524 return -EINVAL;
1525 }
1526}
1527
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001528/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001529 * This function handles some semctl commands which require the rwsem
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001530 * to be held in write mode.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001531 * NOTE: no locks must be held, the rwsem is taken inside this function.
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001532 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001533static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001534 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 struct sem_array *sma;
1537 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001538 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 struct kern_ipc_perm *ipcp;
1540
Manfred Spraul239521f2014-01-27 17:07:04 -08001541 if (cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001542 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001546 down_write(&sem_ids(ns).rwsem);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001547 rcu_read_lock();
1548
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001549 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1550 &semid64.sem_perm, 0);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001551 if (IS_ERR(ipcp)) {
1552 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001553 goto out_unlock1;
1554 }
Steve Grubb073115d2006-04-02 17:07:33 -04001555
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001556 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001559 if (err)
1560 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001562 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001564 sem_lock(sma, NULL, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001565 /* freeary unlocks the ipc object and rcu */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001566 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001567 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001569 sem_lock(sma, NULL, -1);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001570 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1571 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001572 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 break;
1575 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 err = -EINVAL;
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001577 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001580out_unlock0:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001581 sem_unlock(sma, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001582out_unlock1:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001583 rcu_read_unlock();
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001584out_up:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001585 up_write(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return err;
1587}
1588
Al Viroe1fd1f42013-03-05 15:04:55 -05001589SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001592 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001593 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 if (semid < 0)
1596 return -EINVAL;
1597
1598 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001599 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Manfred Spraul239521f2014-01-27 17:07:04 -08001601 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 case IPC_INFO:
1603 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001604 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001606 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 case GETALL:
1608 case GETVAL:
1609 case GETPID:
1610 case GETNCNT:
1611 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001613 return semctl_main(ns, semid, semnum, cmd, p);
1614 case SETVAL:
1615 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 case IPC_RMID:
1617 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001618 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 default:
1620 return -EINVAL;
1621 }
1622}
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624/* If the task doesn't already have a undo_list, then allocate one
1625 * here. We guarantee there is only one thread using this undo list,
1626 * and current is THE ONE
1627 *
1628 * If this allocation and assignment succeeds, but later
1629 * portions of this code fail, there is no need to free the sem_undo_list.
1630 * Just let it stay associated with the task, and it'll be freed later
1631 * at exit time.
1632 *
1633 * This can block, so callers must hold no locks.
1634 */
1635static inline int get_undo_list(struct sem_undo_list **undo_listp)
1636{
1637 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 undo_list = current->sysvsem.undo_list;
1640 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001641 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (undo_list == NULL)
1643 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001644 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001646 INIT_LIST_HEAD(&undo_list->list_proc);
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 current->sysvsem.undo_list = undo_list;
1649 }
1650 *undo_listp = undo_list;
1651 return 0;
1652}
1653
Nick Pigginbf17bb72009-12-15 16:47:28 -08001654static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001656 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Nick Pigginbf17bb72009-12-15 16:47:28 -08001658 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1659 if (un->semid == semid)
1660 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001662 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Nick Pigginbf17bb72009-12-15 16:47:28 -08001665static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1666{
1667 struct sem_undo *un;
1668
Manfred Spraul239521f2014-01-27 17:07:04 -08001669 assert_spin_locked(&ulp->lock);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001670
1671 un = __lookup_undo(ulp, semid);
1672 if (un) {
1673 list_del_rcu(&un->list_proc);
1674 list_add_rcu(&un->list_proc, &ulp->list_proc);
1675 }
1676 return un;
1677}
1678
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001679/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001680 * find_alloc_undo - lookup (and if not present create) undo array
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001681 * @ns: namespace
1682 * @semid: semaphore array id
1683 *
1684 * The function looks up (and if not present creates) the undo structure.
1685 * The size of the undo structure depends on the size of the semaphore
1686 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001687 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1688 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001689 */
1690static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
1692 struct sem_array *sma;
1693 struct sem_undo_list *ulp;
1694 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001695 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 error = get_undo_list(&ulp);
1698 if (error)
1699 return ERR_PTR(error);
1700
Manfred Spraul380af1b2008-07-25 01:48:06 -07001701 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001702 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001704 spin_unlock(&ulp->lock);
Manfred Spraul239521f2014-01-27 17:07:04 -08001705 if (likely(un != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 goto out;
1707
1708 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001709 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001710 sma = sem_obtain_object_check(ns, semid);
1711 if (IS_ERR(sma)) {
1712 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001713 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001714 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 nsems = sma->sem_nsems;
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001717 if (!ipc_rcu_getref(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001718 rcu_read_unlock();
1719 un = ERR_PTR(-EIDRM);
1720 goto out;
1721 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001722 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001724 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001725 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (!new) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001727 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return ERR_PTR(-ENOMEM);
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Manfred Spraul380af1b2008-07-25 01:48:06 -07001731 /* step 3: Acquire the lock on semaphore array */
Linus Torvalds4091fd942013-05-04 10:13:40 -07001732 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001733 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001734 if (!ipc_valid_object(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001735 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001736 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 kfree(new);
1738 un = ERR_PTR(-EIDRM);
1739 goto out;
1740 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001741 spin_lock(&ulp->lock);
1742
1743 /*
1744 * step 4: check for races: did someone else allocate the undo struct?
1745 */
1746 un = lookup_undo(ulp, semid);
1747 if (un) {
1748 kfree(new);
1749 goto success;
1750 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001751 /* step 5: initialize & link new undo structure */
1752 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001753 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001754 new->semid = semid;
1755 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001756 list_add_rcu(&new->list_proc, &ulp->list_proc);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001757 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001758 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001759 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001760
1761success:
1762 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001763 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764out:
1765 return un;
1766}
1767
Heiko Carstensd5460c92009-01-14 14:14:27 +01001768SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1769 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 int error = -EINVAL;
1772 struct sem_array *sma;
1773 struct sembuf fast_sops[SEMOPM_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001774 struct sembuf *sops = fast_sops, *sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 struct sem_undo *un;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001776 int max, locknum;
1777 bool undos = false, alter = false, dupsop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 struct sem_queue queue;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001779 unsigned long dup = 0, jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001780 struct ipc_namespace *ns;
1781
1782 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 if (nsops < 1 || semid < 0)
1785 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001786 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return -E2BIG;
Manfred Spraul239521f2014-01-27 17:07:04 -08001788 if (nsops > SEMOPM_FAST) {
1789 sops = kmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
1790 if (sops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return -ENOMEM;
1792 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001793
Manfred Spraul239521f2014-01-27 17:07:04 -08001794 if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
1795 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 goto out_free;
1797 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (timeout) {
1800 struct timespec _timeout;
1801 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1802 error = -EFAULT;
1803 goto out_free;
1804 }
1805 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1806 _timeout.tv_nsec >= 1000000000L) {
1807 error = -EINVAL;
1808 goto out_free;
1809 }
1810 jiffies_left = timespec_to_jiffies(&_timeout);
1811 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 max = 0;
1814 for (sop = sops; sop < sops + nsops; sop++) {
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001815 unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (sop->sem_num >= max)
1818 max = sop->sem_num;
1819 if (sop->sem_flg & SEM_UNDO)
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001820 undos = true;
1821 if (dup & mask) {
1822 /*
1823 * There was a previous alter access that appears
1824 * to have accessed the same semaphore, thus use
1825 * the dupsop logic. "appears", because the detection
1826 * can only check % BITS_PER_LONG.
1827 */
1828 dupsop = true;
1829 }
1830 if (sop->sem_op != 0) {
1831 alter = true;
1832 dup |= mask;
1833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001837 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001838 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (IS_ERR(un)) {
1840 error = PTR_ERR(un);
1841 goto out_free;
1842 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001843 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001845 rcu_read_lock();
1846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001848 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001849 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001850 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001851 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001853 }
1854
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001855 error = -EFBIG;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001856 if (max >= sma->sem_nsems) {
1857 rcu_read_unlock();
1858 goto out_free;
1859 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001860
1861 error = -EACCES;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001862 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
1863 rcu_read_unlock();
1864 goto out_free;
1865 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001866
1867 error = security_sem_semop(sma, sops, nsops, alter);
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001868 if (error) {
1869 rcu_read_unlock();
1870 goto out_free;
1871 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001872
Manfred Spraul6e224f92013-10-16 13:46:45 -07001873 error = -EIDRM;
1874 locknum = sem_lock(sma, sops, nsops);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001875 /*
1876 * We eventually might perform the following check in a lockless
1877 * fashion, considering ipc_valid_object() locking constraints.
1878 * If nsops == 1 and there is no contention for sem_perm.lock, then
1879 * only a per-semaphore lock is held and it's OK to proceed with the
1880 * check below. More details on the fine grained locking scheme
1881 * entangled here and why it's RMID race safe on comments at sem_lock()
1882 */
1883 if (!ipc_valid_object(&sma->sem_perm))
Manfred Spraul6e224f92013-10-16 13:46:45 -07001884 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001886 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001888 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001889 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001890 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001892 if (un && un->semid == -1)
1893 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001894
Manfred Sprauld198cd62014-06-06 14:37:49 -07001895 queue.sops = sops;
1896 queue.nsops = nsops;
1897 queue.undo = un;
1898 queue.pid = task_tgid_vnr(current);
1899 queue.alter = alter;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001900 queue.dupsop = dupsop;
Manfred Sprauld198cd62014-06-06 14:37:49 -07001901
1902 error = perform_atomic_semop(sma, &queue);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001903 if (error == 0) { /* non-blocking succesfull path */
1904 DEFINE_WAKE_Q(wake_q);
1905
1906 /*
1907 * If the operation was successful, then do
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001908 * the required updates.
1909 */
1910 if (alter)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001911 do_smart_update(sma, sops, nsops, 1, &wake_q);
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001912 else
1913 set_semotime(sma, sops);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001914
1915 sem_unlock(sma, locknum);
1916 rcu_read_unlock();
1917 wake_up_q(&wake_q);
1918
1919 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001921 if (error < 0) /* non-blocking error path */
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001922 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001924 /*
1925 * We need to sleep on this operation, so we put the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 * task into the pending queue and go to sleep.
1927 */
Manfred Spraulb97e8202009-12-15 16:47:32 -08001928 if (nsops == 1) {
1929 struct sem *curr;
Manfred Spraul1a233952017-07-12 14:34:38 -07001930 curr = &sma->sems[sops->sem_num];
Manfred Spraulb97e8202009-12-15 16:47:32 -08001931
Manfred Spraulf269f402013-07-08 16:01:24 -07001932 if (alter) {
1933 if (sma->complex_count) {
1934 list_add_tail(&queue.list,
1935 &sma->pending_alter);
1936 } else {
1937
1938 list_add_tail(&queue.list,
1939 &curr->pending_alter);
1940 }
1941 } else {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001942 list_add_tail(&queue.list, &curr->pending_const);
Manfred Spraulf269f402013-07-08 16:01:24 -07001943 }
Manfred Spraulb97e8202009-12-15 16:47:32 -08001944 } else {
Manfred Spraulf269f402013-07-08 16:01:24 -07001945 if (!sma->complex_count)
1946 merge_queues(sma);
1947
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001948 if (alter)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001949 list_add_tail(&queue.list, &sma->pending_alter);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001950 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001951 list_add_tail(&queue.list, &sma->pending_const);
1952
Manfred Spraulb97e8202009-12-15 16:47:32 -08001953 sma->complex_count++;
1954 }
1955
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001956 do {
1957 queue.status = -EINTR;
1958 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001959
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001960 __set_current_state(TASK_INTERRUPTIBLE);
1961 sem_unlock(sma, locknum);
Linus Torvalds321310c2013-05-04 10:47:57 -07001962 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001964 if (timeout)
1965 jiffies_left = schedule_timeout(jiffies_left);
1966 else
1967 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001969 /*
1970 * fastpath: the semop has completed, either successfully or
1971 * not, from the syscall pov, is quite irrelevant to us at this
1972 * point; we're done.
1973 *
1974 * We _do_ care, nonetheless, about being awoken by a signal or
1975 * spuriously. The queue.status is checked again in the
1976 * slowpath (aka after taking sem_lock), such that we can detect
1977 * scenarios where we were awakened externally, during the
1978 * window between wake_q_add() and wake_up_q().
1979 */
1980 error = READ_ONCE(queue.status);
1981 if (error != -EINTR) {
1982 /*
1983 * User space could assume that semop() is a memory
1984 * barrier: Without the mb(), the cpu could
1985 * speculatively read in userspace stale data that was
1986 * overwritten by the previous owner of the semaphore.
1987 */
1988 smp_mb();
1989 goto out_free;
1990 }
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001991
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001992 rcu_read_lock();
Manfred Spraulc626bc42017-01-10 16:57:48 -08001993 locknum = sem_lock(sma, sops, nsops);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001994
Davidlohr Bueso370b2622016-12-14 15:06:49 -08001995 if (!ipc_valid_object(&sma->sem_perm))
1996 goto out_unlock_free;
1997
1998 error = READ_ONCE(queue.status);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001999
2000 /*
2001 * If queue.status != -EINTR we are woken up by another process.
2002 * Leave without unlink_queue(), but with sem_unlock().
2003 */
2004 if (error != -EINTR)
2005 goto out_unlock_free;
2006
2007 /*
2008 * If an interrupt occurred we have to clean up the queue.
2009 */
2010 if (timeout && jiffies_left == 0)
2011 error = -EAGAIN;
2012 } while (error == -EINTR && !signal_pending(current)); /* spurious */
Manfred Spraul0b0577f2011-11-02 13:38:52 -07002013
Manfred Spraulb97e8202009-12-15 16:47:32 -08002014 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016out_unlock_free:
Rik van Riel6062a8d2013-04-30 19:15:44 -07002017 sem_unlock(sma, locknum);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002018 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08002020 if (sops != fast_sops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 kfree(sops);
2022 return error;
2023}
2024
Heiko Carstensd5460c92009-01-14 14:14:27 +01002025SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
2026 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
2028 return sys_semtimedop(semid, tsops, nsops, NULL);
2029}
2030
2031/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
2032 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 */
2034
2035int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
2036{
2037 struct sem_undo_list *undo_list;
2038 int error;
2039
2040 if (clone_flags & CLONE_SYSVSEM) {
2041 error = get_undo_list(&undo_list);
2042 if (error)
2043 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 atomic_inc(&undo_list->refcnt);
2045 tsk->sysvsem.undo_list = undo_list;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07002046 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 tsk->sysvsem.undo_list = NULL;
2048
2049 return 0;
2050}
2051
2052/*
2053 * add semadj values to semaphores, free undo structures.
2054 * undo structures are not freed when semaphore arrays are destroyed
2055 * so some of them may be out of date.
2056 * IMPLEMENTATION NOTE: There is some confusion over whether the
2057 * set of adjustments that needs to be done should be done in an atomic
2058 * manner or not. That is, if we are attempting to decrement the semval
2059 * should we queue up and wait until we can do so legally?
2060 * The original implementation attempted to do this (queue and wait).
2061 * The current implementation does not do so. The POSIX standard
2062 * and SVID should be consulted to determine what behavior is mandated.
2063 */
2064void exit_sem(struct task_struct *tsk)
2065{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002066 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002068 ulp = tsk->sysvsem.undo_list;
2069 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002071 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002073 if (!atomic_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return;
2075
Manfred Spraul380af1b2008-07-25 01:48:06 -07002076 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07002078 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002079 int semid, i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002080 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Nikolay Borisov2a1613a2016-10-11 13:55:05 -07002082 cond_resched();
2083
Manfred Spraul380af1b2008-07-25 01:48:06 -07002084 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02002085 un = list_entry_rcu(ulp->list_proc.next,
2086 struct sem_undo, list_proc);
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002087 if (&un->list_proc == &ulp->list_proc) {
2088 /*
2089 * We must wait for freeary() before freeing this ulp,
2090 * in case we raced with last sem_undo. There is a small
2091 * possibility where we exit while freeary() didn't
2092 * finish unlocking sem_undo_list.
2093 */
Paul E. McKenneye0892e02017-06-29 12:59:32 -07002094 spin_lock(&ulp->lock);
2095 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002096 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002097 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002098 }
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002099 spin_lock(&ulp->lock);
2100 semid = un->semid;
2101 spin_unlock(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002102
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002103 /* exit_sem raced with IPC_RMID, nothing to do */
2104 if (semid == -1) {
2105 rcu_read_unlock();
2106 continue;
2107 }
2108
2109 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002110 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002111 if (IS_ERR(sma)) {
2112 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002113 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Rik van Riel6062a8d2013-04-30 19:15:44 -07002116 sem_lock(sma, NULL, -1);
Manfred Spraul6e224f92013-10-16 13:46:45 -07002117 /* exit_sem raced with IPC_RMID, nothing to do */
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08002118 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07002119 sem_unlock(sma, -1);
2120 rcu_read_unlock();
2121 continue;
2122 }
Nick Pigginbf17bb72009-12-15 16:47:28 -08002123 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002124 if (un == NULL) {
2125 /* exit_sem raced with IPC_RMID+semget() that created
2126 * exactly the same semid. Nothing to do.
2127 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002128 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002129 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002130 continue;
2131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Manfred Spraul380af1b2008-07-25 01:48:06 -07002133 /* remove un from the linked lists */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07002134 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002135 list_del(&un->list_id);
2136
Herton R. Krzesinskia9795582015-08-14 15:35:05 -07002137 /* we are the last process using this ulp, acquiring ulp->lock
2138 * isn't required. Besides that, we are also protected against
2139 * IPC_RMID as we hold sma->sem_perm lock now
2140 */
Manfred Spraul380af1b2008-07-25 01:48:06 -07002141 list_del_rcu(&un->list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002142
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002143 /* perform adjustments registered in un */
2144 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07002145 struct sem *semaphore = &sma->sems[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002146 if (un->semadj[i]) {
2147 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 /*
2149 * Range checks of the new semaphore value,
2150 * not defined by sus:
2151 * - Some unices ignore the undo entirely
2152 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2153 * - some cap the value (e.g. FreeBSD caps
2154 * at 0, but doesn't enforce SEMVMX)
2155 *
2156 * Linux caps the semaphore value, both at 0
2157 * and at SEMVMX.
2158 *
Manfred Spraul239521f2014-01-27 17:07:04 -08002159 * Manfred <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08002161 if (semaphore->semval < 0)
2162 semaphore->semval = 0;
2163 if (semaphore->semval > SEMVMX)
2164 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002165 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
2167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002169 do_smart_update(sma, NULL, 0, 1, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002170 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002171 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002172 wake_up_q(&wake_q);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002173
Lai Jiangshan693a8b62011-03-18 12:09:35 +08002174 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002176 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
2179#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07002180static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08002182 struct user_namespace *user_ns = seq_user_ns(s);
Kees Cookade9f912017-08-02 13:32:21 -07002183 struct kern_ipc_perm *ipcp = it;
2184 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002185 time_t sem_otime;
2186
Manfred Sprauld8c63372013-09-30 13:45:07 -07002187 /*
2188 * The proc interface isn't aware of sem_lock(), it calls
2189 * ipc_lock_object() directly (in sysvipc_find_ipc).
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002190 * In order to stay compatible with sem_lock(), we must
2191 * enter / leave complex_mode.
Manfred Sprauld8c63372013-09-30 13:45:07 -07002192 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002193 complexmode_enter(sma);
Manfred Sprauld8c63372013-09-30 13:45:07 -07002194
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002195 sem_otime = get_semotime(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Joe Perches7f032d62015-04-15 16:17:54 -07002197 seq_printf(s,
2198 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
2199 sma->sem_perm.key,
2200 sma->sem_perm.id,
2201 sma->sem_perm.mode,
2202 sma->sem_nsems,
2203 from_kuid_munged(user_ns, sma->sem_perm.uid),
2204 from_kgid_munged(user_ns, sma->sem_perm.gid),
2205 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2206 from_kgid_munged(user_ns, sma->sem_perm.cgid),
2207 sem_otime,
2208 sma->sem_ctime);
2209
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002210 complexmode_tryleave(sma);
2211
Joe Perches7f032d62015-04-15 16:17:54 -07002212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214#endif