blob: 084e1f55ca72ade99873154dff5e5bb4eac1da95 [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 Molnar5f921ae2006-03-26 01:37:17 -080085
Paul McQuade7153e402014-06-06 14:37:37 -070086#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include "util.h"
88
Manfred Spraule57940d2011-11-02 13:38:54 -070089/* One semaphore structure for each semaphore in the system. */
90struct sem {
91 int semval; /* current value */
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -070092 /*
93 * PID of the process that last modified the semaphore. For
94 * Linux, specifically these are:
95 * - semop
96 * - semctl, via SETVAL and SETALL.
97 * - at task exit when performing undo adjustments (see exit_sem).
98 */
99 int sempid;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700100 spinlock_t lock; /* spinlock for fine-grained semtimedop */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700101 struct list_head pending_alter; /* pending single-sop operations */
102 /* that alter the semaphore */
103 struct list_head pending_const; /* pending single-sop operations */
104 /* that do not alter the semaphore*/
Manfred Sprauld12e1e52013-07-08 16:01:25 -0700105 time_t sem_otime; /* candidate for sem_otime */
Manfred Spraulf5c936c2013-07-08 16:01:22 -0700106} ____cacheline_aligned_in_smp;
Manfred Spraule57940d2011-11-02 13:38:54 -0700107
108/* One queue for each sleeping process in the system. */
109struct sem_queue {
Manfred Spraule57940d2011-11-02 13:38:54 -0700110 struct list_head list; /* queue of pending operations */
111 struct task_struct *sleeper; /* this process */
112 struct sem_undo *undo; /* undo structure */
113 int pid; /* process id of requesting process */
114 int status; /* completion status of operation */
115 struct sembuf *sops; /* array of pending operations */
Manfred Sprauled247b72014-06-06 14:37:49 -0700116 struct sembuf *blocking; /* the operation that blocked */
Manfred Spraule57940d2011-11-02 13:38:54 -0700117 int nsops; /* number of operations */
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800118 bool alter; /* does *sops alter the array? */
119 bool dupsop; /* sops on more than one sem_num */
Manfred Spraule57940d2011-11-02 13:38:54 -0700120};
121
122/* Each task has a list of undo requests. They are executed automatically
123 * when the process exits.
124 */
125struct sem_undo {
126 struct list_head list_proc; /* per-process list: *
127 * all undos from one process
128 * rcu protected */
129 struct rcu_head rcu; /* rcu struct for sem_undo */
130 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
131 struct list_head list_id; /* per semaphore array list:
132 * all undos for one array */
133 int semid; /* semaphore set identifier */
134 short *semadj; /* array of adjustments */
135 /* one per semaphore */
136};
137
138/* sem_undo_list controls shared access to the list of sem_undo structures
139 * that may be shared among all a CLONE_SYSVSEM task group.
140 */
141struct sem_undo_list {
142 atomic_t refcnt;
143 spinlock_t lock;
144 struct list_head list_proc;
145};
146
147
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800148#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Nadia Derbey1b531f22007-10-18 23:40:55 -0700150#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700152static int newary(struct ipc_namespace *, struct ipc_params *);
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800153static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700155static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#endif
157
158#define SEMMSL_FAST 256 /* 512 bytes on stack */
159#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
160
161/*
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700162 * Locking:
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700163 * a) global sem_lock() for read/write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * sem_undo.id_next,
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700165 * sem_array.complex_count,
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700166 * sem_array.complex_mode
167 * sem_array.pending{_alter,_const},
168 * sem_array.sem_undo
Paul McQuade46c0a8c2014-06-06 14:37:37 -0700169 *
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700170 * b) global or semaphore sem_lock() for read/write:
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700171 * sem_array.sem_base[i].pending_{const,alter}:
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700172 * sem_array.complex_mode (for read)
173 *
174 * c) special:
175 * sem_undo_list.list_proc:
176 * * undo_list->lock for write
177 * * rcu for read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
179
Kirill Korotaeve3893532006-10-02 02:18:22 -0700180#define sc_semmsl sem_ctls[0]
181#define sc_semmns sem_ctls[1]
182#define sc_semopm sem_ctls[2]
183#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800185void sem_init_ns(struct ipc_namespace *ns)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700186{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700187 ns->sc_semmsl = SEMMSL;
188 ns->sc_semmns = SEMMNS;
189 ns->sc_semopm = SEMOPM;
190 ns->sc_semmni = SEMMNI;
191 ns->used_sems = 0;
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800192 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700193}
194
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800195#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700196void sem_exit_ns(struct ipc_namespace *ns)
197{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800198 free_ipcs(ns, &sem_ids(ns), freeary);
Serge E. Hallyn7d6feeb2009-12-15 16:47:27 -0800199 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700200}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800201#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Manfred Spraul239521f2014-01-27 17:07:04 -0800203void __init sem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800205 sem_init_ns(&init_ipc_ns);
Mike Waychison19b49462005-09-06 15:17:10 -0700206 ipc_init_proc_interface("sysvipc/sem",
207 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700208 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Manfred Spraulf269f402013-07-08 16:01:24 -0700211/**
212 * unmerge_queues - unmerge queues, if possible.
213 * @sma: semaphore array
214 *
215 * The function unmerges the wait queues if complex_count is 0.
216 * It must be called prior to dropping the global semaphore array lock.
217 */
218static void unmerge_queues(struct sem_array *sma)
219{
220 struct sem_queue *q, *tq;
221
222 /* complex operations still around? */
223 if (sma->complex_count)
224 return;
225 /*
226 * We will switch back to simple mode.
227 * Move all pending operation back into the per-semaphore
228 * queues.
229 */
230 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
231 struct sem *curr;
232 curr = &sma->sem_base[q->sops[0].sem_num];
233
234 list_add_tail(&q->list, &curr->pending_alter);
235 }
236 INIT_LIST_HEAD(&sma->pending_alter);
237}
238
239/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800240 * merge_queues - merge single semop queues into global queue
Manfred Spraulf269f402013-07-08 16:01:24 -0700241 * @sma: semaphore array
242 *
243 * This function merges all per-semaphore queues into the global queue.
244 * It is necessary to achieve FIFO ordering for the pending single-sop
245 * operations when a multi-semop operation must sleep.
246 * Only the alter operations must be moved, the const operations can stay.
247 */
248static void merge_queues(struct sem_array *sma)
249{
250 int i;
251 for (i = 0; i < sma->sem_nsems; i++) {
252 struct sem *sem = sma->sem_base + i;
253
254 list_splice_init(&sem->pending_alter, &sma->pending_alter);
255 }
256}
257
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700258static void sem_rcu_free(struct rcu_head *head)
259{
260 struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
261 struct sem_array *sma = ipc_rcu_to_struct(p);
262
263 security_sem_free(sma);
264 ipc_rcu_free(head);
265}
266
Nadia Derbey3e148c72007-10-18 23:40:54 -0700267/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700268 * Enter the mode suitable for non-simple operations:
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700269 * Caller must own sem_perm.lock.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700270 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700271static void complexmode_enter(struct sem_array *sma)
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700272{
273 int i;
274 struct sem *sem;
275
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700276 if (sma->complex_mode) {
277 /* We are already in complex_mode. Nothing to do */
Manfred Spraul6d07b682013-09-30 13:45:06 -0700278 return;
279 }
280
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700281 /* We need a full barrier after seting complex_mode:
282 * The write to complex_mode must be visible
283 * before we read the first sem->lock spinlock state.
284 */
285 smp_store_mb(sma->complex_mode, true);
286
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700287 for (i = 0; i < sma->sem_nsems; i++) {
288 sem = sma->sem_base + i;
289 spin_unlock_wait(&sem->lock);
290 }
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700291 /*
292 * spin_unlock_wait() is not a memory barriers, it is only a
293 * control barrier. The code must pair with spin_unlock(&sem->lock),
294 * thus just the control barrier is insufficient.
295 *
296 * smp_rmb() is sufficient, as writes cannot pass the control barrier.
297 */
298 smp_rmb();
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700299}
300
301/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700302 * Try to leave the mode that disallows simple operations:
303 * Caller must own sem_perm.lock.
304 */
305static void complexmode_tryleave(struct sem_array *sma)
306{
307 if (sma->complex_count) {
308 /* Complex ops are sleeping.
309 * We must stay in complex mode
310 */
311 return;
312 }
313 /*
314 * Immediately after setting complex_mode to false,
315 * a simple op can start. Thus: all memory writes
316 * performed by the current operation must be visible
317 * before we set complex_mode to false.
318 */
319 smp_store_release(&sma->complex_mode, false);
320}
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 *
349 * Both facts are tracked by complex_mode.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700350 */
351 sem = sma->sem_base + sops->sem_num;
352
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700353 /*
354 * Initial check for complex_mode. Just an optimization,
355 * no locking, no memory barrier.
356 */
357 if (!sma->complex_mode) {
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 Spraul5864a2f2016-10-11 13:54:50 -0700364 /*
365 * See 51d7d5205d33
366 * ("powerpc: Add smp_mb() to arch_spin_is_locked()"):
367 * A full barrier is required: the write of sem->lock
368 * must be visible before the read is executed
369 */
370 smp_mb();
Rik van Riel6062a8d2013-04-30 19:15:44 -0700371
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700372 if (!smp_load_acquire(&sma->complex_mode)) {
373 /* fast path successful! */
374 return sops->sem_num;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700375 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700376 spin_unlock(&sem->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700377 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700378
379 /* slow path: acquire the full lock */
380 ipc_lock_object(&sma->sem_perm);
381
382 if (sma->complex_count == 0) {
383 /* False alarm:
384 * There is no complex operation, thus we can switch
385 * back to the fast path.
386 */
387 spin_lock(&sem->lock);
388 ipc_unlock_object(&sma->sem_perm);
389 return sops->sem_num;
390 } else {
391 /* Not a false alarm, thus complete the sequence for a
392 * full lock.
393 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700394 complexmode_enter(sma);
395 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 {
406 struct sem *sem = sma->sem_base + locknum;
407 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 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700417static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns,
418 int id, struct sembuf *sops, int nsops, int *locknum)
Nadia Derbey023a5352007-10-18 23:40:51 -0700419{
Rik van Rielc460b662013-04-30 19:15:35 -0700420 struct kern_ipc_perm *ipcp;
421 struct sem_array *sma;
Nadia Derbey03f02c72007-10-18 23:40:51 -0700422
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700423 ipcp = ipc_obtain_object_idr(&sem_ids(ns), id);
Linus Torvalds321310c2013-05-04 10:47:57 -0700424 if (IS_ERR(ipcp))
425 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800426
Rik van Riel6062a8d2013-04-30 19:15:44 -0700427 sma = container_of(ipcp, struct sem_array, sem_perm);
428 *locknum = sem_lock(sma, sops, nsops);
Rik van Rielc460b662013-04-30 19:15:35 -0700429
430 /* ipc_rmid() may have already freed the ID while sem_lock
431 * was spinning: verify that the structure is still valid
432 */
Rafael Aquini72a8ff22014-01-27 17:07:02 -0800433 if (ipc_valid_object(ipcp))
Rik van Rielc460b662013-04-30 19:15:35 -0700434 return container_of(ipcp, struct sem_array, sem_perm);
435
Rik van Riel6062a8d2013-04-30 19:15:44 -0700436 sem_unlock(sma, *locknum);
Linus Torvalds321310c2013-05-04 10:47:57 -0700437 return ERR_PTR(-EINVAL);
Nadia Derbey023a5352007-10-18 23:40:51 -0700438}
439
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700440static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
441{
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700442 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&sem_ids(ns), id);
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700443
444 if (IS_ERR(ipcp))
445 return ERR_CAST(ipcp);
446
447 return container_of(ipcp, struct sem_array, sem_perm);
448}
449
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700450static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
451 int id)
452{
453 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
454
455 if (IS_ERR(ipcp))
456 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800457
Nadia Derbey03f02c72007-10-18 23:40:51 -0700458 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700459}
460
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700461static inline void sem_lock_and_putref(struct sem_array *sma)
462{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700463 sem_lock(sma, NULL, -1);
Fabian Frederick9b24fef2016-08-02 14:03:07 -0700464 ipc_rcu_putref(sma, sem_rcu_free);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700465}
466
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700467static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
468{
469 ipc_rmid(&sem_ids(ns), &s->sem_perm);
470}
471
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700472/**
473 * newary - Create a new semaphore set
474 * @ns: namespace
475 * @params: ptr to the structure that contains key, semflg and nsems
476 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700477 * Called with sem_ids.rwsem held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700478 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700479static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 int id;
482 int retval;
483 struct sem_array *sma;
484 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700485 key_t key = params->key;
486 int nsems = params->u.nsems;
487 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800488 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (!nsems)
491 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700492 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return -ENOSPC;
494
Manfred Spraul239521f2014-01-27 17:07:04 -0800495 size = sizeof(*sma) + nsems * sizeof(struct sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 sma = ipc_rcu_alloc(size);
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800497 if (!sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -ENOMEM;
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800499
Manfred Spraul239521f2014-01-27 17:07:04 -0800500 memset(sma, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 sma->sem_perm.mode = (semflg & S_IRWXUGO);
503 sma->sem_perm.key = key;
504
505 sma->sem_perm.security = NULL;
506 retval = security_sem_alloc(sma);
507 if (retval) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700508 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return retval;
510 }
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 sma->sem_base = (struct sem *) &sma[1];
Manfred Spraulb97e8202009-12-15 16:47:32 -0800513
Rik van Riel6062a8d2013-04-30 19:15:44 -0700514 for (i = 0; i < nsems; i++) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700515 INIT_LIST_HEAD(&sma->sem_base[i].pending_alter);
516 INIT_LIST_HEAD(&sma->sem_base[i].pending_const);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700517 spin_lock_init(&sma->sem_base[i].lock);
518 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800519
520 sma->complex_count = 0;
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700521 sma->complex_mode = true; /* dropped by sem_unlock below */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700522 INIT_LIST_HEAD(&sma->pending_alter);
523 INIT_LIST_HEAD(&sma->pending_const);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700524 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 sma->sem_nsems = nsems;
526 sma->sem_ctime = get_seconds();
Manfred Spraule8577d12014-12-02 15:59:34 -0800527
528 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
529 if (id < 0) {
530 ipc_rcu_putref(sma, sem_rcu_free);
531 return id;
532 }
533 ns->used_sems += nsems;
534
Rik van Riel6062a8d2013-04-30 19:15:44 -0700535 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -0700536 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700538 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700541
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700542/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700543 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700544 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700545static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -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 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700551}
552
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700553/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700554 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700555 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700556static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
557 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700558{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700559 struct sem_array *sma;
560
561 sma = container_of(ipcp, struct sem_array, sem_perm);
562 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700563 return -EINVAL;
564
565 return 0;
566}
567
Heiko Carstensd5460c92009-01-14 14:14:27 +0100568SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700569{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700570 struct ipc_namespace *ns;
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700571 static const struct ipc_ops sem_ops = {
572 .getnew = newary,
573 .associate = sem_security,
574 .more_checks = sem_more_checks,
575 };
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700576 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Kirill Korotaeve3893532006-10-02 02:18:22 -0700578 ns = current->nsproxy->ipc_ns;
579
580 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700582
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700583 sem_params.key = key;
584 sem_params.flg = semflg;
585 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700586
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700587 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Petr Mladek78f50092014-01-27 17:07:00 -0800590/**
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800591 * perform_atomic_semop[_slow] - Attempt to perform semaphore
592 * operations on a given array.
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700593 * @sma: semaphore array
Manfred Sprauld198cd62014-06-06 14:37:49 -0700594 * @q: struct sem_queue that describes the operation
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700595 *
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800596 * Caller blocking are as follows, based the value
597 * indicated by the semaphore operation (sem_op):
598 *
599 * (1) >0 never blocks.
600 * (2) 0 (wait-for-zero operation): semval is non-zero.
601 * (3) <0 attempting to decrement semval to a value smaller than zero.
602 *
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700603 * Returns 0 if the operation was possible.
604 * Returns 1 if the operation is impossible, the caller must sleep.
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800605 * Returns <0 for error codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800607static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Manfred Sprauld198cd62014-06-06 14:37:49 -0700609 int result, sem_op, nsops, pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct sembuf *sop;
Manfred Spraul239521f2014-01-27 17:07:04 -0800611 struct sem *curr;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700612 struct sembuf *sops;
613 struct sem_undo *un;
614
615 sops = q->sops;
616 nsops = q->nsops;
617 un = q->undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 for (sop = sops; sop < sops + nsops; sop++) {
620 curr = sma->sem_base + sop->sem_num;
621 sem_op = sop->sem_op;
622 result = curr->semval;
Petr Mladek78f50092014-01-27 17:07:00 -0800623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!sem_op && result)
625 goto would_block;
626
627 result += sem_op;
628 if (result < 0)
629 goto would_block;
630 if (result > SEMVMX)
631 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (sop->sem_flg & SEM_UNDO) {
634 int undo = un->semadj[sop->sem_num] - sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800635 /* Exceeding the undo range is an error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
637 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800638 un->semadj[sop->sem_num] = undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
Petr Mladek78f50092014-01-27 17:07:00 -0800640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 curr->semval = result;
642 }
643
644 sop--;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700645 pid = q->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 while (sop >= sops) {
647 sma->sem_base[sop->sem_num].sempid = pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 sop--;
649 }
Petr Mladek78f50092014-01-27 17:07:00 -0800650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
652
653out_of_range:
654 result = -ERANGE;
655 goto undo;
656
657would_block:
Manfred Sprauled247b72014-06-06 14:37:49 -0700658 q->blocking = sop;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (sop->sem_flg & IPC_NOWAIT)
661 result = -EAGAIN;
662 else
663 result = 1;
664
665undo:
666 sop--;
667 while (sop >= sops) {
Petr Mladek78f50092014-01-27 17:07:00 -0800668 sem_op = sop->sem_op;
669 sma->sem_base[sop->sem_num].semval -= sem_op;
670 if (sop->sem_flg & SEM_UNDO)
671 un->semadj[sop->sem_num] += sem_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 sop--;
673 }
674
675 return result;
676}
677
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800678static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
679{
680 int result, sem_op, nsops;
681 struct sembuf *sop;
682 struct sem *curr;
683 struct sembuf *sops;
684 struct sem_undo *un;
685
686 sops = q->sops;
687 nsops = q->nsops;
688 un = q->undo;
689
690 if (unlikely(q->dupsop))
691 return perform_atomic_semop_slow(sma, q);
692
693 /*
694 * We scan the semaphore set twice, first to ensure that the entire
695 * operation can succeed, therefore avoiding any pointless writes
696 * to shared memory and having to undo such changes in order to block
697 * until the operations can go through.
698 */
699 for (sop = sops; sop < sops + nsops; sop++) {
700 curr = sma->sem_base + sop->sem_num;
701 sem_op = sop->sem_op;
702 result = curr->semval;
703
704 if (!sem_op && result)
705 goto would_block; /* wait-for-zero */
706
707 result += sem_op;
708 if (result < 0)
709 goto would_block;
710
711 if (result > SEMVMX)
712 return -ERANGE;
713
714 if (sop->sem_flg & SEM_UNDO) {
715 int undo = un->semadj[sop->sem_num] - sem_op;
716
717 /* Exceeding the undo range is an error. */
718 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
719 return -ERANGE;
720 }
721 }
722
723 for (sop = sops; sop < sops + nsops; sop++) {
724 curr = sma->sem_base + sop->sem_num;
725 sem_op = sop->sem_op;
726 result = curr->semval;
727
728 if (sop->sem_flg & SEM_UNDO) {
729 int undo = un->semadj[sop->sem_num] - sem_op;
730
731 un->semadj[sop->sem_num] = undo;
732 }
733 curr->semval += sem_op;
734 curr->sempid = q->pid;
735 }
736
737 return 0;
738
739would_block:
740 q->blocking = sop;
741 return sop->sem_flg & IPC_NOWAIT ? -EAGAIN : 1;
742}
743
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800744static inline void wake_up_sem_queue_prepare(struct sem_queue *q, int error,
745 struct wake_q_head *wake_q)
Nick Piggind4212092009-12-15 16:47:30 -0800746{
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800747 wake_q_add(wake_q, q->sleeper);
748 /*
749 * Rely on the above implicit barrier, such that we can
750 * ensure that we hold reference to the task before setting
751 * q->status. Otherwise we could race with do_exit if the
752 * task is awoken by an external event before calling
753 * wake_up_process().
754 */
755 WRITE_ONCE(q->status, error);
Nick Piggind4212092009-12-15 16:47:30 -0800756}
757
Manfred Spraulb97e8202009-12-15 16:47:32 -0800758static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
759{
760 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700761 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800762 sma->complex_count--;
763}
764
Manfred Spraulfd5db422010-05-26 14:43:40 -0700765/** check_restart(sma, q)
766 * @sma: semaphore array
767 * @q: the operation that just completed
768 *
769 * update_queue is O(N^2) when it restarts scanning the whole queue of
770 * waiting operations. Therefore this function checks if the restart is
771 * really necessary. It is called after a previously waiting operation
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700772 * modified the array.
773 * Note that wait-for-zero operations are handled without restart.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700774 */
Davidlohr Bueso4663d3e2016-12-14 15:06:40 -0800775static inline int check_restart(struct sem_array *sma, struct sem_queue *q)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700776{
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700777 /* pending complex alter operations are too difficult to analyse */
778 if (!list_empty(&sma->pending_alter))
Manfred Spraulfd5db422010-05-26 14:43:40 -0700779 return 1;
780
781 /* we were a sleeping complex operation. Too difficult */
782 if (q->nsops > 1)
783 return 1;
784
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700785 /* It is impossible that someone waits for the new value:
786 * - complex operations always restart.
787 * - wait-for-zero are handled seperately.
788 * - q is a previously sleeping simple operation that
789 * altered the array. It must be a decrement, because
790 * simple increments never sleep.
791 * - If there are older (higher priority) decrements
792 * in the queue, then they have observed the original
793 * semval value and couldn't proceed. The operation
794 * decremented to value - thus they won't proceed either.
795 */
796 return 0;
797}
Manfred Spraulfd5db422010-05-26 14:43:40 -0700798
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700799/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800800 * wake_const_ops - wake up non-alter tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700801 * @sma: semaphore array.
802 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800803 * @wake_q: lockless wake-queue head.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700804 *
805 * wake_const_ops must be called after a semaphore in a semaphore array
806 * was set to 0. If complex const operations are pending, wake_const_ops must
807 * be called with semnum = -1, as well as with the number of each modified
808 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800809 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700810 * is stored in q->pid.
811 * The function returns 1 if at least one operation was completed successfully.
812 */
813static int wake_const_ops(struct sem_array *sma, int semnum,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800814 struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700815{
816 struct sem_queue *q;
817 struct list_head *walk;
818 struct list_head *pending_list;
819 int semop_completed = 0;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700820
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700821 if (semnum == -1)
822 pending_list = &sma->pending_const;
823 else
824 pending_list = &sma->sem_base[semnum].pending_const;
825
826 walk = pending_list->next;
827 while (walk != pending_list) {
828 int error;
829
830 q = container_of(walk, struct sem_queue, list);
831 walk = walk->next;
832
Manfred Sprauld198cd62014-06-06 14:37:49 -0700833 error = perform_atomic_semop(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700834
835 if (error <= 0) {
836 /* operation completed, remove from queue & wakeup */
837
838 unlink_queue(sma, q);
839
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800840 wake_up_sem_queue_prepare(q, error, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700841 if (error == 0)
842 semop_completed = 1;
843 }
844 }
845 return semop_completed;
846}
847
848/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800849 * do_smart_wakeup_zero - wakeup all wait for zero tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700850 * @sma: semaphore array
851 * @sops: operations that were performed
852 * @nsops: number of operations
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800853 * @wake_q: lockless wake-queue head
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700854 *
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800855 * Checks all required queue for wait-for-zero operations, based
856 * on the actual changes that were performed on the semaphore array.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700857 * The function returns 1 if at least one operation was completed successfully.
858 */
859static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800860 int nsops, struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700861{
862 int i;
863 int semop_completed = 0;
864 int got_zero = 0;
865
866 /* first: the per-semaphore queues, if known */
867 if (sops) {
868 for (i = 0; i < nsops; i++) {
869 int num = sops[i].sem_num;
870
871 if (sma->sem_base[num].semval == 0) {
872 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800873 semop_completed |= wake_const_ops(sma, num, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700874 }
875 }
876 } else {
877 /*
878 * No sops means modified semaphores not known.
879 * Assume all were changed.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700880 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700881 for (i = 0; i < sma->sem_nsems; i++) {
882 if (sma->sem_base[i].semval == 0) {
883 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800884 semop_completed |= wake_const_ops(sma, i, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700885 }
886 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700887 }
888 /*
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700889 * If one of the modified semaphores got 0,
890 * then check the global queue, too.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700891 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700892 if (got_zero)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800893 semop_completed |= wake_const_ops(sma, -1, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700894
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700895 return semop_completed;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700896}
897
Manfred Spraul636c6be2009-12-15 16:47:33 -0800898
899/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800900 * update_queue - look for tasks that can be completed.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800901 * @sma: semaphore array.
902 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800903 * @wake_q: lockless wake-queue head.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800904 *
905 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700906 * was modified. If multiple semaphores were modified, update_queue must
907 * be called with semnum = -1, as well as with the number of each modified
908 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800909 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700910 * is stored in q->pid.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700911 * The function internally checks if const operations can now succeed.
912 *
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700913 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800915static int update_queue(struct sem_array *sma, int semnum, struct wake_q_head *wake_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Manfred Spraul636c6be2009-12-15 16:47:33 -0800917 struct sem_queue *q;
918 struct list_head *walk;
919 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700920 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800921
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700922 if (semnum == -1)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700923 pending_list = &sma->pending_alter;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700924 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700925 pending_list = &sma->sem_base[semnum].pending_alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Nick Piggin9cad2002009-12-15 16:47:29 -0800927again:
Manfred Spraul636c6be2009-12-15 16:47:33 -0800928 walk = pending_list->next;
929 while (walk != pending_list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700930 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800931
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700932 q = container_of(walk, struct sem_queue, list);
Manfred Spraul636c6be2009-12-15 16:47:33 -0800933 walk = walk->next;
Nick Piggin9cad2002009-12-15 16:47:29 -0800934
Manfred Sprauld987f8b22009-12-15 16:47:34 -0800935 /* If we are scanning the single sop, per-semaphore list of
936 * one semaphore and that semaphore is 0, then it is not
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700937 * necessary to scan further: simple increments
Manfred Sprauld987f8b22009-12-15 16:47:34 -0800938 * that affect only one entry succeed immediately and cannot
939 * be in the per semaphore pending queue, and decrements
940 * cannot be successful if the value is already 0.
941 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700942 if (semnum != -1 && sma->sem_base[semnum].semval == 0)
Manfred Sprauld987f8b22009-12-15 16:47:34 -0800943 break;
944
Manfred Sprauld198cd62014-06-06 14:37:49 -0700945 error = perform_atomic_semop(sma, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800948 if (error > 0)
949 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700950
Manfred Spraulb97e8202009-12-15 16:47:32 -0800951 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700952
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700953 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700954 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700955 } else {
956 semop_completed = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800957 do_smart_wakeup_zero(sma, q->sops, q->nsops, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700958 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700959 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700960
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800961 wake_up_sem_queue_prepare(q, error, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700962 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800963 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700965 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700968/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800969 * set_semotime - set sem_otime
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700970 * @sma: semaphore array
971 * @sops: operations that modified the array, may be NULL
972 *
973 * sem_otime is replicated to avoid cache line trashing.
974 * This function sets one instance to the current time.
975 */
976static void set_semotime(struct sem_array *sma, struct sembuf *sops)
977{
978 if (sops == NULL) {
979 sma->sem_base[0].sem_otime = get_seconds();
980 } else {
981 sma->sem_base[sops[0].sem_num].sem_otime =
982 get_seconds();
983 }
984}
985
986/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800987 * do_smart_update - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700988 * @sma: semaphore array
989 * @sops: operations that were performed
990 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700991 * @otime: force setting otime
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800992 * @wake_q: lockless wake-queue head
Manfred Spraulfd5db422010-05-26 14:43:40 -0700993 *
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700994 * do_smart_update() does the required calls to update_queue and wakeup_zero,
995 * based on the actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700996 * Note that the function does not do the actual wake-up: the caller is
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800997 * responsible for calling wake_up_q().
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700998 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700999 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001000static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001001 int otime, struct wake_q_head *wake_q)
Manfred Spraulfd5db422010-05-26 14:43:40 -07001002{
1003 int i;
1004
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001005 otime |= do_smart_wakeup_zero(sma, sops, nsops, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001006
Manfred Spraulf269f402013-07-08 16:01:24 -07001007 if (!list_empty(&sma->pending_alter)) {
1008 /* semaphore array uses the global queue - just process it. */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001009 otime |= update_queue(sma, -1, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -07001010 } else {
1011 if (!sops) {
1012 /*
1013 * No sops, thus the modified semaphores are not
1014 * known. Check all.
1015 */
1016 for (i = 0; i < sma->sem_nsems; i++)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001017 otime |= update_queue(sma, i, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -07001018 } else {
1019 /*
1020 * Check the semaphores that were increased:
1021 * - No complex ops, thus all sleeping ops are
1022 * decrease.
1023 * - if we decreased the value, then any sleeping
1024 * semaphore ops wont be able to run: If the
1025 * previous value was too small, then the new
1026 * value will be too small, too.
1027 */
1028 for (i = 0; i < nsops; i++) {
1029 if (sops[i].sem_op > 0) {
1030 otime |= update_queue(sma,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001031 sops[i].sem_num, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -07001032 }
Manfred Spraulab465df2013-05-26 11:08:52 +02001033 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001034 }
Manfred Spraulfd5db422010-05-26 14:43:40 -07001035 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001036 if (otime)
1037 set_semotime(sma, sops);
Manfred Spraulfd5db422010-05-26 14:43:40 -07001038}
1039
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001040/*
Manfred Spraulb220c572014-06-06 14:37:51 -07001041 * check_qop: Test if a queued operation sleeps on the semaphore semnum
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001042 */
1043static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
1044 bool count_zero)
1045{
Manfred Spraulb220c572014-06-06 14:37:51 -07001046 struct sembuf *sop = q->blocking;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001047
Manfred Spraul9b44ee22014-06-06 14:37:52 -07001048 /*
1049 * Linux always (since 0.99.10) reported a task as sleeping on all
1050 * semaphores. This violates SUS, therefore it was changed to the
1051 * standard compliant behavior.
1052 * Give the administrators a chance to notice that an application
1053 * might misbehave because it relies on the Linux behavior.
1054 */
1055 pr_info_once("semctl(GETNCNT/GETZCNT) is since 3.16 Single Unix Specification compliant.\n"
1056 "The task %s (%d) triggered the difference, watch for misbehavior.\n",
1057 current->comm, task_pid_nr(current));
1058
Manfred Spraulb220c572014-06-06 14:37:51 -07001059 if (sop->sem_num != semnum)
1060 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001061
Manfred Spraulb220c572014-06-06 14:37:51 -07001062 if (count_zero && sop->sem_op == 0)
1063 return 1;
1064 if (!count_zero && sop->sem_op < 0)
1065 return 1;
1066
1067 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001068}
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070/* The following counts are associated to each semaphore:
1071 * semncnt number of tasks waiting on semval being nonzero
1072 * semzcnt number of tasks waiting on semval being zero
Manfred Spraulb220c572014-06-06 14:37:51 -07001073 *
1074 * Per definition, a task waits only on the semaphore of the first semop
1075 * that cannot proceed, even if additional operation would block, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001077static int count_semcnt(struct sem_array *sma, ushort semnum,
1078 bool count_zero)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001080 struct list_head *l;
Manfred Spraul239521f2014-01-27 17:07:04 -08001081 struct sem_queue *q;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001082 int semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001084 semcnt = 0;
1085 /* First: check the simple operations. They are easy to evaluate */
1086 if (count_zero)
1087 l = &sma->sem_base[semnum].pending_const;
1088 else
1089 l = &sma->sem_base[semnum].pending_alter;
1090
1091 list_for_each_entry(q, l, list) {
1092 /* all task on a per-semaphore list sleep on exactly
1093 * that semaphore
1094 */
1095 semcnt++;
Rik van Rielde2657f2013-05-09 16:59:59 -04001096 }
1097
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001098 /* Then: check the complex operations. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001099 list_for_each_entry(q, &sma->pending_alter, list) {
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001100 semcnt += check_qop(sma, semnum, q, count_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001102 if (count_zero) {
1103 list_for_each_entry(q, &sma->pending_const, list) {
1104 semcnt += check_qop(sma, semnum, q, count_zero);
1105 }
Rik van Rielebc2e5e2013-05-09 16:53:28 -04001106 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001107 return semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001110/* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1111 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -07001112 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001114static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Manfred Spraul380af1b2008-07-25 01:48:06 -07001116 struct sem_undo *un, *tu;
1117 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001118 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001119 int i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001120 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Manfred Spraul380af1b2008-07-25 01:48:06 -07001122 /* Free the existing undo structures for this semaphore set. */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001123 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001124 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1125 list_del(&un->list_id);
1126 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001128 list_del_rcu(&un->list_proc);
1129 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001130 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001134 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1135 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001136 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001137 }
1138
1139 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -08001140 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001141 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001143 for (i = 0; i < sma->sem_nsems; i++) {
1144 struct sem *sem = sma->sem_base + i;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001145 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1146 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001147 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001148 }
1149 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001150 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001151 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001152 }
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001155 /* Remove the semaphore set from the IDR */
1156 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001157 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001158 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001160 wake_up_q(&wake_q);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001161 ns->used_sems -= sma->sem_nsems;
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001162 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1166{
Manfred Spraul239521f2014-01-27 17:07:04 -08001167 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 case IPC_64:
1169 return copy_to_user(buf, in, sizeof(*in));
1170 case IPC_OLD:
1171 {
1172 struct semid_ds out;
1173
Dan Rosenberg982f7c22010-09-30 15:15:31 -07001174 memset(&out, 0, sizeof(out));
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1177
1178 out.sem_otime = in->sem_otime;
1179 out.sem_ctime = in->sem_ctime;
1180 out.sem_nsems = in->sem_nsems;
1181
1182 return copy_to_user(buf, &out, sizeof(out));
1183 }
1184 default:
1185 return -EINVAL;
1186 }
1187}
1188
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001189static time_t get_semotime(struct sem_array *sma)
1190{
1191 int i;
1192 time_t res;
1193
1194 res = sma->sem_base[0].sem_otime;
1195 for (i = 1; i < sma->sem_nsems; i++) {
1196 time_t to = sma->sem_base[i].sem_otime;
1197
1198 if (to > res)
1199 res = to;
1200 }
1201 return res;
1202}
1203
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001204static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001205 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Amerigo Wange5cc9c72009-12-15 16:47:35 -08001207 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 struct sem_array *sma;
1209
Manfred Spraul239521f2014-01-27 17:07:04 -08001210 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 case IPC_INFO:
1212 case SEM_INFO:
1213 {
1214 struct seminfo seminfo;
1215 int max_id;
1216
1217 err = security_sem_semctl(NULL, cmd);
1218 if (err)
1219 return err;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001220
Manfred Spraul239521f2014-01-27 17:07:04 -08001221 memset(&seminfo, 0, sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -07001222 seminfo.semmni = ns->sc_semmni;
1223 seminfo.semmns = ns->sc_semmns;
1224 seminfo.semmsl = ns->sc_semmsl;
1225 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 seminfo.semvmx = SEMVMX;
1227 seminfo.semmnu = SEMMNU;
1228 seminfo.semmap = SEMMAP;
1229 seminfo.semume = SEMUME;
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001230 down_read(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001232 seminfo.semusz = sem_ids(ns).in_use;
1233 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 } else {
1235 seminfo.semusz = SEMUSZ;
1236 seminfo.semaem = SEMAEM;
1237 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001238 max_id = ipc_get_maxid(&sem_ids(ns));
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001239 up_read(&sem_ids(ns).rwsem);
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001240 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return -EFAULT;
Manfred Spraul239521f2014-01-27 17:07:04 -08001242 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001244 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 case SEM_STAT:
1246 {
1247 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001248 int id = 0;
1249
1250 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Linus Torvalds941b0302013-05-04 11:04:29 -07001252 rcu_read_lock();
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001253 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001254 sma = sem_obtain_object(ns, semid);
1255 if (IS_ERR(sma)) {
1256 err = PTR_ERR(sma);
1257 goto out_unlock;
1258 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001259 id = sma->sem_perm.id;
1260 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001261 sma = sem_obtain_object_check(ns, semid);
1262 if (IS_ERR(sma)) {
1263 err = PTR_ERR(sma);
1264 goto out_unlock;
1265 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001269 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 goto out_unlock;
1271
1272 err = security_sem_semctl(sma, cmd);
1273 if (err)
1274 goto out_unlock;
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001277 tbuf.sem_otime = get_semotime(sma);
1278 tbuf.sem_ctime = sma->sem_ctime;
1279 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001280 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001281 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return -EFAULT;
1283 return id;
1284 }
1285 default:
1286 return -EINVAL;
1287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001289 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return err;
1291}
1292
Al Viroe1fd1f42013-03-05 15:04:55 -05001293static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1294 unsigned long arg)
1295{
1296 struct sem_undo *un;
1297 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001298 struct sem *curr;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001299 int err, val;
1300 DEFINE_WAKE_Q(wake_q);
1301
Al Viroe1fd1f42013-03-05 15:04:55 -05001302#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1303 /* big-endian 64bit */
1304 val = arg >> 32;
1305#else
1306 /* 32bit or little-endian 64bit */
1307 val = arg;
1308#endif
1309
Rik van Riel6062a8d2013-04-30 19:15:44 -07001310 if (val > SEMVMX || val < 0)
1311 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001312
Rik van Riel6062a8d2013-04-30 19:15:44 -07001313 rcu_read_lock();
1314 sma = sem_obtain_object_check(ns, semid);
1315 if (IS_ERR(sma)) {
1316 rcu_read_unlock();
1317 return PTR_ERR(sma);
1318 }
1319
1320 if (semnum < 0 || semnum >= sma->sem_nsems) {
1321 rcu_read_unlock();
1322 return -EINVAL;
1323 }
1324
1325
1326 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1327 rcu_read_unlock();
1328 return -EACCES;
1329 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001330
1331 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001332 if (err) {
1333 rcu_read_unlock();
1334 return -EACCES;
1335 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001336
Rik van Riel6062a8d2013-04-30 19:15:44 -07001337 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001338
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001339 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001340 sem_unlock(sma, -1);
1341 rcu_read_unlock();
1342 return -EIDRM;
1343 }
1344
Al Viroe1fd1f42013-03-05 15:04:55 -05001345 curr = &sma->sem_base[semnum];
1346
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001347 ipc_assert_locked_object(&sma->sem_perm);
Al Viroe1fd1f42013-03-05 15:04:55 -05001348 list_for_each_entry(un, &sma->list_id, list_id)
1349 un->semadj[semnum] = 0;
1350
1351 curr->semval = val;
1352 curr->sempid = task_tgid_vnr(current);
1353 sma->sem_ctime = get_seconds();
1354 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001355 do_smart_update(sma, NULL, 0, 0, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001356 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001357 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001358 wake_up_q(&wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001359 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001360}
1361
Kirill Korotaeve3893532006-10-02 02:18:22 -07001362static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001363 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001366 struct sem *curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001367 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 ushort fast_sem_io[SEMMSL_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001369 ushort *sem_io = fast_sem_io;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001370 DEFINE_WAKE_Q(wake_q);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001371
1372 rcu_read_lock();
1373 sma = sem_obtain_object_check(ns, semid);
1374 if (IS_ERR(sma)) {
1375 rcu_read_unlock();
1376 return PTR_ERR(sma);
1377 }
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 nsems = sma->sem_nsems;
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 err = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001382 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1383 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 err = security_sem_semctl(sma, cmd);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001386 if (err)
1387 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 err = -EACCES;
1390 switch (cmd) {
1391 case GETALL:
1392 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001393 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int i;
1395
Al Viroce857222013-05-03 00:30:49 +01001396 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001397 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001398 err = -EIDRM;
1399 goto out_unlock;
1400 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001401 if (nsems > SEMMSL_FAST) {
Al Viroce857222013-05-03 00:30:49 +01001402 if (!ipc_rcu_getref(sma)) {
Al Viroce857222013-05-03 00:30:49 +01001403 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001404 goto out_unlock;
Al Viroce857222013-05-03 00:30:49 +01001405 }
1406 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001407 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 sem_io = ipc_alloc(sizeof(ushort)*nsems);
Manfred Spraul239521f2014-01-27 17:07:04 -08001409 if (sem_io == NULL) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001410 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return -ENOMEM;
1412 }
1413
Linus Torvalds4091fd942013-05-04 10:13:40 -07001414 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001415 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001416 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001418 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
Al Viroce857222013-05-03 00:30:49 +01001420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 for (i = 0; i < sma->sem_nsems; i++)
1422 sem_io[i] = sma->sem_base[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001423 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001424 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 err = 0;
Manfred Spraul239521f2014-01-27 17:07:04 -08001426 if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 err = -EFAULT;
1428 goto out_free;
1429 }
1430 case SETALL:
1431 {
1432 int i;
1433 struct sem_undo *un;
1434
Rik van Riel6062a8d2013-04-30 19:15:44 -07001435 if (!ipc_rcu_getref(sma)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001436 err = -EIDRM;
1437 goto out_rcu_wakeup;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001438 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001439 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Manfred Spraul239521f2014-01-27 17:07:04 -08001441 if (nsems > SEMMSL_FAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 sem_io = ipc_alloc(sizeof(ushort)*nsems);
Manfred Spraul239521f2014-01-27 17:07:04 -08001443 if (sem_io == NULL) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001444 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -ENOMEM;
1446 }
1447 }
1448
Manfred Spraul239521f2014-01-27 17:07:04 -08001449 if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001450 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 err = -EFAULT;
1452 goto out_free;
1453 }
1454
1455 for (i = 0; i < nsems; i++) {
1456 if (sem_io[i] > SEMVMX) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001457 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 err = -ERANGE;
1459 goto out_free;
1460 }
1461 }
Linus Torvalds4091fd942013-05-04 10:13:40 -07001462 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001463 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001464 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001466 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001469 for (i = 0; i < nsems; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 sma->sem_base[i].semval = sem_io[i];
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001471 sma->sem_base[i].sempid = task_tgid_vnr(current);
1472 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001473
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001474 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001475 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 for (i = 0; i < nsems; i++)
1477 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 sma->sem_ctime = get_seconds();
1480 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001481 do_smart_update(sma, NULL, 0, 0, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 err = 0;
1483 goto out_unlock;
1484 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001485 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487 err = -EINVAL;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001488 if (semnum < 0 || semnum >= nsems)
1489 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Rik van Riel6062a8d2013-04-30 19:15:44 -07001491 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001492 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001493 err = -EIDRM;
1494 goto out_unlock;
1495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 curr = &sma->sem_base[semnum];
1497
1498 switch (cmd) {
1499 case GETVAL:
1500 err = curr->semval;
1501 goto out_unlock;
1502 case GETPID:
1503 err = curr->sempid;
1504 goto out_unlock;
1505 case GETNCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001506 err = count_semcnt(sma, semnum, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 goto out_unlock;
1508 case GETZCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001509 err = count_semcnt(sma, semnum, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001514 sem_unlock(sma, -1);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001515out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001516 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001517 wake_up_q(&wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08001519 if (sem_io != fast_sem_io)
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001520 ipc_free(sem_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return err;
1522}
1523
Pierre Peiffer016d7132008-04-29 01:00:50 -07001524static inline unsigned long
1525copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Manfred Spraul239521f2014-01-27 17:07:04 -08001527 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001529 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 case IPC_OLD:
1533 {
1534 struct semid_ds tbuf_old;
1535
Manfred Spraul239521f2014-01-27 17:07:04 -08001536 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return -EFAULT;
1538
Pierre Peiffer016d7132008-04-29 01:00:50 -07001539 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1540 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1541 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 return 0;
1544 }
1545 default:
1546 return -EINVAL;
1547 }
1548}
1549
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001550/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001551 * This function handles some semctl commands which require the rwsem
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001552 * to be held in write mode.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001553 * NOTE: no locks must be held, the rwsem is taken inside this function.
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001554 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001555static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001556 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 struct sem_array *sma;
1559 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001560 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 struct kern_ipc_perm *ipcp;
1562
Manfred Spraul239521f2014-01-27 17:07:04 -08001563 if (cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001564 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001568 down_write(&sem_ids(ns).rwsem);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001569 rcu_read_lock();
1570
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001571 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1572 &semid64.sem_perm, 0);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001573 if (IS_ERR(ipcp)) {
1574 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001575 goto out_unlock1;
1576 }
Steve Grubb073115d2006-04-02 17:07:33 -04001577
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001578 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001581 if (err)
1582 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001584 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001586 sem_lock(sma, NULL, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001587 /* freeary unlocks the ipc object and rcu */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001588 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001589 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001591 sem_lock(sma, NULL, -1);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001592 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1593 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001594 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 break;
1597 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 err = -EINVAL;
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001599 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001602out_unlock0:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001603 sem_unlock(sma, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001604out_unlock1:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001605 rcu_read_unlock();
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001606out_up:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001607 up_write(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return err;
1609}
1610
Al Viroe1fd1f42013-03-05 15:04:55 -05001611SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001614 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001615 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 if (semid < 0)
1618 return -EINVAL;
1619
1620 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001621 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Manfred Spraul239521f2014-01-27 17:07:04 -08001623 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 case IPC_INFO:
1625 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001626 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001628 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 case GETALL:
1630 case GETVAL:
1631 case GETPID:
1632 case GETNCNT:
1633 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001635 return semctl_main(ns, semid, semnum, cmd, p);
1636 case SETVAL:
1637 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 case IPC_RMID:
1639 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001640 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 default:
1642 return -EINVAL;
1643 }
1644}
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646/* If the task doesn't already have a undo_list, then allocate one
1647 * here. We guarantee there is only one thread using this undo list,
1648 * and current is THE ONE
1649 *
1650 * If this allocation and assignment succeeds, but later
1651 * portions of this code fail, there is no need to free the sem_undo_list.
1652 * Just let it stay associated with the task, and it'll be freed later
1653 * at exit time.
1654 *
1655 * This can block, so callers must hold no locks.
1656 */
1657static inline int get_undo_list(struct sem_undo_list **undo_listp)
1658{
1659 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 undo_list = current->sysvsem.undo_list;
1662 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001663 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (undo_list == NULL)
1665 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001666 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001668 INIT_LIST_HEAD(&undo_list->list_proc);
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 current->sysvsem.undo_list = undo_list;
1671 }
1672 *undo_listp = undo_list;
1673 return 0;
1674}
1675
Nick Pigginbf17bb72009-12-15 16:47:28 -08001676static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001678 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Nick Pigginbf17bb72009-12-15 16:47:28 -08001680 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1681 if (un->semid == semid)
1682 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001684 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
Nick Pigginbf17bb72009-12-15 16:47:28 -08001687static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1688{
1689 struct sem_undo *un;
1690
Manfred Spraul239521f2014-01-27 17:07:04 -08001691 assert_spin_locked(&ulp->lock);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001692
1693 un = __lookup_undo(ulp, semid);
1694 if (un) {
1695 list_del_rcu(&un->list_proc);
1696 list_add_rcu(&un->list_proc, &ulp->list_proc);
1697 }
1698 return un;
1699}
1700
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001701/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001702 * find_alloc_undo - lookup (and if not present create) undo array
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001703 * @ns: namespace
1704 * @semid: semaphore array id
1705 *
1706 * The function looks up (and if not present creates) the undo structure.
1707 * The size of the undo structure depends on the size of the semaphore
1708 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001709 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1710 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001711 */
1712static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct sem_array *sma;
1715 struct sem_undo_list *ulp;
1716 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001717 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 error = get_undo_list(&ulp);
1720 if (error)
1721 return ERR_PTR(error);
1722
Manfred Spraul380af1b2008-07-25 01:48:06 -07001723 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001724 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001726 spin_unlock(&ulp->lock);
Manfred Spraul239521f2014-01-27 17:07:04 -08001727 if (likely(un != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 goto out;
1729
1730 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001731 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001732 sma = sem_obtain_object_check(ns, semid);
1733 if (IS_ERR(sma)) {
1734 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001735 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001736 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 nsems = sma->sem_nsems;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001739 if (!ipc_rcu_getref(sma)) {
1740 rcu_read_unlock();
1741 un = ERR_PTR(-EIDRM);
1742 goto out;
1743 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001744 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001746 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001747 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (!new) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001749 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return ERR_PTR(-ENOMEM);
1751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Manfred Spraul380af1b2008-07-25 01:48:06 -07001753 /* step 3: Acquire the lock on semaphore array */
Linus Torvalds4091fd942013-05-04 10:13:40 -07001754 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001755 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001756 if (!ipc_valid_object(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001757 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001758 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 kfree(new);
1760 un = ERR_PTR(-EIDRM);
1761 goto out;
1762 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001763 spin_lock(&ulp->lock);
1764
1765 /*
1766 * step 4: check for races: did someone else allocate the undo struct?
1767 */
1768 un = lookup_undo(ulp, semid);
1769 if (un) {
1770 kfree(new);
1771 goto success;
1772 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001773 /* step 5: initialize & link new undo structure */
1774 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001775 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001776 new->semid = semid;
1777 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001778 list_add_rcu(&new->list_proc, &ulp->list_proc);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001779 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001780 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001781 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001782
1783success:
1784 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001785 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786out:
1787 return un;
1788}
1789
Heiko Carstensd5460c92009-01-14 14:14:27 +01001790SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1791 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 int error = -EINVAL;
1794 struct sem_array *sma;
1795 struct sembuf fast_sops[SEMOPM_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001796 struct sembuf *sops = fast_sops, *sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 struct sem_undo *un;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001798 int max, locknum;
1799 bool undos = false, alter = false, dupsop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 struct sem_queue queue;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001801 unsigned long dup = 0, jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001802 struct ipc_namespace *ns;
1803
1804 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 if (nsops < 1 || semid < 0)
1807 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001808 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return -E2BIG;
Manfred Spraul239521f2014-01-27 17:07:04 -08001810 if (nsops > SEMOPM_FAST) {
1811 sops = kmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
1812 if (sops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return -ENOMEM;
1814 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001815
Manfred Spraul239521f2014-01-27 17:07:04 -08001816 if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
1817 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 goto out_free;
1819 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (timeout) {
1822 struct timespec _timeout;
1823 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1824 error = -EFAULT;
1825 goto out_free;
1826 }
1827 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1828 _timeout.tv_nsec >= 1000000000L) {
1829 error = -EINVAL;
1830 goto out_free;
1831 }
1832 jiffies_left = timespec_to_jiffies(&_timeout);
1833 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 max = 0;
1836 for (sop = sops; sop < sops + nsops; sop++) {
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001837 unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (sop->sem_num >= max)
1840 max = sop->sem_num;
1841 if (sop->sem_flg & SEM_UNDO)
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001842 undos = true;
1843 if (dup & mask) {
1844 /*
1845 * There was a previous alter access that appears
1846 * to have accessed the same semaphore, thus use
1847 * the dupsop logic. "appears", because the detection
1848 * can only check % BITS_PER_LONG.
1849 */
1850 dupsop = true;
1851 }
1852 if (sop->sem_op != 0) {
1853 alter = true;
1854 dup |= mask;
1855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001859 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001860 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (IS_ERR(un)) {
1862 error = PTR_ERR(un);
1863 goto out_free;
1864 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001865 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001867 rcu_read_lock();
1868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001870 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001871 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001872 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001873 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001875 }
1876
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001877 error = -EFBIG;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001878 if (max >= sma->sem_nsems) {
1879 rcu_read_unlock();
1880 goto out_free;
1881 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001882
1883 error = -EACCES;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001884 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
1885 rcu_read_unlock();
1886 goto out_free;
1887 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001888
1889 error = security_sem_semop(sma, sops, nsops, alter);
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001890 if (error) {
1891 rcu_read_unlock();
1892 goto out_free;
1893 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001894
Manfred Spraul6e224f92013-10-16 13:46:45 -07001895 error = -EIDRM;
1896 locknum = sem_lock(sma, sops, nsops);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001897 /*
1898 * We eventually might perform the following check in a lockless
1899 * fashion, considering ipc_valid_object() locking constraints.
1900 * If nsops == 1 and there is no contention for sem_perm.lock, then
1901 * only a per-semaphore lock is held and it's OK to proceed with the
1902 * check below. More details on the fine grained locking scheme
1903 * entangled here and why it's RMID race safe on comments at sem_lock()
1904 */
1905 if (!ipc_valid_object(&sma->sem_perm))
Manfred Spraul6e224f92013-10-16 13:46:45 -07001906 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001908 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001910 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001911 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001912 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001914 if (un && un->semid == -1)
1915 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001916
Manfred Sprauld198cd62014-06-06 14:37:49 -07001917 queue.sops = sops;
1918 queue.nsops = nsops;
1919 queue.undo = un;
1920 queue.pid = task_tgid_vnr(current);
1921 queue.alter = alter;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001922 queue.dupsop = dupsop;
Manfred Sprauld198cd62014-06-06 14:37:49 -07001923
1924 error = perform_atomic_semop(sma, &queue);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001925 if (error == 0) { /* non-blocking succesfull path */
1926 DEFINE_WAKE_Q(wake_q);
1927
1928 /*
1929 * If the operation was successful, then do
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001930 * the required updates.
1931 */
1932 if (alter)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001933 do_smart_update(sma, sops, nsops, 1, &wake_q);
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001934 else
1935 set_semotime(sma, sops);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001936
1937 sem_unlock(sma, locknum);
1938 rcu_read_unlock();
1939 wake_up_q(&wake_q);
1940
1941 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001943 if (error < 0) /* non-blocking error path */
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001944 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001946 /*
1947 * We need to sleep on this operation, so we put the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 * task into the pending queue and go to sleep.
1949 */
Manfred Spraulb97e8202009-12-15 16:47:32 -08001950 if (nsops == 1) {
1951 struct sem *curr;
1952 curr = &sma->sem_base[sops->sem_num];
1953
Manfred Spraulf269f402013-07-08 16:01:24 -07001954 if (alter) {
1955 if (sma->complex_count) {
1956 list_add_tail(&queue.list,
1957 &sma->pending_alter);
1958 } else {
1959
1960 list_add_tail(&queue.list,
1961 &curr->pending_alter);
1962 }
1963 } else {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001964 list_add_tail(&queue.list, &curr->pending_const);
Manfred Spraulf269f402013-07-08 16:01:24 -07001965 }
Manfred Spraulb97e8202009-12-15 16:47:32 -08001966 } else {
Manfred Spraulf269f402013-07-08 16:01:24 -07001967 if (!sma->complex_count)
1968 merge_queues(sma);
1969
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001970 if (alter)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001971 list_add_tail(&queue.list, &sma->pending_alter);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001972 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001973 list_add_tail(&queue.list, &sma->pending_const);
1974
Manfred Spraulb97e8202009-12-15 16:47:32 -08001975 sma->complex_count++;
1976 }
1977
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001978sleep_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 queue.status = -EINTR;
1980 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001981
Davidlohr Bueso52644c92015-02-17 13:47:55 -08001982 __set_current_state(TASK_INTERRUPTIBLE);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001983 sem_unlock(sma, locknum);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001984 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 if (timeout)
1987 jiffies_left = schedule_timeout(jiffies_left);
1988 else
1989 schedule();
1990
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001991 /*
1992 * fastpath: the semop has completed, either successfully or not, from
1993 * the syscall pov, is quite irrelevant to us at this point; we're done.
1994 *
1995 * We _do_ care, nonetheless, about being awoken by a signal or
1996 * spuriously. The queue.status is checked again in the slowpath (aka
1997 * after taking sem_lock), such that we can detect scenarios where we
1998 * were awakened externally, during the window between wake_q_add() and
1999 * wake_up_q().
2000 */
2001 error = READ_ONCE(queue.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (error != -EINTR) {
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002003 /*
2004 * User space could assume that semop() is a memory barrier:
2005 * Without the mb(), the cpu could speculatively read in user
2006 * space stale data that was overwritten by the previous owner
2007 * of the semaphore.
Manfred Spraulc61284e2010-07-20 13:24:23 -07002008 */
2009 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 goto out_free;
2011 }
2012
Linus Torvalds321310c2013-05-04 10:47:57 -07002013 rcu_read_lock();
Rik van Riel6062a8d2013-04-30 19:15:44 -07002014 sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002015 error = READ_ONCE(queue.status);
Manfred Sprauld694ad62011-07-25 17:11:47 -07002016
2017 /*
2018 * Array removed? If yes, leave without sem_unlock().
2019 */
Nadia Derbey023a5352007-10-18 23:40:51 -07002020 if (IS_ERR(sma)) {
Linus Torvalds321310c2013-05-04 10:47:57 -07002021 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 goto out_free;
2023 }
2024
2025 /*
Manfred Sprauld694ad62011-07-25 17:11:47 -07002026 * If queue.status != -EINTR we are woken up by another process.
2027 * Leave without unlink_queue(), but with sem_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 */
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -08002029 if (error != -EINTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 /*
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002033 * If an interrupt occurred we have to clean up the queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 */
2035 if (timeout && jiffies_left == 0)
2036 error = -EAGAIN;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07002037
2038 /*
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002039 * If the wakeup was spurious, just retry.
Manfred Spraul0b0577f2011-11-02 13:38:52 -07002040 */
2041 if (error == -EINTR && !signal_pending(current))
2042 goto sleep_again;
2043
Manfred Spraulb97e8202009-12-15 16:47:32 -08002044 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046out_unlock_free:
Rik van Riel6062a8d2013-04-30 19:15:44 -07002047 sem_unlock(sma, locknum);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002048 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08002050 if (sops != fast_sops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 kfree(sops);
2052 return error;
2053}
2054
Heiko Carstensd5460c92009-01-14 14:14:27 +01002055SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
2056 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 return sys_semtimedop(semid, tsops, nsops, NULL);
2059}
2060
2061/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
2062 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 */
2064
2065int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
2066{
2067 struct sem_undo_list *undo_list;
2068 int error;
2069
2070 if (clone_flags & CLONE_SYSVSEM) {
2071 error = get_undo_list(&undo_list);
2072 if (error)
2073 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 atomic_inc(&undo_list->refcnt);
2075 tsk->sysvsem.undo_list = undo_list;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07002076 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 tsk->sysvsem.undo_list = NULL;
2078
2079 return 0;
2080}
2081
2082/*
2083 * add semadj values to semaphores, free undo structures.
2084 * undo structures are not freed when semaphore arrays are destroyed
2085 * so some of them may be out of date.
2086 * IMPLEMENTATION NOTE: There is some confusion over whether the
2087 * set of adjustments that needs to be done should be done in an atomic
2088 * manner or not. That is, if we are attempting to decrement the semval
2089 * should we queue up and wait until we can do so legally?
2090 * The original implementation attempted to do this (queue and wait).
2091 * The current implementation does not do so. The POSIX standard
2092 * and SVID should be consulted to determine what behavior is mandated.
2093 */
2094void exit_sem(struct task_struct *tsk)
2095{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002096 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002098 ulp = tsk->sysvsem.undo_list;
2099 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002101 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002103 if (!atomic_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return;
2105
Manfred Spraul380af1b2008-07-25 01:48:06 -07002106 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07002108 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002109 int semid, i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002110 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Nikolay Borisov2a1613a2016-10-11 13:55:05 -07002112 cond_resched();
2113
Manfred Spraul380af1b2008-07-25 01:48:06 -07002114 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02002115 un = list_entry_rcu(ulp->list_proc.next,
2116 struct sem_undo, list_proc);
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002117 if (&un->list_proc == &ulp->list_proc) {
2118 /*
2119 * We must wait for freeary() before freeing this ulp,
2120 * in case we raced with last sem_undo. There is a small
2121 * possibility where we exit while freeary() didn't
2122 * finish unlocking sem_undo_list.
2123 */
2124 spin_unlock_wait(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002125 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002126 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002127 }
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002128 spin_lock(&ulp->lock);
2129 semid = un->semid;
2130 spin_unlock(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002131
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002132 /* exit_sem raced with IPC_RMID, nothing to do */
2133 if (semid == -1) {
2134 rcu_read_unlock();
2135 continue;
2136 }
2137
2138 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002139 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002140 if (IS_ERR(sma)) {
2141 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002142 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Rik van Riel6062a8d2013-04-30 19:15:44 -07002145 sem_lock(sma, NULL, -1);
Manfred Spraul6e224f92013-10-16 13:46:45 -07002146 /* exit_sem raced with IPC_RMID, nothing to do */
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08002147 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07002148 sem_unlock(sma, -1);
2149 rcu_read_unlock();
2150 continue;
2151 }
Nick Pigginbf17bb72009-12-15 16:47:28 -08002152 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002153 if (un == NULL) {
2154 /* exit_sem raced with IPC_RMID+semget() that created
2155 * exactly the same semid. Nothing to do.
2156 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002157 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002158 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002159 continue;
2160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Manfred Spraul380af1b2008-07-25 01:48:06 -07002162 /* remove un from the linked lists */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07002163 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002164 list_del(&un->list_id);
2165
Herton R. Krzesinskia9795582015-08-14 15:35:05 -07002166 /* we are the last process using this ulp, acquiring ulp->lock
2167 * isn't required. Besides that, we are also protected against
2168 * IPC_RMID as we hold sma->sem_perm lock now
2169 */
Manfred Spraul380af1b2008-07-25 01:48:06 -07002170 list_del_rcu(&un->list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002171
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002172 /* perform adjustments registered in un */
2173 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul239521f2014-01-27 17:07:04 -08002174 struct sem *semaphore = &sma->sem_base[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002175 if (un->semadj[i]) {
2176 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 /*
2178 * Range checks of the new semaphore value,
2179 * not defined by sus:
2180 * - Some unices ignore the undo entirely
2181 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2182 * - some cap the value (e.g. FreeBSD caps
2183 * at 0, but doesn't enforce SEMVMX)
2184 *
2185 * Linux caps the semaphore value, both at 0
2186 * and at SEMVMX.
2187 *
Manfred Spraul239521f2014-01-27 17:07:04 -08002188 * Manfred <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08002190 if (semaphore->semval < 0)
2191 semaphore->semval = 0;
2192 if (semaphore->semval > SEMVMX)
2193 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002194 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002198 do_smart_update(sma, NULL, 0, 1, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002199 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002200 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002201 wake_up_q(&wake_q);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002202
Lai Jiangshan693a8b62011-03-18 12:09:35 +08002203 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002205 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
2208#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07002209static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08002211 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07002212 struct sem_array *sma = it;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002213 time_t sem_otime;
2214
Manfred Sprauld8c63372013-09-30 13:45:07 -07002215 /*
2216 * The proc interface isn't aware of sem_lock(), it calls
2217 * ipc_lock_object() directly (in sysvipc_find_ipc).
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002218 * In order to stay compatible with sem_lock(), we must
2219 * enter / leave complex_mode.
Manfred Sprauld8c63372013-09-30 13:45:07 -07002220 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002221 complexmode_enter(sma);
Manfred Sprauld8c63372013-09-30 13:45:07 -07002222
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002223 sem_otime = get_semotime(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Joe Perches7f032d62015-04-15 16:17:54 -07002225 seq_printf(s,
2226 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
2227 sma->sem_perm.key,
2228 sma->sem_perm.id,
2229 sma->sem_perm.mode,
2230 sma->sem_nsems,
2231 from_kuid_munged(user_ns, sma->sem_perm.uid),
2232 from_kgid_munged(user_ns, sma->sem_perm.gid),
2233 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2234 from_kgid_munged(user_ns, sma->sem_perm.cgid),
2235 sem_otime,
2236 sma->sem_ctime);
2237
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002238 complexmode_tryleave(sma);
2239
Joe Perches7f032d62015-04-15 16:17:54 -07002240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242#endif