blob: d73b3eff80f08582c211975f1705d45ba8597d42 [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>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070014 * Further wakeup optimizations, documentation
15 * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040016 *
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaeve3893532006-10-02 02:18:22 -070019 *
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070023 *
24 * Implementation notes: (May 2010)
25 * This file implements System V semaphores.
26 *
27 * User space visible behavior:
28 * - FIFO ordering for semop() operations (just FIFO, not starvation
29 * protection)
30 * - multiple semaphore operations that alter the same semaphore in
31 * one semop() are handled.
32 * - sem_ctime (time of last semctl()) is updated in the IPC_SET, SETVAL and
33 * SETALL calls.
34 * - two Linux specific semctl() commands: SEM_STAT, SEM_INFO.
35 * - undo adjustments at process exit are limited to 0..SEMVMX.
36 * - namespace are supported.
37 * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtine by writing
38 * to /proc/sys/kernel/sem.
39 * - statistics about the usage are reported in /proc/sysvipc/sem.
40 *
41 * Internals:
42 * - scalability:
43 * - all global variables are read-mostly.
44 * - semop() calls and semctl(RMID) are synchronized by RCU.
45 * - most operations do write operations (actually: spin_lock calls) to
46 * the per-semaphore array structure.
47 * Thus: Perfect SMP scaling between independent semaphore arrays.
48 * If multiple semaphores in one array are used, then cache line
49 * trashing on the semaphore array spinlock will limit the scaling.
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -070050 * - semncnt and semzcnt are calculated on demand in count_semcnt()
Manfred Spraulc5cf6352010-05-26 14:43:43 -070051 * - the task that performs a successful semop() scans the list of all
52 * sleeping tasks and completes any pending operations that can be fulfilled.
53 * Semaphores are actively given to waiting tasks (necessary for FIFO).
54 * (see update_queue())
55 * - To improve the scalability, the actual wake-up calls are performed after
56 * dropping all locks. (see wake_up_sem_queue_prepare(),
57 * wake_up_sem_queue_do())
58 * - 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).
62 * - The synchronizations between wake-ups due to a timeout/signal and a
63 * wake-up due to a completed semaphore operation is achieved by using an
64 * intermediate state (IN_WAKEUP).
65 * - UNDO values are stored in an array (one per process and per
66 * semaphore array, lazily allocated). For backwards compatibility, multiple
67 * modes for the UNDO variables are supported (per process, per thread)
68 * (see copy_semundo, CLONE_SYSVSEM)
69 * - There are two lists of the pending operations: a per-array list
70 * and per-semaphore list (stored in the array). This allows to achieve FIFO
71 * ordering without always scanning all pending operations.
72 * The worst-case behavior is nevertheless O(N^2) for N wakeups.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/slab.h>
76#include <linux/spinlock.h>
77#include <linux/init.h>
78#include <linux/proc_fs.h>
79#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/security.h>
81#include <linux/syscalls.h>
82#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080083#include <linux/capability.h>
Mike Waychison19b49462005-09-06 15:17:10 -070084#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070085#include <linux/rwsem.h>
Kirill Korotaeve3893532006-10-02 02:18:22 -070086#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080087#include <linux/ipc_namespace.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080088
Paul McQuade7153e402014-06-06 14:37:37 -070089#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include "util.h"
91
Manfred Spraule57940d2011-11-02 13:38:54 -070092/* One semaphore structure for each semaphore in the system. */
93struct sem {
94 int semval; /* current value */
95 int sempid; /* pid of last operation */
Rik van Riel6062a8d2013-04-30 19:15:44 -070096 spinlock_t lock; /* spinlock for fine-grained semtimedop */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -070097 struct list_head pending_alter; /* pending single-sop operations */
98 /* that alter the semaphore */
99 struct list_head pending_const; /* pending single-sop operations */
100 /* that do not alter the semaphore*/
Manfred Sprauld12e1e52013-07-08 16:01:25 -0700101 time_t sem_otime; /* candidate for sem_otime */
Manfred Spraulf5c936c2013-07-08 16:01:22 -0700102} ____cacheline_aligned_in_smp;
Manfred Spraule57940d2011-11-02 13:38:54 -0700103
104/* One queue for each sleeping process in the system. */
105struct sem_queue {
Manfred Spraule57940d2011-11-02 13:38:54 -0700106 struct list_head list; /* queue of pending operations */
107 struct task_struct *sleeper; /* this process */
108 struct sem_undo *undo; /* undo structure */
109 int pid; /* process id of requesting process */
110 int status; /* completion status of operation */
111 struct sembuf *sops; /* array of pending operations */
112 int nsops; /* number of operations */
113 int alter; /* does *sops alter the array? */
114};
115
116/* Each task has a list of undo requests. They are executed automatically
117 * when the process exits.
118 */
119struct sem_undo {
120 struct list_head list_proc; /* per-process list: *
121 * all undos from one process
122 * rcu protected */
123 struct rcu_head rcu; /* rcu struct for sem_undo */
124 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
125 struct list_head list_id; /* per semaphore array list:
126 * all undos for one array */
127 int semid; /* semaphore set identifier */
128 short *semadj; /* array of adjustments */
129 /* one per semaphore */
130};
131
132/* sem_undo_list controls shared access to the list of sem_undo structures
133 * that may be shared among all a CLONE_SYSVSEM task group.
134 */
135struct sem_undo_list {
136 atomic_t refcnt;
137 spinlock_t lock;
138 struct list_head list_proc;
139};
140
141
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800142#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Nadia Derbey1b531f22007-10-18 23:40:55 -0700144#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700146static int newary(struct ipc_namespace *, struct ipc_params *);
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800147static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700149static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#endif
151
152#define SEMMSL_FAST 256 /* 512 bytes on stack */
153#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
154
155/*
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700156 * Locking:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * sem_undo.id_next,
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700158 * sem_array.complex_count,
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700159 * sem_array.pending{_alter,_cont},
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700160 * sem_array.sem_undo: global sem_lock() for read/write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * sem_undo.proc_next: only "current" is allowed to read/write that field.
Paul McQuade46c0a8c2014-06-06 14:37:37 -0700162 *
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700163 * sem_array.sem_base[i].pending_{const,alter}:
164 * global or semaphore sem_lock() for read/write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
166
Kirill Korotaeve3893532006-10-02 02:18:22 -0700167#define sc_semmsl sem_ctls[0]
168#define sc_semmns sem_ctls[1]
169#define sc_semopm sem_ctls[2]
170#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800172void sem_init_ns(struct ipc_namespace *ns)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700173{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700174 ns->sc_semmsl = SEMMSL;
175 ns->sc_semmns = SEMMNS;
176 ns->sc_semopm = SEMOPM;
177 ns->sc_semmni = SEMMNI;
178 ns->used_sems = 0;
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800179 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700180}
181
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800182#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700183void sem_exit_ns(struct ipc_namespace *ns)
184{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800185 free_ipcs(ns, &sem_ids(ns), freeary);
Serge E. Hallyn7d6feeb2009-12-15 16:47:27 -0800186 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700187}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800188#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Manfred Spraul239521f2014-01-27 17:07:04 -0800190void __init sem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800192 sem_init_ns(&init_ipc_ns);
Mike Waychison19b49462005-09-06 15:17:10 -0700193 ipc_init_proc_interface("sysvipc/sem",
194 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700195 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Manfred Spraulf269f402013-07-08 16:01:24 -0700198/**
199 * unmerge_queues - unmerge queues, if possible.
200 * @sma: semaphore array
201 *
202 * The function unmerges the wait queues if complex_count is 0.
203 * It must be called prior to dropping the global semaphore array lock.
204 */
205static void unmerge_queues(struct sem_array *sma)
206{
207 struct sem_queue *q, *tq;
208
209 /* complex operations still around? */
210 if (sma->complex_count)
211 return;
212 /*
213 * We will switch back to simple mode.
214 * Move all pending operation back into the per-semaphore
215 * queues.
216 */
217 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
218 struct sem *curr;
219 curr = &sma->sem_base[q->sops[0].sem_num];
220
221 list_add_tail(&q->list, &curr->pending_alter);
222 }
223 INIT_LIST_HEAD(&sma->pending_alter);
224}
225
226/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800227 * merge_queues - merge single semop queues into global queue
Manfred Spraulf269f402013-07-08 16:01:24 -0700228 * @sma: semaphore array
229 *
230 * This function merges all per-semaphore queues into the global queue.
231 * It is necessary to achieve FIFO ordering for the pending single-sop
232 * operations when a multi-semop operation must sleep.
233 * Only the alter operations must be moved, the const operations can stay.
234 */
235static void merge_queues(struct sem_array *sma)
236{
237 int i;
238 for (i = 0; i < sma->sem_nsems; i++) {
239 struct sem *sem = sma->sem_base + i;
240
241 list_splice_init(&sem->pending_alter, &sma->pending_alter);
242 }
243}
244
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700245static void sem_rcu_free(struct rcu_head *head)
246{
247 struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
248 struct sem_array *sma = ipc_rcu_to_struct(p);
249
250 security_sem_free(sma);
251 ipc_rcu_free(head);
252}
253
Nadia Derbey3e148c72007-10-18 23:40:54 -0700254/*
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700255 * Wait until all currently ongoing simple ops have completed.
256 * Caller must own sem_perm.lock.
257 * New simple ops cannot start, because simple ops first check
258 * that sem_perm.lock is free.
Manfred Spraul6d07b682013-09-30 13:45:06 -0700259 * that a) sem_perm.lock is free and b) complex_count is 0.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700260 */
261static void sem_wait_array(struct sem_array *sma)
262{
263 int i;
264 struct sem *sem;
265
Manfred Spraul6d07b682013-09-30 13:45:06 -0700266 if (sma->complex_count) {
267 /* The thread that increased sma->complex_count waited on
268 * all sem->lock locks. Thus we don't need to wait again.
269 */
270 return;
271 }
272
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700273 for (i = 0; i < sma->sem_nsems; i++) {
274 sem = sma->sem_base + i;
275 spin_unlock_wait(&sem->lock);
276 }
277}
278
279/*
Rik van Riel6062a8d2013-04-30 19:15:44 -0700280 * If the request contains only one semaphore operation, and there are
281 * no complex transactions pending, lock only the semaphore involved.
282 * Otherwise, lock the entire semaphore array, since we either have
283 * multiple semaphores in our own semops, or we need to look at
284 * semaphores from other pending complex operations.
Rik van Riel6062a8d2013-04-30 19:15:44 -0700285 */
286static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
287 int nsops)
288{
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700289 struct sem *sem;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700290
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700291 if (nsops != 1) {
292 /* Complex operation - acquire a full lock */
293 ipc_lock_object(&sma->sem_perm);
294
295 /* And wait until all simple ops that are processed
296 * right now have dropped their locks.
297 */
298 sem_wait_array(sma);
299 return -1;
300 }
301
302 /*
303 * Only one semaphore affected - try to optimize locking.
304 * The rules are:
305 * - optimized locking is possible if no complex operation
306 * is either enqueued or processed right now.
307 * - The test for enqueued complex ops is simple:
308 * sma->complex_count != 0
309 * - Testing for complex ops that are processed right now is
310 * a bit more difficult. Complex ops acquire the full lock
311 * and first wait that the running simple ops have completed.
312 * (see above)
313 * Thus: If we own a simple lock and the global lock is free
314 * and complex_count is now 0, then it will stay 0 and
315 * thus just locking sem->lock is sufficient.
316 */
317 sem = sma->sem_base + sops->sem_num;
318
319 if (sma->complex_count == 0) {
320 /*
321 * It appears that no complex operation is around.
322 * Acquire the per-semaphore lock.
323 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700324 spin_lock(&sem->lock);
325
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700326 /* Then check that the global lock is free */
327 if (!spin_is_locked(&sma->sem_perm.lock)) {
328 /* spin_is_locked() is not a memory barrier */
329 smp_mb();
Rik van Riel6062a8d2013-04-30 19:15:44 -0700330
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700331 /* Now repeat the test of complex_count:
332 * It can't change anymore until we drop sem->lock.
333 * Thus: if is now 0, then it will stay 0.
334 */
335 if (sma->complex_count == 0) {
336 /* fast path successful! */
337 return sops->sem_num;
338 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700339 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700340 spin_unlock(&sem->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700341 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700342
343 /* slow path: acquire the full lock */
344 ipc_lock_object(&sma->sem_perm);
345
346 if (sma->complex_count == 0) {
347 /* False alarm:
348 * There is no complex operation, thus we can switch
349 * back to the fast path.
350 */
351 spin_lock(&sem->lock);
352 ipc_unlock_object(&sma->sem_perm);
353 return sops->sem_num;
354 } else {
355 /* Not a false alarm, thus complete the sequence for a
356 * full lock.
357 */
358 sem_wait_array(sma);
359 return -1;
360 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700361}
362
363static inline void sem_unlock(struct sem_array *sma, int locknum)
364{
365 if (locknum == -1) {
Manfred Spraulf269f402013-07-08 16:01:24 -0700366 unmerge_queues(sma);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700367 ipc_unlock_object(&sma->sem_perm);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700368 } else {
369 struct sem *sem = sma->sem_base + locknum;
370 spin_unlock(&sem->lock);
371 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700372}
373
374/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700375 * sem_lock_(check_) routines are called in the paths where the rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -0700376 * is not held.
Linus Torvalds321310c2013-05-04 10:47:57 -0700377 *
378 * The caller holds the RCU read lock.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700379 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700380static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns,
381 int id, struct sembuf *sops, int nsops, int *locknum)
Nadia Derbey023a5352007-10-18 23:40:51 -0700382{
Rik van Rielc460b662013-04-30 19:15:35 -0700383 struct kern_ipc_perm *ipcp;
384 struct sem_array *sma;
Nadia Derbey03f02c72007-10-18 23:40:51 -0700385
Rik van Rielc460b662013-04-30 19:15:35 -0700386 ipcp = ipc_obtain_object(&sem_ids(ns), id);
Linus Torvalds321310c2013-05-04 10:47:57 -0700387 if (IS_ERR(ipcp))
388 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800389
Rik van Riel6062a8d2013-04-30 19:15:44 -0700390 sma = container_of(ipcp, struct sem_array, sem_perm);
391 *locknum = sem_lock(sma, sops, nsops);
Rik van Rielc460b662013-04-30 19:15:35 -0700392
393 /* ipc_rmid() may have already freed the ID while sem_lock
394 * was spinning: verify that the structure is still valid
395 */
Rafael Aquini72a8ff22014-01-27 17:07:02 -0800396 if (ipc_valid_object(ipcp))
Rik van Rielc460b662013-04-30 19:15:35 -0700397 return container_of(ipcp, struct sem_array, sem_perm);
398
Rik van Riel6062a8d2013-04-30 19:15:44 -0700399 sem_unlock(sma, *locknum);
Linus Torvalds321310c2013-05-04 10:47:57 -0700400 return ERR_PTR(-EINVAL);
Nadia Derbey023a5352007-10-18 23:40:51 -0700401}
402
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700403static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
404{
405 struct kern_ipc_perm *ipcp = ipc_obtain_object(&sem_ids(ns), id);
406
407 if (IS_ERR(ipcp))
408 return ERR_CAST(ipcp);
409
410 return container_of(ipcp, struct sem_array, sem_perm);
411}
412
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700413static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
414 int id)
415{
416 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
417
418 if (IS_ERR(ipcp))
419 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800420
Nadia Derbey03f02c72007-10-18 23:40:51 -0700421 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700422}
423
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700424static inline void sem_lock_and_putref(struct sem_array *sma)
425{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700426 sem_lock(sma, NULL, -1);
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700427 ipc_rcu_putref(sma, ipc_rcu_free);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700428}
429
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700430static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
431{
432 ipc_rmid(&sem_ids(ns), &s->sem_perm);
433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*
436 * Lockless wakeup algorithm:
437 * Without the check/retry algorithm a lockless wakeup is possible:
438 * - queue.status is initialized to -EINTR before blocking.
439 * - wakeup is performed by
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700440 * * unlinking the queue entry from the pending list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * * setting queue.status to IN_WAKEUP
442 * This is the notification for the blocked thread that a
443 * result value is imminent.
444 * * call wake_up_process
445 * * set queue.status to the final value.
446 * - the previously blocked thread checks queue.status:
Manfred Spraul239521f2014-01-27 17:07:04 -0800447 * * if it's IN_WAKEUP, then it must wait until the value changes
448 * * if it's not -EINTR, then the operation was completed by
449 * update_queue. semtimedop can return queue.status without
450 * performing any operation on the sem array.
451 * * otherwise it must acquire the spinlock and check what's up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *
453 * The two-stage algorithm is necessary to protect against the following
454 * races:
455 * - if queue.status is set after wake_up_process, then the woken up idle
456 * thread could race forward and try (and fail) to acquire sma->lock
457 * before update_queue had a chance to set queue.status
458 * - if queue.status is written before wake_up_process and if the
459 * blocked process is woken up by a signal between writing
460 * queue.status and the wake_up_process, then the woken up
461 * process could return from semtimedop and die by calling
462 * sys_exit before wake_up_process is called. Then wake_up_process
463 * will oops, because the task structure is already invalid.
464 * (yes, this happened on s390 with sysv msg).
465 *
466 */
467#define IN_WAKEUP 1
468
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700469/**
470 * newary - Create a new semaphore set
471 * @ns: namespace
472 * @params: ptr to the structure that contains key, semflg and nsems
473 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700474 * Called with sem_ids.rwsem held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700475 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700476static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 int id;
479 int retval;
480 struct sem_array *sma;
481 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700482 key_t key = params->key;
483 int nsems = params->u.nsems;
484 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800485 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if (!nsems)
488 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700489 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -ENOSPC;
491
Manfred Spraul239521f2014-01-27 17:07:04 -0800492 size = sizeof(*sma) + nsems * sizeof(struct sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 sma = ipc_rcu_alloc(size);
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800494 if (!sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return -ENOMEM;
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800496
Manfred Spraul239521f2014-01-27 17:07:04 -0800497 memset(sma, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 sma->sem_perm.mode = (semflg & S_IRWXUGO);
500 sma->sem_perm.key = key;
501
502 sma->sem_perm.security = NULL;
503 retval = security_sem_alloc(sma);
504 if (retval) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700505 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return retval;
507 }
508
Kirill Korotaeve3893532006-10-02 02:18:22 -0700509 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700510 if (id < 0) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700511 ipc_rcu_putref(sma, sem_rcu_free);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700512 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700514 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 sma->sem_base = (struct sem *) &sma[1];
Manfred Spraulb97e8202009-12-15 16:47:32 -0800517
Rik van Riel6062a8d2013-04-30 19:15:44 -0700518 for (i = 0; i < nsems; i++) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700519 INIT_LIST_HEAD(&sma->sem_base[i].pending_alter);
520 INIT_LIST_HEAD(&sma->sem_base[i].pending_const);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700521 spin_lock_init(&sma->sem_base[i].lock);
522 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800523
524 sma->complex_count = 0;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700525 INIT_LIST_HEAD(&sma->pending_alter);
526 INIT_LIST_HEAD(&sma->pending_const);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700527 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 sma->sem_nsems = nsems;
529 sma->sem_ctime = get_seconds();
Rik van Riel6062a8d2013-04-30 19:15:44 -0700530 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -0700531 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700533 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700536
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700537/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700538 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700539 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700540static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700542 struct sem_array *sma;
543
544 sma = container_of(ipcp, struct sem_array, sem_perm);
545 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700546}
547
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700548/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700549 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700550 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700551static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
552 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700553{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700554 struct sem_array *sma;
555
556 sma = container_of(ipcp, struct sem_array, sem_perm);
557 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700558 return -EINVAL;
559
560 return 0;
561}
562
Heiko Carstensd5460c92009-01-14 14:14:27 +0100563SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700564{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700565 struct ipc_namespace *ns;
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700566 static const struct ipc_ops sem_ops = {
567 .getnew = newary,
568 .associate = sem_security,
569 .more_checks = sem_more_checks,
570 };
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700571 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Kirill Korotaeve3893532006-10-02 02:18:22 -0700573 ns = current->nsproxy->ipc_ns;
574
575 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700577
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700578 sem_params.key = key;
579 sem_params.flg = semflg;
580 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700581
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700582 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Petr Mladek78f50092014-01-27 17:07:00 -0800585/**
586 * perform_atomic_semop - Perform (if possible) a semaphore operation
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700587 * @sma: semaphore array
588 * @sops: array with operations that should be checked
Petr Mladek78f50092014-01-27 17:07:00 -0800589 * @nsops: number of operations
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700590 * @un: undo array
591 * @pid: pid that did the change
592 *
593 * Returns 0 if the operation was possible.
594 * Returns 1 if the operation is impossible, the caller must sleep.
595 * Negative values are error codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700597static int perform_atomic_semop(struct sem_array *sma, struct sembuf *sops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int nsops, struct sem_undo *un, int pid)
599{
600 int result, sem_op;
601 struct sembuf *sop;
Manfred Spraul239521f2014-01-27 17:07:04 -0800602 struct sem *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 for (sop = sops; sop < sops + nsops; sop++) {
605 curr = sma->sem_base + sop->sem_num;
606 sem_op = sop->sem_op;
607 result = curr->semval;
Petr Mladek78f50092014-01-27 17:07:00 -0800608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!sem_op && result)
610 goto would_block;
611
612 result += sem_op;
613 if (result < 0)
614 goto would_block;
615 if (result > SEMVMX)
616 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (sop->sem_flg & SEM_UNDO) {
619 int undo = un->semadj[sop->sem_num] - sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800620 /* Exceeding the undo range is an error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
622 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800623 un->semadj[sop->sem_num] = undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Petr Mladek78f50092014-01-27 17:07:00 -0800625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 curr->semval = result;
627 }
628
629 sop--;
630 while (sop >= sops) {
631 sma->sem_base[sop->sem_num].sempid = pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 sop--;
633 }
Petr Mladek78f50092014-01-27 17:07:00 -0800634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 0;
636
637out_of_range:
638 result = -ERANGE;
639 goto undo;
640
641would_block:
642 if (sop->sem_flg & IPC_NOWAIT)
643 result = -EAGAIN;
644 else
645 result = 1;
646
647undo:
648 sop--;
649 while (sop >= sops) {
Petr Mladek78f50092014-01-27 17:07:00 -0800650 sem_op = sop->sem_op;
651 sma->sem_base[sop->sem_num].semval -= sem_op;
652 if (sop->sem_flg & SEM_UNDO)
653 un->semadj[sop->sem_num] += sem_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 sop--;
655 }
656
657 return result;
658}
659
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700660/** wake_up_sem_queue_prepare(q, error): Prepare wake-up
661 * @q: queue entry that must be signaled
662 * @error: Error value for the signal
663 *
664 * Prepare the wake-up of the queue entry q.
Nick Piggind4212092009-12-15 16:47:30 -0800665 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700666static void wake_up_sem_queue_prepare(struct list_head *pt,
667 struct sem_queue *q, int error)
Nick Piggind4212092009-12-15 16:47:30 -0800668{
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700669 if (list_empty(pt)) {
670 /*
671 * Hold preempt off so that we don't get preempted and have the
672 * wakee busy-wait until we're scheduled back on.
673 */
674 preempt_disable();
675 }
Nick Piggind4212092009-12-15 16:47:30 -0800676 q->status = IN_WAKEUP;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700677 q->pid = error;
678
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700679 list_add_tail(&q->list, pt);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700680}
681
682/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800683 * wake_up_sem_queue_do - do the actual wake-up
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700684 * @pt: list of tasks to be woken up
685 *
686 * Do the actual wake-up.
687 * The function is called without any locks held, thus the semaphore array
688 * could be destroyed already and the tasks can disappear as soon as the
689 * status is set to the actual return code.
690 */
691static void wake_up_sem_queue_do(struct list_head *pt)
692{
693 struct sem_queue *q, *t;
694 int did_something;
695
696 did_something = !list_empty(pt);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700697 list_for_each_entry_safe(q, t, pt, list) {
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700698 wake_up_process(q->sleeper);
699 /* q can disappear immediately after writing q->status. */
700 smp_wmb();
701 q->status = q->pid;
702 }
703 if (did_something)
704 preempt_enable();
Nick Piggind4212092009-12-15 16:47:30 -0800705}
706
Manfred Spraulb97e8202009-12-15 16:47:32 -0800707static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
708{
709 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700710 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800711 sma->complex_count--;
712}
713
Manfred Spraulfd5db422010-05-26 14:43:40 -0700714/** check_restart(sma, q)
715 * @sma: semaphore array
716 * @q: the operation that just completed
717 *
718 * update_queue is O(N^2) when it restarts scanning the whole queue of
719 * waiting operations. Therefore this function checks if the restart is
720 * really necessary. It is called after a previously waiting operation
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700721 * modified the array.
722 * Note that wait-for-zero operations are handled without restart.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700723 */
724static int check_restart(struct sem_array *sma, struct sem_queue *q)
725{
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700726 /* pending complex alter operations are too difficult to analyse */
727 if (!list_empty(&sma->pending_alter))
Manfred Spraulfd5db422010-05-26 14:43:40 -0700728 return 1;
729
730 /* we were a sleeping complex operation. Too difficult */
731 if (q->nsops > 1)
732 return 1;
733
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700734 /* It is impossible that someone waits for the new value:
735 * - complex operations always restart.
736 * - wait-for-zero are handled seperately.
737 * - q is a previously sleeping simple operation that
738 * altered the array. It must be a decrement, because
739 * simple increments never sleep.
740 * - If there are older (higher priority) decrements
741 * in the queue, then they have observed the original
742 * semval value and couldn't proceed. The operation
743 * decremented to value - thus they won't proceed either.
744 */
745 return 0;
746}
Manfred Spraulfd5db422010-05-26 14:43:40 -0700747
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700748/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800749 * wake_const_ops - wake up non-alter tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700750 * @sma: semaphore array.
751 * @semnum: semaphore that was modified.
752 * @pt: list head for the tasks that must be woken up.
753 *
754 * wake_const_ops must be called after a semaphore in a semaphore array
755 * was set to 0. If complex const operations are pending, wake_const_ops must
756 * be called with semnum = -1, as well as with the number of each modified
757 * semaphore.
758 * The tasks that must be woken up are added to @pt. The return code
759 * is stored in q->pid.
760 * The function returns 1 if at least one operation was completed successfully.
761 */
762static int wake_const_ops(struct sem_array *sma, int semnum,
763 struct list_head *pt)
764{
765 struct sem_queue *q;
766 struct list_head *walk;
767 struct list_head *pending_list;
768 int semop_completed = 0;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700769
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700770 if (semnum == -1)
771 pending_list = &sma->pending_const;
772 else
773 pending_list = &sma->sem_base[semnum].pending_const;
774
775 walk = pending_list->next;
776 while (walk != pending_list) {
777 int error;
778
779 q = container_of(walk, struct sem_queue, list);
780 walk = walk->next;
781
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700782 error = perform_atomic_semop(sma, q->sops, q->nsops,
783 q->undo, q->pid);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700784
785 if (error <= 0) {
786 /* operation completed, remove from queue & wakeup */
787
788 unlink_queue(sma, q);
789
790 wake_up_sem_queue_prepare(pt, q, error);
791 if (error == 0)
792 semop_completed = 1;
793 }
794 }
795 return semop_completed;
796}
797
798/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800799 * do_smart_wakeup_zero - wakeup all wait for zero tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700800 * @sma: semaphore array
801 * @sops: operations that were performed
802 * @nsops: number of operations
803 * @pt: list head of the tasks that must be woken up.
804 *
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800805 * Checks all required queue for wait-for-zero operations, based
806 * on the actual changes that were performed on the semaphore array.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700807 * The function returns 1 if at least one operation was completed successfully.
808 */
809static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
810 int nsops, struct list_head *pt)
811{
812 int i;
813 int semop_completed = 0;
814 int got_zero = 0;
815
816 /* first: the per-semaphore queues, if known */
817 if (sops) {
818 for (i = 0; i < nsops; i++) {
819 int num = sops[i].sem_num;
820
821 if (sma->sem_base[num].semval == 0) {
822 got_zero = 1;
823 semop_completed |= wake_const_ops(sma, num, pt);
824 }
825 }
826 } else {
827 /*
828 * No sops means modified semaphores not known.
829 * Assume all were changed.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700830 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700831 for (i = 0; i < sma->sem_nsems; i++) {
832 if (sma->sem_base[i].semval == 0) {
833 got_zero = 1;
834 semop_completed |= wake_const_ops(sma, i, pt);
835 }
836 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700837 }
838 /*
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700839 * If one of the modified semaphores got 0,
840 * then check the global queue, too.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700841 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700842 if (got_zero)
843 semop_completed |= wake_const_ops(sma, -1, pt);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700844
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700845 return semop_completed;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700846}
847
Manfred Spraul636c6be2009-12-15 16:47:33 -0800848
849/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800850 * update_queue - look for tasks that can be completed.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800851 * @sma: semaphore array.
852 * @semnum: semaphore that was modified.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700853 * @pt: list head for the tasks that must be woken up.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800854 *
855 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700856 * was modified. If multiple semaphores were modified, update_queue must
857 * be called with semnum = -1, as well as with the number of each modified
858 * semaphore.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700859 * The tasks that must be woken up are added to @pt. The return code
860 * is stored in q->pid.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700861 * The function internally checks if const operations can now succeed.
862 *
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700863 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700865static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Manfred Spraul636c6be2009-12-15 16:47:33 -0800867 struct sem_queue *q;
868 struct list_head *walk;
869 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:
Manfred Spraul636c6be2009-12-15 16:47:33 -0800878 walk = pending_list->next;
879 while (walk != pending_list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700880 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800881
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700882 q = container_of(walk, struct sem_queue, list);
Manfred Spraul636c6be2009-12-15 16:47:33 -0800883 walk = walk->next;
Nick Piggin9cad2002009-12-15 16:47:29 -0800884
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800885 /* If we are scanning the single sop, per-semaphore list of
886 * one semaphore and that semaphore is 0, then it is not
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700887 * necessary to scan further: simple increments
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800888 * that affect only one entry succeed immediately and cannot
889 * be in the per semaphore pending queue, and decrements
890 * cannot be successful if the value is already 0.
891 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700892 if (semnum != -1 && sma->sem_base[semnum].semval == 0)
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800893 break;
894
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700895 error = perform_atomic_semop(sma, q->sops, q->nsops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 q->undo, q->pid);
897
898 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800899 if (error > 0)
900 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700901
Manfred Spraulb97e8202009-12-15 16:47:32 -0800902 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700903
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700904 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700905 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700906 } else {
907 semop_completed = 1;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700908 do_smart_wakeup_zero(sma, q->sops, q->nsops, pt);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700909 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700910 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700911
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700912 wake_up_sem_queue_prepare(pt, q, error);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700913 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800914 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700916 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700919/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800920 * set_semotime - set sem_otime
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700921 * @sma: semaphore array
922 * @sops: operations that modified the array, may be NULL
923 *
924 * sem_otime is replicated to avoid cache line trashing.
925 * This function sets one instance to the current time.
926 */
927static void set_semotime(struct sem_array *sma, struct sembuf *sops)
928{
929 if (sops == NULL) {
930 sma->sem_base[0].sem_otime = get_seconds();
931 } else {
932 sma->sem_base[sops[0].sem_num].sem_otime =
933 get_seconds();
934 }
935}
936
937/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800938 * do_smart_update - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700939 * @sma: semaphore array
940 * @sops: operations that were performed
941 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700942 * @otime: force setting otime
943 * @pt: list head of the tasks that must be woken up.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700944 *
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700945 * do_smart_update() does the required calls to update_queue and wakeup_zero,
946 * based on the actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700947 * Note that the function does not do the actual wake-up: the caller is
948 * responsible for calling wake_up_sem_queue_do(@pt).
949 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700950 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700951static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
952 int otime, struct list_head *pt)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700953{
954 int i;
955
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700956 otime |= do_smart_wakeup_zero(sma, sops, nsops, pt);
957
Manfred Spraulf269f402013-07-08 16:01:24 -0700958 if (!list_empty(&sma->pending_alter)) {
959 /* semaphore array uses the global queue - just process it. */
960 otime |= update_queue(sma, -1, pt);
961 } else {
962 if (!sops) {
963 /*
964 * No sops, thus the modified semaphores are not
965 * known. Check all.
966 */
967 for (i = 0; i < sma->sem_nsems; i++)
968 otime |= update_queue(sma, i, pt);
969 } else {
970 /*
971 * Check the semaphores that were increased:
972 * - No complex ops, thus all sleeping ops are
973 * decrease.
974 * - if we decreased the value, then any sleeping
975 * semaphore ops wont be able to run: If the
976 * previous value was too small, then the new
977 * value will be too small, too.
978 */
979 for (i = 0; i < nsops; i++) {
980 if (sops[i].sem_op > 0) {
981 otime |= update_queue(sma,
982 sops[i].sem_num, pt);
983 }
Manfred Spraulab465df2013-05-26 11:08:52 +0200984 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700985 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700986 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700987 if (otime)
988 set_semotime(sma, sops);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700989}
990
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -0700991/*
992 * check_qop: Test how often a queued operation sleeps on the semaphore semnum
993 */
994static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
995 bool count_zero)
996{
997 struct sembuf *sops = q->sops;
998 int nsops = q->nsops;
999 int i, semcnt;
1000
1001 semcnt = 0;
1002
1003 for (i = 0; i < nsops; i++) {
1004 if (sops[i].sem_num != semnum)
1005 continue;
1006 if (sops[i].sem_flg & IPC_NOWAIT)
1007 continue;
1008 if (count_zero && sops[i].sem_op == 0)
1009 semcnt++;
1010 if (!count_zero && sops[i].sem_op < 0)
1011 semcnt++;
1012 }
1013 return semcnt;
1014}
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
1019 * This model assumes that a task waits on exactly one semaphore.
1020 * Since semaphore operations are to be performed atomically, tasks actually
1021 * wait on a whole sequence of semaphores simultaneously.
1022 * The counts we return here are a rough approximation, but still
1023 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
1024 */
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001025static int count_semcnt(struct sem_array *sma, ushort semnum,
1026 bool count_zero)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001028 struct list_head *l;
Manfred Spraul239521f2014-01-27 17:07:04 -08001029 struct sem_queue *q;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001030 int semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001032 semcnt = 0;
1033 /* First: check the simple operations. They are easy to evaluate */
1034 if (count_zero)
1035 l = &sma->sem_base[semnum].pending_const;
1036 else
1037 l = &sma->sem_base[semnum].pending_alter;
1038
1039 list_for_each_entry(q, l, list) {
1040 /* all task on a per-semaphore list sleep on exactly
1041 * that semaphore
1042 */
1043 semcnt++;
Rik van Rielde2657f2013-05-09 16:59:59 -04001044 }
1045
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001046 /* Then: check the complex operations. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001047 list_for_each_entry(q, &sma->pending_alter, list) {
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001048 semcnt += check_qop(sma, semnum, q, count_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001050 if (count_zero) {
1051 list_for_each_entry(q, &sma->pending_const, list) {
1052 semcnt += check_qop(sma, semnum, q, count_zero);
1053 }
Rik van Rielebc2e5e2013-05-09 16:53:28 -04001054 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001055 return semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001058/* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1059 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -07001060 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001062static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Manfred Spraul380af1b2008-07-25 01:48:06 -07001064 struct sem_undo *un, *tu;
1065 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001066 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001067 struct list_head tasks;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001068 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Manfred Spraul380af1b2008-07-25 01:48:06 -07001070 /* Free the existing undo structures for this semaphore set. */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001071 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001072 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1073 list_del(&un->list_id);
1074 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001076 list_del_rcu(&un->list_proc);
1077 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001078 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001082 INIT_LIST_HEAD(&tasks);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001083 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1084 unlink_queue(sma, q);
1085 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1086 }
1087
1088 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -08001089 unlink_queue(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001090 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001092 for (i = 0; i < sma->sem_nsems; i++) {
1093 struct sem *sem = sma->sem_base + i;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001094 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1095 unlink_queue(sma, q);
1096 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1097 }
1098 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001099 unlink_queue(sma, q);
1100 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1101 }
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001104 /* Remove the semaphore set from the IDR */
1105 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001106 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001107 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001109 wake_up_sem_queue_do(&tasks);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001110 ns->used_sems -= sma->sem_nsems;
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001111 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
1114static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1115{
Manfred Spraul239521f2014-01-27 17:07:04 -08001116 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 case IPC_64:
1118 return copy_to_user(buf, in, sizeof(*in));
1119 case IPC_OLD:
1120 {
1121 struct semid_ds out;
1122
Dan Rosenberg982f7c22010-09-30 15:15:31 -07001123 memset(&out, 0, sizeof(out));
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1126
1127 out.sem_otime = in->sem_otime;
1128 out.sem_ctime = in->sem_ctime;
1129 out.sem_nsems = in->sem_nsems;
1130
1131 return copy_to_user(buf, &out, sizeof(out));
1132 }
1133 default:
1134 return -EINVAL;
1135 }
1136}
1137
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001138static time_t get_semotime(struct sem_array *sma)
1139{
1140 int i;
1141 time_t res;
1142
1143 res = sma->sem_base[0].sem_otime;
1144 for (i = 1; i < sma->sem_nsems; i++) {
1145 time_t to = sma->sem_base[i].sem_otime;
1146
1147 if (to > res)
1148 res = to;
1149 }
1150 return res;
1151}
1152
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001153static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001154 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Amerigo Wange5cc9c72009-12-15 16:47:35 -08001156 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 struct sem_array *sma;
1158
Manfred Spraul239521f2014-01-27 17:07:04 -08001159 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 case IPC_INFO:
1161 case SEM_INFO:
1162 {
1163 struct seminfo seminfo;
1164 int max_id;
1165
1166 err = security_sem_semctl(NULL, cmd);
1167 if (err)
1168 return err;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001169
Manfred Spraul239521f2014-01-27 17:07:04 -08001170 memset(&seminfo, 0, sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -07001171 seminfo.semmni = ns->sc_semmni;
1172 seminfo.semmns = ns->sc_semmns;
1173 seminfo.semmsl = ns->sc_semmsl;
1174 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 seminfo.semvmx = SEMVMX;
1176 seminfo.semmnu = SEMMNU;
1177 seminfo.semmap = SEMMAP;
1178 seminfo.semume = SEMUME;
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001179 down_read(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001181 seminfo.semusz = sem_ids(ns).in_use;
1182 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 } else {
1184 seminfo.semusz = SEMUSZ;
1185 seminfo.semaem = SEMAEM;
1186 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001187 max_id = ipc_get_maxid(&sem_ids(ns));
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001188 up_read(&sem_ids(ns).rwsem);
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001189 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return -EFAULT;
Manfred Spraul239521f2014-01-27 17:07:04 -08001191 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001193 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 case SEM_STAT:
1195 {
1196 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001197 int id = 0;
1198
1199 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Linus Torvalds941b0302013-05-04 11:04:29 -07001201 rcu_read_lock();
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001202 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001203 sma = sem_obtain_object(ns, semid);
1204 if (IS_ERR(sma)) {
1205 err = PTR_ERR(sma);
1206 goto out_unlock;
1207 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001208 id = sma->sem_perm.id;
1209 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001210 sma = sem_obtain_object_check(ns, semid);
1211 if (IS_ERR(sma)) {
1212 err = PTR_ERR(sma);
1213 goto out_unlock;
1214 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001218 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 goto out_unlock;
1220
1221 err = security_sem_semctl(sma, cmd);
1222 if (err)
1223 goto out_unlock;
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001226 tbuf.sem_otime = get_semotime(sma);
1227 tbuf.sem_ctime = sma->sem_ctime;
1228 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001229 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001230 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return -EFAULT;
1232 return id;
1233 }
1234 default:
1235 return -EINVAL;
1236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001238 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return err;
1240}
1241
Al Viroe1fd1f42013-03-05 15:04:55 -05001242static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1243 unsigned long arg)
1244{
1245 struct sem_undo *un;
1246 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001247 struct sem *curr;
Al Viroe1fd1f42013-03-05 15:04:55 -05001248 int err;
Al Viroe1fd1f42013-03-05 15:04:55 -05001249 struct list_head tasks;
1250 int val;
1251#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1252 /* big-endian 64bit */
1253 val = arg >> 32;
1254#else
1255 /* 32bit or little-endian 64bit */
1256 val = arg;
1257#endif
1258
Rik van Riel6062a8d2013-04-30 19:15:44 -07001259 if (val > SEMVMX || val < 0)
1260 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001261
1262 INIT_LIST_HEAD(&tasks);
Al Viroe1fd1f42013-03-05 15:04:55 -05001263
Rik van Riel6062a8d2013-04-30 19:15:44 -07001264 rcu_read_lock();
1265 sma = sem_obtain_object_check(ns, semid);
1266 if (IS_ERR(sma)) {
1267 rcu_read_unlock();
1268 return PTR_ERR(sma);
1269 }
1270
1271 if (semnum < 0 || semnum >= sma->sem_nsems) {
1272 rcu_read_unlock();
1273 return -EINVAL;
1274 }
1275
1276
1277 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1278 rcu_read_unlock();
1279 return -EACCES;
1280 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001281
1282 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001283 if (err) {
1284 rcu_read_unlock();
1285 return -EACCES;
1286 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001287
Rik van Riel6062a8d2013-04-30 19:15:44 -07001288 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001289
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001290 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001291 sem_unlock(sma, -1);
1292 rcu_read_unlock();
1293 return -EIDRM;
1294 }
1295
Al Viroe1fd1f42013-03-05 15:04:55 -05001296 curr = &sma->sem_base[semnum];
1297
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001298 ipc_assert_locked_object(&sma->sem_perm);
Al Viroe1fd1f42013-03-05 15:04:55 -05001299 list_for_each_entry(un, &sma->list_id, list_id)
1300 un->semadj[semnum] = 0;
1301
1302 curr->semval = val;
1303 curr->sempid = task_tgid_vnr(current);
1304 sma->sem_ctime = get_seconds();
1305 /* maybe some queued-up processes were waiting for this */
1306 do_smart_update(sma, NULL, 0, 0, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001307 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001308 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001309 wake_up_sem_queue_do(&tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001310 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001311}
1312
Kirill Korotaeve3893532006-10-02 02:18:22 -07001313static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001314 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001317 struct sem *curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001318 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 ushort fast_sem_io[SEMMSL_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001320 ushort *sem_io = fast_sem_io;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001321 struct list_head tasks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001323 INIT_LIST_HEAD(&tasks);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001324
1325 rcu_read_lock();
1326 sma = sem_obtain_object_check(ns, semid);
1327 if (IS_ERR(sma)) {
1328 rcu_read_unlock();
1329 return PTR_ERR(sma);
1330 }
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 nsems = sma->sem_nsems;
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 err = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001335 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1336 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 err = security_sem_semctl(sma, cmd);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001339 if (err)
1340 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 err = -EACCES;
1343 switch (cmd) {
1344 case GETALL:
1345 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001346 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int i;
1348
Al Viroce857222013-05-03 00:30:49 +01001349 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001350 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001351 err = -EIDRM;
1352 goto out_unlock;
1353 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001354 if (nsems > SEMMSL_FAST) {
Al Viroce857222013-05-03 00:30:49 +01001355 if (!ipc_rcu_getref(sma)) {
Al Viroce857222013-05-03 00:30:49 +01001356 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001357 goto out_unlock;
Al Viroce857222013-05-03 00:30:49 +01001358 }
1359 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001360 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 sem_io = ipc_alloc(sizeof(ushort)*nsems);
Manfred Spraul239521f2014-01-27 17:07:04 -08001362 if (sem_io == NULL) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001363 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -ENOMEM;
1365 }
1366
Linus Torvalds4091fd92013-05-04 10:13:40 -07001367 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001368 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001369 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001371 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Al Viroce857222013-05-03 00:30:49 +01001373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 for (i = 0; i < sma->sem_nsems; i++)
1375 sem_io[i] = sma->sem_base[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001376 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001377 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 err = 0;
Manfred Spraul239521f2014-01-27 17:07:04 -08001379 if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 err = -EFAULT;
1381 goto out_free;
1382 }
1383 case SETALL:
1384 {
1385 int i;
1386 struct sem_undo *un;
1387
Rik van Riel6062a8d2013-04-30 19:15:44 -07001388 if (!ipc_rcu_getref(sma)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001389 err = -EIDRM;
1390 goto out_rcu_wakeup;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001391 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001392 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Manfred Spraul239521f2014-01-27 17:07:04 -08001394 if (nsems > SEMMSL_FAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 sem_io = ipc_alloc(sizeof(ushort)*nsems);
Manfred Spraul239521f2014-01-27 17:07:04 -08001396 if (sem_io == NULL) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001397 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return -ENOMEM;
1399 }
1400 }
1401
Manfred Spraul239521f2014-01-27 17:07:04 -08001402 if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001403 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 err = -EFAULT;
1405 goto out_free;
1406 }
1407
1408 for (i = 0; i < nsems; i++) {
1409 if (sem_io[i] > SEMVMX) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001410 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 err = -ERANGE;
1412 goto out_free;
1413 }
1414 }
Linus Torvalds4091fd92013-05-04 10:13:40 -07001415 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001416 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001417 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001419 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421
1422 for (i = 0; i < nsems; i++)
1423 sma->sem_base[i].semval = sem_io[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001424
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001425 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001426 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 for (i = 0; i < nsems; i++)
1428 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 sma->sem_ctime = get_seconds();
1431 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001432 do_smart_update(sma, NULL, 0, 0, &tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 err = 0;
1434 goto out_unlock;
1435 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001436 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 err = -EINVAL;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001439 if (semnum < 0 || semnum >= nsems)
1440 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Rik van Riel6062a8d2013-04-30 19:15:44 -07001442 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001443 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001444 err = -EIDRM;
1445 goto out_unlock;
1446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 curr = &sma->sem_base[semnum];
1448
1449 switch (cmd) {
1450 case GETVAL:
1451 err = curr->semval;
1452 goto out_unlock;
1453 case GETPID:
1454 err = curr->sempid;
1455 goto out_unlock;
1456 case GETNCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001457 err = count_semcnt(sma, semnum, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 goto out_unlock;
1459 case GETZCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001460 err = count_semcnt(sma, semnum, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001465 sem_unlock(sma, -1);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001466out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001467 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001468 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08001470 if (sem_io != fast_sem_io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 ipc_free(sem_io, sizeof(ushort)*nsems);
1472 return err;
1473}
1474
Pierre Peiffer016d7132008-04-29 01:00:50 -07001475static inline unsigned long
1476copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Manfred Spraul239521f2014-01-27 17:07:04 -08001478 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001480 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 case IPC_OLD:
1484 {
1485 struct semid_ds tbuf_old;
1486
Manfred Spraul239521f2014-01-27 17:07:04 -08001487 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return -EFAULT;
1489
Pierre Peiffer016d7132008-04-29 01:00:50 -07001490 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1491 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1492 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 return 0;
1495 }
1496 default:
1497 return -EINVAL;
1498 }
1499}
1500
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001501/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001502 * This function handles some semctl commands which require the rwsem
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001503 * to be held in write mode.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001504 * NOTE: no locks must be held, the rwsem is taken inside this function.
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001505 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001506static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001507 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 struct sem_array *sma;
1510 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001511 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 struct kern_ipc_perm *ipcp;
1513
Manfred Spraul239521f2014-01-27 17:07:04 -08001514 if (cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001515 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001519 down_write(&sem_ids(ns).rwsem);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001520 rcu_read_lock();
1521
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001522 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1523 &semid64.sem_perm, 0);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001524 if (IS_ERR(ipcp)) {
1525 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001526 goto out_unlock1;
1527 }
Steve Grubb073115d2006-04-02 17:07:33 -04001528
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001529 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001532 if (err)
1533 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001535 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001537 sem_lock(sma, NULL, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001538 /* freeary unlocks the ipc object and rcu */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001539 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001540 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001542 sem_lock(sma, NULL, -1);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001543 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1544 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001545 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 break;
1548 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 err = -EINVAL;
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001550 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001553out_unlock0:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001554 sem_unlock(sma, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001555out_unlock1:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001556 rcu_read_unlock();
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001557out_up:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001558 up_write(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return err;
1560}
1561
Al Viroe1fd1f42013-03-05 15:04:55 -05001562SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001565 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001566 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 if (semid < 0)
1569 return -EINVAL;
1570
1571 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001572 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Manfred Spraul239521f2014-01-27 17:07:04 -08001574 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 case IPC_INFO:
1576 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001577 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001579 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 case GETALL:
1581 case GETVAL:
1582 case GETPID:
1583 case GETNCNT:
1584 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001586 return semctl_main(ns, semid, semnum, cmd, p);
1587 case SETVAL:
1588 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 case IPC_RMID:
1590 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001591 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 default:
1593 return -EINVAL;
1594 }
1595}
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597/* If the task doesn't already have a undo_list, then allocate one
1598 * here. We guarantee there is only one thread using this undo list,
1599 * and current is THE ONE
1600 *
1601 * If this allocation and assignment succeeds, but later
1602 * portions of this code fail, there is no need to free the sem_undo_list.
1603 * Just let it stay associated with the task, and it'll be freed later
1604 * at exit time.
1605 *
1606 * This can block, so callers must hold no locks.
1607 */
1608static inline int get_undo_list(struct sem_undo_list **undo_listp)
1609{
1610 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 undo_list = current->sysvsem.undo_list;
1613 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001614 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (undo_list == NULL)
1616 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001617 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001619 INIT_LIST_HEAD(&undo_list->list_proc);
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 current->sysvsem.undo_list = undo_list;
1622 }
1623 *undo_listp = undo_list;
1624 return 0;
1625}
1626
Nick Pigginbf17bb72009-12-15 16:47:28 -08001627static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001629 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Nick Pigginbf17bb72009-12-15 16:47:28 -08001631 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1632 if (un->semid == semid)
1633 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001635 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
Nick Pigginbf17bb72009-12-15 16:47:28 -08001638static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1639{
1640 struct sem_undo *un;
1641
Manfred Spraul239521f2014-01-27 17:07:04 -08001642 assert_spin_locked(&ulp->lock);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001643
1644 un = __lookup_undo(ulp, semid);
1645 if (un) {
1646 list_del_rcu(&un->list_proc);
1647 list_add_rcu(&un->list_proc, &ulp->list_proc);
1648 }
1649 return un;
1650}
1651
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001652/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001653 * find_alloc_undo - lookup (and if not present create) undo array
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001654 * @ns: namespace
1655 * @semid: semaphore array id
1656 *
1657 * The function looks up (and if not present creates) the undo structure.
1658 * The size of the undo structure depends on the size of the semaphore
1659 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001660 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1661 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001662 */
1663static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 struct sem_array *sma;
1666 struct sem_undo_list *ulp;
1667 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001668 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 error = get_undo_list(&ulp);
1671 if (error)
1672 return ERR_PTR(error);
1673
Manfred Spraul380af1b2008-07-25 01:48:06 -07001674 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001675 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001677 spin_unlock(&ulp->lock);
Manfred Spraul239521f2014-01-27 17:07:04 -08001678 if (likely(un != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 goto out;
1680
1681 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001682 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001683 sma = sem_obtain_object_check(ns, semid);
1684 if (IS_ERR(sma)) {
1685 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001686 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001687 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 nsems = sma->sem_nsems;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001690 if (!ipc_rcu_getref(sma)) {
1691 rcu_read_unlock();
1692 un = ERR_PTR(-EIDRM);
1693 goto out;
1694 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001695 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001697 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001698 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (!new) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001700 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return ERR_PTR(-ENOMEM);
1702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Manfred Spraul380af1b2008-07-25 01:48:06 -07001704 /* step 3: Acquire the lock on semaphore array */
Linus Torvalds4091fd92013-05-04 10:13:40 -07001705 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001706 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001707 if (!ipc_valid_object(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001708 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001709 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 kfree(new);
1711 un = ERR_PTR(-EIDRM);
1712 goto out;
1713 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001714 spin_lock(&ulp->lock);
1715
1716 /*
1717 * step 4: check for races: did someone else allocate the undo struct?
1718 */
1719 un = lookup_undo(ulp, semid);
1720 if (un) {
1721 kfree(new);
1722 goto success;
1723 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001724 /* step 5: initialize & link new undo structure */
1725 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001726 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001727 new->semid = semid;
1728 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001729 list_add_rcu(&new->list_proc, &ulp->list_proc);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001730 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001731 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001732 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001733
1734success:
1735 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001736 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737out:
1738 return un;
1739}
1740
Manfred Spraulc61284e2010-07-20 13:24:23 -07001741
1742/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001743 * get_queue_result - retrieve the result code from sem_queue
Manfred Spraulc61284e2010-07-20 13:24:23 -07001744 * @q: Pointer to queue structure
1745 *
1746 * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
1747 * q->status, then we must loop until the value is replaced with the final
1748 * value: This may happen if a task is woken up by an unrelated event (e.g.
1749 * signal) and in parallel the task is woken up by another task because it got
1750 * the requested semaphores.
1751 *
1752 * The function can be called with or without holding the semaphore spinlock.
1753 */
1754static int get_queue_result(struct sem_queue *q)
1755{
1756 int error;
1757
1758 error = q->status;
1759 while (unlikely(error == IN_WAKEUP)) {
1760 cpu_relax();
1761 error = q->status;
1762 }
1763
1764 return error;
1765}
1766
Heiko Carstensd5460c92009-01-14 14:14:27 +01001767SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1768 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 int error = -EINVAL;
1771 struct sem_array *sma;
1772 struct sembuf fast_sops[SEMOPM_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001773 struct sembuf *sops = fast_sops, *sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001775 int undos = 0, alter = 0, max, locknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 struct sem_queue queue;
1777 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001778 struct ipc_namespace *ns;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001779 struct list_head tasks;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001780
1781 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 if (nsops < 1 || semid < 0)
1784 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001785 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return -E2BIG;
Manfred Spraul239521f2014-01-27 17:07:04 -08001787 if (nsops > SEMOPM_FAST) {
1788 sops = kmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
1789 if (sops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return -ENOMEM;
1791 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001792 if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
1793 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 goto out_free;
1795 }
1796 if (timeout) {
1797 struct timespec _timeout;
1798 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1799 error = -EFAULT;
1800 goto out_free;
1801 }
1802 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1803 _timeout.tv_nsec >= 1000000000L) {
1804 error = -EINVAL;
1805 goto out_free;
1806 }
1807 jiffies_left = timespec_to_jiffies(&_timeout);
1808 }
1809 max = 0;
1810 for (sop = sops; sop < sops + nsops; sop++) {
1811 if (sop->sem_num >= max)
1812 max = sop->sem_num;
1813 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001814 undos = 1;
1815 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 alter = 1;
1817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Rik van Riel6062a8d2013-04-30 19:15:44 -07001819 INIT_LIST_HEAD(&tasks);
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001822 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001823 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (IS_ERR(un)) {
1825 error = PTR_ERR(un);
1826 goto out_free;
1827 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001828 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001830 rcu_read_lock();
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001833 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001834 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001835 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001836 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001838 }
1839
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001840 error = -EFBIG;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001841 if (max >= sma->sem_nsems)
1842 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001843
1844 error = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001845 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1846 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001847
1848 error = security_sem_semop(sma, sops, nsops, alter);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001849 if (error)
1850 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001851
Manfred Spraul6e224f92013-10-16 13:46:45 -07001852 error = -EIDRM;
1853 locknum = sem_lock(sma, sops, nsops);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001854 /*
1855 * We eventually might perform the following check in a lockless
1856 * fashion, considering ipc_valid_object() locking constraints.
1857 * If nsops == 1 and there is no contention for sem_perm.lock, then
1858 * only a per-semaphore lock is held and it's OK to proceed with the
1859 * check below. More details on the fine grained locking scheme
1860 * entangled here and why it's RMID race safe on comments at sem_lock()
1861 */
1862 if (!ipc_valid_object(&sma->sem_perm))
Manfred Spraul6e224f92013-10-16 13:46:45 -07001863 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001865 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001867 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001868 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001869 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001871 if (un && un->semid == -1)
1872 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001873
Manfred Spraul758a6ba32013-07-08 16:01:26 -07001874 error = perform_atomic_semop(sma, sops, nsops, un,
1875 task_tgid_vnr(current));
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001876 if (error == 0) {
1877 /* If the operation was successful, then do
1878 * the required updates.
1879 */
1880 if (alter)
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001881 do_smart_update(sma, sops, nsops, 1, &tasks);
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001882 else
1883 set_semotime(sma, sops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001885 if (error <= 0)
1886 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 /* We need to sleep on this operation, so we put the current
1889 * task into the pending queue and go to sleep.
1890 */
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 queue.sops = sops;
1893 queue.nsops = nsops;
1894 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001895 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 queue.alter = alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Manfred Spraulb97e8202009-12-15 16:47:32 -08001898 if (nsops == 1) {
1899 struct sem *curr;
1900 curr = &sma->sem_base[sops->sem_num];
1901
Manfred Spraulf269f402013-07-08 16:01:24 -07001902 if (alter) {
1903 if (sma->complex_count) {
1904 list_add_tail(&queue.list,
1905 &sma->pending_alter);
1906 } else {
1907
1908 list_add_tail(&queue.list,
1909 &curr->pending_alter);
1910 }
1911 } else {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001912 list_add_tail(&queue.list, &curr->pending_const);
Manfred Spraulf269f402013-07-08 16:01:24 -07001913 }
Manfred Spraulb97e8202009-12-15 16:47:32 -08001914 } else {
Manfred Spraulf269f402013-07-08 16:01:24 -07001915 if (!sma->complex_count)
1916 merge_queues(sma);
1917
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001918 if (alter)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001919 list_add_tail(&queue.list, &sma->pending_alter);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001920 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001921 list_add_tail(&queue.list, &sma->pending_const);
1922
Manfred Spraulb97e8202009-12-15 16:47:32 -08001923 sma->complex_count++;
1924 }
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 queue.status = -EINTR;
1927 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001928
1929sleep_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 current->state = TASK_INTERRUPTIBLE;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001931 sem_unlock(sma, locknum);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001932 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 if (timeout)
1935 jiffies_left = schedule_timeout(jiffies_left);
1936 else
1937 schedule();
1938
Manfred Spraulc61284e2010-07-20 13:24:23 -07001939 error = get_queue_result(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 if (error != -EINTR) {
1942 /* fast path: update_queue already obtained all requested
Manfred Spraulc61284e2010-07-20 13:24:23 -07001943 * resources.
1944 * Perform a smp_mb(): User space could assume that semop()
1945 * is a memory barrier: Without the mb(), the cpu could
1946 * speculatively read in user space stale data that was
1947 * overwritten by the previous owner of the semaphore.
1948 */
1949 smp_mb();
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 goto out_free;
1952 }
1953
Linus Torvalds321310c2013-05-04 10:47:57 -07001954 rcu_read_lock();
Rik van Riel6062a8d2013-04-30 19:15:44 -07001955 sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
Manfred Sprauld694ad62011-07-25 17:11:47 -07001956
1957 /*
1958 * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
1959 */
1960 error = get_queue_result(&queue);
1961
1962 /*
1963 * Array removed? If yes, leave without sem_unlock().
1964 */
Nadia Derbey023a5352007-10-18 23:40:51 -07001965 if (IS_ERR(sma)) {
Linus Torvalds321310c2013-05-04 10:47:57 -07001966 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 goto out_free;
1968 }
1969
Manfred Spraulc61284e2010-07-20 13:24:23 -07001970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /*
Manfred Sprauld694ad62011-07-25 17:11:47 -07001972 * If queue.status != -EINTR we are woken up by another process.
1973 * Leave without unlink_queue(), but with sem_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 */
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -08001975 if (error != -EINTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 /*
1979 * If an interrupt occurred we have to clean up the queue
1980 */
1981 if (timeout && jiffies_left == 0)
1982 error = -EAGAIN;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001983
1984 /*
1985 * If the wakeup was spurious, just retry
1986 */
1987 if (error == -EINTR && !signal_pending(current))
1988 goto sleep_again;
1989
Manfred Spraulb97e8202009-12-15 16:47:32 -08001990 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992out_unlock_free:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001993 sem_unlock(sma, locknum);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001994out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001995 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001996 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08001998 if (sops != fast_sops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 kfree(sops);
2000 return error;
2001}
2002
Heiko Carstensd5460c92009-01-14 14:14:27 +01002003SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
2004 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 return sys_semtimedop(semid, tsops, nsops, NULL);
2007}
2008
2009/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
2010 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 */
2012
2013int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
2014{
2015 struct sem_undo_list *undo_list;
2016 int error;
2017
2018 if (clone_flags & CLONE_SYSVSEM) {
2019 error = get_undo_list(&undo_list);
2020 if (error)
2021 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 atomic_inc(&undo_list->refcnt);
2023 tsk->sysvsem.undo_list = undo_list;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07002024 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 tsk->sysvsem.undo_list = NULL;
2026
2027 return 0;
2028}
2029
2030/*
2031 * add semadj values to semaphores, free undo structures.
2032 * undo structures are not freed when semaphore arrays are destroyed
2033 * so some of them may be out of date.
2034 * IMPLEMENTATION NOTE: There is some confusion over whether the
2035 * set of adjustments that needs to be done should be done in an atomic
2036 * manner or not. That is, if we are attempting to decrement the semval
2037 * should we queue up and wait until we can do so legally?
2038 * The original implementation attempted to do this (queue and wait).
2039 * The current implementation does not do so. The POSIX standard
2040 * and SVID should be consulted to determine what behavior is mandated.
2041 */
2042void exit_sem(struct task_struct *tsk)
2043{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002044 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002046 ulp = tsk->sysvsem.undo_list;
2047 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002049 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002051 if (!atomic_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return;
2053
Manfred Spraul380af1b2008-07-25 01:48:06 -07002054 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07002056 struct sem_undo *un;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07002057 struct list_head tasks;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002058 int semid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Manfred Spraul380af1b2008-07-25 01:48:06 -07002060 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02002061 un = list_entry_rcu(ulp->list_proc.next,
2062 struct sem_undo, list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002063 if (&un->list_proc == &ulp->list_proc)
2064 semid = -1;
2065 else
2066 semid = un->semid;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002067
Rik van Riel6062a8d2013-04-30 19:15:44 -07002068 if (semid == -1) {
2069 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002070 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002071 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07002072
Rik van Riel6062a8d2013-04-30 19:15:44 -07002073 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, un->semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002074 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002075 if (IS_ERR(sma)) {
2076 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002077 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Rik van Riel6062a8d2013-04-30 19:15:44 -07002080 sem_lock(sma, NULL, -1);
Manfred Spraul6e224f92013-10-16 13:46:45 -07002081 /* exit_sem raced with IPC_RMID, nothing to do */
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08002082 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07002083 sem_unlock(sma, -1);
2084 rcu_read_unlock();
2085 continue;
2086 }
Nick Pigginbf17bb72009-12-15 16:47:28 -08002087 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002088 if (un == NULL) {
2089 /* exit_sem raced with IPC_RMID+semget() that created
2090 * exactly the same semid. Nothing to do.
2091 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002092 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002093 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002094 continue;
2095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Manfred Spraul380af1b2008-07-25 01:48:06 -07002097 /* remove un from the linked lists */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07002098 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002099 list_del(&un->list_id);
2100
Manfred Spraul380af1b2008-07-25 01:48:06 -07002101 spin_lock(&ulp->lock);
2102 list_del_rcu(&un->list_proc);
2103 spin_unlock(&ulp->lock);
2104
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002105 /* perform adjustments registered in un */
2106 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul239521f2014-01-27 17:07:04 -08002107 struct sem *semaphore = &sma->sem_base[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002108 if (un->semadj[i]) {
2109 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /*
2111 * Range checks of the new semaphore value,
2112 * not defined by sus:
2113 * - Some unices ignore the undo entirely
2114 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2115 * - some cap the value (e.g. FreeBSD caps
2116 * at 0, but doesn't enforce SEMVMX)
2117 *
2118 * Linux caps the semaphore value, both at 0
2119 * and at SEMVMX.
2120 *
Manfred Spraul239521f2014-01-27 17:07:04 -08002121 * Manfred <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08002123 if (semaphore->semval < 0)
2124 semaphore->semval = 0;
2125 if (semaphore->semval > SEMVMX)
2126 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002127 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
2129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07002131 INIT_LIST_HEAD(&tasks);
2132 do_smart_update(sma, NULL, 0, 1, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002133 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002134 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07002135 wake_up_sem_queue_do(&tasks);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002136
Lai Jiangshan693a8b62011-03-18 12:09:35 +08002137 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002139 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
2142#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07002143static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08002145 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07002146 struct sem_array *sma = it;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002147 time_t sem_otime;
2148
Manfred Sprauld8c63372013-09-30 13:45:07 -07002149 /*
2150 * The proc interface isn't aware of sem_lock(), it calls
2151 * ipc_lock_object() directly (in sysvipc_find_ipc).
2152 * In order to stay compatible with sem_lock(), we must wait until
2153 * all simple semop() calls have left their critical regions.
2154 */
2155 sem_wait_array(sma);
2156
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002157 sem_otime = get_semotime(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Mike Waychison19b49462005-09-06 15:17:10 -07002159 return seq_printf(s,
Manfred Spraulb97e8202009-12-15 16:47:32 -08002160 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
Mike Waychison19b49462005-09-06 15:17:10 -07002161 sma->sem_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07002162 sma->sem_perm.id,
Mike Waychison19b49462005-09-06 15:17:10 -07002163 sma->sem_perm.mode,
2164 sma->sem_nsems,
Eric W. Biederman1efdb692012-02-07 16:54:11 -08002165 from_kuid_munged(user_ns, sma->sem_perm.uid),
2166 from_kgid_munged(user_ns, sma->sem_perm.gid),
2167 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2168 from_kgid_munged(user_ns, sma->sem_perm.cgid),
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002169 sem_otime,
Mike Waychison19b49462005-09-06 15:17:10 -07002170 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172#endif