blob: fe5db1ed081b7d0153897dad133bd7dfafb8f86a [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 Spraul27d7be12017-02-27 14:28:15 -0800281 sma->complex_mode = true;
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700282
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700283 for (i = 0; i < sma->sem_nsems; i++) {
284 sem = sma->sem_base + i;
Manfred Spraul27d7be12017-02-27 14:28:15 -0800285 spin_lock(&sem->lock);
286 spin_unlock(&sem->lock);
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700287 }
288}
289
290/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700291 * Try to leave the mode that disallows simple operations:
292 * Caller must own sem_perm.lock.
293 */
294static void complexmode_tryleave(struct sem_array *sma)
295{
296 if (sma->complex_count) {
297 /* Complex ops are sleeping.
298 * We must stay in complex mode
299 */
300 return;
301 }
302 /*
303 * Immediately after setting complex_mode to false,
304 * a simple op can start. Thus: all memory writes
305 * performed by the current operation must be visible
306 * before we set complex_mode to false.
307 */
308 smp_store_release(&sma->complex_mode, false);
309}
310
311#define SEM_GLOBAL_LOCK (-1)
312/*
Rik van Riel6062a8d2013-04-30 19:15:44 -0700313 * If the request contains only one semaphore operation, and there are
314 * no complex transactions pending, lock only the semaphore involved.
315 * Otherwise, lock the entire semaphore array, since we either have
316 * multiple semaphores in our own semops, or we need to look at
317 * semaphores from other pending complex operations.
Rik van Riel6062a8d2013-04-30 19:15:44 -0700318 */
319static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
320 int nsops)
321{
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700322 struct sem *sem;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700323
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700324 if (nsops != 1) {
325 /* Complex operation - acquire a full lock */
326 ipc_lock_object(&sma->sem_perm);
327
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700328 /* Prevent parallel simple ops */
329 complexmode_enter(sma);
330 return SEM_GLOBAL_LOCK;
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700331 }
332
333 /*
334 * Only one semaphore affected - try to optimize locking.
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700335 * Optimized locking is possible if no complex operation
336 * is either enqueued or processed right now.
337 *
338 * Both facts are tracked by complex_mode.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700339 */
340 sem = sma->sem_base + sops->sem_num;
341
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700342 /*
343 * Initial check for complex_mode. Just an optimization,
344 * no locking, no memory barrier.
345 */
346 if (!sma->complex_mode) {
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700347 /*
348 * It appears that no complex operation is around.
349 * Acquire the per-semaphore lock.
350 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700351 spin_lock(&sem->lock);
352
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700353 if (!smp_load_acquire(&sma->complex_mode)) {
354 /* fast path successful! */
355 return sops->sem_num;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700356 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700357 spin_unlock(&sem->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700358 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700359
360 /* slow path: acquire the full lock */
361 ipc_lock_object(&sma->sem_perm);
362
363 if (sma->complex_count == 0) {
364 /* False alarm:
365 * There is no complex operation, thus we can switch
366 * back to the fast path.
367 */
368 spin_lock(&sem->lock);
369 ipc_unlock_object(&sma->sem_perm);
370 return sops->sem_num;
371 } else {
372 /* Not a false alarm, thus complete the sequence for a
373 * full lock.
374 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700375 complexmode_enter(sma);
376 return SEM_GLOBAL_LOCK;
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700377 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700378}
379
380static inline void sem_unlock(struct sem_array *sma, int locknum)
381{
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700382 if (locknum == SEM_GLOBAL_LOCK) {
Manfred Spraulf269f402013-07-08 16:01:24 -0700383 unmerge_queues(sma);
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700384 complexmode_tryleave(sma);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700385 ipc_unlock_object(&sma->sem_perm);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700386 } else {
387 struct sem *sem = sma->sem_base + locknum;
388 spin_unlock(&sem->lock);
389 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700390}
391
392/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700393 * sem_lock_(check_) routines are called in the paths where the rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -0700394 * is not held.
Linus Torvalds321310c2013-05-04 10:47:57 -0700395 *
396 * The caller holds the RCU read lock.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700397 */
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700398static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
399{
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700400 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&sem_ids(ns), id);
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700401
402 if (IS_ERR(ipcp))
403 return ERR_CAST(ipcp);
404
405 return container_of(ipcp, struct sem_array, sem_perm);
406}
407
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700408static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
409 int id)
410{
411 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
412
413 if (IS_ERR(ipcp))
414 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800415
Nadia Derbey03f02c72007-10-18 23:40:51 -0700416 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700417}
418
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700419static inline void sem_lock_and_putref(struct sem_array *sma)
420{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700421 sem_lock(sma, NULL, -1);
Fabian Frederick9b24fef2016-08-02 14:03:07 -0700422 ipc_rcu_putref(sma, sem_rcu_free);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700423}
424
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700425static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
426{
427 ipc_rmid(&sem_ids(ns), &s->sem_perm);
428}
429
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700430/**
431 * newary - Create a new semaphore set
432 * @ns: namespace
433 * @params: ptr to the structure that contains key, semflg and nsems
434 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700435 * Called with sem_ids.rwsem held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700436 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700437static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 int id;
440 int retval;
441 struct sem_array *sma;
442 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700443 key_t key = params->key;
444 int nsems = params->u.nsems;
445 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800446 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (!nsems)
449 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700450 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return -ENOSPC;
452
Manfred Spraul239521f2014-01-27 17:07:04 -0800453 size = sizeof(*sma) + nsems * sizeof(struct sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 sma = ipc_rcu_alloc(size);
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800455 if (!sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return -ENOMEM;
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800457
Manfred Spraul239521f2014-01-27 17:07:04 -0800458 memset(sma, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 sma->sem_perm.mode = (semflg & S_IRWXUGO);
461 sma->sem_perm.key = key;
462
463 sma->sem_perm.security = NULL;
464 retval = security_sem_alloc(sma);
465 if (retval) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700466 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return retval;
468 }
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 sma->sem_base = (struct sem *) &sma[1];
Manfred Spraulb97e8202009-12-15 16:47:32 -0800471
Rik van Riel6062a8d2013-04-30 19:15:44 -0700472 for (i = 0; i < nsems; i++) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700473 INIT_LIST_HEAD(&sma->sem_base[i].pending_alter);
474 INIT_LIST_HEAD(&sma->sem_base[i].pending_const);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700475 spin_lock_init(&sma->sem_base[i].lock);
476 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800477
478 sma->complex_count = 0;
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700479 sma->complex_mode = true; /* dropped by sem_unlock below */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700480 INIT_LIST_HEAD(&sma->pending_alter);
481 INIT_LIST_HEAD(&sma->pending_const);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700482 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 sma->sem_nsems = nsems;
484 sma->sem_ctime = get_seconds();
Manfred Spraule8577d12014-12-02 15:59:34 -0800485
486 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
487 if (id < 0) {
488 ipc_rcu_putref(sma, sem_rcu_free);
489 return id;
490 }
491 ns->used_sems += nsems;
492
Rik van Riel6062a8d2013-04-30 19:15:44 -0700493 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -0700494 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700496 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700499
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700500/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700501 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700502 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700503static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700505 struct sem_array *sma;
506
507 sma = container_of(ipcp, struct sem_array, sem_perm);
508 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700509}
510
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700511/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700512 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700513 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700514static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
515 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700516{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700517 struct sem_array *sma;
518
519 sma = container_of(ipcp, struct sem_array, sem_perm);
520 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700521 return -EINVAL;
522
523 return 0;
524}
525
Heiko Carstensd5460c92009-01-14 14:14:27 +0100526SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700527{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700528 struct ipc_namespace *ns;
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700529 static const struct ipc_ops sem_ops = {
530 .getnew = newary,
531 .associate = sem_security,
532 .more_checks = sem_more_checks,
533 };
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700534 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Kirill Korotaeve3893532006-10-02 02:18:22 -0700536 ns = current->nsproxy->ipc_ns;
537
538 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700540
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700541 sem_params.key = key;
542 sem_params.flg = semflg;
543 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700544
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700545 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Petr Mladek78f50092014-01-27 17:07:00 -0800548/**
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800549 * perform_atomic_semop[_slow] - Attempt to perform semaphore
550 * operations on a given array.
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700551 * @sma: semaphore array
Manfred Sprauld198cd62014-06-06 14:37:49 -0700552 * @q: struct sem_queue that describes the operation
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700553 *
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800554 * Caller blocking are as follows, based the value
555 * indicated by the semaphore operation (sem_op):
556 *
557 * (1) >0 never blocks.
558 * (2) 0 (wait-for-zero operation): semval is non-zero.
559 * (3) <0 attempting to decrement semval to a value smaller than zero.
560 *
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700561 * Returns 0 if the operation was possible.
562 * Returns 1 if the operation is impossible, the caller must sleep.
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800563 * Returns <0 for error codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800565static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Manfred Sprauld198cd62014-06-06 14:37:49 -0700567 int result, sem_op, nsops, pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 struct sembuf *sop;
Manfred Spraul239521f2014-01-27 17:07:04 -0800569 struct sem *curr;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700570 struct sembuf *sops;
571 struct sem_undo *un;
572
573 sops = q->sops;
574 nsops = q->nsops;
575 un = q->undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 for (sop = sops; sop < sops + nsops; sop++) {
578 curr = sma->sem_base + sop->sem_num;
579 sem_op = sop->sem_op;
580 result = curr->semval;
Petr Mladek78f50092014-01-27 17:07:00 -0800581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (!sem_op && result)
583 goto would_block;
584
585 result += sem_op;
586 if (result < 0)
587 goto would_block;
588 if (result > SEMVMX)
589 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (sop->sem_flg & SEM_UNDO) {
592 int undo = un->semadj[sop->sem_num] - sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800593 /* Exceeding the undo range is an error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
595 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800596 un->semadj[sop->sem_num] = undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Petr Mladek78f50092014-01-27 17:07:00 -0800598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 curr->semval = result;
600 }
601
602 sop--;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700603 pid = q->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 while (sop >= sops) {
605 sma->sem_base[sop->sem_num].sempid = pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 sop--;
607 }
Petr Mladek78f50092014-01-27 17:07:00 -0800608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610
611out_of_range:
612 result = -ERANGE;
613 goto undo;
614
615would_block:
Manfred Sprauled247b72014-06-06 14:37:49 -0700616 q->blocking = sop;
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (sop->sem_flg & IPC_NOWAIT)
619 result = -EAGAIN;
620 else
621 result = 1;
622
623undo:
624 sop--;
625 while (sop >= sops) {
Petr Mladek78f50092014-01-27 17:07:00 -0800626 sem_op = sop->sem_op;
627 sma->sem_base[sop->sem_num].semval -= sem_op;
628 if (sop->sem_flg & SEM_UNDO)
629 un->semadj[sop->sem_num] += sem_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 sop--;
631 }
632
633 return result;
634}
635
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800636static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
637{
638 int result, sem_op, nsops;
639 struct sembuf *sop;
640 struct sem *curr;
641 struct sembuf *sops;
642 struct sem_undo *un;
643
644 sops = q->sops;
645 nsops = q->nsops;
646 un = q->undo;
647
648 if (unlikely(q->dupsop))
649 return perform_atomic_semop_slow(sma, q);
650
651 /*
652 * We scan the semaphore set twice, first to ensure that the entire
653 * operation can succeed, therefore avoiding any pointless writes
654 * to shared memory and having to undo such changes in order to block
655 * until the operations can go through.
656 */
657 for (sop = sops; sop < sops + nsops; sop++) {
658 curr = sma->sem_base + sop->sem_num;
659 sem_op = sop->sem_op;
660 result = curr->semval;
661
662 if (!sem_op && result)
663 goto would_block; /* wait-for-zero */
664
665 result += sem_op;
666 if (result < 0)
667 goto would_block;
668
669 if (result > SEMVMX)
670 return -ERANGE;
671
672 if (sop->sem_flg & SEM_UNDO) {
673 int undo = un->semadj[sop->sem_num] - sem_op;
674
675 /* Exceeding the undo range is an error. */
676 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
677 return -ERANGE;
678 }
679 }
680
681 for (sop = sops; sop < sops + nsops; sop++) {
682 curr = sma->sem_base + sop->sem_num;
683 sem_op = sop->sem_op;
684 result = curr->semval;
685
686 if (sop->sem_flg & SEM_UNDO) {
687 int undo = un->semadj[sop->sem_num] - sem_op;
688
689 un->semadj[sop->sem_num] = undo;
690 }
691 curr->semval += sem_op;
692 curr->sempid = q->pid;
693 }
694
695 return 0;
696
697would_block:
698 q->blocking = sop;
699 return sop->sem_flg & IPC_NOWAIT ? -EAGAIN : 1;
700}
701
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800702static inline void wake_up_sem_queue_prepare(struct sem_queue *q, int error,
703 struct wake_q_head *wake_q)
Nick Piggind4212092009-12-15 16:47:30 -0800704{
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800705 wake_q_add(wake_q, q->sleeper);
706 /*
707 * Rely on the above implicit barrier, such that we can
708 * ensure that we hold reference to the task before setting
709 * q->status. Otherwise we could race with do_exit if the
710 * task is awoken by an external event before calling
711 * wake_up_process().
712 */
713 WRITE_ONCE(q->status, error);
Nick Piggind4212092009-12-15 16:47:30 -0800714}
715
Manfred Spraulb97e8202009-12-15 16:47:32 -0800716static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
717{
718 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700719 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800720 sma->complex_count--;
721}
722
Manfred Spraulfd5db422010-05-26 14:43:40 -0700723/** check_restart(sma, q)
724 * @sma: semaphore array
725 * @q: the operation that just completed
726 *
727 * update_queue is O(N^2) when it restarts scanning the whole queue of
728 * waiting operations. Therefore this function checks if the restart is
729 * really necessary. It is called after a previously waiting operation
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700730 * modified the array.
731 * Note that wait-for-zero operations are handled without restart.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700732 */
Davidlohr Bueso4663d3e2016-12-14 15:06:40 -0800733static inline int check_restart(struct sem_array *sma, struct sem_queue *q)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700734{
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700735 /* pending complex alter operations are too difficult to analyse */
736 if (!list_empty(&sma->pending_alter))
Manfred Spraulfd5db422010-05-26 14:43:40 -0700737 return 1;
738
739 /* we were a sleeping complex operation. Too difficult */
740 if (q->nsops > 1)
741 return 1;
742
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700743 /* It is impossible that someone waits for the new value:
744 * - complex operations always restart.
745 * - wait-for-zero are handled seperately.
746 * - q is a previously sleeping simple operation that
747 * altered the array. It must be a decrement, because
748 * simple increments never sleep.
749 * - If there are older (higher priority) decrements
750 * in the queue, then they have observed the original
751 * semval value and couldn't proceed. The operation
752 * decremented to value - thus they won't proceed either.
753 */
754 return 0;
755}
Manfred Spraulfd5db422010-05-26 14:43:40 -0700756
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700757/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800758 * wake_const_ops - wake up non-alter tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700759 * @sma: semaphore array.
760 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800761 * @wake_q: lockless wake-queue head.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700762 *
763 * wake_const_ops must be called after a semaphore in a semaphore array
764 * was set to 0. If complex const operations are pending, wake_const_ops must
765 * be called with semnum = -1, as well as with the number of each modified
766 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800767 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700768 * is stored in q->pid.
769 * The function returns 1 if at least one operation was completed successfully.
770 */
771static int wake_const_ops(struct sem_array *sma, int semnum,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800772 struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700773{
Davidlohr Buesof150f022016-12-14 15:06:43 -0800774 struct sem_queue *q, *tmp;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700775 struct list_head *pending_list;
776 int semop_completed = 0;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700777
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700778 if (semnum == -1)
779 pending_list = &sma->pending_const;
780 else
781 pending_list = &sma->sem_base[semnum].pending_const;
782
Davidlohr Buesof150f022016-12-14 15:06:43 -0800783 list_for_each_entry_safe(q, tmp, pending_list, list) {
784 int error = perform_atomic_semop(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700785
Davidlohr Buesof150f022016-12-14 15:06:43 -0800786 if (error > 0)
787 continue;
788 /* operation completed, remove from queue & wakeup */
789 unlink_queue(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700790
Davidlohr Buesof150f022016-12-14 15:06:43 -0800791 wake_up_sem_queue_prepare(q, error, wake_q);
792 if (error == 0)
793 semop_completed = 1;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700794 }
Davidlohr Buesof150f022016-12-14 15:06:43 -0800795
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700796 return semop_completed;
797}
798
799/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800800 * do_smart_wakeup_zero - wakeup all wait for zero tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700801 * @sma: semaphore array
802 * @sops: operations that were performed
803 * @nsops: number of operations
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800804 * @wake_q: lockless wake-queue head
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700805 *
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800806 * Checks all required queue for wait-for-zero operations, based
807 * on the actual changes that were performed on the semaphore array.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700808 * The function returns 1 if at least one operation was completed successfully.
809 */
810static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800811 int nsops, struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700812{
813 int i;
814 int semop_completed = 0;
815 int got_zero = 0;
816
817 /* first: the per-semaphore queues, if known */
818 if (sops) {
819 for (i = 0; i < nsops; i++) {
820 int num = sops[i].sem_num;
821
822 if (sma->sem_base[num].semval == 0) {
823 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800824 semop_completed |= wake_const_ops(sma, num, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700825 }
826 }
827 } else {
828 /*
829 * No sops means modified semaphores not known.
830 * Assume all were changed.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700831 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700832 for (i = 0; i < sma->sem_nsems; i++) {
833 if (sma->sem_base[i].semval == 0) {
834 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800835 semop_completed |= wake_const_ops(sma, i, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700836 }
837 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700838 }
839 /*
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700840 * If one of the modified semaphores got 0,
841 * then check the global queue, too.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700842 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700843 if (got_zero)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800844 semop_completed |= wake_const_ops(sma, -1, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700845
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700846 return semop_completed;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700847}
848
Manfred Spraul636c6be2009-12-15 16:47:33 -0800849
850/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800851 * update_queue - look for tasks that can be completed.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800852 * @sma: semaphore array.
853 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800854 * @wake_q: lockless wake-queue head.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800855 *
856 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700857 * was modified. If multiple semaphores were modified, update_queue must
858 * be called with semnum = -1, as well as with the number of each modified
859 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800860 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700861 * is stored in q->pid.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700862 * The function internally checks if const operations can now succeed.
863 *
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700864 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800866static int update_queue(struct sem_array *sma, int semnum, struct wake_q_head *wake_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Davidlohr Buesof150f022016-12-14 15:06:43 -0800868 struct sem_queue *q, *tmp;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800869 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700870 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800871
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700872 if (semnum == -1)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700873 pending_list = &sma->pending_alter;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700874 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700875 pending_list = &sma->sem_base[semnum].pending_alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Nick Piggin9cad2002009-12-15 16:47:29 -0800877again:
Davidlohr Buesof150f022016-12-14 15:06:43 -0800878 list_for_each_entry_safe(q, tmp, pending_list, list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700879 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800880
Manfred Sprauld987f8b22009-12-15 16:47:34 -0800881 /* If we are scanning the single sop, per-semaphore list of
882 * one semaphore and that semaphore is 0, then it is not
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700883 * necessary to scan further: simple increments
Manfred Sprauld987f8b22009-12-15 16:47:34 -0800884 * that affect only one entry succeed immediately and cannot
885 * be in the per semaphore pending queue, and decrements
886 * cannot be successful if the value is already 0.
887 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700888 if (semnum != -1 && sma->sem_base[semnum].semval == 0)
Manfred Sprauld987f8b22009-12-15 16:47:34 -0800889 break;
890
Manfred Sprauld198cd62014-06-06 14:37:49 -0700891 error = perform_atomic_semop(sma, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800894 if (error > 0)
895 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700896
Manfred Spraulb97e8202009-12-15 16:47:32 -0800897 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700898
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700899 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700900 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700901 } else {
902 semop_completed = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800903 do_smart_wakeup_zero(sma, q->sops, q->nsops, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700904 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700905 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700906
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800907 wake_up_sem_queue_prepare(q, error, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700908 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800909 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700911 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700914/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800915 * set_semotime - set sem_otime
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700916 * @sma: semaphore array
917 * @sops: operations that modified the array, may be NULL
918 *
919 * sem_otime is replicated to avoid cache line trashing.
920 * This function sets one instance to the current time.
921 */
922static void set_semotime(struct sem_array *sma, struct sembuf *sops)
923{
924 if (sops == NULL) {
925 sma->sem_base[0].sem_otime = get_seconds();
926 } else {
927 sma->sem_base[sops[0].sem_num].sem_otime =
928 get_seconds();
929 }
930}
931
932/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800933 * do_smart_update - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700934 * @sma: semaphore array
935 * @sops: operations that were performed
936 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700937 * @otime: force setting otime
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800938 * @wake_q: lockless wake-queue head
Manfred Spraulfd5db422010-05-26 14:43:40 -0700939 *
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700940 * do_smart_update() does the required calls to update_queue and wakeup_zero,
941 * based on the actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700942 * Note that the function does not do the actual wake-up: the caller is
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800943 * responsible for calling wake_up_q().
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700944 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700945 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700946static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800947 int otime, struct wake_q_head *wake_q)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700948{
949 int i;
950
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800951 otime |= do_smart_wakeup_zero(sma, sops, nsops, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700952
Manfred Spraulf269f402013-07-08 16:01:24 -0700953 if (!list_empty(&sma->pending_alter)) {
954 /* semaphore array uses the global queue - just process it. */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800955 otime |= update_queue(sma, -1, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -0700956 } else {
957 if (!sops) {
958 /*
959 * No sops, thus the modified semaphores are not
960 * known. Check all.
961 */
962 for (i = 0; i < sma->sem_nsems; i++)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800963 otime |= update_queue(sma, i, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -0700964 } else {
965 /*
966 * Check the semaphores that were increased:
967 * - No complex ops, thus all sleeping ops are
968 * decrease.
969 * - if we decreased the value, then any sleeping
970 * semaphore ops wont be able to run: If the
971 * previous value was too small, then the new
972 * value will be too small, too.
973 */
974 for (i = 0; i < nsops; i++) {
975 if (sops[i].sem_op > 0) {
976 otime |= update_queue(sma,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800977 sops[i].sem_num, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -0700978 }
Manfred Spraulab465df2013-05-26 11:08:52 +0200979 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700980 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700981 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700982 if (otime)
983 set_semotime(sma, sops);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700984}
985
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -0700986/*
Manfred Spraulb220c572014-06-06 14:37:51 -0700987 * check_qop: Test if a queued operation sleeps on the semaphore semnum
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -0700988 */
989static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
990 bool count_zero)
991{
Manfred Spraulb220c572014-06-06 14:37:51 -0700992 struct sembuf *sop = q->blocking;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -0700993
Manfred Spraul9b44ee22014-06-06 14:37:52 -0700994 /*
995 * Linux always (since 0.99.10) reported a task as sleeping on all
996 * semaphores. This violates SUS, therefore it was changed to the
997 * standard compliant behavior.
998 * Give the administrators a chance to notice that an application
999 * might misbehave because it relies on the Linux behavior.
1000 */
1001 pr_info_once("semctl(GETNCNT/GETZCNT) is since 3.16 Single Unix Specification compliant.\n"
1002 "The task %s (%d) triggered the difference, watch for misbehavior.\n",
1003 current->comm, task_pid_nr(current));
1004
Manfred Spraulb220c572014-06-06 14:37:51 -07001005 if (sop->sem_num != semnum)
1006 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001007
Manfred Spraulb220c572014-06-06 14:37:51 -07001008 if (count_zero && sop->sem_op == 0)
1009 return 1;
1010 if (!count_zero && sop->sem_op < 0)
1011 return 1;
1012
1013 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016/* The following counts are associated to each semaphore:
1017 * semncnt number of tasks waiting on semval being nonzero
1018 * semzcnt number of tasks waiting on semval being zero
Manfred Spraulb220c572014-06-06 14:37:51 -07001019 *
1020 * Per definition, a task waits only on the semaphore of the first semop
1021 * that cannot proceed, even if additional operation would block, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 */
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001023static int count_semcnt(struct sem_array *sma, ushort semnum,
1024 bool count_zero)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001026 struct list_head *l;
Manfred Spraul239521f2014-01-27 17:07:04 -08001027 struct sem_queue *q;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001028 int semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001030 semcnt = 0;
1031 /* First: check the simple operations. They are easy to evaluate */
1032 if (count_zero)
1033 l = &sma->sem_base[semnum].pending_const;
1034 else
1035 l = &sma->sem_base[semnum].pending_alter;
1036
1037 list_for_each_entry(q, l, list) {
1038 /* all task on a per-semaphore list sleep on exactly
1039 * that semaphore
1040 */
1041 semcnt++;
Rik van Rielde2657f2013-05-09 16:59:59 -04001042 }
1043
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001044 /* Then: check the complex operations. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001045 list_for_each_entry(q, &sma->pending_alter, list) {
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001046 semcnt += check_qop(sma, semnum, q, count_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001048 if (count_zero) {
1049 list_for_each_entry(q, &sma->pending_const, list) {
1050 semcnt += check_qop(sma, semnum, q, count_zero);
1051 }
Rik van Rielebc2e5e2013-05-09 16:53:28 -04001052 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001053 return semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001056/* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1057 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -07001058 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001060static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Manfred Spraul380af1b2008-07-25 01:48:06 -07001062 struct sem_undo *un, *tu;
1063 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001064 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001065 int i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001066 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Manfred Spraul380af1b2008-07-25 01:48:06 -07001068 /* Free the existing undo structures for this semaphore set. */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001069 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001070 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1071 list_del(&un->list_id);
1072 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001074 list_del_rcu(&un->list_proc);
1075 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001076 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001080 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1081 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001082 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001083 }
1084
1085 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -08001086 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001087 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001089 for (i = 0; i < sma->sem_nsems; i++) {
1090 struct sem *sem = sma->sem_base + i;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001091 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1092 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001093 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001094 }
1095 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001096 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001097 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001098 }
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001101 /* Remove the semaphore set from the IDR */
1102 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001103 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001104 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001106 wake_up_q(&wake_q);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001107 ns->used_sems -= sma->sem_nsems;
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001108 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
1111static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1112{
Manfred Spraul239521f2014-01-27 17:07:04 -08001113 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 case IPC_64:
1115 return copy_to_user(buf, in, sizeof(*in));
1116 case IPC_OLD:
1117 {
1118 struct semid_ds out;
1119
Dan Rosenberg982f7c22010-09-30 15:15:31 -07001120 memset(&out, 0, sizeof(out));
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1123
1124 out.sem_otime = in->sem_otime;
1125 out.sem_ctime = in->sem_ctime;
1126 out.sem_nsems = in->sem_nsems;
1127
1128 return copy_to_user(buf, &out, sizeof(out));
1129 }
1130 default:
1131 return -EINVAL;
1132 }
1133}
1134
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001135static time_t get_semotime(struct sem_array *sma)
1136{
1137 int i;
1138 time_t res;
1139
1140 res = sma->sem_base[0].sem_otime;
1141 for (i = 1; i < sma->sem_nsems; i++) {
1142 time_t to = sma->sem_base[i].sem_otime;
1143
1144 if (to > res)
1145 res = to;
1146 }
1147 return res;
1148}
1149
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001150static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001151 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Amerigo Wange5cc9c72009-12-15 16:47:35 -08001153 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct sem_array *sma;
1155
Manfred Spraul239521f2014-01-27 17:07:04 -08001156 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 case IPC_INFO:
1158 case SEM_INFO:
1159 {
1160 struct seminfo seminfo;
1161 int max_id;
1162
1163 err = security_sem_semctl(NULL, cmd);
1164 if (err)
1165 return err;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001166
Manfred Spraul239521f2014-01-27 17:07:04 -08001167 memset(&seminfo, 0, sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -07001168 seminfo.semmni = ns->sc_semmni;
1169 seminfo.semmns = ns->sc_semmns;
1170 seminfo.semmsl = ns->sc_semmsl;
1171 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 seminfo.semvmx = SEMVMX;
1173 seminfo.semmnu = SEMMNU;
1174 seminfo.semmap = SEMMAP;
1175 seminfo.semume = SEMUME;
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001176 down_read(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001178 seminfo.semusz = sem_ids(ns).in_use;
1179 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 } else {
1181 seminfo.semusz = SEMUSZ;
1182 seminfo.semaem = SEMAEM;
1183 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001184 max_id = ipc_get_maxid(&sem_ids(ns));
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001185 up_read(&sem_ids(ns).rwsem);
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001186 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return -EFAULT;
Manfred Spraul239521f2014-01-27 17:07:04 -08001188 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001190 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 case SEM_STAT:
1192 {
1193 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001194 int id = 0;
1195
1196 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Linus Torvalds941b0302013-05-04 11:04:29 -07001198 rcu_read_lock();
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001199 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001200 sma = sem_obtain_object(ns, semid);
1201 if (IS_ERR(sma)) {
1202 err = PTR_ERR(sma);
1203 goto out_unlock;
1204 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001205 id = sma->sem_perm.id;
1206 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001207 sma = sem_obtain_object_check(ns, semid);
1208 if (IS_ERR(sma)) {
1209 err = PTR_ERR(sma);
1210 goto out_unlock;
1211 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001215 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto out_unlock;
1217
1218 err = security_sem_semctl(sma, cmd);
1219 if (err)
1220 goto out_unlock;
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001223 tbuf.sem_otime = get_semotime(sma);
1224 tbuf.sem_ctime = sma->sem_ctime;
1225 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001226 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001227 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return -EFAULT;
1229 return id;
1230 }
1231 default:
1232 return -EINVAL;
1233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001235 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return err;
1237}
1238
Al Viroe1fd1f42013-03-05 15:04:55 -05001239static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1240 unsigned long arg)
1241{
1242 struct sem_undo *un;
1243 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001244 struct sem *curr;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001245 int err, val;
1246 DEFINE_WAKE_Q(wake_q);
1247
Al Viroe1fd1f42013-03-05 15:04:55 -05001248#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1249 /* big-endian 64bit */
1250 val = arg >> 32;
1251#else
1252 /* 32bit or little-endian 64bit */
1253 val = arg;
1254#endif
1255
Rik van Riel6062a8d2013-04-30 19:15:44 -07001256 if (val > SEMVMX || val < 0)
1257 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001258
Rik van Riel6062a8d2013-04-30 19:15:44 -07001259 rcu_read_lock();
1260 sma = sem_obtain_object_check(ns, semid);
1261 if (IS_ERR(sma)) {
1262 rcu_read_unlock();
1263 return PTR_ERR(sma);
1264 }
1265
1266 if (semnum < 0 || semnum >= sma->sem_nsems) {
1267 rcu_read_unlock();
1268 return -EINVAL;
1269 }
1270
1271
1272 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1273 rcu_read_unlock();
1274 return -EACCES;
1275 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001276
1277 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001278 if (err) {
1279 rcu_read_unlock();
1280 return -EACCES;
1281 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001282
Rik van Riel6062a8d2013-04-30 19:15:44 -07001283 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001284
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001285 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001286 sem_unlock(sma, -1);
1287 rcu_read_unlock();
1288 return -EIDRM;
1289 }
1290
Al Viroe1fd1f42013-03-05 15:04:55 -05001291 curr = &sma->sem_base[semnum];
1292
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001293 ipc_assert_locked_object(&sma->sem_perm);
Al Viroe1fd1f42013-03-05 15:04:55 -05001294 list_for_each_entry(un, &sma->list_id, list_id)
1295 un->semadj[semnum] = 0;
1296
1297 curr->semval = val;
1298 curr->sempid = task_tgid_vnr(current);
1299 sma->sem_ctime = get_seconds();
1300 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001301 do_smart_update(sma, NULL, 0, 0, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001302 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001303 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001304 wake_up_q(&wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001305 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001306}
1307
Kirill Korotaeve3893532006-10-02 02:18:22 -07001308static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001309 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001312 struct sem *curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001313 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 ushort fast_sem_io[SEMMSL_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001315 ushort *sem_io = fast_sem_io;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001316 DEFINE_WAKE_Q(wake_q);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001317
1318 rcu_read_lock();
1319 sma = sem_obtain_object_check(ns, semid);
1320 if (IS_ERR(sma)) {
1321 rcu_read_unlock();
1322 return PTR_ERR(sma);
1323 }
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 nsems = sma->sem_nsems;
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 err = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001328 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1329 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 err = security_sem_semctl(sma, cmd);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001332 if (err)
1333 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 err = -EACCES;
1336 switch (cmd) {
1337 case GETALL:
1338 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001339 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int i;
1341
Al Viroce857222013-05-03 00:30:49 +01001342 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001343 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001344 err = -EIDRM;
1345 goto out_unlock;
1346 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001347 if (nsems > SEMMSL_FAST) {
Al Viroce857222013-05-03 00:30:49 +01001348 if (!ipc_rcu_getref(sma)) {
Al Viroce857222013-05-03 00:30:49 +01001349 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001350 goto out_unlock;
Al Viroce857222013-05-03 00:30:49 +01001351 }
1352 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001353 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 sem_io = ipc_alloc(sizeof(ushort)*nsems);
Manfred Spraul239521f2014-01-27 17:07:04 -08001355 if (sem_io == NULL) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001356 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -ENOMEM;
1358 }
1359
Linus Torvalds4091fd92013-05-04 10:13:40 -07001360 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001361 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001362 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001364 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Al Viroce857222013-05-03 00:30:49 +01001366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 for (i = 0; i < sma->sem_nsems; i++)
1368 sem_io[i] = sma->sem_base[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001369 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001370 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 err = 0;
Manfred Spraul239521f2014-01-27 17:07:04 -08001372 if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 err = -EFAULT;
1374 goto out_free;
1375 }
1376 case SETALL:
1377 {
1378 int i;
1379 struct sem_undo *un;
1380
Rik van Riel6062a8d2013-04-30 19:15:44 -07001381 if (!ipc_rcu_getref(sma)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001382 err = -EIDRM;
1383 goto out_rcu_wakeup;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001384 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001385 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Manfred Spraul239521f2014-01-27 17:07:04 -08001387 if (nsems > SEMMSL_FAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 sem_io = ipc_alloc(sizeof(ushort)*nsems);
Manfred Spraul239521f2014-01-27 17:07:04 -08001389 if (sem_io == NULL) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001390 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return -ENOMEM;
1392 }
1393 }
1394
Manfred Spraul239521f2014-01-27 17:07:04 -08001395 if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001396 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 err = -EFAULT;
1398 goto out_free;
1399 }
1400
1401 for (i = 0; i < nsems; i++) {
1402 if (sem_io[i] > SEMVMX) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001403 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 err = -ERANGE;
1405 goto out_free;
1406 }
1407 }
Linus Torvalds4091fd92013-05-04 10:13:40 -07001408 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001409 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001410 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001412 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001415 for (i = 0; i < nsems; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 sma->sem_base[i].semval = sem_io[i];
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001417 sma->sem_base[i].sempid = task_tgid_vnr(current);
1418 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001419
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001420 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001421 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 for (i = 0; i < nsems; i++)
1423 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 sma->sem_ctime = get_seconds();
1426 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001427 do_smart_update(sma, NULL, 0, 0, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 err = 0;
1429 goto out_unlock;
1430 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001431 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433 err = -EINVAL;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001434 if (semnum < 0 || semnum >= nsems)
1435 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Rik van Riel6062a8d2013-04-30 19:15:44 -07001437 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001438 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001439 err = -EIDRM;
1440 goto out_unlock;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 curr = &sma->sem_base[semnum];
1443
1444 switch (cmd) {
1445 case GETVAL:
1446 err = curr->semval;
1447 goto out_unlock;
1448 case GETPID:
1449 err = curr->sempid;
1450 goto out_unlock;
1451 case GETNCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001452 err = count_semcnt(sma, semnum, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 goto out_unlock;
1454 case GETZCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001455 err = count_semcnt(sma, semnum, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001460 sem_unlock(sma, -1);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001461out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001462 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001463 wake_up_q(&wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08001465 if (sem_io != fast_sem_io)
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001466 ipc_free(sem_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return err;
1468}
1469
Pierre Peiffer016d7132008-04-29 01:00:50 -07001470static inline unsigned long
1471copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Manfred Spraul239521f2014-01-27 17:07:04 -08001473 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001475 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 case IPC_OLD:
1479 {
1480 struct semid_ds tbuf_old;
1481
Manfred Spraul239521f2014-01-27 17:07:04 -08001482 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return -EFAULT;
1484
Pierre Peiffer016d7132008-04-29 01:00:50 -07001485 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1486 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1487 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 return 0;
1490 }
1491 default:
1492 return -EINVAL;
1493 }
1494}
1495
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001496/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001497 * This function handles some semctl commands which require the rwsem
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001498 * to be held in write mode.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001499 * NOTE: no locks must be held, the rwsem is taken inside this function.
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001500 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001501static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001502 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 struct sem_array *sma;
1505 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001506 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 struct kern_ipc_perm *ipcp;
1508
Manfred Spraul239521f2014-01-27 17:07:04 -08001509 if (cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001510 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001514 down_write(&sem_ids(ns).rwsem);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001515 rcu_read_lock();
1516
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001517 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1518 &semid64.sem_perm, 0);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001519 if (IS_ERR(ipcp)) {
1520 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001521 goto out_unlock1;
1522 }
Steve Grubb073115d2006-04-02 17:07:33 -04001523
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001524 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001527 if (err)
1528 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001530 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001532 sem_lock(sma, NULL, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001533 /* freeary unlocks the ipc object and rcu */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001534 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001535 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001537 sem_lock(sma, NULL, -1);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001538 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1539 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001540 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 break;
1543 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 err = -EINVAL;
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001545 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001548out_unlock0:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001549 sem_unlock(sma, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001550out_unlock1:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001551 rcu_read_unlock();
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001552out_up:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001553 up_write(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return err;
1555}
1556
Al Viroe1fd1f42013-03-05 15:04:55 -05001557SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001560 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001561 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 if (semid < 0)
1564 return -EINVAL;
1565
1566 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001567 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Manfred Spraul239521f2014-01-27 17:07:04 -08001569 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 case IPC_INFO:
1571 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001572 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001574 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 case GETALL:
1576 case GETVAL:
1577 case GETPID:
1578 case GETNCNT:
1579 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001581 return semctl_main(ns, semid, semnum, cmd, p);
1582 case SETVAL:
1583 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 case IPC_RMID:
1585 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001586 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 default:
1588 return -EINVAL;
1589 }
1590}
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592/* If the task doesn't already have a undo_list, then allocate one
1593 * here. We guarantee there is only one thread using this undo list,
1594 * and current is THE ONE
1595 *
1596 * If this allocation and assignment succeeds, but later
1597 * portions of this code fail, there is no need to free the sem_undo_list.
1598 * Just let it stay associated with the task, and it'll be freed later
1599 * at exit time.
1600 *
1601 * This can block, so callers must hold no locks.
1602 */
1603static inline int get_undo_list(struct sem_undo_list **undo_listp)
1604{
1605 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 undo_list = current->sysvsem.undo_list;
1608 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001609 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (undo_list == NULL)
1611 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001612 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001614 INIT_LIST_HEAD(&undo_list->list_proc);
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 current->sysvsem.undo_list = undo_list;
1617 }
1618 *undo_listp = undo_list;
1619 return 0;
1620}
1621
Nick Pigginbf17bb72009-12-15 16:47:28 -08001622static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001624 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Nick Pigginbf17bb72009-12-15 16:47:28 -08001626 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1627 if (un->semid == semid)
1628 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001630 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
Nick Pigginbf17bb72009-12-15 16:47:28 -08001633static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1634{
1635 struct sem_undo *un;
1636
Manfred Spraul239521f2014-01-27 17:07:04 -08001637 assert_spin_locked(&ulp->lock);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001638
1639 un = __lookup_undo(ulp, semid);
1640 if (un) {
1641 list_del_rcu(&un->list_proc);
1642 list_add_rcu(&un->list_proc, &ulp->list_proc);
1643 }
1644 return un;
1645}
1646
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001647/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001648 * find_alloc_undo - lookup (and if not present create) undo array
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001649 * @ns: namespace
1650 * @semid: semaphore array id
1651 *
1652 * The function looks up (and if not present creates) the undo structure.
1653 * The size of the undo structure depends on the size of the semaphore
1654 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001655 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1656 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001657 */
1658static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 struct sem_array *sma;
1661 struct sem_undo_list *ulp;
1662 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001663 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 error = get_undo_list(&ulp);
1666 if (error)
1667 return ERR_PTR(error);
1668
Manfred Spraul380af1b2008-07-25 01:48:06 -07001669 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001670 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001672 spin_unlock(&ulp->lock);
Manfred Spraul239521f2014-01-27 17:07:04 -08001673 if (likely(un != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 goto out;
1675
1676 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001677 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001678 sma = sem_obtain_object_check(ns, semid);
1679 if (IS_ERR(sma)) {
1680 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001681 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001682 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 nsems = sma->sem_nsems;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001685 if (!ipc_rcu_getref(sma)) {
1686 rcu_read_unlock();
1687 un = ERR_PTR(-EIDRM);
1688 goto out;
1689 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001690 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001692 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001693 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (!new) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001695 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return ERR_PTR(-ENOMEM);
1697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Manfred Spraul380af1b2008-07-25 01:48:06 -07001699 /* step 3: Acquire the lock on semaphore array */
Linus Torvalds4091fd92013-05-04 10:13:40 -07001700 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001701 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001702 if (!ipc_valid_object(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001703 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001704 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 kfree(new);
1706 un = ERR_PTR(-EIDRM);
1707 goto out;
1708 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001709 spin_lock(&ulp->lock);
1710
1711 /*
1712 * step 4: check for races: did someone else allocate the undo struct?
1713 */
1714 un = lookup_undo(ulp, semid);
1715 if (un) {
1716 kfree(new);
1717 goto success;
1718 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001719 /* step 5: initialize & link new undo structure */
1720 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001721 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001722 new->semid = semid;
1723 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001724 list_add_rcu(&new->list_proc, &ulp->list_proc);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001725 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001726 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001727 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001728
1729success:
1730 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001731 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732out:
1733 return un;
1734}
1735
Heiko Carstensd5460c92009-01-14 14:14:27 +01001736SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1737 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
1739 int error = -EINVAL;
1740 struct sem_array *sma;
1741 struct sembuf fast_sops[SEMOPM_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001742 struct sembuf *sops = fast_sops, *sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 struct sem_undo *un;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001744 int max, locknum;
1745 bool undos = false, alter = false, dupsop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 struct sem_queue queue;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001747 unsigned long dup = 0, jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001748 struct ipc_namespace *ns;
1749
1750 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 if (nsops < 1 || semid < 0)
1753 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001754 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return -E2BIG;
Manfred Spraul239521f2014-01-27 17:07:04 -08001756 if (nsops > SEMOPM_FAST) {
1757 sops = kmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
1758 if (sops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return -ENOMEM;
1760 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001761
Manfred Spraul239521f2014-01-27 17:07:04 -08001762 if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
1763 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 goto out_free;
1765 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (timeout) {
1768 struct timespec _timeout;
1769 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1770 error = -EFAULT;
1771 goto out_free;
1772 }
1773 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1774 _timeout.tv_nsec >= 1000000000L) {
1775 error = -EINVAL;
1776 goto out_free;
1777 }
1778 jiffies_left = timespec_to_jiffies(&_timeout);
1779 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 max = 0;
1782 for (sop = sops; sop < sops + nsops; sop++) {
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001783 unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (sop->sem_num >= max)
1786 max = sop->sem_num;
1787 if (sop->sem_flg & SEM_UNDO)
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001788 undos = true;
1789 if (dup & mask) {
1790 /*
1791 * There was a previous alter access that appears
1792 * to have accessed the same semaphore, thus use
1793 * the dupsop logic. "appears", because the detection
1794 * can only check % BITS_PER_LONG.
1795 */
1796 dupsop = true;
1797 }
1798 if (sop->sem_op != 0) {
1799 alter = true;
1800 dup |= mask;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001805 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001806 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (IS_ERR(un)) {
1808 error = PTR_ERR(un);
1809 goto out_free;
1810 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001811 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001813 rcu_read_lock();
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001816 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001817 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001818 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001819 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001821 }
1822
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001823 error = -EFBIG;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001824 if (max >= sma->sem_nsems) {
1825 rcu_read_unlock();
1826 goto out_free;
1827 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001828
1829 error = -EACCES;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001830 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
1831 rcu_read_unlock();
1832 goto out_free;
1833 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001834
1835 error = security_sem_semop(sma, sops, nsops, alter);
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001836 if (error) {
1837 rcu_read_unlock();
1838 goto out_free;
1839 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001840
Manfred Spraul6e224f92013-10-16 13:46:45 -07001841 error = -EIDRM;
1842 locknum = sem_lock(sma, sops, nsops);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001843 /*
1844 * We eventually might perform the following check in a lockless
1845 * fashion, considering ipc_valid_object() locking constraints.
1846 * If nsops == 1 and there is no contention for sem_perm.lock, then
1847 * only a per-semaphore lock is held and it's OK to proceed with the
1848 * check below. More details on the fine grained locking scheme
1849 * entangled here and why it's RMID race safe on comments at sem_lock()
1850 */
1851 if (!ipc_valid_object(&sma->sem_perm))
Manfred Spraul6e224f92013-10-16 13:46:45 -07001852 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001854 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001856 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001857 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001858 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001860 if (un && un->semid == -1)
1861 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001862
Manfred Sprauld198cd62014-06-06 14:37:49 -07001863 queue.sops = sops;
1864 queue.nsops = nsops;
1865 queue.undo = un;
1866 queue.pid = task_tgid_vnr(current);
1867 queue.alter = alter;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001868 queue.dupsop = dupsop;
Manfred Sprauld198cd62014-06-06 14:37:49 -07001869
1870 error = perform_atomic_semop(sma, &queue);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001871 if (error == 0) { /* non-blocking succesfull path */
1872 DEFINE_WAKE_Q(wake_q);
1873
1874 /*
1875 * If the operation was successful, then do
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001876 * the required updates.
1877 */
1878 if (alter)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001879 do_smart_update(sma, sops, nsops, 1, &wake_q);
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001880 else
1881 set_semotime(sma, sops);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001882
1883 sem_unlock(sma, locknum);
1884 rcu_read_unlock();
1885 wake_up_q(&wake_q);
1886
1887 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001889 if (error < 0) /* non-blocking error path */
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001890 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001892 /*
1893 * We need to sleep on this operation, so we put the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 * task into the pending queue and go to sleep.
1895 */
Manfred Spraulb97e8202009-12-15 16:47:32 -08001896 if (nsops == 1) {
1897 struct sem *curr;
1898 curr = &sma->sem_base[sops->sem_num];
1899
Manfred Spraulf269f402013-07-08 16:01:24 -07001900 if (alter) {
1901 if (sma->complex_count) {
1902 list_add_tail(&queue.list,
1903 &sma->pending_alter);
1904 } else {
1905
1906 list_add_tail(&queue.list,
1907 &curr->pending_alter);
1908 }
1909 } else {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001910 list_add_tail(&queue.list, &curr->pending_const);
Manfred Spraulf269f402013-07-08 16:01:24 -07001911 }
Manfred Spraulb97e8202009-12-15 16:47:32 -08001912 } else {
Manfred Spraulf269f402013-07-08 16:01:24 -07001913 if (!sma->complex_count)
1914 merge_queues(sma);
1915
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001916 if (alter)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001917 list_add_tail(&queue.list, &sma->pending_alter);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001918 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001919 list_add_tail(&queue.list, &sma->pending_const);
1920
Manfred Spraulb97e8202009-12-15 16:47:32 -08001921 sma->complex_count++;
1922 }
1923
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001924 do {
1925 queue.status = -EINTR;
1926 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001927
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001928 __set_current_state(TASK_INTERRUPTIBLE);
1929 sem_unlock(sma, locknum);
Linus Torvalds321310c2013-05-04 10:47:57 -07001930 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001932 if (timeout)
1933 jiffies_left = schedule_timeout(jiffies_left);
1934 else
1935 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001937 /*
1938 * fastpath: the semop has completed, either successfully or
1939 * not, from the syscall pov, is quite irrelevant to us at this
1940 * point; we're done.
1941 *
1942 * We _do_ care, nonetheless, about being awoken by a signal or
1943 * spuriously. The queue.status is checked again in the
1944 * slowpath (aka after taking sem_lock), such that we can detect
1945 * scenarios where we were awakened externally, during the
1946 * window between wake_q_add() and wake_up_q().
1947 */
1948 error = READ_ONCE(queue.status);
1949 if (error != -EINTR) {
1950 /*
1951 * User space could assume that semop() is a memory
1952 * barrier: Without the mb(), the cpu could
1953 * speculatively read in userspace stale data that was
1954 * overwritten by the previous owner of the semaphore.
1955 */
1956 smp_mb();
1957 goto out_free;
1958 }
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001959
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001960 rcu_read_lock();
Manfred Spraulc626bc42017-01-10 16:57:48 -08001961 locknum = sem_lock(sma, sops, nsops);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001962
Davidlohr Bueso370b2622016-12-14 15:06:49 -08001963 if (!ipc_valid_object(&sma->sem_perm))
1964 goto out_unlock_free;
1965
1966 error = READ_ONCE(queue.status);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001967
1968 /*
1969 * If queue.status != -EINTR we are woken up by another process.
1970 * Leave without unlink_queue(), but with sem_unlock().
1971 */
1972 if (error != -EINTR)
1973 goto out_unlock_free;
1974
1975 /*
1976 * If an interrupt occurred we have to clean up the queue.
1977 */
1978 if (timeout && jiffies_left == 0)
1979 error = -EAGAIN;
1980 } while (error == -EINTR && !signal_pending(current)); /* spurious */
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001981
Manfred Spraulb97e8202009-12-15 16:47:32 -08001982 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984out_unlock_free:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001985 sem_unlock(sma, locknum);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001986 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08001988 if (sops != fast_sops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 kfree(sops);
1990 return error;
1991}
1992
Heiko Carstensd5460c92009-01-14 14:14:27 +01001993SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
1994 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
1996 return sys_semtimedop(semid, tsops, nsops, NULL);
1997}
1998
1999/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
2000 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 */
2002
2003int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
2004{
2005 struct sem_undo_list *undo_list;
2006 int error;
2007
2008 if (clone_flags & CLONE_SYSVSEM) {
2009 error = get_undo_list(&undo_list);
2010 if (error)
2011 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 atomic_inc(&undo_list->refcnt);
2013 tsk->sysvsem.undo_list = undo_list;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07002014 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 tsk->sysvsem.undo_list = NULL;
2016
2017 return 0;
2018}
2019
2020/*
2021 * add semadj values to semaphores, free undo structures.
2022 * undo structures are not freed when semaphore arrays are destroyed
2023 * so some of them may be out of date.
2024 * IMPLEMENTATION NOTE: There is some confusion over whether the
2025 * set of adjustments that needs to be done should be done in an atomic
2026 * manner or not. That is, if we are attempting to decrement the semval
2027 * should we queue up and wait until we can do so legally?
2028 * The original implementation attempted to do this (queue and wait).
2029 * The current implementation does not do so. The POSIX standard
2030 * and SVID should be consulted to determine what behavior is mandated.
2031 */
2032void exit_sem(struct task_struct *tsk)
2033{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002034 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002036 ulp = tsk->sysvsem.undo_list;
2037 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002039 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002041 if (!atomic_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return;
2043
Manfred Spraul380af1b2008-07-25 01:48:06 -07002044 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07002046 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002047 int semid, i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002048 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Nikolay Borisov2a1613a2016-10-11 13:55:05 -07002050 cond_resched();
2051
Manfred Spraul380af1b2008-07-25 01:48:06 -07002052 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02002053 un = list_entry_rcu(ulp->list_proc.next,
2054 struct sem_undo, list_proc);
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002055 if (&un->list_proc == &ulp->list_proc) {
2056 /*
2057 * We must wait for freeary() before freeing this ulp,
2058 * in case we raced with last sem_undo. There is a small
2059 * possibility where we exit while freeary() didn't
2060 * finish unlocking sem_undo_list.
2061 */
2062 spin_unlock_wait(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002063 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002064 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002065 }
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002066 spin_lock(&ulp->lock);
2067 semid = un->semid;
2068 spin_unlock(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002069
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002070 /* exit_sem raced with IPC_RMID, nothing to do */
2071 if (semid == -1) {
2072 rcu_read_unlock();
2073 continue;
2074 }
2075
2076 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002077 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002078 if (IS_ERR(sma)) {
2079 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002080 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Rik van Riel6062a8d2013-04-30 19:15:44 -07002083 sem_lock(sma, NULL, -1);
Manfred Spraul6e224f92013-10-16 13:46:45 -07002084 /* exit_sem raced with IPC_RMID, nothing to do */
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08002085 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07002086 sem_unlock(sma, -1);
2087 rcu_read_unlock();
2088 continue;
2089 }
Nick Pigginbf17bb72009-12-15 16:47:28 -08002090 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002091 if (un == NULL) {
2092 /* exit_sem raced with IPC_RMID+semget() that created
2093 * exactly the same semid. Nothing to do.
2094 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002095 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002096 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002097 continue;
2098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Manfred Spraul380af1b2008-07-25 01:48:06 -07002100 /* remove un from the linked lists */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07002101 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002102 list_del(&un->list_id);
2103
Herton R. Krzesinskia9795582015-08-14 15:35:05 -07002104 /* we are the last process using this ulp, acquiring ulp->lock
2105 * isn't required. Besides that, we are also protected against
2106 * IPC_RMID as we hold sma->sem_perm lock now
2107 */
Manfred Spraul380af1b2008-07-25 01:48:06 -07002108 list_del_rcu(&un->list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002109
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002110 /* perform adjustments registered in un */
2111 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul239521f2014-01-27 17:07:04 -08002112 struct sem *semaphore = &sma->sem_base[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002113 if (un->semadj[i]) {
2114 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 /*
2116 * Range checks of the new semaphore value,
2117 * not defined by sus:
2118 * - Some unices ignore the undo entirely
2119 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2120 * - some cap the value (e.g. FreeBSD caps
2121 * at 0, but doesn't enforce SEMVMX)
2122 *
2123 * Linux caps the semaphore value, both at 0
2124 * and at SEMVMX.
2125 *
Manfred Spraul239521f2014-01-27 17:07:04 -08002126 * Manfred <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08002128 if (semaphore->semval < 0)
2129 semaphore->semval = 0;
2130 if (semaphore->semval > SEMVMX)
2131 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002132 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002136 do_smart_update(sma, NULL, 0, 1, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002137 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002138 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002139 wake_up_q(&wake_q);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002140
Lai Jiangshan693a8b62011-03-18 12:09:35 +08002141 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002143 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07002147static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08002149 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07002150 struct sem_array *sma = it;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002151 time_t sem_otime;
2152
Manfred Sprauld8c63372013-09-30 13:45:07 -07002153 /*
2154 * The proc interface isn't aware of sem_lock(), it calls
2155 * ipc_lock_object() directly (in sysvipc_find_ipc).
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002156 * In order to stay compatible with sem_lock(), we must
2157 * enter / leave complex_mode.
Manfred Sprauld8c63372013-09-30 13:45:07 -07002158 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002159 complexmode_enter(sma);
Manfred Sprauld8c63372013-09-30 13:45:07 -07002160
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002161 sem_otime = get_semotime(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Joe Perches7f032d62015-04-15 16:17:54 -07002163 seq_printf(s,
2164 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
2165 sma->sem_perm.key,
2166 sma->sem_perm.id,
2167 sma->sem_perm.mode,
2168 sma->sem_nsems,
2169 from_kuid_munged(user_ns, sma->sem_perm.uid),
2170 from_kgid_munged(user_ns, sma->sem_perm.gid),
2171 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2172 from_kgid_munged(user_ns, sma->sem_perm.cgid),
2173 sem_otime,
2174 sma->sem_ctime);
2175
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002176 complexmode_tryleave(sma);
2177
Joe Perches7f032d62015-04-15 16:17:54 -07002178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180#endif